Whamcloud - gitweb
LU-14893 lctl: check user for changelog_deregister
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13
14 OSC=${OSC:-"osc"}
15
16 CC=${CC:-cc}
17 CREATETEST=${CREATETEST:-createtest}
18 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
19 OPENFILE=${OPENFILE:-openfile}
20 OPENUNLINK=${OPENUNLINK:-openunlink}
21 READS=${READS:-"reads"}
22 MUNLINK=${MUNLINK:-munlink}
23 SOCKETSERVER=${SOCKETSERVER:-socketserver}
24 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
25 MEMHOG=${MEMHOG:-memhog}
26 DIRECTIO=${DIRECTIO:-directio}
27 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
28 DEF_STRIPE_COUNT=-1
29 CHECK_GRANT=${CHECK_GRANT:-"yes"}
30 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
31 export PARALLEL=${PARALLEL:-"no"}
32
33 TRACE=${TRACE:-""}
34 LUSTRE=${LUSTRE:-$(dirname $0)/..}
35 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
36 . $LUSTRE/tests/test-framework.sh
37 init_test_env $@
38
39 init_logging
40
41 ALWAYS_EXCEPT="$SANITY_EXCEPT "
42 # bug number for skipped test: LU-9693 LU-6493 LU-9693
43 ALWAYS_EXCEPT+="               42a     42b     42c "
44 # bug number:    LU-8411 LU-9054
45 ALWAYS_EXCEPT+=" 407     312"
46
47 if $SHARED_KEY; then
48         # bug number:    LU-14181 LU-14181
49         ALWAYS_EXCEPT+=" 64e      64f"
50 fi
51
52 selinux_status=$(getenforce)
53 if [ "$selinux_status" != "Disabled" ]; then
54         # bug number:
55         ALWAYS_EXCEPT+=""
56 fi
57
58 # skip the grant tests for ARM until they are fixed
59 if [[ $(uname -m) = aarch64 ]]; then
60         # bug number:    LU-11596
61         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
62         # bug number:    LU-11671 LU-11667
63         ALWAYS_EXCEPT+=" 45       317"
64         # bug number:    LU-14067 LU-14067
65         ALWAYS_EXCEPT+=" 400a     400b"
66 fi
67
68 # skip nfs tests on kernels >= 4.12.0 until they are fixed
69 if [ $LINUX_VERSION_CODE -ge $(version_code 4.12.0) ]; then
70         # bug number:   LU-12661
71         ALWAYS_EXCEPT+=" 817"
72 fi
73 # skip cgroup tests on RHEL8.1 kernels until they are fixed
74 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
75       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
76         # bug number:   LU-13063
77         ALWAYS_EXCEPT+=" 411"
78 fi
79
80 #                                  5              12     8   12  (min)"
81 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 60i 64b 68 71 115 135 136 300o"
82
83 if [ "$mds1_FSTYPE" = "zfs" ]; then
84         # bug number for skipped test:
85         ALWAYS_EXCEPT+="              "
86         #                                               13    (min)"
87         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
88 fi
89
90 if [ "$ost1_FSTYPE" = "zfs" ]; then
91         # bug number for skipped test:  LU-1941  LU-1941  LU-1941  LU-1941
92         ALWAYS_EXCEPT+="                130a 130b 130c 130d 130e 130f 130g"
93 fi
94
95 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
96
97 # Get the SLES distro version
98 #
99 # Returns a version string that should only be used in comparing
100 # strings returned by version_code()
101 sles_version_code()
102 {
103         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
104
105         # All SuSE Linux versions have one decimal. version_code expects two
106         local sles_version=$version.0
107         version_code $sles_version
108 }
109
110 # Check if we are running on Ubuntu or SLES so we can make decisions on
111 # what tests to run
112 if [ -r /etc/SuSE-release ]; then
113         sles_version=$(sles_version_code)
114         [ $sles_version -lt $(version_code 11.4.0) ] &&
115                 # bug number for skipped test: LU-4341
116                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
117         [ $sles_version -lt $(version_code 12.0.0) ] &&
118                 # bug number for skipped test: LU-3703
119                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
120 elif [ -r /etc/os-release ]; then
121         if grep -qi ubuntu /etc/os-release; then
122                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
123                                                 -e 's/^VERSION=//p' \
124                                                 /etc/os-release |
125                                                 awk '{ print $1 }'))
126
127                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
128                         # bug number for skipped test:
129                         #                LU-10334 LU-10366
130                         ALWAYS_EXCEPT+=" 103a     410"
131                 fi
132         fi
133 fi
134
135 build_test_filter
136 FAIL_ON_ERROR=false
137
138 cleanup() {
139         echo -n "cln.."
140         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
141         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
142 }
143 setup() {
144         echo -n "mnt.."
145         load_modules
146         setupall || exit 10
147         echo "done"
148 }
149
150 check_swap_layouts_support()
151 {
152         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
153                 skip "Does not support layout lock."
154 }
155
156 check_swap_layout_no_dom()
157 {
158         local FOLDER=$1
159         local SUPP=$(lfs getstripe $FOLDER | grep "pattern:       mdt" | wc -l)
160         [ $SUPP -eq 0 ] || skip "layout swap does not support DOM files so far"
161 }
162
163 check_and_setup_lustre
164 DIR=${DIR:-$MOUNT}
165 assert_DIR
166
167 MAXFREE=${MAXFREE:-$((300000 * $OSTCOUNT))}
168
169 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
170 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
171 rm -rf $DIR/[Rdfs][0-9]*
172
173 # $RUNAS_ID may get set incorrectly somewhere else
174 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
175         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
176
177 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
178
179 if [ "${ONLY}" = "MOUNT" ] ; then
180         echo "Lustre is up, please go on"
181         exit
182 fi
183
184 echo "preparing for tests involving mounts"
185 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
186 touch $EXT2_DEV
187 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
188 echo # add a newline after mke2fs.
189
190 umask 077
191
192 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
193 lctl set_param debug=-1 2> /dev/null || true
194 test_0a() {
195         touch $DIR/$tfile
196         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
197         rm $DIR/$tfile
198         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
199 }
200 run_test 0a "touch; rm ====================="
201
202 test_0b() {
203         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
204         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
205 }
206 run_test 0b "chmod 0755 $DIR ============================="
207
208 test_0c() {
209         $LCTL get_param mdc.*.import | grep "state: FULL" ||
210                 error "import not FULL"
211         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
212                 error "bad target"
213 }
214 run_test 0c "check import proc"
215
216 test_0d() { # LU-3397
217         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
218                 skip "proc exports not supported before 2.10.57"
219
220         local mgs_exp="mgs.MGS.exports"
221         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
222         local exp_client_nid
223         local exp_client_version
224         local exp_val
225         local imp_val
226         local temp_imp=$DIR/$tfile.import
227         local temp_exp=$DIR/$tfile.export
228
229         # save mgc import file to $temp_imp
230         $LCTL get_param mgc.*.import | tee $temp_imp
231         # Check if client uuid is found in MGS export
232         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
233                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
234                         $client_uuid ] &&
235                         break;
236         done
237         # save mgs export file to $temp_exp
238         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
239
240         # Compare the value of field "connect_flags"
241         imp_val=$(grep "connect_flags" $temp_imp)
242         exp_val=$(grep "connect_flags" $temp_exp)
243         [ "$exp_val" == "$imp_val" ] ||
244                 error "export flags '$exp_val' != import flags '$imp_val'"
245
246         # Compare client versions.  Only compare top-3 fields for compatibility
247         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
248         exp_val=$(version_code $(cut -d. -f1,2,3 <<<$exp_client_version))
249         imp_val=$(version_code $(lustre_build_version client | cut -d. -f1,2,3))
250         [ "$exp_val" == "$imp_val" ] ||
251                 error "exp version '$exp_client_version'($exp_val) != " \
252                         "'$(lustre_build_version client)'($imp_val)"
253 }
254 run_test 0d "check export proc ============================="
255
256 test_0e() { # LU-13417
257         (( $MDSCOUNT > 1 )) ||
258                 skip "We need at least 2 MDTs for this test"
259
260         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
261                 skip "Need server version at least 2.14.51"
262
263         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
264         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
265
266         [ $default_lmv_count -eq 1 ] ||
267                 error "$MOUNT default stripe count $default_lmv_count"
268
269         [ $default_lmv_index -eq -1 ] ||
270                 error "$MOUNT default stripe index $default_lmv_index"
271
272         mkdir $MOUNT/$tdir.1 || error "mkdir $MOUNT/$tdir.1 failed"
273         mkdir $MOUNT/$tdir.2 || error "mkdir $MOUNT/$tdir.2 failed"
274
275         local mdt_index1=$($LFS getdirstripe -i $MOUNT/$tdir.1)
276         local mdt_index2=$($LFS getdirstripe -i $MOUNT/$tdir.2)
277
278         [ $mdt_index1 -eq $mdt_index2 ] &&
279                 error "directories are on the same MDT $mdt_index1=$mdt_index2"
280
281         rmdir $MOUNT/$tdir.1 $MOUNT/$tdir.2
282 }
283 run_test 0e "Enable DNE MDT balancing for mkdir in the ROOT"
284
285 test_1() {
286         test_mkdir $DIR/$tdir
287         test_mkdir $DIR/$tdir/d2
288         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
289         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
290         rmdir $DIR/$tdir/d2
291         rmdir $DIR/$tdir
292         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
293 }
294 run_test 1 "mkdir; remkdir; rmdir"
295
296 test_2() {
297         test_mkdir $DIR/$tdir
298         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
299         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
300         rm -r $DIR/$tdir
301         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
302 }
303 run_test 2 "mkdir; touch; rmdir; check file"
304
305 test_3() {
306         test_mkdir $DIR/$tdir
307         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
308         touch $DIR/$tdir/$tfile
309         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
310         rm -r $DIR/$tdir
311         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
312 }
313 run_test 3 "mkdir; touch; rmdir; check dir"
314
315 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
316 test_4() {
317         test_mkdir -i 1 $DIR/$tdir
318
319         touch $DIR/$tdir/$tfile ||
320                 error "Create file under remote directory failed"
321
322         rmdir $DIR/$tdir &&
323                 error "Expect error removing in-use dir $DIR/$tdir"
324
325         test -d $DIR/$tdir || error "Remote directory disappeared"
326
327         rm -rf $DIR/$tdir || error "remove remote dir error"
328 }
329 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
330
331 test_5() {
332         test_mkdir $DIR/$tdir
333         test_mkdir $DIR/$tdir/d2
334         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
335         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
336         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
337 }
338 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
339
340 test_6a() {
341         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
342         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
343         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
344                 error "$tfile does not have perm 0666 or UID $UID"
345         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
346         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
347                 error "$tfile should be 0666 and owned by UID $UID"
348 }
349 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
350
351 test_6c() {
352         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
353
354         touch $DIR/$tfile
355         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
356         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
357                 error "$tfile should be owned by UID $RUNAS_ID"
358         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
359         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
360                 error "$tfile should be owned by UID $RUNAS_ID"
361 }
362 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
363
364 test_6e() {
365         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
366
367         touch $DIR/$tfile
368         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
369         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
370                 error "$tfile should be owned by GID $UID"
371         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
372         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
373                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
374 }
375 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
376
377 test_6g() {
378         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
379
380         test_mkdir $DIR/$tdir
381         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
382         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
383         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
384         test_mkdir $DIR/$tdir/d/subdir
385         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
386                 error "$tdir/d/subdir should be GID $RUNAS_GID"
387         if [[ $MDSCOUNT -gt 1 ]]; then
388                 # check remote dir sgid inherite
389                 $LFS mkdir -i 0 $DIR/$tdir.local ||
390                         error "mkdir $tdir.local failed"
391                 chmod g+s $DIR/$tdir.local ||
392                         error "chmod $tdir.local failed"
393                 chgrp $RUNAS_GID $DIR/$tdir.local ||
394                         error "chgrp $tdir.local failed"
395                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
396                         error "mkdir $tdir.remote failed"
397                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
398                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
399                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
400                         error "$tdir.remote should be mode 02755"
401         fi
402 }
403 run_test 6g "verify new dir in sgid dir inherits group"
404
405 test_6h() { # bug 7331
406         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
407
408         touch $DIR/$tfile || error "touch failed"
409         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
410         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
411                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
412         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
413                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
414 }
415 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
416
417 test_7a() {
418         test_mkdir $DIR/$tdir
419         $MCREATE $DIR/$tdir/$tfile
420         chmod 0666 $DIR/$tdir/$tfile
421         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
422                 error "$tdir/$tfile should be mode 0666"
423 }
424 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
425
426 test_7b() {
427         if [ ! -d $DIR/$tdir ]; then
428                 test_mkdir $DIR/$tdir
429         fi
430         $MCREATE $DIR/$tdir/$tfile
431         echo -n foo > $DIR/$tdir/$tfile
432         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
433         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
434 }
435 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
436
437 test_8() {
438         test_mkdir $DIR/$tdir
439         touch $DIR/$tdir/$tfile
440         chmod 0666 $DIR/$tdir/$tfile
441         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
442                 error "$tfile mode not 0666"
443 }
444 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
445
446 test_9() {
447         test_mkdir $DIR/$tdir
448         test_mkdir $DIR/$tdir/d2
449         test_mkdir $DIR/$tdir/d2/d3
450         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
451 }
452 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
453
454 test_10() {
455         test_mkdir $DIR/$tdir
456         test_mkdir $DIR/$tdir/d2
457         touch $DIR/$tdir/d2/$tfile
458         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
459                 error "$tdir/d2/$tfile not a file"
460 }
461 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
462
463 test_11() {
464         test_mkdir $DIR/$tdir
465         test_mkdir $DIR/$tdir/d2
466         chmod 0666 $DIR/$tdir/d2
467         chmod 0705 $DIR/$tdir/d2
468         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
469                 error "$tdir/d2 mode not 0705"
470 }
471 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
472
473 test_12() {
474         test_mkdir $DIR/$tdir
475         touch $DIR/$tdir/$tfile
476         chmod 0666 $DIR/$tdir/$tfile
477         chmod 0654 $DIR/$tdir/$tfile
478         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
479                 error "$tdir/d2 mode not 0654"
480 }
481 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
482
483 test_13() {
484         test_mkdir $DIR/$tdir
485         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
486         >  $DIR/$tdir/$tfile
487         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
488                 error "$tdir/$tfile size not 0 after truncate"
489 }
490 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
491
492 test_14() {
493         test_mkdir $DIR/$tdir
494         touch $DIR/$tdir/$tfile
495         rm $DIR/$tdir/$tfile
496         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
497 }
498 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
499
500 test_15() {
501         test_mkdir $DIR/$tdir
502         touch $DIR/$tdir/$tfile
503         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
504         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
505                 error "$tdir/${tfile_2} not a file after rename"
506         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
507 }
508 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
509
510 test_16() {
511         test_mkdir $DIR/$tdir
512         touch $DIR/$tdir/$tfile
513         rm -rf $DIR/$tdir/$tfile
514         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
515 }
516 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
517
518 test_17a() {
519         test_mkdir $DIR/$tdir
520         touch $DIR/$tdir/$tfile
521         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
522         ls -l $DIR/$tdir
523         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
524                 error "$tdir/l-exist not a symlink"
525         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
526                 error "$tdir/l-exist not referencing a file"
527         rm -f $DIR/$tdir/l-exist
528         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
529 }
530 run_test 17a "symlinks: create, remove (real)"
531
532 test_17b() {
533         test_mkdir $DIR/$tdir
534         ln -s no-such-file $DIR/$tdir/l-dangle
535         ls -l $DIR/$tdir
536         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
537                 error "$tdir/l-dangle not referencing no-such-file"
538         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
539                 error "$tdir/l-dangle not referencing non-existent file"
540         rm -f $DIR/$tdir/l-dangle
541         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
542 }
543 run_test 17b "symlinks: create, remove (dangling)"
544
545 test_17c() { # bug 3440 - don't save failed open RPC for replay
546         test_mkdir $DIR/$tdir
547         ln -s foo $DIR/$tdir/$tfile
548         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
549 }
550 run_test 17c "symlinks: open dangling (should return error)"
551
552 test_17d() {
553         test_mkdir $DIR/$tdir
554         ln -s foo $DIR/$tdir/$tfile
555         touch $DIR/$tdir/$tfile || error "creating to new symlink"
556 }
557 run_test 17d "symlinks: create dangling"
558
559 test_17e() {
560         test_mkdir $DIR/$tdir
561         local foo=$DIR/$tdir/$tfile
562         ln -s $foo $foo || error "create symlink failed"
563         ls -l $foo || error "ls -l failed"
564         ls $foo && error "ls not failed" || true
565 }
566 run_test 17e "symlinks: create recursive symlink (should return error)"
567
568 test_17f() {
569         test_mkdir $DIR/$tdir
570         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
571         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
572         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
573         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
574         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
575         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
576         ls -l  $DIR/$tdir
577 }
578 run_test 17f "symlinks: long and very long symlink name"
579
580 # str_repeat(S, N) generate a string that is string S repeated N times
581 str_repeat() {
582         local s=$1
583         local n=$2
584         local ret=''
585         while [ $((n -= 1)) -ge 0 ]; do
586                 ret=$ret$s
587         done
588         echo $ret
589 }
590
591 # Long symlinks and LU-2241
592 test_17g() {
593         test_mkdir $DIR/$tdir
594         local TESTS="59 60 61 4094 4095"
595
596         # Fix for inode size boundary in 2.1.4
597         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
598                 TESTS="4094 4095"
599
600         # Patch not applied to 2.2 or 2.3 branches
601         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
602         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
603                 TESTS="4094 4095"
604
605         for i in $TESTS; do
606                 local SYMNAME=$(str_repeat 'x' $i)
607                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
608                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
609         done
610 }
611 run_test 17g "symlinks: really long symlink name and inode boundaries"
612
613 test_17h() { #bug 17378
614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
615         remote_mds_nodsh && skip "remote MDS with nodsh"
616
617         local mdt_idx
618
619         test_mkdir $DIR/$tdir
620         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
621         $LFS setstripe -c -1 $DIR/$tdir
622         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
623         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
624         touch $DIR/$tdir/$tfile || true
625 }
626 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
627
628 test_17i() { #bug 20018
629         [ $PARALLEL == "yes" ] && skip "skip parallel run"
630         remote_mds_nodsh && skip "remote MDS with nodsh"
631
632         local foo=$DIR/$tdir/$tfile
633         local mdt_idx
634
635         test_mkdir -c1 $DIR/$tdir
636         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
637         ln -s $foo $foo || error "create symlink failed"
638 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
639         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
640         ls -l $foo && error "error not detected"
641         return 0
642 }
643 run_test 17i "don't panic on short symlink (should return error)"
644
645 test_17k() { #bug 22301
646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
647         [[ -z "$(which rsync 2>/dev/null)" ]] &&
648                 skip "no rsync command"
649         rsync --help | grep -q xattr ||
650                 skip_env "$(rsync --version | head -n1) does not support xattrs"
651         test_mkdir $DIR/$tdir
652         test_mkdir $DIR/$tdir.new
653         touch $DIR/$tdir/$tfile
654         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
655         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
656                 error "rsync failed with xattrs enabled"
657 }
658 run_test 17k "symlinks: rsync with xattrs enabled"
659
660 test_17l() { # LU-279
661         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
662                 skip "no getfattr command"
663
664         test_mkdir $DIR/$tdir
665         touch $DIR/$tdir/$tfile
666         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
667         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
668                 # -h to not follow symlinks. -m '' to list all the xattrs.
669                 # grep to remove first line: '# file: $path'.
670                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
671                 do
672                         lgetxattr_size_check $path $xattr ||
673                                 error "lgetxattr_size_check $path $xattr failed"
674                 done
675         done
676 }
677 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
678
679 # LU-1540
680 test_17m() {
681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
682         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
683         remote_mds_nodsh && skip "remote MDS with nodsh"
684         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
685         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
686                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
687
688         local short_sym="0123456789"
689         local wdir=$DIR/$tdir
690         local i
691
692         test_mkdir $wdir
693         long_sym=$short_sym
694         # create a long symlink file
695         for ((i = 0; i < 4; ++i)); do
696                 long_sym=${long_sym}${long_sym}
697         done
698
699         echo "create 512 short and long symlink files under $wdir"
700         for ((i = 0; i < 256; ++i)); do
701                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
702                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
703         done
704
705         echo "erase them"
706         rm -f $wdir/*
707         sync
708         wait_delete_completed
709
710         echo "recreate the 512 symlink files with a shorter string"
711         for ((i = 0; i < 512; ++i)); do
712                 # rewrite the symlink file with a shorter string
713                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
714                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
715         done
716
717         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
718         local devname=$(mdsdevname $mds_index)
719
720         echo "stop and checking mds${mds_index}:"
721         # e2fsck should not return error
722         stop mds${mds_index}
723         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
724         rc=$?
725
726         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
727                 error "start mds${mds_index} failed"
728         df $MOUNT > /dev/null 2>&1
729         [ $rc -eq 0 ] ||
730                 error "e2fsck detected error for short/long symlink: rc=$rc"
731         rm -f $wdir/*
732 }
733 run_test 17m "run e2fsck against MDT which contains short/long symlink"
734
735 check_fs_consistency_17n() {
736         local mdt_index
737         local rc=0
738
739         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
740         # so it only check MDT1/MDT2 instead of all of MDTs.
741         for mdt_index in 1 2; do
742                 local devname=$(mdsdevname $mdt_index)
743                 # e2fsck should not return error
744                 stop mds${mdt_index}
745                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
746                         rc=$((rc + $?))
747
748                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
749                         error "mount mds$mdt_index failed"
750                 df $MOUNT > /dev/null 2>&1
751         done
752         return $rc
753 }
754
755 test_17n() {
756         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
758         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
759         remote_mds_nodsh && skip "remote MDS with nodsh"
760         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
761         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
762                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
763
764         local i
765
766         test_mkdir $DIR/$tdir
767         for ((i=0; i<10; i++)); do
768                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
769                         error "create remote dir error $i"
770                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
771                         error "create files under remote dir failed $i"
772         done
773
774         check_fs_consistency_17n ||
775                 error "e2fsck report error after create files under remote dir"
776
777         for ((i = 0; i < 10; i++)); do
778                 rm -rf $DIR/$tdir/remote_dir_${i} ||
779                         error "destroy remote dir error $i"
780         done
781
782         check_fs_consistency_17n ||
783                 error "e2fsck report error after unlink files under remote dir"
784
785         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
786                 skip "lustre < 2.4.50 does not support migrate mv"
787
788         for ((i = 0; i < 10; i++)); do
789                 mkdir -p $DIR/$tdir/remote_dir_${i}
790                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
791                         error "create files under remote dir failed $i"
792                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
793                         error "migrate remote dir error $i"
794         done
795         check_fs_consistency_17n || error "e2fsck report error after migration"
796
797         for ((i = 0; i < 10; i++)); do
798                 rm -rf $DIR/$tdir/remote_dir_${i} ||
799                         error "destroy remote dir error $i"
800         done
801
802         check_fs_consistency_17n || error "e2fsck report error after unlink"
803 }
804 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
805
806 test_17o() {
807         remote_mds_nodsh && skip "remote MDS with nodsh"
808         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
809                 skip "Need MDS version at least 2.3.64"
810
811         local wdir=$DIR/${tdir}o
812         local mdt_index
813         local rc=0
814
815         test_mkdir $wdir
816         touch $wdir/$tfile
817         mdt_index=$($LFS getstripe -m $wdir/$tfile)
818         mdt_index=$((mdt_index + 1))
819
820         cancel_lru_locks mdc
821         #fail mds will wait the failover finish then set
822         #following fail_loc to avoid interfer the recovery process.
823         fail mds${mdt_index}
824
825         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
826         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
827         ls -l $wdir/$tfile && rc=1
828         do_facet mds${mdt_index} lctl set_param fail_loc=0
829         [[ $rc -eq 0 ]] || error "stat file should fail"
830 }
831 run_test 17o "stat file with incompat LMA feature"
832
833 test_18() {
834         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
835         ls $DIR || error "Failed to ls $DIR: $?"
836 }
837 run_test 18 "touch .../f ; ls ... =============================="
838
839 test_19a() {
840         touch $DIR/$tfile
841         ls -l $DIR
842         rm $DIR/$tfile
843         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
844 }
845 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
846
847 test_19b() {
848         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
849 }
850 run_test 19b "ls -l .../f19 (should return error) =============="
851
852 test_19c() {
853         [ $RUNAS_ID -eq $UID ] &&
854                 skip_env "RUNAS_ID = UID = $UID -- skipping"
855
856         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
857 }
858 run_test 19c "$RUNAS touch .../f19 (should return error) =="
859
860 test_19d() {
861         cat $DIR/f19 && error || true
862 }
863 run_test 19d "cat .../f19 (should return error) =============="
864
865 test_20() {
866         touch $DIR/$tfile
867         rm $DIR/$tfile
868         touch $DIR/$tfile
869         rm $DIR/$tfile
870         touch $DIR/$tfile
871         rm $DIR/$tfile
872         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
873 }
874 run_test 20 "touch .../f ; ls -l ..."
875
876 test_21() {
877         test_mkdir $DIR/$tdir
878         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
879         ln -s dangle $DIR/$tdir/link
880         echo foo >> $DIR/$tdir/link
881         cat $DIR/$tdir/dangle
882         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
883         $CHECKSTAT -f -t file $DIR/$tdir/link ||
884                 error "$tdir/link not linked to a file"
885 }
886 run_test 21 "write to dangling link"
887
888 test_22() {
889         local wdir=$DIR/$tdir
890         test_mkdir $wdir
891         chown $RUNAS_ID:$RUNAS_GID $wdir
892         (cd $wdir || error "cd $wdir failed";
893                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
894                 $RUNAS tar xf -)
895         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
896         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
897         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
898                 error "checkstat -u failed"
899 }
900 run_test 22 "unpack tar archive as non-root user"
901
902 # was test_23
903 test_23a() {
904         test_mkdir $DIR/$tdir
905         local file=$DIR/$tdir/$tfile
906
907         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
908         openfile -f O_CREAT:O_EXCL $file &&
909                 error "$file recreate succeeded" || true
910 }
911 run_test 23a "O_CREAT|O_EXCL in subdir"
912
913 test_23b() { # bug 18988
914         test_mkdir $DIR/$tdir
915         local file=$DIR/$tdir/$tfile
916
917         rm -f $file
918         echo foo > $file || error "write filed"
919         echo bar >> $file || error "append filed"
920         $CHECKSTAT -s 8 $file || error "wrong size"
921         rm $file
922 }
923 run_test 23b "O_APPEND check"
924
925 # LU-9409, size with O_APPEND and tiny writes
926 test_23c() {
927         local file=$DIR/$tfile
928
929         # single dd
930         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
931         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
932         rm -f $file
933
934         # racing tiny writes
935         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
936         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
937         wait
938         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
939         rm -f $file
940
941         #racing tiny & normal writes
942         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
943         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
944         wait
945         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
946         rm -f $file
947
948         #racing tiny & normal writes 2, ugly numbers
949         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
950         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
951         wait
952         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
953         rm -f $file
954 }
955 run_test 23c "O_APPEND size checks for tiny writes"
956
957 # LU-11069 file offset is correct after appending writes
958 test_23d() {
959         local file=$DIR/$tfile
960         local offset
961
962         echo CentaurHauls > $file
963         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
964         if ((offset != 26)); then
965                 error "wrong offset, expected 26, got '$offset'"
966         fi
967 }
968 run_test 23d "file offset is correct after appending writes"
969
970 # rename sanity
971 test_24a() {
972         echo '-- same directory rename'
973         test_mkdir $DIR/$tdir
974         touch $DIR/$tdir/$tfile.1
975         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
976         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
977 }
978 run_test 24a "rename file to non-existent target"
979
980 test_24b() {
981         test_mkdir $DIR/$tdir
982         touch $DIR/$tdir/$tfile.{1,2}
983         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
984         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
985         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
986 }
987 run_test 24b "rename file to existing target"
988
989 test_24c() {
990         test_mkdir $DIR/$tdir
991         test_mkdir $DIR/$tdir/d$testnum.1
992         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
993         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
994         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
995 }
996 run_test 24c "rename directory to non-existent target"
997
998 test_24d() {
999         test_mkdir -c1 $DIR/$tdir
1000         test_mkdir -c1 $DIR/$tdir/d$testnum.1
1001         test_mkdir -c1 $DIR/$tdir/d$testnum.2
1002         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
1003         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
1004         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
1005 }
1006 run_test 24d "rename directory to existing target"
1007
1008 test_24e() {
1009         echo '-- cross directory renames --'
1010         test_mkdir $DIR/R5a
1011         test_mkdir $DIR/R5b
1012         touch $DIR/R5a/f
1013         mv $DIR/R5a/f $DIR/R5b/g
1014         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
1015         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
1016 }
1017 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
1018
1019 test_24f() {
1020         test_mkdir $DIR/R6a
1021         test_mkdir $DIR/R6b
1022         touch $DIR/R6a/f $DIR/R6b/g
1023         mv $DIR/R6a/f $DIR/R6b/g
1024         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
1025         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
1026 }
1027 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
1028
1029 test_24g() {
1030         test_mkdir $DIR/R7a
1031         test_mkdir $DIR/R7b
1032         test_mkdir $DIR/R7a/d
1033         mv $DIR/R7a/d $DIR/R7b/e
1034         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
1035         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
1036 }
1037 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
1038
1039 test_24h() {
1040         test_mkdir -c1 $DIR/R8a
1041         test_mkdir -c1 $DIR/R8b
1042         test_mkdir -c1 $DIR/R8a/d
1043         test_mkdir -c1 $DIR/R8b/e
1044         mrename $DIR/R8a/d $DIR/R8b/e
1045         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1046         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1047 }
1048 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1049
1050 test_24i() {
1051         echo "-- rename error cases"
1052         test_mkdir $DIR/R9
1053         test_mkdir $DIR/R9/a
1054         touch $DIR/R9/f
1055         mrename $DIR/R9/f $DIR/R9/a
1056         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1057         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1058         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1059 }
1060 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1061
1062 test_24j() {
1063         test_mkdir $DIR/R10
1064         mrename $DIR/R10/f $DIR/R10/g
1065         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1066         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1067         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1068 }
1069 run_test 24j "source does not exist ============================"
1070
1071 test_24k() {
1072         test_mkdir $DIR/R11a
1073         test_mkdir $DIR/R11a/d
1074         touch $DIR/R11a/f
1075         mv $DIR/R11a/f $DIR/R11a/d
1076         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1077         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1078 }
1079 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1080
1081 # bug 2429 - rename foo foo foo creates invalid file
1082 test_24l() {
1083         f="$DIR/f24l"
1084         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1085 }
1086 run_test 24l "Renaming a file to itself ========================"
1087
1088 test_24m() {
1089         f="$DIR/f24m"
1090         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1091         # on ext3 this does not remove either the source or target files
1092         # though the "expected" operation would be to remove the source
1093         $CHECKSTAT -t file ${f} || error "${f} missing"
1094         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1095 }
1096 run_test 24m "Renaming a file to a hard link to itself ========="
1097
1098 test_24n() {
1099     f="$DIR/f24n"
1100     # this stats the old file after it was renamed, so it should fail
1101     touch ${f}
1102     $CHECKSTAT ${f} || error "${f} missing"
1103     mv ${f} ${f}.rename
1104     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1105     $CHECKSTAT -a ${f} || error "${f} exists"
1106 }
1107 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1108
1109 test_24o() {
1110         test_mkdir $DIR/$tdir
1111         rename_many -s random -v -n 10 $DIR/$tdir
1112 }
1113 run_test 24o "rename of files during htree split"
1114
1115 test_24p() {
1116         test_mkdir $DIR/R12a
1117         test_mkdir $DIR/R12b
1118         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1119         mrename $DIR/R12a $DIR/R12b
1120         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1121         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1122         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1123         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1124 }
1125 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1126
1127 cleanup_multiop_pause() {
1128         trap 0
1129         kill -USR1 $MULTIPID
1130 }
1131
1132 test_24q() {
1133         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1134
1135         test_mkdir $DIR/R13a
1136         test_mkdir $DIR/R13b
1137         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1138         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1139         MULTIPID=$!
1140
1141         trap cleanup_multiop_pause EXIT
1142         mrename $DIR/R13a $DIR/R13b
1143         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1144         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1145         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1146         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1147         cleanup_multiop_pause
1148         wait $MULTIPID || error "multiop close failed"
1149 }
1150 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1151
1152 test_24r() { #bug 3789
1153         test_mkdir $DIR/R14a
1154         test_mkdir $DIR/R14a/b
1155         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1156         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1157         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1158 }
1159 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1160
1161 test_24s() {
1162         test_mkdir $DIR/R15a
1163         test_mkdir $DIR/R15a/b
1164         test_mkdir $DIR/R15a/b/c
1165         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1166         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1167         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1168 }
1169 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1170 test_24t() {
1171         test_mkdir $DIR/R16a
1172         test_mkdir $DIR/R16a/b
1173         test_mkdir $DIR/R16a/b/c
1174         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1175         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1176         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1177 }
1178 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1179
1180 test_24u() { # bug12192
1181         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1182         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1183 }
1184 run_test 24u "create stripe file"
1185
1186 simple_cleanup_common() {
1187         local rc=0
1188         trap 0
1189         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1190
1191         local start=$SECONDS
1192         rm -rf $DIR/$tdir
1193         rc=$?
1194         wait_delete_completed
1195         echo "cleanup time $((SECONDS - start))"
1196         return $rc
1197 }
1198
1199 max_pages_per_rpc() {
1200         local mdtname="$(printf "MDT%04x" ${1:-0})"
1201         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1202 }
1203
1204 test_24v() {
1205         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1206
1207         local nrfiles=${COUNT:-100000}
1208         local fname="$DIR/$tdir/$tfile"
1209
1210         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1211         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1212
1213         test_mkdir "$(dirname $fname)"
1214         # assume MDT0000 has the fewest inodes
1215         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1216         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1217         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1218
1219         trap simple_cleanup_common EXIT
1220
1221         createmany -m "$fname" $nrfiles
1222
1223         cancel_lru_locks mdc
1224         lctl set_param mdc.*.stats clear
1225
1226         # was previously test_24D: LU-6101
1227         # readdir() returns correct number of entries after cursor reload
1228         local num_ls=$(ls $DIR/$tdir | wc -l)
1229         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1230         local num_all=$(ls -a $DIR/$tdir | wc -l)
1231         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1232                 [ $num_all -ne $((nrfiles + 2)) ]; then
1233                         error "Expected $nrfiles files, got $num_ls " \
1234                                 "($num_uniq unique $num_all .&..)"
1235         fi
1236         # LU-5 large readdir
1237         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1238         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1239         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1240         # take into account of overhead in lu_dirpage header and end mark in
1241         # each page, plus one in rpc_num calculation.
1242         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1243         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1244         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1245         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1246         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1247         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1248         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1249         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1250                 error "large readdir doesn't take effect: " \
1251                       "$mds_readpage should be about $rpc_max"
1252
1253         simple_cleanup_common
1254 }
1255 run_test 24v "list large directory (test hash collision, b=17560)"
1256
1257 test_24w() { # bug21506
1258         SZ1=234852
1259         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1260         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1261         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1262         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1263         [[ "$SZ1" -eq "$SZ2" ]] ||
1264                 error "Error reading at the end of the file $tfile"
1265 }
1266 run_test 24w "Reading a file larger than 4Gb"
1267
1268 test_24x() {
1269         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1270         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1271         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1272                 skip "Need MDS version at least 2.7.56"
1273
1274         local MDTIDX=1
1275         local remote_dir=$DIR/$tdir/remote_dir
1276
1277         test_mkdir $DIR/$tdir
1278         $LFS mkdir -i $MDTIDX $remote_dir ||
1279                 error "create remote directory failed"
1280
1281         test_mkdir $DIR/$tdir/src_dir
1282         touch $DIR/$tdir/src_file
1283         test_mkdir $remote_dir/tgt_dir
1284         touch $remote_dir/tgt_file
1285
1286         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1287                 error "rename dir cross MDT failed!"
1288
1289         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1290                 error "rename file cross MDT failed!"
1291
1292         touch $DIR/$tdir/ln_file
1293         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1294                 error "ln file cross MDT failed"
1295
1296         rm -rf $DIR/$tdir || error "Can not delete directories"
1297 }
1298 run_test 24x "cross MDT rename/link"
1299
1300 test_24y() {
1301         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1303
1304         local remote_dir=$DIR/$tdir/remote_dir
1305         local mdtidx=1
1306
1307         test_mkdir $DIR/$tdir
1308         $LFS mkdir -i $mdtidx $remote_dir ||
1309                 error "create remote directory failed"
1310
1311         test_mkdir $remote_dir/src_dir
1312         touch $remote_dir/src_file
1313         test_mkdir $remote_dir/tgt_dir
1314         touch $remote_dir/tgt_file
1315
1316         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1317                 error "rename subdir in the same remote dir failed!"
1318
1319         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1320                 error "rename files in the same remote dir failed!"
1321
1322         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1323                 error "link files in the same remote dir failed!"
1324
1325         rm -rf $DIR/$tdir || error "Can not delete directories"
1326 }
1327 run_test 24y "rename/link on the same dir should succeed"
1328
1329 test_24z() {
1330         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1331         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1332                 skip "Need MDS version at least 2.12.51"
1333
1334         local index
1335
1336         for index in 0 1; do
1337                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1338                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1339         done
1340
1341         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1342
1343         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1344         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1345
1346         local mdts=$(comma_list $(mdts_nodes))
1347
1348         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1349         stack_trap "do_nodes $mdts $LCTL \
1350                 set_param mdt.*.enable_remote_rename=1" EXIT
1351
1352         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1353
1354         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1355         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1356 }
1357 run_test 24z "cross-MDT rename is done as cp"
1358
1359 test_24A() { # LU-3182
1360         local NFILES=5000
1361
1362         rm -rf $DIR/$tdir
1363         test_mkdir $DIR/$tdir
1364         trap simple_cleanup_common EXIT
1365         createmany -m $DIR/$tdir/$tfile $NFILES
1366         local t=$(ls $DIR/$tdir | wc -l)
1367         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1368         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1369         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1370            [ $v -ne $((NFILES + 2)) ] ; then
1371                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1372         fi
1373
1374         simple_cleanup_common || error "Can not delete directories"
1375 }
1376 run_test 24A "readdir() returns correct number of entries."
1377
1378 test_24B() { # LU-4805
1379         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1380
1381         local count
1382
1383         test_mkdir $DIR/$tdir
1384         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1385                 error "create striped dir failed"
1386
1387         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1388         [ $count -eq 2 ] || error "Expected 2, got $count"
1389
1390         touch $DIR/$tdir/striped_dir/a
1391
1392         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1393         [ $count -eq 3 ] || error "Expected 3, got $count"
1394
1395         touch $DIR/$tdir/striped_dir/.f
1396
1397         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1398         [ $count -eq 4 ] || error "Expected 4, got $count"
1399
1400         rm -rf $DIR/$tdir || error "Can not delete directories"
1401 }
1402 run_test 24B "readdir for striped dir return correct number of entries"
1403
1404 test_24C() {
1405         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1406
1407         mkdir $DIR/$tdir
1408         mkdir $DIR/$tdir/d0
1409         mkdir $DIR/$tdir/d1
1410
1411         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1412                 error "create striped dir failed"
1413
1414         cd $DIR/$tdir/d0/striped_dir
1415
1416         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1417         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1418         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1419
1420         [ "$d0_ino" = "$parent_ino" ] ||
1421                 error ".. wrong, expect $d0_ino, get $parent_ino"
1422
1423         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1424                 error "mv striped dir failed"
1425
1426         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1427
1428         [ "$d1_ino" = "$parent_ino" ] ||
1429                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1430 }
1431 run_test 24C "check .. in striped dir"
1432
1433 test_24E() {
1434         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1436
1437         mkdir -p $DIR/$tdir
1438         mkdir $DIR/$tdir/src_dir
1439         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1440                 error "create remote source failed"
1441
1442         touch $DIR/$tdir/src_dir/src_child/a
1443
1444         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1445                 error "create remote target dir failed"
1446
1447         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1448                 error "create remote target child failed"
1449
1450         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1451                 error "rename dir cross MDT failed!"
1452
1453         find $DIR/$tdir
1454
1455         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1456                 error "src_child still exists after rename"
1457
1458         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1459                 error "missing file(a) after rename"
1460
1461         rm -rf $DIR/$tdir || error "Can not delete directories"
1462 }
1463 run_test 24E "cross MDT rename/link"
1464
1465 test_24F () {
1466         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1467
1468         local repeats=1000
1469         [ "$SLOW" = "no" ] && repeats=100
1470
1471         mkdir -p $DIR/$tdir
1472
1473         echo "$repeats repeats"
1474         for ((i = 0; i < repeats; i++)); do
1475                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1476                 touch $DIR/$tdir/test/a || error "touch fails"
1477                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1478                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1479         done
1480
1481         true
1482 }
1483 run_test 24F "hash order vs readdir (LU-11330)"
1484
1485 test_24G () {
1486         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1487
1488         local ino1
1489         local ino2
1490
1491         $LFS mkdir -i 0 $DIR/$tdir-0 || error "mkdir $tdir-0"
1492         $LFS mkdir -i 1 $DIR/$tdir-1 || error "mkdir $tdir-1"
1493         touch $DIR/$tdir-0/f1 || error "touch f1"
1494         ln -s $DIR/$tdir-0/f1 $DIR/$tdir-0/s1 || error "ln s1"
1495         ino1=$(stat -c%i $DIR/$tdir-0/s1)
1496         mv $DIR/$tdir-0/s1 $DIR/$tdir-1 || error "mv s1"
1497         ino2=$(stat -c%i $DIR/$tdir-1/s1)
1498         [ $ino1 -ne $ino2 ] || error "s1 should be migrated"
1499 }
1500 run_test 24G "migrate symlink in rename"
1501
1502 test_25a() {
1503         echo '== symlink sanity ============================================='
1504
1505         test_mkdir $DIR/d25
1506         ln -s d25 $DIR/s25
1507         touch $DIR/s25/foo ||
1508                 error "File creation in symlinked directory failed"
1509 }
1510 run_test 25a "create file in symlinked directory ==============="
1511
1512 test_25b() {
1513         [ ! -d $DIR/d25 ] && test_25a
1514         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1515 }
1516 run_test 25b "lookup file in symlinked directory ==============="
1517
1518 test_26a() {
1519         test_mkdir $DIR/d26
1520         test_mkdir $DIR/d26/d26-2
1521         ln -s d26/d26-2 $DIR/s26
1522         touch $DIR/s26/foo || error "File creation failed"
1523 }
1524 run_test 26a "multiple component symlink ======================="
1525
1526 test_26b() {
1527         test_mkdir -p $DIR/$tdir/d26-2
1528         ln -s $tdir/d26-2/foo $DIR/s26-2
1529         touch $DIR/s26-2 || error "File creation failed"
1530 }
1531 run_test 26b "multiple component symlink at end of lookup ======"
1532
1533 test_26c() {
1534         test_mkdir $DIR/d26.2
1535         touch $DIR/d26.2/foo
1536         ln -s d26.2 $DIR/s26.2-1
1537         ln -s s26.2-1 $DIR/s26.2-2
1538         ln -s s26.2-2 $DIR/s26.2-3
1539         chmod 0666 $DIR/s26.2-3/foo
1540 }
1541 run_test 26c "chain of symlinks"
1542
1543 # recursive symlinks (bug 439)
1544 test_26d() {
1545         ln -s d26-3/foo $DIR/d26-3
1546 }
1547 run_test 26d "create multiple component recursive symlink"
1548
1549 test_26e() {
1550         [ ! -h $DIR/d26-3 ] && test_26d
1551         rm $DIR/d26-3
1552 }
1553 run_test 26e "unlink multiple component recursive symlink"
1554
1555 # recursive symlinks (bug 7022)
1556 test_26f() {
1557         test_mkdir $DIR/$tdir
1558         test_mkdir $DIR/$tdir/$tfile
1559         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1560         test_mkdir -p lndir/bar1
1561         test_mkdir $DIR/$tdir/$tfile/$tfile
1562         cd $tfile                || error "cd $tfile failed"
1563         ln -s .. dotdot          || error "ln dotdot failed"
1564         ln -s dotdot/lndir lndir || error "ln lndir failed"
1565         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1566         output=`ls $tfile/$tfile/lndir/bar1`
1567         [ "$output" = bar1 ] && error "unexpected output"
1568         rm -r $tfile             || error "rm $tfile failed"
1569         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1570 }
1571 run_test 26f "rm -r of a directory which has recursive symlink"
1572
1573 test_27a() {
1574         test_mkdir $DIR/$tdir
1575         $LFS getstripe $DIR/$tdir
1576         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1577         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1578         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1579 }
1580 run_test 27a "one stripe file"
1581
1582 test_27b() {
1583         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1584
1585         test_mkdir $DIR/$tdir
1586         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1587         $LFS getstripe -c $DIR/$tdir/$tfile
1588         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1589                 error "two-stripe file doesn't have two stripes"
1590
1591         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1592 }
1593 run_test 27b "create and write to two stripe file"
1594
1595 # 27c family tests specific striping, setstripe -o
1596 test_27ca() {
1597         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1598         test_mkdir -p $DIR/$tdir
1599         local osts="1"
1600
1601         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1602         $LFS getstripe -i $DIR/$tdir/$tfile
1603         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1604                 error "stripe not on specified OST"
1605
1606         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1607 }
1608 run_test 27ca "one stripe on specified OST"
1609
1610 test_27cb() {
1611         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1612         test_mkdir -p $DIR/$tdir
1613         local osts="1,0"
1614         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1615         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1616         echo "$getstripe"
1617
1618         # Strip getstripe output to a space separated list of OSTs
1619         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1620                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1621         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1622                 error "stripes not on specified OSTs"
1623
1624         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1625 }
1626 run_test 27cb "two stripes on specified OSTs"
1627
1628 test_27cc() {
1629         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1630         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1631                 skip "server does not support overstriping"
1632
1633         test_mkdir -p $DIR/$tdir
1634         local osts="0,0"
1635         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1636         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1637         echo "$getstripe"
1638
1639         # Strip getstripe output to a space separated list of OSTs
1640         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1641                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1642         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1643                 error "stripes not on specified OSTs"
1644
1645         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1646 }
1647 run_test 27cc "two stripes on the same OST"
1648
1649 test_27cd() {
1650         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1651         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1652                 skip "server does not support overstriping"
1653         test_mkdir -p $DIR/$tdir
1654         local osts="0,1,1,0"
1655         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1656         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1657         echo "$getstripe"
1658
1659         # Strip getstripe output to a space separated list of OSTs
1660         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1661                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1662         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1663                 error "stripes not on specified OSTs"
1664
1665         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1666 }
1667 run_test 27cd "four stripes on two OSTs"
1668
1669 test_27ce() {
1670         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1671                 skip_env "too many osts, skipping"
1672         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1673                 skip "server does not support overstriping"
1674         # We do one more stripe than we have OSTs
1675         [ $OSTCOUNT -lt 159 ] || large_xattr_enabled ||
1676                 skip_env "ea_inode feature disabled"
1677
1678         test_mkdir -p $DIR/$tdir
1679         local osts=""
1680         for i in $(seq 0 $OSTCOUNT);
1681         do
1682                 osts=$osts"0"
1683                 if [ $i -ne $OSTCOUNT ]; then
1684                         osts=$osts","
1685                 fi
1686         done
1687         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1688         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1689         echo "$getstripe"
1690
1691         # Strip getstripe output to a space separated list of OSTs
1692         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1693                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1694         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1695                 error "stripes not on specified OSTs"
1696
1697         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1698 }
1699 run_test 27ce "more stripes than OSTs with -o"
1700
1701 test_27cf() {
1702         local osp_proc="osp.$FSNAME-OST0000-osc-MDT000*.active"
1703         local pid=0
1704
1705         test_mkdir -p $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
1706         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=0"
1707         stack_trap "do_facet $SINGLEMDS $LCTL set_param -n $osp_proc=1" EXIT
1708         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 1" ||
1709                 error "failed to set $osp_proc=0"
1710
1711         $LFS setstripe -o 0 $DIR/$tdir/$tfile &
1712         pid=$!
1713         sleep 1
1714         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=1"
1715         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 0" ||
1716                 error "failed to set $osp_proc=1"
1717         wait $pid
1718         [[ $pid -ne 0 ]] ||
1719                 error "should return error due to $osp_proc=0"
1720 }
1721 run_test 27cf "'setstripe -o' on inactive OSTs should return error"
1722
1723 test_27d() {
1724         test_mkdir $DIR/$tdir
1725         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1726                 error "setstripe failed"
1727         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1728         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1729 }
1730 run_test 27d "create file with default settings"
1731
1732 test_27e() {
1733         # LU-5839 adds check for existed layout before setting it
1734         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1735                 skip "Need MDS version at least 2.7.56"
1736
1737         test_mkdir $DIR/$tdir
1738         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1739         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1740         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1741 }
1742 run_test 27e "setstripe existing file (should return error)"
1743
1744 test_27f() {
1745         test_mkdir $DIR/$tdir
1746         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1747                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1748         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1749                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1750         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1751         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1752 }
1753 run_test 27f "setstripe with bad stripe size (should return error)"
1754
1755 test_27g() {
1756         test_mkdir $DIR/$tdir
1757         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1758         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1759                 error "$DIR/$tdir/$tfile has object"
1760 }
1761 run_test 27g "$LFS getstripe with no objects"
1762
1763 test_27ga() {
1764         test_mkdir $DIR/$tdir
1765         touch $DIR/$tdir/$tfile || error "touch failed"
1766         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1767         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1768         local rc=$?
1769         (( rc == 2 )) || error "getstripe did not return ENOENT"
1770 }
1771 run_test 27ga "$LFS getstripe with missing file (should return error)"
1772
1773 test_27i() {
1774         test_mkdir $DIR/$tdir
1775         touch $DIR/$tdir/$tfile || error "touch failed"
1776         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1777                 error "missing objects"
1778 }
1779 run_test 27i "$LFS getstripe with some objects"
1780
1781 test_27j() {
1782         test_mkdir $DIR/$tdir
1783         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1784                 error "setstripe failed" || true
1785 }
1786 run_test 27j "setstripe with bad stripe offset (should return error)"
1787
1788 test_27k() { # bug 2844
1789         test_mkdir $DIR/$tdir
1790         local file=$DIR/$tdir/$tfile
1791         local ll_max_blksize=$((4 * 1024 * 1024))
1792         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1793         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1794         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1795         dd if=/dev/zero of=$file bs=4k count=1
1796         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1797         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1798 }
1799 run_test 27k "limit i_blksize for broken user apps"
1800
1801 test_27l() {
1802         mcreate $DIR/$tfile || error "creating file"
1803         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1804                 error "setstripe should have failed" || true
1805 }
1806 run_test 27l "check setstripe permissions (should return error)"
1807
1808 test_27m() {
1809         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1810
1811         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1812                 skip_env "multiple clients -- skipping"
1813
1814         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1815                    head -n1)
1816         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1817                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1818         fi
1819         trap simple_cleanup_common EXIT
1820         test_mkdir $DIR/$tdir
1821         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1822         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1823                 error "dd should fill OST0"
1824         i=2
1825         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1826                 i=$((i + 1))
1827                 [ $i -gt 256 ] && break
1828         done
1829         i=$((i + 1))
1830         touch $DIR/$tdir/$tfile.$i
1831         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1832             awk '{print $1}'| grep -w "0") ] &&
1833                 error "OST0 was full but new created file still use it"
1834         i=$((i + 1))
1835         touch $DIR/$tdir/$tfile.$i
1836         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1837             awk '{print $1}'| grep -w "0") ] &&
1838                 error "OST0 was full but new created file still use it"
1839         simple_cleanup_common
1840 }
1841 run_test 27m "create file while OST0 was full"
1842
1843 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1844 # if the OST isn't full anymore.
1845 reset_enospc() {
1846         local ostidx=${1:-""}
1847         local delay
1848         local ready
1849         local get_prealloc
1850
1851         local list=$(comma_list $(osts_nodes))
1852         [ "$ostidx" ] && list=$(facet_host ost$((ostidx + 1)))
1853
1854         do_nodes $list lctl set_param fail_loc=0
1855         wait_delete_completed   # initiate all OST_DESTROYs from MDS to OST
1856         delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1857                 awk '{print $1 * 2;exit;}')
1858         get_prealloc="$LCTL get_param -n osc.*MDT*.prealloc_status |
1859                         grep -v \"^0$\""
1860         wait_update_facet $SINGLEMDS "$get_prealloc" "" $delay
1861 }
1862
1863 __exhaust_precreations() {
1864         local OSTIDX=$1
1865         local FAILLOC=$2
1866         local FAILIDX=${3:-$OSTIDX}
1867         local ofacet=ost$((OSTIDX + 1))
1868
1869         mkdir_on_mdt0 $DIR/$tdir
1870         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1871         local mfacet=mds$((mdtidx + 1))
1872         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1873
1874         local OST=$(ostname_from_index $OSTIDX)
1875
1876         # on the mdt's osc
1877         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1878         local last_id=$(do_facet $mfacet lctl get_param -n \
1879                         osp.$mdtosc_proc1.prealloc_last_id)
1880         local next_id=$(do_facet $mfacet lctl get_param -n \
1881                         osp.$mdtosc_proc1.prealloc_next_id)
1882
1883         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1884         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1885
1886         test_mkdir -p $DIR/$tdir/${OST}
1887         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1888 #define OBD_FAIL_OST_ENOSPC              0x215
1889         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1890         echo "Creating to objid $last_id on ost $OST..."
1891         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1892         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1893         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1894 }
1895
1896 exhaust_precreations() {
1897         __exhaust_precreations $1 $2 $3
1898         sleep_maxage
1899 }
1900
1901 exhaust_all_precreations() {
1902         local i
1903         for (( i=0; i < OSTCOUNT; i++ )) ; do
1904                 __exhaust_precreations $i $1 -1
1905         done
1906         sleep_maxage
1907 }
1908
1909 test_27n() {
1910         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1912         remote_mds_nodsh && skip "remote MDS with nodsh"
1913         remote_ost_nodsh && skip "remote OST with nodsh"
1914
1915         reset_enospc
1916         rm -f $DIR/$tdir/$tfile
1917         exhaust_precreations 0 0x80000215
1918         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1919         touch $DIR/$tdir/$tfile || error "touch failed"
1920         $LFS getstripe $DIR/$tdir/$tfile
1921         reset_enospc
1922 }
1923 run_test 27n "create file with some full OSTs"
1924
1925 test_27o() {
1926         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1927         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1928         remote_mds_nodsh && skip "remote MDS with nodsh"
1929         remote_ost_nodsh && skip "remote OST with nodsh"
1930
1931         reset_enospc
1932         rm -f $DIR/$tdir/$tfile
1933         exhaust_all_precreations 0x215
1934
1935         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1936
1937         reset_enospc
1938         rm -rf $DIR/$tdir/*
1939 }
1940 run_test 27o "create file with all full OSTs (should error)"
1941
1942 function create_and_checktime() {
1943         local fname=$1
1944         local loops=$2
1945         local i
1946
1947         for ((i=0; i < $loops; i++)); do
1948                 local start=$SECONDS
1949                 multiop $fname-$i Oc
1950                 ((SECONDS-start < TIMEOUT)) ||
1951                         error "creation took " $((SECONDS-$start)) && return 1
1952         done
1953 }
1954
1955 test_27oo() {
1956         local mdts=$(comma_list $(mdts_nodes))
1957
1958         [ $MDS1_VERSION -lt $(version_code 2.13.57) ] &&
1959                 skip "Need MDS version at least 2.13.57"
1960
1961         local f0=$DIR/${tfile}-0
1962         local f1=$DIR/${tfile}-1
1963
1964         wait_delete_completed
1965
1966         # refill precreated objects
1967         $LFS setstripe -i0 -c1 $f0
1968
1969         saved=$(do_facet mds1 $LCTL get_param -n lov.*0000*.qos_threshold_rr)
1970         # force QoS allocation policy
1971         do_nodes $mdts $LCTL set_param lov.*.qos_threshold_rr=0%
1972         stack_trap "do_nodes $mdts $LCTL set_param \
1973                 lov.*.qos_threshold_rr=$saved" EXIT
1974         sleep_maxage
1975
1976         # one OST is unavailable, but still have few objects preallocated
1977         stop ost1
1978         stack_trap "start ost1 $(ostdevname 1) $OST_MOUNT_OPTS; \
1979                 rm -rf $f1 $DIR/$tdir*" EXIT
1980
1981         for ((i=0; i < 7; i++)); do
1982                 mkdir $DIR/$tdir$i || error "can't create dir"
1983                 $LFS setstripe -c$((OSTCOUNT-1)) $DIR/$tdir$i ||
1984                         error "can't set striping"
1985         done
1986         for ((i=0; i < 7; i++)); do
1987                 create_and_checktime $DIR/$tdir$i/$tfile 100 &
1988         done
1989         wait
1990 }
1991 run_test 27oo "don't let few threads to reserve too many objects"
1992
1993 test_27p() {
1994         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1995         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1996         remote_mds_nodsh && skip "remote MDS with nodsh"
1997         remote_ost_nodsh && skip "remote OST with nodsh"
1998
1999         reset_enospc
2000         rm -f $DIR/$tdir/$tfile
2001         test_mkdir $DIR/$tdir
2002
2003         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
2004         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
2005         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
2006
2007         exhaust_precreations 0 0x80000215
2008         echo foo >> $DIR/$tdir/$tfile || error "append failed"
2009         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
2010         $LFS getstripe $DIR/$tdir/$tfile
2011
2012         reset_enospc
2013 }
2014 run_test 27p "append to a truncated file with some full OSTs"
2015
2016 test_27q() {
2017         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2018         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2019         remote_mds_nodsh && skip "remote MDS with nodsh"
2020         remote_ost_nodsh && skip "remote OST with nodsh"
2021
2022         reset_enospc
2023         rm -f $DIR/$tdir/$tfile
2024
2025         mkdir_on_mdt0 $DIR/$tdir
2026         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
2027         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
2028                 error "truncate $DIR/$tdir/$tfile failed"
2029         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
2030
2031         exhaust_all_precreations 0x215
2032
2033         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
2034         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
2035
2036         reset_enospc
2037 }
2038 run_test 27q "append to truncated file with all OSTs full (should error)"
2039
2040 test_27r() {
2041         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2043         remote_mds_nodsh && skip "remote MDS with nodsh"
2044         remote_ost_nodsh && skip "remote OST with nodsh"
2045
2046         reset_enospc
2047         rm -f $DIR/$tdir/$tfile
2048         exhaust_precreations 0 0x80000215
2049
2050         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
2051
2052         reset_enospc
2053 }
2054 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
2055
2056 test_27s() { # bug 10725
2057         test_mkdir $DIR/$tdir
2058         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
2059         local stripe_count=0
2060         [ $OSTCOUNT -eq 1 ] || stripe_count=2
2061         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
2062                 error "stripe width >= 2^32 succeeded" || true
2063
2064 }
2065 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
2066
2067 test_27t() { # bug 10864
2068         WDIR=$(pwd)
2069         WLFS=$(which lfs)
2070         cd $DIR
2071         touch $tfile
2072         $WLFS getstripe $tfile
2073         cd $WDIR
2074 }
2075 run_test 27t "check that utils parse path correctly"
2076
2077 test_27u() { # bug 4900
2078         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2079         remote_mds_nodsh && skip "remote MDS with nodsh"
2080
2081         local index
2082         local list=$(comma_list $(mdts_nodes))
2083
2084 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
2085         do_nodes $list $LCTL set_param fail_loc=0x139
2086         test_mkdir -p $DIR/$tdir
2087         trap simple_cleanup_common EXIT
2088         createmany -o $DIR/$tdir/t- 1000
2089         do_nodes $list $LCTL set_param fail_loc=0
2090
2091         TLOG=$TMP/$tfile.getstripe
2092         $LFS getstripe $DIR/$tdir > $TLOG
2093         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
2094         unlinkmany $DIR/$tdir/t- 1000
2095         trap 0
2096         [[ $OBJS -gt 0 ]] &&
2097                 error "$OBJS objects created on OST-0. See $TLOG" ||
2098                 rm -f $TLOG
2099 }
2100 run_test 27u "skip object creation on OSC w/o objects"
2101
2102 test_27v() { # bug 4900
2103         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2104         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2105         remote_mds_nodsh && skip "remote MDS with nodsh"
2106         remote_ost_nodsh && skip "remote OST with nodsh"
2107
2108         exhaust_all_precreations 0x215
2109         reset_enospc
2110
2111         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
2112
2113         touch $DIR/$tdir/$tfile
2114         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
2115         # all except ost1
2116         for (( i=1; i < OSTCOUNT; i++ )); do
2117                 do_facet ost$i lctl set_param fail_loc=0x705
2118         done
2119         local START=`date +%s`
2120         createmany -o $DIR/$tdir/$tfile 32
2121
2122         local FINISH=`date +%s`
2123         local TIMEOUT=`lctl get_param -n timeout`
2124         local PROCESS=$((FINISH - START))
2125         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
2126                error "$FINISH - $START >= $TIMEOUT / 2"
2127         sleep $((TIMEOUT / 2 - PROCESS))
2128         reset_enospc
2129 }
2130 run_test 27v "skip object creation on slow OST"
2131
2132 test_27w() { # bug 10997
2133         test_mkdir $DIR/$tdir
2134         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
2135         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
2136                 error "stripe size $size != 65536" || true
2137         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
2138                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
2139 }
2140 run_test 27w "check $LFS setstripe -S and getstrip -d options"
2141
2142 test_27wa() {
2143         [[ $OSTCOUNT -lt 2 ]] &&
2144                 skip_env "skipping multiple stripe count/offset test"
2145
2146         test_mkdir $DIR/$tdir
2147         for i in $(seq 1 $OSTCOUNT); do
2148                 offset=$((i - 1))
2149                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2150                         error "setstripe -c $i -i $offset failed"
2151                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2152                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2153                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2154                 [ $index -ne $offset ] &&
2155                         error "stripe offset $index != $offset" || true
2156         done
2157 }
2158 run_test 27wa "check $LFS setstripe -c -i options"
2159
2160 test_27x() {
2161         remote_ost_nodsh && skip "remote OST with nodsh"
2162         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2164
2165         OFFSET=$(($OSTCOUNT - 1))
2166         OSTIDX=0
2167         local OST=$(ostname_from_index $OSTIDX)
2168
2169         test_mkdir $DIR/$tdir
2170         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2171         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2172         sleep_maxage
2173         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2174         for i in $(seq 0 $OFFSET); do
2175                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2176                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2177                 error "OST0 was degraded but new created file still use it"
2178         done
2179         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2180 }
2181 run_test 27x "create files while OST0 is degraded"
2182
2183 test_27y() {
2184         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2185         remote_mds_nodsh && skip "remote MDS with nodsh"
2186         remote_ost_nodsh && skip "remote OST with nodsh"
2187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2188
2189         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2190         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2191                 osp.$mdtosc.prealloc_last_id)
2192         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2193                 osp.$mdtosc.prealloc_next_id)
2194         local fcount=$((last_id - next_id))
2195         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2196         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2197
2198         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2199                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2200         local OST_DEACTIVE_IDX=-1
2201         local OSC
2202         local OSTIDX
2203         local OST
2204
2205         for OSC in $MDS_OSCS; do
2206                 OST=$(osc_to_ost $OSC)
2207                 OSTIDX=$(index_from_ostuuid $OST)
2208                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2209                         OST_DEACTIVE_IDX=$OSTIDX
2210                 fi
2211                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2212                         echo $OSC "is Deactivated:"
2213                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2214                 fi
2215         done
2216
2217         OSTIDX=$(index_from_ostuuid $OST)
2218         test_mkdir $DIR/$tdir
2219         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
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 degraded:"
2226                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2227                                                 obdfilter.$OST.degraded=1
2228                 fi
2229         done
2230
2231         sleep_maxage
2232         createmany -o $DIR/$tdir/$tfile $fcount
2233
2234         for OSC in $MDS_OSCS; do
2235                 OST=$(osc_to_ost $OSC)
2236                 OSTIDX=$(index_from_ostuuid $OST)
2237                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2238                         echo $OST "is recovered from degraded:"
2239                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2240                                                 obdfilter.$OST.degraded=0
2241                 else
2242                         do_facet $SINGLEMDS lctl --device %$OSC activate
2243                 fi
2244         done
2245
2246         # all osp devices get activated, hence -1 stripe count restored
2247         local stripe_count=0
2248
2249         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2250         # devices get activated.
2251         sleep_maxage
2252         $LFS setstripe -c -1 $DIR/$tfile
2253         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2254         rm -f $DIR/$tfile
2255         [ $stripe_count -ne $OSTCOUNT ] &&
2256                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2257         return 0
2258 }
2259 run_test 27y "create files while OST0 is degraded and the rest inactive"
2260
2261 check_seq_oid()
2262 {
2263         log "check file $1"
2264
2265         lmm_count=$($LFS getstripe -c $1)
2266         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2267         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2268
2269         local old_ifs="$IFS"
2270         IFS=$'[:]'
2271         fid=($($LFS path2fid $1))
2272         IFS="$old_ifs"
2273
2274         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2275         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2276
2277         # compare lmm_seq and lu_fid->f_seq
2278         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2279         # compare lmm_object_id and lu_fid->oid
2280         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2281
2282         # check the trusted.fid attribute of the OST objects of the file
2283         local have_obdidx=false
2284         local stripe_nr=0
2285         $LFS getstripe $1 | while read obdidx oid hex seq; do
2286                 # skip lines up to and including "obdidx"
2287                 [ -z "$obdidx" ] && break
2288                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2289                 $have_obdidx || continue
2290
2291                 local ost=$((obdidx + 1))
2292                 local dev=$(ostdevname $ost)
2293                 local oid_hex
2294
2295                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2296
2297                 seq=$(echo $seq | sed -e "s/^0x//g")
2298                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2299                         oid_hex=$(echo $oid)
2300                 else
2301                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2302                 fi
2303                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2304
2305                 local ff=""
2306                 #
2307                 # Don't unmount/remount the OSTs if we don't need to do that.
2308                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2309                 # update too, until that use mount/ll_decode_filter_fid/mount.
2310                 # Re-enable when debugfs will understand new filter_fid.
2311                 #
2312                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2313                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2314                                 $dev 2>/dev/null" | grep "parent=")
2315                 fi
2316                 if [ -z "$ff" ]; then
2317                         stop ost$ost
2318                         mount_fstype ost$ost
2319                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2320                                 $(facet_mntpt ost$ost)/$obj_file)
2321                         unmount_fstype ost$ost
2322                         start ost$ost $dev $OST_MOUNT_OPTS
2323                         clients_up
2324                 fi
2325
2326                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2327
2328                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2329
2330                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2331                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2332                 #
2333                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2334                 #       stripe_size=1048576 component_id=1 component_start=0 \
2335                 #       component_end=33554432
2336                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2337                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2338                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2339                 local ff_pstripe
2340                 if grep -q 'stripe=' <<<$ff; then
2341                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2342                 else
2343                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2344                         # into f_ver in this case.  See comment on ff_parent.
2345                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2346                 fi
2347
2348                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2349                 [ $ff_pseq = $lmm_seq ] ||
2350                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2351                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2352                 [ $ff_poid = $lmm_oid ] ||
2353                         error "FF parent OID $ff_poid != $lmm_oid"
2354                 (($ff_pstripe == $stripe_nr)) ||
2355                         error "FF stripe $ff_pstripe != $stripe_nr"
2356
2357                 stripe_nr=$((stripe_nr + 1))
2358                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2359                         continue
2360                 if grep -q 'stripe_count=' <<<$ff; then
2361                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2362                                             -e 's/ .*//' <<<$ff)
2363                         [ $lmm_count = $ff_scnt ] ||
2364                                 error "FF stripe count $lmm_count != $ff_scnt"
2365                 fi
2366         done
2367 }
2368
2369 test_27z() {
2370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2371         remote_ost_nodsh && skip "remote OST with nodsh"
2372
2373         test_mkdir $DIR/$tdir
2374         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2375                 { error "setstripe -c -1 failed"; return 1; }
2376         # We need to send a write to every object to get parent FID info set.
2377         # This _should_ also work for setattr, but does not currently.
2378         # touch $DIR/$tdir/$tfile-1 ||
2379         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2380                 { error "dd $tfile-1 failed"; return 2; }
2381         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2382                 { error "setstripe -c -1 failed"; return 3; }
2383         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2384                 { error "dd $tfile-2 failed"; return 4; }
2385
2386         # make sure write RPCs have been sent to OSTs
2387         sync; sleep 5; sync
2388
2389         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2390         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2391 }
2392 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2393
2394 test_27A() { # b=19102
2395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2396
2397         save_layout_restore_at_exit $MOUNT
2398         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2399         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2400                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2401         local default_size=$($LFS getstripe -S $MOUNT)
2402         local default_offset=$($LFS getstripe -i $MOUNT)
2403         local dsize=$(do_facet $SINGLEMDS \
2404                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2405         [ $default_size -eq $dsize ] ||
2406                 error "stripe size $default_size != $dsize"
2407         [ $default_offset -eq -1 ] ||
2408                 error "stripe offset $default_offset != -1"
2409 }
2410 run_test 27A "check filesystem-wide default LOV EA values"
2411
2412 test_27B() { # LU-2523
2413         test_mkdir $DIR/$tdir
2414         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2415         touch $DIR/$tdir/f0
2416         # open f1 with O_LOV_DELAY_CREATE
2417         # rename f0 onto f1
2418         # call setstripe ioctl on open file descriptor for f1
2419         # close
2420         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2421                 $DIR/$tdir/f0
2422
2423         rm -f $DIR/$tdir/f1
2424         # open f1 with O_LOV_DELAY_CREATE
2425         # unlink f1
2426         # call setstripe ioctl on open file descriptor for f1
2427         # close
2428         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2429
2430         # Allow multiop to fail in imitation of NFS's busted semantics.
2431         true
2432 }
2433 run_test 27B "call setstripe on open unlinked file/rename victim"
2434
2435 # 27C family tests full striping and overstriping
2436 test_27Ca() { #LU-2871
2437         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2438
2439         declare -a ost_idx
2440         local index
2441         local found
2442         local i
2443         local j
2444
2445         test_mkdir $DIR/$tdir
2446         cd $DIR/$tdir
2447         for i in $(seq 0 $((OSTCOUNT - 1))); do
2448                 # set stripe across all OSTs starting from OST$i
2449                 $LFS setstripe -i $i -c -1 $tfile$i
2450                 # get striping information
2451                 ost_idx=($($LFS getstripe $tfile$i |
2452                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2453                 echo ${ost_idx[@]}
2454
2455                 # check the layout
2456                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2457                         error "${#ost_idx[@]} != $OSTCOUNT"
2458
2459                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2460                         found=0
2461                         for j in $(echo ${ost_idx[@]}); do
2462                                 if [ $index -eq $j ]; then
2463                                         found=1
2464                                         break
2465                                 fi
2466                         done
2467                         [ $found = 1 ] ||
2468                                 error "Can not find $index in ${ost_idx[@]}"
2469                 done
2470         done
2471 }
2472 run_test 27Ca "check full striping across all OSTs"
2473
2474 test_27Cb() {
2475         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2476                 skip "server does not support overstriping"
2477         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2478                 skip_env "too many osts, skipping"
2479
2480         test_mkdir -p $DIR/$tdir
2481         local setcount=$(($OSTCOUNT * 2))
2482         [ $setcount -lt 160 ] || large_xattr_enabled ||
2483                 skip_env "ea_inode feature disabled"
2484
2485         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2486                 error "setstripe failed"
2487
2488         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2489         [ $count -eq $setcount ] ||
2490                 error "stripe count $count, should be $setcount"
2491
2492         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2493                 error "overstriped should be set in pattern"
2494
2495         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2496                 error "dd failed"
2497 }
2498 run_test 27Cb "more stripes than OSTs with -C"
2499
2500 test_27Cc() {
2501         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2502                 skip "server does not support overstriping"
2503         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2504
2505         test_mkdir -p $DIR/$tdir
2506         local setcount=$(($OSTCOUNT - 1))
2507
2508         [ $setcount -lt 160 ] || large_xattr_enabled ||
2509                 skip_env "ea_inode feature disabled"
2510
2511         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2512                 error "setstripe failed"
2513
2514         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2515         [ $count -eq $setcount ] ||
2516                 error "stripe count $count, should be $setcount"
2517
2518         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2519                 error "overstriped should not be set in pattern"
2520
2521         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2522                 error "dd failed"
2523 }
2524 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2525
2526 test_27Cd() {
2527         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2528                 skip "server does not support overstriping"
2529         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2530         large_xattr_enabled || skip_env "ea_inode feature disabled"
2531
2532         test_mkdir -p $DIR/$tdir
2533         local setcount=$LOV_MAX_STRIPE_COUNT
2534
2535         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2536                 error "setstripe failed"
2537
2538         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2539         [ $count -eq $setcount ] ||
2540                 error "stripe count $count, should be $setcount"
2541
2542         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2543                 error "overstriped should be set in pattern"
2544
2545         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2546                 error "dd failed"
2547
2548         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2549 }
2550 run_test 27Cd "test maximum stripe count"
2551
2552 test_27Ce() {
2553         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2554                 skip "server does not support overstriping"
2555         test_mkdir -p $DIR/$tdir
2556
2557         pool_add $TESTNAME || error "Pool creation failed"
2558         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2559
2560         local setcount=8
2561
2562         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2563                 error "setstripe failed"
2564
2565         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2566         [ $count -eq $setcount ] ||
2567                 error "stripe count $count, should be $setcount"
2568
2569         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2570                 error "overstriped should be set in pattern"
2571
2572         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2573                 error "dd failed"
2574
2575         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2576 }
2577 run_test 27Ce "test pool with overstriping"
2578
2579 test_27Cf() {
2580         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2581                 skip "server does not support overstriping"
2582         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2583                 skip_env "too many osts, skipping"
2584
2585         test_mkdir -p $DIR/$tdir
2586
2587         local setcount=$(($OSTCOUNT * 2))
2588         [ $setcount -lt 160 ] || large_xattr_enabled ||
2589                 skip_env "ea_inode feature disabled"
2590
2591         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2592                 error "setstripe failed"
2593
2594         echo 1 > $DIR/$tdir/$tfile
2595
2596         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2597         [ $count -eq $setcount ] ||
2598                 error "stripe count $count, should be $setcount"
2599
2600         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2601                 error "overstriped should be set in pattern"
2602
2603         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2604                 error "dd failed"
2605
2606         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2607 }
2608 run_test 27Cf "test default inheritance with overstriping"
2609
2610 test_27D() {
2611         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2612         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2613         remote_mds_nodsh && skip "remote MDS with nodsh"
2614
2615         local POOL=${POOL:-testpool}
2616         local first_ost=0
2617         local last_ost=$(($OSTCOUNT - 1))
2618         local ost_step=1
2619         local ost_list=$(seq $first_ost $ost_step $last_ost)
2620         local ost_range="$first_ost $last_ost $ost_step"
2621
2622         test_mkdir $DIR/$tdir
2623         pool_add $POOL || error "pool_add failed"
2624         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2625
2626         local skip27D
2627         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2628                 skip27D+="-s 29"
2629         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2630                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2631                         skip27D+=" -s 30,31"
2632         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2633           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2634                 skip27D+=" -s 32,33"
2635         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2636                 skip27D+=" -s 34"
2637         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2638                 error "llapi_layout_test failed"
2639
2640         destroy_test_pools || error "destroy test pools failed"
2641 }
2642 run_test 27D "validate llapi_layout API"
2643
2644 # Verify that default_easize is increased from its initial value after
2645 # accessing a widely striped file.
2646 test_27E() {
2647         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2648         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2649                 skip "client does not have LU-3338 fix"
2650
2651         # 72 bytes is the minimum space required to store striping
2652         # information for a file striped across one OST:
2653         # (sizeof(struct lov_user_md_v3) +
2654         #  sizeof(struct lov_user_ost_data_v1))
2655         local min_easize=72
2656         $LCTL set_param -n llite.*.default_easize $min_easize ||
2657                 error "lctl set_param failed"
2658         local easize=$($LCTL get_param -n llite.*.default_easize)
2659
2660         [ $easize -eq $min_easize ] ||
2661                 error "failed to set default_easize"
2662
2663         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2664                 error "setstripe failed"
2665         # In order to ensure stat() call actually talks to MDS we need to
2666         # do something drastic to this file to shake off all lock, e.g.
2667         # rename it (kills lookup lock forcing cache cleaning)
2668         mv $DIR/$tfile $DIR/${tfile}-1
2669         ls -l $DIR/${tfile}-1
2670         rm $DIR/${tfile}-1
2671
2672         easize=$($LCTL get_param -n llite.*.default_easize)
2673
2674         [ $easize -gt $min_easize ] ||
2675                 error "default_easize not updated"
2676 }
2677 run_test 27E "check that default extended attribute size properly increases"
2678
2679 test_27F() { # LU-5346/LU-7975
2680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2681         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2682         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2683                 skip "Need MDS version at least 2.8.51"
2684         remote_ost_nodsh && skip "remote OST with nodsh"
2685
2686         test_mkdir $DIR/$tdir
2687         rm -f $DIR/$tdir/f0
2688         $LFS setstripe -c 2 $DIR/$tdir
2689
2690         # stop all OSTs to reproduce situation for LU-7975 ticket
2691         for num in $(seq $OSTCOUNT); do
2692                 stop ost$num
2693         done
2694
2695         # open/create f0 with O_LOV_DELAY_CREATE
2696         # truncate f0 to a non-0 size
2697         # close
2698         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2699
2700         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2701         # open/write it again to force delayed layout creation
2702         cat /etc/hosts > $DIR/$tdir/f0 &
2703         catpid=$!
2704
2705         # restart OSTs
2706         for num in $(seq $OSTCOUNT); do
2707                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2708                         error "ost$num failed to start"
2709         done
2710
2711         wait $catpid || error "cat failed"
2712
2713         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2714         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2715                 error "wrong stripecount"
2716
2717 }
2718 run_test 27F "Client resend delayed layout creation with non-zero size"
2719
2720 test_27G() { #LU-10629
2721         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2722                 skip "Need MDS version at least 2.11.51"
2723         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2724         remote_mds_nodsh && skip "remote MDS with nodsh"
2725         local POOL=${POOL:-testpool}
2726         local ostrange="0 0 1"
2727
2728         test_mkdir $DIR/$tdir
2729         touch $DIR/$tdir/$tfile.nopool
2730         pool_add $POOL || error "pool_add failed"
2731         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2732         $LFS setstripe -p $POOL $DIR/$tdir
2733
2734         local pool=$($LFS getstripe -p $DIR/$tdir)
2735
2736         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2737         touch $DIR/$tdir/$tfile.default
2738         $LFS setstripe -E 1M --pool $POOL -c 1 -E eof -c 1 $DIR/$tdir/$tfile.pfl
2739         $LFS find $DIR/$tdir -type f --pool $POOL
2740         local found=$($LFS find $DIR/$tdir -type f --pool $POOL | wc -l)
2741         [[ "$found" == "2" ]] ||
2742                 error "found $found != 2 files in '$DIR/$tdir' in '$POOL'"
2743
2744         $LFS setstripe -d $DIR/$tdir
2745
2746         pool=$($LFS getstripe -p -d $DIR/$tdir)
2747
2748         [[ "$pool" != "$POOL" ]] || error "$DIR/$tdir is still '$pool'"
2749 }
2750 run_test 27G "Clear OST pool from stripe"
2751
2752 test_27H() {
2753         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2754                 skip "Need MDS version newer than 2.11.54"
2755         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2756         test_mkdir $DIR/$tdir
2757         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2758         touch $DIR/$tdir/$tfile
2759         $LFS getstripe -c $DIR/$tdir/$tfile
2760         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2761                 error "two-stripe file doesn't have two stripes"
2762
2763         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2764         $LFS getstripe -y $DIR/$tdir/$tfile
2765         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2766              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2767                 error "expected l_ost_idx: [02]$ not matched"
2768
2769         # make sure ost list has been cleared
2770         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2771         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2772                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2773         touch $DIR/$tdir/f3
2774         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2775 }
2776 run_test 27H "Set specific OSTs stripe"
2777
2778 test_27I() {
2779         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2780         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2781         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2782                 skip "Need MDS version newer than 2.12.52"
2783         local pool=$TESTNAME
2784         local ostrange="1 1 1"
2785
2786         save_layout_restore_at_exit $MOUNT
2787         $LFS setstripe -c 2 -i 0 $MOUNT
2788         pool_add $pool || error "pool_add failed"
2789         pool_add_targets $pool $ostrange ||
2790                 error "pool_add_targets failed"
2791         test_mkdir $DIR/$tdir
2792         $LFS setstripe -p $pool $DIR/$tdir
2793         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2794         $LFS getstripe $DIR/$tdir/$tfile
2795 }
2796 run_test 27I "check that root dir striping does not break parent dir one"
2797
2798 test_27J() {
2799         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2800                 skip "Need MDS version newer than 2.12.51"
2801
2802         test_mkdir $DIR/$tdir
2803         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2804         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2805
2806         # create foreign file (raw way)
2807         ! $LFS setstripe --flags 0xda08 $DIR/$tdir/$tfile ||
2808                 error "creating $tfile w/ hex flags w/o --foreign should fail"
2809
2810         ! $LFS setstripe --foreign --flags foo \
2811                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tfile ||
2812                         error "creating $tfile with '--flags foo' should fail"
2813
2814         ! $LFS setstripe --foreign --flags 0xffffffff \
2815                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tfile ||
2816                         error "creating $tfile w/ 0xffffffff flags should fail"
2817
2818         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2819                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2820
2821         # verify foreign file (raw way)
2822         parse_foreign_file -f $DIR/$tdir/$tfile |
2823                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2824                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2825         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2826                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2827         parse_foreign_file -f $DIR/$tdir/$tfile |
2828                 grep "lov_foreign_size: 73" ||
2829                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2830         parse_foreign_file -f $DIR/$tdir/$tfile |
2831                 grep "lov_foreign_type: 1" ||
2832                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2833         parse_foreign_file -f $DIR/$tdir/$tfile |
2834                 grep "lov_foreign_flags: 0x0000DA08" ||
2835                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2836         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2837                 grep "lov_foreign_value: 0x" |
2838                 sed -e 's/lov_foreign_value: 0x//')
2839         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2840         [[ $lov = ${lov2// /} ]] ||
2841                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2842
2843         # create foreign file (lfs + API)
2844         $LFS setstripe --foreign=none --flags 0xda08 \
2845                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2846                 error "$DIR/$tdir/${tfile}2: create failed"
2847
2848         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2849                 grep "lfm_magic:.*0x0BD70BD0" ||
2850                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2851         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2852         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2853                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2854         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*none" ||
2855                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2856         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2857                 grep "lfm_flags:.*0x0000DA08" ||
2858                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2859         $LFS getstripe $DIR/$tdir/${tfile}2 |
2860                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2861                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2862
2863         # modify striping should fail
2864         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2865                 error "$DIR/$tdir/$tfile: setstripe should fail"
2866         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2867                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2868
2869         # R/W should fail
2870         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2871         cat $DIR/$tdir/${tfile}2 &&
2872                 error "$DIR/$tdir/${tfile}2: read should fail"
2873         cat /etc/passwd > $DIR/$tdir/$tfile &&
2874                 error "$DIR/$tdir/$tfile: write should fail"
2875         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2876                 error "$DIR/$tdir/${tfile}2: write should fail"
2877
2878         # chmod should work
2879         chmod 222 $DIR/$tdir/$tfile ||
2880                 error "$DIR/$tdir/$tfile: chmod failed"
2881         chmod 222 $DIR/$tdir/${tfile}2 ||
2882                 error "$DIR/$tdir/${tfile}2: chmod failed"
2883
2884         # chown should work
2885         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2886                 error "$DIR/$tdir/$tfile: chown failed"
2887         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2888                 error "$DIR/$tdir/${tfile}2: chown failed"
2889
2890         # rename should work
2891         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2892                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2893         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2894                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2895
2896         #remove foreign file
2897         rm $DIR/$tdir/${tfile}.new ||
2898                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2899         rm $DIR/$tdir/${tfile}2.new ||
2900                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2901 }
2902 run_test 27J "basic ops on file with foreign LOV"
2903
2904 test_27K() {
2905         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2906                 skip "Need MDS version newer than 2.12.49"
2907
2908         test_mkdir $DIR/$tdir
2909         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2910         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2911
2912         # create foreign dir (raw way)
2913         ! $LFS setdirstripe --flags 0xda08 $DIR/$tdir/$tdir ||
2914                 error "creating $tdir w/ hex flags w/o --foreign should fail"
2915
2916         ! $LFS setdirstripe --foreign --flags foo \
2917                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tdir ||
2918                         error "creating $tdir with '--flags foo' should fail"
2919
2920         ! $LFS setdirstripe --foreign --flags 0xffffffff \
2921                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tdir ||
2922                         error "creating $tdir w/ 0xffffffff flags should fail"
2923
2924         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2925                 error "create_foreign_dir FAILED"
2926
2927         # verify foreign dir (raw way)
2928         parse_foreign_dir -d $DIR/$tdir/$tdir |
2929                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2930                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2931         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2932                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2933         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2934                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2935         parse_foreign_dir -d $DIR/$tdir/$tdir |
2936                 grep "lmv_foreign_flags: 55813$" ||
2937                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2938         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2939                 grep "lmv_foreign_value: 0x" |
2940                 sed 's/lmv_foreign_value: 0x//')
2941         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2942                 sed 's/ //g')
2943         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2944
2945         # create foreign dir (lfs + API)
2946         $LFS mkdir --foreign=none --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2947                 $DIR/$tdir/${tdir}2 ||
2948                 error "$DIR/$tdir/${tdir}2: create failed"
2949
2950         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2951                 grep "lfm_magic:.*0x0CD50CD0" ||
2952                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2953         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2954         # - sizeof(lfm_type) - sizeof(lfm_flags)
2955         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2956                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2957         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*none" ||
2958                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2959         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2960                 grep "lfm_flags:.*0x0000DA05" ||
2961                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2962         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2963                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2964                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2965
2966         # file create in dir should fail
2967         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2968         touch $DIR/$tdir/${tdir}2/$tfile &&
2969                 "$DIR/${tdir}2: file create should fail"
2970
2971         # chmod should work
2972         chmod 777 $DIR/$tdir/$tdir ||
2973                 error "$DIR/$tdir: chmod failed"
2974         chmod 777 $DIR/$tdir/${tdir}2 ||
2975                 error "$DIR/${tdir}2: chmod failed"
2976
2977         # chown should work
2978         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2979                 error "$DIR/$tdir: chown failed"
2980         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2981                 error "$DIR/${tdir}2: chown failed"
2982
2983         # rename should work
2984         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2985                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2986         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2987                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2988
2989         #remove foreign dir
2990         rmdir $DIR/$tdir/${tdir}.new ||
2991                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2992         rmdir $DIR/$tdir/${tdir}2.new ||
2993                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2994 }
2995 run_test 27K "basic ops on dir with foreign LMV"
2996
2997 test_27L() {
2998         remote_mds_nodsh && skip "remote MDS with nodsh"
2999
3000         local POOL=${POOL:-$TESTNAME}
3001
3002         pool_add $POOL || error "pool_add failed"
3003
3004         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
3005                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
3006                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
3007 }
3008 run_test 27L "lfs pool_list gives correct pool name"
3009
3010 test_27M() {
3011         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
3012                 skip "Need MDS version >= than 2.12.57"
3013         remote_mds_nodsh && skip "remote MDS with nodsh"
3014         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
3015
3016         test_mkdir $DIR/$tdir
3017
3018         # Set default striping on directory
3019         local setcount=4
3020         local stripe_opt
3021
3022         # if we run against a 2.12 server which lacks overstring support
3023         # then the connect_flag will not report overstriping, even if client
3024         # is 2.14+
3025         if [[ $($LCTL get_param mdc.*.connect_flags) =~ overstriping ]]; then
3026                 stripe_opt="-C $setcount"
3027         elif (( $OSTCOUNT >= $setcount )); then
3028                 stripe_opt="-c $setcount"
3029         else
3030                 skip "server does not support overstriping"
3031         fi
3032         $LFS setstripe $stripe_opt $DIR/$tdir
3033
3034         echo 1 > $DIR/$tdir/${tfile}.1
3035         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
3036         [ $count -eq $setcount ] ||
3037                 error "(1) stripe count $count, should be $setcount"
3038
3039         # Capture existing append_stripe_count setting for restore
3040         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
3041         local mdts=$(comma_list $(mdts_nodes))
3042         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
3043
3044         local appendcount=$orig_count
3045         echo 1 >> $DIR/$tdir/${tfile}.2_append
3046         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
3047         [ $count -eq $appendcount ] ||
3048                 error "(2)stripe count $count, should be $appendcount for append"
3049
3050         # Disable O_APPEND striping, verify it works
3051         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
3052
3053         # Should now get the default striping, which is 4
3054         setcount=4
3055         echo 1 >> $DIR/$tdir/${tfile}.3_append
3056         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
3057         [ $count -eq $setcount ] ||
3058                 error "(3) stripe count $count, should be $setcount"
3059
3060         # Try changing the stripe count for append files
3061         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
3062
3063         # Append striping is now 2 (directory default is still 4)
3064         appendcount=2
3065         echo 1 >> $DIR/$tdir/${tfile}.4_append
3066         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
3067         [ $count -eq $appendcount ] ||
3068                 error "(4) stripe count $count, should be $appendcount for append"
3069
3070         # Test append stripe count of -1
3071         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
3072         appendcount=$OSTCOUNT
3073         echo 1 >> $DIR/$tdir/${tfile}.5
3074         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
3075         [ $count -eq $appendcount ] ||
3076                 error "(5) stripe count $count, should be $appendcount for append"
3077
3078         # Set append striping back to default of 1
3079         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
3080
3081         # Try a new default striping, PFL + DOM
3082         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
3083
3084         # Create normal DOM file, DOM returns stripe count == 0
3085         setcount=0
3086         touch $DIR/$tdir/${tfile}.6
3087         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
3088         [ $count -eq $setcount ] ||
3089                 error "(6) stripe count $count, should be $setcount"
3090
3091         # Show
3092         appendcount=1
3093         echo 1 >> $DIR/$tdir/${tfile}.7_append
3094         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
3095         [ $count -eq $appendcount ] ||
3096                 error "(7) stripe count $count, should be $appendcount for append"
3097
3098         # Clean up DOM layout
3099         $LFS setstripe -d $DIR/$tdir
3100
3101         save_layout_restore_at_exit $MOUNT
3102         # Now test that append striping works when layout is from root
3103         $LFS setstripe -c 2 $MOUNT
3104         # Make a special directory for this
3105         mkdir $DIR/${tdir}/${tdir}.2
3106
3107         # Verify for normal file
3108         setcount=2
3109         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
3110         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
3111         [ $count -eq $setcount ] ||
3112                 error "(8) stripe count $count, should be $setcount"
3113
3114         appendcount=1
3115         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
3116         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
3117         [ $count -eq $appendcount ] ||
3118                 error "(9) stripe count $count, should be $appendcount for append"
3119
3120         # Now test O_APPEND striping with pools
3121         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
3122         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
3123
3124         # Create the pool
3125         pool_add $TESTNAME || error "pool creation failed"
3126         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
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                 grep "lfm_magic:.*0x0CD50CD0" ||
3293                 error "$DIR/$tdir/${tdir}: invalid LMV EA magic"
3294         $LFS getdirstripe -v $DIR/$tdir/${tdir} | grep "lfm_type:.*symlink" ||
3295                 error "$DIR/$tdir/${tdir}: invalid LMV EA type"
3296         $LFS getdirstripe -v $DIR/$tdir/${tdir} |
3297                 grep "lfm_flags:.*0x0000DA05" ||
3298                 error "$DIR/$tdir/${tdir}: invalid LMV EA flags"
3299         $LFS getdirstripe $DIR/$tdir/${tdir} |
3300                 grep "lfm_value.*${uuid1}/${uuid2}" ||
3301                 error "$DIR/$tdir/${tdir}: invalid LMV EA value"
3302
3303         # file create in dir should fail
3304         # ("/{foreign_symlink_prefix}/${uuid1}/${uuid2}/" missing)
3305         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
3306
3307         # rename should succeed
3308         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
3309                 error "$DIR/$tdir/$tdir: rename of foreign_symlink dir has failed"
3310
3311         #remove foreign_symlink dir should fail
3312         rmdir $DIR/$tdir/${tdir}.new &&
3313                 error "$DIR/$tdir/${tdir}.new: remove of foreign_symlink dir should fail"
3314
3315         #test fake symlink
3316         mkdir -p /tmp/${uuid1}/${uuid2} ||
3317                 error "/tmp/${uuid1}/${uuid2}: mkdir has failed"
3318         echo FOOFOO > /tmp/${uuid1}/${uuid2}/foo ||
3319                 error "/tmp/${uuid1}/${uuid2}/foo: echo has failed"
3320         $LCTL set_param llite/$FSNAME-*/foreign_symlink_prefix=/tmp/
3321         $CHECKSTAT -t link -l /tmp/${uuid1}/${uuid2} $DIR/$tdir/${tdir}.new ||
3322                 error "$DIR/$tdir/${tdir}.new: not seen as a symlink"
3323         cat $DIR/$tdir/${tdir}.new/foo | grep FOOFOO ||
3324                 error "$DIR/$tdir/${tdir}.new: symlink resolution has failed"
3325
3326         #check that getstripe fails now that foreign_symlink enabled
3327         $LFS getdirstripe $DIR/$tdir/${tdir}.new ||
3328                 error "$DIR/$tdir/${tdir}.new: getdirstripe should still work with foreign_symlink enabled"
3329
3330         # file create in dir should work now
3331         cp /etc/passwd $DIR/$tdir/${tdir}.new/$tfile ||
3332                 error "$DIR/$tdir/${tdir}.new/$tfile: file create should fail"
3333         diff /etc/passwd $DIR/$tdir/${tdir}.new/$tfile ||
3334                 error "$DIR/$tdir/${tdir}.new/$tfile: diff has failed"
3335         diff /etc/passwd /tmp/${uuid1}/${uuid2}/$tfile ||
3336                 error "/tmp/${uuid1}/${uuid2}/$tfile: diff has failed"
3337
3338         # chmod should still succeed
3339         chmod 755 $DIR/$tdir/${tdir}.new ||
3340                 error "$DIR/$tdir/${tdir}.new: chmod has failed"
3341
3342         # chown should still succeed
3343         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}.new ||
3344                 error "$DIR/$tdir/${tdir}.new: chown has failed"
3345
3346         # rename should still succeed
3347         mv $DIR/$tdir/${tdir}.new $DIR/$tdir/${tdir} ||
3348                 error "$DIR/$tdir/${tdir}.new: rename of foreign_symlink dir has failed"
3349
3350         #remove foreign_symlink dir should still fail
3351         rmdir $DIR/$tdir/${tdir} &&
3352                 error "$DIR/$tdir/${tdir}: remove of foreign_symlink dir should fail"
3353
3354         #use special ioctl() to unlink foreign_symlink file
3355         $LFS unlink_foreign $DIR/$tdir/${tdir} ||
3356                 error "$DIR/$tdir/$tdir: unlink/ioctl failed"
3357
3358         #created file should still exist
3359         [[ -f /tmp/${uuid1}/${uuid2}/$tfile ]] ||
3360                 error "/tmp/${uuid1}/${uuid2}/$tfile has been removed"
3361         diff /etc/passwd /tmp/${uuid1}/${uuid2}/$tfile ||
3362                 error "/tmp/${uuid1}/${uuid2}/$tfile: diff has failed"
3363 }
3364 run_test 27P "basic ops on foreign dir of foreign_symlink type"
3365
3366 test_27Q() {
3367         rm -f $TMP/$tfile $TMP/$tfile.loop $TMP/$tfile.none $TMP/$tfile.broken
3368         stack_trap "rm -f $TMP/$tfile*"
3369
3370         test_mkdir $DIR/$tdir-1
3371         test_mkdir $DIR/$tdir-2
3372
3373         echo 'It is what it is' > $DIR/$tdir-1/$tfile
3374         lov_getstripe_old $DIR/$tdir-1/$tfile || error "$DIR/$tdir-1/$tfile: rc = $?"
3375
3376         ln -s $DIR/$tdir-1/$tfile $DIR/$tdir-2/$tfile
3377         lov_getstripe_old $DIR/$tdir-2/$tfile || error "$DIR/$tdir-2/$tfile: rc = $?"
3378
3379         ln -s $DIR/$tdir-1/$tfile $TMP/$tfile
3380         lov_getstripe_old $TMP/$tfile || error "$TMP/$tfile: rc = $?"
3381
3382         # Create some bad symlinks and ensure that we don't loop
3383         # forever or something. These should return ELOOP (40) and
3384         # ENOENT (2) but I don't want to test for that because there's
3385         # always some weirdo architecture that needs to ruin
3386         # everything by defining these error numbers differently.
3387
3388         ln -s $TMP/$tfile.loop $TMP/$tfile.loop
3389         lov_getstripe_old $TMP/$tfile.loop && error "$TMP/$tfile.loop: rc = $?"
3390
3391         ln -s $TMP/$tfile.none $TMP/$tfile.broken
3392         lov_getstripe_old $TMP/$tfile.broken && error "$TMP/$tfile.broken: rc = $?"
3393
3394         return 0
3395 }
3396 run_test 27Q "llapi_file_get_stripe() works on symlinks"
3397
3398 # createtest also checks that device nodes are created and
3399 # then visible correctly (#2091)
3400 test_28() { # bug 2091
3401         test_mkdir $DIR/d28
3402         $CREATETEST $DIR/d28/ct || error "createtest failed"
3403 }
3404 run_test 28 "create/mknod/mkdir with bad file types ============"
3405
3406 test_29() {
3407         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3408
3409         sync; sleep 1; sync # flush out any dirty pages from previous tests
3410         cancel_lru_locks
3411         test_mkdir $DIR/d29
3412         touch $DIR/d29/foo
3413         log 'first d29'
3414         ls -l $DIR/d29
3415
3416         declare -i LOCKCOUNTORIG=0
3417         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3418                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3419         done
3420         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3421
3422         declare -i LOCKUNUSEDCOUNTORIG=0
3423         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3424                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3425         done
3426
3427         log 'second d29'
3428         ls -l $DIR/d29
3429         log 'done'
3430
3431         declare -i LOCKCOUNTCURRENT=0
3432         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3433                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3434         done
3435
3436         declare -i LOCKUNUSEDCOUNTCURRENT=0
3437         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3438                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3439         done
3440
3441         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3442                 $LCTL set_param -n ldlm.dump_namespaces ""
3443                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3444                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3445                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3446                 return 2
3447         fi
3448         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3449                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3450                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3451                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3452                 return 3
3453         fi
3454 }
3455 run_test 29 "IT_GETATTR regression  ============================"
3456
3457 test_30a() { # was test_30
3458         cp $(which ls) $DIR || cp /bin/ls $DIR
3459         $DIR/ls / || error "Can't execute binary from lustre"
3460         rm $DIR/ls
3461 }
3462 run_test 30a "execute binary from Lustre (execve) =============="
3463
3464 test_30b() {
3465         cp `which ls` $DIR || cp /bin/ls $DIR
3466         chmod go+rx $DIR/ls
3467         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3468         rm $DIR/ls
3469 }
3470 run_test 30b "execute binary from Lustre as non-root ==========="
3471
3472 test_30c() { # b=22376
3473         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3474
3475         cp $(which ls) $DIR || cp /bin/ls $DIR
3476         chmod a-rw $DIR/ls
3477         cancel_lru_locks mdc
3478         cancel_lru_locks osc
3479         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3480         rm -f $DIR/ls
3481 }
3482 run_test 30c "execute binary from Lustre without read perms ===="
3483
3484 test_30d() {
3485         cp $(which dd) $DIR || error "failed to copy dd to $DIR/dd"
3486
3487         for i in {1..10}; do
3488                 $DIR/dd bs=1M count=128 if=/dev/zero of=$DIR/$tfile &
3489                 local PID=$!
3490                 sleep 1
3491                 $LCTL set_param ldlm.namespaces.*MDT*.lru_size=clear
3492                 wait $PID || error "executing dd from Lustre failed"
3493                 rm -f $DIR/$tfile
3494         done
3495
3496         rm -f $DIR/dd
3497 }
3498 run_test 30d "execute binary from Lustre while clear locks"
3499
3500 test_31a() {
3501         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3502         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3503 }
3504 run_test 31a "open-unlink file =================================="
3505
3506 test_31b() {
3507         touch $DIR/f31 || error "touch $DIR/f31 failed"
3508         ln $DIR/f31 $DIR/f31b || error "ln failed"
3509         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3510         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3511 }
3512 run_test 31b "unlink file with multiple links while open ======="
3513
3514 test_31c() {
3515         touch $DIR/f31 || error "touch $DIR/f31 failed"
3516         ln $DIR/f31 $DIR/f31c || error "ln failed"
3517         multiop_bg_pause $DIR/f31 O_uc ||
3518                 error "multiop_bg_pause for $DIR/f31 failed"
3519         MULTIPID=$!
3520         $MULTIOP $DIR/f31c Ouc
3521         kill -USR1 $MULTIPID
3522         wait $MULTIPID
3523 }
3524 run_test 31c "open-unlink file with multiple links ============="
3525
3526 test_31d() {
3527         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3528         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3529 }
3530 run_test 31d "remove of open directory ========================="
3531
3532 test_31e() { # bug 2904
3533         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3534 }
3535 run_test 31e "remove of open non-empty directory ==============="
3536
3537 test_31f() { # bug 4554
3538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3539
3540         set -vx
3541         test_mkdir $DIR/d31f
3542         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3543         cp /etc/hosts $DIR/d31f
3544         ls -l $DIR/d31f
3545         $LFS getstripe $DIR/d31f/hosts
3546         multiop_bg_pause $DIR/d31f D_c || return 1
3547         MULTIPID=$!
3548
3549         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3550         test_mkdir $DIR/d31f
3551         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3552         cp /etc/hosts $DIR/d31f
3553         ls -l $DIR/d31f
3554         $LFS getstripe $DIR/d31f/hosts
3555         multiop_bg_pause $DIR/d31f D_c || return 1
3556         MULTIPID2=$!
3557
3558         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3559         wait $MULTIPID || error "first opendir $MULTIPID failed"
3560
3561         sleep 6
3562
3563         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3564         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3565         set +vx
3566 }
3567 run_test 31f "remove of open directory with open-unlink file ==="
3568
3569 test_31g() {
3570         echo "-- cross directory link --"
3571         test_mkdir -c1 $DIR/${tdir}ga
3572         test_mkdir -c1 $DIR/${tdir}gb
3573         touch $DIR/${tdir}ga/f
3574         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3575         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3576         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3577         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3578         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3579 }
3580 run_test 31g "cross directory link==============="
3581
3582 test_31h() {
3583         echo "-- cross directory link --"
3584         test_mkdir -c1 $DIR/${tdir}
3585         test_mkdir -c1 $DIR/${tdir}/dir
3586         touch $DIR/${tdir}/f
3587         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3588         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3589         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3590         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3591         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3592 }
3593 run_test 31h "cross directory link under child==============="
3594
3595 test_31i() {
3596         echo "-- cross directory link --"
3597         test_mkdir -c1 $DIR/$tdir
3598         test_mkdir -c1 $DIR/$tdir/dir
3599         touch $DIR/$tdir/dir/f
3600         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3601         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3602         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3603         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3604         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3605 }
3606 run_test 31i "cross directory link under parent==============="
3607
3608 test_31j() {
3609         test_mkdir -c1 -p $DIR/$tdir
3610         test_mkdir -c1 -p $DIR/$tdir/dir1
3611         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3612         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3613         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3614         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3615         return 0
3616 }
3617 run_test 31j "link for directory==============="
3618
3619 test_31k() {
3620         test_mkdir -c1 -p $DIR/$tdir
3621         touch $DIR/$tdir/s
3622         touch $DIR/$tdir/exist
3623         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3624         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3625         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3626         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3627         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3628         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3629         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3630         return 0
3631 }
3632 run_test 31k "link to file: the same, non-existing, dir==============="
3633
3634 test_31m() {
3635         mkdir $DIR/d31m
3636         touch $DIR/d31m/s
3637         mkdir $DIR/d31m2
3638         touch $DIR/d31m2/exist
3639         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3640         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3641         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3642         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3643         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3644         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3645         return 0
3646 }
3647 run_test 31m "link to file: the same, non-existing, dir==============="
3648
3649 test_31n() {
3650         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3651         nlink=$(stat --format=%h $DIR/$tfile)
3652         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3653         local fd=$(free_fd)
3654         local cmd="exec $fd<$DIR/$tfile"
3655         eval $cmd
3656         cmd="exec $fd<&-"
3657         trap "eval $cmd" EXIT
3658         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3659         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3660         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3661         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3662         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3663         eval $cmd
3664 }
3665 run_test 31n "check link count of unlinked file"
3666
3667 link_one() {
3668         local tempfile=$(mktemp $1_XXXXXX)
3669         mlink $tempfile $1 2> /dev/null &&
3670                 echo "$BASHPID: link $tempfile to $1 succeeded"
3671         munlink $tempfile
3672 }
3673
3674 test_31o() { # LU-2901
3675         test_mkdir $DIR/$tdir
3676         for LOOP in $(seq 100); do
3677                 rm -f $DIR/$tdir/$tfile*
3678                 for THREAD in $(seq 8); do
3679                         link_one $DIR/$tdir/$tfile.$LOOP &
3680                 done
3681                 wait
3682                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3683                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3684                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3685                         break || true
3686         done
3687 }
3688 run_test 31o "duplicate hard links with same filename"
3689
3690 test_31p() {
3691         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3692
3693         test_mkdir $DIR/$tdir
3694         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3695         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3696
3697         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3698                 error "open unlink test1 failed"
3699         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3700                 error "open unlink test2 failed"
3701
3702         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3703                 error "test1 still exists"
3704         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3705                 error "test2 still exists"
3706 }
3707 run_test 31p "remove of open striped directory"
3708
3709 test_31q() {
3710         [ $MDSCOUNT -lt 3 ] && skip_env "needs >= 3 MDTs"
3711
3712         $LFS mkdir -i 3,1 $DIR/$tdir || error "mkdir failed"
3713         index=$($LFS getdirstripe -i $DIR/$tdir)
3714         [ $index -eq 3 ] || error "first stripe index $index != 3"
3715         index=$($LFS getdirstripe $DIR/$tdir | tail -1 | awk '{print $1}')
3716         [ $index -eq 1 ] || error "second stripe index $index != 1"
3717
3718         # when "-c <stripe_count>" is set, the number of MDTs specified after
3719         # "-i" should equal to the stripe count
3720         $LFS mkdir -i 3,1 -c 3 $DIR/$tdir.2 && error "mkdir should fail" || true
3721 }
3722 run_test 31q "create striped directory on specific MDTs"
3723
3724 cleanup_test32_mount() {
3725         local rc=0
3726         trap 0
3727         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3728         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3729         losetup -d $loopdev || true
3730         rm -rf $DIR/$tdir
3731         return $rc
3732 }
3733
3734 test_32a() {
3735         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3736
3737         echo "== more mountpoints and symlinks ================="
3738         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3739         trap cleanup_test32_mount EXIT
3740         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3741         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3742                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3743         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3744                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3745         cleanup_test32_mount
3746 }
3747 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3748
3749 test_32b() {
3750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3751
3752         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3753         trap cleanup_test32_mount EXIT
3754         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3755         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3756                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3757         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3758                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3759         cleanup_test32_mount
3760 }
3761 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3762
3763 test_32c() {
3764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3765
3766         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3767         trap cleanup_test32_mount EXIT
3768         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3769         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3770                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3771         test_mkdir -p $DIR/$tdir/d2/test_dir
3772         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3773                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3774         cleanup_test32_mount
3775 }
3776 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3777
3778 test_32d() {
3779         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3780
3781         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3782         trap cleanup_test32_mount EXIT
3783         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3784         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3785                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3786         test_mkdir -p $DIR/$tdir/d2/test_dir
3787         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3788                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3789         cleanup_test32_mount
3790 }
3791 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3792
3793 test_32e() {
3794         rm -fr $DIR/$tdir
3795         test_mkdir -p $DIR/$tdir/tmp
3796         local tmp_dir=$DIR/$tdir/tmp
3797         ln -s $DIR/$tdir $tmp_dir/symlink11
3798         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3799         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3800         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3801 }
3802 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3803
3804 test_32f() {
3805         rm -fr $DIR/$tdir
3806         test_mkdir -p $DIR/$tdir/tmp
3807         local tmp_dir=$DIR/$tdir/tmp
3808         ln -s $DIR/$tdir $tmp_dir/symlink11
3809         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3810         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3811         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3812 }
3813 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3814
3815 test_32g() {
3816         local tmp_dir=$DIR/$tdir/tmp
3817         test_mkdir -p $tmp_dir
3818         test_mkdir $DIR/${tdir}2
3819         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3820         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3821         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3822         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3823         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3824         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3825 }
3826 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3827
3828 test_32h() {
3829         rm -fr $DIR/$tdir $DIR/${tdir}2
3830         tmp_dir=$DIR/$tdir/tmp
3831         test_mkdir -p $tmp_dir
3832         test_mkdir $DIR/${tdir}2
3833         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3834         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3835         ls $tmp_dir/symlink12 || error "listing symlink12"
3836         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3837 }
3838 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3839
3840 test_32i() {
3841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3842
3843         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3844         trap cleanup_test32_mount EXIT
3845         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3846         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3847                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3848         touch $DIR/$tdir/test_file
3849         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3850                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3851         cleanup_test32_mount
3852 }
3853 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3854
3855 test_32j() {
3856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3857
3858         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3859         trap cleanup_test32_mount EXIT
3860         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3861         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3862                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3863         touch $DIR/$tdir/test_file
3864         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3865                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3866         cleanup_test32_mount
3867 }
3868 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3869
3870 test_32k() {
3871         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3872
3873         rm -fr $DIR/$tdir
3874         trap cleanup_test32_mount EXIT
3875         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3876         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3877                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3878         test_mkdir -p $DIR/$tdir/d2
3879         touch $DIR/$tdir/d2/test_file || error "touch failed"
3880         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3881                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3882         cleanup_test32_mount
3883 }
3884 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3885
3886 test_32l() {
3887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3888
3889         rm -fr $DIR/$tdir
3890         trap cleanup_test32_mount EXIT
3891         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3892         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3893                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3894         test_mkdir -p $DIR/$tdir/d2
3895         touch $DIR/$tdir/d2/test_file || error "touch failed"
3896         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3897                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3898         cleanup_test32_mount
3899 }
3900 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3901
3902 test_32m() {
3903         rm -fr $DIR/d32m
3904         test_mkdir -p $DIR/d32m/tmp
3905         TMP_DIR=$DIR/d32m/tmp
3906         ln -s $DIR $TMP_DIR/symlink11
3907         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3908         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3909                 error "symlink11 not a link"
3910         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3911                 error "symlink01 not a link"
3912 }
3913 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3914
3915 test_32n() {
3916         rm -fr $DIR/d32n
3917         test_mkdir -p $DIR/d32n/tmp
3918         TMP_DIR=$DIR/d32n/tmp
3919         ln -s $DIR $TMP_DIR/symlink11
3920         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3921         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3922         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3923 }
3924 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3925
3926 test_32o() {
3927         touch $DIR/$tfile
3928         test_mkdir -p $DIR/d32o/tmp
3929         TMP_DIR=$DIR/d32o/tmp
3930         ln -s $DIR/$tfile $TMP_DIR/symlink12
3931         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3932         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3933                 error "symlink12 not a link"
3934         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3935         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3936                 error "$DIR/d32o/tmp/symlink12 not file type"
3937         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3938                 error "$DIR/d32o/symlink02 not file type"
3939 }
3940 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3941
3942 test_32p() {
3943         log 32p_1
3944         rm -fr $DIR/d32p
3945         log 32p_2
3946         rm -f $DIR/$tfile
3947         log 32p_3
3948         touch $DIR/$tfile
3949         log 32p_4
3950         test_mkdir -p $DIR/d32p/tmp
3951         log 32p_5
3952         TMP_DIR=$DIR/d32p/tmp
3953         log 32p_6
3954         ln -s $DIR/$tfile $TMP_DIR/symlink12
3955         log 32p_7
3956         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3957         log 32p_8
3958         cat $DIR/d32p/tmp/symlink12 ||
3959                 error "Can't open $DIR/d32p/tmp/symlink12"
3960         log 32p_9
3961         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3962         log 32p_10
3963 }
3964 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3965
3966 test_32q() {
3967         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3968
3969         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3970         trap cleanup_test32_mount EXIT
3971         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3972         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3973         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3974                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3975         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3976         cleanup_test32_mount
3977 }
3978 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3979
3980 test_32r() {
3981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3982
3983         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3984         trap cleanup_test32_mount EXIT
3985         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3986         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3987         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3988                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3989         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3990         cleanup_test32_mount
3991 }
3992 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3993
3994 test_33aa() {
3995         rm -f $DIR/$tfile
3996         touch $DIR/$tfile
3997         chmod 444 $DIR/$tfile
3998         chown $RUNAS_ID $DIR/$tfile
3999         log 33_1
4000         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4001         log 33_2
4002 }
4003 run_test 33aa "write file with mode 444 (should return error)"
4004
4005 test_33a() {
4006         rm -fr $DIR/$tdir
4007         test_mkdir $DIR/$tdir
4008         chown $RUNAS_ID $DIR/$tdir
4009         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
4010                 error "$RUNAS create $tdir/$tfile failed"
4011         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
4012                 error "open RDWR" || true
4013 }
4014 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
4015
4016 test_33b() {
4017         rm -fr $DIR/$tdir
4018         test_mkdir $DIR/$tdir
4019         chown $RUNAS_ID $DIR/$tdir
4020         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
4021 }
4022 run_test 33b "test open file with malformed flags (No panic)"
4023
4024 test_33c() {
4025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4026         remote_ost_nodsh && skip "remote OST with nodsh"
4027
4028         local ostnum
4029         local ostname
4030         local write_bytes
4031         local all_zeros
4032
4033         all_zeros=true
4034         test_mkdir $DIR/$tdir
4035         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
4036
4037         sync
4038         for ostnum in $(seq $OSTCOUNT); do
4039                 # test-framework's OST numbering is one-based, while Lustre's
4040                 # is zero-based
4041                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4042                 # check if at least some write_bytes stats are counted
4043                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4044                               obdfilter.$ostname.stats |
4045                               awk '/^write_bytes/ {print $7}' )
4046                 echo "baseline_write_bytes@ost$ostnum/$ostname=$write_bytes"
4047                 if (( ${write_bytes:-0} > 0 )); then
4048                         all_zeros=false
4049                         break
4050                 fi
4051         done
4052
4053         $all_zeros || return 0
4054
4055         # Write four bytes
4056         echo foo > $DIR/$tdir/bar
4057         # Really write them
4058         sync
4059
4060         # Total up write_bytes after writing.  We'd better find non-zeros.
4061         for ostnum in $(seq $OSTCOUNT); do
4062                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4063                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4064                               obdfilter/$ostname/stats |
4065                               awk '/^write_bytes/ {print $7}' )
4066                 echo "write_bytes@ost$ostnum/$ostname=$write_bytes"
4067                 if (( ${write_bytes:-0} > 0 )); then
4068                         all_zeros=false
4069                         break
4070                 fi
4071         done
4072
4073         if $all_zeros; then
4074                 for ostnum in $(seq $OSTCOUNT); do
4075                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4076                         echo "Check write_bytes is in obdfilter.*.stats:"
4077                         do_facet ost$ostnum lctl get_param -n \
4078                                 obdfilter.$ostname.stats
4079                 done
4080                 error "OST not keeping write_bytes stats (b=22312)"
4081         fi
4082 }
4083 run_test 33c "test write_bytes stats"
4084
4085 test_33d() {
4086         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4088
4089         local MDTIDX=1
4090         local remote_dir=$DIR/$tdir/remote_dir
4091
4092         test_mkdir $DIR/$tdir
4093         $LFS mkdir -i $MDTIDX $remote_dir ||
4094                 error "create remote directory failed"
4095
4096         touch $remote_dir/$tfile
4097         chmod 444 $remote_dir/$tfile
4098         chown $RUNAS_ID $remote_dir/$tfile
4099
4100         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4101
4102         chown $RUNAS_ID $remote_dir
4103         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
4104                                         error "create" || true
4105         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
4106                                     error "open RDWR" || true
4107         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
4108 }
4109 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
4110
4111 test_33e() {
4112         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4113
4114         mkdir $DIR/$tdir
4115
4116         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4117         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4118         mkdir $DIR/$tdir/local_dir
4119
4120         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4121         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4122         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4123
4124         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4125                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
4126
4127         rmdir $DIR/$tdir/* || error "rmdir failed"
4128
4129         umask 777
4130         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4131         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4132         mkdir $DIR/$tdir/local_dir
4133
4134         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4135         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4136         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4137
4138         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4139                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
4140
4141         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
4142
4143         umask 000
4144         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4145         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4146         mkdir $DIR/$tdir/local_dir
4147
4148         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4149         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4150         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4151
4152         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4153                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
4154 }
4155 run_test 33e "mkdir and striped directory should have same mode"
4156
4157 cleanup_33f() {
4158         trap 0
4159         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
4160 }
4161
4162 test_33f() {
4163         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4164         remote_mds_nodsh && skip "remote MDS with nodsh"
4165
4166         mkdir $DIR/$tdir
4167         chmod go+rwx $DIR/$tdir
4168         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
4169         trap cleanup_33f EXIT
4170
4171         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
4172                 error "cannot create striped directory"
4173
4174         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
4175                 error "cannot create files in striped directory"
4176
4177         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
4178                 error "cannot remove files in striped directory"
4179
4180         $RUNAS rmdir $DIR/$tdir/striped_dir ||
4181                 error "cannot remove striped directory"
4182
4183         cleanup_33f
4184 }
4185 run_test 33f "nonroot user can create, access, and remove a striped directory"
4186
4187 test_33g() {
4188         mkdir -p $DIR/$tdir/dir2
4189
4190         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
4191         echo $err
4192         [[ $err =~ "exists" ]] || error "Not exists error"
4193 }
4194 run_test 33g "nonroot user create already existing root created file"
4195
4196 test_33h() {
4197         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4198         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
4199                 skip "Need MDS version at least 2.13.50"
4200
4201         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
4202                 error "mkdir $tdir failed"
4203         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
4204
4205         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
4206         local index2
4207
4208         for fname in $DIR/$tdir/$tfile.bak \
4209                      $DIR/$tdir/$tfile.SAV \
4210                      $DIR/$tdir/$tfile.orig \
4211                      $DIR/$tdir/$tfile~; do
4212                 touch $fname  || error "touch $fname failed"
4213                 index2=$($LFS getstripe -m $fname)
4214                 [ $index -eq $index2 ] ||
4215                         error "$fname MDT index mismatch $index != $index2"
4216         done
4217
4218         local failed=0
4219         for i in {1..250}; do
4220                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
4221                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
4222                         touch $fname  || error "touch $fname failed"
4223                         index2=$($LFS getstripe -m $fname)
4224                         if [[ $index != $index2 ]]; then
4225                                 failed=$((failed + 1))
4226                                 echo "$fname MDT index mismatch $index != $index2"
4227                         fi
4228                 done
4229         done
4230         echo "$failed MDT index mismatches"
4231         (( failed < 20 )) || error "MDT index mismatch $failed times"
4232
4233 }
4234 run_test 33h "temp file is located on the same MDT as target"
4235
4236 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
4237 test_34a() {
4238         rm -f $DIR/f34
4239         $MCREATE $DIR/f34 || error "mcreate failed"
4240         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4241                 error "getstripe failed"
4242         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
4243         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4244                 error "getstripe failed"
4245         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4246                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4247 }
4248 run_test 34a "truncate file that has not been opened ==========="
4249
4250 test_34b() {
4251         [ ! -f $DIR/f34 ] && test_34a
4252         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4253                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4254         $OPENFILE -f O_RDONLY $DIR/f34
4255         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4256                 error "getstripe failed"
4257         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4258                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4259 }
4260 run_test 34b "O_RDONLY opening file doesn't create objects ====="
4261
4262 test_34c() {
4263         [ ! -f $DIR/f34 ] && test_34a
4264         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4265                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4266         $OPENFILE -f O_RDWR $DIR/f34
4267         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
4268                 error "$LFS getstripe failed"
4269         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4270                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4271 }
4272 run_test 34c "O_RDWR opening file-with-size works =============="
4273
4274 test_34d() {
4275         [ ! -f $DIR/f34 ] && test_34a
4276         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
4277                 error "dd failed"
4278         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4279                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4280         rm $DIR/f34
4281 }
4282 run_test 34d "write to sparse file ============================="
4283
4284 test_34e() {
4285         rm -f $DIR/f34e
4286         $MCREATE $DIR/f34e || error "mcreate failed"
4287         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
4288         $CHECKSTAT -s 1000 $DIR/f34e ||
4289                 error "Size of $DIR/f34e not equal to 1000 bytes"
4290         $OPENFILE -f O_RDWR $DIR/f34e
4291         $CHECKSTAT -s 1000 $DIR/f34e ||
4292                 error "Size of $DIR/f34e not equal to 1000 bytes"
4293 }
4294 run_test 34e "create objects, some with size and some without =="
4295
4296 test_34f() { # bug 6242, 6243
4297         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4298
4299         SIZE34F=48000
4300         rm -f $DIR/f34f
4301         $MCREATE $DIR/f34f || error "mcreate failed"
4302         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
4303         dd if=$DIR/f34f of=$TMP/f34f
4304         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
4305         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
4306         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
4307         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
4308         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
4309 }
4310 run_test 34f "read from a file with no objects until EOF ======="
4311
4312 test_34g() {
4313         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4314
4315         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
4316                 error "dd failed"
4317         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
4318         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4319                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
4320         cancel_lru_locks osc
4321         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4322                 error "wrong size after lock cancel"
4323
4324         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
4325         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4326                 error "expanding truncate failed"
4327         cancel_lru_locks osc
4328         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4329                 error "wrong expanded size after lock cancel"
4330 }
4331 run_test 34g "truncate long file ==============================="
4332
4333 test_34h() {
4334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4335
4336         local gid=10
4337         local sz=1000
4338
4339         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
4340         sync # Flush the cache so that multiop below does not block on cache
4341              # flush when getting the group lock
4342         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
4343         MULTIPID=$!
4344
4345         # Since just timed wait is not good enough, let's do a sync write
4346         # that way we are sure enough time for a roundtrip + processing
4347         # passed + 2 seconds of extra margin.
4348         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
4349         rm $DIR/${tfile}-1
4350         sleep 2
4351
4352         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
4353                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
4354                 kill -9 $MULTIPID
4355         fi
4356         wait $MULTIPID
4357         local nsz=`stat -c %s $DIR/$tfile`
4358         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
4359 }
4360 run_test 34h "ftruncate file under grouplock should not block"
4361
4362 test_35a() {
4363         cp /bin/sh $DIR/f35a
4364         chmod 444 $DIR/f35a
4365         chown $RUNAS_ID $DIR/f35a
4366         $RUNAS $DIR/f35a && error || true
4367         rm $DIR/f35a
4368 }
4369 run_test 35a "exec file with mode 444 (should return and not leak)"
4370
4371 test_36a() {
4372         rm -f $DIR/f36
4373         utime $DIR/f36 || error "utime failed for MDS"
4374 }
4375 run_test 36a "MDS utime check (mknod, utime)"
4376
4377 test_36b() {
4378         echo "" > $DIR/f36
4379         utime $DIR/f36 || error "utime failed for OST"
4380 }
4381 run_test 36b "OST utime check (open, utime)"
4382
4383 test_36c() {
4384         rm -f $DIR/d36/f36
4385         test_mkdir $DIR/d36
4386         chown $RUNAS_ID $DIR/d36
4387         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
4388 }
4389 run_test 36c "non-root MDS utime check (mknod, utime)"
4390
4391 test_36d() {
4392         [ ! -d $DIR/d36 ] && test_36c
4393         echo "" > $DIR/d36/f36
4394         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
4395 }
4396 run_test 36d "non-root OST utime check (open, utime)"
4397
4398 test_36e() {
4399         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
4400
4401         test_mkdir $DIR/$tdir
4402         touch $DIR/$tdir/$tfile
4403         $RUNAS utime $DIR/$tdir/$tfile &&
4404                 error "utime worked, expected failure" || true
4405 }
4406 run_test 36e "utime on non-owned file (should return error)"
4407
4408 subr_36fh() {
4409         local fl="$1"
4410         local LANG_SAVE=$LANG
4411         local LC_LANG_SAVE=$LC_LANG
4412         export LANG=C LC_LANG=C # for date language
4413
4414         DATESTR="Dec 20  2000"
4415         test_mkdir $DIR/$tdir
4416         lctl set_param fail_loc=$fl
4417         date; date +%s
4418         cp /etc/hosts $DIR/$tdir/$tfile
4419         sync & # write RPC generated with "current" inode timestamp, but delayed
4420         sleep 1
4421         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4422         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4423         cancel_lru_locks $OSC
4424         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4425         date; date +%s
4426         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4427                 echo "BEFORE: $LS_BEFORE" && \
4428                 echo "AFTER : $LS_AFTER" && \
4429                 echo "WANT  : $DATESTR" && \
4430                 error "$DIR/$tdir/$tfile timestamps changed" || true
4431
4432         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4433 }
4434
4435 test_36f() {
4436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4437
4438         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4439         subr_36fh "0x80000214"
4440 }
4441 run_test 36f "utime on file racing with OST BRW write =========="
4442
4443 test_36g() {
4444         remote_ost_nodsh && skip "remote OST with nodsh"
4445         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4446         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4447                 skip "Need MDS version at least 2.12.51"
4448
4449         local fmd_max_age
4450         local fmd
4451         local facet="ost1"
4452         local tgt="obdfilter"
4453
4454         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4455
4456         test_mkdir $DIR/$tdir
4457         fmd_max_age=$(do_facet $facet \
4458                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4459                 head -n 1")
4460
4461         echo "FMD max age: ${fmd_max_age}s"
4462         touch $DIR/$tdir/$tfile
4463         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4464                 gawk '{cnt=cnt+$1}  END{print cnt}')
4465         echo "FMD before: $fmd"
4466         [[ $fmd == 0 ]] &&
4467                 error "FMD wasn't create by touch"
4468         sleep $((fmd_max_age + 12))
4469         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4470                 gawk '{cnt=cnt+$1}  END{print cnt}')
4471         echo "FMD after: $fmd"
4472         [[ $fmd == 0 ]] ||
4473                 error "FMD wasn't expired by ping"
4474 }
4475 run_test 36g "FMD cache expiry ====================="
4476
4477 test_36h() {
4478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4479
4480         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4481         subr_36fh "0x80000227"
4482 }
4483 run_test 36h "utime on file racing with OST BRW write =========="
4484
4485 test_36i() {
4486         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4487
4488         test_mkdir $DIR/$tdir
4489         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4490
4491         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4492         local new_mtime=$((mtime + 200))
4493
4494         #change Modify time of striped dir
4495         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4496                         error "change mtime failed"
4497
4498         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4499
4500         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4501 }
4502 run_test 36i "change mtime on striped directory"
4503
4504 # test_37 - duplicate with tests 32q 32r
4505
4506 test_38() {
4507         local file=$DIR/$tfile
4508         touch $file
4509         openfile -f O_DIRECTORY $file
4510         local RC=$?
4511         local ENOTDIR=20
4512         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4513         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4514 }
4515 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4516
4517 test_39a() { # was test_39
4518         touch $DIR/$tfile
4519         touch $DIR/${tfile}2
4520 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4521 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4522 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4523         sleep 2
4524         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4525         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4526                 echo "mtime"
4527                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4528                 echo "atime"
4529                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4530                 echo "ctime"
4531                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4532                 error "O_TRUNC didn't change timestamps"
4533         fi
4534 }
4535 run_test 39a "mtime changed on create"
4536
4537 test_39b() {
4538         test_mkdir -c1 $DIR/$tdir
4539         cp -p /etc/passwd $DIR/$tdir/fopen
4540         cp -p /etc/passwd $DIR/$tdir/flink
4541         cp -p /etc/passwd $DIR/$tdir/funlink
4542         cp -p /etc/passwd $DIR/$tdir/frename
4543         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4544
4545         sleep 1
4546         echo "aaaaaa" >> $DIR/$tdir/fopen
4547         echo "aaaaaa" >> $DIR/$tdir/flink
4548         echo "aaaaaa" >> $DIR/$tdir/funlink
4549         echo "aaaaaa" >> $DIR/$tdir/frename
4550
4551         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4552         local link_new=`stat -c %Y $DIR/$tdir/flink`
4553         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4554         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4555
4556         cat $DIR/$tdir/fopen > /dev/null
4557         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4558         rm -f $DIR/$tdir/funlink2
4559         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4560
4561         for (( i=0; i < 2; i++ )) ; do
4562                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4563                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4564                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4565                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4566
4567                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4568                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4569                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4570                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4571
4572                 cancel_lru_locks $OSC
4573                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4574         done
4575 }
4576 run_test 39b "mtime change on open, link, unlink, rename  ======"
4577
4578 # this should be set to past
4579 TEST_39_MTIME=`date -d "1 year ago" +%s`
4580
4581 # bug 11063
4582 test_39c() {
4583         touch $DIR1/$tfile
4584         sleep 2
4585         local mtime0=`stat -c %Y $DIR1/$tfile`
4586
4587         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4588         local mtime1=`stat -c %Y $DIR1/$tfile`
4589         [ "$mtime1" = $TEST_39_MTIME ] || \
4590                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4591
4592         local d1=`date +%s`
4593         echo hello >> $DIR1/$tfile
4594         local d2=`date +%s`
4595         local mtime2=`stat -c %Y $DIR1/$tfile`
4596         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4597                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4598
4599         mv $DIR1/$tfile $DIR1/$tfile-1
4600
4601         for (( i=0; i < 2; i++ )) ; do
4602                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4603                 [ "$mtime2" = "$mtime3" ] || \
4604                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4605
4606                 cancel_lru_locks $OSC
4607                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4608         done
4609 }
4610 run_test 39c "mtime change on rename ==========================="
4611
4612 # bug 21114
4613 test_39d() {
4614         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4615
4616         touch $DIR1/$tfile
4617         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4618
4619         for (( i=0; i < 2; i++ )) ; do
4620                 local mtime=`stat -c %Y $DIR1/$tfile`
4621                 [ $mtime = $TEST_39_MTIME ] || \
4622                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4623
4624                 cancel_lru_locks $OSC
4625                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4626         done
4627 }
4628 run_test 39d "create, utime, stat =============================="
4629
4630 # bug 21114
4631 test_39e() {
4632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4633
4634         touch $DIR1/$tfile
4635         local mtime1=`stat -c %Y $DIR1/$tfile`
4636
4637         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4638
4639         for (( i=0; i < 2; i++ )) ; do
4640                 local mtime2=`stat -c %Y $DIR1/$tfile`
4641                 [ $mtime2 = $TEST_39_MTIME ] || \
4642                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4643
4644                 cancel_lru_locks $OSC
4645                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4646         done
4647 }
4648 run_test 39e "create, stat, utime, stat ========================"
4649
4650 # bug 21114
4651 test_39f() {
4652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4653
4654         touch $DIR1/$tfile
4655         mtime1=`stat -c %Y $DIR1/$tfile`
4656
4657         sleep 2
4658         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4659
4660         for (( i=0; i < 2; i++ )) ; do
4661                 local mtime2=`stat -c %Y $DIR1/$tfile`
4662                 [ $mtime2 = $TEST_39_MTIME ] || \
4663                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4664
4665                 cancel_lru_locks $OSC
4666                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4667         done
4668 }
4669 run_test 39f "create, stat, sleep, utime, stat ================="
4670
4671 # bug 11063
4672 test_39g() {
4673         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4674
4675         echo hello >> $DIR1/$tfile
4676         local mtime1=`stat -c %Y $DIR1/$tfile`
4677
4678         sleep 2
4679         chmod o+r $DIR1/$tfile
4680
4681         for (( i=0; i < 2; i++ )) ; do
4682                 local mtime2=`stat -c %Y $DIR1/$tfile`
4683                 [ "$mtime1" = "$mtime2" ] || \
4684                         error "lost mtime: $mtime2, should be $mtime1"
4685
4686                 cancel_lru_locks $OSC
4687                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4688         done
4689 }
4690 run_test 39g "write, chmod, stat ==============================="
4691
4692 # bug 11063
4693 test_39h() {
4694         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4695
4696         touch $DIR1/$tfile
4697         sleep 1
4698
4699         local d1=`date`
4700         echo hello >> $DIR1/$tfile
4701         local mtime1=`stat -c %Y $DIR1/$tfile`
4702
4703         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4704         local d2=`date`
4705         if [ "$d1" != "$d2" ]; then
4706                 echo "write and touch not within one second"
4707         else
4708                 for (( i=0; i < 2; i++ )) ; do
4709                         local mtime2=`stat -c %Y $DIR1/$tfile`
4710                         [ "$mtime2" = $TEST_39_MTIME ] || \
4711                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4712
4713                         cancel_lru_locks $OSC
4714                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4715                 done
4716         fi
4717 }
4718 run_test 39h "write, utime within one second, stat ============="
4719
4720 test_39i() {
4721         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4722
4723         touch $DIR1/$tfile
4724         sleep 1
4725
4726         echo hello >> $DIR1/$tfile
4727         local mtime1=`stat -c %Y $DIR1/$tfile`
4728
4729         mv $DIR1/$tfile $DIR1/$tfile-1
4730
4731         for (( i=0; i < 2; i++ )) ; do
4732                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4733
4734                 [ "$mtime1" = "$mtime2" ] || \
4735                         error "lost mtime: $mtime2, should be $mtime1"
4736
4737                 cancel_lru_locks $OSC
4738                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4739         done
4740 }
4741 run_test 39i "write, rename, stat =============================="
4742
4743 test_39j() {
4744         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4745
4746         start_full_debug_logging
4747         touch $DIR1/$tfile
4748         sleep 1
4749
4750         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4751         lctl set_param fail_loc=0x80000412
4752         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4753                 error "multiop failed"
4754         local multipid=$!
4755         local mtime1=`stat -c %Y $DIR1/$tfile`
4756
4757         mv $DIR1/$tfile $DIR1/$tfile-1
4758
4759         kill -USR1 $multipid
4760         wait $multipid || error "multiop close failed"
4761
4762         for (( i=0; i < 2; i++ )) ; do
4763                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4764                 [ "$mtime1" = "$mtime2" ] ||
4765                         error "mtime is lost on close: $mtime2, " \
4766                               "should be $mtime1"
4767
4768                 cancel_lru_locks
4769                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4770         done
4771         lctl set_param fail_loc=0
4772         stop_full_debug_logging
4773 }
4774 run_test 39j "write, rename, close, stat ======================="
4775
4776 test_39k() {
4777         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4778
4779         touch $DIR1/$tfile
4780         sleep 1
4781
4782         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4783         local multipid=$!
4784         local mtime1=`stat -c %Y $DIR1/$tfile`
4785
4786         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4787
4788         kill -USR1 $multipid
4789         wait $multipid || error "multiop close failed"
4790
4791         for (( i=0; i < 2; i++ )) ; do
4792                 local mtime2=`stat -c %Y $DIR1/$tfile`
4793
4794                 [ "$mtime2" = $TEST_39_MTIME ] || \
4795                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4796
4797                 cancel_lru_locks
4798                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4799         done
4800 }
4801 run_test 39k "write, utime, close, stat ========================"
4802
4803 # this should be set to future
4804 TEST_39_ATIME=`date -d "1 year" +%s`
4805
4806 test_39l() {
4807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4808         remote_mds_nodsh && skip "remote MDS with nodsh"
4809
4810         local atime_diff=$(do_facet $SINGLEMDS \
4811                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4812         rm -rf $DIR/$tdir
4813         mkdir_on_mdt0 $DIR/$tdir
4814
4815         # test setting directory atime to future
4816         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4817         local atime=$(stat -c %X $DIR/$tdir)
4818         [ "$atime" = $TEST_39_ATIME ] ||
4819                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4820
4821         # test setting directory atime from future to now
4822         local now=$(date +%s)
4823         touch -a -d @$now $DIR/$tdir
4824
4825         atime=$(stat -c %X $DIR/$tdir)
4826         [ "$atime" -eq "$now"  ] ||
4827                 error "atime is not updated from future: $atime, $now"
4828
4829         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4830         sleep 3
4831
4832         # test setting directory atime when now > dir atime + atime_diff
4833         local d1=$(date +%s)
4834         ls $DIR/$tdir
4835         local d2=$(date +%s)
4836         cancel_lru_locks mdc
4837         atime=$(stat -c %X $DIR/$tdir)
4838         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4839                 error "atime is not updated  : $atime, should be $d2"
4840
4841         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4842         sleep 3
4843
4844         # test not setting directory atime when now < dir atime + atime_diff
4845         ls $DIR/$tdir
4846         cancel_lru_locks mdc
4847         atime=$(stat -c %X $DIR/$tdir)
4848         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4849                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4850
4851         do_facet $SINGLEMDS \
4852                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4853 }
4854 run_test 39l "directory atime update ==========================="
4855
4856 test_39m() {
4857         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4858
4859         touch $DIR1/$tfile
4860         sleep 2
4861         local far_past_mtime=$(date -d "May 29 1953" +%s)
4862         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4863
4864         touch -m -d @$far_past_mtime $DIR1/$tfile
4865         touch -a -d @$far_past_atime $DIR1/$tfile
4866
4867         for (( i=0; i < 2; i++ )) ; do
4868                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4869                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4870                         error "atime or mtime set incorrectly"
4871
4872                 cancel_lru_locks $OSC
4873                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4874         done
4875 }
4876 run_test 39m "test atime and mtime before 1970"
4877
4878 test_39n() { # LU-3832
4879         remote_mds_nodsh && skip "remote MDS with nodsh"
4880
4881         local atime_diff=$(do_facet $SINGLEMDS \
4882                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4883         local atime0
4884         local atime1
4885         local atime2
4886
4887         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4888
4889         rm -rf $DIR/$tfile
4890         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4891         atime0=$(stat -c %X $DIR/$tfile)
4892
4893         sleep 5
4894         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4895         atime1=$(stat -c %X $DIR/$tfile)
4896
4897         sleep 5
4898         cancel_lru_locks mdc
4899         cancel_lru_locks osc
4900         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4901         atime2=$(stat -c %X $DIR/$tfile)
4902
4903         do_facet $SINGLEMDS \
4904                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4905
4906         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4907         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4908 }
4909 run_test 39n "check that O_NOATIME is honored"
4910
4911 test_39o() {
4912         TESTDIR=$DIR/$tdir/$tfile
4913         [ -e $TESTDIR ] && rm -rf $TESTDIR
4914         mkdir -p $TESTDIR
4915         cd $TESTDIR
4916         links1=2
4917         ls
4918         mkdir a b
4919         ls
4920         links2=$(stat -c %h .)
4921         [ $(($links1 + 2)) != $links2 ] &&
4922                 error "wrong links count $(($links1 + 2)) != $links2"
4923         rmdir b
4924         links3=$(stat -c %h .)
4925         [ $(($links1 + 1)) != $links3 ] &&
4926                 error "wrong links count $links1 != $links3"
4927         return 0
4928 }
4929 run_test 39o "directory cached attributes updated after create"
4930
4931 test_39p() {
4932         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4933
4934         local MDTIDX=1
4935         TESTDIR=$DIR/$tdir/$tdir
4936         [ -e $TESTDIR ] && rm -rf $TESTDIR
4937         test_mkdir -p $TESTDIR
4938         cd $TESTDIR
4939         links1=2
4940         ls
4941         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4942         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4943         ls
4944         links2=$(stat -c %h .)
4945         [ $(($links1 + 2)) != $links2 ] &&
4946                 error "wrong links count $(($links1 + 2)) != $links2"
4947         rmdir remote_dir2
4948         links3=$(stat -c %h .)
4949         [ $(($links1 + 1)) != $links3 ] &&
4950                 error "wrong links count $links1 != $links3"
4951         return 0
4952 }
4953 run_test 39p "remote directory cached attributes updated after create ========"
4954
4955 test_39r() {
4956         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
4957                 skip "no atime update on old OST"
4958         if [ "$ost1_FSTYPE" != ldiskfs ]; then
4959                 skip_env "ldiskfs only test"
4960         fi
4961
4962         local saved_adiff
4963         saved_adiff=$(do_facet ost1 \
4964                 lctl get_param -n obdfilter.*OST0000.atime_diff)
4965         stack_trap "do_facet ost1 \
4966                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
4967
4968         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
4969
4970         $LFS setstripe -i 0 $DIR/$tfile
4971         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
4972                 error "can't write initial file"
4973         cancel_lru_locks osc
4974
4975         # exceed atime_diff and access file
4976         sleep 6
4977         dd if=$DIR/$tfile of=/dev/null bs=4k count=1 ||
4978                 error "can't udpate atime"
4979
4980         local atime_cli=$(stat -c %X $DIR/$tfile)
4981         echo "client atime: $atime_cli"
4982         # allow atime update to be written to device
4983         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
4984         sleep 5
4985
4986         local ostdev=$(ostdevname 1)
4987         local fid=($(lfs getstripe -y $DIR/$tfile |
4988                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
4989         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
4990         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
4991
4992         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
4993         local atime_ost=$(do_facet ost1 "$cmd" |&
4994                           awk -F'[: ]' '/atime:/ { print $4 }')
4995         (( atime_cli == atime_ost )) ||
4996                 error "atime on client $atime_cli != ost $atime_ost"
4997 }
4998 run_test 39r "lazy atime update on OST"
4999
5000 test_39q() { # LU-8041
5001         local testdir=$DIR/$tdir
5002         mkdir -p $testdir
5003         multiop_bg_pause $testdir D_c || error "multiop failed"
5004         local multipid=$!
5005         cancel_lru_locks mdc
5006         kill -USR1 $multipid
5007         local atime=$(stat -c %X $testdir)
5008         [ "$atime" -ne 0 ] || error "atime is zero"
5009 }
5010 run_test 39q "close won't zero out atime"
5011
5012 test_40() {
5013         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
5014         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
5015                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
5016         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
5017                 error "$tfile is not 4096 bytes in size"
5018 }
5019 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
5020
5021 test_41() {
5022         # bug 1553
5023         small_write $DIR/f41 18
5024 }
5025 run_test 41 "test small file write + fstat ====================="
5026
5027 count_ost_writes() {
5028         lctl get_param -n ${OSC}.*.stats |
5029                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
5030                         END { printf("%0.0f", writes) }'
5031 }
5032
5033 # decent default
5034 WRITEBACK_SAVE=500
5035 DIRTY_RATIO_SAVE=40
5036 MAX_DIRTY_RATIO=50
5037 BG_DIRTY_RATIO_SAVE=10
5038 MAX_BG_DIRTY_RATIO=25
5039
5040 start_writeback() {
5041         trap 0
5042         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
5043         # dirty_ratio, dirty_background_ratio
5044         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5045                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
5046                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
5047                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
5048         else
5049                 # if file not here, we are a 2.4 kernel
5050                 kill -CONT `pidof kupdated`
5051         fi
5052 }
5053
5054 stop_writeback() {
5055         # setup the trap first, so someone cannot exit the test at the
5056         # exact wrong time and mess up a machine
5057         trap start_writeback EXIT
5058         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
5059         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5060                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
5061                 sysctl -w vm.dirty_writeback_centisecs=0
5062                 sysctl -w vm.dirty_writeback_centisecs=0
5063                 # save and increase /proc/sys/vm/dirty_ratio
5064                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
5065                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
5066                 # save and increase /proc/sys/vm/dirty_background_ratio
5067                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
5068                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
5069         else
5070                 # if file not here, we are a 2.4 kernel
5071                 kill -STOP `pidof kupdated`
5072         fi
5073 }
5074
5075 # ensure that all stripes have some grant before we test client-side cache
5076 setup_test42() {
5077         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
5078                 dd if=/dev/zero of=$i bs=4k count=1
5079                 rm $i
5080         done
5081 }
5082
5083 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
5084 # file truncation, and file removal.
5085 test_42a() {
5086         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5087
5088         setup_test42
5089         cancel_lru_locks $OSC
5090         stop_writeback
5091         sync; sleep 1; sync # just to be safe
5092         BEFOREWRITES=`count_ost_writes`
5093         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
5094         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
5095         AFTERWRITES=`count_ost_writes`
5096         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
5097                 error "$BEFOREWRITES < $AFTERWRITES"
5098         start_writeback
5099 }
5100 run_test 42a "ensure that we don't flush on close"
5101
5102 test_42b() {
5103         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5104
5105         setup_test42
5106         cancel_lru_locks $OSC
5107         stop_writeback
5108         sync
5109         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
5110         BEFOREWRITES=$(count_ost_writes)
5111         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
5112         AFTERWRITES=$(count_ost_writes)
5113         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5114                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
5115         fi
5116         BEFOREWRITES=$(count_ost_writes)
5117         sync || error "sync: $?"
5118         AFTERWRITES=$(count_ost_writes)
5119         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5120                 error "$BEFOREWRITES < $AFTERWRITES on sync"
5121         fi
5122         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
5123         start_writeback
5124         return 0
5125 }
5126 run_test 42b "test destroy of file with cached dirty data ======"
5127
5128 # if these tests just want to test the effect of truncation,
5129 # they have to be very careful.  consider:
5130 # - the first open gets a {0,EOF}PR lock
5131 # - the first write conflicts and gets a {0, count-1}PW
5132 # - the rest of the writes are under {count,EOF}PW
5133 # - the open for truncate tries to match a {0,EOF}PR
5134 #   for the filesize and cancels the PWs.
5135 # any number of fixes (don't get {0,EOF} on open, match
5136 # composite locks, do smarter file size management) fix
5137 # this, but for now we want these tests to verify that
5138 # the cancellation with truncate intent works, so we
5139 # start the file with a full-file pw lock to match against
5140 # until the truncate.
5141 trunc_test() {
5142         test=$1
5143         file=$DIR/$test
5144         offset=$2
5145         cancel_lru_locks $OSC
5146         stop_writeback
5147         # prime the file with 0,EOF PW to match
5148         touch $file
5149         $TRUNCATE $file 0
5150         sync; sync
5151         # now the real test..
5152         dd if=/dev/zero of=$file bs=1024 count=100
5153         BEFOREWRITES=`count_ost_writes`
5154         $TRUNCATE $file $offset
5155         cancel_lru_locks $OSC
5156         AFTERWRITES=`count_ost_writes`
5157         start_writeback
5158 }
5159
5160 test_42c() {
5161         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5162
5163         trunc_test 42c 1024
5164         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
5165                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
5166         rm $file
5167 }
5168 run_test 42c "test partial truncate of file with cached dirty data"
5169
5170 test_42d() {
5171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5172
5173         trunc_test 42d 0
5174         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
5175                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
5176         rm $file
5177 }
5178 run_test 42d "test complete truncate of file with cached dirty data"
5179
5180 test_42e() { # bug22074
5181         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5182
5183         local TDIR=$DIR/${tdir}e
5184         local pages=16 # hardcoded 16 pages, don't change it.
5185         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
5186         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
5187         local max_dirty_mb
5188         local warmup_files
5189
5190         test_mkdir $DIR/${tdir}e
5191         $LFS setstripe -c 1 $TDIR
5192         createmany -o $TDIR/f $files
5193
5194         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
5195
5196         # we assume that with $OSTCOUNT files, at least one of them will
5197         # be allocated on OST0.
5198         warmup_files=$((OSTCOUNT * max_dirty_mb))
5199         createmany -o $TDIR/w $warmup_files
5200
5201         # write a large amount of data into one file and sync, to get good
5202         # avail_grant number from OST.
5203         for ((i=0; i<$warmup_files; i++)); do
5204                 idx=$($LFS getstripe -i $TDIR/w$i)
5205                 [ $idx -ne 0 ] && continue
5206                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
5207                 break
5208         done
5209         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
5210         sync
5211         $LCTL get_param $proc_osc0/cur_dirty_bytes
5212         $LCTL get_param $proc_osc0/cur_grant_bytes
5213
5214         # create as much dirty pages as we can while not to trigger the actual
5215         # RPCs directly. but depends on the env, VFS may trigger flush during this
5216         # period, hopefully we are good.
5217         for ((i=0; i<$warmup_files; i++)); do
5218                 idx=$($LFS getstripe -i $TDIR/w$i)
5219                 [ $idx -ne 0 ] && continue
5220                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
5221         done
5222         $LCTL get_param $proc_osc0/cur_dirty_bytes
5223         $LCTL get_param $proc_osc0/cur_grant_bytes
5224
5225         # perform the real test
5226         $LCTL set_param $proc_osc0/rpc_stats 0
5227         for ((;i<$files; i++)); do
5228                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
5229                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
5230         done
5231         sync
5232         $LCTL get_param $proc_osc0/rpc_stats
5233
5234         local percent=0
5235         local have_ppr=false
5236         $LCTL get_param $proc_osc0/rpc_stats |
5237                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
5238                         # skip lines until we are at the RPC histogram data
5239                         [ "$PPR" == "pages" ] && have_ppr=true && continue
5240                         $have_ppr || continue
5241
5242                         # we only want the percent stat for < 16 pages
5243                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
5244
5245                         percent=$((percent + WPCT))
5246                         if [[ $percent -gt 15 ]]; then
5247                                 error "less than 16-pages write RPCs" \
5248                                       "$percent% > 15%"
5249                                 break
5250                         fi
5251                 done
5252         rm -rf $TDIR
5253 }
5254 run_test 42e "verify sub-RPC writes are not done synchronously"
5255
5256 test_43A() { # was test_43
5257         test_mkdir $DIR/$tdir
5258         cp -p /bin/ls $DIR/$tdir/$tfile
5259         $MULTIOP $DIR/$tdir/$tfile Ow_c &
5260         pid=$!
5261         # give multiop a chance to open
5262         sleep 1
5263
5264         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
5265         kill -USR1 $pid
5266         # Wait for multiop to exit
5267         wait $pid
5268 }
5269 run_test 43A "execution of file opened for write should return -ETXTBSY"
5270
5271 test_43a() {
5272         test_mkdir $DIR/$tdir
5273         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5274         $DIR/$tdir/sleep 60 &
5275         SLEEP_PID=$!
5276         # Make sure exec of $tdir/sleep wins race with truncate
5277         sleep 1
5278         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
5279         kill $SLEEP_PID
5280 }
5281 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
5282
5283 test_43b() {
5284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5285
5286         test_mkdir $DIR/$tdir
5287         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5288         $DIR/$tdir/sleep 60 &
5289         SLEEP_PID=$!
5290         # Make sure exec of $tdir/sleep wins race with truncate
5291         sleep 1
5292         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
5293         kill $SLEEP_PID
5294 }
5295 run_test 43b "truncate of file being executed should return -ETXTBSY"
5296
5297 test_43c() {
5298         local testdir="$DIR/$tdir"
5299         test_mkdir $testdir
5300         cp $SHELL $testdir/
5301         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
5302                 ( cd $testdir && md5sum -c )
5303 }
5304 run_test 43c "md5sum of copy into lustre"
5305
5306 test_44A() { # was test_44
5307         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5308
5309         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
5310         dd if=$DIR/f1 bs=4k count=1 > /dev/null
5311 }
5312 run_test 44A "zero length read from a sparse stripe"
5313
5314 test_44a() {
5315         local nstripe=$($LFS getstripe -c -d $DIR)
5316         [ -z "$nstripe" ] && skip "can't get stripe info"
5317         [[ $nstripe -gt $OSTCOUNT ]] &&
5318                 skip "Wrong default stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
5319
5320         local stride=$($LFS getstripe -S -d $DIR)
5321         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
5322                 nstripe=$($LFS df $DIR | grep OST: | wc -l)
5323         fi
5324
5325         OFFSETS="0 $((stride/2)) $((stride-1))"
5326         for offset in $OFFSETS; do
5327                 for i in $(seq 0 $((nstripe-1))); do
5328                         local GLOBALOFFSETS=""
5329                         # size in Bytes
5330                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
5331                         local myfn=$DIR/d44a-$size
5332                         echo "--------writing $myfn at $size"
5333                         ll_sparseness_write $myfn $size ||
5334                                 error "ll_sparseness_write"
5335                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
5336                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5337                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5338
5339                         for j in $(seq 0 $((nstripe-1))); do
5340                                 # size in Bytes
5341                                 size=$((((j + $nstripe )*$stride + $offset)))
5342                                 ll_sparseness_write $myfn $size ||
5343                                         error "ll_sparseness_write"
5344                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
5345                         done
5346                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5347                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5348                         rm -f $myfn
5349                 done
5350         done
5351 }
5352 run_test 44a "test sparse pwrite ==============================="
5353
5354 dirty_osc_total() {
5355         tot=0
5356         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
5357                 tot=$(($tot + $d))
5358         done
5359         echo $tot
5360 }
5361 do_dirty_record() {
5362         before=`dirty_osc_total`
5363         echo executing "\"$*\""
5364         eval $*
5365         after=`dirty_osc_total`
5366         echo before $before, after $after
5367 }
5368 test_45() {
5369         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5370
5371         f="$DIR/f45"
5372         # Obtain grants from OST if it supports it
5373         echo blah > ${f}_grant
5374         stop_writeback
5375         sync
5376         do_dirty_record "echo blah > $f"
5377         [[ $before -eq $after ]] && error "write wasn't cached"
5378         do_dirty_record "> $f"
5379         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
5380         do_dirty_record "echo blah > $f"
5381         [[ $before -eq $after ]] && error "write wasn't cached"
5382         do_dirty_record "sync"
5383         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
5384         do_dirty_record "echo blah > $f"
5385         [[ $before -eq $after ]] && error "write wasn't cached"
5386         do_dirty_record "cancel_lru_locks osc"
5387         [[ $before -gt $after ]] ||
5388                 error "lock cancellation didn't lower dirty count"
5389         start_writeback
5390 }
5391 run_test 45 "osc io page accounting ============================"
5392
5393 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
5394 # test tickles a bug where re-dirtying a page was failing to be mapped to the
5395 # objects offset and an assert hit when an rpc was built with 1023's mapped
5396 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
5397 test_46() {
5398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5399
5400         f="$DIR/f46"
5401         stop_writeback
5402         sync
5403         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5404         sync
5405         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5406         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5407         sync
5408         start_writeback
5409 }
5410 run_test 46 "dirtying a previously written page ================"
5411
5412 # test_47 is removed "Device nodes check" is moved to test_28
5413
5414 test_48a() { # bug 2399
5415         [ "$mds1_FSTYPE" = "zfs" ] &&
5416         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5417                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5418
5419         test_mkdir $DIR/$tdir
5420         cd $DIR/$tdir
5421         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5422         test_mkdir $DIR/$tdir
5423         touch foo || error "'touch foo' failed after recreating cwd"
5424         test_mkdir bar
5425         touch .foo || error "'touch .foo' failed after recreating cwd"
5426         test_mkdir .bar
5427         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5428         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5429         cd . || error "'cd .' failed after recreating cwd"
5430         mkdir . && error "'mkdir .' worked after recreating cwd"
5431         rmdir . && error "'rmdir .' worked after recreating cwd"
5432         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5433         cd .. || error "'cd ..' failed after recreating cwd"
5434 }
5435 run_test 48a "Access renamed working dir (should return errors)="
5436
5437 test_48b() { # bug 2399
5438         rm -rf $DIR/$tdir
5439         test_mkdir $DIR/$tdir
5440         cd $DIR/$tdir
5441         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5442         touch foo && error "'touch foo' worked after removing cwd"
5443         mkdir foo && error "'mkdir foo' worked after removing cwd"
5444         touch .foo && error "'touch .foo' worked after removing cwd"
5445         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5446         ls . > /dev/null && error "'ls .' worked after removing cwd"
5447         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5448         mkdir . && error "'mkdir .' worked after removing cwd"
5449         rmdir . && error "'rmdir .' worked after removing cwd"
5450         ln -s . foo && error "'ln -s .' worked after removing cwd"
5451         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5452 }
5453 run_test 48b "Access removed working dir (should return errors)="
5454
5455 test_48c() { # bug 2350
5456         #lctl set_param debug=-1
5457         #set -vx
5458         rm -rf $DIR/$tdir
5459         test_mkdir -p $DIR/$tdir/dir
5460         cd $DIR/$tdir/dir
5461         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5462         $TRACE touch foo && error "touch foo worked after removing cwd"
5463         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5464         touch .foo && error "touch .foo worked after removing cwd"
5465         mkdir .foo && error "mkdir .foo worked after removing cwd"
5466         $TRACE ls . && error "'ls .' worked after removing cwd"
5467         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5468         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5469         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5470         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5471         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5472 }
5473 run_test 48c "Access removed working subdir (should return errors)"
5474
5475 test_48d() { # bug 2350
5476         #lctl set_param debug=-1
5477         #set -vx
5478         rm -rf $DIR/$tdir
5479         test_mkdir -p $DIR/$tdir/dir
5480         cd $DIR/$tdir/dir
5481         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5482         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5483         $TRACE touch foo && error "'touch foo' worked after removing parent"
5484         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5485         touch .foo && error "'touch .foo' worked after removing parent"
5486         mkdir .foo && error "mkdir .foo worked after removing parent"
5487         $TRACE ls . && error "'ls .' worked after removing parent"
5488         $TRACE ls .. && error "'ls ..' worked after removing parent"
5489         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5490         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5491         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5492         true
5493 }
5494 run_test 48d "Access removed parent subdir (should return errors)"
5495
5496 test_48e() { # bug 4134
5497         #lctl set_param debug=-1
5498         #set -vx
5499         rm -rf $DIR/$tdir
5500         test_mkdir -p $DIR/$tdir/dir
5501         cd $DIR/$tdir/dir
5502         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5503         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5504         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5505         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5506         # On a buggy kernel addition of "touch foo" after cd .. will
5507         # produce kernel oops in lookup_hash_it
5508         touch ../foo && error "'cd ..' worked after recreate parent"
5509         cd $DIR
5510         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5511 }
5512 run_test 48e "Access to recreated parent subdir (should return errors)"
5513
5514 test_48f() {
5515         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
5516                 skip "need MDS >= 2.13.55"
5517         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
5518         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] ||
5519                 skip "needs different host for mdt1 mdt2"
5520         [[ $(facet_fstype mds1) == ldiskfs ]] || skip "ldiskfs only"
5521
5522         $LFS mkdir -i0 $DIR/$tdir
5523         $LFS mkdir -i 1 $DIR/$tdir/sub1 $DIR/$tdir/sub2 $DIR/$tdir/sub3
5524
5525         for d in sub1 sub2 sub3; do
5526                 #define OBD_FAIL_OSD_REF_DEL    0x19c
5527                 do_facet mds1 $LCTL set_param fail_loc=0x8000019c
5528                 rm -rf $DIR/$tdir/$d && error "rm $d should fail"
5529         done
5530
5531         rm -d --interactive=never $DIR/$tdir || error "rm $tdir fail"
5532 }
5533 run_test 48f "non-zero nlink dir unlink won't LBUG()"
5534
5535 test_49() { # LU-1030
5536         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5537         remote_ost_nodsh && skip "remote OST with nodsh"
5538
5539         # get ost1 size - $FSNAME-OST0000
5540         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5541                 awk '{ print $4 }')
5542         # write 800M at maximum
5543         [[ $ost1_size -lt 2 ]] && ost1_size=2
5544         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5545
5546         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5547         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5548         local dd_pid=$!
5549
5550         # change max_pages_per_rpc while writing the file
5551         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5552         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5553         # loop until dd process exits
5554         while ps ax -opid | grep -wq $dd_pid; do
5555                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5556                 sleep $((RANDOM % 5 + 1))
5557         done
5558         # restore original max_pages_per_rpc
5559         $LCTL set_param $osc1_mppc=$orig_mppc
5560         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5561 }
5562 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5563
5564 test_50() {
5565         # bug 1485
5566         test_mkdir $DIR/$tdir
5567         cd $DIR/$tdir
5568         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5569 }
5570 run_test 50 "special situations: /proc symlinks  ==============="
5571
5572 test_51a() {    # was test_51
5573         # bug 1516 - create an empty entry right after ".." then split dir
5574         test_mkdir -c1 $DIR/$tdir
5575         touch $DIR/$tdir/foo
5576         $MCREATE $DIR/$tdir/bar
5577         rm $DIR/$tdir/foo
5578         createmany -m $DIR/$tdir/longfile 201
5579         FNUM=202
5580         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5581                 $MCREATE $DIR/$tdir/longfile$FNUM
5582                 FNUM=$(($FNUM + 1))
5583                 echo -n "+"
5584         done
5585         echo
5586         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5587 }
5588 run_test 51a "special situations: split htree with empty entry =="
5589
5590 cleanup_print_lfs_df () {
5591         trap 0
5592         $LFS df
5593         $LFS df -i
5594 }
5595
5596 test_51b() {
5597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5598
5599         local dir=$DIR/$tdir
5600         local nrdirs=$((65536 + 100))
5601
5602         # cleanup the directory
5603         rm -fr $dir
5604
5605         mkdir_on_mdt -i $((RANDOM % MDSCOUNT)) $dir
5606
5607         $LFS df
5608         $LFS df -i
5609         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5610         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5611         [[ $numfree -lt $nrdirs ]] &&
5612                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5613
5614         # need to check free space for the directories as well
5615         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5616         numfree=$(( blkfree / $(fs_inode_ksize) ))
5617         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5618
5619         trap cleanup_print_lfs_df EXIT
5620
5621         # create files
5622         createmany -d $dir/d $nrdirs || {
5623                 unlinkmany $dir/d $nrdirs
5624                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5625         }
5626
5627         # really created :
5628         nrdirs=$(ls -U $dir | wc -l)
5629
5630         # unlink all but 100 subdirectories, then check it still works
5631         local left=100
5632         local delete=$((nrdirs - left))
5633
5634         $LFS df
5635         $LFS df -i
5636
5637         # for ldiskfs the nlink count should be 1, but this is OSD specific
5638         # and so this is listed for informational purposes only
5639         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5640         unlinkmany -d $dir/d $delete ||
5641                 error "unlink of first $delete subdirs failed"
5642
5643         echo "nlink between: $(stat -c %h $dir)"
5644         local found=$(ls -U $dir | wc -l)
5645         [ $found -ne $left ] &&
5646                 error "can't find subdirs: found only $found, expected $left"
5647
5648         unlinkmany -d $dir/d $delete $left ||
5649                 error "unlink of second $left subdirs failed"
5650         # regardless of whether the backing filesystem tracks nlink accurately
5651         # or not, the nlink count shouldn't be more than "." and ".." here
5652         local after=$(stat -c %h $dir)
5653         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5654                 echo "nlink after: $after"
5655
5656         cleanup_print_lfs_df
5657 }
5658 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5659
5660 test_51d() {
5661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5662         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5663
5664         test_mkdir $DIR/$tdir
5665         createmany -o $DIR/$tdir/t- 1000
5666         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5667         for N in $(seq 0 $((OSTCOUNT - 1))); do
5668                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5669                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5670                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5671                         '($1 == '$N') { objs += 1 } \
5672                         END { printf("%0.0f", objs) }')
5673                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5674         done
5675         unlinkmany $DIR/$tdir/t- 1000
5676
5677         NLAST=0
5678         for N in $(seq 1 $((OSTCOUNT - 1))); do
5679                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5680                         error "OST $N has less objects vs OST $NLAST" \
5681                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5682                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5683                         error "OST $N has less objects vs OST $NLAST" \
5684                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5685
5686                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5687                         error "OST $N has less #0 objects vs OST $NLAST" \
5688                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5689                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5690                         error "OST $N has less #0 objects vs OST $NLAST" \
5691                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5692                 NLAST=$N
5693         done
5694         rm -f $TMP/$tfile
5695 }
5696 run_test 51d "check object distribution"
5697
5698 test_51e() {
5699         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5700                 skip_env "ldiskfs only test"
5701         fi
5702
5703         test_mkdir -c1 $DIR/$tdir
5704         test_mkdir -c1 $DIR/$tdir/d0
5705
5706         touch $DIR/$tdir/d0/foo
5707         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5708                 error "file exceed 65000 nlink limit!"
5709         unlinkmany $DIR/$tdir/d0/f- 65001
5710         return 0
5711 }
5712 run_test 51e "check file nlink limit"
5713
5714 test_51f() {
5715         test_mkdir $DIR/$tdir
5716
5717         local max=100000
5718         local ulimit_old=$(ulimit -n)
5719         local spare=20 # number of spare fd's for scripts/libraries, etc.
5720         local mdt=$($LFS getstripe -m $DIR/$tdir)
5721         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5722
5723         echo "MDT$mdt numfree=$numfree, max=$max"
5724         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5725         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5726                 while ! ulimit -n $((numfree + spare)); do
5727                         numfree=$((numfree * 3 / 4))
5728                 done
5729                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5730         else
5731                 echo "left ulimit at $ulimit_old"
5732         fi
5733
5734         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5735                 unlinkmany $DIR/$tdir/f $numfree
5736                 error "create+open $numfree files in $DIR/$tdir failed"
5737         }
5738         ulimit -n $ulimit_old
5739
5740         # if createmany exits at 120s there will be fewer than $numfree files
5741         unlinkmany $DIR/$tdir/f $numfree || true
5742 }
5743 run_test 51f "check many open files limit"
5744
5745 test_52a() {
5746         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5747         test_mkdir $DIR/$tdir
5748         touch $DIR/$tdir/foo
5749         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5750         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5751         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5752         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5753         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5754                                         error "link worked"
5755         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5756         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5757         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5758                                                      error "lsattr"
5759         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5760         cp -r $DIR/$tdir $TMP/
5761         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5762 }
5763 run_test 52a "append-only flag test (should return errors)"
5764
5765 test_52b() {
5766         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5767         test_mkdir $DIR/$tdir
5768         touch $DIR/$tdir/foo
5769         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5770         cat test > $DIR/$tdir/foo && error "cat test worked"
5771         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5772         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5773         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5774                                         error "link worked"
5775         echo foo >> $DIR/$tdir/foo && error "echo worked"
5776         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5777         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5778         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5779         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5780                                                         error "lsattr"
5781         chattr -i $DIR/$tdir/foo || error "chattr failed"
5782
5783         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5784 }
5785 run_test 52b "immutable flag test (should return errors) ======="
5786
5787 test_53() {
5788         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5789         remote_mds_nodsh && skip "remote MDS with nodsh"
5790         remote_ost_nodsh && skip "remote OST with nodsh"
5791
5792         local param
5793         local param_seq
5794         local ostname
5795         local mds_last
5796         local mds_last_seq
5797         local ost_last
5798         local ost_last_seq
5799         local ost_last_id
5800         local ostnum
5801         local node
5802         local found=false
5803         local support_last_seq=true
5804
5805         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5806                 support_last_seq=false
5807
5808         # only test MDT0000
5809         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5810         local value
5811         for value in $(do_facet $SINGLEMDS \
5812                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5813                 param=$(echo ${value[0]} | cut -d "=" -f1)
5814                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5815
5816                 if $support_last_seq; then
5817                         param_seq=$(echo $param |
5818                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5819                         mds_last_seq=$(do_facet $SINGLEMDS \
5820                                        $LCTL get_param -n $param_seq)
5821                 fi
5822                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5823
5824                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5825                 node=$(facet_active_host ost$((ostnum+1)))
5826                 param="obdfilter.$ostname.last_id"
5827                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5828                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5829                         ost_last_id=$ost_last
5830
5831                         if $support_last_seq; then
5832                                 ost_last_id=$(echo $ost_last |
5833                                               awk -F':' '{print $2}' |
5834                                               sed -e "s/^0x//g")
5835                                 ost_last_seq=$(echo $ost_last |
5836                                                awk -F':' '{print $1}')
5837                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5838                         fi
5839
5840                         if [[ $ost_last_id != $mds_last ]]; then
5841                                 error "$ost_last_id != $mds_last"
5842                         else
5843                                 found=true
5844                                 break
5845                         fi
5846                 done
5847         done
5848         $found || error "can not match last_seq/last_id for $mdtosc"
5849         return 0
5850 }
5851 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5852
5853 test_54a() {
5854         perl -MSocket -e ';' || skip "no Socket perl module installed"
5855
5856         $SOCKETSERVER $DIR/socket ||
5857                 error "$SOCKETSERVER $DIR/socket failed: $?"
5858         $SOCKETCLIENT $DIR/socket ||
5859                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5860         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5861 }
5862 run_test 54a "unix domain socket test =========================="
5863
5864 test_54b() {
5865         f="$DIR/f54b"
5866         mknod $f c 1 3
5867         chmod 0666 $f
5868         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5869 }
5870 run_test 54b "char device works in lustre ======================"
5871
5872 find_loop_dev() {
5873         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5874         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5875         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5876
5877         for i in $(seq 3 7); do
5878                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5879                 LOOPDEV=$LOOPBASE$i
5880                 LOOPNUM=$i
5881                 break
5882         done
5883 }
5884
5885 cleanup_54c() {
5886         local rc=0
5887         loopdev="$DIR/loop54c"
5888
5889         trap 0
5890         $UMOUNT $DIR/$tdir || rc=$?
5891         losetup -d $loopdev || true
5892         losetup -d $LOOPDEV || true
5893         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5894         return $rc
5895 }
5896
5897 test_54c() {
5898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5899
5900         loopdev="$DIR/loop54c"
5901
5902         find_loop_dev
5903         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5904         trap cleanup_54c EXIT
5905         mknod $loopdev b 7 $LOOPNUM
5906         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5907         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5908         losetup $loopdev $DIR/$tfile ||
5909                 error "can't set up $loopdev for $DIR/$tfile"
5910         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5911         test_mkdir $DIR/$tdir
5912         mount -t ext2 $loopdev $DIR/$tdir ||
5913                 error "error mounting $loopdev on $DIR/$tdir"
5914         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5915                 error "dd write"
5916         df $DIR/$tdir
5917         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5918                 error "dd read"
5919         cleanup_54c
5920 }
5921 run_test 54c "block device works in lustre ====================="
5922
5923 test_54d() {
5924         f="$DIR/f54d"
5925         string="aaaaaa"
5926         mknod $f p
5927         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5928 }
5929 run_test 54d "fifo device works in lustre ======================"
5930
5931 test_54e() {
5932         f="$DIR/f54e"
5933         string="aaaaaa"
5934         cp -aL /dev/console $f
5935         echo $string > $f || error "echo $string to $f failed"
5936 }
5937 run_test 54e "console/tty device works in lustre ======================"
5938
5939 test_56a() {
5940         local numfiles=3
5941         local numdirs=2
5942         local dir=$DIR/$tdir
5943
5944         rm -rf $dir
5945         test_mkdir -p $dir/dir
5946         for i in $(seq $numfiles); do
5947                 touch $dir/file$i
5948                 touch $dir/dir/file$i
5949         done
5950
5951         local numcomp=$($LFS getstripe --component-count $dir)
5952
5953         [[ $numcomp == 0 ]] && numcomp=1
5954
5955         # test lfs getstripe with --recursive
5956         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5957
5958         [[ $filenum -eq $((numfiles * 2)) ]] ||
5959                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5960         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5961         [[ $filenum -eq $numfiles ]] ||
5962                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5963         echo "$LFS getstripe showed obdidx or l_ost_idx"
5964
5965         # test lfs getstripe with file instead of dir
5966         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5967         [[ $filenum -eq 1 ]] ||
5968                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5969         echo "$LFS getstripe file1 passed"
5970
5971         #test lfs getstripe with --verbose
5972         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5973         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5974                 error "$LFS getstripe --verbose $dir: "\
5975                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5976         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5977                 error "$LFS getstripe $dir: showed lmm_magic"
5978
5979         #test lfs getstripe with -v prints lmm_fid
5980         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5981         local countfids=$((numdirs + numfiles * numcomp))
5982         [[ $filenum -eq $countfids ]] ||
5983                 error "$LFS getstripe -v $dir: "\
5984                       "got $filenum want $countfids lmm_fid"
5985         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5986                 error "$LFS getstripe $dir: showed lmm_fid by default"
5987         echo "$LFS getstripe --verbose passed"
5988
5989         #check for FID information
5990         local fid1=$($LFS getstripe --fid $dir/file1)
5991         local fid2=$($LFS getstripe --verbose $dir/file1 |
5992                      awk '/lmm_fid: / { print $2; exit; }')
5993         local fid3=$($LFS path2fid $dir/file1)
5994
5995         [ "$fid1" != "$fid2" ] &&
5996                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5997         [ "$fid1" != "$fid3" ] &&
5998                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5999         echo "$LFS getstripe --fid passed"
6000
6001         #test lfs getstripe with --obd
6002         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
6003                 error "$LFS getstripe --obd wrong_uuid: should return error"
6004
6005         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6006
6007         local ostidx=1
6008         local obduuid=$(ostuuid_from_index $ostidx)
6009         local found=$($LFS getstripe -r --obd $obduuid $dir |
6010                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
6011
6012         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
6013         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
6014                 ((filenum--))
6015         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
6016                 ((filenum--))
6017
6018         [[ $found -eq $filenum ]] ||
6019                 error "$LFS getstripe --obd: found $found expect $filenum"
6020         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
6021                 sed '/^[         ]*'${ostidx}'[  ]/d' |
6022                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
6023                 error "$LFS getstripe --obd: should not show file on other obd"
6024         echo "$LFS getstripe --obd passed"
6025 }
6026 run_test 56a "check $LFS getstripe"
6027
6028 test_56b() {
6029         local dir=$DIR/$tdir
6030         local numdirs=3
6031
6032         test_mkdir $dir
6033         for i in $(seq $numdirs); do
6034                 test_mkdir $dir/dir$i
6035         done
6036
6037         # test lfs getdirstripe default mode is non-recursion, which is
6038         # different from lfs getstripe
6039         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
6040
6041         [[ $dircnt -eq 1 ]] ||
6042                 error "$LFS getdirstripe: found $dircnt, not 1"
6043         dircnt=$($LFS getdirstripe --recursive $dir |
6044                 grep -c lmv_stripe_count)
6045         [[ $dircnt -eq $((numdirs + 1)) ]] ||
6046                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
6047 }
6048 run_test 56b "check $LFS getdirstripe"
6049
6050 test_56c() {
6051         remote_ost_nodsh && skip "remote OST with nodsh"
6052
6053         local ost_idx=0
6054         local ost_name=$(ostname_from_index $ost_idx)
6055         local old_status=$(ost_dev_status $ost_idx)
6056         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6057
6058         [[ -z "$old_status" ]] ||
6059                 skip_env "OST $ost_name is in $old_status status"
6060
6061         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
6062         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6063                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
6064         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6065                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
6066                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
6067         fi
6068
6069         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
6070                 error "$LFS df -v showing inactive devices"
6071         sleep_maxage
6072
6073         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
6074
6075         [[ "$new_status" =~ "D" ]] ||
6076                 error "$ost_name status is '$new_status', missing 'D'"
6077         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
6078                 [[ "$new_status" =~ "N" ]] ||
6079                         error "$ost_name status is '$new_status', missing 'N'"
6080         fi
6081         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6082                 [[ "$new_status" =~ "f" ]] ||
6083                         error "$ost_name status is '$new_status', missing 'f'"
6084         fi
6085
6086         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
6087         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6088                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
6089         [[ -z "$p" ]] && restore_lustre_params < $p || true
6090         sleep_maxage
6091
6092         new_status=$(ost_dev_status $ost_idx)
6093         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
6094                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
6095         # can't check 'f' as devices may actually be on flash
6096 }
6097 run_test 56c "check 'lfs df' showing device status"
6098
6099 test_56d() {
6100         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
6101         local osts=$($LFS df -v $MOUNT | grep -c OST)
6102
6103         $LFS df $MOUNT
6104
6105         (( mdts == MDSCOUNT )) ||
6106                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
6107         (( osts == OSTCOUNT )) ||
6108                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
6109 }
6110 run_test 56d "'lfs df -v' prints only configured devices"
6111
6112 NUMFILES=3
6113 NUMDIRS=3
6114 setup_56() {
6115         local local_tdir="$1"
6116         local local_numfiles="$2"
6117         local local_numdirs="$3"
6118         local dir_params="$4"
6119         local dir_stripe_params="$5"
6120
6121         if [ ! -d "$local_tdir" ] ; then
6122                 test_mkdir -p $dir_stripe_params $local_tdir
6123                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
6124                 for i in $(seq $local_numfiles) ; do
6125                         touch $local_tdir/file$i
6126                 done
6127                 for i in $(seq $local_numdirs) ; do
6128                         test_mkdir $dir_stripe_params $local_tdir/dir$i
6129                         for j in $(seq $local_numfiles) ; do
6130                                 touch $local_tdir/dir$i/file$j
6131                         done
6132                 done
6133         fi
6134 }
6135
6136 setup_56_special() {
6137         local local_tdir=$1
6138         local local_numfiles=$2
6139         local local_numdirs=$3
6140
6141         setup_56 $local_tdir $local_numfiles $local_numdirs
6142
6143         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
6144                 for i in $(seq $local_numfiles) ; do
6145                         mknod $local_tdir/loop${i}b b 7 $i
6146                         mknod $local_tdir/null${i}c c 1 3
6147                         ln -s $local_tdir/file1 $local_tdir/link${i}
6148                 done
6149                 for i in $(seq $local_numdirs) ; do
6150                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
6151                         mknod $local_tdir/dir$i/null${i}c c 1 3
6152                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
6153                 done
6154         fi
6155 }
6156
6157 test_56g() {
6158         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6159         local expected=$(($NUMDIRS + 2))
6160
6161         setup_56 $dir $NUMFILES $NUMDIRS
6162
6163         # test lfs find with -name
6164         for i in $(seq $NUMFILES) ; do
6165                 local nums=$($LFS find -name "*$i" $dir | wc -l)
6166
6167                 [ $nums -eq $expected ] ||
6168                         error "lfs find -name '*$i' $dir wrong: "\
6169                               "found $nums, expected $expected"
6170         done
6171 }
6172 run_test 56g "check lfs find -name"
6173
6174 test_56h() {
6175         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6176         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
6177
6178         setup_56 $dir $NUMFILES $NUMDIRS
6179
6180         # test lfs find with ! -name
6181         for i in $(seq $NUMFILES) ; do
6182                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
6183
6184                 [ $nums -eq $expected ] ||
6185                         error "lfs find ! -name '*$i' $dir wrong: "\
6186                               "found $nums, expected $expected"
6187         done
6188 }
6189 run_test 56h "check lfs find ! -name"
6190
6191 test_56i() {
6192         local dir=$DIR/$tdir
6193
6194         test_mkdir $dir
6195
6196         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
6197         local out=$($cmd)
6198
6199         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
6200 }
6201 run_test 56i "check 'lfs find -ost UUID' skips directories"
6202
6203 test_56j() {
6204         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6205
6206         setup_56_special $dir $NUMFILES $NUMDIRS
6207
6208         local expected=$((NUMDIRS + 1))
6209         local cmd="$LFS find -type d $dir"
6210         local nums=$($cmd | wc -l)
6211
6212         [ $nums -eq $expected ] ||
6213                 error "'$cmd' wrong: found $nums, expected $expected"
6214 }
6215 run_test 56j "check lfs find -type d"
6216
6217 test_56k() {
6218         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6219
6220         setup_56_special $dir $NUMFILES $NUMDIRS
6221
6222         local expected=$(((NUMDIRS + 1) * NUMFILES))
6223         local cmd="$LFS find -type f $dir"
6224         local nums=$($cmd | wc -l)
6225
6226         [ $nums -eq $expected ] ||
6227                 error "'$cmd' wrong: found $nums, expected $expected"
6228 }
6229 run_test 56k "check lfs find -type f"
6230
6231 test_56l() {
6232         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6233
6234         setup_56_special $dir $NUMFILES $NUMDIRS
6235
6236         local expected=$((NUMDIRS + NUMFILES))
6237         local cmd="$LFS find -type b $dir"
6238         local nums=$($cmd | wc -l)
6239
6240         [ $nums -eq $expected ] ||
6241                 error "'$cmd' wrong: found $nums, expected $expected"
6242 }
6243 run_test 56l "check lfs find -type b"
6244
6245 test_56m() {
6246         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6247
6248         setup_56_special $dir $NUMFILES $NUMDIRS
6249
6250         local expected=$((NUMDIRS + NUMFILES))
6251         local cmd="$LFS find -type c $dir"
6252         local nums=$($cmd | wc -l)
6253         [ $nums -eq $expected ] ||
6254                 error "'$cmd' wrong: found $nums, expected $expected"
6255 }
6256 run_test 56m "check lfs find -type c"
6257
6258 test_56n() {
6259         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6260         setup_56_special $dir $NUMFILES $NUMDIRS
6261
6262         local expected=$((NUMDIRS + NUMFILES))
6263         local cmd="$LFS find -type l $dir"
6264         local nums=$($cmd | wc -l)
6265
6266         [ $nums -eq $expected ] ||
6267                 error "'$cmd' wrong: found $nums, expected $expected"
6268 }
6269 run_test 56n "check lfs find -type l"
6270
6271 test_56o() {
6272         local dir=$DIR/$tdir
6273
6274         setup_56 $dir $NUMFILES $NUMDIRS
6275         utime $dir/file1 > /dev/null || error "utime (1)"
6276         utime $dir/file2 > /dev/null || error "utime (2)"
6277         utime $dir/dir1 > /dev/null || error "utime (3)"
6278         utime $dir/dir2 > /dev/null || error "utime (4)"
6279         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
6280         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
6281
6282         local expected=4
6283         local nums=$($LFS find -mtime +0 $dir | wc -l)
6284
6285         [ $nums -eq $expected ] ||
6286                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
6287
6288         expected=12
6289         cmd="$LFS find -mtime 0 $dir"
6290         nums=$($cmd | wc -l)
6291         [ $nums -eq $expected ] ||
6292                 error "'$cmd' wrong: found $nums, expected $expected"
6293 }
6294 run_test 56o "check lfs find -mtime for old files"
6295
6296 test_56ob() {
6297         local dir=$DIR/$tdir
6298         local expected=1
6299         local count=0
6300
6301         # just to make sure there is something that won't be found
6302         test_mkdir $dir
6303         touch $dir/$tfile.now
6304
6305         for age in year week day hour min; do
6306                 count=$((count + 1))
6307
6308                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
6309                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
6310                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
6311
6312                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
6313                 local nums=$($cmd | wc -l)
6314                 [ $nums -eq $expected ] ||
6315                         error "'$cmd' wrong: found $nums, expected $expected"
6316
6317                 cmd="$LFS find $dir -atime $count${age:0:1}"
6318                 nums=$($cmd | wc -l)
6319                 [ $nums -eq $expected ] ||
6320                         error "'$cmd' wrong: found $nums, expected $expected"
6321         done
6322
6323         sleep 2
6324         cmd="$LFS find $dir -ctime +1s -type f"
6325         nums=$($cmd | wc -l)
6326         (( $nums == $count * 2 + 1)) ||
6327                 error "'$cmd' wrong: found $nums, expected $((count * 2 + 1))"
6328 }
6329 run_test 56ob "check lfs find -atime -mtime -ctime with units"
6330
6331 test_newerXY_base() {
6332         local x=$1
6333         local y=$2
6334         local dir=$DIR/$tdir
6335         local ref
6336         local negref
6337
6338         if [ $y == "t" ]; then
6339                 if [ $x == "b" ]; then
6340                         ref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6341                 else
6342                         ref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6343                 fi
6344         else
6345                 ref=$DIR/$tfile.newer.$x$y
6346                 touch $ref || error "touch $ref failed"
6347         fi
6348
6349         echo "before = $ref"
6350         sleep 2
6351         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6352         sleep 2
6353         if [ $y == "t" ]; then
6354                 if [ $x == "b" ]; then
6355                         negref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6356                 else
6357                         negref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6358                 fi
6359         else
6360                 negref=$DIR/$tfile.negnewer.$x$y
6361                 touch $negref || error "touch $negref failed"
6362         fi
6363
6364         echo "after = $negref"
6365         local cmd="$LFS find $dir -newer$x$y $ref"
6366         local nums=$(eval $cmd | wc -l)
6367         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
6368
6369         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6370                 error "'$cmd' wrong: found $nums newer, expected $expected"  ; }
6371
6372         cmd="$LFS find $dir ! -newer$x$y $negref"
6373         nums=$(eval $cmd | wc -l)
6374         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6375                 error "'$cmd' wrong: found $nums older, expected $expected"  ; }
6376
6377         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
6378         nums=$(eval $cmd | wc -l)
6379         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6380                 error "'$cmd' wrong: found $nums between, expected $expected"; }
6381
6382         rm -rf $DIR/*
6383 }
6384
6385 test_56oc() {
6386         test_newerXY_base "a" "a"
6387         test_newerXY_base "a" "m"
6388         test_newerXY_base "a" "c"
6389         test_newerXY_base "m" "a"
6390         test_newerXY_base "m" "m"
6391         test_newerXY_base "m" "c"
6392         test_newerXY_base "c" "a"
6393         test_newerXY_base "c" "m"
6394         test_newerXY_base "c" "c"
6395
6396         [[ -n "$sles_version" ]] &&
6397                 echo "skip timestamp tests on SLES, LU-13665" && return 0
6398
6399         test_newerXY_base "a" "t"
6400         test_newerXY_base "m" "t"
6401         test_newerXY_base "c" "t"
6402
6403         [[ $MDS1_VERSION -lt $(version_code 2.13.54) ||
6404            $CLIENT_VERSION -lt $(version_code 2.13.54) ]] &&
6405                 ! btime_supported && echo "btime unsupported" && return 0
6406
6407         test_newerXY_base "b" "b"
6408         test_newerXY_base "b" "t"
6409 }
6410 run_test 56oc "check lfs find -newerXY work"
6411
6412 btime_supported() {
6413         local dir=$DIR/$tdir
6414         local rc
6415
6416         mkdir -p $dir
6417         touch $dir/$tfile
6418         $LFS find $dir -btime -1d -type f
6419         rc=$?
6420         rm -rf $dir
6421         return $rc
6422 }
6423
6424 test_56od() {
6425         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
6426                 ! btime_supported && skip "btime unsupported on MDS"
6427
6428         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6429                 ! btime_supported && skip "btime unsupported on clients"
6430
6431         local dir=$DIR/$tdir
6432         local ref=$DIR/$tfile.ref
6433         local negref=$DIR/$tfile.negref
6434
6435         mkdir $dir || error "mkdir $dir failed"
6436         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6437         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6438         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6439         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6440         touch $ref || error "touch $ref failed"
6441         # sleep 3 seconds at least
6442         sleep 3
6443
6444         local before=$(do_facet mds1 date +%s)
6445         local skew=$(($(date +%s) - before + 1))
6446
6447         if (( skew < 0 && skew > -5 )); then
6448                 sleep $((0 - skew + 1))
6449                 skew=0
6450         fi
6451
6452         # Set the dir stripe params to limit files all on MDT0,
6453         # otherwise we need to calc the max clock skew between
6454         # the client and MDTs.
6455         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6456         sleep 2
6457         touch $negref || error "touch $negref failed"
6458
6459         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6460         local nums=$($cmd | wc -l)
6461         local expected=$(((NUMFILES + 1) * NUMDIRS))
6462
6463         [ $nums -eq $expected ] ||
6464                 error "'$cmd' wrong: found $nums, expected $expected"
6465
6466         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6467         nums=$($cmd | wc -l)
6468         expected=$((NUMFILES + 1))
6469         [ $nums -eq $expected ] ||
6470                 error "'$cmd' wrong: found $nums, expected $expected"
6471
6472         [ $skew -lt 0 ] && return
6473
6474         local after=$(do_facet mds1 date +%s)
6475         local age=$((after - before + 1 + skew))
6476
6477         cmd="$LFS find $dir -btime -${age}s -type f"
6478         nums=$($cmd | wc -l)
6479         expected=$(((NUMFILES + 1) * NUMDIRS))
6480
6481         echo "Clock skew between client and server: $skew, age:$age"
6482         [ $nums -eq $expected ] ||
6483                 error "'$cmd' wrong: found $nums, expected $expected"
6484
6485         expected=$(($NUMDIRS + 1))
6486         cmd="$LFS find $dir -btime -${age}s -type d"
6487         nums=$($cmd | wc -l)
6488         [ $nums -eq $expected ] ||
6489                 error "'$cmd' wrong: found $nums, expected $expected"
6490         rm -f $ref $negref || error "Failed to remove $ref $negref"
6491 }
6492 run_test 56od "check lfs find -btime with units"
6493
6494 test_56p() {
6495         [ $RUNAS_ID -eq $UID ] &&
6496                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6497
6498         local dir=$DIR/$tdir
6499
6500         setup_56 $dir $NUMFILES $NUMDIRS
6501         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6502
6503         local expected=$NUMFILES
6504         local cmd="$LFS find -uid $RUNAS_ID $dir"
6505         local nums=$($cmd | wc -l)
6506
6507         [ $nums -eq $expected ] ||
6508                 error "'$cmd' wrong: found $nums, expected $expected"
6509
6510         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6511         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6512         nums=$($cmd | wc -l)
6513         [ $nums -eq $expected ] ||
6514                 error "'$cmd' wrong: found $nums, expected $expected"
6515 }
6516 run_test 56p "check lfs find -uid and ! -uid"
6517
6518 test_56q() {
6519         [ $RUNAS_ID -eq $UID ] &&
6520                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6521
6522         local dir=$DIR/$tdir
6523
6524         setup_56 $dir $NUMFILES $NUMDIRS
6525         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6526
6527         local expected=$NUMFILES
6528         local cmd="$LFS find -gid $RUNAS_GID $dir"
6529         local nums=$($cmd | wc -l)
6530
6531         [ $nums -eq $expected ] ||
6532                 error "'$cmd' wrong: found $nums, expected $expected"
6533
6534         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6535         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6536         nums=$($cmd | wc -l)
6537         [ $nums -eq $expected ] ||
6538                 error "'$cmd' wrong: found $nums, expected $expected"
6539 }
6540 run_test 56q "check lfs find -gid and ! -gid"
6541
6542 test_56r() {
6543         local dir=$DIR/$tdir
6544
6545         setup_56 $dir $NUMFILES $NUMDIRS
6546
6547         local expected=12
6548         local cmd="$LFS find -size 0 -type f -lazy $dir"
6549         local nums=$($cmd | wc -l)
6550
6551         [ $nums -eq $expected ] ||
6552                 error "'$cmd' wrong: found $nums, expected $expected"
6553         cmd="$LFS find -size 0 -type f $dir"
6554         nums=$($cmd | wc -l)
6555         [ $nums -eq $expected ] ||
6556                 error "'$cmd' wrong: found $nums, expected $expected"
6557
6558         expected=0
6559         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6560         nums=$($cmd | wc -l)
6561         [ $nums -eq $expected ] ||
6562                 error "'$cmd' wrong: found $nums, expected $expected"
6563         cmd="$LFS find ! -size 0 -type f $dir"
6564         nums=$($cmd | wc -l)
6565         [ $nums -eq $expected ] ||
6566                 error "'$cmd' wrong: found $nums, expected $expected"
6567
6568         echo "test" > $dir/$tfile
6569         echo "test2" > $dir/$tfile.2 && sync
6570         expected=1
6571         cmd="$LFS find -size 5 -type f -lazy $dir"
6572         nums=$($cmd | wc -l)
6573         [ $nums -eq $expected ] ||
6574                 error "'$cmd' wrong: found $nums, expected $expected"
6575         cmd="$LFS find -size 5 -type f $dir"
6576         nums=$($cmd | wc -l)
6577         [ $nums -eq $expected ] ||
6578                 error "'$cmd' wrong: found $nums, expected $expected"
6579
6580         expected=1
6581         cmd="$LFS find -size +5 -type f -lazy $dir"
6582         nums=$($cmd | wc -l)
6583         [ $nums -eq $expected ] ||
6584                 error "'$cmd' wrong: found $nums, expected $expected"
6585         cmd="$LFS find -size +5 -type f $dir"
6586         nums=$($cmd | wc -l)
6587         [ $nums -eq $expected ] ||
6588                 error "'$cmd' wrong: found $nums, expected $expected"
6589
6590         expected=2
6591         cmd="$LFS find -size +0 -type f -lazy $dir"
6592         nums=$($cmd | wc -l)
6593         [ $nums -eq $expected ] ||
6594                 error "'$cmd' wrong: found $nums, expected $expected"
6595         cmd="$LFS find -size +0 -type f $dir"
6596         nums=$($cmd | wc -l)
6597         [ $nums -eq $expected ] ||
6598                 error "'$cmd' wrong: found $nums, expected $expected"
6599
6600         expected=2
6601         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6602         nums=$($cmd | wc -l)
6603         [ $nums -eq $expected ] ||
6604                 error "'$cmd' wrong: found $nums, expected $expected"
6605         cmd="$LFS find ! -size -5 -type f $dir"
6606         nums=$($cmd | wc -l)
6607         [ $nums -eq $expected ] ||
6608                 error "'$cmd' wrong: found $nums, expected $expected"
6609
6610         expected=12
6611         cmd="$LFS find -size -5 -type f -lazy $dir"
6612         nums=$($cmd | wc -l)
6613         [ $nums -eq $expected ] ||
6614                 error "'$cmd' wrong: found $nums, expected $expected"
6615         cmd="$LFS find -size -5 -type f $dir"
6616         nums=$($cmd | wc -l)
6617         [ $nums -eq $expected ] ||
6618                 error "'$cmd' wrong: found $nums, expected $expected"
6619 }
6620 run_test 56r "check lfs find -size works"
6621
6622 test_56ra_sub() {
6623         local expected=$1
6624         local glimpses=$2
6625         local cmd="$3"
6626
6627         cancel_lru_locks $OSC
6628
6629         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6630         local nums=$($cmd | wc -l)
6631
6632         [ $nums -eq $expected ] ||
6633                 error "'$cmd' wrong: found $nums, expected $expected"
6634
6635         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6636
6637         if (( rpcs_before + glimpses != rpcs_after )); then
6638                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6639                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6640
6641                 if [[ $glimpses == 0 ]]; then
6642                         error "'$cmd' should not send glimpse RPCs to OST"
6643                 else
6644                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6645                 fi
6646         fi
6647 }
6648
6649 test_56ra() {
6650         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6651                 skip "MDS < 2.12.58 doesn't return LSOM data"
6652         local dir=$DIR/$tdir
6653         local old_agl=$($LCTL get_param -n llite.*.statahead_agl)
6654
6655         [[ $OSC == "mdc" ]] && skip "statahead not needed for DoM files"
6656
6657         # statahead_agl may cause extra glimpse which confuses results. LU-13017
6658         $LCTL set_param -n llite.*.statahead_agl=0
6659         stack_trap "$LCTL set_param -n llite.*.statahead_agl=$old_agl"
6660
6661         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6662         # open and close all files to ensure LSOM is updated
6663         cancel_lru_locks $OSC
6664         find $dir -type f | xargs cat > /dev/null
6665
6666         #   expect_found  glimpse_rpcs  command_to_run
6667         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6668         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6669         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6670         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6671
6672         echo "test" > $dir/$tfile
6673         echo "test2" > $dir/$tfile.2 && sync
6674         cancel_lru_locks $OSC
6675         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6676
6677         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6678         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6679         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6680         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6681
6682         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6683         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6684         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6685         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6686         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6687         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6688 }
6689 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6690
6691 test_56rb() {
6692         local dir=$DIR/$tdir
6693         local tmp=$TMP/$tfile.log
6694         local mdt_idx;
6695
6696         test_mkdir -p $dir || error "failed to mkdir $dir"
6697         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6698                 error "failed to setstripe $dir/$tfile"
6699         mdt_idx=$($LFS getdirstripe -i $dir)
6700         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6701
6702         stack_trap "rm -f $tmp" EXIT
6703         $LFS find --size +100K --ost 0 $dir |& tee $tmp
6704         ! grep -q obd_uuid $tmp ||
6705                 error "failed to find --size +100K --ost 0 $dir"
6706         $LFS find --size +100K --mdt $mdt_idx $dir |& tee $tmp
6707         ! grep -q obd_uuid $tmp ||
6708                 error "failed to find --size +100K --mdt $mdt_idx $dir"
6709 }
6710 run_test 56rb "check lfs find --size --ost/--mdt works"
6711
6712 test_56rc() {
6713         (( MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
6714         local dir=$DIR/$tdir
6715         local found
6716
6717         test_mkdir -c 2 -H all_char $dir || error "failed to mkdir $dir"
6718         $LFS mkdir -c 2 --mdt-hash all_char $dir/$tdir-all{1..10}
6719         (( $MDSCOUNT > 2 )) &&
6720                 $LFS mkdir -c 3 --mdt-hash fnv_1a_64 $dir/$tdir-fnv{1..10}
6721         mkdir $dir/$tdir-{1..10}
6722         touch $dir/$tfile-{1..10}
6723
6724         found=$($LFS find $dir --mdt-count 2 | wc -l)
6725         expect=11
6726         (( $found == $expect )) || error "found $found 2-stripe, expect $expect"
6727
6728         found=$($LFS find $dir -T +1 | wc -l)
6729         (( $MDSCOUNT > 2 )) && expect=$((expect + 10))
6730         (( $found == $expect )) || error "found $found 2+stripe, expect $expect"
6731
6732         found=$($LFS find $dir --mdt-hash all_char | wc -l)
6733         expect=11
6734         (( $found == $expect )) || error "found $found all_char, expect $expect"
6735
6736         found=$($LFS find $dir --mdt-hash fnv_1a_64 | wc -l)
6737         (( $MDSCOUNT > 2 )) && expect=10 || expect=0
6738         (( $found == $expect )) || error "found $found all_char, expect $expect"
6739 }
6740 run_test 56rc "check lfs find --mdt-count/--mdt-hash works"
6741
6742 test_56s() { # LU-611 #LU-9369
6743         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6744
6745         local dir=$DIR/$tdir
6746         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6747
6748         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6749         for i in $(seq $NUMDIRS); do
6750                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6751         done
6752
6753         local expected=$NUMDIRS
6754         local cmd="$LFS find -c $OSTCOUNT $dir"
6755         local nums=$($cmd | wc -l)
6756
6757         [ $nums -eq $expected ] || {
6758                 $LFS getstripe -R $dir
6759                 error "'$cmd' wrong: found $nums, expected $expected"
6760         }
6761
6762         expected=$((NUMDIRS + onestripe))
6763         cmd="$LFS find -stripe-count +0 -type f $dir"
6764         nums=$($cmd | wc -l)
6765         [ $nums -eq $expected ] || {
6766                 $LFS getstripe -R $dir
6767                 error "'$cmd' wrong: found $nums, expected $expected"
6768         }
6769
6770         expected=$onestripe
6771         cmd="$LFS find -stripe-count 1 -type f $dir"
6772         nums=$($cmd | wc -l)
6773         [ $nums -eq $expected ] || {
6774                 $LFS getstripe -R $dir
6775                 error "'$cmd' wrong: found $nums, expected $expected"
6776         }
6777
6778         cmd="$LFS find -stripe-count -2 -type f $dir"
6779         nums=$($cmd | wc -l)
6780         [ $nums -eq $expected ] || {
6781                 $LFS getstripe -R $dir
6782                 error "'$cmd' wrong: found $nums, expected $expected"
6783         }
6784
6785         expected=0
6786         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6787         nums=$($cmd | wc -l)
6788         [ $nums -eq $expected ] || {
6789                 $LFS getstripe -R $dir
6790                 error "'$cmd' wrong: found $nums, expected $expected"
6791         }
6792 }
6793 run_test 56s "check lfs find -stripe-count works"
6794
6795 test_56t() { # LU-611 #LU-9369
6796         local dir=$DIR/$tdir
6797
6798         setup_56 $dir 0 $NUMDIRS
6799         for i in $(seq $NUMDIRS); do
6800                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6801         done
6802
6803         local expected=$NUMDIRS
6804         local cmd="$LFS find -S 8M $dir"
6805         local nums=$($cmd | wc -l)
6806
6807         [ $nums -eq $expected ] || {
6808                 $LFS getstripe -R $dir
6809                 error "'$cmd' wrong: found $nums, expected $expected"
6810         }
6811         rm -rf $dir
6812
6813         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6814
6815         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6816
6817         expected=$(((NUMDIRS + 1) * NUMFILES))
6818         cmd="$LFS find -stripe-size 512k -type f $dir"
6819         nums=$($cmd | wc -l)
6820         [ $nums -eq $expected ] ||
6821                 error "'$cmd' wrong: found $nums, expected $expected"
6822
6823         cmd="$LFS find -stripe-size +320k -type f $dir"
6824         nums=$($cmd | wc -l)
6825         [ $nums -eq $expected ] ||
6826                 error "'$cmd' wrong: found $nums, expected $expected"
6827
6828         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6829         cmd="$LFS find -stripe-size +200k -type f $dir"
6830         nums=$($cmd | wc -l)
6831         [ $nums -eq $expected ] ||
6832                 error "'$cmd' wrong: found $nums, expected $expected"
6833
6834         cmd="$LFS find -stripe-size -640k -type f $dir"
6835         nums=$($cmd | wc -l)
6836         [ $nums -eq $expected ] ||
6837                 error "'$cmd' wrong: found $nums, expected $expected"
6838
6839         expected=4
6840         cmd="$LFS find -stripe-size 256k -type f $dir"
6841         nums=$($cmd | wc -l)
6842         [ $nums -eq $expected ] ||
6843                 error "'$cmd' wrong: found $nums, expected $expected"
6844
6845         cmd="$LFS find -stripe-size -320k -type f $dir"
6846         nums=$($cmd | wc -l)
6847         [ $nums -eq $expected ] ||
6848                 error "'$cmd' wrong: found $nums, expected $expected"
6849
6850         expected=0
6851         cmd="$LFS find -stripe-size 1024k -type f $dir"
6852         nums=$($cmd | wc -l)
6853         [ $nums -eq $expected ] ||
6854                 error "'$cmd' wrong: found $nums, expected $expected"
6855 }
6856 run_test 56t "check lfs find -stripe-size works"
6857
6858 test_56u() { # LU-611
6859         local dir=$DIR/$tdir
6860
6861         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6862
6863         if [[ $OSTCOUNT -gt 1 ]]; then
6864                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6865                 onestripe=4
6866         else
6867                 onestripe=0
6868         fi
6869
6870         local expected=$(((NUMDIRS + 1) * NUMFILES))
6871         local cmd="$LFS find -stripe-index 0 -type f $dir"
6872         local nums=$($cmd | wc -l)
6873
6874         [ $nums -eq $expected ] ||
6875                 error "'$cmd' wrong: found $nums, expected $expected"
6876
6877         expected=$onestripe
6878         cmd="$LFS find -stripe-index 1 -type f $dir"
6879         nums=$($cmd | wc -l)
6880         [ $nums -eq $expected ] ||
6881                 error "'$cmd' wrong: found $nums, expected $expected"
6882
6883         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6884         nums=$($cmd | wc -l)
6885         [ $nums -eq $expected ] ||
6886                 error "'$cmd' wrong: found $nums, expected $expected"
6887
6888         expected=0
6889         # This should produce an error and not return any files
6890         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6891         nums=$($cmd 2>/dev/null | wc -l)
6892         [ $nums -eq $expected ] ||
6893                 error "'$cmd' wrong: found $nums, expected $expected"
6894
6895         if [[ $OSTCOUNT -gt 1 ]]; then
6896                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6897                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6898                 nums=$($cmd | wc -l)
6899                 [ $nums -eq $expected ] ||
6900                         error "'$cmd' wrong: found $nums, expected $expected"
6901         fi
6902 }
6903 run_test 56u "check lfs find -stripe-index works"
6904
6905 test_56v() {
6906         local mdt_idx=0
6907         local dir=$DIR/$tdir
6908
6909         setup_56 $dir $NUMFILES $NUMDIRS
6910
6911         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6912         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6913
6914         for file in $($LFS find -m $UUID $dir); do
6915                 file_midx=$($LFS getstripe -m $file)
6916                 [ $file_midx -eq $mdt_idx ] ||
6917                         error "lfs find -m $UUID != getstripe -m $file_midx"
6918         done
6919 }
6920 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6921
6922 test_56w() {
6923         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6925
6926         local dir=$DIR/$tdir
6927
6928         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6929
6930         local stripe_size=$($LFS getstripe -S -d $dir) ||
6931                 error "$LFS getstripe -S -d $dir failed"
6932         stripe_size=${stripe_size%% *}
6933
6934         local file_size=$((stripe_size * OSTCOUNT))
6935         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6936         local required_space=$((file_num * file_size))
6937         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6938                            head -n1)
6939         [[ $free_space -le $((required_space / 1024)) ]] &&
6940                 skip_env "need $required_space, have $free_space kbytes"
6941
6942         local dd_bs=65536
6943         local dd_count=$((file_size / dd_bs))
6944
6945         # write data into the files
6946         local i
6947         local j
6948         local file
6949
6950         for i in $(seq $NUMFILES); do
6951                 file=$dir/file$i
6952                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6953                         error "write data into $file failed"
6954         done
6955         for i in $(seq $NUMDIRS); do
6956                 for j in $(seq $NUMFILES); do
6957                         file=$dir/dir$i/file$j
6958                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6959                                 error "write data into $file failed"
6960                 done
6961         done
6962
6963         # $LFS_MIGRATE will fail if hard link migration is unsupported
6964         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6965                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6966                         error "creating links to $dir/dir1/file1 failed"
6967         fi
6968
6969         local expected=-1
6970
6971         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6972
6973         # lfs_migrate file
6974         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6975
6976         echo "$cmd"
6977         eval $cmd || error "$cmd failed"
6978
6979         check_stripe_count $dir/file1 $expected
6980
6981         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6982         then
6983                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6984                 # OST 1 if it is on OST 0. This file is small enough to
6985                 # be on only one stripe.
6986                 file=$dir/migr_1_ost
6987                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6988                         error "write data into $file failed"
6989                 local obdidx=$($LFS getstripe -i $file)
6990                 local oldmd5=$(md5sum $file)
6991                 local newobdidx=0
6992
6993                 [[ $obdidx -eq 0 ]] && newobdidx=1
6994                 cmd="$LFS migrate -i $newobdidx $file"
6995                 echo $cmd
6996                 eval $cmd || error "$cmd failed"
6997
6998                 local realobdix=$($LFS getstripe -i $file)
6999                 local newmd5=$(md5sum $file)
7000
7001                 [[ $newobdidx -ne $realobdix ]] &&
7002                         error "new OST is different (was=$obdidx, "\
7003                               "wanted=$newobdidx, got=$realobdix)"
7004                 [[ "$oldmd5" != "$newmd5" ]] &&
7005                         error "md5sum differ: $oldmd5, $newmd5"
7006         fi
7007
7008         # lfs_migrate dir
7009         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
7010         echo "$cmd"
7011         eval $cmd || error "$cmd failed"
7012
7013         for j in $(seq $NUMFILES); do
7014                 check_stripe_count $dir/dir1/file$j $expected
7015         done
7016
7017         # lfs_migrate works with lfs find
7018         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
7019              $LFS_MIGRATE -y -c $expected"
7020         echo "$cmd"
7021         eval $cmd || error "$cmd failed"
7022
7023         for i in $(seq 2 $NUMFILES); do
7024                 check_stripe_count $dir/file$i $expected
7025         done
7026         for i in $(seq 2 $NUMDIRS); do
7027                 for j in $(seq $NUMFILES); do
7028                 check_stripe_count $dir/dir$i/file$j $expected
7029                 done
7030         done
7031 }
7032 run_test 56w "check lfs_migrate -c stripe_count works"
7033
7034 test_56wb() {
7035         local file1=$DIR/$tdir/file1
7036         local create_pool=false
7037         local initial_pool=$($LFS getstripe -p $DIR)
7038         local pool_list=()
7039         local pool=""
7040
7041         echo -n "Creating test dir..."
7042         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7043         echo "done."
7044
7045         echo -n "Creating test file..."
7046         touch $file1 || error "cannot create file"
7047         echo "done."
7048
7049         echo -n "Detecting existing pools..."
7050         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
7051
7052         if [ ${#pool_list[@]} -gt 0 ]; then
7053                 echo "${pool_list[@]}"
7054                 for thispool in "${pool_list[@]}"; do
7055                         if [[ -z "$initial_pool" ||
7056                               "$initial_pool" != "$thispool" ]]; then
7057                                 pool="$thispool"
7058                                 echo "Using existing pool '$pool'"
7059                                 break
7060                         fi
7061                 done
7062         else
7063                 echo "none detected."
7064         fi
7065         if [ -z "$pool" ]; then
7066                 pool=${POOL:-testpool}
7067                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
7068                 echo -n "Creating pool '$pool'..."
7069                 create_pool=true
7070                 pool_add $pool &> /dev/null ||
7071                         error "pool_add failed"
7072                 echo "done."
7073
7074                 echo -n "Adding target to pool..."
7075                 pool_add_targets $pool 0 0 1 &> /dev/null ||
7076                         error "pool_add_targets failed"
7077                 echo "done."
7078         fi
7079
7080         echo -n "Setting pool using -p option..."
7081         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
7082                 error "migrate failed rc = $?"
7083         echo "done."
7084
7085         echo -n "Verifying test file is in pool after migrating..."
7086         [ "$($LFS getstripe -p $file1)" = $pool ] ||
7087                 error "file was not migrated to pool $pool"
7088         echo "done."
7089
7090         echo -n "Removing test file from pool '$pool'..."
7091         # "lfs migrate $file" won't remove the file from the pool
7092         # until some striping information is changed.
7093         $LFS migrate -c 1 $file1 &> /dev/null ||
7094                 error "cannot remove from pool"
7095         [ "$($LFS getstripe -p $file1)" ] &&
7096                 error "pool still set"
7097         echo "done."
7098
7099         echo -n "Setting pool using --pool option..."
7100         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
7101                 error "migrate failed rc = $?"
7102         echo "done."
7103
7104         # Clean up
7105         rm -f $file1
7106         if $create_pool; then
7107                 destroy_test_pools 2> /dev/null ||
7108                         error "destroy test pools failed"
7109         fi
7110 }
7111 run_test 56wb "check lfs_migrate pool support"
7112
7113 test_56wc() {
7114         local file1="$DIR/$tdir/file1"
7115         local parent_ssize
7116         local parent_scount
7117         local cur_ssize
7118         local cur_scount
7119         local orig_ssize
7120
7121         echo -n "Creating test dir..."
7122         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7123         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
7124                 error "cannot set stripe by '-S 1M -c 1'"
7125         echo "done"
7126
7127         echo -n "Setting initial stripe for test file..."
7128         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
7129                 error "cannot set stripe"
7130         cur_ssize=$($LFS getstripe -S "$file1")
7131         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
7132         echo "done."
7133
7134         # File currently set to -S 512K -c 1
7135
7136         # Ensure -c and -S options are rejected when -R is set
7137         echo -n "Verifying incompatible options are detected..."
7138         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
7139                 error "incompatible -c and -R options not detected"
7140         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
7141                 error "incompatible -S and -R options not detected"
7142         echo "done."
7143
7144         # Ensure unrecognized options are passed through to 'lfs migrate'
7145         echo -n "Verifying -S option is passed through to lfs migrate..."
7146         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
7147                 error "migration failed"
7148         cur_ssize=$($LFS getstripe -S "$file1")
7149         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
7150         echo "done."
7151
7152         # File currently set to -S 1M -c 1
7153
7154         # Ensure long options are supported
7155         echo -n "Verifying long options supported..."
7156         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
7157                 error "long option without argument not supported"
7158         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
7159                 error "long option with argument not supported"
7160         cur_ssize=$($LFS getstripe -S "$file1")
7161         [ $cur_ssize -eq 524288 ] ||
7162                 error "migrate --stripe-size $cur_ssize != 524288"
7163         echo "done."
7164
7165         # File currently set to -S 512K -c 1
7166
7167         if [ "$OSTCOUNT" -gt 1 ]; then
7168                 echo -n "Verifying explicit stripe count can be set..."
7169                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
7170                         error "migrate failed"
7171                 cur_scount=$($LFS getstripe -c "$file1")
7172                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
7173                 echo "done."
7174         fi
7175
7176         # File currently set to -S 512K -c 1 or -S 512K -c 2
7177
7178         # Ensure parent striping is used if -R is set, and no stripe
7179         # count or size is specified
7180         echo -n "Setting stripe for parent directory..."
7181         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7182                 error "cannot set stripe '-S 2M -c 1'"
7183         echo "done."
7184
7185         echo -n "Verifying restripe option uses parent stripe settings..."
7186         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
7187         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
7188         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
7189                 error "migrate failed"
7190         cur_ssize=$($LFS getstripe -S "$file1")
7191         [ $cur_ssize -eq $parent_ssize ] ||
7192                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
7193         cur_scount=$($LFS getstripe -c "$file1")
7194         [ $cur_scount -eq $parent_scount ] ||
7195                 error "migrate -R stripe_count $cur_scount != $parent_scount"
7196         echo "done."
7197
7198         # File currently set to -S 1M -c 1
7199
7200         # Ensure striping is preserved if -R is not set, and no stripe
7201         # count or size is specified
7202         echo -n "Verifying striping size preserved when not specified..."
7203         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
7204         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7205                 error "cannot set stripe on parent directory"
7206         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7207                 error "migrate failed"
7208         cur_ssize=$($LFS getstripe -S "$file1")
7209         [ $cur_ssize -eq $orig_ssize ] ||
7210                 error "migrate by default $cur_ssize != $orig_ssize"
7211         echo "done."
7212
7213         # Ensure file name properly detected when final option has no argument
7214         echo -n "Verifying file name properly detected..."
7215         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7216                 error "file name interpreted as option argument"
7217         echo "done."
7218
7219         # Clean up
7220         rm -f "$file1"
7221 }
7222 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
7223
7224 test_56wd() {
7225         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7226
7227         local file1=$DIR/$tdir/file1
7228
7229         echo -n "Creating test dir..."
7230         test_mkdir $DIR/$tdir || error "cannot create dir"
7231         echo "done."
7232
7233         echo -n "Creating test file..."
7234         touch $file1
7235         echo "done."
7236
7237         # Ensure 'lfs migrate' will fail by using a non-existent option,
7238         # and make sure rsync is not called to recover
7239         echo -n "Make sure --no-rsync option works..."
7240         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
7241                 grep -q 'refusing to fall back to rsync' ||
7242                 error "rsync was called with --no-rsync set"
7243         echo "done."
7244
7245         # Ensure rsync is called without trying 'lfs migrate' first
7246         echo -n "Make sure --rsync option works..."
7247         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
7248                 grep -q 'falling back to rsync' &&
7249                 error "lfs migrate was called with --rsync set"
7250         echo "done."
7251
7252         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
7253         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
7254                 grep -q 'at the same time' ||
7255                 error "--rsync and --no-rsync accepted concurrently"
7256         echo "done."
7257
7258         # Clean up
7259         rm -f $file1
7260 }
7261 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
7262
7263 test_56we() {
7264         local td=$DIR/$tdir
7265         local tf=$td/$tfile
7266
7267         test_mkdir $td || error "cannot create $td"
7268         touch $tf || error "cannot touch $tf"
7269
7270         echo -n "Make sure --non-direct|-D works..."
7271         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
7272                 grep -q "lfs migrate --non-direct" ||
7273                 error "--non-direct option cannot work correctly"
7274         $LFS_MIGRATE -y -D -v $tf 2>&1 |
7275                 grep -q "lfs migrate -D" ||
7276                 error "-D option cannot work correctly"
7277         echo "done."
7278 }
7279 run_test 56we "check lfs_migrate --non-direct|-D support"
7280
7281 test_56x() {
7282         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7283         check_swap_layouts_support
7284
7285         local dir=$DIR/$tdir
7286         local ref1=/etc/passwd
7287         local file1=$dir/file1
7288
7289         test_mkdir $dir || error "creating dir $dir"
7290         $LFS setstripe -c 2 $file1
7291         cp $ref1 $file1
7292         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
7293         stripe=$($LFS getstripe -c $file1)
7294         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7295         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7296
7297         # clean up
7298         rm -f $file1
7299 }
7300 run_test 56x "lfs migration support"
7301
7302 test_56xa() {
7303         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7304         check_swap_layouts_support
7305
7306         local dir=$DIR/$tdir/$testnum
7307
7308         test_mkdir -p $dir
7309
7310         local ref1=/etc/passwd
7311         local file1=$dir/file1
7312
7313         $LFS setstripe -c 2 $file1
7314         cp $ref1 $file1
7315         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
7316
7317         local stripe=$($LFS getstripe -c $file1)
7318
7319         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7320         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7321
7322         # clean up
7323         rm -f $file1
7324 }
7325 run_test 56xa "lfs migration --block support"
7326
7327 check_migrate_links() {
7328         local dir="$1"
7329         local file1="$dir/file1"
7330         local begin="$2"
7331         local count="$3"
7332         local runas="$4"
7333         local total_count=$(($begin + $count - 1))
7334         local symlink_count=10
7335         local uniq_count=10
7336
7337         if [ ! -f "$file1" ]; then
7338                 echo -n "creating initial file..."
7339                 $LFS setstripe -c 1 -S "512k" "$file1" ||
7340                         error "cannot setstripe initial file"
7341                 echo "done"
7342
7343                 echo -n "creating symlinks..."
7344                 for s in $(seq 1 $symlink_count); do
7345                         ln -s "$file1" "$dir/slink$s" ||
7346                                 error "cannot create symlinks"
7347                 done
7348                 echo "done"
7349
7350                 echo -n "creating nonlinked files..."
7351                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
7352                         error "cannot create nonlinked files"
7353                 echo "done"
7354         fi
7355
7356         # create hard links
7357         if [ ! -f "$dir/file$total_count" ]; then
7358                 echo -n "creating hard links $begin:$total_count..."
7359                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
7360                         /dev/null || error "cannot create hard links"
7361                 echo "done"
7362         fi
7363
7364         echo -n "checking number of hard links listed in xattrs..."
7365         local fid=$($LFS getstripe -F "$file1")
7366         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
7367
7368         echo "${#paths[*]}"
7369         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
7370                         skip "hard link list has unexpected size, skipping test"
7371         fi
7372         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
7373                         error "link names should exceed xattrs size"
7374         fi
7375
7376         echo -n "migrating files..."
7377         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
7378         local rc=$?
7379         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
7380         echo "done"
7381
7382         # make sure all links have been properly migrated
7383         echo -n "verifying files..."
7384         fid=$($LFS getstripe -F "$file1") ||
7385                 error "cannot get fid for file $file1"
7386         for i in $(seq 2 $total_count); do
7387                 local fid2=$($LFS getstripe -F $dir/file$i)
7388
7389                 [ "$fid2" == "$fid" ] ||
7390                         error "migrated hard link has mismatched FID"
7391         done
7392
7393         # make sure hard links were properly detected, and migration was
7394         # performed only once for the entire link set; nonlinked files should
7395         # also be migrated
7396         local actual=$(grep -c 'done' <<< "$migrate_out")
7397         local expected=$(($uniq_count + 1))
7398
7399         [ "$actual" -eq  "$expected" ] ||
7400                 error "hard links individually migrated ($actual != $expected)"
7401
7402         # make sure the correct number of hard links are present
7403         local hardlinks=$(stat -c '%h' "$file1")
7404
7405         [ $hardlinks -eq $total_count ] ||
7406                 error "num hard links $hardlinks != $total_count"
7407         echo "done"
7408
7409         return 0
7410 }
7411
7412 test_56xb() {
7413         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
7414                 skip "Need MDS version at least 2.10.55"
7415
7416         local dir="$DIR/$tdir"
7417
7418         test_mkdir "$dir" || error "cannot create dir $dir"
7419
7420         echo "testing lfs migrate mode when all links fit within xattrs"
7421         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
7422
7423         echo "testing rsync mode when all links fit within xattrs"
7424         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
7425
7426         echo "testing lfs migrate mode when all links do not fit within xattrs"
7427         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
7428
7429         echo "testing rsync mode when all links do not fit within xattrs"
7430         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
7431
7432         chown -R $RUNAS_ID $dir
7433         echo "testing non-root lfs migrate mode when not all links are in xattr"
7434         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
7435
7436         # clean up
7437         rm -rf $dir
7438 }
7439 run_test 56xb "lfs migration hard link support"
7440
7441 test_56xc() {
7442         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7443
7444         local dir="$DIR/$tdir"
7445
7446         test_mkdir "$dir" || error "cannot create dir $dir"
7447
7448         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7449         echo -n "Setting initial stripe for 20MB test file..."
7450         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7451                 error "cannot setstripe 20MB file"
7452         echo "done"
7453         echo -n "Sizing 20MB test file..."
7454         $TRUNCATE "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7455         echo "done"
7456         echo -n "Verifying small file autostripe count is 1..."
7457         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7458                 error "cannot migrate 20MB file"
7459         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7460                 error "cannot get stripe for $dir/20mb"
7461         [ $stripe_count -eq 1 ] ||
7462                 error "unexpected stripe count $stripe_count for 20MB file"
7463         rm -f "$dir/20mb"
7464         echo "done"
7465
7466         # Test 2: File is small enough to fit within the available space on
7467         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7468         # have at least an additional 1KB for each desired stripe for test 3
7469         echo -n "Setting stripe for 1GB test file..."
7470         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7471         echo "done"
7472         echo -n "Sizing 1GB test file..."
7473         # File size is 1GB + 3KB
7474         $TRUNCATE "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7475         echo "done"
7476
7477         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7478         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7479         if (( avail > 524288 * OSTCOUNT )); then
7480                 echo -n "Migrating 1GB file..."
7481                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7482                         error "cannot migrate 1GB file"
7483                 echo "done"
7484                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7485                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7486                         error "cannot getstripe for 1GB file"
7487                 [ $stripe_count -eq 2 ] ||
7488                         error "unexpected stripe count $stripe_count != 2"
7489                 echo "done"
7490         fi
7491
7492         # Test 3: File is too large to fit within the available space on
7493         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7494         if [ $OSTCOUNT -ge 3 ]; then
7495                 # The required available space is calculated as
7496                 # file size (1GB + 3KB) / OST count (3).
7497                 local kb_per_ost=349526
7498
7499                 echo -n "Migrating 1GB file with limit..."
7500                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7501                         error "cannot migrate 1GB file with limit"
7502                 echo "done"
7503
7504                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7505                 echo -n "Verifying 1GB autostripe count with limited space..."
7506                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7507                         error "unexpected stripe count $stripe_count (min 3)"
7508                 echo "done"
7509         fi
7510
7511         # clean up
7512         rm -rf $dir
7513 }
7514 run_test 56xc "lfs migration autostripe"
7515
7516 test_56xd() {
7517         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7518
7519         local dir=$DIR/$tdir
7520         local f_mgrt=$dir/$tfile.mgrt
7521         local f_yaml=$dir/$tfile.yaml
7522         local f_copy=$dir/$tfile.copy
7523         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7524         local layout_copy="-c 2 -S 2M -i 1"
7525         local yamlfile=$dir/yamlfile
7526         local layout_before;
7527         local layout_after;
7528
7529         test_mkdir "$dir" || error "cannot create dir $dir"
7530         $LFS setstripe $layout_yaml $f_yaml ||
7531                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7532         $LFS getstripe --yaml $f_yaml > $yamlfile
7533         $LFS setstripe $layout_copy $f_copy ||
7534                 error "cannot setstripe $f_copy with layout $layout_copy"
7535         touch $f_mgrt
7536         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7537
7538         # 1. test option --yaml
7539         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7540                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7541         layout_before=$(get_layout_param $f_yaml)
7542         layout_after=$(get_layout_param $f_mgrt)
7543         [ "$layout_after" == "$layout_before" ] ||
7544                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7545
7546         # 2. test option --copy
7547         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7548                 error "cannot migrate $f_mgrt with --copy $f_copy"
7549         layout_before=$(get_layout_param $f_copy)
7550         layout_after=$(get_layout_param $f_mgrt)
7551         [ "$layout_after" == "$layout_before" ] ||
7552                 error "lfs_migrate --copy: $layout_after != $layout_before"
7553 }
7554 run_test 56xd "check lfs_migrate --yaml and --copy support"
7555
7556 test_56xe() {
7557         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7558
7559         local dir=$DIR/$tdir
7560         local f_comp=$dir/$tfile
7561         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7562         local layout_before=""
7563         local layout_after=""
7564
7565         test_mkdir "$dir" || error "cannot create dir $dir"
7566         $LFS setstripe $layout $f_comp ||
7567                 error "cannot setstripe $f_comp with layout $layout"
7568         layout_before=$(get_layout_param $f_comp)
7569         dd if=/dev/zero of=$f_comp bs=1M count=4
7570
7571         # 1. migrate a comp layout file by lfs_migrate
7572         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7573         layout_after=$(get_layout_param $f_comp)
7574         [ "$layout_before" == "$layout_after" ] ||
7575                 error "lfs_migrate: $layout_before != $layout_after"
7576
7577         # 2. migrate a comp layout file by lfs migrate
7578         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7579         layout_after=$(get_layout_param $f_comp)
7580         [ "$layout_before" == "$layout_after" ] ||
7581                 error "lfs migrate: $layout_before != $layout_after"
7582 }
7583 run_test 56xe "migrate a composite layout file"
7584
7585 test_56xf() {
7586         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7587
7588         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7589                 skip "Need server version at least 2.13.53"
7590
7591         local dir=$DIR/$tdir
7592         local f_comp=$dir/$tfile
7593         local layout="-E 1M -c1 -E -1 -c2"
7594         local fid_before=""
7595         local fid_after=""
7596
7597         test_mkdir "$dir" || error "cannot create dir $dir"
7598         $LFS setstripe $layout $f_comp ||
7599                 error "cannot setstripe $f_comp with layout $layout"
7600         fid_before=$($LFS getstripe --fid $f_comp)
7601         dd if=/dev/zero of=$f_comp bs=1M count=4
7602
7603         # 1. migrate a comp layout file to a comp layout
7604         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7605         fid_after=$($LFS getstripe --fid $f_comp)
7606         [ "$fid_before" == "$fid_after" ] ||
7607                 error "comp-to-comp migrate: $fid_before != $fid_after"
7608
7609         # 2. migrate a comp layout file to a plain layout
7610         $LFS migrate -c2 $f_comp ||
7611                 error "cannot migrate $f_comp by lfs migrate"
7612         fid_after=$($LFS getstripe --fid $f_comp)
7613         [ "$fid_before" == "$fid_after" ] ||
7614                 error "comp-to-plain migrate: $fid_before != $fid_after"
7615
7616         # 3. migrate a plain layout file to a comp layout
7617         $LFS migrate $layout $f_comp ||
7618                 error "cannot migrate $f_comp by lfs migrate"
7619         fid_after=$($LFS getstripe --fid $f_comp)
7620         [ "$fid_before" == "$fid_after" ] ||
7621                 error "plain-to-comp migrate: $fid_before != $fid_after"
7622 }
7623 run_test 56xf "FID is not lost during migration of a composite layout file"
7624
7625 test_56y() {
7626         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7627                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7628
7629         local res=""
7630         local dir=$DIR/$tdir
7631         local f1=$dir/file1
7632         local f2=$dir/file2
7633
7634         test_mkdir -p $dir || error "creating dir $dir"
7635         touch $f1 || error "creating std file $f1"
7636         $MULTIOP $f2 H2c || error "creating released file $f2"
7637
7638         # a directory can be raid0, so ask only for files
7639         res=$($LFS find $dir -L raid0 -type f | wc -l)
7640         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7641
7642         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7643         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7644
7645         # only files can be released, so no need to force file search
7646         res=$($LFS find $dir -L released)
7647         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7648
7649         res=$($LFS find $dir -type f \! -L released)
7650         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7651 }
7652 run_test 56y "lfs find -L raid0|released"
7653
7654 test_56z() { # LU-4824
7655         # This checks to make sure 'lfs find' continues after errors
7656         # There are two classes of errors that should be caught:
7657         # - If multiple paths are provided, all should be searched even if one
7658         #   errors out
7659         # - If errors are encountered during the search, it should not terminate
7660         #   early
7661         local dir=$DIR/$tdir
7662         local i
7663
7664         test_mkdir $dir
7665         for i in d{0..9}; do
7666                 test_mkdir $dir/$i
7667                 touch $dir/$i/$tfile
7668         done
7669         $LFS find $DIR/non_existent_dir $dir &&
7670                 error "$LFS find did not return an error"
7671         # Make a directory unsearchable. This should NOT be the last entry in
7672         # directory order.  Arbitrarily pick the 6th entry
7673         chmod 700 $($LFS find $dir -type d | sed '6!d')
7674
7675         $RUNAS $LFS find $DIR/non_existent $dir
7676         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7677
7678         # The user should be able to see 10 directories and 9 files
7679         (( count == 19 )) ||
7680                 error "$LFS find found $count != 19 entries after error"
7681 }
7682 run_test 56z "lfs find should continue after an error"
7683
7684 test_56aa() { # LU-5937
7685         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7686
7687         local dir=$DIR/$tdir
7688
7689         mkdir $dir
7690         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7691
7692         createmany -o $dir/striped_dir/${tfile}- 1024
7693         local dirs=$($LFS find --size +8k $dir/)
7694
7695         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7696 }
7697 run_test 56aa "lfs find --size under striped dir"
7698
7699 test_56ab() { # LU-10705
7700         test_mkdir $DIR/$tdir
7701         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7702         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7703         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7704         # Flush writes to ensure valid blocks.  Need to be more thorough for
7705         # ZFS, since blocks are not allocated/returned to client immediately.
7706         sync_all_data
7707         wait_zfs_commit ost1 2
7708         cancel_lru_locks osc
7709         ls -ls $DIR/$tdir
7710
7711         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7712
7713         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7714
7715         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7716         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7717
7718         rm -f $DIR/$tdir/$tfile.[123]
7719 }
7720 run_test 56ab "lfs find --blocks"
7721
7722 # LU-11188
7723 test_56aca() {
7724         local dir="$DIR/$tdir"
7725         local perms=(001 002 003 004 005 006 007
7726                      010 020 030 040 050 060 070
7727                      100 200 300 400 500 600 700
7728                      111 222 333 444 555 666 777)
7729         local perm_minus=(8 8 4 8 4 4 2
7730                           8 8 4 8 4 4 2
7731                           8 8 4 8 4 4 2
7732                           4 4 2 4 2 2 1)
7733         local perm_slash=(8  8 12  8 12 12 14
7734                           8  8 12  8 12 12 14
7735                           8  8 12  8 12 12 14
7736                          16 16 24 16 24 24 28)
7737
7738         test_mkdir "$dir"
7739         for perm in ${perms[*]}; do
7740                 touch "$dir/$tfile.$perm"
7741                 chmod $perm "$dir/$tfile.$perm"
7742         done
7743
7744         for ((i = 0; i < ${#perms[*]}; i++)); do
7745                 local num=$($LFS find $dir -perm ${perms[i]} | wc -l)
7746                 (( $num == 1 )) ||
7747                         error "lfs find -perm ${perms[i]}:"\
7748                               "$num != 1"
7749
7750                 num=$($LFS find $dir -perm -${perms[i]} -type f| wc -l)
7751                 (( $num == ${perm_minus[i]} )) ||
7752                         error "lfs find -perm -${perms[i]}:"\
7753                               "$num != ${perm_minus[i]}"
7754
7755                 num=$($LFS find $dir -perm /${perms[i]} -type f| wc -l)
7756                 (( $num == ${perm_slash[i]} )) ||
7757                         error "lfs find -perm /${perms[i]}:"\
7758                               "$num != ${perm_slash[i]}"
7759         done
7760 }
7761 run_test 56aca "check lfs find -perm with octal representation"
7762
7763 test_56acb() {
7764         local dir=$DIR/$tdir
7765         # p is the permission of write and execute for user, group and other
7766         # without the umask. It is used to test +wx.
7767         local p=$(printf "%o" "$((0333 & ~$(umask)))")
7768         local perms=(1000 000 2000 4000 $p 644 111 110 100 004)
7769         local symbolic=(+t  a+t u+t g+t o+t
7770                         g+s u+s o+s +s o+sr
7771                         o=r,ug+o,u+w
7772                         u+ g+ o+ a+ ugo+
7773                         u- g- o- a- ugo-
7774                         u= g= o= a= ugo=
7775                         o=r,ug+o,u+w u=r,a+u,u+w
7776                         g=r,ugo=g,u+w u+x,+X +X
7777                         u+x,u+X u+X u+x,g+X o+r,+X
7778                         u+x,go+X +wx +rwx)
7779
7780         test_mkdir $dir
7781         for perm in ${perms[*]}; do
7782                 touch "$dir/$tfile.$perm"
7783                 chmod $perm "$dir/$tfile.$perm"
7784         done
7785
7786         for (( i = 0; i < ${#symbolic[*]}; i++ )); do
7787                 local num=$($LFS find $dir -perm ${symbolic[i]} | wc -l)
7788
7789                 (( $num == 1 )) ||
7790                         error "lfs find $dir -perm ${symbolic[i]}: $num != 1"
7791         done
7792 }
7793 run_test 56acb "check lfs find -perm with symbolic representation"
7794
7795 test_56acc() {
7796         local dir=$DIR/$tdir
7797         local tests="17777 787 789 abcd
7798                 ug=uu ug=a ug=gu uo=ou urw
7799                 u+xg+x a=r,u+x,"
7800
7801         test_mkdir $dir
7802         for err in $tests; do
7803                 if $LFS find $dir -perm $err 2>/dev/null; then
7804                         error "lfs find -perm $err: parsing should have failed"
7805                 fi
7806         done
7807 }
7808 run_test 56acc "check parsing error for lfs find -perm"
7809
7810 test_56ba() {
7811         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7812                 skip "Need MDS version at least 2.10.50"
7813
7814         # Create composite files with one component
7815         local dir=$DIR/$tdir
7816
7817         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7818         # Create composite files with three components
7819         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7820         # Create non-composite files
7821         createmany -o $dir/${tfile}- 10
7822
7823         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7824
7825         [[ $nfiles == 10 ]] ||
7826                 error "lfs find -E 1M found $nfiles != 10 files"
7827
7828         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7829         [[ $nfiles == 25 ]] ||
7830                 error "lfs find ! -E 1M found $nfiles != 25 files"
7831
7832         # All files have a component that starts at 0
7833         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7834         [[ $nfiles == 35 ]] ||
7835                 error "lfs find --component-start 0 - $nfiles != 35 files"
7836
7837         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7838         [[ $nfiles == 15 ]] ||
7839                 error "lfs find --component-start 2M - $nfiles != 15 files"
7840
7841         # All files created here have a componenet that does not starts at 2M
7842         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7843         [[ $nfiles == 35 ]] ||
7844                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7845
7846         # Find files with a specified number of components
7847         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7848         [[ $nfiles == 15 ]] ||
7849                 error "lfs find --component-count 3 - $nfiles != 15 files"
7850
7851         # Remember non-composite files have a component count of zero
7852         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7853         [[ $nfiles == 10 ]] ||
7854                 error "lfs find --component-count 0 - $nfiles != 10 files"
7855
7856         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7857         [[ $nfiles == 20 ]] ||
7858                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
7859
7860         # All files have a flag called "init"
7861         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
7862         [[ $nfiles == 35 ]] ||
7863                 error "lfs find --component-flags init - $nfiles != 35 files"
7864
7865         # Multi-component files will have a component not initialized
7866         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
7867         [[ $nfiles == 15 ]] ||
7868                 error "lfs find !--component-flags init - $nfiles != 15 files"
7869
7870         rm -rf $dir
7871
7872 }
7873 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
7874
7875 test_56ca() {
7876         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7877                 skip "Need MDS version at least 2.10.57"
7878
7879         local td=$DIR/$tdir
7880         local tf=$td/$tfile
7881         local dir
7882         local nfiles
7883         local cmd
7884         local i
7885         local j
7886
7887         # create mirrored directories and mirrored files
7888         mkdir $td || error "mkdir $td failed"
7889         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7890         createmany -o $tf- 10 || error "create $tf- failed"
7891
7892         for i in $(seq 2); do
7893                 dir=$td/dir$i
7894                 mkdir $dir || error "mkdir $dir failed"
7895                 $LFS mirror create -N$((3 + i)) $dir ||
7896                         error "create mirrored dir $dir failed"
7897                 createmany -o $dir/$tfile- 10 ||
7898                         error "create $dir/$tfile- failed"
7899         done
7900
7901         # change the states of some mirrored files
7902         echo foo > $tf-6
7903         for i in $(seq 2); do
7904                 dir=$td/dir$i
7905                 for j in $(seq 4 9); do
7906                         echo foo > $dir/$tfile-$j
7907                 done
7908         done
7909
7910         # find mirrored files with specific mirror count
7911         cmd="$LFS find --mirror-count 3 --type f $td"
7912         nfiles=$($cmd | wc -l)
7913         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7914
7915         cmd="$LFS find ! --mirror-count 3 --type f $td"
7916         nfiles=$($cmd | wc -l)
7917         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7918
7919         cmd="$LFS find --mirror-count +2 --type f $td"
7920         nfiles=$($cmd | wc -l)
7921         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7922
7923         cmd="$LFS find --mirror-count -6 --type f $td"
7924         nfiles=$($cmd | wc -l)
7925         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7926
7927         # find mirrored files with specific file state
7928         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7929         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7930
7931         cmd="$LFS find --mirror-state=ro --type f $td"
7932         nfiles=$($cmd | wc -l)
7933         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7934
7935         cmd="$LFS find ! --mirror-state=ro --type f $td"
7936         nfiles=$($cmd | wc -l)
7937         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7938
7939         cmd="$LFS find --mirror-state=wp --type f $td"
7940         nfiles=$($cmd | wc -l)
7941         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7942
7943         cmd="$LFS find ! --mirror-state=sp --type f $td"
7944         nfiles=$($cmd | wc -l)
7945         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7946 }
7947 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7948
7949 test_56da() { # LU-14179
7950         local path=$DIR/$tdir
7951
7952         test_mkdir $path
7953         cd $path
7954
7955         local longdir=$(str_repeat 'a' 255)
7956
7957         for i in {1..15}; do
7958                 path=$path/$longdir
7959                 test_mkdir $longdir
7960                 cd $longdir
7961         done
7962
7963         local len=${#path}
7964         local lastdir=$(str_repeat 'a' $((4096 - 1 - $len - 1)))
7965
7966         test_mkdir $lastdir
7967         cd $lastdir
7968         # PATH_MAX-1
7969         (( ${#PWD} == 4095 )) || error "bad PWD length ${#PWD}, expect 4095"
7970
7971         # NAME_MAX
7972         touch $(str_repeat 'f' 255)
7973
7974         $LFS find $DIR/$tdir --type d |& grep "lfs find: error" &&
7975                 error "lfs find reported an error"
7976
7977         rm -rf $DIR/$tdir
7978 }
7979 run_test 56da "test lfs find with long paths"
7980
7981 test_57a() {
7982         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7983         # note test will not do anything if MDS is not local
7984         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7985                 skip_env "ldiskfs only test"
7986         fi
7987         remote_mds_nodsh && skip "remote MDS with nodsh"
7988
7989         local MNTDEV="osd*.*MDT*.mntdev"
7990         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7991         [ -z "$DEV" ] && error "can't access $MNTDEV"
7992         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7993                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7994                         error "can't access $DEV"
7995                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7996                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7997                 rm $TMP/t57a.dump
7998         done
7999 }
8000 run_test 57a "verify MDS filesystem created with large inodes =="
8001
8002 test_57b() {
8003         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8004         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8005                 skip_env "ldiskfs only test"
8006         fi
8007         remote_mds_nodsh && skip "remote MDS with nodsh"
8008
8009         local dir=$DIR/$tdir
8010         local filecount=100
8011         local file1=$dir/f1
8012         local fileN=$dir/f$filecount
8013
8014         rm -rf $dir || error "removing $dir"
8015         test_mkdir -c1 $dir
8016         local mdtidx=$($LFS getstripe -m $dir)
8017         local mdtname=MDT$(printf %04x $mdtidx)
8018         local facet=mds$((mdtidx + 1))
8019
8020         echo "mcreating $filecount files"
8021         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
8022
8023         # verify that files do not have EAs yet
8024         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
8025                 error "$file1 has an EA"
8026         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
8027                 error "$fileN has an EA"
8028
8029         sync
8030         sleep 1
8031         df $dir  #make sure we get new statfs data
8032         local mdsfree=$(do_facet $facet \
8033                         lctl get_param -n osd*.*$mdtname.kbytesfree)
8034         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8035         local file
8036
8037         echo "opening files to create objects/EAs"
8038         for file in $(seq -f $dir/f%g 1 $filecount); do
8039                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
8040                         error "opening $file"
8041         done
8042
8043         # verify that files have EAs now
8044         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
8045         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
8046
8047         sleep 1  #make sure we get new statfs data
8048         df $dir
8049         local mdsfree2=$(do_facet $facet \
8050                          lctl get_param -n osd*.*$mdtname.kbytesfree)
8051         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8052
8053         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
8054                 if [ "$mdsfree" != "$mdsfree2" ]; then
8055                         error "MDC before $mdcfree != after $mdcfree2"
8056                 else
8057                         echo "MDC before $mdcfree != after $mdcfree2"
8058                         echo "unable to confirm if MDS has large inodes"
8059                 fi
8060         fi
8061         rm -rf $dir
8062 }
8063 run_test 57b "default LOV EAs are stored inside large inodes ==="
8064
8065 test_58() {
8066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8067         [ -z "$(which wiretest 2>/dev/null)" ] &&
8068                         skip_env "could not find wiretest"
8069
8070         wiretest
8071 }
8072 run_test 58 "verify cross-platform wire constants =============="
8073
8074 test_59() {
8075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8076
8077         echo "touch 130 files"
8078         createmany -o $DIR/f59- 130
8079         echo "rm 130 files"
8080         unlinkmany $DIR/f59- 130
8081         sync
8082         # wait for commitment of removal
8083         wait_delete_completed
8084 }
8085 run_test 59 "verify cancellation of llog records async ========="
8086
8087 TEST60_HEAD="test_60 run $RANDOM"
8088 test_60a() {
8089         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8090         remote_mgs_nodsh && skip "remote MGS with nodsh"
8091         do_facet mgs "! which run-llog.sh &> /dev/null" &&
8092                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
8093                         skip_env "missing subtest run-llog.sh"
8094
8095         log "$TEST60_HEAD - from kernel mode"
8096         do_facet mgs "$LCTL dk > /dev/null"
8097         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
8098         do_facet mgs $LCTL dk > $TMP/$tfile
8099
8100         # LU-6388: test llog_reader
8101         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
8102         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
8103         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
8104                         skip_env "missing llog_reader"
8105         local fstype=$(facet_fstype mgs)
8106         [ $fstype != ldiskfs -a $fstype != zfs ] &&
8107                 skip_env "Only for ldiskfs or zfs type mgs"
8108
8109         local mntpt=$(facet_mntpt mgs)
8110         local mgsdev=$(mgsdevname 1)
8111         local fid_list
8112         local fid
8113         local rec_list
8114         local rec
8115         local rec_type
8116         local obj_file
8117         local path
8118         local seq
8119         local oid
8120         local pass=true
8121
8122         #get fid and record list
8123         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
8124                 tail -n 4))
8125         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
8126                 tail -n 4))
8127         #remount mgs as ldiskfs or zfs type
8128         stop mgs || error "stop mgs failed"
8129         mount_fstype mgs || error "remount mgs failed"
8130         for ((i = 0; i < ${#fid_list[@]}; i++)); do
8131                 fid=${fid_list[i]}
8132                 rec=${rec_list[i]}
8133                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
8134                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
8135                 oid=$((16#$oid))
8136
8137                 case $fstype in
8138                         ldiskfs )
8139                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
8140                         zfs )
8141                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
8142                 esac
8143                 echo "obj_file is $obj_file"
8144                 do_facet mgs $llog_reader $obj_file
8145
8146                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
8147                         awk '{ print $3 }' | sed -e "s/^type=//g")
8148                 if [ $rec_type != $rec ]; then
8149                         echo "FAILED test_60a wrong record type $rec_type," \
8150                               "should be $rec"
8151                         pass=false
8152                         break
8153                 fi
8154
8155                 #check obj path if record type is LLOG_LOGID_MAGIC
8156                 if [ "$rec" == "1064553b" ]; then
8157                         path=$(do_facet mgs $llog_reader $obj_file |
8158                                 grep "path=" | awk '{ print $NF }' |
8159                                 sed -e "s/^path=//g")
8160                         if [ $obj_file != $mntpt/$path ]; then
8161                                 echo "FAILED test_60a wrong obj path" \
8162                                       "$montpt/$path, should be $obj_file"
8163                                 pass=false
8164                                 break
8165                         fi
8166                 fi
8167         done
8168         rm -f $TMP/$tfile
8169         #restart mgs before "error", otherwise it will block the next test
8170         stop mgs || error "stop mgs failed"
8171         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
8172         $pass || error "test failed, see FAILED test_60a messages for specifics"
8173 }
8174 run_test 60a "llog_test run from kernel module and test llog_reader"
8175
8176 test_60b() { # bug 6411
8177         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8178
8179         dmesg > $DIR/$tfile
8180         LLOG_COUNT=$(do_facet mgs dmesg |
8181                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
8182                           /llog_[a-z]*.c:[0-9]/ {
8183                                 if (marker)
8184                                         from_marker++
8185                                 from_begin++
8186                           }
8187                           END {
8188                                 if (marker)
8189                                         print from_marker
8190                                 else
8191                                         print from_begin
8192                           }")
8193
8194         [[ $LLOG_COUNT -gt 120 ]] &&
8195                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
8196 }
8197 run_test 60b "limit repeated messages from CERROR/CWARN"
8198
8199 test_60c() {
8200         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8201
8202         echo "create 5000 files"
8203         createmany -o $DIR/f60c- 5000
8204 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
8205         lctl set_param fail_loc=0x80000137
8206         unlinkmany $DIR/f60c- 5000
8207         lctl set_param fail_loc=0
8208 }
8209 run_test 60c "unlink file when mds full"
8210
8211 test_60d() {
8212         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8213
8214         SAVEPRINTK=$(lctl get_param -n printk)
8215         # verify "lctl mark" is even working"
8216         MESSAGE="test message ID $RANDOM $$"
8217         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8218         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
8219
8220         lctl set_param printk=0 || error "set lnet.printk failed"
8221         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
8222         MESSAGE="new test message ID $RANDOM $$"
8223         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
8224         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8225         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
8226
8227         lctl set_param -n printk="$SAVEPRINTK"
8228 }
8229 run_test 60d "test printk console message masking"
8230
8231 test_60e() {
8232         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8233         remote_mds_nodsh && skip "remote MDS with nodsh"
8234
8235         touch $DIR/$tfile
8236 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
8237         do_facet mds1 lctl set_param fail_loc=0x15b
8238         rm $DIR/$tfile
8239 }
8240 run_test 60e "no space while new llog is being created"
8241
8242 test_60f() {
8243         local old_path=$($LCTL get_param -n debug_path)
8244
8245         stack_trap "$LCTL set_param debug_path=$old_path"
8246         stack_trap "rm -f $TMP/$tfile*"
8247         rm -f $TMP/$tfile* 2> /dev/null
8248         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8249         $LCTL set_param debug_path=$TMP/$tfile fail_loc=0x8000050e
8250         test_mkdir $DIR/$tdir
8251         # retry in case the open is cached and not released
8252         for (( i = 0; i < 100 && $(ls $TMP/$tfile* | wc -l) == 0; i++ )); do
8253                 echo $i > $DIR/$tdir/$tfile.$i && cat $DIR/$tdir/$tfile.$i
8254                 sleep 0.1
8255         done
8256         ls $TMP/$tfile*
8257         (( $(ls $TMP/$tfile* | wc -l) > 0 )) || error "$TMP/$tfile not dumped"
8258 }
8259 run_test 60f "change debug_path works"
8260
8261 test_60g() {
8262         local pid
8263         local i
8264
8265         test_mkdir -c $MDSCOUNT $DIR/$tdir
8266
8267         (
8268                 local index=0
8269                 while true; do
8270                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
8271                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
8272                                 2>/dev/null
8273                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
8274                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
8275                         index=$((index + 1))
8276                 done
8277         ) &
8278
8279         pid=$!
8280
8281         for i in {0..100}; do
8282                 # define OBD_FAIL_OSD_TXN_START    0x19a
8283                 local index=$((i % MDSCOUNT + 1))
8284
8285                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
8286                         > /dev/null
8287                 sleep 0.01
8288         done
8289
8290         kill -9 $pid
8291
8292         for i in $(seq $MDSCOUNT); do
8293                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
8294         done
8295
8296         mkdir $DIR/$tdir/new || error "mkdir failed"
8297         rmdir $DIR/$tdir/new || error "rmdir failed"
8298
8299         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
8300                 -t namespace
8301         for i in $(seq $MDSCOUNT); do
8302                 wait_update_facet mds$i "$LCTL get_param -n \
8303                         mdd.$(facet_svc mds$i).lfsck_namespace |
8304                         awk '/^status/ { print \\\$2 }'" "completed"
8305         done
8306
8307         ls -R $DIR/$tdir || error "ls failed"
8308         rm -rf $DIR/$tdir || error "rmdir failed"
8309 }
8310 run_test 60g "transaction abort won't cause MDT hung"
8311
8312 test_60h() {
8313         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
8314                 skip "Need MDS version at least 2.12.52"
8315         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
8316
8317         local f
8318
8319         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
8320         #define OBD_FAIL_MDS_STRIPE_FID          0x189
8321         for fail_loc in 0x80000188 0x80000189; do
8322                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
8323                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
8324                         error "mkdir $dir-$fail_loc failed"
8325                 for i in {0..10}; do
8326                         # create may fail on missing stripe
8327                         echo $i > $DIR/$tdir-$fail_loc/$i
8328                 done
8329                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8330                         error "getdirstripe $tdir-$fail_loc failed"
8331                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
8332                         error "migrate $tdir-$fail_loc failed"
8333                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8334                         error "getdirstripe $tdir-$fail_loc failed"
8335                 pushd $DIR/$tdir-$fail_loc
8336                 for f in *; do
8337                         echo $f | cmp $f - || error "$f data mismatch"
8338                 done
8339                 popd
8340                 rm -rf $DIR/$tdir-$fail_loc
8341         done
8342 }
8343 run_test 60h "striped directory with missing stripes can be accessed"
8344
8345 function t60i_load() {
8346         mkdir $DIR/$tdir
8347         #define OBD_FAIL_LLOG_PAUSE_AFTER_PAD               0x131c
8348         $LCTL set_param fail_loc=0x131c fail_val=1
8349         for ((i=0; i<5000; i++)); do
8350                 touch $DIR/$tdir/f$i
8351         done
8352 }
8353
8354 test_60i() {
8355         changelog_register || error "changelog_register failed"
8356         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
8357         changelog_users $SINGLEMDS | grep -q $cl_user ||
8358                 error "User $cl_user not found in changelog_users"
8359         changelog_chmask "ALL"
8360         t60i_load &
8361         local PID=$!
8362         for((i=0; i<100; i++)); do
8363                 changelog_dump >/dev/null ||
8364                         error "can't read changelog"
8365         done
8366         kill $PID
8367         wait $PID
8368         changelog_deregister || error "changelog_deregister failed"
8369         $LCTL set_param fail_loc=0
8370 }
8371 run_test 60i "llog: new record vs reader race"
8372
8373 test_61a() {
8374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8375
8376         f="$DIR/f61"
8377         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
8378         cancel_lru_locks osc
8379         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
8380         sync
8381 }
8382 run_test 61a "mmap() writes don't make sync hang ================"
8383
8384 test_61b() {
8385         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
8386 }
8387 run_test 61b "mmap() of unstriped file is successful"
8388
8389 # bug 2330 - insufficient obd_match error checking causes LBUG
8390 test_62() {
8391         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8392
8393         f="$DIR/f62"
8394         echo foo > $f
8395         cancel_lru_locks osc
8396         lctl set_param fail_loc=0x405
8397         cat $f && error "cat succeeded, expect -EIO"
8398         lctl set_param fail_loc=0
8399 }
8400 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
8401 # match every page all of the time.
8402 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
8403
8404 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
8405 # Though this test is irrelevant anymore, it helped to reveal some
8406 # other grant bugs (LU-4482), let's keep it.
8407 test_63a() {   # was test_63
8408         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8409
8410         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
8411
8412         for i in `seq 10` ; do
8413                 dd if=/dev/zero of=$DIR/f63 bs=8k &
8414                 sleep 5
8415                 kill $!
8416                 sleep 1
8417         done
8418
8419         rm -f $DIR/f63 || true
8420 }
8421 run_test 63a "Verify oig_wait interruption does not crash ======="
8422
8423 # bug 2248 - async write errors didn't return to application on sync
8424 # bug 3677 - async write errors left page locked
8425 test_63b() {
8426         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8427
8428         debugsave
8429         lctl set_param debug=-1
8430
8431         # ensure we have a grant to do async writes
8432         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
8433         rm $DIR/$tfile
8434
8435         sync    # sync lest earlier test intercept the fail_loc
8436
8437         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8438         lctl set_param fail_loc=0x80000406
8439         $MULTIOP $DIR/$tfile Owy && \
8440                 error "sync didn't return ENOMEM"
8441         sync; sleep 2; sync     # do a real sync this time to flush page
8442         lctl get_param -n llite.*.dump_page_cache | grep locked && \
8443                 error "locked page left in cache after async error" || true
8444         debugrestore
8445 }
8446 run_test 63b "async write errors should be returned to fsync ==="
8447
8448 test_64a () {
8449         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8450
8451         lfs df $DIR
8452         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
8453 }
8454 run_test 64a "verify filter grant calculations (in kernel) ====="
8455
8456 test_64b () {
8457         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8458
8459         sh oos.sh $MOUNT || error "oos.sh failed: $?"
8460 }
8461 run_test 64b "check out-of-space detection on client"
8462
8463 test_64c() {
8464         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
8465 }
8466 run_test 64c "verify grant shrink"
8467
8468 import_param() {
8469         local tgt=$1
8470         local param=$2
8471
8472         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
8473 }
8474
8475 # this does exactly what osc_request.c:osc_announce_cached() does in
8476 # order to calculate max amount of grants to ask from server
8477 want_grant() {
8478         local tgt=$1
8479
8480         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
8481         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
8482
8483         ((rpc_in_flight++));
8484         nrpages=$((nrpages * rpc_in_flight))
8485
8486         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
8487
8488         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
8489
8490         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
8491         local undirty=$((nrpages * PAGE_SIZE))
8492
8493         local max_extent_pages
8494         max_extent_pages=$(import_param $tgt grant_max_extent_size)
8495         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
8496         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
8497         local grant_extent_tax
8498         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8499
8500         undirty=$((undirty + nrextents * grant_extent_tax))
8501
8502         echo $undirty
8503 }
8504
8505 # this is size of unit for grant allocation. It should be equal to
8506 # what tgt_grant.c:tgt_grant_chunk() calculates
8507 grant_chunk() {
8508         local tgt=$1
8509         local max_brw_size
8510         local grant_extent_tax
8511
8512         max_brw_size=$(import_param $tgt max_brw_size)
8513
8514         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8515
8516         echo $(((max_brw_size + grant_extent_tax) * 2))
8517 }
8518
8519 test_64d() {
8520         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
8521                 skip "OST < 2.10.55 doesn't limit grants enough"
8522
8523         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
8524
8525         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
8526                 skip "no grant_param connect flag"
8527
8528         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8529
8530         $LCTL set_param -n -n debug="$OLDDEBUG" || true
8531         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8532
8533
8534         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
8535         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
8536
8537         $LFS setstripe $DIR/$tfile -i 0 -c 1
8538         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
8539         ddpid=$!
8540
8541         while kill -0 $ddpid; do
8542                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8543
8544                 if [[ $cur_grant -gt $max_cur_granted ]]; then
8545                         kill $ddpid
8546                         error "cur_grant $cur_grant > $max_cur_granted"
8547                 fi
8548
8549                 sleep 1
8550         done
8551 }
8552 run_test 64d "check grant limit exceed"
8553
8554 check_grants() {
8555         local tgt=$1
8556         local expected=$2
8557         local msg=$3
8558         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8559
8560         ((cur_grants == expected)) ||
8561                 error "$msg: grants mismatch: $cur_grants, expected $expected"
8562 }
8563
8564 round_up_p2() {
8565         echo $((($1 + $2 - 1) & ~($2 - 1)))
8566 }
8567
8568 test_64e() {
8569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8570         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
8571                 skip "Need OSS version at least 2.11.56"
8572
8573         # Remount client to reset grant
8574         remount_client $MOUNT || error "failed to remount client"
8575         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8576
8577         local init_grants=$(import_param $osc_tgt initial_grant)
8578
8579         check_grants $osc_tgt $init_grants "init grants"
8580
8581         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8582         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8583         local gbs=$(import_param $osc_tgt grant_block_size)
8584
8585         # write random number of bytes from max_brw_size / 4 to max_brw_size
8586         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
8587         # align for direct io
8588         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
8589         # round to grant consumption unit
8590         local wb_round_up=$(round_up_p2 $write_bytes gbs)
8591
8592         local grants=$((wb_round_up + extent_tax))
8593
8594         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
8595
8596         # define OBD_FAIL_TGT_NO_GRANT 0x725
8597         # make the server not grant more back
8598         do_facet ost1 $LCTL set_param fail_loc=0x725
8599         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
8600
8601         do_facet ost1 $LCTL set_param fail_loc=0
8602
8603         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
8604
8605         rm -f $DIR/$tfile || error "rm failed"
8606
8607         # Remount client to reset grant
8608         remount_client $MOUNT || error "failed to remount client"
8609         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8610
8611         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8612
8613         # define OBD_FAIL_TGT_NO_GRANT 0x725
8614         # make the server not grant more back
8615         do_facet ost1 $LCTL set_param fail_loc=0x725
8616         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
8617         do_facet ost1 $LCTL set_param fail_loc=0
8618
8619         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
8620 }
8621 run_test 64e "check grant consumption (no grant allocation)"
8622
8623 test_64f() {
8624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8625
8626         # Remount client to reset grant
8627         remount_client $MOUNT || error "failed to remount client"
8628         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8629
8630         local init_grants=$(import_param $osc_tgt initial_grant)
8631         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8632         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8633         local gbs=$(import_param $osc_tgt grant_block_size)
8634         local chunk=$(grant_chunk $osc_tgt)
8635
8636         # write random number of bytes from max_brw_size / 4 to max_brw_size
8637         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
8638         # align for direct io
8639         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
8640         # round to grant consumption unit
8641         local wb_round_up=$(round_up_p2 $write_bytes gbs)
8642
8643         local grants=$((wb_round_up + extent_tax))
8644
8645         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8646         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
8647                 error "error writing to $DIR/$tfile"
8648
8649         check_grants $osc_tgt $((init_grants - grants + chunk)) \
8650                 "direct io with grant allocation"
8651
8652         rm -f $DIR/$tfile || error "rm failed"
8653
8654         # Remount client to reset grant
8655         remount_client $MOUNT || error "failed to remount client"
8656         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8657
8658         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8659
8660         local cmd="oO_WRONLY:w${write_bytes}_yc"
8661
8662         $MULTIOP $DIR/$tfile $cmd &
8663         MULTIPID=$!
8664         sleep 1
8665
8666         check_grants $osc_tgt $((init_grants - grants)) \
8667                 "buffered io, not write rpc"
8668
8669         kill -USR1 $MULTIPID
8670         wait
8671
8672         check_grants $osc_tgt $((init_grants - grants + chunk)) \
8673                 "buffered io, one RPC"
8674 }
8675 run_test 64f "check grant consumption (with grant allocation)"
8676
8677 # bug 1414 - set/get directories' stripe info
8678 test_65a() {
8679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8680
8681         test_mkdir $DIR/$tdir
8682         touch $DIR/$tdir/f1
8683         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
8684 }
8685 run_test 65a "directory with no stripe info"
8686
8687 test_65b() {
8688         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8689
8690         test_mkdir $DIR/$tdir
8691         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8692
8693         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8694                                                 error "setstripe"
8695         touch $DIR/$tdir/f2
8696         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
8697 }
8698 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
8699
8700 test_65c() {
8701         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8702         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
8703
8704         test_mkdir $DIR/$tdir
8705         local stripesize=$($LFS getstripe -S $DIR/$tdir)
8706
8707         $LFS setstripe -S $((stripesize * 4)) -i 1 \
8708                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
8709         touch $DIR/$tdir/f3
8710         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
8711 }
8712 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
8713
8714 test_65d() {
8715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8716
8717         test_mkdir $DIR/$tdir
8718         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
8719         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8720
8721         if [[ $STRIPECOUNT -le 0 ]]; then
8722                 sc=1
8723         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
8724                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
8725                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
8726         else
8727                 sc=$(($STRIPECOUNT - 1))
8728         fi
8729         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
8730         touch $DIR/$tdir/f4 $DIR/$tdir/f5
8731         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
8732                 error "lverify failed"
8733 }
8734 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
8735
8736 test_65e() {
8737         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8738
8739         test_mkdir $DIR/$tdir
8740
8741         $LFS setstripe $DIR/$tdir || error "setstripe"
8742         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8743                                         error "no stripe info failed"
8744         touch $DIR/$tdir/f6
8745         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
8746 }
8747 run_test 65e "directory setstripe defaults"
8748
8749 test_65f() {
8750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8751
8752         test_mkdir $DIR/${tdir}f
8753         $RUNAS $LFS setstripe $DIR/${tdir}f &&
8754                 error "setstripe succeeded" || true
8755 }
8756 run_test 65f "dir setstripe permission (should return error) ==="
8757
8758 test_65g() {
8759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8760
8761         test_mkdir $DIR/$tdir
8762         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8763
8764         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8765                 error "setstripe -S failed"
8766         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
8767         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8768                 error "delete default stripe failed"
8769 }
8770 run_test 65g "directory setstripe -d"
8771
8772 test_65h() {
8773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8774
8775         test_mkdir $DIR/$tdir
8776         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8777
8778         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8779                 error "setstripe -S failed"
8780         test_mkdir $DIR/$tdir/dd1
8781         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
8782                 error "stripe info inherit failed"
8783 }
8784 run_test 65h "directory stripe info inherit ===================="
8785
8786 test_65i() {
8787         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8788
8789         save_layout_restore_at_exit $MOUNT
8790
8791         # bug6367: set non-default striping on root directory
8792         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
8793
8794         # bug12836: getstripe on -1 default directory striping
8795         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
8796
8797         # bug12836: getstripe -v on -1 default directory striping
8798         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
8799
8800         # bug12836: new find on -1 default directory striping
8801         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
8802 }
8803 run_test 65i "various tests to set root directory striping"
8804
8805 test_65j() { # bug6367
8806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8807
8808         sync; sleep 1
8809
8810         # if we aren't already remounting for each test, do so for this test
8811         if [ "$I_MOUNTED" = "yes" ]; then
8812                 cleanup || error "failed to unmount"
8813                 setup
8814         fi
8815
8816         save_layout_restore_at_exit $MOUNT
8817
8818         $LFS setstripe -d $MOUNT || error "setstripe failed"
8819 }
8820 run_test 65j "set default striping on root directory (bug 6367)="
8821
8822 cleanup_65k() {
8823         rm -rf $DIR/$tdir
8824         wait_delete_completed
8825         do_facet $SINGLEMDS "lctl set_param -n \
8826                 osp.$ost*MDT0000.max_create_count=$max_count"
8827         do_facet $SINGLEMDS "lctl set_param -n \
8828                 osp.$ost*MDT0000.create_count=$count"
8829         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8830         echo $INACTIVE_OSC "is Activate"
8831
8832         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8833 }
8834
8835 test_65k() { # bug11679
8836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8837         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8838         remote_mds_nodsh && skip "remote MDS with nodsh"
8839
8840         local disable_precreate=true
8841         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
8842                 disable_precreate=false
8843
8844         echo "Check OST status: "
8845         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
8846                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
8847
8848         for OSC in $MDS_OSCS; do
8849                 echo $OSC "is active"
8850                 do_facet $SINGLEMDS lctl --device %$OSC activate
8851         done
8852
8853         for INACTIVE_OSC in $MDS_OSCS; do
8854                 local ost=$(osc_to_ost $INACTIVE_OSC)
8855                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
8856                                lov.*md*.target_obd |
8857                                awk -F: /$ost/'{ print $1 }' | head -n 1)
8858
8859                 mkdir -p $DIR/$tdir
8860                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
8861                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
8862
8863                 echo "Deactivate: " $INACTIVE_OSC
8864                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
8865
8866                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
8867                               osp.$ost*MDT0000.create_count")
8868                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
8869                                   osp.$ost*MDT0000.max_create_count")
8870                 $disable_precreate &&
8871                         do_facet $SINGLEMDS "lctl set_param -n \
8872                                 osp.$ost*MDT0000.max_create_count=0"
8873
8874                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
8875                         [ -f $DIR/$tdir/$idx ] && continue
8876                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
8877                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
8878                                 { cleanup_65k;
8879                                   error "setstripe $idx should succeed"; }
8880                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
8881                 done
8882                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
8883                 rmdir $DIR/$tdir
8884
8885                 do_facet $SINGLEMDS "lctl set_param -n \
8886                         osp.$ost*MDT0000.max_create_count=$max_count"
8887                 do_facet $SINGLEMDS "lctl set_param -n \
8888                         osp.$ost*MDT0000.create_count=$count"
8889                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8890                 echo $INACTIVE_OSC "is Activate"
8891
8892                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8893         done
8894 }
8895 run_test 65k "validate manual striping works properly with deactivated OSCs"
8896
8897 test_65l() { # bug 12836
8898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8899
8900         test_mkdir -p $DIR/$tdir/test_dir
8901         $LFS setstripe -c -1 $DIR/$tdir/test_dir
8902         $LFS find -mtime -1 $DIR/$tdir >/dev/null
8903 }
8904 run_test 65l "lfs find on -1 stripe dir ========================"
8905
8906 test_65m() {
8907         local layout=$(save_layout $MOUNT)
8908         $RUNAS $LFS setstripe -c 2 $MOUNT && {
8909                 restore_layout $MOUNT $layout
8910                 error "setstripe should fail by non-root users"
8911         }
8912         true
8913 }
8914 run_test 65m "normal user can't set filesystem default stripe"
8915
8916 test_65n() {
8917         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
8918         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
8919                 skip "Need MDS version at least 2.12.50"
8920         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
8921
8922         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
8923         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
8924         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
8925
8926         save_layout_restore_at_exit $MOUNT
8927
8928         # new subdirectory under root directory should not inherit
8929         # the default layout from root
8930         local dir1=$MOUNT/$tdir-1
8931         mkdir $dir1 || error "mkdir $dir1 failed"
8932         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
8933                 error "$dir1 shouldn't have LOV EA"
8934
8935         # delete the default layout on root directory
8936         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
8937
8938         local dir2=$MOUNT/$tdir-2
8939         mkdir $dir2 || error "mkdir $dir2 failed"
8940         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
8941                 error "$dir2 shouldn't have LOV EA"
8942
8943         # set a new striping pattern on root directory
8944         local def_stripe_size=$($LFS getstripe -S $MOUNT)
8945         local new_def_stripe_size=$((def_stripe_size * 2))
8946         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
8947                 error "set stripe size on $MOUNT failed"
8948
8949         # new file created in $dir2 should inherit the new stripe size from
8950         # the filesystem default
8951         local file2=$dir2/$tfile-2
8952         touch $file2 || error "touch $file2 failed"
8953
8954         local file2_stripe_size=$($LFS getstripe -S $file2)
8955         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
8956         {
8957                 echo "file2_stripe_size: '$file2_stripe_size'"
8958                 echo "new_def_stripe_size: '$new_def_stripe_size'"
8959                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
8960         }
8961
8962         local dir3=$MOUNT/$tdir-3
8963         mkdir $dir3 || error "mkdir $dir3 failed"
8964         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
8965         # the root layout, which is the actual default layout that will be used
8966         # when new files are created in $dir3.
8967         local dir3_layout=$(get_layout_param $dir3)
8968         local root_dir_layout=$(get_layout_param $MOUNT)
8969         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
8970         {
8971                 echo "dir3_layout: '$dir3_layout'"
8972                 echo "root_dir_layout: '$root_dir_layout'"
8973                 error "$dir3 should show the default layout from $MOUNT"
8974         }
8975
8976         # set OST pool on root directory
8977         local pool=$TESTNAME
8978         pool_add $pool || error "add $pool failed"
8979         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8980                 error "add targets to $pool failed"
8981
8982         $LFS setstripe -p $pool $MOUNT ||
8983                 error "set OST pool on $MOUNT failed"
8984
8985         # new file created in $dir3 should inherit the pool from
8986         # the filesystem default
8987         local file3=$dir3/$tfile-3
8988         touch $file3 || error "touch $file3 failed"
8989
8990         local file3_pool=$($LFS getstripe -p $file3)
8991         [[ "$file3_pool" = "$pool" ]] ||
8992                 error "$file3 ('$file3_pool') didn't inherit OST pool '$pool'"
8993
8994         local dir4=$MOUNT/$tdir-4
8995         mkdir $dir4 || error "mkdir $dir4 failed"
8996         local dir4_layout=$(get_layout_param $dir4)
8997         root_dir_layout=$(get_layout_param $MOUNT)
8998         echo "$LFS getstripe -d $dir4"
8999         $LFS getstripe -d $dir4
9000         echo "$LFS getstripe -d $MOUNT"
9001         $LFS getstripe -d $MOUNT
9002         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
9003         {
9004                 echo "dir4_layout: '$dir4_layout'"
9005                 echo "root_dir_layout: '$root_dir_layout'"
9006                 error "$dir4 should show the default layout from $MOUNT"
9007         }
9008
9009         # new file created in $dir4 should inherit the pool from
9010         # the filesystem default
9011         local file4=$dir4/$tfile-4
9012         touch $file4 || error "touch $file4 failed"
9013
9014         local file4_pool=$($LFS getstripe -p $file4)
9015         [[ "$file4_pool" = "$pool" ]] ||
9016                 error "$file4 ('$file4_pool') didn't inherit OST pool $pool"
9017
9018         # new subdirectory under non-root directory should inherit
9019         # the default layout from its parent directory
9020         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
9021                 error "set directory layout on $dir4 failed"
9022
9023         local dir5=$dir4/$tdir-5
9024         mkdir $dir5 || error "mkdir $dir5 failed"
9025
9026         dir4_layout=$(get_layout_param $dir4)
9027         local dir5_layout=$(get_layout_param $dir5)
9028         [[ "$dir4_layout" = "$dir5_layout" ]] ||
9029         {
9030                 echo "dir4_layout: '$dir4_layout'"
9031                 echo "dir5_layout: '$dir5_layout'"
9032                 error "$dir5 should inherit the default layout from $dir4"
9033         }
9034
9035         # though subdir under ROOT doesn't inherit default layout, but
9036         # its sub dir/file should be created with default layout.
9037         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
9038         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
9039                 skip "Need MDS version at least 2.12.59"
9040
9041         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
9042         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
9043         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
9044
9045         if [ $default_lmv_hash == "none" ]; then
9046                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
9047         else
9048                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
9049                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
9050         fi
9051
9052         $LFS setdirstripe -D -c 2 $MOUNT ||
9053                 error "setdirstripe -D -c 2 failed"
9054         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
9055         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
9056         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
9057 }
9058 run_test 65n "don't inherit default layout from root for new subdirectories"
9059
9060 # bug 2543 - update blocks count on client
9061 test_66() {
9062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9063
9064         COUNT=${COUNT:-8}
9065         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
9066         sync; sync_all_data; sync; sync_all_data
9067         cancel_lru_locks osc
9068         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
9069         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
9070 }
9071 run_test 66 "update inode blocks count on client ==============="
9072
9073 meminfo() {
9074         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
9075 }
9076
9077 swap_used() {
9078         swapon -s | awk '($1 == "'$1'") { print $4 }'
9079 }
9080
9081 # bug5265, obdfilter oa2dentry return -ENOENT
9082 # #define OBD_FAIL_SRV_ENOENT 0x217
9083 test_69() {
9084         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9085         remote_ost_nodsh && skip "remote OST with nodsh"
9086
9087         f="$DIR/$tfile"
9088         $LFS setstripe -c 1 -i 0 $f
9089
9090         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
9091
9092         do_facet ost1 lctl set_param fail_loc=0x217
9093         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
9094         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
9095
9096         do_facet ost1 lctl set_param fail_loc=0
9097         $DIRECTIO write $f 0 2 || error "write error"
9098
9099         cancel_lru_locks osc
9100         $DIRECTIO read $f 0 1 || error "read error"
9101
9102         do_facet ost1 lctl set_param fail_loc=0x217
9103         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
9104
9105         do_facet ost1 lctl set_param fail_loc=0
9106         rm -f $f
9107 }
9108 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
9109
9110 test_71() {
9111         test_mkdir $DIR/$tdir
9112         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
9113         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
9114 }
9115 run_test 71 "Running dbench on lustre (don't segment fault) ===="
9116
9117 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
9118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9119         [ "$RUNAS_ID" = "$UID" ] &&
9120                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9121         # Check that testing environment is properly set up. Skip if not
9122         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
9123                 skip_env "User $RUNAS_ID does not exist - skipping"
9124
9125         touch $DIR/$tfile
9126         chmod 777 $DIR/$tfile
9127         chmod ug+s $DIR/$tfile
9128         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
9129                 error "$RUNAS dd $DIR/$tfile failed"
9130         # See if we are still setuid/sgid
9131         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9132                 error "S/gid is not dropped on write"
9133         # Now test that MDS is updated too
9134         cancel_lru_locks mdc
9135         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9136                 error "S/gid is not dropped on MDS"
9137         rm -f $DIR/$tfile
9138 }
9139 run_test 72a "Test that remove suid works properly (bug5695) ===="
9140
9141 test_72b() { # bug 24226 -- keep mode setting when size is not changing
9142         local perm
9143
9144         [ "$RUNAS_ID" = "$UID" ] &&
9145                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9146         [ "$RUNAS_ID" -eq 0 ] &&
9147                 skip_env "RUNAS_ID = 0 -- skipping"
9148         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9149         # Check that testing environment is properly set up. Skip if not
9150         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
9151                 skip_env "User $RUNAS_ID does not exist - skipping"
9152
9153         touch $DIR/${tfile}-f{g,u}
9154         test_mkdir $DIR/${tfile}-dg
9155         test_mkdir $DIR/${tfile}-du
9156         chmod 770 $DIR/${tfile}-{f,d}{g,u}
9157         chmod g+s $DIR/${tfile}-{f,d}g
9158         chmod u+s $DIR/${tfile}-{f,d}u
9159         for perm in 777 2777 4777; do
9160                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
9161                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
9162                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
9163                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
9164         done
9165         true
9166 }
9167 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
9168
9169 # bug 3462 - multiple simultaneous MDC requests
9170 test_73() {
9171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9172
9173         test_mkdir $DIR/d73-1
9174         test_mkdir $DIR/d73-2
9175         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
9176         pid1=$!
9177
9178         lctl set_param fail_loc=0x80000129
9179         $MULTIOP $DIR/d73-1/f73-2 Oc &
9180         sleep 1
9181         lctl set_param fail_loc=0
9182
9183         $MULTIOP $DIR/d73-2/f73-3 Oc &
9184         pid3=$!
9185
9186         kill -USR1 $pid1
9187         wait $pid1 || return 1
9188
9189         sleep 25
9190
9191         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
9192         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
9193         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
9194
9195         rm -rf $DIR/d73-*
9196 }
9197 run_test 73 "multiple MDC requests (should not deadlock)"
9198
9199 test_74a() { # bug 6149, 6184
9200         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9201
9202         touch $DIR/f74a
9203         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9204         #
9205         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9206         # will spin in a tight reconnection loop
9207         $LCTL set_param fail_loc=0x8000030e
9208         # get any lock that won't be difficult - lookup works.
9209         ls $DIR/f74a
9210         $LCTL set_param fail_loc=0
9211         rm -f $DIR/f74a
9212         true
9213 }
9214 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
9215
9216 test_74b() { # bug 13310
9217         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9218
9219         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9220         #
9221         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9222         # will spin in a tight reconnection loop
9223         $LCTL set_param fail_loc=0x8000030e
9224         # get a "difficult" lock
9225         touch $DIR/f74b
9226         $LCTL set_param fail_loc=0
9227         rm -f $DIR/f74b
9228         true
9229 }
9230 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
9231
9232 test_74c() {
9233         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9234
9235         #define OBD_FAIL_LDLM_NEW_LOCK
9236         $LCTL set_param fail_loc=0x319
9237         touch $DIR/$tfile && error "touch successful"
9238         $LCTL set_param fail_loc=0
9239         true
9240 }
9241 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
9242
9243 slab_lic=/sys/kernel/slab/lustre_inode_cache
9244 num_objects() {
9245         [ -f $slab_lic/shrink ] && echo 1 > $slab_lic/shrink
9246         [ -f $slab_lic/objects ] && awk '{ print $1 }' $slab_lic/objects ||
9247                 awk '/lustre_inode_cache/ { print $2; exit }' /proc/slabinfo
9248 }
9249
9250 test_76a() { # Now for b=20433, added originally in b=1443
9251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9252
9253         cancel_lru_locks osc
9254         # there may be some slab objects cached per core
9255         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
9256         local before=$(num_objects)
9257         local count=$((512 * cpus))
9258         [ "$SLOW" = "no" ] && count=$((128 * cpus))
9259         local margin=$((count / 10))
9260         if [[ -f $slab_lic/aliases ]]; then
9261                 local aliases=$(cat $slab_lic/aliases)
9262                 (( aliases > 0 )) && margin=$((margin * aliases))
9263         fi
9264
9265         echo "before slab objects: $before"
9266         for i in $(seq $count); do
9267                 touch $DIR/$tfile
9268                 rm -f $DIR/$tfile
9269         done
9270         cancel_lru_locks osc
9271         local after=$(num_objects)
9272         echo "created: $count, after slab objects: $after"
9273         # shared slab counts are not very accurate, allow significant margin
9274         # the main goal is that the cache growth is not permanently > $count
9275         while (( after > before + margin )); do
9276                 sleep 1
9277                 after=$(num_objects)
9278                 wait=$((wait + 1))
9279                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9280                 if (( wait > 60 )); then
9281                         error "inode slab grew from $before+$margin to $after"
9282                 fi
9283         done
9284 }
9285 run_test 76a "confirm clients recycle inodes properly ===="
9286
9287 test_76b() {
9288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9289         [ $CLIENT_VERSION -ge $(version_code 2.13.55) ] || skip "not supported"
9290
9291         local count=512
9292         local before=$(num_objects)
9293
9294         for i in $(seq $count); do
9295                 mkdir $DIR/$tdir
9296                 rmdir $DIR/$tdir
9297         done
9298
9299         local after=$(num_objects)
9300         local wait=0
9301
9302         while (( after > before )); do
9303                 sleep 1
9304                 after=$(num_objects)
9305                 wait=$((wait + 1))
9306                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9307                 if (( wait > 60 )); then
9308                         error "inode slab grew from $before to $after"
9309                 fi
9310         done
9311
9312         echo "slab objects before: $before, after: $after"
9313 }
9314 run_test 76b "confirm clients recycle directory inodes properly ===="
9315
9316 export ORIG_CSUM=""
9317 set_checksums()
9318 {
9319         # Note: in sptlrpc modes which enable its own bulk checksum, the
9320         # original crc32_le bulk checksum will be automatically disabled,
9321         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
9322         # will be checked by sptlrpc code against sptlrpc bulk checksum.
9323         # In this case set_checksums() will not be no-op, because sptlrpc
9324         # bulk checksum will be enabled all through the test.
9325
9326         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
9327         lctl set_param -n osc.*.checksums $1
9328         return 0
9329 }
9330
9331 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9332                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
9333 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9334                              tr -d [] | head -n1)}
9335 set_checksum_type()
9336 {
9337         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
9338         rc=$?
9339         log "set checksum type to $1, rc = $rc"
9340         return $rc
9341 }
9342
9343 get_osc_checksum_type()
9344 {
9345         # arugment 1: OST name, like OST0000
9346         ost=$1
9347         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
9348                         sed 's/.*\[\(.*\)\].*/\1/g')
9349         rc=$?
9350         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
9351         echo $checksum_type
9352 }
9353
9354 F77_TMP=$TMP/f77-temp
9355 F77SZ=8
9356 setup_f77() {
9357         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
9358                 error "error writing to $F77_TMP"
9359 }
9360
9361 test_77a() { # bug 10889
9362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9363         $GSS && skip_env "could not run with gss"
9364
9365         [ ! -f $F77_TMP ] && setup_f77
9366         set_checksums 1
9367         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
9368         set_checksums 0
9369         rm -f $DIR/$tfile
9370 }
9371 run_test 77a "normal checksum read/write operation"
9372
9373 test_77b() { # bug 10889
9374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9375         $GSS && skip_env "could not run with gss"
9376
9377         [ ! -f $F77_TMP ] && setup_f77
9378         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9379         $LCTL set_param fail_loc=0x80000409
9380         set_checksums 1
9381
9382         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9383                 error "dd error: $?"
9384         $LCTL set_param fail_loc=0
9385
9386         for algo in $CKSUM_TYPES; do
9387                 cancel_lru_locks osc
9388                 set_checksum_type $algo
9389                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9390                 $LCTL set_param fail_loc=0x80000408
9391                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
9392                 $LCTL set_param fail_loc=0
9393         done
9394         set_checksums 0
9395         set_checksum_type $ORIG_CSUM_TYPE
9396         rm -f $DIR/$tfile
9397 }
9398 run_test 77b "checksum error on client write, read"
9399
9400 cleanup_77c() {
9401         trap 0
9402         set_checksums 0
9403         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
9404         $check_ost &&
9405                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
9406         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
9407         $check_ost && [ -n "$ost_file_prefix" ] &&
9408                 do_facet ost1 rm -f ${ost_file_prefix}\*
9409 }
9410
9411 test_77c() {
9412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9413         $GSS && skip_env "could not run with gss"
9414         remote_ost_nodsh && skip "remote OST with nodsh"
9415
9416         local bad1
9417         local osc_file_prefix
9418         local osc_file
9419         local check_ost=false
9420         local ost_file_prefix
9421         local ost_file
9422         local orig_cksum
9423         local dump_cksum
9424         local fid
9425
9426         # ensure corruption will occur on first OSS/OST
9427         $LFS setstripe -i 0 $DIR/$tfile
9428
9429         [ ! -f $F77_TMP ] && setup_f77
9430         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9431                 error "dd write error: $?"
9432         fid=$($LFS path2fid $DIR/$tfile)
9433
9434         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
9435         then
9436                 check_ost=true
9437                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
9438                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
9439         else
9440                 echo "OSS do not support bulk pages dump upon error"
9441         fi
9442
9443         osc_file_prefix=$($LCTL get_param -n debug_path)
9444         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
9445
9446         trap cleanup_77c EXIT
9447
9448         set_checksums 1
9449         # enable bulk pages dump upon error on Client
9450         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
9451         # enable bulk pages dump upon error on OSS
9452         $check_ost &&
9453                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
9454
9455         # flush Client cache to allow next read to reach OSS
9456         cancel_lru_locks osc
9457
9458         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
9459         $LCTL set_param fail_loc=0x80000408
9460         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
9461         $LCTL set_param fail_loc=0
9462
9463         rm -f $DIR/$tfile
9464
9465         # check cksum dump on Client
9466         osc_file=$(ls ${osc_file_prefix}*)
9467         [ -n "$osc_file" ] || error "no checksum dump file on Client"
9468         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
9469         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
9470         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
9471         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
9472                      cksum)
9473         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
9474         [[ "$orig_cksum" == "$dump_cksum" ]] ||
9475                 error "dump content does not match on Client"
9476
9477         $check_ost || skip "No need to check cksum dump on OSS"
9478
9479         # check cksum dump on OSS
9480         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
9481         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
9482         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
9483         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
9484         [[ "$orig_cksum" == "$dump_cksum" ]] ||
9485                 error "dump content does not match on OSS"
9486
9487         cleanup_77c
9488 }
9489 run_test 77c "checksum error on client read with debug"
9490
9491 test_77d() { # bug 10889
9492         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9493         $GSS && skip_env "could not run with gss"
9494
9495         stack_trap "rm -f $DIR/$tfile"
9496         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9497         $LCTL set_param fail_loc=0x80000409
9498         set_checksums 1
9499         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
9500                 error "direct write: rc=$?"
9501         $LCTL set_param fail_loc=0
9502         set_checksums 0
9503
9504         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9505         $LCTL set_param fail_loc=0x80000408
9506         set_checksums 1
9507         cancel_lru_locks osc
9508         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
9509                 error "direct read: rc=$?"
9510         $LCTL set_param fail_loc=0
9511         set_checksums 0
9512 }
9513 run_test 77d "checksum error on OST direct write, read"
9514
9515 test_77f() { # bug 10889
9516         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9517         $GSS && skip_env "could not run with gss"
9518
9519         set_checksums 1
9520         stack_trap "rm -f $DIR/$tfile"
9521         for algo in $CKSUM_TYPES; do
9522                 cancel_lru_locks osc
9523                 set_checksum_type $algo
9524                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9525                 $LCTL set_param fail_loc=0x409
9526                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
9527                         error "direct write succeeded"
9528                 $LCTL set_param fail_loc=0
9529         done
9530         set_checksum_type $ORIG_CSUM_TYPE
9531         set_checksums 0
9532 }
9533 run_test 77f "repeat checksum error on write (expect error)"
9534
9535 test_77g() { # bug 10889
9536         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9537         $GSS && skip_env "could not run with gss"
9538         remote_ost_nodsh && skip "remote OST with nodsh"
9539
9540         [ ! -f $F77_TMP ] && setup_f77
9541
9542         local file=$DIR/$tfile
9543         stack_trap "rm -f $file" EXIT
9544
9545         $LFS setstripe -c 1 -i 0 $file
9546         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
9547         do_facet ost1 lctl set_param fail_loc=0x8000021a
9548         set_checksums 1
9549         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
9550                 error "write error: rc=$?"
9551         do_facet ost1 lctl set_param fail_loc=0
9552         set_checksums 0
9553
9554         cancel_lru_locks osc
9555         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
9556         do_facet ost1 lctl set_param fail_loc=0x8000021b
9557         set_checksums 1
9558         cmp $F77_TMP $file || error "file compare failed"
9559         do_facet ost1 lctl set_param fail_loc=0
9560         set_checksums 0
9561 }
9562 run_test 77g "checksum error on OST write, read"
9563
9564 test_77k() { # LU-10906
9565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9566         $GSS && skip_env "could not run with gss"
9567
9568         local cksum_param="osc.$FSNAME*.checksums"
9569         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
9570         local checksum
9571         local i
9572
9573         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
9574         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM || true"
9575         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM"
9576
9577         for i in 0 1; do
9578                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
9579                         error "failed to set checksum=$i on MGS"
9580                 wait_update $HOSTNAME "$get_checksum" $i
9581                 #remount
9582                 echo "remount client, checksum should be $i"
9583                 remount_client $MOUNT || error "failed to remount client"
9584                 checksum=$(eval $get_checksum)
9585                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
9586         done
9587         # remove persistent param to avoid races with checksum mountopt below
9588         do_facet mgs $LCTL set_param -P -d $cksum_param ||
9589                 error "failed to delete checksum on MGS"
9590
9591         for opt in "checksum" "nochecksum"; do
9592                 #remount with mount option
9593                 echo "remount client with option $opt, checksum should be $i"
9594                 umount_client $MOUNT || error "failed to umount client"
9595                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
9596                         error "failed to mount client with option '$opt'"
9597                 checksum=$(eval $get_checksum)
9598                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
9599                 i=$((i - 1))
9600         done
9601
9602         remount_client $MOUNT || error "failed to remount client"
9603 }
9604 run_test 77k "enable/disable checksum correctly"
9605
9606 test_77l() {
9607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9608         $GSS && skip_env "could not run with gss"
9609
9610         set_checksums 1
9611         stack_trap "set_checksums $ORIG_CSUM" EXIT
9612         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
9613
9614         set_checksum_type invalid && error "unexpected success of invalid checksum type"
9615
9616         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9617         for algo in $CKSUM_TYPES; do
9618                 set_checksum_type $algo || error "fail to set checksum type $algo"
9619                 osc_algo=$(get_osc_checksum_type OST0000)
9620                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
9621
9622                 # no locks, no reqs to let the connection idle
9623                 cancel_lru_locks osc
9624                 lru_resize_disable osc
9625                 wait_osc_import_state client ost1 IDLE
9626
9627                 # ensure ost1 is connected
9628                 stat $DIR/$tfile >/dev/null || error "can't stat"
9629                 wait_osc_import_state client ost1 FULL
9630
9631                 osc_algo=$(get_osc_checksum_type OST0000)
9632                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
9633         done
9634         return 0
9635 }
9636 run_test 77l "preferred checksum type is remembered after reconnected"
9637
9638 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
9639 rm -f $F77_TMP
9640 unset F77_TMP
9641
9642 test_77m() {
9643         (( $CLIENT_VERSION >= $(version_code 2.14.52) )) ||
9644                 skip "Need at least version 2.14.52"
9645         local param=checksum_speed
9646
9647         $LCTL get_param $param || error "reading $param failed"
9648
9649         csum_speeds=$($LCTL get_param -n $param)
9650
9651         [[ "$csum_speeds" =~ "adler32" && "$csum_speeds" =~ "crc32" ]] ||
9652                 error "known checksum types are missing"
9653 }
9654 run_test 77m "Verify checksum_speed is correctly read"
9655
9656 cleanup_test_78() {
9657         trap 0
9658         rm -f $DIR/$tfile
9659 }
9660
9661 test_78() { # bug 10901
9662         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9663         remote_ost || skip_env "local OST"
9664
9665         NSEQ=5
9666         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
9667         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
9668         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
9669         echo "MemTotal: $MEMTOTAL"
9670
9671         # reserve 256MB of memory for the kernel and other running processes,
9672         # and then take 1/2 of the remaining memory for the read/write buffers.
9673         if [ $MEMTOTAL -gt 512 ] ;then
9674                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
9675         else
9676                 # for those poor memory-starved high-end clusters...
9677                 MEMTOTAL=$((MEMTOTAL / 2))
9678         fi
9679         echo "Mem to use for directio: $MEMTOTAL"
9680
9681         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
9682         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
9683         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
9684         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
9685                 head -n1)
9686         echo "Smallest OST: $SMALLESTOST"
9687         [[ $SMALLESTOST -lt 10240 ]] &&
9688                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
9689
9690         trap cleanup_test_78 EXIT
9691
9692         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
9693                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
9694
9695         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
9696         echo "File size: $F78SIZE"
9697         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
9698         for i in $(seq 1 $NSEQ); do
9699                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
9700                 echo directIO rdwr round $i of $NSEQ
9701                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
9702         done
9703
9704         cleanup_test_78
9705 }
9706 run_test 78 "handle large O_DIRECT writes correctly ============"
9707
9708 test_79() { # bug 12743
9709         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9710
9711         wait_delete_completed
9712
9713         BKTOTAL=$(calc_osc_kbytes kbytestotal)
9714         BKFREE=$(calc_osc_kbytes kbytesfree)
9715         BKAVAIL=$(calc_osc_kbytes kbytesavail)
9716
9717         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
9718         DFTOTAL=`echo $STRING | cut -d, -f1`
9719         DFUSED=`echo $STRING  | cut -d, -f2`
9720         DFAVAIL=`echo $STRING | cut -d, -f3`
9721         DFFREE=$(($DFTOTAL - $DFUSED))
9722
9723         ALLOWANCE=$((64 * $OSTCOUNT))
9724
9725         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
9726            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
9727                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
9728         fi
9729         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
9730            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
9731                 error "df free($DFFREE) mismatch OST free($BKFREE)"
9732         fi
9733         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
9734            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
9735                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
9736         fi
9737 }
9738 run_test 79 "df report consistency check ======================="
9739
9740 test_80() { # bug 10718
9741         remote_ost_nodsh && skip "remote OST with nodsh"
9742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9743
9744         # relax strong synchronous semantics for slow backends like ZFS
9745         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
9746                 local soc="obdfilter.*.sync_lock_cancel"
9747                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
9748
9749                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
9750                 if [ -z "$save" ]; then
9751                         soc="obdfilter.*.sync_on_lock_cancel"
9752                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
9753                 fi
9754
9755                 if [ "$save" != "never" ]; then
9756                         local hosts=$(comma_list $(osts_nodes))
9757
9758                         do_nodes $hosts $LCTL set_param $soc=never
9759                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
9760                 fi
9761         fi
9762
9763         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
9764         sync; sleep 1; sync
9765         local before=$(date +%s)
9766         cancel_lru_locks osc
9767         local after=$(date +%s)
9768         local diff=$((after - before))
9769         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
9770
9771         rm -f $DIR/$tfile
9772 }
9773 run_test 80 "Page eviction is equally fast at high offsets too"
9774
9775 test_81a() { # LU-456
9776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9777         remote_ost_nodsh && skip "remote OST with nodsh"
9778
9779         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
9780         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
9781         do_facet ost1 lctl set_param fail_loc=0x80000228
9782
9783         # write should trigger a retry and success
9784         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9785         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9786         RC=$?
9787         if [ $RC -ne 0 ] ; then
9788                 error "write should success, but failed for $RC"
9789         fi
9790 }
9791 run_test 81a "OST should retry write when get -ENOSPC ==============="
9792
9793 test_81b() { # LU-456
9794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9795         remote_ost_nodsh && skip "remote OST with nodsh"
9796
9797         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
9798         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
9799         do_facet ost1 lctl set_param fail_loc=0x228
9800
9801         # write should retry several times and return -ENOSPC finally
9802         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9803         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9804         RC=$?
9805         ENOSPC=28
9806         if [ $RC -ne $ENOSPC ] ; then
9807                 error "dd should fail for -ENOSPC, but succeed."
9808         fi
9809 }
9810 run_test 81b "OST should return -ENOSPC when retry still fails ======="
9811
9812 test_99() {
9813         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
9814
9815         test_mkdir $DIR/$tdir.cvsroot
9816         chown $RUNAS_ID $DIR/$tdir.cvsroot
9817
9818         cd $TMP
9819         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
9820
9821         cd /etc/init.d
9822         # some versions of cvs import exit(1) when asked to import links or
9823         # files they can't read.  ignore those files.
9824         local toignore=$(find . -type l -printf '-I %f\n' -o \
9825                          ! -perm /4 -printf '-I %f\n')
9826         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
9827                 $tdir.reposname vtag rtag
9828
9829         cd $DIR
9830         test_mkdir $DIR/$tdir.reposname
9831         chown $RUNAS_ID $DIR/$tdir.reposname
9832         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
9833
9834         cd $DIR/$tdir.reposname
9835         $RUNAS touch foo99
9836         $RUNAS cvs add -m 'addmsg' foo99
9837         $RUNAS cvs update
9838         $RUNAS cvs commit -m 'nomsg' foo99
9839         rm -fr $DIR/$tdir.cvsroot
9840 }
9841 run_test 99 "cvs strange file/directory operations"
9842
9843 test_100() {
9844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9845         [[ "$NETTYPE" =~ tcp ]] ||
9846                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
9847         remote_ost_nodsh && skip "remote OST with nodsh"
9848         remote_mds_nodsh && skip "remote MDS with nodsh"
9849         remote_servers ||
9850                 skip "useless for local single node setup"
9851
9852         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
9853                 [ "$PROT" != "tcp" ] && continue
9854                 RPORT=$(echo $REMOTE | cut -d: -f2)
9855                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
9856
9857                 rc=0
9858                 LPORT=`echo $LOCAL | cut -d: -f2`
9859                 if [ $LPORT -ge 1024 ]; then
9860                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
9861                         netstat -tna
9862                         error_exit "local: $LPORT > 1024, remote: $RPORT"
9863                 fi
9864         done
9865         [ "$rc" = 0 ] || error_exit "privileged port not found" )
9866 }
9867 run_test 100 "check local port using privileged port ==========="
9868
9869 function get_named_value()
9870 {
9871     local tag=$1
9872
9873     grep -w "$tag" | sed "s/^$tag  *\([0-9]*\)  *.*/\1/"
9874 }
9875
9876 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
9877                    awk '/^max_cached_mb/ { print $2 }')
9878
9879 cleanup_101a() {
9880         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
9881         trap 0
9882 }
9883
9884 test_101a() {
9885         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9886
9887         local s
9888         local discard
9889         local nreads=10000
9890         local cache_limit=32
9891
9892         $LCTL set_param -n osc.*-osc*.rpc_stats=0
9893         trap cleanup_101a EXIT
9894         $LCTL set_param -n llite.*.read_ahead_stats=0
9895         $LCTL set_param -n llite.*.max_cached_mb=$cache_limit
9896
9897         #
9898         # randomly read 10000 of 64K chunks from file 3x 32MB in size
9899         #
9900         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
9901         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
9902
9903         discard=0
9904         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
9905                    get_named_value 'read.but.discarded'); do
9906                         discard=$(($discard + $s))
9907         done
9908         cleanup_101a
9909
9910         $LCTL get_param osc.*-osc*.rpc_stats
9911         $LCTL get_param llite.*.read_ahead_stats
9912
9913         # Discard is generally zero, but sometimes a few random reads line up
9914         # and trigger larger readahead, which is wasted & leads to discards.
9915         if [[ $(($discard)) -gt $nreads ]]; then
9916                 error "too many ($discard) discarded pages"
9917         fi
9918         rm -f $DIR/$tfile || true
9919 }
9920 run_test 101a "check read-ahead for random reads"
9921
9922 setup_test101bc() {
9923         test_mkdir $DIR/$tdir
9924         local ssize=$1
9925         local FILE_LENGTH=$2
9926         STRIPE_OFFSET=0
9927
9928         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
9929
9930         local list=$(comma_list $(osts_nodes))
9931         set_osd_param $list '' read_cache_enable 0
9932         set_osd_param $list '' writethrough_cache_enable 0
9933
9934         trap cleanup_test101bc EXIT
9935         # prepare the read-ahead file
9936         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
9937
9938         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
9939                                 count=$FILE_SIZE_MB 2> /dev/null
9940
9941 }
9942
9943 cleanup_test101bc() {
9944         trap 0
9945         rm -rf $DIR/$tdir
9946         rm -f $DIR/$tfile
9947
9948         local list=$(comma_list $(osts_nodes))
9949         set_osd_param $list '' read_cache_enable 1
9950         set_osd_param $list '' writethrough_cache_enable 1
9951 }
9952
9953 calc_total() {
9954         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
9955 }
9956
9957 ra_check_101() {
9958         local READ_SIZE=$1
9959         local STRIPE_SIZE=$2
9960         local FILE_LENGTH=$3
9961         local RA_INC=1048576
9962         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
9963         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
9964                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
9965         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
9966                   get_named_value 'read.but.discarded' | calc_total)
9967         if [[ $DISCARD -gt $discard_limit ]]; then
9968                 $LCTL get_param llite.*.read_ahead_stats
9969                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
9970         else
9971                 echo "Read-ahead success for size ${READ_SIZE}"
9972         fi
9973 }
9974
9975 test_101b() {
9976         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9977         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9978
9979         local STRIPE_SIZE=1048576
9980         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
9981
9982         if [ $SLOW == "yes" ]; then
9983                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
9984         else
9985                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
9986         fi
9987
9988         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
9989
9990         # prepare the read-ahead file
9991         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9992         cancel_lru_locks osc
9993         for BIDX in 2 4 8 16 32 64 128 256
9994         do
9995                 local BSIZE=$((BIDX*4096))
9996                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
9997                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
9998                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
9999                 $LCTL set_param -n llite.*.read_ahead_stats=0
10000                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
10001                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
10002                 cancel_lru_locks osc
10003                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
10004         done
10005         cleanup_test101bc
10006         true
10007 }
10008 run_test 101b "check stride-io mode read-ahead ================="
10009
10010 test_101c() {
10011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10012
10013         local STRIPE_SIZE=1048576
10014         local FILE_LENGTH=$((STRIPE_SIZE*100))
10015         local nreads=10000
10016         local rsize=65536
10017         local osc_rpc_stats
10018
10019         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10020
10021         cancel_lru_locks osc
10022         $LCTL set_param osc.*.rpc_stats=0
10023         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
10024         $LCTL get_param osc.*.rpc_stats
10025         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
10026                 local stats=$($LCTL get_param -n $osc_rpc_stats)
10027                 local lines=$(echo "$stats" | awk 'END {print NR;}')
10028                 local size
10029
10030                 if [ $lines -le 20 ]; then
10031                         echo "continue debug"
10032                         continue
10033                 fi
10034                 for size in 1 2 4 8; do
10035                         local rpc=$(echo "$stats" |
10036                                     awk '($1 == "'$size':") {print $2; exit; }')
10037                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
10038                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
10039                 done
10040                 echo "$osc_rpc_stats check passed!"
10041         done
10042         cleanup_test101bc
10043         true
10044 }
10045 run_test 101c "check stripe_size aligned read-ahead"
10046
10047 test_101d() {
10048         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10049
10050         local file=$DIR/$tfile
10051         local sz_MB=${FILESIZE_101d:-80}
10052         local ra_MB=${READAHEAD_MB:-40}
10053
10054         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
10055         [ $free_MB -lt $sz_MB ] &&
10056                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
10057
10058         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
10059         $LFS setstripe -c -1 $file || error "setstripe failed"
10060
10061         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
10062         echo Cancel LRU locks on lustre client to flush the client cache
10063         cancel_lru_locks osc
10064
10065         echo Disable read-ahead
10066         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10067         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10068         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb=$old_RA" EXIT
10069         $LCTL get_param -n llite.*.max_read_ahead_mb
10070
10071         echo "Reading the test file $file with read-ahead disabled"
10072         local sz_KB=$((sz_MB * 1024 / 4))
10073         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
10074         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
10075         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10076                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10077
10078         echo "Cancel LRU locks on lustre client to flush the client cache"
10079         cancel_lru_locks osc
10080         echo Enable read-ahead with ${ra_MB}MB
10081         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
10082
10083         echo "Reading the test file $file with read-ahead enabled"
10084         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10085                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10086
10087         echo "read-ahead disabled time read $raOFF"
10088         echo "read-ahead enabled time read $raON"
10089
10090         rm -f $file
10091         wait_delete_completed
10092
10093         # use awk for this check instead of bash because it handles decimals
10094         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
10095                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
10096 }
10097 run_test 101d "file read with and without read-ahead enabled"
10098
10099 test_101e() {
10100         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10101
10102         local file=$DIR/$tfile
10103         local size_KB=500  #KB
10104         local count=100
10105         local bsize=1024
10106
10107         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
10108         local need_KB=$((count * size_KB))
10109         [[ $free_KB -le $need_KB ]] &&
10110                 skip_env "Need free space $need_KB, have $free_KB"
10111
10112         echo "Creating $count ${size_KB}K test files"
10113         for ((i = 0; i < $count; i++)); do
10114                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
10115         done
10116
10117         echo "Cancel LRU locks on lustre client to flush the client cache"
10118         cancel_lru_locks $OSC
10119
10120         echo "Reset readahead stats"
10121         $LCTL set_param -n llite.*.read_ahead_stats=0
10122
10123         for ((i = 0; i < $count; i++)); do
10124                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
10125         done
10126
10127         $LCTL get_param llite.*.max_cached_mb
10128         $LCTL get_param llite.*.read_ahead_stats
10129         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10130                      get_named_value 'misses' | calc_total)
10131
10132         for ((i = 0; i < $count; i++)); do
10133                 rm -rf $file.$i 2>/dev/null
10134         done
10135
10136         #10000 means 20% reads are missing in readahead
10137         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
10138 }
10139 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
10140
10141 test_101f() {
10142         which iozone || skip_env "no iozone installed"
10143
10144         local old_debug=$($LCTL get_param debug)
10145         old_debug=${old_debug#*=}
10146         $LCTL set_param debug="reada mmap"
10147
10148         # create a test file
10149         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
10150
10151         echo Cancel LRU locks on lustre client to flush the client cache
10152         cancel_lru_locks osc
10153
10154         echo Reset readahead stats
10155         $LCTL set_param -n llite.*.read_ahead_stats=0
10156
10157         echo mmap read the file with small block size
10158         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
10159                 > /dev/null 2>&1
10160
10161         echo checking missing pages
10162         $LCTL get_param llite.*.read_ahead_stats
10163         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10164                         get_named_value 'misses' | calc_total)
10165
10166         $LCTL set_param debug="$old_debug"
10167         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
10168         rm -f $DIR/$tfile
10169 }
10170 run_test 101f "check mmap read performance"
10171
10172 test_101g_brw_size_test() {
10173         local mb=$1
10174         local pages=$((mb * 1048576 / PAGE_SIZE))
10175         local file=$DIR/$tfile
10176
10177         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
10178                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
10179         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
10180                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
10181                         return 2
10182         done
10183
10184         stack_trap "rm -f $file" EXIT
10185         $LCTL set_param -n osc.*.rpc_stats=0
10186
10187         # 10 RPCs should be enough for the test
10188         local count=10
10189         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
10190                 { error "dd write ${mb} MB blocks failed"; return 3; }
10191         cancel_lru_locks osc
10192         dd of=/dev/null if=$file bs=${mb}M count=$count ||
10193                 { error "dd write ${mb} MB blocks failed"; return 4; }
10194
10195         # calculate number of full-sized read and write RPCs
10196         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
10197                 sed -n '/pages per rpc/,/^$/p' |
10198                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
10199                 END { print reads,writes }'))
10200         # allow one extra full-sized read RPC for async readahead
10201         [[ ${rpcs[0]} == $count || ${rpcs[0]} == $((count + 1)) ]] ||
10202                 { error "${rpcs[0]} != $count read RPCs"; return 5; }
10203         [[ ${rpcs[1]} == $count ]] ||
10204                 { error "${rpcs[1]} != $count write RPCs"; return 6; }
10205 }
10206
10207 test_101g() {
10208         remote_ost_nodsh && skip "remote OST with nodsh"
10209
10210         local rpcs
10211         local osts=$(get_facets OST)
10212         local list=$(comma_list $(osts_nodes))
10213         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10214         local brw_size="obdfilter.*.brw_size"
10215
10216         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10217
10218         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
10219
10220         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
10221                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
10222                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
10223            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
10224                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
10225                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
10226
10227                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
10228                         suffix="M"
10229
10230                 if [[ $orig_mb -lt 16 ]]; then
10231                         save_lustre_params $osts "$brw_size" > $p
10232                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
10233                                 error "set 16MB RPC size failed"
10234
10235                         echo "remount client to enable new RPC size"
10236                         remount_client $MOUNT || error "remount_client failed"
10237                 fi
10238
10239                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
10240                 # should be able to set brw_size=12, but no rpc_stats for that
10241                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
10242         fi
10243
10244         test_101g_brw_size_test 4 || error "4MB RPC test failed"
10245
10246         if [[ $orig_mb -lt 16 ]]; then
10247                 restore_lustre_params < $p
10248                 remount_client $MOUNT || error "remount_client restore failed"
10249         fi
10250
10251         rm -f $p $DIR/$tfile
10252 }
10253 run_test 101g "Big bulk(4/16 MiB) readahead"
10254
10255 test_101h() {
10256         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10257
10258         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
10259                 error "dd 70M file failed"
10260         echo Cancel LRU locks on lustre client to flush the client cache
10261         cancel_lru_locks osc
10262
10263         echo "Reset readahead stats"
10264         $LCTL set_param -n llite.*.read_ahead_stats 0
10265
10266         echo "Read 10M of data but cross 64M bundary"
10267         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
10268         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10269                      get_named_value 'misses' | calc_total)
10270         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
10271         rm -f $p $DIR/$tfile
10272 }
10273 run_test 101h "Readahead should cover current read window"
10274
10275 test_101i() {
10276         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
10277                 error "dd 10M file failed"
10278
10279         local max_per_file_mb=$($LCTL get_param -n \
10280                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
10281         cancel_lru_locks osc
10282         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
10283         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
10284                 error "set max_read_ahead_per_file_mb to 1 failed"
10285
10286         echo "Reset readahead stats"
10287         $LCTL set_param llite.*.read_ahead_stats=0
10288
10289         dd if=$DIR/$tfile of=/dev/null bs=2M
10290
10291         $LCTL get_param llite.*.read_ahead_stats
10292         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10293                      awk '/misses/ { print $2 }')
10294         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
10295         rm -f $DIR/$tfile
10296 }
10297 run_test 101i "allow current readahead to exceed reservation"
10298
10299 test_101j() {
10300         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
10301                 error "setstripe $DIR/$tfile failed"
10302         local file_size=$((1048576 * 16))
10303         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10304         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
10305
10306         echo Disable read-ahead
10307         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10308
10309         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
10310         for blk in $PAGE_SIZE 1048576 $file_size; do
10311                 cancel_lru_locks osc
10312                 echo "Reset readahead stats"
10313                 $LCTL set_param -n llite.*.read_ahead_stats=0
10314                 local count=$(($file_size / $blk))
10315                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
10316                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10317                              get_named_value 'failed.to.fast.read' | calc_total)
10318                 $LCTL get_param -n llite.*.read_ahead_stats
10319                 [ $miss -eq $count ] || error "expected $count got $miss"
10320         done
10321
10322         rm -f $p $DIR/$tfile
10323 }
10324 run_test 101j "A complete read block should be submitted when no RA"
10325
10326 setup_test102() {
10327         test_mkdir $DIR/$tdir
10328         chown $RUNAS_ID $DIR/$tdir
10329         STRIPE_SIZE=65536
10330         STRIPE_OFFSET=1
10331         STRIPE_COUNT=$OSTCOUNT
10332         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
10333
10334         trap cleanup_test102 EXIT
10335         cd $DIR
10336         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
10337         cd $DIR/$tdir
10338         for num in 1 2 3 4; do
10339                 for count in $(seq 1 $STRIPE_COUNT); do
10340                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
10341                                 local size=`expr $STRIPE_SIZE \* $num`
10342                                 local file=file"$num-$idx-$count"
10343                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
10344                         done
10345                 done
10346         done
10347
10348         cd $DIR
10349         $1 tar cf $TMP/f102.tar $tdir --xattrs
10350 }
10351
10352 cleanup_test102() {
10353         trap 0
10354         rm -f $TMP/f102.tar
10355         rm -rf $DIR/d0.sanity/d102
10356 }
10357
10358 test_102a() {
10359         [ "$UID" != 0 ] && skip "must run as root"
10360         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
10361                 skip_env "must have user_xattr"
10362
10363         [ -z "$(which setfattr 2>/dev/null)" ] &&
10364                 skip_env "could not find setfattr"
10365
10366         local testfile=$DIR/$tfile
10367
10368         touch $testfile
10369         echo "set/get xattr..."
10370         setfattr -n trusted.name1 -v value1 $testfile ||
10371                 error "setfattr -n trusted.name1=value1 $testfile failed"
10372         getfattr -n trusted.name1 $testfile 2> /dev/null |
10373           grep "trusted.name1=.value1" ||
10374                 error "$testfile missing trusted.name1=value1"
10375
10376         setfattr -n user.author1 -v author1 $testfile ||
10377                 error "setfattr -n user.author1=author1 $testfile failed"
10378         getfattr -n user.author1 $testfile 2> /dev/null |
10379           grep "user.author1=.author1" ||
10380                 error "$testfile missing trusted.author1=author1"
10381
10382         echo "listxattr..."
10383         setfattr -n trusted.name2 -v value2 $testfile ||
10384                 error "$testfile unable to set trusted.name2"
10385         setfattr -n trusted.name3 -v value3 $testfile ||
10386                 error "$testfile unable to set trusted.name3"
10387         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
10388             grep "trusted.name" | wc -l) -eq 3 ] ||
10389                 error "$testfile missing 3 trusted.name xattrs"
10390
10391         setfattr -n user.author2 -v author2 $testfile ||
10392                 error "$testfile unable to set user.author2"
10393         setfattr -n user.author3 -v author3 $testfile ||
10394                 error "$testfile unable to set user.author3"
10395         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
10396             grep "user.author" | wc -l) -eq 3 ] ||
10397                 error "$testfile missing 3 user.author xattrs"
10398
10399         echo "remove xattr..."
10400         setfattr -x trusted.name1 $testfile ||
10401                 error "$testfile error deleting trusted.name1"
10402         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
10403                 error "$testfile did not delete trusted.name1 xattr"
10404
10405         setfattr -x user.author1 $testfile ||
10406                 error "$testfile error deleting user.author1"
10407         echo "set lustre special xattr ..."
10408         $LFS setstripe -c1 $testfile
10409         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
10410                 awk -F "=" '/trusted.lov/ { print $2 }' )
10411         setfattr -n "trusted.lov" -v $lovea $testfile ||
10412                 error "$testfile doesn't ignore setting trusted.lov again"
10413         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
10414                 error "$testfile allow setting invalid trusted.lov"
10415         rm -f $testfile
10416 }
10417 run_test 102a "user xattr test =================================="
10418
10419 check_102b_layout() {
10420         local layout="$*"
10421         local testfile=$DIR/$tfile
10422
10423         echo "test layout '$layout'"
10424         $LFS setstripe $layout $testfile || error "setstripe failed"
10425         $LFS getstripe -y $testfile
10426
10427         echo "get/set/list trusted.lov xattr ..." # b=10930
10428         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
10429         [[ "$value" =~ "trusted.lov" ]] ||
10430                 error "can't get trusted.lov from $testfile"
10431         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
10432                 error "getstripe failed"
10433
10434         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
10435
10436         value=$(cut -d= -f2 <<<$value)
10437         # LU-13168: truncated xattr should fail if short lov_user_md header
10438         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
10439                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
10440         for len in $lens; do
10441                 echo "setfattr $len $testfile.2"
10442                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
10443                         [ $len -lt 66 ] && error "short xattr len=$len worked"
10444         done
10445         local stripe_size=$($LFS getstripe -S $testfile.2)
10446         local stripe_count=$($LFS getstripe -c $testfile.2)
10447         [[ $stripe_size -eq 65536 ]] ||
10448                 error "stripe size $stripe_size != 65536"
10449         [[ $stripe_count -eq $stripe_count_orig ]] ||
10450                 error "stripe count $stripe_count != $stripe_count_orig"
10451         rm $testfile $testfile.2
10452 }
10453
10454 test_102b() {
10455         [ -z "$(which setfattr 2>/dev/null)" ] &&
10456                 skip_env "could not find setfattr"
10457         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10458
10459         # check plain layout
10460         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
10461
10462         # and also check composite layout
10463         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
10464
10465 }
10466 run_test 102b "getfattr/setfattr for trusted.lov EAs"
10467
10468 test_102c() {
10469         [ -z "$(which setfattr 2>/dev/null)" ] &&
10470                 skip_env "could not find setfattr"
10471         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10472
10473         # b10930: get/set/list lustre.lov xattr
10474         echo "get/set/list lustre.lov xattr ..."
10475         test_mkdir $DIR/$tdir
10476         chown $RUNAS_ID $DIR/$tdir
10477         local testfile=$DIR/$tdir/$tfile
10478         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
10479                 error "setstripe failed"
10480         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
10481                 error "getstripe failed"
10482         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
10483         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
10484
10485         local testfile2=${testfile}2
10486         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
10487                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
10488
10489         $RUNAS $MCREATE $testfile2
10490         $RUNAS setfattr -n lustre.lov -v $value $testfile2
10491         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
10492         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
10493         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
10494         [ $stripe_count -eq $STRIPECOUNT ] ||
10495                 error "stripe count $stripe_count != $STRIPECOUNT"
10496 }
10497 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
10498
10499 compare_stripe_info1() {
10500         local stripe_index_all_zero=true
10501
10502         for num in 1 2 3 4; do
10503                 for count in $(seq 1 $STRIPE_COUNT); do
10504                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
10505                                 local size=$((STRIPE_SIZE * num))
10506                                 local file=file"$num-$offset-$count"
10507                                 stripe_size=$($LFS getstripe -S $PWD/$file)
10508                                 [[ $stripe_size -ne $size ]] &&
10509                                     error "$file: size $stripe_size != $size"
10510                                 stripe_count=$($LFS getstripe -c $PWD/$file)
10511                                 # allow fewer stripes to be created, ORI-601
10512                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
10513                                     error "$file: count $stripe_count != $count"
10514                                 stripe_index=$($LFS getstripe -i $PWD/$file)
10515                                 [[ $stripe_index -ne 0 ]] &&
10516                                         stripe_index_all_zero=false
10517                         done
10518                 done
10519         done
10520         $stripe_index_all_zero &&
10521                 error "all files are being extracted starting from OST index 0"
10522         return 0
10523 }
10524
10525 have_xattrs_include() {
10526         tar --help | grep -q xattrs-include &&
10527                 echo --xattrs-include="lustre.*"
10528 }
10529
10530 test_102d() {
10531         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10532         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10533
10534         XINC=$(have_xattrs_include)
10535         setup_test102
10536         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
10537         cd $DIR/$tdir/$tdir
10538         compare_stripe_info1
10539 }
10540 run_test 102d "tar restore stripe info from tarfile,not keep osts"
10541
10542 test_102f() {
10543         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10544         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10545
10546         XINC=$(have_xattrs_include)
10547         setup_test102
10548         test_mkdir $DIR/$tdir.restore
10549         cd $DIR
10550         tar cf - --xattrs $tdir | tar xf - \
10551                 -C $DIR/$tdir.restore --xattrs $XINC
10552         cd $DIR/$tdir.restore/$tdir
10553         compare_stripe_info1
10554 }
10555 run_test 102f "tar copy files, not keep osts"
10556
10557 grow_xattr() {
10558         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
10559                 skip "must have user_xattr"
10560         [ -z "$(which setfattr 2>/dev/null)" ] &&
10561                 skip_env "could not find setfattr"
10562         [ -z "$(which getfattr 2>/dev/null)" ] &&
10563                 skip_env "could not find getfattr"
10564
10565         local xsize=${1:-1024}  # in bytes
10566         local file=$DIR/$tfile
10567         local value="$(generate_string $xsize)"
10568         local xbig=trusted.big
10569         local toobig=$2
10570
10571         touch $file
10572         log "save $xbig on $file"
10573         if [ -z "$toobig" ]
10574         then
10575                 setfattr -n $xbig -v $value $file ||
10576                         error "saving $xbig on $file failed"
10577         else
10578                 setfattr -n $xbig -v $value $file &&
10579                         error "saving $xbig on $file succeeded"
10580                 return 0
10581         fi
10582
10583         local orig=$(get_xattr_value $xbig $file)
10584         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
10585
10586         local xsml=trusted.sml
10587         log "save $xsml on $file"
10588         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
10589
10590         local new=$(get_xattr_value $xbig $file)
10591         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
10592
10593         log "grow $xsml on $file"
10594         setfattr -n $xsml -v "$value" $file ||
10595                 error "growing $xsml on $file failed"
10596
10597         new=$(get_xattr_value $xbig $file)
10598         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
10599         log "$xbig still valid after growing $xsml"
10600
10601         rm -f $file
10602 }
10603
10604 test_102h() { # bug 15777
10605         grow_xattr 1024
10606 }
10607 run_test 102h "grow xattr from inside inode to external block"
10608
10609 test_102ha() {
10610         large_xattr_enabled || skip_env "ea_inode feature disabled"
10611
10612         echo "setting xattr of max xattr size: $(max_xattr_size)"
10613         grow_xattr $(max_xattr_size)
10614
10615         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
10616         echo "This should fail:"
10617         grow_xattr $(($(max_xattr_size) + 10)) 1
10618 }
10619 run_test 102ha "grow xattr from inside inode to external inode"
10620
10621 test_102i() { # bug 17038
10622         [ -z "$(which getfattr 2>/dev/null)" ] &&
10623                 skip "could not find getfattr"
10624
10625         touch $DIR/$tfile
10626         ln -s $DIR/$tfile $DIR/${tfile}link
10627         getfattr -n trusted.lov $DIR/$tfile ||
10628                 error "lgetxattr on $DIR/$tfile failed"
10629         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
10630                 grep -i "no such attr" ||
10631                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
10632         rm -f $DIR/$tfile $DIR/${tfile}link
10633 }
10634 run_test 102i "lgetxattr test on symbolic link ============"
10635
10636 test_102j() {
10637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10638         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10639
10640         XINC=$(have_xattrs_include)
10641         setup_test102 "$RUNAS"
10642         chown $RUNAS_ID $DIR/$tdir
10643         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
10644         cd $DIR/$tdir/$tdir
10645         compare_stripe_info1 "$RUNAS"
10646 }
10647 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
10648
10649 test_102k() {
10650         [ -z "$(which setfattr 2>/dev/null)" ] &&
10651                 skip "could not find setfattr"
10652
10653         touch $DIR/$tfile
10654         # b22187 just check that does not crash for regular file.
10655         setfattr -n trusted.lov $DIR/$tfile
10656         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
10657         local test_kdir=$DIR/$tdir
10658         test_mkdir $test_kdir
10659         local default_size=$($LFS getstripe -S $test_kdir)
10660         local default_count=$($LFS getstripe -c $test_kdir)
10661         local default_offset=$($LFS getstripe -i $test_kdir)
10662         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
10663                 error 'dir setstripe failed'
10664         setfattr -n trusted.lov $test_kdir
10665         local stripe_size=$($LFS getstripe -S $test_kdir)
10666         local stripe_count=$($LFS getstripe -c $test_kdir)
10667         local stripe_offset=$($LFS getstripe -i $test_kdir)
10668         [ $stripe_size -eq $default_size ] ||
10669                 error "stripe size $stripe_size != $default_size"
10670         [ $stripe_count -eq $default_count ] ||
10671                 error "stripe count $stripe_count != $default_count"
10672         [ $stripe_offset -eq $default_offset ] ||
10673                 error "stripe offset $stripe_offset != $default_offset"
10674         rm -rf $DIR/$tfile $test_kdir
10675 }
10676 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
10677
10678 test_102l() {
10679         [ -z "$(which getfattr 2>/dev/null)" ] &&
10680                 skip "could not find getfattr"
10681
10682         # LU-532 trusted. xattr is invisible to non-root
10683         local testfile=$DIR/$tfile
10684
10685         touch $testfile
10686
10687         echo "listxattr as user..."
10688         chown $RUNAS_ID $testfile
10689         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
10690             grep -q "trusted" &&
10691                 error "$testfile trusted xattrs are user visible"
10692
10693         return 0;
10694 }
10695 run_test 102l "listxattr size test =================================="
10696
10697 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
10698         local path=$DIR/$tfile
10699         touch $path
10700
10701         listxattr_size_check $path || error "listattr_size_check $path failed"
10702 }
10703 run_test 102m "Ensure listxattr fails on small bufffer ========"
10704
10705 cleanup_test102
10706
10707 getxattr() { # getxattr path name
10708         # Return the base64 encoding of the value of xattr name on path.
10709         local path=$1
10710         local name=$2
10711
10712         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
10713         # file: $path
10714         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
10715         #
10716         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
10717
10718         getfattr --absolute-names --encoding=base64 --name=$name $path |
10719                 awk -F= -v name=$name '$1 == name {
10720                         print substr($0, index($0, "=") + 1);
10721         }'
10722 }
10723
10724 test_102n() { # LU-4101 mdt: protect internal xattrs
10725         [ -z "$(which setfattr 2>/dev/null)" ] &&
10726                 skip "could not find setfattr"
10727         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
10728         then
10729                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
10730         fi
10731
10732         local file0=$DIR/$tfile.0
10733         local file1=$DIR/$tfile.1
10734         local xattr0=$TMP/$tfile.0
10735         local xattr1=$TMP/$tfile.1
10736         local namelist="lov lma lmv link fid version som hsm"
10737         local name
10738         local value
10739
10740         rm -rf $file0 $file1 $xattr0 $xattr1
10741         touch $file0 $file1
10742
10743         # Get 'before' xattrs of $file1.
10744         getfattr --absolute-names --dump --match=- $file1 > $xattr0
10745
10746         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
10747                 namelist+=" lfsck_namespace"
10748         for name in $namelist; do
10749                 # Try to copy xattr from $file0 to $file1.
10750                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
10751
10752                 setfattr --name=trusted.$name --value="$value" $file1 ||
10753                         error "setxattr 'trusted.$name' failed"
10754
10755                 # Try to set a garbage xattr.
10756                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
10757
10758                 if [[ x$name == "xlov" ]]; then
10759                         setfattr --name=trusted.lov --value="$value" $file1 &&
10760                         error "setxattr invalid 'trusted.lov' success"
10761                 else
10762                         setfattr --name=trusted.$name --value="$value" $file1 ||
10763                                 error "setxattr invalid 'trusted.$name' failed"
10764                 fi
10765
10766                 # Try to remove the xattr from $file1. We don't care if this
10767                 # appears to succeed or fail, we just don't want there to be
10768                 # any changes or crashes.
10769                 setfattr --remove=$trusted.$name $file1 2> /dev/null
10770         done
10771
10772         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
10773         then
10774                 name="lfsck_ns"
10775                 # Try to copy xattr from $file0 to $file1.
10776                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
10777
10778                 setfattr --name=trusted.$name --value="$value" $file1 ||
10779                         error "setxattr 'trusted.$name' failed"
10780
10781                 # Try to set a garbage xattr.
10782                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
10783
10784                 setfattr --name=trusted.$name --value="$value" $file1 ||
10785                         error "setxattr 'trusted.$name' failed"
10786
10787                 # Try to remove the xattr from $file1. We don't care if this
10788                 # appears to succeed or fail, we just don't want there to be
10789                 # any changes or crashes.
10790                 setfattr --remove=$trusted.$name $file1 2> /dev/null
10791         fi
10792
10793         # Get 'after' xattrs of file1.
10794         getfattr --absolute-names --dump --match=- $file1 > $xattr1
10795
10796         if ! diff $xattr0 $xattr1; then
10797                 error "before and after xattrs of '$file1' differ"
10798         fi
10799
10800         rm -rf $file0 $file1 $xattr0 $xattr1
10801
10802         return 0
10803 }
10804 run_test 102n "silently ignore setxattr on internal trusted xattrs"
10805
10806 test_102p() { # LU-4703 setxattr did not check ownership
10807         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
10808                 skip "MDS needs to be at least 2.5.56"
10809
10810         local testfile=$DIR/$tfile
10811
10812         touch $testfile
10813
10814         echo "setfacl as user..."
10815         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
10816         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
10817
10818         echo "setfattr as user..."
10819         setfacl -m "u:$RUNAS_ID:---" $testfile
10820         $RUNAS setfattr -x system.posix_acl_access $testfile
10821         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
10822 }
10823 run_test 102p "check setxattr(2) correctly fails without permission"
10824
10825 test_102q() {
10826         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
10827                 skip "MDS needs to be at least 2.6.92"
10828
10829         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
10830 }
10831 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
10832
10833 test_102r() {
10834         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
10835                 skip "MDS needs to be at least 2.6.93"
10836
10837         touch $DIR/$tfile || error "touch"
10838         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
10839         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
10840         rm $DIR/$tfile || error "rm"
10841
10842         #normal directory
10843         mkdir -p $DIR/$tdir || error "mkdir"
10844         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10845         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10846         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10847                 error "$testfile error deleting user.author1"
10848         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10849                 grep "user.$(basename $tdir)" &&
10850                 error "$tdir did not delete user.$(basename $tdir)"
10851         rmdir $DIR/$tdir || error "rmdir"
10852
10853         #striped directory
10854         test_mkdir $DIR/$tdir
10855         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10856         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10857         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10858                 error "$testfile error deleting user.author1"
10859         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10860                 grep "user.$(basename $tdir)" &&
10861                 error "$tdir did not delete user.$(basename $tdir)"
10862         rmdir $DIR/$tdir || error "rm striped dir"
10863 }
10864 run_test 102r "set EAs with empty values"
10865
10866 test_102s() {
10867         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10868                 skip "MDS needs to be at least 2.11.52"
10869
10870         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10871
10872         save_lustre_params client "llite.*.xattr_cache" > $save
10873
10874         for cache in 0 1; do
10875                 lctl set_param llite.*.xattr_cache=$cache
10876
10877                 rm -f $DIR/$tfile
10878                 touch $DIR/$tfile || error "touch"
10879                 for prefix in lustre security system trusted user; do
10880                         # Note getxattr() may fail with 'Operation not
10881                         # supported' or 'No such attribute' depending
10882                         # on prefix and cache.
10883                         getfattr -n $prefix.n102s $DIR/$tfile &&
10884                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
10885                 done
10886         done
10887
10888         restore_lustre_params < $save
10889 }
10890 run_test 102s "getting nonexistent xattrs should fail"
10891
10892 test_102t() {
10893         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10894                 skip "MDS needs to be at least 2.11.52"
10895
10896         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10897
10898         save_lustre_params client "llite.*.xattr_cache" > $save
10899
10900         for cache in 0 1; do
10901                 lctl set_param llite.*.xattr_cache=$cache
10902
10903                 for buf_size in 0 256; do
10904                         rm -f $DIR/$tfile
10905                         touch $DIR/$tfile || error "touch"
10906                         setfattr -n user.multiop $DIR/$tfile
10907                         $MULTIOP $DIR/$tfile oa$buf_size ||
10908                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
10909                 done
10910         done
10911
10912         restore_lustre_params < $save
10913 }
10914 run_test 102t "zero length xattr values handled correctly"
10915
10916 run_acl_subtest()
10917 {
10918     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
10919     return $?
10920 }
10921
10922 test_103a() {
10923         [ "$UID" != 0 ] && skip "must run as root"
10924         $GSS && skip_env "could not run under gss"
10925         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
10926                 skip_env "must have acl enabled"
10927         [ -z "$(which setfacl 2>/dev/null)" ] &&
10928                 skip_env "could not find setfacl"
10929         remote_mds_nodsh && skip "remote MDS with nodsh"
10930
10931         gpasswd -a daemon bin                           # LU-5641
10932         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
10933
10934         declare -a identity_old
10935
10936         for num in $(seq $MDSCOUNT); do
10937                 switch_identity $num true || identity_old[$num]=$?
10938         done
10939
10940         SAVE_UMASK=$(umask)
10941         umask 0022
10942         mkdir -p $DIR/$tdir
10943         cd $DIR/$tdir
10944
10945         echo "performing cp ..."
10946         run_acl_subtest cp || error "run_acl_subtest cp failed"
10947         echo "performing getfacl-noacl..."
10948         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
10949         echo "performing misc..."
10950         run_acl_subtest misc || error  "misc test failed"
10951         echo "performing permissions..."
10952         run_acl_subtest permissions || error "permissions failed"
10953         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
10954         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
10955                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
10956                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
10957         then
10958                 echo "performing permissions xattr..."
10959                 run_acl_subtest permissions_xattr ||
10960                         error "permissions_xattr failed"
10961         fi
10962         echo "performing setfacl..."
10963         run_acl_subtest setfacl || error  "setfacl test failed"
10964
10965         # inheritance test got from HP
10966         echo "performing inheritance..."
10967         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
10968         chmod +x make-tree || error "chmod +x failed"
10969         run_acl_subtest inheritance || error "inheritance test failed"
10970         rm -f make-tree
10971
10972         echo "LU-974 ignore umask when acl is enabled..."
10973         run_acl_subtest 974 || error "LU-974 umask test failed"
10974         if [ $MDSCOUNT -ge 2 ]; then
10975                 run_acl_subtest 974_remote ||
10976                         error "LU-974 umask test failed under remote dir"
10977         fi
10978
10979         echo "LU-2561 newly created file is same size as directory..."
10980         if [ "$mds1_FSTYPE" != "zfs" ]; then
10981                 run_acl_subtest 2561 || error "LU-2561 test failed"
10982         else
10983                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
10984         fi
10985
10986         run_acl_subtest 4924 || error "LU-4924 test failed"
10987
10988         cd $SAVE_PWD
10989         umask $SAVE_UMASK
10990
10991         for num in $(seq $MDSCOUNT); do
10992                 if [ "${identity_old[$num]}" = 1 ]; then
10993                         switch_identity $num false || identity_old[$num]=$?
10994                 fi
10995         done
10996 }
10997 run_test 103a "acl test"
10998
10999 test_103b() {
11000         declare -a pids
11001         local U
11002
11003         for U in {0..511}; do
11004                 {
11005                 local O=$(printf "%04o" $U)
11006
11007                 umask $(printf "%04o" $((511 ^ $O)))
11008                 $LFS setstripe -c 1 $DIR/$tfile.s$O
11009                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
11010
11011                 (( $S == ($O & 0666) )) ||
11012                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
11013
11014                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
11015                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
11016                 (( $S == ($O & 0666) )) ||
11017                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
11018
11019                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
11020                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
11021                 (( $S == ($O & 0666) )) ||
11022                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
11023                 rm -f $DIR/$tfile.[smp]$0
11024                 } &
11025                 local pid=$!
11026
11027                 # limit the concurrently running threads to 64. LU-11878
11028                 local idx=$((U % 64))
11029                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
11030                 pids[idx]=$pid
11031         done
11032         wait
11033 }
11034 run_test 103b "umask lfs setstripe"
11035
11036 test_103c() {
11037         mkdir -p $DIR/$tdir
11038         cp -rp $DIR/$tdir $DIR/$tdir.bak
11039
11040         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
11041                 error "$DIR/$tdir shouldn't contain default ACL"
11042         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
11043                 error "$DIR/$tdir.bak shouldn't contain default ACL"
11044         true
11045 }
11046 run_test 103c "'cp -rp' won't set empty acl"
11047
11048 test_103e() {
11049         local numacl
11050         local fileacl
11051         local saved_debug=$($LCTL get_param -n debug)
11052
11053         (( $MDS1_VERSION >= $(version_code 2.14.0) )) ||
11054                 skip "MDS needs to be at least 2.14.0"
11055
11056         large_xattr_enabled || skip_env "ea_inode feature disabled"
11057
11058         mkdir -p $DIR/$tdir
11059         # add big LOV EA to cause reply buffer overflow earlier
11060         $LFS setstripe -C 1000 $DIR/$tdir
11061         lctl set_param mdc.*-mdc*.stats=clear
11062
11063         $LCTL set_param debug=0
11064         stack_trap "$LCTL set_param debug=\"$saved_debug\"" EXIT
11065         stack_trap "$LCTL get_param mdc.*-mdc*.stats" EXIT
11066
11067         # add a large number of default ACLs (expect 8000+ for 2.13+)
11068         for U in {2..7000}; do
11069                 setfacl -d -m user:$U:rwx $DIR/$tdir ||
11070                         error "Able to add just $U default ACLs"
11071         done
11072         numacl=$(getfacl $DIR/$tdir |& grep -c "default:user")
11073         echo "$numacl default ACLs created"
11074
11075         stat $DIR/$tdir || error "Cannot stat directory"
11076         # check file creation
11077         touch $DIR/$tdir/$tfile ||
11078                 error "failed to create $tfile with $numacl default ACLs"
11079         stat $DIR/$tdir/$tfile  || error "Cannot stat file"
11080         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11081         echo "$fileacl ACLs were inherited"
11082         (( $fileacl == $numacl )) ||
11083                 error "Not all default ACLs were inherited: $numacl != $fileacl"
11084         # check that new ACLs creation adds new ACLs to inherited ACLs
11085         setfacl -m user:19000:rwx $DIR/$tdir/$tfile ||
11086                 error "Cannot set new ACL"
11087         numacl=$((numacl + 1))
11088         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11089         (( $fileacl == $numacl )) ||
11090                 error "failed to add new ACL: $fileacl != $numacl as expected"
11091         # adds more ACLs to a file to reach their maximum at 8000+
11092         numacl=0
11093         for U in {20000..25000}; do
11094                 setfacl -m user:$U:rwx $DIR/$tdir/$tfile || break
11095                 numacl=$((numacl + 1))
11096         done
11097         echo "Added $numacl more ACLs to the file"
11098         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11099         echo "Total $fileacl ACLs in file"
11100         stat $DIR/$tdir/$tfile > /dev/null || error "Cannot stat file"
11101         rm -f $DIR/$tdir/$tfile || error "Cannot remove file"
11102         rmdir $DIR/$tdir || error "Cannot remove directory"
11103 }
11104 run_test 103e "inheritance of big amount of default ACLs"
11105
11106 test_103f() {
11107         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
11108                 skip "MDS needs to be at least 2.14.51"
11109
11110         large_xattr_enabled || skip_env "ea_inode feature disabled"
11111
11112         # enable changelog to consume more internal MDD buffers
11113         changelog_register
11114
11115         mkdir -p $DIR/$tdir
11116         # add big LOV EA
11117         $LFS setstripe -C 1000 $DIR/$tdir
11118         setfacl -d -m user:$U:rwx $DIR/$tdir || error "Cannot add default ACLs"
11119         mkdir $DIR/$tdir/inherited || error "failed to create subdirectory"
11120         rmdir $DIR/$tdir/inherited || error "Cannot remove subdirectory"
11121         rmdir $DIR/$tdir || error "Cannot remove directory"
11122 }
11123 run_test 103f "changelog doesn't interfere with default ACLs buffers"
11124
11125 test_104a() {
11126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11127
11128         touch $DIR/$tfile
11129         lfs df || error "lfs df failed"
11130         lfs df -ih || error "lfs df -ih failed"
11131         lfs df -h $DIR || error "lfs df -h $DIR failed"
11132         lfs df -i $DIR || error "lfs df -i $DIR failed"
11133         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
11134         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
11135
11136         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
11137         lctl --device %$OSC deactivate
11138         lfs df || error "lfs df with deactivated OSC failed"
11139         lctl --device %$OSC activate
11140         # wait the osc back to normal
11141         wait_osc_import_ready client ost
11142
11143         lfs df || error "lfs df with reactivated OSC failed"
11144         rm -f $DIR/$tfile
11145 }
11146 run_test 104a "lfs df [-ih] [path] test ========================="
11147
11148 test_104b() {
11149         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11150         [ $RUNAS_ID -eq $UID ] &&
11151                 skip_env "RUNAS_ID = UID = $UID -- skipping"
11152
11153         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
11154                         grep "Permission denied" | wc -l)))
11155         if [ $denied_cnt -ne 0 ]; then
11156                 error "lfs check servers test failed"
11157         fi
11158 }
11159 run_test 104b "$RUNAS lfs check servers test ===================="
11160
11161 #
11162 # Verify $1 is within range of $2.
11163 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
11164 # $1 is <= 2% of $2. Else Fail.
11165 #
11166 value_in_range() {
11167         # Strip all units (M, G, T)
11168         actual=$(echo $1 | tr -d A-Z)
11169         expect=$(echo $2 | tr -d A-Z)
11170
11171         expect_lo=$(($expect * 98 / 100)) # 2% below
11172         expect_hi=$(($expect * 102 / 100)) # 2% above
11173
11174         # permit 2% drift above and below
11175         (( $actual >= $expect_lo && $actual <= $expect_hi ))
11176 }
11177
11178 test_104c() {
11179         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11180         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
11181
11182         local ost_param="osd-zfs.$FSNAME-OST0000."
11183         local mdt_param="osd-zfs.$FSNAME-MDT0000."
11184         local ofacets=$(get_facets OST)
11185         local mfacets=$(get_facets MDS)
11186         local saved_ost_blocks=
11187         local saved_mdt_blocks=
11188
11189         echo "Before recordsize change"
11190         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
11191         df=($(df -h | grep "/mnt/lustre"$))
11192
11193         # For checking.
11194         echo "lfs output : ${lfs_df[*]}"
11195         echo "df  output : ${df[*]}"
11196
11197         for facet in ${ofacets//,/ }; do
11198                 if [ -z $saved_ost_blocks ]; then
11199                         saved_ost_blocks=$(do_facet $facet \
11200                                 lctl get_param -n $ost_param.blocksize)
11201                         echo "OST Blocksize: $saved_ost_blocks"
11202                 fi
11203                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11204                 do_facet $facet zfs set recordsize=32768 $ost
11205         done
11206
11207         # BS too small. Sufficient for functional testing.
11208         for facet in ${mfacets//,/ }; do
11209                 if [ -z $saved_mdt_blocks ]; then
11210                         saved_mdt_blocks=$(do_facet $facet \
11211                                 lctl get_param -n $mdt_param.blocksize)
11212                         echo "MDT Blocksize: $saved_mdt_blocks"
11213                 fi
11214                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11215                 do_facet $facet zfs set recordsize=32768 $mdt
11216         done
11217
11218         # Give new values chance to reflect change
11219         sleep 2
11220
11221         echo "After recordsize change"
11222         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
11223         df_after=($(df -h | grep "/mnt/lustre"$))
11224
11225         # For checking.
11226         echo "lfs output : ${lfs_df_after[*]}"
11227         echo "df  output : ${df_after[*]}"
11228
11229         # Verify lfs df
11230         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
11231                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
11232         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
11233                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
11234         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
11235                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
11236
11237         # Verify df
11238         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
11239                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
11240         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
11241                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
11242         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
11243                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
11244
11245         # Restore MDT recordize back to original
11246         for facet in ${mfacets//,/ }; do
11247                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11248                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
11249         done
11250
11251         # Restore OST recordize back to original
11252         for facet in ${ofacets//,/ }; do
11253                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11254                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
11255         done
11256
11257         return 0
11258 }
11259 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
11260
11261 test_105a() {
11262         # doesn't work on 2.4 kernels
11263         touch $DIR/$tfile
11264         if $(flock_is_enabled); then
11265                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
11266         else
11267                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
11268         fi
11269         rm -f $DIR/$tfile
11270 }
11271 run_test 105a "flock when mounted without -o flock test ========"
11272
11273 test_105b() {
11274         touch $DIR/$tfile
11275         if $(flock_is_enabled); then
11276                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
11277         else
11278                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
11279         fi
11280         rm -f $DIR/$tfile
11281 }
11282 run_test 105b "fcntl when mounted without -o flock test ========"
11283
11284 test_105c() {
11285         touch $DIR/$tfile
11286         if $(flock_is_enabled); then
11287                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
11288         else
11289                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
11290         fi
11291         rm -f $DIR/$tfile
11292 }
11293 run_test 105c "lockf when mounted without -o flock test"
11294
11295 test_105d() { # bug 15924
11296         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11297
11298         test_mkdir $DIR/$tdir
11299         flock_is_enabled || skip_env "mount w/o flock enabled"
11300         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
11301         $LCTL set_param fail_loc=0x80000315
11302         flocks_test 2 $DIR/$tdir
11303 }
11304 run_test 105d "flock race (should not freeze) ========"
11305
11306 test_105e() { # bug 22660 && 22040
11307         flock_is_enabled || skip_env "mount w/o flock enabled"
11308
11309         touch $DIR/$tfile
11310         flocks_test 3 $DIR/$tfile
11311 }
11312 run_test 105e "Two conflicting flocks from same process"
11313
11314 test_106() { #bug 10921
11315         test_mkdir $DIR/$tdir
11316         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
11317         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
11318 }
11319 run_test 106 "attempt exec of dir followed by chown of that dir"
11320
11321 test_107() {
11322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11323
11324         CDIR=`pwd`
11325         local file=core
11326
11327         cd $DIR
11328         rm -f $file
11329
11330         local save_pattern=$(sysctl -n kernel.core_pattern)
11331         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
11332         sysctl -w kernel.core_pattern=$file
11333         sysctl -w kernel.core_uses_pid=0
11334
11335         ulimit -c unlimited
11336         sleep 60 &
11337         SLEEPPID=$!
11338
11339         sleep 1
11340
11341         kill -s 11 $SLEEPPID
11342         wait $SLEEPPID
11343         if [ -e $file ]; then
11344                 size=`stat -c%s $file`
11345                 [ $size -eq 0 ] && error "Fail to create core file $file"
11346         else
11347                 error "Fail to create core file $file"
11348         fi
11349         rm -f $file
11350         sysctl -w kernel.core_pattern=$save_pattern
11351         sysctl -w kernel.core_uses_pid=$save_uses_pid
11352         cd $CDIR
11353 }
11354 run_test 107 "Coredump on SIG"
11355
11356 test_110() {
11357         test_mkdir $DIR/$tdir
11358         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
11359         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
11360                 error "mkdir with 256 char should fail, but did not"
11361         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
11362                 error "create with 255 char failed"
11363         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
11364                 error "create with 256 char should fail, but did not"
11365
11366         ls -l $DIR/$tdir
11367         rm -rf $DIR/$tdir
11368 }
11369 run_test 110 "filename length checking"
11370
11371 #
11372 # Purpose: To verify dynamic thread (OSS) creation.
11373 #
11374 test_115() {
11375         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11376         remote_ost_nodsh && skip "remote OST with nodsh"
11377
11378         # Lustre does not stop service threads once they are started.
11379         # Reset number of running threads to default.
11380         stopall
11381         setupall
11382
11383         local OSTIO_pre
11384         local save_params="$TMP/sanity-$TESTNAME.parameters"
11385
11386         # Get ll_ost_io count before I/O
11387         OSTIO_pre=$(do_facet ost1 \
11388                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
11389         # Exit if lustre is not running (ll_ost_io not running).
11390         [ -z "$OSTIO_pre" ] && error "no OSS threads"
11391
11392         echo "Starting with $OSTIO_pre threads"
11393         local thread_max=$((OSTIO_pre * 2))
11394         local rpc_in_flight=$((thread_max * 2))
11395         # Number of I/O Process proposed to be started.
11396         local nfiles
11397         local facets=$(get_facets OST)
11398
11399         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
11400         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
11401
11402         # Set in_flight to $rpc_in_flight
11403         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
11404                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
11405         nfiles=${rpc_in_flight}
11406         # Set ost thread_max to $thread_max
11407         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
11408
11409         # 5 Minutes should be sufficient for max number of OSS
11410         # threads(thread_max) to be created.
11411         local timeout=300
11412
11413         # Start I/O.
11414         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
11415         test_mkdir $DIR/$tdir
11416         for i in $(seq $nfiles); do
11417                 local file=$DIR/$tdir/${tfile}-$i
11418                 $LFS setstripe -c -1 -i 0 $file
11419                 ($WTL $file $timeout)&
11420         done
11421
11422         # I/O Started - Wait for thread_started to reach thread_max or report
11423         # error if thread_started is more than thread_max.
11424         echo "Waiting for thread_started to reach thread_max"
11425         local thread_started=0
11426         local end_time=$((SECONDS + timeout))
11427
11428         while [ $SECONDS -le $end_time ] ; do
11429                 echo -n "."
11430                 # Get ost i/o thread_started count.
11431                 thread_started=$(do_facet ost1 \
11432                         "$LCTL get_param \
11433                         ost.OSS.ost_io.threads_started | cut -d= -f2")
11434                 # Break out if thread_started is equal/greater than thread_max
11435                 if [[ $thread_started -ge $thread_max ]]; then
11436                         echo ll_ost_io thread_started $thread_started, \
11437                                 equal/greater than thread_max $thread_max
11438                         break
11439                 fi
11440                 sleep 1
11441         done
11442
11443         # Cleanup - We have the numbers, Kill i/o jobs if running.
11444         jobcount=($(jobs -p))
11445         for i in $(seq 0 $((${#jobcount[@]}-1)))
11446         do
11447                 kill -9 ${jobcount[$i]}
11448                 if [ $? -ne 0 ] ; then
11449                         echo Warning: \
11450                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
11451                 fi
11452         done
11453
11454         # Cleanup files left by WTL binary.
11455         for i in $(seq $nfiles); do
11456                 local file=$DIR/$tdir/${tfile}-$i
11457                 rm -rf $file
11458                 if [ $? -ne 0 ] ; then
11459                         echo "Warning: Failed to delete file $file"
11460                 fi
11461         done
11462
11463         restore_lustre_params <$save_params
11464         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
11465
11466         # Error out if no new thread has started or Thread started is greater
11467         # than thread max.
11468         if [[ $thread_started -le $OSTIO_pre ||
11469                         $thread_started -gt $thread_max ]]; then
11470                 error "ll_ost_io: thread_started $thread_started" \
11471                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
11472                       "No new thread started or thread started greater " \
11473                       "than thread_max."
11474         fi
11475 }
11476 run_test 115 "verify dynamic thread creation===================="
11477
11478 free_min_max () {
11479         wait_delete_completed
11480         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
11481         echo "OST kbytes available: ${AVAIL[@]}"
11482         MAXV=${AVAIL[0]}
11483         MAXI=0
11484         MINV=${AVAIL[0]}
11485         MINI=0
11486         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
11487                 #echo OST $i: ${AVAIL[i]}kb
11488                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
11489                         MAXV=${AVAIL[i]}
11490                         MAXI=$i
11491                 fi
11492                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
11493                         MINV=${AVAIL[i]}
11494                         MINI=$i
11495                 fi
11496         done
11497         echo "Min free space: OST $MINI: $MINV"
11498         echo "Max free space: OST $MAXI: $MAXV"
11499 }
11500
11501 test_116a() { # was previously test_116()
11502         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11503         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11504         remote_mds_nodsh && skip "remote MDS with nodsh"
11505
11506         echo -n "Free space priority "
11507         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
11508                 head -n1
11509         declare -a AVAIL
11510         free_min_max
11511
11512         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
11513         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
11514         trap simple_cleanup_common EXIT
11515
11516         # Check if we need to generate uneven OSTs
11517         test_mkdir -p $DIR/$tdir/OST${MINI}
11518         local FILL=$((MINV / 4))
11519         local DIFF=$((MAXV - MINV))
11520         local DIFF2=$((DIFF * 100 / MINV))
11521
11522         local threshold=$(do_facet $SINGLEMDS \
11523                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
11524         threshold=${threshold%%%}
11525         echo -n "Check for uneven OSTs: "
11526         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
11527
11528         if [[ $DIFF2 -gt $threshold ]]; then
11529                 echo "ok"
11530                 echo "Don't need to fill OST$MINI"
11531         else
11532                 # generate uneven OSTs. Write 2% over the QOS threshold value
11533                 echo "no"
11534                 DIFF=$((threshold - DIFF2 + 2))
11535                 DIFF2=$((MINV * DIFF / 100))
11536                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
11537                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
11538                         error "setstripe failed"
11539                 DIFF=$((DIFF2 / 2048))
11540                 i=0
11541                 while [ $i -lt $DIFF ]; do
11542                         i=$((i + 1))
11543                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
11544                                 bs=2M count=1 2>/dev/null
11545                         echo -n .
11546                 done
11547                 echo .
11548                 sync
11549                 sleep_maxage
11550                 free_min_max
11551         fi
11552
11553         DIFF=$((MAXV - MINV))
11554         DIFF2=$((DIFF * 100 / MINV))
11555         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
11556         if [ $DIFF2 -gt $threshold ]; then
11557                 echo "ok"
11558         else
11559                 echo "failed - QOS mode won't be used"
11560                 simple_cleanup_common
11561                 skip "QOS imbalance criteria not met"
11562         fi
11563
11564         MINI1=$MINI
11565         MINV1=$MINV
11566         MAXI1=$MAXI
11567         MAXV1=$MAXV
11568
11569         # now fill using QOS
11570         $LFS setstripe -c 1 $DIR/$tdir
11571         FILL=$((FILL / 200))
11572         if [ $FILL -gt 600 ]; then
11573                 FILL=600
11574         fi
11575         echo "writing $FILL files to QOS-assigned OSTs"
11576         i=0
11577         while [ $i -lt $FILL ]; do
11578                 i=$((i + 1))
11579                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
11580                         count=1 2>/dev/null
11581                 echo -n .
11582         done
11583         echo "wrote $i 200k files"
11584         sync
11585         sleep_maxage
11586
11587         echo "Note: free space may not be updated, so measurements might be off"
11588         free_min_max
11589         DIFF2=$((MAXV - MINV))
11590         echo "free space delta: orig $DIFF final $DIFF2"
11591         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
11592         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
11593         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
11594         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
11595         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
11596         if [[ $DIFF -gt 0 ]]; then
11597                 FILL=$((DIFF2 * 100 / DIFF - 100))
11598                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
11599         fi
11600
11601         # Figure out which files were written where
11602         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
11603                awk '/'$MINI1': / {print $2; exit}')
11604         echo $UUID
11605         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
11606         echo "$MINC files created on smaller OST $MINI1"
11607         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
11608                awk '/'$MAXI1': / {print $2; exit}')
11609         echo $UUID
11610         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
11611         echo "$MAXC files created on larger OST $MAXI1"
11612         if [[ $MINC -gt 0 ]]; then
11613                 FILL=$((MAXC * 100 / MINC - 100))
11614                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
11615         fi
11616         [[ $MAXC -gt $MINC ]] ||
11617                 error_ignore LU-9 "stripe QOS didn't balance free space"
11618         simple_cleanup_common
11619 }
11620 run_test 116a "stripe QOS: free space balance ==================="
11621
11622 test_116b() { # LU-2093
11623         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11624         remote_mds_nodsh && skip "remote MDS with nodsh"
11625
11626 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
11627         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
11628                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
11629         [ -z "$old_rr" ] && skip "no QOS"
11630         do_facet $SINGLEMDS lctl set_param \
11631                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
11632         mkdir -p $DIR/$tdir
11633         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
11634         createmany -o $DIR/$tdir/f- 20 || error "can't create"
11635         do_facet $SINGLEMDS lctl set_param fail_loc=0
11636         rm -rf $DIR/$tdir
11637         do_facet $SINGLEMDS lctl set_param \
11638                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
11639 }
11640 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
11641
11642 test_117() # bug 10891
11643 {
11644         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11645
11646         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
11647         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
11648         lctl set_param fail_loc=0x21e
11649         > $DIR/$tfile || error "truncate failed"
11650         lctl set_param fail_loc=0
11651         echo "Truncate succeeded."
11652         rm -f $DIR/$tfile
11653 }
11654 run_test 117 "verify osd extend =========="
11655
11656 NO_SLOW_RESENDCOUNT=4
11657 export OLD_RESENDCOUNT=""
11658 set_resend_count () {
11659         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
11660         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
11661         lctl set_param -n $PROC_RESENDCOUNT $1
11662         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
11663 }
11664
11665 # for reduce test_118* time (b=14842)
11666 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
11667
11668 # Reset async IO behavior after error case
11669 reset_async() {
11670         FILE=$DIR/reset_async
11671
11672         # Ensure all OSCs are cleared
11673         $LFS setstripe -c -1 $FILE
11674         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
11675         sync
11676         rm $FILE
11677 }
11678
11679 test_118a() #bug 11710
11680 {
11681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11682
11683         reset_async
11684
11685         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11686         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11687         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11688
11689         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11690                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11691                 return 1;
11692         fi
11693         rm -f $DIR/$tfile
11694 }
11695 run_test 118a "verify O_SYNC works =========="
11696
11697 test_118b()
11698 {
11699         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11700         remote_ost_nodsh && skip "remote OST with nodsh"
11701
11702         reset_async
11703
11704         #define OBD_FAIL_SRV_ENOENT 0x217
11705         set_nodes_failloc "$(osts_nodes)" 0x217
11706         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11707         RC=$?
11708         set_nodes_failloc "$(osts_nodes)" 0
11709         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11710         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11711                     grep -c writeback)
11712
11713         if [[ $RC -eq 0 ]]; then
11714                 error "Must return error due to dropped pages, rc=$RC"
11715                 return 1;
11716         fi
11717
11718         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11719                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11720                 return 1;
11721         fi
11722
11723         echo "Dirty pages not leaked on ENOENT"
11724
11725         # Due to the above error the OSC will issue all RPCs syncronously
11726         # until a subsequent RPC completes successfully without error.
11727         $MULTIOP $DIR/$tfile Ow4096yc
11728         rm -f $DIR/$tfile
11729
11730         return 0
11731 }
11732 run_test 118b "Reclaim dirty pages on fatal error =========="
11733
11734 test_118c()
11735 {
11736         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11737
11738         # for 118c, restore the original resend count, LU-1940
11739         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
11740                                 set_resend_count $OLD_RESENDCOUNT
11741         remote_ost_nodsh && skip "remote OST with nodsh"
11742
11743         reset_async
11744
11745         #define OBD_FAIL_OST_EROFS               0x216
11746         set_nodes_failloc "$(osts_nodes)" 0x216
11747
11748         # multiop should block due to fsync until pages are written
11749         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
11750         MULTIPID=$!
11751         sleep 1
11752
11753         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
11754                 error "Multiop failed to block on fsync, pid=$MULTIPID"
11755         fi
11756
11757         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11758                     grep -c writeback)
11759         if [[ $WRITEBACK -eq 0 ]]; then
11760                 error "No page in writeback, writeback=$WRITEBACK"
11761         fi
11762
11763         set_nodes_failloc "$(osts_nodes)" 0
11764         wait $MULTIPID
11765         RC=$?
11766         if [[ $RC -ne 0 ]]; then
11767                 error "Multiop fsync failed, rc=$RC"
11768         fi
11769
11770         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11771         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11772                     grep -c writeback)
11773         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11774                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11775         fi
11776
11777         rm -f $DIR/$tfile
11778         echo "Dirty pages flushed via fsync on EROFS"
11779         return 0
11780 }
11781 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
11782
11783 # continue to use small resend count to reduce test_118* time (b=14842)
11784 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
11785
11786 test_118d()
11787 {
11788         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11789         remote_ost_nodsh && skip "remote OST with nodsh"
11790
11791         reset_async
11792
11793         #define OBD_FAIL_OST_BRW_PAUSE_BULK
11794         set_nodes_failloc "$(osts_nodes)" 0x214
11795         # multiop should block due to fsync until pages are written
11796         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
11797         MULTIPID=$!
11798         sleep 1
11799
11800         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
11801                 error "Multiop failed to block on fsync, pid=$MULTIPID"
11802         fi
11803
11804         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11805                     grep -c writeback)
11806         if [[ $WRITEBACK -eq 0 ]]; then
11807                 error "No page in writeback, writeback=$WRITEBACK"
11808         fi
11809
11810         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
11811         set_nodes_failloc "$(osts_nodes)" 0
11812
11813         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11814         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11815                     grep -c writeback)
11816         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11817                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11818         fi
11819
11820         rm -f $DIR/$tfile
11821         echo "Dirty pages gaurenteed flushed via fsync"
11822         return 0
11823 }
11824 run_test 118d "Fsync validation inject a delay of the bulk =========="
11825
11826 test_118f() {
11827         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11828
11829         reset_async
11830
11831         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
11832         lctl set_param fail_loc=0x8000040a
11833
11834         # Should simulate EINVAL error which is fatal
11835         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11836         RC=$?
11837         if [[ $RC -eq 0 ]]; then
11838                 error "Must return error due to dropped pages, rc=$RC"
11839         fi
11840
11841         lctl set_param fail_loc=0x0
11842
11843         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11844         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11845         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11846                     grep -c writeback)
11847         if [[ $LOCKED -ne 0 ]]; then
11848                 error "Locked pages remain in cache, locked=$LOCKED"
11849         fi
11850
11851         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11852                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11853         fi
11854
11855         rm -f $DIR/$tfile
11856         echo "No pages locked after fsync"
11857
11858         reset_async
11859         return 0
11860 }
11861 run_test 118f "Simulate unrecoverable OSC side error =========="
11862
11863 test_118g() {
11864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11865
11866         reset_async
11867
11868         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
11869         lctl set_param fail_loc=0x406
11870
11871         # simulate local -ENOMEM
11872         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11873         RC=$?
11874
11875         lctl set_param fail_loc=0
11876         if [[ $RC -eq 0 ]]; then
11877                 error "Must return error due to dropped pages, rc=$RC"
11878         fi
11879
11880         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11881         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11882         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11883                         grep -c writeback)
11884         if [[ $LOCKED -ne 0 ]]; then
11885                 error "Locked pages remain in cache, locked=$LOCKED"
11886         fi
11887
11888         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11889                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11890         fi
11891
11892         rm -f $DIR/$tfile
11893         echo "No pages locked after fsync"
11894
11895         reset_async
11896         return 0
11897 }
11898 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
11899
11900 test_118h() {
11901         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11902         remote_ost_nodsh && skip "remote OST with nodsh"
11903
11904         reset_async
11905
11906         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
11907         set_nodes_failloc "$(osts_nodes)" 0x20e
11908         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
11909         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11910         RC=$?
11911
11912         set_nodes_failloc "$(osts_nodes)" 0
11913         if [[ $RC -eq 0 ]]; then
11914                 error "Must return error due to dropped pages, rc=$RC"
11915         fi
11916
11917         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11918         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11919         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11920                     grep -c writeback)
11921         if [[ $LOCKED -ne 0 ]]; then
11922                 error "Locked pages remain in cache, locked=$LOCKED"
11923         fi
11924
11925         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11926                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11927         fi
11928
11929         rm -f $DIR/$tfile
11930         echo "No pages locked after fsync"
11931
11932         return 0
11933 }
11934 run_test 118h "Verify timeout in handling recoverables errors  =========="
11935
11936 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
11937
11938 test_118i() {
11939         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11940         remote_ost_nodsh && skip "remote OST with nodsh"
11941
11942         reset_async
11943
11944         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
11945         set_nodes_failloc "$(osts_nodes)" 0x20e
11946
11947         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
11948         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
11949         PID=$!
11950         sleep 5
11951         set_nodes_failloc "$(osts_nodes)" 0
11952
11953         wait $PID
11954         RC=$?
11955         if [[ $RC -ne 0 ]]; then
11956                 error "got error, but should be not, rc=$RC"
11957         fi
11958
11959         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11960         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11961         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11962         if [[ $LOCKED -ne 0 ]]; then
11963                 error "Locked pages remain in cache, locked=$LOCKED"
11964         fi
11965
11966         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11967                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11968         fi
11969
11970         rm -f $DIR/$tfile
11971         echo "No pages locked after fsync"
11972
11973         return 0
11974 }
11975 run_test 118i "Fix error before timeout in recoverable error  =========="
11976
11977 [ "$SLOW" = "no" ] && set_resend_count 4
11978
11979 test_118j() {
11980         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11981         remote_ost_nodsh && skip "remote OST with nodsh"
11982
11983         reset_async
11984
11985         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
11986         set_nodes_failloc "$(osts_nodes)" 0x220
11987
11988         # return -EIO from OST
11989         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11990         RC=$?
11991         set_nodes_failloc "$(osts_nodes)" 0x0
11992         if [[ $RC -eq 0 ]]; then
11993                 error "Must return error due to dropped pages, rc=$RC"
11994         fi
11995
11996         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11997         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11998         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11999         if [[ $LOCKED -ne 0 ]]; then
12000                 error "Locked pages remain in cache, locked=$LOCKED"
12001         fi
12002
12003         # in recoverable error on OST we want resend and stay until it finished
12004         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12005                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12006         fi
12007
12008         rm -f $DIR/$tfile
12009         echo "No pages locked after fsync"
12010
12011         return 0
12012 }
12013 run_test 118j "Simulate unrecoverable OST side error =========="
12014
12015 test_118k()
12016 {
12017         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12018         remote_ost_nodsh && skip "remote OSTs with nodsh"
12019
12020         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12021         set_nodes_failloc "$(osts_nodes)" 0x20e
12022         test_mkdir $DIR/$tdir
12023
12024         for ((i=0;i<10;i++)); do
12025                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
12026                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
12027                 SLEEPPID=$!
12028                 sleep 0.500s
12029                 kill $SLEEPPID
12030                 wait $SLEEPPID
12031         done
12032
12033         set_nodes_failloc "$(osts_nodes)" 0
12034         rm -rf $DIR/$tdir
12035 }
12036 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
12037
12038 test_118l() # LU-646
12039 {
12040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12041
12042         test_mkdir $DIR/$tdir
12043         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
12044         rm -rf $DIR/$tdir
12045 }
12046 run_test 118l "fsync dir"
12047
12048 test_118m() # LU-3066
12049 {
12050         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12051
12052         test_mkdir $DIR/$tdir
12053         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
12054         rm -rf $DIR/$tdir
12055 }
12056 run_test 118m "fdatasync dir ========="
12057
12058 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12059
12060 test_118n()
12061 {
12062         local begin
12063         local end
12064
12065         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12066         remote_ost_nodsh && skip "remote OSTs with nodsh"
12067
12068         # Sleep to avoid a cached response.
12069         #define OBD_STATFS_CACHE_SECONDS 1
12070         sleep 2
12071
12072         # Inject a 10 second delay in the OST_STATFS handler.
12073         #define OBD_FAIL_OST_STATFS_DELAY 0x242
12074         set_nodes_failloc "$(osts_nodes)" 0x242
12075
12076         begin=$SECONDS
12077         stat --file-system $MOUNT > /dev/null
12078         end=$SECONDS
12079
12080         set_nodes_failloc "$(osts_nodes)" 0
12081
12082         if ((end - begin > 20)); then
12083             error "statfs took $((end - begin)) seconds, expected 10"
12084         fi
12085 }
12086 run_test 118n "statfs() sends OST_STATFS requests in parallel"
12087
12088 test_119a() # bug 11737
12089 {
12090         BSIZE=$((512 * 1024))
12091         directio write $DIR/$tfile 0 1 $BSIZE
12092         # We ask to read two blocks, which is more than a file size.
12093         # directio will indicate an error when requested and actual
12094         # sizes aren't equeal (a normal situation in this case) and
12095         # print actual read amount.
12096         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
12097         if [ "$NOB" != "$BSIZE" ]; then
12098                 error "read $NOB bytes instead of $BSIZE"
12099         fi
12100         rm -f $DIR/$tfile
12101 }
12102 run_test 119a "Short directIO read must return actual read amount"
12103
12104 test_119b() # bug 11737
12105 {
12106         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12107
12108         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
12109         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
12110         sync
12111         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
12112                 error "direct read failed"
12113         rm -f $DIR/$tfile
12114 }
12115 run_test 119b "Sparse directIO read must return actual read amount"
12116
12117 test_119c() # bug 13099
12118 {
12119         BSIZE=1048576
12120         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
12121         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
12122         rm -f $DIR/$tfile
12123 }
12124 run_test 119c "Testing for direct read hitting hole"
12125
12126 test_119d() # bug 15950
12127 {
12128         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12129
12130         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
12131         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
12132         BSIZE=1048576
12133         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
12134         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
12135         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
12136         lctl set_param fail_loc=0x40d
12137         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
12138         pid_dio=$!
12139         sleep 1
12140         cat $DIR/$tfile > /dev/null &
12141         lctl set_param fail_loc=0
12142         pid_reads=$!
12143         wait $pid_dio
12144         log "the DIO writes have completed, now wait for the reads (should not block very long)"
12145         sleep 2
12146         [ -n "`ps h -p $pid_reads -o comm`" ] && \
12147         error "the read rpcs have not completed in 2s"
12148         rm -f $DIR/$tfile
12149         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
12150 }
12151 run_test 119d "The DIO path should try to send a new rpc once one is completed"
12152
12153 test_120a() {
12154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12155         remote_mds_nodsh && skip "remote MDS with nodsh"
12156         test_mkdir -i0 -c1 $DIR/$tdir
12157         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12158                 skip_env "no early lock cancel on server"
12159
12160         lru_resize_disable mdc
12161         lru_resize_disable osc
12162         cancel_lru_locks mdc
12163         # asynchronous object destroy at MDT could cause bl ast to client
12164         cancel_lru_locks osc
12165
12166         stat $DIR/$tdir > /dev/null
12167         can1=$(do_facet mds1 \
12168                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12169                awk '/ldlm_cancel/ {print $2}')
12170         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12171                awk '/ldlm_bl_callback/ {print $2}')
12172         test_mkdir -i0 -c1 $DIR/$tdir/d1
12173         can2=$(do_facet mds1 \
12174                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12175                awk '/ldlm_cancel/ {print $2}')
12176         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12177                awk '/ldlm_bl_callback/ {print $2}')
12178         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12179         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12180         lru_resize_enable mdc
12181         lru_resize_enable osc
12182 }
12183 run_test 120a "Early Lock Cancel: mkdir test"
12184
12185 test_120b() {
12186         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12187         remote_mds_nodsh && skip "remote MDS with nodsh"
12188         test_mkdir $DIR/$tdir
12189         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12190                 skip_env "no early lock cancel on server"
12191
12192         lru_resize_disable mdc
12193         lru_resize_disable osc
12194         cancel_lru_locks mdc
12195         stat $DIR/$tdir > /dev/null
12196         can1=$(do_facet $SINGLEMDS \
12197                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12198                awk '/ldlm_cancel/ {print $2}')
12199         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12200                awk '/ldlm_bl_callback/ {print $2}')
12201         touch $DIR/$tdir/f1
12202         can2=$(do_facet $SINGLEMDS \
12203                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12204                awk '/ldlm_cancel/ {print $2}')
12205         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12206                awk '/ldlm_bl_callback/ {print $2}')
12207         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12208         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12209         lru_resize_enable mdc
12210         lru_resize_enable osc
12211 }
12212 run_test 120b "Early Lock Cancel: create test"
12213
12214 test_120c() {
12215         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12216         remote_mds_nodsh && skip "remote MDS with nodsh"
12217         test_mkdir -i0 -c1 $DIR/$tdir
12218         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12219                 skip "no early lock cancel on server"
12220
12221         lru_resize_disable mdc
12222         lru_resize_disable osc
12223         test_mkdir -i0 -c1 $DIR/$tdir/d1
12224         test_mkdir -i0 -c1 $DIR/$tdir/d2
12225         touch $DIR/$tdir/d1/f1
12226         cancel_lru_locks mdc
12227         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
12228         can1=$(do_facet mds1 \
12229                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12230                awk '/ldlm_cancel/ {print $2}')
12231         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12232                awk '/ldlm_bl_callback/ {print $2}')
12233         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12234         can2=$(do_facet mds1 \
12235                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12236                awk '/ldlm_cancel/ {print $2}')
12237         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12238                awk '/ldlm_bl_callback/ {print $2}')
12239         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12240         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12241         lru_resize_enable mdc
12242         lru_resize_enable osc
12243 }
12244 run_test 120c "Early Lock Cancel: link test"
12245
12246 test_120d() {
12247         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12248         remote_mds_nodsh && skip "remote MDS with nodsh"
12249         test_mkdir -i0 -c1 $DIR/$tdir
12250         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12251                 skip_env "no early lock cancel on server"
12252
12253         lru_resize_disable mdc
12254         lru_resize_disable osc
12255         touch $DIR/$tdir
12256         cancel_lru_locks mdc
12257         stat $DIR/$tdir > /dev/null
12258         can1=$(do_facet mds1 \
12259                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12260                awk '/ldlm_cancel/ {print $2}')
12261         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12262                awk '/ldlm_bl_callback/ {print $2}')
12263         chmod a+x $DIR/$tdir
12264         can2=$(do_facet mds1 \
12265                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12266                awk '/ldlm_cancel/ {print $2}')
12267         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12268                awk '/ldlm_bl_callback/ {print $2}')
12269         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12270         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12271         lru_resize_enable mdc
12272         lru_resize_enable osc
12273 }
12274 run_test 120d "Early Lock Cancel: setattr test"
12275
12276 test_120e() {
12277         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12278         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12279                 skip_env "no early lock cancel on server"
12280         remote_mds_nodsh && skip "remote MDS with nodsh"
12281
12282         local dlmtrace_set=false
12283
12284         test_mkdir -i0 -c1 $DIR/$tdir
12285         lru_resize_disable mdc
12286         lru_resize_disable osc
12287         ! $LCTL get_param debug | grep -q dlmtrace &&
12288                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
12289         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
12290         cancel_lru_locks mdc
12291         cancel_lru_locks osc
12292         dd if=$DIR/$tdir/f1 of=/dev/null
12293         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
12294         # XXX client can not do early lock cancel of OST lock
12295         # during unlink (LU-4206), so cancel osc lock now.
12296         sleep 2
12297         cancel_lru_locks osc
12298         can1=$(do_facet mds1 \
12299                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12300                awk '/ldlm_cancel/ {print $2}')
12301         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12302                awk '/ldlm_bl_callback/ {print $2}')
12303         unlink $DIR/$tdir/f1
12304         sleep 5
12305         can2=$(do_facet mds1 \
12306                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12307                awk '/ldlm_cancel/ {print $2}')
12308         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12309                awk '/ldlm_bl_callback/ {print $2}')
12310         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
12311                 $LCTL dk $TMP/cancel.debug.txt
12312         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
12313                 $LCTL dk $TMP/blocking.debug.txt
12314         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
12315         lru_resize_enable mdc
12316         lru_resize_enable osc
12317 }
12318 run_test 120e "Early Lock Cancel: unlink test"
12319
12320 test_120f() {
12321         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12322         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12323                 skip_env "no early lock cancel on server"
12324         remote_mds_nodsh && skip "remote MDS with nodsh"
12325
12326         test_mkdir -i0 -c1 $DIR/$tdir
12327         lru_resize_disable mdc
12328         lru_resize_disable osc
12329         test_mkdir -i0 -c1 $DIR/$tdir/d1
12330         test_mkdir -i0 -c1 $DIR/$tdir/d2
12331         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
12332         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
12333         cancel_lru_locks mdc
12334         cancel_lru_locks osc
12335         dd if=$DIR/$tdir/d1/f1 of=/dev/null
12336         dd if=$DIR/$tdir/d2/f2 of=/dev/null
12337         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
12338         # XXX client can not do early lock cancel of OST lock
12339         # during rename (LU-4206), so cancel osc lock now.
12340         sleep 2
12341         cancel_lru_locks osc
12342         can1=$(do_facet mds1 \
12343                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12344                awk '/ldlm_cancel/ {print $2}')
12345         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12346                awk '/ldlm_bl_callback/ {print $2}')
12347         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12348         sleep 5
12349         can2=$(do_facet mds1 \
12350                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12351                awk '/ldlm_cancel/ {print $2}')
12352         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12353                awk '/ldlm_bl_callback/ {print $2}')
12354         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12355         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12356         lru_resize_enable mdc
12357         lru_resize_enable osc
12358 }
12359 run_test 120f "Early Lock Cancel: rename test"
12360
12361 test_120g() {
12362         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12363         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12364                 skip_env "no early lock cancel on server"
12365         remote_mds_nodsh && skip "remote MDS with nodsh"
12366
12367         lru_resize_disable mdc
12368         lru_resize_disable osc
12369         count=10000
12370         echo create $count files
12371         test_mkdir $DIR/$tdir
12372         cancel_lru_locks mdc
12373         cancel_lru_locks osc
12374         t0=$(date +%s)
12375
12376         can0=$(do_facet $SINGLEMDS \
12377                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12378                awk '/ldlm_cancel/ {print $2}')
12379         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12380                awk '/ldlm_bl_callback/ {print $2}')
12381         createmany -o $DIR/$tdir/f $count
12382         sync
12383         can1=$(do_facet $SINGLEMDS \
12384                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12385                awk '/ldlm_cancel/ {print $2}')
12386         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12387                awk '/ldlm_bl_callback/ {print $2}')
12388         t1=$(date +%s)
12389         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
12390         echo rm $count files
12391         rm -r $DIR/$tdir
12392         sync
12393         can2=$(do_facet $SINGLEMDS \
12394                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12395                awk '/ldlm_cancel/ {print $2}')
12396         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12397                awk '/ldlm_bl_callback/ {print $2}')
12398         t2=$(date +%s)
12399         echo total: $count removes in $((t2-t1))
12400         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
12401         sleep 2
12402         # wait for commitment of removal
12403         lru_resize_enable mdc
12404         lru_resize_enable osc
12405 }
12406 run_test 120g "Early Lock Cancel: performance test"
12407
12408 test_121() { #bug #10589
12409         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12410
12411         rm -rf $DIR/$tfile
12412         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
12413 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
12414         lctl set_param fail_loc=0x310
12415         cancel_lru_locks osc > /dev/null
12416         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
12417         lctl set_param fail_loc=0
12418         [[ $reads -eq $writes ]] ||
12419                 error "read $reads blocks, must be $writes blocks"
12420 }
12421 run_test 121 "read cancel race ========="
12422
12423 test_123a_base() { # was test 123, statahead(bug 11401)
12424         local lsx="$1"
12425
12426         SLOWOK=0
12427         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
12428                 log "testing UP system. Performance may be lower than expected."
12429                 SLOWOK=1
12430         fi
12431
12432         rm -rf $DIR/$tdir
12433         test_mkdir $DIR/$tdir
12434         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
12435         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
12436         MULT=10
12437         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
12438                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
12439
12440                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
12441                 lctl set_param -n llite.*.statahead_max 0
12442                 lctl get_param llite.*.statahead_max
12443                 cancel_lru_locks mdc
12444                 cancel_lru_locks osc
12445                 stime=$(date +%s)
12446                 time $lsx $DIR/$tdir | wc -l
12447                 etime=$(date +%s)
12448                 delta=$((etime - stime))
12449                 log "$lsx $i files without statahead: $delta sec"
12450                 lctl set_param llite.*.statahead_max=$max
12451
12452                 swrong=$(lctl get_param -n llite.*.statahead_stats |
12453                         grep "statahead wrong:" | awk '{print $3}')
12454                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
12455                 cancel_lru_locks mdc
12456                 cancel_lru_locks osc
12457                 stime=$(date +%s)
12458                 time $lsx $DIR/$tdir | wc -l
12459                 etime=$(date +%s)
12460                 delta_sa=$((etime - stime))
12461                 log "$lsx $i files with statahead: $delta_sa sec"
12462                 lctl get_param -n llite.*.statahead_stats
12463                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
12464                         grep "statahead wrong:" | awk '{print $3}')
12465
12466                 [[ $swrong -lt $ewrong ]] &&
12467                         log "statahead was stopped, maybe too many locks held!"
12468                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
12469
12470                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
12471                         max=$(lctl get_param -n llite.*.statahead_max |
12472                                 head -n 1)
12473                         lctl set_param -n llite.*.statahead_max 0
12474                         lctl get_param llite.*.statahead_max
12475                         cancel_lru_locks mdc
12476                         cancel_lru_locks osc
12477                         stime=$(date +%s)
12478                         time $lsx $DIR/$tdir | wc -l
12479                         etime=$(date +%s)
12480                         delta=$((etime - stime))
12481                         log "$lsx $i files again without statahead: $delta sec"
12482                         lctl set_param llite.*.statahead_max=$max
12483                         if [ $((delta_sa * 100 > delta * 105 && delta_sa > delta + 2)) ]; then
12484                                 if [  $SLOWOK -eq 0 ]; then
12485                                         error "$lsx $i files is slower with statahead!"
12486                                 else
12487                                         log "$lsx $i files is slower with statahead!"
12488                                 fi
12489                                 break
12490                         fi
12491                 fi
12492
12493                 [ $delta -gt 20 ] && break
12494                 [ $delta -gt 8 ] && MULT=$((50 / delta))
12495                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
12496         done
12497         log "$lsx done"
12498
12499         stime=$(date +%s)
12500         rm -r $DIR/$tdir
12501         sync
12502         etime=$(date +%s)
12503         delta=$((etime - stime))
12504         log "rm -r $DIR/$tdir/: $delta seconds"
12505         log "rm done"
12506         lctl get_param -n llite.*.statahead_stats
12507 }
12508
12509 test_123aa() {
12510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12511
12512         test_123a_base "ls -l"
12513 }
12514 run_test 123aa "verify statahead work"
12515
12516 test_123ab() {
12517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12518
12519         statx_supported || skip_env "Test must be statx() syscall supported"
12520
12521         test_123a_base "$STATX -l"
12522 }
12523 run_test 123ab "verify statahead work by using statx"
12524
12525 test_123ac() {
12526         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12527
12528         statx_supported || skip_env "Test must be statx() syscall supported"
12529
12530         local rpcs_before
12531         local rpcs_after
12532         local agl_before
12533         local agl_after
12534
12535         cancel_lru_locks $OSC
12536         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
12537         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
12538                 awk '/agl.total:/ {print $3}')
12539         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
12540         test_123a_base "$STATX --cached=always -D"
12541         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
12542                 awk '/agl.total:/ {print $3}')
12543         [ $agl_before -eq $agl_after ] ||
12544                 error "Should not trigger AGL thread - $agl_before:$agl_after"
12545         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
12546         [ $rpcs_after -eq $rpcs_before ] ||
12547                 error "$STATX should not send glimpse RPCs to $OSC"
12548 }
12549 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
12550
12551 test_123b () { # statahead(bug 15027)
12552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12553
12554         test_mkdir $DIR/$tdir
12555         createmany -o $DIR/$tdir/$tfile-%d 1000
12556
12557         cancel_lru_locks mdc
12558         cancel_lru_locks osc
12559
12560 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
12561         lctl set_param fail_loc=0x80000803
12562         ls -lR $DIR/$tdir > /dev/null
12563         log "ls done"
12564         lctl set_param fail_loc=0x0
12565         lctl get_param -n llite.*.statahead_stats
12566         rm -r $DIR/$tdir
12567         sync
12568
12569 }
12570 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
12571
12572 test_123c() {
12573         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
12574
12575         test_mkdir -i 0 -c 1 $DIR/$tdir.0
12576         test_mkdir -i 1 -c 1 $DIR/$tdir.1
12577         touch $DIR/$tdir.1/{1..3}
12578         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
12579
12580         remount_client $MOUNT
12581
12582         $MULTIOP $DIR/$tdir.0 Q
12583
12584         # let statahead to complete
12585         ls -l $DIR/$tdir.0 > /dev/null
12586
12587         testid=$(echo $TESTNAME | tr '_' ' ')
12588         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
12589                 error "statahead warning" || true
12590 }
12591 run_test 123c "Can not initialize inode warning on DNE statahead"
12592
12593 test_124a() {
12594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12595         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12596                 skip_env "no lru resize on server"
12597
12598         local NR=2000
12599
12600         test_mkdir $DIR/$tdir
12601
12602         log "create $NR files at $DIR/$tdir"
12603         createmany -o $DIR/$tdir/f $NR ||
12604                 error "failed to create $NR files in $DIR/$tdir"
12605
12606         cancel_lru_locks mdc
12607         ls -l $DIR/$tdir > /dev/null
12608
12609         local NSDIR=""
12610         local LRU_SIZE=0
12611         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
12612                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
12613                 LRU_SIZE=$($LCTL get_param -n $PARAM)
12614                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
12615                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
12616                         log "NSDIR=$NSDIR"
12617                         log "NS=$(basename $NSDIR)"
12618                         break
12619                 fi
12620         done
12621
12622         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
12623                 skip "Not enough cached locks created!"
12624         fi
12625         log "LRU=$LRU_SIZE"
12626
12627         local SLEEP=30
12628
12629         # We know that lru resize allows one client to hold $LIMIT locks
12630         # for 10h. After that locks begin to be killed by client.
12631         local MAX_HRS=10
12632         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
12633         log "LIMIT=$LIMIT"
12634         if [ $LIMIT -lt $LRU_SIZE ]; then
12635                 skip "Limit is too small $LIMIT"
12636         fi
12637
12638         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
12639         # killing locks. Some time was spent for creating locks. This means
12640         # that up to the moment of sleep finish we must have killed some of
12641         # them (10-100 locks). This depends on how fast ther were created.
12642         # Many of them were touched in almost the same moment and thus will
12643         # be killed in groups.
12644         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE * 100))
12645
12646         # Use $LRU_SIZE_B here to take into account real number of locks
12647         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
12648         local LRU_SIZE_B=$LRU_SIZE
12649         log "LVF=$LVF"
12650         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
12651         log "OLD_LVF=$OLD_LVF"
12652         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
12653
12654         # Let's make sure that we really have some margin. Client checks
12655         # cached locks every 10 sec.
12656         SLEEP=$((SLEEP+20))
12657         log "Sleep ${SLEEP} sec"
12658         local SEC=0
12659         while ((SEC<$SLEEP)); do
12660                 echo -n "..."
12661                 sleep 5
12662                 SEC=$((SEC+5))
12663                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
12664                 echo -n "$LRU_SIZE"
12665         done
12666         echo ""
12667         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
12668         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
12669
12670         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
12671                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
12672                 unlinkmany $DIR/$tdir/f $NR
12673                 return
12674         }
12675
12676         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
12677         log "unlink $NR files at $DIR/$tdir"
12678         unlinkmany $DIR/$tdir/f $NR
12679 }
12680 run_test 124a "lru resize ======================================="
12681
12682 get_max_pool_limit()
12683 {
12684         local limit=$($LCTL get_param \
12685                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
12686         local max=0
12687         for l in $limit; do
12688                 if [[ $l -gt $max ]]; then
12689                         max=$l
12690                 fi
12691         done
12692         echo $max
12693 }
12694
12695 test_124b() {
12696         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12697         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12698                 skip_env "no lru resize on server"
12699
12700         LIMIT=$(get_max_pool_limit)
12701
12702         NR=$(($(default_lru_size)*20))
12703         if [[ $NR -gt $LIMIT ]]; then
12704                 log "Limit lock number by $LIMIT locks"
12705                 NR=$LIMIT
12706         fi
12707
12708         IFree=$(mdsrate_inodes_available)
12709         if [ $IFree -lt $NR ]; then
12710                 log "Limit lock number by $IFree inodes"
12711                 NR=$IFree
12712         fi
12713
12714         lru_resize_disable mdc
12715         test_mkdir -p $DIR/$tdir/disable_lru_resize
12716
12717         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
12718         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
12719         cancel_lru_locks mdc
12720         stime=`date +%s`
12721         PID=""
12722         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12723         PID="$PID $!"
12724         sleep 2
12725         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12726         PID="$PID $!"
12727         sleep 2
12728         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12729         PID="$PID $!"
12730         wait $PID
12731         etime=`date +%s`
12732         nolruresize_delta=$((etime-stime))
12733         log "ls -la time: $nolruresize_delta seconds"
12734         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
12735         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
12736
12737         lru_resize_enable mdc
12738         test_mkdir -p $DIR/$tdir/enable_lru_resize
12739
12740         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
12741         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
12742         cancel_lru_locks mdc
12743         stime=`date +%s`
12744         PID=""
12745         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
12746         PID="$PID $!"
12747         sleep 2
12748         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
12749         PID="$PID $!"
12750         sleep 2
12751         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
12752         PID="$PID $!"
12753         wait $PID
12754         etime=`date +%s`
12755         lruresize_delta=$((etime-stime))
12756         log "ls -la time: $lruresize_delta seconds"
12757         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
12758
12759         if [ $lruresize_delta -gt $nolruresize_delta ]; then
12760                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
12761         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
12762                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
12763         else
12764                 log "lru resize performs the same with no lru resize"
12765         fi
12766         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
12767 }
12768 run_test 124b "lru resize (performance test) ======================="
12769
12770 test_124c() {
12771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12772         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12773                 skip_env "no lru resize on server"
12774
12775         # cache ununsed locks on client
12776         local nr=100
12777         cancel_lru_locks mdc
12778         test_mkdir $DIR/$tdir
12779         createmany -o $DIR/$tdir/f $nr ||
12780                 error "failed to create $nr files in $DIR/$tdir"
12781         ls -l $DIR/$tdir > /dev/null
12782
12783         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12784         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12785         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
12786         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
12787         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
12788
12789         # set lru_max_age to 1 sec
12790         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
12791         echo "sleep $((recalc_p * 2)) seconds..."
12792         sleep $((recalc_p * 2))
12793
12794         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
12795         # restore lru_max_age
12796         $LCTL set_param -n $nsdir.lru_max_age $max_age
12797         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
12798         unlinkmany $DIR/$tdir/f $nr
12799 }
12800 run_test 124c "LRUR cancel very aged locks"
12801
12802 test_124d() {
12803         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12804         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12805                 skip_env "no lru resize on server"
12806
12807         # cache ununsed locks on client
12808         local nr=100
12809
12810         lru_resize_disable mdc
12811         stack_trap "lru_resize_enable mdc" EXIT
12812
12813         cancel_lru_locks mdc
12814
12815         # asynchronous object destroy at MDT could cause bl ast to client
12816         test_mkdir $DIR/$tdir
12817         createmany -o $DIR/$tdir/f $nr ||
12818                 error "failed to create $nr files in $DIR/$tdir"
12819         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
12820
12821         ls -l $DIR/$tdir > /dev/null
12822
12823         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12824         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12825         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
12826         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
12827
12828         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
12829
12830         # set lru_max_age to 1 sec
12831         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
12832         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
12833
12834         echo "sleep $((recalc_p * 2)) seconds..."
12835         sleep $((recalc_p * 2))
12836
12837         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
12838
12839         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
12840 }
12841 run_test 124d "cancel very aged locks if lru-resize diasbaled"
12842
12843 test_125() { # 13358
12844         $LCTL get_param -n llite.*.client_type | grep -q local ||
12845                 skip "must run as local client"
12846         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
12847                 skip_env "must have acl enabled"
12848         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
12849
12850         test_mkdir $DIR/$tdir
12851         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
12852         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
12853         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
12854 }
12855 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
12856
12857 test_126() { # bug 12829/13455
12858         $GSS && skip_env "must run as gss disabled"
12859         $LCTL get_param -n llite.*.client_type | grep -q local ||
12860                 skip "must run as local client"
12861         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
12862
12863         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
12864         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
12865         rm -f $DIR/$tfile
12866         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
12867 }
12868 run_test 126 "check that the fsgid provided by the client is taken into account"
12869
12870 test_127a() { # bug 15521
12871         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12872         local name count samp unit min max sum sumsq
12873
12874         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
12875         echo "stats before reset"
12876         $LCTL get_param osc.*.stats
12877         $LCTL set_param osc.*.stats=0
12878         local fsize=$((2048 * 1024))
12879
12880         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
12881         cancel_lru_locks osc
12882         dd if=$DIR/$tfile of=/dev/null bs=$fsize
12883
12884         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
12885         stack_trap "rm -f $TMP/$tfile.tmp"
12886         while read name count samp unit min max sum sumsq; do
12887                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
12888                 [ ! $min ] && error "Missing min value for $name proc entry"
12889                 eval $name=$count || error "Wrong proc format"
12890
12891                 case $name in
12892                 read_bytes|write_bytes)
12893                         [[ "$unit" =~ "bytes" ]] ||
12894                                 error "unit is not 'bytes': $unit"
12895                         (( $min >= 4096 )) || error "min is too small: $min"
12896                         (( $min <= $fsize )) || error "min is too big: $min"
12897                         (( $max >= 4096 )) || error "max is too small: $max"
12898                         (( $max <= $fsize )) || error "max is too big: $max"
12899                         (( $sum == $fsize )) || error "sum is wrong: $sum"
12900                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
12901                                 error "sumsquare is too small: $sumsq"
12902                         (( $sumsq <= $fsize * $fsize )) ||
12903                                 error "sumsquare is too big: $sumsq"
12904                         ;;
12905                 ost_read|ost_write)
12906                         [[ "$unit" =~ "usec" ]] ||
12907                                 error "unit is not 'usec': $unit"
12908                         ;;
12909                 *)      ;;
12910                 esac
12911         done < $DIR/$tfile.tmp
12912
12913         #check that we actually got some stats
12914         [ "$read_bytes" ] || error "Missing read_bytes stats"
12915         [ "$write_bytes" ] || error "Missing write_bytes stats"
12916         [ "$read_bytes" != 0 ] || error "no read done"
12917         [ "$write_bytes" != 0 ] || error "no write done"
12918 }
12919 run_test 127a "verify the client stats are sane"
12920
12921 test_127b() { # bug LU-333
12922         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12923         local name count samp unit min max sum sumsq
12924
12925         echo "stats before reset"
12926         $LCTL get_param llite.*.stats
12927         $LCTL set_param llite.*.stats=0
12928
12929         # perform 2 reads and writes so MAX is different from SUM.
12930         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
12931         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
12932         cancel_lru_locks osc
12933         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
12934         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
12935
12936         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
12937         stack_trap "rm -f $TMP/$tfile.tmp"
12938         while read name count samp unit min max sum sumsq; do
12939                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
12940                 eval $name=$count || error "Wrong proc format"
12941
12942                 case $name in
12943                 read_bytes|write_bytes)
12944                         [[ "$unit" =~ "bytes" ]] ||
12945                                 error "unit is not 'bytes': $unit"
12946                         (( $count == 2 )) || error "count is not 2: $count"
12947                         (( $min == $PAGE_SIZE )) ||
12948                                 error "min is not $PAGE_SIZE: $min"
12949                         (( $max == $PAGE_SIZE )) ||
12950                                 error "max is not $PAGE_SIZE: $max"
12951                         (( $sum == $PAGE_SIZE * 2 )) ||
12952                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
12953                         ;;
12954                 read|write)
12955                         [[ "$unit" =~ "usec" ]] ||
12956                                 error "unit is not 'usec': $unit"
12957                         ;;
12958                 *)      ;;
12959                 esac
12960         done < $TMP/$tfile.tmp
12961
12962         #check that we actually got some stats
12963         [ "$read_bytes" ] || error "Missing read_bytes stats"
12964         [ "$write_bytes" ] || error "Missing write_bytes stats"
12965         [ "$read_bytes" != 0 ] || error "no read done"
12966         [ "$write_bytes" != 0 ] || error "no write done"
12967 }
12968 run_test 127b "verify the llite client stats are sane"
12969
12970 test_127c() { # LU-12394
12971         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
12972         local size
12973         local bsize
12974         local reads
12975         local writes
12976         local count
12977
12978         $LCTL set_param llite.*.extents_stats=1
12979         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
12980
12981         # Use two stripes so there is enough space in default config
12982         $LFS setstripe -c 2 $DIR/$tfile
12983
12984         # Extent stats start at 0-4K and go in power of two buckets
12985         # LL_HIST_START = 12 --> 2^12 = 4K
12986         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
12987         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
12988         # small configs
12989         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
12990                 do
12991                 # Write and read, 2x each, second time at a non-zero offset
12992                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
12993                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
12994                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
12995                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
12996                 rm -f $DIR/$tfile
12997         done
12998
12999         $LCTL get_param llite.*.extents_stats
13000
13001         count=2
13002         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
13003                 do
13004                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
13005                                 grep -m 1 $bsize)
13006                 reads=$(echo $bucket | awk '{print $5}')
13007                 writes=$(echo $bucket | awk '{print $9}')
13008                 [ "$reads" -eq $count ] ||
13009                         error "$reads reads in < $bsize bucket, expect $count"
13010                 [ "$writes" -eq $count ] ||
13011                         error "$writes writes in < $bsize bucket, expect $count"
13012         done
13013
13014         # Test mmap write and read
13015         $LCTL set_param llite.*.extents_stats=c
13016         size=512
13017         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
13018         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
13019         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
13020
13021         $LCTL get_param llite.*.extents_stats
13022
13023         count=$(((size*1024) / PAGE_SIZE))
13024
13025         bsize=$((2 * PAGE_SIZE / 1024))K
13026
13027         bucket=$($LCTL get_param -n llite.*.extents_stats |
13028                         grep -m 1 $bsize)
13029         reads=$(echo $bucket | awk '{print $5}')
13030         writes=$(echo $bucket | awk '{print $9}')
13031         # mmap writes fault in the page first, creating an additonal read
13032         [ "$reads" -eq $((2 * count)) ] ||
13033                 error "$reads reads in < $bsize bucket, expect $count"
13034         [ "$writes" -eq $count ] ||
13035                 error "$writes writes in < $bsize bucket, expect $count"
13036 }
13037 run_test 127c "test llite extent stats with regular & mmap i/o"
13038
13039 test_128() { # bug 15212
13040         touch $DIR/$tfile
13041         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
13042                 find $DIR/$tfile
13043                 find $DIR/$tfile
13044         EOF
13045
13046         result=$(grep error $TMP/$tfile.log)
13047         rm -f $DIR/$tfile $TMP/$tfile.log
13048         [ -z "$result" ] ||
13049                 error "consecutive find's under interactive lfs failed"
13050 }
13051 run_test 128 "interactive lfs for 2 consecutive find's"
13052
13053 set_dir_limits () {
13054         local mntdev
13055         local canondev
13056         local node
13057
13058         local ldproc=/proc/fs/ldiskfs
13059         local facets=$(get_facets MDS)
13060
13061         for facet in ${facets//,/ }; do
13062                 canondev=$(ldiskfs_canon \
13063                            *.$(convert_facet2label $facet).mntdev $facet)
13064                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
13065                         ldproc=/sys/fs/ldiskfs
13066                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
13067                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
13068         done
13069 }
13070
13071 check_mds_dmesg() {
13072         local facets=$(get_facets MDS)
13073         for facet in ${facets//,/ }; do
13074                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
13075         done
13076         return 1
13077 }
13078
13079 test_129() {
13080         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13081         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
13082                 skip "Need MDS version with at least 2.5.56"
13083         if [ "$mds1_FSTYPE" != ldiskfs ]; then
13084                 skip_env "ldiskfs only test"
13085         fi
13086         remote_mds_nodsh && skip "remote MDS with nodsh"
13087
13088         local ENOSPC=28
13089         local has_warning=false
13090
13091         rm -rf $DIR/$tdir
13092         mkdir -p $DIR/$tdir
13093
13094         # block size of mds1
13095         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
13096         set_dir_limits $maxsize $((maxsize * 6 / 8))
13097         stack_trap "set_dir_limits 0 0"
13098         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
13099         local dirsize=$(stat -c%s "$DIR/$tdir")
13100         local nfiles=0
13101         while (( $dirsize <= $maxsize )); do
13102                 $MCREATE $DIR/$tdir/file_base_$nfiles
13103                 rc=$?
13104                 # check two errors:
13105                 # ENOSPC for ext4 max_dir_size, which has been used since
13106                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
13107                 if (( rc == ENOSPC )); then
13108                         set_dir_limits 0 0
13109                         echo "rc=$rc returned as expected after $nfiles files"
13110
13111                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
13112                                 error "create failed w/o dir size limit"
13113
13114                         # messages may be rate limited if test is run repeatedly
13115                         check_mds_dmesg '"is approaching max"' ||
13116                                 echo "warning message should be output"
13117                         check_mds_dmesg '"has reached max"' ||
13118                                 echo "reached message should be output"
13119
13120                         dirsize=$(stat -c%s "$DIR/$tdir")
13121
13122                         [[ $dirsize -ge $maxsize ]] && return 0
13123                         error "dirsize $dirsize < $maxsize after $nfiles files"
13124                 elif (( rc != 0 )); then
13125                         break
13126                 fi
13127                 nfiles=$((nfiles + 1))
13128                 dirsize=$(stat -c%s "$DIR/$tdir")
13129         done
13130
13131         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
13132 }
13133 run_test 129 "test directory size limit ========================"
13134
13135 OLDIFS="$IFS"
13136 cleanup_130() {
13137         trap 0
13138         IFS="$OLDIFS"
13139 }
13140
13141 test_130a() {
13142         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13143         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13144
13145         trap cleanup_130 EXIT RETURN
13146
13147         local fm_file=$DIR/$tfile
13148         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
13149         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
13150                 error "dd failed for $fm_file"
13151
13152         # LU-1795: test filefrag/FIEMAP once, even if unsupported
13153         filefrag -ves $fm_file
13154         RC=$?
13155         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13156                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13157         [ $RC != 0 ] && error "filefrag $fm_file failed"
13158
13159         filefrag_op=$(filefrag -ve -k $fm_file |
13160                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13161         lun=$($LFS getstripe -i $fm_file)
13162
13163         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
13164         IFS=$'\n'
13165         tot_len=0
13166         for line in $filefrag_op
13167         do
13168                 frag_lun=`echo $line | cut -d: -f5`
13169                 ext_len=`echo $line | cut -d: -f4`
13170                 if (( $frag_lun != $lun )); then
13171                         cleanup_130
13172                         error "FIEMAP on 1-stripe file($fm_file) failed"
13173                         return
13174                 fi
13175                 (( tot_len += ext_len ))
13176         done
13177
13178         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
13179                 cleanup_130
13180                 error "FIEMAP on 1-stripe file($fm_file) failed;"
13181                 return
13182         fi
13183
13184         cleanup_130
13185
13186         echo "FIEMAP on single striped file succeeded"
13187 }
13188 run_test 130a "FIEMAP (1-stripe file)"
13189
13190 test_130b() {
13191         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13192
13193         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13194         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13195
13196         trap cleanup_130 EXIT RETURN
13197
13198         local fm_file=$DIR/$tfile
13199         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13200                         error "setstripe on $fm_file"
13201         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13202                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13203
13204         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
13205                 error "dd failed on $fm_file"
13206
13207         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13208         filefrag_op=$(filefrag -ve -k $fm_file |
13209                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13210
13211         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13212                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13213
13214         IFS=$'\n'
13215         tot_len=0
13216         num_luns=1
13217         for line in $filefrag_op
13218         do
13219                 frag_lun=$(echo $line | cut -d: -f5 |
13220                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13221                 ext_len=$(echo $line | cut -d: -f4)
13222                 if (( $frag_lun != $last_lun )); then
13223                         if (( tot_len != 1024 )); then
13224                                 cleanup_130
13225                                 error "FIEMAP on $fm_file failed; returned " \
13226                                 "len $tot_len for OST $last_lun instead of 1024"
13227                                 return
13228                         else
13229                                 (( num_luns += 1 ))
13230                                 tot_len=0
13231                         fi
13232                 fi
13233                 (( tot_len += ext_len ))
13234                 last_lun=$frag_lun
13235         done
13236         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
13237                 cleanup_130
13238                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13239                         "luns or wrong len for OST $last_lun"
13240                 return
13241         fi
13242
13243         cleanup_130
13244
13245         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
13246 }
13247 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
13248
13249 test_130c() {
13250         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13251
13252         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13253         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13254
13255         trap cleanup_130 EXIT RETURN
13256
13257         local fm_file=$DIR/$tfile
13258         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
13259         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13260                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13261
13262         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
13263                         error "dd failed on $fm_file"
13264
13265         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13266         filefrag_op=$(filefrag -ve -k $fm_file |
13267                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13268
13269         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13270                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13271
13272         IFS=$'\n'
13273         tot_len=0
13274         num_luns=1
13275         for line in $filefrag_op
13276         do
13277                 frag_lun=$(echo $line | cut -d: -f5 |
13278                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13279                 ext_len=$(echo $line | cut -d: -f4)
13280                 if (( $frag_lun != $last_lun )); then
13281                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
13282                         if (( logical != 512 )); then
13283                                 cleanup_130
13284                                 error "FIEMAP on $fm_file failed; returned " \
13285                                 "logical start for lun $logical instead of 512"
13286                                 return
13287                         fi
13288                         if (( tot_len != 512 )); then
13289                                 cleanup_130
13290                                 error "FIEMAP on $fm_file failed; returned " \
13291                                 "len $tot_len for OST $last_lun instead of 1024"
13292                                 return
13293                         else
13294                                 (( num_luns += 1 ))
13295                                 tot_len=0
13296                         fi
13297                 fi
13298                 (( tot_len += ext_len ))
13299                 last_lun=$frag_lun
13300         done
13301         if (( num_luns != 2 || tot_len != 512 )); then
13302                 cleanup_130
13303                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13304                         "luns or wrong len for OST $last_lun"
13305                 return
13306         fi
13307
13308         cleanup_130
13309
13310         echo "FIEMAP on 2-stripe file with hole succeeded"
13311 }
13312 run_test 130c "FIEMAP (2-stripe file with hole)"
13313
13314 test_130d() {
13315         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
13316
13317         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13318         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13319
13320         trap cleanup_130 EXIT RETURN
13321
13322         local fm_file=$DIR/$tfile
13323         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13324                         error "setstripe on $fm_file"
13325         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13326                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13327
13328         local actual_stripe_count=$($LFS getstripe -c $fm_file)
13329         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
13330                 error "dd failed on $fm_file"
13331
13332         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13333         filefrag_op=$(filefrag -ve -k $fm_file |
13334                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13335
13336         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13337                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13338
13339         IFS=$'\n'
13340         tot_len=0
13341         num_luns=1
13342         for line in $filefrag_op
13343         do
13344                 frag_lun=$(echo $line | cut -d: -f5 |
13345                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13346                 ext_len=$(echo $line | cut -d: -f4)
13347                 if (( $frag_lun != $last_lun )); then
13348                         if (( tot_len != 1024 )); then
13349                                 cleanup_130
13350                                 error "FIEMAP on $fm_file failed; returned " \
13351                                 "len $tot_len for OST $last_lun instead of 1024"
13352                                 return
13353                         else
13354                                 (( num_luns += 1 ))
13355                                 tot_len=0
13356                         fi
13357                 fi
13358                 (( tot_len += ext_len ))
13359                 last_lun=$frag_lun
13360         done
13361         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
13362                 cleanup_130
13363                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13364                         "luns or wrong len for OST $last_lun"
13365                 return
13366         fi
13367
13368         cleanup_130
13369
13370         echo "FIEMAP on N-stripe file succeeded"
13371 }
13372 run_test 130d "FIEMAP (N-stripe file)"
13373
13374 test_130e() {
13375         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13376
13377         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13378         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13379
13380         trap cleanup_130 EXIT RETURN
13381
13382         local fm_file=$DIR/$tfile
13383         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
13384
13385         NUM_BLKS=512
13386         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
13387         for ((i = 0; i < $NUM_BLKS; i++)); do
13388                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) \
13389                         conv=notrunc > /dev/null 2>&1
13390         done
13391
13392         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13393         filefrag_op=$(filefrag -ve -k $fm_file |
13394                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13395
13396         last_lun=$(echo $filefrag_op | cut -d: -f5)
13397
13398         IFS=$'\n'
13399         tot_len=0
13400         num_luns=1
13401         for line in $filefrag_op; do
13402                 frag_lun=$(echo $line | cut -d: -f5)
13403                 ext_len=$(echo $line | cut -d: -f4)
13404                 if [[ "$frag_lun" != "$last_lun" ]]; then
13405                         if (( tot_len != $EXPECTED_LEN )); then
13406                                 cleanup_130
13407                                 error "OST$last_lun $tot_len != $EXPECTED_LEN"
13408                         else
13409                                 (( num_luns += 1 ))
13410                                 tot_len=0
13411                         fi
13412                 fi
13413                 (( tot_len += ext_len ))
13414                 last_lun=$frag_lun
13415         done
13416         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
13417                 cleanup_130
13418                 error "OST$last_lun $num_luns != 2, $tot_len != $EXPECTED_LEN"
13419         fi
13420
13421         echo "FIEMAP with continuation calls succeeded"
13422 }
13423 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
13424
13425 test_130f() {
13426         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13427         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13428
13429         local fm_file=$DIR/$tfile
13430         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
13431                 error "multiop create with lov_delay_create on $fm_file"
13432
13433         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13434         filefrag_extents=$(filefrag -vek $fm_file |
13435                            awk '/extents? found/ { print $2 }')
13436         if [[ "$filefrag_extents" != "0" ]]; then
13437                 error "$fm_file: filefrag_extents=$filefrag_extents != 0"
13438         fi
13439
13440         rm -f $fm_file
13441 }
13442 run_test 130f "FIEMAP (unstriped file)"
13443
13444 test_130g() {
13445         local file=$DIR/$tfile
13446         local nr=$((OSTCOUNT * 100))
13447
13448         $LFS setstripe -C $nr $file ||
13449                 error "failed to setstripe -C $nr $file"
13450
13451         dd if=/dev/zero of=$file count=$nr bs=1M
13452         sync
13453         nr=$($LFS getstripe -c $file)
13454
13455         local extents=$(filefrag -v $file |
13456                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}' | wc -l)
13457
13458         echo "filefrag list $extents extents in file with stripecount $nr"
13459         if (( extents < nr )); then
13460                 $LFS getstripe $file
13461                 filefrag -v $file
13462                 error "filefrag printed $extents < $nr extents"
13463         fi
13464
13465         rm -f $file
13466 }
13467 run_test 130g "FIEMAP (overstripe file)"
13468
13469 # Test for writev/readv
13470 test_131a() {
13471         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
13472                 error "writev test failed"
13473         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
13474                 error "readv failed"
13475         rm -f $DIR/$tfile
13476 }
13477 run_test 131a "test iov's crossing stripe boundary for writev/readv"
13478
13479 test_131b() {
13480         local fsize=$((524288 + 1048576 + 1572864))
13481         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
13482                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
13483                         error "append writev test failed"
13484
13485         ((fsize += 1572864 + 1048576))
13486         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
13487                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
13488                         error "append writev test failed"
13489         rm -f $DIR/$tfile
13490 }
13491 run_test 131b "test append writev"
13492
13493 test_131c() {
13494         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
13495         error "NOT PASS"
13496 }
13497 run_test 131c "test read/write on file w/o objects"
13498
13499 test_131d() {
13500         rwv -f $DIR/$tfile -w -n 1 1572864
13501         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
13502         if [ "$NOB" != 1572864 ]; then
13503                 error "Short read filed: read $NOB bytes instead of 1572864"
13504         fi
13505         rm -f $DIR/$tfile
13506 }
13507 run_test 131d "test short read"
13508
13509 test_131e() {
13510         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
13511         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
13512         error "read hitting hole failed"
13513         rm -f $DIR/$tfile
13514 }
13515 run_test 131e "test read hitting hole"
13516
13517 check_stats() {
13518         local facet=$1
13519         local op=$2
13520         local want=${3:-0}
13521         local res
13522
13523         case $facet in
13524         mds*) res=$(do_facet $facet \
13525                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
13526                  ;;
13527         ost*) res=$(do_facet $facet \
13528                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
13529                  ;;
13530         *) error "Wrong facet '$facet'" ;;
13531         esac
13532         [ "$res" ] || error "The counter for $op on $facet was not incremented"
13533         # if the argument $3 is zero, it means any stat increment is ok.
13534         if [[ $want -gt 0 ]]; then
13535                 local count=$(echo $res | awk '{ print $2 }')
13536                 [[ $count -ne $want ]] &&
13537                         error "The $op counter on $facet is $count, not $want"
13538         fi
13539 }
13540
13541 test_133a() {
13542         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13543         remote_ost_nodsh && skip "remote OST with nodsh"
13544         remote_mds_nodsh && skip "remote MDS with nodsh"
13545         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
13546                 skip_env "MDS doesn't support rename stats"
13547
13548         local testdir=$DIR/${tdir}/stats_testdir
13549
13550         mkdir -p $DIR/${tdir}
13551
13552         # clear stats.
13553         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13554         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13555
13556         # verify mdt stats first.
13557         mkdir ${testdir} || error "mkdir failed"
13558         check_stats $SINGLEMDS "mkdir" 1
13559         touch ${testdir}/${tfile} || error "touch failed"
13560         check_stats $SINGLEMDS "open" 1
13561         check_stats $SINGLEMDS "close" 1
13562         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
13563                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
13564                 check_stats $SINGLEMDS "mknod" 2
13565         }
13566         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
13567         check_stats $SINGLEMDS "unlink" 1
13568         rm -f ${testdir}/${tfile} || error "file remove failed"
13569         check_stats $SINGLEMDS "unlink" 2
13570
13571         # remove working dir and check mdt stats again.
13572         rmdir ${testdir} || error "rmdir failed"
13573         check_stats $SINGLEMDS "rmdir" 1
13574
13575         local testdir1=$DIR/${tdir}/stats_testdir1
13576         mkdir -p ${testdir}
13577         mkdir -p ${testdir1}
13578         touch ${testdir1}/test1
13579         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
13580         check_stats $SINGLEMDS "crossdir_rename" 1
13581
13582         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
13583         check_stats $SINGLEMDS "samedir_rename" 1
13584
13585         rm -rf $DIR/${tdir}
13586 }
13587 run_test 133a "Verifying MDT stats ========================================"
13588
13589 test_133b() {
13590         local res
13591
13592         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13593         remote_ost_nodsh && skip "remote OST with nodsh"
13594         remote_mds_nodsh && skip "remote MDS with nodsh"
13595
13596         local testdir=$DIR/${tdir}/stats_testdir
13597
13598         mkdir -p ${testdir} || error "mkdir failed"
13599         touch ${testdir}/${tfile} || error "touch failed"
13600         cancel_lru_locks mdc
13601
13602         # clear stats.
13603         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13604         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13605
13606         # extra mdt stats verification.
13607         chmod 444 ${testdir}/${tfile} || error "chmod failed"
13608         check_stats $SINGLEMDS "setattr" 1
13609         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13610         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
13611         then            # LU-1740
13612                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
13613                 check_stats $SINGLEMDS "getattr" 1
13614         fi
13615         rm -rf $DIR/${tdir}
13616
13617         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
13618         # so the check below is not reliable
13619         [ $MDSCOUNT -eq 1 ] || return 0
13620
13621         # Sleep to avoid a cached response.
13622         #define OBD_STATFS_CACHE_SECONDS 1
13623         sleep 2
13624         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13625         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
13626         $LFS df || error "lfs failed"
13627         check_stats $SINGLEMDS "statfs" 1
13628
13629         # check aggregated statfs (LU-10018)
13630         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
13631                 return 0
13632         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
13633                 return 0
13634         sleep 2
13635         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13636         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
13637         df $DIR
13638         check_stats $SINGLEMDS "statfs" 1
13639
13640         # We want to check that the client didn't send OST_STATFS to
13641         # ost1 but the MDT also uses OST_STATFS for precreate. So some
13642         # extra care is needed here.
13643         if remote_mds; then
13644                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
13645                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
13646
13647                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
13648                 [ "$res" ] && error "OST got STATFS"
13649         fi
13650
13651         return 0
13652 }
13653 run_test 133b "Verifying extra MDT stats =================================="
13654
13655 test_133c() {
13656         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13657         remote_ost_nodsh && skip "remote OST with nodsh"
13658         remote_mds_nodsh && skip "remote MDS with nodsh"
13659
13660         local testdir=$DIR/$tdir/stats_testdir
13661
13662         test_mkdir -p $testdir
13663
13664         # verify obdfilter stats.
13665         $LFS setstripe -c 1 -i 0 $testdir/$tfile
13666         sync
13667         cancel_lru_locks osc
13668         wait_delete_completed
13669
13670         # clear stats.
13671         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13672         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13673
13674         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
13675                 error "dd failed"
13676         sync
13677         cancel_lru_locks osc
13678         check_stats ost1 "write" 1
13679
13680         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
13681         check_stats ost1 "read" 1
13682
13683         > $testdir/$tfile || error "truncate failed"
13684         check_stats ost1 "punch" 1
13685
13686         rm -f $testdir/$tfile || error "file remove failed"
13687         wait_delete_completed
13688         check_stats ost1 "destroy" 1
13689
13690         rm -rf $DIR/$tdir
13691 }
13692 run_test 133c "Verifying OST stats ========================================"
13693
13694 order_2() {
13695         local value=$1
13696         local orig=$value
13697         local order=1
13698
13699         while [ $value -ge 2 ]; do
13700                 order=$((order*2))
13701                 value=$((value/2))
13702         done
13703
13704         if [ $orig -gt $order ]; then
13705                 order=$((order*2))
13706         fi
13707         echo $order
13708 }
13709
13710 size_in_KMGT() {
13711     local value=$1
13712     local size=('K' 'M' 'G' 'T');
13713     local i=0
13714     local size_string=$value
13715
13716     while [ $value -ge 1024 ]; do
13717         if [ $i -gt 3 ]; then
13718             #T is the biggest unit we get here, if that is bigger,
13719             #just return XXXT
13720             size_string=${value}T
13721             break
13722         fi
13723         value=$((value >> 10))
13724         if [ $value -lt 1024 ]; then
13725             size_string=${value}${size[$i]}
13726             break
13727         fi
13728         i=$((i + 1))
13729     done
13730
13731     echo $size_string
13732 }
13733
13734 get_rename_size() {
13735         local size=$1
13736         local context=${2:-.}
13737         local sample=$(do_facet $SINGLEMDS $LCTL \
13738                 get_param mdt.$FSNAME-MDT0000.rename_stats |
13739                 grep -A1 $context |
13740                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
13741         echo $sample
13742 }
13743
13744 test_133d() {
13745         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13746         remote_ost_nodsh && skip "remote OST with nodsh"
13747         remote_mds_nodsh && skip "remote MDS with nodsh"
13748         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
13749                 skip_env "MDS doesn't support rename stats"
13750
13751         local testdir1=$DIR/${tdir}/stats_testdir1
13752         local testdir2=$DIR/${tdir}/stats_testdir2
13753         mkdir -p $DIR/${tdir}
13754
13755         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
13756
13757         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
13758         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
13759
13760         createmany -o $testdir1/test 512 || error "createmany failed"
13761
13762         # check samedir rename size
13763         mv ${testdir1}/test0 ${testdir1}/test_0
13764
13765         local testdir1_size=$(ls -l $DIR/${tdir} |
13766                 awk '/stats_testdir1/ {print $5}')
13767         local testdir2_size=$(ls -l $DIR/${tdir} |
13768                 awk '/stats_testdir2/ {print $5}')
13769
13770         testdir1_size=$(order_2 $testdir1_size)
13771         testdir2_size=$(order_2 $testdir2_size)
13772
13773         testdir1_size=$(size_in_KMGT $testdir1_size)
13774         testdir2_size=$(size_in_KMGT $testdir2_size)
13775
13776         echo "source rename dir size: ${testdir1_size}"
13777         echo "target rename dir size: ${testdir2_size}"
13778
13779         local cmd="do_facet $SINGLEMDS $LCTL "
13780         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
13781
13782         eval $cmd || error "$cmd failed"
13783         local samedir=$($cmd | grep 'same_dir')
13784         local same_sample=$(get_rename_size $testdir1_size)
13785         [ -z "$samedir" ] && error "samedir_rename_size count error"
13786         [[ $same_sample -eq 1 ]] ||
13787                 error "samedir_rename_size error $same_sample"
13788         echo "Check same dir rename stats success"
13789
13790         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
13791
13792         # check crossdir rename size
13793         mv ${testdir1}/test_0 ${testdir2}/test_0
13794
13795         testdir1_size=$(ls -l $DIR/${tdir} |
13796                 awk '/stats_testdir1/ {print $5}')
13797         testdir2_size=$(ls -l $DIR/${tdir} |
13798                 awk '/stats_testdir2/ {print $5}')
13799
13800         testdir1_size=$(order_2 $testdir1_size)
13801         testdir2_size=$(order_2 $testdir2_size)
13802
13803         testdir1_size=$(size_in_KMGT $testdir1_size)
13804         testdir2_size=$(size_in_KMGT $testdir2_size)
13805
13806         echo "source rename dir size: ${testdir1_size}"
13807         echo "target rename dir size: ${testdir2_size}"
13808
13809         eval $cmd || error "$cmd failed"
13810         local crossdir=$($cmd | grep 'crossdir')
13811         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
13812         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
13813         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
13814         [[ $src_sample -eq 1 ]] ||
13815                 error "crossdir_rename_size error $src_sample"
13816         [[ $tgt_sample -eq 1 ]] ||
13817                 error "crossdir_rename_size error $tgt_sample"
13818         echo "Check cross dir rename stats success"
13819         rm -rf $DIR/${tdir}
13820 }
13821 run_test 133d "Verifying rename_stats ========================================"
13822
13823 test_133e() {
13824         remote_mds_nodsh && skip "remote MDS with nodsh"
13825         remote_ost_nodsh && skip "remote OST with nodsh"
13826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13827
13828         local testdir=$DIR/${tdir}/stats_testdir
13829         local ctr f0 f1 bs=32768 count=42 sum
13830
13831         mkdir -p ${testdir} || error "mkdir failed"
13832
13833         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
13834
13835         for ctr in {write,read}_bytes; do
13836                 sync
13837                 cancel_lru_locks osc
13838
13839                 do_facet ost1 $LCTL set_param -n \
13840                         "obdfilter.*.exports.clear=clear"
13841
13842                 if [ $ctr = write_bytes ]; then
13843                         f0=/dev/zero
13844                         f1=${testdir}/${tfile}
13845                 else
13846                         f0=${testdir}/${tfile}
13847                         f1=/dev/null
13848                 fi
13849
13850                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
13851                         error "dd failed"
13852                 sync
13853                 cancel_lru_locks osc
13854
13855                 sum=$(do_facet ost1 $LCTL get_param \
13856                         "obdfilter.*.exports.*.stats" |
13857                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
13858                                 $1 == ctr { sum += $7 }
13859                                 END { printf("%0.0f", sum) }')
13860
13861                 if ((sum != bs * count)); then
13862                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
13863                 fi
13864         done
13865
13866         rm -rf $DIR/${tdir}
13867 }
13868 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
13869
13870 test_133f() {
13871         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
13872                 skip "too old lustre for get_param -R ($facet_ver)"
13873
13874         # verifying readability.
13875         $LCTL get_param -R '*' &> /dev/null
13876
13877         # Verifing writability with badarea_io.
13878         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
13879         local skipped_params='force_lbug|changelog_mask|daemon_file'
13880         $LCTL list_param -FR '*' | grep '=' | tr -d = |
13881                 egrep -v "$skipped_params" |
13882                 xargs -n 1 find $proc_dirs -name |
13883                 xargs -n 1 badarea_io ||
13884                 error "client badarea_io failed"
13885
13886         # remount the FS in case writes/reads /proc break the FS
13887         cleanup || error "failed to unmount"
13888         setup || error "failed to setup"
13889 }
13890 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
13891
13892 test_133g() {
13893         remote_mds_nodsh && skip "remote MDS with nodsh"
13894         remote_ost_nodsh && skip "remote OST with nodsh"
13895
13896         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
13897         local skipped_params="'force_lbug|changelog_mask|daemon_file'"
13898         local facet
13899         for facet in mds1 ost1; do
13900                 local facet_ver=$(lustre_version_code $facet)
13901                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
13902                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
13903                 else
13904                         log "$facet: too old lustre for get_param -R"
13905                 fi
13906                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
13907                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
13908                                 tr -d = | egrep -v $skipped_params |
13909                                 xargs -n 1 find $proc_dirs -name |
13910                                 xargs -n 1 badarea_io" ||
13911                                         error "$facet badarea_io failed"
13912                 else
13913                         skip_noexit "$facet: too old lustre for get_param -R"
13914                 fi
13915         done
13916
13917         # remount the FS in case writes/reads /proc break the FS
13918         cleanup || error "failed to unmount"
13919         setup || error "failed to setup"
13920 }
13921 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
13922
13923 test_133h() {
13924         remote_mds_nodsh && skip "remote MDS with nodsh"
13925         remote_ost_nodsh && skip "remote OST with nodsh"
13926         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
13927                 skip "Need MDS version at least 2.9.54"
13928
13929         local facet
13930         for facet in client mds1 ost1; do
13931                 # Get the list of files that are missing the terminating newline
13932                 local plist=$(do_facet $facet
13933                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
13934                 local ent
13935                 for ent in $plist; do
13936                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
13937                                 awk -v FS='\v' -v RS='\v\v' \
13938                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
13939                                         print FILENAME}'" 2>/dev/null)
13940                         [ -z $missing ] || {
13941                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
13942                                 error "file does not end with newline: $facet-$ent"
13943                         }
13944                 done
13945         done
13946 }
13947 run_test 133h "Proc files should end with newlines"
13948
13949 test_134a() {
13950         remote_mds_nodsh && skip "remote MDS with nodsh"
13951         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
13952                 skip "Need MDS version at least 2.7.54"
13953
13954         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
13955         cancel_lru_locks mdc
13956
13957         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13958         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13959         [ $unused -eq 0 ] || error "$unused locks are not cleared"
13960
13961         local nr=1000
13962         createmany -o $DIR/$tdir/f $nr ||
13963                 error "failed to create $nr files in $DIR/$tdir"
13964         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13965
13966         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
13967         do_facet mds1 $LCTL set_param fail_loc=0x327
13968         do_facet mds1 $LCTL set_param fail_val=500
13969         touch $DIR/$tdir/m
13970
13971         echo "sleep 10 seconds ..."
13972         sleep 10
13973         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
13974
13975         do_facet mds1 $LCTL set_param fail_loc=0
13976         do_facet mds1 $LCTL set_param fail_val=0
13977         [ $lck_cnt -lt $unused ] ||
13978                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
13979
13980         rm $DIR/$tdir/m
13981         unlinkmany $DIR/$tdir/f $nr
13982 }
13983 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
13984
13985 test_134b() {
13986         remote_mds_nodsh && skip "remote MDS with nodsh"
13987         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
13988                 skip "Need MDS version at least 2.7.54"
13989
13990         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
13991         cancel_lru_locks mdc
13992
13993         local low_wm=$(do_facet mds1 $LCTL get_param -n \
13994                         ldlm.lock_reclaim_threshold_mb)
13995         # disable reclaim temporarily
13996         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
13997
13998         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
13999         do_facet mds1 $LCTL set_param fail_loc=0x328
14000         do_facet mds1 $LCTL set_param fail_val=500
14001
14002         $LCTL set_param debug=+trace
14003
14004         local nr=600
14005         createmany -o $DIR/$tdir/f $nr &
14006         local create_pid=$!
14007
14008         echo "Sleep $TIMEOUT seconds ..."
14009         sleep $TIMEOUT
14010         if ! ps -p $create_pid  > /dev/null 2>&1; then
14011                 do_facet mds1 $LCTL set_param fail_loc=0
14012                 do_facet mds1 $LCTL set_param fail_val=0
14013                 do_facet mds1 $LCTL set_param \
14014                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
14015                 error "createmany finished incorrectly!"
14016         fi
14017         do_facet mds1 $LCTL set_param fail_loc=0
14018         do_facet mds1 $LCTL set_param fail_val=0
14019         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
14020         wait $create_pid || return 1
14021
14022         unlinkmany $DIR/$tdir/f $nr
14023 }
14024 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
14025
14026 test_135() {
14027         remote_mds_nodsh && skip "remote MDS with nodsh"
14028         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14029                 skip "Need MDS version at least 2.13.50"
14030         local fname
14031
14032         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14033
14034 #define OBD_FAIL_PLAIN_RECORDS 0x1319
14035         #set only one record at plain llog
14036         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
14037
14038         #fill already existed plain llog each 64767
14039         #wrapping whole catalog
14040         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14041
14042         createmany -o $DIR/$tdir/$tfile_ 64700
14043         for (( i = 0; i < 64700; i = i + 2 ))
14044         do
14045                 rm $DIR/$tdir/$tfile_$i &
14046                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14047                 local pid=$!
14048                 wait $pid
14049         done
14050
14051         #waiting osp synchronization
14052         wait_delete_completed
14053 }
14054 run_test 135 "Race catalog processing"
14055
14056 test_136() {
14057         remote_mds_nodsh && skip "remote MDS with nodsh"
14058         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14059                 skip "Need MDS version at least 2.13.50"
14060         local fname
14061
14062         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14063         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
14064         #set only one record at plain llog
14065 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
14066         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
14067
14068         #fill already existed 2 plain llogs each 64767
14069         #wrapping whole catalog
14070         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14071         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
14072         wait_delete_completed
14073
14074         createmany -o $DIR/$tdir/$tfile_ 10
14075         sleep 25
14076
14077         do_facet $SINGLEMDS $LCTL set_param fail_val=3
14078         for (( i = 0; i < 10; i = i + 3 ))
14079         do
14080                 rm $DIR/$tdir/$tfile_$i &
14081                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14082                 local pid=$!
14083                 wait $pid
14084                 sleep 7
14085                 rm $DIR/$tdir/$tfile_$((i + 2)) &
14086         done
14087
14088         #waiting osp synchronization
14089         wait_delete_completed
14090 }
14091 run_test 136 "Race catalog processing 2"
14092
14093 test_140() { #bug-17379
14094         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14095
14096         test_mkdir $DIR/$tdir
14097         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
14098         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
14099
14100         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
14101         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
14102         local i=0
14103         while i=$((i + 1)); do
14104                 test_mkdir $i
14105                 cd $i || error "Changing to $i"
14106                 ln -s ../stat stat || error "Creating stat symlink"
14107                 # Read the symlink until ELOOP present,
14108                 # not LBUGing the system is considered success,
14109                 # we didn't overrun the stack.
14110                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
14111                 if [ $ret -ne 0 ]; then
14112                         if [ $ret -eq 40 ]; then
14113                                 break  # -ELOOP
14114                         else
14115                                 error "Open stat symlink"
14116                                         return
14117                         fi
14118                 fi
14119         done
14120         i=$((i - 1))
14121         echo "The symlink depth = $i"
14122         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
14123                 error "Invalid symlink depth"
14124
14125         # Test recursive symlink
14126         ln -s symlink_self symlink_self
14127         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
14128         echo "open symlink_self returns $ret"
14129         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
14130 }
14131 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
14132
14133 test_150a() {
14134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14135
14136         local TF="$TMP/$tfile"
14137
14138         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14139         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14140         cp $TF $DIR/$tfile
14141         cancel_lru_locks $OSC
14142         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
14143         remount_client $MOUNT
14144         df -P $MOUNT
14145         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
14146
14147         $TRUNCATE $TF 6000
14148         $TRUNCATE $DIR/$tfile 6000
14149         cancel_lru_locks $OSC
14150         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
14151
14152         echo "12345" >>$TF
14153         echo "12345" >>$DIR/$tfile
14154         cancel_lru_locks $OSC
14155         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
14156
14157         echo "12345" >>$TF
14158         echo "12345" >>$DIR/$tfile
14159         cancel_lru_locks $OSC
14160         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
14161 }
14162 run_test 150a "truncate/append tests"
14163
14164 test_150b() {
14165         check_set_fallocate_or_skip
14166
14167         touch $DIR/$tfile
14168         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14169         check_fallocate $DIR/$tfile || error "fallocate failed"
14170 }
14171 run_test 150b "Verify fallocate (prealloc) functionality"
14172
14173 test_150bb() {
14174         check_set_fallocate_or_skip
14175
14176         touch $DIR/$tfile
14177         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14178         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=20 || error "dd failed"
14179         > $DIR/$tfile
14180         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14181         # precomputed md5sum for 20MB of zeroes
14182         local expect="8f4e33f3dc3e414ff94e5fb6905cba8c"
14183         local sum=($(md5sum $DIR/$tfile))
14184
14185         [[ "${sum[0]}" == "$expect" ]] || error "fallocate unwritten is not zero"
14186
14187         check_set_fallocate 1
14188
14189         > $DIR/$tfile
14190         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14191         sum=($(md5sum $DIR/$tfile))
14192
14193         [[ "${sum[0]}" == "$expect" ]] || error "fallocate zero is not zero"
14194 }
14195 run_test 150bb "Verify fallocate modes both zero space"
14196
14197 test_150c() {
14198         check_set_fallocate_or_skip
14199         local striping="-c2"
14200
14201         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14202         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tfile || error "setstripe failed"
14203         fallocate -l ${OSTCOUNT}m $DIR/$tfile || error "fallocate failed"
14204         local bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14205         local want=$((OSTCOUNT * 1048576))
14206
14207         # Must allocate all requested space, not more than 5% extra
14208         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14209                 error "bytes $bytes is not $want"
14210
14211         rm -f $DIR/$tfile
14212
14213         echo "verify fallocate on PFL file"
14214
14215         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14216
14217         $LFS setstripe -E1M $striping -E16M -c3 -Eeof -c 4 $DIR/$tfile ||
14218                 error "Create $DIR/$tfile failed"
14219         fallocate -l $((1048576 * 512)) $DIR/$tfile ||
14220                         error "fallocate failed"
14221         bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14222         want=$((512 * 1048576))
14223
14224         # Must allocate all requested space, not more than 5% extra
14225         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14226                 error "bytes $bytes is not $want"
14227 }
14228 run_test 150c "Verify fallocate Size and Blocks"
14229
14230 test_150d() {
14231         check_set_fallocate_or_skip
14232         local striping="-c2"
14233
14234         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14235
14236         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14237         $LFS setstripe -E1M $striping -E eof -c $OSTCOUNT -S1M $DIR/$tdir ||
14238                 error "setstripe failed"
14239         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
14240         local bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
14241         local want=$((OSTCOUNT * 1048576))
14242
14243         # Must allocate all requested space, not more than 5% extra
14244         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14245                 error "bytes $bytes is not $want"
14246 }
14247 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
14248
14249 test_150e() {
14250         check_set_fallocate_or_skip
14251
14252         echo "df before:"
14253         $LFS df
14254         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14255         $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14256                 error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14257
14258         # Find OST with Minimum Size
14259         min_size_ost=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14260                        sort -un | head -1)
14261
14262         # Get 100MB per OST of the available space to reduce run time
14263         # else 60% of the available space if we are running SLOW tests
14264         if [ $SLOW == "no" ]; then
14265                 local space=$((1024 * 100 * OSTCOUNT))
14266         else
14267                 local space=$(((min_size_ost * 60)/100 * OSTCOUNT))
14268         fi
14269
14270         fallocate -l${space}k $DIR/$tfile ||
14271                 error "fallocate ${space}k $DIR/$tfile failed"
14272         echo "'fallocate -l ${space}k $DIR/$tfile' succeeded"
14273
14274         # get size immediately after fallocate. This should be correctly
14275         # updated
14276         local size=$(stat -c '%s' $DIR/$tfile)
14277         local used=$(( $(stat -c '%b * %B' $DIR/$tfile) / 1024))
14278
14279         # Sleep for a while for statfs to get updated. And not pull from cache.
14280         sleep 2
14281
14282         echo "df after fallocate:"
14283         $LFS df
14284
14285         (( size / 1024 == space )) || error "size $size != requested $space"
14286         [ "$ost1_FSTYPE" != ldiskfs ] || (( used >= space )) ||
14287                 error "used $used < space $space"
14288
14289         rm $DIR/$tfile || error "rm failed"
14290         sync
14291         wait_delete_completed
14292
14293         echo "df after unlink:"
14294         $LFS df
14295 }
14296 run_test 150e "Verify 60% of available OST space consumed by fallocate"
14297
14298 test_150f() {
14299         local size
14300         local blocks
14301         local want_size_before=20480 # in bytes
14302         local want_blocks_before=40 # 512 sized blocks
14303         local want_blocks_after=24  # 512 sized blocks
14304         local length=$(((want_blocks_before - want_blocks_after) * 512))
14305
14306         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
14307                 skip "need at least 2.14.0 for fallocate punch"
14308
14309         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
14310                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
14311         fi
14312
14313         check_set_fallocate_or_skip
14314         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14315
14316         [[ "x$DOM" == "xyes" ]] &&
14317                 $LFS setstripe -E1M -L mdt -E eof $DIR/$tfile
14318
14319         echo "Verify fallocate punch: Range within the file range"
14320         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
14321                 error "dd failed for bs 4096 and count 5"
14322
14323         # Call fallocate with punch range which is within the file range
14324         fallocate -p --offset 4096 -l $length $DIR/$tfile ||
14325                 error "fallocate failed: offset 4096 and length $length"
14326         # client must see changes immediately after fallocate
14327         size=$(stat -c '%s' $DIR/$tfile)
14328         blocks=$(stat -c '%b' $DIR/$tfile)
14329
14330         # Verify punch worked.
14331         (( blocks == want_blocks_after )) ||
14332                 error "punch failed: blocks $blocks != $want_blocks_after"
14333
14334         (( size == want_size_before )) ||
14335                 error "punch failed: size $size != $want_size_before"
14336
14337         # Verify there is hole in file
14338         local data_off=$(lseek_test -d 4096 $DIR/$tfile)
14339         # precomputed md5sum
14340         local expect="4a9a834a2db02452929c0a348273b4aa"
14341
14342         cksum=($(md5sum $DIR/$tfile))
14343         [[ "${cksum[0]}" == "$expect" ]] ||
14344                 error "unexpected MD5SUM after punch: ${cksum[0]}"
14345
14346         # Start second sub-case for fallocate punch.
14347         echo "Verify fallocate punch: Range overlapping and less than blocksize"
14348         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
14349                 error "dd failed for bs 4096 and count 5"
14350
14351         # Punch range less than block size will have no change in block count
14352         want_blocks_after=40  # 512 sized blocks
14353
14354         # Punch overlaps two blocks and less than blocksize
14355         fallocate -p --offset 4000 -l 3000 $DIR/$tfile ||
14356                 error "fallocate failed: offset 4000 length 3000"
14357         size=$(stat -c '%s' $DIR/$tfile)
14358         blocks=$(stat -c '%b' $DIR/$tfile)
14359
14360         # Verify punch worked.
14361         (( blocks == want_blocks_after )) ||
14362                 error "punch failed: blocks $blocks != $want_blocks_after"
14363
14364         (( size == want_size_before )) ||
14365                 error "punch failed: size $size != $want_size_before"
14366
14367         # Verify if range is really zero'ed out. We expect Zeros.
14368         # precomputed md5sum
14369         expect="c57ec5d769c3dbe3426edc3f7d7e11d3"
14370         cksum=($(md5sum $DIR/$tfile))
14371         [[ "${cksum[0]}" == "$expect" ]] ||
14372                 error "unexpected MD5SUM after punch: ${cksum[0]}"
14373 }
14374 run_test 150f "Verify fallocate punch functionality"
14375
14376 test_150g() {
14377         local space
14378         local size
14379         local blocks
14380         local blocks_after
14381         local size_after
14382         local BS=4096 # Block size in bytes
14383
14384         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
14385                 skip "need at least 2.14.0 for fallocate punch"
14386
14387         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
14388                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
14389         fi
14390
14391         check_set_fallocate_or_skip
14392         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14393
14394         if [[ "x$DOM" == "xyes" ]]; then
14395                 $LFS setstripe -E2M -L mdt -E eof -c${OSTCOUNT} $DIR/$tfile ||
14396                         error "$LFS setstripe DoM + ${OSTCOUNT} OST failed"
14397         else
14398                 $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14399                         error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14400         fi
14401
14402         # Get 100MB per OST of the available space to reduce run time
14403         # else 60% of the available space if we are running SLOW tests
14404         if [ $SLOW == "no" ]; then
14405                 space=$((1024 * 100 * OSTCOUNT))
14406         else
14407                 # Find OST with Minimum Size
14408                 space=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14409                         sort -un | head -1)
14410                 echo "min size OST: $space"
14411                 space=$(((space * 60)/100 * OSTCOUNT))
14412         fi
14413         # space in 1k units, round to 4k blocks
14414         local blkcount=$((space * 1024 / $BS))
14415
14416         echo "Verify fallocate punch: Very large Range"
14417         fallocate -l${space}k $DIR/$tfile ||
14418                 error "fallocate ${space}k $DIR/$tfile failed"
14419         # write 1M at the end, start and in the middle
14420         yes 'A' | dd of=$DIR/$tfile bs=$BS count=256 ||
14421                 error "dd failed: bs $BS count 256"
14422         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount - 256)) count=256 ||
14423                 error "dd failed: bs $BS count 256 seek $((blkcount - 256))"
14424         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount / 2)) count=1024 ||
14425                 error "dd failed: bs $BS count 256 seek $((blkcount / 2))"
14426
14427         # Gather stats.
14428         size=$(stat -c '%s' $DIR/$tfile)
14429
14430         # gather punch length.
14431         local punch_size=$((size - (BS * 2)))
14432
14433         echo "punch_size = $punch_size"
14434         echo "size - punch_size: $((size - punch_size))"
14435         echo "size - punch_size in blocks: $(((size - punch_size)/BS))"
14436
14437         # Call fallocate to punch all except 2 blocks. We leave the
14438         # first and the last block
14439         echo "fallocate -p --offset $BS -l $punch_size $DIR/$tfile"
14440         fallocate -p --offset $BS -l $punch_size $DIR/$tfile ||
14441                 error "fallocate failed: offset $BS length $punch_size"
14442
14443         size_after=$(stat -c '%s' $DIR/$tfile)
14444         blocks_after=$(stat -c '%b' $DIR/$tfile)
14445
14446         # Verify punch worked.
14447         # Size should be kept
14448         (( size == size_after )) ||
14449                 error "punch failed: size $size != $size_after"
14450
14451         # two 4k data blocks to remain plus possible 1 extra extent block
14452         (( blocks_after <= ((BS / 512) * 3) )) ||
14453                 error "too many blocks remains: $blocks_after"
14454
14455         # Verify that file has hole between the first and the last blocks
14456         local hole_start=$(lseek_test -l 0 $DIR/$tfile)
14457         local hole_end=$(lseek_test -d $BS $DIR/$tfile)
14458
14459         echo "Hole at [$hole_start, $hole_end)"
14460         (( hole_start == BS )) ||
14461                 error "no hole at offset $BS after punch"
14462
14463         (( hole_end == BS + punch_size )) ||
14464                 error "data at offset $hole_end < $((BS + punch_size))"
14465 }
14466 run_test 150g "Verify fallocate punch on large range"
14467
14468 #LU-2902 roc_hit was not able to read all values from lproc
14469 function roc_hit_init() {
14470         local list=$(comma_list $(osts_nodes))
14471         local dir=$DIR/$tdir-check
14472         local file=$dir/$tfile
14473         local BEFORE
14474         local AFTER
14475         local idx
14476
14477         test_mkdir $dir
14478         #use setstripe to do a write to every ost
14479         for i in $(seq 0 $((OSTCOUNT-1))); do
14480                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
14481                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
14482                 idx=$(printf %04x $i)
14483                 BEFORE=$(get_osd_param $list *OST*$idx stats |
14484                         awk '$1 == "cache_access" {sum += $7}
14485                                 END { printf("%0.0f", sum) }')
14486
14487                 cancel_lru_locks osc
14488                 cat $file >/dev/null
14489
14490                 AFTER=$(get_osd_param $list *OST*$idx stats |
14491                         awk '$1 == "cache_access" {sum += $7}
14492                                 END { printf("%0.0f", sum) }')
14493
14494                 echo BEFORE:$BEFORE AFTER:$AFTER
14495                 if ! let "AFTER - BEFORE == 4"; then
14496                         rm -rf $dir
14497                         error "roc_hit is not safe to use"
14498                 fi
14499                 rm $file
14500         done
14501
14502         rm -rf $dir
14503 }
14504
14505 function roc_hit() {
14506         local list=$(comma_list $(osts_nodes))
14507         echo $(get_osd_param $list '' stats |
14508                 awk '$1 == "cache_hit" {sum += $7}
14509                         END { printf("%0.0f", sum) }')
14510 }
14511
14512 function set_cache() {
14513         local on=1
14514
14515         if [ "$2" == "off" ]; then
14516                 on=0;
14517         fi
14518         local list=$(comma_list $(osts_nodes))
14519         set_osd_param $list '' $1_cache_enable $on
14520
14521         cancel_lru_locks osc
14522 }
14523
14524 test_151() {
14525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14526         remote_ost_nodsh && skip "remote OST with nodsh"
14527
14528         local CPAGES=3
14529         local list=$(comma_list $(osts_nodes))
14530
14531         # check whether obdfilter is cache capable at all
14532         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
14533                 skip "not cache-capable obdfilter"
14534         fi
14535
14536         # check cache is enabled on all obdfilters
14537         if get_osd_param $list '' read_cache_enable | grep 0; then
14538                 skip "oss cache is disabled"
14539         fi
14540
14541         set_osd_param $list '' writethrough_cache_enable 1
14542
14543         # check write cache is enabled on all obdfilters
14544         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
14545                 skip "oss write cache is NOT enabled"
14546         fi
14547
14548         roc_hit_init
14549
14550         #define OBD_FAIL_OBD_NO_LRU  0x609
14551         do_nodes $list $LCTL set_param fail_loc=0x609
14552
14553         # pages should be in the case right after write
14554         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
14555                 error "dd failed"
14556
14557         local BEFORE=$(roc_hit)
14558         cancel_lru_locks osc
14559         cat $DIR/$tfile >/dev/null
14560         local AFTER=$(roc_hit)
14561
14562         do_nodes $list $LCTL set_param fail_loc=0
14563
14564         if ! let "AFTER - BEFORE == CPAGES"; then
14565                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
14566         fi
14567
14568         cancel_lru_locks osc
14569         # invalidates OST cache
14570         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
14571         set_osd_param $list '' read_cache_enable 0
14572         cat $DIR/$tfile >/dev/null
14573
14574         # now data shouldn't be found in the cache
14575         BEFORE=$(roc_hit)
14576         cancel_lru_locks osc
14577         cat $DIR/$tfile >/dev/null
14578         AFTER=$(roc_hit)
14579         if let "AFTER - BEFORE != 0"; then
14580                 error "IN CACHE: before: $BEFORE, after: $AFTER"
14581         fi
14582
14583         set_osd_param $list '' read_cache_enable 1
14584         rm -f $DIR/$tfile
14585 }
14586 run_test 151 "test cache on oss and controls ==============================="
14587
14588 test_152() {
14589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14590
14591         local TF="$TMP/$tfile"
14592
14593         # simulate ENOMEM during write
14594 #define OBD_FAIL_OST_NOMEM      0x226
14595         lctl set_param fail_loc=0x80000226
14596         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14597         cp $TF $DIR/$tfile
14598         sync || error "sync failed"
14599         lctl set_param fail_loc=0
14600
14601         # discard client's cache
14602         cancel_lru_locks osc
14603
14604         # simulate ENOMEM during read
14605         lctl set_param fail_loc=0x80000226
14606         cmp $TF $DIR/$tfile || error "cmp failed"
14607         lctl set_param fail_loc=0
14608
14609         rm -f $TF
14610 }
14611 run_test 152 "test read/write with enomem ============================"
14612
14613 test_153() {
14614         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
14615 }
14616 run_test 153 "test if fdatasync does not crash ======================="
14617
14618 dot_lustre_fid_permission_check() {
14619         local fid=$1
14620         local ffid=$MOUNT/.lustre/fid/$fid
14621         local test_dir=$2
14622
14623         echo "stat fid $fid"
14624         stat $ffid > /dev/null || error "stat $ffid failed."
14625         echo "touch fid $fid"
14626         touch $ffid || error "touch $ffid failed."
14627         echo "write to fid $fid"
14628         cat /etc/hosts > $ffid || error "write $ffid failed."
14629         echo "read fid $fid"
14630         diff /etc/hosts $ffid || error "read $ffid failed."
14631         echo "append write to fid $fid"
14632         cat /etc/hosts >> $ffid || error "append write $ffid failed."
14633         echo "rename fid $fid"
14634         mv $ffid $test_dir/$tfile.1 &&
14635                 error "rename $ffid to $tfile.1 should fail."
14636         touch $test_dir/$tfile.1
14637         mv $test_dir/$tfile.1 $ffid &&
14638                 error "rename $tfile.1 to $ffid should fail."
14639         rm -f $test_dir/$tfile.1
14640         echo "truncate fid $fid"
14641         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
14642         echo "link fid $fid"
14643         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
14644         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
14645                 echo "setfacl fid $fid"
14646                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
14647                 echo "getfacl fid $fid"
14648                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
14649         fi
14650         echo "unlink fid $fid"
14651         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
14652         echo "mknod fid $fid"
14653         mknod $ffid c 1 3 && error "mknod $ffid should fail."
14654
14655         fid=[0xf00000400:0x1:0x0]
14656         ffid=$MOUNT/.lustre/fid/$fid
14657
14658         echo "stat non-exist fid $fid"
14659         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
14660         echo "write to non-exist fid $fid"
14661         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
14662         echo "link new fid $fid"
14663         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
14664
14665         mkdir -p $test_dir/$tdir
14666         touch $test_dir/$tdir/$tfile
14667         fid=$($LFS path2fid $test_dir/$tdir)
14668         rc=$?
14669         [ $rc -ne 0 ] &&
14670                 error "error: could not get fid for $test_dir/$dir/$tfile."
14671
14672         ffid=$MOUNT/.lustre/fid/$fid
14673
14674         echo "ls $fid"
14675         ls $ffid > /dev/null || error "ls $ffid failed."
14676         echo "touch $fid/$tfile.1"
14677         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
14678
14679         echo "touch $MOUNT/.lustre/fid/$tfile"
14680         touch $MOUNT/.lustre/fid/$tfile && \
14681                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
14682
14683         echo "setxattr to $MOUNT/.lustre/fid"
14684         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
14685
14686         echo "listxattr for $MOUNT/.lustre/fid"
14687         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
14688
14689         echo "delxattr from $MOUNT/.lustre/fid"
14690         setfattr -x trusted.name1 $MOUNT/.lustre/fid
14691
14692         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
14693         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
14694                 error "touch invalid fid should fail."
14695
14696         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
14697         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
14698                 error "touch non-normal fid should fail."
14699
14700         echo "rename $tdir to $MOUNT/.lustre/fid"
14701         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
14702                 error "rename to $MOUNT/.lustre/fid should fail."
14703
14704         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
14705         then            # LU-3547
14706                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
14707                 local new_obf_mode=777
14708
14709                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
14710                 chmod $new_obf_mode $DIR/.lustre/fid ||
14711                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
14712
14713                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
14714                 [ $obf_mode -eq $new_obf_mode ] ||
14715                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
14716
14717                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
14718                 chmod $old_obf_mode $DIR/.lustre/fid ||
14719                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
14720         fi
14721
14722         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
14723         fid=$($LFS path2fid $test_dir/$tfile-2)
14724
14725         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
14726         then # LU-5424
14727                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
14728                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
14729                         error "create lov data thru .lustre failed"
14730         fi
14731         echo "cp /etc/passwd $test_dir/$tfile-2"
14732         cp /etc/passwd $test_dir/$tfile-2 ||
14733                 error "copy to $test_dir/$tfile-2 failed."
14734         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
14735         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
14736                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
14737
14738         rm -rf $test_dir/tfile.lnk
14739         rm -rf $test_dir/$tfile-2
14740 }
14741
14742 test_154A() {
14743         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
14744                 skip "Need MDS version at least 2.4.1"
14745
14746         local tf=$DIR/$tfile
14747         touch $tf
14748
14749         local fid=$($LFS path2fid $tf)
14750         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
14751
14752         # check that we get the same pathname back
14753         local rootpath
14754         local found
14755         for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do
14756                 echo "$rootpath $fid"
14757                 found=$($LFS fid2path $rootpath "$fid")
14758                 [ -z "$found" ] && error "fid2path unable to get '$fid' path"
14759                 [ "$found" == "$tf" ] || error "fid2path $found != $tf"
14760         done
14761
14762         # check wrong root path format
14763         rootpath=$MOUNT"_wrong"
14764         found=$($LFS fid2path $rootpath "$fid")
14765         [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)"
14766 }
14767 run_test 154A "lfs path2fid and fid2path basic checks"
14768
14769 test_154B() {
14770         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
14771                 skip "Need MDS version at least 2.4.1"
14772
14773         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
14774         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
14775         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
14776         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
14777
14778         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
14779         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
14780
14781         # check that we get the same pathname
14782         echo "PFID: $PFID, name: $name"
14783         local FOUND=$($LFS fid2path $MOUNT "$PFID")
14784         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
14785         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
14786                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
14787
14788         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
14789 }
14790 run_test 154B "verify the ll_decode_linkea tool"
14791
14792 test_154a() {
14793         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14794         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14795         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
14796                 skip "Need MDS version at least 2.2.51"
14797         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
14798
14799         cp /etc/hosts $DIR/$tfile
14800
14801         fid=$($LFS path2fid $DIR/$tfile)
14802         rc=$?
14803         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
14804
14805         dot_lustre_fid_permission_check "$fid" $DIR ||
14806                 error "dot lustre permission check $fid failed"
14807
14808         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
14809
14810         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
14811
14812         touch $MOUNT/.lustre/file &&
14813                 error "creation is not allowed under .lustre"
14814
14815         mkdir $MOUNT/.lustre/dir &&
14816                 error "mkdir is not allowed under .lustre"
14817
14818         rm -rf $DIR/$tfile
14819 }
14820 run_test 154a "Open-by-FID"
14821
14822 test_154b() {
14823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14824         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14825         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14826         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
14827                 skip "Need MDS version at least 2.2.51"
14828
14829         local remote_dir=$DIR/$tdir/remote_dir
14830         local MDTIDX=1
14831         local rc=0
14832
14833         mkdir -p $DIR/$tdir
14834         $LFS mkdir -i $MDTIDX $remote_dir ||
14835                 error "create remote directory failed"
14836
14837         cp /etc/hosts $remote_dir/$tfile
14838
14839         fid=$($LFS path2fid $remote_dir/$tfile)
14840         rc=$?
14841         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
14842
14843         dot_lustre_fid_permission_check "$fid" $remote_dir ||
14844                 error "dot lustre permission check $fid failed"
14845         rm -rf $DIR/$tdir
14846 }
14847 run_test 154b "Open-by-FID for remote directory"
14848
14849 test_154c() {
14850         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
14851                 skip "Need MDS version at least 2.4.1"
14852
14853         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
14854         local FID1=$($LFS path2fid $DIR/$tfile.1)
14855         local FID2=$($LFS path2fid $DIR/$tfile.2)
14856         local FID3=$($LFS path2fid $DIR/$tfile.3)
14857
14858         local N=1
14859         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
14860                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
14861                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
14862                 local want=FID$N
14863                 [ "$FID" = "${!want}" ] ||
14864                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
14865                 N=$((N + 1))
14866         done
14867
14868         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
14869         do
14870                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
14871                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
14872                 N=$((N + 1))
14873         done
14874 }
14875 run_test 154c "lfs path2fid and fid2path multiple arguments"
14876
14877 test_154d() {
14878         remote_mds_nodsh && skip "remote MDS with nodsh"
14879         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
14880                 skip "Need MDS version at least 2.5.53"
14881
14882         if remote_mds; then
14883                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
14884         else
14885                 nid="0@lo"
14886         fi
14887         local proc_ofile="mdt.*.exports.'$nid'.open_files"
14888         local fd
14889         local cmd
14890
14891         rm -f $DIR/$tfile
14892         touch $DIR/$tfile
14893
14894         local fid=$($LFS path2fid $DIR/$tfile)
14895         # Open the file
14896         fd=$(free_fd)
14897         cmd="exec $fd<$DIR/$tfile"
14898         eval $cmd
14899         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
14900         echo "$fid_list" | grep "$fid"
14901         rc=$?
14902
14903         cmd="exec $fd>/dev/null"
14904         eval $cmd
14905         if [ $rc -ne 0 ]; then
14906                 error "FID $fid not found in open files list $fid_list"
14907         fi
14908 }
14909 run_test 154d "Verify open file fid"
14910
14911 test_154e()
14912 {
14913         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
14914                 skip "Need MDS version at least 2.6.50"
14915
14916         if ls -a $MOUNT | grep -q '^\.lustre$'; then
14917                 error ".lustre returned by readdir"
14918         fi
14919 }
14920 run_test 154e ".lustre is not returned by readdir"
14921
14922 test_154f() {
14923         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
14924
14925         # create parent directory on a single MDT to avoid cross-MDT hardlinks
14926         mkdir_on_mdt0 $DIR/$tdir
14927         # test dirs inherit from its stripe
14928         mkdir -p $DIR/$tdir/foo1 || error "mkdir error"
14929         mkdir -p $DIR/$tdir/foo2 || error "mkdir error"
14930         cp /etc/hosts $DIR/$tdir/foo1/$tfile
14931         ln $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/link
14932         touch $DIR/f
14933
14934         # get fid of parents
14935         local FID0=$($LFS path2fid $DIR/$tdir)
14936         local FID1=$($LFS path2fid $DIR/$tdir/foo1)
14937         local FID2=$($LFS path2fid $DIR/$tdir/foo2)
14938         local FID3=$($LFS path2fid $DIR)
14939
14940         # check that path2fid --parents returns expected <parent_fid>/name
14941         # 1) test for a directory (single parent)
14942         local parent=$($LFS path2fid --parents $DIR/$tdir/foo1)
14943         [ "$parent" == "$FID0/foo1" ] ||
14944                 error "expected parent: $FID0/foo1, got: $parent"
14945
14946         # 2) test for a file with nlink > 1 (multiple parents)
14947         parent=$($LFS path2fid --parents $DIR/$tdir/foo1/$tfile)
14948         echo "$parent" | grep -F "$FID1/$tfile" ||
14949                 error "$FID1/$tfile not returned in parent list"
14950         echo "$parent" | grep -F "$FID2/link" ||
14951                 error "$FID2/link not returned in parent list"
14952
14953         # 3) get parent by fid
14954         local file_fid=$($LFS path2fid $DIR/$tdir/foo1/$tfile)
14955         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
14956         echo "$parent" | grep -F "$FID1/$tfile" ||
14957                 error "$FID1/$tfile not returned in parent list (by fid)"
14958         echo "$parent" | grep -F "$FID2/link" ||
14959                 error "$FID2/link not returned in parent list (by fid)"
14960
14961         # 4) test for entry in root directory
14962         parent=$($LFS path2fid --parents $DIR/f)
14963         echo "$parent" | grep -F "$FID3/f" ||
14964                 error "$FID3/f not returned in parent list"
14965
14966         # 5) test it on root directory
14967         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
14968                 error "$MOUNT should not have parents"
14969
14970         # enable xattr caching and check that linkea is correctly updated
14971         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
14972         save_lustre_params client "llite.*.xattr_cache" > $save
14973         lctl set_param llite.*.xattr_cache 1
14974
14975         # 6.1) linkea update on rename
14976         mv $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/$tfile.moved
14977
14978         # get parents by fid
14979         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
14980         # foo1 should no longer be returned in parent list
14981         echo "$parent" | grep -F "$FID1" &&
14982                 error "$FID1 should no longer be in parent list"
14983         # the new path should appear
14984         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
14985                 error "$FID2/$tfile.moved is not in parent list"
14986
14987         # 6.2) linkea update on unlink
14988         rm -f $DIR/$tdir/foo2/link
14989         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
14990         # foo2/link should no longer be returned in parent list
14991         echo "$parent" | grep -F "$FID2/link" &&
14992                 error "$FID2/link should no longer be in parent list"
14993         true
14994
14995         rm -f $DIR/f
14996         restore_lustre_params < $save
14997         rm -f $save
14998 }
14999 run_test 154f "get parent fids by reading link ea"
15000
15001 test_154g()
15002 {
15003         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15004         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
15005            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
15006                 skip "Need MDS version at least 2.6.92"
15007
15008         mkdir_on_mdt0 $DIR/$tdir
15009         llapi_fid_test -d $DIR/$tdir
15010 }
15011 run_test 154g "various llapi FID tests"
15012
15013 test_155_small_load() {
15014     local temp=$TMP/$tfile
15015     local file=$DIR/$tfile
15016
15017     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
15018         error "dd of=$temp bs=6096 count=1 failed"
15019     cp $temp $file
15020     cancel_lru_locks $OSC
15021     cmp $temp $file || error "$temp $file differ"
15022
15023     $TRUNCATE $temp 6000
15024     $TRUNCATE $file 6000
15025     cmp $temp $file || error "$temp $file differ (truncate1)"
15026
15027     echo "12345" >>$temp
15028     echo "12345" >>$file
15029     cmp $temp $file || error "$temp $file differ (append1)"
15030
15031     echo "12345" >>$temp
15032     echo "12345" >>$file
15033     cmp $temp $file || error "$temp $file differ (append2)"
15034
15035     rm -f $temp $file
15036     true
15037 }
15038
15039 test_155_big_load() {
15040         remote_ost_nodsh && skip "remote OST with nodsh"
15041
15042         local temp=$TMP/$tfile
15043         local file=$DIR/$tfile
15044
15045         free_min_max
15046         local cache_size=$(do_facet ost$((MAXI+1)) \
15047                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
15048         local large_file_size=$((cache_size * 2))
15049
15050         echo "OSS cache size: $cache_size KB"
15051         echo "Large file size: $large_file_size KB"
15052
15053         [ $MAXV -le $large_file_size ] &&
15054                 skip_env "max available OST size needs > $large_file_size KB"
15055
15056         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
15057
15058         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
15059                 error "dd of=$temp bs=$large_file_size count=1k failed"
15060         cp $temp $file
15061         ls -lh $temp $file
15062         cancel_lru_locks osc
15063         cmp $temp $file || error "$temp $file differ"
15064
15065         rm -f $temp $file
15066         true
15067 }
15068
15069 save_writethrough() {
15070         local facets=$(get_facets OST)
15071
15072         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
15073 }
15074
15075 test_155a() {
15076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15077
15078         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15079
15080         save_writethrough $p
15081
15082         set_cache read on
15083         set_cache writethrough on
15084         test_155_small_load
15085         restore_lustre_params < $p
15086         rm -f $p
15087 }
15088 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
15089
15090 test_155b() {
15091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15092
15093         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15094
15095         save_writethrough $p
15096
15097         set_cache read on
15098         set_cache writethrough off
15099         test_155_small_load
15100         restore_lustre_params < $p
15101         rm -f $p
15102 }
15103 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
15104
15105 test_155c() {
15106         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15107
15108         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15109
15110         save_writethrough $p
15111
15112         set_cache read off
15113         set_cache writethrough on
15114         test_155_small_load
15115         restore_lustre_params < $p
15116         rm -f $p
15117 }
15118 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
15119
15120 test_155d() {
15121         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15122
15123         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15124
15125         save_writethrough $p
15126
15127         set_cache read off
15128         set_cache writethrough off
15129         test_155_small_load
15130         restore_lustre_params < $p
15131         rm -f $p
15132 }
15133 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
15134
15135 test_155e() {
15136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15137
15138         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15139
15140         save_writethrough $p
15141
15142         set_cache read on
15143         set_cache writethrough on
15144         test_155_big_load
15145         restore_lustre_params < $p
15146         rm -f $p
15147 }
15148 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
15149
15150 test_155f() {
15151         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15152
15153         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15154
15155         save_writethrough $p
15156
15157         set_cache read on
15158         set_cache writethrough off
15159         test_155_big_load
15160         restore_lustre_params < $p
15161         rm -f $p
15162 }
15163 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
15164
15165 test_155g() {
15166         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15167
15168         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15169
15170         save_writethrough $p
15171
15172         set_cache read off
15173         set_cache writethrough on
15174         test_155_big_load
15175         restore_lustre_params < $p
15176         rm -f $p
15177 }
15178 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
15179
15180 test_155h() {
15181         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15182
15183         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15184
15185         save_writethrough $p
15186
15187         set_cache read off
15188         set_cache writethrough off
15189         test_155_big_load
15190         restore_lustre_params < $p
15191         rm -f $p
15192 }
15193 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
15194
15195 test_156() {
15196         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15197         remote_ost_nodsh && skip "remote OST with nodsh"
15198         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
15199                 skip "stats not implemented on old servers"
15200         [ "$ost1_FSTYPE" = "zfs" ] &&
15201                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
15202
15203         local CPAGES=3
15204         local BEFORE
15205         local AFTER
15206         local file="$DIR/$tfile"
15207         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15208
15209         save_writethrough $p
15210         roc_hit_init
15211
15212         log "Turn on read and write cache"
15213         set_cache read on
15214         set_cache writethrough on
15215
15216         log "Write data and read it back."
15217         log "Read should be satisfied from the cache."
15218         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15219         BEFORE=$(roc_hit)
15220         cancel_lru_locks osc
15221         cat $file >/dev/null
15222         AFTER=$(roc_hit)
15223         if ! let "AFTER - BEFORE == CPAGES"; then
15224                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
15225         else
15226                 log "cache hits: before: $BEFORE, after: $AFTER"
15227         fi
15228
15229         log "Read again; it should be satisfied from the cache."
15230         BEFORE=$AFTER
15231         cancel_lru_locks osc
15232         cat $file >/dev/null
15233         AFTER=$(roc_hit)
15234         if ! let "AFTER - BEFORE == CPAGES"; then
15235                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
15236         else
15237                 log "cache hits:: before: $BEFORE, after: $AFTER"
15238         fi
15239
15240         log "Turn off the read cache and turn on the write cache"
15241         set_cache read off
15242         set_cache writethrough on
15243
15244         log "Read again; it should be satisfied from the cache."
15245         BEFORE=$(roc_hit)
15246         cancel_lru_locks osc
15247         cat $file >/dev/null
15248         AFTER=$(roc_hit)
15249         if ! let "AFTER - BEFORE == CPAGES"; then
15250                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
15251         else
15252                 log "cache hits:: before: $BEFORE, after: $AFTER"
15253         fi
15254
15255         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15256                 # > 2.12.56 uses pagecache if cached
15257                 log "Read again; it should not be satisfied from the cache."
15258                 BEFORE=$AFTER
15259                 cancel_lru_locks osc
15260                 cat $file >/dev/null
15261                 AFTER=$(roc_hit)
15262                 if ! let "AFTER - BEFORE == 0"; then
15263                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
15264                 else
15265                         log "cache hits:: before: $BEFORE, after: $AFTER"
15266                 fi
15267         fi
15268
15269         log "Write data and read it back."
15270         log "Read should be satisfied from the cache."
15271         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15272         BEFORE=$(roc_hit)
15273         cancel_lru_locks osc
15274         cat $file >/dev/null
15275         AFTER=$(roc_hit)
15276         if ! let "AFTER - BEFORE == CPAGES"; then
15277                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
15278         else
15279                 log "cache hits:: before: $BEFORE, after: $AFTER"
15280         fi
15281
15282         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15283                 # > 2.12.56 uses pagecache if cached
15284                 log "Read again; it should not be satisfied from the cache."
15285                 BEFORE=$AFTER
15286                 cancel_lru_locks osc
15287                 cat $file >/dev/null
15288                 AFTER=$(roc_hit)
15289                 if ! let "AFTER - BEFORE == 0"; then
15290                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
15291                 else
15292                         log "cache hits:: before: $BEFORE, after: $AFTER"
15293                 fi
15294         fi
15295
15296         log "Turn off read and write cache"
15297         set_cache read off
15298         set_cache writethrough off
15299
15300         log "Write data and read it back"
15301         log "It should not be satisfied from the cache."
15302         rm -f $file
15303         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15304         cancel_lru_locks osc
15305         BEFORE=$(roc_hit)
15306         cat $file >/dev/null
15307         AFTER=$(roc_hit)
15308         if ! let "AFTER - BEFORE == 0"; then
15309                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
15310         else
15311                 log "cache hits:: before: $BEFORE, after: $AFTER"
15312         fi
15313
15314         log "Turn on the read cache and turn off the write cache"
15315         set_cache read on
15316         set_cache writethrough off
15317
15318         log "Write data and read it back"
15319         log "It should not be satisfied from the cache."
15320         rm -f $file
15321         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15322         BEFORE=$(roc_hit)
15323         cancel_lru_locks osc
15324         cat $file >/dev/null
15325         AFTER=$(roc_hit)
15326         if ! let "AFTER - BEFORE == 0"; then
15327                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
15328         else
15329                 log "cache hits:: before: $BEFORE, after: $AFTER"
15330         fi
15331
15332         log "Read again; it should be satisfied from the cache."
15333         BEFORE=$(roc_hit)
15334         cancel_lru_locks osc
15335         cat $file >/dev/null
15336         AFTER=$(roc_hit)
15337         if ! let "AFTER - BEFORE == CPAGES"; then
15338                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
15339         else
15340                 log "cache hits:: before: $BEFORE, after: $AFTER"
15341         fi
15342
15343         restore_lustre_params < $p
15344         rm -f $p $file
15345 }
15346 run_test 156 "Verification of tunables"
15347
15348 test_160a() {
15349         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15350         remote_mds_nodsh && skip "remote MDS with nodsh"
15351         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
15352                 skip "Need MDS version at least 2.2.0"
15353
15354         changelog_register || error "changelog_register failed"
15355         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
15356         changelog_users $SINGLEMDS | grep -q $cl_user ||
15357                 error "User $cl_user not found in changelog_users"
15358
15359         mkdir_on_mdt0 $DIR/$tdir
15360
15361         # change something
15362         test_mkdir -p $DIR/$tdir/pics/2008/zachy
15363         changelog_clear 0 || error "changelog_clear failed"
15364         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
15365         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
15366         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15367         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15368         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15369         rm $DIR/$tdir/pics/desktop.jpg
15370
15371         echo "verifying changelog mask"
15372         changelog_chmask "-MKDIR"
15373         changelog_chmask "-CLOSE"
15374
15375         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
15376         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
15377
15378         changelog_chmask "+MKDIR"
15379         changelog_chmask "+CLOSE"
15380
15381         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
15382         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
15383
15384         MKDIRS=$(changelog_dump | grep -c "MKDIR")
15385         CLOSES=$(changelog_dump | grep -c "CLOSE")
15386         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
15387         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
15388
15389         # verify contents
15390         echo "verifying target fid"
15391         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
15392         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
15393         [ "$fidc" == "$fidf" ] ||
15394                 error "changelog '$tfile' fid $fidc != file fid $fidf"
15395         echo "verifying parent fid"
15396         # The FID returned from the Changelog may be the directory shard on
15397         # a different MDT, and not the FID returned by path2fid on the parent.
15398         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
15399         # since this is what will matter when recreating this file in the tree.
15400         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
15401         local pathp=$($LFS fid2path $MOUNT "$fidp")
15402         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
15403                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
15404
15405         echo "getting records for $cl_user"
15406         changelog_users $SINGLEMDS
15407         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
15408         local nclr=3
15409         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
15410                 error "changelog_clear failed"
15411         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
15412         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
15413         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
15414                 error "user index expect $user_rec1 + $nclr != $user_rec2"
15415
15416         local min0_rec=$(changelog_users $SINGLEMDS |
15417                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
15418         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
15419                           awk '{ print $1; exit; }')
15420
15421         changelog_dump | tail -n 5
15422         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
15423         [ $first_rec == $((min0_rec + 1)) ] ||
15424                 error "first index should be $min0_rec + 1 not $first_rec"
15425
15426         # LU-3446 changelog index reset on MDT restart
15427         local cur_rec1=$(changelog_users $SINGLEMDS |
15428                          awk '/^current.index:/ { print $NF }')
15429         changelog_clear 0 ||
15430                 error "clear all changelog records for $cl_user failed"
15431         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
15432         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
15433                 error "Fail to start $SINGLEMDS"
15434         local cur_rec2=$(changelog_users $SINGLEMDS |
15435                          awk '/^current.index:/ { print $NF }')
15436         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
15437         [ $cur_rec1 == $cur_rec2 ] ||
15438                 error "current index should be $cur_rec1 not $cur_rec2"
15439
15440         echo "verifying users from this test are deregistered"
15441         changelog_deregister || error "changelog_deregister failed"
15442         changelog_users $SINGLEMDS | grep -q $cl_user &&
15443                 error "User '$cl_user' still in changelog_users"
15444
15445         # lctl get_param -n mdd.*.changelog_users
15446         # current_index: 144
15447         # ID    index (idle seconds)
15448         # cl3   144   (2) mask=<list>
15449         if [ -z "$(changelog_users $SINGLEMDS | grep -v current.index)" ]; then
15450                 # this is the normal case where all users were deregistered
15451                 # make sure no new records are added when no users are present
15452                 local last_rec1=$(changelog_users $SINGLEMDS |
15453                                   awk '/^current.index:/ { print $NF }')
15454                 touch $DIR/$tdir/chloe
15455                 local last_rec2=$(changelog_users $SINGLEMDS |
15456                                   awk '/^current.index:/ { print $NF }')
15457                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
15458                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
15459         else
15460                 # any changelog users must be leftovers from a previous test
15461                 changelog_users $SINGLEMDS
15462                 echo "other changelog users; can't verify off"
15463         fi
15464 }
15465 run_test 160a "changelog sanity"
15466
15467 test_160b() { # LU-3587
15468         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15469         remote_mds_nodsh && skip "remote MDS with nodsh"
15470         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
15471                 skip "Need MDS version at least 2.2.0"
15472
15473         changelog_register || error "changelog_register failed"
15474         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
15475         changelog_users $SINGLEMDS | grep -q $cl_user ||
15476                 error "User '$cl_user' not found in changelog_users"
15477
15478         local longname1=$(str_repeat a 255)
15479         local longname2=$(str_repeat b 255)
15480
15481         cd $DIR
15482         echo "creating very long named file"
15483         touch $longname1 || error "create of '$longname1' failed"
15484         echo "renaming very long named file"
15485         mv $longname1 $longname2
15486
15487         changelog_dump | grep RENME | tail -n 5
15488         rm -f $longname2
15489 }
15490 run_test 160b "Verify that very long rename doesn't crash in changelog"
15491
15492 test_160c() {
15493         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15494         remote_mds_nodsh && skip "remote MDS with nodsh"
15495
15496         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
15497                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
15498                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
15499                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
15500
15501         local rc=0
15502
15503         # Registration step
15504         changelog_register || error "changelog_register failed"
15505
15506         rm -rf $DIR/$tdir
15507         mkdir -p $DIR/$tdir
15508         $MCREATE $DIR/$tdir/foo_160c
15509         changelog_chmask "-TRUNC"
15510         $TRUNCATE $DIR/$tdir/foo_160c 200
15511         changelog_chmask "+TRUNC"
15512         $TRUNCATE $DIR/$tdir/foo_160c 199
15513         changelog_dump | tail -n 5
15514         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
15515         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
15516 }
15517 run_test 160c "verify that changelog log catch the truncate event"
15518
15519 test_160d() {
15520         remote_mds_nodsh && skip "remote MDS with nodsh"
15521         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15523         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
15524                 skip "Need MDS version at least 2.7.60"
15525
15526         # Registration step
15527         changelog_register || error "changelog_register failed"
15528
15529         mkdir -p $DIR/$tdir/migrate_dir
15530         changelog_clear 0 || error "changelog_clear failed"
15531
15532         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
15533         changelog_dump | tail -n 5
15534         local migrates=$(changelog_dump | grep -c "MIGRT")
15535         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
15536 }
15537 run_test 160d "verify that changelog log catch the migrate event"
15538
15539 test_160e() {
15540         remote_mds_nodsh && skip "remote MDS with nodsh"
15541
15542         # Create a user
15543         changelog_register || error "changelog_register failed"
15544
15545         local MDT0=$(facet_svc $SINGLEMDS)
15546         local rc
15547
15548         # No user (expect fail)
15549         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister
15550         rc=$?
15551         if [ $rc -eq 0 ]; then
15552                 error "Should fail without user"
15553         elif [ $rc -ne 4 ]; then
15554                 error "changelog_deregister failed with $rc, expect 4(CMD_HELP)"
15555         fi
15556
15557         # Delete a future user (expect fail)
15558         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
15559         rc=$?
15560         if [ $rc -eq 0 ]; then
15561                 error "Deleted non-existant user cl77"
15562         elif [ $rc -ne 2 ]; then
15563                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
15564         fi
15565
15566         # Clear to a bad index (1 billion should be safe)
15567         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
15568         rc=$?
15569
15570         if [ $rc -eq 0 ]; then
15571                 error "Successfully cleared to invalid CL index"
15572         elif [ $rc -ne 22 ]; then
15573                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
15574         fi
15575 }
15576 run_test 160e "changelog negative testing (should return errors)"
15577
15578 test_160f() {
15579         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15580         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15581                 skip "Need MDS version at least 2.10.56"
15582
15583         local mdts=$(comma_list $(mdts_nodes))
15584
15585         # Create a user
15586         changelog_register || error "first changelog_register failed"
15587         changelog_register || error "second changelog_register failed"
15588         local cl_users
15589         declare -A cl_user1
15590         declare -A cl_user2
15591         local user_rec1
15592         local user_rec2
15593         local i
15594
15595         # generate some changelog records to accumulate on each MDT
15596         # use all_char because created files should be evenly distributed
15597         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
15598                 error "test_mkdir $tdir failed"
15599         log "$(date +%s): creating first files"
15600         for ((i = 0; i < MDSCOUNT * 2; i++)); do
15601                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT)) ||
15602                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT)) failed"
15603         done
15604
15605         # check changelogs have been generated
15606         local start=$SECONDS
15607         local idle_time=$((MDSCOUNT * 5 + 5))
15608         local nbcl=$(changelog_dump | wc -l)
15609         [[ $nbcl -eq 0 ]] && error "no changelogs found"
15610
15611         for param in "changelog_max_idle_time=$idle_time" \
15612                      "changelog_gc=1" \
15613                      "changelog_min_gc_interval=2" \
15614                      "changelog_min_free_cat_entries=3"; do
15615                 local MDT0=$(facet_svc $SINGLEMDS)
15616                 local var="${param%=*}"
15617                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
15618
15619                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
15620                 do_nodes $mdts $LCTL set_param mdd.*.$param
15621         done
15622
15623         # force cl_user2 to be idle (1st part), but also cancel the
15624         # cl_user1 records so that it is not evicted later in the test.
15625         local sleep1=$((idle_time / 2))
15626         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
15627         sleep $sleep1
15628
15629         # simulate changelog catalog almost full
15630         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
15631         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
15632
15633         for i in $(seq $MDSCOUNT); do
15634                 cl_users=(${CL_USERS[mds$i]})
15635                 cl_user1[mds$i]="${cl_users[0]}"
15636                 cl_user2[mds$i]="${cl_users[1]}"
15637
15638                 [ -n "${cl_user1[mds$i]}" ] ||
15639                         error "mds$i: no user registered"
15640                 [ -n "${cl_user2[mds$i]}" ] ||
15641                         error "mds$i: only ${cl_user2[mds$i]} is registered"
15642
15643                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15644                 [ -n "$user_rec1" ] ||
15645                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15646                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
15647                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15648                 [ -n "$user_rec2" ] ||
15649                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15650                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
15651                      "$user_rec1 + 2 == $user_rec2"
15652                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
15653                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
15654                               "$user_rec1 + 2, but is $user_rec2"
15655                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
15656                 [ -n "$user_rec2" ] ||
15657                         error "mds$i: User ${cl_user2[mds$i]} not registered"
15658                 [ $user_rec1 == $user_rec2 ] ||
15659                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
15660                               "$user_rec1, but is $user_rec2"
15661         done
15662
15663         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
15664         local sleep2=$((idle_time - (SECONDS - start) + 1))
15665         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
15666         sleep $sleep2
15667
15668         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
15669         # cl_user1 should be OK because it recently processed records.
15670         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
15671         for ((i = 0; i < MDSCOUNT * 2; i++)); do
15672                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT+2))||
15673                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT+2)) failed"
15674         done
15675
15676         # ensure gc thread is done
15677         for i in $(mdts_nodes); do
15678                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
15679                         error "$i: GC-thread not done"
15680         done
15681
15682         local first_rec
15683         for (( i = 1; i <= MDSCOUNT; i++ )); do
15684                 # check cl_user1 still registered
15685                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
15686                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15687                 # check cl_user2 unregistered
15688                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
15689                         error "mds$i: User ${cl_user2[mds$i]} still registered"
15690
15691                 # check changelogs are present and starting at $user_rec1 + 1
15692                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15693                 [ -n "$user_rec1" ] ||
15694                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15695                 first_rec=$($LFS changelog $(facet_svc mds$i) |
15696                             awk '{ print $1; exit; }')
15697
15698                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
15699                 [ $((user_rec1 + 1)) == $first_rec ] ||
15700                         error "mds$i: rec $first_rec != $user_rec1 + 1"
15701         done
15702 }
15703 run_test 160f "changelog garbage collect (timestamped users)"
15704
15705 test_160g() {
15706         remote_mds_nodsh && skip "remote MDS with nodsh"
15707         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15708                 skip "Need MDS version at least 2.10.56"
15709
15710         local mdts=$(comma_list $(mdts_nodes))
15711
15712         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
15713         do_nodes $mdts $LCTL set_param fail_loc=0x1314
15714
15715         # Create a user
15716         changelog_register || error "first changelog_register failed"
15717         changelog_register || error "second changelog_register failed"
15718         local cl_users
15719         declare -A cl_user1
15720         declare -A cl_user2
15721         local user_rec1
15722         local user_rec2
15723         local i
15724
15725         # generate some changelog records to accumulate on each MDT
15726         # use all_char because created files should be evenly distributed
15727         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
15728                 error "test_mkdir $tdir failed"
15729         for ((i = 0; i < MDSCOUNT; i++)); do
15730                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
15731                         error "create $DIR/$tdir/d$i.1 failed"
15732         done
15733
15734         # check changelogs have been generated
15735         local nbcl=$(changelog_dump | wc -l)
15736         (( $nbcl > 0 )) || error "no changelogs found"
15737
15738         # reduce the max_idle_indexes value to make sure we exceed it
15739         for param in "changelog_max_idle_indexes=1" \
15740                      "changelog_gc=1" \
15741                      "changelog_min_gc_interval=2" \
15742                      "changelog_min_free_cat_entries=3"; do
15743                 local MDT0=$(facet_svc $SINGLEMDS)
15744                 local var="${param%=*}"
15745                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
15746
15747                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
15748                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
15749                         error "unable to set mdd.*.$param"
15750         done
15751
15752         # simulate changelog catalog almost full
15753         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
15754         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
15755
15756         local start=$SECONDS
15757         for i in $(seq $MDSCOUNT); do
15758                 cl_users=(${CL_USERS[mds$i]})
15759                 cl_user1[mds$i]="${cl_users[0]}"
15760                 cl_user2[mds$i]="${cl_users[1]}"
15761
15762                 [ -n "${cl_user1[mds$i]}" ] ||
15763                         error "mds$i: no user registered"
15764                 [ -n "${cl_user2[mds$i]}" ] ||
15765                         error "mds$i: only ${cl_user1[mds$i]} is registered"
15766
15767                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15768                 [ -n "$user_rec1" ] ||
15769                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15770                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
15771                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15772                 [ -n "$user_rec2" ] ||
15773                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15774                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
15775                      "$user_rec1 + 2 == $user_rec2"
15776                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
15777                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
15778                               "$user_rec1 + 2, but is $user_rec2"
15779                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
15780                 [ -n "$user_rec2" ] ||
15781                         error "mds$i: User ${cl_user2[mds$i]} not registered"
15782                 [ $user_rec1 == $user_rec2 ] ||
15783                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
15784                               "$user_rec1, but is $user_rec2"
15785         done
15786
15787         # ensure we are past the previous changelog_min_gc_interval set above
15788         local sleep2=$((start + 2 - SECONDS))
15789         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
15790
15791         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
15792         # cl_user1 should be OK because it recently processed records.
15793         for ((i = 0; i < MDSCOUNT; i++)); do
15794                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 $DIR/$tdir/d$i.4 ||
15795                         error "create $DIR/$tdir/d$i.3 failed"
15796         done
15797
15798         # ensure gc thread is done
15799         for i in $(mdts_nodes); do
15800                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
15801                         error "$i: GC-thread not done"
15802         done
15803
15804         local first_rec
15805         for (( i = 1; i <= MDSCOUNT; i++ )); do
15806                 # check cl_user1 still registered
15807                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
15808                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15809                 # check cl_user2 unregistered
15810                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
15811                         error "mds$i: User ${cl_user2[mds$i]} still registered"
15812
15813                 # check changelogs are present and starting at $user_rec1 + 1
15814                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15815                 [ -n "$user_rec1" ] ||
15816                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15817                 first_rec=$($LFS changelog $(facet_svc mds$i) |
15818                             awk '{ print $1; exit; }')
15819
15820                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
15821                 [ $((user_rec1 + 1)) == $first_rec ] ||
15822                         error "mds$i: rec $first_rec != $user_rec1 + 1"
15823         done
15824 }
15825 run_test 160g "changelog garbage collect (old users)"
15826
15827 test_160h() {
15828         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15829         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15830                 skip "Need MDS version at least 2.10.56"
15831
15832         local mdts=$(comma_list $(mdts_nodes))
15833
15834         # Create a user
15835         changelog_register || error "first changelog_register failed"
15836         changelog_register || error "second changelog_register failed"
15837         local cl_users
15838         declare -A cl_user1
15839         declare -A cl_user2
15840         local user_rec1
15841         local user_rec2
15842         local i
15843
15844         # generate some changelog records to accumulate on each MDT
15845         # use all_char because created files should be evenly distributed
15846         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
15847                 error "test_mkdir $tdir failed"
15848         for ((i = 0; i < MDSCOUNT; i++)); do
15849                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
15850                         error "create $DIR/$tdir/d$i.1 failed"
15851         done
15852
15853         # check changelogs have been generated
15854         local nbcl=$(changelog_dump | wc -l)
15855         [[ $nbcl -eq 0 ]] && error "no changelogs found"
15856
15857         for param in "changelog_max_idle_time=10" \
15858                      "changelog_gc=1" \
15859                      "changelog_min_gc_interval=2"; do
15860                 local MDT0=$(facet_svc $SINGLEMDS)
15861                 local var="${param%=*}"
15862                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
15863
15864                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
15865                 do_nodes $mdts $LCTL set_param mdd.*.$param
15866         done
15867
15868         # force cl_user2 to be idle (1st part)
15869         sleep 9
15870
15871         for i in $(seq $MDSCOUNT); do
15872                 cl_users=(${CL_USERS[mds$i]})
15873                 cl_user1[mds$i]="${cl_users[0]}"
15874                 cl_user2[mds$i]="${cl_users[1]}"
15875
15876                 [ -n "${cl_user1[mds$i]}" ] ||
15877                         error "mds$i: no user registered"
15878                 [ -n "${cl_user2[mds$i]}" ] ||
15879                         error "mds$i: only ${cl_user2[mds$i]} is registered"
15880
15881                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15882                 [ -n "$user_rec1" ] ||
15883                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15884                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
15885                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15886                 [ -n "$user_rec2" ] ||
15887                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15888                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
15889                      "$user_rec1 + 2 == $user_rec2"
15890                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
15891                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
15892                               "$user_rec1 + 2, but is $user_rec2"
15893                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
15894                 [ -n "$user_rec2" ] ||
15895                         error "mds$i: User ${cl_user2[mds$i]} not registered"
15896                 [ $user_rec1 == $user_rec2 ] ||
15897                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
15898                               "$user_rec1, but is $user_rec2"
15899         done
15900
15901         # force cl_user2 to be idle (2nd part) and to reach
15902         # changelog_max_idle_time
15903         sleep 2
15904
15905         # force each GC-thread start and block then
15906         # one per MDT/MDD, set fail_val accordingly
15907         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
15908         do_nodes $mdts $LCTL set_param fail_loc=0x1316
15909
15910         # generate more changelogs to trigger fail_loc
15911         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
15912                 error "create $DIR/$tdir/${tfile}bis failed"
15913
15914         # stop MDT to stop GC-thread, should be done in back-ground as it will
15915         # block waiting for the thread to be released and exit
15916         declare -A stop_pids
15917         for i in $(seq $MDSCOUNT); do
15918                 stop mds$i &
15919                 stop_pids[mds$i]=$!
15920         done
15921
15922         for i in $(mdts_nodes); do
15923                 local facet
15924                 local nb=0
15925                 local facets=$(facets_up_on_host $i)
15926
15927                 for facet in ${facets//,/ }; do
15928                         if [[ $facet == mds* ]]; then
15929                                 nb=$((nb + 1))
15930                         fi
15931                 done
15932                 # ensure each MDS's gc threads are still present and all in "R"
15933                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
15934                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
15935                         error "$i: expected $nb GC-thread"
15936                 wait_update $i \
15937                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
15938                         "R" 20 ||
15939                         error "$i: GC-thread not found in R-state"
15940                 # check umounts of each MDT on MDS have reached kthread_stop()
15941                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
15942                         error "$i: expected $nb umount"
15943                 wait_update $i \
15944                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
15945                         error "$i: umount not found in D-state"
15946         done
15947
15948         # release all GC-threads
15949         do_nodes $mdts $LCTL set_param fail_loc=0
15950
15951         # wait for MDT stop to complete
15952         for i in $(seq $MDSCOUNT); do
15953                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
15954         done
15955
15956         # XXX
15957         # may try to check if any orphan changelog records are present
15958         # via ldiskfs/zfs and llog_reader...
15959
15960         # re-start/mount MDTs
15961         for i in $(seq $MDSCOUNT); do
15962                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
15963                         error "Fail to start mds$i"
15964         done
15965
15966         local first_rec
15967         for i in $(seq $MDSCOUNT); do
15968                 # check cl_user1 still registered
15969                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
15970                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15971                 # check cl_user2 unregistered
15972                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
15973                         error "mds$i: User ${cl_user2[mds$i]} still registered"
15974
15975                 # check changelogs are present and starting at $user_rec1 + 1
15976                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15977                 [ -n "$user_rec1" ] ||
15978                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15979                 first_rec=$($LFS changelog $(facet_svc mds$i) |
15980                             awk '{ print $1; exit; }')
15981
15982                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
15983                 [ $((user_rec1 + 1)) == $first_rec ] ||
15984                         error "mds$i: first index should be $user_rec1 + 1, " \
15985                               "but is $first_rec"
15986         done
15987 }
15988 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
15989               "during mount"
15990
15991 test_160i() {
15992
15993         local mdts=$(comma_list $(mdts_nodes))
15994
15995         changelog_register || error "first changelog_register failed"
15996
15997         # generate some changelog records to accumulate on each MDT
15998         # use all_char because created files should be evenly distributed
15999         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16000                 error "test_mkdir $tdir failed"
16001         for ((i = 0; i < MDSCOUNT; i++)); do
16002                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16003                         error "create $DIR/$tdir/d$i.1 failed"
16004         done
16005
16006         # check changelogs have been generated
16007         local nbcl=$(changelog_dump | wc -l)
16008         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16009
16010         # simulate race between register and unregister
16011         # XXX as fail_loc is set per-MDS, with DNE configs the race
16012         # simulation will only occur for one MDT per MDS and for the
16013         # others the normal race scenario will take place
16014         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
16015         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
16016         do_nodes $mdts $LCTL set_param fail_val=1
16017
16018         # unregister 1st user
16019         changelog_deregister &
16020         local pid1=$!
16021         # wait some time for deregister work to reach race rdv
16022         sleep 2
16023         # register 2nd user
16024         changelog_register || error "2nd user register failed"
16025
16026         wait $pid1 || error "1st user deregister failed"
16027
16028         local i
16029         local last_rec
16030         declare -A LAST_REC
16031         for i in $(seq $MDSCOUNT); do
16032                 if changelog_users mds$i | grep "^cl"; then
16033                         # make sure new records are added with one user present
16034                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
16035                                           awk '/^current.index:/ { print $NF }')
16036                 else
16037                         error "mds$i has no user registered"
16038                 fi
16039         done
16040
16041         # generate more changelog records to accumulate on each MDT
16042         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16043                 error "create $DIR/$tdir/${tfile}bis failed"
16044
16045         for i in $(seq $MDSCOUNT); do
16046                 last_rec=$(changelog_users $SINGLEMDS |
16047                            awk '/^current.index:/ { print $NF }')
16048                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
16049                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
16050                         error "changelogs are off on mds$i"
16051         done
16052 }
16053 run_test 160i "changelog user register/unregister race"
16054
16055 test_160j() {
16056         remote_mds_nodsh && skip "remote MDS with nodsh"
16057         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
16058                 skip "Need MDS version at least 2.12.56"
16059
16060         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
16061         stack_trap "umount $MOUNT2" EXIT
16062
16063         changelog_register || error "first changelog_register failed"
16064         stack_trap "changelog_deregister" EXIT
16065
16066         # generate some changelog
16067         # use all_char because created files should be evenly distributed
16068         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16069                 error "mkdir $tdir failed"
16070         for ((i = 0; i < MDSCOUNT; i++)); do
16071                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16072                         error "create $DIR/$tdir/d$i.1 failed"
16073         done
16074
16075         # open the changelog device
16076         exec 3>/dev/changelog-$FSNAME-MDT0000
16077         stack_trap "exec 3>&-" EXIT
16078         exec 4</dev/changelog-$FSNAME-MDT0000
16079         stack_trap "exec 4<&-" EXIT
16080
16081         # umount the first lustre mount
16082         umount $MOUNT
16083         stack_trap "mount_client $MOUNT" EXIT
16084
16085         # read changelog, which may or may not fail, but should not crash
16086         cat <&4 >/dev/null
16087
16088         # clear changelog
16089         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16090         changelog_users $SINGLEMDS | grep -q $cl_user ||
16091                 error "User $cl_user not found in changelog_users"
16092
16093         printf 'clear:'$cl_user':0' >&3
16094 }
16095 run_test 160j "client can be umounted while its chanangelog is being used"
16096
16097 test_160k() {
16098         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16099         remote_mds_nodsh && skip "remote MDS with nodsh"
16100
16101         mkdir -p $DIR/$tdir/1/1
16102
16103         changelog_register || error "changelog_register failed"
16104         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16105
16106         changelog_users $SINGLEMDS | grep -q $cl_user ||
16107                 error "User '$cl_user' not found in changelog_users"
16108 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
16109         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
16110         rmdir $DIR/$tdir/1/1 & sleep 1
16111         mkdir $DIR/$tdir/2
16112         touch $DIR/$tdir/2/2
16113         rm -rf $DIR/$tdir/2
16114
16115         wait
16116         sleep 4
16117
16118         changelog_dump | grep rmdir || error "rmdir not recorded"
16119 }
16120 run_test 160k "Verify that changelog records are not lost"
16121
16122 # Verifies that a file passed as a parameter has recently had an operation
16123 # performed on it that has generated an MTIME changelog which contains the
16124 # correct parent FID. As files might reside on a different MDT from the
16125 # parent directory in DNE configurations, the FIDs are translated to paths
16126 # before being compared, which should be identical
16127 compare_mtime_changelog() {
16128         local file="${1}"
16129         local mdtidx
16130         local mtime
16131         local cl_fid
16132         local pdir
16133         local dir
16134
16135         mdtidx=$($LFS getstripe --mdt-index $file)
16136         mdtidx=$(printf "%04x" $mdtidx)
16137
16138         # Obtain the parent FID from the MTIME changelog
16139         mtime=$($LFS changelog $FSNAME-MDT$mdtidx | tail -n 1 | grep MTIME)
16140         [ -z "$mtime" ] && error "MTIME changelog not recorded"
16141
16142         cl_fid=$(sed -e 's/.* p=//' -e 's/ .*//' <<<$mtime)
16143         [ -z "$cl_fid" ] && error "parent FID not present"
16144
16145         # Verify that the path for the parent FID is the same as the path for
16146         # the test directory
16147         pdir=$($LFS fid2path $MOUNT "$cl_fid")
16148
16149         dir=$(dirname $1)
16150
16151         [[ "${pdir%/}" == "$dir" ]] ||
16152                 error "MTIME changelog parent FID is wrong, expected $dir, got $pdir"
16153 }
16154
16155 test_160l() {
16156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16157
16158         remote_mds_nodsh && skip "remote MDS with nodsh"
16159         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
16160                 skip "Need MDS version at least 2.13.55"
16161
16162         local cl_user
16163
16164         changelog_register || error "changelog_register failed"
16165         cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16166
16167         changelog_users $SINGLEMDS | grep -q $cl_user ||
16168                 error "User '$cl_user' not found in changelog_users"
16169
16170         # Clear some types so that MTIME changelogs are generated
16171         changelog_chmask "-CREAT"
16172         changelog_chmask "-CLOSE"
16173
16174         test_mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
16175
16176         # Test CL_MTIME during setattr
16177         touch $DIR/$tdir/$tfile
16178         compare_mtime_changelog $DIR/$tdir/$tfile
16179
16180         # Test CL_MTIME during close
16181         $MULTIOP $DIR/$tdir/${tfile}_2 O_2w4096c || error "multiop failed"
16182         compare_mtime_changelog $DIR/$tdir/${tfile}_2
16183 }
16184 run_test 160l "Verify that MTIME changelog records contain the parent FID"
16185
16186 test_160m() {
16187         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16188         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16189                 skip "Need MDS version at least 2.14.51"
16190         local cl_users
16191         local cl_user1
16192         local cl_user2
16193         local pid1
16194
16195         # Create a user
16196         changelog_register || error "first changelog_register failed"
16197         changelog_register || error "second changelog_register failed"
16198
16199         cl_users=(${CL_USERS[mds1]})
16200         cl_user1="${cl_users[0]}"
16201         cl_user2="${cl_users[1]}"
16202         # generate some changelog records to accumulate on MDT0
16203         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16204         createmany -m $DIR/$tdir/$tfile 50 ||
16205                 error "create $DIR/$tdir/$tfile failed"
16206         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16207         rm -f $DIR/$tdir
16208
16209         # check changelogs have been generated
16210         local nbcl=$(changelog_dump | wc -l)
16211         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16212
16213 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
16214         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
16215
16216         __changelog_clear mds1 $cl_user1 +10
16217         __changelog_clear mds1 $cl_user2 0 &
16218         pid1=$!
16219         sleep 2
16220         __changelog_clear mds1 $cl_user1 0 ||
16221                 error "fail to cancel record for $cl_user1"
16222         wait $pid1
16223         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16224 }
16225 run_test 160m "Changelog clear race"
16226
16227 test_160n() {
16228         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16229         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16230                 skip "Need MDS version at least 2.14.51"
16231         local cl_users
16232         local cl_user1
16233         local cl_user2
16234         local pid1
16235         local first_rec
16236         local last_rec=0
16237
16238         # Create a user
16239         changelog_register || error "first changelog_register failed"
16240
16241         cl_users=(${CL_USERS[mds1]})
16242         cl_user1="${cl_users[0]}"
16243
16244         # generate some changelog records to accumulate on MDT0
16245         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16246         first_rec=$(changelog_users $SINGLEMDS |
16247                         awk '/^current.index:/ { print $NF }')
16248         while (( last_rec < (( first_rec + 65000)) )); do
16249                 createmany -m $DIR/$tdir/$tfile 10000 ||
16250                         error "create $DIR/$tdir/$tfile failed"
16251
16252                 for i in $(seq 0 10000); do
16253                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
16254                                 > /dev/null
16255                 done
16256
16257                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
16258                         error "unlinkmany failed unlink"
16259                 last_rec=$(changelog_users $SINGLEMDS |
16260                         awk '/^current.index:/ { print $NF }')
16261                 echo last record $last_rec
16262                 (( last_rec == 0 )) && error "no changelog found"
16263         done
16264
16265 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
16266         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
16267
16268         __changelog_clear mds1 $cl_user1 0 &
16269         pid1=$!
16270         sleep 2
16271         __changelog_clear mds1 $cl_user1 0 ||
16272                 error "fail to cancel record for $cl_user1"
16273         wait $pid1
16274         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16275 }
16276 run_test 160n "Changelog destroy race"
16277
16278 test_160o() {
16279         local mdt="$(facet_svc $SINGLEMDS)"
16280
16281         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16282         remote_mds_nodsh && skip "remote MDS with nodsh"
16283         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
16284                 skip "Need MDS version at least 2.14.52"
16285
16286         changelog_register --user test_160o -m unlnk+close+open ||
16287                 error "changelog_register failed"
16288         # drop server mask so it doesn't interfere
16289         do_facet $SINGLEMDS $LCTL --device $mdt \
16290                                 changelog_register -u "Tt3_-#" &&
16291                 error "bad symbols in name should fail"
16292
16293         do_facet $SINGLEMDS $LCTL --device $mdt \
16294                                 changelog_register -u test_160o &&
16295                 error "the same name registration should fail"
16296
16297         do_facet $SINGLEMDS $LCTL --device $mdt \
16298                         changelog_register -u test_160toolongname &&
16299                 error "too long name registration should fail"
16300
16301         changelog_chmask "MARK+HSM"
16302         lctl get_param mdd.*.changelog*mask
16303         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16304         changelog_users $SINGLEMDS | grep -q $cl_user ||
16305                 error "User $cl_user not found in changelog_users"
16306         #verify username
16307         echo $cl_user | grep -q test_160o ||
16308                 error "User $cl_user has no specific name 'test160o'"
16309
16310         # change something
16311         changelog_clear 0 || error "changelog_clear failed"
16312         # generate some changelog records to accumulate on MDT0
16313         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16314         touch $DIR/$tdir/$tfile                 # open 1
16315
16316         OPENS=$(changelog_dump | grep -c "OPEN")
16317         [[ $OPENS -eq 1 ]] || error "OPEN changelog mask count $OPENS != 1"
16318
16319         # must be no MKDIR it wasn't set as user mask
16320         MKDIR=$(changelog_dump | grep -c "MKDIR")
16321         [[ $MKDIR -eq 0 ]] || error "MKDIR changelog mask found $MKDIR > 0"
16322
16323         oldmask=$(do_facet $SINGLEMDS $LCTL get_param \
16324                                 mdd.$mdt.changelog_current_mask -n)
16325         # register maskless user
16326         changelog_register || error "changelog_register failed"
16327         # effective mask should be not changed because it is not minimal
16328         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16329                                 mdd.$mdt.changelog_current_mask -n)
16330         [[ $mask == $oldmask ]] || error "mask was changed: $mask vs $oldmask"
16331         # set server mask to minimal value
16332         changelog_chmask "MARK"
16333         # check effective mask again, should be treated as DEFMASK now
16334         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16335                                 mdd.$mdt.changelog_current_mask -n)
16336         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
16337
16338         do_facet $SINGLEMDS $LCTL --device $mdt \
16339                                 changelog_deregister -u test_160o ||
16340                 error "cannot deregister by name"
16341 }
16342 run_test 160o "changelog user name and mask"
16343
16344 test_160p() {
16345         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16346         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16347                 skip "Need MDS version at least 2.14.51"
16348         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
16349         local cl_users
16350         local cl_user1
16351         local entry_count
16352
16353         # Create a user
16354         changelog_register || error "first changelog_register failed"
16355
16356         cl_users=(${CL_USERS[mds1]})
16357         cl_user1="${cl_users[0]}"
16358
16359         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16360         createmany -m $DIR/$tdir/$tfile 50 ||
16361                 error "create $DIR/$tdir/$tfile failed"
16362         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16363         rm -rf $DIR/$tdir
16364
16365         # check changelogs have been generated
16366         entry_count=$(changelog_dump | wc -l)
16367         ((entry_count != 0)) || error "no changelog entries found"
16368
16369         # remove changelog_users and check that orphan entries are removed
16370         stop mds1
16371         do_facet mds1 "$DEBUGFS -w -R 'rm changelog_users' $(mdsdevname 1)"
16372         start mds1 || error "cannot start mdt"
16373         entry_count=$(changelog_dump | wc -l)
16374         ((entry_count == 0)) ||
16375                 error "found $entry_count changelog entries, expected none"
16376 }
16377 run_test 160p "Changelog orphan cleanup with no users"
16378
16379 test_161a() {
16380         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16381
16382         test_mkdir -c1 $DIR/$tdir
16383         cp /etc/hosts $DIR/$tdir/$tfile
16384         test_mkdir -c1 $DIR/$tdir/foo1
16385         test_mkdir -c1 $DIR/$tdir/foo2
16386         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
16387         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
16388         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
16389         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
16390         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
16391         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
16392                 $LFS fid2path $DIR $FID
16393                 error "bad link ea"
16394         fi
16395         # middle
16396         rm $DIR/$tdir/foo2/zachary
16397         # last
16398         rm $DIR/$tdir/foo2/thor
16399         # first
16400         rm $DIR/$tdir/$tfile
16401         # rename
16402         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
16403         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
16404                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
16405         rm $DIR/$tdir/foo2/maggie
16406
16407         # overflow the EA
16408         local longname=$tfile.avg_len_is_thirty_two_
16409         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
16410                 error_noexit 'failed to unlink many hardlinks'" EXIT
16411         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
16412                 error "failed to hardlink many files"
16413         links=$($LFS fid2path $DIR $FID | wc -l)
16414         echo -n "${links}/1000 links in link EA"
16415         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
16416 }
16417 run_test 161a "link ea sanity"
16418
16419 test_161b() {
16420         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16421         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
16422
16423         local MDTIDX=1
16424         local remote_dir=$DIR/$tdir/remote_dir
16425
16426         mkdir -p $DIR/$tdir
16427         $LFS mkdir -i $MDTIDX $remote_dir ||
16428                 error "create remote directory failed"
16429
16430         cp /etc/hosts $remote_dir/$tfile
16431         mkdir -p $remote_dir/foo1
16432         mkdir -p $remote_dir/foo2
16433         ln $remote_dir/$tfile $remote_dir/foo1/sofia
16434         ln $remote_dir/$tfile $remote_dir/foo2/zachary
16435         ln $remote_dir/$tfile $remote_dir/foo1/luna
16436         ln $remote_dir/$tfile $remote_dir/foo2/thor
16437
16438         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
16439                      tr -d ']')
16440         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
16441                 $LFS fid2path $DIR $FID
16442                 error "bad link ea"
16443         fi
16444         # middle
16445         rm $remote_dir/foo2/zachary
16446         # last
16447         rm $remote_dir/foo2/thor
16448         # first
16449         rm $remote_dir/$tfile
16450         # rename
16451         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
16452         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
16453         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
16454                 $LFS fid2path $DIR $FID
16455                 error "bad link rename"
16456         fi
16457         rm $remote_dir/foo2/maggie
16458
16459         # overflow the EA
16460         local longname=filename_avg_len_is_thirty_two_
16461         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
16462                 error "failed to hardlink many files"
16463         links=$($LFS fid2path $DIR $FID | wc -l)
16464         echo -n "${links}/1000 links in link EA"
16465         [[ ${links} -gt 60 ]] ||
16466                 error "expected at least 60 links in link EA"
16467         unlinkmany $remote_dir/foo2/$longname 1000 ||
16468         error "failed to unlink many hardlinks"
16469 }
16470 run_test 161b "link ea sanity under remote directory"
16471
16472 test_161c() {
16473         remote_mds_nodsh && skip "remote MDS with nodsh"
16474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16475         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
16476                 skip "Need MDS version at least 2.1.5"
16477
16478         # define CLF_RENAME_LAST 0x0001
16479         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
16480         changelog_register || error "changelog_register failed"
16481
16482         rm -rf $DIR/$tdir
16483         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
16484         touch $DIR/$tdir/foo_161c
16485         touch $DIR/$tdir/bar_161c
16486         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
16487         changelog_dump | grep RENME | tail -n 5
16488         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
16489         changelog_clear 0 || error "changelog_clear failed"
16490         if [ x$flags != "x0x1" ]; then
16491                 error "flag $flags is not 0x1"
16492         fi
16493
16494         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
16495         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
16496         touch $DIR/$tdir/foo_161c
16497         touch $DIR/$tdir/bar_161c
16498         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
16499         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
16500         changelog_dump | grep RENME | tail -n 5
16501         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
16502         changelog_clear 0 || error "changelog_clear failed"
16503         if [ x$flags != "x0x0" ]; then
16504                 error "flag $flags is not 0x0"
16505         fi
16506         echo "rename overwrite a target having nlink > 1," \
16507                 "changelog record has flags of $flags"
16508
16509         # rename doesn't overwrite a target (changelog flag 0x0)
16510         touch $DIR/$tdir/foo_161c
16511         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
16512         changelog_dump | grep RENME | tail -n 5
16513         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
16514         changelog_clear 0 || error "changelog_clear failed"
16515         if [ x$flags != "x0x0" ]; then
16516                 error "flag $flags is not 0x0"
16517         fi
16518         echo "rename doesn't overwrite a target," \
16519                 "changelog record has flags of $flags"
16520
16521         # define CLF_UNLINK_LAST 0x0001
16522         # unlink a file having nlink = 1 (changelog flag 0x1)
16523         rm -f $DIR/$tdir/foo2_161c
16524         changelog_dump | grep UNLNK | tail -n 5
16525         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
16526         changelog_clear 0 || error "changelog_clear failed"
16527         if [ x$flags != "x0x1" ]; then
16528                 error "flag $flags is not 0x1"
16529         fi
16530         echo "unlink a file having nlink = 1," \
16531                 "changelog record has flags of $flags"
16532
16533         # unlink a file having nlink > 1 (changelog flag 0x0)
16534         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
16535         rm -f $DIR/$tdir/foobar_161c
16536         changelog_dump | grep UNLNK | tail -n 5
16537         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
16538         changelog_clear 0 || error "changelog_clear failed"
16539         if [ x$flags != "x0x0" ]; then
16540                 error "flag $flags is not 0x0"
16541         fi
16542         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
16543 }
16544 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
16545
16546 test_161d() {
16547         remote_mds_nodsh && skip "remote MDS with nodsh"
16548         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
16549
16550         local pid
16551         local fid
16552
16553         changelog_register || error "changelog_register failed"
16554
16555         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
16556         # interfer with $MOUNT/.lustre/fid/ access
16557         mkdir $DIR/$tdir
16558         [[ $? -eq 0 ]] || error "mkdir failed"
16559
16560         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
16561         $LCTL set_param fail_loc=0x8000140c
16562         # 5s pause
16563         $LCTL set_param fail_val=5
16564
16565         # create file
16566         echo foofoo > $DIR/$tdir/$tfile &
16567         pid=$!
16568
16569         # wait for create to be delayed
16570         sleep 2
16571
16572         ps -p $pid
16573         [[ $? -eq 0 ]] || error "create should be blocked"
16574
16575         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
16576         stack_trap "rm -f $tempfile"
16577         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
16578         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
16579         # some delay may occur during ChangeLog publishing and file read just
16580         # above, that could allow file write to happen finally
16581         [[ -s $tempfile ]] && echo "file should be empty"
16582
16583         $LCTL set_param fail_loc=0
16584
16585         wait $pid
16586         [[ $? -eq 0 ]] || error "create failed"
16587 }
16588 run_test 161d "create with concurrent .lustre/fid access"
16589
16590 check_path() {
16591         local expected="$1"
16592         shift
16593         local fid="$2"
16594
16595         local path
16596         path=$($LFS fid2path "$@")
16597         local rc=$?
16598
16599         if [ $rc -ne 0 ]; then
16600                 error "path looked up of '$expected' failed: rc=$rc"
16601         elif [ "$path" != "$expected" ]; then
16602                 error "path looked up '$path' instead of '$expected'"
16603         else
16604                 echo "FID '$fid' resolves to path '$path' as expected"
16605         fi
16606 }
16607
16608 test_162a() { # was test_162
16609         test_mkdir -p -c1 $DIR/$tdir/d2
16610         touch $DIR/$tdir/d2/$tfile
16611         touch $DIR/$tdir/d2/x1
16612         touch $DIR/$tdir/d2/x2
16613         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
16614         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
16615         # regular file
16616         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
16617         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
16618
16619         # softlink
16620         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
16621         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
16622         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
16623
16624         # softlink to wrong file
16625         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
16626         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
16627         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
16628
16629         # hardlink
16630         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
16631         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
16632         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
16633         # fid2path dir/fsname should both work
16634         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
16635         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
16636
16637         # hardlink count: check that there are 2 links
16638         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
16639         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
16640
16641         # hardlink indexing: remove the first link
16642         rm $DIR/$tdir/d2/p/q/r/hlink
16643         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
16644 }
16645 run_test 162a "path lookup sanity"
16646
16647 test_162b() {
16648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16649         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16650
16651         mkdir $DIR/$tdir
16652         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
16653                                 error "create striped dir failed"
16654
16655         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
16656                                         tail -n 1 | awk '{print $2}')
16657         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
16658
16659         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
16660         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
16661
16662         # regular file
16663         for ((i=0;i<5;i++)); do
16664                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
16665                         error "get fid for f$i failed"
16666                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
16667
16668                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
16669                         error "get fid for d$i failed"
16670                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
16671         done
16672
16673         return 0
16674 }
16675 run_test 162b "striped directory path lookup sanity"
16676
16677 # LU-4239: Verify fid2path works with paths 100 or more directories deep
16678 test_162c() {
16679         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
16680                 skip "Need MDS version at least 2.7.51"
16681
16682         local lpath=$tdir.local
16683         local rpath=$tdir.remote
16684
16685         test_mkdir $DIR/$lpath
16686         test_mkdir $DIR/$rpath
16687
16688         for ((i = 0; i <= 101; i++)); do
16689                 lpath="$lpath/$i"
16690                 mkdir $DIR/$lpath
16691                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
16692                         error "get fid for local directory $DIR/$lpath failed"
16693                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
16694
16695                 rpath="$rpath/$i"
16696                 test_mkdir $DIR/$rpath
16697                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
16698                         error "get fid for remote directory $DIR/$rpath failed"
16699                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
16700         done
16701
16702         return 0
16703 }
16704 run_test 162c "fid2path works with paths 100 or more directories deep"
16705
16706 oalr_event_count() {
16707         local event="${1}"
16708         local trace="${2}"
16709
16710         awk -v name="${FSNAME}-OST0000" \
16711             -v event="${event}" \
16712             '$1 == "TRACE" && $2 == event && $3 == name' \
16713             "${trace}" |
16714         wc -l
16715 }
16716
16717 oalr_expect_event_count() {
16718         local event="${1}"
16719         local trace="${2}"
16720         local expect="${3}"
16721         local count
16722
16723         count=$(oalr_event_count "${event}" "${trace}")
16724         if ((count == expect)); then
16725                 return 0
16726         fi
16727
16728         error_noexit "${event} event count was '${count}', expected ${expect}"
16729         cat "${trace}" >&2
16730         exit 1
16731 }
16732
16733 cleanup_165() {
16734         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
16735         stop ost1
16736         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
16737 }
16738
16739 setup_165() {
16740         sync # Flush previous IOs so we can count log entries.
16741         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
16742         stack_trap cleanup_165 EXIT
16743 }
16744
16745 test_165a() {
16746         local trace="/tmp/${tfile}.trace"
16747         local rc
16748         local count
16749
16750         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
16751                 skip "OFD access log unsupported"
16752
16753         setup_165
16754         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
16755         sleep 5
16756
16757         do_facet ost1 ofd_access_log_reader --list
16758         stop ost1
16759
16760         do_facet ost1 killall -TERM ofd_access_log_reader
16761         wait
16762         rc=$?
16763
16764         if ((rc != 0)); then
16765                 error "ofd_access_log_reader exited with rc = '${rc}'"
16766         fi
16767
16768         # Parse trace file for discovery events:
16769         oalr_expect_event_count alr_log_add "${trace}" 1
16770         oalr_expect_event_count alr_log_eof "${trace}" 1
16771         oalr_expect_event_count alr_log_free "${trace}" 1
16772 }
16773 run_test 165a "ofd access log discovery"
16774
16775 test_165b() {
16776         local trace="/tmp/${tfile}.trace"
16777         local file="${DIR}/${tfile}"
16778         local pfid1
16779         local pfid2
16780         local -a entry
16781         local rc
16782         local count
16783         local size
16784         local flags
16785
16786         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
16787                 skip "OFD access log unsupported"
16788
16789         setup_165
16790         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
16791         sleep 5
16792
16793         do_facet ost1 ofd_access_log_reader --list
16794
16795         lfs setstripe -c 1 -i 0 "${file}"
16796         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
16797                 error "cannot create '${file}'"
16798
16799         sleep 5
16800         do_facet ost1 killall -TERM ofd_access_log_reader
16801         wait
16802         rc=$?
16803
16804         if ((rc != 0)); then
16805                 error "ofd_access_log_reader exited with rc = '${rc}'"
16806         fi
16807
16808         oalr_expect_event_count alr_log_entry "${trace}" 1
16809
16810         pfid1=$($LFS path2fid "${file}")
16811
16812         # 1     2             3   4    5     6   7    8    9     10
16813         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
16814         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
16815
16816         echo "entry = '${entry[*]}'" >&2
16817
16818         pfid2=${entry[4]}
16819         if [[ "${pfid1}" != "${pfid2}" ]]; then
16820                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
16821         fi
16822
16823         size=${entry[8]}
16824         if ((size != 1048576)); then
16825                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
16826         fi
16827
16828         flags=${entry[10]}
16829         if [[ "${flags}" != "w" ]]; then
16830                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
16831         fi
16832
16833         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
16834         sleep 5
16835
16836         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c ||
16837                 error "cannot read '${file}'"
16838         sleep 5
16839
16840         do_facet ost1 killall -TERM ofd_access_log_reader
16841         wait
16842         rc=$?
16843
16844         if ((rc != 0)); then
16845                 error "ofd_access_log_reader exited with rc = '${rc}'"
16846         fi
16847
16848         oalr_expect_event_count alr_log_entry "${trace}" 1
16849
16850         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
16851         echo "entry = '${entry[*]}'" >&2
16852
16853         pfid2=${entry[4]}
16854         if [[ "${pfid1}" != "${pfid2}" ]]; then
16855                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
16856         fi
16857
16858         size=${entry[8]}
16859         if ((size != 524288)); then
16860                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
16861         fi
16862
16863         flags=${entry[10]}
16864         if [[ "${flags}" != "r" ]]; then
16865                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
16866         fi
16867 }
16868 run_test 165b "ofd access log entries are produced and consumed"
16869
16870 test_165c() {
16871         local trace="/tmp/${tfile}.trace"
16872         local file="${DIR}/${tdir}/${tfile}"
16873
16874         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
16875                 skip "OFD access log unsupported"
16876
16877         test_mkdir "${DIR}/${tdir}"
16878
16879         setup_165
16880         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
16881         sleep 5
16882
16883         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
16884
16885         # 4096 / 64 = 64. Create twice as many entries.
16886         for ((i = 0; i < 128; i++)); do
16887                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c ||
16888                         error "cannot create file"
16889         done
16890
16891         sync
16892
16893         do_facet ost1 killall -TERM ofd_access_log_reader
16894         wait
16895         rc=$?
16896         if ((rc != 0)); then
16897                 error "ofd_access_log_reader exited with rc = '${rc}'"
16898         fi
16899
16900         unlinkmany  "${file}-%d" 128
16901 }
16902 run_test 165c "full ofd access logs do not block IOs"
16903
16904 oal_get_read_count() {
16905         local stats="$1"
16906
16907         # STATS lustre-OST0001 alr_read_count 1
16908
16909         do_facet ost1 cat "${stats}" |
16910         awk '$1 == "STATS" && $3 == "alr_read_count" { count = $4; }
16911              END { print count; }'
16912 }
16913
16914 oal_expect_read_count() {
16915         local stats="$1"
16916         local count
16917         local expect="$2"
16918
16919         # Ask ofd_access_log_reader to write stats.
16920         do_facet ost1 killall -USR1 ofd_access_log_reader
16921
16922         # Allow some time for things to happen.
16923         sleep 1
16924
16925         count=$(oal_get_read_count "${stats}")
16926         if ((count == expect)); then
16927                 return 0
16928         fi
16929
16930         error_noexit "bad read count, got ${count}, expected ${expect}"
16931         do_facet ost1 cat "${stats}" >&2
16932         exit 1
16933 }
16934
16935 test_165d() {
16936         local stats="/tmp/${tfile}.stats"
16937         local file="${DIR}/${tdir}/${tfile}"
16938         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
16939
16940         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
16941                 skip "OFD access log unsupported"
16942
16943         test_mkdir "${DIR}/${tdir}"
16944
16945         setup_165
16946         do_facet ost1 ofd_access_log_reader --stats="${stats}" &
16947         sleep 5
16948
16949         lfs setstripe -c 1 -i 0 "${file}"
16950
16951         do_facet ost1 lctl set_param "${param}=rw"
16952         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
16953                 error "cannot create '${file}'"
16954         oal_expect_read_count "${stats}" 1
16955
16956         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
16957                 error "cannot read '${file}'"
16958         oal_expect_read_count "${stats}" 2
16959
16960         do_facet ost1 lctl set_param "${param}=r"
16961         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
16962                 error "cannot create '${file}'"
16963         oal_expect_read_count "${stats}" 2
16964
16965         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
16966                 error "cannot read '${file}'"
16967         oal_expect_read_count "${stats}" 3
16968
16969         do_facet ost1 lctl set_param "${param}=w"
16970         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
16971                 error "cannot create '${file}'"
16972         oal_expect_read_count "${stats}" 4
16973
16974         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
16975                 error "cannot read '${file}'"
16976         oal_expect_read_count "${stats}" 4
16977
16978         do_facet ost1 lctl set_param "${param}=0"
16979         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
16980                 error "cannot create '${file}'"
16981         oal_expect_read_count "${stats}" 4
16982
16983         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
16984                 error "cannot read '${file}'"
16985         oal_expect_read_count "${stats}" 4
16986
16987         do_facet ost1 killall -TERM ofd_access_log_reader
16988         wait
16989         rc=$?
16990         if ((rc != 0)); then
16991                 error "ofd_access_log_reader exited with rc = '${rc}'"
16992         fi
16993 }
16994 run_test 165d "ofd_access_log mask works"
16995
16996 test_165e() {
16997         local stats="/tmp/${tfile}.stats"
16998         local file0="${DIR}/${tdir}-0/${tfile}"
16999         local file1="${DIR}/${tdir}-1/${tfile}"
17000
17001         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17002                 skip "OFD access log unsupported"
17003
17004         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
17005
17006         test_mkdir -c 1 -i 0 "${DIR}/${tdir}-0"
17007         test_mkdir -c 1 -i 1 "${DIR}/${tdir}-1"
17008
17009         lfs setstripe -c 1 -i 0 "${file0}"
17010         lfs setstripe -c 1 -i 0 "${file1}"
17011
17012         setup_165
17013         do_facet ost1 ofd_access_log_reader -I 1 --stats="${stats}" &
17014         sleep 5
17015
17016         $MULTIOP "${file0}" oO_CREAT:O_WRONLY:w512c ||
17017                 error "cannot create '${file0}'"
17018         sync
17019         oal_expect_read_count "${stats}" 0
17020
17021         $MULTIOP "${file1}" oO_CREAT:O_WRONLY:w512c ||
17022                 error "cannot create '${file1}'"
17023         sync
17024         oal_expect_read_count "${stats}" 1
17025
17026         do_facet ost1 killall -TERM ofd_access_log_reader
17027         wait
17028         rc=$?
17029         if ((rc != 0)); then
17030                 error "ofd_access_log_reader exited with rc = '${rc}'"
17031         fi
17032 }
17033 run_test 165e "ofd_access_log MDT index filter works"
17034
17035 test_165f() {
17036         local trace="/tmp/${tfile}.trace"
17037         local rc
17038         local count
17039
17040         setup_165
17041         do_facet ost1 timeout 60 ofd_access_log_reader \
17042                 --exit-on-close --debug=- --trace=- > "${trace}" &
17043         sleep 5
17044         stop ost1
17045
17046         wait
17047         rc=$?
17048
17049         if ((rc != 0)); then
17050                 error_noexit "ofd_access_log_reader exited with rc = '${rc}'"
17051                 cat "${trace}"
17052                 exit 1
17053         fi
17054 }
17055 run_test 165f "ofd_access_log_reader --exit-on-close works"
17056
17057 test_169() {
17058         # do directio so as not to populate the page cache
17059         log "creating a 10 Mb file"
17060         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
17061                 error "multiop failed while creating a file"
17062         log "starting reads"
17063         dd if=$DIR/$tfile of=/dev/null bs=4096 &
17064         log "truncating the file"
17065         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
17066                 error "multiop failed while truncating the file"
17067         log "killing dd"
17068         kill %+ || true # reads might have finished
17069         echo "wait until dd is finished"
17070         wait
17071         log "removing the temporary file"
17072         rm -rf $DIR/$tfile || error "tmp file removal failed"
17073 }
17074 run_test 169 "parallel read and truncate should not deadlock"
17075
17076 test_170() {
17077         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17078
17079         $LCTL clear     # bug 18514
17080         $LCTL debug_daemon start $TMP/${tfile}_log_good
17081         touch $DIR/$tfile
17082         $LCTL debug_daemon stop
17083         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
17084                 error "sed failed to read log_good"
17085
17086         $LCTL debug_daemon start $TMP/${tfile}_log_good
17087         rm -rf $DIR/$tfile
17088         $LCTL debug_daemon stop
17089
17090         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
17091                error "lctl df log_bad failed"
17092
17093         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17094         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17095
17096         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
17097         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
17098
17099         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
17100                 error "bad_line good_line1 good_line2 are empty"
17101
17102         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
17103         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
17104         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
17105
17106         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
17107         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17108         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17109
17110         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
17111                 error "bad_line_new good_line_new are empty"
17112
17113         local expected_good=$((good_line1 + good_line2*2))
17114
17115         rm -f $TMP/${tfile}*
17116         # LU-231, short malformed line may not be counted into bad lines
17117         if [ $bad_line -ne $bad_line_new ] &&
17118                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
17119                 error "expected $bad_line bad lines, but got $bad_line_new"
17120                 return 1
17121         fi
17122
17123         if [ $expected_good -ne $good_line_new ]; then
17124                 error "expected $expected_good good lines, but got $good_line_new"
17125                 return 2
17126         fi
17127         true
17128 }
17129 run_test 170 "test lctl df to handle corrupted log ====================="
17130
17131 test_171() { # bug20592
17132         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17133
17134         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
17135         $LCTL set_param fail_loc=0x50e
17136         $LCTL set_param fail_val=3000
17137         multiop_bg_pause $DIR/$tfile O_s || true
17138         local MULTIPID=$!
17139         kill -USR1 $MULTIPID
17140         # cause log dump
17141         sleep 3
17142         wait $MULTIPID
17143         if dmesg | grep "recursive fault"; then
17144                 error "caught a recursive fault"
17145         fi
17146         $LCTL set_param fail_loc=0
17147         true
17148 }
17149 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
17150
17151 # it would be good to share it with obdfilter-survey/iokit-libecho code
17152 setup_obdecho_osc () {
17153         local rc=0
17154         local ost_nid=$1
17155         local obdfilter_name=$2
17156         echo "Creating new osc for $obdfilter_name on $ost_nid"
17157         # make sure we can find loopback nid
17158         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
17159
17160         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
17161                            ${obdfilter_name}_osc_UUID || rc=2; }
17162         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
17163                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
17164         return $rc
17165 }
17166
17167 cleanup_obdecho_osc () {
17168         local obdfilter_name=$1
17169         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
17170         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
17171         return 0
17172 }
17173
17174 obdecho_test() {
17175         local OBD=$1
17176         local node=$2
17177         local pages=${3:-64}
17178         local rc=0
17179         local id
17180
17181         local count=10
17182         local obd_size=$(get_obd_size $node $OBD)
17183         local page_size=$(get_page_size $node)
17184         if [[ -n "$obd_size" ]]; then
17185                 local new_count=$((obd_size / (pages * page_size / 1024)))
17186                 [[ $new_count -ge $count ]] || count=$new_count
17187         fi
17188
17189         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
17190         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
17191                            rc=2; }
17192         if [ $rc -eq 0 ]; then
17193             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
17194             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
17195         fi
17196         echo "New object id is $id"
17197         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
17198                            rc=4; }
17199         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
17200                            "test_brw $count w v $pages $id" || rc=4; }
17201         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
17202                            rc=4; }
17203         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
17204                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
17205         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
17206                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
17207         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
17208         return $rc
17209 }
17210
17211 test_180a() {
17212         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17213
17214         if ! [ -d /sys/fs/lustre/echo_client ] &&
17215            ! module_loaded obdecho; then
17216                 load_module obdecho/obdecho &&
17217                         stack_trap "rmmod obdecho" EXIT ||
17218                         error "unable to load obdecho on client"
17219         fi
17220
17221         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
17222         local host=$($LCTL get_param -n osc.$osc.import |
17223                      awk '/current_connection:/ { print $2 }' )
17224         local target=$($LCTL get_param -n osc.$osc.import |
17225                        awk '/target:/ { print $2 }' )
17226         target=${target%_UUID}
17227
17228         if [ -n "$target" ]; then
17229                 setup_obdecho_osc $host $target &&
17230                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
17231                         { error "obdecho setup failed with $?"; return; }
17232
17233                 obdecho_test ${target}_osc client ||
17234                         error "obdecho_test failed on ${target}_osc"
17235         else
17236                 $LCTL get_param osc.$osc.import
17237                 error "there is no osc.$osc.import target"
17238         fi
17239 }
17240 run_test 180a "test obdecho on osc"
17241
17242 test_180b() {
17243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17244         remote_ost_nodsh && skip "remote OST with nodsh"
17245
17246         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
17247                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
17248                 error "failed to load module obdecho"
17249
17250         local target=$(do_facet ost1 $LCTL dl |
17251                        awk '/obdfilter/ { print $4; exit; }')
17252
17253         if [ -n "$target" ]; then
17254                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
17255         else
17256                 do_facet ost1 $LCTL dl
17257                 error "there is no obdfilter target on ost1"
17258         fi
17259 }
17260 run_test 180b "test obdecho directly on obdfilter"
17261
17262 test_180c() { # LU-2598
17263         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17264         remote_ost_nodsh && skip "remote OST with nodsh"
17265         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
17266                 skip "Need MDS version at least 2.4.0"
17267
17268         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
17269                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
17270                 error "failed to load module obdecho"
17271
17272         local target=$(do_facet ost1 $LCTL dl |
17273                        awk '/obdfilter/ { print $4; exit; }')
17274
17275         if [ -n "$target" ]; then
17276                 local pages=16384 # 64MB bulk I/O RPC size
17277
17278                 obdecho_test "$target" ost1 "$pages" ||
17279                         error "obdecho_test with pages=$pages failed with $?"
17280         else
17281                 do_facet ost1 $LCTL dl
17282                 error "there is no obdfilter target on ost1"
17283         fi
17284 }
17285 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
17286
17287 test_181() { # bug 22177
17288         test_mkdir $DIR/$tdir
17289         # create enough files to index the directory
17290         createmany -o $DIR/$tdir/foobar 4000
17291         # print attributes for debug purpose
17292         lsattr -d .
17293         # open dir
17294         multiop_bg_pause $DIR/$tdir D_Sc || return 1
17295         MULTIPID=$!
17296         # remove the files & current working dir
17297         unlinkmany $DIR/$tdir/foobar 4000
17298         rmdir $DIR/$tdir
17299         kill -USR1 $MULTIPID
17300         wait $MULTIPID
17301         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
17302         return 0
17303 }
17304 run_test 181 "Test open-unlinked dir ========================"
17305
17306 test_182() {
17307         local fcount=1000
17308         local tcount=10
17309
17310         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
17311
17312         $LCTL set_param mdc.*.rpc_stats=clear
17313
17314         for (( i = 0; i < $tcount; i++ )) ; do
17315                 mkdir $DIR/$tdir/$i
17316         done
17317
17318         for (( i = 0; i < $tcount; i++ )) ; do
17319                 createmany -o $DIR/$tdir/$i/f- $fcount &
17320         done
17321         wait
17322
17323         for (( i = 0; i < $tcount; i++ )) ; do
17324                 unlinkmany $DIR/$tdir/$i/f- $fcount &
17325         done
17326         wait
17327
17328         $LCTL get_param mdc.*.rpc_stats
17329
17330         rm -rf $DIR/$tdir
17331 }
17332 run_test 182 "Test parallel modify metadata operations ================"
17333
17334 test_183() { # LU-2275
17335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17336         remote_mds_nodsh && skip "remote MDS with nodsh"
17337         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
17338                 skip "Need MDS version at least 2.3.56"
17339
17340         mkdir_on_mdt0 $DIR/$tdir || error "creating dir $DIR/$tdir"
17341         echo aaa > $DIR/$tdir/$tfile
17342
17343 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
17344         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
17345
17346         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
17347         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
17348
17349         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17350
17351         # Flush negative dentry cache
17352         touch $DIR/$tdir/$tfile
17353
17354         # We are not checking for any leaked references here, they'll
17355         # become evident next time we do cleanup with module unload.
17356         rm -rf $DIR/$tdir
17357 }
17358 run_test 183 "No crash or request leak in case of strange dispositions ========"
17359
17360 # test suite 184 is for LU-2016, LU-2017
17361 test_184a() {
17362         check_swap_layouts_support
17363
17364         dir0=$DIR/$tdir/$testnum
17365         test_mkdir -p -c1 $dir0
17366         ref1=/etc/passwd
17367         ref2=/etc/group
17368         file1=$dir0/f1
17369         file2=$dir0/f2
17370         $LFS setstripe -c1 $file1
17371         cp $ref1 $file1
17372         $LFS setstripe -c2 $file2
17373         cp $ref2 $file2
17374         gen1=$($LFS getstripe -g $file1)
17375         gen2=$($LFS getstripe -g $file2)
17376
17377         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
17378         gen=$($LFS getstripe -g $file1)
17379         [[ $gen1 != $gen ]] ||
17380                 "Layout generation on $file1 does not change"
17381         gen=$($LFS getstripe -g $file2)
17382         [[ $gen2 != $gen ]] ||
17383                 "Layout generation on $file2 does not change"
17384
17385         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
17386         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17387
17388         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
17389 }
17390 run_test 184a "Basic layout swap"
17391
17392 test_184b() {
17393         check_swap_layouts_support
17394
17395         dir0=$DIR/$tdir/$testnum
17396         mkdir -p $dir0 || error "creating dir $dir0"
17397         file1=$dir0/f1
17398         file2=$dir0/f2
17399         file3=$dir0/f3
17400         dir1=$dir0/d1
17401         dir2=$dir0/d2
17402         mkdir $dir1 $dir2
17403         $LFS setstripe -c1 $file1
17404         $LFS setstripe -c2 $file2
17405         $LFS setstripe -c1 $file3
17406         chown $RUNAS_ID $file3
17407         gen1=$($LFS getstripe -g $file1)
17408         gen2=$($LFS getstripe -g $file2)
17409
17410         $LFS swap_layouts $dir1 $dir2 &&
17411                 error "swap of directories layouts should fail"
17412         $LFS swap_layouts $dir1 $file1 &&
17413                 error "swap of directory and file layouts should fail"
17414         $RUNAS $LFS swap_layouts $file1 $file2 &&
17415                 error "swap of file we cannot write should fail"
17416         $LFS swap_layouts $file1 $file3 &&
17417                 error "swap of file with different owner should fail"
17418         /bin/true # to clear error code
17419 }
17420 run_test 184b "Forbidden layout swap (will generate errors)"
17421
17422 test_184c() {
17423         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
17424         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
17425         check_swap_layouts_support
17426         check_swap_layout_no_dom $DIR
17427
17428         local dir0=$DIR/$tdir/$testnum
17429         mkdir -p $dir0 || error "creating dir $dir0"
17430
17431         local ref1=$dir0/ref1
17432         local ref2=$dir0/ref2
17433         local file1=$dir0/file1
17434         local file2=$dir0/file2
17435         # create a file large enough for the concurrent test
17436         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
17437         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
17438         echo "ref file size: ref1($(stat -c %s $ref1))," \
17439              "ref2($(stat -c %s $ref2))"
17440
17441         cp $ref2 $file2
17442         dd if=$ref1 of=$file1 bs=16k &
17443         local DD_PID=$!
17444
17445         # Make sure dd starts to copy file, but wait at most 5 seconds
17446         local loops=0
17447         while [ ! -s $file1 -a $((loops++)) -lt 50 ]; do sleep 0.1; done
17448
17449         $LFS swap_layouts $file1 $file2
17450         local rc=$?
17451         wait $DD_PID
17452         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
17453         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
17454
17455         # how many bytes copied before swapping layout
17456         local copied=$(stat -c %s $file2)
17457         local remaining=$(stat -c %s $ref1)
17458         remaining=$((remaining - copied))
17459         echo "Copied $copied bytes before swapping layout..."
17460
17461         cmp -n $copied $file1 $ref2 | grep differ &&
17462                 error "Content mismatch [0, $copied) of ref2 and file1"
17463         cmp -n $copied $file2 $ref1 ||
17464                 error "Content mismatch [0, $copied) of ref1 and file2"
17465         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
17466                 error "Content mismatch [$copied, EOF) of ref1 and file1"
17467
17468         # clean up
17469         rm -f $ref1 $ref2 $file1 $file2
17470 }
17471 run_test 184c "Concurrent write and layout swap"
17472
17473 test_184d() {
17474         check_swap_layouts_support
17475         check_swap_layout_no_dom $DIR
17476         [ -z "$(which getfattr 2>/dev/null)" ] &&
17477                 skip_env "no getfattr command"
17478
17479         local file1=$DIR/$tdir/$tfile-1
17480         local file2=$DIR/$tdir/$tfile-2
17481         local file3=$DIR/$tdir/$tfile-3
17482         local lovea1
17483         local lovea2
17484
17485         mkdir -p $DIR/$tdir
17486         touch $file1 || error "create $file1 failed"
17487         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
17488                 error "create $file2 failed"
17489         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
17490                 error "create $file3 failed"
17491         lovea1=$(get_layout_param $file1)
17492
17493         $LFS swap_layouts $file2 $file3 ||
17494                 error "swap $file2 $file3 layouts failed"
17495         $LFS swap_layouts $file1 $file2 ||
17496                 error "swap $file1 $file2 layouts failed"
17497
17498         lovea2=$(get_layout_param $file2)
17499         echo "$lovea1"
17500         echo "$lovea2"
17501         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
17502
17503         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
17504         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
17505 }
17506 run_test 184d "allow stripeless layouts swap"
17507
17508 test_184e() {
17509         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
17510                 skip "Need MDS version at least 2.6.94"
17511         check_swap_layouts_support
17512         check_swap_layout_no_dom $DIR
17513         [ -z "$(which getfattr 2>/dev/null)" ] &&
17514                 skip_env "no getfattr command"
17515
17516         local file1=$DIR/$tdir/$tfile-1
17517         local file2=$DIR/$tdir/$tfile-2
17518         local file3=$DIR/$tdir/$tfile-3
17519         local lovea
17520
17521         mkdir -p $DIR/$tdir
17522         touch $file1 || error "create $file1 failed"
17523         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
17524                 error "create $file2 failed"
17525         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
17526                 error "create $file3 failed"
17527
17528         $LFS swap_layouts $file1 $file2 ||
17529                 error "swap $file1 $file2 layouts failed"
17530
17531         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
17532         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
17533
17534         echo 123 > $file1 || error "Should be able to write into $file1"
17535
17536         $LFS swap_layouts $file1 $file3 ||
17537                 error "swap $file1 $file3 layouts failed"
17538
17539         echo 123 > $file1 || error "Should be able to write into $file1"
17540
17541         rm -rf $file1 $file2 $file3
17542 }
17543 run_test 184e "Recreate layout after stripeless layout swaps"
17544
17545 test_184f() {
17546         # Create a file with name longer than sizeof(struct stat) ==
17547         # 144 to see if we can get chars from the file name to appear
17548         # in the returned striping. Note that 'f' == 0x66.
17549         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
17550
17551         mkdir -p $DIR/$tdir
17552         mcreate $DIR/$tdir/$file
17553         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
17554                 error "IOC_MDC_GETFILEINFO returned garbage striping"
17555         fi
17556 }
17557 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
17558
17559 test_185() { # LU-2441
17560         # LU-3553 - no volatile file support in old servers
17561         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
17562                 skip "Need MDS version at least 2.3.60"
17563
17564         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
17565         touch $DIR/$tdir/spoo
17566         local mtime1=$(stat -c "%Y" $DIR/$tdir)
17567         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
17568                 error "cannot create/write a volatile file"
17569         [ "$FILESET" == "" ] &&
17570         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
17571                 error "FID is still valid after close"
17572
17573         multiop_bg_pause $DIR/$tdir vVw4096_c
17574         local multi_pid=$!
17575
17576         local OLD_IFS=$IFS
17577         IFS=":"
17578         local fidv=($fid)
17579         IFS=$OLD_IFS
17580         # assume that the next FID for this client is sequential, since stdout
17581         # is unfortunately eaten by multiop_bg_pause
17582         local n=$((${fidv[1]} + 1))
17583         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
17584         if [ "$FILESET" == "" ]; then
17585                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
17586                         error "FID is missing before close"
17587         fi
17588         kill -USR1 $multi_pid
17589         # 1 second delay, so if mtime change we will see it
17590         sleep 1
17591         local mtime2=$(stat -c "%Y" $DIR/$tdir)
17592         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
17593 }
17594 run_test 185 "Volatile file support"
17595
17596 function create_check_volatile() {
17597         local idx=$1
17598         local tgt
17599
17600         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
17601         local PID=$!
17602         sleep 1
17603         local FID=$(cat /tmp/${tfile}.fid)
17604         [ "$FID" == "" ] && error "can't get FID for volatile"
17605         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
17606         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
17607         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
17608         kill -USR1 $PID
17609         wait
17610         sleep 1
17611         cancel_lru_locks mdc # flush opencache
17612         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
17613         return 0
17614 }
17615
17616 test_185a(){
17617         # LU-12516 - volatile creation via .lustre
17618         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
17619                 skip "Need MDS version at least 2.3.55"
17620
17621         create_check_volatile 0
17622         [ $MDSCOUNT -lt 2 ] && return 0
17623
17624         # DNE case
17625         create_check_volatile 1
17626
17627         return 0
17628 }
17629 run_test 185a "Volatile file creation in .lustre/fid/"
17630
17631 test_187a() {
17632         remote_mds_nodsh && skip "remote MDS with nodsh"
17633         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
17634                 skip "Need MDS version at least 2.3.0"
17635
17636         local dir0=$DIR/$tdir/$testnum
17637         mkdir -p $dir0 || error "creating dir $dir0"
17638
17639         local file=$dir0/file1
17640         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
17641         local dv1=$($LFS data_version $file)
17642         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
17643         local dv2=$($LFS data_version $file)
17644         [[ $dv1 != $dv2 ]] ||
17645                 error "data version did not change on write $dv1 == $dv2"
17646
17647         # clean up
17648         rm -f $file1
17649 }
17650 run_test 187a "Test data version change"
17651
17652 test_187b() {
17653         remote_mds_nodsh && skip "remote MDS with nodsh"
17654         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
17655                 skip "Need MDS version at least 2.3.0"
17656
17657         local dir0=$DIR/$tdir/$testnum
17658         mkdir -p $dir0 || error "creating dir $dir0"
17659
17660         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
17661         [[ ${DV[0]} != ${DV[1]} ]] ||
17662                 error "data version did not change on write"\
17663                       " ${DV[0]} == ${DV[1]}"
17664
17665         # clean up
17666         rm -f $file1
17667 }
17668 run_test 187b "Test data version change on volatile file"
17669
17670 test_200() {
17671         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17672         remote_mgs_nodsh && skip "remote MGS with nodsh"
17673         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
17674
17675         local POOL=${POOL:-cea1}
17676         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
17677         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
17678         # Pool OST targets
17679         local first_ost=0
17680         local last_ost=$(($OSTCOUNT - 1))
17681         local ost_step=2
17682         local ost_list=$(seq $first_ost $ost_step $last_ost)
17683         local ost_range="$first_ost $last_ost $ost_step"
17684         local test_path=$POOL_ROOT/$POOL_DIR_NAME
17685         local file_dir=$POOL_ROOT/file_tst
17686         local subdir=$test_path/subdir
17687         local rc=0
17688
17689         while : ; do
17690                 # former test_200a test_200b
17691                 pool_add $POOL                          || { rc=$? ; break; }
17692                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
17693                 # former test_200c test_200d
17694                 mkdir -p $test_path
17695                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
17696                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
17697                 mkdir -p $subdir
17698                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
17699                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
17700                                                         || { rc=$? ; break; }
17701                 # former test_200e test_200f
17702                 local files=$((OSTCOUNT*3))
17703                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
17704                                                         || { rc=$? ; break; }
17705                 pool_create_files $POOL $file_dir $files "$ost_list" \
17706                                                         || { rc=$? ; break; }
17707                 # former test_200g test_200h
17708                 pool_lfs_df $POOL                       || { rc=$? ; break; }
17709                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
17710
17711                 # former test_201a test_201b test_201c
17712                 pool_remove_first_target $POOL          || { rc=$? ; break; }
17713
17714                 local f=$test_path/$tfile
17715                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
17716                 pool_remove $POOL $f                    || { rc=$? ; break; }
17717                 break
17718         done
17719
17720         destroy_test_pools
17721
17722         return $rc
17723 }
17724 run_test 200 "OST pools"
17725
17726 # usage: default_attr <count | size | offset>
17727 default_attr() {
17728         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
17729 }
17730
17731 # usage: check_default_stripe_attr
17732 check_default_stripe_attr() {
17733         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
17734         case $1 in
17735         --stripe-count|-c)
17736                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
17737         --stripe-size|-S)
17738                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
17739         --stripe-index|-i)
17740                 EXPECTED=-1;;
17741         *)
17742                 error "unknown getstripe attr '$1'"
17743         esac
17744
17745         [ $ACTUAL == $EXPECTED ] ||
17746                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
17747 }
17748
17749 test_204a() {
17750         test_mkdir $DIR/$tdir
17751         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
17752
17753         check_default_stripe_attr --stripe-count
17754         check_default_stripe_attr --stripe-size
17755         check_default_stripe_attr --stripe-index
17756 }
17757 run_test 204a "Print default stripe attributes"
17758
17759 test_204b() {
17760         test_mkdir $DIR/$tdir
17761         $LFS setstripe --stripe-count 1 $DIR/$tdir
17762
17763         check_default_stripe_attr --stripe-size
17764         check_default_stripe_attr --stripe-index
17765 }
17766 run_test 204b "Print default stripe size and offset"
17767
17768 test_204c() {
17769         test_mkdir $DIR/$tdir
17770         $LFS setstripe --stripe-size 65536 $DIR/$tdir
17771
17772         check_default_stripe_attr --stripe-count
17773         check_default_stripe_attr --stripe-index
17774 }
17775 run_test 204c "Print default stripe count and offset"
17776
17777 test_204d() {
17778         test_mkdir $DIR/$tdir
17779         $LFS setstripe --stripe-index 0 $DIR/$tdir
17780
17781         check_default_stripe_attr --stripe-count
17782         check_default_stripe_attr --stripe-size
17783 }
17784 run_test 204d "Print default stripe count and size"
17785
17786 test_204e() {
17787         test_mkdir $DIR/$tdir
17788         $LFS setstripe -d $DIR/$tdir
17789
17790         check_default_stripe_attr --stripe-count --raw
17791         check_default_stripe_attr --stripe-size --raw
17792         check_default_stripe_attr --stripe-index --raw
17793 }
17794 run_test 204e "Print raw stripe attributes"
17795
17796 test_204f() {
17797         test_mkdir $DIR/$tdir
17798         $LFS setstripe --stripe-count 1 $DIR/$tdir
17799
17800         check_default_stripe_attr --stripe-size --raw
17801         check_default_stripe_attr --stripe-index --raw
17802 }
17803 run_test 204f "Print raw stripe size and offset"
17804
17805 test_204g() {
17806         test_mkdir $DIR/$tdir
17807         $LFS setstripe --stripe-size 65536 $DIR/$tdir
17808
17809         check_default_stripe_attr --stripe-count --raw
17810         check_default_stripe_attr --stripe-index --raw
17811 }
17812 run_test 204g "Print raw stripe count and offset"
17813
17814 test_204h() {
17815         test_mkdir $DIR/$tdir
17816         $LFS setstripe --stripe-index 0 $DIR/$tdir
17817
17818         check_default_stripe_attr --stripe-count --raw
17819         check_default_stripe_attr --stripe-size --raw
17820 }
17821 run_test 204h "Print raw stripe count and size"
17822
17823 # Figure out which job scheduler is being used, if any,
17824 # or use a fake one
17825 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
17826         JOBENV=SLURM_JOB_ID
17827 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
17828         JOBENV=LSB_JOBID
17829 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
17830         JOBENV=PBS_JOBID
17831 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
17832         JOBENV=LOADL_STEP_ID
17833 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
17834         JOBENV=JOB_ID
17835 else
17836         $LCTL list_param jobid_name > /dev/null 2>&1
17837         if [ $? -eq 0 ]; then
17838                 JOBENV=nodelocal
17839         else
17840                 JOBENV=FAKE_JOBID
17841         fi
17842 fi
17843 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
17844
17845 verify_jobstats() {
17846         local cmd=($1)
17847         shift
17848         local facets="$@"
17849
17850 # we don't really need to clear the stats for this test to work, since each
17851 # command has a unique jobid, but it makes debugging easier if needed.
17852 #       for facet in $facets; do
17853 #               local dev=$(convert_facet2label $facet)
17854 #               # clear old jobstats
17855 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
17856 #       done
17857
17858         # use a new JobID for each test, or we might see an old one
17859         [ "$JOBENV" = "FAKE_JOBID" ] &&
17860                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
17861
17862         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
17863
17864         [ "$JOBENV" = "nodelocal" ] && {
17865                 FAKE_JOBID=id.$testnum.%e.$RANDOM
17866                 $LCTL set_param jobid_name=$FAKE_JOBID
17867                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
17868         }
17869
17870         log "Test: ${cmd[*]}"
17871         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
17872
17873         if [ $JOBENV = "FAKE_JOBID" ]; then
17874                 FAKE_JOBID=$JOBVAL ${cmd[*]}
17875         else
17876                 ${cmd[*]}
17877         fi
17878
17879         # all files are created on OST0000
17880         for facet in $facets; do
17881                 local stats="*.$(convert_facet2label $facet).job_stats"
17882
17883                 # strip out libtool wrappers for in-tree executables
17884                 if [ $(do_facet $facet lctl get_param $stats |
17885                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
17886                         do_facet $facet lctl get_param $stats
17887                         error "No jobstats for $JOBVAL found on $facet::$stats"
17888                 fi
17889         done
17890 }
17891
17892 jobstats_set() {
17893         local new_jobenv=$1
17894
17895         set_persistent_param_and_check client "jobid_var" \
17896                 "$FSNAME.sys.jobid_var" $new_jobenv
17897 }
17898
17899 test_205a() { # Job stats
17900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17901         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
17902                 skip "Need MDS version with at least 2.7.1"
17903         remote_mgs_nodsh && skip "remote MGS with nodsh"
17904         remote_mds_nodsh && skip "remote MDS with nodsh"
17905         remote_ost_nodsh && skip "remote OST with nodsh"
17906         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
17907                 skip "Server doesn't support jobstats"
17908         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
17909
17910         local old_jobenv=$($LCTL get_param -n jobid_var)
17911         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
17912
17913         if [[ $PERM_CMD == *"set_param -P"* ]]; then
17914                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
17915         else
17916                 stack_trap "do_facet mgs $PERM_CMD \
17917                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
17918         fi
17919         changelog_register
17920
17921         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
17922                                 mdt.*.job_cleanup_interval | head -n 1)
17923         local new_interval=5
17924         do_facet $SINGLEMDS \
17925                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
17926         stack_trap "do_facet $SINGLEMDS \
17927                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
17928         local start=$SECONDS
17929
17930         local cmd
17931         # mkdir
17932         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir"
17933         verify_jobstats "$cmd" "$SINGLEMDS"
17934         # rmdir
17935         cmd="rmdir $DIR/$tdir"
17936         verify_jobstats "$cmd" "$SINGLEMDS"
17937         # mkdir on secondary MDT
17938         if [ $MDSCOUNT -gt 1 ]; then
17939                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
17940                 verify_jobstats "$cmd" "mds2"
17941         fi
17942         # mknod
17943         cmd="mknod $DIR/$tfile c 1 3"
17944         verify_jobstats "$cmd" "$SINGLEMDS"
17945         # unlink
17946         cmd="rm -f $DIR/$tfile"
17947         verify_jobstats "$cmd" "$SINGLEMDS"
17948         # create all files on OST0000 so verify_jobstats can find OST stats
17949         # open & close
17950         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
17951         verify_jobstats "$cmd" "$SINGLEMDS"
17952         # setattr
17953         cmd="touch $DIR/$tfile"
17954         verify_jobstats "$cmd" "$SINGLEMDS ost1"
17955         # write
17956         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
17957         verify_jobstats "$cmd" "ost1"
17958         # read
17959         cancel_lru_locks osc
17960         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
17961         verify_jobstats "$cmd" "ost1"
17962         # truncate
17963         cmd="$TRUNCATE $DIR/$tfile 0"
17964         verify_jobstats "$cmd" "$SINGLEMDS ost1"
17965         # rename
17966         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
17967         verify_jobstats "$cmd" "$SINGLEMDS"
17968         # jobstats expiry - sleep until old stats should be expired
17969         local left=$((new_interval + 5 - (SECONDS - start)))
17970         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
17971                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
17972                         "0" $left
17973         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir.expire"
17974         verify_jobstats "$cmd" "$SINGLEMDS"
17975         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
17976             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
17977
17978         # Ensure that jobid are present in changelog (if supported by MDS)
17979         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
17980                 changelog_dump | tail -10
17981                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
17982                 [ $jobids -eq 9 ] ||
17983                         error "Wrong changelog jobid count $jobids != 9"
17984
17985                 # LU-5862
17986                 JOBENV="disable"
17987                 jobstats_set $JOBENV
17988                 touch $DIR/$tfile
17989                 changelog_dump | grep $tfile
17990                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
17991                 [ $jobids -eq 0 ] ||
17992                         error "Unexpected jobids when jobid_var=$JOBENV"
17993         fi
17994
17995         # test '%j' access to environment variable - if supported
17996         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"; then
17997                 JOBENV="JOBCOMPLEX"
17998                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
17999
18000                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18001         fi
18002
18003         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%H.E"; then
18004                 JOBENV="JOBCOMPLEX"
18005                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname -s).E"
18006
18007                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18008         fi
18009
18010         # test '%j' access to per-session jobid - if supported
18011         if lctl list_param jobid_this_session > /dev/null 2>&1
18012         then
18013                 lctl set_param jobid_var=session jobid_name="S.%j.%e.%u.%h.E"
18014                 lctl set_param jobid_this_session=$USER
18015
18016                 JOBENV="JOBCOMPLEX"
18017                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18018
18019                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18020         fi
18021 }
18022 run_test 205a "Verify job stats"
18023
18024 # LU-13117, LU-13597
18025 test_205b() {
18026         (( $MDS1_VERSION >= $(version_code 2.13.54.91) )) ||
18027                 skip "Need MDS version at least 2.13.54.91"
18028
18029         job_stats="mdt.*.job_stats"
18030         $LCTL set_param $job_stats=clear
18031         # Setting jobid_var to USER might not be supported
18032         $LCTL set_param jobid_var=USER || true
18033         $LCTL set_param jobid_name="%e.%u"
18034         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
18035         do_facet $SINGLEMDS $LCTL get_param $job_stats |
18036                 grep "job_id:.*foolish" &&
18037                         error "Unexpected jobid found"
18038         do_facet $SINGLEMDS $LCTL get_param $job_stats |
18039                 grep "open:.*min.*max.*sum" ||
18040                         error "wrong job_stats format found"
18041 }
18042 run_test 205b "Verify job stats jobid and output format"
18043
18044 # LU-13733
18045 test_205c() {
18046         $LCTL set_param llite.*.stats=0
18047         dd if=/dev/zero of=$DIR/$tfile.1 bs=4k count=1
18048         $LCTL get_param llite.*.stats
18049         $LCTL get_param llite.*.stats | grep \
18050                 "write_bytes *1 samples \[bytes\] 4096 4096 4096 16777216" ||
18051                         error "wrong client stats format found"
18052 }
18053 run_test 205c "Verify client stats format"
18054
18055 # LU-1480, LU-1773 and LU-1657
18056 test_206() {
18057         mkdir -p $DIR/$tdir
18058         $LFS setstripe -c -1 $DIR/$tdir
18059 #define OBD_FAIL_LOV_INIT 0x1403
18060         $LCTL set_param fail_loc=0xa0001403
18061         $LCTL set_param fail_val=1
18062         touch $DIR/$tdir/$tfile || true
18063 }
18064 run_test 206 "fail lov_init_raid0() doesn't lbug"
18065
18066 test_207a() {
18067         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
18068         local fsz=`stat -c %s $DIR/$tfile`
18069         cancel_lru_locks mdc
18070
18071         # do not return layout in getattr intent
18072 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
18073         $LCTL set_param fail_loc=0x170
18074         local sz=`stat -c %s $DIR/$tfile`
18075
18076         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
18077
18078         rm -rf $DIR/$tfile
18079 }
18080 run_test 207a "can refresh layout at glimpse"
18081
18082 test_207b() {
18083         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
18084         local cksum=`md5sum $DIR/$tfile`
18085         local fsz=`stat -c %s $DIR/$tfile`
18086         cancel_lru_locks mdc
18087         cancel_lru_locks osc
18088
18089         # do not return layout in getattr intent
18090 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
18091         $LCTL set_param fail_loc=0x171
18092
18093         # it will refresh layout after the file is opened but before read issues
18094         echo checksum is "$cksum"
18095         echo "$cksum" |md5sum -c --quiet || error "file differs"
18096
18097         rm -rf $DIR/$tfile
18098 }
18099 run_test 207b "can refresh layout at open"
18100
18101 test_208() {
18102         # FIXME: in this test suite, only RD lease is used. This is okay
18103         # for now as only exclusive open is supported. After generic lease
18104         # is done, this test suite should be revised. - Jinshan
18105
18106         remote_mds_nodsh && skip "remote MDS with nodsh"
18107         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
18108                 skip "Need MDS version at least 2.4.52"
18109
18110         echo "==== test 1: verify get lease work"
18111         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
18112
18113         echo "==== test 2: verify lease can be broken by upcoming open"
18114         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
18115         local PID=$!
18116         sleep 1
18117
18118         $MULTIOP $DIR/$tfile oO_RDONLY:c
18119         kill -USR1 $PID && wait $PID || error "break lease error"
18120
18121         echo "==== test 3: verify lease can't be granted if an open already exists"
18122         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
18123         local PID=$!
18124         sleep 1
18125
18126         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
18127         kill -USR1 $PID && wait $PID || error "open file error"
18128
18129         echo "==== test 4: lease can sustain over recovery"
18130         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
18131         PID=$!
18132         sleep 1
18133
18134         fail mds1
18135
18136         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
18137
18138         echo "==== test 5: lease broken can't be regained by replay"
18139         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
18140         PID=$!
18141         sleep 1
18142
18143         # open file to break lease and then recovery
18144         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
18145         fail mds1
18146
18147         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
18148
18149         rm -f $DIR/$tfile
18150 }
18151 run_test 208 "Exclusive open"
18152
18153 test_209() {
18154         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
18155                 skip_env "must have disp_stripe"
18156
18157         touch $DIR/$tfile
18158         sync; sleep 5; sync;
18159
18160         echo 3 > /proc/sys/vm/drop_caches
18161         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
18162                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
18163         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
18164
18165         # open/close 500 times
18166         for i in $(seq 500); do
18167                 cat $DIR/$tfile
18168         done
18169
18170         echo 3 > /proc/sys/vm/drop_caches
18171         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
18172                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
18173         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
18174
18175         echo "before: $req_before, after: $req_after"
18176         [ $((req_after - req_before)) -ge 300 ] &&
18177                 error "open/close requests are not freed"
18178         return 0
18179 }
18180 run_test 209 "read-only open/close requests should be freed promptly"
18181
18182 test_210() {
18183         local pid
18184
18185         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
18186         pid=$!
18187         sleep 1
18188
18189         $LFS getstripe $DIR/$tfile
18190         kill -USR1 $pid
18191         wait $pid || error "multiop failed"
18192
18193         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
18194         pid=$!
18195         sleep 1
18196
18197         $LFS getstripe $DIR/$tfile
18198         kill -USR1 $pid
18199         wait $pid || error "multiop failed"
18200 }
18201 run_test 210 "lfs getstripe does not break leases"
18202
18203 test_212() {
18204         size=`date +%s`
18205         size=$((size % 8192 + 1))
18206         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
18207         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
18208         rm -f $DIR/f212 $DIR/f212.xyz
18209 }
18210 run_test 212 "Sendfile test ============================================"
18211
18212 test_213() {
18213         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
18214         cancel_lru_locks osc
18215         lctl set_param fail_loc=0x8000040f
18216         # generate a read lock
18217         cat $DIR/$tfile > /dev/null
18218         # write to the file, it will try to cancel the above read lock.
18219         cat /etc/hosts >> $DIR/$tfile
18220 }
18221 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
18222
18223 test_214() { # for bug 20133
18224         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
18225         for (( i=0; i < 340; i++ )) ; do
18226                 touch $DIR/$tdir/d214c/a$i
18227         done
18228
18229         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
18230         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
18231         ls $DIR/d214c || error "ls $DIR/d214c failed"
18232         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
18233         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
18234 }
18235 run_test 214 "hash-indexed directory test - bug 20133"
18236
18237 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
18238 create_lnet_proc_files() {
18239         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
18240 }
18241
18242 # counterpart of create_lnet_proc_files
18243 remove_lnet_proc_files() {
18244         rm -f $TMP/lnet_$1.sys
18245 }
18246
18247 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
18248 # 3rd arg as regexp for body
18249 check_lnet_proc_stats() {
18250         local l=$(cat "$TMP/lnet_$1" |wc -l)
18251         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
18252
18253         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
18254 }
18255
18256 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
18257 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
18258 # optional and can be regexp for 2nd line (lnet.routes case)
18259 check_lnet_proc_entry() {
18260         local blp=2          # blp stands for 'position of 1st line of body'
18261         [ -z "$5" ] || blp=3 # lnet.routes case
18262
18263         local l=$(cat "$TMP/lnet_$1" |wc -l)
18264         # subtracting one from $blp because the body can be empty
18265         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
18266
18267         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
18268                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
18269
18270         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
18271                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
18272
18273         # bail out if any unexpected line happened
18274         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
18275         [ "$?" != 0 ] || error "$2 misformatted"
18276 }
18277
18278 test_215() { # for bugs 18102, 21079, 21517
18279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18280
18281         local N='(0|[1-9][0-9]*)'       # non-negative numeric
18282         local P='[1-9][0-9]*'           # positive numeric
18283         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
18284         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
18285         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
18286         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
18287
18288         local L1 # regexp for 1st line
18289         local L2 # regexp for 2nd line (optional)
18290         local BR # regexp for the rest (body)
18291
18292         # lnet.stats should look as 11 space-separated non-negative numerics
18293         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
18294         create_lnet_proc_files "stats"
18295         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
18296         remove_lnet_proc_files "stats"
18297
18298         # lnet.routes should look like this:
18299         # Routing disabled/enabled
18300         # net hops priority state router
18301         # where net is a string like tcp0, hops > 0, priority >= 0,
18302         # state is up/down,
18303         # router is a string like 192.168.1.1@tcp2
18304         L1="^Routing (disabled|enabled)$"
18305         L2="^net +hops +priority +state +router$"
18306         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
18307         create_lnet_proc_files "routes"
18308         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
18309         remove_lnet_proc_files "routes"
18310
18311         # lnet.routers should look like this:
18312         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
18313         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
18314         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
18315         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
18316         L1="^ref +rtr_ref +alive +router$"
18317         BR="^$P +$P +(up|down) +$NID$"
18318         create_lnet_proc_files "routers"
18319         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
18320         remove_lnet_proc_files "routers"
18321
18322         # lnet.peers should look like this:
18323         # nid refs state last max rtr min tx min queue
18324         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
18325         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
18326         # numeric (0 or >0 or <0), queue >= 0.
18327         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
18328         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
18329         create_lnet_proc_files "peers"
18330         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
18331         remove_lnet_proc_files "peers"
18332
18333         # lnet.buffers  should look like this:
18334         # pages count credits min
18335         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
18336         L1="^pages +count +credits +min$"
18337         BR="^ +$N +$N +$I +$I$"
18338         create_lnet_proc_files "buffers"
18339         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
18340         remove_lnet_proc_files "buffers"
18341
18342         # lnet.nis should look like this:
18343         # nid status alive refs peer rtr max tx min
18344         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
18345         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
18346         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
18347         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
18348         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
18349         create_lnet_proc_files "nis"
18350         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
18351         remove_lnet_proc_files "nis"
18352
18353         # can we successfully write to lnet.stats?
18354         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
18355 }
18356 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
18357
18358 test_216() { # bug 20317
18359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18360         remote_ost_nodsh && skip "remote OST with nodsh"
18361
18362         local node
18363         local facets=$(get_facets OST)
18364         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
18365
18366         save_lustre_params client "osc.*.contention_seconds" > $p
18367         save_lustre_params $facets \
18368                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
18369         save_lustre_params $facets \
18370                 "ldlm.namespaces.filter-*.contended_locks" >> $p
18371         save_lustre_params $facets \
18372                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
18373         clear_stats osc.*.osc_stats
18374
18375         # agressive lockless i/o settings
18376         do_nodes $(comma_list $(osts_nodes)) \
18377                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
18378                         ldlm.namespaces.filter-*.contended_locks=0 \
18379                         ldlm.namespaces.filter-*.contention_seconds=60"
18380         lctl set_param -n osc.*.contention_seconds=60
18381
18382         $DIRECTIO write $DIR/$tfile 0 10 4096
18383         $CHECKSTAT -s 40960 $DIR/$tfile
18384
18385         # disable lockless i/o
18386         do_nodes $(comma_list $(osts_nodes)) \
18387                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
18388                         ldlm.namespaces.filter-*.contended_locks=32 \
18389                         ldlm.namespaces.filter-*.contention_seconds=0"
18390         lctl set_param -n osc.*.contention_seconds=0
18391         clear_stats osc.*.osc_stats
18392
18393         dd if=/dev/zero of=$DIR/$tfile count=0
18394         $CHECKSTAT -s 0 $DIR/$tfile
18395
18396         restore_lustre_params <$p
18397         rm -f $p
18398         rm $DIR/$tfile
18399 }
18400 run_test 216 "check lockless direct write updates file size and kms correctly"
18401
18402 test_217() { # bug 22430
18403         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18404
18405         local node
18406         local nid
18407
18408         for node in $(nodes_list); do
18409                 nid=$(host_nids_address $node $NETTYPE)
18410                 if [[ $nid = *-* ]] ; then
18411                         echo "lctl ping $(h2nettype $nid)"
18412                         lctl ping $(h2nettype $nid)
18413                 else
18414                         echo "skipping $node (no hyphen detected)"
18415                 fi
18416         done
18417 }
18418 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
18419
18420 test_218() {
18421        # do directio so as not to populate the page cache
18422        log "creating a 10 Mb file"
18423        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
18424        log "starting reads"
18425        dd if=$DIR/$tfile of=/dev/null bs=4096 &
18426        log "truncating the file"
18427        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
18428        log "killing dd"
18429        kill %+ || true # reads might have finished
18430        echo "wait until dd is finished"
18431        wait
18432        log "removing the temporary file"
18433        rm -rf $DIR/$tfile || error "tmp file removal failed"
18434 }
18435 run_test 218 "parallel read and truncate should not deadlock"
18436
18437 test_219() {
18438         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18439
18440         # write one partial page
18441         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
18442         # set no grant so vvp_io_commit_write will do sync write
18443         $LCTL set_param fail_loc=0x411
18444         # write a full page at the end of file
18445         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
18446
18447         $LCTL set_param fail_loc=0
18448         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
18449         $LCTL set_param fail_loc=0x411
18450         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
18451
18452         # LU-4201
18453         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
18454         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
18455 }
18456 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
18457
18458 test_220() { #LU-325
18459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18460         remote_ost_nodsh && skip "remote OST with nodsh"
18461         remote_mds_nodsh && skip "remote MDS with nodsh"
18462         remote_mgs_nodsh && skip "remote MGS with nodsh"
18463
18464         local OSTIDX=0
18465
18466         # create on MDT0000 so the last_id and next_id are correct
18467         mkdir_on_mdt0 $DIR/$tdir
18468         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
18469         OST=${OST%_UUID}
18470
18471         # on the mdt's osc
18472         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
18473         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
18474                         osp.$mdtosc_proc1.prealloc_last_id)
18475         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
18476                         osp.$mdtosc_proc1.prealloc_next_id)
18477
18478         $LFS df -i
18479
18480         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
18481         #define OBD_FAIL_OST_ENOINO              0x229
18482         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
18483         create_pool $FSNAME.$TESTNAME || return 1
18484         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
18485
18486         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
18487
18488         MDSOBJS=$((last_id - next_id))
18489         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
18490
18491         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
18492         echo "OST still has $count kbytes free"
18493
18494         echo "create $MDSOBJS files @next_id..."
18495         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
18496
18497         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
18498                         osp.$mdtosc_proc1.prealloc_last_id)
18499         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
18500                         osp.$mdtosc_proc1.prealloc_next_id)
18501
18502         echo "after creation, last_id=$last_id2, next_id=$next_id2"
18503         $LFS df -i
18504
18505         echo "cleanup..."
18506
18507         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
18508         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
18509
18510         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
18511                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
18512         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
18513                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
18514         echo "unlink $MDSOBJS files @$next_id..."
18515         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
18516 }
18517 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
18518
18519 test_221() {
18520         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18521
18522         dd if=`which date` of=$MOUNT/date oflag=sync
18523         chmod +x $MOUNT/date
18524
18525         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
18526         $LCTL set_param fail_loc=0x80001401
18527
18528         $MOUNT/date > /dev/null
18529         rm -f $MOUNT/date
18530 }
18531 run_test 221 "make sure fault and truncate race to not cause OOM"
18532
18533 test_222a () {
18534         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18535
18536         rm -rf $DIR/$tdir
18537         test_mkdir $DIR/$tdir
18538         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18539         createmany -o $DIR/$tdir/$tfile 10
18540         cancel_lru_locks mdc
18541         cancel_lru_locks osc
18542         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
18543         $LCTL set_param fail_loc=0x31a
18544         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
18545         $LCTL set_param fail_loc=0
18546         rm -r $DIR/$tdir
18547 }
18548 run_test 222a "AGL for ls should not trigger CLIO lock failure"
18549
18550 test_222b () {
18551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18552
18553         rm -rf $DIR/$tdir
18554         test_mkdir $DIR/$tdir
18555         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18556         createmany -o $DIR/$tdir/$tfile 10
18557         cancel_lru_locks mdc
18558         cancel_lru_locks osc
18559         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
18560         $LCTL set_param fail_loc=0x31a
18561         rm -r $DIR/$tdir || error "AGL for rmdir failed"
18562         $LCTL set_param fail_loc=0
18563 }
18564 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
18565
18566 test_223 () {
18567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18568
18569         rm -rf $DIR/$tdir
18570         test_mkdir $DIR/$tdir
18571         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18572         createmany -o $DIR/$tdir/$tfile 10
18573         cancel_lru_locks mdc
18574         cancel_lru_locks osc
18575         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
18576         $LCTL set_param fail_loc=0x31b
18577         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
18578         $LCTL set_param fail_loc=0
18579         rm -r $DIR/$tdir
18580 }
18581 run_test 223 "osc reenqueue if without AGL lock granted ======================="
18582
18583 test_224a() { # LU-1039, MRP-303
18584         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18585
18586         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
18587         $LCTL set_param fail_loc=0x508
18588         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
18589         $LCTL set_param fail_loc=0
18590         df $DIR
18591 }
18592 run_test 224a "Don't panic on bulk IO failure"
18593
18594 test_224b() { # LU-1039, MRP-303
18595         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18596
18597         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
18598         cancel_lru_locks osc
18599         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
18600         $LCTL set_param fail_loc=0x515
18601         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
18602         $LCTL set_param fail_loc=0
18603         df $DIR
18604 }
18605 run_test 224b "Don't panic on bulk IO failure"
18606
18607 test_224c() { # LU-6441
18608         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18609         remote_mds_nodsh && skip "remote MDS with nodsh"
18610
18611         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
18612         save_writethrough $p
18613         set_cache writethrough on
18614
18615         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
18616         local at_max=$($LCTL get_param -n at_max)
18617         local timeout=$($LCTL get_param -n timeout)
18618         local test_at="at_max"
18619         local param_at="$FSNAME.sys.at_max"
18620         local test_timeout="timeout"
18621         local param_timeout="$FSNAME.sys.timeout"
18622
18623         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
18624
18625         set_persistent_param_and_check client "$test_at" "$param_at" 0
18626         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
18627
18628         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
18629         do_facet ost1 "$LCTL set_param fail_loc=0x520"
18630         $LFS setstripe -c 1 -i 0 $DIR/$tfile
18631         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
18632         sync
18633         do_facet ost1 "$LCTL set_param fail_loc=0"
18634
18635         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
18636         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
18637                 $timeout
18638
18639         $LCTL set_param -n $pages_per_rpc
18640         restore_lustre_params < $p
18641         rm -f $p
18642 }
18643 run_test 224c "Don't hang if one of md lost during large bulk RPC"
18644
18645 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
18646 test_225a () {
18647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18648         if [ -z ${MDSSURVEY} ]; then
18649                 skip_env "mds-survey not found"
18650         fi
18651         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
18652                 skip "Need MDS version at least 2.2.51"
18653
18654         local mds=$(facet_host $SINGLEMDS)
18655         local target=$(do_nodes $mds 'lctl dl' |
18656                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
18657
18658         local cmd1="file_count=1000 thrhi=4"
18659         local cmd2="dir_count=2 layer=mdd stripe_count=0"
18660         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
18661         local cmd="$cmd1 $cmd2 $cmd3"
18662
18663         rm -f ${TMP}/mds_survey*
18664         echo + $cmd
18665         eval $cmd || error "mds-survey with zero-stripe failed"
18666         cat ${TMP}/mds_survey*
18667         rm -f ${TMP}/mds_survey*
18668 }
18669 run_test 225a "Metadata survey sanity with zero-stripe"
18670
18671 test_225b () {
18672         if [ -z ${MDSSURVEY} ]; then
18673                 skip_env "mds-survey not found"
18674         fi
18675         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
18676                 skip "Need MDS version at least 2.2.51"
18677         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18678         remote_mds_nodsh && skip "remote MDS with nodsh"
18679         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
18680                 skip_env "Need to mount OST to test"
18681         fi
18682
18683         local mds=$(facet_host $SINGLEMDS)
18684         local target=$(do_nodes $mds 'lctl dl' |
18685                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
18686
18687         local cmd1="file_count=1000 thrhi=4"
18688         local cmd2="dir_count=2 layer=mdd stripe_count=1"
18689         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
18690         local cmd="$cmd1 $cmd2 $cmd3"
18691
18692         rm -f ${TMP}/mds_survey*
18693         echo + $cmd
18694         eval $cmd || error "mds-survey with stripe_count failed"
18695         cat ${TMP}/mds_survey*
18696         rm -f ${TMP}/mds_survey*
18697 }
18698 run_test 225b "Metadata survey sanity with stripe_count = 1"
18699
18700 mcreate_path2fid () {
18701         local mode=$1
18702         local major=$2
18703         local minor=$3
18704         local name=$4
18705         local desc=$5
18706         local path=$DIR/$tdir/$name
18707         local fid
18708         local rc
18709         local fid_path
18710
18711         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
18712                 error "cannot create $desc"
18713
18714         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
18715         rc=$?
18716         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
18717
18718         fid_path=$($LFS fid2path $MOUNT $fid)
18719         rc=$?
18720         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
18721
18722         [ "$path" == "$fid_path" ] ||
18723                 error "fid2path returned $fid_path, expected $path"
18724
18725         echo "pass with $path and $fid"
18726 }
18727
18728 test_226a () {
18729         rm -rf $DIR/$tdir
18730         mkdir -p $DIR/$tdir
18731
18732         mcreate_path2fid 0010666 0 0 fifo "FIFO"
18733         mcreate_path2fid 0020666 1 3 null "character special file (null)"
18734         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
18735         mcreate_path2fid 0040666 0 0 dir "directory"
18736         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
18737         mcreate_path2fid 0100666 0 0 file "regular file"
18738         mcreate_path2fid 0120666 0 0 link "symbolic link"
18739         mcreate_path2fid 0140666 0 0 sock "socket"
18740 }
18741 run_test 226a "call path2fid and fid2path on files of all type"
18742
18743 test_226b () {
18744         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18745
18746         local MDTIDX=1
18747
18748         rm -rf $DIR/$tdir
18749         mkdir -p $DIR/$tdir
18750         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
18751                 error "create remote directory failed"
18752         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
18753         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
18754                                 "character special file (null)"
18755         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
18756                                 "character special file (no device)"
18757         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
18758         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
18759                                 "block special file (loop)"
18760         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
18761         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
18762         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
18763 }
18764 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
18765
18766 test_226c () {
18767         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18768         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
18769                 skip "Need MDS version at least 2.13.55"
18770
18771         local submnt=/mnt/submnt
18772         local srcfile=/etc/passwd
18773         local dstfile=$submnt/passwd
18774         local path
18775         local fid
18776
18777         rm -rf $DIR/$tdir
18778         rm -rf $submnt
18779         $LFS setdirstripe -c -1 -i 1 $DIR/$tdir ||
18780                 error "create remote directory failed"
18781         mkdir -p $submnt || error "create $submnt failed"
18782         $MOUNT_CMD $MGSNID:/$FSNAME/$tdir $submnt ||
18783                 error "mount $submnt failed"
18784         stack_trap "umount $submnt" EXIT
18785
18786         cp $srcfile $dstfile
18787         fid=$($LFS path2fid $dstfile)
18788         path=$($LFS fid2path $submnt "$fid")
18789         [ "$path" = "$dstfile" ] ||
18790                 error "fid2path $submnt $fid failed ($path != $dstfile)"
18791 }
18792 run_test 226c "call path2fid and fid2path under remote dir with subdir mount"
18793
18794 # LU-1299 Executing or running ldd on a truncated executable does not
18795 # cause an out-of-memory condition.
18796 test_227() {
18797         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18798         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
18799
18800         dd if=$(which date) of=$MOUNT/date bs=1k count=1
18801         chmod +x $MOUNT/date
18802
18803         $MOUNT/date > /dev/null
18804         ldd $MOUNT/date > /dev/null
18805         rm -f $MOUNT/date
18806 }
18807 run_test 227 "running truncated executable does not cause OOM"
18808
18809 # LU-1512 try to reuse idle OI blocks
18810 test_228a() {
18811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18812         remote_mds_nodsh && skip "remote MDS with nodsh"
18813         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
18814
18815         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
18816         local myDIR=$DIR/$tdir
18817
18818         mkdir -p $myDIR
18819         #define OBD_FAIL_SEQ_EXHAUST             0x1002
18820         $LCTL set_param fail_loc=0x80001002
18821         createmany -o $myDIR/t- 10000
18822         $LCTL set_param fail_loc=0
18823         # The guard is current the largest FID holder
18824         touch $myDIR/guard
18825         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
18826                     tr -d '[')
18827         local IDX=$(($SEQ % 64))
18828
18829         do_facet $SINGLEMDS sync
18830         # Make sure journal flushed.
18831         sleep 6
18832         local blk1=$(do_facet $SINGLEMDS \
18833                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
18834                      grep Blockcount | awk '{print $4}')
18835
18836         # Remove old files, some OI blocks will become idle.
18837         unlinkmany $myDIR/t- 10000
18838         # Create new files, idle OI blocks should be reused.
18839         createmany -o $myDIR/t- 2000
18840         do_facet $SINGLEMDS sync
18841         # Make sure journal flushed.
18842         sleep 6
18843         local blk2=$(do_facet $SINGLEMDS \
18844                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
18845                      grep Blockcount | awk '{print $4}')
18846
18847         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
18848 }
18849 run_test 228a "try to reuse idle OI blocks"
18850
18851 test_228b() {
18852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18853         remote_mds_nodsh && skip "remote MDS with nodsh"
18854         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
18855
18856         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
18857         local myDIR=$DIR/$tdir
18858
18859         mkdir -p $myDIR
18860         #define OBD_FAIL_SEQ_EXHAUST             0x1002
18861         $LCTL set_param fail_loc=0x80001002
18862         createmany -o $myDIR/t- 10000
18863         $LCTL set_param fail_loc=0
18864         # The guard is current the largest FID holder
18865         touch $myDIR/guard
18866         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
18867                     tr -d '[')
18868         local IDX=$(($SEQ % 64))
18869
18870         do_facet $SINGLEMDS sync
18871         # Make sure journal flushed.
18872         sleep 6
18873         local blk1=$(do_facet $SINGLEMDS \
18874                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
18875                      grep Blockcount | awk '{print $4}')
18876
18877         # Remove old files, some OI blocks will become idle.
18878         unlinkmany $myDIR/t- 10000
18879
18880         # stop the MDT
18881         stop $SINGLEMDS || error "Fail to stop MDT."
18882         # remount the MDT
18883         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
18884
18885         df $MOUNT || error "Fail to df."
18886         # Create new files, idle OI blocks should be reused.
18887         createmany -o $myDIR/t- 2000
18888         do_facet $SINGLEMDS sync
18889         # Make sure journal flushed.
18890         sleep 6
18891         local blk2=$(do_facet $SINGLEMDS \
18892                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
18893                      grep Blockcount | awk '{print $4}')
18894
18895         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
18896 }
18897 run_test 228b "idle OI blocks can be reused after MDT restart"
18898
18899 #LU-1881
18900 test_228c() {
18901         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18902         remote_mds_nodsh && skip "remote MDS with nodsh"
18903         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
18904
18905         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
18906         local myDIR=$DIR/$tdir
18907
18908         mkdir -p $myDIR
18909         #define OBD_FAIL_SEQ_EXHAUST             0x1002
18910         $LCTL set_param fail_loc=0x80001002
18911         # 20000 files can guarantee there are index nodes in the OI file
18912         createmany -o $myDIR/t- 20000
18913         $LCTL set_param fail_loc=0
18914         # The guard is current the largest FID holder
18915         touch $myDIR/guard
18916         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
18917                     tr -d '[')
18918         local IDX=$(($SEQ % 64))
18919
18920         do_facet $SINGLEMDS sync
18921         # Make sure journal flushed.
18922         sleep 6
18923         local blk1=$(do_facet $SINGLEMDS \
18924                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
18925                      grep Blockcount | awk '{print $4}')
18926
18927         # Remove old files, some OI blocks will become idle.
18928         unlinkmany $myDIR/t- 20000
18929         rm -f $myDIR/guard
18930         # The OI file should become empty now
18931
18932         # Create new files, idle OI blocks should be reused.
18933         createmany -o $myDIR/t- 2000
18934         do_facet $SINGLEMDS sync
18935         # Make sure journal flushed.
18936         sleep 6
18937         local blk2=$(do_facet $SINGLEMDS \
18938                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
18939                      grep Blockcount | awk '{print $4}')
18940
18941         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
18942 }
18943 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
18944
18945 test_229() { # LU-2482, LU-3448
18946         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18947         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
18948         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
18949                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
18950
18951         rm -f $DIR/$tfile
18952
18953         # Create a file with a released layout and stripe count 2.
18954         $MULTIOP $DIR/$tfile H2c ||
18955                 error "failed to create file with released layout"
18956
18957         $LFS getstripe -v $DIR/$tfile
18958
18959         local pattern=$($LFS getstripe -L $DIR/$tfile)
18960         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
18961
18962         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
18963                 error "getstripe"
18964         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
18965         stat $DIR/$tfile || error "failed to stat released file"
18966
18967         chown $RUNAS_ID $DIR/$tfile ||
18968                 error "chown $RUNAS_ID $DIR/$tfile failed"
18969
18970         chgrp $RUNAS_ID $DIR/$tfile ||
18971                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
18972
18973         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
18974         rm $DIR/$tfile || error "failed to remove released file"
18975 }
18976 run_test 229 "getstripe/stat/rm/attr changes work on released files"
18977
18978 test_230a() {
18979         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18980         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18981         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
18982                 skip "Need MDS version at least 2.11.52"
18983
18984         local MDTIDX=1
18985
18986         test_mkdir $DIR/$tdir
18987         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
18988         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
18989         [ $mdt_idx -ne 0 ] &&
18990                 error "create local directory on wrong MDT $mdt_idx"
18991
18992         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
18993                         error "create remote directory failed"
18994         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
18995         [ $mdt_idx -ne $MDTIDX ] &&
18996                 error "create remote directory on wrong MDT $mdt_idx"
18997
18998         createmany -o $DIR/$tdir/test_230/t- 10 ||
18999                 error "create files on remote directory failed"
19000         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
19001         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
19002         rm -r $DIR/$tdir || error "unlink remote directory failed"
19003 }
19004 run_test 230a "Create remote directory and files under the remote directory"
19005
19006 test_230b() {
19007         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19008         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19009         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19010                 skip "Need MDS version at least 2.11.52"
19011
19012         local MDTIDX=1
19013         local mdt_index
19014         local i
19015         local file
19016         local pid
19017         local stripe_count
19018         local migrate_dir=$DIR/$tdir/migrate_dir
19019         local other_dir=$DIR/$tdir/other_dir
19020
19021         test_mkdir $DIR/$tdir
19022         test_mkdir -i0 -c1 $migrate_dir
19023         test_mkdir -i0 -c1 $other_dir
19024         for ((i=0; i<10; i++)); do
19025                 mkdir -p $migrate_dir/dir_${i}
19026                 createmany -o $migrate_dir/dir_${i}/f 10 ||
19027                         error "create files under remote dir failed $i"
19028         done
19029
19030         cp /etc/passwd $migrate_dir/$tfile
19031         cp /etc/passwd $other_dir/$tfile
19032         chattr +SAD $migrate_dir
19033         chattr +SAD $migrate_dir/$tfile
19034
19035         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19036         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19037         local old_dir_mode=$(stat -c%f $migrate_dir)
19038         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
19039
19040         mkdir -p $migrate_dir/dir_default_stripe2
19041         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
19042         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
19043
19044         mkdir -p $other_dir
19045         ln $migrate_dir/$tfile $other_dir/luna
19046         ln $migrate_dir/$tfile $migrate_dir/sofia
19047         ln $other_dir/$tfile $migrate_dir/david
19048         ln -s $migrate_dir/$tfile $other_dir/zachary
19049         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
19050         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
19051
19052         local len
19053         local lnktgt
19054
19055         # inline symlink
19056         for len in 58 59 60; do
19057                 lnktgt=$(str_repeat 'l' $len)
19058                 touch $migrate_dir/$lnktgt
19059                 ln -s $lnktgt $migrate_dir/${len}char_ln
19060         done
19061
19062         # PATH_MAX
19063         for len in 4094 4095; do
19064                 lnktgt=$(str_repeat 'l' $len)
19065                 ln -s $lnktgt $migrate_dir/${len}char_ln
19066         done
19067
19068         # NAME_MAX
19069         for len in 254 255; do
19070                 touch $migrate_dir/$(str_repeat 'l' $len)
19071         done
19072
19073         $LFS migrate -m $MDTIDX $migrate_dir ||
19074                 error "fails on migrating remote dir to MDT1"
19075
19076         echo "migratate to MDT1, then checking.."
19077         for ((i = 0; i < 10; i++)); do
19078                 for file in $(find $migrate_dir/dir_${i}); do
19079                         mdt_index=$($LFS getstripe -m $file)
19080                         # broken symlink getstripe will fail
19081                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
19082                                 error "$file is not on MDT${MDTIDX}"
19083                 done
19084         done
19085
19086         # the multiple link file should still in MDT0
19087         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
19088         [ $mdt_index == 0 ] ||
19089                 error "$file is not on MDT${MDTIDX}"
19090
19091         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19092         [ "$old_dir_flag" = "$new_dir_flag" ] ||
19093                 error " expect $old_dir_flag get $new_dir_flag"
19094
19095         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19096         [ "$old_file_flag" = "$new_file_flag" ] ||
19097                 error " expect $old_file_flag get $new_file_flag"
19098
19099         local new_dir_mode=$(stat -c%f $migrate_dir)
19100         [ "$old_dir_mode" = "$new_dir_mode" ] ||
19101                 error "expect mode $old_dir_mode get $new_dir_mode"
19102
19103         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
19104         [ "$old_file_mode" = "$new_file_mode" ] ||
19105                 error "expect mode $old_file_mode get $new_file_mode"
19106
19107         diff /etc/passwd $migrate_dir/$tfile ||
19108                 error "$tfile different after migration"
19109
19110         diff /etc/passwd $other_dir/luna ||
19111                 error "luna different after migration"
19112
19113         diff /etc/passwd $migrate_dir/sofia ||
19114                 error "sofia different after migration"
19115
19116         diff /etc/passwd $migrate_dir/david ||
19117                 error "david different after migration"
19118
19119         diff /etc/passwd $other_dir/zachary ||
19120                 error "zachary different after migration"
19121
19122         diff /etc/passwd $migrate_dir/${tfile}_ln ||
19123                 error "${tfile}_ln different after migration"
19124
19125         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
19126                 error "${tfile}_ln_other different after migration"
19127
19128         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
19129         [ $stripe_count = 2 ] ||
19130                 error "dir strpe_count $d != 2 after migration."
19131
19132         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
19133         [ $stripe_count = 2 ] ||
19134                 error "file strpe_count $d != 2 after migration."
19135
19136         #migrate back to MDT0
19137         MDTIDX=0
19138
19139         $LFS migrate -m $MDTIDX $migrate_dir ||
19140                 error "fails on migrating remote dir to MDT0"
19141
19142         echo "migrate back to MDT0, checking.."
19143         for file in $(find $migrate_dir); do
19144                 mdt_index=$($LFS getstripe -m $file)
19145                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
19146                         error "$file is not on MDT${MDTIDX}"
19147         done
19148
19149         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19150         [ "$old_dir_flag" = "$new_dir_flag" ] ||
19151                 error " expect $old_dir_flag get $new_dir_flag"
19152
19153         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19154         [ "$old_file_flag" = "$new_file_flag" ] ||
19155                 error " expect $old_file_flag get $new_file_flag"
19156
19157         local new_dir_mode=$(stat -c%f $migrate_dir)
19158         [ "$old_dir_mode" = "$new_dir_mode" ] ||
19159                 error "expect mode $old_dir_mode get $new_dir_mode"
19160
19161         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
19162         [ "$old_file_mode" = "$new_file_mode" ] ||
19163                 error "expect mode $old_file_mode get $new_file_mode"
19164
19165         diff /etc/passwd ${migrate_dir}/$tfile ||
19166                 error "$tfile different after migration"
19167
19168         diff /etc/passwd ${other_dir}/luna ||
19169                 error "luna different after migration"
19170
19171         diff /etc/passwd ${migrate_dir}/sofia ||
19172                 error "sofia different after migration"
19173
19174         diff /etc/passwd ${other_dir}/zachary ||
19175                 error "zachary different after migration"
19176
19177         diff /etc/passwd $migrate_dir/${tfile}_ln ||
19178                 error "${tfile}_ln different after migration"
19179
19180         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
19181                 error "${tfile}_ln_other different after migration"
19182
19183         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
19184         [ $stripe_count = 2 ] ||
19185                 error "dir strpe_count $d != 2 after migration."
19186
19187         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
19188         [ $stripe_count = 2 ] ||
19189                 error "file strpe_count $d != 2 after migration."
19190
19191         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19192 }
19193 run_test 230b "migrate directory"
19194
19195 test_230c() {
19196         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19197         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19198         remote_mds_nodsh && skip "remote MDS with nodsh"
19199         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19200                 skip "Need MDS version at least 2.11.52"
19201
19202         local MDTIDX=1
19203         local total=3
19204         local mdt_index
19205         local file
19206         local migrate_dir=$DIR/$tdir/migrate_dir
19207
19208         #If migrating directory fails in the middle, all entries of
19209         #the directory is still accessiable.
19210         test_mkdir $DIR/$tdir
19211         test_mkdir -i0 -c1 $migrate_dir
19212         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
19213         stat $migrate_dir
19214         createmany -o $migrate_dir/f $total ||
19215                 error "create files under ${migrate_dir} failed"
19216
19217         # fail after migrating top dir, and this will fail only once, so the
19218         # first sub file migration will fail (currently f3), others succeed.
19219         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
19220         do_facet mds1 lctl set_param fail_loc=0x1801
19221         local t=$(ls $migrate_dir | wc -l)
19222         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
19223                 error "migrate should fail"
19224         local u=$(ls $migrate_dir | wc -l)
19225         [ "$u" == "$t" ] || error "$u != $t during migration"
19226
19227         # add new dir/file should succeed
19228         mkdir $migrate_dir/dir ||
19229                 error "mkdir failed under migrating directory"
19230         touch $migrate_dir/file ||
19231                 error "create file failed under migrating directory"
19232
19233         # add file with existing name should fail
19234         for file in $migrate_dir/f*; do
19235                 stat $file > /dev/null || error "stat $file failed"
19236                 $OPENFILE -f O_CREAT:O_EXCL $file &&
19237                         error "open(O_CREAT|O_EXCL) $file should fail"
19238                 $MULTIOP $file m && error "create $file should fail"
19239                 touch $DIR/$tdir/remote_dir/$tfile ||
19240                         error "touch $tfile failed"
19241                 ln $DIR/$tdir/remote_dir/$tfile $file &&
19242                         error "link $file should fail"
19243                 mdt_index=$($LFS getstripe -m $file)
19244                 if [ $mdt_index == 0 ]; then
19245                         # file failed to migrate is not allowed to rename to
19246                         mv $DIR/$tdir/remote_dir/$tfile $file &&
19247                                 error "rename to $file should fail"
19248                 else
19249                         mv $DIR/$tdir/remote_dir/$tfile $file ||
19250                                 error "rename to $file failed"
19251                 fi
19252                 echo hello >> $file || error "write $file failed"
19253         done
19254
19255         # resume migration with different options should fail
19256         $LFS migrate -m 0 $migrate_dir &&
19257                 error "migrate -m 0 $migrate_dir should fail"
19258
19259         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
19260                 error "migrate -c 2 $migrate_dir should fail"
19261
19262         # resume migration should succeed
19263         $LFS migrate -m $MDTIDX $migrate_dir ||
19264                 error "migrate $migrate_dir failed"
19265
19266         echo "Finish migration, then checking.."
19267         for file in $(find $migrate_dir); do
19268                 mdt_index=$($LFS getstripe -m $file)
19269                 [ $mdt_index == $MDTIDX ] ||
19270                         error "$file is not on MDT${MDTIDX}"
19271         done
19272
19273         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19274 }
19275 run_test 230c "check directory accessiblity if migration failed"
19276
19277 test_230d() {
19278         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19279         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19280         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19281                 skip "Need MDS version at least 2.11.52"
19282         # LU-11235
19283         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
19284
19285         local migrate_dir=$DIR/$tdir/migrate_dir
19286         local old_index
19287         local new_index
19288         local old_count
19289         local new_count
19290         local new_hash
19291         local mdt_index
19292         local i
19293         local j
19294
19295         old_index=$((RANDOM % MDSCOUNT))
19296         old_count=$((MDSCOUNT - old_index))
19297         new_index=$((RANDOM % MDSCOUNT))
19298         new_count=$((MDSCOUNT - new_index))
19299         new_hash=1 # for all_char
19300
19301         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
19302         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
19303
19304         test_mkdir $DIR/$tdir
19305         test_mkdir -i $old_index -c $old_count $migrate_dir
19306
19307         for ((i=0; i<100; i++)); do
19308                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
19309                 createmany -o $migrate_dir/dir_${i}/f 100 ||
19310                         error "create files under remote dir failed $i"
19311         done
19312
19313         echo -n "Migrate from MDT$old_index "
19314         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
19315         echo -n "to MDT$new_index"
19316         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
19317         echo
19318
19319         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
19320         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
19321                 error "migrate remote dir error"
19322
19323         echo "Finish migration, then checking.."
19324         for file in $(find $migrate_dir); do
19325                 mdt_index=$($LFS getstripe -m $file)
19326                 if [ $mdt_index -lt $new_index ] ||
19327                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
19328                         error "$file is on MDT$mdt_index"
19329                 fi
19330         done
19331
19332         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19333 }
19334 run_test 230d "check migrate big directory"
19335
19336 test_230e() {
19337         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19338         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19339         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19340                 skip "Need MDS version at least 2.11.52"
19341
19342         local i
19343         local j
19344         local a_fid
19345         local b_fid
19346
19347         mkdir_on_mdt0 $DIR/$tdir
19348         mkdir $DIR/$tdir/migrate_dir
19349         mkdir $DIR/$tdir/other_dir
19350         touch $DIR/$tdir/migrate_dir/a
19351         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
19352         ls $DIR/$tdir/other_dir
19353
19354         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19355                 error "migrate dir fails"
19356
19357         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
19358         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
19359
19360         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19361         [ $mdt_index == 0 ] || error "a is not on MDT0"
19362
19363         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
19364                 error "migrate dir fails"
19365
19366         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
19367         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
19368
19369         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19370         [ $mdt_index == 1 ] || error "a is not on MDT1"
19371
19372         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
19373         [ $mdt_index == 1 ] || error "b is not on MDT1"
19374
19375         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
19376         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
19377
19378         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
19379
19380         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19381 }
19382 run_test 230e "migrate mulitple local link files"
19383
19384 test_230f() {
19385         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19386         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19387         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19388                 skip "Need MDS version at least 2.11.52"
19389
19390         local a_fid
19391         local ln_fid
19392
19393         mkdir -p $DIR/$tdir
19394         mkdir $DIR/$tdir/migrate_dir
19395         $LFS mkdir -i1 $DIR/$tdir/other_dir
19396         touch $DIR/$tdir/migrate_dir/a
19397         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
19398         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
19399         ls $DIR/$tdir/other_dir
19400
19401         # a should be migrated to MDT1, since no other links on MDT0
19402         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19403                 error "#1 migrate dir fails"
19404         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
19405         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
19406         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19407         [ $mdt_index == 1 ] || error "a is not on MDT1"
19408
19409         # a should stay on MDT1, because it is a mulitple link file
19410         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
19411                 error "#2 migrate dir fails"
19412         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19413         [ $mdt_index == 1 ] || error "a is not on MDT1"
19414
19415         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19416                 error "#3 migrate dir fails"
19417
19418         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
19419         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
19420         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
19421
19422         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
19423         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
19424
19425         # a should be migrated to MDT0, since no other links on MDT1
19426         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
19427                 error "#4 migrate dir fails"
19428         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19429         [ $mdt_index == 0 ] || error "a is not on MDT0"
19430
19431         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19432 }
19433 run_test 230f "migrate mulitple remote link files"
19434
19435 test_230g() {
19436         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19437         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19438         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19439                 skip "Need MDS version at least 2.11.52"
19440
19441         mkdir -p $DIR/$tdir/migrate_dir
19442
19443         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
19444                 error "migrating dir to non-exist MDT succeeds"
19445         true
19446 }
19447 run_test 230g "migrate dir to non-exist MDT"
19448
19449 test_230h() {
19450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19451         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19452         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19453                 skip "Need MDS version at least 2.11.52"
19454
19455         local mdt_index
19456
19457         mkdir -p $DIR/$tdir/migrate_dir
19458
19459         $LFS migrate -m1 $DIR &&
19460                 error "migrating mountpoint1 should fail"
19461
19462         $LFS migrate -m1 $DIR/$tdir/.. &&
19463                 error "migrating mountpoint2 should fail"
19464
19465         # same as mv
19466         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
19467                 error "migrating $tdir/migrate_dir/.. should fail"
19468
19469         true
19470 }
19471 run_test 230h "migrate .. and root"
19472
19473 test_230i() {
19474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19475         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19476         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19477                 skip "Need MDS version at least 2.11.52"
19478
19479         mkdir -p $DIR/$tdir/migrate_dir
19480
19481         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
19482                 error "migration fails with a tailing slash"
19483
19484         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
19485                 error "migration fails with two tailing slashes"
19486 }
19487 run_test 230i "lfs migrate -m tolerates trailing slashes"
19488
19489 test_230j() {
19490         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19491         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
19492                 skip "Need MDS version at least 2.11.52"
19493
19494         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
19495         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
19496                 error "create $tfile failed"
19497         cat /etc/passwd > $DIR/$tdir/$tfile
19498
19499         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
19500
19501         cmp /etc/passwd $DIR/$tdir/$tfile ||
19502                 error "DoM file mismatch after migration"
19503 }
19504 run_test 230j "DoM file data not changed after dir migration"
19505
19506 test_230k() {
19507         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
19508         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19509                 skip "Need MDS version at least 2.11.56"
19510
19511         local total=20
19512         local files_on_starting_mdt=0
19513
19514         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
19515         $LFS getdirstripe $DIR/$tdir
19516         for i in $(seq $total); do
19517                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
19518                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
19519                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19520         done
19521
19522         echo "$files_on_starting_mdt files on MDT0"
19523
19524         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
19525         $LFS getdirstripe $DIR/$tdir
19526
19527         files_on_starting_mdt=0
19528         for i in $(seq $total); do
19529                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
19530                         error "file $tfile.$i mismatch after migration"
19531                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
19532                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19533         done
19534
19535         echo "$files_on_starting_mdt files on MDT1 after migration"
19536         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
19537
19538         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
19539         $LFS getdirstripe $DIR/$tdir
19540
19541         files_on_starting_mdt=0
19542         for i in $(seq $total); do
19543                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
19544                         error "file $tfile.$i mismatch after 2nd migration"
19545                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
19546                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19547         done
19548
19549         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
19550         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
19551
19552         true
19553 }
19554 run_test 230k "file data not changed after dir migration"
19555
19556 test_230l() {
19557         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19558         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19559                 skip "Need MDS version at least 2.11.56"
19560
19561         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
19562         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
19563                 error "create files under remote dir failed $i"
19564         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
19565 }
19566 run_test 230l "readdir between MDTs won't crash"
19567
19568 test_230m() {
19569         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19570         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19571                 skip "Need MDS version at least 2.11.56"
19572
19573         local MDTIDX=1
19574         local mig_dir=$DIR/$tdir/migrate_dir
19575         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19576         local shortstr="b"
19577         local val
19578
19579         echo "Creating files and dirs with xattrs"
19580         test_mkdir $DIR/$tdir
19581         test_mkdir -i0 -c1 $mig_dir
19582         mkdir $mig_dir/dir
19583         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
19584                 error "cannot set xattr attr1 on dir"
19585         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
19586                 error "cannot set xattr attr2 on dir"
19587         touch $mig_dir/dir/f0
19588         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
19589                 error "cannot set xattr attr1 on file"
19590         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
19591                 error "cannot set xattr attr2 on file"
19592         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
19593         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
19594         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
19595         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
19596         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
19597         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
19598         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
19599         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
19600         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
19601
19602         echo "Migrating to MDT1"
19603         $LFS migrate -m $MDTIDX $mig_dir ||
19604                 error "fails on migrating dir to MDT1"
19605
19606         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
19607         echo "Checking xattrs"
19608         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
19609         [ "$val" = $longstr ] ||
19610                 error "expecting xattr1 $longstr on dir, found $val"
19611         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
19612         [ "$val" = $shortstr ] ||
19613                 error "expecting xattr2 $shortstr on dir, found $val"
19614         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
19615         [ "$val" = $longstr ] ||
19616                 error "expecting xattr1 $longstr on file, found $val"
19617         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
19618         [ "$val" = $shortstr ] ||
19619                 error "expecting xattr2 $shortstr on file, found $val"
19620 }
19621 run_test 230m "xattrs not changed after dir migration"
19622
19623 test_230n() {
19624         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19625         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
19626                 skip "Need MDS version at least 2.13.53"
19627
19628         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
19629         cat /etc/hosts > $DIR/$tdir/$tfile
19630         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
19631         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
19632
19633         cmp /etc/hosts $DIR/$tdir/$tfile ||
19634                 error "File data mismatch after migration"
19635 }
19636 run_test 230n "Dir migration with mirrored file"
19637
19638 test_230o() {
19639         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
19640         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
19641                 skip "Need MDS version at least 2.13.52"
19642
19643         local mdts=$(comma_list $(mdts_nodes))
19644         local timeout=100
19645         local restripe_status
19646         local delta
19647         local i
19648
19649         [[ $mds1_FSTYPE == zfs ]] && timeout=300
19650
19651         # in case "crush" hash type is not set
19652         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
19653
19654         restripe_status=$(do_facet mds1 $LCTL get_param -n \
19655                            mdt.*MDT0000.enable_dir_restripe)
19656         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
19657         stack_trap "do_nodes $mdts $LCTL set_param \
19658                     mdt.*.enable_dir_restripe=$restripe_status"
19659
19660         mkdir $DIR/$tdir
19661         createmany -m $DIR/$tdir/f 100 ||
19662                 error "create files under remote dir failed $i"
19663         createmany -d $DIR/$tdir/d 100 ||
19664                 error "create dirs under remote dir failed $i"
19665
19666         for i in $(seq 2 $MDSCOUNT); do
19667                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
19668                 $LFS setdirstripe -c $i $DIR/$tdir ||
19669                         error "split -c $i $tdir failed"
19670                 wait_update $HOSTNAME \
19671                         "$LFS getdirstripe -H $DIR/$tdir" "crush" $timeout ||
19672                         error "dir split not finished"
19673                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
19674                         awk '/migrate/ {sum += $2} END { print sum }')
19675                 echo "$delta migrated when dir split $((i - 1)) to $i stripes"
19676                 # delta is around total_files/stripe_count
19677                 (( $delta < 200 / (i - 1) + 4 )) ||
19678                         error "$delta files migrated >= $((200 / (i - 1) + 4))"
19679         done
19680 }
19681 run_test 230o "dir split"
19682
19683 test_230p() {
19684         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
19685         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
19686                 skip "Need MDS version at least 2.13.52"
19687
19688         local mdts=$(comma_list $(mdts_nodes))
19689         local timeout=100
19690         local restripe_status
19691         local delta
19692         local c
19693
19694         [[ $mds1_FSTYPE == zfs ]] && timeout=300
19695
19696         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
19697
19698         restripe_status=$(do_facet mds1 $LCTL get_param -n \
19699                            mdt.*MDT0000.enable_dir_restripe)
19700         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
19701         stack_trap "do_nodes $mdts $LCTL set_param \
19702                     mdt.*.enable_dir_restripe=$restripe_status"
19703
19704         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
19705         createmany -m $DIR/$tdir/f 100 ||
19706                 error "create files under remote dir failed"
19707         createmany -d $DIR/$tdir/d 100 ||
19708                 error "create dirs under remote dir failed"
19709
19710         for c in $(seq $((MDSCOUNT - 1)) -1 1); do
19711                 local mdt_hash="crush"
19712
19713                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
19714                 $LFS setdirstripe -c $c $DIR/$tdir ||
19715                         error "split -c $c $tdir failed"
19716                 if (( MDS1_VERSION >= $(version_code 2.14.51) )); then
19717                         mdt_hash="$mdt_hash,fixed"
19718                 elif [ $c -eq 1 ]; then
19719                         mdt_hash="none"
19720                 fi
19721                 wait_update $HOSTNAME \
19722                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash $timeout ||
19723                         error "dir merge not finished"
19724                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
19725                         awk '/migrate/ {sum += $2} END { print sum }')
19726                 echo "$delta migrated when dir merge $((c + 1)) to $c stripes"
19727                 # delta is around total_files/stripe_count
19728                 (( delta < 200 / c + 4 )) ||
19729                         error "$delta files migrated >= $((200 / c + 4))"
19730         done
19731 }
19732 run_test 230p "dir merge"
19733
19734 test_230q() {
19735         (( MDSCOUNT > 1)) || skip "needs >= 2 MDTs"
19736         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
19737                 skip "Need MDS version at least 2.13.52"
19738
19739         local mdts=$(comma_list $(mdts_nodes))
19740         local saved_threshold=$(do_facet mds1 \
19741                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
19742         local saved_delta=$(do_facet mds1 \
19743                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
19744         local threshold=100
19745         local delta=2
19746         local total=0
19747         local stripe_count=0
19748         local stripe_index
19749         local nr_files
19750         local create
19751
19752         # test with fewer files on ZFS
19753         [ "$mds1_FSTYPE" == "zfs" ] && threshold=40
19754
19755         stack_trap "do_nodes $mdts $LCTL set_param \
19756                     mdt.*.dir_split_count=$saved_threshold"
19757         stack_trap "do_nodes $mdts $LCTL set_param \
19758                     mdt.*.dir_split_delta=$saved_delta"
19759         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
19760         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
19761         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
19762         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
19763         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
19764         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
19765
19766         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
19767         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
19768
19769         create=$((threshold * 3 / 2))
19770         while [ $stripe_count -lt $MDSCOUNT ]; do
19771                 createmany -m $DIR/$tdir/f $total $create ||
19772                         error "create sub files failed"
19773                 stat $DIR/$tdir > /dev/null
19774                 total=$((total + create))
19775                 stripe_count=$((stripe_count + delta))
19776                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
19777
19778                 wait_update $HOSTNAME \
19779                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
19780                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
19781
19782                 wait_update $HOSTNAME \
19783                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
19784                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
19785
19786                 nr_files=$($LFS find -m 1 $DIR/$tdir | grep -c -w $stripe_index)
19787                 echo "$nr_files/$total files on MDT$stripe_index after split"
19788                 # allow 10% margin of imbalance with crush hash
19789                 (( $nr_files <= $total / $stripe_count + $create / 10)) ||
19790                         error "$nr_files files on MDT$stripe_index after split"
19791
19792                 nr_files=$($LFS find -type f $DIR/$tdir | wc -l)
19793                 [ $nr_files -eq $total ] ||
19794                         error "total sub files $nr_files != $total"
19795         done
19796
19797         (( MDS1_VERSION >= $(version_code 2.14.51) )) || return 0
19798
19799         echo "fixed layout directory won't auto split"
19800         $LFS migrate -m 0 $DIR/$tdir || error "migrate $tdir failed"
19801         wait_update $HOSTNAME "$LFS getdirstripe -H $DIR/$tdir" "crush,fixed" \
19802                 10 || error "stripe hash $($LFS getdirstripe -H $DIR/$tdir)"
19803         wait_update $HOSTNAME "$LFS getdirstripe -c $DIR/$tdir" 1 10 ||
19804                 error "stripe count $($LFS getdirstripe -c $DIR/$tdir)"
19805 }
19806 run_test 230q "dir auto split"
19807
19808 test_230r() {
19809         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
19810         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
19811         [[ $MDS1_VERSION -ge $(version_code 2.13.54) ]] ||
19812                 skip "Need MDS version at least 2.13.54"
19813
19814         # maximum amount of local locks:
19815         # parent striped dir - 2 locks
19816         # new stripe in parent to migrate to - 1 lock
19817         # source and target - 2 locks
19818         # Total 5 locks for regular file
19819         mkdir -p $DIR/$tdir
19820         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
19821         touch $DIR/$tdir/dir1/eee
19822
19823         # create 4 hardlink for 4 more locks
19824         # Total: 9 locks > RS_MAX_LOCKS (8)
19825         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
19826         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
19827         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
19828         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
19829         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
19830         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
19831         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
19832         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
19833
19834         cancel_lru_locks mdc
19835
19836         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
19837                 error "migrate dir fails"
19838
19839         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19840 }
19841 run_test 230r "migrate with too many local locks"
19842
19843 test_230s() {
19844         [ $MDS1_VERSION -ge $(version_code 2.13.57) ] ||
19845                 skip "Need MDS version at least 2.13.57"
19846
19847         local mdts=$(comma_list $(mdts_nodes))
19848         local restripe_status=$(do_facet mds1 $LCTL get_param -n \
19849                                 mdt.*MDT0000.enable_dir_restripe)
19850
19851         stack_trap "do_nodes $mdts $LCTL set_param \
19852                     mdt.*.enable_dir_restripe=$restripe_status"
19853
19854         local st
19855         for st in 0 1; do
19856                 do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=$st"
19857                 test_mkdir $DIR/$tdir
19858                 $LFS mkdir $DIR/$tdir |& grep "File exists" ||
19859                         error "$LFS mkdir doesn't return -EEXIST if target exists"
19860                 rmdir $DIR/$tdir
19861         done
19862 }
19863 run_test 230s "lfs mkdir should return -EEXIST if target exists"
19864
19865 test_230t()
19866 {
19867         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
19868         [[ $MDS1_VERSION -ge $(version_code 2.14.50) ]] ||
19869                 skip "Need MDS version at least 2.14.50"
19870
19871         test_mkdir $DIR/$tdir || error "mkdir $tdir failed"
19872         test_mkdir $DIR/$tdir/subdir || error "mkdir subdir failed"
19873         $LFS project -p 1 -s $DIR/$tdir ||
19874                 error "set $tdir project id failed"
19875         $LFS project -p 2 -s $DIR/$tdir/subdir ||
19876                 error "set subdir project id failed"
19877         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir || error "migrate failed"
19878 }
19879 run_test 230t "migrate directory with project ID set"
19880
19881 test_231a()
19882 {
19883         # For simplicity this test assumes that max_pages_per_rpc
19884         # is the same across all OSCs
19885         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
19886         local bulk_size=$((max_pages * PAGE_SIZE))
19887         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
19888                                        head -n 1)
19889
19890         mkdir -p $DIR/$tdir
19891         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
19892                 error "failed to set stripe with -S ${brw_size}M option"
19893
19894         # clear the OSC stats
19895         $LCTL set_param osc.*.stats=0 &>/dev/null
19896         stop_writeback
19897
19898         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
19899         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
19900                 oflag=direct &>/dev/null || error "dd failed"
19901
19902         sync; sleep 1; sync # just to be safe
19903         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
19904         if [ x$nrpcs != "x1" ]; then
19905                 $LCTL get_param osc.*.stats
19906                 error "found $nrpcs ost_write RPCs, not 1 as expected"
19907         fi
19908
19909         start_writeback
19910         # Drop the OSC cache, otherwise we will read from it
19911         cancel_lru_locks osc
19912
19913         # clear the OSC stats
19914         $LCTL set_param osc.*.stats=0 &>/dev/null
19915
19916         # Client reads $bulk_size.
19917         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
19918                 iflag=direct &>/dev/null || error "dd failed"
19919
19920         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
19921         if [ x$nrpcs != "x1" ]; then
19922                 $LCTL get_param osc.*.stats
19923                 error "found $nrpcs ost_read RPCs, not 1 as expected"
19924         fi
19925 }
19926 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
19927
19928 test_231b() {
19929         mkdir -p $DIR/$tdir
19930         local i
19931         for i in {0..1023}; do
19932                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
19933                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
19934                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
19935         done
19936         sync
19937 }
19938 run_test 231b "must not assert on fully utilized OST request buffer"
19939
19940 test_232a() {
19941         mkdir -p $DIR/$tdir
19942         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
19943
19944         #define OBD_FAIL_LDLM_OST_LVB            0x31c
19945         do_facet ost1 $LCTL set_param fail_loc=0x31c
19946
19947         # ignore dd failure
19948         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
19949
19950         do_facet ost1 $LCTL set_param fail_loc=0
19951         umount_client $MOUNT || error "umount failed"
19952         mount_client $MOUNT || error "mount failed"
19953         stop ost1 || error "cannot stop ost1"
19954         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
19955 }
19956 run_test 232a "failed lock should not block umount"
19957
19958 test_232b() {
19959         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
19960                 skip "Need MDS version at least 2.10.58"
19961
19962         mkdir -p $DIR/$tdir
19963         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
19964         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
19965         sync
19966         cancel_lru_locks osc
19967
19968         #define OBD_FAIL_LDLM_OST_LVB            0x31c
19969         do_facet ost1 $LCTL set_param fail_loc=0x31c
19970
19971         # ignore failure
19972         $LFS data_version $DIR/$tdir/$tfile || true
19973
19974         do_facet ost1 $LCTL set_param fail_loc=0
19975         umount_client $MOUNT || error "umount failed"
19976         mount_client $MOUNT || error "mount failed"
19977         stop ost1 || error "cannot stop ost1"
19978         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
19979 }
19980 run_test 232b "failed data version lock should not block umount"
19981
19982 test_233a() {
19983         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
19984                 skip "Need MDS version at least 2.3.64"
19985         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
19986
19987         local fid=$($LFS path2fid $MOUNT)
19988
19989         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
19990                 error "cannot access $MOUNT using its FID '$fid'"
19991 }
19992 run_test 233a "checking that OBF of the FS root succeeds"
19993
19994 test_233b() {
19995         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
19996                 skip "Need MDS version at least 2.5.90"
19997         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
19998
19999         local fid=$($LFS path2fid $MOUNT/.lustre)
20000
20001         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20002                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
20003
20004         fid=$($LFS path2fid $MOUNT/.lustre/fid)
20005         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20006                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
20007 }
20008 run_test 233b "checking that OBF of the FS .lustre succeeds"
20009
20010 test_234() {
20011         local p="$TMP/sanityN-$TESTNAME.parameters"
20012         save_lustre_params client "llite.*.xattr_cache" > $p
20013         lctl set_param llite.*.xattr_cache 1 ||
20014                 skip_env "xattr cache is not supported"
20015
20016         mkdir -p $DIR/$tdir || error "mkdir failed"
20017         touch $DIR/$tdir/$tfile || error "touch failed"
20018         # OBD_FAIL_LLITE_XATTR_ENOMEM
20019         $LCTL set_param fail_loc=0x1405
20020         getfattr -n user.attr $DIR/$tdir/$tfile &&
20021                 error "getfattr should have failed with ENOMEM"
20022         $LCTL set_param fail_loc=0x0
20023         rm -rf $DIR/$tdir
20024
20025         restore_lustre_params < $p
20026         rm -f $p
20027 }
20028 run_test 234 "xattr cache should not crash on ENOMEM"
20029
20030 test_235() {
20031         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
20032                 skip "Need MDS version at least 2.4.52"
20033
20034         flock_deadlock $DIR/$tfile
20035         local RC=$?
20036         case $RC in
20037                 0)
20038                 ;;
20039                 124) error "process hangs on a deadlock"
20040                 ;;
20041                 *) error "error executing flock_deadlock $DIR/$tfile"
20042                 ;;
20043         esac
20044 }
20045 run_test 235 "LU-1715: flock deadlock detection does not work properly"
20046
20047 #LU-2935
20048 test_236() {
20049         check_swap_layouts_support
20050
20051         local ref1=/etc/passwd
20052         local ref2=/etc/group
20053         local file1=$DIR/$tdir/f1
20054         local file2=$DIR/$tdir/f2
20055
20056         test_mkdir -c1 $DIR/$tdir
20057         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
20058         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
20059         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
20060         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
20061         local fd=$(free_fd)
20062         local cmd="exec $fd<>$file2"
20063         eval $cmd
20064         rm $file2
20065         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
20066                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
20067         cmd="exec $fd>&-"
20068         eval $cmd
20069         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
20070
20071         #cleanup
20072         rm -rf $DIR/$tdir
20073 }
20074 run_test 236 "Layout swap on open unlinked file"
20075
20076 # LU-4659 linkea consistency
20077 test_238() {
20078         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
20079                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
20080                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
20081                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
20082
20083         touch $DIR/$tfile
20084         ln $DIR/$tfile $DIR/$tfile.lnk
20085         touch $DIR/$tfile.new
20086         mv $DIR/$tfile.new $DIR/$tfile
20087         local fid1=$($LFS path2fid $DIR/$tfile)
20088         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
20089         local path1=$($LFS fid2path $FSNAME "$fid1")
20090         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
20091         local path2=$($LFS fid2path $FSNAME "$fid2")
20092         [ $tfile.lnk == $path2 ] ||
20093                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
20094         rm -f $DIR/$tfile*
20095 }
20096 run_test 238 "Verify linkea consistency"
20097
20098 test_239A() { # was test_239
20099         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
20100                 skip "Need MDS version at least 2.5.60"
20101
20102         local list=$(comma_list $(mdts_nodes))
20103
20104         mkdir -p $DIR/$tdir
20105         createmany -o $DIR/$tdir/f- 5000
20106         unlinkmany $DIR/$tdir/f- 5000
20107         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
20108                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
20109         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
20110                         osp.*MDT*.sync_in_flight" | calc_sum)
20111         [ "$changes" -eq 0 ] || error "$changes not synced"
20112 }
20113 run_test 239A "osp_sync test"
20114
20115 test_239a() { #LU-5297
20116         remote_mds_nodsh && skip "remote MDS with nodsh"
20117
20118         touch $DIR/$tfile
20119         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
20120         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
20121         chgrp $RUNAS_GID $DIR/$tfile
20122         wait_delete_completed
20123 }
20124 run_test 239a "process invalid osp sync record correctly"
20125
20126 test_239b() { #LU-5297
20127         remote_mds_nodsh && skip "remote MDS with nodsh"
20128
20129         touch $DIR/$tfile1
20130         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
20131         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
20132         chgrp $RUNAS_GID $DIR/$tfile1
20133         wait_delete_completed
20134         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
20135         touch $DIR/$tfile2
20136         chgrp $RUNAS_GID $DIR/$tfile2
20137         wait_delete_completed
20138 }
20139 run_test 239b "process osp sync record with ENOMEM error correctly"
20140
20141 test_240() {
20142         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20143         remote_mds_nodsh && skip "remote MDS with nodsh"
20144
20145         mkdir -p $DIR/$tdir
20146
20147         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
20148                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
20149         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
20150                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
20151
20152         umount_client $MOUNT || error "umount failed"
20153         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
20154         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
20155         mount_client $MOUNT || error "failed to mount client"
20156
20157         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
20158         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
20159 }
20160 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
20161
20162 test_241_bio() {
20163         local count=$1
20164         local bsize=$2
20165
20166         for LOOP in $(seq $count); do
20167                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
20168                 cancel_lru_locks $OSC || true
20169         done
20170 }
20171
20172 test_241_dio() {
20173         local count=$1
20174         local bsize=$2
20175
20176         for LOOP in $(seq $1); do
20177                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
20178                         2>/dev/null
20179         done
20180 }
20181
20182 test_241a() { # was test_241
20183         local bsize=$PAGE_SIZE
20184
20185         (( bsize < 40960 )) && bsize=40960
20186         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
20187         ls -la $DIR/$tfile
20188         cancel_lru_locks $OSC
20189         test_241_bio 1000 $bsize &
20190         PID=$!
20191         test_241_dio 1000 $bsize
20192         wait $PID
20193 }
20194 run_test 241a "bio vs dio"
20195
20196 test_241b() {
20197         local bsize=$PAGE_SIZE
20198
20199         (( bsize < 40960 )) && bsize=40960
20200         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
20201         ls -la $DIR/$tfile
20202         test_241_dio 1000 $bsize &
20203         PID=$!
20204         test_241_dio 1000 $bsize
20205         wait $PID
20206 }
20207 run_test 241b "dio vs dio"
20208
20209 test_242() {
20210         remote_mds_nodsh && skip "remote MDS with nodsh"
20211
20212         mkdir_on_mdt0 $DIR/$tdir
20213         touch $DIR/$tdir/$tfile
20214
20215         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
20216         do_facet mds1 lctl set_param fail_loc=0x105
20217         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
20218
20219         do_facet mds1 lctl set_param fail_loc=0
20220         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
20221 }
20222 run_test 242 "mdt_readpage failure should not cause directory unreadable"
20223
20224 test_243()
20225 {
20226         test_mkdir $DIR/$tdir
20227         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
20228 }
20229 run_test 243 "various group lock tests"
20230
20231 test_244a()
20232 {
20233         test_mkdir $DIR/$tdir
20234         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
20235         sendfile_grouplock $DIR/$tdir/$tfile || \
20236                 error "sendfile+grouplock failed"
20237         rm -rf $DIR/$tdir
20238 }
20239 run_test 244a "sendfile with group lock tests"
20240
20241 test_244b()
20242 {
20243         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
20244
20245         local threads=50
20246         local size=$((1024*1024))
20247
20248         test_mkdir $DIR/$tdir
20249         for i in $(seq 1 $threads); do
20250                 local file=$DIR/$tdir/file_$((i / 10))
20251                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
20252                 local pids[$i]=$!
20253         done
20254         for i in $(seq 1 $threads); do
20255                 wait ${pids[$i]}
20256         done
20257 }
20258 run_test 244b "multi-threaded write with group lock"
20259
20260 test_245() {
20261         local flagname="multi_mod_rpcs"
20262         local connect_data_name="max_mod_rpcs"
20263         local out
20264
20265         # check if multiple modify RPCs flag is set
20266         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
20267                 grep "connect_flags:")
20268         echo "$out"
20269
20270         echo "$out" | grep -qw $flagname
20271         if [ $? -ne 0 ]; then
20272                 echo "connect flag $flagname is not set"
20273                 return
20274         fi
20275
20276         # check if multiple modify RPCs data is set
20277         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
20278         echo "$out"
20279
20280         echo "$out" | grep -qw $connect_data_name ||
20281                 error "import should have connect data $connect_data_name"
20282 }
20283 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
20284
20285 cleanup_247() {
20286         local submount=$1
20287
20288         trap 0
20289         umount_client $submount
20290         rmdir $submount
20291 }
20292
20293 test_247a() {
20294         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20295                 grep -q subtree ||
20296                 skip_env "Fileset feature is not supported"
20297
20298         local submount=${MOUNT}_$tdir
20299
20300         mkdir $MOUNT/$tdir
20301         mkdir -p $submount || error "mkdir $submount failed"
20302         FILESET="$FILESET/$tdir" mount_client $submount ||
20303                 error "mount $submount failed"
20304         trap "cleanup_247 $submount" EXIT
20305         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
20306         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
20307                 error "read $MOUNT/$tdir/$tfile failed"
20308         cleanup_247 $submount
20309 }
20310 run_test 247a "mount subdir as fileset"
20311
20312 test_247b() {
20313         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20314                 skip_env "Fileset feature is not supported"
20315
20316         local submount=${MOUNT}_$tdir
20317
20318         rm -rf $MOUNT/$tdir
20319         mkdir -p $submount || error "mkdir $submount failed"
20320         SKIP_FILESET=1
20321         FILESET="$FILESET/$tdir" mount_client $submount &&
20322                 error "mount $submount should fail"
20323         rmdir $submount
20324 }
20325 run_test 247b "mount subdir that dose not exist"
20326
20327 test_247c() {
20328         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20329                 skip_env "Fileset feature is not supported"
20330
20331         local submount=${MOUNT}_$tdir
20332
20333         mkdir -p $MOUNT/$tdir/dir1
20334         mkdir -p $submount || error "mkdir $submount failed"
20335         trap "cleanup_247 $submount" EXIT
20336         FILESET="$FILESET/$tdir" mount_client $submount ||
20337                 error "mount $submount failed"
20338         local fid=$($LFS path2fid $MOUNT/)
20339         $LFS fid2path $submount $fid && error "fid2path should fail"
20340         cleanup_247 $submount
20341 }
20342 run_test 247c "running fid2path outside subdirectory root"
20343
20344 test_247d() {
20345         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20346                 skip "Fileset feature is not supported"
20347
20348         local submount=${MOUNT}_$tdir
20349
20350         mkdir -p $MOUNT/$tdir/dir1
20351         mkdir -p $submount || error "mkdir $submount failed"
20352         FILESET="$FILESET/$tdir" mount_client $submount ||
20353                 error "mount $submount failed"
20354         trap "cleanup_247 $submount" EXIT
20355
20356         local td=$submount/dir1
20357         local fid=$($LFS path2fid $td)
20358         [ -z "$fid" ] && error "path2fid unable to get $td FID"
20359
20360         # check that we get the same pathname back
20361         local rootpath
20362         local found
20363         for rootpath in "$submount" "$submount///" "$submount/dir1"; do
20364                 echo "$rootpath $fid"
20365                 found=$($LFS fid2path $rootpath "$fid")
20366                 [ -n "found" ] || error "fid2path should succeed"
20367                 [ "$found" == "$td" ] || error "fid2path $found != $td"
20368         done
20369         # check wrong root path format
20370         rootpath=$submount"_wrong"
20371         found=$($LFS fid2path $rootpath "$fid")
20372         [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)"
20373
20374         cleanup_247 $submount
20375 }
20376 run_test 247d "running fid2path inside subdirectory root"
20377
20378 # LU-8037
20379 test_247e() {
20380         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20381                 grep -q subtree ||
20382                 skip "Fileset feature is not supported"
20383
20384         local submount=${MOUNT}_$tdir
20385
20386         mkdir $MOUNT/$tdir
20387         mkdir -p $submount || error "mkdir $submount failed"
20388         FILESET="$FILESET/.." mount_client $submount &&
20389                 error "mount $submount should fail"
20390         rmdir $submount
20391 }
20392 run_test 247e "mount .. as fileset"
20393
20394 test_247f() {
20395         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20396         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
20397                 skip "Need at least version 2.13.52"
20398         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
20399                 skip "Need at least version 2.14.50"
20400         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20401                 grep -q subtree ||
20402                 skip "Fileset feature is not supported"
20403
20404         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
20405         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
20406                 error "mkdir remote failed"
20407         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote/subdir ||
20408                 error "mkdir remote/subdir failed"
20409         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
20410                 error "mkdir striped failed"
20411         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
20412
20413         local submount=${MOUNT}_$tdir
20414
20415         mkdir -p $submount || error "mkdir $submount failed"
20416         stack_trap "rmdir $submount"
20417
20418         local dir
20419         local stat
20420         local fileset=$FILESET
20421         local mdts=$(comma_list $(mdts_nodes))
20422
20423         stat=$(do_facet mds1 $LCTL get_param -n \
20424                 mdt.*MDT0000.enable_remote_subdir_mount)
20425         stack_trap "do_nodes $mdts $LCTL set_param \
20426                 mdt.*.enable_remote_subdir_mount=$stat"
20427
20428         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
20429         stack_trap "umount_client $submount"
20430         FILESET="$fileset/$tdir/remote" mount_client $submount &&
20431                 error "mount remote dir $dir should fail"
20432
20433         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
20434                 $tdir/striped/. ; do
20435                 FILESET="$fileset/$dir" mount_client $submount ||
20436                         error "mount $dir failed"
20437                 umount_client $submount
20438         done
20439
20440         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
20441         FILESET="$fileset/$tdir/remote" mount_client $submount ||
20442                 error "mount $tdir/remote failed"
20443 }
20444 run_test 247f "mount striped or remote directory as fileset"
20445
20446 test_247g() {
20447         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
20448         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
20449                 skip "Need at least version 2.14.50"
20450
20451         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
20452                 error "mkdir $tdir failed"
20453         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
20454
20455         local submount=${MOUNT}_$tdir
20456
20457         mkdir -p $submount || error "mkdir $submount failed"
20458         stack_trap "rmdir $submount"
20459
20460         FILESET="$fileset/$tdir" mount_client $submount ||
20461                 error "mount $dir failed"
20462         stack_trap "umount $submount"
20463
20464         local mdts=$(comma_list $(mdts_nodes))
20465
20466         local nrpcs
20467
20468         stat $submount > /dev/null
20469         cancel_lru_locks $MDC
20470         stat $submount > /dev/null
20471         stat $submount/$tfile > /dev/null
20472         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
20473         stat $submount/$tfile > /dev/null
20474         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
20475                 awk '/getattr/ {sum += $2} END {print sum}')
20476
20477         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
20478 }
20479 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
20480
20481 test_248a() {
20482         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
20483         [ -z "$fast_read_sav" ] && skip "no fast read support"
20484
20485         # create a large file for fast read verification
20486         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
20487
20488         # make sure the file is created correctly
20489         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
20490                 { rm -f $DIR/$tfile; skip "file creation error"; }
20491
20492         echo "Test 1: verify that fast read is 4 times faster on cache read"
20493
20494         # small read with fast read enabled
20495         $LCTL set_param -n llite.*.fast_read=1
20496         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
20497                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20498                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20499         # small read with fast read disabled
20500         $LCTL set_param -n llite.*.fast_read=0
20501         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
20502                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20503                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20504
20505         # verify that fast read is 4 times faster for cache read
20506         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
20507                 error_not_in_vm "fast read was not 4 times faster: " \
20508                            "$t_fast vs $t_slow"
20509
20510         echo "Test 2: verify the performance between big and small read"
20511         $LCTL set_param -n llite.*.fast_read=1
20512
20513         # 1k non-cache read
20514         cancel_lru_locks osc
20515         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
20516                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20517                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20518
20519         # 1M non-cache read
20520         cancel_lru_locks osc
20521         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
20522                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20523                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20524
20525         # verify that big IO is not 4 times faster than small IO
20526         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
20527                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
20528
20529         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
20530         rm -f $DIR/$tfile
20531 }
20532 run_test 248a "fast read verification"
20533
20534 test_248b() {
20535         # Default short_io_bytes=16384, try both smaller and larger sizes.
20536         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
20537         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
20538         echo "bs=53248 count=113 normal buffered write"
20539         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
20540                 error "dd of initial data file failed"
20541         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
20542
20543         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
20544         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
20545                 error "dd with sync normal writes failed"
20546         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
20547
20548         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
20549         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
20550                 error "dd with sync small writes failed"
20551         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
20552
20553         cancel_lru_locks osc
20554
20555         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
20556         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
20557         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
20558         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
20559                 iflag=direct || error "dd with O_DIRECT small read failed"
20560         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
20561         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
20562                 error "compare $TMP/$tfile.1 failed"
20563
20564         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
20565         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
20566
20567         # just to see what the maximum tunable value is, and test parsing
20568         echo "test invalid parameter 2MB"
20569         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
20570                 error "too-large short_io_bytes allowed"
20571         echo "test maximum parameter 512KB"
20572         # if we can set a larger short_io_bytes, run test regardless of version
20573         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
20574                 # older clients may not allow setting it this large, that's OK
20575                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
20576                         skip "Need at least client version 2.13.50"
20577                 error "medium short_io_bytes failed"
20578         fi
20579         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
20580         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
20581
20582         echo "test large parameter 64KB"
20583         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
20584         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
20585
20586         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
20587         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
20588                 error "dd with sync large writes failed"
20589         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
20590
20591         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
20592         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
20593         num=$((113 * 4096 / PAGE_SIZE))
20594         echo "bs=$size count=$num oflag=direct large write $tfile.3"
20595         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
20596                 error "dd with O_DIRECT large writes failed"
20597         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
20598                 error "compare $DIR/$tfile.3 failed"
20599
20600         cancel_lru_locks osc
20601
20602         echo "bs=$size count=$num iflag=direct large read $tfile.2"
20603         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
20604                 error "dd with O_DIRECT large read failed"
20605         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
20606                 error "compare $TMP/$tfile.2 failed"
20607
20608         echo "bs=$size count=$num iflag=direct large read $tfile.3"
20609         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
20610                 error "dd with O_DIRECT large read failed"
20611         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
20612                 error "compare $TMP/$tfile.3 failed"
20613 }
20614 run_test 248b "test short_io read and write for both small and large sizes"
20615
20616 test_249() { # LU-7890
20617         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
20618                 skip "Need at least version 2.8.54"
20619
20620         rm -f $DIR/$tfile
20621         $LFS setstripe -c 1 $DIR/$tfile
20622         # Offset 2T == 4k * 512M
20623         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
20624                 error "dd to 2T offset failed"
20625 }
20626 run_test 249 "Write above 2T file size"
20627
20628 test_250() {
20629         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
20630          && skip "no 16TB file size limit on ZFS"
20631
20632         $LFS setstripe -c 1 $DIR/$tfile
20633         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
20634         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
20635         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
20636         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
20637                 conv=notrunc,fsync && error "append succeeded"
20638         return 0
20639 }
20640 run_test 250 "Write above 16T limit"
20641
20642 test_251() {
20643         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
20644
20645         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
20646         #Skip once - writing the first stripe will succeed
20647         $LCTL set_param fail_loc=0xa0001407 fail_val=1
20648         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
20649                 error "short write happened"
20650
20651         $LCTL set_param fail_loc=0xa0001407 fail_val=1
20652         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
20653                 error "short read happened"
20654
20655         rm -f $DIR/$tfile
20656 }
20657 run_test 251 "Handling short read and write correctly"
20658
20659 test_252() {
20660         remote_mds_nodsh && skip "remote MDS with nodsh"
20661         remote_ost_nodsh && skip "remote OST with nodsh"
20662         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
20663                 skip_env "ldiskfs only test"
20664         fi
20665
20666         local tgt
20667         local dev
20668         local out
20669         local uuid
20670         local num
20671         local gen
20672
20673         # check lr_reader on OST0000
20674         tgt=ost1
20675         dev=$(facet_device $tgt)
20676         out=$(do_facet $tgt $LR_READER $dev)
20677         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
20678         echo "$out"
20679         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
20680         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
20681                 error "Invalid uuid returned by $LR_READER on target $tgt"
20682         echo -e "uuid returned by $LR_READER is '$uuid'\n"
20683
20684         # check lr_reader -c on MDT0000
20685         tgt=mds1
20686         dev=$(facet_device $tgt)
20687         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
20688                 skip "$LR_READER does not support additional options"
20689         fi
20690         out=$(do_facet $tgt $LR_READER -c $dev)
20691         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
20692         echo "$out"
20693         num=$(echo "$out" | grep -c "mdtlov")
20694         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
20695                 error "Invalid number of mdtlov clients returned by $LR_READER"
20696         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
20697
20698         # check lr_reader -cr on MDT0000
20699         out=$(do_facet $tgt $LR_READER -cr $dev)
20700         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
20701         echo "$out"
20702         echo "$out" | grep -q "^reply_data:$" ||
20703                 error "$LR_READER should have returned 'reply_data' section"
20704         num=$(echo "$out" | grep -c "client_generation")
20705         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
20706 }
20707 run_test 252 "check lr_reader tool"
20708
20709 test_253() {
20710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20711         remote_mds_nodsh && skip "remote MDS with nodsh"
20712         remote_mgs_nodsh && skip "remote MGS with nodsh"
20713
20714         local ostidx=0
20715         local rc=0
20716         local ost_name=$(ostname_from_index $ostidx)
20717
20718         # on the mdt's osc
20719         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
20720         do_facet $SINGLEMDS $LCTL get_param -n \
20721                 osp.$mdtosc_proc1.reserved_mb_high ||
20722                 skip  "remote MDS does not support reserved_mb_high"
20723
20724         rm -rf $DIR/$tdir
20725         wait_mds_ost_sync
20726         wait_delete_completed
20727         mkdir $DIR/$tdir
20728
20729         pool_add $TESTNAME || error "Pool creation failed"
20730         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
20731
20732         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
20733                 error "Setstripe failed"
20734
20735         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
20736
20737         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
20738                     grep "watermarks")
20739         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
20740
20741         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
20742                         osp.$mdtosc_proc1.prealloc_status)
20743         echo "prealloc_status $oa_status"
20744
20745         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
20746                 error "File creation should fail"
20747
20748         #object allocation was stopped, but we still able to append files
20749         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
20750                 oflag=append || error "Append failed"
20751
20752         rm -f $DIR/$tdir/$tfile.0
20753
20754         # For this test, we want to delete the files we created to go out of
20755         # space but leave the watermark, so we remain nearly out of space
20756         ost_watermarks_enospc_delete_files $tfile $ostidx
20757
20758         wait_delete_completed
20759
20760         sleep_maxage
20761
20762         for i in $(seq 10 12); do
20763                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
20764                         2>/dev/null || error "File creation failed after rm"
20765         done
20766
20767         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
20768                         osp.$mdtosc_proc1.prealloc_status)
20769         echo "prealloc_status $oa_status"
20770
20771         if (( oa_status != 0 )); then
20772                 error "Object allocation still disable after rm"
20773         fi
20774 }
20775 run_test 253 "Check object allocation limit"
20776
20777 test_254() {
20778         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20779         remote_mds_nodsh && skip "remote MDS with nodsh"
20780         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
20781                 skip "MDS does not support changelog_size"
20782
20783         local cl_user
20784         local MDT0=$(facet_svc $SINGLEMDS)
20785
20786         changelog_register || error "changelog_register failed"
20787
20788         changelog_clear 0 || error "changelog_clear failed"
20789
20790         local size1=$(do_facet $SINGLEMDS \
20791                       $LCTL get_param -n mdd.$MDT0.changelog_size)
20792         echo "Changelog size $size1"
20793
20794         rm -rf $DIR/$tdir
20795         $LFS mkdir -i 0 $DIR/$tdir
20796         # change something
20797         mkdir -p $DIR/$tdir/pics/2008/zachy
20798         touch $DIR/$tdir/pics/2008/zachy/timestamp
20799         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
20800         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
20801         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
20802         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
20803         rm $DIR/$tdir/pics/desktop.jpg
20804
20805         local size2=$(do_facet $SINGLEMDS \
20806                       $LCTL get_param -n mdd.$MDT0.changelog_size)
20807         echo "Changelog size after work $size2"
20808
20809         (( $size2 > $size1 )) ||
20810                 error "new Changelog size=$size2 less than old size=$size1"
20811 }
20812 run_test 254 "Check changelog size"
20813
20814 ladvise_no_type()
20815 {
20816         local type=$1
20817         local file=$2
20818
20819         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
20820                 awk -F: '{print $2}' | grep $type > /dev/null
20821         if [ $? -ne 0 ]; then
20822                 return 0
20823         fi
20824         return 1
20825 }
20826
20827 ladvise_no_ioctl()
20828 {
20829         local file=$1
20830
20831         lfs ladvise -a willread $file > /dev/null 2>&1
20832         if [ $? -eq 0 ]; then
20833                 return 1
20834         fi
20835
20836         lfs ladvise -a willread $file 2>&1 |
20837                 grep "Inappropriate ioctl for device" > /dev/null
20838         if [ $? -eq 0 ]; then
20839                 return 0
20840         fi
20841         return 1
20842 }
20843
20844 percent() {
20845         bc <<<"scale=2; ($1 - $2) * 100 / $2"
20846 }
20847
20848 # run a random read IO workload
20849 # usage: random_read_iops <filename> <filesize> <iosize>
20850 random_read_iops() {
20851         local file=$1
20852         local fsize=$2
20853         local iosize=${3:-4096}
20854
20855         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
20856                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
20857 }
20858
20859 drop_file_oss_cache() {
20860         local file="$1"
20861         local nodes="$2"
20862
20863         $LFS ladvise -a dontneed $file 2>/dev/null ||
20864                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
20865 }
20866
20867 ladvise_willread_performance()
20868 {
20869         local repeat=10
20870         local average_origin=0
20871         local average_cache=0
20872         local average_ladvise=0
20873
20874         for ((i = 1; i <= $repeat; i++)); do
20875                 echo "Iter $i/$repeat: reading without willread hint"
20876                 cancel_lru_locks osc
20877                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
20878                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
20879                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
20880                 average_origin=$(bc <<<"$average_origin + $speed_origin")
20881
20882                 cancel_lru_locks osc
20883                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
20884                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
20885                 average_cache=$(bc <<<"$average_cache + $speed_cache")
20886
20887                 cancel_lru_locks osc
20888                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
20889                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
20890                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
20891                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
20892                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
20893         done
20894         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
20895         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
20896         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
20897
20898         speedup_cache=$(percent $average_cache $average_origin)
20899         speedup_ladvise=$(percent $average_ladvise $average_origin)
20900
20901         echo "Average uncached read: $average_origin"
20902         echo "Average speedup with OSS cached read: " \
20903                 "$average_cache = +$speedup_cache%"
20904         echo "Average speedup with ladvise willread: " \
20905                 "$average_ladvise = +$speedup_ladvise%"
20906
20907         local lowest_speedup=20
20908         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
20909                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
20910                         "got $average_cache%. Skipping ladvise willread check."
20911                 return 0
20912         fi
20913
20914         # the test won't work on ZFS until it supports 'ladvise dontneed', but
20915         # it is still good to run until then to exercise 'ladvise willread'
20916         ! $LFS ladvise -a dontneed $DIR/$tfile &&
20917                 [ "$ost1_FSTYPE" = "zfs" ] &&
20918                 echo "osd-zfs does not support dontneed or drop_caches" &&
20919                 return 0
20920
20921         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
20922         [[ ${average_ladvise%.*} > $lowest_speedup ]] ||
20923                 error_not_in_vm "Speedup with willread is less than " \
20924                         "$lowest_speedup%, got $average_ladvise%"
20925 }
20926
20927 test_255a() {
20928         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
20929                 skip "lustre < 2.8.54 does not support ladvise "
20930         remote_ost_nodsh && skip "remote OST with nodsh"
20931
20932         stack_trap "rm -f $DIR/$tfile"
20933         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
20934
20935         ladvise_no_type willread $DIR/$tfile &&
20936                 skip "willread ladvise is not supported"
20937
20938         ladvise_no_ioctl $DIR/$tfile &&
20939                 skip "ladvise ioctl is not supported"
20940
20941         local size_mb=100
20942         local size=$((size_mb * 1048576))
20943         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
20944                 error "dd to $DIR/$tfile failed"
20945
20946         lfs ladvise -a willread $DIR/$tfile ||
20947                 error "Ladvise failed with no range argument"
20948
20949         lfs ladvise -a willread -s 0 $DIR/$tfile ||
20950                 error "Ladvise failed with no -l or -e argument"
20951
20952         lfs ladvise -a willread -e 1 $DIR/$tfile ||
20953                 error "Ladvise failed with only -e argument"
20954
20955         lfs ladvise -a willread -l 1 $DIR/$tfile ||
20956                 error "Ladvise failed with only -l argument"
20957
20958         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
20959                 error "End offset should not be smaller than start offset"
20960
20961         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
20962                 error "End offset should not be equal to start offset"
20963
20964         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
20965                 error "Ladvise failed with overflowing -s argument"
20966
20967         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
20968                 error "Ladvise failed with overflowing -e argument"
20969
20970         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
20971                 error "Ladvise failed with overflowing -l argument"
20972
20973         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
20974                 error "Ladvise succeeded with conflicting -l and -e arguments"
20975
20976         echo "Synchronous ladvise should wait"
20977         local delay=4
20978 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
20979         do_nodes $(comma_list $(osts_nodes)) \
20980                 $LCTL set_param fail_val=$delay fail_loc=0x237
20981
20982         local start_ts=$SECONDS
20983         lfs ladvise -a willread $DIR/$tfile ||
20984                 error "Ladvise failed with no range argument"
20985         local end_ts=$SECONDS
20986         local inteval_ts=$((end_ts - start_ts))
20987
20988         if [ $inteval_ts -lt $(($delay - 1)) ]; then
20989                 error "Synchronous advice didn't wait reply"
20990         fi
20991
20992         echo "Asynchronous ladvise shouldn't wait"
20993         local start_ts=$SECONDS
20994         lfs ladvise -a willread -b $DIR/$tfile ||
20995                 error "Ladvise failed with no range argument"
20996         local end_ts=$SECONDS
20997         local inteval_ts=$((end_ts - start_ts))
20998
20999         if [ $inteval_ts -gt $(($delay / 2)) ]; then
21000                 error "Asynchronous advice blocked"
21001         fi
21002
21003         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
21004         ladvise_willread_performance
21005 }
21006 run_test 255a "check 'lfs ladvise -a willread'"
21007
21008 facet_meminfo() {
21009         local facet=$1
21010         local info=$2
21011
21012         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
21013 }
21014
21015 test_255b() {
21016         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
21017                 skip "lustre < 2.8.54 does not support ladvise "
21018         remote_ost_nodsh && skip "remote OST with nodsh"
21019
21020         stack_trap "rm -f $DIR/$tfile"
21021         lfs setstripe -c 1 -i 0 $DIR/$tfile
21022
21023         ladvise_no_type dontneed $DIR/$tfile &&
21024                 skip "dontneed ladvise is not supported"
21025
21026         ladvise_no_ioctl $DIR/$tfile &&
21027                 skip "ladvise ioctl is not supported"
21028
21029         ! $LFS ladvise -a dontneed $DIR/$tfile &&
21030                 [ "$ost1_FSTYPE" = "zfs" ] &&
21031                 skip "zfs-osd does not support 'ladvise dontneed'"
21032
21033         local size_mb=100
21034         local size=$((size_mb * 1048576))
21035         # In order to prevent disturbance of other processes, only check 3/4
21036         # of the memory usage
21037         local kibibytes=$((size_mb * 1024 * 3 / 4))
21038
21039         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
21040                 error "dd to $DIR/$tfile failed"
21041
21042         #force write to complete before dropping OST cache & checking memory
21043         sync
21044
21045         local total=$(facet_meminfo ost1 MemTotal)
21046         echo "Total memory: $total KiB"
21047
21048         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
21049         local before_read=$(facet_meminfo ost1 Cached)
21050         echo "Cache used before read: $before_read KiB"
21051
21052         lfs ladvise -a willread $DIR/$tfile ||
21053                 error "Ladvise willread failed"
21054         local after_read=$(facet_meminfo ost1 Cached)
21055         echo "Cache used after read: $after_read KiB"
21056
21057         lfs ladvise -a dontneed $DIR/$tfile ||
21058                 error "Ladvise dontneed again failed"
21059         local no_read=$(facet_meminfo ost1 Cached)
21060         echo "Cache used after dontneed ladvise: $no_read KiB"
21061
21062         if [ $total -lt $((before_read + kibibytes)) ]; then
21063                 echo "Memory is too small, abort checking"
21064                 return 0
21065         fi
21066
21067         if [ $((before_read + kibibytes)) -gt $after_read ]; then
21068                 error "Ladvise willread should use more memory" \
21069                         "than $kibibytes KiB"
21070         fi
21071
21072         if [ $((no_read + kibibytes)) -gt $after_read ]; then
21073                 error "Ladvise dontneed should release more memory" \
21074                         "than $kibibytes KiB"
21075         fi
21076 }
21077 run_test 255b "check 'lfs ladvise -a dontneed'"
21078
21079 test_255c() {
21080         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
21081                 skip "lustre < 2.10.50 does not support lockahead"
21082
21083         local ost1_imp=$(get_osc_import_name client ost1)
21084         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
21085                          cut -d'.' -f2)
21086         local count
21087         local new_count
21088         local difference
21089         local i
21090         local rc
21091
21092         test_mkdir -p $DIR/$tdir
21093         $LFS setstripe -i 0 -c 1 $DIR/$tdir
21094
21095         #test 10 returns only success/failure
21096         i=10
21097         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21098         rc=$?
21099         if [ $rc -eq 255 ]; then
21100                 error "Ladvise test${i} failed, ${rc}"
21101         fi
21102
21103         #test 11 counts lock enqueue requests, all others count new locks
21104         i=11
21105         count=$(do_facet ost1 \
21106                 $LCTL get_param -n ost.OSS.ost.stats)
21107         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
21108
21109         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21110         rc=$?
21111         if [ $rc -eq 255 ]; then
21112                 error "Ladvise test${i} failed, ${rc}"
21113         fi
21114
21115         new_count=$(do_facet ost1 \
21116                 $LCTL get_param -n ost.OSS.ost.stats)
21117         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
21118                    awk '{ print $2 }')
21119
21120         difference="$((new_count - count))"
21121         if [ $difference -ne $rc ]; then
21122                 error "Ladvise test${i}, bad enqueue count, returned " \
21123                       "${rc}, actual ${difference}"
21124         fi
21125
21126         for i in $(seq 12 21); do
21127                 # If we do not do this, we run the risk of having too many
21128                 # locks and starting lock cancellation while we are checking
21129                 # lock counts.
21130                 cancel_lru_locks osc
21131
21132                 count=$($LCTL get_param -n \
21133                        ldlm.namespaces.$imp_name.lock_unused_count)
21134
21135                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
21136                 rc=$?
21137                 if [ $rc -eq 255 ]; then
21138                         error "Ladvise test ${i} failed, ${rc}"
21139                 fi
21140
21141                 new_count=$($LCTL get_param -n \
21142                        ldlm.namespaces.$imp_name.lock_unused_count)
21143                 difference="$((new_count - count))"
21144
21145                 # Test 15 output is divided by 100 to map down to valid return
21146                 if [ $i -eq 15 ]; then
21147                         rc="$((rc * 100))"
21148                 fi
21149
21150                 if [ $difference -ne $rc ]; then
21151                         error "Ladvise test ${i}, bad lock count, returned " \
21152                               "${rc}, actual ${difference}"
21153                 fi
21154         done
21155
21156         #test 22 returns only success/failure
21157         i=22
21158         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21159         rc=$?
21160         if [ $rc -eq 255 ]; then
21161                 error "Ladvise test${i} failed, ${rc}"
21162         fi
21163 }
21164 run_test 255c "suite of ladvise lockahead tests"
21165
21166 test_256() {
21167         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21168         remote_mds_nodsh && skip "remote MDS with nodsh"
21169         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
21170         changelog_users $SINGLEMDS | grep "^cl" &&
21171                 skip "active changelog user"
21172
21173         local cl_user
21174         local cat_sl
21175         local mdt_dev
21176
21177         mdt_dev=$(mdsdevname 1)
21178         echo $mdt_dev
21179
21180         changelog_register || error "changelog_register failed"
21181
21182         rm -rf $DIR/$tdir
21183         mkdir_on_mdt0 $DIR/$tdir
21184
21185         changelog_clear 0 || error "changelog_clear failed"
21186
21187         # change something
21188         touch $DIR/$tdir/{1..10}
21189
21190         # stop the MDT
21191         stop $SINGLEMDS || error "Fail to stop MDT"
21192
21193         # remount the MDT
21194
21195         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
21196
21197         #after mount new plainllog is used
21198         touch $DIR/$tdir/{11..19}
21199         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
21200         stack_trap "rm -f $tmpfile"
21201         cat_sl=$(do_facet $SINGLEMDS "sync; \
21202                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
21203                  llog_reader $tmpfile | grep -c type=1064553b")
21204         do_facet $SINGLEMDS llog_reader $tmpfile
21205
21206         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
21207
21208         changelog_clear 0 || error "changelog_clear failed"
21209
21210         cat_sl=$(do_facet $SINGLEMDS "sync; \
21211                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
21212                  llog_reader $tmpfile | grep -c type=1064553b")
21213
21214         if (( cat_sl == 2 )); then
21215                 error "Empty plain llog was not deleted from changelog catalog"
21216         elif (( cat_sl != 1 )); then
21217                 error "Active plain llog shouldn't be deleted from catalog"
21218         fi
21219 }
21220 run_test 256 "Check llog delete for empty and not full state"
21221
21222 test_257() {
21223         remote_mds_nodsh && skip "remote MDS with nodsh"
21224         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21225                 skip "Need MDS version at least 2.8.55"
21226
21227         test_mkdir $DIR/$tdir
21228
21229         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
21230                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
21231         stat $DIR/$tdir
21232
21233 #define OBD_FAIL_MDS_XATTR_REP                  0x161
21234         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
21235         local facet=mds$((mdtidx + 1))
21236         set_nodes_failloc $(facet_active_host $facet) 0x80000161
21237         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
21238
21239         stop $facet || error "stop MDS failed"
21240         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
21241                 error "start MDS fail"
21242         wait_recovery_complete $facet
21243 }
21244 run_test 257 "xattr locks are not lost"
21245
21246 # Verify we take the i_mutex when security requires it
21247 test_258a() {
21248 #define OBD_FAIL_IMUTEX_SEC 0x141c
21249         $LCTL set_param fail_loc=0x141c
21250         touch $DIR/$tfile
21251         chmod u+s $DIR/$tfile
21252         chmod a+rwx $DIR/$tfile
21253         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
21254         RC=$?
21255         if [ $RC -ne 0 ]; then
21256                 error "error, failed to take i_mutex, rc=$?"
21257         fi
21258         rm -f $DIR/$tfile
21259 }
21260 run_test 258a "verify i_mutex security behavior when suid attributes is set"
21261
21262 # Verify we do NOT take the i_mutex in the normal case
21263 test_258b() {
21264 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
21265         $LCTL set_param fail_loc=0x141d
21266         touch $DIR/$tfile
21267         chmod a+rwx $DIR
21268         chmod a+rw $DIR/$tfile
21269         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
21270         RC=$?
21271         if [ $RC -ne 0 ]; then
21272                 error "error, took i_mutex unnecessarily, rc=$?"
21273         fi
21274         rm -f $DIR/$tfile
21275
21276 }
21277 run_test 258b "verify i_mutex security behavior"
21278
21279 test_259() {
21280         local file=$DIR/$tfile
21281         local before
21282         local after
21283
21284         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
21285
21286         stack_trap "rm -f $file" EXIT
21287
21288         wait_delete_completed
21289         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21290         echo "before: $before"
21291
21292         $LFS setstripe -i 0 -c 1 $file
21293         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
21294         sync_all_data
21295         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21296         echo "after write: $after"
21297
21298 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
21299         do_facet ost1 $LCTL set_param fail_loc=0x2301
21300         $TRUNCATE $file 0
21301         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21302         echo "after truncate: $after"
21303
21304         stop ost1
21305         do_facet ost1 $LCTL set_param fail_loc=0
21306         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21307         sleep 2
21308         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21309         echo "after restart: $after"
21310         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
21311                 error "missing truncate?"
21312
21313         return 0
21314 }
21315 run_test 259 "crash at delayed truncate"
21316
21317 test_260() {
21318 #define OBD_FAIL_MDC_CLOSE               0x806
21319         $LCTL set_param fail_loc=0x80000806
21320         touch $DIR/$tfile
21321
21322 }
21323 run_test 260 "Check mdc_close fail"
21324
21325 ### Data-on-MDT sanity tests ###
21326 test_270a() {
21327         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21328                 skip "Need MDS version at least 2.10.55 for DoM"
21329
21330         # create DoM file
21331         local dom=$DIR/$tdir/dom_file
21332         local tmp=$DIR/$tdir/tmp_file
21333
21334         mkdir_on_mdt0 $DIR/$tdir
21335
21336         # basic checks for DoM component creation
21337         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
21338                 error "Can set MDT layout to non-first entry"
21339
21340         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
21341                 error "Can define multiple entries as MDT layout"
21342
21343         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
21344
21345         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
21346         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
21347         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
21348
21349         local mdtidx=$($LFS getstripe -m $dom)
21350         local mdtname=MDT$(printf %04x $mdtidx)
21351         local facet=mds$((mdtidx + 1))
21352         local space_check=1
21353
21354         # Skip free space checks with ZFS
21355         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
21356
21357         # write
21358         sync
21359         local size_tmp=$((65536 * 3))
21360         local mdtfree1=$(do_facet $facet \
21361                          lctl get_param -n osd*.*$mdtname.kbytesfree)
21362
21363         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
21364         # check also direct IO along write
21365         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
21366         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
21367         sync
21368         cmp $tmp $dom || error "file data is different"
21369         [ $(stat -c%s $dom) == $size_tmp ] ||
21370                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
21371         if [ $space_check == 1 ]; then
21372                 local mdtfree2=$(do_facet $facet \
21373                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
21374
21375                 # increase in usage from by $size_tmp
21376                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
21377                         error "MDT free space wrong after write: " \
21378                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
21379         fi
21380
21381         # truncate
21382         local size_dom=10000
21383
21384         $TRUNCATE $dom $size_dom
21385         [ $(stat -c%s $dom) == $size_dom ] ||
21386                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
21387         if [ $space_check == 1 ]; then
21388                 mdtfree1=$(do_facet $facet \
21389                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21390                 # decrease in usage from $size_tmp to new $size_dom
21391                 [ $(($mdtfree1 - $mdtfree2)) -ge \
21392                   $(((size_tmp - size_dom) / 1024)) ] ||
21393                         error "MDT free space is wrong after truncate: " \
21394                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
21395         fi
21396
21397         # append
21398         cat $tmp >> $dom
21399         sync
21400         size_dom=$((size_dom + size_tmp))
21401         [ $(stat -c%s $dom) == $size_dom ] ||
21402                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
21403         if [ $space_check == 1 ]; then
21404                 mdtfree2=$(do_facet $facet \
21405                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21406                 # increase in usage by $size_tmp from previous
21407                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
21408                         error "MDT free space is wrong after append: " \
21409                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
21410         fi
21411
21412         # delete
21413         rm $dom
21414         if [ $space_check == 1 ]; then
21415                 mdtfree1=$(do_facet $facet \
21416                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21417                 # decrease in usage by $size_dom from previous
21418                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
21419                         error "MDT free space is wrong after removal: " \
21420                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
21421         fi
21422
21423         # combined striping
21424         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
21425                 error "Can't create DoM + OST striping"
21426
21427         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
21428         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
21429         # check also direct IO along write
21430         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
21431         sync
21432         cmp $tmp $dom || error "file data is different"
21433         [ $(stat -c%s $dom) == $size_tmp ] ||
21434                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
21435         rm $dom $tmp
21436
21437         return 0
21438 }
21439 run_test 270a "DoM: basic functionality tests"
21440
21441 test_270b() {
21442         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21443                 skip "Need MDS version at least 2.10.55"
21444
21445         local dom=$DIR/$tdir/dom_file
21446         local max_size=1048576
21447
21448         mkdir -p $DIR/$tdir
21449         $LFS setstripe -E $max_size -L mdt $dom
21450
21451         # truncate over the limit
21452         $TRUNCATE $dom $(($max_size + 1)) &&
21453                 error "successful truncate over the maximum size"
21454         # write over the limit
21455         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
21456                 error "successful write over the maximum size"
21457         # append over the limit
21458         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
21459         echo "12345" >> $dom && error "successful append over the maximum size"
21460         rm $dom
21461
21462         return 0
21463 }
21464 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
21465
21466 test_270c() {
21467         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21468                 skip "Need MDS version at least 2.10.55"
21469
21470         mkdir -p $DIR/$tdir
21471         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21472
21473         # check files inherit DoM EA
21474         touch $DIR/$tdir/first
21475         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
21476                 error "bad pattern"
21477         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
21478                 error "bad stripe count"
21479         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
21480                 error "bad stripe size"
21481
21482         # check directory inherits DoM EA and uses it as default
21483         mkdir $DIR/$tdir/subdir
21484         touch $DIR/$tdir/subdir/second
21485         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
21486                 error "bad pattern in sub-directory"
21487         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
21488                 error "bad stripe count in sub-directory"
21489         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
21490                 error "bad stripe size in sub-directory"
21491         return 0
21492 }
21493 run_test 270c "DoM: DoM EA inheritance tests"
21494
21495 test_270d() {
21496         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21497                 skip "Need MDS version at least 2.10.55"
21498
21499         mkdir -p $DIR/$tdir
21500         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21501
21502         # inherit default DoM striping
21503         mkdir $DIR/$tdir/subdir
21504         touch $DIR/$tdir/subdir/f1
21505
21506         # change default directory striping
21507         $LFS setstripe -c 1 $DIR/$tdir/subdir
21508         touch $DIR/$tdir/subdir/f2
21509         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
21510                 error "wrong default striping in file 2"
21511         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
21512                 error "bad pattern in file 2"
21513         return 0
21514 }
21515 run_test 270d "DoM: change striping from DoM to RAID0"
21516
21517 test_270e() {
21518         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21519                 skip "Need MDS version at least 2.10.55"
21520
21521         mkdir -p $DIR/$tdir/dom
21522         mkdir -p $DIR/$tdir/norm
21523         DOMFILES=20
21524         NORMFILES=10
21525         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
21526         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
21527
21528         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
21529         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
21530
21531         # find DoM files by layout
21532         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
21533         [ $NUM -eq  $DOMFILES ] ||
21534                 error "lfs find -L: found $NUM, expected $DOMFILES"
21535         echo "Test 1: lfs find 20 DOM files by layout: OK"
21536
21537         # there should be 1 dir with default DOM striping
21538         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
21539         [ $NUM -eq  1 ] ||
21540                 error "lfs find -L: found $NUM, expected 1 dir"
21541         echo "Test 2: lfs find 1 DOM dir by layout: OK"
21542
21543         # find DoM files by stripe size
21544         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
21545         [ $NUM -eq  $DOMFILES ] ||
21546                 error "lfs find -S: found $NUM, expected $DOMFILES"
21547         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
21548
21549         # find files by stripe offset except DoM files
21550         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
21551         [ $NUM -eq  $NORMFILES ] ||
21552                 error "lfs find -i: found $NUM, expected $NORMFILES"
21553         echo "Test 5: lfs find no DOM files by stripe index: OK"
21554         return 0
21555 }
21556 run_test 270e "DoM: lfs find with DoM files test"
21557
21558 test_270f() {
21559         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21560                 skip "Need MDS version at least 2.10.55"
21561
21562         local mdtname=${FSNAME}-MDT0000-mdtlov
21563         local dom=$DIR/$tdir/dom_file
21564         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
21565                                                 lod.$mdtname.dom_stripesize)
21566         local dom_limit=131072
21567
21568         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
21569         local dom_current=$(do_facet mds1 $LCTL get_param -n \
21570                                                 lod.$mdtname.dom_stripesize)
21571         [ ${dom_limit} -eq ${dom_current} ] ||
21572                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
21573
21574         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21575         $LFS setstripe -d $DIR/$tdir
21576         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
21577                 error "Can't set directory default striping"
21578
21579         # exceed maximum stripe size
21580         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
21581                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
21582         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
21583                 error "Able to create DoM component size more than LOD limit"
21584
21585         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
21586         dom_current=$(do_facet mds1 $LCTL get_param -n \
21587                                                 lod.$mdtname.dom_stripesize)
21588         [ 0 -eq ${dom_current} ] ||
21589                 error "Can't set zero DoM stripe limit"
21590         rm $dom
21591
21592         # attempt to create DoM file on server with disabled DoM should
21593         # remove DoM entry from layout and be succeed
21594         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
21595                 error "Can't create DoM file (DoM is disabled)"
21596         [ $($LFS getstripe -L $dom) == "mdt" ] &&
21597                 error "File has DoM component while DoM is disabled"
21598         rm $dom
21599
21600         # attempt to create DoM file with only DoM stripe should return error
21601         $LFS setstripe -E $dom_limit -L mdt $dom &&
21602                 error "Able to create DoM-only file while DoM is disabled"
21603
21604         # too low values to be aligned with smallest stripe size 64K
21605         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
21606         dom_current=$(do_facet mds1 $LCTL get_param -n \
21607                                                 lod.$mdtname.dom_stripesize)
21608         [ 30000 -eq ${dom_current} ] &&
21609                 error "Can set too small DoM stripe limit"
21610
21611         # 64K is a minimal stripe size in Lustre, expect limit of that size
21612         [ 65536 -eq ${dom_current} ] ||
21613                 error "Limit is not set to 64K but ${dom_current}"
21614
21615         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
21616         dom_current=$(do_facet mds1 $LCTL get_param -n \
21617                                                 lod.$mdtname.dom_stripesize)
21618         echo $dom_current
21619         [ 2147483648 -eq ${dom_current} ] &&
21620                 error "Can set too large DoM stripe limit"
21621
21622         do_facet mds1 $LCTL set_param -n \
21623                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
21624         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
21625                 error "Can't create DoM component size after limit change"
21626         do_facet mds1 $LCTL set_param -n \
21627                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
21628         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
21629                 error "Can't create DoM file after limit decrease"
21630         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
21631                 error "Can create big DoM component after limit decrease"
21632         touch ${dom}_def ||
21633                 error "Can't create file with old default layout"
21634
21635         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
21636         return 0
21637 }
21638 run_test 270f "DoM: maximum DoM stripe size checks"
21639
21640 test_270g() {
21641         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
21642                 skip "Need MDS version at least 2.13.52"
21643         local dom=$DIR/$tdir/$tfile
21644
21645         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21646         local lodname=${FSNAME}-MDT0000-mdtlov
21647
21648         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21649         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
21650         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
21651         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21652
21653         local dom_limit=1024
21654         local dom_threshold="50%"
21655
21656         $LFS setstripe -d $DIR/$tdir
21657         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
21658                 error "Can't set directory default striping"
21659
21660         do_facet mds1 $LCTL set_param -n \
21661                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
21662         # set 0 threshold and create DOM file to change tunable stripesize
21663         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
21664         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
21665                 error "Failed to create $dom file"
21666         # now tunable dom_cur_stripesize should reach maximum
21667         local dom_current=$(do_facet mds1 $LCTL get_param -n \
21668                                         lod.${lodname}.dom_stripesize_cur_kb)
21669         [[ $dom_current == $dom_limit ]] ||
21670                 error "Current DOM stripesize is not maximum"
21671         rm $dom
21672
21673         # set threshold for further tests
21674         do_facet mds1 $LCTL set_param -n \
21675                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
21676         echo "DOM threshold is $dom_threshold free space"
21677         local dom_def
21678         local dom_set
21679         # Spoof bfree to exceed threshold
21680         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
21681         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
21682         for spfree in 40 20 0 15 30 55; do
21683                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
21684                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
21685                         error "Failed to create $dom file"
21686                 dom_def=$(do_facet mds1 $LCTL get_param -n \
21687                                         lod.${lodname}.dom_stripesize_cur_kb)
21688                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
21689                 [[ $dom_def != $dom_current ]] ||
21690                         error "Default stripe size was not changed"
21691                 if [[ $spfree > 0 ]] ; then
21692                         dom_set=$($LFS getstripe -S $dom)
21693                         [[ $dom_set == $((dom_def * 1024)) ]] ||
21694                                 error "DOM component size is still old"
21695                 else
21696                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
21697                                 error "DoM component is set with no free space"
21698                 fi
21699                 rm $dom
21700                 dom_current=$dom_def
21701         done
21702 }
21703 run_test 270g "DoM: default DoM stripe size depends on free space"
21704
21705 test_270h() {
21706         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
21707                 skip "Need MDS version at least 2.13.53"
21708
21709         local mdtname=${FSNAME}-MDT0000-mdtlov
21710         local dom=$DIR/$tdir/$tfile
21711         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21712
21713         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
21714         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21715
21716         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21717         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
21718                 error "can't create OST file"
21719         # mirrored file with DOM entry in the second mirror
21720         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
21721                 error "can't create mirror with DoM component"
21722
21723         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
21724
21725         # DOM component in the middle and has other enries in the same mirror,
21726         # should succeed but lost DoM component
21727         $LFS setstripe --copy=${dom}_1 $dom ||
21728                 error "Can't create file from OST|DOM mirror layout"
21729         # check new file has no DoM layout after all
21730         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
21731                 error "File has DoM component while DoM is disabled"
21732 }
21733 run_test 270h "DoM: DoM stripe removal when disabled on server"
21734
21735 test_271a() {
21736         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21737                 skip "Need MDS version at least 2.10.55"
21738
21739         local dom=$DIR/$tdir/dom
21740
21741         mkdir -p $DIR/$tdir
21742
21743         $LFS setstripe -E 1024K -L mdt $dom
21744
21745         lctl set_param -n mdc.*.stats=clear
21746         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
21747         cat $dom > /dev/null
21748         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
21749         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
21750         ls $dom
21751         rm -f $dom
21752 }
21753 run_test 271a "DoM: data is cached for read after write"
21754
21755 test_271b() {
21756         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21757                 skip "Need MDS version at least 2.10.55"
21758
21759         local dom=$DIR/$tdir/dom
21760
21761         mkdir -p $DIR/$tdir
21762
21763         $LFS setstripe -E 1024K -L mdt -E EOF $dom
21764
21765         lctl set_param -n mdc.*.stats=clear
21766         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
21767         cancel_lru_locks mdc
21768         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
21769         # second stat to check size is cached on client
21770         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
21771         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
21772         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
21773         rm -f $dom
21774 }
21775 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
21776
21777 test_271ba() {
21778         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21779                 skip "Need MDS version at least 2.10.55"
21780
21781         local dom=$DIR/$tdir/dom
21782
21783         mkdir -p $DIR/$tdir
21784
21785         $LFS setstripe -E 1024K -L mdt -E EOF $dom
21786
21787         lctl set_param -n mdc.*.stats=clear
21788         lctl set_param -n osc.*.stats=clear
21789         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
21790         cancel_lru_locks mdc
21791         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
21792         # second stat to check size is cached on client
21793         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
21794         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
21795         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
21796         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
21797         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
21798         rm -f $dom
21799 }
21800 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
21801
21802
21803 get_mdc_stats() {
21804         local mdtidx=$1
21805         local param=$2
21806         local mdt=MDT$(printf %04x $mdtidx)
21807
21808         if [ -z $param ]; then
21809                 lctl get_param -n mdc.*$mdt*.stats
21810         else
21811                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
21812         fi
21813 }
21814
21815 test_271c() {
21816         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21817                 skip "Need MDS version at least 2.10.55"
21818
21819         local dom=$DIR/$tdir/dom
21820
21821         mkdir -p $DIR/$tdir
21822
21823         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21824
21825         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
21826         local facet=mds$((mdtidx + 1))
21827
21828         cancel_lru_locks mdc
21829         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
21830         createmany -o $dom 1000
21831         lctl set_param -n mdc.*.stats=clear
21832         smalliomany -w $dom 1000 200
21833         get_mdc_stats $mdtidx
21834         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
21835         # Each file has 1 open, 1 IO enqueues, total 2000
21836         # but now we have also +1 getxattr for security.capability, total 3000
21837         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
21838         unlinkmany $dom 1000
21839
21840         cancel_lru_locks mdc
21841         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
21842         createmany -o $dom 1000
21843         lctl set_param -n mdc.*.stats=clear
21844         smalliomany -w $dom 1000 200
21845         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
21846         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
21847         # for OPEN and IO lock.
21848         [ $((enq - enq_2)) -ge 1000 ] ||
21849                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
21850         unlinkmany $dom 1000
21851         return 0
21852 }
21853 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
21854
21855 cleanup_271def_tests() {
21856         trap 0
21857         rm -f $1
21858 }
21859
21860 test_271d() {
21861         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
21862                 skip "Need MDS version at least 2.10.57"
21863
21864         local dom=$DIR/$tdir/dom
21865         local tmp=$TMP/$tfile
21866         trap "cleanup_271def_tests $tmp" EXIT
21867
21868         mkdir -p $DIR/$tdir
21869
21870         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21871
21872         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
21873
21874         cancel_lru_locks mdc
21875         dd if=/dev/urandom of=$tmp bs=1000 count=1
21876         dd if=$tmp of=$dom bs=1000 count=1
21877         cancel_lru_locks mdc
21878
21879         cat /etc/hosts >> $tmp
21880         lctl set_param -n mdc.*.stats=clear
21881
21882         # append data to the same file it should update local page
21883         echo "Append to the same page"
21884         cat /etc/hosts >> $dom
21885         local num=$(get_mdc_stats $mdtidx ost_read)
21886         local ra=$(get_mdc_stats $mdtidx req_active)
21887         local rw=$(get_mdc_stats $mdtidx req_waittime)
21888
21889         [ -z $num ] || error "$num READ RPC occured"
21890         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
21891         echo "... DONE"
21892
21893         # compare content
21894         cmp $tmp $dom || error "file miscompare"
21895
21896         cancel_lru_locks mdc
21897         lctl set_param -n mdc.*.stats=clear
21898
21899         echo "Open and read file"
21900         cat $dom > /dev/null
21901         local num=$(get_mdc_stats $mdtidx ost_read)
21902         local ra=$(get_mdc_stats $mdtidx req_active)
21903         local rw=$(get_mdc_stats $mdtidx req_waittime)
21904
21905         [ -z $num ] || error "$num READ RPC occured"
21906         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
21907         echo "... DONE"
21908
21909         # compare content
21910         cmp $tmp $dom || error "file miscompare"
21911
21912         return 0
21913 }
21914 run_test 271d "DoM: read on open (1K file in reply buffer)"
21915
21916 test_271f() {
21917         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
21918                 skip "Need MDS version at least 2.10.57"
21919
21920         local dom=$DIR/$tdir/dom
21921         local tmp=$TMP/$tfile
21922         trap "cleanup_271def_tests $tmp" EXIT
21923
21924         mkdir -p $DIR/$tdir
21925
21926         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21927
21928         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
21929
21930         cancel_lru_locks mdc
21931         dd if=/dev/urandom of=$tmp bs=265000 count=1
21932         dd if=$tmp of=$dom bs=265000 count=1
21933         cancel_lru_locks mdc
21934         cat /etc/hosts >> $tmp
21935         lctl set_param -n mdc.*.stats=clear
21936
21937         echo "Append to the same page"
21938         cat /etc/hosts >> $dom
21939         local num=$(get_mdc_stats $mdtidx ost_read)
21940         local ra=$(get_mdc_stats $mdtidx req_active)
21941         local rw=$(get_mdc_stats $mdtidx req_waittime)
21942
21943         [ -z $num ] || error "$num READ RPC occured"
21944         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
21945         echo "... DONE"
21946
21947         # compare content
21948         cmp $tmp $dom || error "file miscompare"
21949
21950         cancel_lru_locks mdc
21951         lctl set_param -n mdc.*.stats=clear
21952
21953         echo "Open and read file"
21954         cat $dom > /dev/null
21955         local num=$(get_mdc_stats $mdtidx ost_read)
21956         local ra=$(get_mdc_stats $mdtidx req_active)
21957         local rw=$(get_mdc_stats $mdtidx req_waittime)
21958
21959         [ -z $num ] && num=0
21960         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
21961         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
21962         echo "... DONE"
21963
21964         # compare content
21965         cmp $tmp $dom || error "file miscompare"
21966
21967         return 0
21968 }
21969 run_test 271f "DoM: read on open (200K file and read tail)"
21970
21971 test_271g() {
21972         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
21973                 skip "Skipping due to old client or server version"
21974
21975         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
21976         # to get layout
21977         $CHECKSTAT -t file $DIR1/$tfile
21978
21979         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
21980         MULTIOP_PID=$!
21981         sleep 1
21982         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
21983         $LCTL set_param fail_loc=0x80000314
21984         rm $DIR1/$tfile || error "Unlink fails"
21985         RC=$?
21986         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
21987         [ $RC -eq 0 ] || error "Failed write to stale object"
21988 }
21989 run_test 271g "Discard DoM data vs client flush race"
21990
21991 test_272a() {
21992         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
21993                 skip "Need MDS version at least 2.11.50"
21994
21995         local dom=$DIR/$tdir/dom
21996         mkdir -p $DIR/$tdir
21997
21998         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
21999         dd if=/dev/urandom of=$dom bs=512K count=1 ||
22000                 error "failed to write data into $dom"
22001         local old_md5=$(md5sum $dom)
22002
22003         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
22004                 error "failed to migrate to the same DoM component"
22005
22006         local new_md5=$(md5sum $dom)
22007
22008         [ "$old_md5" == "$new_md5" ] ||
22009                 error "md5sum differ: $old_md5, $new_md5"
22010
22011         [ $($LFS getstripe -c $dom) -eq 2 ] ||
22012                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
22013 }
22014 run_test 272a "DoM migration: new layout with the same DOM component"
22015
22016 test_272b() {
22017         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22018                 skip "Need MDS version at least 2.11.50"
22019
22020         local dom=$DIR/$tdir/dom
22021         mkdir -p $DIR/$tdir
22022         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22023
22024         local mdtidx=$($LFS getstripe -m $dom)
22025         local mdtname=MDT$(printf %04x $mdtidx)
22026         local facet=mds$((mdtidx + 1))
22027
22028         local mdtfree1=$(do_facet $facet \
22029                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22030         dd if=/dev/urandom of=$dom bs=2M count=1 ||
22031                 error "failed to write data into $dom"
22032         local old_md5=$(md5sum $dom)
22033         cancel_lru_locks mdc
22034         local mdtfree1=$(do_facet $facet \
22035                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22036
22037         $LFS migrate -c2 $dom ||
22038                 error "failed to migrate to the new composite layout"
22039         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22040                 error "MDT stripe was not removed"
22041
22042         cancel_lru_locks mdc
22043         local new_md5=$(md5sum $dom)
22044         [ "$old_md5" == "$new_md5" ] ||
22045                 error "$old_md5 != $new_md5"
22046
22047         # Skip free space checks with ZFS
22048         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22049                 local mdtfree2=$(do_facet $facet \
22050                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22051                 [ $mdtfree2 -gt $mdtfree1 ] ||
22052                         error "MDT space is not freed after migration"
22053         fi
22054         return 0
22055 }
22056 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
22057
22058 test_272c() {
22059         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22060                 skip "Need MDS version at least 2.11.50"
22061
22062         local dom=$DIR/$tdir/$tfile
22063         mkdir -p $DIR/$tdir
22064         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22065
22066         local mdtidx=$($LFS getstripe -m $dom)
22067         local mdtname=MDT$(printf %04x $mdtidx)
22068         local facet=mds$((mdtidx + 1))
22069
22070         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
22071                 error "failed to write data into $dom"
22072         local old_md5=$(md5sum $dom)
22073         cancel_lru_locks mdc
22074         local mdtfree1=$(do_facet $facet \
22075                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22076
22077         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
22078                 error "failed to migrate to the new composite layout"
22079         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
22080                 error "MDT stripe was not removed"
22081
22082         cancel_lru_locks mdc
22083         local new_md5=$(md5sum $dom)
22084         [ "$old_md5" == "$new_md5" ] ||
22085                 error "$old_md5 != $new_md5"
22086
22087         # Skip free space checks with ZFS
22088         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22089                 local mdtfree2=$(do_facet $facet \
22090                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22091                 [ $mdtfree2 -gt $mdtfree1 ] ||
22092                         error "MDS space is not freed after migration"
22093         fi
22094         return 0
22095 }
22096 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
22097
22098 test_272d() {
22099         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22100                 skip "Need MDS version at least 2.12.55"
22101
22102         local dom=$DIR/$tdir/$tfile
22103         mkdir -p $DIR/$tdir
22104         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22105
22106         local mdtidx=$($LFS getstripe -m $dom)
22107         local mdtname=MDT$(printf %04x $mdtidx)
22108         local facet=mds$((mdtidx + 1))
22109
22110         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
22111                 error "failed to write data into $dom"
22112         local old_md5=$(md5sum $dom)
22113         cancel_lru_locks mdc
22114         local mdtfree1=$(do_facet $facet \
22115                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22116
22117         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
22118                 error "failed mirroring to the new composite layout"
22119         $LFS mirror resync $dom ||
22120                 error "failed mirror resync"
22121         $LFS mirror split --mirror-id 1 -d $dom ||
22122                 error "failed mirror split"
22123
22124         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22125                 error "MDT stripe was not removed"
22126
22127         cancel_lru_locks mdc
22128         local new_md5=$(md5sum $dom)
22129         [ "$old_md5" == "$new_md5" ] ||
22130                 error "$old_md5 != $new_md5"
22131
22132         # Skip free space checks with ZFS
22133         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22134                 local mdtfree2=$(do_facet $facet \
22135                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22136                 [ $mdtfree2 -gt $mdtfree1 ] ||
22137                         error "MDS space is not freed after DOM mirror deletion"
22138         fi
22139         return 0
22140 }
22141 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
22142
22143 test_272e() {
22144         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22145                 skip "Need MDS version at least 2.12.55"
22146
22147         local dom=$DIR/$tdir/$tfile
22148         mkdir -p $DIR/$tdir
22149         $LFS setstripe -c 2 $dom
22150
22151         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
22152                 error "failed to write data into $dom"
22153         local old_md5=$(md5sum $dom)
22154         cancel_lru_locks mdc
22155
22156         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
22157                 error "failed mirroring to the DOM layout"
22158         $LFS mirror resync $dom ||
22159                 error "failed mirror resync"
22160         $LFS mirror split --mirror-id 1 -d $dom ||
22161                 error "failed mirror split"
22162
22163         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22164                 error "MDT stripe was not removed"
22165
22166         cancel_lru_locks mdc
22167         local new_md5=$(md5sum $dom)
22168         [ "$old_md5" == "$new_md5" ] ||
22169                 error "$old_md5 != $new_md5"
22170
22171         return 0
22172 }
22173 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
22174
22175 test_272f() {
22176         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22177                 skip "Need MDS version at least 2.12.55"
22178
22179         local dom=$DIR/$tdir/$tfile
22180         mkdir -p $DIR/$tdir
22181         $LFS setstripe -c 2 $dom
22182
22183         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
22184                 error "failed to write data into $dom"
22185         local old_md5=$(md5sum $dom)
22186         cancel_lru_locks mdc
22187
22188         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
22189                 error "failed migrating to the DOM file"
22190
22191         cancel_lru_locks mdc
22192         local new_md5=$(md5sum $dom)
22193         [ "$old_md5" != "$new_md5" ] &&
22194                 error "$old_md5 != $new_md5"
22195
22196         return 0
22197 }
22198 run_test 272f "DoM migration: OST-striped file to DOM file"
22199
22200 test_273a() {
22201         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22202                 skip "Need MDS version at least 2.11.50"
22203
22204         # Layout swap cannot be done if either file has DOM component,
22205         # this will never be supported, migration should be used instead
22206
22207         local dom=$DIR/$tdir/$tfile
22208         mkdir -p $DIR/$tdir
22209
22210         $LFS setstripe -c2 ${dom}_plain
22211         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
22212         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
22213                 error "can swap layout with DoM component"
22214         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
22215                 error "can swap layout with DoM component"
22216
22217         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
22218         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
22219                 error "can swap layout with DoM component"
22220         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
22221                 error "can swap layout with DoM component"
22222         return 0
22223 }
22224 run_test 273a "DoM: layout swapping should fail with DOM"
22225
22226 test_273b() {
22227         mkdir -p $DIR/$tdir
22228         $LFS setstripe -E 1M -L mdt -E -1 -c -1 $DIR/$tdir
22229
22230 #define OBD_FAIL_MDS_COMMITRW_DELAY      0x16b
22231         do_facet mds1 $LCTL set_param fail_loc=0x8000016b fail_val=2
22232
22233         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
22234 }
22235 run_test 273b "DoM: race writeback and object destroy"
22236
22237 test_275() {
22238         remote_ost_nodsh && skip "remote OST with nodsh"
22239         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
22240                 skip "Need OST version >= 2.10.57"
22241
22242         local file=$DIR/$tfile
22243         local oss
22244
22245         oss=$(comma_list $(osts_nodes))
22246
22247         dd if=/dev/urandom of=$file bs=1M count=2 ||
22248                 error "failed to create a file"
22249         cancel_lru_locks osc
22250
22251         #lock 1
22252         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
22253                 error "failed to read a file"
22254
22255 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
22256         $LCTL set_param fail_loc=0x8000031f
22257
22258         cancel_lru_locks osc &
22259         sleep 1
22260
22261 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
22262         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
22263         #IO takes another lock, but matches the PENDING one
22264         #and places it to the IO RPC
22265         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
22266                 error "failed to read a file with PENDING lock"
22267 }
22268 run_test 275 "Read on a canceled duplicate lock"
22269
22270 test_276() {
22271         remote_ost_nodsh && skip "remote OST with nodsh"
22272         local pid
22273
22274         do_facet ost1 "(while true; do \
22275                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
22276                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
22277         pid=$!
22278
22279         for LOOP in $(seq 20); do
22280                 stop ost1
22281                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
22282         done
22283         kill -9 $pid
22284         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
22285                 rm $TMP/sanity_276_pid"
22286 }
22287 run_test 276 "Race between mount and obd_statfs"
22288
22289 test_277() {
22290         $LCTL set_param ldlm.namespaces.*.lru_size=0
22291         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22292         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
22293                         grep ^used_mb | awk '{print $2}')
22294         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
22295         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
22296                 oflag=direct conv=notrunc
22297         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
22298                         grep ^used_mb | awk '{print $2}')
22299         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
22300 }
22301 run_test 277 "Direct IO shall drop page cache"
22302
22303 test_278() {
22304         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
22305         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
22306         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
22307                 skip "needs the same host for mdt1 mdt2" && return
22308
22309         local pid1
22310         local pid2
22311
22312 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
22313         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
22314         stop mds2 &
22315         pid2=$!
22316
22317         stop mds1
22318
22319         echo "Starting MDTs"
22320         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22321         wait $pid2
22322 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
22323 #will return NULL
22324         do_facet mds2 $LCTL set_param fail_loc=0
22325
22326         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
22327         wait_recovery_complete mds2
22328 }
22329 run_test 278 "Race starting MDS between MDTs stop/start"
22330
22331 test_280() {
22332         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
22333                 skip "Need MGS version at least 2.13.52"
22334         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22335         combined_mgs_mds || skip "needs combined MGS/MDT"
22336
22337         umount_client $MOUNT
22338 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
22339         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
22340
22341         mount_client $MOUNT &
22342         sleep 1
22343         stop mgs || error "stop mgs failed"
22344         #for a race mgs would crash
22345         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
22346         # make sure we unmount client before remounting
22347         wait
22348         umount_client $MOUNT
22349         mount_client $MOUNT || error "mount client failed"
22350 }
22351 run_test 280 "Race between MGS umount and client llog processing"
22352
22353 cleanup_test_300() {
22354         trap 0
22355         umask $SAVE_UMASK
22356 }
22357 test_striped_dir() {
22358         local mdt_index=$1
22359         local stripe_count
22360         local stripe_index
22361
22362         mkdir -p $DIR/$tdir
22363
22364         SAVE_UMASK=$(umask)
22365         trap cleanup_test_300 RETURN EXIT
22366
22367         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
22368                                                 $DIR/$tdir/striped_dir ||
22369                 error "set striped dir error"
22370
22371         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
22372         [ "$mode" = "755" ] || error "expect 755 got $mode"
22373
22374         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
22375                 error "getdirstripe failed"
22376         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
22377         if [ "$stripe_count" != "2" ]; then
22378                 error "1:stripe_count is $stripe_count, expect 2"
22379         fi
22380         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
22381         if [ "$stripe_count" != "2" ]; then
22382                 error "2:stripe_count is $stripe_count, expect 2"
22383         fi
22384
22385         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
22386         if [ "$stripe_index" != "$mdt_index" ]; then
22387                 error "stripe_index is $stripe_index, expect $mdt_index"
22388         fi
22389
22390         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
22391                 error "nlink error after create striped dir"
22392
22393         mkdir $DIR/$tdir/striped_dir/a
22394         mkdir $DIR/$tdir/striped_dir/b
22395
22396         stat $DIR/$tdir/striped_dir/a ||
22397                 error "create dir under striped dir failed"
22398         stat $DIR/$tdir/striped_dir/b ||
22399                 error "create dir under striped dir failed"
22400
22401         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
22402                 error "nlink error after mkdir"
22403
22404         rmdir $DIR/$tdir/striped_dir/a
22405         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
22406                 error "nlink error after rmdir"
22407
22408         rmdir $DIR/$tdir/striped_dir/b
22409         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
22410                 error "nlink error after rmdir"
22411
22412         chattr +i $DIR/$tdir/striped_dir
22413         createmany -o $DIR/$tdir/striped_dir/f 10 &&
22414                 error "immutable flags not working under striped dir!"
22415         chattr -i $DIR/$tdir/striped_dir
22416
22417         rmdir $DIR/$tdir/striped_dir ||
22418                 error "rmdir striped dir error"
22419
22420         cleanup_test_300
22421
22422         true
22423 }
22424
22425 test_300a() {
22426         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22427                 skip "skipped for lustre < 2.7.0"
22428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22429         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22430
22431         test_striped_dir 0 || error "failed on striped dir on MDT0"
22432         test_striped_dir 1 || error "failed on striped dir on MDT0"
22433 }
22434 run_test 300a "basic striped dir sanity test"
22435
22436 test_300b() {
22437         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22438                 skip "skipped for lustre < 2.7.0"
22439         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22440         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22441
22442         local i
22443         local mtime1
22444         local mtime2
22445         local mtime3
22446
22447         test_mkdir $DIR/$tdir || error "mkdir fail"
22448         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22449                 error "set striped dir error"
22450         for i in {0..9}; do
22451                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
22452                 sleep 1
22453                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
22454                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
22455                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
22456                 sleep 1
22457                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
22458                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
22459                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
22460         done
22461         true
22462 }
22463 run_test 300b "check ctime/mtime for striped dir"
22464
22465 test_300c() {
22466         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22467                 skip "skipped for lustre < 2.7.0"
22468         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22469         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22470
22471         local file_count
22472
22473         mkdir_on_mdt0 $DIR/$tdir
22474         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
22475                 error "set striped dir error"
22476
22477         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
22478                 error "chown striped dir failed"
22479
22480         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
22481                 error "create 5k files failed"
22482
22483         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
22484
22485         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
22486
22487         rm -rf $DIR/$tdir
22488 }
22489 run_test 300c "chown && check ls under striped directory"
22490
22491 test_300d() {
22492         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22493                 skip "skipped for lustre < 2.7.0"
22494         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22495         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22496
22497         local stripe_count
22498         local file
22499
22500         mkdir -p $DIR/$tdir
22501         $LFS setstripe -c 2 $DIR/$tdir
22502
22503         #local striped directory
22504         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22505                 error "set striped dir error"
22506         #look at the directories for debug purposes
22507         ls -l $DIR/$tdir
22508         $LFS getdirstripe $DIR/$tdir
22509         ls -l $DIR/$tdir/striped_dir
22510         $LFS getdirstripe $DIR/$tdir/striped_dir
22511         createmany -o $DIR/$tdir/striped_dir/f 10 ||
22512                 error "create 10 files failed"
22513
22514         #remote striped directory
22515         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
22516                 error "set striped dir error"
22517         #look at the directories for debug purposes
22518         ls -l $DIR/$tdir
22519         $LFS getdirstripe $DIR/$tdir
22520         ls -l $DIR/$tdir/remote_striped_dir
22521         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
22522         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
22523                 error "create 10 files failed"
22524
22525         for file in $(find $DIR/$tdir); do
22526                 stripe_count=$($LFS getstripe -c $file)
22527                 [ $stripe_count -eq 2 ] ||
22528                         error "wrong stripe $stripe_count for $file"
22529         done
22530
22531         rm -rf $DIR/$tdir
22532 }
22533 run_test 300d "check default stripe under striped directory"
22534
22535 test_300e() {
22536         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22537                 skip "Need MDS version at least 2.7.55"
22538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22539         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22540
22541         local stripe_count
22542         local file
22543
22544         mkdir -p $DIR/$tdir
22545
22546         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22547                 error "set striped dir error"
22548
22549         touch $DIR/$tdir/striped_dir/a
22550         touch $DIR/$tdir/striped_dir/b
22551         touch $DIR/$tdir/striped_dir/c
22552
22553         mkdir $DIR/$tdir/striped_dir/dir_a
22554         mkdir $DIR/$tdir/striped_dir/dir_b
22555         mkdir $DIR/$tdir/striped_dir/dir_c
22556
22557         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
22558                 error "set striped adir under striped dir error"
22559
22560         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
22561                 error "set striped bdir under striped dir error"
22562
22563         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
22564                 error "set striped cdir under striped dir error"
22565
22566         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
22567                 error "rename dir under striped dir fails"
22568
22569         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
22570                 error "rename dir under different stripes fails"
22571
22572         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
22573                 error "rename file under striped dir should succeed"
22574
22575         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
22576                 error "rename dir under striped dir should succeed"
22577
22578         rm -rf $DIR/$tdir
22579 }
22580 run_test 300e "check rename under striped directory"
22581
22582 test_300f() {
22583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22584         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22585         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22586                 skip "Need MDS version at least 2.7.55"
22587
22588         local stripe_count
22589         local file
22590
22591         rm -rf $DIR/$tdir
22592         mkdir -p $DIR/$tdir
22593
22594         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22595                 error "set striped dir error"
22596
22597         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
22598                 error "set striped dir error"
22599
22600         touch $DIR/$tdir/striped_dir/a
22601         mkdir $DIR/$tdir/striped_dir/dir_a
22602         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
22603                 error "create striped dir under striped dir fails"
22604
22605         touch $DIR/$tdir/striped_dir1/b
22606         mkdir $DIR/$tdir/striped_dir1/dir_b
22607         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
22608                 error "create striped dir under striped dir fails"
22609
22610         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
22611                 error "rename dir under different striped dir should fail"
22612
22613         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
22614                 error "rename striped dir under diff striped dir should fail"
22615
22616         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
22617                 error "rename file under diff striped dirs fails"
22618
22619         rm -rf $DIR/$tdir
22620 }
22621 run_test 300f "check rename cross striped directory"
22622
22623 test_300_check_default_striped_dir()
22624 {
22625         local dirname=$1
22626         local default_count=$2
22627         local default_index=$3
22628         local stripe_count
22629         local stripe_index
22630         local dir_stripe_index
22631         local dir
22632
22633         echo "checking $dirname $default_count $default_index"
22634         $LFS setdirstripe -D -c $default_count -i $default_index \
22635                                 -H all_char $DIR/$tdir/$dirname ||
22636                 error "set default stripe on striped dir error"
22637         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
22638         [ $stripe_count -eq $default_count ] ||
22639                 error "expect $default_count get $stripe_count for $dirname"
22640
22641         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
22642         [ $stripe_index -eq $default_index ] ||
22643                 error "expect $default_index get $stripe_index for $dirname"
22644
22645         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
22646                                                 error "create dirs failed"
22647
22648         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
22649         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
22650         for dir in $(find $DIR/$tdir/$dirname/*); do
22651                 stripe_count=$($LFS getdirstripe -c $dir)
22652                 (( $stripe_count == $default_count )) ||
22653                 (( $stripe_count == $MDSCOUNT && $default_count == -1 )) ||
22654                 (( $stripe_count == 0 )) || (( $default_count == 1 )) ||
22655                 error "stripe count $default_count != $stripe_count for $dir"
22656
22657                 stripe_index=$($LFS getdirstripe -i $dir)
22658                 [ $default_index -eq -1 ] ||
22659                         [ $stripe_index -eq $default_index ] ||
22660                         error "$stripe_index != $default_index for $dir"
22661
22662                 #check default stripe
22663                 stripe_count=$($LFS getdirstripe -D -c $dir)
22664                 [ $stripe_count -eq $default_count ] ||
22665                 error "default count $default_count != $stripe_count for $dir"
22666
22667                 stripe_index=$($LFS getdirstripe -D -i $dir)
22668                 [ $stripe_index -eq $default_index ] ||
22669                 error "default index $default_index != $stripe_index for $dir"
22670         done
22671         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
22672 }
22673
22674 test_300g() {
22675         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22676         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22677                 skip "Need MDS version at least 2.7.55"
22678
22679         local dir
22680         local stripe_count
22681         local stripe_index
22682
22683         mkdir_on_mdt0 $DIR/$tdir
22684         mkdir $DIR/$tdir/normal_dir
22685
22686         #Checking when client cache stripe index
22687         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22688         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
22689                 error "create striped_dir failed"
22690
22691         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
22692                 error "create dir0 fails"
22693         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
22694         [ $stripe_index -eq 0 ] ||
22695                 error "dir0 expect index 0 got $stripe_index"
22696
22697         mkdir $DIR/$tdir/striped_dir/dir1 ||
22698                 error "create dir1 fails"
22699         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
22700         [ $stripe_index -eq 1 ] ||
22701                 error "dir1 expect index 1 got $stripe_index"
22702
22703         #check default stripe count/stripe index
22704         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
22705         test_300_check_default_striped_dir normal_dir 1 0
22706         test_300_check_default_striped_dir normal_dir -1 1
22707         test_300_check_default_striped_dir normal_dir 2 -1
22708
22709         #delete default stripe information
22710         echo "delete default stripeEA"
22711         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
22712                 error "set default stripe on striped dir error"
22713
22714         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
22715         for dir in $(find $DIR/$tdir/normal_dir/*); do
22716                 stripe_count=$($LFS getdirstripe -c $dir)
22717                 [ $stripe_count -eq 0 ] ||
22718                         error "expect 1 get $stripe_count for $dir"
22719         done
22720 }
22721 run_test 300g "check default striped directory for normal directory"
22722
22723 test_300h() {
22724         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22725         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22726                 skip "Need MDS version at least 2.7.55"
22727
22728         local dir
22729         local stripe_count
22730
22731         mkdir $DIR/$tdir
22732         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
22733                 error "set striped dir error"
22734
22735         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
22736         test_300_check_default_striped_dir striped_dir 1 0
22737         test_300_check_default_striped_dir striped_dir -1 1
22738         test_300_check_default_striped_dir striped_dir 2 -1
22739
22740         #delete default stripe information
22741         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
22742                 error "set default stripe on striped dir error"
22743
22744         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
22745         for dir in $(find $DIR/$tdir/striped_dir/*); do
22746                 stripe_count=$($LFS getdirstripe -c $dir)
22747                 [ $stripe_count -eq 0 ] ||
22748                         error "expect 1 get $stripe_count for $dir"
22749         done
22750 }
22751 run_test 300h "check default striped directory for striped directory"
22752
22753 test_300i() {
22754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22755         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22756         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22757                 skip "Need MDS version at least 2.7.55"
22758
22759         local stripe_count
22760         local file
22761
22762         mkdir $DIR/$tdir
22763
22764         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
22765                 error "set striped dir error"
22766
22767         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
22768                 error "create files under striped dir failed"
22769
22770         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
22771                 error "set striped hashdir error"
22772
22773         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
22774                 error "create dir0 under hash dir failed"
22775         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
22776                 error "create dir1 under hash dir failed"
22777         $LFS setdirstripe -i0 -c$MDSCOUNT -H crush $DIR/$tdir/hashdir/d2 ||
22778                 error "create dir2 under hash dir failed"
22779
22780         # unfortunately, we need to umount to clear dir layout cache for now
22781         # once we fully implement dir layout, we can drop this
22782         umount_client $MOUNT || error "umount failed"
22783         mount_client $MOUNT || error "mount failed"
22784
22785         $LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir
22786         local dircnt=$($LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir | wc -l)
22787         [ $dircnt -eq 2 ] || error "lfs find striped dir got:$dircnt,except:1"
22788
22789         #set the stripe to be unknown hash type
22790         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
22791         $LCTL set_param fail_loc=0x1901
22792         for ((i = 0; i < 10; i++)); do
22793                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
22794                         error "stat f-$i failed"
22795                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
22796         done
22797
22798         touch $DIR/$tdir/striped_dir/f0 &&
22799                 error "create under striped dir with unknown hash should fail"
22800
22801         $LCTL set_param fail_loc=0
22802
22803         umount_client $MOUNT || error "umount failed"
22804         mount_client $MOUNT || error "mount failed"
22805
22806         return 0
22807 }
22808 run_test 300i "client handle unknown hash type striped directory"
22809
22810 test_300j() {
22811         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22812         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22813         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22814                 skip "Need MDS version at least 2.7.55"
22815
22816         local stripe_count
22817         local file
22818
22819         mkdir $DIR/$tdir
22820
22821         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
22822         $LCTL set_param fail_loc=0x1702
22823         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
22824                 error "set striped dir error"
22825
22826         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
22827                 error "create files under striped dir failed"
22828
22829         $LCTL set_param fail_loc=0
22830
22831         rm -rf $DIR/$tdir || error "unlink striped dir fails"
22832
22833         return 0
22834 }
22835 run_test 300j "test large update record"
22836
22837 test_300k() {
22838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22839         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22840         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22841                 skip "Need MDS version at least 2.7.55"
22842
22843         # this test needs a huge transaction
22844         local kb
22845         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
22846              osd*.$FSNAME-MDT0000.kbytestotal")
22847         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
22848
22849         local stripe_count
22850         local file
22851
22852         mkdir $DIR/$tdir
22853
22854         #define OBD_FAIL_LARGE_STRIPE   0x1703
22855         $LCTL set_param fail_loc=0x1703
22856         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
22857                 error "set striped dir error"
22858         $LCTL set_param fail_loc=0
22859
22860         $LFS getdirstripe $DIR/$tdir/striped_dir ||
22861                 error "getstripeddir fails"
22862         rm -rf $DIR/$tdir/striped_dir ||
22863                 error "unlink striped dir fails"
22864
22865         return 0
22866 }
22867 run_test 300k "test large striped directory"
22868
22869 test_300l() {
22870         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22871         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22872         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22873                 skip "Need MDS version at least 2.7.55"
22874
22875         local stripe_index
22876
22877         test_mkdir -p $DIR/$tdir/striped_dir
22878         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
22879                         error "chown $RUNAS_ID failed"
22880         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
22881                 error "set default striped dir failed"
22882
22883         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
22884         $LCTL set_param fail_loc=0x80000158
22885         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
22886
22887         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
22888         [ $stripe_index -eq 1 ] ||
22889                 error "expect 1 get $stripe_index for $dir"
22890 }
22891 run_test 300l "non-root user to create dir under striped dir with stale layout"
22892
22893 test_300m() {
22894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22895         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
22896         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22897                 skip "Need MDS version at least 2.7.55"
22898
22899         mkdir -p $DIR/$tdir/striped_dir
22900         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
22901                 error "set default stripes dir error"
22902
22903         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
22904
22905         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
22906         [ $stripe_count -eq 0 ] ||
22907                         error "expect 0 get $stripe_count for a"
22908
22909         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
22910                 error "set default stripes dir error"
22911
22912         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
22913
22914         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
22915         [ $stripe_count -eq 0 ] ||
22916                         error "expect 0 get $stripe_count for b"
22917
22918         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
22919                 error "set default stripes dir error"
22920
22921         mkdir $DIR/$tdir/striped_dir/c &&
22922                 error "default stripe_index is invalid, mkdir c should fails"
22923
22924         rm -rf $DIR/$tdir || error "rmdir fails"
22925 }
22926 run_test 300m "setstriped directory on single MDT FS"
22927
22928 cleanup_300n() {
22929         local list=$(comma_list $(mdts_nodes))
22930
22931         trap 0
22932         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
22933 }
22934
22935 test_300n() {
22936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22937         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22938         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22939                 skip "Need MDS version at least 2.7.55"
22940         remote_mds_nodsh && skip "remote MDS with nodsh"
22941
22942         local stripe_index
22943         local list=$(comma_list $(mdts_nodes))
22944
22945         trap cleanup_300n RETURN EXIT
22946         mkdir -p $DIR/$tdir
22947         chmod 777 $DIR/$tdir
22948         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
22949                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
22950                 error "create striped dir succeeds with gid=0"
22951
22952         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
22953         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
22954                 error "create striped dir fails with gid=-1"
22955
22956         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
22957         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
22958                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
22959                 error "set default striped dir succeeds with gid=0"
22960
22961
22962         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
22963         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
22964                 error "set default striped dir fails with gid=-1"
22965
22966
22967         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
22968         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
22969                                         error "create test_dir fails"
22970         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
22971                                         error "create test_dir1 fails"
22972         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
22973                                         error "create test_dir2 fails"
22974         cleanup_300n
22975 }
22976 run_test 300n "non-root user to create dir under striped dir with default EA"
22977
22978 test_300o() {
22979         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22980         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22981         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22982                 skip "Need MDS version at least 2.7.55"
22983
22984         local numfree1
22985         local numfree2
22986
22987         mkdir -p $DIR/$tdir
22988
22989         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
22990         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
22991         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
22992                 skip "not enough free inodes $numfree1 $numfree2"
22993         fi
22994
22995         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
22996         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
22997         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
22998                 skip "not enough free space $numfree1 $numfree2"
22999         fi
23000
23001         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
23002                 error "setdirstripe fails"
23003
23004         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
23005                 error "create dirs fails"
23006
23007         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
23008         ls $DIR/$tdir/striped_dir > /dev/null ||
23009                 error "ls striped dir fails"
23010         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
23011                 error "unlink big striped dir fails"
23012 }
23013 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
23014
23015 test_300p() {
23016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23017         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23018         remote_mds_nodsh && skip "remote MDS with nodsh"
23019
23020         mkdir_on_mdt0 $DIR/$tdir
23021
23022         #define OBD_FAIL_OUT_ENOSPC     0x1704
23023         do_facet mds2 lctl set_param fail_loc=0x80001704
23024         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
23025                  && error "create striped directory should fail"
23026
23027         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
23028
23029         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
23030         true
23031 }
23032 run_test 300p "create striped directory without space"
23033
23034 test_300q() {
23035         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23036         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23037
23038         local fd=$(free_fd)
23039         local cmd="exec $fd<$tdir"
23040         cd $DIR
23041         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
23042         eval $cmd
23043         cmd="exec $fd<&-"
23044         trap "eval $cmd" EXIT
23045         cd $tdir || error "cd $tdir fails"
23046         rmdir  ../$tdir || error "rmdir $tdir fails"
23047         mkdir local_dir && error "create dir succeeds"
23048         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
23049         eval $cmd
23050         return 0
23051 }
23052 run_test 300q "create remote directory under orphan directory"
23053
23054 test_300r() {
23055         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23056                 skip "Need MDS version at least 2.7.55" && return
23057         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23058
23059         mkdir $DIR/$tdir
23060
23061         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
23062                 error "set striped dir error"
23063
23064         $LFS getdirstripe $DIR/$tdir/striped_dir ||
23065                 error "getstripeddir fails"
23066
23067         local stripe_count
23068         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
23069                       awk '/lmv_stripe_count:/ { print $2 }')
23070
23071         [ $MDSCOUNT -ne $stripe_count ] &&
23072                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
23073
23074         rm -rf $DIR/$tdir/striped_dir ||
23075                 error "unlink striped dir fails"
23076 }
23077 run_test 300r "test -1 striped directory"
23078
23079 test_300s_helper() {
23080         local count=$1
23081
23082         local stripe_dir=$DIR/$tdir/striped_dir.$count
23083
23084         $LFS mkdir -c $count $stripe_dir ||
23085                 error "lfs mkdir -c error"
23086
23087         $LFS getdirstripe $stripe_dir ||
23088                 error "lfs getdirstripe fails"
23089
23090         local stripe_count
23091         stripe_count=$($LFS getdirstripe $stripe_dir |
23092                       awk '/lmv_stripe_count:/ { print $2 }')
23093
23094         [ $count -ne $stripe_count ] &&
23095                 error_noexit "bad stripe count $stripe_count expected $count"
23096
23097         local dupe_stripes
23098         dupe_stripes=$($LFS getdirstripe $stripe_dir |
23099                 awk '/0x/ {count[$1] += 1}; END {
23100                         for (idx in count) {
23101                                 if (count[idx]>1) {
23102                                         print "index " idx " count " count[idx]
23103                                 }
23104                         }
23105                 }')
23106
23107         if [[ -n "$dupe_stripes" ]] ; then
23108                 lfs getdirstripe $stripe_dir
23109                 error_noexit "Dupe MDT above: $dupe_stripes "
23110         fi
23111
23112         rm -rf $stripe_dir ||
23113                 error_noexit "unlink $stripe_dir fails"
23114 }
23115
23116 test_300s() {
23117         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23118                 skip "Need MDS version at least 2.7.55" && return
23119         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23120
23121         mkdir $DIR/$tdir
23122         for count in $(seq 2 $MDSCOUNT); do
23123                 test_300s_helper $count
23124         done
23125 }
23126 run_test 300s "test lfs mkdir -c without -i"
23127
23128
23129 prepare_remote_file() {
23130         mkdir $DIR/$tdir/src_dir ||
23131                 error "create remote source failed"
23132
23133         cp /etc/hosts $DIR/$tdir/src_dir/a ||
23134                  error "cp to remote source failed"
23135         touch $DIR/$tdir/src_dir/a
23136
23137         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
23138                 error "create remote target dir failed"
23139
23140         touch $DIR/$tdir/tgt_dir/b
23141
23142         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
23143                 error "rename dir cross MDT failed!"
23144
23145         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
23146                 error "src_child still exists after rename"
23147
23148         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
23149                 error "missing file(a) after rename"
23150
23151         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
23152                 error "diff after rename"
23153 }
23154
23155 test_310a() {
23156         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
23157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23158
23159         local remote_file=$DIR/$tdir/tgt_dir/b
23160
23161         mkdir -p $DIR/$tdir
23162
23163         prepare_remote_file || error "prepare remote file failed"
23164
23165         #open-unlink file
23166         $OPENUNLINK $remote_file $remote_file ||
23167                 error "openunlink $remote_file failed"
23168         $CHECKSTAT -a $remote_file || error "$remote_file exists"
23169 }
23170 run_test 310a "open unlink remote file"
23171
23172 test_310b() {
23173         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
23174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23175
23176         local remote_file=$DIR/$tdir/tgt_dir/b
23177
23178         mkdir -p $DIR/$tdir
23179
23180         prepare_remote_file || error "prepare remote file failed"
23181
23182         ln $remote_file $DIR/$tfile || error "link failed for remote file"
23183         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
23184         $CHECKSTAT -t file $remote_file || error "check file failed"
23185 }
23186 run_test 310b "unlink remote file with multiple links while open"
23187
23188 test_310c() {
23189         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23190         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
23191
23192         local remote_file=$DIR/$tdir/tgt_dir/b
23193
23194         mkdir -p $DIR/$tdir
23195
23196         prepare_remote_file || error "prepare remote file failed"
23197
23198         ln $remote_file $DIR/$tfile || error "link failed for remote file"
23199         multiop_bg_pause $remote_file O_uc ||
23200                         error "mulitop failed for remote file"
23201         MULTIPID=$!
23202         $MULTIOP $DIR/$tfile Ouc
23203         kill -USR1 $MULTIPID
23204         wait $MULTIPID
23205 }
23206 run_test 310c "open-unlink remote file with multiple links"
23207
23208 #LU-4825
23209 test_311() {
23210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23211         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
23212         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
23213                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
23214         remote_mds_nodsh && skip "remote MDS with nodsh"
23215
23216         local old_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
23217         local mdts=$(comma_list $(mdts_nodes))
23218
23219         mkdir -p $DIR/$tdir
23220         $LFS setstripe -i 0 -c 1 $DIR/$tdir
23221         createmany -o $DIR/$tdir/$tfile. 1000
23222
23223         # statfs data is not real time, let's just calculate it
23224         old_iused=$((old_iused + 1000))
23225
23226         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23227                         osp.*OST0000*MDT0000.create_count")
23228         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23229                                 osp.*OST0000*MDT0000.max_create_count")
23230         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
23231
23232         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
23233         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
23234         [ $index -ne 0 ] || error "$tfile stripe index is 0"
23235
23236         unlinkmany $DIR/$tdir/$tfile. 1000
23237
23238         do_nodes $mdts "$LCTL set_param -n \
23239                         osp.*OST0000*.max_create_count=$max_count"
23240         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
23241                 do_nodes $mdts "$LCTL set_param -n \
23242                                 osp.*OST0000*.create_count=$count"
23243         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
23244                         grep "=0" && error "create_count is zero"
23245
23246         local new_iused
23247         for i in $(seq 120); do
23248                 new_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
23249                 # system may be too busy to destroy all objs in time, use
23250                 # a somewhat small value to not fail autotest
23251                 [ $((old_iused - new_iused)) -gt 400 ] && break
23252                 sleep 1
23253         done
23254
23255         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
23256         [ $((old_iused - new_iused)) -gt 400 ] ||
23257                 error "objs not destroyed after unlink"
23258 }
23259 run_test 311 "disable OSP precreate, and unlink should destroy objs"
23260
23261 zfs_oid_to_objid()
23262 {
23263         local ost=$1
23264         local objid=$2
23265
23266         local vdevdir=$(dirname $(facet_vdevice $ost))
23267         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
23268         local zfs_zapid=$(do_facet $ost $cmd |
23269                           grep -w "/O/0/d$((objid%32))" -C 5 |
23270                           awk '/Object/{getline; print $1}')
23271         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
23272                           awk "/$objid = /"'{printf $3}')
23273
23274         echo $zfs_objid
23275 }
23276
23277 zfs_object_blksz() {
23278         local ost=$1
23279         local objid=$2
23280
23281         local vdevdir=$(dirname $(facet_vdevice $ost))
23282         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
23283         local blksz=$(do_facet $ost $cmd $objid |
23284                       awk '/dblk/{getline; printf $4}')
23285
23286         case "${blksz: -1}" in
23287                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
23288                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
23289                 *) ;;
23290         esac
23291
23292         echo $blksz
23293 }
23294
23295 test_312() { # LU-4856
23296         remote_ost_nodsh && skip "remote OST with nodsh"
23297         [ "$ost1_FSTYPE" = "zfs" ] ||
23298                 skip_env "the test only applies to zfs"
23299
23300         local max_blksz=$(do_facet ost1 \
23301                           $ZFS get -p recordsize $(facet_device ost1) |
23302                           awk '!/VALUE/{print $3}')
23303
23304         # to make life a little bit easier
23305         $LFS mkdir -c 1 -i 0 $DIR/$tdir
23306         $LFS setstripe -c 1 -i 0 $DIR/$tdir
23307
23308         local tf=$DIR/$tdir/$tfile
23309         touch $tf
23310         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23311
23312         # Get ZFS object id
23313         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23314         # block size change by sequential overwrite
23315         local bs
23316
23317         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
23318                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
23319
23320                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
23321                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
23322         done
23323         rm -f $tf
23324
23325         # block size change by sequential append write
23326         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
23327         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23328         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23329         local count
23330
23331         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
23332                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
23333                         oflag=sync conv=notrunc
23334
23335                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
23336                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
23337                         error "blksz error, actual $blksz, " \
23338                                 "expected: 2 * $count * $PAGE_SIZE"
23339         done
23340         rm -f $tf
23341
23342         # random write
23343         touch $tf
23344         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23345         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23346
23347         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
23348         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23349         [ $blksz -eq $PAGE_SIZE ] ||
23350                 error "blksz error: $blksz, expected: $PAGE_SIZE"
23351
23352         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
23353         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23354         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
23355
23356         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
23357         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23358         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
23359 }
23360 run_test 312 "make sure ZFS adjusts its block size by write pattern"
23361
23362 test_313() {
23363         remote_ost_nodsh && skip "remote OST with nodsh"
23364
23365         local file=$DIR/$tfile
23366
23367         rm -f $file
23368         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
23369
23370         # define OBD_FAIL_TGT_RCVD_EIO           0x720
23371         do_facet ost1 "$LCTL set_param fail_loc=0x720"
23372         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
23373                 error "write should failed"
23374         do_facet ost1 "$LCTL set_param fail_loc=0"
23375         rm -f $file
23376 }
23377 run_test 313 "io should fail after last_rcvd update fail"
23378
23379 test_314() {
23380         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
23381
23382         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
23383         do_facet ost1 "$LCTL set_param fail_loc=0x720"
23384         rm -f $DIR/$tfile
23385         wait_delete_completed
23386         do_facet ost1 "$LCTL set_param fail_loc=0"
23387 }
23388 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
23389
23390 test_315() { # LU-618
23391         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
23392
23393         local file=$DIR/$tfile
23394         rm -f $file
23395
23396         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
23397                 error "multiop file write failed"
23398         $MULTIOP $file oO_RDONLY:r4063232_c &
23399         PID=$!
23400
23401         sleep 2
23402
23403         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
23404         kill -USR1 $PID
23405
23406         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
23407         rm -f $file
23408 }
23409 run_test 315 "read should be accounted"
23410
23411 test_316() {
23412         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
23413         large_xattr_enabled || skip_env "ea_inode feature disabled"
23414
23415         rm -rf $DIR/$tdir/d
23416         mkdir -p $DIR/$tdir/d
23417         chown nobody $DIR/$tdir/d
23418         touch $DIR/$tdir/d/file
23419
23420         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
23421 }
23422 run_test 316 "lfs mv"
23423
23424 test_317() {
23425         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
23426                 skip "Need MDS version at least 2.11.53"
23427         if [ "$ost1_FSTYPE" == "zfs" ]; then
23428                 skip "LU-10370: no implementation for ZFS"
23429         fi
23430
23431         local trunc_sz
23432         local grant_blk_size
23433
23434         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
23435                         awk '/grant_block_size:/ { print $2; exit; }')
23436         #
23437         # Create File of size 5M. Truncate it to below size's and verify
23438         # blocks count.
23439         #
23440         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
23441                 error "Create file $DIR/$tfile failed"
23442         stack_trap "rm -f $DIR/$tfile" EXIT
23443
23444         for trunc_sz in 2097152 4097 4000 509 0; do
23445                 $TRUNCATE $DIR/$tfile $trunc_sz ||
23446                         error "truncate $tfile to $trunc_sz failed"
23447                 local sz=$(stat --format=%s $DIR/$tfile)
23448                 local blk=$(stat --format=%b $DIR/$tfile)
23449                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
23450                                      grant_blk_size) * 8))
23451
23452                 if [[ $blk -ne $trunc_blk ]]; then
23453                         $(which stat) $DIR/$tfile
23454                         error "Expected Block $trunc_blk got $blk for $tfile"
23455                 fi
23456
23457                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
23458                         error "Expected Size $trunc_sz got $sz for $tfile"
23459         done
23460
23461         #
23462         # sparse file test
23463         # Create file with a hole and write actual two blocks. Block count
23464         # must be 16.
23465         #
23466         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
23467                 conv=fsync || error "Create file : $DIR/$tfile"
23468
23469         # Calculate the final truncate size.
23470         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
23471
23472         #
23473         # truncate to size $trunc_sz bytes. Strip the last block
23474         # The block count must drop to 8
23475         #
23476         $TRUNCATE $DIR/$tfile $trunc_sz ||
23477                 error "truncate $tfile to $trunc_sz failed"
23478
23479         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
23480         sz=$(stat --format=%s $DIR/$tfile)
23481         blk=$(stat --format=%b $DIR/$tfile)
23482
23483         if [[ $blk -ne $trunc_bsz ]]; then
23484                 $(which stat) $DIR/$tfile
23485                 error "Expected Block $trunc_bsz got $blk for $tfile"
23486         fi
23487
23488         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
23489                 error "Expected Size $trunc_sz got $sz for $tfile"
23490 }
23491 run_test 317 "Verify blocks get correctly update after truncate"
23492
23493 test_318() {
23494         local llite_name="llite.$($LFS getname $MOUNT | awk '{print $1}')"
23495         local old_max_active=$($LCTL get_param -n \
23496                             ${llite_name}.max_read_ahead_async_active \
23497                             2>/dev/null)
23498
23499         $LCTL set_param llite.*.max_read_ahead_async_active=256
23500         local max_active=$($LCTL get_param -n \
23501                            ${llite_name}.max_read_ahead_async_active \
23502                            2>/dev/null)
23503         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
23504
23505         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
23506                 error "set max_read_ahead_async_active should succeed"
23507
23508         $LCTL set_param llite.*.max_read_ahead_async_active=512
23509         max_active=$($LCTL get_param -n \
23510                      ${llite_name}.max_read_ahead_async_active 2>/dev/null)
23511         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
23512
23513         # restore @max_active
23514         [ $old_max_active -ne 0 ] && $LCTL set_param \
23515                 llite.*.max_read_ahead_async_active=$old_max_active
23516
23517         local old_threshold=$($LCTL get_param -n \
23518                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
23519         local max_per_file_mb=$($LCTL get_param -n \
23520                 ${llite_name}.max_read_ahead_per_file_mb 2>/dev/null)
23521
23522         local invalid=$(($max_per_file_mb + 1))
23523         $LCTL set_param \
23524                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
23525                         && error "set $invalid should fail"
23526
23527         local valid=$(($invalid - 1))
23528         $LCTL set_param \
23529                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
23530                         error "set $valid should succeed"
23531         local threshold=$($LCTL get_param -n \
23532                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
23533         [ $threshold -eq $valid ] || error \
23534                 "expect threshold $valid got $threshold"
23535         $LCTL set_param \
23536                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
23537 }
23538 run_test 318 "Verify async readahead tunables"
23539
23540 test_319() {
23541         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
23542
23543         local before=$(date +%s)
23544         local evict
23545         local mdir=$DIR/$tdir
23546         local file=$mdir/xxx
23547
23548         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
23549         touch $file
23550
23551 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
23552         $LCTL set_param fail_val=5 fail_loc=0x8000032c
23553         $LFS mv -m1 $file &
23554
23555         sleep 1
23556         dd if=$file of=/dev/null
23557         wait
23558         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
23559           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
23560
23561         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
23562 }
23563 run_test 319 "lost lease lock on migrate error"
23564
23565 test_398a() { # LU-4198
23566         local ost1_imp=$(get_osc_import_name client ost1)
23567         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23568                          cut -d'.' -f2)
23569
23570         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23571         $LCTL set_param ldlm.namespaces.*.lru_size=clear
23572
23573         # request a new lock on client
23574         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23575
23576         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
23577         local lock_count=$($LCTL get_param -n \
23578                            ldlm.namespaces.$imp_name.lru_size)
23579         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
23580
23581         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
23582
23583         # no lock cached, should use lockless IO and not enqueue new lock
23584         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
23585         lock_count=$($LCTL get_param -n \
23586                      ldlm.namespaces.$imp_name.lru_size)
23587         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
23588 }
23589 run_test 398a "direct IO should cancel lock otherwise lockless"
23590
23591 test_398b() { # LU-4198
23592         which fio || skip_env "no fio installed"
23593         $LFS setstripe -c -1 $DIR/$tfile
23594
23595         local size=12
23596         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
23597
23598         local njobs=4
23599         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
23600         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
23601                 --numjobs=$njobs --fallocate=none \
23602                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23603                 --filename=$DIR/$tfile &
23604         bg_pid=$!
23605
23606         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
23607         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
23608                 --numjobs=$njobs --fallocate=none \
23609                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23610                 --filename=$DIR/$tfile || true
23611         wait $bg_pid
23612
23613         rm -f $DIR/$tfile
23614 }
23615 run_test 398b "DIO and buffer IO race"
23616
23617 test_398c() { # LU-4198
23618         local ost1_imp=$(get_osc_import_name client ost1)
23619         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23620                          cut -d'.' -f2)
23621
23622         which fio || skip_env "no fio installed"
23623
23624         saved_debug=$($LCTL get_param -n debug)
23625         $LCTL set_param debug=0
23626
23627         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
23628         ((size /= 1024)) # by megabytes
23629         ((size /= 2)) # write half of the OST at most
23630         [ $size -gt 40 ] && size=40 #reduce test time anyway
23631
23632         $LFS setstripe -c 1 $DIR/$tfile
23633
23634         # it seems like ldiskfs reserves more space than necessary if the
23635         # writing blocks are not mapped, so it extends the file firstly
23636         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
23637         cancel_lru_locks osc
23638
23639         # clear and verify rpc_stats later
23640         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
23641
23642         local njobs=4
23643         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
23644         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
23645                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
23646                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23647                 --filename=$DIR/$tfile
23648         [ $? -eq 0 ] || error "fio write error"
23649
23650         [ $($LCTL get_param -n ldlm.namespaces.$imp_name.lock_count) -eq 0 ] ||
23651                 error "Locks were requested while doing AIO"
23652
23653         # get the percentage of 1-page I/O
23654         pct=$($LCTL get_param osc.${imp_name}.rpc_stats |
23655                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
23656                 awk '{print $7}')
23657         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
23658
23659         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
23660         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
23661                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
23662                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23663                 --filename=$DIR/$tfile
23664         [ $? -eq 0 ] || error "fio mixed read write error"
23665
23666         echo "AIO with large block size ${size}M"
23667         fio --name=rand-rw --rw=randrw --bs=${size}M --direct=1 \
23668                 --numjobs=1 --fallocate=none --ioengine=libaio \
23669                 --iodepth=16 --allow_file_create=0 --size=${size}M \
23670                 --filename=$DIR/$tfile
23671         [ $? -eq 0 ] || error "fio large block size failed"
23672
23673         rm -f $DIR/$tfile
23674         $LCTL set_param debug="$saved_debug"
23675 }
23676 run_test 398c "run fio to test AIO"
23677
23678 test_398d() { #  LU-13846
23679         which aiocp || skip_env "no aiocp installed"
23680         local aio_file=$DIR/$tfile.aio
23681
23682         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
23683
23684         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=64
23685         aiocp -a $PAGE_SIZE -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file
23686         stack_trap "rm -f $DIR/$tfile $aio_file"
23687
23688         diff $DIR/$tfile $aio_file || error "file diff after aiocp"
23689
23690         # make sure we don't crash and fail properly
23691         aiocp -a 512 -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
23692                 error "aio not aligned with PAGE SIZE should fail"
23693
23694         rm -f $DIR/$tfile $aio_file
23695 }
23696 run_test 398d "run aiocp to verify block size > stripe size"
23697
23698 test_398e() {
23699         dd if=/dev/zero of=$DIR/$tfile bs=1234 count=1
23700         touch $DIR/$tfile.new
23701         dd if=$DIR/$tfile of=$DIR/$tfile.new bs=1M count=1 oflag=direct
23702 }
23703 run_test 398e "O_Direct open cleared by fcntl doesn't cause hang"
23704
23705 test_398f() { #  LU-14687
23706         which aiocp || skip_env "no aiocp installed"
23707         local aio_file=$DIR/$tfile.aio
23708
23709         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
23710
23711         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
23712         stack_trap "rm -f $DIR/$tfile $aio_file"
23713
23714         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
23715         $LCTL set_param fail_loc=0x1418
23716         # make sure we don't crash and fail properly
23717         aiocp -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
23718                 error "aio with page allocation failure succeeded"
23719         $LCTL set_param fail_loc=0
23720         diff $DIR/$tfile $aio_file
23721         [[ $? != 0 ]] || error "no diff after failed aiocp"
23722 }
23723 run_test 398f "verify aio handles ll_direct_rw_pages errors correctly"
23724
23725 # NB: To get the parallel DIO behavior in LU-13798, there must be > 1
23726 # stripe and i/o size must be > stripe size
23727 # Old style synchronous DIO waits after submitting each chunk, resulting in a
23728 # single RPC in flight.  This test shows async DIO submission is working by
23729 # showing multiple RPCs in flight.
23730 test_398g() { #  LU-13798
23731         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
23732
23733         # We need to do some i/o first to acquire enough grant to put our RPCs
23734         # in flight; otherwise a new connection may not have enough grant
23735         # available
23736         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
23737                 error "parallel dio failed"
23738         stack_trap "rm -f $DIR/$tfile"
23739
23740         # Reduce RPC size to 1M to avoid combination in to larger RPCs
23741         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
23742         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
23743         stack_trap "$LCTL set_param -n $pages_per_rpc"
23744
23745         # Recreate file so it's empty
23746         rm -f $DIR/$tfile
23747         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
23748         #Pause rpc completion to guarantee we see multiple rpcs in flight
23749         #define OBD_FAIL_OST_BRW_PAUSE_BULK
23750         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
23751         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
23752
23753         # Clear rpc stats
23754         $LCTL set_param osc.*.rpc_stats=c
23755
23756         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
23757                 error "parallel dio failed"
23758         stack_trap "rm -f $DIR/$tfile"
23759
23760         $LCTL get_param osc.*-OST0000-*.rpc_stats
23761         pct=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
23762                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
23763                 grep "8:" | awk '{print $8}')
23764         # We look at the "8 rpcs in flight" field, and verify A) it is present
23765         # and B) it includes all RPCs.  This proves we had 8 RPCs in flight,
23766         # as expected for an 8M DIO to a file with 1M stripes.
23767         [ $pct -eq 100 ] || error "we should see 8 RPCs in flight"
23768
23769         # Verify turning off parallel dio works as expected
23770         # Clear rpc stats
23771         $LCTL set_param osc.*.rpc_stats=c
23772         $LCTL set_param llite.*.parallel_dio=0
23773         stack_trap '$LCTL set_param llite.*.parallel_dio=1'
23774
23775         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
23776                 error "dio with parallel dio disabled failed"
23777
23778         # Ideally, we would see only one RPC in flight here, but there is an
23779         # unavoidable race between i/o completion and RPC in flight counting,
23780         # so while only 1 i/o is in flight at a time, the RPC in flight counter
23781         # will sometimes exceed 1 (3 or 4 is not rare on VM testing).
23782         # So instead we just verify it's always < 8.
23783         $LCTL get_param osc.*-OST0000-*.rpc_stats
23784         ret=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
23785                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
23786                 grep '^$' -B1 | grep . | awk '{print $1}')
23787         [ $ret != "8:" ] ||
23788                 error "we should see fewer than 8 RPCs in flight (saw $ret)"
23789 }
23790 run_test 398g "verify parallel dio async RPC submission"
23791
23792 test_398h() { #  LU-13798
23793         local dio_file=$DIR/$tfile.dio
23794
23795         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
23796
23797         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
23798         stack_trap "rm -f $DIR/$tfile $dio_file"
23799
23800         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct ||
23801                 error "parallel dio failed"
23802         diff $DIR/$tfile $dio_file
23803         [[ $? == 0 ]] || error "file diff after aiocp"
23804 }
23805 run_test 398h "verify correctness of read & write with i/o size >> stripe size"
23806
23807 test_398i() { #  LU-13798
23808         local dio_file=$DIR/$tfile.dio
23809
23810         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
23811
23812         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
23813         stack_trap "rm -f $DIR/$tfile $dio_file"
23814
23815         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
23816         $LCTL set_param fail_loc=0x1418
23817         # make sure we don't crash and fail properly
23818         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct &&
23819                 error "parallel dio page allocation failure succeeded"
23820         diff $DIR/$tfile $dio_file
23821         [[ $? != 0 ]] || error "no diff after failed aiocp"
23822 }
23823 run_test 398i "verify parallel dio handles ll_direct_rw_pages errors correctly"
23824
23825 test_398j() { #  LU-13798
23826         # Stripe size > RPC size but less than i/o size tests split across
23827         # stripes and RPCs for individual i/o op
23828         $LFS setstripe -o 0,0 -S 4M $DIR/$tfile $DIR/$tfile.2
23829
23830         # Reduce RPC size to 1M to guarantee split to multiple RPCs per stripe
23831         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
23832         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
23833         stack_trap "$LCTL set_param -n $pages_per_rpc"
23834
23835         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
23836                 error "parallel dio write failed"
23837         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.2"
23838
23839         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct ||
23840                 error "parallel dio read failed"
23841         diff $DIR/$tfile $DIR/$tfile.2
23842         [[ $? == 0 ]] || error "file diff after parallel dio read"
23843 }
23844 run_test 398j "test parallel dio where stripe size > rpc_size"
23845
23846 test_398k() { #  LU-13798
23847         wait_delete_completed
23848         wait_mds_ost_sync
23849
23850         # 4 stripe file; we will cause out of space on OST0
23851         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
23852
23853         # Fill OST0 (if it's not too large)
23854         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
23855                    head -n1)
23856         if [[ $ORIGFREE -gt $MAXFREE ]]; then
23857                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
23858         fi
23859         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
23860         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
23861                 error "dd should fill OST0"
23862         stack_trap "rm -f $DIR/$tfile.1"
23863
23864         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
23865         err=$?
23866
23867         ls -la $DIR/$tfile
23868         $CHECKSTAT -t file -s 0 $DIR/$tfile ||
23869                 error "file is not 0 bytes in size"
23870
23871         # dd above should not succeed, but don't error until here so we can
23872         # get debug info above
23873         [[ $err != 0 ]] ||
23874                 error "parallel dio write with enospc succeeded"
23875         stack_trap "rm -f $DIR/$tfile"
23876 }
23877 run_test 398k "test enospc on first stripe"
23878
23879 test_398l() { #  LU-13798
23880         wait_delete_completed
23881         wait_mds_ost_sync
23882
23883         # 4 stripe file; we will cause out of space on OST0
23884         # Note the 1M stripe size and the > 1M i/o size mean this ENOSPC
23885         # happens on the second i/o chunk we issue
23886         $LFS setstripe -o 1,0,1,0 -S 1M $DIR/$tfile $DIR/$tfile.2
23887
23888         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=2 oflag=direct
23889         stack_trap "rm -f $DIR/$tfile"
23890
23891         # Fill OST0 (if it's not too large)
23892         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
23893                    head -n1)
23894         if [[ $ORIGFREE -gt $MAXFREE ]]; then
23895                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
23896         fi
23897         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
23898         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
23899                 error "dd should fill OST0"
23900         stack_trap "rm -f $DIR/$tfile.1"
23901
23902         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 oflag=direct
23903         err=$?
23904         stack_trap "rm -f $DIR/$tfile.2"
23905
23906         # Check that short write completed as expected
23907         ls -la $DIR/$tfile.2
23908         $CHECKSTAT -t file -s 1048576 $DIR/$tfile.2 ||
23909                 error "file is not 1M in size"
23910
23911         # dd above should not succeed, but don't error until here so we can
23912         # get debug info above
23913         [[ $err != 0 ]] ||
23914                 error "parallel dio write with enospc succeeded"
23915
23916         # Truncate source file to same length as output file and diff them
23917         $TRUNCATE $DIR/$tfile 1048576
23918         diff $DIR/$tfile $DIR/$tfile.2
23919         [[ $? == 0 ]] || error "data incorrect after short write"
23920 }
23921 run_test 398l "test enospc on intermediate stripe/RPC"
23922
23923 test_398m() { #  LU-13798
23924         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
23925
23926         # Set up failure on OST0, the first stripe:
23927         #define OBD_FAIL_OST_BRW_WRITE_BULK     0x20e
23928         #NB: Fail val is ost # + 1, because we cannot use cfs_fail_val = 0
23929         # So this fail_val specifies OST0
23930         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=1
23931         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
23932
23933         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
23934                 error "parallel dio write with failure on first stripe succeeded"
23935         stack_trap "rm -f $DIR/$tfile"
23936         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
23937
23938         # Place data in file for read
23939         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
23940                 error "parallel dio write failed"
23941
23942         # Fail read on OST0, first stripe
23943         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
23944         do_facet ost1 $LCTL set_param fail_loc=0x20f fail_val=1
23945         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
23946                 error "parallel dio read with error on first stripe succeeded"
23947         rm -f $DIR/$tfile.2
23948         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
23949
23950         # Switch to testing on OST1, second stripe
23951         # Clear file contents, maintain striping
23952         echo > $DIR/$tfile
23953         # Set up failure on OST1, second stripe:
23954         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=2
23955         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
23956
23957         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
23958                 error "parallel dio write with failure on first stripe succeeded"
23959         stack_trap "rm -f $DIR/$tfile"
23960         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
23961
23962         # Place data in file for read
23963         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
23964                 error "parallel dio write failed"
23965
23966         # Fail read on OST1, second stripe
23967         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
23968         do_facet ost2 $LCTL set_param fail_loc=0x20f fail_val=2
23969         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
23970                 error "parallel dio read with error on first stripe succeeded"
23971         rm -f $DIR/$tfile.2
23972         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
23973 }
23974 run_test 398m "test RPC failures with parallel dio"
23975
23976 # Parallel submission of DIO should not cause problems for append, but it's
23977 # important to verify.
23978 test_398n() { #  LU-13798
23979         $LFS setstripe -C 2 -S 1M $DIR/$tfile
23980
23981         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 ||
23982                 error "dd to create source file failed"
23983         stack_trap "rm -f $DIR/$tfile"
23984
23985         dd if=$DIR/$tfile of=$DIR/$tfile.1 bs=8M count=8 oflag=direct oflag=append ||
23986                 error "parallel dio write with failure on second stripe succeeded"
23987         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.1"
23988         diff $DIR/$tfile $DIR/$tfile.1
23989         [[ $? == 0 ]] || error "data incorrect after append"
23990
23991 }
23992 run_test 398n "test append with parallel DIO"
23993
23994 test_fake_rw() {
23995         local read_write=$1
23996         if [ "$read_write" = "write" ]; then
23997                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
23998         elif [ "$read_write" = "read" ]; then
23999                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
24000         else
24001                 error "argument error"
24002         fi
24003
24004         # turn off debug for performance testing
24005         local saved_debug=$($LCTL get_param -n debug)
24006         $LCTL set_param debug=0
24007
24008         $LFS setstripe -c 1 -i 0 $DIR/$tfile
24009
24010         # get ost1 size - $FSNAME-OST0000
24011         local ost1_avail_size=$($LFS df $DIR | awk /${ost1_svc}/'{ print $4 }')
24012         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
24013         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
24014
24015         if [ "$read_write" = "read" ]; then
24016                 $TRUNCATE $DIR/$tfile $(expr 1048576 \* $blocks)
24017         fi
24018
24019         local start_time=$(date +%s.%N)
24020         $dd_cmd bs=1M count=$blocks oflag=sync ||
24021                 error "real dd $read_write error"
24022         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
24023
24024         if [ "$read_write" = "write" ]; then
24025                 rm -f $DIR/$tfile
24026         fi
24027
24028         # define OBD_FAIL_OST_FAKE_RW           0x238
24029         do_facet ost1 $LCTL set_param fail_loc=0x238
24030
24031         local start_time=$(date +%s.%N)
24032         $dd_cmd bs=1M count=$blocks oflag=sync ||
24033                 error "fake dd $read_write error"
24034         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
24035
24036         if [ "$read_write" = "write" ]; then
24037                 # verify file size
24038                 cancel_lru_locks osc
24039                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
24040                         error "$tfile size not $blocks MB"
24041         fi
24042         do_facet ost1 $LCTL set_param fail_loc=0
24043
24044         echo "fake $read_write $duration_fake vs. normal $read_write" \
24045                 "$duration in seconds"
24046         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
24047                 error_not_in_vm "fake write is slower"
24048
24049         $LCTL set_param -n debug="$saved_debug"
24050         rm -f $DIR/$tfile
24051 }
24052 test_399a() { # LU-7655 for OST fake write
24053         remote_ost_nodsh && skip "remote OST with nodsh"
24054
24055         test_fake_rw write
24056 }
24057 run_test 399a "fake write should not be slower than normal write"
24058
24059 test_399b() { # LU-8726 for OST fake read
24060         remote_ost_nodsh && skip "remote OST with nodsh"
24061         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
24062                 skip_env "ldiskfs only test"
24063         fi
24064
24065         test_fake_rw read
24066 }
24067 run_test 399b "fake read should not be slower than normal read"
24068
24069 test_400a() { # LU-1606, was conf-sanity test_74
24070         if ! which $CC > /dev/null 2>&1; then
24071                 skip_env "$CC is not installed"
24072         fi
24073
24074         local extra_flags=''
24075         local out=$TMP/$tfile
24076         local prefix=/usr/include/lustre
24077         local prog
24078
24079         # Oleg removes c files in his test rig so test if any c files exist
24080         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
24081                 skip_env "Needed c test files are missing"
24082
24083         if ! [[ -d $prefix ]]; then
24084                 # Assume we're running in tree and fixup the include path.
24085                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
24086                 extra_flags+=" -L$LUSTRE/utils/.lib"
24087         fi
24088
24089         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
24090                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
24091                         error "client api broken"
24092         done
24093         rm -f $out
24094 }
24095 run_test 400a "Lustre client api program can compile and link"
24096
24097 test_400b() { # LU-1606, LU-5011
24098         local header
24099         local out=$TMP/$tfile
24100         local prefix=/usr/include/linux/lustre
24101
24102         # We use a hard coded prefix so that this test will not fail
24103         # when run in tree. There are headers in lustre/include/lustre/
24104         # that are not packaged (like lustre_idl.h) and have more
24105         # complicated include dependencies (like config.h and lnet/types.h).
24106         # Since this test about correct packaging we just skip them when
24107         # they don't exist (see below) rather than try to fixup cppflags.
24108
24109         if ! which $CC > /dev/null 2>&1; then
24110                 skip_env "$CC is not installed"
24111         fi
24112
24113         for header in $prefix/*.h; do
24114                 if ! [[ -f "$header" ]]; then
24115                         continue
24116                 fi
24117
24118                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
24119                         continue # lustre_ioctl.h is internal header
24120                 fi
24121
24122                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
24123                         error "cannot compile '$header'"
24124         done
24125         rm -f $out
24126 }
24127 run_test 400b "packaged headers can be compiled"
24128
24129 test_401a() { #LU-7437
24130         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
24131         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
24132
24133         #count the number of parameters by "list_param -R"
24134         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
24135         #count the number of parameters by listing proc files
24136         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
24137         echo "proc_dirs='$proc_dirs'"
24138         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
24139         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
24140                       sort -u | wc -l)
24141
24142         [ $params -eq $procs ] ||
24143                 error "found $params parameters vs. $procs proc files"
24144
24145         # test the list_param -D option only returns directories
24146         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
24147         #count the number of parameters by listing proc directories
24148         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
24149                 sort -u | wc -l)
24150
24151         [ $params -eq $procs ] ||
24152                 error "found $params parameters vs. $procs proc files"
24153 }
24154 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
24155
24156 test_401b() {
24157         # jobid_var may not allow arbitrary values, so use jobid_name
24158         # if available
24159         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24160                 local testname=jobid_name tmp='testing%p'
24161         else
24162                 local testname=jobid_var tmp=testing
24163         fi
24164
24165         local save=$($LCTL get_param -n $testname)
24166
24167         $LCTL set_param foo=bar $testname=$tmp bar=baz &&
24168                 error "no error returned when setting bad parameters"
24169
24170         local jobid_new=$($LCTL get_param -n foe $testname baz)
24171         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
24172
24173         $LCTL set_param -n fog=bam $testname=$save bat=fog
24174         local jobid_old=$($LCTL get_param -n foe $testname bag)
24175         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
24176 }
24177 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
24178
24179 test_401c() {
24180         # jobid_var may not allow arbitrary values, so use jobid_name
24181         # if available
24182         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24183                 local testname=jobid_name
24184         else
24185                 local testname=jobid_var
24186         fi
24187
24188         local jobid_var_old=$($LCTL get_param -n $testname)
24189         local jobid_var_new
24190
24191         $LCTL set_param $testname= &&
24192                 error "no error returned for 'set_param a='"
24193
24194         jobid_var_new=$($LCTL get_param -n $testname)
24195         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
24196                 error "$testname was changed by setting without value"
24197
24198         $LCTL set_param $testname &&
24199                 error "no error returned for 'set_param a'"
24200
24201         jobid_var_new=$($LCTL get_param -n $testname)
24202         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
24203                 error "$testname was changed by setting without value"
24204 }
24205 run_test 401c "Verify 'lctl set_param' without value fails in either format."
24206
24207 test_401d() {
24208         # jobid_var may not allow arbitrary values, so use jobid_name
24209         # if available
24210         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24211                 local testname=jobid_name new_value='foo=bar%p'
24212         else
24213                 local testname=jobid_var new_valuie=foo=bar
24214         fi
24215
24216         local jobid_var_old=$($LCTL get_param -n $testname)
24217         local jobid_var_new
24218
24219         $LCTL set_param $testname=$new_value ||
24220                 error "'set_param a=b' did not accept a value containing '='"
24221
24222         jobid_var_new=$($LCTL get_param -n $testname)
24223         [[ "$jobid_var_new" == "$new_value" ]] ||
24224                 error "'set_param a=b' failed on a value containing '='"
24225
24226         # Reset the $testname to test the other format
24227         $LCTL set_param $testname=$jobid_var_old
24228         jobid_var_new=$($LCTL get_param -n $testname)
24229         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
24230                 error "failed to reset $testname"
24231
24232         $LCTL set_param $testname $new_value ||
24233                 error "'set_param a b' did not accept a value containing '='"
24234
24235         jobid_var_new=$($LCTL get_param -n $testname)
24236         [[ "$jobid_var_new" == "$new_value" ]] ||
24237                 error "'set_param a b' failed on a value containing '='"
24238
24239         $LCTL set_param $testname $jobid_var_old
24240         jobid_var_new=$($LCTL get_param -n $testname)
24241         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
24242                 error "failed to reset $testname"
24243 }
24244 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
24245
24246 test_401e() { # LU-14779
24247         $LCTL list_param -R "ldlm.namespaces.MGC*" ||
24248                 error "lctl list_param MGC* failed"
24249         $LCTL get_param "ldlm.namespaces.MGC*" || error "lctl get_param failed"
24250         $LCTL get_param "ldlm.namespaces.MGC*.lru_size" ||
24251                 error "lctl get_param lru_size failed"
24252 }
24253 run_test 401e "verify 'lctl get_param' works with NID in parameter"
24254
24255 test_402() {
24256         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
24257         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
24258                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
24259         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
24260                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
24261                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
24262         remote_mds_nodsh && skip "remote MDS with nodsh"
24263
24264         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
24265 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
24266         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
24267         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
24268                 echo "Touch failed - OK"
24269 }
24270 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
24271
24272 test_403() {
24273         local file1=$DIR/$tfile.1
24274         local file2=$DIR/$tfile.2
24275         local tfile=$TMP/$tfile
24276
24277         rm -f $file1 $file2 $tfile
24278
24279         touch $file1
24280         ln $file1 $file2
24281
24282         # 30 sec OBD_TIMEOUT in ll_getattr()
24283         # right before populating st_nlink
24284         $LCTL set_param fail_loc=0x80001409
24285         stat -c %h $file1 > $tfile &
24286
24287         # create an alias, drop all locks and reclaim the dentry
24288         < $file2
24289         cancel_lru_locks mdc
24290         cancel_lru_locks osc
24291         sysctl -w vm.drop_caches=2
24292
24293         wait
24294
24295         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
24296
24297         rm -f $tfile $file1 $file2
24298 }
24299 run_test 403 "i_nlink should not drop to zero due to aliasing"
24300
24301 test_404() { # LU-6601
24302         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
24303                 skip "Need server version newer than 2.8.52"
24304         remote_mds_nodsh && skip "remote MDS with nodsh"
24305
24306         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
24307                 awk '/osp .*-osc-MDT/ { print $4}')
24308
24309         local osp
24310         for osp in $mosps; do
24311                 echo "Deactivate: " $osp
24312                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
24313                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
24314                         awk -vp=$osp '$4 == p { print $2 }')
24315                 [ $stat = IN ] || {
24316                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
24317                         error "deactivate error"
24318                 }
24319                 echo "Activate: " $osp
24320                 do_facet $SINGLEMDS $LCTL --device %$osp activate
24321                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
24322                         awk -vp=$osp '$4 == p { print $2 }')
24323                 [ $stat = UP ] || {
24324                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
24325                         error "activate error"
24326                 }
24327         done
24328 }
24329 run_test 404 "validate manual {de}activated works properly for OSPs"
24330
24331 test_405() {
24332         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
24333         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
24334                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
24335                         skip "Layout swap lock is not supported"
24336
24337         check_swap_layouts_support
24338         check_swap_layout_no_dom $DIR
24339
24340         test_mkdir $DIR/$tdir
24341         swap_lock_test -d $DIR/$tdir ||
24342                 error "One layout swap locked test failed"
24343 }
24344 run_test 405 "Various layout swap lock tests"
24345
24346 test_406() {
24347         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24348         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
24349         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
24350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24351         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
24352                 skip "Need MDS version at least 2.8.50"
24353
24354         local def_stripe_size=$($LFS getstripe -S $MOUNT)
24355         local test_pool=$TESTNAME
24356
24357         pool_add $test_pool || error "pool_add failed"
24358         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
24359                 error "pool_add_targets failed"
24360
24361         save_layout_restore_at_exit $MOUNT
24362
24363         # parent set default stripe count only, child will stripe from both
24364         # parent and fs default
24365         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
24366                 error "setstripe $MOUNT failed"
24367         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
24368         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
24369         for i in $(seq 10); do
24370                 local f=$DIR/$tdir/$tfile.$i
24371                 touch $f || error "touch failed"
24372                 local count=$($LFS getstripe -c $f)
24373                 [ $count -eq $OSTCOUNT ] ||
24374                         error "$f stripe count $count != $OSTCOUNT"
24375                 local offset=$($LFS getstripe -i $f)
24376                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
24377                 local size=$($LFS getstripe -S $f)
24378                 [ $size -eq $((def_stripe_size * 2)) ] ||
24379                         error "$f stripe size $size != $((def_stripe_size * 2))"
24380                 local pool=$($LFS getstripe -p $f)
24381                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
24382         done
24383
24384         # change fs default striping, delete parent default striping, now child
24385         # will stripe from new fs default striping only
24386         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
24387                 error "change $MOUNT default stripe failed"
24388         $LFS setstripe -c 0 $DIR/$tdir ||
24389                 error "delete $tdir default stripe failed"
24390         for i in $(seq 11 20); do
24391                 local f=$DIR/$tdir/$tfile.$i
24392                 touch $f || error "touch $f failed"
24393                 local count=$($LFS getstripe -c $f)
24394                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
24395                 local offset=$($LFS getstripe -i $f)
24396                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
24397                 local size=$($LFS getstripe -S $f)
24398                 [ $size -eq $def_stripe_size ] ||
24399                         error "$f stripe size $size != $def_stripe_size"
24400                 local pool=$($LFS getstripe -p $f)
24401                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
24402         done
24403
24404         unlinkmany $DIR/$tdir/$tfile. 1 20
24405
24406         local f=$DIR/$tdir/$tfile
24407         pool_remove_all_targets $test_pool $f
24408         pool_remove $test_pool $f
24409 }
24410 run_test 406 "DNE support fs default striping"
24411
24412 test_407() {
24413         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24414         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
24415                 skip "Need MDS version at least 2.8.55"
24416         remote_mds_nodsh && skip "remote MDS with nodsh"
24417
24418         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
24419                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
24420         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
24421                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
24422         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
24423
24424         #define OBD_FAIL_DT_TXN_STOP    0x2019
24425         for idx in $(seq $MDSCOUNT); do
24426                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
24427         done
24428         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
24429         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
24430                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
24431         true
24432 }
24433 run_test 407 "transaction fail should cause operation fail"
24434
24435 test_408() {
24436         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
24437
24438         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
24439         lctl set_param fail_loc=0x8000040a
24440         # let ll_prepare_partial_page() fail
24441         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
24442
24443         rm -f $DIR/$tfile
24444
24445         # create at least 100 unused inodes so that
24446         # shrink_icache_memory(0) should not return 0
24447         touch $DIR/$tfile-{0..100}
24448         rm -f $DIR/$tfile-{0..100}
24449         sync
24450
24451         echo 2 > /proc/sys/vm/drop_caches
24452 }
24453 run_test 408 "drop_caches should not hang due to page leaks"
24454
24455 test_409()
24456 {
24457         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
24458
24459         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
24460         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
24461         touch $DIR/$tdir/guard || error "(2) Fail to create"
24462
24463         local PREFIX=$(str_repeat 'A' 128)
24464         echo "Create 1K hard links start at $(date)"
24465         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
24466                 error "(3) Fail to hard link"
24467
24468         echo "Links count should be right although linkEA overflow"
24469         stat $DIR/$tdir/guard || error "(4) Fail to stat"
24470         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
24471         [ $linkcount -eq 1001 ] ||
24472                 error "(5) Unexpected hard links count: $linkcount"
24473
24474         echo "List all links start at $(date)"
24475         ls -l $DIR/$tdir/foo > /dev/null ||
24476                 error "(6) Fail to list $DIR/$tdir/foo"
24477
24478         echo "Unlink hard links start at $(date)"
24479         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
24480                 error "(7) Fail to unlink"
24481         echo "Unlink hard links finished at $(date)"
24482 }
24483 run_test 409 "Large amount of cross-MDTs hard links on the same file"
24484
24485 test_410()
24486 {
24487         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
24488                 skip "Need client version at least 2.9.59"
24489         [ -f $LUSTRE/tests/kernel/kinode.ko ] ||
24490                 skip "Need MODULES build"
24491
24492         # Create a file, and stat it from the kernel
24493         local testfile=$DIR/$tfile
24494         touch $testfile
24495
24496         local run_id=$RANDOM
24497         local my_ino=$(stat --format "%i" $testfile)
24498
24499         # Try to insert the module. This will always fail as the
24500         # module is designed to not be inserted.
24501         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
24502             &> /dev/null
24503
24504         # Anything but success is a test failure
24505         dmesg | grep -q \
24506             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
24507             error "no inode match"
24508 }
24509 run_test 410 "Test inode number returned from kernel thread"
24510
24511 cleanup_test411_cgroup() {
24512         trap 0
24513         rmdir "$1"
24514 }
24515
24516 test_411() {
24517         local cg_basedir=/sys/fs/cgroup/memory
24518         # LU-9966
24519         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
24520                 skip "no setup for cgroup"
24521
24522         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
24523                 error "test file creation failed"
24524         cancel_lru_locks osc
24525
24526         # Create a very small memory cgroup to force a slab allocation error
24527         local cgdir=$cg_basedir/osc_slab_alloc
24528         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
24529         trap "cleanup_test411_cgroup $cgdir" EXIT
24530         echo 2M > $cgdir/memory.kmem.limit_in_bytes
24531         echo 1M > $cgdir/memory.limit_in_bytes
24532
24533         # Should not LBUG, just be killed by oom-killer
24534         # dd will return 0 even allocation failure in some environment.
24535         # So don't check return value
24536         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
24537         cleanup_test411_cgroup $cgdir
24538
24539         return 0
24540 }
24541 run_test 411 "Slab allocation error with cgroup does not LBUG"
24542
24543 test_412() {
24544         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24545         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
24546                 skip "Need server version at least 2.10.55"
24547         fi
24548
24549         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
24550                 error "mkdir failed"
24551         $LFS getdirstripe $DIR/$tdir
24552         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
24553         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
24554                 error "expect $((MDSCOUT - 1)) get $stripe_index"
24555         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
24556         [ $stripe_count -eq 2 ] ||
24557                 error "expect 2 get $stripe_count"
24558 }
24559 run_test 412 "mkdir on specific MDTs"
24560
24561 generate_uneven_mdts() {
24562         local threshold=$1
24563         local ffree
24564         local bavail
24565         local max
24566         local min
24567         local max_index
24568         local min_index
24569         local tmp
24570         local i
24571
24572         echo
24573         echo "Check for uneven MDTs: "
24574
24575         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
24576         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
24577         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
24578
24579         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24580         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24581         max_index=0
24582         min_index=0
24583         for ((i = 1; i < ${#ffree[@]}; i++)); do
24584                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
24585                 if [ $tmp -gt $max ]; then
24586                         max=$tmp
24587                         max_index=$i
24588                 fi
24589                 if [ $tmp -lt $min ]; then
24590                         min=$tmp
24591                         min_index=$i
24592                 fi
24593         done
24594
24595         # Check if we need to generate uneven MDTs
24596         local diff=$(((max - min) * 100 / min))
24597         local testdir=$DIR/$tdir-fillmdt
24598
24599         mkdir -p $testdir
24600
24601         i=0
24602         while (( diff < threshold )); do
24603                 # generate uneven MDTs, create till $threshold% diff
24604                 echo -n "weight diff=$diff% must be > $threshold% ..."
24605                 echo "Fill MDT$min_index with 100 files: loop $i"
24606                 testdir=$DIR/$tdir-fillmdt/$i
24607                 [ -d $testdir ] || $LFS mkdir -i $min_index $testdir ||
24608                         error "mkdir $testdir failed"
24609                 $LFS setstripe -E 1M -L mdt $testdir ||
24610                         error "setstripe $testdir failed"
24611                 for F in f.{0..99}; do
24612                         dd if=/dev/zero of=$testdir/$F bs=1M count=1 > \
24613                                 /dev/null 2>&1 || error "dd $F failed"
24614                 done
24615
24616                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
24617                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
24618                 max=$(((${ffree[max_index]} >> 8) * \
24619                         (${bavail[max_index]} * bsize >> 16)))
24620                 min=$(((${ffree[min_index]} >> 8) * \
24621                         (${bavail[min_index]} * bsize >> 16)))
24622                 diff=$(((max - min) * 100 / min))
24623                 i=$((i + 1))
24624         done
24625
24626         echo "MDT filesfree available: ${ffree[@]}"
24627         echo "MDT blocks available: ${bavail[@]}"
24628         echo "weight diff=$diff%"
24629 }
24630
24631 test_qos_mkdir() {
24632         local mkdir_cmd=$1
24633         local stripe_count=$2
24634         local mdts=$(comma_list $(mdts_nodes))
24635
24636         local testdir
24637         local lmv_qos_prio_free
24638         local lmv_qos_threshold_rr
24639         local lmv_qos_maxage
24640         local lod_qos_prio_free
24641         local lod_qos_threshold_rr
24642         local lod_qos_maxage
24643         local count
24644         local i
24645
24646         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
24647         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
24648         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
24649                 head -n1)
24650         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
24651         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
24652         stack_trap "$LCTL set_param \
24653                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null"
24654         stack_trap "$LCTL set_param \
24655                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null"
24656         stack_trap "$LCTL set_param \
24657                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
24658
24659         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
24660                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
24661         lod_qos_prio_free=${lod_qos_prio_free%%%}
24662         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
24663                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
24664         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
24665         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
24666                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
24667         stack_trap "do_nodes $mdts $LCTL set_param \
24668                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null"
24669         stack_trap "do_nodes $mdts $LCTL set_param \
24670                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null"
24671         stack_trap "do_nodes $mdts $LCTL set_param \
24672                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null"
24673
24674         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
24675         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
24676
24677         testdir=$DIR/$tdir-s$stripe_count/rr
24678
24679         local stripe_index=$($LFS getstripe -m $testdir)
24680         local test_mkdir_rr=true
24681
24682         echo "dirstripe: '$($LFS getdirstripe $testdir)'"
24683         getfattr -d -m dmv -e hex $testdir | grep dmv
24684         if (( $? == 0 && $MDS1_VERSION >= $(version_code 2.14.51) )); then
24685                 echo "defstripe: '$($LFS getdirstripe -D $testdir)'"
24686                 (( $($LFS getdirstripe -D --max-inherit-rr $testdir) == 0 )) &&
24687                         test_mkdir_rr=false
24688         fi
24689
24690         echo
24691         $test_mkdir_rr &&
24692                 echo "Mkdir (stripe_count $stripe_count) roundrobin:" ||
24693                 echo "Mkdir (stripe_count $stripe_count) on stripe $stripe_index"
24694
24695         stack_trap "unlinkmany -d $testdir/subdir $((100 * MDSCOUNT))"
24696         for (( i = 0; i < 100 * MDSCOUNT; i++ )); do
24697                 eval $mkdir_cmd $testdir/subdir$i ||
24698                         error "$mkdir_cmd subdir$i failed"
24699         done
24700
24701         for (( i = 0; i < $MDSCOUNT; i++ )); do
24702                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
24703                 echo "$count directories created on MDT$i"
24704                 if $test_mkdir_rr; then
24705                         (( $count == 100 )) ||
24706                                 error "subdirs are not evenly distributed"
24707                 elif (( $i == $stripe_index )); then
24708                         (( $count == 100 * MDSCOUNT )) ||
24709                                 error "$count subdirs created on MDT$i"
24710                 else
24711                         (( $count == 0 )) ||
24712                                 error "$count subdirs created on MDT$i"
24713                 fi
24714
24715                 if $test_mkdir_rr && [ $stripe_count -gt 1 ]; then
24716                         count=$($LFS getdirstripe $testdir/* |
24717                                 grep -c -P "^\s+$i\t")
24718                         echo "$count stripes created on MDT$i"
24719                         # deviation should < 5% of average
24720                         (( $count >= 95 * stripe_count &&
24721                            $count <= 105 * stripe_count)) ||
24722                                 error "stripes are not evenly distributed"
24723                 fi
24724         done
24725
24726         echo
24727         echo "Check for uneven MDTs: "
24728
24729         local ffree
24730         local bavail
24731         local max
24732         local min
24733         local max_index
24734         local min_index
24735         local tmp
24736
24737         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
24738         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
24739         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
24740
24741         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24742         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24743         max_index=0
24744         min_index=0
24745         for ((i = 1; i < ${#ffree[@]}; i++)); do
24746                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
24747                 if [ $tmp -gt $max ]; then
24748                         max=$tmp
24749                         max_index=$i
24750                 fi
24751                 if [ $tmp -lt $min ]; then
24752                         min=$tmp
24753                         min_index=$i
24754                 fi
24755         done
24756
24757         (( ${ffree[min_index]} > 0 )) ||
24758                 skip "no free files in MDT$min_index"
24759         (( ${ffree[min_index]} < 100000000 )) ||
24760                 skip "too many free files in MDT$min_index"
24761
24762         echo "MDT filesfree available: ${ffree[@]}"
24763         echo "MDT blocks available: ${bavail[@]}"
24764         echo "weight diff=$(((max - min) * 100 / min))%"
24765         echo
24766         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
24767
24768         $LCTL set_param lmv.*.qos_threshold_rr=0 > /dev/null
24769         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
24770         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=0 > /dev/null
24771         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
24772         # decrease statfs age, so that it can be updated in time
24773         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
24774         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
24775
24776         sleep 1
24777
24778         testdir=$DIR/$tdir-s$stripe_count/qos
24779         local num=200
24780
24781         stack_trap "unlinkmany -d $testdir/subdir $((num * MDSCOUNT))"
24782         for (( i = 0; i < num * MDSCOUNT; i++ )); do
24783                 eval $mkdir_cmd $testdir/subdir$i ||
24784                         error "$mkdir_cmd subdir$i failed"
24785         done
24786
24787         for (( i = 0; i < $MDSCOUNT; i++ )); do
24788                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
24789                 echo "$count directories created on MDT$i"
24790
24791                 if [ $stripe_count -gt 1 ]; then
24792                         count=$($LFS getdirstripe $testdir/* |
24793                                 grep -c -P "^\s+$i\t")
24794                         echo "$count stripes created on MDT$i"
24795                 fi
24796         done
24797
24798         max=$($LFS getdirstripe -i $testdir/* | grep -c "^$max_index$")
24799         min=$($LFS getdirstripe -i $testdir/* | grep -c "^$min_index$")
24800
24801         # D-value should > 10% of averge
24802         (( max - min >= num / 10 )) ||
24803                 error "subdirs shouldn't be evenly distributed: $max - $min < $((num / 10))"
24804
24805         # 5% for stripes
24806         if (( stripe_count > 1 )); then
24807                 max=$($LFS getdirstripe $testdir/* |
24808                       grep -c -P "^\s+$max_index\t")
24809                 min=$($LFS getdirstripe $testdir/* |
24810                         grep -c -P "^\s+$min_index\t")
24811                 (( max - min >= num * stripe_count / 20 )) ||
24812                         error "stripes shouldn't be evenly distributed: $max - $min < $((num / 20)) * $stripe_count"
24813         fi
24814 }
24815
24816 most_full_mdt() {
24817         local ffree
24818         local bavail
24819         local bsize
24820         local min
24821         local min_index
24822         local tmp
24823
24824         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
24825         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
24826         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
24827
24828         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24829         min_index=0
24830         for ((i = 1; i < ${#ffree[@]}; i++)); do
24831                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
24832                 (( tmp < min )) && min=$tmp && min_index=$i
24833         done
24834
24835         echo -n $min_index
24836 }
24837
24838 test_413a() {
24839         [ $MDSCOUNT -lt 2 ] &&
24840                 skip "We need at least 2 MDTs for this test"
24841
24842         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
24843                 skip "Need server version at least 2.12.52"
24844
24845         local stripe_count
24846
24847         generate_uneven_mdts 100
24848         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
24849                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
24850                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
24851                 $LFS mkdir -i $(most_full_mdt) $DIR/$tdir-s$stripe_count/qos ||
24852                         error "mkdir failed"
24853                 test_qos_mkdir "$LFS mkdir -i -1 -c $stripe_count" $stripe_count
24854         done
24855 }
24856 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
24857
24858 test_413b() {
24859         [ $MDSCOUNT -lt 2 ] &&
24860                 skip "We need at least 2 MDTs for this test"
24861
24862         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
24863                 skip "Need server version at least 2.12.52"
24864
24865         local testdir
24866         local stripe_count
24867
24868         generate_uneven_mdts 100
24869         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
24870                 testdir=$DIR/$tdir-s$stripe_count
24871                 mkdir $testdir || error "mkdir $testdir failed"
24872                 mkdir $testdir/rr || error "mkdir rr failed"
24873                 $LFS mkdir -i $(most_full_mdt) $testdir/qos ||
24874                         error "mkdir qos failed"
24875                 $LFS setdirstripe -D -c $stripe_count --max-inherit-rr 2 \
24876                         $testdir/rr || error "setdirstripe rr failed"
24877                 $LFS setdirstripe -D -c $stripe_count $testdir/qos ||
24878                         error "setdirstripe failed"
24879                 test_qos_mkdir "mkdir" $stripe_count
24880         done
24881 }
24882 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
24883
24884 test_413c() {
24885         (( $MDSCOUNT >= 2 )) ||
24886                 skip "We need at least 2 MDTs for this test"
24887
24888         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
24889                 skip "Need server version at least 2.14.51"
24890
24891         local testdir
24892         local inherit
24893         local inherit_rr
24894
24895         testdir=$DIR/${tdir}-s1
24896         mkdir $testdir || error "mkdir $testdir failed"
24897         mkdir $testdir/rr || error "mkdir rr failed"
24898         $LFS mkdir -i $(most_full_mdt) $testdir/qos || error "mkdir qos failed"
24899         # default max_inherit is -1, default max_inherit_rr is 0
24900         $LFS setdirstripe -D -c 1 $testdir/rr ||
24901                 error "setdirstripe rr failed"
24902         $LFS setdirstripe -D -c 1 -i -1 -X 2 --max-inherit-rr 1 $testdir/qos ||
24903                 error "setdirstripe qos failed"
24904         test_qos_mkdir "mkdir" 1
24905
24906         mkdir $testdir/rr/level1 || error "mkdir rr/level1 failed"
24907         inherit=$($LFS getdirstripe -D -X $testdir/rr/level1)
24908         (( $inherit == -1 )) || error "rr/level1 inherit $inherit != -1"
24909         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/rr/level1)
24910         (( $inherit_rr == 0 )) || error "rr/level1 inherit-rr $inherit_rr != 0"
24911
24912         mkdir $testdir/qos/level1 || error "mkdir qos/level1 failed"
24913         inherit=$($LFS getdirstripe -D -X $testdir/qos/level1)
24914         (( $inherit == 1 )) || error "qos/level1 inherit $inherit != 1"
24915         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/qos/level1)
24916         (( $inherit_rr == 0 )) || error "qos/level1 inherit-rr $inherit_rr != 0"
24917         mkdir $testdir/qos/level1/level2 || error "mkdir level2 failed"
24918         getfattr -d -m dmv -e hex $testdir/qos/level1/level2 | grep dmv &&
24919                 error "level2 shouldn't have default LMV" || true
24920 }
24921 run_test 413c "mkdir with default LMV max inherit rr"
24922
24923 test_413d() {
24924         (( MDSCOUNT >= 2 )) ||
24925                 skip "We need at least 2 MDTs for this test"
24926
24927         (( MDS1_VERSION >= $(version_code 2.14.51) )) ||
24928                 skip "Need server version at least 2.14.51"
24929
24930         local lmv_qos_threshold_rr
24931
24932         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
24933                 head -n1)
24934         stack_trap "$LCTL set_param \
24935                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
24936
24937         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
24938         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
24939         getfattr -d -m dmv -e hex $DIR/$tdir | grep dmv &&
24940                 error "$tdir shouldn't have default LMV"
24941         createmany -d $DIR/$tdir/sub $((100 * MDSCOUNT)) ||
24942                 error "mkdir sub failed"
24943
24944         local count=$($LFS getstripe -m $DIR/$tdir/* | grep -c ^0)
24945
24946         (( count == 100 )) || error "$count subdirs on MDT0"
24947 }
24948 run_test 413d "inherit ROOT default LMV"
24949
24950 test_413z() {
24951         local pids=""
24952         local subdir
24953         local pid
24954
24955         for subdir in $(\ls -1 -d $DIR/d413*-fillmdt/*); do
24956                 unlinkmany $subdir/f. 100 &
24957                 pids="$pids $!"
24958         done
24959
24960         for pid in $pids; do
24961                 wait $pid
24962         done
24963 }
24964 run_test 413z "413 test cleanup"
24965
24966 test_414() {
24967 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
24968         $LCTL set_param fail_loc=0x80000521
24969         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
24970         rm -f $DIR/$tfile
24971 }
24972 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
24973
24974 test_415() {
24975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24976         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
24977                 skip "Need server version at least 2.11.52"
24978
24979         # LU-11102
24980         local total
24981         local setattr_pid
24982         local start_time
24983         local end_time
24984         local duration
24985
24986         total=500
24987         # this test may be slow on ZFS
24988         [ "$mds1_FSTYPE" == "zfs" ] && total=100
24989
24990         # though this test is designed for striped directory, let's test normal
24991         # directory too since lock is always saved as CoS lock.
24992         test_mkdir $DIR/$tdir || error "mkdir $tdir"
24993         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
24994
24995         (
24996                 while true; do
24997                         touch $DIR/$tdir
24998                 done
24999         ) &
25000         setattr_pid=$!
25001
25002         start_time=$(date +%s)
25003         for i in $(seq $total); do
25004                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
25005                         > /dev/null
25006         done
25007         end_time=$(date +%s)
25008         duration=$((end_time - start_time))
25009
25010         kill -9 $setattr_pid
25011
25012         echo "rename $total files took $duration sec"
25013         [ $duration -lt 100 ] || error "rename took $duration sec"
25014 }
25015 run_test 415 "lock revoke is not missing"
25016
25017 test_416() {
25018         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
25019                 skip "Need server version at least 2.11.55"
25020
25021         # define OBD_FAIL_OSD_TXN_START    0x19a
25022         do_facet mds1 lctl set_param fail_loc=0x19a
25023
25024         lfs mkdir -c $MDSCOUNT $DIR/$tdir
25025
25026         true
25027 }
25028 run_test 416 "transaction start failure won't cause system hung"
25029
25030 cleanup_417() {
25031         trap 0
25032         do_nodes $(comma_list $(mdts_nodes)) \
25033                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
25034         do_nodes $(comma_list $(mdts_nodes)) \
25035                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
25036         do_nodes $(comma_list $(mdts_nodes)) \
25037                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
25038 }
25039
25040 test_417() {
25041         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25042         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
25043                 skip "Need MDS version at least 2.11.56"
25044
25045         trap cleanup_417 RETURN EXIT
25046
25047         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
25048         do_nodes $(comma_list $(mdts_nodes)) \
25049                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
25050         $LFS migrate -m 0 $DIR/$tdir.1 &&
25051                 error "migrate dir $tdir.1 should fail"
25052
25053         do_nodes $(comma_list $(mdts_nodes)) \
25054                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
25055         $LFS mkdir -i 1 $DIR/$tdir.2 &&
25056                 error "create remote dir $tdir.2 should fail"
25057
25058         do_nodes $(comma_list $(mdts_nodes)) \
25059                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
25060         $LFS mkdir -c 2 $DIR/$tdir.3 &&
25061                 error "create striped dir $tdir.3 should fail"
25062         true
25063 }
25064 run_test 417 "disable remote dir, striped dir and dir migration"
25065
25066 # Checks that the outputs of df [-i] and lfs df [-i] match
25067 #
25068 # usage: check_lfs_df <blocks | inodes> <mountpoint>
25069 check_lfs_df() {
25070         local dir=$2
25071         local inodes
25072         local df_out
25073         local lfs_df_out
25074         local count
25075         local passed=false
25076
25077         # blocks or inodes
25078         [ "$1" == "blocks" ] && inodes= || inodes="-i"
25079
25080         for count in {1..100}; do
25081                 cancel_lru_locks
25082                 sync; sleep 0.2
25083
25084                 # read the lines of interest
25085                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
25086                         error "df $inodes $dir | tail -n +2 failed"
25087                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
25088                         error "lfs df $inodes $dir | grep summary: failed"
25089
25090                 # skip first substrings of each output as they are different
25091                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
25092                 # compare the two outputs
25093                 passed=true
25094                 for i in {1..5}; do
25095                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
25096                 done
25097                 $passed && break
25098         done
25099
25100         if ! $passed; then
25101                 df -P $inodes $dir
25102                 echo
25103                 lfs df $inodes $dir
25104                 error "df and lfs df $1 output mismatch: "      \
25105                       "df ${inodes}: ${df_out[*]}, "            \
25106                       "lfs df ${inodes}: ${lfs_df_out[*]}"
25107         fi
25108 }
25109
25110 test_418() {
25111         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25112
25113         local dir=$DIR/$tdir
25114         local numfiles=$((RANDOM % 4096 + 2))
25115         local numblocks=$((RANDOM % 256 + 1))
25116
25117         wait_delete_completed
25118         test_mkdir $dir
25119
25120         # check block output
25121         check_lfs_df blocks $dir
25122         # check inode output
25123         check_lfs_df inodes $dir
25124
25125         # create a single file and retest
25126         echo "Creating a single file and testing"
25127         createmany -o $dir/$tfile- 1 &>/dev/null ||
25128                 error "creating 1 file in $dir failed"
25129         check_lfs_df blocks $dir
25130         check_lfs_df inodes $dir
25131
25132         # create a random number of files
25133         echo "Creating $((numfiles - 1)) files and testing"
25134         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
25135                 error "creating $((numfiles - 1)) files in $dir failed"
25136
25137         # write a random number of blocks to the first test file
25138         echo "Writing $numblocks 4K blocks and testing"
25139         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
25140                 count=$numblocks &>/dev/null ||
25141                 error "dd to $dir/${tfile}-0 failed"
25142
25143         # retest
25144         check_lfs_df blocks $dir
25145         check_lfs_df inodes $dir
25146
25147         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
25148                 error "unlinking $numfiles files in $dir failed"
25149 }
25150 run_test 418 "df and lfs df outputs match"
25151
25152 test_419()
25153 {
25154         local dir=$DIR/$tdir
25155
25156         mkdir -p $dir
25157         touch $dir/file
25158
25159         cancel_lru_locks mdc
25160
25161         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
25162         $LCTL set_param fail_loc=0x1410
25163         cat $dir/file
25164         $LCTL set_param fail_loc=0
25165         rm -rf $dir
25166 }
25167 run_test 419 "Verify open file by name doesn't crash kernel"
25168
25169 test_420()
25170 {
25171         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
25172                 skip "Need MDS version at least 2.12.53"
25173
25174         local SAVE_UMASK=$(umask)
25175         local dir=$DIR/$tdir
25176         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
25177
25178         mkdir -p $dir
25179         umask 0000
25180         mkdir -m03777 $dir/testdir
25181         ls -dn $dir/testdir
25182         # Need to remove trailing '.' when SELinux is enabled
25183         local dirperms=$(ls -dn $dir/testdir |
25184                          awk '{ sub(/\.$/, "", $1); print $1}')
25185         [ $dirperms == "drwxrwsrwt" ] ||
25186                 error "incorrect perms on $dir/testdir"
25187
25188         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
25189                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
25190         ls -n $dir/testdir/testfile
25191         local fileperms=$(ls -n $dir/testdir/testfile |
25192                           awk '{ sub(/\.$/, "", $1); print $1}')
25193         [ $fileperms == "-rwxr-xr-x" ] ||
25194                 error "incorrect perms on $dir/testdir/testfile"
25195
25196         umask $SAVE_UMASK
25197 }
25198 run_test 420 "clear SGID bit on non-directories for non-members"
25199
25200 test_421a() {
25201         local cnt
25202         local fid1
25203         local fid2
25204
25205         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25206                 skip "Need MDS version at least 2.12.54"
25207
25208         test_mkdir $DIR/$tdir
25209         createmany -o $DIR/$tdir/f 3
25210         cnt=$(ls -1 $DIR/$tdir | wc -l)
25211         [ $cnt != 3 ] && error "unexpected #files: $cnt"
25212
25213         fid1=$(lfs path2fid $DIR/$tdir/f1)
25214         fid2=$(lfs path2fid $DIR/$tdir/f2)
25215         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
25216
25217         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
25218         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
25219
25220         cnt=$(ls -1 $DIR/$tdir | wc -l)
25221         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
25222
25223         rm -f $DIR/$tdir/f3 || error "can't remove f3"
25224         createmany -o $DIR/$tdir/f 3
25225         cnt=$(ls -1 $DIR/$tdir | wc -l)
25226         [ $cnt != 3 ] && error "unexpected #files: $cnt"
25227
25228         fid1=$(lfs path2fid $DIR/$tdir/f1)
25229         fid2=$(lfs path2fid $DIR/$tdir/f2)
25230         echo "remove using fsname $FSNAME"
25231         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
25232
25233         cnt=$(ls -1 $DIR/$tdir | wc -l)
25234         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
25235 }
25236 run_test 421a "simple rm by fid"
25237
25238 test_421b() {
25239         local cnt
25240         local FID1
25241         local FID2
25242
25243         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25244                 skip "Need MDS version at least 2.12.54"
25245
25246         test_mkdir $DIR/$tdir
25247         createmany -o $DIR/$tdir/f 3
25248         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
25249         MULTIPID=$!
25250
25251         FID1=$(lfs path2fid $DIR/$tdir/f1)
25252         FID2=$(lfs path2fid $DIR/$tdir/f2)
25253         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
25254
25255         kill -USR1 $MULTIPID
25256         wait
25257
25258         cnt=$(ls $DIR/$tdir | wc -l)
25259         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
25260 }
25261 run_test 421b "rm by fid on open file"
25262
25263 test_421c() {
25264         local cnt
25265         local FIDS
25266
25267         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25268                 skip "Need MDS version at least 2.12.54"
25269
25270         test_mkdir $DIR/$tdir
25271         createmany -o $DIR/$tdir/f 3
25272         touch $DIR/$tdir/$tfile
25273         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
25274         cnt=$(ls -1 $DIR/$tdir | wc -l)
25275         [ $cnt != 184 ] && error "unexpected #files: $cnt"
25276
25277         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
25278         $LFS rmfid $DIR $FID1 || error "rmfid failed"
25279
25280         cnt=$(ls $DIR/$tdir | wc -l)
25281         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
25282 }
25283 run_test 421c "rm by fid against hardlinked files"
25284
25285 test_421d() {
25286         local cnt
25287         local FIDS
25288
25289         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25290                 skip "Need MDS version at least 2.12.54"
25291
25292         test_mkdir $DIR/$tdir
25293         createmany -o $DIR/$tdir/f 4097
25294         cnt=$(ls -1 $DIR/$tdir | wc -l)
25295         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
25296
25297         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
25298         $LFS rmfid $DIR $FIDS || error "rmfid failed"
25299
25300         cnt=$(ls $DIR/$tdir | wc -l)
25301         rm -rf $DIR/$tdir
25302         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25303 }
25304 run_test 421d "rmfid en masse"
25305
25306 test_421e() {
25307         local cnt
25308         local FID
25309
25310         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25311         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25312                 skip "Need MDS version at least 2.12.54"
25313
25314         mkdir -p $DIR/$tdir
25315         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
25316         createmany -o $DIR/$tdir/striped_dir/f 512
25317         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25318         [ $cnt != 512 ] && error "unexpected #files: $cnt"
25319
25320         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
25321                 sed "s/[/][^:]*://g")
25322         $LFS rmfid $DIR $FIDS || error "rmfid failed"
25323
25324         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
25325         rm -rf $DIR/$tdir
25326         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25327 }
25328 run_test 421e "rmfid in DNE"
25329
25330 test_421f() {
25331         local cnt
25332         local FID
25333
25334         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25335                 skip "Need MDS version at least 2.12.54"
25336
25337         test_mkdir $DIR/$tdir
25338         touch $DIR/$tdir/f
25339         cnt=$(ls -1 $DIR/$tdir | wc -l)
25340         [ $cnt != 1 ] && error "unexpected #files: $cnt"
25341
25342         FID=$(lfs path2fid $DIR/$tdir/f)
25343         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
25344         # rmfid should fail
25345         cnt=$(ls -1 $DIR/$tdir | wc -l)
25346         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
25347
25348         chmod a+rw $DIR/$tdir
25349         ls -la $DIR/$tdir
25350         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
25351         # rmfid should fail
25352         cnt=$(ls -1 $DIR/$tdir | wc -l)
25353         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
25354
25355         rm -f $DIR/$tdir/f
25356         $RUNAS touch $DIR/$tdir/f
25357         FID=$(lfs path2fid $DIR/$tdir/f)
25358         echo "rmfid as root"
25359         $LFS rmfid $DIR $FID || error "rmfid as root failed"
25360         cnt=$(ls -1 $DIR/$tdir | wc -l)
25361         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
25362
25363         rm -f $DIR/$tdir/f
25364         $RUNAS touch $DIR/$tdir/f
25365         cnt=$(ls -1 $DIR/$tdir | wc -l)
25366         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
25367         FID=$(lfs path2fid $DIR/$tdir/f)
25368         # rmfid w/o user_fid2path mount option should fail
25369         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
25370         cnt=$(ls -1 $DIR/$tdir | wc -l)
25371         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
25372
25373         tmpdir=$(mktemp -d /tmp/lustre-XXXXXX)
25374         stack_trap "rmdir $tmpdir"
25375         mount_client $tmpdir "$MOUNT_OPTS,user_fid2path" ||
25376                 error "failed to mount client'"
25377         stack_trap "umount_client $tmpdir"
25378
25379         $RUNAS $LFS rmfid $tmpdir $FID || error "rmfid failed"
25380         # rmfid should succeed
25381         cnt=$(ls -1 $tmpdir/$tdir | wc -l)
25382         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
25383
25384         # rmfid shouldn't allow to remove files due to dir's permission
25385         chmod a+rwx $tmpdir/$tdir
25386         touch $tmpdir/$tdir/f
25387         ls -la $tmpdir/$tdir
25388         FID=$(lfs path2fid $tmpdir/$tdir/f)
25389         $RUNAS $LFS rmfid $tmpdir $FID && error "rmfid didn't fail"
25390         return 0
25391 }
25392 run_test 421f "rmfid checks permissions"
25393
25394 test_421g() {
25395         local cnt
25396         local FIDS
25397
25398         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25399         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25400                 skip "Need MDS version at least 2.12.54"
25401
25402         mkdir -p $DIR/$tdir
25403         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
25404         createmany -o $DIR/$tdir/striped_dir/f 512
25405         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25406         [ $cnt != 512 ] && error "unexpected #files: $cnt"
25407
25408         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
25409                 sed "s/[/][^:]*://g")
25410
25411         rm -f $DIR/$tdir/striped_dir/f1*
25412         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25413         removed=$((512 - cnt))
25414
25415         # few files have been just removed, so we expect
25416         # rmfid to fail on their fids
25417         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
25418         [ $removed != $errors ] && error "$errors != $removed"
25419
25420         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
25421         rm -rf $DIR/$tdir
25422         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25423 }
25424 run_test 421g "rmfid to return errors properly"
25425
25426 test_422() {
25427         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
25428         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
25429         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
25430         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
25431         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
25432
25433         local amc=$(at_max_get client)
25434         local amo=$(at_max_get mds1)
25435         local timeout=`lctl get_param -n timeout`
25436
25437         at_max_set 0 client
25438         at_max_set 0 mds1
25439
25440 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
25441         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
25442                         fail_val=$(((2*timeout + 10)*1000))
25443         touch $DIR/$tdir/d3/file &
25444         sleep 2
25445 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
25446         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
25447                         fail_val=$((2*timeout + 5))
25448         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
25449         local pid=$!
25450         sleep 1
25451         kill -9 $pid
25452         sleep $((2 * timeout))
25453         echo kill $pid
25454         kill -9 $pid
25455         lctl mark touch
25456         touch $DIR/$tdir/d2/file3
25457         touch $DIR/$tdir/d2/file4
25458         touch $DIR/$tdir/d2/file5
25459
25460         wait
25461         at_max_set $amc client
25462         at_max_set $amo mds1
25463
25464         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
25465         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
25466                 error "Watchdog is always throttled"
25467 }
25468 run_test 422 "kill a process with RPC in progress"
25469
25470 stat_test() {
25471     df -h $MOUNT &
25472     df -h $MOUNT &
25473     df -h $MOUNT &
25474     df -h $MOUNT &
25475     df -h $MOUNT &
25476     df -h $MOUNT &
25477 }
25478
25479 test_423() {
25480     local _stats
25481     # ensure statfs cache is expired
25482     sleep 2;
25483
25484     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
25485     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
25486
25487     return 0
25488 }
25489 run_test 423 "statfs should return a right data"
25490
25491 test_424() {
25492 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
25493         $LCTL set_param fail_loc=0x80000522
25494         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
25495         rm -f $DIR/$tfile
25496 }
25497 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
25498
25499 test_425() {
25500         test_mkdir -c -1 $DIR/$tdir
25501         $LFS setstripe -c -1 $DIR/$tdir
25502
25503         lru_resize_disable "" 100
25504         stack_trap "lru_resize_enable" EXIT
25505
25506         sleep 5
25507
25508         for i in $(seq $((MDSCOUNT * 125))); do
25509                 local t=$DIR/$tdir/$tfile_$i
25510
25511                 dd if=/dev/zero of=$t bs=4K count=1 > /dev/null 2>&1 ||
25512                         error_noexit "Create file $t"
25513         done
25514         stack_trap "rm -rf $DIR/$tdir" EXIT
25515
25516         for oscparam in $($LCTL list_param ldlm.namespaces.*osc-[-0-9a-f]*); do
25517                 local lru_size=$($LCTL get_param -n $oscparam.lru_size)
25518                 local lock_count=$($LCTL get_param -n $oscparam.lock_count)
25519
25520                 [ $lock_count -le $lru_size ] ||
25521                         error "osc lock count $lock_count > lru size $lru_size"
25522         done
25523
25524         for mdcparam in $($LCTL list_param ldlm.namespaces.*mdc-*); do
25525                 local lru_size=$($LCTL get_param -n $mdcparam.lru_size)
25526                 local lock_count=$($LCTL get_param -n $mdcparam.lock_count)
25527
25528                 [ $lock_count -le $lru_size ] ||
25529                         error "mdc lock count $lock_count > lru size $lru_size"
25530         done
25531 }
25532 run_test 425 "lock count should not exceed lru size"
25533
25534 test_426() {
25535         splice-test -r $DIR/$tfile
25536         splice-test -rd $DIR/$tfile
25537         splice-test $DIR/$tfile
25538         splice-test -d $DIR/$tfile
25539 }
25540 run_test 426 "splice test on Lustre"
25541
25542 test_427() {
25543         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
25544         (( $MDS1_VERSION >= $(version_code 2.12.4) )) ||
25545                 skip "Need MDS version at least 2.12.4"
25546         local log
25547
25548         mkdir $DIR/$tdir
25549         mkdir $DIR/$tdir/1
25550         mkdir $DIR/$tdir/2
25551         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/1/dir
25552         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/2/dir2
25553
25554         $LFS getdirstripe $DIR/$tdir/1/dir
25555
25556         #first setfattr for creating updatelog
25557         setfattr -n user.attr0 -v "some text" $DIR/$tdir/1/dir
25558
25559 #define OBD_FAIL_OUT_OBJECT_MISS        0x1708
25560         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x80001708
25561         setfattr -n user.attr1 -v "some text" $DIR/$tdir/1/dir &
25562         setfattr -n user.attr2 -v "another attr"  $DIR/$tdir/2/dir2 &
25563
25564         sleep 2
25565         fail mds2
25566         wait_recovery_complete mds2 $((2*TIMEOUT))
25567
25568         log=$(do_facet mds1 dmesg | tac | sed "/${TESTNAME//_/ }/,$ d")
25569         echo $log | grep "get update log failed" &&
25570                 error "update log corruption is detected" || true
25571 }
25572 run_test 427 "Failed DNE2 update request shouldn't corrupt updatelog"
25573
25574 test_428() {
25575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25576         local cache_limit=$CACHE_MAX
25577
25578         stack_trap "$LCTL set_param -n llite.*.max_cached_mb=$cache_limit"
25579         $LCTL set_param -n llite.*.max_cached_mb=64
25580
25581         mkdir $DIR/$tdir
25582         $LFS setstripe -c 1 $DIR/$tdir
25583         eval touch $DIR/$tdir/$tfile.{1..$OSTCOUNT}
25584         stack_trap "rm -f $DIR/$tdir/$tfile.*"
25585         #test write
25586         for f in $(seq 4); do
25587                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$f bs=128M count=1 &
25588         done
25589         wait
25590
25591         cancel_lru_locks osc
25592         # Test read
25593         for f in $(seq 4); do
25594                 dd if=$DIR/$tdir/$tfile.$f of=/dev/null bs=128M count=1 &
25595         done
25596         wait
25597 }
25598 run_test 428 "large block size IO should not hang"
25599
25600 test_429() { # LU-7915 / LU-10948
25601         local ll_opencache_threshold_count="llite.*.opencache_threshold_count"
25602         local testfile=$DIR/$tfile
25603         local mdc_rpcstats="mdc.$FSNAME-MDT0000-*.stats"
25604         local new_flag=1
25605         local first_rpc
25606         local second_rpc
25607         local third_rpc
25608
25609         $LCTL get_param $ll_opencache_threshold_count ||
25610                 skip "client does not have opencache parameter"
25611
25612         set_opencache $new_flag
25613         stack_trap "restore_opencache"
25614         [ $($LCTL get_param -n $ll_opencache_threshold_count) == $new_flag ] ||
25615                 error "enable opencache failed"
25616         touch $testfile
25617         # drop MDC DLM locks
25618         cancel_lru_locks mdc
25619         # clear MDC RPC stats counters
25620         $LCTL set_param $mdc_rpcstats=clear
25621
25622         # According to the current implementation, we need to run 3 times
25623         # open & close file to verify if opencache is enabled correctly.
25624         # 1st, RPCs are sent for lookup/open and open handle is released on
25625         #      close finally.
25626         # 2nd, RPC is sent for open, MDS_OPEN_LOCK is fetched automatically,
25627         #      so open handle won't be released thereafter.
25628         # 3rd, No RPC is sent out.
25629         $MULTIOP $testfile oc || error "multiop failed"
25630         first_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25631         echo "1st: $first_rpc RPCs in flight"
25632
25633         $MULTIOP $testfile oc || error "multiop failed"
25634         second_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25635         echo "2nd: $second_rpc RPCs in flight"
25636
25637         $MULTIOP $testfile oc || error "multiop failed"
25638         third_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25639         echo "3rd: $third_rpc RPCs in flight"
25640
25641         #verify no MDC RPC is sent
25642         [[ $second_rpc == $third_rpc ]] || error "MDC RPC is still sent"
25643 }
25644 run_test 429 "verify if opencache flag on client side does work"
25645
25646 lseek_test_430() {
25647         local offset
25648         local file=$1
25649
25650         # data at [200K, 400K)
25651         dd if=/dev/urandom of=$file bs=256K count=1 seek=1 ||
25652                 error "256K->512K dd fails"
25653         # data at [2M, 3M)
25654         dd if=/dev/urandom of=$file bs=1M count=1 seek=2 ||
25655                 error "2M->3M dd fails"
25656         # data at [4M, 5M)
25657         dd if=/dev/urandom of=$file bs=1M count=1 seek=4 ||
25658                 error "4M->5M dd fails"
25659         echo "Data at 256K...512K, 2M...3M and 4M...5M"
25660         # start at first component hole #1
25661         printf "Seeking hole from 1000 ... "
25662         offset=$(lseek_test -l 1000 $file)
25663         echo $offset
25664         [[ $offset == 1000 ]] || error "offset $offset != 1000"
25665         printf "Seeking data from 1000 ... "
25666         offset=$(lseek_test -d 1000 $file)
25667         echo $offset
25668         [[ $offset == 262144 ]] || error "offset $offset != 262144"
25669
25670         # start at first component data block
25671         printf "Seeking hole from 300000 ... "
25672         offset=$(lseek_test -l 300000 $file)
25673         echo $offset
25674         [[ $offset == 524288 ]] || error "offset $offset != 524288"
25675         printf "Seeking data from 300000 ... "
25676         offset=$(lseek_test -d 300000 $file)
25677         echo $offset
25678         [[ $offset == 300000 ]] || error "offset $offset != 300000"
25679
25680         # start at the first component but beyond end of object size
25681         printf "Seeking hole from 1000000 ... "
25682         offset=$(lseek_test -l 1000000 $file)
25683         echo $offset
25684         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
25685         printf "Seeking data from 1000000 ... "
25686         offset=$(lseek_test -d 1000000 $file)
25687         echo $offset
25688         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
25689
25690         # start at second component stripe 2 (empty file)
25691         printf "Seeking hole from 1500000 ... "
25692         offset=$(lseek_test -l 1500000 $file)
25693         echo $offset
25694         [[ $offset == 1500000 ]] || error "offset $offset != 1500000"
25695         printf "Seeking data from 1500000 ... "
25696         offset=$(lseek_test -d 1500000 $file)
25697         echo $offset
25698         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
25699
25700         # start at second component stripe 1 (all data)
25701         printf "Seeking hole from 3000000 ... "
25702         offset=$(lseek_test -l 3000000 $file)
25703         echo $offset
25704         [[ $offset == 3145728 ]] || error "offset $offset != 3145728"
25705         printf "Seeking data from 3000000 ... "
25706         offset=$(lseek_test -d 3000000 $file)
25707         echo $offset
25708         [[ $offset == 3000000 ]] || error "offset $offset != 3000000"
25709
25710         dd if=/dev/urandom of=$file bs=640K count=1 seek=1 ||
25711                 error "2nd dd fails"
25712         echo "Add data block at 640K...1280K"
25713
25714         # start at before new data block, in hole
25715         printf "Seeking hole from 600000 ... "
25716         offset=$(lseek_test -l 600000 $file)
25717         echo $offset
25718         [[ $offset == 600000 ]] || error "offset $offset != 600000"
25719         printf "Seeking data from 600000 ... "
25720         offset=$(lseek_test -d 600000 $file)
25721         echo $offset
25722         [[ $offset == 655360 ]] || error "offset $offset != 655360"
25723
25724         # start at the first component new data block
25725         printf "Seeking hole from 1000000 ... "
25726         offset=$(lseek_test -l 1000000 $file)
25727         echo $offset
25728         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
25729         printf "Seeking data from 1000000 ... "
25730         offset=$(lseek_test -d 1000000 $file)
25731         echo $offset
25732         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
25733
25734         # start at second component stripe 2, new data
25735         printf "Seeking hole from 1200000 ... "
25736         offset=$(lseek_test -l 1200000 $file)
25737         echo $offset
25738         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
25739         printf "Seeking data from 1200000 ... "
25740         offset=$(lseek_test -d 1200000 $file)
25741         echo $offset
25742         [[ $offset == 1200000 ]] || error "offset $offset != 1200000"
25743
25744         # start beyond file end
25745         printf "Using offset > filesize ... "
25746         lseek_test -l 4000000 $file && error "lseek should fail"
25747         printf "Using offset > filesize ... "
25748         lseek_test -d 4000000 $file && error "lseek should fail"
25749
25750         printf "Done\n\n"
25751 }
25752
25753 test_430a() {
25754         $LCTL get_param mdc.*.import | grep -q 'connect_flags:.*seek' ||
25755                 skip "MDT does not support SEEK_HOLE"
25756
25757         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
25758                 skip "OST does not support SEEK_HOLE"
25759
25760         local file=$DIR/$tdir/$tfile
25761
25762         mkdir -p $DIR/$tdir
25763
25764         $LFS setstripe -E 1M -L mdt -E eof -c2 $file
25765         # OST stripe #1 will have continuous data at [1M, 3M)
25766         # OST stripe #2 is empty
25767         echo "Component #1: 1M DoM, component #2: EOF, 2 stripes 1M"
25768         lseek_test_430 $file
25769         rm $file
25770         $LFS setstripe -E 1M -c2 -S 64K -E 10M -c2 -S 1M $file
25771         echo "Component #1: 1M, 2 stripes 64K, component #2: EOF, 2 stripes 1M"
25772         lseek_test_430 $file
25773         rm $file
25774         $LFS setstripe -c2 -S 512K $file
25775         echo "Two stripes, stripe size 512K"
25776         lseek_test_430 $file
25777         rm $file
25778         # FLR with stale mirror
25779         $LFS setstripe -N -E 512K -c1 -S 64K -E eof -c2 -S 512K \
25780                        -N -c2 -S 1M $file
25781         echo "Mirrored file:"
25782         echo "Component #1: 512K, stripe 64K, component #2: EOF, 2 stripes 512K"
25783         echo "Plain 2 stripes 1M"
25784         lseek_test_430 $file
25785         rm $file
25786 }
25787 run_test 430a "lseek: SEEK_DATA/SEEK_HOLE basic functionality"
25788
25789 test_430b() {
25790         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
25791                 skip "OST does not support SEEK_HOLE"
25792
25793         local offset
25794         local file=$DIR/$tdir/$tfile
25795
25796         mkdir -p $DIR/$tdir
25797         # Empty layout lseek should fail
25798         $MCREATE $file
25799         # seek from 0
25800         printf "Seeking hole from 0 ... "
25801         lseek_test -l 0 $file && error "lseek should fail"
25802         printf "Seeking data from 0 ... "
25803         lseek_test -d 0 $file && error "lseek should fail"
25804         rm $file
25805
25806         # 1M-hole file
25807         $LFS setstripe -E 1M -c2 -E eof $file
25808         $TRUNCATE $file 1048576
25809         printf "Seeking hole from 1000000 ... "
25810         offset=$(lseek_test -l 1000000 $file)
25811         echo $offset
25812         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
25813         printf "Seeking data from 1000000 ... "
25814         lseek_test -d 1000000 $file && error "lseek should fail"
25815         rm $file
25816
25817         # full component followed by non-inited one
25818         $LFS setstripe -E 1M -c2 -E eof $file
25819         dd if=/dev/urandom of=$file bs=1M count=1
25820         printf "Seeking hole from 1000000 ... "
25821         offset=$(lseek_test -l 1000000 $file)
25822         echo $offset
25823         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
25824         printf "Seeking hole from 1048576 ... "
25825         lseek_test -l 1048576 $file && error "lseek should fail"
25826         # init second component and truncate back
25827         echo "123" >> $file
25828         $TRUNCATE $file 1048576
25829         printf "Seeking hole from 1000000 ... "
25830         offset=$(lseek_test -l 1000000 $file)
25831         echo $offset
25832         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
25833         printf "Seeking hole from 1048576 ... "
25834         lseek_test -l 1048576 $file && error "lseek should fail"
25835         # boundary checks for big values
25836         dd if=/dev/urandom of=$file.10g bs=1 count=1 seek=10G
25837         offset=$(lseek_test -d 0 $file.10g)
25838         [[ $offset == 10737418240 ]] || error "offset $offset != 10737418240"
25839         dd if=/dev/urandom of=$file.100g bs=1 count=1 seek=100G
25840         offset=$(lseek_test -d 0 $file.100g)
25841         [[ $offset == 107374182400 ]] || error "offset $offset != 107374182400"
25842         return 0
25843 }
25844 run_test 430b "lseek: SEEK_DATA/SEEK_HOLE special cases"
25845
25846 test_430c() {
25847         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
25848                 skip "OST does not support SEEK_HOLE"
25849
25850         local file=$DIR/$tdir/$tfile
25851         local start
25852
25853         mkdir -p $DIR/$tdir
25854         dd if=/dev/urandom of=$file bs=1k count=1 seek=5M
25855
25856         # cp version 8.33+ prefers lseek over fiemap
25857         if [[ $(cp --version | head -n1 | sed "s/[^0-9]//g") -ge 833 ]]; then
25858                 start=$SECONDS
25859                 time cp $file /dev/null
25860                 (( SECONDS - start < 5 )) ||
25861                         error "cp: too long runtime $((SECONDS - start))"
25862
25863         fi
25864         # tar version 1.29+ supports SEEK_HOLE/DATA
25865         if [[ $(tar --version | head -n1 | sed "s/[^0-9]//g") -ge 129 ]]; then
25866                 start=$SECONDS
25867                 time tar cS $file - | cat > /dev/null
25868                 (( SECONDS - start < 5 )) ||
25869                         error "tar: too long runtime $((SECONDS - start))"
25870         fi
25871 }
25872 run_test 430c "lseek: external tools check"
25873
25874 test_431() { # LU-14187
25875         local file=$DIR/$tdir/$tfile
25876
25877         mkdir -p $DIR/$tdir
25878         $LFS setstripe -c 1 -i 0 $file || error "lfs setstripe failed"
25879         dd if=/dev/urandom of=$file bs=4k count=1
25880         dd if=/dev/urandom of=$file bs=4k count=1 seek=10 conv=notrunc
25881         dd if=/dev/urandom of=$file bs=4k count=1 seek=12 conv=notrunc
25882         #define OBD_FAIL_OST_RESTART_IO 0x251
25883         do_facet ost1 "$LCTL set_param fail_loc=0x251"
25884         $LFS setstripe -c 1 -i 0 $file.0 || error "lfs setstripe failed"
25885         cp $file $file.0
25886         cancel_lru_locks
25887         sync_all_data
25888         echo 3 > /proc/sys/vm/drop_caches
25889         diff  $file $file.0 || error "data diff"
25890 }
25891 run_test 431 "Restart transaction for IO"
25892
25893 cleanup_test_432() {
25894         do_facet mgs $LCTL nodemap_activate 0
25895         wait_nm_sync active
25896 }
25897
25898 test_432() {
25899         local tmpdir=$TMP/dir432
25900
25901         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
25902                 skip "Need MDS version at least 2.14.52"
25903
25904         stack_trap cleanup_test_432 EXIT
25905         mkdir $DIR/$tdir
25906         mkdir $tmpdir
25907
25908         do_facet mgs $LCTL nodemap_activate 1
25909         wait_nm_sync active
25910         do_facet mgs $LCTL nodemap_modify --name default \
25911                 --property admin --value 1
25912         do_facet mgs $LCTL nodemap_modify --name default \
25913                 --property trusted --value 1
25914         cancel_lru_locks mdc
25915         wait_nm_sync default admin_nodemap
25916         wait_nm_sync default trusted_nodemap
25917
25918         if [ $(mv $tmpdir $DIR/$tdir/ 2>&1 |
25919                grep -ci "Operation not permitted") -ne 0 ]; then
25920                 error "mv $tmpdir $DIR/$tdir/ hits 'Operation not permitted'"
25921         fi
25922 }
25923 run_test 432 "mv dir from outside Lustre"
25924
25925 prep_801() {
25926         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
25927         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
25928                 skip "Need server version at least 2.9.55"
25929
25930         start_full_debug_logging
25931 }
25932
25933 post_801() {
25934         stop_full_debug_logging
25935 }
25936
25937 barrier_stat() {
25938         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
25939                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
25940                            awk '/The barrier for/ { print $7 }')
25941                 echo $st
25942         else
25943                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
25944                 echo \'$st\'
25945         fi
25946 }
25947
25948 barrier_expired() {
25949         local expired
25950
25951         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
25952                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
25953                           awk '/will be expired/ { print $7 }')
25954         else
25955                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
25956         fi
25957
25958         echo $expired
25959 }
25960
25961 test_801a() {
25962         prep_801
25963
25964         echo "Start barrier_freeze at: $(date)"
25965         #define OBD_FAIL_BARRIER_DELAY          0x2202
25966         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
25967         # Do not reduce barrier time - See LU-11873
25968         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
25969
25970         sleep 2
25971         local b_status=$(barrier_stat)
25972         echo "Got barrier status at: $(date)"
25973         [ "$b_status" = "'freezing_p1'" ] ||
25974                 error "(1) unexpected barrier status $b_status"
25975
25976         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
25977         wait
25978         b_status=$(barrier_stat)
25979         [ "$b_status" = "'frozen'" ] ||
25980                 error "(2) unexpected barrier status $b_status"
25981
25982         local expired=$(barrier_expired)
25983         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
25984         sleep $((expired + 3))
25985
25986         b_status=$(barrier_stat)
25987         [ "$b_status" = "'expired'" ] ||
25988                 error "(3) unexpected barrier status $b_status"
25989
25990         # Do not reduce barrier time - See LU-11873
25991         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
25992                 error "(4) fail to freeze barrier"
25993
25994         b_status=$(barrier_stat)
25995         [ "$b_status" = "'frozen'" ] ||
25996                 error "(5) unexpected barrier status $b_status"
25997
25998         echo "Start barrier_thaw at: $(date)"
25999         #define OBD_FAIL_BARRIER_DELAY          0x2202
26000         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
26001         do_facet mgs $LCTL barrier_thaw $FSNAME &
26002
26003         sleep 2
26004         b_status=$(barrier_stat)
26005         echo "Got barrier status at: $(date)"
26006         [ "$b_status" = "'thawing'" ] ||
26007                 error "(6) unexpected barrier status $b_status"
26008
26009         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
26010         wait
26011         b_status=$(barrier_stat)
26012         [ "$b_status" = "'thawed'" ] ||
26013                 error "(7) unexpected barrier status $b_status"
26014
26015         #define OBD_FAIL_BARRIER_FAILURE        0x2203
26016         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
26017         do_facet mgs $LCTL barrier_freeze $FSNAME
26018
26019         b_status=$(barrier_stat)
26020         [ "$b_status" = "'failed'" ] ||
26021                 error "(8) unexpected barrier status $b_status"
26022
26023         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
26024         do_facet mgs $LCTL barrier_thaw $FSNAME
26025
26026         post_801
26027 }
26028 run_test 801a "write barrier user interfaces and stat machine"
26029
26030 test_801b() {
26031         prep_801
26032
26033         mkdir $DIR/$tdir || error "(1) fail to mkdir"
26034         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
26035         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
26036         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
26037         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
26038
26039         cancel_lru_locks mdc
26040
26041         # 180 seconds should be long enough
26042         do_facet mgs $LCTL barrier_freeze $FSNAME 180
26043
26044         local b_status=$(barrier_stat)
26045         [ "$b_status" = "'frozen'" ] ||
26046                 error "(6) unexpected barrier status $b_status"
26047
26048         mkdir $DIR/$tdir/d0/d10 &
26049         mkdir_pid=$!
26050
26051         touch $DIR/$tdir/d1/f13 &
26052         touch_pid=$!
26053
26054         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
26055         ln_pid=$!
26056
26057         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
26058         mv_pid=$!
26059
26060         rm -f $DIR/$tdir/d4/f12 &
26061         rm_pid=$!
26062
26063         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
26064
26065         # To guarantee taht the 'stat' is not blocked
26066         b_status=$(barrier_stat)
26067         [ "$b_status" = "'frozen'" ] ||
26068                 error "(8) unexpected barrier status $b_status"
26069
26070         # let above commands to run at background
26071         sleep 5
26072
26073         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
26074         ps -p $touch_pid || error "(10) touch should be blocked"
26075         ps -p $ln_pid || error "(11) link should be blocked"
26076         ps -p $mv_pid || error "(12) rename should be blocked"
26077         ps -p $rm_pid || error "(13) unlink should be blocked"
26078
26079         b_status=$(barrier_stat)
26080         [ "$b_status" = "'frozen'" ] ||
26081                 error "(14) unexpected barrier status $b_status"
26082
26083         do_facet mgs $LCTL barrier_thaw $FSNAME
26084         b_status=$(barrier_stat)
26085         [ "$b_status" = "'thawed'" ] ||
26086                 error "(15) unexpected barrier status $b_status"
26087
26088         wait $mkdir_pid || error "(16) mkdir should succeed"
26089         wait $touch_pid || error "(17) touch should succeed"
26090         wait $ln_pid || error "(18) link should succeed"
26091         wait $mv_pid || error "(19) rename should succeed"
26092         wait $rm_pid || error "(20) unlink should succeed"
26093
26094         post_801
26095 }
26096 run_test 801b "modification will be blocked by write barrier"
26097
26098 test_801c() {
26099         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26100
26101         prep_801
26102
26103         stop mds2 || error "(1) Fail to stop mds2"
26104
26105         do_facet mgs $LCTL barrier_freeze $FSNAME 30
26106
26107         local b_status=$(barrier_stat)
26108         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
26109                 do_facet mgs $LCTL barrier_thaw $FSNAME
26110                 error "(2) unexpected barrier status $b_status"
26111         }
26112
26113         do_facet mgs $LCTL barrier_rescan $FSNAME ||
26114                 error "(3) Fail to rescan barrier bitmap"
26115
26116         # Do not reduce barrier time - See LU-11873
26117         do_facet mgs $LCTL barrier_freeze $FSNAME 20
26118
26119         b_status=$(barrier_stat)
26120         [ "$b_status" = "'frozen'" ] ||
26121                 error "(4) unexpected barrier status $b_status"
26122
26123         do_facet mgs $LCTL barrier_thaw $FSNAME
26124         b_status=$(barrier_stat)
26125         [ "$b_status" = "'thawed'" ] ||
26126                 error "(5) unexpected barrier status $b_status"
26127
26128         local devname=$(mdsdevname 2)
26129
26130         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
26131
26132         do_facet mgs $LCTL barrier_rescan $FSNAME ||
26133                 error "(7) Fail to rescan barrier bitmap"
26134
26135         post_801
26136 }
26137 run_test 801c "rescan barrier bitmap"
26138
26139 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
26140 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
26141 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
26142 saved_MOUNT_OPTS=$MOUNT_OPTS
26143
26144 cleanup_802a() {
26145         trap 0
26146
26147         stopall
26148         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
26149         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
26150         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
26151         MOUNT_OPTS=$saved_MOUNT_OPTS
26152         setupall
26153 }
26154
26155 test_802a() {
26156         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
26157         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
26158         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
26159                 skip "Need server version at least 2.9.55"
26160
26161         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
26162
26163         mkdir $DIR/$tdir || error "(1) fail to mkdir"
26164
26165         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
26166                 error "(2) Fail to copy"
26167
26168         trap cleanup_802a EXIT
26169
26170         # sync by force before remount as readonly
26171         sync; sync_all_data; sleep 3; sync_all_data
26172
26173         stopall
26174
26175         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
26176         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
26177         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
26178
26179         echo "Mount the server as read only"
26180         setupall server_only || error "(3) Fail to start servers"
26181
26182         echo "Mount client without ro should fail"
26183         mount_client $MOUNT &&
26184                 error "(4) Mount client without 'ro' should fail"
26185
26186         echo "Mount client with ro should succeed"
26187         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
26188         mount_client $MOUNT ||
26189                 error "(5) Mount client with 'ro' should succeed"
26190
26191         echo "Modify should be refused"
26192         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
26193
26194         echo "Read should be allowed"
26195         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
26196                 error "(7) Read should succeed under ro mode"
26197
26198         cleanup_802a
26199 }
26200 run_test 802a "simulate readonly device"
26201
26202 test_802b() {
26203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26204         remote_mds_nodsh && skip "remote MDS with nodsh"
26205
26206         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
26207                 skip "readonly option not available"
26208
26209         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
26210
26211         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
26212                 error "(2) Fail to copy"
26213
26214         # write back all cached data before setting MDT to readonly
26215         cancel_lru_locks
26216         sync_all_data
26217
26218         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
26219         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
26220
26221         echo "Modify should be refused"
26222         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
26223
26224         echo "Read should be allowed"
26225         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
26226                 error "(7) Read should succeed under ro mode"
26227
26228         # disable readonly
26229         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
26230 }
26231 run_test 802b "be able to set MDTs to readonly"
26232
26233 test_803a() {
26234         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26235         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
26236                 skip "MDS needs to be newer than 2.10.54"
26237
26238         mkdir_on_mdt0 $DIR/$tdir
26239         # Create some objects on all MDTs to trigger related logs objects
26240         for idx in $(seq $MDSCOUNT); do
26241                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
26242                         $DIR/$tdir/dir${idx} ||
26243                         error "Fail to create $DIR/$tdir/dir${idx}"
26244         done
26245
26246         sync; sleep 3
26247         wait_delete_completed # ensure old test cleanups are finished
26248         echo "before create:"
26249         $LFS df -i $MOUNT
26250         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26251
26252         for i in {1..10}; do
26253                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
26254                         error "Fail to create $DIR/$tdir/foo$i"
26255         done
26256
26257         sync; sleep 3
26258         echo "after create:"
26259         $LFS df -i $MOUNT
26260         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26261
26262         # allow for an llog to be cleaned up during the test
26263         [ $after_used -ge $((before_used + 10 - 1)) ] ||
26264                 error "before ($before_used) + 10 > after ($after_used)"
26265
26266         for i in {1..10}; do
26267                 rm -rf $DIR/$tdir/foo$i ||
26268                         error "Fail to remove $DIR/$tdir/foo$i"
26269         done
26270
26271         sleep 3 # avoid MDT return cached statfs
26272         wait_delete_completed
26273         echo "after unlink:"
26274         $LFS df -i $MOUNT
26275         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26276
26277         # allow for an llog to be created during the test
26278         [ $after_used -le $((before_used + 1)) ] ||
26279                 error "after ($after_used) > before ($before_used) + 1"
26280 }
26281 run_test 803a "verify agent object for remote object"
26282
26283 test_803b() {
26284         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26285         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
26286                 skip "MDS needs to be newer than 2.13.56"
26287         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26288
26289         for i in $(seq 0 $((MDSCOUNT - 1))); do
26290                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
26291         done
26292
26293         local before=0
26294         local after=0
26295
26296         local tmp
26297
26298         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
26299         for i in $(seq 0 $((MDSCOUNT - 1))); do
26300                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
26301                         awk '/getattr/ { print $2 }')
26302                 before=$((before + tmp))
26303         done
26304         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
26305         for i in $(seq 0 $((MDSCOUNT - 1))); do
26306                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
26307                         awk '/getattr/ { print $2 }')
26308                 after=$((after + tmp))
26309         done
26310
26311         [ $before -eq $after ] || error "getattr count $before != $after"
26312 }
26313 run_test 803b "remote object can getattr from cache"
26314
26315 test_804() {
26316         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26317         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
26318                 skip "MDS needs to be newer than 2.10.54"
26319         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
26320
26321         mkdir -p $DIR/$tdir
26322         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
26323                 error "Fail to create $DIR/$tdir/dir0"
26324
26325         local fid=$($LFS path2fid $DIR/$tdir/dir0)
26326         local dev=$(mdsdevname 2)
26327
26328         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26329                 grep ${fid} || error "NOT found agent entry for dir0"
26330
26331         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
26332                 error "Fail to create $DIR/$tdir/dir1"
26333
26334         touch $DIR/$tdir/dir1/foo0 ||
26335                 error "Fail to create $DIR/$tdir/dir1/foo0"
26336         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
26337         local rc=0
26338
26339         for idx in $(seq $MDSCOUNT); do
26340                 dev=$(mdsdevname $idx)
26341                 do_facet mds${idx} \
26342                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26343                         grep ${fid} && rc=$idx
26344         done
26345
26346         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
26347                 error "Fail to rename foo0 to foo1"
26348         if [ $rc -eq 0 ]; then
26349                 for idx in $(seq $MDSCOUNT); do
26350                         dev=$(mdsdevname $idx)
26351                         do_facet mds${idx} \
26352                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26353                         grep ${fid} && rc=$idx
26354                 done
26355         fi
26356
26357         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
26358                 error "Fail to rename foo1 to foo2"
26359         if [ $rc -eq 0 ]; then
26360                 for idx in $(seq $MDSCOUNT); do
26361                         dev=$(mdsdevname $idx)
26362                         do_facet mds${idx} \
26363                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26364                         grep ${fid} && rc=$idx
26365                 done
26366         fi
26367
26368         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
26369
26370         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
26371                 error "Fail to link to $DIR/$tdir/dir1/foo2"
26372         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
26373                 error "Fail to rename foo2 to foo0"
26374         unlink $DIR/$tdir/dir1/foo0 ||
26375                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
26376         rm -rf $DIR/$tdir/dir0 ||
26377                 error "Fail to rm $DIR/$tdir/dir0"
26378
26379         for idx in $(seq $MDSCOUNT); do
26380                 dev=$(mdsdevname $idx)
26381                 rc=0
26382
26383                 stop mds${idx}
26384                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
26385                         rc=$?
26386                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
26387                         error "mount mds$idx failed"
26388                 df $MOUNT > /dev/null 2>&1
26389
26390                 # e2fsck should not return error
26391                 [ $rc -eq 0 ] ||
26392                         error "e2fsck detected error on MDT${idx}: rc=$rc"
26393         done
26394 }
26395 run_test 804 "verify agent entry for remote entry"
26396
26397 cleanup_805() {
26398         do_facet $SINGLEMDS zfs set quota=$old $fsset
26399         unlinkmany $DIR/$tdir/f- 1000000
26400         trap 0
26401 }
26402
26403 test_805() {
26404         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
26405         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
26406         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
26407                 skip "netfree not implemented before 0.7"
26408         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
26409                 skip "Need MDS version at least 2.10.57"
26410
26411         local fsset
26412         local freekb
26413         local usedkb
26414         local old
26415         local quota
26416         local pref="osd-zfs.$FSNAME-MDT0000."
26417
26418         # limit available space on MDS dataset to meet nospace issue
26419         # quickly. then ZFS 0.7.2 can use reserved space if asked
26420         # properly (using netfree flag in osd_declare_destroy()
26421         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
26422         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
26423                 gawk '{print $3}')
26424         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
26425         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
26426         let "usedkb=usedkb-freekb"
26427         let "freekb=freekb/2"
26428         if let "freekb > 5000"; then
26429                 let "freekb=5000"
26430         fi
26431         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
26432         trap cleanup_805 EXIT
26433         mkdir_on_mdt0 $DIR/$tdir
26434         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
26435                 error "Can't set PFL layout"
26436         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
26437         rm -rf $DIR/$tdir || error "not able to remove"
26438         do_facet $SINGLEMDS zfs set quota=$old $fsset
26439         trap 0
26440 }
26441 run_test 805 "ZFS can remove from full fs"
26442
26443 # Size-on-MDS test
26444 check_lsom_data()
26445 {
26446         local file=$1
26447         local expect=$(stat -c %s $file)
26448
26449         check_lsom_size $1 $expect
26450
26451         local blocks=$($LFS getsom -b $file)
26452         expect=$(stat -c %b $file)
26453         [[ $blocks == $expect ]] ||
26454                 error "$file expected blocks: $expect, got: $blocks"
26455 }
26456
26457 check_lsom_size()
26458 {
26459         local size
26460         local expect=$2
26461
26462         cancel_lru_locks mdc
26463
26464         size=$($LFS getsom -s $1)
26465         [[ $size == $expect ]] ||
26466                 error "$file expected size: $expect, got: $size"
26467 }
26468
26469 test_806() {
26470         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26471                 skip "Need MDS version at least 2.11.52"
26472
26473         local bs=1048576
26474
26475         touch $DIR/$tfile || error "touch $tfile failed"
26476
26477         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
26478         save_lustre_params client "llite.*.xattr_cache" > $save
26479         lctl set_param llite.*.xattr_cache=0
26480         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
26481
26482         # single-threaded write
26483         echo "Test SOM for single-threaded write"
26484         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
26485                 error "write $tfile failed"
26486         check_lsom_size $DIR/$tfile $bs
26487
26488         local num=32
26489         local size=$(($num * $bs))
26490         local offset=0
26491         local i
26492
26493         echo "Test SOM for single client multi-threaded($num) write"
26494         $TRUNCATE $DIR/$tfile 0
26495         for ((i = 0; i < $num; i++)); do
26496                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26497                 local pids[$i]=$!
26498                 offset=$((offset + $bs))
26499         done
26500         for (( i=0; i < $num; i++ )); do
26501                 wait ${pids[$i]}
26502         done
26503         check_lsom_size $DIR/$tfile $size
26504
26505         $TRUNCATE $DIR/$tfile 0
26506         for ((i = 0; i < $num; i++)); do
26507                 offset=$((offset - $bs))
26508                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26509                 local pids[$i]=$!
26510         done
26511         for (( i=0; i < $num; i++ )); do
26512                 wait ${pids[$i]}
26513         done
26514         check_lsom_size $DIR/$tfile $size
26515
26516         # multi-client writes
26517         num=$(get_node_count ${CLIENTS//,/ })
26518         size=$(($num * $bs))
26519         offset=0
26520         i=0
26521
26522         echo "Test SOM for multi-client ($num) writes"
26523         $TRUNCATE $DIR/$tfile 0
26524         for client in ${CLIENTS//,/ }; do
26525                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26526                 local pids[$i]=$!
26527                 i=$((i + 1))
26528                 offset=$((offset + $bs))
26529         done
26530         for (( i=0; i < $num; i++ )); do
26531                 wait ${pids[$i]}
26532         done
26533         check_lsom_size $DIR/$tfile $offset
26534
26535         i=0
26536         $TRUNCATE $DIR/$tfile 0
26537         for client in ${CLIENTS//,/ }; do
26538                 offset=$((offset - $bs))
26539                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26540                 local pids[$i]=$!
26541                 i=$((i + 1))
26542         done
26543         for (( i=0; i < $num; i++ )); do
26544                 wait ${pids[$i]}
26545         done
26546         check_lsom_size $DIR/$tfile $size
26547
26548         # verify truncate
26549         echo "Test SOM for truncate"
26550         $TRUNCATE $DIR/$tfile 1048576
26551         check_lsom_size $DIR/$tfile 1048576
26552         $TRUNCATE $DIR/$tfile 1234
26553         check_lsom_size $DIR/$tfile 1234
26554
26555         # verify SOM blocks count
26556         echo "Verify SOM block count"
26557         $TRUNCATE $DIR/$tfile 0
26558         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
26559                 error "failed to write file $tfile"
26560         check_lsom_data $DIR/$tfile
26561 }
26562 run_test 806 "Verify Lazy Size on MDS"
26563
26564 test_807() {
26565         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
26566         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26567                 skip "Need MDS version at least 2.11.52"
26568
26569         # Registration step
26570         changelog_register || error "changelog_register failed"
26571         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
26572         changelog_users $SINGLEMDS | grep -q $cl_user ||
26573                 error "User $cl_user not found in changelog_users"
26574
26575         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
26576         save_lustre_params client "llite.*.xattr_cache" > $save
26577         lctl set_param llite.*.xattr_cache=0
26578         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
26579
26580         rm -rf $DIR/$tdir || error "rm $tdir failed"
26581         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
26582         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
26583         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
26584         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
26585                 error "truncate $tdir/trunc failed"
26586
26587         local bs=1048576
26588         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
26589                 error "write $tfile failed"
26590
26591         # multi-client wirtes
26592         local num=$(get_node_count ${CLIENTS//,/ })
26593         local offset=0
26594         local i=0
26595
26596         echo "Test SOM for multi-client ($num) writes"
26597         touch $DIR/$tfile || error "touch $tfile failed"
26598         $TRUNCATE $DIR/$tfile 0
26599         for client in ${CLIENTS//,/ }; do
26600                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26601                 local pids[$i]=$!
26602                 i=$((i + 1))
26603                 offset=$((offset + $bs))
26604         done
26605         for (( i=0; i < $num; i++ )); do
26606                 wait ${pids[$i]}
26607         done
26608
26609         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
26610         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
26611         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
26612         check_lsom_data $DIR/$tdir/trunc
26613         check_lsom_data $DIR/$tdir/single_dd
26614         check_lsom_data $DIR/$tfile
26615
26616         rm -rf $DIR/$tdir
26617         # Deregistration step
26618         changelog_deregister || error "changelog_deregister failed"
26619 }
26620 run_test 807 "verify LSOM syncing tool"
26621
26622 check_som_nologged()
26623 {
26624         local lines=$($LFS changelog $FSNAME-MDT0000 |
26625                 grep 'x=trusted.som' | wc -l)
26626         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
26627 }
26628
26629 test_808() {
26630         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
26631                 skip "Need MDS version at least 2.11.55"
26632
26633         # Registration step
26634         changelog_register || error "changelog_register failed"
26635
26636         touch $DIR/$tfile || error "touch $tfile failed"
26637         check_som_nologged
26638
26639         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
26640                 error "write $tfile failed"
26641         check_som_nologged
26642
26643         $TRUNCATE $DIR/$tfile 1234
26644         check_som_nologged
26645
26646         $TRUNCATE $DIR/$tfile 1048576
26647         check_som_nologged
26648
26649         # Deregistration step
26650         changelog_deregister || error "changelog_deregister failed"
26651 }
26652 run_test 808 "Check trusted.som xattr not logged in Changelogs"
26653
26654 check_som_nodata()
26655 {
26656         $LFS getsom $1
26657         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
26658 }
26659
26660 test_809() {
26661         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
26662                 skip "Need MDS version at least 2.11.56"
26663
26664         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
26665                 error "failed to create DoM-only file $DIR/$tfile"
26666         touch $DIR/$tfile || error "touch $tfile failed"
26667         check_som_nodata $DIR/$tfile
26668
26669         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
26670                 error "write $tfile failed"
26671         check_som_nodata $DIR/$tfile
26672
26673         $TRUNCATE $DIR/$tfile 1234
26674         check_som_nodata $DIR/$tfile
26675
26676         $TRUNCATE $DIR/$tfile 4097
26677         check_som_nodata $DIR/$file
26678 }
26679 run_test 809 "Verify no SOM xattr store for DoM-only files"
26680
26681 test_810() {
26682         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26683         $GSS && skip_env "could not run with gss"
26684         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
26685                 skip "OST < 2.12.58 doesn't align checksum"
26686
26687         set_checksums 1
26688         stack_trap "set_checksums $ORIG_CSUM" EXIT
26689         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
26690
26691         local csum
26692         local before
26693         local after
26694         for csum in $CKSUM_TYPES; do
26695                 #define OBD_FAIL_OSC_NO_GRANT   0x411
26696                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
26697                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
26698                         eval set -- $i
26699                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
26700                         before=$(md5sum $DIR/$tfile)
26701                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
26702                         after=$(md5sum $DIR/$tfile)
26703                         [ "$before" == "$after" ] ||
26704                                 error "$csum: $before != $after bs=$1 seek=$2"
26705                 done
26706         done
26707 }
26708 run_test 810 "partial page writes on ZFS (LU-11663)"
26709
26710 test_812a() {
26711         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
26712                 skip "OST < 2.12.51 doesn't support this fail_loc"
26713
26714         $LFS setstripe -c 1 -i 0 $DIR/$tfile
26715         # ensure ost1 is connected
26716         stat $DIR/$tfile >/dev/null || error "can't stat"
26717         wait_osc_import_state client ost1 FULL
26718         # no locks, no reqs to let the connection idle
26719         cancel_lru_locks osc
26720
26721         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
26722 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
26723         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
26724         wait_osc_import_state client ost1 CONNECTING
26725         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
26726
26727         stat $DIR/$tfile >/dev/null || error "can't stat file"
26728 }
26729 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
26730
26731 test_812b() { # LU-12378
26732         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
26733                 skip "OST < 2.12.51 doesn't support this fail_loc"
26734
26735         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
26736         # ensure ost1 is connected
26737         stat $DIR/$tfile >/dev/null || error "can't stat"
26738         wait_osc_import_state client ost1 FULL
26739         # no locks, no reqs to let the connection idle
26740         cancel_lru_locks osc
26741
26742         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
26743 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
26744         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
26745         wait_osc_import_state client ost1 CONNECTING
26746         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
26747
26748         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
26749         wait_osc_import_state client ost1 IDLE
26750 }
26751 run_test 812b "do not drop no resend request for idle connect"
26752
26753 test_812c() {
26754         local old
26755
26756         old=$($LCTL get_param -n osc.*.idle_timeout | head -n 1)
26757
26758         $LFS setstripe -c 1 -o 0 $DIR/$tfile
26759         $LFS getstripe $DIR/$tfile
26760         $LCTL set_param osc.*.idle_timeout=10
26761         stack_trap "$LCTL set_param osc.*.idle_timeout=$old" EXIT
26762         # ensure ost1 is connected
26763         stat $DIR/$tfile >/dev/null || error "can't stat"
26764         wait_osc_import_state client ost1 FULL
26765         # no locks, no reqs to let the connection idle
26766         cancel_lru_locks osc
26767
26768 #define OBD_FAIL_PTLRPC_IDLE_RACE        0x533
26769         $LCTL set_param fail_loc=0x80000533
26770         sleep 15
26771         dd if=/dev/zero of=$DIR/$tfile count=1 conv=sync || error "dd failed"
26772 }
26773 run_test 812c "idle import vs lock enqueue race"
26774
26775 test_813() {
26776         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
26777         [ -z "$file_heat_sav" ] && skip "no file heat support"
26778
26779         local readsample
26780         local writesample
26781         local readbyte
26782         local writebyte
26783         local readsample1
26784         local writesample1
26785         local readbyte1
26786         local writebyte1
26787
26788         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
26789         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
26790
26791         $LCTL set_param -n llite.*.file_heat=1
26792         echo "Turn on file heat"
26793         echo "Period second: $period_second, Decay percentage: $decay_pct"
26794
26795         echo "QQQQ" > $DIR/$tfile
26796         echo "QQQQ" > $DIR/$tfile
26797         echo "QQQQ" > $DIR/$tfile
26798         cat $DIR/$tfile > /dev/null
26799         cat $DIR/$tfile > /dev/null
26800         cat $DIR/$tfile > /dev/null
26801         cat $DIR/$tfile > /dev/null
26802
26803         local out=$($LFS heat_get $DIR/$tfile)
26804
26805         $LFS heat_get $DIR/$tfile
26806         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
26807         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
26808         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
26809         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
26810
26811         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
26812         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
26813         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
26814         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
26815
26816         sleep $((period_second + 3))
26817         echo "Sleep $((period_second + 3)) seconds..."
26818         # The recursion formula to calculate the heat of the file f is as
26819         # follow:
26820         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
26821         # Where Hi is the heat value in the period between time points i*I and
26822         # (i+1)*I; Ci is the access count in the period; the symbol P refers
26823         # to the weight of Ci.
26824         out=$($LFS heat_get $DIR/$tfile)
26825         $LFS heat_get $DIR/$tfile
26826         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
26827         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
26828         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
26829         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
26830
26831         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
26832                 error "read sample ($readsample) is wrong"
26833         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
26834                 error "write sample ($writesample) is wrong"
26835         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
26836                 error "read bytes ($readbyte) is wrong"
26837         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
26838                 error "write bytes ($writebyte) is wrong"
26839
26840         echo "QQQQ" > $DIR/$tfile
26841         echo "QQQQ" > $DIR/$tfile
26842         echo "QQQQ" > $DIR/$tfile
26843         cat $DIR/$tfile > /dev/null
26844         cat $DIR/$tfile > /dev/null
26845         cat $DIR/$tfile > /dev/null
26846         cat $DIR/$tfile > /dev/null
26847
26848         sleep $((period_second + 3))
26849         echo "Sleep $((period_second + 3)) seconds..."
26850
26851         out=$($LFS heat_get $DIR/$tfile)
26852         $LFS heat_get $DIR/$tfile
26853         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
26854         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
26855         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
26856         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
26857
26858         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
26859                 4 * $decay_pct) / 100") -eq 1 ] ||
26860                 error "read sample ($readsample1) is wrong"
26861         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
26862                 3 * $decay_pct) / 100") -eq 1 ] ||
26863                 error "write sample ($writesample1) is wrong"
26864         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
26865                 20 * $decay_pct) / 100") -eq 1 ] ||
26866                 error "read bytes ($readbyte1) is wrong"
26867         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
26868                 15 * $decay_pct) / 100") -eq 1 ] ||
26869                 error "write bytes ($writebyte1) is wrong"
26870
26871         echo "Turn off file heat for the file $DIR/$tfile"
26872         $LFS heat_set -o $DIR/$tfile
26873
26874         echo "QQQQ" > $DIR/$tfile
26875         echo "QQQQ" > $DIR/$tfile
26876         echo "QQQQ" > $DIR/$tfile
26877         cat $DIR/$tfile > /dev/null
26878         cat $DIR/$tfile > /dev/null
26879         cat $DIR/$tfile > /dev/null
26880         cat $DIR/$tfile > /dev/null
26881
26882         out=$($LFS heat_get $DIR/$tfile)
26883         $LFS heat_get $DIR/$tfile
26884         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
26885         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
26886         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
26887         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
26888
26889         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
26890         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
26891         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
26892         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
26893
26894         echo "Trun on file heat for the file $DIR/$tfile"
26895         $LFS heat_set -O $DIR/$tfile
26896
26897         echo "QQQQ" > $DIR/$tfile
26898         echo "QQQQ" > $DIR/$tfile
26899         echo "QQQQ" > $DIR/$tfile
26900         cat $DIR/$tfile > /dev/null
26901         cat $DIR/$tfile > /dev/null
26902         cat $DIR/$tfile > /dev/null
26903         cat $DIR/$tfile > /dev/null
26904
26905         out=$($LFS heat_get $DIR/$tfile)
26906         $LFS heat_get $DIR/$tfile
26907         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
26908         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
26909         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
26910         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
26911
26912         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
26913         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
26914         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
26915         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
26916
26917         $LFS heat_set -c $DIR/$tfile
26918         $LCTL set_param -n llite.*.file_heat=0
26919         echo "Turn off file heat support for the Lustre filesystem"
26920
26921         echo "QQQQ" > $DIR/$tfile
26922         echo "QQQQ" > $DIR/$tfile
26923         echo "QQQQ" > $DIR/$tfile
26924         cat $DIR/$tfile > /dev/null
26925         cat $DIR/$tfile > /dev/null
26926         cat $DIR/$tfile > /dev/null
26927         cat $DIR/$tfile > /dev/null
26928
26929         out=$($LFS heat_get $DIR/$tfile)
26930         $LFS heat_get $DIR/$tfile
26931         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
26932         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
26933         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
26934         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
26935
26936         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
26937         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
26938         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
26939         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
26940
26941         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
26942         rm -f $DIR/$tfile
26943 }
26944 run_test 813 "File heat verfication"
26945
26946 test_814()
26947 {
26948         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
26949         echo -n y >> $DIR/$tfile
26950         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
26951         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
26952 }
26953 run_test 814 "sparse cp works as expected (LU-12361)"
26954
26955 test_815()
26956 {
26957         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
26958         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
26959 }
26960 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
26961
26962 test_816() {
26963         local ost1_imp=$(get_osc_import_name client ost1)
26964         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
26965                          cut -d'.' -f2)
26966
26967         $LFS setstripe -c 1 -i 0 $DIR/$tfile
26968         # ensure ost1 is connected
26969
26970         stat $DIR/$tfile >/dev/null || error "can't stat"
26971         wait_osc_import_state client ost1 FULL
26972         # no locks, no reqs to let the connection idle
26973         cancel_lru_locks osc
26974         lru_resize_disable osc
26975         local before
26976         local now
26977         before=$($LCTL get_param -n \
26978                  ldlm.namespaces.$imp_name.lru_size)
26979
26980         wait_osc_import_state client ost1 IDLE
26981         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
26982         now=$($LCTL get_param -n \
26983               ldlm.namespaces.$imp_name.lru_size)
26984         [ $before == $now ] || error "lru_size changed $before != $now"
26985 }
26986 run_test 816 "do not reset lru_resize on idle reconnect"
26987
26988 cleanup_817() {
26989         umount $tmpdir
26990         exportfs -u localhost:$DIR/nfsexp
26991         rm -rf $DIR/nfsexp
26992 }
26993
26994 test_817() {
26995         systemctl restart nfs-server.service || skip "failed to restart nfsd"
26996
26997         mkdir -p $DIR/nfsexp
26998         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
26999                 error "failed to export nfs"
27000
27001         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
27002         stack_trap cleanup_817 EXIT
27003
27004         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
27005                 error "failed to mount nfs to $tmpdir"
27006
27007         cp /bin/true $tmpdir
27008         $DIR/nfsexp/true || error "failed to execute 'true' command"
27009 }
27010 run_test 817 "nfsd won't cache write lock for exec file"
27011
27012 test_818() {
27013         mkdir $DIR/$tdir
27014         $LFS setstripe -c1 -i0 $DIR/$tfile
27015         $LFS setstripe -c1 -i1 $DIR/$tfile
27016         stop $SINGLEMDS
27017         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
27018         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
27019         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
27020                 error "start $SINGLEMDS failed"
27021         rm -rf $DIR/$tdir
27022 }
27023 run_test 818 "unlink with failed llog"
27024
27025 test_819a() {
27026         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27027         cancel_lru_locks osc
27028         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
27029         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
27030         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
27031         rm -f $TDIR/$tfile
27032 }
27033 run_test 819a "too big niobuf in read"
27034
27035 test_819b() {
27036         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
27037         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
27038         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27039         cancel_lru_locks osc
27040         sleep 1
27041         rm -f $TDIR/$tfile
27042 }
27043 run_test 819b "too big niobuf in write"
27044
27045
27046 function test_820_start_ost() {
27047         sleep 5
27048
27049         for num in $(seq $OSTCOUNT); do
27050                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
27051         done
27052 }
27053
27054 test_820() {
27055         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27056
27057         mkdir $DIR/$tdir
27058         umount_client $MOUNT || error "umount failed"
27059         for num in $(seq $OSTCOUNT); do
27060                 stop ost$num
27061         done
27062
27063         # mount client with no active OSTs
27064         # so that the client can't initialize max LOV EA size
27065         # from OSC notifications
27066         mount_client $MOUNT || error "mount failed"
27067         # delay OST starting to keep this 0 max EA size for a while
27068         test_820_start_ost &
27069
27070         # create a directory on MDS2
27071         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
27072                 error "Failed to create directory"
27073         # open intent should update default EA size
27074         # see mdc_update_max_ea_from_body()
27075         # notice this is the very first RPC to MDS2
27076         out=$(cp /etc/services $DIR/$tdir/mds2 2>&1)
27077         ret=$?
27078         echo $out
27079         # With SSK, this situation can lead to -EPERM being returned.
27080         # In that case, simply retry.
27081         if [ $ret -ne 0 ] && $SHARED_KEY; then
27082                 if echo "$out" | grep -q "not permitted"; then
27083                         cp /etc/services $DIR/$tdir/mds2
27084                         ret=$?
27085                 fi
27086         fi
27087         [ $ret -eq 0 ] || error "Failed to copy files to mds$n"
27088 }
27089 run_test 820 "update max EA from open intent"
27090
27091 test_822() {
27092         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
27093
27094         save_lustre_params mds1 \
27095                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
27096         do_facet $SINGLEMDS "$LCTL set_param -n \
27097                         osp.$FSNAME-OST*MDT0000.max_create_count=0"
27098         do_facet $SINGLEMDS "$LCTL set_param -n \
27099                         osp.$FSNAME-OST0000*MDT0000.max_create_count=20000"
27100
27101         # wait for statfs update to clear OS_STATFS_NOPRECREATE
27102         local maxage=$(do_facet mds1 $LCTL get_param -n \
27103                        osp.$FSNAME-OST0000*MDT0000.maxage)
27104         sleep $((maxage + 1))
27105
27106         #define OBD_FAIL_NET_ERROR_RPC          0x532
27107         do_facet mds1 "$LCTL set_param fail_loc=0x80000532 fail_val=5"
27108
27109         stack_trap "restore_lustre_params < $p; rm $p"
27110
27111         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
27112                       osp.$FSNAME-OST0000*MDT0000.create_count")
27113         for i in $(seq 1 $count); do
27114                 touch $DIR/$tfile.${i} || error "touch failed"
27115         done
27116 }
27117 run_test 822 "test precreate failure"
27118
27119 #
27120 # tests that do cleanup/setup should be run at the end
27121 #
27122
27123 test_900() {
27124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27125         local ls
27126
27127         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
27128         $LCTL set_param fail_loc=0x903
27129
27130         cancel_lru_locks MGC
27131
27132         FAIL_ON_ERROR=true cleanup
27133         FAIL_ON_ERROR=true setup
27134 }
27135 run_test 900 "umount should not race with any mgc requeue thread"
27136
27137 # LUS-6253/LU-11185
27138 test_901() {
27139         local oldc
27140         local newc
27141         local olds
27142         local news
27143         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27144
27145         # some get_param have a bug to handle dot in param name
27146         cancel_lru_locks MGC
27147         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
27148         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
27149         umount_client $MOUNT || error "umount failed"
27150         mount_client $MOUNT || error "mount failed"
27151         cancel_lru_locks MGC
27152         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
27153         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
27154
27155         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
27156         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
27157
27158         return 0
27159 }
27160 run_test 901 "don't leak a mgc lock on client umount"
27161
27162 # LU-13377
27163 test_902() {
27164         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
27165                 skip "client does not have LU-13377 fix"
27166         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
27167         $LCTL set_param fail_loc=0x1415
27168         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27169         cancel_lru_locks osc
27170         rm -f $DIR/$tfile
27171 }
27172 run_test 902 "test short write doesn't hang lustre"
27173
27174 complete $SECONDS
27175 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
27176 check_and_cleanup_lustre
27177 if [ "$I_MOUNTED" != "yes" ]; then
27178         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
27179 fi
27180 exit_status