Whamcloud - gitweb
LU-14889 lproc: Add server checksum_type
[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  15   (min)"
81 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 60i 64b 68 71 115 135 136 230d 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 #LU-14949
3725 test_31r() {
3726         touch $DIR/$tfile.target
3727         touch $DIR/$tfile.source
3728
3729         #OBD_FAIL_LLITE_OPEN_DELAY 0x1419
3730         $LCTL set_param fail_loc=0x1419 fail_val=3
3731         cat $DIR/$tfile.target &
3732         CATPID=$!
3733
3734         # Guarantee open is waiting before we get here
3735         sleep 1
3736         mv $DIR/$tfile.source $DIR/$tfile.target
3737
3738         wait $CATPID
3739         RC=$?
3740         if [[ $RC -ne 0 ]]; then
3741                 error "open with cat failed, rc=$RC"
3742         fi
3743 }
3744 run_test 31r "open-rename(replace) race"
3745
3746 cleanup_test32_mount() {
3747         local rc=0
3748         trap 0
3749         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3750         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3751         losetup -d $loopdev || true
3752         rm -rf $DIR/$tdir
3753         return $rc
3754 }
3755
3756 test_32a() {
3757         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3758
3759         echo "== more mountpoints and symlinks ================="
3760         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3761         trap cleanup_test32_mount EXIT
3762         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3763         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3764                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3765         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3766                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3767         cleanup_test32_mount
3768 }
3769 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3770
3771 test_32b() {
3772         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3773
3774         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3775         trap cleanup_test32_mount EXIT
3776         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3777         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3778                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3779         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3780                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3781         cleanup_test32_mount
3782 }
3783 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3784
3785 test_32c() {
3786         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3787
3788         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3789         trap cleanup_test32_mount EXIT
3790         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3791         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3792                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3793         test_mkdir -p $DIR/$tdir/d2/test_dir
3794         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3795                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3796         cleanup_test32_mount
3797 }
3798 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3799
3800 test_32d() {
3801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3802
3803         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3804         trap cleanup_test32_mount EXIT
3805         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3806         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3807                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3808         test_mkdir -p $DIR/$tdir/d2/test_dir
3809         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3810                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3811         cleanup_test32_mount
3812 }
3813 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3814
3815 test_32e() {
3816         rm -fr $DIR/$tdir
3817         test_mkdir -p $DIR/$tdir/tmp
3818         local tmp_dir=$DIR/$tdir/tmp
3819         ln -s $DIR/$tdir $tmp_dir/symlink11
3820         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3821         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3822         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3823 }
3824 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3825
3826 test_32f() {
3827         rm -fr $DIR/$tdir
3828         test_mkdir -p $DIR/$tdir/tmp
3829         local tmp_dir=$DIR/$tdir/tmp
3830         ln -s $DIR/$tdir $tmp_dir/symlink11
3831         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3832         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3833         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3834 }
3835 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3836
3837 test_32g() {
3838         local tmp_dir=$DIR/$tdir/tmp
3839         test_mkdir -p $tmp_dir
3840         test_mkdir $DIR/${tdir}2
3841         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3842         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3843         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3844         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3845         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3846         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3847 }
3848 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3849
3850 test_32h() {
3851         rm -fr $DIR/$tdir $DIR/${tdir}2
3852         tmp_dir=$DIR/$tdir/tmp
3853         test_mkdir -p $tmp_dir
3854         test_mkdir $DIR/${tdir}2
3855         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3856         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3857         ls $tmp_dir/symlink12 || error "listing symlink12"
3858         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3859 }
3860 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3861
3862 test_32i() {
3863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3864
3865         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3866         trap cleanup_test32_mount EXIT
3867         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3868         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3869                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3870         touch $DIR/$tdir/test_file
3871         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3872                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3873         cleanup_test32_mount
3874 }
3875 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3876
3877 test_32j() {
3878         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3879
3880         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3881         trap cleanup_test32_mount EXIT
3882         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3883         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3884                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3885         touch $DIR/$tdir/test_file
3886         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3887                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3888         cleanup_test32_mount
3889 }
3890 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3891
3892 test_32k() {
3893         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3894
3895         rm -fr $DIR/$tdir
3896         trap cleanup_test32_mount EXIT
3897         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3898         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3899                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3900         test_mkdir -p $DIR/$tdir/d2
3901         touch $DIR/$tdir/d2/test_file || error "touch failed"
3902         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3903                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3904         cleanup_test32_mount
3905 }
3906 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3907
3908 test_32l() {
3909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3910
3911         rm -fr $DIR/$tdir
3912         trap cleanup_test32_mount EXIT
3913         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3914         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3915                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3916         test_mkdir -p $DIR/$tdir/d2
3917         touch $DIR/$tdir/d2/test_file || error "touch failed"
3918         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3919                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3920         cleanup_test32_mount
3921 }
3922 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3923
3924 test_32m() {
3925         rm -fr $DIR/d32m
3926         test_mkdir -p $DIR/d32m/tmp
3927         TMP_DIR=$DIR/d32m/tmp
3928         ln -s $DIR $TMP_DIR/symlink11
3929         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3930         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3931                 error "symlink11 not a link"
3932         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3933                 error "symlink01 not a link"
3934 }
3935 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3936
3937 test_32n() {
3938         rm -fr $DIR/d32n
3939         test_mkdir -p $DIR/d32n/tmp
3940         TMP_DIR=$DIR/d32n/tmp
3941         ln -s $DIR $TMP_DIR/symlink11
3942         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3943         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3944         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3945 }
3946 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3947
3948 test_32o() {
3949         touch $DIR/$tfile
3950         test_mkdir -p $DIR/d32o/tmp
3951         TMP_DIR=$DIR/d32o/tmp
3952         ln -s $DIR/$tfile $TMP_DIR/symlink12
3953         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3954         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3955                 error "symlink12 not a link"
3956         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3957         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3958                 error "$DIR/d32o/tmp/symlink12 not file type"
3959         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3960                 error "$DIR/d32o/symlink02 not file type"
3961 }
3962 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3963
3964 test_32p() {
3965         log 32p_1
3966         rm -fr $DIR/d32p
3967         log 32p_2
3968         rm -f $DIR/$tfile
3969         log 32p_3
3970         touch $DIR/$tfile
3971         log 32p_4
3972         test_mkdir -p $DIR/d32p/tmp
3973         log 32p_5
3974         TMP_DIR=$DIR/d32p/tmp
3975         log 32p_6
3976         ln -s $DIR/$tfile $TMP_DIR/symlink12
3977         log 32p_7
3978         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3979         log 32p_8
3980         cat $DIR/d32p/tmp/symlink12 ||
3981                 error "Can't open $DIR/d32p/tmp/symlink12"
3982         log 32p_9
3983         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3984         log 32p_10
3985 }
3986 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3987
3988 test_32q() {
3989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3990
3991         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3992         trap cleanup_test32_mount EXIT
3993         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3994         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3995         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3996                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3997         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3998         cleanup_test32_mount
3999 }
4000 run_test 32q "stat follows mountpoints in Lustre (should return error)"
4001
4002 test_32r() {
4003         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4004
4005         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
4006         trap cleanup_test32_mount EXIT
4007         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4008         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
4009         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4010                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4011         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
4012         cleanup_test32_mount
4013 }
4014 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
4015
4016 test_33aa() {
4017         rm -f $DIR/$tfile
4018         touch $DIR/$tfile
4019         chmod 444 $DIR/$tfile
4020         chown $RUNAS_ID $DIR/$tfile
4021         log 33_1
4022         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4023         log 33_2
4024 }
4025 run_test 33aa "write file with mode 444 (should return error)"
4026
4027 test_33a() {
4028         rm -fr $DIR/$tdir
4029         test_mkdir $DIR/$tdir
4030         chown $RUNAS_ID $DIR/$tdir
4031         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
4032                 error "$RUNAS create $tdir/$tfile failed"
4033         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
4034                 error "open RDWR" || true
4035 }
4036 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
4037
4038 test_33b() {
4039         rm -fr $DIR/$tdir
4040         test_mkdir $DIR/$tdir
4041         chown $RUNAS_ID $DIR/$tdir
4042         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
4043 }
4044 run_test 33b "test open file with malformed flags (No panic)"
4045
4046 test_33c() {
4047         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4048         remote_ost_nodsh && skip "remote OST with nodsh"
4049
4050         local ostnum
4051         local ostname
4052         local write_bytes
4053         local all_zeros
4054
4055         all_zeros=true
4056         test_mkdir $DIR/$tdir
4057         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
4058
4059         sync
4060         for ostnum in $(seq $OSTCOUNT); do
4061                 # test-framework's OST numbering is one-based, while Lustre's
4062                 # is zero-based
4063                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4064                 # check if at least some write_bytes stats are counted
4065                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4066                               obdfilter.$ostname.stats |
4067                               awk '/^write_bytes/ {print $7}' )
4068                 echo "baseline_write_bytes@ost$ostnum/$ostname=$write_bytes"
4069                 if (( ${write_bytes:-0} > 0 )); then
4070                         all_zeros=false
4071                         break
4072                 fi
4073         done
4074
4075         $all_zeros || return 0
4076
4077         # Write four bytes
4078         echo foo > $DIR/$tdir/bar
4079         # Really write them
4080         sync
4081
4082         # Total up write_bytes after writing.  We'd better find non-zeros.
4083         for ostnum in $(seq $OSTCOUNT); do
4084                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4085                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4086                               obdfilter/$ostname/stats |
4087                               awk '/^write_bytes/ {print $7}' )
4088                 echo "write_bytes@ost$ostnum/$ostname=$write_bytes"
4089                 if (( ${write_bytes:-0} > 0 )); then
4090                         all_zeros=false
4091                         break
4092                 fi
4093         done
4094
4095         if $all_zeros; then
4096                 for ostnum in $(seq $OSTCOUNT); do
4097                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4098                         echo "Check write_bytes is in obdfilter.*.stats:"
4099                         do_facet ost$ostnum lctl get_param -n \
4100                                 obdfilter.$ostname.stats
4101                 done
4102                 error "OST not keeping write_bytes stats (b=22312)"
4103         fi
4104 }
4105 run_test 33c "test write_bytes stats"
4106
4107 test_33d() {
4108         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4109         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4110
4111         local MDTIDX=1
4112         local remote_dir=$DIR/$tdir/remote_dir
4113
4114         test_mkdir $DIR/$tdir
4115         $LFS mkdir -i $MDTIDX $remote_dir ||
4116                 error "create remote directory failed"
4117
4118         touch $remote_dir/$tfile
4119         chmod 444 $remote_dir/$tfile
4120         chown $RUNAS_ID $remote_dir/$tfile
4121
4122         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4123
4124         chown $RUNAS_ID $remote_dir
4125         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
4126                                         error "create" || true
4127         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
4128                                     error "open RDWR" || true
4129         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
4130 }
4131 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
4132
4133 test_33e() {
4134         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4135
4136         mkdir $DIR/$tdir
4137
4138         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4139         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4140         mkdir $DIR/$tdir/local_dir
4141
4142         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4143         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4144         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4145
4146         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4147                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
4148
4149         rmdir $DIR/$tdir/* || error "rmdir failed"
4150
4151         umask 777
4152         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4153         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4154         mkdir $DIR/$tdir/local_dir
4155
4156         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4157         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4158         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4159
4160         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4161                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
4162
4163         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
4164
4165         umask 000
4166         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4167         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4168         mkdir $DIR/$tdir/local_dir
4169
4170         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4171         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4172         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4173
4174         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4175                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
4176 }
4177 run_test 33e "mkdir and striped directory should have same mode"
4178
4179 cleanup_33f() {
4180         trap 0
4181         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
4182 }
4183
4184 test_33f() {
4185         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4186         remote_mds_nodsh && skip "remote MDS with nodsh"
4187
4188         mkdir $DIR/$tdir
4189         chmod go+rwx $DIR/$tdir
4190         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
4191         trap cleanup_33f EXIT
4192
4193         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
4194                 error "cannot create striped directory"
4195
4196         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
4197                 error "cannot create files in striped directory"
4198
4199         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
4200                 error "cannot remove files in striped directory"
4201
4202         $RUNAS rmdir $DIR/$tdir/striped_dir ||
4203                 error "cannot remove striped directory"
4204
4205         cleanup_33f
4206 }
4207 run_test 33f "nonroot user can create, access, and remove a striped directory"
4208
4209 test_33g() {
4210         mkdir -p $DIR/$tdir/dir2
4211
4212         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
4213         echo $err
4214         [[ $err =~ "exists" ]] || error "Not exists error"
4215 }
4216 run_test 33g "nonroot user create already existing root created file"
4217
4218 test_33h() {
4219         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4220         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
4221                 skip "Need MDS version at least 2.13.50"
4222
4223         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
4224                 error "mkdir $tdir failed"
4225         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
4226
4227         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
4228         local index2
4229
4230         for fname in $DIR/$tdir/$tfile.bak \
4231                      $DIR/$tdir/$tfile.SAV \
4232                      $DIR/$tdir/$tfile.orig \
4233                      $DIR/$tdir/$tfile~; do
4234                 touch $fname  || error "touch $fname failed"
4235                 index2=$($LFS getstripe -m $fname)
4236                 [ $index -eq $index2 ] ||
4237                         error "$fname MDT index mismatch $index != $index2"
4238         done
4239
4240         local failed=0
4241         for i in {1..250}; do
4242                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
4243                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
4244                         touch $fname  || error "touch $fname failed"
4245                         index2=$($LFS getstripe -m $fname)
4246                         if [[ $index != $index2 ]]; then
4247                                 failed=$((failed + 1))
4248                                 echo "$fname MDT index mismatch $index != $index2"
4249                         fi
4250                 done
4251         done
4252         echo "$failed MDT index mismatches"
4253         (( failed < 20 )) || error "MDT index mismatch $failed times"
4254
4255 }
4256 run_test 33h "temp file is located on the same MDT as target"
4257
4258 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
4259 test_34a() {
4260         rm -f $DIR/f34
4261         $MCREATE $DIR/f34 || error "mcreate failed"
4262         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4263                 error "getstripe failed"
4264         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
4265         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4266                 error "getstripe failed"
4267         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4268                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4269 }
4270 run_test 34a "truncate file that has not been opened ==========="
4271
4272 test_34b() {
4273         [ ! -f $DIR/f34 ] && test_34a
4274         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4275                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4276         $OPENFILE -f O_RDONLY $DIR/f34
4277         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4278                 error "getstripe failed"
4279         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4280                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4281 }
4282 run_test 34b "O_RDONLY opening file doesn't create objects ====="
4283
4284 test_34c() {
4285         [ ! -f $DIR/f34 ] && test_34a
4286         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4287                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4288         $OPENFILE -f O_RDWR $DIR/f34
4289         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
4290                 error "$LFS getstripe failed"
4291         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4292                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4293 }
4294 run_test 34c "O_RDWR opening file-with-size works =============="
4295
4296 test_34d() {
4297         [ ! -f $DIR/f34 ] && test_34a
4298         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
4299                 error "dd failed"
4300         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4301                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4302         rm $DIR/f34
4303 }
4304 run_test 34d "write to sparse file ============================="
4305
4306 test_34e() {
4307         rm -f $DIR/f34e
4308         $MCREATE $DIR/f34e || error "mcreate failed"
4309         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
4310         $CHECKSTAT -s 1000 $DIR/f34e ||
4311                 error "Size of $DIR/f34e not equal to 1000 bytes"
4312         $OPENFILE -f O_RDWR $DIR/f34e
4313         $CHECKSTAT -s 1000 $DIR/f34e ||
4314                 error "Size of $DIR/f34e not equal to 1000 bytes"
4315 }
4316 run_test 34e "create objects, some with size and some without =="
4317
4318 test_34f() { # bug 6242, 6243
4319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4320
4321         SIZE34F=48000
4322         rm -f $DIR/f34f
4323         $MCREATE $DIR/f34f || error "mcreate failed"
4324         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
4325         dd if=$DIR/f34f of=$TMP/f34f
4326         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
4327         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
4328         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
4329         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
4330         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
4331 }
4332 run_test 34f "read from a file with no objects until EOF ======="
4333
4334 test_34g() {
4335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4336
4337         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
4338                 error "dd failed"
4339         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
4340         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4341                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
4342         cancel_lru_locks osc
4343         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4344                 error "wrong size after lock cancel"
4345
4346         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
4347         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4348                 error "expanding truncate failed"
4349         cancel_lru_locks osc
4350         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4351                 error "wrong expanded size after lock cancel"
4352 }
4353 run_test 34g "truncate long file ==============================="
4354
4355 test_34h() {
4356         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4357
4358         local gid=10
4359         local sz=1000
4360
4361         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
4362         sync # Flush the cache so that multiop below does not block on cache
4363              # flush when getting the group lock
4364         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
4365         MULTIPID=$!
4366
4367         # Since just timed wait is not good enough, let's do a sync write
4368         # that way we are sure enough time for a roundtrip + processing
4369         # passed + 2 seconds of extra margin.
4370         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
4371         rm $DIR/${tfile}-1
4372         sleep 2
4373
4374         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
4375                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
4376                 kill -9 $MULTIPID
4377         fi
4378         wait $MULTIPID
4379         local nsz=`stat -c %s $DIR/$tfile`
4380         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
4381 }
4382 run_test 34h "ftruncate file under grouplock should not block"
4383
4384 test_35a() {
4385         cp /bin/sh $DIR/f35a
4386         chmod 444 $DIR/f35a
4387         chown $RUNAS_ID $DIR/f35a
4388         $RUNAS $DIR/f35a && error || true
4389         rm $DIR/f35a
4390 }
4391 run_test 35a "exec file with mode 444 (should return and not leak)"
4392
4393 test_36a() {
4394         rm -f $DIR/f36
4395         utime $DIR/f36 || error "utime failed for MDS"
4396 }
4397 run_test 36a "MDS utime check (mknod, utime)"
4398
4399 test_36b() {
4400         echo "" > $DIR/f36
4401         utime $DIR/f36 || error "utime failed for OST"
4402 }
4403 run_test 36b "OST utime check (open, utime)"
4404
4405 test_36c() {
4406         rm -f $DIR/d36/f36
4407         test_mkdir $DIR/d36
4408         chown $RUNAS_ID $DIR/d36
4409         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
4410 }
4411 run_test 36c "non-root MDS utime check (mknod, utime)"
4412
4413 test_36d() {
4414         [ ! -d $DIR/d36 ] && test_36c
4415         echo "" > $DIR/d36/f36
4416         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
4417 }
4418 run_test 36d "non-root OST utime check (open, utime)"
4419
4420 test_36e() {
4421         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
4422
4423         test_mkdir $DIR/$tdir
4424         touch $DIR/$tdir/$tfile
4425         $RUNAS utime $DIR/$tdir/$tfile &&
4426                 error "utime worked, expected failure" || true
4427 }
4428 run_test 36e "utime on non-owned file (should return error)"
4429
4430 subr_36fh() {
4431         local fl="$1"
4432         local LANG_SAVE=$LANG
4433         local LC_LANG_SAVE=$LC_LANG
4434         export LANG=C LC_LANG=C # for date language
4435
4436         DATESTR="Dec 20  2000"
4437         test_mkdir $DIR/$tdir
4438         lctl set_param fail_loc=$fl
4439         date; date +%s
4440         cp /etc/hosts $DIR/$tdir/$tfile
4441         sync & # write RPC generated with "current" inode timestamp, but delayed
4442         sleep 1
4443         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4444         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4445         cancel_lru_locks $OSC
4446         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4447         date; date +%s
4448         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4449                 echo "BEFORE: $LS_BEFORE" && \
4450                 echo "AFTER : $LS_AFTER" && \
4451                 echo "WANT  : $DATESTR" && \
4452                 error "$DIR/$tdir/$tfile timestamps changed" || true
4453
4454         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4455 }
4456
4457 test_36f() {
4458         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4459
4460         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4461         subr_36fh "0x80000214"
4462 }
4463 run_test 36f "utime on file racing with OST BRW write =========="
4464
4465 test_36g() {
4466         remote_ost_nodsh && skip "remote OST with nodsh"
4467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4468         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4469                 skip "Need MDS version at least 2.12.51"
4470
4471         local fmd_max_age
4472         local fmd
4473         local facet="ost1"
4474         local tgt="obdfilter"
4475
4476         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4477
4478         test_mkdir $DIR/$tdir
4479         fmd_max_age=$(do_facet $facet \
4480                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4481                 head -n 1")
4482
4483         echo "FMD max age: ${fmd_max_age}s"
4484         touch $DIR/$tdir/$tfile
4485         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4486                 gawk '{cnt=cnt+$1}  END{print cnt}')
4487         echo "FMD before: $fmd"
4488         [[ $fmd == 0 ]] &&
4489                 error "FMD wasn't create by touch"
4490         sleep $((fmd_max_age + 12))
4491         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4492                 gawk '{cnt=cnt+$1}  END{print cnt}')
4493         echo "FMD after: $fmd"
4494         [[ $fmd == 0 ]] ||
4495                 error "FMD wasn't expired by ping"
4496 }
4497 run_test 36g "FMD cache expiry ====================="
4498
4499 test_36h() {
4500         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4501
4502         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4503         subr_36fh "0x80000227"
4504 }
4505 run_test 36h "utime on file racing with OST BRW write =========="
4506
4507 test_36i() {
4508         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4509
4510         test_mkdir $DIR/$tdir
4511         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4512
4513         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4514         local new_mtime=$((mtime + 200))
4515
4516         #change Modify time of striped dir
4517         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4518                         error "change mtime failed"
4519
4520         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4521
4522         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4523 }
4524 run_test 36i "change mtime on striped directory"
4525
4526 # test_37 - duplicate with tests 32q 32r
4527
4528 test_38() {
4529         local file=$DIR/$tfile
4530         touch $file
4531         openfile -f O_DIRECTORY $file
4532         local RC=$?
4533         local ENOTDIR=20
4534         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4535         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4536 }
4537 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4538
4539 test_39a() { # was test_39
4540         touch $DIR/$tfile
4541         touch $DIR/${tfile}2
4542 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4543 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4544 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4545         sleep 2
4546         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4547         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4548                 echo "mtime"
4549                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4550                 echo "atime"
4551                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4552                 echo "ctime"
4553                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4554                 error "O_TRUNC didn't change timestamps"
4555         fi
4556 }
4557 run_test 39a "mtime changed on create"
4558
4559 test_39b() {
4560         test_mkdir -c1 $DIR/$tdir
4561         cp -p /etc/passwd $DIR/$tdir/fopen
4562         cp -p /etc/passwd $DIR/$tdir/flink
4563         cp -p /etc/passwd $DIR/$tdir/funlink
4564         cp -p /etc/passwd $DIR/$tdir/frename
4565         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4566
4567         sleep 1
4568         echo "aaaaaa" >> $DIR/$tdir/fopen
4569         echo "aaaaaa" >> $DIR/$tdir/flink
4570         echo "aaaaaa" >> $DIR/$tdir/funlink
4571         echo "aaaaaa" >> $DIR/$tdir/frename
4572
4573         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4574         local link_new=`stat -c %Y $DIR/$tdir/flink`
4575         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4576         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4577
4578         cat $DIR/$tdir/fopen > /dev/null
4579         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4580         rm -f $DIR/$tdir/funlink2
4581         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4582
4583         for (( i=0; i < 2; i++ )) ; do
4584                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4585                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4586                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4587                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4588
4589                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4590                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4591                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4592                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4593
4594                 cancel_lru_locks $OSC
4595                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4596         done
4597 }
4598 run_test 39b "mtime change on open, link, unlink, rename  ======"
4599
4600 # this should be set to past
4601 TEST_39_MTIME=`date -d "1 year ago" +%s`
4602
4603 # bug 11063
4604 test_39c() {
4605         touch $DIR1/$tfile
4606         sleep 2
4607         local mtime0=`stat -c %Y $DIR1/$tfile`
4608
4609         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4610         local mtime1=`stat -c %Y $DIR1/$tfile`
4611         [ "$mtime1" = $TEST_39_MTIME ] || \
4612                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4613
4614         local d1=`date +%s`
4615         echo hello >> $DIR1/$tfile
4616         local d2=`date +%s`
4617         local mtime2=`stat -c %Y $DIR1/$tfile`
4618         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4619                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4620
4621         mv $DIR1/$tfile $DIR1/$tfile-1
4622
4623         for (( i=0; i < 2; i++ )) ; do
4624                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4625                 [ "$mtime2" = "$mtime3" ] || \
4626                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4627
4628                 cancel_lru_locks $OSC
4629                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4630         done
4631 }
4632 run_test 39c "mtime change on rename ==========================="
4633
4634 # bug 21114
4635 test_39d() {
4636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4637
4638         touch $DIR1/$tfile
4639         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4640
4641         for (( i=0; i < 2; i++ )) ; do
4642                 local mtime=`stat -c %Y $DIR1/$tfile`
4643                 [ $mtime = $TEST_39_MTIME ] || \
4644                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4645
4646                 cancel_lru_locks $OSC
4647                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4648         done
4649 }
4650 run_test 39d "create, utime, stat =============================="
4651
4652 # bug 21114
4653 test_39e() {
4654         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4655
4656         touch $DIR1/$tfile
4657         local mtime1=`stat -c %Y $DIR1/$tfile`
4658
4659         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4660
4661         for (( i=0; i < 2; i++ )) ; do
4662                 local mtime2=`stat -c %Y $DIR1/$tfile`
4663                 [ $mtime2 = $TEST_39_MTIME ] || \
4664                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4665
4666                 cancel_lru_locks $OSC
4667                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4668         done
4669 }
4670 run_test 39e "create, stat, utime, stat ========================"
4671
4672 # bug 21114
4673 test_39f() {
4674         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4675
4676         touch $DIR1/$tfile
4677         mtime1=`stat -c %Y $DIR1/$tfile`
4678
4679         sleep 2
4680         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4681
4682         for (( i=0; i < 2; i++ )) ; do
4683                 local mtime2=`stat -c %Y $DIR1/$tfile`
4684                 [ $mtime2 = $TEST_39_MTIME ] || \
4685                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4686
4687                 cancel_lru_locks $OSC
4688                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4689         done
4690 }
4691 run_test 39f "create, stat, sleep, utime, stat ================="
4692
4693 # bug 11063
4694 test_39g() {
4695         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4696
4697         echo hello >> $DIR1/$tfile
4698         local mtime1=`stat -c %Y $DIR1/$tfile`
4699
4700         sleep 2
4701         chmod o+r $DIR1/$tfile
4702
4703         for (( i=0; i < 2; i++ )) ; do
4704                 local mtime2=`stat -c %Y $DIR1/$tfile`
4705                 [ "$mtime1" = "$mtime2" ] || \
4706                         error "lost mtime: $mtime2, should be $mtime1"
4707
4708                 cancel_lru_locks $OSC
4709                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4710         done
4711 }
4712 run_test 39g "write, chmod, stat ==============================="
4713
4714 # bug 11063
4715 test_39h() {
4716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4717
4718         touch $DIR1/$tfile
4719         sleep 1
4720
4721         local d1=`date`
4722         echo hello >> $DIR1/$tfile
4723         local mtime1=`stat -c %Y $DIR1/$tfile`
4724
4725         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4726         local d2=`date`
4727         if [ "$d1" != "$d2" ]; then
4728                 echo "write and touch not within one second"
4729         else
4730                 for (( i=0; i < 2; i++ )) ; do
4731                         local mtime2=`stat -c %Y $DIR1/$tfile`
4732                         [ "$mtime2" = $TEST_39_MTIME ] || \
4733                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4734
4735                         cancel_lru_locks $OSC
4736                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4737                 done
4738         fi
4739 }
4740 run_test 39h "write, utime within one second, stat ============="
4741
4742 test_39i() {
4743         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4744
4745         touch $DIR1/$tfile
4746         sleep 1
4747
4748         echo hello >> $DIR1/$tfile
4749         local mtime1=`stat -c %Y $DIR1/$tfile`
4750
4751         mv $DIR1/$tfile $DIR1/$tfile-1
4752
4753         for (( i=0; i < 2; i++ )) ; do
4754                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4755
4756                 [ "$mtime1" = "$mtime2" ] || \
4757                         error "lost mtime: $mtime2, should be $mtime1"
4758
4759                 cancel_lru_locks $OSC
4760                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4761         done
4762 }
4763 run_test 39i "write, rename, stat =============================="
4764
4765 test_39j() {
4766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4767
4768         start_full_debug_logging
4769         touch $DIR1/$tfile
4770         sleep 1
4771
4772         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4773         lctl set_param fail_loc=0x80000412
4774         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4775                 error "multiop failed"
4776         local multipid=$!
4777         local mtime1=`stat -c %Y $DIR1/$tfile`
4778
4779         mv $DIR1/$tfile $DIR1/$tfile-1
4780
4781         kill -USR1 $multipid
4782         wait $multipid || error "multiop close failed"
4783
4784         for (( i=0; i < 2; i++ )) ; do
4785                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4786                 [ "$mtime1" = "$mtime2" ] ||
4787                         error "mtime is lost on close: $mtime2, " \
4788                               "should be $mtime1"
4789
4790                 cancel_lru_locks
4791                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4792         done
4793         lctl set_param fail_loc=0
4794         stop_full_debug_logging
4795 }
4796 run_test 39j "write, rename, close, stat ======================="
4797
4798 test_39k() {
4799         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4800
4801         touch $DIR1/$tfile
4802         sleep 1
4803
4804         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4805         local multipid=$!
4806         local mtime1=`stat -c %Y $DIR1/$tfile`
4807
4808         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4809
4810         kill -USR1 $multipid
4811         wait $multipid || error "multiop close failed"
4812
4813         for (( i=0; i < 2; i++ )) ; do
4814                 local mtime2=`stat -c %Y $DIR1/$tfile`
4815
4816                 [ "$mtime2" = $TEST_39_MTIME ] || \
4817                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4818
4819                 cancel_lru_locks
4820                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4821         done
4822 }
4823 run_test 39k "write, utime, close, stat ========================"
4824
4825 # this should be set to future
4826 TEST_39_ATIME=`date -d "1 year" +%s`
4827
4828 test_39l() {
4829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4830         remote_mds_nodsh && skip "remote MDS with nodsh"
4831
4832         local atime_diff=$(do_facet $SINGLEMDS \
4833                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4834         rm -rf $DIR/$tdir
4835         mkdir_on_mdt0 $DIR/$tdir
4836
4837         # test setting directory atime to future
4838         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4839         local atime=$(stat -c %X $DIR/$tdir)
4840         [ "$atime" = $TEST_39_ATIME ] ||
4841                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4842
4843         # test setting directory atime from future to now
4844         local now=$(date +%s)
4845         touch -a -d @$now $DIR/$tdir
4846
4847         atime=$(stat -c %X $DIR/$tdir)
4848         [ "$atime" -eq "$now"  ] ||
4849                 error "atime is not updated from future: $atime, $now"
4850
4851         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4852         sleep 3
4853
4854         # test setting directory atime when now > dir atime + atime_diff
4855         local d1=$(date +%s)
4856         ls $DIR/$tdir
4857         local d2=$(date +%s)
4858         cancel_lru_locks mdc
4859         atime=$(stat -c %X $DIR/$tdir)
4860         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4861                 error "atime is not updated  : $atime, should be $d2"
4862
4863         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4864         sleep 3
4865
4866         # test not setting directory atime when now < dir atime + atime_diff
4867         ls $DIR/$tdir
4868         cancel_lru_locks mdc
4869         atime=$(stat -c %X $DIR/$tdir)
4870         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4871                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4872
4873         do_facet $SINGLEMDS \
4874                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4875 }
4876 run_test 39l "directory atime update ==========================="
4877
4878 test_39m() {
4879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4880
4881         touch $DIR1/$tfile
4882         sleep 2
4883         local far_past_mtime=$(date -d "May 29 1953" +%s)
4884         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4885
4886         touch -m -d @$far_past_mtime $DIR1/$tfile
4887         touch -a -d @$far_past_atime $DIR1/$tfile
4888
4889         for (( i=0; i < 2; i++ )) ; do
4890                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4891                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4892                         error "atime or mtime set incorrectly"
4893
4894                 cancel_lru_locks $OSC
4895                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4896         done
4897 }
4898 run_test 39m "test atime and mtime before 1970"
4899
4900 test_39n() { # LU-3832
4901         remote_mds_nodsh && skip "remote MDS with nodsh"
4902
4903         local atime_diff=$(do_facet $SINGLEMDS \
4904                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4905         local atime0
4906         local atime1
4907         local atime2
4908
4909         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4910
4911         rm -rf $DIR/$tfile
4912         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4913         atime0=$(stat -c %X $DIR/$tfile)
4914
4915         sleep 5
4916         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4917         atime1=$(stat -c %X $DIR/$tfile)
4918
4919         sleep 5
4920         cancel_lru_locks mdc
4921         cancel_lru_locks osc
4922         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4923         atime2=$(stat -c %X $DIR/$tfile)
4924
4925         do_facet $SINGLEMDS \
4926                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4927
4928         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4929         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4930 }
4931 run_test 39n "check that O_NOATIME is honored"
4932
4933 test_39o() {
4934         TESTDIR=$DIR/$tdir/$tfile
4935         [ -e $TESTDIR ] && rm -rf $TESTDIR
4936         mkdir -p $TESTDIR
4937         cd $TESTDIR
4938         links1=2
4939         ls
4940         mkdir a b
4941         ls
4942         links2=$(stat -c %h .)
4943         [ $(($links1 + 2)) != $links2 ] &&
4944                 error "wrong links count $(($links1 + 2)) != $links2"
4945         rmdir b
4946         links3=$(stat -c %h .)
4947         [ $(($links1 + 1)) != $links3 ] &&
4948                 error "wrong links count $links1 != $links3"
4949         return 0
4950 }
4951 run_test 39o "directory cached attributes updated after create"
4952
4953 test_39p() {
4954         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4955
4956         local MDTIDX=1
4957         TESTDIR=$DIR/$tdir/$tdir
4958         [ -e $TESTDIR ] && rm -rf $TESTDIR
4959         test_mkdir -p $TESTDIR
4960         cd $TESTDIR
4961         links1=2
4962         ls
4963         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4964         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4965         ls
4966         links2=$(stat -c %h .)
4967         [ $(($links1 + 2)) != $links2 ] &&
4968                 error "wrong links count $(($links1 + 2)) != $links2"
4969         rmdir remote_dir2
4970         links3=$(stat -c %h .)
4971         [ $(($links1 + 1)) != $links3 ] &&
4972                 error "wrong links count $links1 != $links3"
4973         return 0
4974 }
4975 run_test 39p "remote directory cached attributes updated after create ========"
4976
4977 test_39r() {
4978         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
4979                 skip "no atime update on old OST"
4980         if [ "$ost1_FSTYPE" != ldiskfs ]; then
4981                 skip_env "ldiskfs only test"
4982         fi
4983
4984         local saved_adiff
4985         saved_adiff=$(do_facet ost1 \
4986                 lctl get_param -n obdfilter.*OST0000.atime_diff)
4987         stack_trap "do_facet ost1 \
4988                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
4989
4990         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
4991
4992         $LFS setstripe -i 0 $DIR/$tfile
4993         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
4994                 error "can't write initial file"
4995         cancel_lru_locks osc
4996
4997         # exceed atime_diff and access file
4998         sleep 6
4999         dd if=$DIR/$tfile of=/dev/null bs=4k count=1 ||
5000                 error "can't udpate atime"
5001
5002         local atime_cli=$(stat -c %X $DIR/$tfile)
5003         echo "client atime: $atime_cli"
5004         # allow atime update to be written to device
5005         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
5006         sleep 5
5007
5008         local ostdev=$(ostdevname 1)
5009         local fid=($(lfs getstripe -y $DIR/$tfile |
5010                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
5011         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
5012         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
5013
5014         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
5015         local atime_ost=$(do_facet ost1 "$cmd" |&
5016                           awk -F'[: ]' '/atime:/ { print $4 }')
5017         (( atime_cli == atime_ost )) ||
5018                 error "atime on client $atime_cli != ost $atime_ost"
5019 }
5020 run_test 39r "lazy atime update on OST"
5021
5022 test_39q() { # LU-8041
5023         local testdir=$DIR/$tdir
5024         mkdir -p $testdir
5025         multiop_bg_pause $testdir D_c || error "multiop failed"
5026         local multipid=$!
5027         cancel_lru_locks mdc
5028         kill -USR1 $multipid
5029         local atime=$(stat -c %X $testdir)
5030         [ "$atime" -ne 0 ] || error "atime is zero"
5031 }
5032 run_test 39q "close won't zero out atime"
5033
5034 test_40() {
5035         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
5036         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
5037                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
5038         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
5039                 error "$tfile is not 4096 bytes in size"
5040 }
5041 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
5042
5043 test_41() {
5044         # bug 1553
5045         small_write $DIR/f41 18
5046 }
5047 run_test 41 "test small file write + fstat ====================="
5048
5049 count_ost_writes() {
5050         lctl get_param -n ${OSC}.*.stats |
5051                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
5052                         END { printf("%0.0f", writes) }'
5053 }
5054
5055 # decent default
5056 WRITEBACK_SAVE=500
5057 DIRTY_RATIO_SAVE=40
5058 MAX_DIRTY_RATIO=50
5059 BG_DIRTY_RATIO_SAVE=10
5060 MAX_BG_DIRTY_RATIO=25
5061
5062 start_writeback() {
5063         trap 0
5064         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
5065         # dirty_ratio, dirty_background_ratio
5066         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5067                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
5068                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
5069                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
5070         else
5071                 # if file not here, we are a 2.4 kernel
5072                 kill -CONT `pidof kupdated`
5073         fi
5074 }
5075
5076 stop_writeback() {
5077         # setup the trap first, so someone cannot exit the test at the
5078         # exact wrong time and mess up a machine
5079         trap start_writeback EXIT
5080         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
5081         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5082                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
5083                 sysctl -w vm.dirty_writeback_centisecs=0
5084                 sysctl -w vm.dirty_writeback_centisecs=0
5085                 # save and increase /proc/sys/vm/dirty_ratio
5086                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
5087                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
5088                 # save and increase /proc/sys/vm/dirty_background_ratio
5089                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
5090                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
5091         else
5092                 # if file not here, we are a 2.4 kernel
5093                 kill -STOP `pidof kupdated`
5094         fi
5095 }
5096
5097 # ensure that all stripes have some grant before we test client-side cache
5098 setup_test42() {
5099         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
5100                 dd if=/dev/zero of=$i bs=4k count=1
5101                 rm $i
5102         done
5103 }
5104
5105 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
5106 # file truncation, and file removal.
5107 test_42a() {
5108         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5109
5110         setup_test42
5111         cancel_lru_locks $OSC
5112         stop_writeback
5113         sync; sleep 1; sync # just to be safe
5114         BEFOREWRITES=`count_ost_writes`
5115         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
5116         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
5117         AFTERWRITES=`count_ost_writes`
5118         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
5119                 error "$BEFOREWRITES < $AFTERWRITES"
5120         start_writeback
5121 }
5122 run_test 42a "ensure that we don't flush on close"
5123
5124 test_42b() {
5125         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5126
5127         setup_test42
5128         cancel_lru_locks $OSC
5129         stop_writeback
5130         sync
5131         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
5132         BEFOREWRITES=$(count_ost_writes)
5133         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
5134         AFTERWRITES=$(count_ost_writes)
5135         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5136                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
5137         fi
5138         BEFOREWRITES=$(count_ost_writes)
5139         sync || error "sync: $?"
5140         AFTERWRITES=$(count_ost_writes)
5141         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5142                 error "$BEFOREWRITES < $AFTERWRITES on sync"
5143         fi
5144         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
5145         start_writeback
5146         return 0
5147 }
5148 run_test 42b "test destroy of file with cached dirty data ======"
5149
5150 # if these tests just want to test the effect of truncation,
5151 # they have to be very careful.  consider:
5152 # - the first open gets a {0,EOF}PR lock
5153 # - the first write conflicts and gets a {0, count-1}PW
5154 # - the rest of the writes are under {count,EOF}PW
5155 # - the open for truncate tries to match a {0,EOF}PR
5156 #   for the filesize and cancels the PWs.
5157 # any number of fixes (don't get {0,EOF} on open, match
5158 # composite locks, do smarter file size management) fix
5159 # this, but for now we want these tests to verify that
5160 # the cancellation with truncate intent works, so we
5161 # start the file with a full-file pw lock to match against
5162 # until the truncate.
5163 trunc_test() {
5164         test=$1
5165         file=$DIR/$test
5166         offset=$2
5167         cancel_lru_locks $OSC
5168         stop_writeback
5169         # prime the file with 0,EOF PW to match
5170         touch $file
5171         $TRUNCATE $file 0
5172         sync; sync
5173         # now the real test..
5174         dd if=/dev/zero of=$file bs=1024 count=100
5175         BEFOREWRITES=`count_ost_writes`
5176         $TRUNCATE $file $offset
5177         cancel_lru_locks $OSC
5178         AFTERWRITES=`count_ost_writes`
5179         start_writeback
5180 }
5181
5182 test_42c() {
5183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5184
5185         trunc_test 42c 1024
5186         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
5187                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
5188         rm $file
5189 }
5190 run_test 42c "test partial truncate of file with cached dirty data"
5191
5192 test_42d() {
5193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5194
5195         trunc_test 42d 0
5196         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
5197                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
5198         rm $file
5199 }
5200 run_test 42d "test complete truncate of file with cached dirty data"
5201
5202 test_42e() { # bug22074
5203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5204
5205         local TDIR=$DIR/${tdir}e
5206         local pages=16 # hardcoded 16 pages, don't change it.
5207         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
5208         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
5209         local max_dirty_mb
5210         local warmup_files
5211
5212         test_mkdir $DIR/${tdir}e
5213         $LFS setstripe -c 1 $TDIR
5214         createmany -o $TDIR/f $files
5215
5216         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
5217
5218         # we assume that with $OSTCOUNT files, at least one of them will
5219         # be allocated on OST0.
5220         warmup_files=$((OSTCOUNT * max_dirty_mb))
5221         createmany -o $TDIR/w $warmup_files
5222
5223         # write a large amount of data into one file and sync, to get good
5224         # avail_grant number from OST.
5225         for ((i=0; i<$warmup_files; i++)); do
5226                 idx=$($LFS getstripe -i $TDIR/w$i)
5227                 [ $idx -ne 0 ] && continue
5228                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
5229                 break
5230         done
5231         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
5232         sync
5233         $LCTL get_param $proc_osc0/cur_dirty_bytes
5234         $LCTL get_param $proc_osc0/cur_grant_bytes
5235
5236         # create as much dirty pages as we can while not to trigger the actual
5237         # RPCs directly. but depends on the env, VFS may trigger flush during this
5238         # period, hopefully we are good.
5239         for ((i=0; i<$warmup_files; i++)); do
5240                 idx=$($LFS getstripe -i $TDIR/w$i)
5241                 [ $idx -ne 0 ] && continue
5242                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
5243         done
5244         $LCTL get_param $proc_osc0/cur_dirty_bytes
5245         $LCTL get_param $proc_osc0/cur_grant_bytes
5246
5247         # perform the real test
5248         $LCTL set_param $proc_osc0/rpc_stats 0
5249         for ((;i<$files; i++)); do
5250                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
5251                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
5252         done
5253         sync
5254         $LCTL get_param $proc_osc0/rpc_stats
5255
5256         local percent=0
5257         local have_ppr=false
5258         $LCTL get_param $proc_osc0/rpc_stats |
5259                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
5260                         # skip lines until we are at the RPC histogram data
5261                         [ "$PPR" == "pages" ] && have_ppr=true && continue
5262                         $have_ppr || continue
5263
5264                         # we only want the percent stat for < 16 pages
5265                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
5266
5267                         percent=$((percent + WPCT))
5268                         if [[ $percent -gt 15 ]]; then
5269                                 error "less than 16-pages write RPCs" \
5270                                       "$percent% > 15%"
5271                                 break
5272                         fi
5273                 done
5274         rm -rf $TDIR
5275 }
5276 run_test 42e "verify sub-RPC writes are not done synchronously"
5277
5278 test_43A() { # was test_43
5279         test_mkdir $DIR/$tdir
5280         cp -p /bin/ls $DIR/$tdir/$tfile
5281         $MULTIOP $DIR/$tdir/$tfile Ow_c &
5282         pid=$!
5283         # give multiop a chance to open
5284         sleep 1
5285
5286         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
5287         kill -USR1 $pid
5288         # Wait for multiop to exit
5289         wait $pid
5290 }
5291 run_test 43A "execution of file opened for write should return -ETXTBSY"
5292
5293 test_43a() {
5294         test_mkdir $DIR/$tdir
5295         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5296         $DIR/$tdir/sleep 60 &
5297         SLEEP_PID=$!
5298         # Make sure exec of $tdir/sleep wins race with truncate
5299         sleep 1
5300         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
5301         kill $SLEEP_PID
5302 }
5303 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
5304
5305 test_43b() {
5306         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5307
5308         test_mkdir $DIR/$tdir
5309         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5310         $DIR/$tdir/sleep 60 &
5311         SLEEP_PID=$!
5312         # Make sure exec of $tdir/sleep wins race with truncate
5313         sleep 1
5314         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
5315         kill $SLEEP_PID
5316 }
5317 run_test 43b "truncate of file being executed should return -ETXTBSY"
5318
5319 test_43c() {
5320         local testdir="$DIR/$tdir"
5321         test_mkdir $testdir
5322         cp $SHELL $testdir/
5323         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
5324                 ( cd $testdir && md5sum -c )
5325 }
5326 run_test 43c "md5sum of copy into lustre"
5327
5328 test_44A() { # was test_44
5329         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5330
5331         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
5332         dd if=$DIR/f1 bs=4k count=1 > /dev/null
5333 }
5334 run_test 44A "zero length read from a sparse stripe"
5335
5336 test_44a() {
5337         local nstripe=$($LFS getstripe -c -d $DIR)
5338         [ -z "$nstripe" ] && skip "can't get stripe info"
5339         [[ $nstripe -gt $OSTCOUNT ]] &&
5340                 skip "Wrong default stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
5341
5342         local stride=$($LFS getstripe -S -d $DIR)
5343         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
5344                 nstripe=$($LFS df $DIR | grep OST: | wc -l)
5345         fi
5346
5347         OFFSETS="0 $((stride/2)) $((stride-1))"
5348         for offset in $OFFSETS; do
5349                 for i in $(seq 0 $((nstripe-1))); do
5350                         local GLOBALOFFSETS=""
5351                         # size in Bytes
5352                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
5353                         local myfn=$DIR/d44a-$size
5354                         echo "--------writing $myfn at $size"
5355                         ll_sparseness_write $myfn $size ||
5356                                 error "ll_sparseness_write"
5357                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
5358                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5359                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5360
5361                         for j in $(seq 0 $((nstripe-1))); do
5362                                 # size in Bytes
5363                                 size=$((((j + $nstripe )*$stride + $offset)))
5364                                 ll_sparseness_write $myfn $size ||
5365                                         error "ll_sparseness_write"
5366                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
5367                         done
5368                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5369                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5370                         rm -f $myfn
5371                 done
5372         done
5373 }
5374 run_test 44a "test sparse pwrite ==============================="
5375
5376 dirty_osc_total() {
5377         tot=0
5378         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
5379                 tot=$(($tot + $d))
5380         done
5381         echo $tot
5382 }
5383 do_dirty_record() {
5384         before=`dirty_osc_total`
5385         echo executing "\"$*\""
5386         eval $*
5387         after=`dirty_osc_total`
5388         echo before $before, after $after
5389 }
5390 test_45() {
5391         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5392
5393         f="$DIR/f45"
5394         # Obtain grants from OST if it supports it
5395         echo blah > ${f}_grant
5396         stop_writeback
5397         sync
5398         do_dirty_record "echo blah > $f"
5399         [[ $before -eq $after ]] && error "write wasn't cached"
5400         do_dirty_record "> $f"
5401         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
5402         do_dirty_record "echo blah > $f"
5403         [[ $before -eq $after ]] && error "write wasn't cached"
5404         do_dirty_record "sync"
5405         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
5406         do_dirty_record "echo blah > $f"
5407         [[ $before -eq $after ]] && error "write wasn't cached"
5408         do_dirty_record "cancel_lru_locks osc"
5409         [[ $before -gt $after ]] ||
5410                 error "lock cancellation didn't lower dirty count"
5411         start_writeback
5412 }
5413 run_test 45 "osc io page accounting ============================"
5414
5415 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
5416 # test tickles a bug where re-dirtying a page was failing to be mapped to the
5417 # objects offset and an assert hit when an rpc was built with 1023's mapped
5418 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
5419 test_46() {
5420         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5421
5422         f="$DIR/f46"
5423         stop_writeback
5424         sync
5425         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5426         sync
5427         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5428         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5429         sync
5430         start_writeback
5431 }
5432 run_test 46 "dirtying a previously written page ================"
5433
5434 # test_47 is removed "Device nodes check" is moved to test_28
5435
5436 test_48a() { # bug 2399
5437         [ "$mds1_FSTYPE" = "zfs" ] &&
5438         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5439                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5440
5441         test_mkdir $DIR/$tdir
5442         cd $DIR/$tdir
5443         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5444         test_mkdir $DIR/$tdir
5445         touch foo || error "'touch foo' failed after recreating cwd"
5446         test_mkdir bar
5447         touch .foo || error "'touch .foo' failed after recreating cwd"
5448         test_mkdir .bar
5449         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5450         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5451         cd . || error "'cd .' failed after recreating cwd"
5452         mkdir . && error "'mkdir .' worked after recreating cwd"
5453         rmdir . && error "'rmdir .' worked after recreating cwd"
5454         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5455         cd .. || error "'cd ..' failed after recreating cwd"
5456 }
5457 run_test 48a "Access renamed working dir (should return errors)="
5458
5459 test_48b() { # bug 2399
5460         rm -rf $DIR/$tdir
5461         test_mkdir $DIR/$tdir
5462         cd $DIR/$tdir
5463         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5464         touch foo && error "'touch foo' worked after removing cwd"
5465         mkdir foo && error "'mkdir foo' worked after removing cwd"
5466         touch .foo && error "'touch .foo' worked after removing cwd"
5467         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5468         ls . > /dev/null && error "'ls .' worked after removing cwd"
5469         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5470         mkdir . && error "'mkdir .' worked after removing cwd"
5471         rmdir . && error "'rmdir .' worked after removing cwd"
5472         ln -s . foo && error "'ln -s .' worked after removing cwd"
5473         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5474 }
5475 run_test 48b "Access removed working dir (should return errors)="
5476
5477 test_48c() { # bug 2350
5478         #lctl set_param debug=-1
5479         #set -vx
5480         rm -rf $DIR/$tdir
5481         test_mkdir -p $DIR/$tdir/dir
5482         cd $DIR/$tdir/dir
5483         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5484         $TRACE touch foo && error "touch foo worked after removing cwd"
5485         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5486         touch .foo && error "touch .foo worked after removing cwd"
5487         mkdir .foo && error "mkdir .foo worked after removing cwd"
5488         $TRACE ls . && error "'ls .' worked after removing cwd"
5489         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5490         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5491         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5492         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5493         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5494 }
5495 run_test 48c "Access removed working subdir (should return errors)"
5496
5497 test_48d() { # bug 2350
5498         #lctl set_param debug=-1
5499         #set -vx
5500         rm -rf $DIR/$tdir
5501         test_mkdir -p $DIR/$tdir/dir
5502         cd $DIR/$tdir/dir
5503         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5504         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5505         $TRACE touch foo && error "'touch foo' worked after removing parent"
5506         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5507         touch .foo && error "'touch .foo' worked after removing parent"
5508         mkdir .foo && error "mkdir .foo worked after removing parent"
5509         $TRACE ls . && error "'ls .' worked after removing parent"
5510         $TRACE ls .. && error "'ls ..' worked after removing parent"
5511         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5512         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5513         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5514         true
5515 }
5516 run_test 48d "Access removed parent subdir (should return errors)"
5517
5518 test_48e() { # bug 4134
5519         #lctl set_param debug=-1
5520         #set -vx
5521         rm -rf $DIR/$tdir
5522         test_mkdir -p $DIR/$tdir/dir
5523         cd $DIR/$tdir/dir
5524         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5525         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5526         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5527         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5528         # On a buggy kernel addition of "touch foo" after cd .. will
5529         # produce kernel oops in lookup_hash_it
5530         touch ../foo && error "'cd ..' worked after recreate parent"
5531         cd $DIR
5532         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5533 }
5534 run_test 48e "Access to recreated parent subdir (should return errors)"
5535
5536 test_48f() {
5537         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
5538                 skip "need MDS >= 2.13.55"
5539         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
5540         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] ||
5541                 skip "needs different host for mdt1 mdt2"
5542         [[ $(facet_fstype mds1) == ldiskfs ]] || skip "ldiskfs only"
5543
5544         $LFS mkdir -i0 $DIR/$tdir
5545         $LFS mkdir -i 1 $DIR/$tdir/sub1 $DIR/$tdir/sub2 $DIR/$tdir/sub3
5546
5547         for d in sub1 sub2 sub3; do
5548                 #define OBD_FAIL_OSD_REF_DEL    0x19c
5549                 do_facet mds1 $LCTL set_param fail_loc=0x8000019c
5550                 rm -rf $DIR/$tdir/$d && error "rm $d should fail"
5551         done
5552
5553         rm -d --interactive=never $DIR/$tdir || error "rm $tdir fail"
5554 }
5555 run_test 48f "non-zero nlink dir unlink won't LBUG()"
5556
5557 test_49() { # LU-1030
5558         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5559         remote_ost_nodsh && skip "remote OST with nodsh"
5560
5561         # get ost1 size - $FSNAME-OST0000
5562         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5563                 awk '{ print $4 }')
5564         # write 800M at maximum
5565         [[ $ost1_size -lt 2 ]] && ost1_size=2
5566         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5567
5568         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5569         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5570         local dd_pid=$!
5571
5572         # change max_pages_per_rpc while writing the file
5573         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5574         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5575         # loop until dd process exits
5576         while ps ax -opid | grep -wq $dd_pid; do
5577                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5578                 sleep $((RANDOM % 5 + 1))
5579         done
5580         # restore original max_pages_per_rpc
5581         $LCTL set_param $osc1_mppc=$orig_mppc
5582         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5583 }
5584 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5585
5586 test_50() {
5587         # bug 1485
5588         test_mkdir $DIR/$tdir
5589         cd $DIR/$tdir
5590         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5591 }
5592 run_test 50 "special situations: /proc symlinks  ==============="
5593
5594 test_51a() {    # was test_51
5595         # bug 1516 - create an empty entry right after ".." then split dir
5596         test_mkdir -c1 $DIR/$tdir
5597         touch $DIR/$tdir/foo
5598         $MCREATE $DIR/$tdir/bar
5599         rm $DIR/$tdir/foo
5600         createmany -m $DIR/$tdir/longfile 201
5601         FNUM=202
5602         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5603                 $MCREATE $DIR/$tdir/longfile$FNUM
5604                 FNUM=$(($FNUM + 1))
5605                 echo -n "+"
5606         done
5607         echo
5608         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5609 }
5610 run_test 51a "special situations: split htree with empty entry =="
5611
5612 cleanup_print_lfs_df () {
5613         trap 0
5614         $LFS df
5615         $LFS df -i
5616 }
5617
5618 test_51b() {
5619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5620
5621         local dir=$DIR/$tdir
5622         local nrdirs=$((65536 + 100))
5623
5624         # cleanup the directory
5625         rm -fr $dir
5626
5627         mkdir_on_mdt -i $((RANDOM % MDSCOUNT)) $dir
5628
5629         $LFS df
5630         $LFS df -i
5631         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5632         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5633         [[ $numfree -lt $nrdirs ]] &&
5634                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5635
5636         # need to check free space for the directories as well
5637         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5638         numfree=$(( blkfree / $(fs_inode_ksize) ))
5639         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5640
5641         trap cleanup_print_lfs_df EXIT
5642
5643         # create files
5644         createmany -d $dir/d $nrdirs || {
5645                 unlinkmany $dir/d $nrdirs
5646                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5647         }
5648
5649         # really created :
5650         nrdirs=$(ls -U $dir | wc -l)
5651
5652         # unlink all but 100 subdirectories, then check it still works
5653         local left=100
5654         local delete=$((nrdirs - left))
5655
5656         $LFS df
5657         $LFS df -i
5658
5659         # for ldiskfs the nlink count should be 1, but this is OSD specific
5660         # and so this is listed for informational purposes only
5661         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5662         unlinkmany -d $dir/d $delete ||
5663                 error "unlink of first $delete subdirs failed"
5664
5665         echo "nlink between: $(stat -c %h $dir)"
5666         local found=$(ls -U $dir | wc -l)
5667         [ $found -ne $left ] &&
5668                 error "can't find subdirs: found only $found, expected $left"
5669
5670         unlinkmany -d $dir/d $delete $left ||
5671                 error "unlink of second $left subdirs failed"
5672         # regardless of whether the backing filesystem tracks nlink accurately
5673         # or not, the nlink count shouldn't be more than "." and ".." here
5674         local after=$(stat -c %h $dir)
5675         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5676                 echo "nlink after: $after"
5677
5678         cleanup_print_lfs_df
5679 }
5680 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5681
5682 test_51d() {
5683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5684         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5685
5686         test_mkdir $DIR/$tdir
5687         createmany -o $DIR/$tdir/t- 1000
5688         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5689         for N in $(seq 0 $((OSTCOUNT - 1))); do
5690                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5691                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5692                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5693                         '($1 == '$N') { objs += 1 } \
5694                         END { printf("%0.0f", objs) }')
5695                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5696         done
5697         unlinkmany $DIR/$tdir/t- 1000
5698
5699         NLAST=0
5700         for N in $(seq 1 $((OSTCOUNT - 1))); do
5701                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5702                         error "OST $N has less objects vs OST $NLAST" \
5703                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5704                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5705                         error "OST $N has less objects vs OST $NLAST" \
5706                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5707
5708                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5709                         error "OST $N has less #0 objects vs OST $NLAST" \
5710                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5711                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5712                         error "OST $N has less #0 objects vs OST $NLAST" \
5713                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5714                 NLAST=$N
5715         done
5716         rm -f $TMP/$tfile
5717 }
5718 run_test 51d "check object distribution"
5719
5720 test_51e() {
5721         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5722                 skip_env "ldiskfs only test"
5723         fi
5724
5725         test_mkdir -c1 $DIR/$tdir
5726         test_mkdir -c1 $DIR/$tdir/d0
5727
5728         touch $DIR/$tdir/d0/foo
5729         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5730                 error "file exceed 65000 nlink limit!"
5731         unlinkmany $DIR/$tdir/d0/f- 65001
5732         return 0
5733 }
5734 run_test 51e "check file nlink limit"
5735
5736 test_51f() {
5737         test_mkdir $DIR/$tdir
5738
5739         local max=100000
5740         local ulimit_old=$(ulimit -n)
5741         local spare=20 # number of spare fd's for scripts/libraries, etc.
5742         local mdt=$($LFS getstripe -m $DIR/$tdir)
5743         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5744
5745         echo "MDT$mdt numfree=$numfree, max=$max"
5746         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5747         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5748                 while ! ulimit -n $((numfree + spare)); do
5749                         numfree=$((numfree * 3 / 4))
5750                 done
5751                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5752         else
5753                 echo "left ulimit at $ulimit_old"
5754         fi
5755
5756         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5757                 unlinkmany $DIR/$tdir/f $numfree
5758                 error "create+open $numfree files in $DIR/$tdir failed"
5759         }
5760         ulimit -n $ulimit_old
5761
5762         # if createmany exits at 120s there will be fewer than $numfree files
5763         unlinkmany $DIR/$tdir/f $numfree || true
5764 }
5765 run_test 51f "check many open files limit"
5766
5767 test_52a() {
5768         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5769         test_mkdir $DIR/$tdir
5770         touch $DIR/$tdir/foo
5771         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5772         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5773         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5774         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5775         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5776                                         error "link worked"
5777         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5778         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5779         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5780                                                      error "lsattr"
5781         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5782         cp -r $DIR/$tdir $TMP/
5783         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5784 }
5785 run_test 52a "append-only flag test (should return errors)"
5786
5787 test_52b() {
5788         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5789         test_mkdir $DIR/$tdir
5790         touch $DIR/$tdir/foo
5791         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5792         cat test > $DIR/$tdir/foo && error "cat test worked"
5793         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5794         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5795         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5796                                         error "link worked"
5797         echo foo >> $DIR/$tdir/foo && error "echo worked"
5798         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5799         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5800         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5801         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5802                                                         error "lsattr"
5803         chattr -i $DIR/$tdir/foo || error "chattr failed"
5804
5805         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5806 }
5807 run_test 52b "immutable flag test (should return errors) ======="
5808
5809 test_53() {
5810         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5811         remote_mds_nodsh && skip "remote MDS with nodsh"
5812         remote_ost_nodsh && skip "remote OST with nodsh"
5813
5814         local param
5815         local param_seq
5816         local ostname
5817         local mds_last
5818         local mds_last_seq
5819         local ost_last
5820         local ost_last_seq
5821         local ost_last_id
5822         local ostnum
5823         local node
5824         local found=false
5825         local support_last_seq=true
5826
5827         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5828                 support_last_seq=false
5829
5830         # only test MDT0000
5831         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5832         local value
5833         for value in $(do_facet $SINGLEMDS \
5834                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5835                 param=$(echo ${value[0]} | cut -d "=" -f1)
5836                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5837
5838                 if $support_last_seq; then
5839                         param_seq=$(echo $param |
5840                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5841                         mds_last_seq=$(do_facet $SINGLEMDS \
5842                                        $LCTL get_param -n $param_seq)
5843                 fi
5844                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5845
5846                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5847                 node=$(facet_active_host ost$((ostnum+1)))
5848                 param="obdfilter.$ostname.last_id"
5849                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5850                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5851                         ost_last_id=$ost_last
5852
5853                         if $support_last_seq; then
5854                                 ost_last_id=$(echo $ost_last |
5855                                               awk -F':' '{print $2}' |
5856                                               sed -e "s/^0x//g")
5857                                 ost_last_seq=$(echo $ost_last |
5858                                                awk -F':' '{print $1}')
5859                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5860                         fi
5861
5862                         if [[ $ost_last_id != $mds_last ]]; then
5863                                 error "$ost_last_id != $mds_last"
5864                         else
5865                                 found=true
5866                                 break
5867                         fi
5868                 done
5869         done
5870         $found || error "can not match last_seq/last_id for $mdtosc"
5871         return 0
5872 }
5873 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5874
5875 test_54a() {
5876         perl -MSocket -e ';' || skip "no Socket perl module installed"
5877
5878         $SOCKETSERVER $DIR/socket ||
5879                 error "$SOCKETSERVER $DIR/socket failed: $?"
5880         $SOCKETCLIENT $DIR/socket ||
5881                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5882         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5883 }
5884 run_test 54a "unix domain socket test =========================="
5885
5886 test_54b() {
5887         f="$DIR/f54b"
5888         mknod $f c 1 3
5889         chmod 0666 $f
5890         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5891 }
5892 run_test 54b "char device works in lustre ======================"
5893
5894 find_loop_dev() {
5895         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5896         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5897         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5898
5899         for i in $(seq 3 7); do
5900                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5901                 LOOPDEV=$LOOPBASE$i
5902                 LOOPNUM=$i
5903                 break
5904         done
5905 }
5906
5907 cleanup_54c() {
5908         local rc=0
5909         loopdev="$DIR/loop54c"
5910
5911         trap 0
5912         $UMOUNT $DIR/$tdir || rc=$?
5913         losetup -d $loopdev || true
5914         losetup -d $LOOPDEV || true
5915         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5916         return $rc
5917 }
5918
5919 test_54c() {
5920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5921
5922         loopdev="$DIR/loop54c"
5923
5924         find_loop_dev
5925         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5926         trap cleanup_54c EXIT
5927         mknod $loopdev b 7 $LOOPNUM
5928         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5929         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5930         losetup $loopdev $DIR/$tfile ||
5931                 error "can't set up $loopdev for $DIR/$tfile"
5932         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5933         test_mkdir $DIR/$tdir
5934         mount -t ext2 $loopdev $DIR/$tdir ||
5935                 error "error mounting $loopdev on $DIR/$tdir"
5936         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5937                 error "dd write"
5938         df $DIR/$tdir
5939         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5940                 error "dd read"
5941         cleanup_54c
5942 }
5943 run_test 54c "block device works in lustre ====================="
5944
5945 test_54d() {
5946         f="$DIR/f54d"
5947         string="aaaaaa"
5948         mknod $f p
5949         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5950 }
5951 run_test 54d "fifo device works in lustre ======================"
5952
5953 test_54e() {
5954         f="$DIR/f54e"
5955         string="aaaaaa"
5956         cp -aL /dev/console $f
5957         echo $string > $f || error "echo $string to $f failed"
5958 }
5959 run_test 54e "console/tty device works in lustre ======================"
5960
5961 test_56a() {
5962         local numfiles=3
5963         local numdirs=2
5964         local dir=$DIR/$tdir
5965
5966         rm -rf $dir
5967         test_mkdir -p $dir/dir
5968         for i in $(seq $numfiles); do
5969                 touch $dir/file$i
5970                 touch $dir/dir/file$i
5971         done
5972
5973         local numcomp=$($LFS getstripe --component-count $dir)
5974
5975         [[ $numcomp == 0 ]] && numcomp=1
5976
5977         # test lfs getstripe with --recursive
5978         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5979
5980         [[ $filenum -eq $((numfiles * 2)) ]] ||
5981                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5982         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5983         [[ $filenum -eq $numfiles ]] ||
5984                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5985         echo "$LFS getstripe showed obdidx or l_ost_idx"
5986
5987         # test lfs getstripe with file instead of dir
5988         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5989         [[ $filenum -eq 1 ]] ||
5990                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5991         echo "$LFS getstripe file1 passed"
5992
5993         #test lfs getstripe with --verbose
5994         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5995         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5996                 error "$LFS getstripe --verbose $dir: "\
5997                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5998         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5999                 error "$LFS getstripe $dir: showed lmm_magic"
6000
6001         #test lfs getstripe with -v prints lmm_fid
6002         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
6003         local countfids=$((numdirs + numfiles * numcomp))
6004         [[ $filenum -eq $countfids ]] ||
6005                 error "$LFS getstripe -v $dir: "\
6006                       "got $filenum want $countfids lmm_fid"
6007         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
6008                 error "$LFS getstripe $dir: showed lmm_fid by default"
6009         echo "$LFS getstripe --verbose passed"
6010
6011         #check for FID information
6012         local fid1=$($LFS getstripe --fid $dir/file1)
6013         local fid2=$($LFS getstripe --verbose $dir/file1 |
6014                      awk '/lmm_fid: / { print $2; exit; }')
6015         local fid3=$($LFS path2fid $dir/file1)
6016
6017         [ "$fid1" != "$fid2" ] &&
6018                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
6019         [ "$fid1" != "$fid3" ] &&
6020                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
6021         echo "$LFS getstripe --fid passed"
6022
6023         #test lfs getstripe with --obd
6024         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
6025                 error "$LFS getstripe --obd wrong_uuid: should return error"
6026
6027         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6028
6029         local ostidx=1
6030         local obduuid=$(ostuuid_from_index $ostidx)
6031         local found=$($LFS getstripe -r --obd $obduuid $dir |
6032                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
6033
6034         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
6035         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
6036                 ((filenum--))
6037         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
6038                 ((filenum--))
6039
6040         [[ $found -eq $filenum ]] ||
6041                 error "$LFS getstripe --obd: found $found expect $filenum"
6042         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
6043                 sed '/^[         ]*'${ostidx}'[  ]/d' |
6044                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
6045                 error "$LFS getstripe --obd: should not show file on other obd"
6046         echo "$LFS getstripe --obd passed"
6047 }
6048 run_test 56a "check $LFS getstripe"
6049
6050 test_56b() {
6051         local dir=$DIR/$tdir
6052         local numdirs=3
6053
6054         test_mkdir $dir
6055         for i in $(seq $numdirs); do
6056                 test_mkdir $dir/dir$i
6057         done
6058
6059         # test lfs getdirstripe default mode is non-recursion, which is
6060         # different from lfs getstripe
6061         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
6062
6063         [[ $dircnt -eq 1 ]] ||
6064                 error "$LFS getdirstripe: found $dircnt, not 1"
6065         dircnt=$($LFS getdirstripe --recursive $dir |
6066                 grep -c lmv_stripe_count)
6067         [[ $dircnt -eq $((numdirs + 1)) ]] ||
6068                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
6069 }
6070 run_test 56b "check $LFS getdirstripe"
6071
6072 test_56c() {
6073         remote_ost_nodsh && skip "remote OST with nodsh"
6074
6075         local ost_idx=0
6076         local ost_name=$(ostname_from_index $ost_idx)
6077         local old_status=$(ost_dev_status $ost_idx)
6078         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6079
6080         [[ -z "$old_status" ]] ||
6081                 skip_env "OST $ost_name is in $old_status status"
6082
6083         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
6084         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6085                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
6086         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6087                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
6088                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
6089         fi
6090
6091         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
6092                 error "$LFS df -v showing inactive devices"
6093         sleep_maxage
6094
6095         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
6096
6097         [[ "$new_status" =~ "D" ]] ||
6098                 error "$ost_name status is '$new_status', missing 'D'"
6099         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
6100                 [[ "$new_status" =~ "N" ]] ||
6101                         error "$ost_name status is '$new_status', missing 'N'"
6102         fi
6103         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6104                 [[ "$new_status" =~ "f" ]] ||
6105                         error "$ost_name status is '$new_status', missing 'f'"
6106         fi
6107
6108         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
6109         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6110                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
6111         [[ -z "$p" ]] && restore_lustre_params < $p || true
6112         sleep_maxage
6113
6114         new_status=$(ost_dev_status $ost_idx)
6115         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
6116                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
6117         # can't check 'f' as devices may actually be on flash
6118 }
6119 run_test 56c "check 'lfs df' showing device status"
6120
6121 test_56d() {
6122         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
6123         local osts=$($LFS df -v $MOUNT | grep -c OST)
6124
6125         $LFS df $MOUNT
6126
6127         (( mdts == MDSCOUNT )) ||
6128                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
6129         (( osts == OSTCOUNT )) ||
6130                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
6131 }
6132 run_test 56d "'lfs df -v' prints only configured devices"
6133
6134 test_56e() {
6135         err_enoent=2 # No such file or directory
6136         err_eopnotsupp=95 # Operation not supported
6137
6138         enoent_mnt=/pmt1 # Invalid dentry. Path not present
6139         notsup_mnt=/tmp  # Valid dentry, but Not a lustreFS
6140
6141         # Check for handling of path not exists
6142         output=$($LFS df $enoent_mnt 2>&1)
6143         ret=$?
6144
6145         fs=$(echo $output | awk -F: '{print $2}' | awk '{print $3}' | tr -d \')
6146         [[ $fs = $enoent_mnt && $ret -eq $err_enoent ]] ||
6147                 error "expect failure $err_enoent, not $ret"
6148
6149         # Check for handling of non-Lustre FS
6150         output=$($LFS df $notsup_mnt)
6151         ret=$?
6152
6153         fs=$(echo $output | awk '{print $1}' | awk -F: '{print $2}')
6154         [[ $fs = $notsup_mnt && $ret -eq $err_eopnotsupp ]] ||
6155                 error "expect success $err_eopnotsupp, not $ret"
6156
6157         # Check for multiple LustreFS argument
6158         output=$($LFS df $MOUNT $MOUNT $MOUNT | grep -c "filesystem_summary:")
6159         ret=$?
6160
6161         [[ $output -eq 3 && $ret -eq 0 ]] ||
6162                 error "expect success 3, not $output, rc = $ret"
6163
6164         # Check for correct non-Lustre FS handling among multiple
6165         # LustreFS argument
6166         output=$($LFS df $MOUNT $notsup_mnt $MOUNT |
6167                 grep -c "filesystem_summary:"; exit ${PIPESTATUS[0]})
6168         ret=$?
6169
6170         [[ $output -eq 2 && $ret -eq $err_eopnotsupp ]] ||
6171                 error "expect success 2, not $output, rc = $ret"
6172 }
6173 run_test 56e "'lfs df' Handle non LustreFS & multiple LustreFS"
6174
6175 NUMFILES=3
6176 NUMDIRS=3
6177 setup_56() {
6178         local local_tdir="$1"
6179         local local_numfiles="$2"
6180         local local_numdirs="$3"
6181         local dir_params="$4"
6182         local dir_stripe_params="$5"
6183
6184         if [ ! -d "$local_tdir" ] ; then
6185                 test_mkdir -p $dir_stripe_params $local_tdir
6186                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
6187                 for i in $(seq $local_numfiles) ; do
6188                         touch $local_tdir/file$i
6189                 done
6190                 for i in $(seq $local_numdirs) ; do
6191                         test_mkdir $dir_stripe_params $local_tdir/dir$i
6192                         for j in $(seq $local_numfiles) ; do
6193                                 touch $local_tdir/dir$i/file$j
6194                         done
6195                 done
6196         fi
6197 }
6198
6199 setup_56_special() {
6200         local local_tdir=$1
6201         local local_numfiles=$2
6202         local local_numdirs=$3
6203
6204         setup_56 $local_tdir $local_numfiles $local_numdirs
6205
6206         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
6207                 for i in $(seq $local_numfiles) ; do
6208                         mknod $local_tdir/loop${i}b b 7 $i
6209                         mknod $local_tdir/null${i}c c 1 3
6210                         ln -s $local_tdir/file1 $local_tdir/link${i}
6211                 done
6212                 for i in $(seq $local_numdirs) ; do
6213                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
6214                         mknod $local_tdir/dir$i/null${i}c c 1 3
6215                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
6216                 done
6217         fi
6218 }
6219
6220 test_56g() {
6221         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6222         local expected=$(($NUMDIRS + 2))
6223
6224         setup_56 $dir $NUMFILES $NUMDIRS
6225
6226         # test lfs find with -name
6227         for i in $(seq $NUMFILES) ; do
6228                 local nums=$($LFS find -name "*$i" $dir | wc -l)
6229
6230                 [ $nums -eq $expected ] ||
6231                         error "lfs find -name '*$i' $dir wrong: "\
6232                               "found $nums, expected $expected"
6233         done
6234 }
6235 run_test 56g "check lfs find -name"
6236
6237 test_56h() {
6238         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6239         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
6240
6241         setup_56 $dir $NUMFILES $NUMDIRS
6242
6243         # test lfs find with ! -name
6244         for i in $(seq $NUMFILES) ; do
6245                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
6246
6247                 [ $nums -eq $expected ] ||
6248                         error "lfs find ! -name '*$i' $dir wrong: "\
6249                               "found $nums, expected $expected"
6250         done
6251 }
6252 run_test 56h "check lfs find ! -name"
6253
6254 test_56i() {
6255         local dir=$DIR/$tdir
6256
6257         test_mkdir $dir
6258
6259         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
6260         local out=$($cmd)
6261
6262         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
6263 }
6264 run_test 56i "check 'lfs find -ost UUID' skips directories"
6265
6266 test_56j() {
6267         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6268
6269         setup_56_special $dir $NUMFILES $NUMDIRS
6270
6271         local expected=$((NUMDIRS + 1))
6272         local cmd="$LFS find -type d $dir"
6273         local nums=$($cmd | wc -l)
6274
6275         [ $nums -eq $expected ] ||
6276                 error "'$cmd' wrong: found $nums, expected $expected"
6277 }
6278 run_test 56j "check lfs find -type d"
6279
6280 test_56k() {
6281         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6282
6283         setup_56_special $dir $NUMFILES $NUMDIRS
6284
6285         local expected=$(((NUMDIRS + 1) * NUMFILES))
6286         local cmd="$LFS find -type f $dir"
6287         local nums=$($cmd | wc -l)
6288
6289         [ $nums -eq $expected ] ||
6290                 error "'$cmd' wrong: found $nums, expected $expected"
6291 }
6292 run_test 56k "check lfs find -type f"
6293
6294 test_56l() {
6295         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6296
6297         setup_56_special $dir $NUMFILES $NUMDIRS
6298
6299         local expected=$((NUMDIRS + NUMFILES))
6300         local cmd="$LFS find -type b $dir"
6301         local nums=$($cmd | wc -l)
6302
6303         [ $nums -eq $expected ] ||
6304                 error "'$cmd' wrong: found $nums, expected $expected"
6305 }
6306 run_test 56l "check lfs find -type b"
6307
6308 test_56m() {
6309         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6310
6311         setup_56_special $dir $NUMFILES $NUMDIRS
6312
6313         local expected=$((NUMDIRS + NUMFILES))
6314         local cmd="$LFS find -type c $dir"
6315         local nums=$($cmd | wc -l)
6316         [ $nums -eq $expected ] ||
6317                 error "'$cmd' wrong: found $nums, expected $expected"
6318 }
6319 run_test 56m "check lfs find -type c"
6320
6321 test_56n() {
6322         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6323         setup_56_special $dir $NUMFILES $NUMDIRS
6324
6325         local expected=$((NUMDIRS + NUMFILES))
6326         local cmd="$LFS find -type l $dir"
6327         local nums=$($cmd | wc -l)
6328
6329         [ $nums -eq $expected ] ||
6330                 error "'$cmd' wrong: found $nums, expected $expected"
6331 }
6332 run_test 56n "check lfs find -type l"
6333
6334 test_56o() {
6335         local dir=$DIR/$tdir
6336
6337         setup_56 $dir $NUMFILES $NUMDIRS
6338         utime $dir/file1 > /dev/null || error "utime (1)"
6339         utime $dir/file2 > /dev/null || error "utime (2)"
6340         utime $dir/dir1 > /dev/null || error "utime (3)"
6341         utime $dir/dir2 > /dev/null || error "utime (4)"
6342         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
6343         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
6344
6345         local expected=4
6346         local nums=$($LFS find -mtime +0 $dir | wc -l)
6347
6348         [ $nums -eq $expected ] ||
6349                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
6350
6351         expected=12
6352         cmd="$LFS find -mtime 0 $dir"
6353         nums=$($cmd | wc -l)
6354         [ $nums -eq $expected ] ||
6355                 error "'$cmd' wrong: found $nums, expected $expected"
6356 }
6357 run_test 56o "check lfs find -mtime for old files"
6358
6359 test_56ob() {
6360         local dir=$DIR/$tdir
6361         local expected=1
6362         local count=0
6363
6364         # just to make sure there is something that won't be found
6365         test_mkdir $dir
6366         touch $dir/$tfile.now
6367
6368         for age in year week day hour min; do
6369                 count=$((count + 1))
6370
6371                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
6372                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
6373                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
6374
6375                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
6376                 local nums=$($cmd | wc -l)
6377                 [ $nums -eq $expected ] ||
6378                         error "'$cmd' wrong: found $nums, expected $expected"
6379
6380                 cmd="$LFS find $dir -atime $count${age:0:1}"
6381                 nums=$($cmd | wc -l)
6382                 [ $nums -eq $expected ] ||
6383                         error "'$cmd' wrong: found $nums, expected $expected"
6384         done
6385
6386         sleep 2
6387         cmd="$LFS find $dir -ctime +1s -type f"
6388         nums=$($cmd | wc -l)
6389         (( $nums == $count * 2 + 1)) ||
6390                 error "'$cmd' wrong: found $nums, expected $((count * 2 + 1))"
6391 }
6392 run_test 56ob "check lfs find -atime -mtime -ctime with units"
6393
6394 test_newerXY_base() {
6395         local x=$1
6396         local y=$2
6397         local dir=$DIR/$tdir
6398         local ref
6399         local negref
6400
6401         if [ $y == "t" ]; then
6402                 if [ $x == "b" ]; then
6403                         ref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6404                 else
6405                         ref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6406                 fi
6407         else
6408                 ref=$DIR/$tfile.newer.$x$y
6409                 touch $ref || error "touch $ref failed"
6410         fi
6411
6412         echo "before = $ref"
6413         sleep 2
6414         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6415         sleep 2
6416         if [ $y == "t" ]; then
6417                 if [ $x == "b" ]; then
6418                         negref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6419                 else
6420                         negref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6421                 fi
6422         else
6423                 negref=$DIR/$tfile.negnewer.$x$y
6424                 touch $negref || error "touch $negref failed"
6425         fi
6426
6427         echo "after = $negref"
6428         local cmd="$LFS find $dir -newer$x$y $ref"
6429         local nums=$(eval $cmd | wc -l)
6430         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
6431
6432         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6433                 error "'$cmd' wrong: found $nums newer, expected $expected"  ; }
6434
6435         cmd="$LFS find $dir ! -newer$x$y $negref"
6436         nums=$(eval $cmd | wc -l)
6437         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6438                 error "'$cmd' wrong: found $nums older, expected $expected"  ; }
6439
6440         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
6441         nums=$(eval $cmd | wc -l)
6442         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6443                 error "'$cmd' wrong: found $nums between, expected $expected"; }
6444
6445         rm -rf $DIR/*
6446 }
6447
6448 test_56oc() {
6449         test_newerXY_base "a" "a"
6450         test_newerXY_base "a" "m"
6451         test_newerXY_base "a" "c"
6452         test_newerXY_base "m" "a"
6453         test_newerXY_base "m" "m"
6454         test_newerXY_base "m" "c"
6455         test_newerXY_base "c" "a"
6456         test_newerXY_base "c" "m"
6457         test_newerXY_base "c" "c"
6458
6459         [[ -n "$sles_version" ]] &&
6460                 echo "skip timestamp tests on SLES, LU-13665" && return 0
6461
6462         test_newerXY_base "a" "t"
6463         test_newerXY_base "m" "t"
6464         test_newerXY_base "c" "t"
6465
6466         [[ $MDS1_VERSION -lt $(version_code 2.13.54) ||
6467            $CLIENT_VERSION -lt $(version_code 2.13.54) ]] &&
6468                 ! btime_supported && echo "btime unsupported" && return 0
6469
6470         test_newerXY_base "b" "b"
6471         test_newerXY_base "b" "t"
6472 }
6473 run_test 56oc "check lfs find -newerXY work"
6474
6475 btime_supported() {
6476         local dir=$DIR/$tdir
6477         local rc
6478
6479         mkdir -p $dir
6480         touch $dir/$tfile
6481         $LFS find $dir -btime -1d -type f
6482         rc=$?
6483         rm -rf $dir
6484         return $rc
6485 }
6486
6487 test_56od() {
6488         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
6489                 ! btime_supported && skip "btime unsupported on MDS"
6490
6491         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6492                 ! btime_supported && skip "btime unsupported on clients"
6493
6494         local dir=$DIR/$tdir
6495         local ref=$DIR/$tfile.ref
6496         local negref=$DIR/$tfile.negref
6497
6498         mkdir $dir || error "mkdir $dir failed"
6499         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6500         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6501         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6502         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6503         touch $ref || error "touch $ref failed"
6504         # sleep 3 seconds at least
6505         sleep 3
6506
6507         local before=$(do_facet mds1 date +%s)
6508         local skew=$(($(date +%s) - before + 1))
6509
6510         if (( skew < 0 && skew > -5 )); then
6511                 sleep $((0 - skew + 1))
6512                 skew=0
6513         fi
6514
6515         # Set the dir stripe params to limit files all on MDT0,
6516         # otherwise we need to calc the max clock skew between
6517         # the client and MDTs.
6518         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6519         sleep 2
6520         touch $negref || error "touch $negref failed"
6521
6522         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6523         local nums=$($cmd | wc -l)
6524         local expected=$(((NUMFILES + 1) * NUMDIRS))
6525
6526         [ $nums -eq $expected ] ||
6527                 error "'$cmd' wrong: found $nums, expected $expected"
6528
6529         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6530         nums=$($cmd | wc -l)
6531         expected=$((NUMFILES + 1))
6532         [ $nums -eq $expected ] ||
6533                 error "'$cmd' wrong: found $nums, expected $expected"
6534
6535         [ $skew -lt 0 ] && return
6536
6537         local after=$(do_facet mds1 date +%s)
6538         local age=$((after - before + 1 + skew))
6539
6540         cmd="$LFS find $dir -btime -${age}s -type f"
6541         nums=$($cmd | wc -l)
6542         expected=$(((NUMFILES + 1) * NUMDIRS))
6543
6544         echo "Clock skew between client and server: $skew, age:$age"
6545         [ $nums -eq $expected ] ||
6546                 error "'$cmd' wrong: found $nums, expected $expected"
6547
6548         expected=$(($NUMDIRS + 1))
6549         cmd="$LFS find $dir -btime -${age}s -type d"
6550         nums=$($cmd | wc -l)
6551         [ $nums -eq $expected ] ||
6552                 error "'$cmd' wrong: found $nums, expected $expected"
6553         rm -f $ref $negref || error "Failed to remove $ref $negref"
6554 }
6555 run_test 56od "check lfs find -btime with units"
6556
6557 test_56p() {
6558         [ $RUNAS_ID -eq $UID ] &&
6559                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6560
6561         local dir=$DIR/$tdir
6562
6563         setup_56 $dir $NUMFILES $NUMDIRS
6564         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6565
6566         local expected=$NUMFILES
6567         local cmd="$LFS find -uid $RUNAS_ID $dir"
6568         local nums=$($cmd | wc -l)
6569
6570         [ $nums -eq $expected ] ||
6571                 error "'$cmd' wrong: found $nums, expected $expected"
6572
6573         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6574         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6575         nums=$($cmd | wc -l)
6576         [ $nums -eq $expected ] ||
6577                 error "'$cmd' wrong: found $nums, expected $expected"
6578 }
6579 run_test 56p "check lfs find -uid and ! -uid"
6580
6581 test_56q() {
6582         [ $RUNAS_ID -eq $UID ] &&
6583                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6584
6585         local dir=$DIR/$tdir
6586
6587         setup_56 $dir $NUMFILES $NUMDIRS
6588         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6589
6590         local expected=$NUMFILES
6591         local cmd="$LFS find -gid $RUNAS_GID $dir"
6592         local nums=$($cmd | wc -l)
6593
6594         [ $nums -eq $expected ] ||
6595                 error "'$cmd' wrong: found $nums, expected $expected"
6596
6597         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6598         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6599         nums=$($cmd | wc -l)
6600         [ $nums -eq $expected ] ||
6601                 error "'$cmd' wrong: found $nums, expected $expected"
6602 }
6603 run_test 56q "check lfs find -gid and ! -gid"
6604
6605 test_56r() {
6606         local dir=$DIR/$tdir
6607
6608         setup_56 $dir $NUMFILES $NUMDIRS
6609
6610         local expected=12
6611         local cmd="$LFS find -size 0 -type f -lazy $dir"
6612         local nums=$($cmd | wc -l)
6613
6614         [ $nums -eq $expected ] ||
6615                 error "'$cmd' wrong: found $nums, expected $expected"
6616         cmd="$LFS find -size 0 -type f $dir"
6617         nums=$($cmd | wc -l)
6618         [ $nums -eq $expected ] ||
6619                 error "'$cmd' wrong: found $nums, expected $expected"
6620
6621         expected=0
6622         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6623         nums=$($cmd | wc -l)
6624         [ $nums -eq $expected ] ||
6625                 error "'$cmd' wrong: found $nums, expected $expected"
6626         cmd="$LFS find ! -size 0 -type f $dir"
6627         nums=$($cmd | wc -l)
6628         [ $nums -eq $expected ] ||
6629                 error "'$cmd' wrong: found $nums, expected $expected"
6630
6631         echo "test" > $dir/$tfile
6632         echo "test2" > $dir/$tfile.2 && sync
6633         expected=1
6634         cmd="$LFS find -size 5 -type f -lazy $dir"
6635         nums=$($cmd | wc -l)
6636         [ $nums -eq $expected ] ||
6637                 error "'$cmd' wrong: found $nums, expected $expected"
6638         cmd="$LFS find -size 5 -type f $dir"
6639         nums=$($cmd | wc -l)
6640         [ $nums -eq $expected ] ||
6641                 error "'$cmd' wrong: found $nums, expected $expected"
6642
6643         expected=1
6644         cmd="$LFS find -size +5 -type f -lazy $dir"
6645         nums=$($cmd | wc -l)
6646         [ $nums -eq $expected ] ||
6647                 error "'$cmd' wrong: found $nums, expected $expected"
6648         cmd="$LFS find -size +5 -type f $dir"
6649         nums=$($cmd | wc -l)
6650         [ $nums -eq $expected ] ||
6651                 error "'$cmd' wrong: found $nums, expected $expected"
6652
6653         expected=2
6654         cmd="$LFS find -size +0 -type f -lazy $dir"
6655         nums=$($cmd | wc -l)
6656         [ $nums -eq $expected ] ||
6657                 error "'$cmd' wrong: found $nums, expected $expected"
6658         cmd="$LFS find -size +0 -type f $dir"
6659         nums=$($cmd | wc -l)
6660         [ $nums -eq $expected ] ||
6661                 error "'$cmd' wrong: found $nums, expected $expected"
6662
6663         expected=2
6664         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6665         nums=$($cmd | wc -l)
6666         [ $nums -eq $expected ] ||
6667                 error "'$cmd' wrong: found $nums, expected $expected"
6668         cmd="$LFS find ! -size -5 -type f $dir"
6669         nums=$($cmd | wc -l)
6670         [ $nums -eq $expected ] ||
6671                 error "'$cmd' wrong: found $nums, expected $expected"
6672
6673         expected=12
6674         cmd="$LFS find -size -5 -type f -lazy $dir"
6675         nums=$($cmd | wc -l)
6676         [ $nums -eq $expected ] ||
6677                 error "'$cmd' wrong: found $nums, expected $expected"
6678         cmd="$LFS find -size -5 -type f $dir"
6679         nums=$($cmd | wc -l)
6680         [ $nums -eq $expected ] ||
6681                 error "'$cmd' wrong: found $nums, expected $expected"
6682 }
6683 run_test 56r "check lfs find -size works"
6684
6685 test_56ra_sub() {
6686         local expected=$1
6687         local glimpses=$2
6688         local cmd="$3"
6689
6690         cancel_lru_locks $OSC
6691
6692         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6693         local nums=$($cmd | wc -l)
6694
6695         [ $nums -eq $expected ] ||
6696                 error "'$cmd' wrong: found $nums, expected $expected"
6697
6698         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6699
6700         if (( rpcs_before + glimpses != rpcs_after )); then
6701                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6702                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6703
6704                 if [[ $glimpses == 0 ]]; then
6705                         error "'$cmd' should not send glimpse RPCs to OST"
6706                 else
6707                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6708                 fi
6709         fi
6710 }
6711
6712 test_56ra() {
6713         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6714                 skip "MDS < 2.12.58 doesn't return LSOM data"
6715         local dir=$DIR/$tdir
6716         local old_agl=$($LCTL get_param -n llite.*.statahead_agl)
6717
6718         [[ $OSC == "mdc" ]] && skip "statahead not needed for DoM files"
6719
6720         # statahead_agl may cause extra glimpse which confuses results. LU-13017
6721         $LCTL set_param -n llite.*.statahead_agl=0
6722         stack_trap "$LCTL set_param -n llite.*.statahead_agl=$old_agl"
6723
6724         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6725         # open and close all files to ensure LSOM is updated
6726         cancel_lru_locks $OSC
6727         find $dir -type f | xargs cat > /dev/null
6728
6729         #   expect_found  glimpse_rpcs  command_to_run
6730         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6731         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6732         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6733         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6734
6735         echo "test" > $dir/$tfile
6736         echo "test2" > $dir/$tfile.2 && sync
6737         cancel_lru_locks $OSC
6738         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6739
6740         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6741         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6742         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6743         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6744
6745         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6746         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6747         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6748         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6749         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6750         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6751 }
6752 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6753
6754 test_56rb() {
6755         local dir=$DIR/$tdir
6756         local tmp=$TMP/$tfile.log
6757         local mdt_idx;
6758
6759         test_mkdir -p $dir || error "failed to mkdir $dir"
6760         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6761                 error "failed to setstripe $dir/$tfile"
6762         mdt_idx=$($LFS getdirstripe -i $dir)
6763         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6764
6765         stack_trap "rm -f $tmp" EXIT
6766         $LFS find --size +100K --ost 0 $dir |& tee $tmp
6767         ! grep -q obd_uuid $tmp ||
6768                 error "failed to find --size +100K --ost 0 $dir"
6769         $LFS find --size +100K --mdt $mdt_idx $dir |& tee $tmp
6770         ! grep -q obd_uuid $tmp ||
6771                 error "failed to find --size +100K --mdt $mdt_idx $dir"
6772 }
6773 run_test 56rb "check lfs find --size --ost/--mdt works"
6774
6775 test_56rc() {
6776         (( MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
6777         local dir=$DIR/$tdir
6778         local found
6779
6780         test_mkdir -c 2 -H all_char $dir || error "failed to mkdir $dir"
6781         $LFS mkdir -c 2 --mdt-hash all_char $dir/$tdir-all{1..10}
6782         (( $MDSCOUNT > 2 )) &&
6783                 $LFS mkdir -c 3 --mdt-hash fnv_1a_64 $dir/$tdir-fnv{1..10}
6784         mkdir $dir/$tdir-{1..10}
6785         touch $dir/$tfile-{1..10}
6786
6787         found=$($LFS find $dir --mdt-count 2 | wc -l)
6788         expect=11
6789         (( $found == $expect )) || error "found $found 2-stripe, expect $expect"
6790
6791         found=$($LFS find $dir -T +1 | wc -l)
6792         (( $MDSCOUNT > 2 )) && expect=$((expect + 10))
6793         (( $found == $expect )) || error "found $found 2+stripe, expect $expect"
6794
6795         found=$($LFS find $dir --mdt-hash all_char | wc -l)
6796         expect=11
6797         (( $found == $expect )) || error "found $found all_char, expect $expect"
6798
6799         found=$($LFS find $dir --mdt-hash fnv_1a_64 | wc -l)
6800         (( $MDSCOUNT > 2 )) && expect=10 || expect=0
6801         (( $found == $expect )) || error "found $found all_char, expect $expect"
6802 }
6803 run_test 56rc "check lfs find --mdt-count/--mdt-hash works"
6804
6805 test_56s() { # LU-611 #LU-9369
6806         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6807
6808         local dir=$DIR/$tdir
6809         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6810
6811         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6812         for i in $(seq $NUMDIRS); do
6813                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6814         done
6815
6816         local expected=$NUMDIRS
6817         local cmd="$LFS find -c $OSTCOUNT $dir"
6818         local nums=$($cmd | wc -l)
6819
6820         [ $nums -eq $expected ] || {
6821                 $LFS getstripe -R $dir
6822                 error "'$cmd' wrong: found $nums, expected $expected"
6823         }
6824
6825         expected=$((NUMDIRS + onestripe))
6826         cmd="$LFS find -stripe-count +0 -type f $dir"
6827         nums=$($cmd | wc -l)
6828         [ $nums -eq $expected ] || {
6829                 $LFS getstripe -R $dir
6830                 error "'$cmd' wrong: found $nums, expected $expected"
6831         }
6832
6833         expected=$onestripe
6834         cmd="$LFS find -stripe-count 1 -type f $dir"
6835         nums=$($cmd | wc -l)
6836         [ $nums -eq $expected ] || {
6837                 $LFS getstripe -R $dir
6838                 error "'$cmd' wrong: found $nums, expected $expected"
6839         }
6840
6841         cmd="$LFS find -stripe-count -2 -type f $dir"
6842         nums=$($cmd | wc -l)
6843         [ $nums -eq $expected ] || {
6844                 $LFS getstripe -R $dir
6845                 error "'$cmd' wrong: found $nums, expected $expected"
6846         }
6847
6848         expected=0
6849         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6850         nums=$($cmd | wc -l)
6851         [ $nums -eq $expected ] || {
6852                 $LFS getstripe -R $dir
6853                 error "'$cmd' wrong: found $nums, expected $expected"
6854         }
6855 }
6856 run_test 56s "check lfs find -stripe-count works"
6857
6858 test_56t() { # LU-611 #LU-9369
6859         local dir=$DIR/$tdir
6860
6861         setup_56 $dir 0 $NUMDIRS
6862         for i in $(seq $NUMDIRS); do
6863                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6864         done
6865
6866         local expected=$NUMDIRS
6867         local cmd="$LFS find -S 8M $dir"
6868         local nums=$($cmd | wc -l)
6869
6870         [ $nums -eq $expected ] || {
6871                 $LFS getstripe -R $dir
6872                 error "'$cmd' wrong: found $nums, expected $expected"
6873         }
6874         rm -rf $dir
6875
6876         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6877
6878         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6879
6880         expected=$(((NUMDIRS + 1) * NUMFILES))
6881         cmd="$LFS find -stripe-size 512k -type f $dir"
6882         nums=$($cmd | wc -l)
6883         [ $nums -eq $expected ] ||
6884                 error "'$cmd' wrong: found $nums, expected $expected"
6885
6886         cmd="$LFS find -stripe-size +320k -type f $dir"
6887         nums=$($cmd | wc -l)
6888         [ $nums -eq $expected ] ||
6889                 error "'$cmd' wrong: found $nums, expected $expected"
6890
6891         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6892         cmd="$LFS find -stripe-size +200k -type f $dir"
6893         nums=$($cmd | wc -l)
6894         [ $nums -eq $expected ] ||
6895                 error "'$cmd' wrong: found $nums, expected $expected"
6896
6897         cmd="$LFS find -stripe-size -640k -type f $dir"
6898         nums=$($cmd | wc -l)
6899         [ $nums -eq $expected ] ||
6900                 error "'$cmd' wrong: found $nums, expected $expected"
6901
6902         expected=4
6903         cmd="$LFS find -stripe-size 256k -type f $dir"
6904         nums=$($cmd | wc -l)
6905         [ $nums -eq $expected ] ||
6906                 error "'$cmd' wrong: found $nums, expected $expected"
6907
6908         cmd="$LFS find -stripe-size -320k -type f $dir"
6909         nums=$($cmd | wc -l)
6910         [ $nums -eq $expected ] ||
6911                 error "'$cmd' wrong: found $nums, expected $expected"
6912
6913         expected=0
6914         cmd="$LFS find -stripe-size 1024k -type f $dir"
6915         nums=$($cmd | wc -l)
6916         [ $nums -eq $expected ] ||
6917                 error "'$cmd' wrong: found $nums, expected $expected"
6918 }
6919 run_test 56t "check lfs find -stripe-size works"
6920
6921 test_56u() { # LU-611
6922         local dir=$DIR/$tdir
6923
6924         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6925
6926         if [[ $OSTCOUNT -gt 1 ]]; then
6927                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6928                 onestripe=4
6929         else
6930                 onestripe=0
6931         fi
6932
6933         local expected=$(((NUMDIRS + 1) * NUMFILES))
6934         local cmd="$LFS find -stripe-index 0 -type f $dir"
6935         local nums=$($cmd | wc -l)
6936
6937         [ $nums -eq $expected ] ||
6938                 error "'$cmd' wrong: found $nums, expected $expected"
6939
6940         expected=$onestripe
6941         cmd="$LFS find -stripe-index 1 -type f $dir"
6942         nums=$($cmd | wc -l)
6943         [ $nums -eq $expected ] ||
6944                 error "'$cmd' wrong: found $nums, expected $expected"
6945
6946         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6947         nums=$($cmd | wc -l)
6948         [ $nums -eq $expected ] ||
6949                 error "'$cmd' wrong: found $nums, expected $expected"
6950
6951         expected=0
6952         # This should produce an error and not return any files
6953         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6954         nums=$($cmd 2>/dev/null | wc -l)
6955         [ $nums -eq $expected ] ||
6956                 error "'$cmd' wrong: found $nums, expected $expected"
6957
6958         if [[ $OSTCOUNT -gt 1 ]]; then
6959                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6960                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6961                 nums=$($cmd | wc -l)
6962                 [ $nums -eq $expected ] ||
6963                         error "'$cmd' wrong: found $nums, expected $expected"
6964         fi
6965 }
6966 run_test 56u "check lfs find -stripe-index works"
6967
6968 test_56v() {
6969         local mdt_idx=0
6970         local dir=$DIR/$tdir
6971
6972         setup_56 $dir $NUMFILES $NUMDIRS
6973
6974         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6975         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6976
6977         for file in $($LFS find -m $UUID $dir); do
6978                 file_midx=$($LFS getstripe -m $file)
6979                 [ $file_midx -eq $mdt_idx ] ||
6980                         error "lfs find -m $UUID != getstripe -m $file_midx"
6981         done
6982 }
6983 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6984
6985 test_56w() {
6986         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6987         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6988
6989         local dir=$DIR/$tdir
6990
6991         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6992
6993         local stripe_size=$($LFS getstripe -S -d $dir) ||
6994                 error "$LFS getstripe -S -d $dir failed"
6995         stripe_size=${stripe_size%% *}
6996
6997         local file_size=$((stripe_size * OSTCOUNT))
6998         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6999         local required_space=$((file_num * file_size))
7000         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
7001                            head -n1)
7002         [[ $free_space -le $((required_space / 1024)) ]] &&
7003                 skip_env "need $required_space, have $free_space kbytes"
7004
7005         local dd_bs=65536
7006         local dd_count=$((file_size / dd_bs))
7007
7008         # write data into the files
7009         local i
7010         local j
7011         local file
7012
7013         for i in $(seq $NUMFILES); do
7014                 file=$dir/file$i
7015                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7016                         error "write data into $file failed"
7017         done
7018         for i in $(seq $NUMDIRS); do
7019                 for j in $(seq $NUMFILES); do
7020                         file=$dir/dir$i/file$j
7021                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7022                                 error "write data into $file failed"
7023                 done
7024         done
7025
7026         # $LFS_MIGRATE will fail if hard link migration is unsupported
7027         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
7028                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
7029                         error "creating links to $dir/dir1/file1 failed"
7030         fi
7031
7032         local expected=-1
7033
7034         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
7035
7036         # lfs_migrate file
7037         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
7038
7039         echo "$cmd"
7040         eval $cmd || error "$cmd failed"
7041
7042         check_stripe_count $dir/file1 $expected
7043
7044         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
7045         then
7046                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
7047                 # OST 1 if it is on OST 0. This file is small enough to
7048                 # be on only one stripe.
7049                 file=$dir/migr_1_ost
7050                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
7051                         error "write data into $file failed"
7052                 local obdidx=$($LFS getstripe -i $file)
7053                 local oldmd5=$(md5sum $file)
7054                 local newobdidx=0
7055
7056                 [[ $obdidx -eq 0 ]] && newobdidx=1
7057                 cmd="$LFS migrate -i $newobdidx $file"
7058                 echo $cmd
7059                 eval $cmd || error "$cmd failed"
7060
7061                 local realobdix=$($LFS getstripe -i $file)
7062                 local newmd5=$(md5sum $file)
7063
7064                 [[ $newobdidx -ne $realobdix ]] &&
7065                         error "new OST is different (was=$obdidx, "\
7066                               "wanted=$newobdidx, got=$realobdix)"
7067                 [[ "$oldmd5" != "$newmd5" ]] &&
7068                         error "md5sum differ: $oldmd5, $newmd5"
7069         fi
7070
7071         # lfs_migrate dir
7072         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
7073         echo "$cmd"
7074         eval $cmd || error "$cmd failed"
7075
7076         for j in $(seq $NUMFILES); do
7077                 check_stripe_count $dir/dir1/file$j $expected
7078         done
7079
7080         # lfs_migrate works with lfs find
7081         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
7082              $LFS_MIGRATE -y -c $expected"
7083         echo "$cmd"
7084         eval $cmd || error "$cmd failed"
7085
7086         for i in $(seq 2 $NUMFILES); do
7087                 check_stripe_count $dir/file$i $expected
7088         done
7089         for i in $(seq 2 $NUMDIRS); do
7090                 for j in $(seq $NUMFILES); do
7091                 check_stripe_count $dir/dir$i/file$j $expected
7092                 done
7093         done
7094 }
7095 run_test 56w "check lfs_migrate -c stripe_count works"
7096
7097 test_56wb() {
7098         local file1=$DIR/$tdir/file1
7099         local create_pool=false
7100         local initial_pool=$($LFS getstripe -p $DIR)
7101         local pool_list=()
7102         local pool=""
7103
7104         echo -n "Creating test dir..."
7105         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7106         echo "done."
7107
7108         echo -n "Creating test file..."
7109         touch $file1 || error "cannot create file"
7110         echo "done."
7111
7112         echo -n "Detecting existing pools..."
7113         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
7114
7115         if [ ${#pool_list[@]} -gt 0 ]; then
7116                 echo "${pool_list[@]}"
7117                 for thispool in "${pool_list[@]}"; do
7118                         if [[ -z "$initial_pool" ||
7119                               "$initial_pool" != "$thispool" ]]; then
7120                                 pool="$thispool"
7121                                 echo "Using existing pool '$pool'"
7122                                 break
7123                         fi
7124                 done
7125         else
7126                 echo "none detected."
7127         fi
7128         if [ -z "$pool" ]; then
7129                 pool=${POOL:-testpool}
7130                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
7131                 echo -n "Creating pool '$pool'..."
7132                 create_pool=true
7133                 pool_add $pool &> /dev/null ||
7134                         error "pool_add failed"
7135                 echo "done."
7136
7137                 echo -n "Adding target to pool..."
7138                 pool_add_targets $pool 0 0 1 &> /dev/null ||
7139                         error "pool_add_targets failed"
7140                 echo "done."
7141         fi
7142
7143         echo -n "Setting pool using -p option..."
7144         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
7145                 error "migrate failed rc = $?"
7146         echo "done."
7147
7148         echo -n "Verifying test file is in pool after migrating..."
7149         [ "$($LFS getstripe -p $file1)" = $pool ] ||
7150                 error "file was not migrated to pool $pool"
7151         echo "done."
7152
7153         echo -n "Removing test file from pool '$pool'..."
7154         # "lfs migrate $file" won't remove the file from the pool
7155         # until some striping information is changed.
7156         $LFS migrate -c 1 $file1 &> /dev/null ||
7157                 error "cannot remove from pool"
7158         [ "$($LFS getstripe -p $file1)" ] &&
7159                 error "pool still set"
7160         echo "done."
7161
7162         echo -n "Setting pool using --pool option..."
7163         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
7164                 error "migrate failed rc = $?"
7165         echo "done."
7166
7167         # Clean up
7168         rm -f $file1
7169         if $create_pool; then
7170                 destroy_test_pools 2> /dev/null ||
7171                         error "destroy test pools failed"
7172         fi
7173 }
7174 run_test 56wb "check lfs_migrate pool support"
7175
7176 test_56wc() {
7177         local file1="$DIR/$tdir/file1"
7178         local parent_ssize
7179         local parent_scount
7180         local cur_ssize
7181         local cur_scount
7182         local orig_ssize
7183
7184         echo -n "Creating test dir..."
7185         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7186         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
7187                 error "cannot set stripe by '-S 1M -c 1'"
7188         echo "done"
7189
7190         echo -n "Setting initial stripe for test file..."
7191         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
7192                 error "cannot set stripe"
7193         cur_ssize=$($LFS getstripe -S "$file1")
7194         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
7195         echo "done."
7196
7197         # File currently set to -S 512K -c 1
7198
7199         # Ensure -c and -S options are rejected when -R is set
7200         echo -n "Verifying incompatible options are detected..."
7201         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
7202                 error "incompatible -c and -R options not detected"
7203         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
7204                 error "incompatible -S and -R options not detected"
7205         echo "done."
7206
7207         # Ensure unrecognized options are passed through to 'lfs migrate'
7208         echo -n "Verifying -S option is passed through to lfs migrate..."
7209         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
7210                 error "migration failed"
7211         cur_ssize=$($LFS getstripe -S "$file1")
7212         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
7213         echo "done."
7214
7215         # File currently set to -S 1M -c 1
7216
7217         # Ensure long options are supported
7218         echo -n "Verifying long options supported..."
7219         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
7220                 error "long option without argument not supported"
7221         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
7222                 error "long option with argument not supported"
7223         cur_ssize=$($LFS getstripe -S "$file1")
7224         [ $cur_ssize -eq 524288 ] ||
7225                 error "migrate --stripe-size $cur_ssize != 524288"
7226         echo "done."
7227
7228         # File currently set to -S 512K -c 1
7229
7230         if [ "$OSTCOUNT" -gt 1 ]; then
7231                 echo -n "Verifying explicit stripe count can be set..."
7232                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
7233                         error "migrate failed"
7234                 cur_scount=$($LFS getstripe -c "$file1")
7235                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
7236                 echo "done."
7237         fi
7238
7239         # File currently set to -S 512K -c 1 or -S 512K -c 2
7240
7241         # Ensure parent striping is used if -R is set, and no stripe
7242         # count or size is specified
7243         echo -n "Setting stripe for parent directory..."
7244         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7245                 error "cannot set stripe '-S 2M -c 1'"
7246         echo "done."
7247
7248         echo -n "Verifying restripe option uses parent stripe settings..."
7249         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
7250         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
7251         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
7252                 error "migrate failed"
7253         cur_ssize=$($LFS getstripe -S "$file1")
7254         [ $cur_ssize -eq $parent_ssize ] ||
7255                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
7256         cur_scount=$($LFS getstripe -c "$file1")
7257         [ $cur_scount -eq $parent_scount ] ||
7258                 error "migrate -R stripe_count $cur_scount != $parent_scount"
7259         echo "done."
7260
7261         # File currently set to -S 1M -c 1
7262
7263         # Ensure striping is preserved if -R is not set, and no stripe
7264         # count or size is specified
7265         echo -n "Verifying striping size preserved when not specified..."
7266         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
7267         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7268                 error "cannot set stripe on parent directory"
7269         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7270                 error "migrate failed"
7271         cur_ssize=$($LFS getstripe -S "$file1")
7272         [ $cur_ssize -eq $orig_ssize ] ||
7273                 error "migrate by default $cur_ssize != $orig_ssize"
7274         echo "done."
7275
7276         # Ensure file name properly detected when final option has no argument
7277         echo -n "Verifying file name properly detected..."
7278         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7279                 error "file name interpreted as option argument"
7280         echo "done."
7281
7282         # Clean up
7283         rm -f "$file1"
7284 }
7285 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
7286
7287 test_56wd() {
7288         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7289
7290         local file1=$DIR/$tdir/file1
7291
7292         echo -n "Creating test dir..."
7293         test_mkdir $DIR/$tdir || error "cannot create dir"
7294         echo "done."
7295
7296         echo -n "Creating test file..."
7297         touch $file1
7298         echo "done."
7299
7300         # Ensure 'lfs migrate' will fail by using a non-existent option,
7301         # and make sure rsync is not called to recover
7302         echo -n "Make sure --no-rsync option works..."
7303         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
7304                 grep -q 'refusing to fall back to rsync' ||
7305                 error "rsync was called with --no-rsync set"
7306         echo "done."
7307
7308         # Ensure rsync is called without trying 'lfs migrate' first
7309         echo -n "Make sure --rsync option works..."
7310         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
7311                 grep -q 'falling back to rsync' &&
7312                 error "lfs migrate was called with --rsync set"
7313         echo "done."
7314
7315         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
7316         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
7317                 grep -q 'at the same time' ||
7318                 error "--rsync and --no-rsync accepted concurrently"
7319         echo "done."
7320
7321         # Clean up
7322         rm -f $file1
7323 }
7324 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
7325
7326 test_56we() {
7327         local td=$DIR/$tdir
7328         local tf=$td/$tfile
7329
7330         test_mkdir $td || error "cannot create $td"
7331         touch $tf || error "cannot touch $tf"
7332
7333         echo -n "Make sure --non-direct|-D works..."
7334         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
7335                 grep -q "lfs migrate --non-direct" ||
7336                 error "--non-direct option cannot work correctly"
7337         $LFS_MIGRATE -y -D -v $tf 2>&1 |
7338                 grep -q "lfs migrate -D" ||
7339                 error "-D option cannot work correctly"
7340         echo "done."
7341 }
7342 run_test 56we "check lfs_migrate --non-direct|-D support"
7343
7344 test_56x() {
7345         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7346         check_swap_layouts_support
7347
7348         local dir=$DIR/$tdir
7349         local ref1=/etc/passwd
7350         local file1=$dir/file1
7351
7352         test_mkdir $dir || error "creating dir $dir"
7353         $LFS setstripe -c 2 $file1
7354         cp $ref1 $file1
7355         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
7356         stripe=$($LFS getstripe -c $file1)
7357         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7358         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7359
7360         # clean up
7361         rm -f $file1
7362 }
7363 run_test 56x "lfs migration support"
7364
7365 test_56xa() {
7366         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7367         check_swap_layouts_support
7368
7369         local dir=$DIR/$tdir/$testnum
7370
7371         test_mkdir -p $dir
7372
7373         local ref1=/etc/passwd
7374         local file1=$dir/file1
7375
7376         $LFS setstripe -c 2 $file1
7377         cp $ref1 $file1
7378         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
7379
7380         local stripe=$($LFS getstripe -c $file1)
7381
7382         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7383         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7384
7385         # clean up
7386         rm -f $file1
7387 }
7388 run_test 56xa "lfs migration --block support"
7389
7390 check_migrate_links() {
7391         local dir="$1"
7392         local file1="$dir/file1"
7393         local begin="$2"
7394         local count="$3"
7395         local runas="$4"
7396         local total_count=$(($begin + $count - 1))
7397         local symlink_count=10
7398         local uniq_count=10
7399
7400         if [ ! -f "$file1" ]; then
7401                 echo -n "creating initial file..."
7402                 $LFS setstripe -c 1 -S "512k" "$file1" ||
7403                         error "cannot setstripe initial file"
7404                 echo "done"
7405
7406                 echo -n "creating symlinks..."
7407                 for s in $(seq 1 $symlink_count); do
7408                         ln -s "$file1" "$dir/slink$s" ||
7409                                 error "cannot create symlinks"
7410                 done
7411                 echo "done"
7412
7413                 echo -n "creating nonlinked files..."
7414                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
7415                         error "cannot create nonlinked files"
7416                 echo "done"
7417         fi
7418
7419         # create hard links
7420         if [ ! -f "$dir/file$total_count" ]; then
7421                 echo -n "creating hard links $begin:$total_count..."
7422                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
7423                         /dev/null || error "cannot create hard links"
7424                 echo "done"
7425         fi
7426
7427         echo -n "checking number of hard links listed in xattrs..."
7428         local fid=$($LFS getstripe -F "$file1")
7429         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
7430
7431         echo "${#paths[*]}"
7432         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
7433                         skip "hard link list has unexpected size, skipping test"
7434         fi
7435         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
7436                         error "link names should exceed xattrs size"
7437         fi
7438
7439         echo -n "migrating files..."
7440         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
7441         local rc=$?
7442         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
7443         echo "done"
7444
7445         # make sure all links have been properly migrated
7446         echo -n "verifying files..."
7447         fid=$($LFS getstripe -F "$file1") ||
7448                 error "cannot get fid for file $file1"
7449         for i in $(seq 2 $total_count); do
7450                 local fid2=$($LFS getstripe -F $dir/file$i)
7451
7452                 [ "$fid2" == "$fid" ] ||
7453                         error "migrated hard link has mismatched FID"
7454         done
7455
7456         # make sure hard links were properly detected, and migration was
7457         # performed only once for the entire link set; nonlinked files should
7458         # also be migrated
7459         local actual=$(grep -c 'done' <<< "$migrate_out")
7460         local expected=$(($uniq_count + 1))
7461
7462         [ "$actual" -eq  "$expected" ] ||
7463                 error "hard links individually migrated ($actual != $expected)"
7464
7465         # make sure the correct number of hard links are present
7466         local hardlinks=$(stat -c '%h' "$file1")
7467
7468         [ $hardlinks -eq $total_count ] ||
7469                 error "num hard links $hardlinks != $total_count"
7470         echo "done"
7471
7472         return 0
7473 }
7474
7475 test_56xb() {
7476         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
7477                 skip "Need MDS version at least 2.10.55"
7478
7479         local dir="$DIR/$tdir"
7480
7481         test_mkdir "$dir" || error "cannot create dir $dir"
7482
7483         echo "testing lfs migrate mode when all links fit within xattrs"
7484         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
7485
7486         echo "testing rsync mode when all links fit within xattrs"
7487         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
7488
7489         echo "testing lfs migrate mode when all links do not fit within xattrs"
7490         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
7491
7492         echo "testing rsync mode when all links do not fit within xattrs"
7493         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
7494
7495         chown -R $RUNAS_ID $dir
7496         echo "testing non-root lfs migrate mode when not all links are in xattr"
7497         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
7498
7499         # clean up
7500         rm -rf $dir
7501 }
7502 run_test 56xb "lfs migration hard link support"
7503
7504 test_56xc() {
7505         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7506
7507         local dir="$DIR/$tdir"
7508
7509         test_mkdir "$dir" || error "cannot create dir $dir"
7510
7511         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7512         echo -n "Setting initial stripe for 20MB test file..."
7513         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7514                 error "cannot setstripe 20MB file"
7515         echo "done"
7516         echo -n "Sizing 20MB test file..."
7517         $TRUNCATE "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7518         echo "done"
7519         echo -n "Verifying small file autostripe count is 1..."
7520         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7521                 error "cannot migrate 20MB file"
7522         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7523                 error "cannot get stripe for $dir/20mb"
7524         [ $stripe_count -eq 1 ] ||
7525                 error "unexpected stripe count $stripe_count for 20MB file"
7526         rm -f "$dir/20mb"
7527         echo "done"
7528
7529         # Test 2: File is small enough to fit within the available space on
7530         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7531         # have at least an additional 1KB for each desired stripe for test 3
7532         echo -n "Setting stripe for 1GB test file..."
7533         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7534         echo "done"
7535         echo -n "Sizing 1GB test file..."
7536         # File size is 1GB + 3KB
7537         $TRUNCATE "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7538         echo "done"
7539
7540         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7541         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7542         if (( avail > 524288 * OSTCOUNT )); then
7543                 echo -n "Migrating 1GB file..."
7544                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7545                         error "cannot migrate 1GB file"
7546                 echo "done"
7547                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7548                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7549                         error "cannot getstripe for 1GB file"
7550                 [ $stripe_count -eq 2 ] ||
7551                         error "unexpected stripe count $stripe_count != 2"
7552                 echo "done"
7553         fi
7554
7555         # Test 3: File is too large to fit within the available space on
7556         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7557         if [ $OSTCOUNT -ge 3 ]; then
7558                 # The required available space is calculated as
7559                 # file size (1GB + 3KB) / OST count (3).
7560                 local kb_per_ost=349526
7561
7562                 echo -n "Migrating 1GB file with limit..."
7563                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7564                         error "cannot migrate 1GB file with limit"
7565                 echo "done"
7566
7567                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7568                 echo -n "Verifying 1GB autostripe count with limited space..."
7569                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7570                         error "unexpected stripe count $stripe_count (min 3)"
7571                 echo "done"
7572         fi
7573
7574         # clean up
7575         rm -rf $dir
7576 }
7577 run_test 56xc "lfs migration autostripe"
7578
7579 test_56xd() {
7580         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7581
7582         local dir=$DIR/$tdir
7583         local f_mgrt=$dir/$tfile.mgrt
7584         local f_yaml=$dir/$tfile.yaml
7585         local f_copy=$dir/$tfile.copy
7586         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7587         local layout_copy="-c 2 -S 2M -i 1"
7588         local yamlfile=$dir/yamlfile
7589         local layout_before;
7590         local layout_after;
7591
7592         test_mkdir "$dir" || error "cannot create dir $dir"
7593         $LFS setstripe $layout_yaml $f_yaml ||
7594                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7595         $LFS getstripe --yaml $f_yaml > $yamlfile
7596         $LFS setstripe $layout_copy $f_copy ||
7597                 error "cannot setstripe $f_copy with layout $layout_copy"
7598         touch $f_mgrt
7599         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7600
7601         # 1. test option --yaml
7602         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7603                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7604         layout_before=$(get_layout_param $f_yaml)
7605         layout_after=$(get_layout_param $f_mgrt)
7606         [ "$layout_after" == "$layout_before" ] ||
7607                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7608
7609         # 2. test option --copy
7610         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7611                 error "cannot migrate $f_mgrt with --copy $f_copy"
7612         layout_before=$(get_layout_param $f_copy)
7613         layout_after=$(get_layout_param $f_mgrt)
7614         [ "$layout_after" == "$layout_before" ] ||
7615                 error "lfs_migrate --copy: $layout_after != $layout_before"
7616 }
7617 run_test 56xd "check lfs_migrate --yaml and --copy support"
7618
7619 test_56xe() {
7620         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7621
7622         local dir=$DIR/$tdir
7623         local f_comp=$dir/$tfile
7624         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7625         local layout_before=""
7626         local layout_after=""
7627
7628         test_mkdir "$dir" || error "cannot create dir $dir"
7629         $LFS setstripe $layout $f_comp ||
7630                 error "cannot setstripe $f_comp with layout $layout"
7631         layout_before=$(get_layout_param $f_comp)
7632         dd if=/dev/zero of=$f_comp bs=1M count=4
7633
7634         # 1. migrate a comp layout file by lfs_migrate
7635         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7636         layout_after=$(get_layout_param $f_comp)
7637         [ "$layout_before" == "$layout_after" ] ||
7638                 error "lfs_migrate: $layout_before != $layout_after"
7639
7640         # 2. migrate a comp layout file by lfs migrate
7641         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7642         layout_after=$(get_layout_param $f_comp)
7643         [ "$layout_before" == "$layout_after" ] ||
7644                 error "lfs migrate: $layout_before != $layout_after"
7645 }
7646 run_test 56xe "migrate a composite layout file"
7647
7648 test_56xf() {
7649         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7650
7651         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7652                 skip "Need server version at least 2.13.53"
7653
7654         local dir=$DIR/$tdir
7655         local f_comp=$dir/$tfile
7656         local layout="-E 1M -c1 -E -1 -c2"
7657         local fid_before=""
7658         local fid_after=""
7659
7660         test_mkdir "$dir" || error "cannot create dir $dir"
7661         $LFS setstripe $layout $f_comp ||
7662                 error "cannot setstripe $f_comp with layout $layout"
7663         fid_before=$($LFS getstripe --fid $f_comp)
7664         dd if=/dev/zero of=$f_comp bs=1M count=4
7665
7666         # 1. migrate a comp layout file to a comp layout
7667         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7668         fid_after=$($LFS getstripe --fid $f_comp)
7669         [ "$fid_before" == "$fid_after" ] ||
7670                 error "comp-to-comp migrate: $fid_before != $fid_after"
7671
7672         # 2. migrate a comp layout file to a plain layout
7673         $LFS migrate -c2 $f_comp ||
7674                 error "cannot migrate $f_comp by lfs migrate"
7675         fid_after=$($LFS getstripe --fid $f_comp)
7676         [ "$fid_before" == "$fid_after" ] ||
7677                 error "comp-to-plain migrate: $fid_before != $fid_after"
7678
7679         # 3. migrate a plain layout file to a comp layout
7680         $LFS migrate $layout $f_comp ||
7681                 error "cannot migrate $f_comp by lfs migrate"
7682         fid_after=$($LFS getstripe --fid $f_comp)
7683         [ "$fid_before" == "$fid_after" ] ||
7684                 error "plain-to-comp migrate: $fid_before != $fid_after"
7685 }
7686 run_test 56xf "FID is not lost during migration of a composite layout file"
7687
7688 check_file_ost_range() {
7689         local file="$1"
7690         shift
7691         local range="$*"
7692         local -a file_range
7693         local idx
7694
7695         file_range=($($LFS getstripe -y "$file" |
7696                 awk '/l_ost_idx:/ { print $NF }'))
7697
7698         if [[ "${#file_range[@]}" = 0 ]]; then
7699                 echo "No osts found for $file"
7700                 return 1
7701         fi
7702
7703         for idx in "${file_range[@]}"; do
7704                 [[ " $range " =~ " $idx " ]] ||
7705                         return 1
7706         done
7707
7708         return 0
7709 }
7710
7711 sub_test_56xg() {
7712         local stripe_opt="$1"
7713         local pool="$2"
7714         shift 2
7715         local pool_ostidx="$(seq $* | tr '\n' ' ')"
7716
7717         $LFS migrate $stripe_opt -p $pool $DIR/$tfile ||
7718                 error "Fail to migrate $tfile on $pool"
7719         [[ "$($LFS getstripe -p $DIR/$tfile)" = "$pool" ]] ||
7720                 error "$tfile is not in pool $pool"
7721         check_file_ost_range "$DIR/$tfile" $pool_ostidx ||
7722                 error "$tfile osts mismatch with pool $pool (osts $pool_ostidx)"
7723 }
7724
7725 test_56xg() {
7726         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7727         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
7728         [[ $MDS1_VERSION -gt $(version_code 2.14.52) ]] ||
7729                 skip "Need MDS version newer than 2.14.52"
7730
7731         local -a pool_names=("${TESTNAME}_0" "${TESTNAME}_1" "${TESTNAME}_2")
7732         local -a pool_ranges=("0 0" "1 1" "0 1")
7733
7734         # init pools
7735         for i in "${!pool_names[@]}"; do
7736                 pool_add ${pool_names[$i]} ||
7737                         error "pool_add failed (pool: ${pool_names[$i]})"
7738                 pool_add_targets ${pool_names[$i]} ${pool_ranges[$i]} ||
7739                         error "pool_add_targets failed (pool: ${pool_names[$i]})"
7740         done
7741
7742         # init the file to migrate
7743         $LFS setstripe -c1 -i1 $DIR/$tfile ||
7744                 error "Unable to create $tfile on OST1"
7745         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=4 status=none ||
7746                 error "Unable to write on $tfile"
7747
7748         echo "1. migrate $tfile on pool ${pool_names[0]}"
7749         sub_test_56xg "-c-1" "${pool_names[0]}" ${pool_ranges[0]}
7750
7751         echo "2. migrate $tfile on pool ${pool_names[2]}"
7752         sub_test_56xg "-c-1 -S2M" "${pool_names[2]}" ${pool_ranges[2]}
7753
7754         echo "3. migrate $tfile on pool ${pool_names[1]}"
7755         sub_test_56xg "-n -c-1" "${pool_names[1]}" ${pool_ranges[1]}
7756
7757         echo "4. migrate $tfile on pool ${pool_names[2]} with default stripe parameters"
7758         sub_test_56xg "" "${pool_names[2]}" ${pool_ranges[2]}
7759         echo
7760
7761         # Clean pools
7762         destroy_test_pools ||
7763                 error "pool_destroy failed"
7764 }
7765 run_test 56xg "lfs migrate pool support"
7766
7767 test_56y() {
7768         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7769                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7770
7771         local res=""
7772         local dir=$DIR/$tdir
7773         local f1=$dir/file1
7774         local f2=$dir/file2
7775
7776         test_mkdir -p $dir || error "creating dir $dir"
7777         touch $f1 || error "creating std file $f1"
7778         $MULTIOP $f2 H2c || error "creating released file $f2"
7779
7780         # a directory can be raid0, so ask only for files
7781         res=$($LFS find $dir -L raid0 -type f | wc -l)
7782         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7783
7784         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7785         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7786
7787         # only files can be released, so no need to force file search
7788         res=$($LFS find $dir -L released)
7789         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7790
7791         res=$($LFS find $dir -type f \! -L released)
7792         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7793 }
7794 run_test 56y "lfs find -L raid0|released"
7795
7796 test_56z() { # LU-4824
7797         # This checks to make sure 'lfs find' continues after errors
7798         # There are two classes of errors that should be caught:
7799         # - If multiple paths are provided, all should be searched even if one
7800         #   errors out
7801         # - If errors are encountered during the search, it should not terminate
7802         #   early
7803         local dir=$DIR/$tdir
7804         local i
7805
7806         test_mkdir $dir
7807         for i in d{0..9}; do
7808                 test_mkdir $dir/$i
7809                 touch $dir/$i/$tfile
7810         done
7811         $LFS find $DIR/non_existent_dir $dir &&
7812                 error "$LFS find did not return an error"
7813         # Make a directory unsearchable. This should NOT be the last entry in
7814         # directory order.  Arbitrarily pick the 6th entry
7815         chmod 700 $($LFS find $dir -type d | sed '6!d')
7816
7817         $RUNAS $LFS find $DIR/non_existent $dir
7818         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7819
7820         # The user should be able to see 10 directories and 9 files
7821         (( count == 19 )) ||
7822                 error "$LFS find found $count != 19 entries after error"
7823 }
7824 run_test 56z "lfs find should continue after an error"
7825
7826 test_56aa() { # LU-5937
7827         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7828
7829         local dir=$DIR/$tdir
7830
7831         mkdir $dir
7832         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7833
7834         createmany -o $dir/striped_dir/${tfile}- 1024
7835         local dirs=$($LFS find --size +8k $dir/)
7836
7837         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7838 }
7839 run_test 56aa "lfs find --size under striped dir"
7840
7841 test_56ab() { # LU-10705
7842         test_mkdir $DIR/$tdir
7843         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7844         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7845         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7846         # Flush writes to ensure valid blocks.  Need to be more thorough for
7847         # ZFS, since blocks are not allocated/returned to client immediately.
7848         sync_all_data
7849         wait_zfs_commit ost1 2
7850         cancel_lru_locks osc
7851         ls -ls $DIR/$tdir
7852
7853         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7854
7855         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7856
7857         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7858         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7859
7860         rm -f $DIR/$tdir/$tfile.[123]
7861 }
7862 run_test 56ab "lfs find --blocks"
7863
7864 # LU-11188
7865 test_56aca() {
7866         local dir="$DIR/$tdir"
7867         local perms=(001 002 003 004 005 006 007
7868                      010 020 030 040 050 060 070
7869                      100 200 300 400 500 600 700
7870                      111 222 333 444 555 666 777)
7871         local perm_minus=(8 8 4 8 4 4 2
7872                           8 8 4 8 4 4 2
7873                           8 8 4 8 4 4 2
7874                           4 4 2 4 2 2 1)
7875         local perm_slash=(8  8 12  8 12 12 14
7876                           8  8 12  8 12 12 14
7877                           8  8 12  8 12 12 14
7878                          16 16 24 16 24 24 28)
7879
7880         test_mkdir "$dir"
7881         for perm in ${perms[*]}; do
7882                 touch "$dir/$tfile.$perm"
7883                 chmod $perm "$dir/$tfile.$perm"
7884         done
7885
7886         for ((i = 0; i < ${#perms[*]}; i++)); do
7887                 local num=$($LFS find $dir -perm ${perms[i]} | wc -l)
7888                 (( $num == 1 )) ||
7889                         error "lfs find -perm ${perms[i]}:"\
7890                               "$num != 1"
7891
7892                 num=$($LFS find $dir -perm -${perms[i]} -type f| wc -l)
7893                 (( $num == ${perm_minus[i]} )) ||
7894                         error "lfs find -perm -${perms[i]}:"\
7895                               "$num != ${perm_minus[i]}"
7896
7897                 num=$($LFS find $dir -perm /${perms[i]} -type f| wc -l)
7898                 (( $num == ${perm_slash[i]} )) ||
7899                         error "lfs find -perm /${perms[i]}:"\
7900                               "$num != ${perm_slash[i]}"
7901         done
7902 }
7903 run_test 56aca "check lfs find -perm with octal representation"
7904
7905 test_56acb() {
7906         local dir=$DIR/$tdir
7907         # p is the permission of write and execute for user, group and other
7908         # without the umask. It is used to test +wx.
7909         local p=$(printf "%o" "$((0333 & ~$(umask)))")
7910         local perms=(1000 000 2000 4000 $p 644 111 110 100 004)
7911         local symbolic=(+t  a+t u+t g+t o+t
7912                         g+s u+s o+s +s o+sr
7913                         o=r,ug+o,u+w
7914                         u+ g+ o+ a+ ugo+
7915                         u- g- o- a- ugo-
7916                         u= g= o= a= ugo=
7917                         o=r,ug+o,u+w u=r,a+u,u+w
7918                         g=r,ugo=g,u+w u+x,+X +X
7919                         u+x,u+X u+X u+x,g+X o+r,+X
7920                         u+x,go+X +wx +rwx)
7921
7922         test_mkdir $dir
7923         for perm in ${perms[*]}; do
7924                 touch "$dir/$tfile.$perm"
7925                 chmod $perm "$dir/$tfile.$perm"
7926         done
7927
7928         for (( i = 0; i < ${#symbolic[*]}; i++ )); do
7929                 local num=$($LFS find $dir -perm ${symbolic[i]} | wc -l)
7930
7931                 (( $num == 1 )) ||
7932                         error "lfs find $dir -perm ${symbolic[i]}: $num != 1"
7933         done
7934 }
7935 run_test 56acb "check lfs find -perm with symbolic representation"
7936
7937 test_56acc() {
7938         local dir=$DIR/$tdir
7939         local tests="17777 787 789 abcd
7940                 ug=uu ug=a ug=gu uo=ou urw
7941                 u+xg+x a=r,u+x,"
7942
7943         test_mkdir $dir
7944         for err in $tests; do
7945                 if $LFS find $dir -perm $err 2>/dev/null; then
7946                         error "lfs find -perm $err: parsing should have failed"
7947                 fi
7948         done
7949 }
7950 run_test 56acc "check parsing error for lfs find -perm"
7951
7952 test_56ba() {
7953         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7954                 skip "Need MDS version at least 2.10.50"
7955
7956         # Create composite files with one component
7957         local dir=$DIR/$tdir
7958
7959         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7960         # Create composite files with three components
7961         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7962         # Create non-composite files
7963         createmany -o $dir/${tfile}- 10
7964
7965         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7966
7967         [[ $nfiles == 10 ]] ||
7968                 error "lfs find -E 1M found $nfiles != 10 files"
7969
7970         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7971         [[ $nfiles == 25 ]] ||
7972                 error "lfs find ! -E 1M found $nfiles != 25 files"
7973
7974         # All files have a component that starts at 0
7975         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7976         [[ $nfiles == 35 ]] ||
7977                 error "lfs find --component-start 0 - $nfiles != 35 files"
7978
7979         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7980         [[ $nfiles == 15 ]] ||
7981                 error "lfs find --component-start 2M - $nfiles != 15 files"
7982
7983         # All files created here have a componenet that does not starts at 2M
7984         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7985         [[ $nfiles == 35 ]] ||
7986                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7987
7988         # Find files with a specified number of components
7989         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7990         [[ $nfiles == 15 ]] ||
7991                 error "lfs find --component-count 3 - $nfiles != 15 files"
7992
7993         # Remember non-composite files have a component count of zero
7994         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7995         [[ $nfiles == 10 ]] ||
7996                 error "lfs find --component-count 0 - $nfiles != 10 files"
7997
7998         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7999         [[ $nfiles == 20 ]] ||
8000                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
8001
8002         # All files have a flag called "init"
8003         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
8004         [[ $nfiles == 35 ]] ||
8005                 error "lfs find --component-flags init - $nfiles != 35 files"
8006
8007         # Multi-component files will have a component not initialized
8008         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
8009         [[ $nfiles == 15 ]] ||
8010                 error "lfs find !--component-flags init - $nfiles != 15 files"
8011
8012         rm -rf $dir
8013
8014 }
8015 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
8016
8017 test_56ca() {
8018         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
8019                 skip "Need MDS version at least 2.10.57"
8020
8021         local td=$DIR/$tdir
8022         local tf=$td/$tfile
8023         local dir
8024         local nfiles
8025         local cmd
8026         local i
8027         local j
8028
8029         # create mirrored directories and mirrored files
8030         mkdir $td || error "mkdir $td failed"
8031         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
8032         createmany -o $tf- 10 || error "create $tf- failed"
8033
8034         for i in $(seq 2); do
8035                 dir=$td/dir$i
8036                 mkdir $dir || error "mkdir $dir failed"
8037                 $LFS mirror create -N$((3 + i)) $dir ||
8038                         error "create mirrored dir $dir failed"
8039                 createmany -o $dir/$tfile- 10 ||
8040                         error "create $dir/$tfile- failed"
8041         done
8042
8043         # change the states of some mirrored files
8044         echo foo > $tf-6
8045         for i in $(seq 2); do
8046                 dir=$td/dir$i
8047                 for j in $(seq 4 9); do
8048                         echo foo > $dir/$tfile-$j
8049                 done
8050         done
8051
8052         # find mirrored files with specific mirror count
8053         cmd="$LFS find --mirror-count 3 --type f $td"
8054         nfiles=$($cmd | wc -l)
8055         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
8056
8057         cmd="$LFS find ! --mirror-count 3 --type f $td"
8058         nfiles=$($cmd | wc -l)
8059         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
8060
8061         cmd="$LFS find --mirror-count +2 --type f $td"
8062         nfiles=$($cmd | wc -l)
8063         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8064
8065         cmd="$LFS find --mirror-count -6 --type f $td"
8066         nfiles=$($cmd | wc -l)
8067         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8068
8069         # find mirrored files with specific file state
8070         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
8071         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
8072
8073         cmd="$LFS find --mirror-state=ro --type f $td"
8074         nfiles=$($cmd | wc -l)
8075         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
8076
8077         cmd="$LFS find ! --mirror-state=ro --type f $td"
8078         nfiles=$($cmd | wc -l)
8079         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8080
8081         cmd="$LFS find --mirror-state=wp --type f $td"
8082         nfiles=$($cmd | wc -l)
8083         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8084
8085         cmd="$LFS find ! --mirror-state=sp --type f $td"
8086         nfiles=$($cmd | wc -l)
8087         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8088 }
8089 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
8090
8091 test_56da() { # LU-14179
8092         local path=$DIR/$tdir
8093
8094         test_mkdir $path
8095         cd $path
8096
8097         local longdir=$(str_repeat 'a' 255)
8098
8099         for i in {1..15}; do
8100                 path=$path/$longdir
8101                 test_mkdir $longdir
8102                 cd $longdir
8103         done
8104
8105         local len=${#path}
8106         local lastdir=$(str_repeat 'a' $((4096 - 1 - $len - 1)))
8107
8108         test_mkdir $lastdir
8109         cd $lastdir
8110         # PATH_MAX-1
8111         (( ${#PWD} == 4095 )) || error "bad PWD length ${#PWD}, expect 4095"
8112
8113         # NAME_MAX
8114         touch $(str_repeat 'f' 255)
8115
8116         $LFS find $DIR/$tdir --type d |& grep "lfs find: error" &&
8117                 error "lfs find reported an error"
8118
8119         rm -rf $DIR/$tdir
8120 }
8121 run_test 56da "test lfs find with long paths"
8122
8123 test_57a() {
8124         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8125         # note test will not do anything if MDS is not local
8126         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8127                 skip_env "ldiskfs only test"
8128         fi
8129         remote_mds_nodsh && skip "remote MDS with nodsh"
8130
8131         local MNTDEV="osd*.*MDT*.mntdev"
8132         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
8133         [ -z "$DEV" ] && error "can't access $MNTDEV"
8134         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
8135                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
8136                         error "can't access $DEV"
8137                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
8138                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
8139                 rm $TMP/t57a.dump
8140         done
8141 }
8142 run_test 57a "verify MDS filesystem created with large inodes =="
8143
8144 test_57b() {
8145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8146         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8147                 skip_env "ldiskfs only test"
8148         fi
8149         remote_mds_nodsh && skip "remote MDS with nodsh"
8150
8151         local dir=$DIR/$tdir
8152         local filecount=100
8153         local file1=$dir/f1
8154         local fileN=$dir/f$filecount
8155
8156         rm -rf $dir || error "removing $dir"
8157         test_mkdir -c1 $dir
8158         local mdtidx=$($LFS getstripe -m $dir)
8159         local mdtname=MDT$(printf %04x $mdtidx)
8160         local facet=mds$((mdtidx + 1))
8161
8162         echo "mcreating $filecount files"
8163         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
8164
8165         # verify that files do not have EAs yet
8166         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
8167                 error "$file1 has an EA"
8168         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
8169                 error "$fileN has an EA"
8170
8171         sync
8172         sleep 1
8173         df $dir  #make sure we get new statfs data
8174         local mdsfree=$(do_facet $facet \
8175                         lctl get_param -n osd*.*$mdtname.kbytesfree)
8176         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8177         local file
8178
8179         echo "opening files to create objects/EAs"
8180         for file in $(seq -f $dir/f%g 1 $filecount); do
8181                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
8182                         error "opening $file"
8183         done
8184
8185         # verify that files have EAs now
8186         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
8187         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
8188
8189         sleep 1  #make sure we get new statfs data
8190         df $dir
8191         local mdsfree2=$(do_facet $facet \
8192                          lctl get_param -n osd*.*$mdtname.kbytesfree)
8193         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8194
8195         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
8196                 if [ "$mdsfree" != "$mdsfree2" ]; then
8197                         error "MDC before $mdcfree != after $mdcfree2"
8198                 else
8199                         echo "MDC before $mdcfree != after $mdcfree2"
8200                         echo "unable to confirm if MDS has large inodes"
8201                 fi
8202         fi
8203         rm -rf $dir
8204 }
8205 run_test 57b "default LOV EAs are stored inside large inodes ==="
8206
8207 test_58() {
8208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8209         [ -z "$(which wiretest 2>/dev/null)" ] &&
8210                         skip_env "could not find wiretest"
8211
8212         wiretest
8213 }
8214 run_test 58 "verify cross-platform wire constants =============="
8215
8216 test_59() {
8217         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8218
8219         echo "touch 130 files"
8220         createmany -o $DIR/f59- 130
8221         echo "rm 130 files"
8222         unlinkmany $DIR/f59- 130
8223         sync
8224         # wait for commitment of removal
8225         wait_delete_completed
8226 }
8227 run_test 59 "verify cancellation of llog records async ========="
8228
8229 TEST60_HEAD="test_60 run $RANDOM"
8230 test_60a() {
8231         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8232         remote_mgs_nodsh && skip "remote MGS with nodsh"
8233         do_facet mgs "! which run-llog.sh &> /dev/null" &&
8234                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
8235                         skip_env "missing subtest run-llog.sh"
8236
8237         log "$TEST60_HEAD - from kernel mode"
8238         do_facet mgs "$LCTL dk > /dev/null"
8239         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
8240         do_facet mgs $LCTL dk > $TMP/$tfile
8241
8242         # LU-6388: test llog_reader
8243         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
8244         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
8245         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
8246                         skip_env "missing llog_reader"
8247         local fstype=$(facet_fstype mgs)
8248         [ $fstype != ldiskfs -a $fstype != zfs ] &&
8249                 skip_env "Only for ldiskfs or zfs type mgs"
8250
8251         local mntpt=$(facet_mntpt mgs)
8252         local mgsdev=$(mgsdevname 1)
8253         local fid_list
8254         local fid
8255         local rec_list
8256         local rec
8257         local rec_type
8258         local obj_file
8259         local path
8260         local seq
8261         local oid
8262         local pass=true
8263
8264         #get fid and record list
8265         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
8266                 tail -n 4))
8267         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
8268                 tail -n 4))
8269         #remount mgs as ldiskfs or zfs type
8270         stop mgs || error "stop mgs failed"
8271         mount_fstype mgs || error "remount mgs failed"
8272         for ((i = 0; i < ${#fid_list[@]}; i++)); do
8273                 fid=${fid_list[i]}
8274                 rec=${rec_list[i]}
8275                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
8276                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
8277                 oid=$((16#$oid))
8278
8279                 case $fstype in
8280                         ldiskfs )
8281                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
8282                         zfs )
8283                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
8284                 esac
8285                 echo "obj_file is $obj_file"
8286                 do_facet mgs $llog_reader $obj_file
8287
8288                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
8289                         awk '{ print $3 }' | sed -e "s/^type=//g")
8290                 if [ $rec_type != $rec ]; then
8291                         echo "FAILED test_60a wrong record type $rec_type," \
8292                               "should be $rec"
8293                         pass=false
8294                         break
8295                 fi
8296
8297                 #check obj path if record type is LLOG_LOGID_MAGIC
8298                 if [ "$rec" == "1064553b" ]; then
8299                         path=$(do_facet mgs $llog_reader $obj_file |
8300                                 grep "path=" | awk '{ print $NF }' |
8301                                 sed -e "s/^path=//g")
8302                         if [ $obj_file != $mntpt/$path ]; then
8303                                 echo "FAILED test_60a wrong obj path" \
8304                                       "$montpt/$path, should be $obj_file"
8305                                 pass=false
8306                                 break
8307                         fi
8308                 fi
8309         done
8310         rm -f $TMP/$tfile
8311         #restart mgs before "error", otherwise it will block the next test
8312         stop mgs || error "stop mgs failed"
8313         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
8314         $pass || error "test failed, see FAILED test_60a messages for specifics"
8315 }
8316 run_test 60a "llog_test run from kernel module and test llog_reader"
8317
8318 test_60b() { # bug 6411
8319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8320
8321         dmesg > $DIR/$tfile
8322         LLOG_COUNT=$(do_facet mgs dmesg |
8323                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
8324                           /llog_[a-z]*.c:[0-9]/ {
8325                                 if (marker)
8326                                         from_marker++
8327                                 from_begin++
8328                           }
8329                           END {
8330                                 if (marker)
8331                                         print from_marker
8332                                 else
8333                                         print from_begin
8334                           }")
8335
8336         [[ $LLOG_COUNT -gt 120 ]] &&
8337                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
8338 }
8339 run_test 60b "limit repeated messages from CERROR/CWARN"
8340
8341 test_60c() {
8342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8343
8344         echo "create 5000 files"
8345         createmany -o $DIR/f60c- 5000
8346 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
8347         lctl set_param fail_loc=0x80000137
8348         unlinkmany $DIR/f60c- 5000
8349         lctl set_param fail_loc=0
8350 }
8351 run_test 60c "unlink file when mds full"
8352
8353 test_60d() {
8354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8355
8356         SAVEPRINTK=$(lctl get_param -n printk)
8357         # verify "lctl mark" is even working"
8358         MESSAGE="test message ID $RANDOM $$"
8359         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8360         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
8361
8362         lctl set_param printk=0 || error "set lnet.printk failed"
8363         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
8364         MESSAGE="new test message ID $RANDOM $$"
8365         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
8366         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8367         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
8368
8369         lctl set_param -n printk="$SAVEPRINTK"
8370 }
8371 run_test 60d "test printk console message masking"
8372
8373 test_60e() {
8374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8375         remote_mds_nodsh && skip "remote MDS with nodsh"
8376
8377         touch $DIR/$tfile
8378 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
8379         do_facet mds1 lctl set_param fail_loc=0x15b
8380         rm $DIR/$tfile
8381 }
8382 run_test 60e "no space while new llog is being created"
8383
8384 test_60f() {
8385         local old_path=$($LCTL get_param -n debug_path)
8386
8387         stack_trap "$LCTL set_param debug_path=$old_path"
8388         stack_trap "rm -f $TMP/$tfile*"
8389         rm -f $TMP/$tfile* 2> /dev/null
8390         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8391         $LCTL set_param debug_path=$TMP/$tfile fail_loc=0x8000050e
8392         test_mkdir $DIR/$tdir
8393         # retry in case the open is cached and not released
8394         for (( i = 0; i < 100 && $(ls $TMP/$tfile* | wc -l) == 0; i++ )); do
8395                 echo $i > $DIR/$tdir/$tfile.$i && cat $DIR/$tdir/$tfile.$i
8396                 sleep 0.1
8397         done
8398         ls $TMP/$tfile*
8399         (( $(ls $TMP/$tfile* | wc -l) > 0 )) || error "$TMP/$tfile not dumped"
8400 }
8401 run_test 60f "change debug_path works"
8402
8403 test_60g() {
8404         local pid
8405         local i
8406
8407         test_mkdir -c $MDSCOUNT $DIR/$tdir
8408
8409         (
8410                 local index=0
8411                 while true; do
8412                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
8413                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
8414                                 2>/dev/null
8415                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
8416                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
8417                         index=$((index + 1))
8418                 done
8419         ) &
8420
8421         pid=$!
8422
8423         for i in {0..100}; do
8424                 # define OBD_FAIL_OSD_TXN_START    0x19a
8425                 local index=$((i % MDSCOUNT + 1))
8426
8427                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
8428                         > /dev/null
8429                 sleep 0.01
8430         done
8431
8432         kill -9 $pid
8433
8434         for i in $(seq $MDSCOUNT); do
8435                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
8436         done
8437
8438         mkdir $DIR/$tdir/new || error "mkdir failed"
8439         rmdir $DIR/$tdir/new || error "rmdir failed"
8440
8441         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
8442                 -t namespace
8443         for i in $(seq $MDSCOUNT); do
8444                 wait_update_facet mds$i "$LCTL get_param -n \
8445                         mdd.$(facet_svc mds$i).lfsck_namespace |
8446                         awk '/^status/ { print \\\$2 }'" "completed"
8447         done
8448
8449         ls -R $DIR/$tdir || error "ls failed"
8450         rm -rf $DIR/$tdir || error "rmdir failed"
8451 }
8452 run_test 60g "transaction abort won't cause MDT hung"
8453
8454 test_60h() {
8455         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
8456                 skip "Need MDS version at least 2.12.52"
8457         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
8458
8459         local f
8460
8461         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
8462         #define OBD_FAIL_MDS_STRIPE_FID          0x189
8463         for fail_loc in 0x80000188 0x80000189; do
8464                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
8465                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
8466                         error "mkdir $dir-$fail_loc failed"
8467                 for i in {0..10}; do
8468                         # create may fail on missing stripe
8469                         echo $i > $DIR/$tdir-$fail_loc/$i
8470                 done
8471                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8472                         error "getdirstripe $tdir-$fail_loc failed"
8473                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
8474                         error "migrate $tdir-$fail_loc failed"
8475                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8476                         error "getdirstripe $tdir-$fail_loc failed"
8477                 pushd $DIR/$tdir-$fail_loc
8478                 for f in *; do
8479                         echo $f | cmp $f - || error "$f data mismatch"
8480                 done
8481                 popd
8482                 rm -rf $DIR/$tdir-$fail_loc
8483         done
8484 }
8485 run_test 60h "striped directory with missing stripes can be accessed"
8486
8487 function t60i_load() {
8488         mkdir $DIR/$tdir
8489         #define OBD_FAIL_LLOG_PAUSE_AFTER_PAD               0x131c
8490         $LCTL set_param fail_loc=0x131c fail_val=1
8491         for ((i=0; i<5000; i++)); do
8492                 touch $DIR/$tdir/f$i
8493         done
8494 }
8495
8496 test_60i() {
8497         changelog_register || error "changelog_register failed"
8498         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
8499         changelog_users $SINGLEMDS | grep -q $cl_user ||
8500                 error "User $cl_user not found in changelog_users"
8501         changelog_chmask "ALL"
8502         t60i_load &
8503         local PID=$!
8504         for((i=0; i<100; i++)); do
8505                 changelog_dump >/dev/null ||
8506                         error "can't read changelog"
8507         done
8508         kill $PID
8509         wait $PID
8510         changelog_deregister || error "changelog_deregister failed"
8511         $LCTL set_param fail_loc=0
8512 }
8513 run_test 60i "llog: new record vs reader race"
8514
8515 test_61a() {
8516         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8517
8518         f="$DIR/f61"
8519         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
8520         cancel_lru_locks osc
8521         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
8522         sync
8523 }
8524 run_test 61a "mmap() writes don't make sync hang ================"
8525
8526 test_61b() {
8527         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
8528 }
8529 run_test 61b "mmap() of unstriped file is successful"
8530
8531 # bug 2330 - insufficient obd_match error checking causes LBUG
8532 test_62() {
8533         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8534
8535         f="$DIR/f62"
8536         echo foo > $f
8537         cancel_lru_locks osc
8538         lctl set_param fail_loc=0x405
8539         cat $f && error "cat succeeded, expect -EIO"
8540         lctl set_param fail_loc=0
8541 }
8542 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
8543 # match every page all of the time.
8544 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
8545
8546 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
8547 # Though this test is irrelevant anymore, it helped to reveal some
8548 # other grant bugs (LU-4482), let's keep it.
8549 test_63a() {   # was test_63
8550         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8551
8552         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
8553
8554         for i in `seq 10` ; do
8555                 dd if=/dev/zero of=$DIR/f63 bs=8k &
8556                 sleep 5
8557                 kill $!
8558                 sleep 1
8559         done
8560
8561         rm -f $DIR/f63 || true
8562 }
8563 run_test 63a "Verify oig_wait interruption does not crash ======="
8564
8565 # bug 2248 - async write errors didn't return to application on sync
8566 # bug 3677 - async write errors left page locked
8567 test_63b() {
8568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8569
8570         debugsave
8571         lctl set_param debug=-1
8572
8573         # ensure we have a grant to do async writes
8574         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
8575         rm $DIR/$tfile
8576
8577         sync    # sync lest earlier test intercept the fail_loc
8578
8579         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8580         lctl set_param fail_loc=0x80000406
8581         $MULTIOP $DIR/$tfile Owy && \
8582                 error "sync didn't return ENOMEM"
8583         sync; sleep 2; sync     # do a real sync this time to flush page
8584         lctl get_param -n llite.*.dump_page_cache | grep locked && \
8585                 error "locked page left in cache after async error" || true
8586         debugrestore
8587 }
8588 run_test 63b "async write errors should be returned to fsync ==="
8589
8590 test_64a () {
8591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8592
8593         lfs df $DIR
8594         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
8595 }
8596 run_test 64a "verify filter grant calculations (in kernel) ====="
8597
8598 test_64b () {
8599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8600
8601         sh oos.sh $MOUNT || error "oos.sh failed: $?"
8602 }
8603 run_test 64b "check out-of-space detection on client"
8604
8605 test_64c() {
8606         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
8607 }
8608 run_test 64c "verify grant shrink"
8609
8610 import_param() {
8611         local tgt=$1
8612         local param=$2
8613
8614         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
8615 }
8616
8617 # this does exactly what osc_request.c:osc_announce_cached() does in
8618 # order to calculate max amount of grants to ask from server
8619 want_grant() {
8620         local tgt=$1
8621
8622         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
8623         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
8624
8625         ((rpc_in_flight++));
8626         nrpages=$((nrpages * rpc_in_flight))
8627
8628         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
8629
8630         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
8631
8632         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
8633         local undirty=$((nrpages * PAGE_SIZE))
8634
8635         local max_extent_pages
8636         max_extent_pages=$(import_param $tgt grant_max_extent_size)
8637         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
8638         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
8639         local grant_extent_tax
8640         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8641
8642         undirty=$((undirty + nrextents * grant_extent_tax))
8643
8644         echo $undirty
8645 }
8646
8647 # this is size of unit for grant allocation. It should be equal to
8648 # what tgt_grant.c:tgt_grant_chunk() calculates
8649 grant_chunk() {
8650         local tgt=$1
8651         local max_brw_size
8652         local grant_extent_tax
8653
8654         max_brw_size=$(import_param $tgt max_brw_size)
8655
8656         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8657
8658         echo $(((max_brw_size + grant_extent_tax) * 2))
8659 }
8660
8661 test_64d() {
8662         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
8663                 skip "OST < 2.10.55 doesn't limit grants enough"
8664
8665         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
8666
8667         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
8668                 skip "no grant_param connect flag"
8669
8670         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8671
8672         $LCTL set_param -n -n debug="$OLDDEBUG" || true
8673         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8674
8675
8676         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
8677         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
8678
8679         $LFS setstripe $DIR/$tfile -i 0 -c 1
8680         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
8681         ddpid=$!
8682
8683         while kill -0 $ddpid; do
8684                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8685
8686                 if [[ $cur_grant -gt $max_cur_granted ]]; then
8687                         kill $ddpid
8688                         error "cur_grant $cur_grant > $max_cur_granted"
8689                 fi
8690
8691                 sleep 1
8692         done
8693 }
8694 run_test 64d "check grant limit exceed"
8695
8696 check_grants() {
8697         local tgt=$1
8698         local expected=$2
8699         local msg=$3
8700         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8701
8702         ((cur_grants == expected)) ||
8703                 error "$msg: grants mismatch: $cur_grants, expected $expected"
8704 }
8705
8706 round_up_p2() {
8707         echo $((($1 + $2 - 1) & ~($2 - 1)))
8708 }
8709
8710 test_64e() {
8711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8712         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
8713                 skip "Need OSS version at least 2.11.56"
8714
8715         # Remount client to reset grant
8716         remount_client $MOUNT || error "failed to remount client"
8717         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8718
8719         local init_grants=$(import_param $osc_tgt initial_grant)
8720
8721         check_grants $osc_tgt $init_grants "init grants"
8722
8723         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8724         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8725         local gbs=$(import_param $osc_tgt grant_block_size)
8726
8727         # write random number of bytes from max_brw_size / 4 to max_brw_size
8728         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
8729         # align for direct io
8730         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
8731         # round to grant consumption unit
8732         local wb_round_up=$(round_up_p2 $write_bytes gbs)
8733
8734         local grants=$((wb_round_up + extent_tax))
8735
8736         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
8737
8738         # define OBD_FAIL_TGT_NO_GRANT 0x725
8739         # make the server not grant more back
8740         do_facet ost1 $LCTL set_param fail_loc=0x725
8741         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
8742
8743         do_facet ost1 $LCTL set_param fail_loc=0
8744
8745         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
8746
8747         rm -f $DIR/$tfile || error "rm failed"
8748
8749         # Remount client to reset grant
8750         remount_client $MOUNT || error "failed to remount client"
8751         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8752
8753         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8754
8755         # define OBD_FAIL_TGT_NO_GRANT 0x725
8756         # make the server not grant more back
8757         do_facet ost1 $LCTL set_param fail_loc=0x725
8758         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
8759         do_facet ost1 $LCTL set_param fail_loc=0
8760
8761         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
8762 }
8763 run_test 64e "check grant consumption (no grant allocation)"
8764
8765 test_64f() {
8766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8767
8768         # Remount client to reset grant
8769         remount_client $MOUNT || error "failed to remount client"
8770         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8771
8772         local init_grants=$(import_param $osc_tgt initial_grant)
8773         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8774         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8775         local gbs=$(import_param $osc_tgt grant_block_size)
8776         local chunk=$(grant_chunk $osc_tgt)
8777
8778         # write random number of bytes from max_brw_size / 4 to max_brw_size
8779         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
8780         # align for direct io
8781         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
8782         # round to grant consumption unit
8783         local wb_round_up=$(round_up_p2 $write_bytes gbs)
8784
8785         local grants=$((wb_round_up + extent_tax))
8786
8787         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8788         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
8789                 error "error writing to $DIR/$tfile"
8790
8791         check_grants $osc_tgt $((init_grants - grants + chunk)) \
8792                 "direct io with grant allocation"
8793
8794         rm -f $DIR/$tfile || error "rm failed"
8795
8796         # Remount client to reset grant
8797         remount_client $MOUNT || error "failed to remount client"
8798         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8799
8800         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8801
8802         local cmd="oO_WRONLY:w${write_bytes}_yc"
8803
8804         $MULTIOP $DIR/$tfile $cmd &
8805         MULTIPID=$!
8806         sleep 1
8807
8808         check_grants $osc_tgt $((init_grants - grants)) \
8809                 "buffered io, not write rpc"
8810
8811         kill -USR1 $MULTIPID
8812         wait
8813
8814         check_grants $osc_tgt $((init_grants - grants + chunk)) \
8815                 "buffered io, one RPC"
8816 }
8817 run_test 64f "check grant consumption (with grant allocation)"
8818
8819 # bug 1414 - set/get directories' stripe info
8820 test_65a() {
8821         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8822
8823         test_mkdir $DIR/$tdir
8824         touch $DIR/$tdir/f1
8825         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
8826 }
8827 run_test 65a "directory with no stripe info"
8828
8829 test_65b() {
8830         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8831
8832         test_mkdir $DIR/$tdir
8833         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8834
8835         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8836                                                 error "setstripe"
8837         touch $DIR/$tdir/f2
8838         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
8839 }
8840 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
8841
8842 test_65c() {
8843         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8844         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
8845
8846         test_mkdir $DIR/$tdir
8847         local stripesize=$($LFS getstripe -S $DIR/$tdir)
8848
8849         $LFS setstripe -S $((stripesize * 4)) -i 1 \
8850                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
8851         touch $DIR/$tdir/f3
8852         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
8853 }
8854 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
8855
8856 test_65d() {
8857         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8858
8859         test_mkdir $DIR/$tdir
8860         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
8861         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8862
8863         if [[ $STRIPECOUNT -le 0 ]]; then
8864                 sc=1
8865         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
8866                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
8867                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
8868         else
8869                 sc=$(($STRIPECOUNT - 1))
8870         fi
8871         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
8872         touch $DIR/$tdir/f4 $DIR/$tdir/f5
8873         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
8874                 error "lverify failed"
8875 }
8876 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
8877
8878 test_65e() {
8879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8880
8881         test_mkdir $DIR/$tdir
8882
8883         $LFS setstripe $DIR/$tdir || error "setstripe"
8884         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8885                                         error "no stripe info failed"
8886         touch $DIR/$tdir/f6
8887         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
8888 }
8889 run_test 65e "directory setstripe defaults"
8890
8891 test_65f() {
8892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8893
8894         test_mkdir $DIR/${tdir}f
8895         $RUNAS $LFS setstripe $DIR/${tdir}f &&
8896                 error "setstripe succeeded" || true
8897 }
8898 run_test 65f "dir setstripe permission (should return error) ==="
8899
8900 test_65g() {
8901         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8902
8903         test_mkdir $DIR/$tdir
8904         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8905
8906         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8907                 error "setstripe -S failed"
8908         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
8909         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8910                 error "delete default stripe failed"
8911 }
8912 run_test 65g "directory setstripe -d"
8913
8914 test_65h() {
8915         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8916
8917         test_mkdir $DIR/$tdir
8918         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8919
8920         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8921                 error "setstripe -S failed"
8922         test_mkdir $DIR/$tdir/dd1
8923         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
8924                 error "stripe info inherit failed"
8925 }
8926 run_test 65h "directory stripe info inherit ===================="
8927
8928 test_65i() {
8929         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8930
8931         save_layout_restore_at_exit $MOUNT
8932
8933         # bug6367: set non-default striping on root directory
8934         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
8935
8936         # bug12836: getstripe on -1 default directory striping
8937         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
8938
8939         # bug12836: getstripe -v on -1 default directory striping
8940         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
8941
8942         # bug12836: new find on -1 default directory striping
8943         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
8944 }
8945 run_test 65i "various tests to set root directory striping"
8946
8947 test_65j() { # bug6367
8948         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8949
8950         sync; sleep 1
8951
8952         # if we aren't already remounting for each test, do so for this test
8953         if [ "$I_MOUNTED" = "yes" ]; then
8954                 cleanup || error "failed to unmount"
8955                 setup
8956         fi
8957
8958         save_layout_restore_at_exit $MOUNT
8959
8960         $LFS setstripe -d $MOUNT || error "setstripe failed"
8961 }
8962 run_test 65j "set default striping on root directory (bug 6367)="
8963
8964 cleanup_65k() {
8965         rm -rf $DIR/$tdir
8966         wait_delete_completed
8967         do_facet $SINGLEMDS "lctl set_param -n \
8968                 osp.$ost*MDT0000.max_create_count=$max_count"
8969         do_facet $SINGLEMDS "lctl set_param -n \
8970                 osp.$ost*MDT0000.create_count=$count"
8971         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8972         echo $INACTIVE_OSC "is Activate"
8973
8974         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8975 }
8976
8977 test_65k() { # bug11679
8978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8979         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8980         remote_mds_nodsh && skip "remote MDS with nodsh"
8981
8982         local disable_precreate=true
8983         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
8984                 disable_precreate=false
8985
8986         echo "Check OST status: "
8987         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
8988                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
8989
8990         for OSC in $MDS_OSCS; do
8991                 echo $OSC "is active"
8992                 do_facet $SINGLEMDS lctl --device %$OSC activate
8993         done
8994
8995         for INACTIVE_OSC in $MDS_OSCS; do
8996                 local ost=$(osc_to_ost $INACTIVE_OSC)
8997                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
8998                                lov.*md*.target_obd |
8999                                awk -F: /$ost/'{ print $1 }' | head -n 1)
9000
9001                 mkdir -p $DIR/$tdir
9002                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
9003                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
9004
9005                 echo "Deactivate: " $INACTIVE_OSC
9006                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
9007
9008                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
9009                               osp.$ost*MDT0000.create_count")
9010                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
9011                                   osp.$ost*MDT0000.max_create_count")
9012                 $disable_precreate &&
9013                         do_facet $SINGLEMDS "lctl set_param -n \
9014                                 osp.$ost*MDT0000.max_create_count=0"
9015
9016                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
9017                         [ -f $DIR/$tdir/$idx ] && continue
9018                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
9019                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
9020                                 { cleanup_65k;
9021                                   error "setstripe $idx should succeed"; }
9022                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
9023                 done
9024                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
9025                 rmdir $DIR/$tdir
9026
9027                 do_facet $SINGLEMDS "lctl set_param -n \
9028                         osp.$ost*MDT0000.max_create_count=$max_count"
9029                 do_facet $SINGLEMDS "lctl set_param -n \
9030                         osp.$ost*MDT0000.create_count=$count"
9031                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9032                 echo $INACTIVE_OSC "is Activate"
9033
9034                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9035         done
9036 }
9037 run_test 65k "validate manual striping works properly with deactivated OSCs"
9038
9039 test_65l() { # bug 12836
9040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9041
9042         test_mkdir -p $DIR/$tdir/test_dir
9043         $LFS setstripe -c -1 $DIR/$tdir/test_dir
9044         $LFS find -mtime -1 $DIR/$tdir >/dev/null
9045 }
9046 run_test 65l "lfs find on -1 stripe dir ========================"
9047
9048 test_65m() {
9049         local layout=$(save_layout $MOUNT)
9050         $RUNAS $LFS setstripe -c 2 $MOUNT && {
9051                 restore_layout $MOUNT $layout
9052                 error "setstripe should fail by non-root users"
9053         }
9054         true
9055 }
9056 run_test 65m "normal user can't set filesystem default stripe"
9057
9058 test_65n() {
9059         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
9060         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
9061                 skip "Need MDS version at least 2.12.50"
9062         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
9063
9064         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
9065         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
9066         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
9067
9068         save_layout_restore_at_exit $MOUNT
9069
9070         # new subdirectory under root directory should not inherit
9071         # the default layout from root
9072         local dir1=$MOUNT/$tdir-1
9073         mkdir $dir1 || error "mkdir $dir1 failed"
9074         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
9075                 error "$dir1 shouldn't have LOV EA"
9076
9077         # delete the default layout on root directory
9078         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
9079
9080         local dir2=$MOUNT/$tdir-2
9081         mkdir $dir2 || error "mkdir $dir2 failed"
9082         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
9083                 error "$dir2 shouldn't have LOV EA"
9084
9085         # set a new striping pattern on root directory
9086         local def_stripe_size=$($LFS getstripe -S $MOUNT)
9087         local new_def_stripe_size=$((def_stripe_size * 2))
9088         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
9089                 error "set stripe size on $MOUNT failed"
9090
9091         # new file created in $dir2 should inherit the new stripe size from
9092         # the filesystem default
9093         local file2=$dir2/$tfile-2
9094         touch $file2 || error "touch $file2 failed"
9095
9096         local file2_stripe_size=$($LFS getstripe -S $file2)
9097         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
9098         {
9099                 echo "file2_stripe_size: '$file2_stripe_size'"
9100                 echo "new_def_stripe_size: '$new_def_stripe_size'"
9101                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
9102         }
9103
9104         local dir3=$MOUNT/$tdir-3
9105         mkdir $dir3 || error "mkdir $dir3 failed"
9106         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
9107         # the root layout, which is the actual default layout that will be used
9108         # when new files are created in $dir3.
9109         local dir3_layout=$(get_layout_param $dir3)
9110         local root_dir_layout=$(get_layout_param $MOUNT)
9111         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
9112         {
9113                 echo "dir3_layout: '$dir3_layout'"
9114                 echo "root_dir_layout: '$root_dir_layout'"
9115                 error "$dir3 should show the default layout from $MOUNT"
9116         }
9117
9118         # set OST pool on root directory
9119         local pool=$TESTNAME
9120         pool_add $pool || error "add $pool failed"
9121         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
9122                 error "add targets to $pool failed"
9123
9124         $LFS setstripe -p $pool $MOUNT ||
9125                 error "set OST pool on $MOUNT failed"
9126
9127         # new file created in $dir3 should inherit the pool from
9128         # the filesystem default
9129         local file3=$dir3/$tfile-3
9130         touch $file3 || error "touch $file3 failed"
9131
9132         local file3_pool=$($LFS getstripe -p $file3)
9133         [[ "$file3_pool" = "$pool" ]] ||
9134                 error "$file3 ('$file3_pool') didn't inherit OST pool '$pool'"
9135
9136         local dir4=$MOUNT/$tdir-4
9137         mkdir $dir4 || error "mkdir $dir4 failed"
9138         local dir4_layout=$(get_layout_param $dir4)
9139         root_dir_layout=$(get_layout_param $MOUNT)
9140         echo "$LFS getstripe -d $dir4"
9141         $LFS getstripe -d $dir4
9142         echo "$LFS getstripe -d $MOUNT"
9143         $LFS getstripe -d $MOUNT
9144         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
9145         {
9146                 echo "dir4_layout: '$dir4_layout'"
9147                 echo "root_dir_layout: '$root_dir_layout'"
9148                 error "$dir4 should show the default layout from $MOUNT"
9149         }
9150
9151         # new file created in $dir4 should inherit the pool from
9152         # the filesystem default
9153         local file4=$dir4/$tfile-4
9154         touch $file4 || error "touch $file4 failed"
9155
9156         local file4_pool=$($LFS getstripe -p $file4)
9157         [[ "$file4_pool" = "$pool" ]] ||
9158                 error "$file4 ('$file4_pool') didn't inherit OST pool $pool"
9159
9160         # new subdirectory under non-root directory should inherit
9161         # the default layout from its parent directory
9162         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
9163                 error "set directory layout on $dir4 failed"
9164
9165         local dir5=$dir4/$tdir-5
9166         mkdir $dir5 || error "mkdir $dir5 failed"
9167
9168         dir4_layout=$(get_layout_param $dir4)
9169         local dir5_layout=$(get_layout_param $dir5)
9170         [[ "$dir4_layout" = "$dir5_layout" ]] ||
9171         {
9172                 echo "dir4_layout: '$dir4_layout'"
9173                 echo "dir5_layout: '$dir5_layout'"
9174                 error "$dir5 should inherit the default layout from $dir4"
9175         }
9176
9177         # though subdir under ROOT doesn't inherit default layout, but
9178         # its sub dir/file should be created with default layout.
9179         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
9180         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
9181                 skip "Need MDS version at least 2.12.59"
9182
9183         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
9184         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
9185         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
9186
9187         if [ $default_lmv_hash == "none" ]; then
9188                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
9189         else
9190                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
9191                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
9192         fi
9193
9194         $LFS setdirstripe -D -c 2 $MOUNT ||
9195                 error "setdirstripe -D -c 2 failed"
9196         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
9197         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
9198         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
9199 }
9200 run_test 65n "don't inherit default layout from root for new subdirectories"
9201
9202 # bug 2543 - update blocks count on client
9203 test_66() {
9204         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9205
9206         COUNT=${COUNT:-8}
9207         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
9208         sync; sync_all_data; sync; sync_all_data
9209         cancel_lru_locks osc
9210         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
9211         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
9212 }
9213 run_test 66 "update inode blocks count on client ==============="
9214
9215 meminfo() {
9216         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
9217 }
9218
9219 swap_used() {
9220         swapon -s | awk '($1 == "'$1'") { print $4 }'
9221 }
9222
9223 # bug5265, obdfilter oa2dentry return -ENOENT
9224 # #define OBD_FAIL_SRV_ENOENT 0x217
9225 test_69() {
9226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9227         remote_ost_nodsh && skip "remote OST with nodsh"
9228
9229         f="$DIR/$tfile"
9230         $LFS setstripe -c 1 -i 0 $f
9231
9232         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
9233
9234         do_facet ost1 lctl set_param fail_loc=0x217
9235         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
9236         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
9237
9238         do_facet ost1 lctl set_param fail_loc=0
9239         $DIRECTIO write $f 0 2 || error "write error"
9240
9241         cancel_lru_locks osc
9242         $DIRECTIO read $f 0 1 || error "read error"
9243
9244         do_facet ost1 lctl set_param fail_loc=0x217
9245         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
9246
9247         do_facet ost1 lctl set_param fail_loc=0
9248         rm -f $f
9249 }
9250 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
9251
9252 test_71() {
9253         test_mkdir $DIR/$tdir
9254         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
9255         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
9256 }
9257 run_test 71 "Running dbench on lustre (don't segment fault) ===="
9258
9259 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
9260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9261         [ "$RUNAS_ID" = "$UID" ] &&
9262                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9263         # Check that testing environment is properly set up. Skip if not
9264         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
9265                 skip_env "User $RUNAS_ID does not exist - skipping"
9266
9267         touch $DIR/$tfile
9268         chmod 777 $DIR/$tfile
9269         chmod ug+s $DIR/$tfile
9270         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
9271                 error "$RUNAS dd $DIR/$tfile failed"
9272         # See if we are still setuid/sgid
9273         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9274                 error "S/gid is not dropped on write"
9275         # Now test that MDS is updated too
9276         cancel_lru_locks mdc
9277         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9278                 error "S/gid is not dropped on MDS"
9279         rm -f $DIR/$tfile
9280 }
9281 run_test 72a "Test that remove suid works properly (bug5695) ===="
9282
9283 test_72b() { # bug 24226 -- keep mode setting when size is not changing
9284         local perm
9285
9286         [ "$RUNAS_ID" = "$UID" ] &&
9287                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9288         [ "$RUNAS_ID" -eq 0 ] &&
9289                 skip_env "RUNAS_ID = 0 -- skipping"
9290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9291         # Check that testing environment is properly set up. Skip if not
9292         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
9293                 skip_env "User $RUNAS_ID does not exist - skipping"
9294
9295         touch $DIR/${tfile}-f{g,u}
9296         test_mkdir $DIR/${tfile}-dg
9297         test_mkdir $DIR/${tfile}-du
9298         chmod 770 $DIR/${tfile}-{f,d}{g,u}
9299         chmod g+s $DIR/${tfile}-{f,d}g
9300         chmod u+s $DIR/${tfile}-{f,d}u
9301         for perm in 777 2777 4777; do
9302                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
9303                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
9304                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
9305                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
9306         done
9307         true
9308 }
9309 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
9310
9311 # bug 3462 - multiple simultaneous MDC requests
9312 test_73() {
9313         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9314
9315         test_mkdir $DIR/d73-1
9316         test_mkdir $DIR/d73-2
9317         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
9318         pid1=$!
9319
9320         lctl set_param fail_loc=0x80000129
9321         $MULTIOP $DIR/d73-1/f73-2 Oc &
9322         sleep 1
9323         lctl set_param fail_loc=0
9324
9325         $MULTIOP $DIR/d73-2/f73-3 Oc &
9326         pid3=$!
9327
9328         kill -USR1 $pid1
9329         wait $pid1 || return 1
9330
9331         sleep 25
9332
9333         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
9334         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
9335         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
9336
9337         rm -rf $DIR/d73-*
9338 }
9339 run_test 73 "multiple MDC requests (should not deadlock)"
9340
9341 test_74a() { # bug 6149, 6184
9342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9343
9344         touch $DIR/f74a
9345         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9346         #
9347         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9348         # will spin in a tight reconnection loop
9349         $LCTL set_param fail_loc=0x8000030e
9350         # get any lock that won't be difficult - lookup works.
9351         ls $DIR/f74a
9352         $LCTL set_param fail_loc=0
9353         rm -f $DIR/f74a
9354         true
9355 }
9356 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
9357
9358 test_74b() { # bug 13310
9359         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9360
9361         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9362         #
9363         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9364         # will spin in a tight reconnection loop
9365         $LCTL set_param fail_loc=0x8000030e
9366         # get a "difficult" lock
9367         touch $DIR/f74b
9368         $LCTL set_param fail_loc=0
9369         rm -f $DIR/f74b
9370         true
9371 }
9372 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
9373
9374 test_74c() {
9375         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9376
9377         #define OBD_FAIL_LDLM_NEW_LOCK
9378         $LCTL set_param fail_loc=0x319
9379         touch $DIR/$tfile && error "touch successful"
9380         $LCTL set_param fail_loc=0
9381         true
9382 }
9383 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
9384
9385 slab_lic=/sys/kernel/slab/lustre_inode_cache
9386 num_objects() {
9387         [ -f $slab_lic/shrink ] && echo 1 > $slab_lic/shrink
9388         [ -f $slab_lic/objects ] && awk '{ print $1 }' $slab_lic/objects ||
9389                 awk '/lustre_inode_cache/ { print $2; exit }' /proc/slabinfo
9390 }
9391
9392 test_76a() { # Now for b=20433, added originally in b=1443
9393         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9394
9395         cancel_lru_locks osc
9396         # there may be some slab objects cached per core
9397         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
9398         local before=$(num_objects)
9399         local count=$((512 * cpus))
9400         [ "$SLOW" = "no" ] && count=$((128 * cpus))
9401         local margin=$((count / 10))
9402         if [[ -f $slab_lic/aliases ]]; then
9403                 local aliases=$(cat $slab_lic/aliases)
9404                 (( aliases > 0 )) && margin=$((margin * aliases))
9405         fi
9406
9407         echo "before slab objects: $before"
9408         for i in $(seq $count); do
9409                 touch $DIR/$tfile
9410                 rm -f $DIR/$tfile
9411         done
9412         cancel_lru_locks osc
9413         local after=$(num_objects)
9414         echo "created: $count, after slab objects: $after"
9415         # shared slab counts are not very accurate, allow significant margin
9416         # the main goal is that the cache growth is not permanently > $count
9417         while (( after > before + margin )); do
9418                 sleep 1
9419                 after=$(num_objects)
9420                 wait=$((wait + 1))
9421                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9422                 if (( wait > 60 )); then
9423                         error "inode slab grew from $before+$margin to $after"
9424                 fi
9425         done
9426 }
9427 run_test 76a "confirm clients recycle inodes properly ===="
9428
9429 test_76b() {
9430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9431         [ $CLIENT_VERSION -ge $(version_code 2.13.55) ] || skip "not supported"
9432
9433         local count=512
9434         local before=$(num_objects)
9435
9436         for i in $(seq $count); do
9437                 mkdir $DIR/$tdir
9438                 rmdir $DIR/$tdir
9439         done
9440
9441         local after=$(num_objects)
9442         local wait=0
9443
9444         while (( after > before )); do
9445                 sleep 1
9446                 after=$(num_objects)
9447                 wait=$((wait + 1))
9448                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9449                 if (( wait > 60 )); then
9450                         error "inode slab grew from $before to $after"
9451                 fi
9452         done
9453
9454         echo "slab objects before: $before, after: $after"
9455 }
9456 run_test 76b "confirm clients recycle directory inodes properly ===="
9457
9458 export ORIG_CSUM=""
9459 set_checksums()
9460 {
9461         # Note: in sptlrpc modes which enable its own bulk checksum, the
9462         # original crc32_le bulk checksum will be automatically disabled,
9463         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
9464         # will be checked by sptlrpc code against sptlrpc bulk checksum.
9465         # In this case set_checksums() will not be no-op, because sptlrpc
9466         # bulk checksum will be enabled all through the test.
9467
9468         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
9469         lctl set_param -n osc.*.checksums $1
9470         return 0
9471 }
9472
9473 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9474                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
9475 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9476                              tr -d [] | head -n1)}
9477 set_checksum_type()
9478 {
9479         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
9480         rc=$?
9481         log "set checksum type to $1, rc = $rc"
9482         return $rc
9483 }
9484
9485 get_osc_checksum_type()
9486 {
9487         # arugment 1: OST name, like OST0000
9488         ost=$1
9489         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
9490                         sed 's/.*\[\(.*\)\].*/\1/g')
9491         rc=$?
9492         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
9493         echo $checksum_type
9494 }
9495
9496 F77_TMP=$TMP/f77-temp
9497 F77SZ=8
9498 setup_f77() {
9499         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
9500                 error "error writing to $F77_TMP"
9501 }
9502
9503 test_77a() { # bug 10889
9504         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9505         $GSS && skip_env "could not run with gss"
9506
9507         [ ! -f $F77_TMP ] && setup_f77
9508         set_checksums 1
9509         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
9510         set_checksums 0
9511         rm -f $DIR/$tfile
9512 }
9513 run_test 77a "normal checksum read/write operation"
9514
9515 test_77b() { # bug 10889
9516         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9517         $GSS && skip_env "could not run with gss"
9518
9519         [ ! -f $F77_TMP ] && setup_f77
9520         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9521         $LCTL set_param fail_loc=0x80000409
9522         set_checksums 1
9523
9524         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9525                 error "dd error: $?"
9526         $LCTL set_param fail_loc=0
9527
9528         for algo in $CKSUM_TYPES; do
9529                 cancel_lru_locks osc
9530                 set_checksum_type $algo
9531                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9532                 $LCTL set_param fail_loc=0x80000408
9533                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
9534                 $LCTL set_param fail_loc=0
9535         done
9536         set_checksums 0
9537         set_checksum_type $ORIG_CSUM_TYPE
9538         rm -f $DIR/$tfile
9539 }
9540 run_test 77b "checksum error on client write, read"
9541
9542 cleanup_77c() {
9543         trap 0
9544         set_checksums 0
9545         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
9546         $check_ost &&
9547                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
9548         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
9549         $check_ost && [ -n "$ost_file_prefix" ] &&
9550                 do_facet ost1 rm -f ${ost_file_prefix}\*
9551 }
9552
9553 test_77c() {
9554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9555         $GSS && skip_env "could not run with gss"
9556         remote_ost_nodsh && skip "remote OST with nodsh"
9557
9558         local bad1
9559         local osc_file_prefix
9560         local osc_file
9561         local check_ost=false
9562         local ost_file_prefix
9563         local ost_file
9564         local orig_cksum
9565         local dump_cksum
9566         local fid
9567
9568         # ensure corruption will occur on first OSS/OST
9569         $LFS setstripe -i 0 $DIR/$tfile
9570
9571         [ ! -f $F77_TMP ] && setup_f77
9572         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9573                 error "dd write error: $?"
9574         fid=$($LFS path2fid $DIR/$tfile)
9575
9576         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
9577         then
9578                 check_ost=true
9579                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
9580                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
9581         else
9582                 echo "OSS do not support bulk pages dump upon error"
9583         fi
9584
9585         osc_file_prefix=$($LCTL get_param -n debug_path)
9586         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
9587
9588         trap cleanup_77c EXIT
9589
9590         set_checksums 1
9591         # enable bulk pages dump upon error on Client
9592         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
9593         # enable bulk pages dump upon error on OSS
9594         $check_ost &&
9595                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
9596
9597         # flush Client cache to allow next read to reach OSS
9598         cancel_lru_locks osc
9599
9600         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
9601         $LCTL set_param fail_loc=0x80000408
9602         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
9603         $LCTL set_param fail_loc=0
9604
9605         rm -f $DIR/$tfile
9606
9607         # check cksum dump on Client
9608         osc_file=$(ls ${osc_file_prefix}*)
9609         [ -n "$osc_file" ] || error "no checksum dump file on Client"
9610         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
9611         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
9612         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
9613         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
9614                      cksum)
9615         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
9616         [[ "$orig_cksum" == "$dump_cksum" ]] ||
9617                 error "dump content does not match on Client"
9618
9619         $check_ost || skip "No need to check cksum dump on OSS"
9620
9621         # check cksum dump on OSS
9622         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
9623         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
9624         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
9625         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
9626         [[ "$orig_cksum" == "$dump_cksum" ]] ||
9627                 error "dump content does not match on OSS"
9628
9629         cleanup_77c
9630 }
9631 run_test 77c "checksum error on client read with debug"
9632
9633 test_77d() { # bug 10889
9634         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9635         $GSS && skip_env "could not run with gss"
9636
9637         stack_trap "rm -f $DIR/$tfile"
9638         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9639         $LCTL set_param fail_loc=0x80000409
9640         set_checksums 1
9641         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
9642                 error "direct write: rc=$?"
9643         $LCTL set_param fail_loc=0
9644         set_checksums 0
9645
9646         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9647         $LCTL set_param fail_loc=0x80000408
9648         set_checksums 1
9649         cancel_lru_locks osc
9650         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
9651                 error "direct read: rc=$?"
9652         $LCTL set_param fail_loc=0
9653         set_checksums 0
9654 }
9655 run_test 77d "checksum error on OST direct write, read"
9656
9657 test_77f() { # bug 10889
9658         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9659         $GSS && skip_env "could not run with gss"
9660
9661         set_checksums 1
9662         stack_trap "rm -f $DIR/$tfile"
9663         for algo in $CKSUM_TYPES; do
9664                 cancel_lru_locks osc
9665                 set_checksum_type $algo
9666                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9667                 $LCTL set_param fail_loc=0x409
9668                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
9669                         error "direct write succeeded"
9670                 $LCTL set_param fail_loc=0
9671         done
9672         set_checksum_type $ORIG_CSUM_TYPE
9673         set_checksums 0
9674 }
9675 run_test 77f "repeat checksum error on write (expect error)"
9676
9677 test_77g() { # bug 10889
9678         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9679         $GSS && skip_env "could not run with gss"
9680         remote_ost_nodsh && skip "remote OST with nodsh"
9681
9682         [ ! -f $F77_TMP ] && setup_f77
9683
9684         local file=$DIR/$tfile
9685         stack_trap "rm -f $file" EXIT
9686
9687         $LFS setstripe -c 1 -i 0 $file
9688         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
9689         do_facet ost1 lctl set_param fail_loc=0x8000021a
9690         set_checksums 1
9691         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
9692                 error "write error: rc=$?"
9693         do_facet ost1 lctl set_param fail_loc=0
9694         set_checksums 0
9695
9696         cancel_lru_locks osc
9697         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
9698         do_facet ost1 lctl set_param fail_loc=0x8000021b
9699         set_checksums 1
9700         cmp $F77_TMP $file || error "file compare failed"
9701         do_facet ost1 lctl set_param fail_loc=0
9702         set_checksums 0
9703 }
9704 run_test 77g "checksum error on OST write, read"
9705
9706 test_77k() { # LU-10906
9707         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9708         $GSS && skip_env "could not run with gss"
9709
9710         local cksum_param="osc.$FSNAME*.checksums"
9711         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
9712         local checksum
9713         local i
9714
9715         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
9716         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM || true"
9717         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM"
9718
9719         for i in 0 1; do
9720                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
9721                         error "failed to set checksum=$i on MGS"
9722                 wait_update $HOSTNAME "$get_checksum" $i
9723                 #remount
9724                 echo "remount client, checksum should be $i"
9725                 remount_client $MOUNT || error "failed to remount client"
9726                 checksum=$(eval $get_checksum)
9727                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
9728         done
9729         # remove persistent param to avoid races with checksum mountopt below
9730         do_facet mgs $LCTL set_param -P -d $cksum_param ||
9731                 error "failed to delete checksum on MGS"
9732
9733         for opt in "checksum" "nochecksum"; do
9734                 #remount with mount option
9735                 echo "remount client with option $opt, checksum should be $i"
9736                 umount_client $MOUNT || error "failed to umount client"
9737                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
9738                         error "failed to mount client with option '$opt'"
9739                 checksum=$(eval $get_checksum)
9740                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
9741                 i=$((i - 1))
9742         done
9743
9744         remount_client $MOUNT || error "failed to remount client"
9745 }
9746 run_test 77k "enable/disable checksum correctly"
9747
9748 test_77l() {
9749         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9750         $GSS && skip_env "could not run with gss"
9751
9752         set_checksums 1
9753         stack_trap "set_checksums $ORIG_CSUM" EXIT
9754         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
9755
9756         set_checksum_type invalid && error "unexpected success of invalid checksum type"
9757
9758         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9759         for algo in $CKSUM_TYPES; do
9760                 set_checksum_type $algo || error "fail to set checksum type $algo"
9761                 osc_algo=$(get_osc_checksum_type OST0000)
9762                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
9763
9764                 # no locks, no reqs to let the connection idle
9765                 cancel_lru_locks osc
9766                 lru_resize_disable osc
9767                 wait_osc_import_state client ost1 IDLE
9768
9769                 # ensure ost1 is connected
9770                 stat $DIR/$tfile >/dev/null || error "can't stat"
9771                 wait_osc_import_state client ost1 FULL
9772
9773                 osc_algo=$(get_osc_checksum_type OST0000)
9774                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
9775         done
9776         return 0
9777 }
9778 run_test 77l "preferred checksum type is remembered after reconnected"
9779
9780 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
9781 rm -f $F77_TMP
9782 unset F77_TMP
9783
9784 test_77m() {
9785         (( $CLIENT_VERSION >= $(version_code 2.14.52) )) ||
9786                 skip "Need at least version 2.14.52"
9787         local param=checksum_speed
9788
9789         $LCTL get_param $param || error "reading $param failed"
9790
9791         csum_speeds=$($LCTL get_param -n $param)
9792
9793         [[ "$csum_speeds" =~ "adler32" && "$csum_speeds" =~ "crc32" ]] ||
9794                 error "known checksum types are missing"
9795 }
9796 run_test 77m "Verify checksum_speed is correctly read"
9797
9798 check_filefrag_77n() {
9799         local nr_ext=0
9800         local starts=()
9801         local ends=()
9802
9803         while read extidx a b start end rest; do
9804                 if [[ "${extidx}" =~ ^[0-9]+: ]]; then
9805                         nr_ext=$(( $nr_ext + 1 ))
9806                         starts+=( ${start%..} )
9807                         ends+=( ${end%:} )
9808                 fi
9809         done < <( filefrag -sv $1 )
9810
9811         [[ $nr_ext -eq 2 ]] && [[ "${starts[-1]}" == $(( ${ends[0]} + 1 )) ]] && return 0
9812         return 1
9813 }
9814
9815 test_77n() {
9816         [[ "$CKSUM_TYPES" =~ t10 ]] || skip "no T10 checksum support on osc"
9817
9818         touch $DIR/$tfile
9819         $TRUNCATE $DIR/$tfile 0
9820         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=0
9821         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=2
9822         check_filefrag_77n $DIR/$tfile ||
9823                 skip "$tfile blocks not contiguous around hole"
9824
9825         set_checksums 1
9826         stack_trap "set_checksums $ORIG_CSUM" EXIT
9827         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
9828         stack_trap "rm -f $DIR/$tfile"
9829
9830         for algo in $CKSUM_TYPES; do
9831                 if [[ "$algo" =~ ^t10 ]]; then
9832                         set_checksum_type $algo ||
9833                                 error "fail to set checksum type $algo"
9834                         dd if=$DIR/$tfile of=/dev/null bs=12k count=1 iflag=direct ||
9835                                 error "fail to read $tfile with $algo"
9836                 fi
9837         done
9838         rm -f $DIR/$tfile
9839         return 0
9840 }
9841 run_test 77n "Verify read from a hole inside contiguous blocks with T10PI"
9842
9843 test_77o() {
9844         (( $CLIENT_VERSION >= $(version_code 2.14.54) )) ||
9845                 skip "Need at least version 2.14.54"
9846         local ofd=obdfilter
9847         local mdt=mdt
9848
9849         # print OST checksum_type
9850         echo "$ofd.$FSNAME-*.checksum_type:"
9851         do_nodes $(comma_list $(osts_nodes)) \
9852                 $LCTL get_param -n $ofd.$FSNAME-*.checksum_type
9853
9854         # print MDT checksum_type
9855         echo "$mdt.$FSNAME-*.checksum_type:"
9856         do_nodes $(comma_list $(mdts_nodes)) \
9857                 $LCTL get_param -n $mdt.$FSNAME-*.checksum_type
9858
9859         local o_count=$(do_nodes $(comma_list $(osts_nodes)) \
9860                    $LCTL get_param -n $ofd.$FSNAME-*.checksum_type | wc -l)
9861
9862         (( $o_count == $OSTCOUNT )) ||
9863                 error "found $o_count checksums, not \$MDSCOUNT=$OSTCOUNT"
9864
9865         local m_count=$(do_nodes $(comma_list $(mdts_nodes)) \
9866                    $LCTL get_param -n $mdt.$FSNAME-*.checksum_type | wc -l)
9867
9868         (( $m_count == $MDSCOUNT )) ||
9869                 error "found $m_count checksums, not \$MDSCOUNT=$MDSCOUNT"
9870 }
9871 run_test 77o "Verify checksum_type for server (mdt and ofd(obdfilter))"
9872
9873 cleanup_test_78() {
9874         trap 0
9875         rm -f $DIR/$tfile
9876 }
9877
9878 test_78() { # bug 10901
9879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9880         remote_ost || skip_env "local OST"
9881
9882         NSEQ=5
9883         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
9884         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
9885         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
9886         echo "MemTotal: $MEMTOTAL"
9887
9888         # reserve 256MB of memory for the kernel and other running processes,
9889         # and then take 1/2 of the remaining memory for the read/write buffers.
9890         if [ $MEMTOTAL -gt 512 ] ;then
9891                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
9892         else
9893                 # for those poor memory-starved high-end clusters...
9894                 MEMTOTAL=$((MEMTOTAL / 2))
9895         fi
9896         echo "Mem to use for directio: $MEMTOTAL"
9897
9898         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
9899         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
9900         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
9901         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
9902                 head -n1)
9903         echo "Smallest OST: $SMALLESTOST"
9904         [[ $SMALLESTOST -lt 10240 ]] &&
9905                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
9906
9907         trap cleanup_test_78 EXIT
9908
9909         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
9910                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
9911
9912         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
9913         echo "File size: $F78SIZE"
9914         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
9915         for i in $(seq 1 $NSEQ); do
9916                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
9917                 echo directIO rdwr round $i of $NSEQ
9918                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
9919         done
9920
9921         cleanup_test_78
9922 }
9923 run_test 78 "handle large O_DIRECT writes correctly ============"
9924
9925 test_79() { # bug 12743
9926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9927
9928         wait_delete_completed
9929
9930         BKTOTAL=$(calc_osc_kbytes kbytestotal)
9931         BKFREE=$(calc_osc_kbytes kbytesfree)
9932         BKAVAIL=$(calc_osc_kbytes kbytesavail)
9933
9934         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
9935         DFTOTAL=`echo $STRING | cut -d, -f1`
9936         DFUSED=`echo $STRING  | cut -d, -f2`
9937         DFAVAIL=`echo $STRING | cut -d, -f3`
9938         DFFREE=$(($DFTOTAL - $DFUSED))
9939
9940         ALLOWANCE=$((64 * $OSTCOUNT))
9941
9942         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
9943            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
9944                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
9945         fi
9946         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
9947            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
9948                 error "df free($DFFREE) mismatch OST free($BKFREE)"
9949         fi
9950         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
9951            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
9952                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
9953         fi
9954 }
9955 run_test 79 "df report consistency check ======================="
9956
9957 test_80() { # bug 10718
9958         remote_ost_nodsh && skip "remote OST with nodsh"
9959         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9960
9961         # relax strong synchronous semantics for slow backends like ZFS
9962         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
9963                 local soc="obdfilter.*.sync_lock_cancel"
9964                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
9965
9966                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
9967                 if [ -z "$save" ]; then
9968                         soc="obdfilter.*.sync_on_lock_cancel"
9969                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
9970                 fi
9971
9972                 if [ "$save" != "never" ]; then
9973                         local hosts=$(comma_list $(osts_nodes))
9974
9975                         do_nodes $hosts $LCTL set_param $soc=never
9976                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
9977                 fi
9978         fi
9979
9980         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
9981         sync; sleep 1; sync
9982         local before=$(date +%s)
9983         cancel_lru_locks osc
9984         local after=$(date +%s)
9985         local diff=$((after - before))
9986         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
9987
9988         rm -f $DIR/$tfile
9989 }
9990 run_test 80 "Page eviction is equally fast at high offsets too"
9991
9992 test_81a() { # LU-456
9993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9994         remote_ost_nodsh && skip "remote OST with nodsh"
9995
9996         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
9997         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
9998         do_facet ost1 lctl set_param fail_loc=0x80000228
9999
10000         # write should trigger a retry and success
10001         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10002         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10003         RC=$?
10004         if [ $RC -ne 0 ] ; then
10005                 error "write should success, but failed for $RC"
10006         fi
10007 }
10008 run_test 81a "OST should retry write when get -ENOSPC ==============="
10009
10010 test_81b() { # LU-456
10011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10012         remote_ost_nodsh && skip "remote OST with nodsh"
10013
10014         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10015         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
10016         do_facet ost1 lctl set_param fail_loc=0x228
10017
10018         # write should retry several times and return -ENOSPC finally
10019         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10020         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10021         RC=$?
10022         ENOSPC=28
10023         if [ $RC -ne $ENOSPC ] ; then
10024                 error "dd should fail for -ENOSPC, but succeed."
10025         fi
10026 }
10027 run_test 81b "OST should return -ENOSPC when retry still fails ======="
10028
10029 test_99() {
10030         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
10031
10032         test_mkdir $DIR/$tdir.cvsroot
10033         chown $RUNAS_ID $DIR/$tdir.cvsroot
10034
10035         cd $TMP
10036         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
10037
10038         cd /etc/init.d
10039         # some versions of cvs import exit(1) when asked to import links or
10040         # files they can't read.  ignore those files.
10041         local toignore=$(find . -type l -printf '-I %f\n' -o \
10042                          ! -perm /4 -printf '-I %f\n')
10043         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
10044                 $tdir.reposname vtag rtag
10045
10046         cd $DIR
10047         test_mkdir $DIR/$tdir.reposname
10048         chown $RUNAS_ID $DIR/$tdir.reposname
10049         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
10050
10051         cd $DIR/$tdir.reposname
10052         $RUNAS touch foo99
10053         $RUNAS cvs add -m 'addmsg' foo99
10054         $RUNAS cvs update
10055         $RUNAS cvs commit -m 'nomsg' foo99
10056         rm -fr $DIR/$tdir.cvsroot
10057 }
10058 run_test 99 "cvs strange file/directory operations"
10059
10060 test_100() {
10061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10062         [[ "$NETTYPE" =~ tcp ]] ||
10063                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
10064         remote_ost_nodsh && skip "remote OST with nodsh"
10065         remote_mds_nodsh && skip "remote MDS with nodsh"
10066         remote_servers ||
10067                 skip "useless for local single node setup"
10068
10069         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
10070                 [ "$PROT" != "tcp" ] && continue
10071                 RPORT=$(echo $REMOTE | cut -d: -f2)
10072                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
10073
10074                 rc=0
10075                 LPORT=`echo $LOCAL | cut -d: -f2`
10076                 if [ $LPORT -ge 1024 ]; then
10077                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
10078                         netstat -tna
10079                         error_exit "local: $LPORT > 1024, remote: $RPORT"
10080                 fi
10081         done
10082         [ "$rc" = 0 ] || error_exit "privileged port not found" )
10083 }
10084 run_test 100 "check local port using privileged port ==========="
10085
10086 function get_named_value()
10087 {
10088     local tag=$1
10089
10090     grep -w "$tag" | sed "s/^$tag  *\([0-9]*\)  *.*/\1/"
10091 }
10092
10093 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
10094                    awk '/^max_cached_mb/ { print $2 }')
10095
10096 cleanup_101a() {
10097         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
10098         trap 0
10099 }
10100
10101 test_101a() {
10102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10103
10104         local s
10105         local discard
10106         local nreads=10000
10107         local cache_limit=32
10108
10109         $LCTL set_param -n osc.*-osc*.rpc_stats=0
10110         trap cleanup_101a EXIT
10111         $LCTL set_param -n llite.*.read_ahead_stats=0
10112         $LCTL set_param -n llite.*.max_cached_mb=$cache_limit
10113
10114         #
10115         # randomly read 10000 of 64K chunks from file 3x 32MB in size
10116         #
10117         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
10118         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
10119
10120         discard=0
10121         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
10122                    get_named_value 'read.but.discarded'); do
10123                         discard=$(($discard + $s))
10124         done
10125         cleanup_101a
10126
10127         $LCTL get_param osc.*-osc*.rpc_stats
10128         $LCTL get_param llite.*.read_ahead_stats
10129
10130         # Discard is generally zero, but sometimes a few random reads line up
10131         # and trigger larger readahead, which is wasted & leads to discards.
10132         if [[ $(($discard)) -gt $nreads ]]; then
10133                 error "too many ($discard) discarded pages"
10134         fi
10135         rm -f $DIR/$tfile || true
10136 }
10137 run_test 101a "check read-ahead for random reads"
10138
10139 setup_test101bc() {
10140         test_mkdir $DIR/$tdir
10141         local ssize=$1
10142         local FILE_LENGTH=$2
10143         STRIPE_OFFSET=0
10144
10145         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
10146
10147         local list=$(comma_list $(osts_nodes))
10148         set_osd_param $list '' read_cache_enable 0
10149         set_osd_param $list '' writethrough_cache_enable 0
10150
10151         trap cleanup_test101bc EXIT
10152         # prepare the read-ahead file
10153         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
10154
10155         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
10156                                 count=$FILE_SIZE_MB 2> /dev/null
10157
10158 }
10159
10160 cleanup_test101bc() {
10161         trap 0
10162         rm -rf $DIR/$tdir
10163         rm -f $DIR/$tfile
10164
10165         local list=$(comma_list $(osts_nodes))
10166         set_osd_param $list '' read_cache_enable 1
10167         set_osd_param $list '' writethrough_cache_enable 1
10168 }
10169
10170 calc_total() {
10171         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
10172 }
10173
10174 ra_check_101() {
10175         local READ_SIZE=$1
10176         local STRIPE_SIZE=$2
10177         local FILE_LENGTH=$3
10178         local RA_INC=1048576
10179         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
10180         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
10181                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
10182         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
10183                   get_named_value 'read.but.discarded' | calc_total)
10184         if [[ $DISCARD -gt $discard_limit ]]; then
10185                 $LCTL get_param llite.*.read_ahead_stats
10186                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
10187         else
10188                 echo "Read-ahead success for size ${READ_SIZE}"
10189         fi
10190 }
10191
10192 test_101b() {
10193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10194         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10195
10196         local STRIPE_SIZE=1048576
10197         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
10198
10199         if [ $SLOW == "yes" ]; then
10200                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
10201         else
10202                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
10203         fi
10204
10205         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
10206
10207         # prepare the read-ahead file
10208         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10209         cancel_lru_locks osc
10210         for BIDX in 2 4 8 16 32 64 128 256
10211         do
10212                 local BSIZE=$((BIDX*4096))
10213                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
10214                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
10215                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
10216                 $LCTL set_param -n llite.*.read_ahead_stats=0
10217                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
10218                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
10219                 cancel_lru_locks osc
10220                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
10221         done
10222         cleanup_test101bc
10223         true
10224 }
10225 run_test 101b "check stride-io mode read-ahead ================="
10226
10227 test_101c() {
10228         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10229
10230         local STRIPE_SIZE=1048576
10231         local FILE_LENGTH=$((STRIPE_SIZE*100))
10232         local nreads=10000
10233         local rsize=65536
10234         local osc_rpc_stats
10235
10236         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10237
10238         cancel_lru_locks osc
10239         $LCTL set_param osc.*.rpc_stats=0
10240         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
10241         $LCTL get_param osc.*.rpc_stats
10242         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
10243                 local stats=$($LCTL get_param -n $osc_rpc_stats)
10244                 local lines=$(echo "$stats" | awk 'END {print NR;}')
10245                 local size
10246
10247                 if [ $lines -le 20 ]; then
10248                         echo "continue debug"
10249                         continue
10250                 fi
10251                 for size in 1 2 4 8; do
10252                         local rpc=$(echo "$stats" |
10253                                     awk '($1 == "'$size':") {print $2; exit; }')
10254                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
10255                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
10256                 done
10257                 echo "$osc_rpc_stats check passed!"
10258         done
10259         cleanup_test101bc
10260         true
10261 }
10262 run_test 101c "check stripe_size aligned read-ahead"
10263
10264 test_101d() {
10265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10266
10267         local file=$DIR/$tfile
10268         local sz_MB=${FILESIZE_101d:-80}
10269         local ra_MB=${READAHEAD_MB:-40}
10270
10271         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
10272         [ $free_MB -lt $sz_MB ] &&
10273                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
10274
10275         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
10276         $LFS setstripe -c -1 $file || error "setstripe failed"
10277
10278         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
10279         echo Cancel LRU locks on lustre client to flush the client cache
10280         cancel_lru_locks osc
10281
10282         echo Disable read-ahead
10283         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10284         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10285         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb=$old_RA" EXIT
10286         $LCTL get_param -n llite.*.max_read_ahead_mb
10287
10288         echo "Reading the test file $file with read-ahead disabled"
10289         local sz_KB=$((sz_MB * 1024 / 4))
10290         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
10291         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
10292         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10293                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10294
10295         echo "Cancel LRU locks on lustre client to flush the client cache"
10296         cancel_lru_locks osc
10297         echo Enable read-ahead with ${ra_MB}MB
10298         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
10299
10300         echo "Reading the test file $file with read-ahead enabled"
10301         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10302                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10303
10304         echo "read-ahead disabled time read $raOFF"
10305         echo "read-ahead enabled time read $raON"
10306
10307         rm -f $file
10308         wait_delete_completed
10309
10310         # use awk for this check instead of bash because it handles decimals
10311         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
10312                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
10313 }
10314 run_test 101d "file read with and without read-ahead enabled"
10315
10316 test_101e() {
10317         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10318
10319         local file=$DIR/$tfile
10320         local size_KB=500  #KB
10321         local count=100
10322         local bsize=1024
10323
10324         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
10325         local need_KB=$((count * size_KB))
10326         [[ $free_KB -le $need_KB ]] &&
10327                 skip_env "Need free space $need_KB, have $free_KB"
10328
10329         echo "Creating $count ${size_KB}K test files"
10330         for ((i = 0; i < $count; i++)); do
10331                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
10332         done
10333
10334         echo "Cancel LRU locks on lustre client to flush the client cache"
10335         cancel_lru_locks $OSC
10336
10337         echo "Reset readahead stats"
10338         $LCTL set_param -n llite.*.read_ahead_stats=0
10339
10340         for ((i = 0; i < $count; i++)); do
10341                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
10342         done
10343
10344         $LCTL get_param llite.*.max_cached_mb
10345         $LCTL get_param llite.*.read_ahead_stats
10346         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10347                      get_named_value 'misses' | calc_total)
10348
10349         for ((i = 0; i < $count; i++)); do
10350                 rm -rf $file.$i 2>/dev/null
10351         done
10352
10353         #10000 means 20% reads are missing in readahead
10354         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
10355 }
10356 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
10357
10358 test_101f() {
10359         which iozone || skip_env "no iozone installed"
10360
10361         local old_debug=$($LCTL get_param debug)
10362         old_debug=${old_debug#*=}
10363         $LCTL set_param debug="reada mmap"
10364
10365         # create a test file
10366         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
10367
10368         echo Cancel LRU locks on lustre client to flush the client cache
10369         cancel_lru_locks osc
10370
10371         echo Reset readahead stats
10372         $LCTL set_param -n llite.*.read_ahead_stats=0
10373
10374         echo mmap read the file with small block size
10375         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
10376                 > /dev/null 2>&1
10377
10378         echo checking missing pages
10379         $LCTL get_param llite.*.read_ahead_stats
10380         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10381                         get_named_value 'misses' | calc_total)
10382
10383         $LCTL set_param debug="$old_debug"
10384         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
10385         rm -f $DIR/$tfile
10386 }
10387 run_test 101f "check mmap read performance"
10388
10389 test_101g_brw_size_test() {
10390         local mb=$1
10391         local pages=$((mb * 1048576 / PAGE_SIZE))
10392         local file=$DIR/$tfile
10393
10394         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
10395                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
10396         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
10397                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
10398                         return 2
10399         done
10400
10401         stack_trap "rm -f $file" EXIT
10402         $LCTL set_param -n osc.*.rpc_stats=0
10403
10404         # 10 RPCs should be enough for the test
10405         local count=10
10406         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
10407                 { error "dd write ${mb} MB blocks failed"; return 3; }
10408         cancel_lru_locks osc
10409         dd of=/dev/null if=$file bs=${mb}M count=$count ||
10410                 { error "dd write ${mb} MB blocks failed"; return 4; }
10411
10412         # calculate number of full-sized read and write RPCs
10413         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
10414                 sed -n '/pages per rpc/,/^$/p' |
10415                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
10416                 END { print reads,writes }'))
10417         # allow one extra full-sized read RPC for async readahead
10418         [[ ${rpcs[0]} == $count || ${rpcs[0]} == $((count + 1)) ]] ||
10419                 { error "${rpcs[0]} != $count read RPCs"; return 5; }
10420         [[ ${rpcs[1]} == $count ]] ||
10421                 { error "${rpcs[1]} != $count write RPCs"; return 6; }
10422 }
10423
10424 test_101g() {
10425         remote_ost_nodsh && skip "remote OST with nodsh"
10426
10427         local rpcs
10428         local osts=$(get_facets OST)
10429         local list=$(comma_list $(osts_nodes))
10430         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10431         local brw_size="obdfilter.*.brw_size"
10432
10433         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10434
10435         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
10436
10437         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
10438                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
10439                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
10440            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
10441                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
10442                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
10443
10444                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
10445                         suffix="M"
10446
10447                 if [[ $orig_mb -lt 16 ]]; then
10448                         save_lustre_params $osts "$brw_size" > $p
10449                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
10450                                 error "set 16MB RPC size failed"
10451
10452                         echo "remount client to enable new RPC size"
10453                         remount_client $MOUNT || error "remount_client failed"
10454                 fi
10455
10456                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
10457                 # should be able to set brw_size=12, but no rpc_stats for that
10458                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
10459         fi
10460
10461         test_101g_brw_size_test 4 || error "4MB RPC test failed"
10462
10463         if [[ $orig_mb -lt 16 ]]; then
10464                 restore_lustre_params < $p
10465                 remount_client $MOUNT || error "remount_client restore failed"
10466         fi
10467
10468         rm -f $p $DIR/$tfile
10469 }
10470 run_test 101g "Big bulk(4/16 MiB) readahead"
10471
10472 test_101h() {
10473         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10474
10475         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
10476                 error "dd 70M file failed"
10477         echo Cancel LRU locks on lustre client to flush the client cache
10478         cancel_lru_locks osc
10479
10480         echo "Reset readahead stats"
10481         $LCTL set_param -n llite.*.read_ahead_stats 0
10482
10483         echo "Read 10M of data but cross 64M bundary"
10484         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
10485         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10486                      get_named_value 'misses' | calc_total)
10487         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
10488         rm -f $p $DIR/$tfile
10489 }
10490 run_test 101h "Readahead should cover current read window"
10491
10492 test_101i() {
10493         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
10494                 error "dd 10M file failed"
10495
10496         local max_per_file_mb=$($LCTL get_param -n \
10497                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
10498         cancel_lru_locks osc
10499         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
10500         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
10501                 error "set max_read_ahead_per_file_mb to 1 failed"
10502
10503         echo "Reset readahead stats"
10504         $LCTL set_param llite.*.read_ahead_stats=0
10505
10506         dd if=$DIR/$tfile of=/dev/null bs=2M
10507
10508         $LCTL get_param llite.*.read_ahead_stats
10509         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10510                      awk '/misses/ { print $2 }')
10511         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
10512         rm -f $DIR/$tfile
10513 }
10514 run_test 101i "allow current readahead to exceed reservation"
10515
10516 test_101j() {
10517         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
10518                 error "setstripe $DIR/$tfile failed"
10519         local file_size=$((1048576 * 16))
10520         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10521         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
10522
10523         echo Disable read-ahead
10524         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10525
10526         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
10527         for blk in $PAGE_SIZE 1048576 $file_size; do
10528                 cancel_lru_locks osc
10529                 echo "Reset readahead stats"
10530                 $LCTL set_param -n llite.*.read_ahead_stats=0
10531                 local count=$(($file_size / $blk))
10532                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
10533                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10534                              get_named_value 'failed.to.fast.read' | calc_total)
10535                 $LCTL get_param -n llite.*.read_ahead_stats
10536                 [ $miss -eq $count ] || error "expected $count got $miss"
10537         done
10538
10539         rm -f $p $DIR/$tfile
10540 }
10541 run_test 101j "A complete read block should be submitted when no RA"
10542
10543 setup_test102() {
10544         test_mkdir $DIR/$tdir
10545         chown $RUNAS_ID $DIR/$tdir
10546         STRIPE_SIZE=65536
10547         STRIPE_OFFSET=1
10548         STRIPE_COUNT=$OSTCOUNT
10549         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
10550
10551         trap cleanup_test102 EXIT
10552         cd $DIR
10553         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
10554         cd $DIR/$tdir
10555         for num in 1 2 3 4; do
10556                 for count in $(seq 1 $STRIPE_COUNT); do
10557                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
10558                                 local size=`expr $STRIPE_SIZE \* $num`
10559                                 local file=file"$num-$idx-$count"
10560                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
10561                         done
10562                 done
10563         done
10564
10565         cd $DIR
10566         $1 tar cf $TMP/f102.tar $tdir --xattrs
10567 }
10568
10569 cleanup_test102() {
10570         trap 0
10571         rm -f $TMP/f102.tar
10572         rm -rf $DIR/d0.sanity/d102
10573 }
10574
10575 test_102a() {
10576         [ "$UID" != 0 ] && skip "must run as root"
10577         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
10578                 skip_env "must have user_xattr"
10579
10580         [ -z "$(which setfattr 2>/dev/null)" ] &&
10581                 skip_env "could not find setfattr"
10582
10583         local testfile=$DIR/$tfile
10584
10585         touch $testfile
10586         echo "set/get xattr..."
10587         setfattr -n trusted.name1 -v value1 $testfile ||
10588                 error "setfattr -n trusted.name1=value1 $testfile failed"
10589         getfattr -n trusted.name1 $testfile 2> /dev/null |
10590           grep "trusted.name1=.value1" ||
10591                 error "$testfile missing trusted.name1=value1"
10592
10593         setfattr -n user.author1 -v author1 $testfile ||
10594                 error "setfattr -n user.author1=author1 $testfile failed"
10595         getfattr -n user.author1 $testfile 2> /dev/null |
10596           grep "user.author1=.author1" ||
10597                 error "$testfile missing trusted.author1=author1"
10598
10599         echo "listxattr..."
10600         setfattr -n trusted.name2 -v value2 $testfile ||
10601                 error "$testfile unable to set trusted.name2"
10602         setfattr -n trusted.name3 -v value3 $testfile ||
10603                 error "$testfile unable to set trusted.name3"
10604         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
10605             grep "trusted.name" | wc -l) -eq 3 ] ||
10606                 error "$testfile missing 3 trusted.name xattrs"
10607
10608         setfattr -n user.author2 -v author2 $testfile ||
10609                 error "$testfile unable to set user.author2"
10610         setfattr -n user.author3 -v author3 $testfile ||
10611                 error "$testfile unable to set user.author3"
10612         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
10613             grep "user.author" | wc -l) -eq 3 ] ||
10614                 error "$testfile missing 3 user.author xattrs"
10615
10616         echo "remove xattr..."
10617         setfattr -x trusted.name1 $testfile ||
10618                 error "$testfile error deleting trusted.name1"
10619         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
10620                 error "$testfile did not delete trusted.name1 xattr"
10621
10622         setfattr -x user.author1 $testfile ||
10623                 error "$testfile error deleting user.author1"
10624         echo "set lustre special xattr ..."
10625         $LFS setstripe -c1 $testfile
10626         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
10627                 awk -F "=" '/trusted.lov/ { print $2 }' )
10628         setfattr -n "trusted.lov" -v $lovea $testfile ||
10629                 error "$testfile doesn't ignore setting trusted.lov again"
10630         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
10631                 error "$testfile allow setting invalid trusted.lov"
10632         rm -f $testfile
10633 }
10634 run_test 102a "user xattr test =================================="
10635
10636 check_102b_layout() {
10637         local layout="$*"
10638         local testfile=$DIR/$tfile
10639
10640         echo "test layout '$layout'"
10641         $LFS setstripe $layout $testfile || error "setstripe failed"
10642         $LFS getstripe -y $testfile
10643
10644         echo "get/set/list trusted.lov xattr ..." # b=10930
10645         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
10646         [[ "$value" =~ "trusted.lov" ]] ||
10647                 error "can't get trusted.lov from $testfile"
10648         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
10649                 error "getstripe failed"
10650
10651         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
10652
10653         value=$(cut -d= -f2 <<<$value)
10654         # LU-13168: truncated xattr should fail if short lov_user_md header
10655         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
10656                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
10657         for len in $lens; do
10658                 echo "setfattr $len $testfile.2"
10659                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
10660                         [ $len -lt 66 ] && error "short xattr len=$len worked"
10661         done
10662         local stripe_size=$($LFS getstripe -S $testfile.2)
10663         local stripe_count=$($LFS getstripe -c $testfile.2)
10664         [[ $stripe_size -eq 65536 ]] ||
10665                 error "stripe size $stripe_size != 65536"
10666         [[ $stripe_count -eq $stripe_count_orig ]] ||
10667                 error "stripe count $stripe_count != $stripe_count_orig"
10668         rm $testfile $testfile.2
10669 }
10670
10671 test_102b() {
10672         [ -z "$(which setfattr 2>/dev/null)" ] &&
10673                 skip_env "could not find setfattr"
10674         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10675
10676         # check plain layout
10677         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
10678
10679         # and also check composite layout
10680         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
10681
10682 }
10683 run_test 102b "getfattr/setfattr for trusted.lov EAs"
10684
10685 test_102c() {
10686         [ -z "$(which setfattr 2>/dev/null)" ] &&
10687                 skip_env "could not find setfattr"
10688         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10689
10690         # b10930: get/set/list lustre.lov xattr
10691         echo "get/set/list lustre.lov xattr ..."
10692         test_mkdir $DIR/$tdir
10693         chown $RUNAS_ID $DIR/$tdir
10694         local testfile=$DIR/$tdir/$tfile
10695         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
10696                 error "setstripe failed"
10697         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
10698                 error "getstripe failed"
10699         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
10700         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
10701
10702         local testfile2=${testfile}2
10703         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
10704                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
10705
10706         $RUNAS $MCREATE $testfile2
10707         $RUNAS setfattr -n lustre.lov -v $value $testfile2
10708         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
10709         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
10710         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
10711         [ $stripe_count -eq $STRIPECOUNT ] ||
10712                 error "stripe count $stripe_count != $STRIPECOUNT"
10713 }
10714 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
10715
10716 compare_stripe_info1() {
10717         local stripe_index_all_zero=true
10718
10719         for num in 1 2 3 4; do
10720                 for count in $(seq 1 $STRIPE_COUNT); do
10721                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
10722                                 local size=$((STRIPE_SIZE * num))
10723                                 local file=file"$num-$offset-$count"
10724                                 stripe_size=$($LFS getstripe -S $PWD/$file)
10725                                 [[ $stripe_size -ne $size ]] &&
10726                                     error "$file: size $stripe_size != $size"
10727                                 stripe_count=$($LFS getstripe -c $PWD/$file)
10728                                 # allow fewer stripes to be created, ORI-601
10729                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
10730                                     error "$file: count $stripe_count != $count"
10731                                 stripe_index=$($LFS getstripe -i $PWD/$file)
10732                                 [[ $stripe_index -ne 0 ]] &&
10733                                         stripe_index_all_zero=false
10734                         done
10735                 done
10736         done
10737         $stripe_index_all_zero &&
10738                 error "all files are being extracted starting from OST index 0"
10739         return 0
10740 }
10741
10742 have_xattrs_include() {
10743         tar --help | grep -q xattrs-include &&
10744                 echo --xattrs-include="lustre.*"
10745 }
10746
10747 test_102d() {
10748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10749         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10750
10751         XINC=$(have_xattrs_include)
10752         setup_test102
10753         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
10754         cd $DIR/$tdir/$tdir
10755         compare_stripe_info1
10756 }
10757 run_test 102d "tar restore stripe info from tarfile,not keep osts"
10758
10759 test_102f() {
10760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10761         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10762
10763         XINC=$(have_xattrs_include)
10764         setup_test102
10765         test_mkdir $DIR/$tdir.restore
10766         cd $DIR
10767         tar cf - --xattrs $tdir | tar xf - \
10768                 -C $DIR/$tdir.restore --xattrs $XINC
10769         cd $DIR/$tdir.restore/$tdir
10770         compare_stripe_info1
10771 }
10772 run_test 102f "tar copy files, not keep osts"
10773
10774 grow_xattr() {
10775         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
10776                 skip "must have user_xattr"
10777         [ -z "$(which setfattr 2>/dev/null)" ] &&
10778                 skip_env "could not find setfattr"
10779         [ -z "$(which getfattr 2>/dev/null)" ] &&
10780                 skip_env "could not find getfattr"
10781
10782         local xsize=${1:-1024}  # in bytes
10783         local file=$DIR/$tfile
10784         local value="$(generate_string $xsize)"
10785         local xbig=trusted.big
10786         local toobig=$2
10787
10788         touch $file
10789         log "save $xbig on $file"
10790         if [ -z "$toobig" ]
10791         then
10792                 setfattr -n $xbig -v $value $file ||
10793                         error "saving $xbig on $file failed"
10794         else
10795                 setfattr -n $xbig -v $value $file &&
10796                         error "saving $xbig on $file succeeded"
10797                 return 0
10798         fi
10799
10800         local orig=$(get_xattr_value $xbig $file)
10801         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
10802
10803         local xsml=trusted.sml
10804         log "save $xsml on $file"
10805         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
10806
10807         local new=$(get_xattr_value $xbig $file)
10808         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
10809
10810         log "grow $xsml on $file"
10811         setfattr -n $xsml -v "$value" $file ||
10812                 error "growing $xsml on $file failed"
10813
10814         new=$(get_xattr_value $xbig $file)
10815         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
10816         log "$xbig still valid after growing $xsml"
10817
10818         rm -f $file
10819 }
10820
10821 test_102h() { # bug 15777
10822         grow_xattr 1024
10823 }
10824 run_test 102h "grow xattr from inside inode to external block"
10825
10826 test_102ha() {
10827         large_xattr_enabled || skip_env "ea_inode feature disabled"
10828
10829         echo "setting xattr of max xattr size: $(max_xattr_size)"
10830         grow_xattr $(max_xattr_size)
10831
10832         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
10833         echo "This should fail:"
10834         grow_xattr $(($(max_xattr_size) + 10)) 1
10835 }
10836 run_test 102ha "grow xattr from inside inode to external inode"
10837
10838 test_102i() { # bug 17038
10839         [ -z "$(which getfattr 2>/dev/null)" ] &&
10840                 skip "could not find getfattr"
10841
10842         touch $DIR/$tfile
10843         ln -s $DIR/$tfile $DIR/${tfile}link
10844         getfattr -n trusted.lov $DIR/$tfile ||
10845                 error "lgetxattr on $DIR/$tfile failed"
10846         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
10847                 grep -i "no such attr" ||
10848                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
10849         rm -f $DIR/$tfile $DIR/${tfile}link
10850 }
10851 run_test 102i "lgetxattr test on symbolic link ============"
10852
10853 test_102j() {
10854         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10855         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10856
10857         XINC=$(have_xattrs_include)
10858         setup_test102 "$RUNAS"
10859         chown $RUNAS_ID $DIR/$tdir
10860         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
10861         cd $DIR/$tdir/$tdir
10862         compare_stripe_info1 "$RUNAS"
10863 }
10864 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
10865
10866 test_102k() {
10867         [ -z "$(which setfattr 2>/dev/null)" ] &&
10868                 skip "could not find setfattr"
10869
10870         touch $DIR/$tfile
10871         # b22187 just check that does not crash for regular file.
10872         setfattr -n trusted.lov $DIR/$tfile
10873         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
10874         local test_kdir=$DIR/$tdir
10875         test_mkdir $test_kdir
10876         local default_size=$($LFS getstripe -S $test_kdir)
10877         local default_count=$($LFS getstripe -c $test_kdir)
10878         local default_offset=$($LFS getstripe -i $test_kdir)
10879         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
10880                 error 'dir setstripe failed'
10881         setfattr -n trusted.lov $test_kdir
10882         local stripe_size=$($LFS getstripe -S $test_kdir)
10883         local stripe_count=$($LFS getstripe -c $test_kdir)
10884         local stripe_offset=$($LFS getstripe -i $test_kdir)
10885         [ $stripe_size -eq $default_size ] ||
10886                 error "stripe size $stripe_size != $default_size"
10887         [ $stripe_count -eq $default_count ] ||
10888                 error "stripe count $stripe_count != $default_count"
10889         [ $stripe_offset -eq $default_offset ] ||
10890                 error "stripe offset $stripe_offset != $default_offset"
10891         rm -rf $DIR/$tfile $test_kdir
10892 }
10893 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
10894
10895 test_102l() {
10896         [ -z "$(which getfattr 2>/dev/null)" ] &&
10897                 skip "could not find getfattr"
10898
10899         # LU-532 trusted. xattr is invisible to non-root
10900         local testfile=$DIR/$tfile
10901
10902         touch $testfile
10903
10904         echo "listxattr as user..."
10905         chown $RUNAS_ID $testfile
10906         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
10907             grep -q "trusted" &&
10908                 error "$testfile trusted xattrs are user visible"
10909
10910         return 0;
10911 }
10912 run_test 102l "listxattr size test =================================="
10913
10914 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
10915         local path=$DIR/$tfile
10916         touch $path
10917
10918         listxattr_size_check $path || error "listattr_size_check $path failed"
10919 }
10920 run_test 102m "Ensure listxattr fails on small bufffer ========"
10921
10922 cleanup_test102
10923
10924 getxattr() { # getxattr path name
10925         # Return the base64 encoding of the value of xattr name on path.
10926         local path=$1
10927         local name=$2
10928
10929         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
10930         # file: $path
10931         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
10932         #
10933         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
10934
10935         getfattr --absolute-names --encoding=base64 --name=$name $path |
10936                 awk -F= -v name=$name '$1 == name {
10937                         print substr($0, index($0, "=") + 1);
10938         }'
10939 }
10940
10941 test_102n() { # LU-4101 mdt: protect internal xattrs
10942         [ -z "$(which setfattr 2>/dev/null)" ] &&
10943                 skip "could not find setfattr"
10944         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
10945         then
10946                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
10947         fi
10948
10949         local file0=$DIR/$tfile.0
10950         local file1=$DIR/$tfile.1
10951         local xattr0=$TMP/$tfile.0
10952         local xattr1=$TMP/$tfile.1
10953         local namelist="lov lma lmv link fid version som hsm"
10954         local name
10955         local value
10956
10957         rm -rf $file0 $file1 $xattr0 $xattr1
10958         touch $file0 $file1
10959
10960         # Get 'before' xattrs of $file1.
10961         getfattr --absolute-names --dump --match=- $file1 > $xattr0
10962
10963         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
10964                 namelist+=" lfsck_namespace"
10965         for name in $namelist; do
10966                 # Try to copy xattr from $file0 to $file1.
10967                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
10968
10969                 setfattr --name=trusted.$name --value="$value" $file1 ||
10970                         error "setxattr 'trusted.$name' failed"
10971
10972                 # Try to set a garbage xattr.
10973                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
10974
10975                 if [[ x$name == "xlov" ]]; then
10976                         setfattr --name=trusted.lov --value="$value" $file1 &&
10977                         error "setxattr invalid 'trusted.lov' success"
10978                 else
10979                         setfattr --name=trusted.$name --value="$value" $file1 ||
10980                                 error "setxattr invalid 'trusted.$name' failed"
10981                 fi
10982
10983                 # Try to remove the xattr from $file1. We don't care if this
10984                 # appears to succeed or fail, we just don't want there to be
10985                 # any changes or crashes.
10986                 setfattr --remove=$trusted.$name $file1 2> /dev/null
10987         done
10988
10989         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
10990         then
10991                 name="lfsck_ns"
10992                 # Try to copy xattr from $file0 to $file1.
10993                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
10994
10995                 setfattr --name=trusted.$name --value="$value" $file1 ||
10996                         error "setxattr 'trusted.$name' failed"
10997
10998                 # Try to set a garbage xattr.
10999                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11000
11001                 setfattr --name=trusted.$name --value="$value" $file1 ||
11002                         error "setxattr 'trusted.$name' failed"
11003
11004                 # Try to remove the xattr from $file1. We don't care if this
11005                 # appears to succeed or fail, we just don't want there to be
11006                 # any changes or crashes.
11007                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11008         fi
11009
11010         # Get 'after' xattrs of file1.
11011         getfattr --absolute-names --dump --match=- $file1 > $xattr1
11012
11013         if ! diff $xattr0 $xattr1; then
11014                 error "before and after xattrs of '$file1' differ"
11015         fi
11016
11017         rm -rf $file0 $file1 $xattr0 $xattr1
11018
11019         return 0
11020 }
11021 run_test 102n "silently ignore setxattr on internal trusted xattrs"
11022
11023 test_102p() { # LU-4703 setxattr did not check ownership
11024         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
11025                 skip "MDS needs to be at least 2.5.56"
11026
11027         local testfile=$DIR/$tfile
11028
11029         touch $testfile
11030
11031         echo "setfacl as user..."
11032         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
11033         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
11034
11035         echo "setfattr as user..."
11036         setfacl -m "u:$RUNAS_ID:---" $testfile
11037         $RUNAS setfattr -x system.posix_acl_access $testfile
11038         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
11039 }
11040 run_test 102p "check setxattr(2) correctly fails without permission"
11041
11042 test_102q() {
11043         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
11044                 skip "MDS needs to be at least 2.6.92"
11045
11046         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
11047 }
11048 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
11049
11050 test_102r() {
11051         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
11052                 skip "MDS needs to be at least 2.6.93"
11053
11054         touch $DIR/$tfile || error "touch"
11055         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
11056         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
11057         rm $DIR/$tfile || error "rm"
11058
11059         #normal directory
11060         mkdir -p $DIR/$tdir || error "mkdir"
11061         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11062         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11063         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11064                 error "$testfile error deleting user.author1"
11065         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11066                 grep "user.$(basename $tdir)" &&
11067                 error "$tdir did not delete user.$(basename $tdir)"
11068         rmdir $DIR/$tdir || error "rmdir"
11069
11070         #striped directory
11071         test_mkdir $DIR/$tdir
11072         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11073         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11074         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11075                 error "$testfile error deleting user.author1"
11076         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11077                 grep "user.$(basename $tdir)" &&
11078                 error "$tdir did not delete user.$(basename $tdir)"
11079         rmdir $DIR/$tdir || error "rm striped dir"
11080 }
11081 run_test 102r "set EAs with empty values"
11082
11083 test_102s() {
11084         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11085                 skip "MDS needs to be at least 2.11.52"
11086
11087         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11088
11089         save_lustre_params client "llite.*.xattr_cache" > $save
11090
11091         for cache in 0 1; do
11092                 lctl set_param llite.*.xattr_cache=$cache
11093
11094                 rm -f $DIR/$tfile
11095                 touch $DIR/$tfile || error "touch"
11096                 for prefix in lustre security system trusted user; do
11097                         # Note getxattr() may fail with 'Operation not
11098                         # supported' or 'No such attribute' depending
11099                         # on prefix and cache.
11100                         getfattr -n $prefix.n102s $DIR/$tfile &&
11101                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
11102                 done
11103         done
11104
11105         restore_lustre_params < $save
11106 }
11107 run_test 102s "getting nonexistent xattrs should fail"
11108
11109 test_102t() {
11110         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11111                 skip "MDS needs to be at least 2.11.52"
11112
11113         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11114
11115         save_lustre_params client "llite.*.xattr_cache" > $save
11116
11117         for cache in 0 1; do
11118                 lctl set_param llite.*.xattr_cache=$cache
11119
11120                 for buf_size in 0 256; do
11121                         rm -f $DIR/$tfile
11122                         touch $DIR/$tfile || error "touch"
11123                         setfattr -n user.multiop $DIR/$tfile
11124                         $MULTIOP $DIR/$tfile oa$buf_size ||
11125                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
11126                 done
11127         done
11128
11129         restore_lustre_params < $save
11130 }
11131 run_test 102t "zero length xattr values handled correctly"
11132
11133 run_acl_subtest()
11134 {
11135     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
11136     return $?
11137 }
11138
11139 test_103a() {
11140         [ "$UID" != 0 ] && skip "must run as root"
11141         $GSS && skip_env "could not run under gss"
11142         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
11143                 skip_env "must have acl enabled"
11144         [ -z "$(which setfacl 2>/dev/null)" ] &&
11145                 skip_env "could not find setfacl"
11146         remote_mds_nodsh && skip "remote MDS with nodsh"
11147
11148         gpasswd -a daemon bin                           # LU-5641
11149         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
11150
11151         declare -a identity_old
11152
11153         for num in $(seq $MDSCOUNT); do
11154                 switch_identity $num true || identity_old[$num]=$?
11155         done
11156
11157         SAVE_UMASK=$(umask)
11158         umask 0022
11159         mkdir -p $DIR/$tdir
11160         cd $DIR/$tdir
11161
11162         echo "performing cp ..."
11163         run_acl_subtest cp || error "run_acl_subtest cp failed"
11164         echo "performing getfacl-noacl..."
11165         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
11166         echo "performing misc..."
11167         run_acl_subtest misc || error  "misc test failed"
11168         echo "performing permissions..."
11169         run_acl_subtest permissions || error "permissions failed"
11170         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
11171         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
11172                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
11173                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
11174         then
11175                 echo "performing permissions xattr..."
11176                 run_acl_subtest permissions_xattr ||
11177                         error "permissions_xattr failed"
11178         fi
11179         echo "performing setfacl..."
11180         run_acl_subtest setfacl || error  "setfacl test failed"
11181
11182         # inheritance test got from HP
11183         echo "performing inheritance..."
11184         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
11185         chmod +x make-tree || error "chmod +x failed"
11186         run_acl_subtest inheritance || error "inheritance test failed"
11187         rm -f make-tree
11188
11189         echo "LU-974 ignore umask when acl is enabled..."
11190         run_acl_subtest 974 || error "LU-974 umask test failed"
11191         if [ $MDSCOUNT -ge 2 ]; then
11192                 run_acl_subtest 974_remote ||
11193                         error "LU-974 umask test failed under remote dir"
11194         fi
11195
11196         echo "LU-2561 newly created file is same size as directory..."
11197         if [ "$mds1_FSTYPE" != "zfs" ]; then
11198                 run_acl_subtest 2561 || error "LU-2561 test failed"
11199         else
11200                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
11201         fi
11202
11203         run_acl_subtest 4924 || error "LU-4924 test failed"
11204
11205         cd $SAVE_PWD
11206         umask $SAVE_UMASK
11207
11208         for num in $(seq $MDSCOUNT); do
11209                 if [ "${identity_old[$num]}" = 1 ]; then
11210                         switch_identity $num false || identity_old[$num]=$?
11211                 fi
11212         done
11213 }
11214 run_test 103a "acl test"
11215
11216 test_103b() {
11217         declare -a pids
11218         local U
11219
11220         for U in {0..511}; do
11221                 {
11222                 local O=$(printf "%04o" $U)
11223
11224                 umask $(printf "%04o" $((511 ^ $O)))
11225                 $LFS setstripe -c 1 $DIR/$tfile.s$O
11226                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
11227
11228                 (( $S == ($O & 0666) )) ||
11229                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
11230
11231                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
11232                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
11233                 (( $S == ($O & 0666) )) ||
11234                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
11235
11236                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
11237                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
11238                 (( $S == ($O & 0666) )) ||
11239                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
11240                 rm -f $DIR/$tfile.[smp]$0
11241                 } &
11242                 local pid=$!
11243
11244                 # limit the concurrently running threads to 64. LU-11878
11245                 local idx=$((U % 64))
11246                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
11247                 pids[idx]=$pid
11248         done
11249         wait
11250 }
11251 run_test 103b "umask lfs setstripe"
11252
11253 test_103c() {
11254         mkdir -p $DIR/$tdir
11255         cp -rp $DIR/$tdir $DIR/$tdir.bak
11256
11257         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
11258                 error "$DIR/$tdir shouldn't contain default ACL"
11259         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
11260                 error "$DIR/$tdir.bak shouldn't contain default ACL"
11261         true
11262 }
11263 run_test 103c "'cp -rp' won't set empty acl"
11264
11265 test_103e() {
11266         local numacl
11267         local fileacl
11268         local saved_debug=$($LCTL get_param -n debug)
11269
11270         (( $MDS1_VERSION >= $(version_code 2.14.0) )) ||
11271                 skip "MDS needs to be at least 2.14.0"
11272
11273         large_xattr_enabled || skip_env "ea_inode feature disabled"
11274
11275         mkdir -p $DIR/$tdir
11276         # add big LOV EA to cause reply buffer overflow earlier
11277         $LFS setstripe -C 1000 $DIR/$tdir
11278         lctl set_param mdc.*-mdc*.stats=clear
11279
11280         $LCTL set_param debug=0
11281         stack_trap "$LCTL set_param debug=\"$saved_debug\"" EXIT
11282         stack_trap "$LCTL get_param mdc.*-mdc*.stats" EXIT
11283
11284         # add a large number of default ACLs (expect 8000+ for 2.13+)
11285         for U in {2..7000}; do
11286                 setfacl -d -m user:$U:rwx $DIR/$tdir ||
11287                         error "Able to add just $U default ACLs"
11288         done
11289         numacl=$(getfacl $DIR/$tdir |& grep -c "default:user")
11290         echo "$numacl default ACLs created"
11291
11292         stat $DIR/$tdir || error "Cannot stat directory"
11293         # check file creation
11294         touch $DIR/$tdir/$tfile ||
11295                 error "failed to create $tfile with $numacl default ACLs"
11296         stat $DIR/$tdir/$tfile  || error "Cannot stat file"
11297         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11298         echo "$fileacl ACLs were inherited"
11299         (( $fileacl == $numacl )) ||
11300                 error "Not all default ACLs were inherited: $numacl != $fileacl"
11301         # check that new ACLs creation adds new ACLs to inherited ACLs
11302         setfacl -m user:19000:rwx $DIR/$tdir/$tfile ||
11303                 error "Cannot set new ACL"
11304         numacl=$((numacl + 1))
11305         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11306         (( $fileacl == $numacl )) ||
11307                 error "failed to add new ACL: $fileacl != $numacl as expected"
11308         # adds more ACLs to a file to reach their maximum at 8000+
11309         numacl=0
11310         for U in {20000..25000}; do
11311                 setfacl -m user:$U:rwx $DIR/$tdir/$tfile || break
11312                 numacl=$((numacl + 1))
11313         done
11314         echo "Added $numacl more ACLs to the file"
11315         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11316         echo "Total $fileacl ACLs in file"
11317         stat $DIR/$tdir/$tfile > /dev/null || error "Cannot stat file"
11318         rm -f $DIR/$tdir/$tfile || error "Cannot remove file"
11319         rmdir $DIR/$tdir || error "Cannot remove directory"
11320 }
11321 run_test 103e "inheritance of big amount of default ACLs"
11322
11323 test_103f() {
11324         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
11325                 skip "MDS needs to be at least 2.14.51"
11326
11327         large_xattr_enabled || skip_env "ea_inode feature disabled"
11328
11329         # enable changelog to consume more internal MDD buffers
11330         changelog_register
11331
11332         mkdir -p $DIR/$tdir
11333         # add big LOV EA
11334         $LFS setstripe -C 1000 $DIR/$tdir
11335         setfacl -d -m user:$U:rwx $DIR/$tdir || error "Cannot add default ACLs"
11336         mkdir $DIR/$tdir/inherited || error "failed to create subdirectory"
11337         rmdir $DIR/$tdir/inherited || error "Cannot remove subdirectory"
11338         rmdir $DIR/$tdir || error "Cannot remove directory"
11339 }
11340 run_test 103f "changelog doesn't interfere with default ACLs buffers"
11341
11342 test_104a() {
11343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11344
11345         touch $DIR/$tfile
11346         lfs df || error "lfs df failed"
11347         lfs df -ih || error "lfs df -ih failed"
11348         lfs df -h $DIR || error "lfs df -h $DIR failed"
11349         lfs df -i $DIR || error "lfs df -i $DIR failed"
11350         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
11351         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
11352
11353         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
11354         lctl --device %$OSC deactivate
11355         lfs df || error "lfs df with deactivated OSC failed"
11356         lctl --device %$OSC activate
11357         # wait the osc back to normal
11358         wait_osc_import_ready client ost
11359
11360         lfs df || error "lfs df with reactivated OSC failed"
11361         rm -f $DIR/$tfile
11362 }
11363 run_test 104a "lfs df [-ih] [path] test ========================="
11364
11365 test_104b() {
11366         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11367         [ $RUNAS_ID -eq $UID ] &&
11368                 skip_env "RUNAS_ID = UID = $UID -- skipping"
11369
11370         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
11371                         grep "Permission denied" | wc -l)))
11372         if [ $denied_cnt -ne 0 ]; then
11373                 error "lfs check servers test failed"
11374         fi
11375 }
11376 run_test 104b "$RUNAS lfs check servers test ===================="
11377
11378 #
11379 # Verify $1 is within range of $2.
11380 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
11381 # $1 is <= 2% of $2. Else Fail.
11382 #
11383 value_in_range() {
11384         # Strip all units (M, G, T)
11385         actual=$(echo $1 | tr -d A-Z)
11386         expect=$(echo $2 | tr -d A-Z)
11387
11388         expect_lo=$(($expect * 98 / 100)) # 2% below
11389         expect_hi=$(($expect * 102 / 100)) # 2% above
11390
11391         # permit 2% drift above and below
11392         (( $actual >= $expect_lo && $actual <= $expect_hi ))
11393 }
11394
11395 test_104c() {
11396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11397         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
11398
11399         local ost_param="osd-zfs.$FSNAME-OST0000."
11400         local mdt_param="osd-zfs.$FSNAME-MDT0000."
11401         local ofacets=$(get_facets OST)
11402         local mfacets=$(get_facets MDS)
11403         local saved_ost_blocks=
11404         local saved_mdt_blocks=
11405
11406         echo "Before recordsize change"
11407         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
11408         df=($(df -h | grep "/mnt/lustre"$))
11409
11410         # For checking.
11411         echo "lfs output : ${lfs_df[*]}"
11412         echo "df  output : ${df[*]}"
11413
11414         for facet in ${ofacets//,/ }; do
11415                 if [ -z $saved_ost_blocks ]; then
11416                         saved_ost_blocks=$(do_facet $facet \
11417                                 lctl get_param -n $ost_param.blocksize)
11418                         echo "OST Blocksize: $saved_ost_blocks"
11419                 fi
11420                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11421                 do_facet $facet zfs set recordsize=32768 $ost
11422         done
11423
11424         # BS too small. Sufficient for functional testing.
11425         for facet in ${mfacets//,/ }; do
11426                 if [ -z $saved_mdt_blocks ]; then
11427                         saved_mdt_blocks=$(do_facet $facet \
11428                                 lctl get_param -n $mdt_param.blocksize)
11429                         echo "MDT Blocksize: $saved_mdt_blocks"
11430                 fi
11431                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11432                 do_facet $facet zfs set recordsize=32768 $mdt
11433         done
11434
11435         # Give new values chance to reflect change
11436         sleep 2
11437
11438         echo "After recordsize change"
11439         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
11440         df_after=($(df -h | grep "/mnt/lustre"$))
11441
11442         # For checking.
11443         echo "lfs output : ${lfs_df_after[*]}"
11444         echo "df  output : ${df_after[*]}"
11445
11446         # Verify lfs df
11447         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
11448                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
11449         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
11450                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
11451         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
11452                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
11453
11454         # Verify df
11455         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
11456                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
11457         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
11458                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
11459         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
11460                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
11461
11462         # Restore MDT recordize back to original
11463         for facet in ${mfacets//,/ }; do
11464                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11465                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
11466         done
11467
11468         # Restore OST recordize back to original
11469         for facet in ${ofacets//,/ }; do
11470                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11471                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
11472         done
11473
11474         return 0
11475 }
11476 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
11477
11478 test_105a() {
11479         # doesn't work on 2.4 kernels
11480         touch $DIR/$tfile
11481         if $(flock_is_enabled); then
11482                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
11483         else
11484                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
11485         fi
11486         rm -f $DIR/$tfile
11487 }
11488 run_test 105a "flock when mounted without -o flock test ========"
11489
11490 test_105b() {
11491         touch $DIR/$tfile
11492         if $(flock_is_enabled); then
11493                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
11494         else
11495                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
11496         fi
11497         rm -f $DIR/$tfile
11498 }
11499 run_test 105b "fcntl when mounted without -o flock test ========"
11500
11501 test_105c() {
11502         touch $DIR/$tfile
11503         if $(flock_is_enabled); then
11504                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
11505         else
11506                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
11507         fi
11508         rm -f $DIR/$tfile
11509 }
11510 run_test 105c "lockf when mounted without -o flock test"
11511
11512 test_105d() { # bug 15924
11513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11514
11515         test_mkdir $DIR/$tdir
11516         flock_is_enabled || skip_env "mount w/o flock enabled"
11517         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
11518         $LCTL set_param fail_loc=0x80000315
11519         flocks_test 2 $DIR/$tdir
11520 }
11521 run_test 105d "flock race (should not freeze) ========"
11522
11523 test_105e() { # bug 22660 && 22040
11524         flock_is_enabled || skip_env "mount w/o flock enabled"
11525
11526         touch $DIR/$tfile
11527         flocks_test 3 $DIR/$tfile
11528 }
11529 run_test 105e "Two conflicting flocks from same process"
11530
11531 test_106() { #bug 10921
11532         test_mkdir $DIR/$tdir
11533         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
11534         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
11535 }
11536 run_test 106 "attempt exec of dir followed by chown of that dir"
11537
11538 test_107() {
11539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11540
11541         CDIR=`pwd`
11542         local file=core
11543
11544         cd $DIR
11545         rm -f $file
11546
11547         local save_pattern=$(sysctl -n kernel.core_pattern)
11548         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
11549         sysctl -w kernel.core_pattern=$file
11550         sysctl -w kernel.core_uses_pid=0
11551
11552         ulimit -c unlimited
11553         sleep 60 &
11554         SLEEPPID=$!
11555
11556         sleep 1
11557
11558         kill -s 11 $SLEEPPID
11559         wait $SLEEPPID
11560         if [ -e $file ]; then
11561                 size=`stat -c%s $file`
11562                 [ $size -eq 0 ] && error "Fail to create core file $file"
11563         else
11564                 error "Fail to create core file $file"
11565         fi
11566         rm -f $file
11567         sysctl -w kernel.core_pattern=$save_pattern
11568         sysctl -w kernel.core_uses_pid=$save_uses_pid
11569         cd $CDIR
11570 }
11571 run_test 107 "Coredump on SIG"
11572
11573 test_110() {
11574         test_mkdir $DIR/$tdir
11575         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
11576         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
11577                 error "mkdir with 256 char should fail, but did not"
11578         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
11579                 error "create with 255 char failed"
11580         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
11581                 error "create with 256 char should fail, but did not"
11582
11583         ls -l $DIR/$tdir
11584         rm -rf $DIR/$tdir
11585 }
11586 run_test 110 "filename length checking"
11587
11588 #
11589 # Purpose: To verify dynamic thread (OSS) creation.
11590 #
11591 test_115() {
11592         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11593         remote_ost_nodsh && skip "remote OST with nodsh"
11594
11595         # Lustre does not stop service threads once they are started.
11596         # Reset number of running threads to default.
11597         stopall
11598         setupall
11599
11600         local OSTIO_pre
11601         local save_params="$TMP/sanity-$TESTNAME.parameters"
11602
11603         # Get ll_ost_io count before I/O
11604         OSTIO_pre=$(do_facet ost1 \
11605                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
11606         # Exit if lustre is not running (ll_ost_io not running).
11607         [ -z "$OSTIO_pre" ] && error "no OSS threads"
11608
11609         echo "Starting with $OSTIO_pre threads"
11610         local thread_max=$((OSTIO_pre * 2))
11611         local rpc_in_flight=$((thread_max * 2))
11612         # Number of I/O Process proposed to be started.
11613         local nfiles
11614         local facets=$(get_facets OST)
11615
11616         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
11617         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
11618
11619         # Set in_flight to $rpc_in_flight
11620         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
11621                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
11622         nfiles=${rpc_in_flight}
11623         # Set ost thread_max to $thread_max
11624         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
11625
11626         # 5 Minutes should be sufficient for max number of OSS
11627         # threads(thread_max) to be created.
11628         local timeout=300
11629
11630         # Start I/O.
11631         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
11632         test_mkdir $DIR/$tdir
11633         for i in $(seq $nfiles); do
11634                 local file=$DIR/$tdir/${tfile}-$i
11635                 $LFS setstripe -c -1 -i 0 $file
11636                 ($WTL $file $timeout)&
11637         done
11638
11639         # I/O Started - Wait for thread_started to reach thread_max or report
11640         # error if thread_started is more than thread_max.
11641         echo "Waiting for thread_started to reach thread_max"
11642         local thread_started=0
11643         local end_time=$((SECONDS + timeout))
11644
11645         while [ $SECONDS -le $end_time ] ; do
11646                 echo -n "."
11647                 # Get ost i/o thread_started count.
11648                 thread_started=$(do_facet ost1 \
11649                         "$LCTL get_param \
11650                         ost.OSS.ost_io.threads_started | cut -d= -f2")
11651                 # Break out if thread_started is equal/greater than thread_max
11652                 if [[ $thread_started -ge $thread_max ]]; then
11653                         echo ll_ost_io thread_started $thread_started, \
11654                                 equal/greater than thread_max $thread_max
11655                         break
11656                 fi
11657                 sleep 1
11658         done
11659
11660         # Cleanup - We have the numbers, Kill i/o jobs if running.
11661         jobcount=($(jobs -p))
11662         for i in $(seq 0 $((${#jobcount[@]}-1)))
11663         do
11664                 kill -9 ${jobcount[$i]}
11665                 if [ $? -ne 0 ] ; then
11666                         echo Warning: \
11667                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
11668                 fi
11669         done
11670
11671         # Cleanup files left by WTL binary.
11672         for i in $(seq $nfiles); do
11673                 local file=$DIR/$tdir/${tfile}-$i
11674                 rm -rf $file
11675                 if [ $? -ne 0 ] ; then
11676                         echo "Warning: Failed to delete file $file"
11677                 fi
11678         done
11679
11680         restore_lustre_params <$save_params
11681         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
11682
11683         # Error out if no new thread has started or Thread started is greater
11684         # than thread max.
11685         if [[ $thread_started -le $OSTIO_pre ||
11686                         $thread_started -gt $thread_max ]]; then
11687                 error "ll_ost_io: thread_started $thread_started" \
11688                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
11689                       "No new thread started or thread started greater " \
11690                       "than thread_max."
11691         fi
11692 }
11693 run_test 115 "verify dynamic thread creation===================="
11694
11695 free_min_max () {
11696         wait_delete_completed
11697         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
11698         echo "OST kbytes available: ${AVAIL[@]}"
11699         MAXV=${AVAIL[0]}
11700         MAXI=0
11701         MINV=${AVAIL[0]}
11702         MINI=0
11703         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
11704                 #echo OST $i: ${AVAIL[i]}kb
11705                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
11706                         MAXV=${AVAIL[i]}
11707                         MAXI=$i
11708                 fi
11709                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
11710                         MINV=${AVAIL[i]}
11711                         MINI=$i
11712                 fi
11713         done
11714         echo "Min free space: OST $MINI: $MINV"
11715         echo "Max free space: OST $MAXI: $MAXV"
11716 }
11717
11718 test_116a() { # was previously test_116()
11719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11720         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11721         remote_mds_nodsh && skip "remote MDS with nodsh"
11722
11723         echo -n "Free space priority "
11724         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
11725                 head -n1
11726         declare -a AVAIL
11727         free_min_max
11728
11729         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
11730         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
11731         trap simple_cleanup_common EXIT
11732
11733         # Check if we need to generate uneven OSTs
11734         test_mkdir -p $DIR/$tdir/OST${MINI}
11735         local FILL=$((MINV / 4))
11736         local DIFF=$((MAXV - MINV))
11737         local DIFF2=$((DIFF * 100 / MINV))
11738
11739         local threshold=$(do_facet $SINGLEMDS \
11740                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
11741         threshold=${threshold%%%}
11742         echo -n "Check for uneven OSTs: "
11743         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
11744
11745         if [[ $DIFF2 -gt $threshold ]]; then
11746                 echo "ok"
11747                 echo "Don't need to fill OST$MINI"
11748         else
11749                 # generate uneven OSTs. Write 2% over the QOS threshold value
11750                 echo "no"
11751                 DIFF=$((threshold - DIFF2 + 2))
11752                 DIFF2=$((MINV * DIFF / 100))
11753                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
11754                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
11755                         error "setstripe failed"
11756                 DIFF=$((DIFF2 / 2048))
11757                 i=0
11758                 while [ $i -lt $DIFF ]; do
11759                         i=$((i + 1))
11760                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
11761                                 bs=2M count=1 2>/dev/null
11762                         echo -n .
11763                 done
11764                 echo .
11765                 sync
11766                 sleep_maxage
11767                 free_min_max
11768         fi
11769
11770         DIFF=$((MAXV - MINV))
11771         DIFF2=$((DIFF * 100 / MINV))
11772         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
11773         if [ $DIFF2 -gt $threshold ]; then
11774                 echo "ok"
11775         else
11776                 echo "failed - QOS mode won't be used"
11777                 simple_cleanup_common
11778                 skip "QOS imbalance criteria not met"
11779         fi
11780
11781         MINI1=$MINI
11782         MINV1=$MINV
11783         MAXI1=$MAXI
11784         MAXV1=$MAXV
11785
11786         # now fill using QOS
11787         $LFS setstripe -c 1 $DIR/$tdir
11788         FILL=$((FILL / 200))
11789         if [ $FILL -gt 600 ]; then
11790                 FILL=600
11791         fi
11792         echo "writing $FILL files to QOS-assigned OSTs"
11793         i=0
11794         while [ $i -lt $FILL ]; do
11795                 i=$((i + 1))
11796                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
11797                         count=1 2>/dev/null
11798                 echo -n .
11799         done
11800         echo "wrote $i 200k files"
11801         sync
11802         sleep_maxage
11803
11804         echo "Note: free space may not be updated, so measurements might be off"
11805         free_min_max
11806         DIFF2=$((MAXV - MINV))
11807         echo "free space delta: orig $DIFF final $DIFF2"
11808         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
11809         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
11810         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
11811         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
11812         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
11813         if [[ $DIFF -gt 0 ]]; then
11814                 FILL=$((DIFF2 * 100 / DIFF - 100))
11815                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
11816         fi
11817
11818         # Figure out which files were written where
11819         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
11820                awk '/'$MINI1': / {print $2; exit}')
11821         echo $UUID
11822         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
11823         echo "$MINC files created on smaller OST $MINI1"
11824         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
11825                awk '/'$MAXI1': / {print $2; exit}')
11826         echo $UUID
11827         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
11828         echo "$MAXC files created on larger OST $MAXI1"
11829         if [[ $MINC -gt 0 ]]; then
11830                 FILL=$((MAXC * 100 / MINC - 100))
11831                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
11832         fi
11833         [[ $MAXC -gt $MINC ]] ||
11834                 error_ignore LU-9 "stripe QOS didn't balance free space"
11835         simple_cleanup_common
11836 }
11837 run_test 116a "stripe QOS: free space balance ==================="
11838
11839 test_116b() { # LU-2093
11840         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11841         remote_mds_nodsh && skip "remote MDS with nodsh"
11842
11843 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
11844         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
11845                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
11846         [ -z "$old_rr" ] && skip "no QOS"
11847         do_facet $SINGLEMDS lctl set_param \
11848                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
11849         mkdir -p $DIR/$tdir
11850         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
11851         createmany -o $DIR/$tdir/f- 20 || error "can't create"
11852         do_facet $SINGLEMDS lctl set_param fail_loc=0
11853         rm -rf $DIR/$tdir
11854         do_facet $SINGLEMDS lctl set_param \
11855                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
11856 }
11857 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
11858
11859 test_117() # bug 10891
11860 {
11861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11862
11863         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
11864         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
11865         lctl set_param fail_loc=0x21e
11866         > $DIR/$tfile || error "truncate failed"
11867         lctl set_param fail_loc=0
11868         echo "Truncate succeeded."
11869         rm -f $DIR/$tfile
11870 }
11871 run_test 117 "verify osd extend =========="
11872
11873 NO_SLOW_RESENDCOUNT=4
11874 export OLD_RESENDCOUNT=""
11875 set_resend_count () {
11876         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
11877         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
11878         lctl set_param -n $PROC_RESENDCOUNT $1
11879         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
11880 }
11881
11882 # for reduce test_118* time (b=14842)
11883 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
11884
11885 # Reset async IO behavior after error case
11886 reset_async() {
11887         FILE=$DIR/reset_async
11888
11889         # Ensure all OSCs are cleared
11890         $LFS setstripe -c -1 $FILE
11891         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
11892         sync
11893         rm $FILE
11894 }
11895
11896 test_118a() #bug 11710
11897 {
11898         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11899
11900         reset_async
11901
11902         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11903         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11904         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11905
11906         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11907                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11908                 return 1;
11909         fi
11910         rm -f $DIR/$tfile
11911 }
11912 run_test 118a "verify O_SYNC works =========="
11913
11914 test_118b()
11915 {
11916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11917         remote_ost_nodsh && skip "remote OST with nodsh"
11918
11919         reset_async
11920
11921         #define OBD_FAIL_SRV_ENOENT 0x217
11922         set_nodes_failloc "$(osts_nodes)" 0x217
11923         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11924         RC=$?
11925         set_nodes_failloc "$(osts_nodes)" 0
11926         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11927         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11928                     grep -c writeback)
11929
11930         if [[ $RC -eq 0 ]]; then
11931                 error "Must return error due to dropped pages, rc=$RC"
11932                 return 1;
11933         fi
11934
11935         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11936                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11937                 return 1;
11938         fi
11939
11940         echo "Dirty pages not leaked on ENOENT"
11941
11942         # Due to the above error the OSC will issue all RPCs syncronously
11943         # until a subsequent RPC completes successfully without error.
11944         $MULTIOP $DIR/$tfile Ow4096yc
11945         rm -f $DIR/$tfile
11946
11947         return 0
11948 }
11949 run_test 118b "Reclaim dirty pages on fatal error =========="
11950
11951 test_118c()
11952 {
11953         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11954
11955         # for 118c, restore the original resend count, LU-1940
11956         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
11957                                 set_resend_count $OLD_RESENDCOUNT
11958         remote_ost_nodsh && skip "remote OST with nodsh"
11959
11960         reset_async
11961
11962         #define OBD_FAIL_OST_EROFS               0x216
11963         set_nodes_failloc "$(osts_nodes)" 0x216
11964
11965         # multiop should block due to fsync until pages are written
11966         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
11967         MULTIPID=$!
11968         sleep 1
11969
11970         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
11971                 error "Multiop failed to block on fsync, pid=$MULTIPID"
11972         fi
11973
11974         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11975                     grep -c writeback)
11976         if [[ $WRITEBACK -eq 0 ]]; then
11977                 error "No page in writeback, writeback=$WRITEBACK"
11978         fi
11979
11980         set_nodes_failloc "$(osts_nodes)" 0
11981         wait $MULTIPID
11982         RC=$?
11983         if [[ $RC -ne 0 ]]; then
11984                 error "Multiop fsync failed, rc=$RC"
11985         fi
11986
11987         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11988         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11989                     grep -c writeback)
11990         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11991                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11992         fi
11993
11994         rm -f $DIR/$tfile
11995         echo "Dirty pages flushed via fsync on EROFS"
11996         return 0
11997 }
11998 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
11999
12000 # continue to use small resend count to reduce test_118* time (b=14842)
12001 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12002
12003 test_118d()
12004 {
12005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12006         remote_ost_nodsh && skip "remote OST with nodsh"
12007
12008         reset_async
12009
12010         #define OBD_FAIL_OST_BRW_PAUSE_BULK
12011         set_nodes_failloc "$(osts_nodes)" 0x214
12012         # multiop should block due to fsync until pages are written
12013         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12014         MULTIPID=$!
12015         sleep 1
12016
12017         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12018                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12019         fi
12020
12021         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12022                     grep -c writeback)
12023         if [[ $WRITEBACK -eq 0 ]]; then
12024                 error "No page in writeback, writeback=$WRITEBACK"
12025         fi
12026
12027         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
12028         set_nodes_failloc "$(osts_nodes)" 0
12029
12030         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12031         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12032                     grep -c writeback)
12033         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12034                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12035         fi
12036
12037         rm -f $DIR/$tfile
12038         echo "Dirty pages gaurenteed flushed via fsync"
12039         return 0
12040 }
12041 run_test 118d "Fsync validation inject a delay of the bulk =========="
12042
12043 test_118f() {
12044         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12045
12046         reset_async
12047
12048         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
12049         lctl set_param fail_loc=0x8000040a
12050
12051         # Should simulate EINVAL error which is fatal
12052         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12053         RC=$?
12054         if [[ $RC -eq 0 ]]; then
12055                 error "Must return error due to dropped pages, rc=$RC"
12056         fi
12057
12058         lctl set_param fail_loc=0x0
12059
12060         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12061         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12062         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12063                     grep -c writeback)
12064         if [[ $LOCKED -ne 0 ]]; then
12065                 error "Locked pages remain in cache, locked=$LOCKED"
12066         fi
12067
12068         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12069                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12070         fi
12071
12072         rm -f $DIR/$tfile
12073         echo "No pages locked after fsync"
12074
12075         reset_async
12076         return 0
12077 }
12078 run_test 118f "Simulate unrecoverable OSC side error =========="
12079
12080 test_118g() {
12081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12082
12083         reset_async
12084
12085         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
12086         lctl set_param fail_loc=0x406
12087
12088         # simulate local -ENOMEM
12089         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12090         RC=$?
12091
12092         lctl set_param fail_loc=0
12093         if [[ $RC -eq 0 ]]; then
12094                 error "Must return error due to dropped pages, rc=$RC"
12095         fi
12096
12097         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12098         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12099         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12100                         grep -c writeback)
12101         if [[ $LOCKED -ne 0 ]]; then
12102                 error "Locked pages remain in cache, locked=$LOCKED"
12103         fi
12104
12105         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12106                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12107         fi
12108
12109         rm -f $DIR/$tfile
12110         echo "No pages locked after fsync"
12111
12112         reset_async
12113         return 0
12114 }
12115 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
12116
12117 test_118h() {
12118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12119         remote_ost_nodsh && skip "remote OST with nodsh"
12120
12121         reset_async
12122
12123         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12124         set_nodes_failloc "$(osts_nodes)" 0x20e
12125         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12126         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12127         RC=$?
12128
12129         set_nodes_failloc "$(osts_nodes)" 0
12130         if [[ $RC -eq 0 ]]; then
12131                 error "Must return error due to dropped pages, rc=$RC"
12132         fi
12133
12134         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12135         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12136         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12137                     grep -c writeback)
12138         if [[ $LOCKED -ne 0 ]]; then
12139                 error "Locked pages remain in cache, locked=$LOCKED"
12140         fi
12141
12142         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12143                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12144         fi
12145
12146         rm -f $DIR/$tfile
12147         echo "No pages locked after fsync"
12148
12149         return 0
12150 }
12151 run_test 118h "Verify timeout in handling recoverables errors  =========="
12152
12153 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12154
12155 test_118i() {
12156         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12157         remote_ost_nodsh && skip "remote OST with nodsh"
12158
12159         reset_async
12160
12161         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12162         set_nodes_failloc "$(osts_nodes)" 0x20e
12163
12164         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12165         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12166         PID=$!
12167         sleep 5
12168         set_nodes_failloc "$(osts_nodes)" 0
12169
12170         wait $PID
12171         RC=$?
12172         if [[ $RC -ne 0 ]]; then
12173                 error "got error, but should be not, rc=$RC"
12174         fi
12175
12176         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12177         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12178         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12179         if [[ $LOCKED -ne 0 ]]; then
12180                 error "Locked pages remain in cache, locked=$LOCKED"
12181         fi
12182
12183         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12184                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12185         fi
12186
12187         rm -f $DIR/$tfile
12188         echo "No pages locked after fsync"
12189
12190         return 0
12191 }
12192 run_test 118i "Fix error before timeout in recoverable error  =========="
12193
12194 [ "$SLOW" = "no" ] && set_resend_count 4
12195
12196 test_118j() {
12197         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12198         remote_ost_nodsh && skip "remote OST with nodsh"
12199
12200         reset_async
12201
12202         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
12203         set_nodes_failloc "$(osts_nodes)" 0x220
12204
12205         # return -EIO from OST
12206         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12207         RC=$?
12208         set_nodes_failloc "$(osts_nodes)" 0x0
12209         if [[ $RC -eq 0 ]]; then
12210                 error "Must return error due to dropped pages, rc=$RC"
12211         fi
12212
12213         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12214         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12215         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12216         if [[ $LOCKED -ne 0 ]]; then
12217                 error "Locked pages remain in cache, locked=$LOCKED"
12218         fi
12219
12220         # in recoverable error on OST we want resend and stay until it finished
12221         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12222                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12223         fi
12224
12225         rm -f $DIR/$tfile
12226         echo "No pages locked after fsync"
12227
12228         return 0
12229 }
12230 run_test 118j "Simulate unrecoverable OST side error =========="
12231
12232 test_118k()
12233 {
12234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12235         remote_ost_nodsh && skip "remote OSTs with nodsh"
12236
12237         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12238         set_nodes_failloc "$(osts_nodes)" 0x20e
12239         test_mkdir $DIR/$tdir
12240
12241         for ((i=0;i<10;i++)); do
12242                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
12243                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
12244                 SLEEPPID=$!
12245                 sleep 0.500s
12246                 kill $SLEEPPID
12247                 wait $SLEEPPID
12248         done
12249
12250         set_nodes_failloc "$(osts_nodes)" 0
12251         rm -rf $DIR/$tdir
12252 }
12253 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
12254
12255 test_118l() # LU-646
12256 {
12257         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12258
12259         test_mkdir $DIR/$tdir
12260         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
12261         rm -rf $DIR/$tdir
12262 }
12263 run_test 118l "fsync dir"
12264
12265 test_118m() # LU-3066
12266 {
12267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12268
12269         test_mkdir $DIR/$tdir
12270         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
12271         rm -rf $DIR/$tdir
12272 }
12273 run_test 118m "fdatasync dir ========="
12274
12275 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12276
12277 test_118n()
12278 {
12279         local begin
12280         local end
12281
12282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12283         remote_ost_nodsh && skip "remote OSTs with nodsh"
12284
12285         # Sleep to avoid a cached response.
12286         #define OBD_STATFS_CACHE_SECONDS 1
12287         sleep 2
12288
12289         # Inject a 10 second delay in the OST_STATFS handler.
12290         #define OBD_FAIL_OST_STATFS_DELAY 0x242
12291         set_nodes_failloc "$(osts_nodes)" 0x242
12292
12293         begin=$SECONDS
12294         stat --file-system $MOUNT > /dev/null
12295         end=$SECONDS
12296
12297         set_nodes_failloc "$(osts_nodes)" 0
12298
12299         if ((end - begin > 20)); then
12300             error "statfs took $((end - begin)) seconds, expected 10"
12301         fi
12302 }
12303 run_test 118n "statfs() sends OST_STATFS requests in parallel"
12304
12305 test_119a() # bug 11737
12306 {
12307         BSIZE=$((512 * 1024))
12308         directio write $DIR/$tfile 0 1 $BSIZE
12309         # We ask to read two blocks, which is more than a file size.
12310         # directio will indicate an error when requested and actual
12311         # sizes aren't equeal (a normal situation in this case) and
12312         # print actual read amount.
12313         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
12314         if [ "$NOB" != "$BSIZE" ]; then
12315                 error "read $NOB bytes instead of $BSIZE"
12316         fi
12317         rm -f $DIR/$tfile
12318 }
12319 run_test 119a "Short directIO read must return actual read amount"
12320
12321 test_119b() # bug 11737
12322 {
12323         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12324
12325         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
12326         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
12327         sync
12328         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
12329                 error "direct read failed"
12330         rm -f $DIR/$tfile
12331 }
12332 run_test 119b "Sparse directIO read must return actual read amount"
12333
12334 test_119c() # bug 13099
12335 {
12336         BSIZE=1048576
12337         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
12338         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
12339         rm -f $DIR/$tfile
12340 }
12341 run_test 119c "Testing for direct read hitting hole"
12342
12343 test_119d() # bug 15950
12344 {
12345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12346
12347         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
12348         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
12349         BSIZE=1048576
12350         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
12351         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
12352         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
12353         lctl set_param fail_loc=0x40d
12354         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
12355         pid_dio=$!
12356         sleep 1
12357         cat $DIR/$tfile > /dev/null &
12358         lctl set_param fail_loc=0
12359         pid_reads=$!
12360         wait $pid_dio
12361         log "the DIO writes have completed, now wait for the reads (should not block very long)"
12362         sleep 2
12363         [ -n "`ps h -p $pid_reads -o comm`" ] && \
12364         error "the read rpcs have not completed in 2s"
12365         rm -f $DIR/$tfile
12366         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
12367 }
12368 run_test 119d "The DIO path should try to send a new rpc once one is completed"
12369
12370 test_120a() {
12371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12372         remote_mds_nodsh && skip "remote MDS with nodsh"
12373         test_mkdir -i0 -c1 $DIR/$tdir
12374         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12375                 skip_env "no early lock cancel on server"
12376
12377         lru_resize_disable mdc
12378         lru_resize_disable osc
12379         cancel_lru_locks mdc
12380         # asynchronous object destroy at MDT could cause bl ast to client
12381         cancel_lru_locks osc
12382
12383         stat $DIR/$tdir > /dev/null
12384         can1=$(do_facet mds1 \
12385                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12386                awk '/ldlm_cancel/ {print $2}')
12387         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12388                awk '/ldlm_bl_callback/ {print $2}')
12389         test_mkdir -i0 -c1 $DIR/$tdir/d1
12390         can2=$(do_facet mds1 \
12391                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12392                awk '/ldlm_cancel/ {print $2}')
12393         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12394                awk '/ldlm_bl_callback/ {print $2}')
12395         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12396         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12397         lru_resize_enable mdc
12398         lru_resize_enable osc
12399 }
12400 run_test 120a "Early Lock Cancel: mkdir test"
12401
12402 test_120b() {
12403         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12404         remote_mds_nodsh && skip "remote MDS with nodsh"
12405         test_mkdir $DIR/$tdir
12406         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12407                 skip_env "no early lock cancel on server"
12408
12409         lru_resize_disable mdc
12410         lru_resize_disable osc
12411         cancel_lru_locks mdc
12412         stat $DIR/$tdir > /dev/null
12413         can1=$(do_facet $SINGLEMDS \
12414                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12415                awk '/ldlm_cancel/ {print $2}')
12416         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12417                awk '/ldlm_bl_callback/ {print $2}')
12418         touch $DIR/$tdir/f1
12419         can2=$(do_facet $SINGLEMDS \
12420                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12421                awk '/ldlm_cancel/ {print $2}')
12422         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12423                awk '/ldlm_bl_callback/ {print $2}')
12424         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12425         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12426         lru_resize_enable mdc
12427         lru_resize_enable osc
12428 }
12429 run_test 120b "Early Lock Cancel: create test"
12430
12431 test_120c() {
12432         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12433         remote_mds_nodsh && skip "remote MDS with nodsh"
12434         test_mkdir -i0 -c1 $DIR/$tdir
12435         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12436                 skip "no early lock cancel on server"
12437
12438         lru_resize_disable mdc
12439         lru_resize_disable osc
12440         test_mkdir -i0 -c1 $DIR/$tdir/d1
12441         test_mkdir -i0 -c1 $DIR/$tdir/d2
12442         touch $DIR/$tdir/d1/f1
12443         cancel_lru_locks mdc
12444         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
12445         can1=$(do_facet mds1 \
12446                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12447                awk '/ldlm_cancel/ {print $2}')
12448         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12449                awk '/ldlm_bl_callback/ {print $2}')
12450         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12451         can2=$(do_facet mds1 \
12452                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12453                awk '/ldlm_cancel/ {print $2}')
12454         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12455                awk '/ldlm_bl_callback/ {print $2}')
12456         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12457         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12458         lru_resize_enable mdc
12459         lru_resize_enable osc
12460 }
12461 run_test 120c "Early Lock Cancel: link test"
12462
12463 test_120d() {
12464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12465         remote_mds_nodsh && skip "remote MDS with nodsh"
12466         test_mkdir -i0 -c1 $DIR/$tdir
12467         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12468                 skip_env "no early lock cancel on server"
12469
12470         lru_resize_disable mdc
12471         lru_resize_disable osc
12472         touch $DIR/$tdir
12473         cancel_lru_locks mdc
12474         stat $DIR/$tdir > /dev/null
12475         can1=$(do_facet mds1 \
12476                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12477                awk '/ldlm_cancel/ {print $2}')
12478         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12479                awk '/ldlm_bl_callback/ {print $2}')
12480         chmod a+x $DIR/$tdir
12481         can2=$(do_facet mds1 \
12482                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12483                awk '/ldlm_cancel/ {print $2}')
12484         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12485                awk '/ldlm_bl_callback/ {print $2}')
12486         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12487         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12488         lru_resize_enable mdc
12489         lru_resize_enable osc
12490 }
12491 run_test 120d "Early Lock Cancel: setattr test"
12492
12493 test_120e() {
12494         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12495         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12496                 skip_env "no early lock cancel on server"
12497         remote_mds_nodsh && skip "remote MDS with nodsh"
12498
12499         local dlmtrace_set=false
12500
12501         test_mkdir -i0 -c1 $DIR/$tdir
12502         lru_resize_disable mdc
12503         lru_resize_disable osc
12504         ! $LCTL get_param debug | grep -q dlmtrace &&
12505                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
12506         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
12507         cancel_lru_locks mdc
12508         cancel_lru_locks osc
12509         dd if=$DIR/$tdir/f1 of=/dev/null
12510         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
12511         # XXX client can not do early lock cancel of OST lock
12512         # during unlink (LU-4206), so cancel osc lock now.
12513         sleep 2
12514         cancel_lru_locks osc
12515         can1=$(do_facet mds1 \
12516                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12517                awk '/ldlm_cancel/ {print $2}')
12518         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12519                awk '/ldlm_bl_callback/ {print $2}')
12520         unlink $DIR/$tdir/f1
12521         sleep 5
12522         can2=$(do_facet mds1 \
12523                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12524                awk '/ldlm_cancel/ {print $2}')
12525         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12526                awk '/ldlm_bl_callback/ {print $2}')
12527         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
12528                 $LCTL dk $TMP/cancel.debug.txt
12529         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
12530                 $LCTL dk $TMP/blocking.debug.txt
12531         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
12532         lru_resize_enable mdc
12533         lru_resize_enable osc
12534 }
12535 run_test 120e "Early Lock Cancel: unlink test"
12536
12537 test_120f() {
12538         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12539         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12540                 skip_env "no early lock cancel on server"
12541         remote_mds_nodsh && skip "remote MDS with nodsh"
12542
12543         test_mkdir -i0 -c1 $DIR/$tdir
12544         lru_resize_disable mdc
12545         lru_resize_disable osc
12546         test_mkdir -i0 -c1 $DIR/$tdir/d1
12547         test_mkdir -i0 -c1 $DIR/$tdir/d2
12548         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
12549         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
12550         cancel_lru_locks mdc
12551         cancel_lru_locks osc
12552         dd if=$DIR/$tdir/d1/f1 of=/dev/null
12553         dd if=$DIR/$tdir/d2/f2 of=/dev/null
12554         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
12555         # XXX client can not do early lock cancel of OST lock
12556         # during rename (LU-4206), so cancel osc lock now.
12557         sleep 2
12558         cancel_lru_locks osc
12559         can1=$(do_facet mds1 \
12560                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12561                awk '/ldlm_cancel/ {print $2}')
12562         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12563                awk '/ldlm_bl_callback/ {print $2}')
12564         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12565         sleep 5
12566         can2=$(do_facet mds1 \
12567                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12568                awk '/ldlm_cancel/ {print $2}')
12569         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12570                awk '/ldlm_bl_callback/ {print $2}')
12571         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12572         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12573         lru_resize_enable mdc
12574         lru_resize_enable osc
12575 }
12576 run_test 120f "Early Lock Cancel: rename test"
12577
12578 test_120g() {
12579         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12580         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12581                 skip_env "no early lock cancel on server"
12582         remote_mds_nodsh && skip "remote MDS with nodsh"
12583
12584         lru_resize_disable mdc
12585         lru_resize_disable osc
12586         count=10000
12587         echo create $count files
12588         test_mkdir $DIR/$tdir
12589         cancel_lru_locks mdc
12590         cancel_lru_locks osc
12591         t0=$(date +%s)
12592
12593         can0=$(do_facet $SINGLEMDS \
12594                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12595                awk '/ldlm_cancel/ {print $2}')
12596         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12597                awk '/ldlm_bl_callback/ {print $2}')
12598         createmany -o $DIR/$tdir/f $count
12599         sync
12600         can1=$(do_facet $SINGLEMDS \
12601                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12602                awk '/ldlm_cancel/ {print $2}')
12603         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12604                awk '/ldlm_bl_callback/ {print $2}')
12605         t1=$(date +%s)
12606         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
12607         echo rm $count files
12608         rm -r $DIR/$tdir
12609         sync
12610         can2=$(do_facet $SINGLEMDS \
12611                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12612                awk '/ldlm_cancel/ {print $2}')
12613         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12614                awk '/ldlm_bl_callback/ {print $2}')
12615         t2=$(date +%s)
12616         echo total: $count removes in $((t2-t1))
12617         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
12618         sleep 2
12619         # wait for commitment of removal
12620         lru_resize_enable mdc
12621         lru_resize_enable osc
12622 }
12623 run_test 120g "Early Lock Cancel: performance test"
12624
12625 test_121() { #bug #10589
12626         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12627
12628         rm -rf $DIR/$tfile
12629         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
12630 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
12631         lctl set_param fail_loc=0x310
12632         cancel_lru_locks osc > /dev/null
12633         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
12634         lctl set_param fail_loc=0
12635         [[ $reads -eq $writes ]] ||
12636                 error "read $reads blocks, must be $writes blocks"
12637 }
12638 run_test 121 "read cancel race ========="
12639
12640 test_123a_base() { # was test 123, statahead(bug 11401)
12641         local lsx="$1"
12642
12643         SLOWOK=0
12644         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
12645                 log "testing UP system. Performance may be lower than expected."
12646                 SLOWOK=1
12647         fi
12648
12649         rm -rf $DIR/$tdir
12650         test_mkdir $DIR/$tdir
12651         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
12652         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
12653         MULT=10
12654         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
12655                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
12656
12657                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
12658                 lctl set_param -n llite.*.statahead_max 0
12659                 lctl get_param llite.*.statahead_max
12660                 cancel_lru_locks mdc
12661                 cancel_lru_locks osc
12662                 stime=$(date +%s)
12663                 time $lsx $DIR/$tdir | wc -l
12664                 etime=$(date +%s)
12665                 delta=$((etime - stime))
12666                 log "$lsx $i files without statahead: $delta sec"
12667                 lctl set_param llite.*.statahead_max=$max
12668
12669                 swrong=$(lctl get_param -n llite.*.statahead_stats |
12670                         grep "statahead wrong:" | awk '{print $3}')
12671                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
12672                 cancel_lru_locks mdc
12673                 cancel_lru_locks osc
12674                 stime=$(date +%s)
12675                 time $lsx $DIR/$tdir | wc -l
12676                 etime=$(date +%s)
12677                 delta_sa=$((etime - stime))
12678                 log "$lsx $i files with statahead: $delta_sa sec"
12679                 lctl get_param -n llite.*.statahead_stats
12680                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
12681                         grep "statahead wrong:" | awk '{print $3}')
12682
12683                 [[ $swrong -lt $ewrong ]] &&
12684                         log "statahead was stopped, maybe too many locks held!"
12685                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
12686
12687                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
12688                         max=$(lctl get_param -n llite.*.statahead_max |
12689                                 head -n 1)
12690                         lctl set_param -n llite.*.statahead_max 0
12691                         lctl get_param llite.*.statahead_max
12692                         cancel_lru_locks mdc
12693                         cancel_lru_locks osc
12694                         stime=$(date +%s)
12695                         time $lsx $DIR/$tdir | wc -l
12696                         etime=$(date +%s)
12697                         delta=$((etime - stime))
12698                         log "$lsx $i files again without statahead: $delta sec"
12699                         lctl set_param llite.*.statahead_max=$max
12700                         if [ $((delta_sa * 100 > delta * 105 && delta_sa > delta + 2)) ]; then
12701                                 if [  $SLOWOK -eq 0 ]; then
12702                                         error "$lsx $i files is slower with statahead!"
12703                                 else
12704                                         log "$lsx $i files is slower with statahead!"
12705                                 fi
12706                                 break
12707                         fi
12708                 fi
12709
12710                 [ $delta -gt 20 ] && break
12711                 [ $delta -gt 8 ] && MULT=$((50 / delta))
12712                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
12713         done
12714         log "$lsx done"
12715
12716         stime=$(date +%s)
12717         rm -r $DIR/$tdir
12718         sync
12719         etime=$(date +%s)
12720         delta=$((etime - stime))
12721         log "rm -r $DIR/$tdir/: $delta seconds"
12722         log "rm done"
12723         lctl get_param -n llite.*.statahead_stats
12724 }
12725
12726 test_123aa() {
12727         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12728
12729         test_123a_base "ls -l"
12730 }
12731 run_test 123aa "verify statahead work"
12732
12733 test_123ab() {
12734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12735
12736         statx_supported || skip_env "Test must be statx() syscall supported"
12737
12738         test_123a_base "$STATX -l"
12739 }
12740 run_test 123ab "verify statahead work by using statx"
12741
12742 test_123ac() {
12743         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12744
12745         statx_supported || skip_env "Test must be statx() syscall supported"
12746
12747         local rpcs_before
12748         local rpcs_after
12749         local agl_before
12750         local agl_after
12751
12752         cancel_lru_locks $OSC
12753         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
12754         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
12755                 awk '/agl.total:/ {print $3}')
12756         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
12757         test_123a_base "$STATX --cached=always -D"
12758         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
12759                 awk '/agl.total:/ {print $3}')
12760         [ $agl_before -eq $agl_after ] ||
12761                 error "Should not trigger AGL thread - $agl_before:$agl_after"
12762         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
12763         [ $rpcs_after -eq $rpcs_before ] ||
12764                 error "$STATX should not send glimpse RPCs to $OSC"
12765 }
12766 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
12767
12768 test_123b () { # statahead(bug 15027)
12769         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12770
12771         test_mkdir $DIR/$tdir
12772         createmany -o $DIR/$tdir/$tfile-%d 1000
12773
12774         cancel_lru_locks mdc
12775         cancel_lru_locks osc
12776
12777 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
12778         lctl set_param fail_loc=0x80000803
12779         ls -lR $DIR/$tdir > /dev/null
12780         log "ls done"
12781         lctl set_param fail_loc=0x0
12782         lctl get_param -n llite.*.statahead_stats
12783         rm -r $DIR/$tdir
12784         sync
12785
12786 }
12787 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
12788
12789 test_123c() {
12790         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
12791
12792         test_mkdir -i 0 -c 1 $DIR/$tdir.0
12793         test_mkdir -i 1 -c 1 $DIR/$tdir.1
12794         touch $DIR/$tdir.1/{1..3}
12795         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
12796
12797         remount_client $MOUNT
12798
12799         $MULTIOP $DIR/$tdir.0 Q
12800
12801         # let statahead to complete
12802         ls -l $DIR/$tdir.0 > /dev/null
12803
12804         testid=$(echo $TESTNAME | tr '_' ' ')
12805         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
12806                 error "statahead warning" || true
12807 }
12808 run_test 123c "Can not initialize inode warning on DNE statahead"
12809
12810 test_124a() {
12811         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12812         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12813                 skip_env "no lru resize on server"
12814
12815         local NR=2000
12816
12817         test_mkdir $DIR/$tdir
12818
12819         log "create $NR files at $DIR/$tdir"
12820         createmany -o $DIR/$tdir/f $NR ||
12821                 error "failed to create $NR files in $DIR/$tdir"
12822
12823         cancel_lru_locks mdc
12824         ls -l $DIR/$tdir > /dev/null
12825
12826         local NSDIR=""
12827         local LRU_SIZE=0
12828         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
12829                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
12830                 LRU_SIZE=$($LCTL get_param -n $PARAM)
12831                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
12832                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
12833                         log "NSDIR=$NSDIR"
12834                         log "NS=$(basename $NSDIR)"
12835                         break
12836                 fi
12837         done
12838
12839         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
12840                 skip "Not enough cached locks created!"
12841         fi
12842         log "LRU=$LRU_SIZE"
12843
12844         local SLEEP=30
12845
12846         # We know that lru resize allows one client to hold $LIMIT locks
12847         # for 10h. After that locks begin to be killed by client.
12848         local MAX_HRS=10
12849         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
12850         log "LIMIT=$LIMIT"
12851         if [ $LIMIT -lt $LRU_SIZE ]; then
12852                 skip "Limit is too small $LIMIT"
12853         fi
12854
12855         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
12856         # killing locks. Some time was spent for creating locks. This means
12857         # that up to the moment of sleep finish we must have killed some of
12858         # them (10-100 locks). This depends on how fast ther were created.
12859         # Many of them were touched in almost the same moment and thus will
12860         # be killed in groups.
12861         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE * 100))
12862
12863         # Use $LRU_SIZE_B here to take into account real number of locks
12864         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
12865         local LRU_SIZE_B=$LRU_SIZE
12866         log "LVF=$LVF"
12867         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
12868         log "OLD_LVF=$OLD_LVF"
12869         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
12870
12871         # Let's make sure that we really have some margin. Client checks
12872         # cached locks every 10 sec.
12873         SLEEP=$((SLEEP+20))
12874         log "Sleep ${SLEEP} sec"
12875         local SEC=0
12876         while ((SEC<$SLEEP)); do
12877                 echo -n "..."
12878                 sleep 5
12879                 SEC=$((SEC+5))
12880                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
12881                 echo -n "$LRU_SIZE"
12882         done
12883         echo ""
12884         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
12885         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
12886
12887         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
12888                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
12889                 unlinkmany $DIR/$tdir/f $NR
12890                 return
12891         }
12892
12893         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
12894         log "unlink $NR files at $DIR/$tdir"
12895         unlinkmany $DIR/$tdir/f $NR
12896 }
12897 run_test 124a "lru resize ======================================="
12898
12899 get_max_pool_limit()
12900 {
12901         local limit=$($LCTL get_param \
12902                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
12903         local max=0
12904         for l in $limit; do
12905                 if [[ $l -gt $max ]]; then
12906                         max=$l
12907                 fi
12908         done
12909         echo $max
12910 }
12911
12912 test_124b() {
12913         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12914         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12915                 skip_env "no lru resize on server"
12916
12917         LIMIT=$(get_max_pool_limit)
12918
12919         NR=$(($(default_lru_size)*20))
12920         if [[ $NR -gt $LIMIT ]]; then
12921                 log "Limit lock number by $LIMIT locks"
12922                 NR=$LIMIT
12923         fi
12924
12925         IFree=$(mdsrate_inodes_available)
12926         if [ $IFree -lt $NR ]; then
12927                 log "Limit lock number by $IFree inodes"
12928                 NR=$IFree
12929         fi
12930
12931         lru_resize_disable mdc
12932         test_mkdir -p $DIR/$tdir/disable_lru_resize
12933
12934         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
12935         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
12936         cancel_lru_locks mdc
12937         stime=`date +%s`
12938         PID=""
12939         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12940         PID="$PID $!"
12941         sleep 2
12942         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12943         PID="$PID $!"
12944         sleep 2
12945         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12946         PID="$PID $!"
12947         wait $PID
12948         etime=`date +%s`
12949         nolruresize_delta=$((etime-stime))
12950         log "ls -la time: $nolruresize_delta seconds"
12951         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
12952         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
12953
12954         lru_resize_enable mdc
12955         test_mkdir -p $DIR/$tdir/enable_lru_resize
12956
12957         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
12958         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
12959         cancel_lru_locks mdc
12960         stime=`date +%s`
12961         PID=""
12962         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
12963         PID="$PID $!"
12964         sleep 2
12965         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
12966         PID="$PID $!"
12967         sleep 2
12968         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
12969         PID="$PID $!"
12970         wait $PID
12971         etime=`date +%s`
12972         lruresize_delta=$((etime-stime))
12973         log "ls -la time: $lruresize_delta seconds"
12974         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
12975
12976         if [ $lruresize_delta -gt $nolruresize_delta ]; then
12977                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
12978         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
12979                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
12980         else
12981                 log "lru resize performs the same with no lru resize"
12982         fi
12983         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
12984 }
12985 run_test 124b "lru resize (performance test) ======================="
12986
12987 test_124c() {
12988         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12989         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12990                 skip_env "no lru resize on server"
12991
12992         # cache ununsed locks on client
12993         local nr=100
12994         cancel_lru_locks mdc
12995         test_mkdir $DIR/$tdir
12996         createmany -o $DIR/$tdir/f $nr ||
12997                 error "failed to create $nr files in $DIR/$tdir"
12998         ls -l $DIR/$tdir > /dev/null
12999
13000         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13001         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13002         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13003         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13004         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13005
13006         # set lru_max_age to 1 sec
13007         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13008         echo "sleep $((recalc_p * 2)) seconds..."
13009         sleep $((recalc_p * 2))
13010
13011         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13012         # restore lru_max_age
13013         $LCTL set_param -n $nsdir.lru_max_age $max_age
13014         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13015         unlinkmany $DIR/$tdir/f $nr
13016 }
13017 run_test 124c "LRUR cancel very aged locks"
13018
13019 test_124d() {
13020         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13021         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13022                 skip_env "no lru resize on server"
13023
13024         # cache ununsed locks on client
13025         local nr=100
13026
13027         lru_resize_disable mdc
13028         stack_trap "lru_resize_enable mdc" EXIT
13029
13030         cancel_lru_locks mdc
13031
13032         # asynchronous object destroy at MDT could cause bl ast to client
13033         test_mkdir $DIR/$tdir
13034         createmany -o $DIR/$tdir/f $nr ||
13035                 error "failed to create $nr files in $DIR/$tdir"
13036         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
13037
13038         ls -l $DIR/$tdir > /dev/null
13039
13040         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13041         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13042         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13043         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13044
13045         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13046
13047         # set lru_max_age to 1 sec
13048         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13049         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
13050
13051         echo "sleep $((recalc_p * 2)) seconds..."
13052         sleep $((recalc_p * 2))
13053
13054         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13055
13056         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13057 }
13058 run_test 124d "cancel very aged locks if lru-resize diasbaled"
13059
13060 test_125() { # 13358
13061         $LCTL get_param -n llite.*.client_type | grep -q local ||
13062                 skip "must run as local client"
13063         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
13064                 skip_env "must have acl enabled"
13065         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13066
13067         test_mkdir $DIR/$tdir
13068         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
13069         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
13070         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
13071 }
13072 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
13073
13074 test_126() { # bug 12829/13455
13075         $GSS && skip_env "must run as gss disabled"
13076         $LCTL get_param -n llite.*.client_type | grep -q local ||
13077                 skip "must run as local client"
13078         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
13079
13080         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
13081         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
13082         rm -f $DIR/$tfile
13083         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
13084 }
13085 run_test 126 "check that the fsgid provided by the client is taken into account"
13086
13087 test_127a() { # bug 15521
13088         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13089         local name count samp unit min max sum sumsq
13090
13091         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
13092         echo "stats before reset"
13093         $LCTL get_param osc.*.stats
13094         $LCTL set_param osc.*.stats=0
13095         local fsize=$((2048 * 1024))
13096
13097         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
13098         cancel_lru_locks osc
13099         dd if=$DIR/$tfile of=/dev/null bs=$fsize
13100
13101         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
13102         stack_trap "rm -f $TMP/$tfile.tmp"
13103         while read name count samp unit min max sum sumsq; do
13104                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13105                 [ ! $min ] && error "Missing min value for $name proc entry"
13106                 eval $name=$count || error "Wrong proc format"
13107
13108                 case $name in
13109                 read_bytes|write_bytes)
13110                         [[ "$unit" =~ "bytes" ]] ||
13111                                 error "unit is not 'bytes': $unit"
13112                         (( $min >= 4096 )) || error "min is too small: $min"
13113                         (( $min <= $fsize )) || error "min is too big: $min"
13114                         (( $max >= 4096 )) || error "max is too small: $max"
13115                         (( $max <= $fsize )) || error "max is too big: $max"
13116                         (( $sum == $fsize )) || error "sum is wrong: $sum"
13117                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
13118                                 error "sumsquare is too small: $sumsq"
13119                         (( $sumsq <= $fsize * $fsize )) ||
13120                                 error "sumsquare is too big: $sumsq"
13121                         ;;
13122                 ost_read|ost_write)
13123                         [[ "$unit" =~ "usec" ]] ||
13124                                 error "unit is not 'usec': $unit"
13125                         ;;
13126                 *)      ;;
13127                 esac
13128         done < $DIR/$tfile.tmp
13129
13130         #check that we actually got some stats
13131         [ "$read_bytes" ] || error "Missing read_bytes stats"
13132         [ "$write_bytes" ] || error "Missing write_bytes stats"
13133         [ "$read_bytes" != 0 ] || error "no read done"
13134         [ "$write_bytes" != 0 ] || error "no write done"
13135 }
13136 run_test 127a "verify the client stats are sane"
13137
13138 test_127b() { # bug LU-333
13139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13140         local name count samp unit min max sum sumsq
13141
13142         echo "stats before reset"
13143         $LCTL get_param llite.*.stats
13144         $LCTL set_param llite.*.stats=0
13145
13146         # perform 2 reads and writes so MAX is different from SUM.
13147         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13148         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13149         cancel_lru_locks osc
13150         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13151         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13152
13153         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
13154         stack_trap "rm -f $TMP/$tfile.tmp"
13155         while read name count samp unit min max sum sumsq; do
13156                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13157                 eval $name=$count || error "Wrong proc format"
13158
13159                 case $name in
13160                 read_bytes|write_bytes)
13161                         [[ "$unit" =~ "bytes" ]] ||
13162                                 error "unit is not 'bytes': $unit"
13163                         (( $count == 2 )) || error "count is not 2: $count"
13164                         (( $min == $PAGE_SIZE )) ||
13165                                 error "min is not $PAGE_SIZE: $min"
13166                         (( $max == $PAGE_SIZE )) ||
13167                                 error "max is not $PAGE_SIZE: $max"
13168                         (( $sum == $PAGE_SIZE * 2 )) ||
13169                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
13170                         ;;
13171                 read|write)
13172                         [[ "$unit" =~ "usec" ]] ||
13173                                 error "unit is not 'usec': $unit"
13174                         ;;
13175                 *)      ;;
13176                 esac
13177         done < $TMP/$tfile.tmp
13178
13179         #check that we actually got some stats
13180         [ "$read_bytes" ] || error "Missing read_bytes stats"
13181         [ "$write_bytes" ] || error "Missing write_bytes stats"
13182         [ "$read_bytes" != 0 ] || error "no read done"
13183         [ "$write_bytes" != 0 ] || error "no write done"
13184 }
13185 run_test 127b "verify the llite client stats are sane"
13186
13187 test_127c() { # LU-12394
13188         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13189         local size
13190         local bsize
13191         local reads
13192         local writes
13193         local count
13194
13195         $LCTL set_param llite.*.extents_stats=1
13196         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
13197
13198         # Use two stripes so there is enough space in default config
13199         $LFS setstripe -c 2 $DIR/$tfile
13200
13201         # Extent stats start at 0-4K and go in power of two buckets
13202         # LL_HIST_START = 12 --> 2^12 = 4K
13203         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
13204         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
13205         # small configs
13206         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
13207                 do
13208                 # Write and read, 2x each, second time at a non-zero offset
13209                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
13210                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
13211                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
13212                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
13213                 rm -f $DIR/$tfile
13214         done
13215
13216         $LCTL get_param llite.*.extents_stats
13217
13218         count=2
13219         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
13220                 do
13221                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
13222                                 grep -m 1 $bsize)
13223                 reads=$(echo $bucket | awk '{print $5}')
13224                 writes=$(echo $bucket | awk '{print $9}')
13225                 [ "$reads" -eq $count ] ||
13226                         error "$reads reads in < $bsize bucket, expect $count"
13227                 [ "$writes" -eq $count ] ||
13228                         error "$writes writes in < $bsize bucket, expect $count"
13229         done
13230
13231         # Test mmap write and read
13232         $LCTL set_param llite.*.extents_stats=c
13233         size=512
13234         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
13235         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
13236         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
13237
13238         $LCTL get_param llite.*.extents_stats
13239
13240         count=$(((size*1024) / PAGE_SIZE))
13241
13242         bsize=$((2 * PAGE_SIZE / 1024))K
13243
13244         bucket=$($LCTL get_param -n llite.*.extents_stats |
13245                         grep -m 1 $bsize)
13246         reads=$(echo $bucket | awk '{print $5}')
13247         writes=$(echo $bucket | awk '{print $9}')
13248         # mmap writes fault in the page first, creating an additonal read
13249         [ "$reads" -eq $((2 * count)) ] ||
13250                 error "$reads reads in < $bsize bucket, expect $count"
13251         [ "$writes" -eq $count ] ||
13252                 error "$writes writes in < $bsize bucket, expect $count"
13253 }
13254 run_test 127c "test llite extent stats with regular & mmap i/o"
13255
13256 test_128() { # bug 15212
13257         touch $DIR/$tfile
13258         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
13259                 find $DIR/$tfile
13260                 find $DIR/$tfile
13261         EOF
13262
13263         result=$(grep error $TMP/$tfile.log)
13264         rm -f $DIR/$tfile $TMP/$tfile.log
13265         [ -z "$result" ] ||
13266                 error "consecutive find's under interactive lfs failed"
13267 }
13268 run_test 128 "interactive lfs for 2 consecutive find's"
13269
13270 set_dir_limits () {
13271         local mntdev
13272         local canondev
13273         local node
13274
13275         local ldproc=/proc/fs/ldiskfs
13276         local facets=$(get_facets MDS)
13277
13278         for facet in ${facets//,/ }; do
13279                 canondev=$(ldiskfs_canon \
13280                            *.$(convert_facet2label $facet).mntdev $facet)
13281                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
13282                         ldproc=/sys/fs/ldiskfs
13283                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
13284                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
13285         done
13286 }
13287
13288 check_mds_dmesg() {
13289         local facets=$(get_facets MDS)
13290         for facet in ${facets//,/ }; do
13291                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
13292         done
13293         return 1
13294 }
13295
13296 test_129() {
13297         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13298         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
13299                 skip "Need MDS version with at least 2.5.56"
13300         if [ "$mds1_FSTYPE" != ldiskfs ]; then
13301                 skip_env "ldiskfs only test"
13302         fi
13303         remote_mds_nodsh && skip "remote MDS with nodsh"
13304
13305         local ENOSPC=28
13306         local has_warning=false
13307
13308         rm -rf $DIR/$tdir
13309         mkdir -p $DIR/$tdir
13310
13311         # block size of mds1
13312         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
13313         set_dir_limits $maxsize $((maxsize * 6 / 8))
13314         stack_trap "set_dir_limits 0 0"
13315         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
13316         local dirsize=$(stat -c%s "$DIR/$tdir")
13317         local nfiles=0
13318         while (( $dirsize <= $maxsize )); do
13319                 $MCREATE $DIR/$tdir/file_base_$nfiles
13320                 rc=$?
13321                 # check two errors:
13322                 # ENOSPC for ext4 max_dir_size, which has been used since
13323                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
13324                 if (( rc == ENOSPC )); then
13325                         set_dir_limits 0 0
13326                         echo "rc=$rc returned as expected after $nfiles files"
13327
13328                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
13329                                 error "create failed w/o dir size limit"
13330
13331                         # messages may be rate limited if test is run repeatedly
13332                         check_mds_dmesg '"is approaching max"' ||
13333                                 echo "warning message should be output"
13334                         check_mds_dmesg '"has reached max"' ||
13335                                 echo "reached message should be output"
13336
13337                         dirsize=$(stat -c%s "$DIR/$tdir")
13338
13339                         [[ $dirsize -ge $maxsize ]] && return 0
13340                         error "dirsize $dirsize < $maxsize after $nfiles files"
13341                 elif (( rc != 0 )); then
13342                         break
13343                 fi
13344                 nfiles=$((nfiles + 1))
13345                 dirsize=$(stat -c%s "$DIR/$tdir")
13346         done
13347
13348         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
13349 }
13350 run_test 129 "test directory size limit ========================"
13351
13352 OLDIFS="$IFS"
13353 cleanup_130() {
13354         trap 0
13355         IFS="$OLDIFS"
13356 }
13357
13358 test_130a() {
13359         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13360         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13361
13362         trap cleanup_130 EXIT RETURN
13363
13364         local fm_file=$DIR/$tfile
13365         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
13366         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
13367                 error "dd failed for $fm_file"
13368
13369         # LU-1795: test filefrag/FIEMAP once, even if unsupported
13370         filefrag -ves $fm_file
13371         RC=$?
13372         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13373                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13374         [ $RC != 0 ] && error "filefrag $fm_file failed"
13375
13376         filefrag_op=$(filefrag -ve -k $fm_file |
13377                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13378         lun=$($LFS getstripe -i $fm_file)
13379
13380         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
13381         IFS=$'\n'
13382         tot_len=0
13383         for line in $filefrag_op
13384         do
13385                 frag_lun=`echo $line | cut -d: -f5`
13386                 ext_len=`echo $line | cut -d: -f4`
13387                 if (( $frag_lun != $lun )); then
13388                         cleanup_130
13389                         error "FIEMAP on 1-stripe file($fm_file) failed"
13390                         return
13391                 fi
13392                 (( tot_len += ext_len ))
13393         done
13394
13395         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
13396                 cleanup_130
13397                 error "FIEMAP on 1-stripe file($fm_file) failed;"
13398                 return
13399         fi
13400
13401         cleanup_130
13402
13403         echo "FIEMAP on single striped file succeeded"
13404 }
13405 run_test 130a "FIEMAP (1-stripe file)"
13406
13407 test_130b() {
13408         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13409
13410         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13411         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13412
13413         trap cleanup_130 EXIT RETURN
13414
13415         local fm_file=$DIR/$tfile
13416         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13417                         error "setstripe on $fm_file"
13418         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13419                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13420
13421         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
13422                 error "dd failed on $fm_file"
13423
13424         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13425         filefrag_op=$(filefrag -ve -k $fm_file |
13426                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13427
13428         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13429                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13430
13431         IFS=$'\n'
13432         tot_len=0
13433         num_luns=1
13434         for line in $filefrag_op
13435         do
13436                 frag_lun=$(echo $line | cut -d: -f5 |
13437                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13438                 ext_len=$(echo $line | cut -d: -f4)
13439                 if (( $frag_lun != $last_lun )); then
13440                         if (( tot_len != 1024 )); then
13441                                 cleanup_130
13442                                 error "FIEMAP on $fm_file failed; returned " \
13443                                 "len $tot_len for OST $last_lun instead of 1024"
13444                                 return
13445                         else
13446                                 (( num_luns += 1 ))
13447                                 tot_len=0
13448                         fi
13449                 fi
13450                 (( tot_len += ext_len ))
13451                 last_lun=$frag_lun
13452         done
13453         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
13454                 cleanup_130
13455                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13456                         "luns or wrong len for OST $last_lun"
13457                 return
13458         fi
13459
13460         cleanup_130
13461
13462         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
13463 }
13464 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
13465
13466 test_130c() {
13467         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13468
13469         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13470         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13471
13472         trap cleanup_130 EXIT RETURN
13473
13474         local fm_file=$DIR/$tfile
13475         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
13476         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13477                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13478
13479         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
13480                         error "dd failed on $fm_file"
13481
13482         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13483         filefrag_op=$(filefrag -ve -k $fm_file |
13484                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13485
13486         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13487                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13488
13489         IFS=$'\n'
13490         tot_len=0
13491         num_luns=1
13492         for line in $filefrag_op
13493         do
13494                 frag_lun=$(echo $line | cut -d: -f5 |
13495                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13496                 ext_len=$(echo $line | cut -d: -f4)
13497                 if (( $frag_lun != $last_lun )); then
13498                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
13499                         if (( logical != 512 )); then
13500                                 cleanup_130
13501                                 error "FIEMAP on $fm_file failed; returned " \
13502                                 "logical start for lun $logical instead of 512"
13503                                 return
13504                         fi
13505                         if (( tot_len != 512 )); then
13506                                 cleanup_130
13507                                 error "FIEMAP on $fm_file failed; returned " \
13508                                 "len $tot_len for OST $last_lun instead of 1024"
13509                                 return
13510                         else
13511                                 (( num_luns += 1 ))
13512                                 tot_len=0
13513                         fi
13514                 fi
13515                 (( tot_len += ext_len ))
13516                 last_lun=$frag_lun
13517         done
13518         if (( num_luns != 2 || tot_len != 512 )); then
13519                 cleanup_130
13520                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13521                         "luns or wrong len for OST $last_lun"
13522                 return
13523         fi
13524
13525         cleanup_130
13526
13527         echo "FIEMAP on 2-stripe file with hole succeeded"
13528 }
13529 run_test 130c "FIEMAP (2-stripe file with hole)"
13530
13531 test_130d() {
13532         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
13533
13534         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13535         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13536
13537         trap cleanup_130 EXIT RETURN
13538
13539         local fm_file=$DIR/$tfile
13540         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13541                         error "setstripe on $fm_file"
13542         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13543                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13544
13545         local actual_stripe_count=$($LFS getstripe -c $fm_file)
13546         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
13547                 error "dd failed on $fm_file"
13548
13549         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13550         filefrag_op=$(filefrag -ve -k $fm_file |
13551                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13552
13553         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13554                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13555
13556         IFS=$'\n'
13557         tot_len=0
13558         num_luns=1
13559         for line in $filefrag_op
13560         do
13561                 frag_lun=$(echo $line | cut -d: -f5 |
13562                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13563                 ext_len=$(echo $line | cut -d: -f4)
13564                 if (( $frag_lun != $last_lun )); then
13565                         if (( tot_len != 1024 )); then
13566                                 cleanup_130
13567                                 error "FIEMAP on $fm_file failed; returned " \
13568                                 "len $tot_len for OST $last_lun instead of 1024"
13569                                 return
13570                         else
13571                                 (( num_luns += 1 ))
13572                                 tot_len=0
13573                         fi
13574                 fi
13575                 (( tot_len += ext_len ))
13576                 last_lun=$frag_lun
13577         done
13578         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
13579                 cleanup_130
13580                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13581                         "luns or wrong len for OST $last_lun"
13582                 return
13583         fi
13584
13585         cleanup_130
13586
13587         echo "FIEMAP on N-stripe file succeeded"
13588 }
13589 run_test 130d "FIEMAP (N-stripe file)"
13590
13591 test_130e() {
13592         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13593
13594         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13595         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13596
13597         trap cleanup_130 EXIT RETURN
13598
13599         local fm_file=$DIR/$tfile
13600         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
13601
13602         NUM_BLKS=512
13603         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
13604         for ((i = 0; i < $NUM_BLKS; i++)); do
13605                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) \
13606                         conv=notrunc > /dev/null 2>&1
13607         done
13608
13609         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13610         filefrag_op=$(filefrag -ve -k $fm_file |
13611                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13612
13613         last_lun=$(echo $filefrag_op | cut -d: -f5)
13614
13615         IFS=$'\n'
13616         tot_len=0
13617         num_luns=1
13618         for line in $filefrag_op; do
13619                 frag_lun=$(echo $line | cut -d: -f5)
13620                 ext_len=$(echo $line | cut -d: -f4)
13621                 if [[ "$frag_lun" != "$last_lun" ]]; then
13622                         if (( tot_len != $EXPECTED_LEN )); then
13623                                 cleanup_130
13624                                 error "OST$last_lun $tot_len != $EXPECTED_LEN"
13625                         else
13626                                 (( num_luns += 1 ))
13627                                 tot_len=0
13628                         fi
13629                 fi
13630                 (( tot_len += ext_len ))
13631                 last_lun=$frag_lun
13632         done
13633         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
13634                 cleanup_130
13635                 error "OST$last_lun $num_luns != 2, $tot_len != $EXPECTED_LEN"
13636         fi
13637
13638         echo "FIEMAP with continuation calls succeeded"
13639 }
13640 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
13641
13642 test_130f() {
13643         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13644         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13645
13646         local fm_file=$DIR/$tfile
13647         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
13648                 error "multiop create with lov_delay_create on $fm_file"
13649
13650         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13651         filefrag_extents=$(filefrag -vek $fm_file |
13652                            awk '/extents? found/ { print $2 }')
13653         if [[ "$filefrag_extents" != "0" ]]; then
13654                 error "$fm_file: filefrag_extents=$filefrag_extents != 0"
13655         fi
13656
13657         rm -f $fm_file
13658 }
13659 run_test 130f "FIEMAP (unstriped file)"
13660
13661 test_130g() {
13662         local file=$DIR/$tfile
13663         local nr=$((OSTCOUNT * 100))
13664
13665         $LFS setstripe -C $nr $file ||
13666                 error "failed to setstripe -C $nr $file"
13667
13668         dd if=/dev/zero of=$file count=$nr bs=1M
13669         sync
13670         nr=$($LFS getstripe -c $file)
13671
13672         local extents=$(filefrag -v $file |
13673                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}' | wc -l)
13674
13675         echo "filefrag list $extents extents in file with stripecount $nr"
13676         if (( extents < nr )); then
13677                 $LFS getstripe $file
13678                 filefrag -v $file
13679                 error "filefrag printed $extents < $nr extents"
13680         fi
13681
13682         rm -f $file
13683 }
13684 run_test 130g "FIEMAP (overstripe file)"
13685
13686 # Test for writev/readv
13687 test_131a() {
13688         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
13689                 error "writev test failed"
13690         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
13691                 error "readv failed"
13692         rm -f $DIR/$tfile
13693 }
13694 run_test 131a "test iov's crossing stripe boundary for writev/readv"
13695
13696 test_131b() {
13697         local fsize=$((524288 + 1048576 + 1572864))
13698         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
13699                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
13700                         error "append writev test failed"
13701
13702         ((fsize += 1572864 + 1048576))
13703         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
13704                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
13705                         error "append writev test failed"
13706         rm -f $DIR/$tfile
13707 }
13708 run_test 131b "test append writev"
13709
13710 test_131c() {
13711         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
13712         error "NOT PASS"
13713 }
13714 run_test 131c "test read/write on file w/o objects"
13715
13716 test_131d() {
13717         rwv -f $DIR/$tfile -w -n 1 1572864
13718         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
13719         if [ "$NOB" != 1572864 ]; then
13720                 error "Short read filed: read $NOB bytes instead of 1572864"
13721         fi
13722         rm -f $DIR/$tfile
13723 }
13724 run_test 131d "test short read"
13725
13726 test_131e() {
13727         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
13728         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
13729         error "read hitting hole failed"
13730         rm -f $DIR/$tfile
13731 }
13732 run_test 131e "test read hitting hole"
13733
13734 check_stats() {
13735         local facet=$1
13736         local op=$2
13737         local want=${3:-0}
13738         local res
13739
13740         case $facet in
13741         mds*) res=$(do_facet $facet \
13742                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
13743                  ;;
13744         ost*) res=$(do_facet $facet \
13745                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
13746                  ;;
13747         *) error "Wrong facet '$facet'" ;;
13748         esac
13749         [ "$res" ] || error "The counter for $op on $facet was not incremented"
13750         # if the argument $3 is zero, it means any stat increment is ok.
13751         if [[ $want -gt 0 ]]; then
13752                 local count=$(echo $res | awk '{ print $2 }')
13753                 [[ $count -ne $want ]] &&
13754                         error "The $op counter on $facet is $count, not $want"
13755         fi
13756 }
13757
13758 test_133a() {
13759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13760         remote_ost_nodsh && skip "remote OST with nodsh"
13761         remote_mds_nodsh && skip "remote MDS with nodsh"
13762         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
13763                 skip_env "MDS doesn't support rename stats"
13764
13765         local testdir=$DIR/${tdir}/stats_testdir
13766
13767         mkdir -p $DIR/${tdir}
13768
13769         # clear stats.
13770         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13771         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13772
13773         # verify mdt stats first.
13774         mkdir ${testdir} || error "mkdir failed"
13775         check_stats $SINGLEMDS "mkdir" 1
13776         touch ${testdir}/${tfile} || error "touch failed"
13777         check_stats $SINGLEMDS "open" 1
13778         check_stats $SINGLEMDS "close" 1
13779         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
13780                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
13781                 check_stats $SINGLEMDS "mknod" 2
13782         }
13783         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
13784         check_stats $SINGLEMDS "unlink" 1
13785         rm -f ${testdir}/${tfile} || error "file remove failed"
13786         check_stats $SINGLEMDS "unlink" 2
13787
13788         # remove working dir and check mdt stats again.
13789         rmdir ${testdir} || error "rmdir failed"
13790         check_stats $SINGLEMDS "rmdir" 1
13791
13792         local testdir1=$DIR/${tdir}/stats_testdir1
13793         mkdir -p ${testdir}
13794         mkdir -p ${testdir1}
13795         touch ${testdir1}/test1
13796         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
13797         check_stats $SINGLEMDS "crossdir_rename" 1
13798
13799         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
13800         check_stats $SINGLEMDS "samedir_rename" 1
13801
13802         rm -rf $DIR/${tdir}
13803 }
13804 run_test 133a "Verifying MDT stats ========================================"
13805
13806 test_133b() {
13807         local res
13808
13809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13810         remote_ost_nodsh && skip "remote OST with nodsh"
13811         remote_mds_nodsh && skip "remote MDS with nodsh"
13812
13813         local testdir=$DIR/${tdir}/stats_testdir
13814
13815         mkdir -p ${testdir} || error "mkdir failed"
13816         touch ${testdir}/${tfile} || error "touch failed"
13817         cancel_lru_locks mdc
13818
13819         # clear stats.
13820         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13821         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13822
13823         # extra mdt stats verification.
13824         chmod 444 ${testdir}/${tfile} || error "chmod failed"
13825         check_stats $SINGLEMDS "setattr" 1
13826         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13827         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
13828         then            # LU-1740
13829                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
13830                 check_stats $SINGLEMDS "getattr" 1
13831         fi
13832         rm -rf $DIR/${tdir}
13833
13834         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
13835         # so the check below is not reliable
13836         [ $MDSCOUNT -eq 1 ] || return 0
13837
13838         # Sleep to avoid a cached response.
13839         #define OBD_STATFS_CACHE_SECONDS 1
13840         sleep 2
13841         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13842         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
13843         $LFS df || error "lfs failed"
13844         check_stats $SINGLEMDS "statfs" 1
13845
13846         # check aggregated statfs (LU-10018)
13847         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
13848                 return 0
13849         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
13850                 return 0
13851         sleep 2
13852         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13853         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
13854         df $DIR
13855         check_stats $SINGLEMDS "statfs" 1
13856
13857         # We want to check that the client didn't send OST_STATFS to
13858         # ost1 but the MDT also uses OST_STATFS for precreate. So some
13859         # extra care is needed here.
13860         if remote_mds; then
13861                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
13862                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
13863
13864                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
13865                 [ "$res" ] && error "OST got STATFS"
13866         fi
13867
13868         return 0
13869 }
13870 run_test 133b "Verifying extra MDT stats =================================="
13871
13872 test_133c() {
13873         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13874         remote_ost_nodsh && skip "remote OST with nodsh"
13875         remote_mds_nodsh && skip "remote MDS with nodsh"
13876
13877         local testdir=$DIR/$tdir/stats_testdir
13878
13879         test_mkdir -p $testdir
13880
13881         # verify obdfilter stats.
13882         $LFS setstripe -c 1 -i 0 $testdir/$tfile
13883         sync
13884         cancel_lru_locks osc
13885         wait_delete_completed
13886
13887         # clear stats.
13888         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13889         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13890
13891         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
13892                 error "dd failed"
13893         sync
13894         cancel_lru_locks osc
13895         check_stats ost1 "write" 1
13896
13897         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
13898         check_stats ost1 "read" 1
13899
13900         > $testdir/$tfile || error "truncate failed"
13901         check_stats ost1 "punch" 1
13902
13903         rm -f $testdir/$tfile || error "file remove failed"
13904         wait_delete_completed
13905         check_stats ost1 "destroy" 1
13906
13907         rm -rf $DIR/$tdir
13908 }
13909 run_test 133c "Verifying OST stats ========================================"
13910
13911 order_2() {
13912         local value=$1
13913         local orig=$value
13914         local order=1
13915
13916         while [ $value -ge 2 ]; do
13917                 order=$((order*2))
13918                 value=$((value/2))
13919         done
13920
13921         if [ $orig -gt $order ]; then
13922                 order=$((order*2))
13923         fi
13924         echo $order
13925 }
13926
13927 size_in_KMGT() {
13928     local value=$1
13929     local size=('K' 'M' 'G' 'T');
13930     local i=0
13931     local size_string=$value
13932
13933     while [ $value -ge 1024 ]; do
13934         if [ $i -gt 3 ]; then
13935             #T is the biggest unit we get here, if that is bigger,
13936             #just return XXXT
13937             size_string=${value}T
13938             break
13939         fi
13940         value=$((value >> 10))
13941         if [ $value -lt 1024 ]; then
13942             size_string=${value}${size[$i]}
13943             break
13944         fi
13945         i=$((i + 1))
13946     done
13947
13948     echo $size_string
13949 }
13950
13951 get_rename_size() {
13952         local size=$1
13953         local context=${2:-.}
13954         local sample=$(do_facet $SINGLEMDS $LCTL \
13955                 get_param mdt.$FSNAME-MDT0000.rename_stats |
13956                 grep -A1 $context |
13957                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
13958         echo $sample
13959 }
13960
13961 test_133d() {
13962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13963         remote_ost_nodsh && skip "remote OST with nodsh"
13964         remote_mds_nodsh && skip "remote MDS with nodsh"
13965         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
13966                 skip_env "MDS doesn't support rename stats"
13967
13968         local testdir1=$DIR/${tdir}/stats_testdir1
13969         local testdir2=$DIR/${tdir}/stats_testdir2
13970         mkdir -p $DIR/${tdir}
13971
13972         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
13973
13974         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
13975         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
13976
13977         createmany -o $testdir1/test 512 || error "createmany failed"
13978
13979         # check samedir rename size
13980         mv ${testdir1}/test0 ${testdir1}/test_0
13981
13982         local testdir1_size=$(ls -l $DIR/${tdir} |
13983                 awk '/stats_testdir1/ {print $5}')
13984         local testdir2_size=$(ls -l $DIR/${tdir} |
13985                 awk '/stats_testdir2/ {print $5}')
13986
13987         testdir1_size=$(order_2 $testdir1_size)
13988         testdir2_size=$(order_2 $testdir2_size)
13989
13990         testdir1_size=$(size_in_KMGT $testdir1_size)
13991         testdir2_size=$(size_in_KMGT $testdir2_size)
13992
13993         echo "source rename dir size: ${testdir1_size}"
13994         echo "target rename dir size: ${testdir2_size}"
13995
13996         local cmd="do_facet $SINGLEMDS $LCTL "
13997         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
13998
13999         eval $cmd || error "$cmd failed"
14000         local samedir=$($cmd | grep 'same_dir')
14001         local same_sample=$(get_rename_size $testdir1_size)
14002         [ -z "$samedir" ] && error "samedir_rename_size count error"
14003         [[ $same_sample -eq 1 ]] ||
14004                 error "samedir_rename_size error $same_sample"
14005         echo "Check same dir rename stats success"
14006
14007         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14008
14009         # check crossdir rename size
14010         mv ${testdir1}/test_0 ${testdir2}/test_0
14011
14012         testdir1_size=$(ls -l $DIR/${tdir} |
14013                 awk '/stats_testdir1/ {print $5}')
14014         testdir2_size=$(ls -l $DIR/${tdir} |
14015                 awk '/stats_testdir2/ {print $5}')
14016
14017         testdir1_size=$(order_2 $testdir1_size)
14018         testdir2_size=$(order_2 $testdir2_size)
14019
14020         testdir1_size=$(size_in_KMGT $testdir1_size)
14021         testdir2_size=$(size_in_KMGT $testdir2_size)
14022
14023         echo "source rename dir size: ${testdir1_size}"
14024         echo "target rename dir size: ${testdir2_size}"
14025
14026         eval $cmd || error "$cmd failed"
14027         local crossdir=$($cmd | grep 'crossdir')
14028         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
14029         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
14030         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
14031         [[ $src_sample -eq 1 ]] ||
14032                 error "crossdir_rename_size error $src_sample"
14033         [[ $tgt_sample -eq 1 ]] ||
14034                 error "crossdir_rename_size error $tgt_sample"
14035         echo "Check cross dir rename stats success"
14036         rm -rf $DIR/${tdir}
14037 }
14038 run_test 133d "Verifying rename_stats ========================================"
14039
14040 test_133e() {
14041         remote_mds_nodsh && skip "remote MDS with nodsh"
14042         remote_ost_nodsh && skip "remote OST with nodsh"
14043         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14044
14045         local testdir=$DIR/${tdir}/stats_testdir
14046         local ctr f0 f1 bs=32768 count=42 sum
14047
14048         mkdir -p ${testdir} || error "mkdir failed"
14049
14050         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
14051
14052         for ctr in {write,read}_bytes; do
14053                 sync
14054                 cancel_lru_locks osc
14055
14056                 do_facet ost1 $LCTL set_param -n \
14057                         "obdfilter.*.exports.clear=clear"
14058
14059                 if [ $ctr = write_bytes ]; then
14060                         f0=/dev/zero
14061                         f1=${testdir}/${tfile}
14062                 else
14063                         f0=${testdir}/${tfile}
14064                         f1=/dev/null
14065                 fi
14066
14067                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
14068                         error "dd failed"
14069                 sync
14070                 cancel_lru_locks osc
14071
14072                 sum=$(do_facet ost1 $LCTL get_param \
14073                         "obdfilter.*.exports.*.stats" |
14074                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
14075                                 $1 == ctr { sum += $7 }
14076                                 END { printf("%0.0f", sum) }')
14077
14078                 if ((sum != bs * count)); then
14079                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
14080                 fi
14081         done
14082
14083         rm -rf $DIR/${tdir}
14084 }
14085 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
14086
14087 test_133f() {
14088         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
14089                 skip "too old lustre for get_param -R ($facet_ver)"
14090
14091         # verifying readability.
14092         $LCTL get_param -R '*' &> /dev/null
14093
14094         # Verifing writability with badarea_io.
14095         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14096         local skipped_params='force_lbug|changelog_mask|daemon_file'
14097         $LCTL list_param -FR '*' | grep '=' | tr -d = |
14098                 egrep -v "$skipped_params" |
14099                 xargs -n 1 find $proc_dirs -name |
14100                 xargs -n 1 badarea_io ||
14101                 error "client badarea_io failed"
14102
14103         # remount the FS in case writes/reads /proc break the FS
14104         cleanup || error "failed to unmount"
14105         setup || error "failed to setup"
14106 }
14107 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
14108
14109 test_133g() {
14110         remote_mds_nodsh && skip "remote MDS with nodsh"
14111         remote_ost_nodsh && skip "remote OST with nodsh"
14112
14113         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14114         local skipped_params="'force_lbug|changelog_mask|daemon_file'"
14115         local facet
14116         for facet in mds1 ost1; do
14117                 local facet_ver=$(lustre_version_code $facet)
14118                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
14119                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
14120                 else
14121                         log "$facet: too old lustre for get_param -R"
14122                 fi
14123                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
14124                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
14125                                 tr -d = | egrep -v $skipped_params |
14126                                 xargs -n 1 find $proc_dirs -name |
14127                                 xargs -n 1 badarea_io" ||
14128                                         error "$facet badarea_io failed"
14129                 else
14130                         skip_noexit "$facet: too old lustre for get_param -R"
14131                 fi
14132         done
14133
14134         # remount the FS in case writes/reads /proc break the FS
14135         cleanup || error "failed to unmount"
14136         setup || error "failed to setup"
14137 }
14138 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
14139
14140 test_133h() {
14141         remote_mds_nodsh && skip "remote MDS with nodsh"
14142         remote_ost_nodsh && skip "remote OST with nodsh"
14143         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
14144                 skip "Need MDS version at least 2.9.54"
14145
14146         local facet
14147         for facet in client mds1 ost1; do
14148                 # Get the list of files that are missing the terminating newline
14149                 local plist=$(do_facet $facet
14150                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
14151                 local ent
14152                 for ent in $plist; do
14153                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
14154                                 awk -v FS='\v' -v RS='\v\v' \
14155                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
14156                                         print FILENAME}'" 2>/dev/null)
14157                         [ -z $missing ] || {
14158                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
14159                                 error "file does not end with newline: $facet-$ent"
14160                         }
14161                 done
14162         done
14163 }
14164 run_test 133h "Proc files should end with newlines"
14165
14166 test_134a() {
14167         remote_mds_nodsh && skip "remote MDS with nodsh"
14168         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14169                 skip "Need MDS version at least 2.7.54"
14170
14171         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14172         cancel_lru_locks mdc
14173
14174         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
14175         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14176         [ $unused -eq 0 ] || error "$unused locks are not cleared"
14177
14178         local nr=1000
14179         createmany -o $DIR/$tdir/f $nr ||
14180                 error "failed to create $nr files in $DIR/$tdir"
14181         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14182
14183         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
14184         do_facet mds1 $LCTL set_param fail_loc=0x327
14185         do_facet mds1 $LCTL set_param fail_val=500
14186         touch $DIR/$tdir/m
14187
14188         echo "sleep 10 seconds ..."
14189         sleep 10
14190         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
14191
14192         do_facet mds1 $LCTL set_param fail_loc=0
14193         do_facet mds1 $LCTL set_param fail_val=0
14194         [ $lck_cnt -lt $unused ] ||
14195                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
14196
14197         rm $DIR/$tdir/m
14198         unlinkmany $DIR/$tdir/f $nr
14199 }
14200 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
14201
14202 test_134b() {
14203         remote_mds_nodsh && skip "remote MDS with nodsh"
14204         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14205                 skip "Need MDS version at least 2.7.54"
14206
14207         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14208         cancel_lru_locks mdc
14209
14210         local low_wm=$(do_facet mds1 $LCTL get_param -n \
14211                         ldlm.lock_reclaim_threshold_mb)
14212         # disable reclaim temporarily
14213         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
14214
14215         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
14216         do_facet mds1 $LCTL set_param fail_loc=0x328
14217         do_facet mds1 $LCTL set_param fail_val=500
14218
14219         $LCTL set_param debug=+trace
14220
14221         local nr=600
14222         createmany -o $DIR/$tdir/f $nr &
14223         local create_pid=$!
14224
14225         echo "Sleep $TIMEOUT seconds ..."
14226         sleep $TIMEOUT
14227         if ! ps -p $create_pid  > /dev/null 2>&1; then
14228                 do_facet mds1 $LCTL set_param fail_loc=0
14229                 do_facet mds1 $LCTL set_param fail_val=0
14230                 do_facet mds1 $LCTL set_param \
14231                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
14232                 error "createmany finished incorrectly!"
14233         fi
14234         do_facet mds1 $LCTL set_param fail_loc=0
14235         do_facet mds1 $LCTL set_param fail_val=0
14236         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
14237         wait $create_pid || return 1
14238
14239         unlinkmany $DIR/$tdir/f $nr
14240 }
14241 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
14242
14243 test_135() {
14244         remote_mds_nodsh && skip "remote MDS with nodsh"
14245         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14246                 skip "Need MDS version at least 2.13.50"
14247         local fname
14248
14249         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14250
14251 #define OBD_FAIL_PLAIN_RECORDS 0x1319
14252         #set only one record at plain llog
14253         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
14254
14255         #fill already existed plain llog each 64767
14256         #wrapping whole catalog
14257         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14258
14259         createmany -o $DIR/$tdir/$tfile_ 64700
14260         for (( i = 0; i < 64700; i = i + 2 ))
14261         do
14262                 rm $DIR/$tdir/$tfile_$i &
14263                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14264                 local pid=$!
14265                 wait $pid
14266         done
14267
14268         #waiting osp synchronization
14269         wait_delete_completed
14270 }
14271 run_test 135 "Race catalog processing"
14272
14273 test_136() {
14274         remote_mds_nodsh && skip "remote MDS with nodsh"
14275         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14276                 skip "Need MDS version at least 2.13.50"
14277         local fname
14278
14279         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14280         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
14281         #set only one record at plain llog
14282 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
14283         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
14284
14285         #fill already existed 2 plain llogs each 64767
14286         #wrapping whole catalog
14287         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14288         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
14289         wait_delete_completed
14290
14291         createmany -o $DIR/$tdir/$tfile_ 10
14292         sleep 25
14293
14294         do_facet $SINGLEMDS $LCTL set_param fail_val=3
14295         for (( i = 0; i < 10; i = i + 3 ))
14296         do
14297                 rm $DIR/$tdir/$tfile_$i &
14298                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14299                 local pid=$!
14300                 wait $pid
14301                 sleep 7
14302                 rm $DIR/$tdir/$tfile_$((i + 2)) &
14303         done
14304
14305         #waiting osp synchronization
14306         wait_delete_completed
14307 }
14308 run_test 136 "Race catalog processing 2"
14309
14310 test_140() { #bug-17379
14311         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14312
14313         test_mkdir $DIR/$tdir
14314         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
14315         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
14316
14317         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
14318         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
14319         local i=0
14320         while i=$((i + 1)); do
14321                 test_mkdir $i
14322                 cd $i || error "Changing to $i"
14323                 ln -s ../stat stat || error "Creating stat symlink"
14324                 # Read the symlink until ELOOP present,
14325                 # not LBUGing the system is considered success,
14326                 # we didn't overrun the stack.
14327                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
14328                 if [ $ret -ne 0 ]; then
14329                         if [ $ret -eq 40 ]; then
14330                                 break  # -ELOOP
14331                         else
14332                                 error "Open stat symlink"
14333                                         return
14334                         fi
14335                 fi
14336         done
14337         i=$((i - 1))
14338         echo "The symlink depth = $i"
14339         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
14340                 error "Invalid symlink depth"
14341
14342         # Test recursive symlink
14343         ln -s symlink_self symlink_self
14344         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
14345         echo "open symlink_self returns $ret"
14346         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
14347 }
14348 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
14349
14350 test_150a() {
14351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14352
14353         local TF="$TMP/$tfile"
14354
14355         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14356         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14357         cp $TF $DIR/$tfile
14358         cancel_lru_locks $OSC
14359         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
14360         remount_client $MOUNT
14361         df -P $MOUNT
14362         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
14363
14364         $TRUNCATE $TF 6000
14365         $TRUNCATE $DIR/$tfile 6000
14366         cancel_lru_locks $OSC
14367         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
14368
14369         echo "12345" >>$TF
14370         echo "12345" >>$DIR/$tfile
14371         cancel_lru_locks $OSC
14372         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
14373
14374         echo "12345" >>$TF
14375         echo "12345" >>$DIR/$tfile
14376         cancel_lru_locks $OSC
14377         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
14378 }
14379 run_test 150a "truncate/append tests"
14380
14381 test_150b() {
14382         check_set_fallocate_or_skip
14383
14384         touch $DIR/$tfile
14385         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14386         check_fallocate $DIR/$tfile || error "fallocate failed"
14387 }
14388 run_test 150b "Verify fallocate (prealloc) functionality"
14389
14390 test_150bb() {
14391         check_set_fallocate_or_skip
14392
14393         touch $DIR/$tfile
14394         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14395         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=20 || error "dd failed"
14396         > $DIR/$tfile
14397         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14398         # precomputed md5sum for 20MB of zeroes
14399         local expect="8f4e33f3dc3e414ff94e5fb6905cba8c"
14400         local sum=($(md5sum $DIR/$tfile))
14401
14402         [[ "${sum[0]}" == "$expect" ]] || error "fallocate unwritten is not zero"
14403
14404         check_set_fallocate 1
14405
14406         > $DIR/$tfile
14407         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14408         sum=($(md5sum $DIR/$tfile))
14409
14410         [[ "${sum[0]}" == "$expect" ]] || error "fallocate zero is not zero"
14411 }
14412 run_test 150bb "Verify fallocate modes both zero space"
14413
14414 test_150c() {
14415         check_set_fallocate_or_skip
14416         local striping="-c2"
14417
14418         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14419         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tfile || error "setstripe failed"
14420         fallocate -l ${OSTCOUNT}m $DIR/$tfile || error "fallocate failed"
14421         local bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14422         local want=$((OSTCOUNT * 1048576))
14423
14424         # Must allocate all requested space, not more than 5% extra
14425         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14426                 error "bytes $bytes is not $want"
14427
14428         rm -f $DIR/$tfile
14429
14430         echo "verify fallocate on PFL file"
14431
14432         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14433
14434         $LFS setstripe -E1M $striping -E16M -c3 -Eeof -c 4 $DIR/$tfile ||
14435                 error "Create $DIR/$tfile failed"
14436         fallocate -l $((1048576 * 512)) $DIR/$tfile ||
14437                         error "fallocate failed"
14438         bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14439         want=$((512 * 1048576))
14440
14441         # Must allocate all requested space, not more than 5% extra
14442         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14443                 error "bytes $bytes is not $want"
14444 }
14445 run_test 150c "Verify fallocate Size and Blocks"
14446
14447 test_150d() {
14448         check_set_fallocate_or_skip
14449         local striping="-c2"
14450
14451         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14452
14453         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14454         $LFS setstripe -E1M $striping -E eof -c $OSTCOUNT -S1M $DIR/$tdir ||
14455                 error "setstripe failed"
14456         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
14457         local bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
14458         local want=$((OSTCOUNT * 1048576))
14459
14460         # Must allocate all requested space, not more than 5% extra
14461         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14462                 error "bytes $bytes is not $want"
14463 }
14464 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
14465
14466 test_150e() {
14467         check_set_fallocate_or_skip
14468
14469         echo "df before:"
14470         $LFS df
14471         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14472         $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14473                 error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14474
14475         # Find OST with Minimum Size
14476         min_size_ost=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14477                        sort -un | head -1)
14478
14479         # Get 100MB per OST of the available space to reduce run time
14480         # else 60% of the available space if we are running SLOW tests
14481         if [ $SLOW == "no" ]; then
14482                 local space=$((1024 * 100 * OSTCOUNT))
14483         else
14484                 local space=$(((min_size_ost * 60)/100 * OSTCOUNT))
14485         fi
14486
14487         fallocate -l${space}k $DIR/$tfile ||
14488                 error "fallocate ${space}k $DIR/$tfile failed"
14489         echo "'fallocate -l ${space}k $DIR/$tfile' succeeded"
14490
14491         # get size immediately after fallocate. This should be correctly
14492         # updated
14493         local size=$(stat -c '%s' $DIR/$tfile)
14494         local used=$(( $(stat -c '%b * %B' $DIR/$tfile) / 1024))
14495
14496         # Sleep for a while for statfs to get updated. And not pull from cache.
14497         sleep 2
14498
14499         echo "df after fallocate:"
14500         $LFS df
14501
14502         (( size / 1024 == space )) || error "size $size != requested $space"
14503         [ "$ost1_FSTYPE" != ldiskfs ] || (( used >= space )) ||
14504                 error "used $used < space $space"
14505
14506         rm $DIR/$tfile || error "rm failed"
14507         sync
14508         wait_delete_completed
14509
14510         echo "df after unlink:"
14511         $LFS df
14512 }
14513 run_test 150e "Verify 60% of available OST space consumed by fallocate"
14514
14515 test_150f() {
14516         local size
14517         local blocks
14518         local want_size_before=20480 # in bytes
14519         local want_blocks_before=40 # 512 sized blocks
14520         local want_blocks_after=24  # 512 sized blocks
14521         local length=$(((want_blocks_before - want_blocks_after) * 512))
14522
14523         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
14524                 skip "need at least 2.14.0 for fallocate punch"
14525
14526         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
14527                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
14528         fi
14529
14530         check_set_fallocate_or_skip
14531         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14532
14533         [[ "x$DOM" == "xyes" ]] &&
14534                 $LFS setstripe -E1M -L mdt -E eof $DIR/$tfile
14535
14536         echo "Verify fallocate punch: Range within the file range"
14537         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
14538                 error "dd failed for bs 4096 and count 5"
14539
14540         # Call fallocate with punch range which is within the file range
14541         fallocate -p --offset 4096 -l $length $DIR/$tfile ||
14542                 error "fallocate failed: offset 4096 and length $length"
14543         # client must see changes immediately after fallocate
14544         size=$(stat -c '%s' $DIR/$tfile)
14545         blocks=$(stat -c '%b' $DIR/$tfile)
14546
14547         # Verify punch worked.
14548         (( blocks == want_blocks_after )) ||
14549                 error "punch failed: blocks $blocks != $want_blocks_after"
14550
14551         (( size == want_size_before )) ||
14552                 error "punch failed: size $size != $want_size_before"
14553
14554         # Verify there is hole in file
14555         local data_off=$(lseek_test -d 4096 $DIR/$tfile)
14556         # precomputed md5sum
14557         local expect="4a9a834a2db02452929c0a348273b4aa"
14558
14559         cksum=($(md5sum $DIR/$tfile))
14560         [[ "${cksum[0]}" == "$expect" ]] ||
14561                 error "unexpected MD5SUM after punch: ${cksum[0]}"
14562
14563         # Start second sub-case for fallocate punch.
14564         echo "Verify fallocate punch: Range overlapping and less than blocksize"
14565         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
14566                 error "dd failed for bs 4096 and count 5"
14567
14568         # Punch range less than block size will have no change in block count
14569         want_blocks_after=40  # 512 sized blocks
14570
14571         # Punch overlaps two blocks and less than blocksize
14572         fallocate -p --offset 4000 -l 3000 $DIR/$tfile ||
14573                 error "fallocate failed: offset 4000 length 3000"
14574         size=$(stat -c '%s' $DIR/$tfile)
14575         blocks=$(stat -c '%b' $DIR/$tfile)
14576
14577         # Verify punch worked.
14578         (( blocks == want_blocks_after )) ||
14579                 error "punch failed: blocks $blocks != $want_blocks_after"
14580
14581         (( size == want_size_before )) ||
14582                 error "punch failed: size $size != $want_size_before"
14583
14584         # Verify if range is really zero'ed out. We expect Zeros.
14585         # precomputed md5sum
14586         expect="c57ec5d769c3dbe3426edc3f7d7e11d3"
14587         cksum=($(md5sum $DIR/$tfile))
14588         [[ "${cksum[0]}" == "$expect" ]] ||
14589                 error "unexpected MD5SUM after punch: ${cksum[0]}"
14590 }
14591 run_test 150f "Verify fallocate punch functionality"
14592
14593 test_150g() {
14594         local space
14595         local size
14596         local blocks
14597         local blocks_after
14598         local size_after
14599         local BS=4096 # Block size in bytes
14600
14601         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
14602                 skip "need at least 2.14.0 for fallocate punch"
14603
14604         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
14605                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
14606         fi
14607
14608         check_set_fallocate_or_skip
14609         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14610
14611         if [[ "x$DOM" == "xyes" ]]; then
14612                 $LFS setstripe -E2M -L mdt -E eof -c${OSTCOUNT} $DIR/$tfile ||
14613                         error "$LFS setstripe DoM + ${OSTCOUNT} OST failed"
14614         else
14615                 $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14616                         error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14617         fi
14618
14619         # Get 100MB per OST of the available space to reduce run time
14620         # else 60% of the available space if we are running SLOW tests
14621         if [ $SLOW == "no" ]; then
14622                 space=$((1024 * 100 * OSTCOUNT))
14623         else
14624                 # Find OST with Minimum Size
14625                 space=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14626                         sort -un | head -1)
14627                 echo "min size OST: $space"
14628                 space=$(((space * 60)/100 * OSTCOUNT))
14629         fi
14630         # space in 1k units, round to 4k blocks
14631         local blkcount=$((space * 1024 / $BS))
14632
14633         echo "Verify fallocate punch: Very large Range"
14634         fallocate -l${space}k $DIR/$tfile ||
14635                 error "fallocate ${space}k $DIR/$tfile failed"
14636         # write 1M at the end, start and in the middle
14637         yes 'A' | dd of=$DIR/$tfile bs=$BS count=256 ||
14638                 error "dd failed: bs $BS count 256"
14639         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount - 256)) count=256 ||
14640                 error "dd failed: bs $BS count 256 seek $((blkcount - 256))"
14641         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount / 2)) count=1024 ||
14642                 error "dd failed: bs $BS count 256 seek $((blkcount / 2))"
14643
14644         # Gather stats.
14645         size=$(stat -c '%s' $DIR/$tfile)
14646
14647         # gather punch length.
14648         local punch_size=$((size - (BS * 2)))
14649
14650         echo "punch_size = $punch_size"
14651         echo "size - punch_size: $((size - punch_size))"
14652         echo "size - punch_size in blocks: $(((size - punch_size)/BS))"
14653
14654         # Call fallocate to punch all except 2 blocks. We leave the
14655         # first and the last block
14656         echo "fallocate -p --offset $BS -l $punch_size $DIR/$tfile"
14657         fallocate -p --offset $BS -l $punch_size $DIR/$tfile ||
14658                 error "fallocate failed: offset $BS length $punch_size"
14659
14660         size_after=$(stat -c '%s' $DIR/$tfile)
14661         blocks_after=$(stat -c '%b' $DIR/$tfile)
14662
14663         # Verify punch worked.
14664         # Size should be kept
14665         (( size == size_after )) ||
14666                 error "punch failed: size $size != $size_after"
14667
14668         # two 4k data blocks to remain plus possible 1 extra extent block
14669         (( blocks_after <= ((BS / 512) * 3) )) ||
14670                 error "too many blocks remains: $blocks_after"
14671
14672         # Verify that file has hole between the first and the last blocks
14673         local hole_start=$(lseek_test -l 0 $DIR/$tfile)
14674         local hole_end=$(lseek_test -d $BS $DIR/$tfile)
14675
14676         echo "Hole at [$hole_start, $hole_end)"
14677         (( hole_start == BS )) ||
14678                 error "no hole at offset $BS after punch"
14679
14680         (( hole_end == BS + punch_size )) ||
14681                 error "data at offset $hole_end < $((BS + punch_size))"
14682 }
14683 run_test 150g "Verify fallocate punch on large range"
14684
14685 #LU-2902 roc_hit was not able to read all values from lproc
14686 function roc_hit_init() {
14687         local list=$(comma_list $(osts_nodes))
14688         local dir=$DIR/$tdir-check
14689         local file=$dir/$tfile
14690         local BEFORE
14691         local AFTER
14692         local idx
14693
14694         test_mkdir $dir
14695         #use setstripe to do a write to every ost
14696         for i in $(seq 0 $((OSTCOUNT-1))); do
14697                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
14698                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
14699                 idx=$(printf %04x $i)
14700                 BEFORE=$(get_osd_param $list *OST*$idx stats |
14701                         awk '$1 == "cache_access" {sum += $7}
14702                                 END { printf("%0.0f", sum) }')
14703
14704                 cancel_lru_locks osc
14705                 cat $file >/dev/null
14706
14707                 AFTER=$(get_osd_param $list *OST*$idx stats |
14708                         awk '$1 == "cache_access" {sum += $7}
14709                                 END { printf("%0.0f", sum) }')
14710
14711                 echo BEFORE:$BEFORE AFTER:$AFTER
14712                 if ! let "AFTER - BEFORE == 4"; then
14713                         rm -rf $dir
14714                         error "roc_hit is not safe to use"
14715                 fi
14716                 rm $file
14717         done
14718
14719         rm -rf $dir
14720 }
14721
14722 function roc_hit() {
14723         local list=$(comma_list $(osts_nodes))
14724         echo $(get_osd_param $list '' stats |
14725                 awk '$1 == "cache_hit" {sum += $7}
14726                         END { printf("%0.0f", sum) }')
14727 }
14728
14729 function set_cache() {
14730         local on=1
14731
14732         if [ "$2" == "off" ]; then
14733                 on=0;
14734         fi
14735         local list=$(comma_list $(osts_nodes))
14736         set_osd_param $list '' $1_cache_enable $on
14737
14738         cancel_lru_locks osc
14739 }
14740
14741 test_151() {
14742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14743         remote_ost_nodsh && skip "remote OST with nodsh"
14744
14745         local CPAGES=3
14746         local list=$(comma_list $(osts_nodes))
14747
14748         # check whether obdfilter is cache capable at all
14749         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
14750                 skip "not cache-capable obdfilter"
14751         fi
14752
14753         # check cache is enabled on all obdfilters
14754         if get_osd_param $list '' read_cache_enable | grep 0; then
14755                 skip "oss cache is disabled"
14756         fi
14757
14758         set_osd_param $list '' writethrough_cache_enable 1
14759
14760         # check write cache is enabled on all obdfilters
14761         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
14762                 skip "oss write cache is NOT enabled"
14763         fi
14764
14765         roc_hit_init
14766
14767         #define OBD_FAIL_OBD_NO_LRU  0x609
14768         do_nodes $list $LCTL set_param fail_loc=0x609
14769
14770         # pages should be in the case right after write
14771         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
14772                 error "dd failed"
14773
14774         local BEFORE=$(roc_hit)
14775         cancel_lru_locks osc
14776         cat $DIR/$tfile >/dev/null
14777         local AFTER=$(roc_hit)
14778
14779         do_nodes $list $LCTL set_param fail_loc=0
14780
14781         if ! let "AFTER - BEFORE == CPAGES"; then
14782                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
14783         fi
14784
14785         cancel_lru_locks osc
14786         # invalidates OST cache
14787         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
14788         set_osd_param $list '' read_cache_enable 0
14789         cat $DIR/$tfile >/dev/null
14790
14791         # now data shouldn't be found in the cache
14792         BEFORE=$(roc_hit)
14793         cancel_lru_locks osc
14794         cat $DIR/$tfile >/dev/null
14795         AFTER=$(roc_hit)
14796         if let "AFTER - BEFORE != 0"; then
14797                 error "IN CACHE: before: $BEFORE, after: $AFTER"
14798         fi
14799
14800         set_osd_param $list '' read_cache_enable 1
14801         rm -f $DIR/$tfile
14802 }
14803 run_test 151 "test cache on oss and controls ==============================="
14804
14805 test_152() {
14806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14807
14808         local TF="$TMP/$tfile"
14809
14810         # simulate ENOMEM during write
14811 #define OBD_FAIL_OST_NOMEM      0x226
14812         lctl set_param fail_loc=0x80000226
14813         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14814         cp $TF $DIR/$tfile
14815         sync || error "sync failed"
14816         lctl set_param fail_loc=0
14817
14818         # discard client's cache
14819         cancel_lru_locks osc
14820
14821         # simulate ENOMEM during read
14822         lctl set_param fail_loc=0x80000226
14823         cmp $TF $DIR/$tfile || error "cmp failed"
14824         lctl set_param fail_loc=0
14825
14826         rm -f $TF
14827 }
14828 run_test 152 "test read/write with enomem ============================"
14829
14830 test_153() {
14831         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
14832 }
14833 run_test 153 "test if fdatasync does not crash ======================="
14834
14835 dot_lustre_fid_permission_check() {
14836         local fid=$1
14837         local ffid=$MOUNT/.lustre/fid/$fid
14838         local test_dir=$2
14839
14840         echo "stat fid $fid"
14841         stat $ffid > /dev/null || error "stat $ffid failed."
14842         echo "touch fid $fid"
14843         touch $ffid || error "touch $ffid failed."
14844         echo "write to fid $fid"
14845         cat /etc/hosts > $ffid || error "write $ffid failed."
14846         echo "read fid $fid"
14847         diff /etc/hosts $ffid || error "read $ffid failed."
14848         echo "append write to fid $fid"
14849         cat /etc/hosts >> $ffid || error "append write $ffid failed."
14850         echo "rename fid $fid"
14851         mv $ffid $test_dir/$tfile.1 &&
14852                 error "rename $ffid to $tfile.1 should fail."
14853         touch $test_dir/$tfile.1
14854         mv $test_dir/$tfile.1 $ffid &&
14855                 error "rename $tfile.1 to $ffid should fail."
14856         rm -f $test_dir/$tfile.1
14857         echo "truncate fid $fid"
14858         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
14859         echo "link fid $fid"
14860         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
14861         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
14862                 echo "setfacl fid $fid"
14863                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
14864                 echo "getfacl fid $fid"
14865                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
14866         fi
14867         echo "unlink fid $fid"
14868         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
14869         echo "mknod fid $fid"
14870         mknod $ffid c 1 3 && error "mknod $ffid should fail."
14871
14872         fid=[0xf00000400:0x1:0x0]
14873         ffid=$MOUNT/.lustre/fid/$fid
14874
14875         echo "stat non-exist fid $fid"
14876         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
14877         echo "write to non-exist fid $fid"
14878         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
14879         echo "link new fid $fid"
14880         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
14881
14882         mkdir -p $test_dir/$tdir
14883         touch $test_dir/$tdir/$tfile
14884         fid=$($LFS path2fid $test_dir/$tdir)
14885         rc=$?
14886         [ $rc -ne 0 ] &&
14887                 error "error: could not get fid for $test_dir/$dir/$tfile."
14888
14889         ffid=$MOUNT/.lustre/fid/$fid
14890
14891         echo "ls $fid"
14892         ls $ffid > /dev/null || error "ls $ffid failed."
14893         echo "touch $fid/$tfile.1"
14894         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
14895
14896         echo "touch $MOUNT/.lustre/fid/$tfile"
14897         touch $MOUNT/.lustre/fid/$tfile && \
14898                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
14899
14900         echo "setxattr to $MOUNT/.lustre/fid"
14901         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
14902
14903         echo "listxattr for $MOUNT/.lustre/fid"
14904         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
14905
14906         echo "delxattr from $MOUNT/.lustre/fid"
14907         setfattr -x trusted.name1 $MOUNT/.lustre/fid
14908
14909         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
14910         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
14911                 error "touch invalid fid should fail."
14912
14913         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
14914         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
14915                 error "touch non-normal fid should fail."
14916
14917         echo "rename $tdir to $MOUNT/.lustre/fid"
14918         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
14919                 error "rename to $MOUNT/.lustre/fid should fail."
14920
14921         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
14922         then            # LU-3547
14923                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
14924                 local new_obf_mode=777
14925
14926                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
14927                 chmod $new_obf_mode $DIR/.lustre/fid ||
14928                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
14929
14930                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
14931                 [ $obf_mode -eq $new_obf_mode ] ||
14932                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
14933
14934                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
14935                 chmod $old_obf_mode $DIR/.lustre/fid ||
14936                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
14937         fi
14938
14939         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
14940         fid=$($LFS path2fid $test_dir/$tfile-2)
14941
14942         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
14943         then # LU-5424
14944                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
14945                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
14946                         error "create lov data thru .lustre failed"
14947         fi
14948         echo "cp /etc/passwd $test_dir/$tfile-2"
14949         cp /etc/passwd $test_dir/$tfile-2 ||
14950                 error "copy to $test_dir/$tfile-2 failed."
14951         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
14952         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
14953                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
14954
14955         rm -rf $test_dir/tfile.lnk
14956         rm -rf $test_dir/$tfile-2
14957 }
14958
14959 test_154A() {
14960         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
14961                 skip "Need MDS version at least 2.4.1"
14962
14963         local tf=$DIR/$tfile
14964         touch $tf
14965
14966         local fid=$($LFS path2fid $tf)
14967         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
14968
14969         # check that we get the same pathname back
14970         local rootpath
14971         local found
14972         for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do
14973                 echo "$rootpath $fid"
14974                 found=$($LFS fid2path $rootpath "$fid")
14975                 [ -z "$found" ] && error "fid2path unable to get '$fid' path"
14976                 [ "$found" == "$tf" ] || error "fid2path $found != $tf"
14977         done
14978
14979         # check wrong root path format
14980         rootpath=$MOUNT"_wrong"
14981         found=$($LFS fid2path $rootpath "$fid")
14982         [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)"
14983 }
14984 run_test 154A "lfs path2fid and fid2path basic checks"
14985
14986 test_154B() {
14987         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
14988                 skip "Need MDS version at least 2.4.1"
14989
14990         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
14991         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
14992         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
14993         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
14994
14995         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
14996         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
14997
14998         # check that we get the same pathname
14999         echo "PFID: $PFID, name: $name"
15000         local FOUND=$($LFS fid2path $MOUNT "$PFID")
15001         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
15002         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
15003                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
15004
15005         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
15006 }
15007 run_test 154B "verify the ll_decode_linkea tool"
15008
15009 test_154a() {
15010         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15011         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15012         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
15013                 skip "Need MDS version at least 2.2.51"
15014         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
15015
15016         cp /etc/hosts $DIR/$tfile
15017
15018         fid=$($LFS path2fid $DIR/$tfile)
15019         rc=$?
15020         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
15021
15022         dot_lustre_fid_permission_check "$fid" $DIR ||
15023                 error "dot lustre permission check $fid failed"
15024
15025         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
15026
15027         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
15028
15029         touch $MOUNT/.lustre/file &&
15030                 error "creation is not allowed under .lustre"
15031
15032         mkdir $MOUNT/.lustre/dir &&
15033                 error "mkdir is not allowed under .lustre"
15034
15035         rm -rf $DIR/$tfile
15036 }
15037 run_test 154a "Open-by-FID"
15038
15039 test_154b() {
15040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15041         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15042         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15043         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
15044                 skip "Need MDS version at least 2.2.51"
15045
15046         local remote_dir=$DIR/$tdir/remote_dir
15047         local MDTIDX=1
15048         local rc=0
15049
15050         mkdir -p $DIR/$tdir
15051         $LFS mkdir -i $MDTIDX $remote_dir ||
15052                 error "create remote directory failed"
15053
15054         cp /etc/hosts $remote_dir/$tfile
15055
15056         fid=$($LFS path2fid $remote_dir/$tfile)
15057         rc=$?
15058         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
15059
15060         dot_lustre_fid_permission_check "$fid" $remote_dir ||
15061                 error "dot lustre permission check $fid failed"
15062         rm -rf $DIR/$tdir
15063 }
15064 run_test 154b "Open-by-FID for remote directory"
15065
15066 test_154c() {
15067         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15068                 skip "Need MDS version at least 2.4.1"
15069
15070         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
15071         local FID1=$($LFS path2fid $DIR/$tfile.1)
15072         local FID2=$($LFS path2fid $DIR/$tfile.2)
15073         local FID3=$($LFS path2fid $DIR/$tfile.3)
15074
15075         local N=1
15076         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
15077                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
15078                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
15079                 local want=FID$N
15080                 [ "$FID" = "${!want}" ] ||
15081                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
15082                 N=$((N + 1))
15083         done
15084
15085         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
15086         do
15087                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
15088                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
15089                 N=$((N + 1))
15090         done
15091 }
15092 run_test 154c "lfs path2fid and fid2path multiple arguments"
15093
15094 test_154d() {
15095         remote_mds_nodsh && skip "remote MDS with nodsh"
15096         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
15097                 skip "Need MDS version at least 2.5.53"
15098
15099         if remote_mds; then
15100                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
15101         else
15102                 nid="0@lo"
15103         fi
15104         local proc_ofile="mdt.*.exports.'$nid'.open_files"
15105         local fd
15106         local cmd
15107
15108         rm -f $DIR/$tfile
15109         touch $DIR/$tfile
15110
15111         local fid=$($LFS path2fid $DIR/$tfile)
15112         # Open the file
15113         fd=$(free_fd)
15114         cmd="exec $fd<$DIR/$tfile"
15115         eval $cmd
15116         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
15117         echo "$fid_list" | grep "$fid"
15118         rc=$?
15119
15120         cmd="exec $fd>/dev/null"
15121         eval $cmd
15122         if [ $rc -ne 0 ]; then
15123                 error "FID $fid not found in open files list $fid_list"
15124         fi
15125 }
15126 run_test 154d "Verify open file fid"
15127
15128 test_154e()
15129 {
15130         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
15131                 skip "Need MDS version at least 2.6.50"
15132
15133         if ls -a $MOUNT | grep -q '^\.lustre$'; then
15134                 error ".lustre returned by readdir"
15135         fi
15136 }
15137 run_test 154e ".lustre is not returned by readdir"
15138
15139 test_154f() {
15140         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15141
15142         # create parent directory on a single MDT to avoid cross-MDT hardlinks
15143         mkdir_on_mdt0 $DIR/$tdir
15144         # test dirs inherit from its stripe
15145         mkdir -p $DIR/$tdir/foo1 || error "mkdir error"
15146         mkdir -p $DIR/$tdir/foo2 || error "mkdir error"
15147         cp /etc/hosts $DIR/$tdir/foo1/$tfile
15148         ln $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/link
15149         touch $DIR/f
15150
15151         # get fid of parents
15152         local FID0=$($LFS path2fid $DIR/$tdir)
15153         local FID1=$($LFS path2fid $DIR/$tdir/foo1)
15154         local FID2=$($LFS path2fid $DIR/$tdir/foo2)
15155         local FID3=$($LFS path2fid $DIR)
15156
15157         # check that path2fid --parents returns expected <parent_fid>/name
15158         # 1) test for a directory (single parent)
15159         local parent=$($LFS path2fid --parents $DIR/$tdir/foo1)
15160         [ "$parent" == "$FID0/foo1" ] ||
15161                 error "expected parent: $FID0/foo1, got: $parent"
15162
15163         # 2) test for a file with nlink > 1 (multiple parents)
15164         parent=$($LFS path2fid --parents $DIR/$tdir/foo1/$tfile)
15165         echo "$parent" | grep -F "$FID1/$tfile" ||
15166                 error "$FID1/$tfile not returned in parent list"
15167         echo "$parent" | grep -F "$FID2/link" ||
15168                 error "$FID2/link not returned in parent list"
15169
15170         # 3) get parent by fid
15171         local file_fid=$($LFS path2fid $DIR/$tdir/foo1/$tfile)
15172         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15173         echo "$parent" | grep -F "$FID1/$tfile" ||
15174                 error "$FID1/$tfile not returned in parent list (by fid)"
15175         echo "$parent" | grep -F "$FID2/link" ||
15176                 error "$FID2/link not returned in parent list (by fid)"
15177
15178         # 4) test for entry in root directory
15179         parent=$($LFS path2fid --parents $DIR/f)
15180         echo "$parent" | grep -F "$FID3/f" ||
15181                 error "$FID3/f not returned in parent list"
15182
15183         # 5) test it on root directory
15184         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
15185                 error "$MOUNT should not have parents"
15186
15187         # enable xattr caching and check that linkea is correctly updated
15188         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
15189         save_lustre_params client "llite.*.xattr_cache" > $save
15190         lctl set_param llite.*.xattr_cache 1
15191
15192         # 6.1) linkea update on rename
15193         mv $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/$tfile.moved
15194
15195         # get parents by fid
15196         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15197         # foo1 should no longer be returned in parent list
15198         echo "$parent" | grep -F "$FID1" &&
15199                 error "$FID1 should no longer be in parent list"
15200         # the new path should appear
15201         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
15202                 error "$FID2/$tfile.moved is not in parent list"
15203
15204         # 6.2) linkea update on unlink
15205         rm -f $DIR/$tdir/foo2/link
15206         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15207         # foo2/link should no longer be returned in parent list
15208         echo "$parent" | grep -F "$FID2/link" &&
15209                 error "$FID2/link should no longer be in parent list"
15210         true
15211
15212         rm -f $DIR/f
15213         restore_lustre_params < $save
15214         rm -f $save
15215 }
15216 run_test 154f "get parent fids by reading link ea"
15217
15218 test_154g()
15219 {
15220         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15221         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
15222            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
15223                 skip "Need MDS version at least 2.6.92"
15224
15225         mkdir_on_mdt0 $DIR/$tdir
15226         llapi_fid_test -d $DIR/$tdir
15227 }
15228 run_test 154g "various llapi FID tests"
15229
15230 test_155_small_load() {
15231     local temp=$TMP/$tfile
15232     local file=$DIR/$tfile
15233
15234     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
15235         error "dd of=$temp bs=6096 count=1 failed"
15236     cp $temp $file
15237     cancel_lru_locks $OSC
15238     cmp $temp $file || error "$temp $file differ"
15239
15240     $TRUNCATE $temp 6000
15241     $TRUNCATE $file 6000
15242     cmp $temp $file || error "$temp $file differ (truncate1)"
15243
15244     echo "12345" >>$temp
15245     echo "12345" >>$file
15246     cmp $temp $file || error "$temp $file differ (append1)"
15247
15248     echo "12345" >>$temp
15249     echo "12345" >>$file
15250     cmp $temp $file || error "$temp $file differ (append2)"
15251
15252     rm -f $temp $file
15253     true
15254 }
15255
15256 test_155_big_load() {
15257         remote_ost_nodsh && skip "remote OST with nodsh"
15258
15259         local temp=$TMP/$tfile
15260         local file=$DIR/$tfile
15261
15262         free_min_max
15263         local cache_size=$(do_facet ost$((MAXI+1)) \
15264                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
15265         local large_file_size=$((cache_size * 2))
15266
15267         echo "OSS cache size: $cache_size KB"
15268         echo "Large file size: $large_file_size KB"
15269
15270         [ $MAXV -le $large_file_size ] &&
15271                 skip_env "max available OST size needs > $large_file_size KB"
15272
15273         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
15274
15275         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
15276                 error "dd of=$temp bs=$large_file_size count=1k failed"
15277         cp $temp $file
15278         ls -lh $temp $file
15279         cancel_lru_locks osc
15280         cmp $temp $file || error "$temp $file differ"
15281
15282         rm -f $temp $file
15283         true
15284 }
15285
15286 save_writethrough() {
15287         local facets=$(get_facets OST)
15288
15289         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
15290 }
15291
15292 test_155a() {
15293         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15294
15295         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15296
15297         save_writethrough $p
15298
15299         set_cache read on
15300         set_cache writethrough on
15301         test_155_small_load
15302         restore_lustre_params < $p
15303         rm -f $p
15304 }
15305 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
15306
15307 test_155b() {
15308         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15309
15310         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15311
15312         save_writethrough $p
15313
15314         set_cache read on
15315         set_cache writethrough off
15316         test_155_small_load
15317         restore_lustre_params < $p
15318         rm -f $p
15319 }
15320 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
15321
15322 test_155c() {
15323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15324
15325         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15326
15327         save_writethrough $p
15328
15329         set_cache read off
15330         set_cache writethrough on
15331         test_155_small_load
15332         restore_lustre_params < $p
15333         rm -f $p
15334 }
15335 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
15336
15337 test_155d() {
15338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15339
15340         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15341
15342         save_writethrough $p
15343
15344         set_cache read off
15345         set_cache writethrough off
15346         test_155_small_load
15347         restore_lustre_params < $p
15348         rm -f $p
15349 }
15350 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
15351
15352 test_155e() {
15353         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15354
15355         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15356
15357         save_writethrough $p
15358
15359         set_cache read on
15360         set_cache writethrough on
15361         test_155_big_load
15362         restore_lustre_params < $p
15363         rm -f $p
15364 }
15365 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
15366
15367 test_155f() {
15368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15369
15370         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15371
15372         save_writethrough $p
15373
15374         set_cache read on
15375         set_cache writethrough off
15376         test_155_big_load
15377         restore_lustre_params < $p
15378         rm -f $p
15379 }
15380 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
15381
15382 test_155g() {
15383         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15384
15385         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15386
15387         save_writethrough $p
15388
15389         set_cache read off
15390         set_cache writethrough on
15391         test_155_big_load
15392         restore_lustre_params < $p
15393         rm -f $p
15394 }
15395 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
15396
15397 test_155h() {
15398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15399
15400         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15401
15402         save_writethrough $p
15403
15404         set_cache read off
15405         set_cache writethrough off
15406         test_155_big_load
15407         restore_lustre_params < $p
15408         rm -f $p
15409 }
15410 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
15411
15412 test_156() {
15413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15414         remote_ost_nodsh && skip "remote OST with nodsh"
15415         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
15416                 skip "stats not implemented on old servers"
15417         [ "$ost1_FSTYPE" = "zfs" ] &&
15418                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
15419
15420         local CPAGES=3
15421         local BEFORE
15422         local AFTER
15423         local file="$DIR/$tfile"
15424         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15425
15426         save_writethrough $p
15427         roc_hit_init
15428
15429         log "Turn on read and write cache"
15430         set_cache read on
15431         set_cache writethrough on
15432
15433         log "Write data and read it back."
15434         log "Read should be satisfied from the cache."
15435         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15436         BEFORE=$(roc_hit)
15437         cancel_lru_locks osc
15438         cat $file >/dev/null
15439         AFTER=$(roc_hit)
15440         if ! let "AFTER - BEFORE == CPAGES"; then
15441                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
15442         else
15443                 log "cache hits: before: $BEFORE, after: $AFTER"
15444         fi
15445
15446         log "Read again; it should be satisfied from the cache."
15447         BEFORE=$AFTER
15448         cancel_lru_locks osc
15449         cat $file >/dev/null
15450         AFTER=$(roc_hit)
15451         if ! let "AFTER - BEFORE == CPAGES"; then
15452                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
15453         else
15454                 log "cache hits:: before: $BEFORE, after: $AFTER"
15455         fi
15456
15457         log "Turn off the read cache and turn on the write cache"
15458         set_cache read off
15459         set_cache writethrough on
15460
15461         log "Read again; it should be satisfied from the cache."
15462         BEFORE=$(roc_hit)
15463         cancel_lru_locks osc
15464         cat $file >/dev/null
15465         AFTER=$(roc_hit)
15466         if ! let "AFTER - BEFORE == CPAGES"; then
15467                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
15468         else
15469                 log "cache hits:: before: $BEFORE, after: $AFTER"
15470         fi
15471
15472         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15473                 # > 2.12.56 uses pagecache if cached
15474                 log "Read again; it should not be satisfied from the cache."
15475                 BEFORE=$AFTER
15476                 cancel_lru_locks osc
15477                 cat $file >/dev/null
15478                 AFTER=$(roc_hit)
15479                 if ! let "AFTER - BEFORE == 0"; then
15480                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
15481                 else
15482                         log "cache hits:: before: $BEFORE, after: $AFTER"
15483                 fi
15484         fi
15485
15486         log "Write data and read it back."
15487         log "Read should be satisfied from the cache."
15488         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15489         BEFORE=$(roc_hit)
15490         cancel_lru_locks osc
15491         cat $file >/dev/null
15492         AFTER=$(roc_hit)
15493         if ! let "AFTER - BEFORE == CPAGES"; then
15494                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
15495         else
15496                 log "cache hits:: before: $BEFORE, after: $AFTER"
15497         fi
15498
15499         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15500                 # > 2.12.56 uses pagecache if cached
15501                 log "Read again; it should not be satisfied from the cache."
15502                 BEFORE=$AFTER
15503                 cancel_lru_locks osc
15504                 cat $file >/dev/null
15505                 AFTER=$(roc_hit)
15506                 if ! let "AFTER - BEFORE == 0"; then
15507                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
15508                 else
15509                         log "cache hits:: before: $BEFORE, after: $AFTER"
15510                 fi
15511         fi
15512
15513         log "Turn off read and write cache"
15514         set_cache read off
15515         set_cache writethrough off
15516
15517         log "Write data and read it back"
15518         log "It should not be satisfied from the cache."
15519         rm -f $file
15520         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15521         cancel_lru_locks osc
15522         BEFORE=$(roc_hit)
15523         cat $file >/dev/null
15524         AFTER=$(roc_hit)
15525         if ! let "AFTER - BEFORE == 0"; then
15526                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
15527         else
15528                 log "cache hits:: before: $BEFORE, after: $AFTER"
15529         fi
15530
15531         log "Turn on the read cache and turn off the write cache"
15532         set_cache read on
15533         set_cache writethrough off
15534
15535         log "Write data and read it back"
15536         log "It should not be satisfied from the cache."
15537         rm -f $file
15538         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15539         BEFORE=$(roc_hit)
15540         cancel_lru_locks osc
15541         cat $file >/dev/null
15542         AFTER=$(roc_hit)
15543         if ! let "AFTER - BEFORE == 0"; then
15544                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
15545         else
15546                 log "cache hits:: before: $BEFORE, after: $AFTER"
15547         fi
15548
15549         log "Read again; it should be satisfied from the cache."
15550         BEFORE=$(roc_hit)
15551         cancel_lru_locks osc
15552         cat $file >/dev/null
15553         AFTER=$(roc_hit)
15554         if ! let "AFTER - BEFORE == CPAGES"; then
15555                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
15556         else
15557                 log "cache hits:: before: $BEFORE, after: $AFTER"
15558         fi
15559
15560         restore_lustre_params < $p
15561         rm -f $p $file
15562 }
15563 run_test 156 "Verification of tunables"
15564
15565 test_160a() {
15566         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15567         remote_mds_nodsh && skip "remote MDS with nodsh"
15568         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
15569                 skip "Need MDS version at least 2.2.0"
15570
15571         changelog_register || error "changelog_register failed"
15572         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
15573         changelog_users $SINGLEMDS | grep -q $cl_user ||
15574                 error "User $cl_user not found in changelog_users"
15575
15576         mkdir_on_mdt0 $DIR/$tdir
15577
15578         # change something
15579         test_mkdir -p $DIR/$tdir/pics/2008/zachy
15580         changelog_clear 0 || error "changelog_clear failed"
15581         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
15582         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
15583         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15584         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15585         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15586         rm $DIR/$tdir/pics/desktop.jpg
15587
15588         echo "verifying changelog mask"
15589         changelog_chmask "-MKDIR"
15590         changelog_chmask "-CLOSE"
15591
15592         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
15593         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
15594
15595         changelog_chmask "+MKDIR"
15596         changelog_chmask "+CLOSE"
15597
15598         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
15599         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
15600
15601         MKDIRS=$(changelog_dump | grep -c "MKDIR")
15602         CLOSES=$(changelog_dump | grep -c "CLOSE")
15603         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
15604         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
15605
15606         # verify contents
15607         echo "verifying target fid"
15608         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
15609         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
15610         [ "$fidc" == "$fidf" ] ||
15611                 error "changelog '$tfile' fid $fidc != file fid $fidf"
15612         echo "verifying parent fid"
15613         # The FID returned from the Changelog may be the directory shard on
15614         # a different MDT, and not the FID returned by path2fid on the parent.
15615         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
15616         # since this is what will matter when recreating this file in the tree.
15617         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
15618         local pathp=$($LFS fid2path $MOUNT "$fidp")
15619         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
15620                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
15621
15622         echo "getting records for $cl_user"
15623         changelog_users $SINGLEMDS
15624         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
15625         local nclr=3
15626         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
15627                 error "changelog_clear failed"
15628         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
15629         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
15630         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
15631                 error "user index expect $user_rec1 + $nclr != $user_rec2"
15632
15633         local min0_rec=$(changelog_users $SINGLEMDS |
15634                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
15635         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
15636                           awk '{ print $1; exit; }')
15637
15638         changelog_dump | tail -n 5
15639         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
15640         [ $first_rec == $((min0_rec + 1)) ] ||
15641                 error "first index should be $min0_rec + 1 not $first_rec"
15642
15643         # LU-3446 changelog index reset on MDT restart
15644         local cur_rec1=$(changelog_users $SINGLEMDS |
15645                          awk '/^current.index:/ { print $NF }')
15646         changelog_clear 0 ||
15647                 error "clear all changelog records for $cl_user failed"
15648         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
15649         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
15650                 error "Fail to start $SINGLEMDS"
15651         local cur_rec2=$(changelog_users $SINGLEMDS |
15652                          awk '/^current.index:/ { print $NF }')
15653         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
15654         [ $cur_rec1 == $cur_rec2 ] ||
15655                 error "current index should be $cur_rec1 not $cur_rec2"
15656
15657         echo "verifying users from this test are deregistered"
15658         changelog_deregister || error "changelog_deregister failed"
15659         changelog_users $SINGLEMDS | grep -q $cl_user &&
15660                 error "User '$cl_user' still in changelog_users"
15661
15662         # lctl get_param -n mdd.*.changelog_users
15663         # current_index: 144
15664         # ID    index (idle seconds)
15665         # cl3   144   (2) mask=<list>
15666         if [ -z "$(changelog_users $SINGLEMDS | grep -v current.index)" ]; then
15667                 # this is the normal case where all users were deregistered
15668                 # make sure no new records are added when no users are present
15669                 local last_rec1=$(changelog_users $SINGLEMDS |
15670                                   awk '/^current.index:/ { print $NF }')
15671                 touch $DIR/$tdir/chloe
15672                 local last_rec2=$(changelog_users $SINGLEMDS |
15673                                   awk '/^current.index:/ { print $NF }')
15674                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
15675                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
15676         else
15677                 # any changelog users must be leftovers from a previous test
15678                 changelog_users $SINGLEMDS
15679                 echo "other changelog users; can't verify off"
15680         fi
15681 }
15682 run_test 160a "changelog sanity"
15683
15684 test_160b() { # LU-3587
15685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15686         remote_mds_nodsh && skip "remote MDS with nodsh"
15687         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
15688                 skip "Need MDS version at least 2.2.0"
15689
15690         changelog_register || error "changelog_register failed"
15691         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
15692         changelog_users $SINGLEMDS | grep -q $cl_user ||
15693                 error "User '$cl_user' not found in changelog_users"
15694
15695         local longname1=$(str_repeat a 255)
15696         local longname2=$(str_repeat b 255)
15697
15698         cd $DIR
15699         echo "creating very long named file"
15700         touch $longname1 || error "create of '$longname1' failed"
15701         echo "renaming very long named file"
15702         mv $longname1 $longname2
15703
15704         changelog_dump | grep RENME | tail -n 5
15705         rm -f $longname2
15706 }
15707 run_test 160b "Verify that very long rename doesn't crash in changelog"
15708
15709 test_160c() {
15710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15711         remote_mds_nodsh && skip "remote MDS with nodsh"
15712
15713         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
15714                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
15715                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
15716                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
15717
15718         local rc=0
15719
15720         # Registration step
15721         changelog_register || error "changelog_register failed"
15722
15723         rm -rf $DIR/$tdir
15724         mkdir -p $DIR/$tdir
15725         $MCREATE $DIR/$tdir/foo_160c
15726         changelog_chmask "-TRUNC"
15727         $TRUNCATE $DIR/$tdir/foo_160c 200
15728         changelog_chmask "+TRUNC"
15729         $TRUNCATE $DIR/$tdir/foo_160c 199
15730         changelog_dump | tail -n 5
15731         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
15732         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
15733 }
15734 run_test 160c "verify that changelog log catch the truncate event"
15735
15736 test_160d() {
15737         remote_mds_nodsh && skip "remote MDS with nodsh"
15738         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15739         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15740         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
15741                 skip "Need MDS version at least 2.7.60"
15742
15743         # Registration step
15744         changelog_register || error "changelog_register failed"
15745
15746         mkdir -p $DIR/$tdir/migrate_dir
15747         changelog_clear 0 || error "changelog_clear failed"
15748
15749         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
15750         changelog_dump | tail -n 5
15751         local migrates=$(changelog_dump | grep -c "MIGRT")
15752         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
15753 }
15754 run_test 160d "verify that changelog log catch the migrate event"
15755
15756 test_160e() {
15757         remote_mds_nodsh && skip "remote MDS with nodsh"
15758
15759         # Create a user
15760         changelog_register || error "changelog_register failed"
15761
15762         local MDT0=$(facet_svc $SINGLEMDS)
15763         local rc
15764
15765         # No user (expect fail)
15766         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister
15767         rc=$?
15768         if [ $rc -eq 0 ]; then
15769                 error "Should fail without user"
15770         elif [ $rc -ne 4 ]; then
15771                 error "changelog_deregister failed with $rc, expect 4(CMD_HELP)"
15772         fi
15773
15774         # Delete a future user (expect fail)
15775         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
15776         rc=$?
15777         if [ $rc -eq 0 ]; then
15778                 error "Deleted non-existant user cl77"
15779         elif [ $rc -ne 2 ]; then
15780                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
15781         fi
15782
15783         # Clear to a bad index (1 billion should be safe)
15784         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
15785         rc=$?
15786
15787         if [ $rc -eq 0 ]; then
15788                 error "Successfully cleared to invalid CL index"
15789         elif [ $rc -ne 22 ]; then
15790                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
15791         fi
15792 }
15793 run_test 160e "changelog negative testing (should return errors)"
15794
15795 test_160f() {
15796         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15797         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15798                 skip "Need MDS version at least 2.10.56"
15799
15800         local mdts=$(comma_list $(mdts_nodes))
15801
15802         # Create a user
15803         changelog_register || error "first changelog_register failed"
15804         changelog_register || error "second changelog_register failed"
15805         local cl_users
15806         declare -A cl_user1
15807         declare -A cl_user2
15808         local user_rec1
15809         local user_rec2
15810         local i
15811
15812         # generate some changelog records to accumulate on each MDT
15813         # use all_char because created files should be evenly distributed
15814         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
15815                 error "test_mkdir $tdir failed"
15816         log "$(date +%s): creating first files"
15817         for ((i = 0; i < MDSCOUNT * 2; i++)); do
15818                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT)) ||
15819                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT)) failed"
15820         done
15821
15822         # check changelogs have been generated
15823         local start=$SECONDS
15824         local idle_time=$((MDSCOUNT * 5 + 5))
15825         local nbcl=$(changelog_dump | wc -l)
15826         [[ $nbcl -eq 0 ]] && error "no changelogs found"
15827
15828         for param in "changelog_max_idle_time=$idle_time" \
15829                      "changelog_gc=1" \
15830                      "changelog_min_gc_interval=2" \
15831                      "changelog_min_free_cat_entries=3"; do
15832                 local MDT0=$(facet_svc $SINGLEMDS)
15833                 local var="${param%=*}"
15834                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
15835
15836                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
15837                 do_nodes $mdts $LCTL set_param mdd.*.$param
15838         done
15839
15840         # force cl_user2 to be idle (1st part), but also cancel the
15841         # cl_user1 records so that it is not evicted later in the test.
15842         local sleep1=$((idle_time / 2))
15843         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
15844         sleep $sleep1
15845
15846         # simulate changelog catalog almost full
15847         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
15848         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
15849
15850         for i in $(seq $MDSCOUNT); do
15851                 cl_users=(${CL_USERS[mds$i]})
15852                 cl_user1[mds$i]="${cl_users[0]}"
15853                 cl_user2[mds$i]="${cl_users[1]}"
15854
15855                 [ -n "${cl_user1[mds$i]}" ] ||
15856                         error "mds$i: no user registered"
15857                 [ -n "${cl_user2[mds$i]}" ] ||
15858                         error "mds$i: only ${cl_user2[mds$i]} is registered"
15859
15860                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15861                 [ -n "$user_rec1" ] ||
15862                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15863                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
15864                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15865                 [ -n "$user_rec2" ] ||
15866                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15867                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
15868                      "$user_rec1 + 2 == $user_rec2"
15869                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
15870                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
15871                               "$user_rec1 + 2, but is $user_rec2"
15872                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
15873                 [ -n "$user_rec2" ] ||
15874                         error "mds$i: User ${cl_user2[mds$i]} not registered"
15875                 [ $user_rec1 == $user_rec2 ] ||
15876                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
15877                               "$user_rec1, but is $user_rec2"
15878         done
15879
15880         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
15881         local sleep2=$((idle_time - (SECONDS - start) + 1))
15882         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
15883         sleep $sleep2
15884
15885         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
15886         # cl_user1 should be OK because it recently processed records.
15887         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
15888         for ((i = 0; i < MDSCOUNT * 2; i++)); do
15889                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT+2))||
15890                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT+2)) failed"
15891         done
15892
15893         # ensure gc thread is done
15894         for i in $(mdts_nodes); do
15895                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
15896                         error "$i: GC-thread not done"
15897         done
15898
15899         local first_rec
15900         for (( i = 1; i <= MDSCOUNT; i++ )); do
15901                 # check cl_user1 still registered
15902                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
15903                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15904                 # check cl_user2 unregistered
15905                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
15906                         error "mds$i: User ${cl_user2[mds$i]} still registered"
15907
15908                 # check changelogs are present and starting at $user_rec1 + 1
15909                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15910                 [ -n "$user_rec1" ] ||
15911                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15912                 first_rec=$($LFS changelog $(facet_svc mds$i) |
15913                             awk '{ print $1; exit; }')
15914
15915                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
15916                 [ $((user_rec1 + 1)) == $first_rec ] ||
15917                         error "mds$i: rec $first_rec != $user_rec1 + 1"
15918         done
15919 }
15920 run_test 160f "changelog garbage collect (timestamped users)"
15921
15922 test_160g() {
15923         remote_mds_nodsh && skip "remote MDS with nodsh"
15924         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15925                 skip "Need MDS version at least 2.10.56"
15926
15927         local mdts=$(comma_list $(mdts_nodes))
15928
15929         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
15930         do_nodes $mdts $LCTL set_param fail_loc=0x1314
15931
15932         # Create a user
15933         changelog_register || error "first changelog_register failed"
15934         changelog_register || error "second changelog_register failed"
15935         local cl_users
15936         declare -A cl_user1
15937         declare -A cl_user2
15938         local user_rec1
15939         local user_rec2
15940         local i
15941
15942         # generate some changelog records to accumulate on each MDT
15943         # use all_char because created files should be evenly distributed
15944         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
15945                 error "test_mkdir $tdir failed"
15946         for ((i = 0; i < MDSCOUNT; i++)); do
15947                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
15948                         error "create $DIR/$tdir/d$i.1 failed"
15949         done
15950
15951         # check changelogs have been generated
15952         local nbcl=$(changelog_dump | wc -l)
15953         (( $nbcl > 0 )) || error "no changelogs found"
15954
15955         # reduce the max_idle_indexes value to make sure we exceed it
15956         for param in "changelog_max_idle_indexes=1" \
15957                      "changelog_gc=1" \
15958                      "changelog_min_gc_interval=2" \
15959                      "changelog_min_free_cat_entries=3"; do
15960                 local MDT0=$(facet_svc $SINGLEMDS)
15961                 local var="${param%=*}"
15962                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
15963
15964                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
15965                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
15966                         error "unable to set mdd.*.$param"
15967         done
15968
15969         # simulate changelog catalog almost full
15970         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
15971         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
15972
15973         local start=$SECONDS
15974         for i in $(seq $MDSCOUNT); do
15975                 cl_users=(${CL_USERS[mds$i]})
15976                 cl_user1[mds$i]="${cl_users[0]}"
15977                 cl_user2[mds$i]="${cl_users[1]}"
15978
15979                 [ -n "${cl_user1[mds$i]}" ] ||
15980                         error "mds$i: no user registered"
15981                 [ -n "${cl_user2[mds$i]}" ] ||
15982                         error "mds$i: only ${cl_user1[mds$i]} is registered"
15983
15984                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15985                 [ -n "$user_rec1" ] ||
15986                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15987                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
15988                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15989                 [ -n "$user_rec2" ] ||
15990                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15991                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
15992                      "$user_rec1 + 2 == $user_rec2"
15993                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
15994                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
15995                               "$user_rec1 + 2, but is $user_rec2"
15996                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
15997                 [ -n "$user_rec2" ] ||
15998                         error "mds$i: User ${cl_user2[mds$i]} not registered"
15999                 [ $user_rec1 == $user_rec2 ] ||
16000                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16001                               "$user_rec1, but is $user_rec2"
16002         done
16003
16004         # ensure we are past the previous changelog_min_gc_interval set above
16005         local sleep2=$((start + 2 - SECONDS))
16006         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
16007
16008         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
16009         # cl_user1 should be OK because it recently processed records.
16010         for ((i = 0; i < MDSCOUNT; i++)); do
16011                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 $DIR/$tdir/d$i.4 ||
16012                         error "create $DIR/$tdir/d$i.3 failed"
16013         done
16014
16015         # ensure gc thread is done
16016         for i in $(mdts_nodes); do
16017                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
16018                         error "$i: GC-thread not done"
16019         done
16020
16021         local first_rec
16022         for (( i = 1; i <= MDSCOUNT; i++ )); do
16023                 # check cl_user1 still registered
16024                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16025                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16026                 # check cl_user2 unregistered
16027                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16028                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16029
16030                 # check changelogs are present and starting at $user_rec1 + 1
16031                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16032                 [ -n "$user_rec1" ] ||
16033                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16034                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16035                             awk '{ print $1; exit; }')
16036
16037                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
16038                 [ $((user_rec1 + 1)) == $first_rec ] ||
16039                         error "mds$i: rec $first_rec != $user_rec1 + 1"
16040         done
16041 }
16042 run_test 160g "changelog garbage collect (old users)"
16043
16044 test_160h() {
16045         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16046         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16047                 skip "Need MDS version at least 2.10.56"
16048
16049         local mdts=$(comma_list $(mdts_nodes))
16050
16051         # Create a user
16052         changelog_register || error "first changelog_register failed"
16053         changelog_register || error "second changelog_register failed"
16054         local cl_users
16055         declare -A cl_user1
16056         declare -A cl_user2
16057         local user_rec1
16058         local user_rec2
16059         local i
16060
16061         # generate some changelog records to accumulate on each MDT
16062         # use all_char because created files should be evenly distributed
16063         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16064                 error "test_mkdir $tdir failed"
16065         for ((i = 0; i < MDSCOUNT; i++)); do
16066                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16067                         error "create $DIR/$tdir/d$i.1 failed"
16068         done
16069
16070         # check changelogs have been generated
16071         local nbcl=$(changelog_dump | wc -l)
16072         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16073
16074         for param in "changelog_max_idle_time=10" \
16075                      "changelog_gc=1" \
16076                      "changelog_min_gc_interval=2"; do
16077                 local MDT0=$(facet_svc $SINGLEMDS)
16078                 local var="${param%=*}"
16079                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16080
16081                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16082                 do_nodes $mdts $LCTL set_param mdd.*.$param
16083         done
16084
16085         # force cl_user2 to be idle (1st part)
16086         sleep 9
16087
16088         for i in $(seq $MDSCOUNT); do
16089                 cl_users=(${CL_USERS[mds$i]})
16090                 cl_user1[mds$i]="${cl_users[0]}"
16091                 cl_user2[mds$i]="${cl_users[1]}"
16092
16093                 [ -n "${cl_user1[mds$i]}" ] ||
16094                         error "mds$i: no user registered"
16095                 [ -n "${cl_user2[mds$i]}" ] ||
16096                         error "mds$i: only ${cl_user2[mds$i]} is registered"
16097
16098                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16099                 [ -n "$user_rec1" ] ||
16100                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16101                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16102                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16103                 [ -n "$user_rec2" ] ||
16104                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16105                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16106                      "$user_rec1 + 2 == $user_rec2"
16107                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16108                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16109                               "$user_rec1 + 2, but is $user_rec2"
16110                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16111                 [ -n "$user_rec2" ] ||
16112                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16113                 [ $user_rec1 == $user_rec2 ] ||
16114                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16115                               "$user_rec1, but is $user_rec2"
16116         done
16117
16118         # force cl_user2 to be idle (2nd part) and to reach
16119         # changelog_max_idle_time
16120         sleep 2
16121
16122         # force each GC-thread start and block then
16123         # one per MDT/MDD, set fail_val accordingly
16124         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
16125         do_nodes $mdts $LCTL set_param fail_loc=0x1316
16126
16127         # generate more changelogs to trigger fail_loc
16128         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16129                 error "create $DIR/$tdir/${tfile}bis failed"
16130
16131         # stop MDT to stop GC-thread, should be done in back-ground as it will
16132         # block waiting for the thread to be released and exit
16133         declare -A stop_pids
16134         for i in $(seq $MDSCOUNT); do
16135                 stop mds$i &
16136                 stop_pids[mds$i]=$!
16137         done
16138
16139         for i in $(mdts_nodes); do
16140                 local facet
16141                 local nb=0
16142                 local facets=$(facets_up_on_host $i)
16143
16144                 for facet in ${facets//,/ }; do
16145                         if [[ $facet == mds* ]]; then
16146                                 nb=$((nb + 1))
16147                         fi
16148                 done
16149                 # ensure each MDS's gc threads are still present and all in "R"
16150                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
16151                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
16152                         error "$i: expected $nb GC-thread"
16153                 wait_update $i \
16154                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
16155                         "R" 20 ||
16156                         error "$i: GC-thread not found in R-state"
16157                 # check umounts of each MDT on MDS have reached kthread_stop()
16158                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
16159                         error "$i: expected $nb umount"
16160                 wait_update $i \
16161                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
16162                         error "$i: umount not found in D-state"
16163         done
16164
16165         # release all GC-threads
16166         do_nodes $mdts $LCTL set_param fail_loc=0
16167
16168         # wait for MDT stop to complete
16169         for i in $(seq $MDSCOUNT); do
16170                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
16171         done
16172
16173         # XXX
16174         # may try to check if any orphan changelog records are present
16175         # via ldiskfs/zfs and llog_reader...
16176
16177         # re-start/mount MDTs
16178         for i in $(seq $MDSCOUNT); do
16179                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
16180                         error "Fail to start mds$i"
16181         done
16182
16183         local first_rec
16184         for i in $(seq $MDSCOUNT); do
16185                 # check cl_user1 still registered
16186                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16187                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16188                 # check cl_user2 unregistered
16189                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16190                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16191
16192                 # check changelogs are present and starting at $user_rec1 + 1
16193                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16194                 [ -n "$user_rec1" ] ||
16195                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16196                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16197                             awk '{ print $1; exit; }')
16198
16199                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
16200                 [ $((user_rec1 + 1)) == $first_rec ] ||
16201                         error "mds$i: first index should be $user_rec1 + 1, " \
16202                               "but is $first_rec"
16203         done
16204 }
16205 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
16206               "during mount"
16207
16208 test_160i() {
16209
16210         local mdts=$(comma_list $(mdts_nodes))
16211
16212         changelog_register || error "first changelog_register failed"
16213
16214         # generate some changelog records to accumulate on each MDT
16215         # use all_char because created files should be evenly distributed
16216         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16217                 error "test_mkdir $tdir failed"
16218         for ((i = 0; i < MDSCOUNT; i++)); do
16219                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16220                         error "create $DIR/$tdir/d$i.1 failed"
16221         done
16222
16223         # check changelogs have been generated
16224         local nbcl=$(changelog_dump | wc -l)
16225         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16226
16227         # simulate race between register and unregister
16228         # XXX as fail_loc is set per-MDS, with DNE configs the race
16229         # simulation will only occur for one MDT per MDS and for the
16230         # others the normal race scenario will take place
16231         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
16232         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
16233         do_nodes $mdts $LCTL set_param fail_val=1
16234
16235         # unregister 1st user
16236         changelog_deregister &
16237         local pid1=$!
16238         # wait some time for deregister work to reach race rdv
16239         sleep 2
16240         # register 2nd user
16241         changelog_register || error "2nd user register failed"
16242
16243         wait $pid1 || error "1st user deregister failed"
16244
16245         local i
16246         local last_rec
16247         declare -A LAST_REC
16248         for i in $(seq $MDSCOUNT); do
16249                 if changelog_users mds$i | grep "^cl"; then
16250                         # make sure new records are added with one user present
16251                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
16252                                           awk '/^current.index:/ { print $NF }')
16253                 else
16254                         error "mds$i has no user registered"
16255                 fi
16256         done
16257
16258         # generate more changelog records to accumulate on each MDT
16259         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16260                 error "create $DIR/$tdir/${tfile}bis failed"
16261
16262         for i in $(seq $MDSCOUNT); do
16263                 last_rec=$(changelog_users $SINGLEMDS |
16264                            awk '/^current.index:/ { print $NF }')
16265                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
16266                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
16267                         error "changelogs are off on mds$i"
16268         done
16269 }
16270 run_test 160i "changelog user register/unregister race"
16271
16272 test_160j() {
16273         remote_mds_nodsh && skip "remote MDS with nodsh"
16274         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
16275                 skip "Need MDS version at least 2.12.56"
16276
16277         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
16278         stack_trap "umount $MOUNT2" EXIT
16279
16280         changelog_register || error "first changelog_register failed"
16281         stack_trap "changelog_deregister" EXIT
16282
16283         # generate some changelog
16284         # use all_char because created files should be evenly distributed
16285         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16286                 error "mkdir $tdir failed"
16287         for ((i = 0; i < MDSCOUNT; i++)); do
16288                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16289                         error "create $DIR/$tdir/d$i.1 failed"
16290         done
16291
16292         # open the changelog device
16293         exec 3>/dev/changelog-$FSNAME-MDT0000
16294         stack_trap "exec 3>&-" EXIT
16295         exec 4</dev/changelog-$FSNAME-MDT0000
16296         stack_trap "exec 4<&-" EXIT
16297
16298         # umount the first lustre mount
16299         umount $MOUNT
16300         stack_trap "mount_client $MOUNT" EXIT
16301
16302         # read changelog, which may or may not fail, but should not crash
16303         cat <&4 >/dev/null
16304
16305         # clear changelog
16306         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16307         changelog_users $SINGLEMDS | grep -q $cl_user ||
16308                 error "User $cl_user not found in changelog_users"
16309
16310         printf 'clear:'$cl_user':0' >&3
16311 }
16312 run_test 160j "client can be umounted while its chanangelog is being used"
16313
16314 test_160k() {
16315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16316         remote_mds_nodsh && skip "remote MDS with nodsh"
16317
16318         mkdir -p $DIR/$tdir/1/1
16319
16320         changelog_register || error "changelog_register failed"
16321         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16322
16323         changelog_users $SINGLEMDS | grep -q $cl_user ||
16324                 error "User '$cl_user' not found in changelog_users"
16325 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
16326         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
16327         rmdir $DIR/$tdir/1/1 & sleep 1
16328         mkdir $DIR/$tdir/2
16329         touch $DIR/$tdir/2/2
16330         rm -rf $DIR/$tdir/2
16331
16332         wait
16333         sleep 4
16334
16335         changelog_dump | grep rmdir || error "rmdir not recorded"
16336 }
16337 run_test 160k "Verify that changelog records are not lost"
16338
16339 # Verifies that a file passed as a parameter has recently had an operation
16340 # performed on it that has generated an MTIME changelog which contains the
16341 # correct parent FID. As files might reside on a different MDT from the
16342 # parent directory in DNE configurations, the FIDs are translated to paths
16343 # before being compared, which should be identical
16344 compare_mtime_changelog() {
16345         local file="${1}"
16346         local mdtidx
16347         local mtime
16348         local cl_fid
16349         local pdir
16350         local dir
16351
16352         mdtidx=$($LFS getstripe --mdt-index $file)
16353         mdtidx=$(printf "%04x" $mdtidx)
16354
16355         # Obtain the parent FID from the MTIME changelog
16356         mtime=$($LFS changelog $FSNAME-MDT$mdtidx | tail -n 1 | grep MTIME)
16357         [ -z "$mtime" ] && error "MTIME changelog not recorded"
16358
16359         cl_fid=$(sed -e 's/.* p=//' -e 's/ .*//' <<<$mtime)
16360         [ -z "$cl_fid" ] && error "parent FID not present"
16361
16362         # Verify that the path for the parent FID is the same as the path for
16363         # the test directory
16364         pdir=$($LFS fid2path $MOUNT "$cl_fid")
16365
16366         dir=$(dirname $1)
16367
16368         [[ "${pdir%/}" == "$dir" ]] ||
16369                 error "MTIME changelog parent FID is wrong, expected $dir, got $pdir"
16370 }
16371
16372 test_160l() {
16373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16374
16375         remote_mds_nodsh && skip "remote MDS with nodsh"
16376         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
16377                 skip "Need MDS version at least 2.13.55"
16378
16379         local cl_user
16380
16381         changelog_register || error "changelog_register failed"
16382         cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16383
16384         changelog_users $SINGLEMDS | grep -q $cl_user ||
16385                 error "User '$cl_user' not found in changelog_users"
16386
16387         # Clear some types so that MTIME changelogs are generated
16388         changelog_chmask "-CREAT"
16389         changelog_chmask "-CLOSE"
16390
16391         test_mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
16392
16393         # Test CL_MTIME during setattr
16394         touch $DIR/$tdir/$tfile
16395         compare_mtime_changelog $DIR/$tdir/$tfile
16396
16397         # Test CL_MTIME during close
16398         $MULTIOP $DIR/$tdir/${tfile}_2 O_2w4096c || error "multiop failed"
16399         compare_mtime_changelog $DIR/$tdir/${tfile}_2
16400 }
16401 run_test 160l "Verify that MTIME changelog records contain the parent FID"
16402
16403 test_160m() {
16404         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16405         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16406                 skip "Need MDS version at least 2.14.51"
16407         local cl_users
16408         local cl_user1
16409         local cl_user2
16410         local pid1
16411
16412         # Create a user
16413         changelog_register || error "first changelog_register failed"
16414         changelog_register || error "second changelog_register failed"
16415
16416         cl_users=(${CL_USERS[mds1]})
16417         cl_user1="${cl_users[0]}"
16418         cl_user2="${cl_users[1]}"
16419         # generate some changelog records to accumulate on MDT0
16420         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16421         createmany -m $DIR/$tdir/$tfile 50 ||
16422                 error "create $DIR/$tdir/$tfile failed"
16423         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16424         rm -f $DIR/$tdir
16425
16426         # check changelogs have been generated
16427         local nbcl=$(changelog_dump | wc -l)
16428         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16429
16430 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
16431         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
16432
16433         __changelog_clear mds1 $cl_user1 +10
16434         __changelog_clear mds1 $cl_user2 0 &
16435         pid1=$!
16436         sleep 2
16437         __changelog_clear mds1 $cl_user1 0 ||
16438                 error "fail to cancel record for $cl_user1"
16439         wait $pid1
16440         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16441 }
16442 run_test 160m "Changelog clear race"
16443
16444 test_160n() {
16445         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16446         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16447                 skip "Need MDS version at least 2.14.51"
16448         local cl_users
16449         local cl_user1
16450         local cl_user2
16451         local pid1
16452         local first_rec
16453         local last_rec=0
16454
16455         # Create a user
16456         changelog_register || error "first changelog_register failed"
16457
16458         cl_users=(${CL_USERS[mds1]})
16459         cl_user1="${cl_users[0]}"
16460
16461         # generate some changelog records to accumulate on MDT0
16462         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16463         first_rec=$(changelog_users $SINGLEMDS |
16464                         awk '/^current.index:/ { print $NF }')
16465         while (( last_rec < (( first_rec + 65000)) )); do
16466                 createmany -m $DIR/$tdir/$tfile 10000 ||
16467                         error "create $DIR/$tdir/$tfile failed"
16468
16469                 for i in $(seq 0 10000); do
16470                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
16471                                 > /dev/null
16472                 done
16473
16474                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
16475                         error "unlinkmany failed unlink"
16476                 last_rec=$(changelog_users $SINGLEMDS |
16477                         awk '/^current.index:/ { print $NF }')
16478                 echo last record $last_rec
16479                 (( last_rec == 0 )) && error "no changelog found"
16480         done
16481
16482 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
16483         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
16484
16485         __changelog_clear mds1 $cl_user1 0 &
16486         pid1=$!
16487         sleep 2
16488         __changelog_clear mds1 $cl_user1 0 ||
16489                 error "fail to cancel record for $cl_user1"
16490         wait $pid1
16491         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16492 }
16493 run_test 160n "Changelog destroy race"
16494
16495 test_160o() {
16496         local mdt="$(facet_svc $SINGLEMDS)"
16497
16498         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16499         remote_mds_nodsh && skip "remote MDS with nodsh"
16500         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
16501                 skip "Need MDS version at least 2.14.52"
16502
16503         changelog_register --user test_160o -m unlnk+close+open ||
16504                 error "changelog_register failed"
16505
16506         do_facet $SINGLEMDS $LCTL --device $mdt \
16507                                 changelog_register -u "Tt3_-#" &&
16508                 error "bad symbols in name should fail"
16509
16510         do_facet $SINGLEMDS $LCTL --device $mdt \
16511                                 changelog_register -u test_160o &&
16512                 error "the same name registration should fail"
16513
16514         do_facet $SINGLEMDS $LCTL --device $mdt \
16515                         changelog_register -u test_160toolongname &&
16516                 error "too long name registration should fail"
16517
16518         changelog_chmask "MARK+HSM"
16519         lctl get_param mdd.*.changelog*mask
16520         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16521         changelog_users $SINGLEMDS | grep -q $cl_user ||
16522                 error "User $cl_user not found in changelog_users"
16523         #verify username
16524         echo $cl_user | grep -q test_160o ||
16525                 error "User $cl_user has no specific name 'test160o'"
16526
16527         # change something
16528         changelog_clear 0 || error "changelog_clear failed"
16529         # generate some changelog records to accumulate on MDT0
16530         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16531         touch $DIR/$tdir/$tfile                 # open 1
16532
16533         OPENS=$(changelog_dump | grep -c "OPEN")
16534         [[ $OPENS -eq 1 ]] || error "OPEN changelog mask count $OPENS != 1"
16535
16536         # must be no MKDIR it wasn't set as user mask
16537         MKDIR=$(changelog_dump | grep -c "MKDIR")
16538         [[ $MKDIR -eq 0 ]] || error "MKDIR changelog mask found $MKDIR > 0"
16539
16540         oldmask=$(do_facet $SINGLEMDS $LCTL get_param \
16541                                 mdd.$mdt.changelog_current_mask -n)
16542         # register maskless user
16543         changelog_register || error "changelog_register failed"
16544         # effective mask should be not changed because it is not minimal
16545         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16546                                 mdd.$mdt.changelog_current_mask -n)
16547         [[ $mask == $oldmask ]] || error "mask was changed: $mask vs $oldmask"
16548         # set server mask to minimal value
16549         changelog_chmask "MARK"
16550         # check effective mask again, should be treated as DEFMASK now
16551         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16552                                 mdd.$mdt.changelog_current_mask -n)
16553         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
16554
16555         do_facet $SINGLEMDS $LCTL --device $mdt \
16556                                 changelog_deregister -u test_160o ||
16557                 error "cannot deregister by name"
16558 }
16559 run_test 160o "changelog user name and mask"
16560
16561 test_160p() {
16562         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16563         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16564                 skip "Need MDS version at least 2.14.51"
16565         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
16566         local cl_users
16567         local cl_user1
16568         local entry_count
16569
16570         # Create a user
16571         changelog_register || error "first changelog_register failed"
16572
16573         cl_users=(${CL_USERS[mds1]})
16574         cl_user1="${cl_users[0]}"
16575
16576         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16577         createmany -m $DIR/$tdir/$tfile 50 ||
16578                 error "create $DIR/$tdir/$tfile failed"
16579         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16580         rm -rf $DIR/$tdir
16581
16582         # check changelogs have been generated
16583         entry_count=$(changelog_dump | wc -l)
16584         ((entry_count != 0)) || error "no changelog entries found"
16585
16586         # remove changelog_users and check that orphan entries are removed
16587         stop mds1
16588         do_facet mds1 "$DEBUGFS -w -R 'rm changelog_users' $(mdsdevname 1)"
16589         start mds1 || error "cannot start mdt"
16590         entry_count=$(changelog_dump | wc -l)
16591         ((entry_count == 0)) ||
16592                 error "found $entry_count changelog entries, expected none"
16593 }
16594 run_test 160p "Changelog orphan cleanup with no users"
16595
16596 test_160q() {
16597         local mdt="$(facet_svc $SINGLEMDS)"
16598         local clu
16599
16600         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16601         remote_mds_nodsh && skip "remote MDS with nodsh"
16602         [ $MDS1_VERSION -ge $(version_code 2.14.54) ] ||
16603                 skip "Need MDS version at least 2.14.54"
16604
16605         # set server mask to minimal value like server init does
16606         changelog_chmask "MARK"
16607         clu=$(do_facet $SINGLEMDS $LCTL --device $mdt changelog_register -n) ||
16608                 error "changelog_register failed"
16609         # check effective mask again, should be treated as DEFMASK now
16610         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16611                                 mdd.$mdt.changelog_current_mask -n)
16612         do_facet $SINGLEMDS $LCTL --device $mdt changelog_deregister $clu ||
16613                 error "changelog_deregister failed"
16614         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
16615 }
16616 run_test 160q "changelog effective mask is DEFMASK if not set"
16617
16618 test_161a() {
16619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16620
16621         test_mkdir -c1 $DIR/$tdir
16622         cp /etc/hosts $DIR/$tdir/$tfile
16623         test_mkdir -c1 $DIR/$tdir/foo1
16624         test_mkdir -c1 $DIR/$tdir/foo2
16625         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
16626         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
16627         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
16628         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
16629         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
16630         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
16631                 $LFS fid2path $DIR $FID
16632                 error "bad link ea"
16633         fi
16634         # middle
16635         rm $DIR/$tdir/foo2/zachary
16636         # last
16637         rm $DIR/$tdir/foo2/thor
16638         # first
16639         rm $DIR/$tdir/$tfile
16640         # rename
16641         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
16642         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
16643                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
16644         rm $DIR/$tdir/foo2/maggie
16645
16646         # overflow the EA
16647         local longname=$tfile.avg_len_is_thirty_two_
16648         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
16649                 error_noexit 'failed to unlink many hardlinks'" EXIT
16650         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
16651                 error "failed to hardlink many files"
16652         links=$($LFS fid2path $DIR $FID | wc -l)
16653         echo -n "${links}/1000 links in link EA"
16654         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
16655 }
16656 run_test 161a "link ea sanity"
16657
16658 test_161b() {
16659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16660         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
16661
16662         local MDTIDX=1
16663         local remote_dir=$DIR/$tdir/remote_dir
16664
16665         mkdir -p $DIR/$tdir
16666         $LFS mkdir -i $MDTIDX $remote_dir ||
16667                 error "create remote directory failed"
16668
16669         cp /etc/hosts $remote_dir/$tfile
16670         mkdir -p $remote_dir/foo1
16671         mkdir -p $remote_dir/foo2
16672         ln $remote_dir/$tfile $remote_dir/foo1/sofia
16673         ln $remote_dir/$tfile $remote_dir/foo2/zachary
16674         ln $remote_dir/$tfile $remote_dir/foo1/luna
16675         ln $remote_dir/$tfile $remote_dir/foo2/thor
16676
16677         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
16678                      tr -d ']')
16679         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
16680                 $LFS fid2path $DIR $FID
16681                 error "bad link ea"
16682         fi
16683         # middle
16684         rm $remote_dir/foo2/zachary
16685         # last
16686         rm $remote_dir/foo2/thor
16687         # first
16688         rm $remote_dir/$tfile
16689         # rename
16690         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
16691         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
16692         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
16693                 $LFS fid2path $DIR $FID
16694                 error "bad link rename"
16695         fi
16696         rm $remote_dir/foo2/maggie
16697
16698         # overflow the EA
16699         local longname=filename_avg_len_is_thirty_two_
16700         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
16701                 error "failed to hardlink many files"
16702         links=$($LFS fid2path $DIR $FID | wc -l)
16703         echo -n "${links}/1000 links in link EA"
16704         [[ ${links} -gt 60 ]] ||
16705                 error "expected at least 60 links in link EA"
16706         unlinkmany $remote_dir/foo2/$longname 1000 ||
16707         error "failed to unlink many hardlinks"
16708 }
16709 run_test 161b "link ea sanity under remote directory"
16710
16711 test_161c() {
16712         remote_mds_nodsh && skip "remote MDS with nodsh"
16713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16714         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
16715                 skip "Need MDS version at least 2.1.5"
16716
16717         # define CLF_RENAME_LAST 0x0001
16718         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
16719         changelog_register || error "changelog_register failed"
16720
16721         rm -rf $DIR/$tdir
16722         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
16723         touch $DIR/$tdir/foo_161c
16724         touch $DIR/$tdir/bar_161c
16725         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
16726         changelog_dump | grep RENME | tail -n 5
16727         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
16728         changelog_clear 0 || error "changelog_clear failed"
16729         if [ x$flags != "x0x1" ]; then
16730                 error "flag $flags is not 0x1"
16731         fi
16732
16733         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
16734         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
16735         touch $DIR/$tdir/foo_161c
16736         touch $DIR/$tdir/bar_161c
16737         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
16738         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
16739         changelog_dump | grep RENME | tail -n 5
16740         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
16741         changelog_clear 0 || error "changelog_clear failed"
16742         if [ x$flags != "x0x0" ]; then
16743                 error "flag $flags is not 0x0"
16744         fi
16745         echo "rename overwrite a target having nlink > 1," \
16746                 "changelog record has flags of $flags"
16747
16748         # rename doesn't overwrite a target (changelog flag 0x0)
16749         touch $DIR/$tdir/foo_161c
16750         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
16751         changelog_dump | grep RENME | tail -n 5
16752         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
16753         changelog_clear 0 || error "changelog_clear failed"
16754         if [ x$flags != "x0x0" ]; then
16755                 error "flag $flags is not 0x0"
16756         fi
16757         echo "rename doesn't overwrite a target," \
16758                 "changelog record has flags of $flags"
16759
16760         # define CLF_UNLINK_LAST 0x0001
16761         # unlink a file having nlink = 1 (changelog flag 0x1)
16762         rm -f $DIR/$tdir/foo2_161c
16763         changelog_dump | grep UNLNK | tail -n 5
16764         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
16765         changelog_clear 0 || error "changelog_clear failed"
16766         if [ x$flags != "x0x1" ]; then
16767                 error "flag $flags is not 0x1"
16768         fi
16769         echo "unlink a file having nlink = 1," \
16770                 "changelog record has flags of $flags"
16771
16772         # unlink a file having nlink > 1 (changelog flag 0x0)
16773         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
16774         rm -f $DIR/$tdir/foobar_161c
16775         changelog_dump | grep UNLNK | tail -n 5
16776         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
16777         changelog_clear 0 || error "changelog_clear failed"
16778         if [ x$flags != "x0x0" ]; then
16779                 error "flag $flags is not 0x0"
16780         fi
16781         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
16782 }
16783 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
16784
16785 test_161d() {
16786         remote_mds_nodsh && skip "remote MDS with nodsh"
16787         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
16788
16789         local pid
16790         local fid
16791
16792         changelog_register || error "changelog_register failed"
16793
16794         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
16795         # interfer with $MOUNT/.lustre/fid/ access
16796         mkdir $DIR/$tdir
16797         [[ $? -eq 0 ]] || error "mkdir failed"
16798
16799         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
16800         $LCTL set_param fail_loc=0x8000140c
16801         # 5s pause
16802         $LCTL set_param fail_val=5
16803
16804         # create file
16805         echo foofoo > $DIR/$tdir/$tfile &
16806         pid=$!
16807
16808         # wait for create to be delayed
16809         sleep 2
16810
16811         ps -p $pid
16812         [[ $? -eq 0 ]] || error "create should be blocked"
16813
16814         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
16815         stack_trap "rm -f $tempfile"
16816         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
16817         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
16818         # some delay may occur during ChangeLog publishing and file read just
16819         # above, that could allow file write to happen finally
16820         [[ -s $tempfile ]] && echo "file should be empty"
16821
16822         $LCTL set_param fail_loc=0
16823
16824         wait $pid
16825         [[ $? -eq 0 ]] || error "create failed"
16826 }
16827 run_test 161d "create with concurrent .lustre/fid access"
16828
16829 check_path() {
16830         local expected="$1"
16831         shift
16832         local fid="$2"
16833
16834         local path
16835         path=$($LFS fid2path "$@")
16836         local rc=$?
16837
16838         if [ $rc -ne 0 ]; then
16839                 error "path looked up of '$expected' failed: rc=$rc"
16840         elif [ "$path" != "$expected" ]; then
16841                 error "path looked up '$path' instead of '$expected'"
16842         else
16843                 echo "FID '$fid' resolves to path '$path' as expected"
16844         fi
16845 }
16846
16847 test_162a() { # was test_162
16848         test_mkdir -p -c1 $DIR/$tdir/d2
16849         touch $DIR/$tdir/d2/$tfile
16850         touch $DIR/$tdir/d2/x1
16851         touch $DIR/$tdir/d2/x2
16852         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
16853         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
16854         # regular file
16855         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
16856         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
16857
16858         # softlink
16859         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
16860         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
16861         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
16862
16863         # softlink to wrong file
16864         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
16865         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
16866         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
16867
16868         # hardlink
16869         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
16870         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
16871         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
16872         # fid2path dir/fsname should both work
16873         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
16874         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
16875
16876         # hardlink count: check that there are 2 links
16877         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
16878         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
16879
16880         # hardlink indexing: remove the first link
16881         rm $DIR/$tdir/d2/p/q/r/hlink
16882         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
16883 }
16884 run_test 162a "path lookup sanity"
16885
16886 test_162b() {
16887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16888         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16889
16890         mkdir $DIR/$tdir
16891         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
16892                                 error "create striped dir failed"
16893
16894         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
16895                                         tail -n 1 | awk '{print $2}')
16896         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
16897
16898         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
16899         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
16900
16901         # regular file
16902         for ((i=0;i<5;i++)); do
16903                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
16904                         error "get fid for f$i failed"
16905                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
16906
16907                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
16908                         error "get fid for d$i failed"
16909                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
16910         done
16911
16912         return 0
16913 }
16914 run_test 162b "striped directory path lookup sanity"
16915
16916 # LU-4239: Verify fid2path works with paths 100 or more directories deep
16917 test_162c() {
16918         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
16919                 skip "Need MDS version at least 2.7.51"
16920
16921         local lpath=$tdir.local
16922         local rpath=$tdir.remote
16923
16924         test_mkdir $DIR/$lpath
16925         test_mkdir $DIR/$rpath
16926
16927         for ((i = 0; i <= 101; i++)); do
16928                 lpath="$lpath/$i"
16929                 mkdir $DIR/$lpath
16930                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
16931                         error "get fid for local directory $DIR/$lpath failed"
16932                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
16933
16934                 rpath="$rpath/$i"
16935                 test_mkdir $DIR/$rpath
16936                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
16937                         error "get fid for remote directory $DIR/$rpath failed"
16938                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
16939         done
16940
16941         return 0
16942 }
16943 run_test 162c "fid2path works with paths 100 or more directories deep"
16944
16945 oalr_event_count() {
16946         local event="${1}"
16947         local trace="${2}"
16948
16949         awk -v name="${FSNAME}-OST0000" \
16950             -v event="${event}" \
16951             '$1 == "TRACE" && $2 == event && $3 == name' \
16952             "${trace}" |
16953         wc -l
16954 }
16955
16956 oalr_expect_event_count() {
16957         local event="${1}"
16958         local trace="${2}"
16959         local expect="${3}"
16960         local count
16961
16962         count=$(oalr_event_count "${event}" "${trace}")
16963         if ((count == expect)); then
16964                 return 0
16965         fi
16966
16967         error_noexit "${event} event count was '${count}', expected ${expect}"
16968         cat "${trace}" >&2
16969         exit 1
16970 }
16971
16972 cleanup_165() {
16973         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
16974         stop ost1
16975         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
16976 }
16977
16978 setup_165() {
16979         sync # Flush previous IOs so we can count log entries.
16980         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
16981         stack_trap cleanup_165 EXIT
16982 }
16983
16984 test_165a() {
16985         local trace="/tmp/${tfile}.trace"
16986         local rc
16987         local count
16988
16989         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
16990                 skip "OFD access log unsupported"
16991
16992         setup_165
16993         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
16994         sleep 5
16995
16996         do_facet ost1 ofd_access_log_reader --list
16997         stop ost1
16998
16999         do_facet ost1 killall -TERM ofd_access_log_reader
17000         wait
17001         rc=$?
17002
17003         if ((rc != 0)); then
17004                 error "ofd_access_log_reader exited with rc = '${rc}'"
17005         fi
17006
17007         # Parse trace file for discovery events:
17008         oalr_expect_event_count alr_log_add "${trace}" 1
17009         oalr_expect_event_count alr_log_eof "${trace}" 1
17010         oalr_expect_event_count alr_log_free "${trace}" 1
17011 }
17012 run_test 165a "ofd access log discovery"
17013
17014 test_165b() {
17015         local trace="/tmp/${tfile}.trace"
17016         local file="${DIR}/${tfile}"
17017         local pfid1
17018         local pfid2
17019         local -a entry
17020         local rc
17021         local count
17022         local size
17023         local flags
17024
17025         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17026                 skip "OFD access log unsupported"
17027
17028         setup_165
17029         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17030         sleep 5
17031
17032         do_facet ost1 ofd_access_log_reader --list
17033
17034         lfs setstripe -c 1 -i 0 "${file}"
17035         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17036                 error "cannot create '${file}'"
17037
17038         sleep 5
17039         do_facet ost1 killall -TERM ofd_access_log_reader
17040         wait
17041         rc=$?
17042
17043         if ((rc != 0)); then
17044                 error "ofd_access_log_reader exited with rc = '${rc}'"
17045         fi
17046
17047         oalr_expect_event_count alr_log_entry "${trace}" 1
17048
17049         pfid1=$($LFS path2fid "${file}")
17050
17051         # 1     2             3   4    5     6   7    8    9     10
17052         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
17053         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17054
17055         echo "entry = '${entry[*]}'" >&2
17056
17057         pfid2=${entry[4]}
17058         if [[ "${pfid1}" != "${pfid2}" ]]; then
17059                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17060         fi
17061
17062         size=${entry[8]}
17063         if ((size != 1048576)); then
17064                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
17065         fi
17066
17067         flags=${entry[10]}
17068         if [[ "${flags}" != "w" ]]; then
17069                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
17070         fi
17071
17072         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17073         sleep 5
17074
17075         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c ||
17076                 error "cannot read '${file}'"
17077         sleep 5
17078
17079         do_facet ost1 killall -TERM ofd_access_log_reader
17080         wait
17081         rc=$?
17082
17083         if ((rc != 0)); then
17084                 error "ofd_access_log_reader exited with rc = '${rc}'"
17085         fi
17086
17087         oalr_expect_event_count alr_log_entry "${trace}" 1
17088
17089         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17090         echo "entry = '${entry[*]}'" >&2
17091
17092         pfid2=${entry[4]}
17093         if [[ "${pfid1}" != "${pfid2}" ]]; then
17094                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17095         fi
17096
17097         size=${entry[8]}
17098         if ((size != 524288)); then
17099                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
17100         fi
17101
17102         flags=${entry[10]}
17103         if [[ "${flags}" != "r" ]]; then
17104                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
17105         fi
17106 }
17107 run_test 165b "ofd access log entries are produced and consumed"
17108
17109 test_165c() {
17110         local trace="/tmp/${tfile}.trace"
17111         local file="${DIR}/${tdir}/${tfile}"
17112
17113         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17114                 skip "OFD access log unsupported"
17115
17116         test_mkdir "${DIR}/${tdir}"
17117
17118         setup_165
17119         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17120         sleep 5
17121
17122         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
17123
17124         # 4096 / 64 = 64. Create twice as many entries.
17125         for ((i = 0; i < 128; i++)); do
17126                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c ||
17127                         error "cannot create file"
17128         done
17129
17130         sync
17131
17132         do_facet ost1 killall -TERM ofd_access_log_reader
17133         wait
17134         rc=$?
17135         if ((rc != 0)); then
17136                 error "ofd_access_log_reader exited with rc = '${rc}'"
17137         fi
17138
17139         unlinkmany  "${file}-%d" 128
17140 }
17141 run_test 165c "full ofd access logs do not block IOs"
17142
17143 oal_get_read_count() {
17144         local stats="$1"
17145
17146         # STATS lustre-OST0001 alr_read_count 1
17147
17148         do_facet ost1 cat "${stats}" |
17149         awk '$1 == "STATS" && $3 == "alr_read_count" { count = $4; }
17150              END { print count; }'
17151 }
17152
17153 oal_expect_read_count() {
17154         local stats="$1"
17155         local count
17156         local expect="$2"
17157
17158         # Ask ofd_access_log_reader to write stats.
17159         do_facet ost1 killall -USR1 ofd_access_log_reader
17160
17161         # Allow some time for things to happen.
17162         sleep 1
17163
17164         count=$(oal_get_read_count "${stats}")
17165         if ((count == expect)); then
17166                 return 0
17167         fi
17168
17169         error_noexit "bad read count, got ${count}, expected ${expect}"
17170         do_facet ost1 cat "${stats}" >&2
17171         exit 1
17172 }
17173
17174 test_165d() {
17175         local stats="/tmp/${tfile}.stats"
17176         local file="${DIR}/${tdir}/${tfile}"
17177         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
17178
17179         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17180                 skip "OFD access log unsupported"
17181
17182         test_mkdir "${DIR}/${tdir}"
17183
17184         setup_165
17185         do_facet ost1 ofd_access_log_reader --stats="${stats}" &
17186         sleep 5
17187
17188         lfs setstripe -c 1 -i 0 "${file}"
17189
17190         do_facet ost1 lctl set_param "${param}=rw"
17191         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17192                 error "cannot create '${file}'"
17193         oal_expect_read_count "${stats}" 1
17194
17195         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17196                 error "cannot read '${file}'"
17197         oal_expect_read_count "${stats}" 2
17198
17199         do_facet ost1 lctl set_param "${param}=r"
17200         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17201                 error "cannot create '${file}'"
17202         oal_expect_read_count "${stats}" 2
17203
17204         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17205                 error "cannot read '${file}'"
17206         oal_expect_read_count "${stats}" 3
17207
17208         do_facet ost1 lctl set_param "${param}=w"
17209         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17210                 error "cannot create '${file}'"
17211         oal_expect_read_count "${stats}" 4
17212
17213         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17214                 error "cannot read '${file}'"
17215         oal_expect_read_count "${stats}" 4
17216
17217         do_facet ost1 lctl set_param "${param}=0"
17218         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17219                 error "cannot create '${file}'"
17220         oal_expect_read_count "${stats}" 4
17221
17222         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17223                 error "cannot read '${file}'"
17224         oal_expect_read_count "${stats}" 4
17225
17226         do_facet ost1 killall -TERM ofd_access_log_reader
17227         wait
17228         rc=$?
17229         if ((rc != 0)); then
17230                 error "ofd_access_log_reader exited with rc = '${rc}'"
17231         fi
17232 }
17233 run_test 165d "ofd_access_log mask works"
17234
17235 test_165e() {
17236         local stats="/tmp/${tfile}.stats"
17237         local file0="${DIR}/${tdir}-0/${tfile}"
17238         local file1="${DIR}/${tdir}-1/${tfile}"
17239
17240         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17241                 skip "OFD access log unsupported"
17242
17243         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
17244
17245         test_mkdir -c 1 -i 0 "${DIR}/${tdir}-0"
17246         test_mkdir -c 1 -i 1 "${DIR}/${tdir}-1"
17247
17248         lfs setstripe -c 1 -i 0 "${file0}"
17249         lfs setstripe -c 1 -i 0 "${file1}"
17250
17251         setup_165
17252         do_facet ost1 ofd_access_log_reader -I 1 --stats="${stats}" &
17253         sleep 5
17254
17255         $MULTIOP "${file0}" oO_CREAT:O_WRONLY:w512c ||
17256                 error "cannot create '${file0}'"
17257         sync
17258         oal_expect_read_count "${stats}" 0
17259
17260         $MULTIOP "${file1}" oO_CREAT:O_WRONLY:w512c ||
17261                 error "cannot create '${file1}'"
17262         sync
17263         oal_expect_read_count "${stats}" 1
17264
17265         do_facet ost1 killall -TERM ofd_access_log_reader
17266         wait
17267         rc=$?
17268         if ((rc != 0)); then
17269                 error "ofd_access_log_reader exited with rc = '${rc}'"
17270         fi
17271 }
17272 run_test 165e "ofd_access_log MDT index filter works"
17273
17274 test_165f() {
17275         local trace="/tmp/${tfile}.trace"
17276         local rc
17277         local count
17278
17279         setup_165
17280         do_facet ost1 timeout 60 ofd_access_log_reader \
17281                 --exit-on-close --debug=- --trace=- > "${trace}" &
17282         sleep 5
17283         stop ost1
17284
17285         wait
17286         rc=$?
17287
17288         if ((rc != 0)); then
17289                 error_noexit "ofd_access_log_reader exited with rc = '${rc}'"
17290                 cat "${trace}"
17291                 exit 1
17292         fi
17293 }
17294 run_test 165f "ofd_access_log_reader --exit-on-close works"
17295
17296 test_169() {
17297         # do directio so as not to populate the page cache
17298         log "creating a 10 Mb file"
17299         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
17300                 error "multiop failed while creating a file"
17301         log "starting reads"
17302         dd if=$DIR/$tfile of=/dev/null bs=4096 &
17303         log "truncating the file"
17304         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
17305                 error "multiop failed while truncating the file"
17306         log "killing dd"
17307         kill %+ || true # reads might have finished
17308         echo "wait until dd is finished"
17309         wait
17310         log "removing the temporary file"
17311         rm -rf $DIR/$tfile || error "tmp file removal failed"
17312 }
17313 run_test 169 "parallel read and truncate should not deadlock"
17314
17315 test_170() {
17316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17317
17318         $LCTL clear     # bug 18514
17319         $LCTL debug_daemon start $TMP/${tfile}_log_good
17320         touch $DIR/$tfile
17321         $LCTL debug_daemon stop
17322         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
17323                 error "sed failed to read log_good"
17324
17325         $LCTL debug_daemon start $TMP/${tfile}_log_good
17326         rm -rf $DIR/$tfile
17327         $LCTL debug_daemon stop
17328
17329         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
17330                error "lctl df log_bad failed"
17331
17332         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17333         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17334
17335         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
17336         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
17337
17338         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
17339                 error "bad_line good_line1 good_line2 are empty"
17340
17341         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
17342         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
17343         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
17344
17345         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
17346         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17347         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17348
17349         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
17350                 error "bad_line_new good_line_new are empty"
17351
17352         local expected_good=$((good_line1 + good_line2*2))
17353
17354         rm -f $TMP/${tfile}*
17355         # LU-231, short malformed line may not be counted into bad lines
17356         if [ $bad_line -ne $bad_line_new ] &&
17357                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
17358                 error "expected $bad_line bad lines, but got $bad_line_new"
17359                 return 1
17360         fi
17361
17362         if [ $expected_good -ne $good_line_new ]; then
17363                 error "expected $expected_good good lines, but got $good_line_new"
17364                 return 2
17365         fi
17366         true
17367 }
17368 run_test 170 "test lctl df to handle corrupted log ====================="
17369
17370 test_171() { # bug20592
17371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17372
17373         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
17374         $LCTL set_param fail_loc=0x50e
17375         $LCTL set_param fail_val=3000
17376         multiop_bg_pause $DIR/$tfile O_s || true
17377         local MULTIPID=$!
17378         kill -USR1 $MULTIPID
17379         # cause log dump
17380         sleep 3
17381         wait $MULTIPID
17382         if dmesg | grep "recursive fault"; then
17383                 error "caught a recursive fault"
17384         fi
17385         $LCTL set_param fail_loc=0
17386         true
17387 }
17388 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
17389
17390 # it would be good to share it with obdfilter-survey/iokit-libecho code
17391 setup_obdecho_osc () {
17392         local rc=0
17393         local ost_nid=$1
17394         local obdfilter_name=$2
17395         echo "Creating new osc for $obdfilter_name on $ost_nid"
17396         # make sure we can find loopback nid
17397         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
17398
17399         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
17400                            ${obdfilter_name}_osc_UUID || rc=2; }
17401         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
17402                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
17403         return $rc
17404 }
17405
17406 cleanup_obdecho_osc () {
17407         local obdfilter_name=$1
17408         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
17409         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
17410         return 0
17411 }
17412
17413 obdecho_test() {
17414         local OBD=$1
17415         local node=$2
17416         local pages=${3:-64}
17417         local rc=0
17418         local id
17419
17420         local count=10
17421         local obd_size=$(get_obd_size $node $OBD)
17422         local page_size=$(get_page_size $node)
17423         if [[ -n "$obd_size" ]]; then
17424                 local new_count=$((obd_size / (pages * page_size / 1024)))
17425                 [[ $new_count -ge $count ]] || count=$new_count
17426         fi
17427
17428         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
17429         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
17430                            rc=2; }
17431         if [ $rc -eq 0 ]; then
17432             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
17433             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
17434         fi
17435         echo "New object id is $id"
17436         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
17437                            rc=4; }
17438         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
17439                            "test_brw $count w v $pages $id" || rc=4; }
17440         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
17441                            rc=4; }
17442         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
17443                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
17444         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
17445                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
17446         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
17447         return $rc
17448 }
17449
17450 test_180a() {
17451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17452
17453         if ! [ -d /sys/fs/lustre/echo_client ] &&
17454            ! module_loaded obdecho; then
17455                 load_module obdecho/obdecho &&
17456                         stack_trap "rmmod obdecho" EXIT ||
17457                         error "unable to load obdecho on client"
17458         fi
17459
17460         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
17461         local host=$($LCTL get_param -n osc.$osc.import |
17462                      awk '/current_connection:/ { print $2 }' )
17463         local target=$($LCTL get_param -n osc.$osc.import |
17464                        awk '/target:/ { print $2 }' )
17465         target=${target%_UUID}
17466
17467         if [ -n "$target" ]; then
17468                 setup_obdecho_osc $host $target &&
17469                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
17470                         { error "obdecho setup failed with $?"; return; }
17471
17472                 obdecho_test ${target}_osc client ||
17473                         error "obdecho_test failed on ${target}_osc"
17474         else
17475                 $LCTL get_param osc.$osc.import
17476                 error "there is no osc.$osc.import target"
17477         fi
17478 }
17479 run_test 180a "test obdecho on osc"
17480
17481 test_180b() {
17482         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17483         remote_ost_nodsh && skip "remote OST with nodsh"
17484
17485         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
17486                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
17487                 error "failed to load module obdecho"
17488
17489         local target=$(do_facet ost1 $LCTL dl |
17490                        awk '/obdfilter/ { print $4; exit; }')
17491
17492         if [ -n "$target" ]; then
17493                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
17494         else
17495                 do_facet ost1 $LCTL dl
17496                 error "there is no obdfilter target on ost1"
17497         fi
17498 }
17499 run_test 180b "test obdecho directly on obdfilter"
17500
17501 test_180c() { # LU-2598
17502         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17503         remote_ost_nodsh && skip "remote OST with nodsh"
17504         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
17505                 skip "Need MDS version at least 2.4.0"
17506
17507         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
17508                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
17509                 error "failed to load module obdecho"
17510
17511         local target=$(do_facet ost1 $LCTL dl |
17512                        awk '/obdfilter/ { print $4; exit; }')
17513
17514         if [ -n "$target" ]; then
17515                 local pages=16384 # 64MB bulk I/O RPC size
17516
17517                 obdecho_test "$target" ost1 "$pages" ||
17518                         error "obdecho_test with pages=$pages failed with $?"
17519         else
17520                 do_facet ost1 $LCTL dl
17521                 error "there is no obdfilter target on ost1"
17522         fi
17523 }
17524 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
17525
17526 test_181() { # bug 22177
17527         test_mkdir $DIR/$tdir
17528         # create enough files to index the directory
17529         createmany -o $DIR/$tdir/foobar 4000
17530         # print attributes for debug purpose
17531         lsattr -d .
17532         # open dir
17533         multiop_bg_pause $DIR/$tdir D_Sc || return 1
17534         MULTIPID=$!
17535         # remove the files & current working dir
17536         unlinkmany $DIR/$tdir/foobar 4000
17537         rmdir $DIR/$tdir
17538         kill -USR1 $MULTIPID
17539         wait $MULTIPID
17540         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
17541         return 0
17542 }
17543 run_test 181 "Test open-unlinked dir ========================"
17544
17545 test_182() {
17546         local fcount=1000
17547         local tcount=10
17548
17549         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
17550
17551         $LCTL set_param mdc.*.rpc_stats=clear
17552
17553         for (( i = 0; i < $tcount; i++ )) ; do
17554                 mkdir $DIR/$tdir/$i
17555         done
17556
17557         for (( i = 0; i < $tcount; i++ )) ; do
17558                 createmany -o $DIR/$tdir/$i/f- $fcount &
17559         done
17560         wait
17561
17562         for (( i = 0; i < $tcount; i++ )) ; do
17563                 unlinkmany $DIR/$tdir/$i/f- $fcount &
17564         done
17565         wait
17566
17567         $LCTL get_param mdc.*.rpc_stats
17568
17569         rm -rf $DIR/$tdir
17570 }
17571 run_test 182 "Test parallel modify metadata operations ================"
17572
17573 test_183() { # LU-2275
17574         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17575         remote_mds_nodsh && skip "remote MDS with nodsh"
17576         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
17577                 skip "Need MDS version at least 2.3.56"
17578
17579         mkdir_on_mdt0 $DIR/$tdir || error "creating dir $DIR/$tdir"
17580         echo aaa > $DIR/$tdir/$tfile
17581
17582 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
17583         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
17584
17585         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
17586         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
17587
17588         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17589
17590         # Flush negative dentry cache
17591         touch $DIR/$tdir/$tfile
17592
17593         # We are not checking for any leaked references here, they'll
17594         # become evident next time we do cleanup with module unload.
17595         rm -rf $DIR/$tdir
17596 }
17597 run_test 183 "No crash or request leak in case of strange dispositions ========"
17598
17599 # test suite 184 is for LU-2016, LU-2017
17600 test_184a() {
17601         check_swap_layouts_support
17602
17603         dir0=$DIR/$tdir/$testnum
17604         test_mkdir -p -c1 $dir0
17605         ref1=/etc/passwd
17606         ref2=/etc/group
17607         file1=$dir0/f1
17608         file2=$dir0/f2
17609         $LFS setstripe -c1 $file1
17610         cp $ref1 $file1
17611         $LFS setstripe -c2 $file2
17612         cp $ref2 $file2
17613         gen1=$($LFS getstripe -g $file1)
17614         gen2=$($LFS getstripe -g $file2)
17615
17616         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
17617         gen=$($LFS getstripe -g $file1)
17618         [[ $gen1 != $gen ]] ||
17619                 "Layout generation on $file1 does not change"
17620         gen=$($LFS getstripe -g $file2)
17621         [[ $gen2 != $gen ]] ||
17622                 "Layout generation on $file2 does not change"
17623
17624         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
17625         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17626
17627         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
17628 }
17629 run_test 184a "Basic layout swap"
17630
17631 test_184b() {
17632         check_swap_layouts_support
17633
17634         dir0=$DIR/$tdir/$testnum
17635         mkdir -p $dir0 || error "creating dir $dir0"
17636         file1=$dir0/f1
17637         file2=$dir0/f2
17638         file3=$dir0/f3
17639         dir1=$dir0/d1
17640         dir2=$dir0/d2
17641         mkdir $dir1 $dir2
17642         $LFS setstripe -c1 $file1
17643         $LFS setstripe -c2 $file2
17644         $LFS setstripe -c1 $file3
17645         chown $RUNAS_ID $file3
17646         gen1=$($LFS getstripe -g $file1)
17647         gen2=$($LFS getstripe -g $file2)
17648
17649         $LFS swap_layouts $dir1 $dir2 &&
17650                 error "swap of directories layouts should fail"
17651         $LFS swap_layouts $dir1 $file1 &&
17652                 error "swap of directory and file layouts should fail"
17653         $RUNAS $LFS swap_layouts $file1 $file2 &&
17654                 error "swap of file we cannot write should fail"
17655         $LFS swap_layouts $file1 $file3 &&
17656                 error "swap of file with different owner should fail"
17657         /bin/true # to clear error code
17658 }
17659 run_test 184b "Forbidden layout swap (will generate errors)"
17660
17661 test_184c() {
17662         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
17663         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
17664         check_swap_layouts_support
17665         check_swap_layout_no_dom $DIR
17666
17667         local dir0=$DIR/$tdir/$testnum
17668         mkdir -p $dir0 || error "creating dir $dir0"
17669
17670         local ref1=$dir0/ref1
17671         local ref2=$dir0/ref2
17672         local file1=$dir0/file1
17673         local file2=$dir0/file2
17674         # create a file large enough for the concurrent test
17675         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
17676         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
17677         echo "ref file size: ref1($(stat -c %s $ref1))," \
17678              "ref2($(stat -c %s $ref2))"
17679
17680         cp $ref2 $file2
17681         dd if=$ref1 of=$file1 bs=16k &
17682         local DD_PID=$!
17683
17684         # Make sure dd starts to copy file, but wait at most 5 seconds
17685         local loops=0
17686         while [ ! -s $file1 -a $((loops++)) -lt 50 ]; do sleep 0.1; done
17687
17688         $LFS swap_layouts $file1 $file2
17689         local rc=$?
17690         wait $DD_PID
17691         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
17692         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
17693
17694         # how many bytes copied before swapping layout
17695         local copied=$(stat -c %s $file2)
17696         local remaining=$(stat -c %s $ref1)
17697         remaining=$((remaining - copied))
17698         echo "Copied $copied bytes before swapping layout..."
17699
17700         cmp -n $copied $file1 $ref2 | grep differ &&
17701                 error "Content mismatch [0, $copied) of ref2 and file1"
17702         cmp -n $copied $file2 $ref1 ||
17703                 error "Content mismatch [0, $copied) of ref1 and file2"
17704         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
17705                 error "Content mismatch [$copied, EOF) of ref1 and file1"
17706
17707         # clean up
17708         rm -f $ref1 $ref2 $file1 $file2
17709 }
17710 run_test 184c "Concurrent write and layout swap"
17711
17712 test_184d() {
17713         check_swap_layouts_support
17714         check_swap_layout_no_dom $DIR
17715         [ -z "$(which getfattr 2>/dev/null)" ] &&
17716                 skip_env "no getfattr command"
17717
17718         local file1=$DIR/$tdir/$tfile-1
17719         local file2=$DIR/$tdir/$tfile-2
17720         local file3=$DIR/$tdir/$tfile-3
17721         local lovea1
17722         local lovea2
17723
17724         mkdir -p $DIR/$tdir
17725         touch $file1 || error "create $file1 failed"
17726         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
17727                 error "create $file2 failed"
17728         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
17729                 error "create $file3 failed"
17730         lovea1=$(get_layout_param $file1)
17731
17732         $LFS swap_layouts $file2 $file3 ||
17733                 error "swap $file2 $file3 layouts failed"
17734         $LFS swap_layouts $file1 $file2 ||
17735                 error "swap $file1 $file2 layouts failed"
17736
17737         lovea2=$(get_layout_param $file2)
17738         echo "$lovea1"
17739         echo "$lovea2"
17740         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
17741
17742         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
17743         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
17744 }
17745 run_test 184d "allow stripeless layouts swap"
17746
17747 test_184e() {
17748         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
17749                 skip "Need MDS version at least 2.6.94"
17750         check_swap_layouts_support
17751         check_swap_layout_no_dom $DIR
17752         [ -z "$(which getfattr 2>/dev/null)" ] &&
17753                 skip_env "no getfattr command"
17754
17755         local file1=$DIR/$tdir/$tfile-1
17756         local file2=$DIR/$tdir/$tfile-2
17757         local file3=$DIR/$tdir/$tfile-3
17758         local lovea
17759
17760         mkdir -p $DIR/$tdir
17761         touch $file1 || error "create $file1 failed"
17762         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
17763                 error "create $file2 failed"
17764         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
17765                 error "create $file3 failed"
17766
17767         $LFS swap_layouts $file1 $file2 ||
17768                 error "swap $file1 $file2 layouts failed"
17769
17770         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
17771         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
17772
17773         echo 123 > $file1 || error "Should be able to write into $file1"
17774
17775         $LFS swap_layouts $file1 $file3 ||
17776                 error "swap $file1 $file3 layouts failed"
17777
17778         echo 123 > $file1 || error "Should be able to write into $file1"
17779
17780         rm -rf $file1 $file2 $file3
17781 }
17782 run_test 184e "Recreate layout after stripeless layout swaps"
17783
17784 test_184f() {
17785         # Create a file with name longer than sizeof(struct stat) ==
17786         # 144 to see if we can get chars from the file name to appear
17787         # in the returned striping. Note that 'f' == 0x66.
17788         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
17789
17790         mkdir -p $DIR/$tdir
17791         mcreate $DIR/$tdir/$file
17792         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
17793                 error "IOC_MDC_GETFILEINFO returned garbage striping"
17794         fi
17795 }
17796 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
17797
17798 test_185() { # LU-2441
17799         # LU-3553 - no volatile file support in old servers
17800         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
17801                 skip "Need MDS version at least 2.3.60"
17802
17803         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
17804         touch $DIR/$tdir/spoo
17805         local mtime1=$(stat -c "%Y" $DIR/$tdir)
17806         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
17807                 error "cannot create/write a volatile file"
17808         [ "$FILESET" == "" ] &&
17809         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
17810                 error "FID is still valid after close"
17811
17812         multiop_bg_pause $DIR/$tdir vVw4096_c
17813         local multi_pid=$!
17814
17815         local OLD_IFS=$IFS
17816         IFS=":"
17817         local fidv=($fid)
17818         IFS=$OLD_IFS
17819         # assume that the next FID for this client is sequential, since stdout
17820         # is unfortunately eaten by multiop_bg_pause
17821         local n=$((${fidv[1]} + 1))
17822         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
17823         if [ "$FILESET" == "" ]; then
17824                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
17825                         error "FID is missing before close"
17826         fi
17827         kill -USR1 $multi_pid
17828         # 1 second delay, so if mtime change we will see it
17829         sleep 1
17830         local mtime2=$(stat -c "%Y" $DIR/$tdir)
17831         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
17832 }
17833 run_test 185 "Volatile file support"
17834
17835 function create_check_volatile() {
17836         local idx=$1
17837         local tgt
17838
17839         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
17840         local PID=$!
17841         sleep 1
17842         local FID=$(cat /tmp/${tfile}.fid)
17843         [ "$FID" == "" ] && error "can't get FID for volatile"
17844         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
17845         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
17846         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
17847         kill -USR1 $PID
17848         wait
17849         sleep 1
17850         cancel_lru_locks mdc # flush opencache
17851         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
17852         return 0
17853 }
17854
17855 test_185a(){
17856         # LU-12516 - volatile creation via .lustre
17857         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
17858                 skip "Need MDS version at least 2.3.55"
17859
17860         create_check_volatile 0
17861         [ $MDSCOUNT -lt 2 ] && return 0
17862
17863         # DNE case
17864         create_check_volatile 1
17865
17866         return 0
17867 }
17868 run_test 185a "Volatile file creation in .lustre/fid/"
17869
17870 test_187a() {
17871         remote_mds_nodsh && skip "remote MDS with nodsh"
17872         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
17873                 skip "Need MDS version at least 2.3.0"
17874
17875         local dir0=$DIR/$tdir/$testnum
17876         mkdir -p $dir0 || error "creating dir $dir0"
17877
17878         local file=$dir0/file1
17879         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
17880         local dv1=$($LFS data_version $file)
17881         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
17882         local dv2=$($LFS data_version $file)
17883         [[ $dv1 != $dv2 ]] ||
17884                 error "data version did not change on write $dv1 == $dv2"
17885
17886         # clean up
17887         rm -f $file1
17888 }
17889 run_test 187a "Test data version change"
17890
17891 test_187b() {
17892         remote_mds_nodsh && skip "remote MDS with nodsh"
17893         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
17894                 skip "Need MDS version at least 2.3.0"
17895
17896         local dir0=$DIR/$tdir/$testnum
17897         mkdir -p $dir0 || error "creating dir $dir0"
17898
17899         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
17900         [[ ${DV[0]} != ${DV[1]} ]] ||
17901                 error "data version did not change on write"\
17902                       " ${DV[0]} == ${DV[1]}"
17903
17904         # clean up
17905         rm -f $file1
17906 }
17907 run_test 187b "Test data version change on volatile file"
17908
17909 test_200() {
17910         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17911         remote_mgs_nodsh && skip "remote MGS with nodsh"
17912         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
17913
17914         local POOL=${POOL:-cea1}
17915         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
17916         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
17917         # Pool OST targets
17918         local first_ost=0
17919         local last_ost=$(($OSTCOUNT - 1))
17920         local ost_step=2
17921         local ost_list=$(seq $first_ost $ost_step $last_ost)
17922         local ost_range="$first_ost $last_ost $ost_step"
17923         local test_path=$POOL_ROOT/$POOL_DIR_NAME
17924         local file_dir=$POOL_ROOT/file_tst
17925         local subdir=$test_path/subdir
17926         local rc=0
17927
17928         while : ; do
17929                 # former test_200a test_200b
17930                 pool_add $POOL                          || { rc=$? ; break; }
17931                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
17932                 # former test_200c test_200d
17933                 mkdir -p $test_path
17934                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
17935                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
17936                 mkdir -p $subdir
17937                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
17938                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
17939                                                         || { rc=$? ; break; }
17940                 # former test_200e test_200f
17941                 local files=$((OSTCOUNT*3))
17942                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
17943                                                         || { rc=$? ; break; }
17944                 pool_create_files $POOL $file_dir $files "$ost_list" \
17945                                                         || { rc=$? ; break; }
17946                 # former test_200g test_200h
17947                 pool_lfs_df $POOL                       || { rc=$? ; break; }
17948                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
17949
17950                 # former test_201a test_201b test_201c
17951                 pool_remove_first_target $POOL          || { rc=$? ; break; }
17952
17953                 local f=$test_path/$tfile
17954                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
17955                 pool_remove $POOL $f                    || { rc=$? ; break; }
17956                 break
17957         done
17958
17959         destroy_test_pools
17960
17961         return $rc
17962 }
17963 run_test 200 "OST pools"
17964
17965 # usage: default_attr <count | size | offset>
17966 default_attr() {
17967         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
17968 }
17969
17970 # usage: check_default_stripe_attr
17971 check_default_stripe_attr() {
17972         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
17973         case $1 in
17974         --stripe-count|-c)
17975                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
17976         --stripe-size|-S)
17977                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
17978         --stripe-index|-i)
17979                 EXPECTED=-1;;
17980         *)
17981                 error "unknown getstripe attr '$1'"
17982         esac
17983
17984         [ $ACTUAL == $EXPECTED ] ||
17985                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
17986 }
17987
17988 test_204a() {
17989         test_mkdir $DIR/$tdir
17990         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
17991
17992         check_default_stripe_attr --stripe-count
17993         check_default_stripe_attr --stripe-size
17994         check_default_stripe_attr --stripe-index
17995 }
17996 run_test 204a "Print default stripe attributes"
17997
17998 test_204b() {
17999         test_mkdir $DIR/$tdir
18000         $LFS setstripe --stripe-count 1 $DIR/$tdir
18001
18002         check_default_stripe_attr --stripe-size
18003         check_default_stripe_attr --stripe-index
18004 }
18005 run_test 204b "Print default stripe size and offset"
18006
18007 test_204c() {
18008         test_mkdir $DIR/$tdir
18009         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18010
18011         check_default_stripe_attr --stripe-count
18012         check_default_stripe_attr --stripe-index
18013 }
18014 run_test 204c "Print default stripe count and offset"
18015
18016 test_204d() {
18017         test_mkdir $DIR/$tdir
18018         $LFS setstripe --stripe-index 0 $DIR/$tdir
18019
18020         check_default_stripe_attr --stripe-count
18021         check_default_stripe_attr --stripe-size
18022 }
18023 run_test 204d "Print default stripe count and size"
18024
18025 test_204e() {
18026         test_mkdir $DIR/$tdir
18027         $LFS setstripe -d $DIR/$tdir
18028
18029         check_default_stripe_attr --stripe-count --raw
18030         check_default_stripe_attr --stripe-size --raw
18031         check_default_stripe_attr --stripe-index --raw
18032 }
18033 run_test 204e "Print raw stripe attributes"
18034
18035 test_204f() {
18036         test_mkdir $DIR/$tdir
18037         $LFS setstripe --stripe-count 1 $DIR/$tdir
18038
18039         check_default_stripe_attr --stripe-size --raw
18040         check_default_stripe_attr --stripe-index --raw
18041 }
18042 run_test 204f "Print raw stripe size and offset"
18043
18044 test_204g() {
18045         test_mkdir $DIR/$tdir
18046         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18047
18048         check_default_stripe_attr --stripe-count --raw
18049         check_default_stripe_attr --stripe-index --raw
18050 }
18051 run_test 204g "Print raw stripe count and offset"
18052
18053 test_204h() {
18054         test_mkdir $DIR/$tdir
18055         $LFS setstripe --stripe-index 0 $DIR/$tdir
18056
18057         check_default_stripe_attr --stripe-count --raw
18058         check_default_stripe_attr --stripe-size --raw
18059 }
18060 run_test 204h "Print raw stripe count and size"
18061
18062 # Figure out which job scheduler is being used, if any,
18063 # or use a fake one
18064 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
18065         JOBENV=SLURM_JOB_ID
18066 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
18067         JOBENV=LSB_JOBID
18068 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
18069         JOBENV=PBS_JOBID
18070 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
18071         JOBENV=LOADL_STEP_ID
18072 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
18073         JOBENV=JOB_ID
18074 else
18075         $LCTL list_param jobid_name > /dev/null 2>&1
18076         if [ $? -eq 0 ]; then
18077                 JOBENV=nodelocal
18078         else
18079                 JOBENV=FAKE_JOBID
18080         fi
18081 fi
18082 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
18083
18084 verify_jobstats() {
18085         local cmd=($1)
18086         shift
18087         local facets="$@"
18088
18089 # we don't really need to clear the stats for this test to work, since each
18090 # command has a unique jobid, but it makes debugging easier if needed.
18091 #       for facet in $facets; do
18092 #               local dev=$(convert_facet2label $facet)
18093 #               # clear old jobstats
18094 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
18095 #       done
18096
18097         # use a new JobID for each test, or we might see an old one
18098         [ "$JOBENV" = "FAKE_JOBID" ] &&
18099                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
18100
18101         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
18102
18103         [ "$JOBENV" = "nodelocal" ] && {
18104                 FAKE_JOBID=id.$testnum.%e.$RANDOM
18105                 $LCTL set_param jobid_name=$FAKE_JOBID
18106                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
18107         }
18108
18109         log "Test: ${cmd[*]}"
18110         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
18111
18112         if [ $JOBENV = "FAKE_JOBID" ]; then
18113                 FAKE_JOBID=$JOBVAL ${cmd[*]}
18114         else
18115                 ${cmd[*]}
18116         fi
18117
18118         # all files are created on OST0000
18119         for facet in $facets; do
18120                 local stats="*.$(convert_facet2label $facet).job_stats"
18121
18122                 # strip out libtool wrappers for in-tree executables
18123                 if [ $(do_facet $facet lctl get_param $stats |
18124                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
18125                         do_facet $facet lctl get_param $stats
18126                         error "No jobstats for $JOBVAL found on $facet::$stats"
18127                 fi
18128         done
18129 }
18130
18131 jobstats_set() {
18132         local new_jobenv=$1
18133
18134         set_persistent_param_and_check client "jobid_var" \
18135                 "$FSNAME.sys.jobid_var" $new_jobenv
18136 }
18137
18138 test_205a() { # Job stats
18139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18140         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
18141                 skip "Need MDS version with at least 2.7.1"
18142         remote_mgs_nodsh && skip "remote MGS with nodsh"
18143         remote_mds_nodsh && skip "remote MDS with nodsh"
18144         remote_ost_nodsh && skip "remote OST with nodsh"
18145         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
18146                 skip "Server doesn't support jobstats"
18147         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
18148
18149         local old_jobenv=$($LCTL get_param -n jobid_var)
18150         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
18151
18152         if [[ $PERM_CMD == *"set_param -P"* ]]; then
18153                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
18154         else
18155                 stack_trap "do_facet mgs $PERM_CMD \
18156                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
18157         fi
18158         changelog_register
18159
18160         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
18161                                 mdt.*.job_cleanup_interval | head -n 1)
18162         local new_interval=5
18163         do_facet $SINGLEMDS \
18164                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
18165         stack_trap "do_facet $SINGLEMDS \
18166                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
18167         local start=$SECONDS
18168
18169         local cmd
18170         # mkdir
18171         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir"
18172         verify_jobstats "$cmd" "$SINGLEMDS"
18173         # rmdir
18174         cmd="rmdir $DIR/$tdir"
18175         verify_jobstats "$cmd" "$SINGLEMDS"
18176         # mkdir on secondary MDT
18177         if [ $MDSCOUNT -gt 1 ]; then
18178                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
18179                 verify_jobstats "$cmd" "mds2"
18180         fi
18181         # mknod
18182         cmd="mknod $DIR/$tfile c 1 3"
18183         verify_jobstats "$cmd" "$SINGLEMDS"
18184         # unlink
18185         cmd="rm -f $DIR/$tfile"
18186         verify_jobstats "$cmd" "$SINGLEMDS"
18187         # create all files on OST0000 so verify_jobstats can find OST stats
18188         # open & close
18189         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
18190         verify_jobstats "$cmd" "$SINGLEMDS"
18191         # setattr
18192         cmd="touch $DIR/$tfile"
18193         verify_jobstats "$cmd" "$SINGLEMDS ost1"
18194         # write
18195         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
18196         verify_jobstats "$cmd" "ost1"
18197         # read
18198         cancel_lru_locks osc
18199         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
18200         verify_jobstats "$cmd" "ost1"
18201         # truncate
18202         cmd="$TRUNCATE $DIR/$tfile 0"
18203         verify_jobstats "$cmd" "$SINGLEMDS ost1"
18204         # rename
18205         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
18206         verify_jobstats "$cmd" "$SINGLEMDS"
18207         # jobstats expiry - sleep until old stats should be expired
18208         local left=$((new_interval + 5 - (SECONDS - start)))
18209         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
18210                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
18211                         "0" $left
18212         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir.expire"
18213         verify_jobstats "$cmd" "$SINGLEMDS"
18214         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
18215             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
18216
18217         # Ensure that jobid are present in changelog (if supported by MDS)
18218         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
18219                 changelog_dump | tail -10
18220                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
18221                 [ $jobids -eq 9 ] ||
18222                         error "Wrong changelog jobid count $jobids != 9"
18223
18224                 # LU-5862
18225                 JOBENV="disable"
18226                 jobstats_set $JOBENV
18227                 touch $DIR/$tfile
18228                 changelog_dump | grep $tfile
18229                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
18230                 [ $jobids -eq 0 ] ||
18231                         error "Unexpected jobids when jobid_var=$JOBENV"
18232         fi
18233
18234         # test '%j' access to environment variable - if supported
18235         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"; then
18236                 JOBENV="JOBCOMPLEX"
18237                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18238
18239                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18240         fi
18241
18242         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%H.E"; then
18243                 JOBENV="JOBCOMPLEX"
18244                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname -s).E"
18245
18246                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18247         fi
18248
18249         # test '%j' access to per-session jobid - if supported
18250         if lctl list_param jobid_this_session > /dev/null 2>&1
18251         then
18252                 lctl set_param jobid_var=session jobid_name="S.%j.%e.%u.%h.E"
18253                 lctl set_param jobid_this_session=$USER
18254
18255                 JOBENV="JOBCOMPLEX"
18256                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18257
18258                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18259         fi
18260 }
18261 run_test 205a "Verify job stats"
18262
18263 # LU-13117, LU-13597
18264 test_205b() {
18265         (( $MDS1_VERSION >= $(version_code 2.13.54.91) )) ||
18266                 skip "Need MDS version at least 2.13.54.91"
18267
18268         job_stats="mdt.*.job_stats"
18269         $LCTL set_param $job_stats=clear
18270         # Setting jobid_var to USER might not be supported
18271         $LCTL set_param jobid_var=USER || true
18272         $LCTL set_param jobid_name="%e.%u"
18273         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
18274         do_facet $SINGLEMDS $LCTL get_param $job_stats |
18275                 grep "job_id:.*foolish" &&
18276                         error "Unexpected jobid found"
18277         do_facet $SINGLEMDS $LCTL get_param $job_stats |
18278                 grep "open:.*min.*max.*sum" ||
18279                         error "wrong job_stats format found"
18280 }
18281 run_test 205b "Verify job stats jobid and output format"
18282
18283 # LU-13733
18284 test_205c() {
18285         $LCTL set_param llite.*.stats=0
18286         dd if=/dev/zero of=$DIR/$tfile.1 bs=4k count=1
18287         $LCTL get_param llite.*.stats
18288         $LCTL get_param llite.*.stats | grep \
18289                 "write_bytes *1 samples \[bytes\] 4096 4096 4096 16777216" ||
18290                         error "wrong client stats format found"
18291 }
18292 run_test 205c "Verify client stats format"
18293
18294 # LU-1480, LU-1773 and LU-1657
18295 test_206() {
18296         mkdir -p $DIR/$tdir
18297         $LFS setstripe -c -1 $DIR/$tdir
18298 #define OBD_FAIL_LOV_INIT 0x1403
18299         $LCTL set_param fail_loc=0xa0001403
18300         $LCTL set_param fail_val=1
18301         touch $DIR/$tdir/$tfile || true
18302 }
18303 run_test 206 "fail lov_init_raid0() doesn't lbug"
18304
18305 test_207a() {
18306         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
18307         local fsz=`stat -c %s $DIR/$tfile`
18308         cancel_lru_locks mdc
18309
18310         # do not return layout in getattr intent
18311 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
18312         $LCTL set_param fail_loc=0x170
18313         local sz=`stat -c %s $DIR/$tfile`
18314
18315         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
18316
18317         rm -rf $DIR/$tfile
18318 }
18319 run_test 207a "can refresh layout at glimpse"
18320
18321 test_207b() {
18322         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
18323         local cksum=`md5sum $DIR/$tfile`
18324         local fsz=`stat -c %s $DIR/$tfile`
18325         cancel_lru_locks mdc
18326         cancel_lru_locks osc
18327
18328         # do not return layout in getattr intent
18329 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
18330         $LCTL set_param fail_loc=0x171
18331
18332         # it will refresh layout after the file is opened but before read issues
18333         echo checksum is "$cksum"
18334         echo "$cksum" |md5sum -c --quiet || error "file differs"
18335
18336         rm -rf $DIR/$tfile
18337 }
18338 run_test 207b "can refresh layout at open"
18339
18340 test_208() {
18341         # FIXME: in this test suite, only RD lease is used. This is okay
18342         # for now as only exclusive open is supported. After generic lease
18343         # is done, this test suite should be revised. - Jinshan
18344
18345         remote_mds_nodsh && skip "remote MDS with nodsh"
18346         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
18347                 skip "Need MDS version at least 2.4.52"
18348
18349         echo "==== test 1: verify get lease work"
18350         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
18351
18352         echo "==== test 2: verify lease can be broken by upcoming open"
18353         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
18354         local PID=$!
18355         sleep 1
18356
18357         $MULTIOP $DIR/$tfile oO_RDWR:c
18358         kill -USR1 $PID && wait $PID || error "break lease error"
18359
18360         echo "==== test 3: verify lease can't be granted if an open already exists"
18361         $MULTIOP $DIR/$tfile oO_RDWR:_c &
18362         local PID=$!
18363         sleep 1
18364
18365         $MULTIOP $DIR/$tfile oO_RDWR:eReUc && error "apply lease should fail"
18366         kill -USR1 $PID && wait $PID || error "open file error"
18367
18368         echo "==== test 4: lease can sustain over recovery"
18369         $MULTIOP $DIR/$tfile oO_RDWR:eR_E+eUc &
18370         PID=$!
18371         sleep 1
18372
18373         fail mds1
18374
18375         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
18376
18377         echo "==== test 5: lease broken can't be regained by replay"
18378         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
18379         PID=$!
18380         sleep 1
18381
18382         # open file to break lease and then recovery
18383         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
18384         fail mds1
18385
18386         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
18387
18388         rm -f $DIR/$tfile
18389 }
18390 run_test 208 "Exclusive open"
18391
18392 test_209() {
18393         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
18394                 skip_env "must have disp_stripe"
18395
18396         touch $DIR/$tfile
18397         sync; sleep 5; sync;
18398
18399         echo 3 > /proc/sys/vm/drop_caches
18400         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
18401                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
18402         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
18403
18404         # open/close 500 times
18405         for i in $(seq 500); do
18406                 cat $DIR/$tfile
18407         done
18408
18409         echo 3 > /proc/sys/vm/drop_caches
18410         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
18411                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
18412         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
18413
18414         echo "before: $req_before, after: $req_after"
18415         [ $((req_after - req_before)) -ge 300 ] &&
18416                 error "open/close requests are not freed"
18417         return 0
18418 }
18419 run_test 209 "read-only open/close requests should be freed promptly"
18420
18421 test_210() {
18422         local pid
18423
18424         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
18425         pid=$!
18426         sleep 1
18427
18428         $LFS getstripe $DIR/$tfile
18429         kill -USR1 $pid
18430         wait $pid || error "multiop failed"
18431
18432         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
18433         pid=$!
18434         sleep 1
18435
18436         $LFS getstripe $DIR/$tfile
18437         kill -USR1 $pid
18438         wait $pid || error "multiop failed"
18439 }
18440 run_test 210 "lfs getstripe does not break leases"
18441
18442 test_212() {
18443         size=`date +%s`
18444         size=$((size % 8192 + 1))
18445         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
18446         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
18447         rm -f $DIR/f212 $DIR/f212.xyz
18448 }
18449 run_test 212 "Sendfile test ============================================"
18450
18451 test_213() {
18452         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
18453         cancel_lru_locks osc
18454         lctl set_param fail_loc=0x8000040f
18455         # generate a read lock
18456         cat $DIR/$tfile > /dev/null
18457         # write to the file, it will try to cancel the above read lock.
18458         cat /etc/hosts >> $DIR/$tfile
18459 }
18460 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
18461
18462 test_214() { # for bug 20133
18463         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
18464         for (( i=0; i < 340; i++ )) ; do
18465                 touch $DIR/$tdir/d214c/a$i
18466         done
18467
18468         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
18469         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
18470         ls $DIR/d214c || error "ls $DIR/d214c failed"
18471         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
18472         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
18473 }
18474 run_test 214 "hash-indexed directory test - bug 20133"
18475
18476 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
18477 create_lnet_proc_files() {
18478         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
18479 }
18480
18481 # counterpart of create_lnet_proc_files
18482 remove_lnet_proc_files() {
18483         rm -f $TMP/lnet_$1.sys
18484 }
18485
18486 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
18487 # 3rd arg as regexp for body
18488 check_lnet_proc_stats() {
18489         local l=$(cat "$TMP/lnet_$1" |wc -l)
18490         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
18491
18492         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
18493 }
18494
18495 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
18496 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
18497 # optional and can be regexp for 2nd line (lnet.routes case)
18498 check_lnet_proc_entry() {
18499         local blp=2          # blp stands for 'position of 1st line of body'
18500         [ -z "$5" ] || blp=3 # lnet.routes case
18501
18502         local l=$(cat "$TMP/lnet_$1" |wc -l)
18503         # subtracting one from $blp because the body can be empty
18504         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
18505
18506         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
18507                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
18508
18509         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
18510                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
18511
18512         # bail out if any unexpected line happened
18513         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
18514         [ "$?" != 0 ] || error "$2 misformatted"
18515 }
18516
18517 test_215() { # for bugs 18102, 21079, 21517
18518         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18519
18520         local N='(0|[1-9][0-9]*)'       # non-negative numeric
18521         local P='[1-9][0-9]*'           # positive numeric
18522         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
18523         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
18524         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
18525         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
18526
18527         local L1 # regexp for 1st line
18528         local L2 # regexp for 2nd line (optional)
18529         local BR # regexp for the rest (body)
18530
18531         # lnet.stats should look as 11 space-separated non-negative numerics
18532         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
18533         create_lnet_proc_files "stats"
18534         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
18535         remove_lnet_proc_files "stats"
18536
18537         # lnet.routes should look like this:
18538         # Routing disabled/enabled
18539         # net hops priority state router
18540         # where net is a string like tcp0, hops > 0, priority >= 0,
18541         # state is up/down,
18542         # router is a string like 192.168.1.1@tcp2
18543         L1="^Routing (disabled|enabled)$"
18544         L2="^net +hops +priority +state +router$"
18545         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
18546         create_lnet_proc_files "routes"
18547         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
18548         remove_lnet_proc_files "routes"
18549
18550         # lnet.routers should look like this:
18551         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
18552         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
18553         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
18554         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
18555         L1="^ref +rtr_ref +alive +router$"
18556         BR="^$P +$P +(up|down) +$NID$"
18557         create_lnet_proc_files "routers"
18558         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
18559         remove_lnet_proc_files "routers"
18560
18561         # lnet.peers should look like this:
18562         # nid refs state last max rtr min tx min queue
18563         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
18564         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
18565         # numeric (0 or >0 or <0), queue >= 0.
18566         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
18567         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
18568         create_lnet_proc_files "peers"
18569         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
18570         remove_lnet_proc_files "peers"
18571
18572         # lnet.buffers  should look like this:
18573         # pages count credits min
18574         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
18575         L1="^pages +count +credits +min$"
18576         BR="^ +$N +$N +$I +$I$"
18577         create_lnet_proc_files "buffers"
18578         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
18579         remove_lnet_proc_files "buffers"
18580
18581         # lnet.nis should look like this:
18582         # nid status alive refs peer rtr max tx min
18583         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
18584         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
18585         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
18586         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
18587         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
18588         create_lnet_proc_files "nis"
18589         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
18590         remove_lnet_proc_files "nis"
18591
18592         # can we successfully write to lnet.stats?
18593         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
18594 }
18595 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
18596
18597 test_216() { # bug 20317
18598         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18599         remote_ost_nodsh && skip "remote OST with nodsh"
18600
18601         local node
18602         local facets=$(get_facets OST)
18603         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
18604
18605         save_lustre_params client "osc.*.contention_seconds" > $p
18606         save_lustre_params $facets \
18607                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
18608         save_lustre_params $facets \
18609                 "ldlm.namespaces.filter-*.contended_locks" >> $p
18610         save_lustre_params $facets \
18611                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
18612         clear_stats osc.*.osc_stats
18613
18614         # agressive lockless i/o settings
18615         do_nodes $(comma_list $(osts_nodes)) \
18616                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
18617                         ldlm.namespaces.filter-*.contended_locks=0 \
18618                         ldlm.namespaces.filter-*.contention_seconds=60"
18619         lctl set_param -n osc.*.contention_seconds=60
18620
18621         $DIRECTIO write $DIR/$tfile 0 10 4096
18622         $CHECKSTAT -s 40960 $DIR/$tfile
18623
18624         # disable lockless i/o
18625         do_nodes $(comma_list $(osts_nodes)) \
18626                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
18627                         ldlm.namespaces.filter-*.contended_locks=32 \
18628                         ldlm.namespaces.filter-*.contention_seconds=0"
18629         lctl set_param -n osc.*.contention_seconds=0
18630         clear_stats osc.*.osc_stats
18631
18632         dd if=/dev/zero of=$DIR/$tfile count=0
18633         $CHECKSTAT -s 0 $DIR/$tfile
18634
18635         restore_lustre_params <$p
18636         rm -f $p
18637         rm $DIR/$tfile
18638 }
18639 run_test 216 "check lockless direct write updates file size and kms correctly"
18640
18641 test_217() { # bug 22430
18642         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18643
18644         local node
18645         local nid
18646
18647         for node in $(nodes_list); do
18648                 nid=$(host_nids_address $node $NETTYPE)
18649                 if [[ $nid = *-* ]] ; then
18650                         echo "lctl ping $(h2nettype $nid)"
18651                         lctl ping $(h2nettype $nid)
18652                 else
18653                         echo "skipping $node (no hyphen detected)"
18654                 fi
18655         done
18656 }
18657 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
18658
18659 test_218() {
18660        # do directio so as not to populate the page cache
18661        log "creating a 10 Mb file"
18662        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
18663        log "starting reads"
18664        dd if=$DIR/$tfile of=/dev/null bs=4096 &
18665        log "truncating the file"
18666        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
18667        log "killing dd"
18668        kill %+ || true # reads might have finished
18669        echo "wait until dd is finished"
18670        wait
18671        log "removing the temporary file"
18672        rm -rf $DIR/$tfile || error "tmp file removal failed"
18673 }
18674 run_test 218 "parallel read and truncate should not deadlock"
18675
18676 test_219() {
18677         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18678
18679         # write one partial page
18680         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
18681         # set no grant so vvp_io_commit_write will do sync write
18682         $LCTL set_param fail_loc=0x411
18683         # write a full page at the end of file
18684         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
18685
18686         $LCTL set_param fail_loc=0
18687         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
18688         $LCTL set_param fail_loc=0x411
18689         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
18690
18691         # LU-4201
18692         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
18693         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
18694 }
18695 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
18696
18697 test_220() { #LU-325
18698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18699         remote_ost_nodsh && skip "remote OST with nodsh"
18700         remote_mds_nodsh && skip "remote MDS with nodsh"
18701         remote_mgs_nodsh && skip "remote MGS with nodsh"
18702
18703         local OSTIDX=0
18704
18705         # create on MDT0000 so the last_id and next_id are correct
18706         mkdir_on_mdt0 $DIR/$tdir
18707         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
18708         OST=${OST%_UUID}
18709
18710         # on the mdt's osc
18711         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
18712         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
18713                         osp.$mdtosc_proc1.prealloc_last_id)
18714         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
18715                         osp.$mdtosc_proc1.prealloc_next_id)
18716
18717         $LFS df -i
18718
18719         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
18720         #define OBD_FAIL_OST_ENOINO              0x229
18721         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
18722         create_pool $FSNAME.$TESTNAME || return 1
18723         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
18724
18725         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
18726
18727         MDSOBJS=$((last_id - next_id))
18728         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
18729
18730         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
18731         echo "OST still has $count kbytes free"
18732
18733         echo "create $MDSOBJS files @next_id..."
18734         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
18735
18736         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
18737                         osp.$mdtosc_proc1.prealloc_last_id)
18738         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
18739                         osp.$mdtosc_proc1.prealloc_next_id)
18740
18741         echo "after creation, last_id=$last_id2, next_id=$next_id2"
18742         $LFS df -i
18743
18744         echo "cleanup..."
18745
18746         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
18747         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
18748
18749         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
18750                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
18751         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
18752                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
18753         echo "unlink $MDSOBJS files @$next_id..."
18754         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
18755 }
18756 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
18757
18758 test_221() {
18759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18760
18761         dd if=`which date` of=$MOUNT/date oflag=sync
18762         chmod +x $MOUNT/date
18763
18764         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
18765         $LCTL set_param fail_loc=0x80001401
18766
18767         $MOUNT/date > /dev/null
18768         rm -f $MOUNT/date
18769 }
18770 run_test 221 "make sure fault and truncate race to not cause OOM"
18771
18772 test_222a () {
18773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18774
18775         rm -rf $DIR/$tdir
18776         test_mkdir $DIR/$tdir
18777         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18778         createmany -o $DIR/$tdir/$tfile 10
18779         cancel_lru_locks mdc
18780         cancel_lru_locks osc
18781         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
18782         $LCTL set_param fail_loc=0x31a
18783         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
18784         $LCTL set_param fail_loc=0
18785         rm -r $DIR/$tdir
18786 }
18787 run_test 222a "AGL for ls should not trigger CLIO lock failure"
18788
18789 test_222b () {
18790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18791
18792         rm -rf $DIR/$tdir
18793         test_mkdir $DIR/$tdir
18794         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18795         createmany -o $DIR/$tdir/$tfile 10
18796         cancel_lru_locks mdc
18797         cancel_lru_locks osc
18798         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
18799         $LCTL set_param fail_loc=0x31a
18800         rm -r $DIR/$tdir || error "AGL for rmdir failed"
18801         $LCTL set_param fail_loc=0
18802 }
18803 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
18804
18805 test_223 () {
18806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18807
18808         rm -rf $DIR/$tdir
18809         test_mkdir $DIR/$tdir
18810         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18811         createmany -o $DIR/$tdir/$tfile 10
18812         cancel_lru_locks mdc
18813         cancel_lru_locks osc
18814         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
18815         $LCTL set_param fail_loc=0x31b
18816         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
18817         $LCTL set_param fail_loc=0
18818         rm -r $DIR/$tdir
18819 }
18820 run_test 223 "osc reenqueue if without AGL lock granted ======================="
18821
18822 test_224a() { # LU-1039, MRP-303
18823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18824
18825         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
18826         $LCTL set_param fail_loc=0x508
18827         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
18828         $LCTL set_param fail_loc=0
18829         df $DIR
18830 }
18831 run_test 224a "Don't panic on bulk IO failure"
18832
18833 test_224b() { # LU-1039, MRP-303
18834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18835
18836         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
18837         cancel_lru_locks osc
18838         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
18839         $LCTL set_param fail_loc=0x515
18840         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
18841         $LCTL set_param fail_loc=0
18842         df $DIR
18843 }
18844 run_test 224b "Don't panic on bulk IO failure"
18845
18846 test_224c() { # LU-6441
18847         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18848         remote_mds_nodsh && skip "remote MDS with nodsh"
18849
18850         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
18851         save_writethrough $p
18852         set_cache writethrough on
18853
18854         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
18855         local at_max=$($LCTL get_param -n at_max)
18856         local timeout=$($LCTL get_param -n timeout)
18857         local test_at="at_max"
18858         local param_at="$FSNAME.sys.at_max"
18859         local test_timeout="timeout"
18860         local param_timeout="$FSNAME.sys.timeout"
18861
18862         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
18863
18864         set_persistent_param_and_check client "$test_at" "$param_at" 0
18865         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
18866
18867         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
18868         do_facet ost1 "$LCTL set_param fail_loc=0x520"
18869         $LFS setstripe -c 1 -i 0 $DIR/$tfile
18870         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
18871         sync
18872         do_facet ost1 "$LCTL set_param fail_loc=0"
18873
18874         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
18875         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
18876                 $timeout
18877
18878         $LCTL set_param -n $pages_per_rpc
18879         restore_lustre_params < $p
18880         rm -f $p
18881 }
18882 run_test 224c "Don't hang if one of md lost during large bulk RPC"
18883
18884 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
18885 test_225a () {
18886         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18887         if [ -z ${MDSSURVEY} ]; then
18888                 skip_env "mds-survey not found"
18889         fi
18890         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
18891                 skip "Need MDS version at least 2.2.51"
18892
18893         local mds=$(facet_host $SINGLEMDS)
18894         local target=$(do_nodes $mds 'lctl dl' |
18895                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
18896
18897         local cmd1="file_count=1000 thrhi=4"
18898         local cmd2="dir_count=2 layer=mdd stripe_count=0"
18899         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
18900         local cmd="$cmd1 $cmd2 $cmd3"
18901
18902         rm -f ${TMP}/mds_survey*
18903         echo + $cmd
18904         eval $cmd || error "mds-survey with zero-stripe failed"
18905         cat ${TMP}/mds_survey*
18906         rm -f ${TMP}/mds_survey*
18907 }
18908 run_test 225a "Metadata survey sanity with zero-stripe"
18909
18910 test_225b () {
18911         if [ -z ${MDSSURVEY} ]; then
18912                 skip_env "mds-survey not found"
18913         fi
18914         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
18915                 skip "Need MDS version at least 2.2.51"
18916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18917         remote_mds_nodsh && skip "remote MDS with nodsh"
18918         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
18919                 skip_env "Need to mount OST to test"
18920         fi
18921
18922         local mds=$(facet_host $SINGLEMDS)
18923         local target=$(do_nodes $mds 'lctl dl' |
18924                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
18925
18926         local cmd1="file_count=1000 thrhi=4"
18927         local cmd2="dir_count=2 layer=mdd stripe_count=1"
18928         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
18929         local cmd="$cmd1 $cmd2 $cmd3"
18930
18931         rm -f ${TMP}/mds_survey*
18932         echo + $cmd
18933         eval $cmd || error "mds-survey with stripe_count failed"
18934         cat ${TMP}/mds_survey*
18935         rm -f ${TMP}/mds_survey*
18936 }
18937 run_test 225b "Metadata survey sanity with stripe_count = 1"
18938
18939 mcreate_path2fid () {
18940         local mode=$1
18941         local major=$2
18942         local minor=$3
18943         local name=$4
18944         local desc=$5
18945         local path=$DIR/$tdir/$name
18946         local fid
18947         local rc
18948         local fid_path
18949
18950         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
18951                 error "cannot create $desc"
18952
18953         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
18954         rc=$?
18955         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
18956
18957         fid_path=$($LFS fid2path $MOUNT $fid)
18958         rc=$?
18959         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
18960
18961         [ "$path" == "$fid_path" ] ||
18962                 error "fid2path returned $fid_path, expected $path"
18963
18964         echo "pass with $path and $fid"
18965 }
18966
18967 test_226a () {
18968         rm -rf $DIR/$tdir
18969         mkdir -p $DIR/$tdir
18970
18971         mcreate_path2fid 0010666 0 0 fifo "FIFO"
18972         mcreate_path2fid 0020666 1 3 null "character special file (null)"
18973         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
18974         mcreate_path2fid 0040666 0 0 dir "directory"
18975         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
18976         mcreate_path2fid 0100666 0 0 file "regular file"
18977         mcreate_path2fid 0120666 0 0 link "symbolic link"
18978         mcreate_path2fid 0140666 0 0 sock "socket"
18979 }
18980 run_test 226a "call path2fid and fid2path on files of all type"
18981
18982 test_226b () {
18983         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18984
18985         local MDTIDX=1
18986
18987         rm -rf $DIR/$tdir
18988         mkdir -p $DIR/$tdir
18989         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
18990                 error "create remote directory failed"
18991         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
18992         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
18993                                 "character special file (null)"
18994         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
18995                                 "character special file (no device)"
18996         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
18997         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
18998                                 "block special file (loop)"
18999         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
19000         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
19001         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
19002 }
19003 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
19004
19005 test_226c () {
19006         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19007         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
19008                 skip "Need MDS version at least 2.13.55"
19009
19010         local submnt=/mnt/submnt
19011         local srcfile=/etc/passwd
19012         local dstfile=$submnt/passwd
19013         local path
19014         local fid
19015
19016         rm -rf $DIR/$tdir
19017         rm -rf $submnt
19018         $LFS setdirstripe -c -1 -i 1 $DIR/$tdir ||
19019                 error "create remote directory failed"
19020         mkdir -p $submnt || error "create $submnt failed"
19021         $MOUNT_CMD $MGSNID:/$FSNAME/$tdir $submnt ||
19022                 error "mount $submnt failed"
19023         stack_trap "umount $submnt" EXIT
19024
19025         cp $srcfile $dstfile
19026         fid=$($LFS path2fid $dstfile)
19027         path=$($LFS fid2path $submnt "$fid")
19028         [ "$path" = "$dstfile" ] ||
19029                 error "fid2path $submnt $fid failed ($path != $dstfile)"
19030 }
19031 run_test 226c "call path2fid and fid2path under remote dir with subdir mount"
19032
19033 # LU-1299 Executing or running ldd on a truncated executable does not
19034 # cause an out-of-memory condition.
19035 test_227() {
19036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19037         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
19038
19039         dd if=$(which date) of=$MOUNT/date bs=1k count=1
19040         chmod +x $MOUNT/date
19041
19042         $MOUNT/date > /dev/null
19043         ldd $MOUNT/date > /dev/null
19044         rm -f $MOUNT/date
19045 }
19046 run_test 227 "running truncated executable does not cause OOM"
19047
19048 # LU-1512 try to reuse idle OI blocks
19049 test_228a() {
19050         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19051         remote_mds_nodsh && skip "remote MDS with nodsh"
19052         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19053
19054         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19055         local myDIR=$DIR/$tdir
19056
19057         mkdir -p $myDIR
19058         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19059         $LCTL set_param fail_loc=0x80001002
19060         createmany -o $myDIR/t- 10000
19061         $LCTL set_param fail_loc=0
19062         # The guard is current the largest FID holder
19063         touch $myDIR/guard
19064         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19065                     tr -d '[')
19066         local IDX=$(($SEQ % 64))
19067
19068         do_facet $SINGLEMDS sync
19069         # Make sure journal flushed.
19070         sleep 6
19071         local blk1=$(do_facet $SINGLEMDS \
19072                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19073                      grep Blockcount | awk '{print $4}')
19074
19075         # Remove old files, some OI blocks will become idle.
19076         unlinkmany $myDIR/t- 10000
19077         # Create new files, idle OI blocks should be reused.
19078         createmany -o $myDIR/t- 2000
19079         do_facet $SINGLEMDS sync
19080         # Make sure journal flushed.
19081         sleep 6
19082         local blk2=$(do_facet $SINGLEMDS \
19083                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19084                      grep Blockcount | awk '{print $4}')
19085
19086         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19087 }
19088 run_test 228a "try to reuse idle OI blocks"
19089
19090 test_228b() {
19091         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19092         remote_mds_nodsh && skip "remote MDS with nodsh"
19093         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19094
19095         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19096         local myDIR=$DIR/$tdir
19097
19098         mkdir -p $myDIR
19099         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19100         $LCTL set_param fail_loc=0x80001002
19101         createmany -o $myDIR/t- 10000
19102         $LCTL set_param fail_loc=0
19103         # The guard is current the largest FID holder
19104         touch $myDIR/guard
19105         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19106                     tr -d '[')
19107         local IDX=$(($SEQ % 64))
19108
19109         do_facet $SINGLEMDS sync
19110         # Make sure journal flushed.
19111         sleep 6
19112         local blk1=$(do_facet $SINGLEMDS \
19113                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19114                      grep Blockcount | awk '{print $4}')
19115
19116         # Remove old files, some OI blocks will become idle.
19117         unlinkmany $myDIR/t- 10000
19118
19119         # stop the MDT
19120         stop $SINGLEMDS || error "Fail to stop MDT."
19121         # remount the MDT
19122         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
19123
19124         df $MOUNT || error "Fail to df."
19125         # Create new files, idle OI blocks should be reused.
19126         createmany -o $myDIR/t- 2000
19127         do_facet $SINGLEMDS sync
19128         # Make sure journal flushed.
19129         sleep 6
19130         local blk2=$(do_facet $SINGLEMDS \
19131                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19132                      grep Blockcount | awk '{print $4}')
19133
19134         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19135 }
19136 run_test 228b "idle OI blocks can be reused after MDT restart"
19137
19138 #LU-1881
19139 test_228c() {
19140         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19141         remote_mds_nodsh && skip "remote MDS with nodsh"
19142         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19143
19144         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19145         local myDIR=$DIR/$tdir
19146
19147         mkdir -p $myDIR
19148         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19149         $LCTL set_param fail_loc=0x80001002
19150         # 20000 files can guarantee there are index nodes in the OI file
19151         createmany -o $myDIR/t- 20000
19152         $LCTL set_param fail_loc=0
19153         # The guard is current the largest FID holder
19154         touch $myDIR/guard
19155         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19156                     tr -d '[')
19157         local IDX=$(($SEQ % 64))
19158
19159         do_facet $SINGLEMDS sync
19160         # Make sure journal flushed.
19161         sleep 6
19162         local blk1=$(do_facet $SINGLEMDS \
19163                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19164                      grep Blockcount | awk '{print $4}')
19165
19166         # Remove old files, some OI blocks will become idle.
19167         unlinkmany $myDIR/t- 20000
19168         rm -f $myDIR/guard
19169         # The OI file should become empty now
19170
19171         # Create new files, idle OI blocks should be reused.
19172         createmany -o $myDIR/t- 2000
19173         do_facet $SINGLEMDS sync
19174         # Make sure journal flushed.
19175         sleep 6
19176         local blk2=$(do_facet $SINGLEMDS \
19177                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19178                      grep Blockcount | awk '{print $4}')
19179
19180         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19181 }
19182 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
19183
19184 test_229() { # LU-2482, LU-3448
19185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19186         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19187         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
19188                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
19189
19190         rm -f $DIR/$tfile
19191
19192         # Create a file with a released layout and stripe count 2.
19193         $MULTIOP $DIR/$tfile H2c ||
19194                 error "failed to create file with released layout"
19195
19196         $LFS getstripe -v $DIR/$tfile
19197
19198         local pattern=$($LFS getstripe -L $DIR/$tfile)
19199         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
19200
19201         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
19202                 error "getstripe"
19203         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
19204         stat $DIR/$tfile || error "failed to stat released file"
19205
19206         chown $RUNAS_ID $DIR/$tfile ||
19207                 error "chown $RUNAS_ID $DIR/$tfile failed"
19208
19209         chgrp $RUNAS_ID $DIR/$tfile ||
19210                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
19211
19212         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
19213         rm $DIR/$tfile || error "failed to remove released file"
19214 }
19215 run_test 229 "getstripe/stat/rm/attr changes work on released files"
19216
19217 test_230a() {
19218         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19219         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19220         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19221                 skip "Need MDS version at least 2.11.52"
19222
19223         local MDTIDX=1
19224
19225         test_mkdir $DIR/$tdir
19226         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
19227         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
19228         [ $mdt_idx -ne 0 ] &&
19229                 error "create local directory on wrong MDT $mdt_idx"
19230
19231         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
19232                         error "create remote directory failed"
19233         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
19234         [ $mdt_idx -ne $MDTIDX ] &&
19235                 error "create remote directory on wrong MDT $mdt_idx"
19236
19237         createmany -o $DIR/$tdir/test_230/t- 10 ||
19238                 error "create files on remote directory failed"
19239         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
19240         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
19241         rm -r $DIR/$tdir || error "unlink remote directory failed"
19242 }
19243 run_test 230a "Create remote directory and files under the remote directory"
19244
19245 test_230b() {
19246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19247         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19248         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19249                 skip "Need MDS version at least 2.11.52"
19250
19251         local MDTIDX=1
19252         local mdt_index
19253         local i
19254         local file
19255         local pid
19256         local stripe_count
19257         local migrate_dir=$DIR/$tdir/migrate_dir
19258         local other_dir=$DIR/$tdir/other_dir
19259
19260         test_mkdir $DIR/$tdir
19261         test_mkdir -i0 -c1 $migrate_dir
19262         test_mkdir -i0 -c1 $other_dir
19263         for ((i=0; i<10; i++)); do
19264                 mkdir -p $migrate_dir/dir_${i}
19265                 createmany -o $migrate_dir/dir_${i}/f 10 ||
19266                         error "create files under remote dir failed $i"
19267         done
19268
19269         cp /etc/passwd $migrate_dir/$tfile
19270         cp /etc/passwd $other_dir/$tfile
19271         chattr +SAD $migrate_dir
19272         chattr +SAD $migrate_dir/$tfile
19273
19274         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19275         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19276         local old_dir_mode=$(stat -c%f $migrate_dir)
19277         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
19278
19279         mkdir -p $migrate_dir/dir_default_stripe2
19280         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
19281         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
19282
19283         mkdir -p $other_dir
19284         ln $migrate_dir/$tfile $other_dir/luna
19285         ln $migrate_dir/$tfile $migrate_dir/sofia
19286         ln $other_dir/$tfile $migrate_dir/david
19287         ln -s $migrate_dir/$tfile $other_dir/zachary
19288         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
19289         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
19290
19291         local len
19292         local lnktgt
19293
19294         # inline symlink
19295         for len in 58 59 60; do
19296                 lnktgt=$(str_repeat 'l' $len)
19297                 touch $migrate_dir/$lnktgt
19298                 ln -s $lnktgt $migrate_dir/${len}char_ln
19299         done
19300
19301         # PATH_MAX
19302         for len in 4094 4095; do
19303                 lnktgt=$(str_repeat 'l' $len)
19304                 ln -s $lnktgt $migrate_dir/${len}char_ln
19305         done
19306
19307         # NAME_MAX
19308         for len in 254 255; do
19309                 touch $migrate_dir/$(str_repeat 'l' $len)
19310         done
19311
19312         $LFS migrate -m $MDTIDX $migrate_dir ||
19313                 error "fails on migrating remote dir to MDT1"
19314
19315         echo "migratate to MDT1, then checking.."
19316         for ((i = 0; i < 10; i++)); do
19317                 for file in $(find $migrate_dir/dir_${i}); do
19318                         mdt_index=$($LFS getstripe -m $file)
19319                         # broken symlink getstripe will fail
19320                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
19321                                 error "$file is not on MDT${MDTIDX}"
19322                 done
19323         done
19324
19325         # the multiple link file should still in MDT0
19326         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
19327         [ $mdt_index == 0 ] ||
19328                 error "$file is not on MDT${MDTIDX}"
19329
19330         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19331         [ "$old_dir_flag" = "$new_dir_flag" ] ||
19332                 error " expect $old_dir_flag get $new_dir_flag"
19333
19334         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19335         [ "$old_file_flag" = "$new_file_flag" ] ||
19336                 error " expect $old_file_flag get $new_file_flag"
19337
19338         local new_dir_mode=$(stat -c%f $migrate_dir)
19339         [ "$old_dir_mode" = "$new_dir_mode" ] ||
19340                 error "expect mode $old_dir_mode get $new_dir_mode"
19341
19342         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
19343         [ "$old_file_mode" = "$new_file_mode" ] ||
19344                 error "expect mode $old_file_mode get $new_file_mode"
19345
19346         diff /etc/passwd $migrate_dir/$tfile ||
19347                 error "$tfile different after migration"
19348
19349         diff /etc/passwd $other_dir/luna ||
19350                 error "luna different after migration"
19351
19352         diff /etc/passwd $migrate_dir/sofia ||
19353                 error "sofia different after migration"
19354
19355         diff /etc/passwd $migrate_dir/david ||
19356                 error "david different after migration"
19357
19358         diff /etc/passwd $other_dir/zachary ||
19359                 error "zachary different after migration"
19360
19361         diff /etc/passwd $migrate_dir/${tfile}_ln ||
19362                 error "${tfile}_ln different after migration"
19363
19364         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
19365                 error "${tfile}_ln_other different after migration"
19366
19367         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
19368         [ $stripe_count = 2 ] ||
19369                 error "dir strpe_count $d != 2 after migration."
19370
19371         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
19372         [ $stripe_count = 2 ] ||
19373                 error "file strpe_count $d != 2 after migration."
19374
19375         #migrate back to MDT0
19376         MDTIDX=0
19377
19378         $LFS migrate -m $MDTIDX $migrate_dir ||
19379                 error "fails on migrating remote dir to MDT0"
19380
19381         echo "migrate back to MDT0, checking.."
19382         for file in $(find $migrate_dir); do
19383                 mdt_index=$($LFS getstripe -m $file)
19384                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
19385                         error "$file is not on MDT${MDTIDX}"
19386         done
19387
19388         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19389         [ "$old_dir_flag" = "$new_dir_flag" ] ||
19390                 error " expect $old_dir_flag get $new_dir_flag"
19391
19392         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19393         [ "$old_file_flag" = "$new_file_flag" ] ||
19394                 error " expect $old_file_flag get $new_file_flag"
19395
19396         local new_dir_mode=$(stat -c%f $migrate_dir)
19397         [ "$old_dir_mode" = "$new_dir_mode" ] ||
19398                 error "expect mode $old_dir_mode get $new_dir_mode"
19399
19400         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
19401         [ "$old_file_mode" = "$new_file_mode" ] ||
19402                 error "expect mode $old_file_mode get $new_file_mode"
19403
19404         diff /etc/passwd ${migrate_dir}/$tfile ||
19405                 error "$tfile different after migration"
19406
19407         diff /etc/passwd ${other_dir}/luna ||
19408                 error "luna different after migration"
19409
19410         diff /etc/passwd ${migrate_dir}/sofia ||
19411                 error "sofia different after migration"
19412
19413         diff /etc/passwd ${other_dir}/zachary ||
19414                 error "zachary different after migration"
19415
19416         diff /etc/passwd $migrate_dir/${tfile}_ln ||
19417                 error "${tfile}_ln different after migration"
19418
19419         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
19420                 error "${tfile}_ln_other different after migration"
19421
19422         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
19423         [ $stripe_count = 2 ] ||
19424                 error "dir strpe_count $d != 2 after migration."
19425
19426         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
19427         [ $stripe_count = 2 ] ||
19428                 error "file strpe_count $d != 2 after migration."
19429
19430         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19431 }
19432 run_test 230b "migrate directory"
19433
19434 test_230c() {
19435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19436         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19437         remote_mds_nodsh && skip "remote MDS with nodsh"
19438         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19439                 skip "Need MDS version at least 2.11.52"
19440
19441         local MDTIDX=1
19442         local total=3
19443         local mdt_index
19444         local file
19445         local migrate_dir=$DIR/$tdir/migrate_dir
19446
19447         #If migrating directory fails in the middle, all entries of
19448         #the directory is still accessiable.
19449         test_mkdir $DIR/$tdir
19450         test_mkdir -i0 -c1 $migrate_dir
19451         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
19452         stat $migrate_dir
19453         createmany -o $migrate_dir/f $total ||
19454                 error "create files under ${migrate_dir} failed"
19455
19456         # fail after migrating top dir, and this will fail only once, so the
19457         # first sub file migration will fail (currently f3), others succeed.
19458         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
19459         do_facet mds1 lctl set_param fail_loc=0x1801
19460         local t=$(ls $migrate_dir | wc -l)
19461         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
19462                 error "migrate should fail"
19463         local u=$(ls $migrate_dir | wc -l)
19464         [ "$u" == "$t" ] || error "$u != $t during migration"
19465
19466         # add new dir/file should succeed
19467         mkdir $migrate_dir/dir ||
19468                 error "mkdir failed under migrating directory"
19469         touch $migrate_dir/file ||
19470                 error "create file failed under migrating directory"
19471
19472         # add file with existing name should fail
19473         for file in $migrate_dir/f*; do
19474                 stat $file > /dev/null || error "stat $file failed"
19475                 $OPENFILE -f O_CREAT:O_EXCL $file &&
19476                         error "open(O_CREAT|O_EXCL) $file should fail"
19477                 $MULTIOP $file m && error "create $file should fail"
19478                 touch $DIR/$tdir/remote_dir/$tfile ||
19479                         error "touch $tfile failed"
19480                 ln $DIR/$tdir/remote_dir/$tfile $file &&
19481                         error "link $file should fail"
19482                 mdt_index=$($LFS getstripe -m $file)
19483                 if [ $mdt_index == 0 ]; then
19484                         # file failed to migrate is not allowed to rename to
19485                         mv $DIR/$tdir/remote_dir/$tfile $file &&
19486                                 error "rename to $file should fail"
19487                 else
19488                         mv $DIR/$tdir/remote_dir/$tfile $file ||
19489                                 error "rename to $file failed"
19490                 fi
19491                 echo hello >> $file || error "write $file failed"
19492         done
19493
19494         # resume migration with different options should fail
19495         $LFS migrate -m 0 $migrate_dir &&
19496                 error "migrate -m 0 $migrate_dir should fail"
19497
19498         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
19499                 error "migrate -c 2 $migrate_dir should fail"
19500
19501         # resume migration should succeed
19502         $LFS migrate -m $MDTIDX $migrate_dir ||
19503                 error "migrate $migrate_dir failed"
19504
19505         echo "Finish migration, then checking.."
19506         for file in $(find $migrate_dir); do
19507                 mdt_index=$($LFS getstripe -m $file)
19508                 [ $mdt_index == $MDTIDX ] ||
19509                         error "$file is not on MDT${MDTIDX}"
19510         done
19511
19512         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19513 }
19514 run_test 230c "check directory accessiblity if migration failed"
19515
19516 test_230d() {
19517         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19518         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19519         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19520                 skip "Need MDS version at least 2.11.52"
19521         # LU-11235
19522         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
19523
19524         local migrate_dir=$DIR/$tdir/migrate_dir
19525         local old_index
19526         local new_index
19527         local old_count
19528         local new_count
19529         local new_hash
19530         local mdt_index
19531         local i
19532         local j
19533
19534         old_index=$((RANDOM % MDSCOUNT))
19535         old_count=$((MDSCOUNT - old_index))
19536         new_index=$((RANDOM % MDSCOUNT))
19537         new_count=$((MDSCOUNT - new_index))
19538         new_hash=1 # for all_char
19539
19540         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
19541         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
19542
19543         test_mkdir $DIR/$tdir
19544         test_mkdir -i $old_index -c $old_count $migrate_dir
19545
19546         for ((i=0; i<100; i++)); do
19547                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
19548                 createmany -o $migrate_dir/dir_${i}/f 100 ||
19549                         error "create files under remote dir failed $i"
19550         done
19551
19552         echo -n "Migrate from MDT$old_index "
19553         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
19554         echo -n "to MDT$new_index"
19555         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
19556         echo
19557
19558         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
19559         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
19560                 error "migrate remote dir error"
19561
19562         echo "Finish migration, then checking.."
19563         for file in $(find $migrate_dir); do
19564                 mdt_index=$($LFS getstripe -m $file)
19565                 if [ $mdt_index -lt $new_index ] ||
19566                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
19567                         error "$file is on MDT$mdt_index"
19568                 fi
19569         done
19570
19571         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19572 }
19573 run_test 230d "check migrate big directory"
19574
19575 test_230e() {
19576         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19577         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19578         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19579                 skip "Need MDS version at least 2.11.52"
19580
19581         local i
19582         local j
19583         local a_fid
19584         local b_fid
19585
19586         mkdir_on_mdt0 $DIR/$tdir
19587         mkdir $DIR/$tdir/migrate_dir
19588         mkdir $DIR/$tdir/other_dir
19589         touch $DIR/$tdir/migrate_dir/a
19590         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
19591         ls $DIR/$tdir/other_dir
19592
19593         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19594                 error "migrate dir fails"
19595
19596         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
19597         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
19598
19599         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19600         [ $mdt_index == 0 ] || error "a is not on MDT0"
19601
19602         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
19603                 error "migrate dir fails"
19604
19605         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
19606         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
19607
19608         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19609         [ $mdt_index == 1 ] || error "a is not on MDT1"
19610
19611         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
19612         [ $mdt_index == 1 ] || error "b is not on MDT1"
19613
19614         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
19615         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
19616
19617         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
19618
19619         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19620 }
19621 run_test 230e "migrate mulitple local link files"
19622
19623 test_230f() {
19624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19625         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19626         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19627                 skip "Need MDS version at least 2.11.52"
19628
19629         local a_fid
19630         local ln_fid
19631
19632         mkdir -p $DIR/$tdir
19633         mkdir $DIR/$tdir/migrate_dir
19634         $LFS mkdir -i1 $DIR/$tdir/other_dir
19635         touch $DIR/$tdir/migrate_dir/a
19636         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
19637         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
19638         ls $DIR/$tdir/other_dir
19639
19640         # a should be migrated to MDT1, since no other links on MDT0
19641         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19642                 error "#1 migrate dir fails"
19643         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
19644         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
19645         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19646         [ $mdt_index == 1 ] || error "a is not on MDT1"
19647
19648         # a should stay on MDT1, because it is a mulitple link file
19649         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
19650                 error "#2 migrate dir fails"
19651         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19652         [ $mdt_index == 1 ] || error "a is not on MDT1"
19653
19654         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19655                 error "#3 migrate dir fails"
19656
19657         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
19658         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
19659         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
19660
19661         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
19662         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
19663
19664         # a should be migrated to MDT0, since no other links on MDT1
19665         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
19666                 error "#4 migrate dir fails"
19667         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19668         [ $mdt_index == 0 ] || error "a is not on MDT0"
19669
19670         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19671 }
19672 run_test 230f "migrate mulitple remote link files"
19673
19674 test_230g() {
19675         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19676         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19677         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19678                 skip "Need MDS version at least 2.11.52"
19679
19680         mkdir -p $DIR/$tdir/migrate_dir
19681
19682         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
19683                 error "migrating dir to non-exist MDT succeeds"
19684         true
19685 }
19686 run_test 230g "migrate dir to non-exist MDT"
19687
19688 test_230h() {
19689         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19690         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19691         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19692                 skip "Need MDS version at least 2.11.52"
19693
19694         local mdt_index
19695
19696         mkdir -p $DIR/$tdir/migrate_dir
19697
19698         $LFS migrate -m1 $DIR &&
19699                 error "migrating mountpoint1 should fail"
19700
19701         $LFS migrate -m1 $DIR/$tdir/.. &&
19702                 error "migrating mountpoint2 should fail"
19703
19704         # same as mv
19705         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
19706                 error "migrating $tdir/migrate_dir/.. should fail"
19707
19708         true
19709 }
19710 run_test 230h "migrate .. and root"
19711
19712 test_230i() {
19713         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19714         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19715         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19716                 skip "Need MDS version at least 2.11.52"
19717
19718         mkdir -p $DIR/$tdir/migrate_dir
19719
19720         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
19721                 error "migration fails with a tailing slash"
19722
19723         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
19724                 error "migration fails with two tailing slashes"
19725 }
19726 run_test 230i "lfs migrate -m tolerates trailing slashes"
19727
19728 test_230j() {
19729         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19730         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
19731                 skip "Need MDS version at least 2.11.52"
19732
19733         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
19734         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
19735                 error "create $tfile failed"
19736         cat /etc/passwd > $DIR/$tdir/$tfile
19737
19738         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
19739
19740         cmp /etc/passwd $DIR/$tdir/$tfile ||
19741                 error "DoM file mismatch after migration"
19742 }
19743 run_test 230j "DoM file data not changed after dir migration"
19744
19745 test_230k() {
19746         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
19747         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19748                 skip "Need MDS version at least 2.11.56"
19749
19750         local total=20
19751         local files_on_starting_mdt=0
19752
19753         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
19754         $LFS getdirstripe $DIR/$tdir
19755         for i in $(seq $total); do
19756                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
19757                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
19758                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19759         done
19760
19761         echo "$files_on_starting_mdt files on MDT0"
19762
19763         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
19764         $LFS getdirstripe $DIR/$tdir
19765
19766         files_on_starting_mdt=0
19767         for i in $(seq $total); do
19768                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
19769                         error "file $tfile.$i mismatch after migration"
19770                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
19771                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19772         done
19773
19774         echo "$files_on_starting_mdt files on MDT1 after migration"
19775         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
19776
19777         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
19778         $LFS getdirstripe $DIR/$tdir
19779
19780         files_on_starting_mdt=0
19781         for i in $(seq $total); do
19782                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
19783                         error "file $tfile.$i mismatch after 2nd migration"
19784                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
19785                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19786         done
19787
19788         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
19789         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
19790
19791         true
19792 }
19793 run_test 230k "file data not changed after dir migration"
19794
19795 test_230l() {
19796         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19797         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19798                 skip "Need MDS version at least 2.11.56"
19799
19800         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
19801         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
19802                 error "create files under remote dir failed $i"
19803         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
19804 }
19805 run_test 230l "readdir between MDTs won't crash"
19806
19807 test_230m() {
19808         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19809         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19810                 skip "Need MDS version at least 2.11.56"
19811
19812         local MDTIDX=1
19813         local mig_dir=$DIR/$tdir/migrate_dir
19814         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19815         local shortstr="b"
19816         local val
19817
19818         echo "Creating files and dirs with xattrs"
19819         test_mkdir $DIR/$tdir
19820         test_mkdir -i0 -c1 $mig_dir
19821         mkdir $mig_dir/dir
19822         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
19823                 error "cannot set xattr attr1 on dir"
19824         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
19825                 error "cannot set xattr attr2 on dir"
19826         touch $mig_dir/dir/f0
19827         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
19828                 error "cannot set xattr attr1 on file"
19829         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
19830                 error "cannot set xattr attr2 on file"
19831         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
19832         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
19833         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
19834         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
19835         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
19836         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
19837         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
19838         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
19839         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
19840
19841         echo "Migrating to MDT1"
19842         $LFS migrate -m $MDTIDX $mig_dir ||
19843                 error "fails on migrating dir to MDT1"
19844
19845         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
19846         echo "Checking xattrs"
19847         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
19848         [ "$val" = $longstr ] ||
19849                 error "expecting xattr1 $longstr on dir, found $val"
19850         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
19851         [ "$val" = $shortstr ] ||
19852                 error "expecting xattr2 $shortstr on dir, found $val"
19853         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
19854         [ "$val" = $longstr ] ||
19855                 error "expecting xattr1 $longstr on file, found $val"
19856         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
19857         [ "$val" = $shortstr ] ||
19858                 error "expecting xattr2 $shortstr on file, found $val"
19859 }
19860 run_test 230m "xattrs not changed after dir migration"
19861
19862 test_230n() {
19863         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19864         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
19865                 skip "Need MDS version at least 2.13.53"
19866
19867         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
19868         cat /etc/hosts > $DIR/$tdir/$tfile
19869         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
19870         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
19871
19872         cmp /etc/hosts $DIR/$tdir/$tfile ||
19873                 error "File data mismatch after migration"
19874 }
19875 run_test 230n "Dir migration with mirrored file"
19876
19877 test_230o() {
19878         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
19879         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
19880                 skip "Need MDS version at least 2.13.52"
19881
19882         local mdts=$(comma_list $(mdts_nodes))
19883         local timeout=100
19884         local restripe_status
19885         local delta
19886         local i
19887
19888         [[ $mds1_FSTYPE == zfs ]] && timeout=300
19889
19890         # in case "crush" hash type is not set
19891         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
19892
19893         restripe_status=$(do_facet mds1 $LCTL get_param -n \
19894                            mdt.*MDT0000.enable_dir_restripe)
19895         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
19896         stack_trap "do_nodes $mdts $LCTL set_param \
19897                     mdt.*.enable_dir_restripe=$restripe_status"
19898
19899         mkdir $DIR/$tdir
19900         createmany -m $DIR/$tdir/f 100 ||
19901                 error "create files under remote dir failed $i"
19902         createmany -d $DIR/$tdir/d 100 ||
19903                 error "create dirs under remote dir failed $i"
19904
19905         for i in $(seq 2 $MDSCOUNT); do
19906                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
19907                 $LFS setdirstripe -c $i $DIR/$tdir ||
19908                         error "split -c $i $tdir failed"
19909                 wait_update $HOSTNAME \
19910                         "$LFS getdirstripe -H $DIR/$tdir" "crush" $timeout ||
19911                         error "dir split not finished"
19912                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
19913                         awk '/migrate/ {sum += $2} END { print sum }')
19914                 echo "$delta migrated when dir split $((i - 1)) to $i stripes"
19915                 # delta is around total_files/stripe_count
19916                 (( $delta < 200 / (i - 1) + 4 )) ||
19917                         error "$delta files migrated >= $((200 / (i - 1) + 4))"
19918         done
19919 }
19920 run_test 230o "dir split"
19921
19922 test_230p() {
19923         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
19924         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
19925                 skip "Need MDS version at least 2.13.52"
19926
19927         local mdts=$(comma_list $(mdts_nodes))
19928         local timeout=100
19929         local restripe_status
19930         local delta
19931         local c
19932
19933         [[ $mds1_FSTYPE == zfs ]] && timeout=300
19934
19935         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
19936
19937         restripe_status=$(do_facet mds1 $LCTL get_param -n \
19938                            mdt.*MDT0000.enable_dir_restripe)
19939         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
19940         stack_trap "do_nodes $mdts $LCTL set_param \
19941                     mdt.*.enable_dir_restripe=$restripe_status"
19942
19943         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
19944         createmany -m $DIR/$tdir/f 100 ||
19945                 error "create files under remote dir failed"
19946         createmany -d $DIR/$tdir/d 100 ||
19947                 error "create dirs under remote dir failed"
19948
19949         for c in $(seq $((MDSCOUNT - 1)) -1 1); do
19950                 local mdt_hash="crush"
19951
19952                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
19953                 $LFS setdirstripe -c $c $DIR/$tdir ||
19954                         error "split -c $c $tdir failed"
19955                 if (( MDS1_VERSION >= $(version_code 2.14.51) )); then
19956                         mdt_hash="$mdt_hash,fixed"
19957                 elif [ $c -eq 1 ]; then
19958                         mdt_hash="none"
19959                 fi
19960                 wait_update $HOSTNAME \
19961                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash $timeout ||
19962                         error "dir merge not finished"
19963                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
19964                         awk '/migrate/ {sum += $2} END { print sum }')
19965                 echo "$delta migrated when dir merge $((c + 1)) to $c stripes"
19966                 # delta is around total_files/stripe_count
19967                 (( delta < 200 / c + 4 )) ||
19968                         error "$delta files migrated >= $((200 / c + 4))"
19969         done
19970 }
19971 run_test 230p "dir merge"
19972
19973 test_230q() {
19974         (( MDSCOUNT > 1)) || skip "needs >= 2 MDTs"
19975         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
19976                 skip "Need MDS version at least 2.13.52"
19977
19978         local mdts=$(comma_list $(mdts_nodes))
19979         local saved_threshold=$(do_facet mds1 \
19980                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
19981         local saved_delta=$(do_facet mds1 \
19982                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
19983         local threshold=100
19984         local delta=2
19985         local total=0
19986         local stripe_count=0
19987         local stripe_index
19988         local nr_files
19989         local create
19990
19991         # test with fewer files on ZFS
19992         [ "$mds1_FSTYPE" == "zfs" ] && threshold=40
19993
19994         stack_trap "do_nodes $mdts $LCTL set_param \
19995                     mdt.*.dir_split_count=$saved_threshold"
19996         stack_trap "do_nodes $mdts $LCTL set_param \
19997                     mdt.*.dir_split_delta=$saved_delta"
19998         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
19999         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
20000         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
20001         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
20002         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
20003         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20004
20005         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
20006         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20007
20008         create=$((threshold * 3 / 2))
20009         while [ $stripe_count -lt $MDSCOUNT ]; do
20010                 createmany -m $DIR/$tdir/f $total $create ||
20011                         error "create sub files failed"
20012                 stat $DIR/$tdir > /dev/null
20013                 total=$((total + create))
20014                 stripe_count=$((stripe_count + delta))
20015                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
20016
20017                 wait_update $HOSTNAME \
20018                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
20019                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
20020
20021                 wait_update $HOSTNAME \
20022                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
20023                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
20024
20025                 nr_files=$($LFS find -m 1 $DIR/$tdir | grep -c -w $stripe_index)
20026                 echo "$nr_files/$total files on MDT$stripe_index after split"
20027                 # allow 10% margin of imbalance with crush hash
20028                 (( $nr_files <= $total / $stripe_count + $create / 10)) ||
20029                         error "$nr_files files on MDT$stripe_index after split"
20030
20031                 nr_files=$($LFS find -type f $DIR/$tdir | wc -l)
20032                 [ $nr_files -eq $total ] ||
20033                         error "total sub files $nr_files != $total"
20034         done
20035
20036         (( MDS1_VERSION >= $(version_code 2.14.51) )) || return 0
20037
20038         echo "fixed layout directory won't auto split"
20039         $LFS migrate -m 0 $DIR/$tdir || error "migrate $tdir failed"
20040         wait_update $HOSTNAME "$LFS getdirstripe -H $DIR/$tdir" "crush,fixed" \
20041                 10 || error "stripe hash $($LFS getdirstripe -H $DIR/$tdir)"
20042         wait_update $HOSTNAME "$LFS getdirstripe -c $DIR/$tdir" 1 10 ||
20043                 error "stripe count $($LFS getdirstripe -c $DIR/$tdir)"
20044 }
20045 run_test 230q "dir auto split"
20046
20047 test_230r() {
20048         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
20049         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20050         [[ $MDS1_VERSION -ge $(version_code 2.13.54) ]] ||
20051                 skip "Need MDS version at least 2.13.54"
20052
20053         # maximum amount of local locks:
20054         # parent striped dir - 2 locks
20055         # new stripe in parent to migrate to - 1 lock
20056         # source and target - 2 locks
20057         # Total 5 locks for regular file
20058         mkdir -p $DIR/$tdir
20059         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
20060         touch $DIR/$tdir/dir1/eee
20061
20062         # create 4 hardlink for 4 more locks
20063         # Total: 9 locks > RS_MAX_LOCKS (8)
20064         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
20065         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
20066         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
20067         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
20068         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
20069         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
20070         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
20071         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
20072
20073         cancel_lru_locks mdc
20074
20075         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
20076                 error "migrate dir fails"
20077
20078         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20079 }
20080 run_test 230r "migrate with too many local locks"
20081
20082 test_230s() {
20083         [ $MDS1_VERSION -ge $(version_code 2.13.57) ] ||
20084                 skip "Need MDS version at least 2.13.57"
20085
20086         local mdts=$(comma_list $(mdts_nodes))
20087         local restripe_status=$(do_facet mds1 $LCTL get_param -n \
20088                                 mdt.*MDT0000.enable_dir_restripe)
20089
20090         stack_trap "do_nodes $mdts $LCTL set_param \
20091                     mdt.*.enable_dir_restripe=$restripe_status"
20092
20093         local st
20094         for st in 0 1; do
20095                 do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=$st"
20096                 test_mkdir $DIR/$tdir
20097                 $LFS mkdir $DIR/$tdir |& grep "File exists" ||
20098                         error "$LFS mkdir doesn't return -EEXIST if target exists"
20099                 rmdir $DIR/$tdir
20100         done
20101 }
20102 run_test 230s "lfs mkdir should return -EEXIST if target exists"
20103
20104 test_230t()
20105 {
20106         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20107         [[ $MDS1_VERSION -ge $(version_code 2.14.50) ]] ||
20108                 skip "Need MDS version at least 2.14.50"
20109
20110         test_mkdir $DIR/$tdir || error "mkdir $tdir failed"
20111         test_mkdir $DIR/$tdir/subdir || error "mkdir subdir failed"
20112         $LFS project -p 1 -s $DIR/$tdir ||
20113                 error "set $tdir project id failed"
20114         $LFS project -p 2 -s $DIR/$tdir/subdir ||
20115                 error "set subdir project id failed"
20116         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir || error "migrate failed"
20117 }
20118 run_test 230t "migrate directory with project ID set"
20119
20120 test_231a()
20121 {
20122         # For simplicity this test assumes that max_pages_per_rpc
20123         # is the same across all OSCs
20124         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
20125         local bulk_size=$((max_pages * PAGE_SIZE))
20126         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
20127                                        head -n 1)
20128
20129         mkdir -p $DIR/$tdir
20130         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
20131                 error "failed to set stripe with -S ${brw_size}M option"
20132
20133         # clear the OSC stats
20134         $LCTL set_param osc.*.stats=0 &>/dev/null
20135         stop_writeback
20136
20137         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
20138         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
20139                 oflag=direct &>/dev/null || error "dd failed"
20140
20141         sync; sleep 1; sync # just to be safe
20142         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
20143         if [ x$nrpcs != "x1" ]; then
20144                 $LCTL get_param osc.*.stats
20145                 error "found $nrpcs ost_write RPCs, not 1 as expected"
20146         fi
20147
20148         start_writeback
20149         # Drop the OSC cache, otherwise we will read from it
20150         cancel_lru_locks osc
20151
20152         # clear the OSC stats
20153         $LCTL set_param osc.*.stats=0 &>/dev/null
20154
20155         # Client reads $bulk_size.
20156         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
20157                 iflag=direct &>/dev/null || error "dd failed"
20158
20159         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
20160         if [ x$nrpcs != "x1" ]; then
20161                 $LCTL get_param osc.*.stats
20162                 error "found $nrpcs ost_read RPCs, not 1 as expected"
20163         fi
20164 }
20165 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
20166
20167 test_231b() {
20168         mkdir -p $DIR/$tdir
20169         local i
20170         for i in {0..1023}; do
20171                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
20172                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
20173                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
20174         done
20175         sync
20176 }
20177 run_test 231b "must not assert on fully utilized OST request buffer"
20178
20179 test_232a() {
20180         mkdir -p $DIR/$tdir
20181         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
20182
20183         #define OBD_FAIL_LDLM_OST_LVB            0x31c
20184         do_facet ost1 $LCTL set_param fail_loc=0x31c
20185
20186         # ignore dd failure
20187         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
20188
20189         do_facet ost1 $LCTL set_param fail_loc=0
20190         umount_client $MOUNT || error "umount failed"
20191         mount_client $MOUNT || error "mount failed"
20192         stop ost1 || error "cannot stop ost1"
20193         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
20194 }
20195 run_test 232a "failed lock should not block umount"
20196
20197 test_232b() {
20198         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
20199                 skip "Need MDS version at least 2.10.58"
20200
20201         mkdir -p $DIR/$tdir
20202         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
20203         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
20204         sync
20205         cancel_lru_locks osc
20206
20207         #define OBD_FAIL_LDLM_OST_LVB            0x31c
20208         do_facet ost1 $LCTL set_param fail_loc=0x31c
20209
20210         # ignore failure
20211         $LFS data_version $DIR/$tdir/$tfile || true
20212
20213         do_facet ost1 $LCTL set_param fail_loc=0
20214         umount_client $MOUNT || error "umount failed"
20215         mount_client $MOUNT || error "mount failed"
20216         stop ost1 || error "cannot stop ost1"
20217         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
20218 }
20219 run_test 232b "failed data version lock should not block umount"
20220
20221 test_233a() {
20222         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
20223                 skip "Need MDS version at least 2.3.64"
20224         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
20225
20226         local fid=$($LFS path2fid $MOUNT)
20227
20228         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20229                 error "cannot access $MOUNT using its FID '$fid'"
20230 }
20231 run_test 233a "checking that OBF of the FS root succeeds"
20232
20233 test_233b() {
20234         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
20235                 skip "Need MDS version at least 2.5.90"
20236         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
20237
20238         local fid=$($LFS path2fid $MOUNT/.lustre)
20239
20240         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20241                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
20242
20243         fid=$($LFS path2fid $MOUNT/.lustre/fid)
20244         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20245                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
20246 }
20247 run_test 233b "checking that OBF of the FS .lustre succeeds"
20248
20249 test_234() {
20250         local p="$TMP/sanityN-$TESTNAME.parameters"
20251         save_lustre_params client "llite.*.xattr_cache" > $p
20252         lctl set_param llite.*.xattr_cache 1 ||
20253                 skip_env "xattr cache is not supported"
20254
20255         mkdir -p $DIR/$tdir || error "mkdir failed"
20256         touch $DIR/$tdir/$tfile || error "touch failed"
20257         # OBD_FAIL_LLITE_XATTR_ENOMEM
20258         $LCTL set_param fail_loc=0x1405
20259         getfattr -n user.attr $DIR/$tdir/$tfile &&
20260                 error "getfattr should have failed with ENOMEM"
20261         $LCTL set_param fail_loc=0x0
20262         rm -rf $DIR/$tdir
20263
20264         restore_lustre_params < $p
20265         rm -f $p
20266 }
20267 run_test 234 "xattr cache should not crash on ENOMEM"
20268
20269 test_235() {
20270         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
20271                 skip "Need MDS version at least 2.4.52"
20272
20273         flock_deadlock $DIR/$tfile
20274         local RC=$?
20275         case $RC in
20276                 0)
20277                 ;;
20278                 124) error "process hangs on a deadlock"
20279                 ;;
20280                 *) error "error executing flock_deadlock $DIR/$tfile"
20281                 ;;
20282         esac
20283 }
20284 run_test 235 "LU-1715: flock deadlock detection does not work properly"
20285
20286 #LU-2935
20287 test_236() {
20288         check_swap_layouts_support
20289
20290         local ref1=/etc/passwd
20291         local ref2=/etc/group
20292         local file1=$DIR/$tdir/f1
20293         local file2=$DIR/$tdir/f2
20294
20295         test_mkdir -c1 $DIR/$tdir
20296         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
20297         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
20298         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
20299         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
20300         local fd=$(free_fd)
20301         local cmd="exec $fd<>$file2"
20302         eval $cmd
20303         rm $file2
20304         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
20305                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
20306         cmd="exec $fd>&-"
20307         eval $cmd
20308         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
20309
20310         #cleanup
20311         rm -rf $DIR/$tdir
20312 }
20313 run_test 236 "Layout swap on open unlinked file"
20314
20315 # LU-4659 linkea consistency
20316 test_238() {
20317         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
20318                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
20319                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
20320                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
20321
20322         touch $DIR/$tfile
20323         ln $DIR/$tfile $DIR/$tfile.lnk
20324         touch $DIR/$tfile.new
20325         mv $DIR/$tfile.new $DIR/$tfile
20326         local fid1=$($LFS path2fid $DIR/$tfile)
20327         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
20328         local path1=$($LFS fid2path $FSNAME "$fid1")
20329         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
20330         local path2=$($LFS fid2path $FSNAME "$fid2")
20331         [ $tfile.lnk == $path2 ] ||
20332                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
20333         rm -f $DIR/$tfile*
20334 }
20335 run_test 238 "Verify linkea consistency"
20336
20337 test_239A() { # was test_239
20338         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
20339                 skip "Need MDS version at least 2.5.60"
20340
20341         local list=$(comma_list $(mdts_nodes))
20342
20343         mkdir -p $DIR/$tdir
20344         createmany -o $DIR/$tdir/f- 5000
20345         unlinkmany $DIR/$tdir/f- 5000
20346         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
20347                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
20348         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
20349                         osp.*MDT*.sync_in_flight" | calc_sum)
20350         [ "$changes" -eq 0 ] || error "$changes not synced"
20351 }
20352 run_test 239A "osp_sync test"
20353
20354 test_239a() { #LU-5297
20355         remote_mds_nodsh && skip "remote MDS with nodsh"
20356
20357         touch $DIR/$tfile
20358         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
20359         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
20360         chgrp $RUNAS_GID $DIR/$tfile
20361         wait_delete_completed
20362 }
20363 run_test 239a "process invalid osp sync record correctly"
20364
20365 test_239b() { #LU-5297
20366         remote_mds_nodsh && skip "remote MDS with nodsh"
20367
20368         touch $DIR/$tfile1
20369         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
20370         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
20371         chgrp $RUNAS_GID $DIR/$tfile1
20372         wait_delete_completed
20373         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
20374         touch $DIR/$tfile2
20375         chgrp $RUNAS_GID $DIR/$tfile2
20376         wait_delete_completed
20377 }
20378 run_test 239b "process osp sync record with ENOMEM error correctly"
20379
20380 test_240() {
20381         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20382         remote_mds_nodsh && skip "remote MDS with nodsh"
20383
20384         mkdir -p $DIR/$tdir
20385
20386         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
20387                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
20388         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
20389                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
20390
20391         umount_client $MOUNT || error "umount failed"
20392         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
20393         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
20394         mount_client $MOUNT || error "failed to mount client"
20395
20396         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
20397         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
20398 }
20399 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
20400
20401 test_241_bio() {
20402         local count=$1
20403         local bsize=$2
20404
20405         for LOOP in $(seq $count); do
20406                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
20407                 cancel_lru_locks $OSC || true
20408         done
20409 }
20410
20411 test_241_dio() {
20412         local count=$1
20413         local bsize=$2
20414
20415         for LOOP in $(seq $1); do
20416                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
20417                         2>/dev/null
20418         done
20419 }
20420
20421 test_241a() { # was test_241
20422         local bsize=$PAGE_SIZE
20423
20424         (( bsize < 40960 )) && bsize=40960
20425         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
20426         ls -la $DIR/$tfile
20427         cancel_lru_locks $OSC
20428         test_241_bio 1000 $bsize &
20429         PID=$!
20430         test_241_dio 1000 $bsize
20431         wait $PID
20432 }
20433 run_test 241a "bio vs dio"
20434
20435 test_241b() {
20436         local bsize=$PAGE_SIZE
20437
20438         (( bsize < 40960 )) && bsize=40960
20439         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
20440         ls -la $DIR/$tfile
20441         test_241_dio 1000 $bsize &
20442         PID=$!
20443         test_241_dio 1000 $bsize
20444         wait $PID
20445 }
20446 run_test 241b "dio vs dio"
20447
20448 test_242() {
20449         remote_mds_nodsh && skip "remote MDS with nodsh"
20450
20451         mkdir_on_mdt0 $DIR/$tdir
20452         touch $DIR/$tdir/$tfile
20453
20454         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
20455         do_facet mds1 lctl set_param fail_loc=0x105
20456         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
20457
20458         do_facet mds1 lctl set_param fail_loc=0
20459         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
20460 }
20461 run_test 242 "mdt_readpage failure should not cause directory unreadable"
20462
20463 test_243()
20464 {
20465         test_mkdir $DIR/$tdir
20466         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
20467 }
20468 run_test 243 "various group lock tests"
20469
20470 test_244a()
20471 {
20472         test_mkdir $DIR/$tdir
20473         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
20474         sendfile_grouplock $DIR/$tdir/$tfile || \
20475                 error "sendfile+grouplock failed"
20476         rm -rf $DIR/$tdir
20477 }
20478 run_test 244a "sendfile with group lock tests"
20479
20480 test_244b()
20481 {
20482         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
20483
20484         local threads=50
20485         local size=$((1024*1024))
20486
20487         test_mkdir $DIR/$tdir
20488         for i in $(seq 1 $threads); do
20489                 local file=$DIR/$tdir/file_$((i / 10))
20490                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
20491                 local pids[$i]=$!
20492         done
20493         for i in $(seq 1 $threads); do
20494                 wait ${pids[$i]}
20495         done
20496 }
20497 run_test 244b "multi-threaded write with group lock"
20498
20499 test_245() {
20500         local flagname="multi_mod_rpcs"
20501         local connect_data_name="max_mod_rpcs"
20502         local out
20503
20504         # check if multiple modify RPCs flag is set
20505         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
20506                 grep "connect_flags:")
20507         echo "$out"
20508
20509         echo "$out" | grep -qw $flagname
20510         if [ $? -ne 0 ]; then
20511                 echo "connect flag $flagname is not set"
20512                 return
20513         fi
20514
20515         # check if multiple modify RPCs data is set
20516         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
20517         echo "$out"
20518
20519         echo "$out" | grep -qw $connect_data_name ||
20520                 error "import should have connect data $connect_data_name"
20521 }
20522 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
20523
20524 cleanup_247() {
20525         local submount=$1
20526
20527         trap 0
20528         umount_client $submount
20529         rmdir $submount
20530 }
20531
20532 test_247a() {
20533         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20534                 grep -q subtree ||
20535                 skip_env "Fileset feature is not supported"
20536
20537         local submount=${MOUNT}_$tdir
20538
20539         mkdir $MOUNT/$tdir
20540         mkdir -p $submount || error "mkdir $submount failed"
20541         FILESET="$FILESET/$tdir" mount_client $submount ||
20542                 error "mount $submount failed"
20543         trap "cleanup_247 $submount" EXIT
20544         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
20545         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
20546                 error "read $MOUNT/$tdir/$tfile failed"
20547         cleanup_247 $submount
20548 }
20549 run_test 247a "mount subdir as fileset"
20550
20551 test_247b() {
20552         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20553                 skip_env "Fileset feature is not supported"
20554
20555         local submount=${MOUNT}_$tdir
20556
20557         rm -rf $MOUNT/$tdir
20558         mkdir -p $submount || error "mkdir $submount failed"
20559         SKIP_FILESET=1
20560         FILESET="$FILESET/$tdir" mount_client $submount &&
20561                 error "mount $submount should fail"
20562         rmdir $submount
20563 }
20564 run_test 247b "mount subdir that dose not exist"
20565
20566 test_247c() {
20567         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20568                 skip_env "Fileset feature is not supported"
20569
20570         local submount=${MOUNT}_$tdir
20571
20572         mkdir -p $MOUNT/$tdir/dir1
20573         mkdir -p $submount || error "mkdir $submount failed"
20574         trap "cleanup_247 $submount" EXIT
20575         FILESET="$FILESET/$tdir" mount_client $submount ||
20576                 error "mount $submount failed"
20577         local fid=$($LFS path2fid $MOUNT/)
20578         $LFS fid2path $submount $fid && error "fid2path should fail"
20579         cleanup_247 $submount
20580 }
20581 run_test 247c "running fid2path outside subdirectory root"
20582
20583 test_247d() {
20584         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20585                 skip "Fileset feature is not supported"
20586
20587         local submount=${MOUNT}_$tdir
20588
20589         mkdir -p $MOUNT/$tdir/dir1
20590         mkdir -p $submount || error "mkdir $submount failed"
20591         FILESET="$FILESET/$tdir" mount_client $submount ||
20592                 error "mount $submount failed"
20593         trap "cleanup_247 $submount" EXIT
20594
20595         local td=$submount/dir1
20596         local fid=$($LFS path2fid $td)
20597         [ -z "$fid" ] && error "path2fid unable to get $td FID"
20598
20599         # check that we get the same pathname back
20600         local rootpath
20601         local found
20602         for rootpath in "$submount" "$submount///" "$submount/dir1"; do
20603                 echo "$rootpath $fid"
20604                 found=$($LFS fid2path $rootpath "$fid")
20605                 [ -n "found" ] || error "fid2path should succeed"
20606                 [ "$found" == "$td" ] || error "fid2path $found != $td"
20607         done
20608         # check wrong root path format
20609         rootpath=$submount"_wrong"
20610         found=$($LFS fid2path $rootpath "$fid")
20611         [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)"
20612
20613         cleanup_247 $submount
20614 }
20615 run_test 247d "running fid2path inside subdirectory root"
20616
20617 # LU-8037
20618 test_247e() {
20619         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20620                 grep -q subtree ||
20621                 skip "Fileset feature is not supported"
20622
20623         local submount=${MOUNT}_$tdir
20624
20625         mkdir $MOUNT/$tdir
20626         mkdir -p $submount || error "mkdir $submount failed"
20627         FILESET="$FILESET/.." mount_client $submount &&
20628                 error "mount $submount should fail"
20629         rmdir $submount
20630 }
20631 run_test 247e "mount .. as fileset"
20632
20633 test_247f() {
20634         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20635         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
20636                 skip "Need at least version 2.13.52"
20637         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
20638                 skip "Need at least version 2.14.50"
20639         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20640                 grep -q subtree ||
20641                 skip "Fileset feature is not supported"
20642
20643         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
20644         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
20645                 error "mkdir remote failed"
20646         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote/subdir ||
20647                 error "mkdir remote/subdir failed"
20648         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
20649                 error "mkdir striped failed"
20650         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
20651
20652         local submount=${MOUNT}_$tdir
20653
20654         mkdir -p $submount || error "mkdir $submount failed"
20655         stack_trap "rmdir $submount"
20656
20657         local dir
20658         local stat
20659         local fileset=$FILESET
20660         local mdts=$(comma_list $(mdts_nodes))
20661
20662         stat=$(do_facet mds1 $LCTL get_param -n \
20663                 mdt.*MDT0000.enable_remote_subdir_mount)
20664         stack_trap "do_nodes $mdts $LCTL set_param \
20665                 mdt.*.enable_remote_subdir_mount=$stat"
20666
20667         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
20668         stack_trap "umount_client $submount"
20669         FILESET="$fileset/$tdir/remote" mount_client $submount &&
20670                 error "mount remote dir $dir should fail"
20671
20672         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
20673                 $tdir/striped/. ; do
20674                 FILESET="$fileset/$dir" mount_client $submount ||
20675                         error "mount $dir failed"
20676                 umount_client $submount
20677         done
20678
20679         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
20680         FILESET="$fileset/$tdir/remote" mount_client $submount ||
20681                 error "mount $tdir/remote failed"
20682 }
20683 run_test 247f "mount striped or remote directory as fileset"
20684
20685 test_247g() {
20686         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
20687         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
20688                 skip "Need at least version 2.14.50"
20689
20690         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
20691                 error "mkdir $tdir failed"
20692         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
20693
20694         local submount=${MOUNT}_$tdir
20695
20696         mkdir -p $submount || error "mkdir $submount failed"
20697         stack_trap "rmdir $submount"
20698
20699         FILESET="$fileset/$tdir" mount_client $submount ||
20700                 error "mount $dir failed"
20701         stack_trap "umount $submount"
20702
20703         local mdts=$(comma_list $(mdts_nodes))
20704
20705         local nrpcs
20706
20707         stat $submount > /dev/null
20708         cancel_lru_locks $MDC
20709         stat $submount > /dev/null
20710         stat $submount/$tfile > /dev/null
20711         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
20712         stat $submount/$tfile > /dev/null
20713         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
20714                 awk '/getattr/ {sum += $2} END {print sum}')
20715
20716         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
20717 }
20718 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
20719
20720 test_248a() {
20721         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
20722         [ -z "$fast_read_sav" ] && skip "no fast read support"
20723
20724         # create a large file for fast read verification
20725         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
20726
20727         # make sure the file is created correctly
20728         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
20729                 { rm -f $DIR/$tfile; skip "file creation error"; }
20730
20731         echo "Test 1: verify that fast read is 4 times faster on cache read"
20732
20733         # small read with fast read enabled
20734         $LCTL set_param -n llite.*.fast_read=1
20735         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
20736                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20737                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20738         # small read with fast read disabled
20739         $LCTL set_param -n llite.*.fast_read=0
20740         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
20741                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20742                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20743
20744         # verify that fast read is 4 times faster for cache read
20745         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
20746                 error_not_in_vm "fast read was not 4 times faster: " \
20747                            "$t_fast vs $t_slow"
20748
20749         echo "Test 2: verify the performance between big and small read"
20750         $LCTL set_param -n llite.*.fast_read=1
20751
20752         # 1k non-cache read
20753         cancel_lru_locks osc
20754         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
20755                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20756                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20757
20758         # 1M non-cache read
20759         cancel_lru_locks osc
20760         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
20761                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20762                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20763
20764         # verify that big IO is not 4 times faster than small IO
20765         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
20766                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
20767
20768         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
20769         rm -f $DIR/$tfile
20770 }
20771 run_test 248a "fast read verification"
20772
20773 test_248b() {
20774         # Default short_io_bytes=16384, try both smaller and larger sizes.
20775         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
20776         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
20777         echo "bs=53248 count=113 normal buffered write"
20778         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
20779                 error "dd of initial data file failed"
20780         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
20781
20782         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
20783         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
20784                 error "dd with sync normal writes failed"
20785         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
20786
20787         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
20788         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
20789                 error "dd with sync small writes failed"
20790         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
20791
20792         cancel_lru_locks osc
20793
20794         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
20795         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
20796         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
20797         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
20798                 iflag=direct || error "dd with O_DIRECT small read failed"
20799         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
20800         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
20801                 error "compare $TMP/$tfile.1 failed"
20802
20803         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
20804         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
20805
20806         # just to see what the maximum tunable value is, and test parsing
20807         echo "test invalid parameter 2MB"
20808         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
20809                 error "too-large short_io_bytes allowed"
20810         echo "test maximum parameter 512KB"
20811         # if we can set a larger short_io_bytes, run test regardless of version
20812         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
20813                 # older clients may not allow setting it this large, that's OK
20814                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
20815                         skip "Need at least client version 2.13.50"
20816                 error "medium short_io_bytes failed"
20817         fi
20818         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
20819         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
20820
20821         echo "test large parameter 64KB"
20822         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
20823         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
20824
20825         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
20826         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
20827                 error "dd with sync large writes failed"
20828         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
20829
20830         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
20831         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
20832         num=$((113 * 4096 / PAGE_SIZE))
20833         echo "bs=$size count=$num oflag=direct large write $tfile.3"
20834         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
20835                 error "dd with O_DIRECT large writes failed"
20836         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
20837                 error "compare $DIR/$tfile.3 failed"
20838
20839         cancel_lru_locks osc
20840
20841         echo "bs=$size count=$num iflag=direct large read $tfile.2"
20842         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
20843                 error "dd with O_DIRECT large read failed"
20844         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
20845                 error "compare $TMP/$tfile.2 failed"
20846
20847         echo "bs=$size count=$num iflag=direct large read $tfile.3"
20848         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
20849                 error "dd with O_DIRECT large read failed"
20850         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
20851                 error "compare $TMP/$tfile.3 failed"
20852 }
20853 run_test 248b "test short_io read and write for both small and large sizes"
20854
20855 test_249() { # LU-7890
20856         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
20857                 skip "Need at least version 2.8.54"
20858
20859         rm -f $DIR/$tfile
20860         $LFS setstripe -c 1 $DIR/$tfile
20861         # Offset 2T == 4k * 512M
20862         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
20863                 error "dd to 2T offset failed"
20864 }
20865 run_test 249 "Write above 2T file size"
20866
20867 test_250() {
20868         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
20869          && skip "no 16TB file size limit on ZFS"
20870
20871         $LFS setstripe -c 1 $DIR/$tfile
20872         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
20873         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
20874         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
20875         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
20876                 conv=notrunc,fsync && error "append succeeded"
20877         return 0
20878 }
20879 run_test 250 "Write above 16T limit"
20880
20881 test_251() {
20882         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
20883
20884         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
20885         #Skip once - writing the first stripe will succeed
20886         $LCTL set_param fail_loc=0xa0001407 fail_val=1
20887         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
20888                 error "short write happened"
20889
20890         $LCTL set_param fail_loc=0xa0001407 fail_val=1
20891         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
20892                 error "short read happened"
20893
20894         rm -f $DIR/$tfile
20895 }
20896 run_test 251 "Handling short read and write correctly"
20897
20898 test_252() {
20899         remote_mds_nodsh && skip "remote MDS with nodsh"
20900         remote_ost_nodsh && skip "remote OST with nodsh"
20901         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
20902                 skip_env "ldiskfs only test"
20903         fi
20904
20905         local tgt
20906         local dev
20907         local out
20908         local uuid
20909         local num
20910         local gen
20911
20912         # check lr_reader on OST0000
20913         tgt=ost1
20914         dev=$(facet_device $tgt)
20915         out=$(do_facet $tgt $LR_READER $dev)
20916         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
20917         echo "$out"
20918         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
20919         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
20920                 error "Invalid uuid returned by $LR_READER on target $tgt"
20921         echo -e "uuid returned by $LR_READER is '$uuid'\n"
20922
20923         # check lr_reader -c on MDT0000
20924         tgt=mds1
20925         dev=$(facet_device $tgt)
20926         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
20927                 skip "$LR_READER does not support additional options"
20928         fi
20929         out=$(do_facet $tgt $LR_READER -c $dev)
20930         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
20931         echo "$out"
20932         num=$(echo "$out" | grep -c "mdtlov")
20933         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
20934                 error "Invalid number of mdtlov clients returned by $LR_READER"
20935         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
20936
20937         # check lr_reader -cr on MDT0000
20938         out=$(do_facet $tgt $LR_READER -cr $dev)
20939         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
20940         echo "$out"
20941         echo "$out" | grep -q "^reply_data:$" ||
20942                 error "$LR_READER should have returned 'reply_data' section"
20943         num=$(echo "$out" | grep -c "client_generation")
20944         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
20945 }
20946 run_test 252 "check lr_reader tool"
20947
20948 test_253() {
20949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20950         remote_mds_nodsh && skip "remote MDS with nodsh"
20951         remote_mgs_nodsh && skip "remote MGS with nodsh"
20952
20953         local ostidx=0
20954         local rc=0
20955         local ost_name=$(ostname_from_index $ostidx)
20956
20957         # on the mdt's osc
20958         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
20959         do_facet $SINGLEMDS $LCTL get_param -n \
20960                 osp.$mdtosc_proc1.reserved_mb_high ||
20961                 skip  "remote MDS does not support reserved_mb_high"
20962
20963         rm -rf $DIR/$tdir
20964         wait_mds_ost_sync
20965         wait_delete_completed
20966         mkdir $DIR/$tdir
20967
20968         pool_add $TESTNAME || error "Pool creation failed"
20969         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
20970
20971         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
20972                 error "Setstripe failed"
20973
20974         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
20975
20976         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
20977                     grep "watermarks")
20978         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
20979
20980         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
20981                         osp.$mdtosc_proc1.prealloc_status)
20982         echo "prealloc_status $oa_status"
20983
20984         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
20985                 error "File creation should fail"
20986
20987         #object allocation was stopped, but we still able to append files
20988         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
20989                 oflag=append || error "Append failed"
20990
20991         rm -f $DIR/$tdir/$tfile.0
20992
20993         # For this test, we want to delete the files we created to go out of
20994         # space but leave the watermark, so we remain nearly out of space
20995         ost_watermarks_enospc_delete_files $tfile $ostidx
20996
20997         wait_delete_completed
20998
20999         sleep_maxage
21000
21001         for i in $(seq 10 12); do
21002                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
21003                         2>/dev/null || error "File creation failed after rm"
21004         done
21005
21006         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
21007                         osp.$mdtosc_proc1.prealloc_status)
21008         echo "prealloc_status $oa_status"
21009
21010         if (( oa_status != 0 )); then
21011                 error "Object allocation still disable after rm"
21012         fi
21013 }
21014 run_test 253 "Check object allocation limit"
21015
21016 test_254() {
21017         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21018         remote_mds_nodsh && skip "remote MDS with nodsh"
21019         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
21020                 skip "MDS does not support changelog_size"
21021
21022         local cl_user
21023         local MDT0=$(facet_svc $SINGLEMDS)
21024
21025         changelog_register || error "changelog_register failed"
21026
21027         changelog_clear 0 || error "changelog_clear failed"
21028
21029         local size1=$(do_facet $SINGLEMDS \
21030                       $LCTL get_param -n mdd.$MDT0.changelog_size)
21031         echo "Changelog size $size1"
21032
21033         rm -rf $DIR/$tdir
21034         $LFS mkdir -i 0 $DIR/$tdir
21035         # change something
21036         mkdir -p $DIR/$tdir/pics/2008/zachy
21037         touch $DIR/$tdir/pics/2008/zachy/timestamp
21038         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
21039         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
21040         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
21041         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
21042         rm $DIR/$tdir/pics/desktop.jpg
21043
21044         local size2=$(do_facet $SINGLEMDS \
21045                       $LCTL get_param -n mdd.$MDT0.changelog_size)
21046         echo "Changelog size after work $size2"
21047
21048         (( $size2 > $size1 )) ||
21049                 error "new Changelog size=$size2 less than old size=$size1"
21050 }
21051 run_test 254 "Check changelog size"
21052
21053 ladvise_no_type()
21054 {
21055         local type=$1
21056         local file=$2
21057
21058         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
21059                 awk -F: '{print $2}' | grep $type > /dev/null
21060         if [ $? -ne 0 ]; then
21061                 return 0
21062         fi
21063         return 1
21064 }
21065
21066 ladvise_no_ioctl()
21067 {
21068         local file=$1
21069
21070         lfs ladvise -a willread $file > /dev/null 2>&1
21071         if [ $? -eq 0 ]; then
21072                 return 1
21073         fi
21074
21075         lfs ladvise -a willread $file 2>&1 |
21076                 grep "Inappropriate ioctl for device" > /dev/null
21077         if [ $? -eq 0 ]; then
21078                 return 0
21079         fi
21080         return 1
21081 }
21082
21083 percent() {
21084         bc <<<"scale=2; ($1 - $2) * 100 / $2"
21085 }
21086
21087 # run a random read IO workload
21088 # usage: random_read_iops <filename> <filesize> <iosize>
21089 random_read_iops() {
21090         local file=$1
21091         local fsize=$2
21092         local iosize=${3:-4096}
21093
21094         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
21095                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
21096 }
21097
21098 drop_file_oss_cache() {
21099         local file="$1"
21100         local nodes="$2"
21101
21102         $LFS ladvise -a dontneed $file 2>/dev/null ||
21103                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
21104 }
21105
21106 ladvise_willread_performance()
21107 {
21108         local repeat=10
21109         local average_origin=0
21110         local average_cache=0
21111         local average_ladvise=0
21112
21113         for ((i = 1; i <= $repeat; i++)); do
21114                 echo "Iter $i/$repeat: reading without willread hint"
21115                 cancel_lru_locks osc
21116                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
21117                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
21118                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
21119                 average_origin=$(bc <<<"$average_origin + $speed_origin")
21120
21121                 cancel_lru_locks osc
21122                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
21123                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
21124                 average_cache=$(bc <<<"$average_cache + $speed_cache")
21125
21126                 cancel_lru_locks osc
21127                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
21128                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
21129                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
21130                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
21131                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
21132         done
21133         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
21134         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
21135         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
21136
21137         speedup_cache=$(percent $average_cache $average_origin)
21138         speedup_ladvise=$(percent $average_ladvise $average_origin)
21139
21140         echo "Average uncached read: $average_origin"
21141         echo "Average speedup with OSS cached read: " \
21142                 "$average_cache = +$speedup_cache%"
21143         echo "Average speedup with ladvise willread: " \
21144                 "$average_ladvise = +$speedup_ladvise%"
21145
21146         local lowest_speedup=20
21147         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
21148                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
21149                         "got $average_cache%. Skipping ladvise willread check."
21150                 return 0
21151         fi
21152
21153         # the test won't work on ZFS until it supports 'ladvise dontneed', but
21154         # it is still good to run until then to exercise 'ladvise willread'
21155         ! $LFS ladvise -a dontneed $DIR/$tfile &&
21156                 [ "$ost1_FSTYPE" = "zfs" ] &&
21157                 echo "osd-zfs does not support dontneed or drop_caches" &&
21158                 return 0
21159
21160         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
21161         [[ ${average_ladvise%.*} > $lowest_speedup ]] ||
21162                 error_not_in_vm "Speedup with willread is less than " \
21163                         "$lowest_speedup%, got $average_ladvise%"
21164 }
21165
21166 test_255a() {
21167         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
21168                 skip "lustre < 2.8.54 does not support ladvise "
21169         remote_ost_nodsh && skip "remote OST with nodsh"
21170
21171         stack_trap "rm -f $DIR/$tfile"
21172         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
21173
21174         ladvise_no_type willread $DIR/$tfile &&
21175                 skip "willread ladvise is not supported"
21176
21177         ladvise_no_ioctl $DIR/$tfile &&
21178                 skip "ladvise ioctl is not supported"
21179
21180         local size_mb=100
21181         local size=$((size_mb * 1048576))
21182         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
21183                 error "dd to $DIR/$tfile failed"
21184
21185         lfs ladvise -a willread $DIR/$tfile ||
21186                 error "Ladvise failed with no range argument"
21187
21188         lfs ladvise -a willread -s 0 $DIR/$tfile ||
21189                 error "Ladvise failed with no -l or -e argument"
21190
21191         lfs ladvise -a willread -e 1 $DIR/$tfile ||
21192                 error "Ladvise failed with only -e argument"
21193
21194         lfs ladvise -a willread -l 1 $DIR/$tfile ||
21195                 error "Ladvise failed with only -l argument"
21196
21197         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
21198                 error "End offset should not be smaller than start offset"
21199
21200         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
21201                 error "End offset should not be equal to start offset"
21202
21203         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
21204                 error "Ladvise failed with overflowing -s argument"
21205
21206         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
21207                 error "Ladvise failed with overflowing -e argument"
21208
21209         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
21210                 error "Ladvise failed with overflowing -l argument"
21211
21212         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
21213                 error "Ladvise succeeded with conflicting -l and -e arguments"
21214
21215         echo "Synchronous ladvise should wait"
21216         local delay=4
21217 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
21218         do_nodes $(comma_list $(osts_nodes)) \
21219                 $LCTL set_param fail_val=$delay fail_loc=0x237
21220
21221         local start_ts=$SECONDS
21222         lfs ladvise -a willread $DIR/$tfile ||
21223                 error "Ladvise failed with no range argument"
21224         local end_ts=$SECONDS
21225         local inteval_ts=$((end_ts - start_ts))
21226
21227         if [ $inteval_ts -lt $(($delay - 1)) ]; then
21228                 error "Synchronous advice didn't wait reply"
21229         fi
21230
21231         echo "Asynchronous ladvise shouldn't wait"
21232         local start_ts=$SECONDS
21233         lfs ladvise -a willread -b $DIR/$tfile ||
21234                 error "Ladvise failed with no range argument"
21235         local end_ts=$SECONDS
21236         local inteval_ts=$((end_ts - start_ts))
21237
21238         if [ $inteval_ts -gt $(($delay / 2)) ]; then
21239                 error "Asynchronous advice blocked"
21240         fi
21241
21242         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
21243         ladvise_willread_performance
21244 }
21245 run_test 255a "check 'lfs ladvise -a willread'"
21246
21247 facet_meminfo() {
21248         local facet=$1
21249         local info=$2
21250
21251         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
21252 }
21253
21254 test_255b() {
21255         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
21256                 skip "lustre < 2.8.54 does not support ladvise "
21257         remote_ost_nodsh && skip "remote OST with nodsh"
21258
21259         stack_trap "rm -f $DIR/$tfile"
21260         lfs setstripe -c 1 -i 0 $DIR/$tfile
21261
21262         ladvise_no_type dontneed $DIR/$tfile &&
21263                 skip "dontneed ladvise is not supported"
21264
21265         ladvise_no_ioctl $DIR/$tfile &&
21266                 skip "ladvise ioctl is not supported"
21267
21268         ! $LFS ladvise -a dontneed $DIR/$tfile &&
21269                 [ "$ost1_FSTYPE" = "zfs" ] &&
21270                 skip "zfs-osd does not support 'ladvise dontneed'"
21271
21272         local size_mb=100
21273         local size=$((size_mb * 1048576))
21274         # In order to prevent disturbance of other processes, only check 3/4
21275         # of the memory usage
21276         local kibibytes=$((size_mb * 1024 * 3 / 4))
21277
21278         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
21279                 error "dd to $DIR/$tfile failed"
21280
21281         #force write to complete before dropping OST cache & checking memory
21282         sync
21283
21284         local total=$(facet_meminfo ost1 MemTotal)
21285         echo "Total memory: $total KiB"
21286
21287         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
21288         local before_read=$(facet_meminfo ost1 Cached)
21289         echo "Cache used before read: $before_read KiB"
21290
21291         lfs ladvise -a willread $DIR/$tfile ||
21292                 error "Ladvise willread failed"
21293         local after_read=$(facet_meminfo ost1 Cached)
21294         echo "Cache used after read: $after_read KiB"
21295
21296         lfs ladvise -a dontneed $DIR/$tfile ||
21297                 error "Ladvise dontneed again failed"
21298         local no_read=$(facet_meminfo ost1 Cached)
21299         echo "Cache used after dontneed ladvise: $no_read KiB"
21300
21301         if [ $total -lt $((before_read + kibibytes)) ]; then
21302                 echo "Memory is too small, abort checking"
21303                 return 0
21304         fi
21305
21306         if [ $((before_read + kibibytes)) -gt $after_read ]; then
21307                 error "Ladvise willread should use more memory" \
21308                         "than $kibibytes KiB"
21309         fi
21310
21311         if [ $((no_read + kibibytes)) -gt $after_read ]; then
21312                 error "Ladvise dontneed should release more memory" \
21313                         "than $kibibytes KiB"
21314         fi
21315 }
21316 run_test 255b "check 'lfs ladvise -a dontneed'"
21317
21318 test_255c() {
21319         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
21320                 skip "lustre < 2.10.50 does not support lockahead"
21321
21322         local ost1_imp=$(get_osc_import_name client ost1)
21323         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
21324                          cut -d'.' -f2)
21325         local count
21326         local new_count
21327         local difference
21328         local i
21329         local rc
21330
21331         test_mkdir -p $DIR/$tdir
21332         $LFS setstripe -i 0 -c 1 $DIR/$tdir
21333
21334         #test 10 returns only success/failure
21335         i=10
21336         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21337         rc=$?
21338         if [ $rc -eq 255 ]; then
21339                 error "Ladvise test${i} failed, ${rc}"
21340         fi
21341
21342         #test 11 counts lock enqueue requests, all others count new locks
21343         i=11
21344         count=$(do_facet ost1 \
21345                 $LCTL get_param -n ost.OSS.ost.stats)
21346         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
21347
21348         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21349         rc=$?
21350         if [ $rc -eq 255 ]; then
21351                 error "Ladvise test${i} failed, ${rc}"
21352         fi
21353
21354         new_count=$(do_facet ost1 \
21355                 $LCTL get_param -n ost.OSS.ost.stats)
21356         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
21357                    awk '{ print $2 }')
21358
21359         difference="$((new_count - count))"
21360         if [ $difference -ne $rc ]; then
21361                 error "Ladvise test${i}, bad enqueue count, returned " \
21362                       "${rc}, actual ${difference}"
21363         fi
21364
21365         for i in $(seq 12 21); do
21366                 # If we do not do this, we run the risk of having too many
21367                 # locks and starting lock cancellation while we are checking
21368                 # lock counts.
21369                 cancel_lru_locks osc
21370
21371                 count=$($LCTL get_param -n \
21372                        ldlm.namespaces.$imp_name.lock_unused_count)
21373
21374                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
21375                 rc=$?
21376                 if [ $rc -eq 255 ]; then
21377                         error "Ladvise test ${i} failed, ${rc}"
21378                 fi
21379
21380                 new_count=$($LCTL get_param -n \
21381                        ldlm.namespaces.$imp_name.lock_unused_count)
21382                 difference="$((new_count - count))"
21383
21384                 # Test 15 output is divided by 100 to map down to valid return
21385                 if [ $i -eq 15 ]; then
21386                         rc="$((rc * 100))"
21387                 fi
21388
21389                 if [ $difference -ne $rc ]; then
21390                         error "Ladvise test ${i}, bad lock count, returned " \
21391                               "${rc}, actual ${difference}"
21392                 fi
21393         done
21394
21395         #test 22 returns only success/failure
21396         i=22
21397         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21398         rc=$?
21399         if [ $rc -eq 255 ]; then
21400                 error "Ladvise test${i} failed, ${rc}"
21401         fi
21402 }
21403 run_test 255c "suite of ladvise lockahead tests"
21404
21405 test_256() {
21406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21407         remote_mds_nodsh && skip "remote MDS with nodsh"
21408         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
21409         changelog_users $SINGLEMDS | grep "^cl" &&
21410                 skip "active changelog user"
21411
21412         local cl_user
21413         local cat_sl
21414         local mdt_dev
21415
21416         mdt_dev=$(mdsdevname 1)
21417         echo $mdt_dev
21418
21419         changelog_register || error "changelog_register failed"
21420
21421         rm -rf $DIR/$tdir
21422         mkdir_on_mdt0 $DIR/$tdir
21423
21424         changelog_clear 0 || error "changelog_clear failed"
21425
21426         # change something
21427         touch $DIR/$tdir/{1..10}
21428
21429         # stop the MDT
21430         stop $SINGLEMDS || error "Fail to stop MDT"
21431
21432         # remount the MDT
21433
21434         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
21435
21436         #after mount new plainllog is used
21437         touch $DIR/$tdir/{11..19}
21438         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
21439         stack_trap "rm -f $tmpfile"
21440         cat_sl=$(do_facet $SINGLEMDS "sync; \
21441                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
21442                  llog_reader $tmpfile | grep -c type=1064553b")
21443         do_facet $SINGLEMDS llog_reader $tmpfile
21444
21445         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
21446
21447         changelog_clear 0 || error "changelog_clear failed"
21448
21449         cat_sl=$(do_facet $SINGLEMDS "sync; \
21450                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
21451                  llog_reader $tmpfile | grep -c type=1064553b")
21452
21453         if (( cat_sl == 2 )); then
21454                 error "Empty plain llog was not deleted from changelog catalog"
21455         elif (( cat_sl != 1 )); then
21456                 error "Active plain llog shouldn't be deleted from catalog"
21457         fi
21458 }
21459 run_test 256 "Check llog delete for empty and not full state"
21460
21461 test_257() {
21462         remote_mds_nodsh && skip "remote MDS with nodsh"
21463         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21464                 skip "Need MDS version at least 2.8.55"
21465
21466         test_mkdir $DIR/$tdir
21467
21468         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
21469                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
21470         stat $DIR/$tdir
21471
21472 #define OBD_FAIL_MDS_XATTR_REP                  0x161
21473         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
21474         local facet=mds$((mdtidx + 1))
21475         set_nodes_failloc $(facet_active_host $facet) 0x80000161
21476         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
21477
21478         stop $facet || error "stop MDS failed"
21479         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
21480                 error "start MDS fail"
21481         wait_recovery_complete $facet
21482 }
21483 run_test 257 "xattr locks are not lost"
21484
21485 # Verify we take the i_mutex when security requires it
21486 test_258a() {
21487 #define OBD_FAIL_IMUTEX_SEC 0x141c
21488         $LCTL set_param fail_loc=0x141c
21489         touch $DIR/$tfile
21490         chmod u+s $DIR/$tfile
21491         chmod a+rwx $DIR/$tfile
21492         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
21493         RC=$?
21494         if [ $RC -ne 0 ]; then
21495                 error "error, failed to take i_mutex, rc=$?"
21496         fi
21497         rm -f $DIR/$tfile
21498 }
21499 run_test 258a "verify i_mutex security behavior when suid attributes is set"
21500
21501 # Verify we do NOT take the i_mutex in the normal case
21502 test_258b() {
21503 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
21504         $LCTL set_param fail_loc=0x141d
21505         touch $DIR/$tfile
21506         chmod a+rwx $DIR
21507         chmod a+rw $DIR/$tfile
21508         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
21509         RC=$?
21510         if [ $RC -ne 0 ]; then
21511                 error "error, took i_mutex unnecessarily, rc=$?"
21512         fi
21513         rm -f $DIR/$tfile
21514
21515 }
21516 run_test 258b "verify i_mutex security behavior"
21517
21518 test_259() {
21519         local file=$DIR/$tfile
21520         local before
21521         local after
21522
21523         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
21524
21525         stack_trap "rm -f $file" EXIT
21526
21527         wait_delete_completed
21528         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21529         echo "before: $before"
21530
21531         $LFS setstripe -i 0 -c 1 $file
21532         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
21533         sync_all_data
21534         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21535         echo "after write: $after"
21536
21537 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
21538         do_facet ost1 $LCTL set_param fail_loc=0x2301
21539         $TRUNCATE $file 0
21540         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21541         echo "after truncate: $after"
21542
21543         stop ost1
21544         do_facet ost1 $LCTL set_param fail_loc=0
21545         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21546         sleep 2
21547         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21548         echo "after restart: $after"
21549         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
21550                 error "missing truncate?"
21551
21552         return 0
21553 }
21554 run_test 259 "crash at delayed truncate"
21555
21556 test_260() {
21557 #define OBD_FAIL_MDC_CLOSE               0x806
21558         $LCTL set_param fail_loc=0x80000806
21559         touch $DIR/$tfile
21560
21561 }
21562 run_test 260 "Check mdc_close fail"
21563
21564 ### Data-on-MDT sanity tests ###
21565 test_270a() {
21566         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21567                 skip "Need MDS version at least 2.10.55 for DoM"
21568
21569         # create DoM file
21570         local dom=$DIR/$tdir/dom_file
21571         local tmp=$DIR/$tdir/tmp_file
21572
21573         mkdir_on_mdt0 $DIR/$tdir
21574
21575         # basic checks for DoM component creation
21576         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
21577                 error "Can set MDT layout to non-first entry"
21578
21579         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
21580                 error "Can define multiple entries as MDT layout"
21581
21582         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
21583
21584         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
21585         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
21586         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
21587
21588         local mdtidx=$($LFS getstripe -m $dom)
21589         local mdtname=MDT$(printf %04x $mdtidx)
21590         local facet=mds$((mdtidx + 1))
21591         local space_check=1
21592
21593         # Skip free space checks with ZFS
21594         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
21595
21596         # write
21597         sync
21598         local size_tmp=$((65536 * 3))
21599         local mdtfree1=$(do_facet $facet \
21600                          lctl get_param -n osd*.*$mdtname.kbytesfree)
21601
21602         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
21603         # check also direct IO along write
21604         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
21605         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
21606         sync
21607         cmp $tmp $dom || error "file data is different"
21608         [ $(stat -c%s $dom) == $size_tmp ] ||
21609                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
21610         if [ $space_check == 1 ]; then
21611                 local mdtfree2=$(do_facet $facet \
21612                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
21613
21614                 # increase in usage from by $size_tmp
21615                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
21616                         error "MDT free space wrong after write: " \
21617                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
21618         fi
21619
21620         # truncate
21621         local size_dom=10000
21622
21623         $TRUNCATE $dom $size_dom
21624         [ $(stat -c%s $dom) == $size_dom ] ||
21625                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
21626         if [ $space_check == 1 ]; then
21627                 mdtfree1=$(do_facet $facet \
21628                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21629                 # decrease in usage from $size_tmp to new $size_dom
21630                 [ $(($mdtfree1 - $mdtfree2)) -ge \
21631                   $(((size_tmp - size_dom) / 1024)) ] ||
21632                         error "MDT free space is wrong after truncate: " \
21633                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
21634         fi
21635
21636         # append
21637         cat $tmp >> $dom
21638         sync
21639         size_dom=$((size_dom + size_tmp))
21640         [ $(stat -c%s $dom) == $size_dom ] ||
21641                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
21642         if [ $space_check == 1 ]; then
21643                 mdtfree2=$(do_facet $facet \
21644                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21645                 # increase in usage by $size_tmp from previous
21646                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
21647                         error "MDT free space is wrong after append: " \
21648                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
21649         fi
21650
21651         # delete
21652         rm $dom
21653         if [ $space_check == 1 ]; then
21654                 mdtfree1=$(do_facet $facet \
21655                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21656                 # decrease in usage by $size_dom from previous
21657                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
21658                         error "MDT free space is wrong after removal: " \
21659                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
21660         fi
21661
21662         # combined striping
21663         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
21664                 error "Can't create DoM + OST striping"
21665
21666         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
21667         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
21668         # check also direct IO along write
21669         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
21670         sync
21671         cmp $tmp $dom || error "file data is different"
21672         [ $(stat -c%s $dom) == $size_tmp ] ||
21673                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
21674         rm $dom $tmp
21675
21676         return 0
21677 }
21678 run_test 270a "DoM: basic functionality tests"
21679
21680 test_270b() {
21681         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21682                 skip "Need MDS version at least 2.10.55"
21683
21684         local dom=$DIR/$tdir/dom_file
21685         local max_size=1048576
21686
21687         mkdir -p $DIR/$tdir
21688         $LFS setstripe -E $max_size -L mdt $dom
21689
21690         # truncate over the limit
21691         $TRUNCATE $dom $(($max_size + 1)) &&
21692                 error "successful truncate over the maximum size"
21693         # write over the limit
21694         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
21695                 error "successful write over the maximum size"
21696         # append over the limit
21697         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
21698         echo "12345" >> $dom && error "successful append over the maximum size"
21699         rm $dom
21700
21701         return 0
21702 }
21703 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
21704
21705 test_270c() {
21706         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21707                 skip "Need MDS version at least 2.10.55"
21708
21709         mkdir -p $DIR/$tdir
21710         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21711
21712         # check files inherit DoM EA
21713         touch $DIR/$tdir/first
21714         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
21715                 error "bad pattern"
21716         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
21717                 error "bad stripe count"
21718         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
21719                 error "bad stripe size"
21720
21721         # check directory inherits DoM EA and uses it as default
21722         mkdir $DIR/$tdir/subdir
21723         touch $DIR/$tdir/subdir/second
21724         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
21725                 error "bad pattern in sub-directory"
21726         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
21727                 error "bad stripe count in sub-directory"
21728         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
21729                 error "bad stripe size in sub-directory"
21730         return 0
21731 }
21732 run_test 270c "DoM: DoM EA inheritance tests"
21733
21734 test_270d() {
21735         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21736                 skip "Need MDS version at least 2.10.55"
21737
21738         mkdir -p $DIR/$tdir
21739         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21740
21741         # inherit default DoM striping
21742         mkdir $DIR/$tdir/subdir
21743         touch $DIR/$tdir/subdir/f1
21744
21745         # change default directory striping
21746         $LFS setstripe -c 1 $DIR/$tdir/subdir
21747         touch $DIR/$tdir/subdir/f2
21748         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
21749                 error "wrong default striping in file 2"
21750         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
21751                 error "bad pattern in file 2"
21752         return 0
21753 }
21754 run_test 270d "DoM: change striping from DoM to RAID0"
21755
21756 test_270e() {
21757         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21758                 skip "Need MDS version at least 2.10.55"
21759
21760         mkdir -p $DIR/$tdir/dom
21761         mkdir -p $DIR/$tdir/norm
21762         DOMFILES=20
21763         NORMFILES=10
21764         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
21765         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
21766
21767         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
21768         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
21769
21770         # find DoM files by layout
21771         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
21772         [ $NUM -eq  $DOMFILES ] ||
21773                 error "lfs find -L: found $NUM, expected $DOMFILES"
21774         echo "Test 1: lfs find 20 DOM files by layout: OK"
21775
21776         # there should be 1 dir with default DOM striping
21777         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
21778         [ $NUM -eq  1 ] ||
21779                 error "lfs find -L: found $NUM, expected 1 dir"
21780         echo "Test 2: lfs find 1 DOM dir by layout: OK"
21781
21782         # find DoM files by stripe size
21783         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
21784         [ $NUM -eq  $DOMFILES ] ||
21785                 error "lfs find -S: found $NUM, expected $DOMFILES"
21786         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
21787
21788         # find files by stripe offset except DoM files
21789         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
21790         [ $NUM -eq  $NORMFILES ] ||
21791                 error "lfs find -i: found $NUM, expected $NORMFILES"
21792         echo "Test 5: lfs find no DOM files by stripe index: OK"
21793         return 0
21794 }
21795 run_test 270e "DoM: lfs find with DoM files test"
21796
21797 test_270f() {
21798         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21799                 skip "Need MDS version at least 2.10.55"
21800
21801         local mdtname=${FSNAME}-MDT0000-mdtlov
21802         local dom=$DIR/$tdir/dom_file
21803         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
21804                                                 lod.$mdtname.dom_stripesize)
21805         local dom_limit=131072
21806
21807         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
21808         local dom_current=$(do_facet mds1 $LCTL get_param -n \
21809                                                 lod.$mdtname.dom_stripesize)
21810         [ ${dom_limit} -eq ${dom_current} ] ||
21811                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
21812
21813         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21814         $LFS setstripe -d $DIR/$tdir
21815         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
21816                 error "Can't set directory default striping"
21817
21818         # exceed maximum stripe size
21819         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
21820                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
21821         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
21822                 error "Able to create DoM component size more than LOD limit"
21823
21824         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
21825         dom_current=$(do_facet mds1 $LCTL get_param -n \
21826                                                 lod.$mdtname.dom_stripesize)
21827         [ 0 -eq ${dom_current} ] ||
21828                 error "Can't set zero DoM stripe limit"
21829         rm $dom
21830
21831         # attempt to create DoM file on server with disabled DoM should
21832         # remove DoM entry from layout and be succeed
21833         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
21834                 error "Can't create DoM file (DoM is disabled)"
21835         [ $($LFS getstripe -L $dom) == "mdt" ] &&
21836                 error "File has DoM component while DoM is disabled"
21837         rm $dom
21838
21839         # attempt to create DoM file with only DoM stripe should return error
21840         $LFS setstripe -E $dom_limit -L mdt $dom &&
21841                 error "Able to create DoM-only file while DoM is disabled"
21842
21843         # too low values to be aligned with smallest stripe size 64K
21844         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
21845         dom_current=$(do_facet mds1 $LCTL get_param -n \
21846                                                 lod.$mdtname.dom_stripesize)
21847         [ 30000 -eq ${dom_current} ] &&
21848                 error "Can set too small DoM stripe limit"
21849
21850         # 64K is a minimal stripe size in Lustre, expect limit of that size
21851         [ 65536 -eq ${dom_current} ] ||
21852                 error "Limit is not set to 64K but ${dom_current}"
21853
21854         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
21855         dom_current=$(do_facet mds1 $LCTL get_param -n \
21856                                                 lod.$mdtname.dom_stripesize)
21857         echo $dom_current
21858         [ 2147483648 -eq ${dom_current} ] &&
21859                 error "Can set too large DoM stripe limit"
21860
21861         do_facet mds1 $LCTL set_param -n \
21862                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
21863         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
21864                 error "Can't create DoM component size after limit change"
21865         do_facet mds1 $LCTL set_param -n \
21866                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
21867         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
21868                 error "Can't create DoM file after limit decrease"
21869         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
21870                 error "Can create big DoM component after limit decrease"
21871         touch ${dom}_def ||
21872                 error "Can't create file with old default layout"
21873
21874         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
21875         return 0
21876 }
21877 run_test 270f "DoM: maximum DoM stripe size checks"
21878
21879 test_270g() {
21880         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
21881                 skip "Need MDS version at least 2.13.52"
21882         local dom=$DIR/$tdir/$tfile
21883
21884         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21885         local lodname=${FSNAME}-MDT0000-mdtlov
21886
21887         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21888         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
21889         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
21890         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21891
21892         local dom_limit=1024
21893         local dom_threshold="50%"
21894
21895         $LFS setstripe -d $DIR/$tdir
21896         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
21897                 error "Can't set directory default striping"
21898
21899         do_facet mds1 $LCTL set_param -n \
21900                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
21901         # set 0 threshold and create DOM file to change tunable stripesize
21902         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
21903         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
21904                 error "Failed to create $dom file"
21905         # now tunable dom_cur_stripesize should reach maximum
21906         local dom_current=$(do_facet mds1 $LCTL get_param -n \
21907                                         lod.${lodname}.dom_stripesize_cur_kb)
21908         [[ $dom_current == $dom_limit ]] ||
21909                 error "Current DOM stripesize is not maximum"
21910         rm $dom
21911
21912         # set threshold for further tests
21913         do_facet mds1 $LCTL set_param -n \
21914                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
21915         echo "DOM threshold is $dom_threshold free space"
21916         local dom_def
21917         local dom_set
21918         # Spoof bfree to exceed threshold
21919         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
21920         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
21921         for spfree in 40 20 0 15 30 55; do
21922                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
21923                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
21924                         error "Failed to create $dom file"
21925                 dom_def=$(do_facet mds1 $LCTL get_param -n \
21926                                         lod.${lodname}.dom_stripesize_cur_kb)
21927                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
21928                 [[ $dom_def != $dom_current ]] ||
21929                         error "Default stripe size was not changed"
21930                 if [[ $spfree > 0 ]] ; then
21931                         dom_set=$($LFS getstripe -S $dom)
21932                         [[ $dom_set == $((dom_def * 1024)) ]] ||
21933                                 error "DOM component size is still old"
21934                 else
21935                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
21936                                 error "DoM component is set with no free space"
21937                 fi
21938                 rm $dom
21939                 dom_current=$dom_def
21940         done
21941 }
21942 run_test 270g "DoM: default DoM stripe size depends on free space"
21943
21944 test_270h() {
21945         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
21946                 skip "Need MDS version at least 2.13.53"
21947
21948         local mdtname=${FSNAME}-MDT0000-mdtlov
21949         local dom=$DIR/$tdir/$tfile
21950         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21951
21952         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
21953         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21954
21955         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21956         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
21957                 error "can't create OST file"
21958         # mirrored file with DOM entry in the second mirror
21959         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
21960                 error "can't create mirror with DoM component"
21961
21962         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
21963
21964         # DOM component in the middle and has other enries in the same mirror,
21965         # should succeed but lost DoM component
21966         $LFS setstripe --copy=${dom}_1 $dom ||
21967                 error "Can't create file from OST|DOM mirror layout"
21968         # check new file has no DoM layout after all
21969         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
21970                 error "File has DoM component while DoM is disabled"
21971 }
21972 run_test 270h "DoM: DoM stripe removal when disabled on server"
21973
21974 test_271a() {
21975         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21976                 skip "Need MDS version at least 2.10.55"
21977
21978         local dom=$DIR/$tdir/dom
21979
21980         mkdir -p $DIR/$tdir
21981
21982         $LFS setstripe -E 1024K -L mdt $dom
21983
21984         lctl set_param -n mdc.*.stats=clear
21985         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
21986         cat $dom > /dev/null
21987         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
21988         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
21989         ls $dom
21990         rm -f $dom
21991 }
21992 run_test 271a "DoM: data is cached for read after write"
21993
21994 test_271b() {
21995         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21996                 skip "Need MDS version at least 2.10.55"
21997
21998         local dom=$DIR/$tdir/dom
21999
22000         mkdir -p $DIR/$tdir
22001
22002         $LFS setstripe -E 1024K -L mdt -E EOF $dom
22003
22004         lctl set_param -n mdc.*.stats=clear
22005         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
22006         cancel_lru_locks mdc
22007         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
22008         # second stat to check size is cached on client
22009         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
22010         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
22011         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
22012         rm -f $dom
22013 }
22014 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
22015
22016 test_271ba() {
22017         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22018                 skip "Need MDS version at least 2.10.55"
22019
22020         local dom=$DIR/$tdir/dom
22021
22022         mkdir -p $DIR/$tdir
22023
22024         $LFS setstripe -E 1024K -L mdt -E EOF $dom
22025
22026         lctl set_param -n mdc.*.stats=clear
22027         lctl set_param -n osc.*.stats=clear
22028         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
22029         cancel_lru_locks mdc
22030         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
22031         # second stat to check size is cached on client
22032         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
22033         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
22034         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
22035         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
22036         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
22037         rm -f $dom
22038 }
22039 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
22040
22041
22042 get_mdc_stats() {
22043         local mdtidx=$1
22044         local param=$2
22045         local mdt=MDT$(printf %04x $mdtidx)
22046
22047         if [ -z $param ]; then
22048                 lctl get_param -n mdc.*$mdt*.stats
22049         else
22050                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
22051         fi
22052 }
22053
22054 test_271c() {
22055         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22056                 skip "Need MDS version at least 2.10.55"
22057
22058         local dom=$DIR/$tdir/dom
22059
22060         mkdir -p $DIR/$tdir
22061
22062         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22063
22064         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
22065         local facet=mds$((mdtidx + 1))
22066
22067         cancel_lru_locks mdc
22068         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
22069         createmany -o $dom 1000
22070         lctl set_param -n mdc.*.stats=clear
22071         smalliomany -w $dom 1000 200
22072         get_mdc_stats $mdtidx
22073         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
22074         # Each file has 1 open, 1 IO enqueues, total 2000
22075         # but now we have also +1 getxattr for security.capability, total 3000
22076         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
22077         unlinkmany $dom 1000
22078
22079         cancel_lru_locks mdc
22080         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
22081         createmany -o $dom 1000
22082         lctl set_param -n mdc.*.stats=clear
22083         smalliomany -w $dom 1000 200
22084         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
22085         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
22086         # for OPEN and IO lock.
22087         [ $((enq - enq_2)) -ge 1000 ] ||
22088                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
22089         unlinkmany $dom 1000
22090         return 0
22091 }
22092 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
22093
22094 cleanup_271def_tests() {
22095         trap 0
22096         rm -f $1
22097 }
22098
22099 test_271d() {
22100         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
22101                 skip "Need MDS version at least 2.10.57"
22102
22103         local dom=$DIR/$tdir/dom
22104         local tmp=$TMP/$tfile
22105         trap "cleanup_271def_tests $tmp" EXIT
22106
22107         mkdir -p $DIR/$tdir
22108
22109         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22110
22111         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
22112
22113         cancel_lru_locks mdc
22114         dd if=/dev/urandom of=$tmp bs=1000 count=1
22115         dd if=$tmp of=$dom bs=1000 count=1
22116         cancel_lru_locks mdc
22117
22118         cat /etc/hosts >> $tmp
22119         lctl set_param -n mdc.*.stats=clear
22120
22121         # append data to the same file it should update local page
22122         echo "Append to the same page"
22123         cat /etc/hosts >> $dom
22124         local num=$(get_mdc_stats $mdtidx ost_read)
22125         local ra=$(get_mdc_stats $mdtidx req_active)
22126         local rw=$(get_mdc_stats $mdtidx req_waittime)
22127
22128         [ -z $num ] || error "$num READ RPC occured"
22129         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22130         echo "... DONE"
22131
22132         # compare content
22133         cmp $tmp $dom || error "file miscompare"
22134
22135         cancel_lru_locks mdc
22136         lctl set_param -n mdc.*.stats=clear
22137
22138         echo "Open and read file"
22139         cat $dom > /dev/null
22140         local num=$(get_mdc_stats $mdtidx ost_read)
22141         local ra=$(get_mdc_stats $mdtidx req_active)
22142         local rw=$(get_mdc_stats $mdtidx req_waittime)
22143
22144         [ -z $num ] || error "$num READ RPC occured"
22145         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22146         echo "... DONE"
22147
22148         # compare content
22149         cmp $tmp $dom || error "file miscompare"
22150
22151         return 0
22152 }
22153 run_test 271d "DoM: read on open (1K file in reply buffer)"
22154
22155 test_271f() {
22156         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
22157                 skip "Need MDS version at least 2.10.57"
22158
22159         local dom=$DIR/$tdir/dom
22160         local tmp=$TMP/$tfile
22161         trap "cleanup_271def_tests $tmp" EXIT
22162
22163         mkdir -p $DIR/$tdir
22164
22165         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22166
22167         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
22168
22169         cancel_lru_locks mdc
22170         dd if=/dev/urandom of=$tmp bs=265000 count=1
22171         dd if=$tmp of=$dom bs=265000 count=1
22172         cancel_lru_locks mdc
22173         cat /etc/hosts >> $tmp
22174         lctl set_param -n mdc.*.stats=clear
22175
22176         echo "Append to the same page"
22177         cat /etc/hosts >> $dom
22178         local num=$(get_mdc_stats $mdtidx ost_read)
22179         local ra=$(get_mdc_stats $mdtidx req_active)
22180         local rw=$(get_mdc_stats $mdtidx req_waittime)
22181
22182         [ -z $num ] || error "$num READ RPC occured"
22183         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22184         echo "... DONE"
22185
22186         # compare content
22187         cmp $tmp $dom || error "file miscompare"
22188
22189         cancel_lru_locks mdc
22190         lctl set_param -n mdc.*.stats=clear
22191
22192         echo "Open and read file"
22193         cat $dom > /dev/null
22194         local num=$(get_mdc_stats $mdtidx ost_read)
22195         local ra=$(get_mdc_stats $mdtidx req_active)
22196         local rw=$(get_mdc_stats $mdtidx req_waittime)
22197
22198         [ -z $num ] && num=0
22199         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
22200         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22201         echo "... DONE"
22202
22203         # compare content
22204         cmp $tmp $dom || error "file miscompare"
22205
22206         return 0
22207 }
22208 run_test 271f "DoM: read on open (200K file and read tail)"
22209
22210 test_271g() {
22211         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
22212                 skip "Skipping due to old client or server version"
22213
22214         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
22215         # to get layout
22216         $CHECKSTAT -t file $DIR1/$tfile
22217
22218         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
22219         MULTIOP_PID=$!
22220         sleep 1
22221         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
22222         $LCTL set_param fail_loc=0x80000314
22223         rm $DIR1/$tfile || error "Unlink fails"
22224         RC=$?
22225         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
22226         [ $RC -eq 0 ] || error "Failed write to stale object"
22227 }
22228 run_test 271g "Discard DoM data vs client flush race"
22229
22230 test_272a() {
22231         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22232                 skip "Need MDS version at least 2.11.50"
22233
22234         local dom=$DIR/$tdir/dom
22235         mkdir -p $DIR/$tdir
22236
22237         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
22238         dd if=/dev/urandom of=$dom bs=512K count=1 ||
22239                 error "failed to write data into $dom"
22240         local old_md5=$(md5sum $dom)
22241
22242         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
22243                 error "failed to migrate to the same DoM component"
22244
22245         local new_md5=$(md5sum $dom)
22246
22247         [ "$old_md5" == "$new_md5" ] ||
22248                 error "md5sum differ: $old_md5, $new_md5"
22249
22250         [ $($LFS getstripe -c $dom) -eq 2 ] ||
22251                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
22252 }
22253 run_test 272a "DoM migration: new layout with the same DOM component"
22254
22255 test_272b() {
22256         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22257                 skip "Need MDS version at least 2.11.50"
22258
22259         local dom=$DIR/$tdir/dom
22260         mkdir -p $DIR/$tdir
22261         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22262
22263         local mdtidx=$($LFS getstripe -m $dom)
22264         local mdtname=MDT$(printf %04x $mdtidx)
22265         local facet=mds$((mdtidx + 1))
22266
22267         local mdtfree1=$(do_facet $facet \
22268                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22269         dd if=/dev/urandom of=$dom bs=2M count=1 ||
22270                 error "failed to write data into $dom"
22271         local old_md5=$(md5sum $dom)
22272         cancel_lru_locks mdc
22273         local mdtfree1=$(do_facet $facet \
22274                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22275
22276         $LFS migrate -c2 $dom ||
22277                 error "failed to migrate to the new composite layout"
22278         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22279                 error "MDT stripe was not removed"
22280
22281         cancel_lru_locks mdc
22282         local new_md5=$(md5sum $dom)
22283         [ "$old_md5" == "$new_md5" ] ||
22284                 error "$old_md5 != $new_md5"
22285
22286         # Skip free space checks with ZFS
22287         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22288                 local mdtfree2=$(do_facet $facet \
22289                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22290                 [ $mdtfree2 -gt $mdtfree1 ] ||
22291                         error "MDT space is not freed after migration"
22292         fi
22293         return 0
22294 }
22295 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
22296
22297 test_272c() {
22298         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22299                 skip "Need MDS version at least 2.11.50"
22300
22301         local dom=$DIR/$tdir/$tfile
22302         mkdir -p $DIR/$tdir
22303         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22304
22305         local mdtidx=$($LFS getstripe -m $dom)
22306         local mdtname=MDT$(printf %04x $mdtidx)
22307         local facet=mds$((mdtidx + 1))
22308
22309         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
22310                 error "failed to write data into $dom"
22311         local old_md5=$(md5sum $dom)
22312         cancel_lru_locks mdc
22313         local mdtfree1=$(do_facet $facet \
22314                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22315
22316         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
22317                 error "failed to migrate to the new composite layout"
22318         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
22319                 error "MDT stripe was not removed"
22320
22321         cancel_lru_locks mdc
22322         local new_md5=$(md5sum $dom)
22323         [ "$old_md5" == "$new_md5" ] ||
22324                 error "$old_md5 != $new_md5"
22325
22326         # Skip free space checks with ZFS
22327         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22328                 local mdtfree2=$(do_facet $facet \
22329                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22330                 [ $mdtfree2 -gt $mdtfree1 ] ||
22331                         error "MDS space is not freed after migration"
22332         fi
22333         return 0
22334 }
22335 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
22336
22337 test_272d() {
22338         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22339                 skip "Need MDS version at least 2.12.55"
22340
22341         local dom=$DIR/$tdir/$tfile
22342         mkdir -p $DIR/$tdir
22343         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22344
22345         local mdtidx=$($LFS getstripe -m $dom)
22346         local mdtname=MDT$(printf %04x $mdtidx)
22347         local facet=mds$((mdtidx + 1))
22348
22349         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
22350                 error "failed to write data into $dom"
22351         local old_md5=$(md5sum $dom)
22352         cancel_lru_locks mdc
22353         local mdtfree1=$(do_facet $facet \
22354                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22355
22356         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
22357                 error "failed mirroring to the new composite layout"
22358         $LFS mirror resync $dom ||
22359                 error "failed mirror resync"
22360         $LFS mirror split --mirror-id 1 -d $dom ||
22361                 error "failed mirror split"
22362
22363         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22364                 error "MDT stripe was not removed"
22365
22366         cancel_lru_locks mdc
22367         local new_md5=$(md5sum $dom)
22368         [ "$old_md5" == "$new_md5" ] ||
22369                 error "$old_md5 != $new_md5"
22370
22371         # Skip free space checks with ZFS
22372         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22373                 local mdtfree2=$(do_facet $facet \
22374                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22375                 [ $mdtfree2 -gt $mdtfree1 ] ||
22376                         error "MDS space is not freed after DOM mirror deletion"
22377         fi
22378         return 0
22379 }
22380 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
22381
22382 test_272e() {
22383         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22384                 skip "Need MDS version at least 2.12.55"
22385
22386         local dom=$DIR/$tdir/$tfile
22387         mkdir -p $DIR/$tdir
22388         $LFS setstripe -c 2 $dom
22389
22390         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
22391                 error "failed to write data into $dom"
22392         local old_md5=$(md5sum $dom)
22393         cancel_lru_locks mdc
22394
22395         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
22396                 error "failed mirroring to the DOM layout"
22397         $LFS mirror resync $dom ||
22398                 error "failed mirror resync"
22399         $LFS mirror split --mirror-id 1 -d $dom ||
22400                 error "failed mirror split"
22401
22402         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22403                 error "MDT stripe was not removed"
22404
22405         cancel_lru_locks mdc
22406         local new_md5=$(md5sum $dom)
22407         [ "$old_md5" == "$new_md5" ] ||
22408                 error "$old_md5 != $new_md5"
22409
22410         return 0
22411 }
22412 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
22413
22414 test_272f() {
22415         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22416                 skip "Need MDS version at least 2.12.55"
22417
22418         local dom=$DIR/$tdir/$tfile
22419         mkdir -p $DIR/$tdir
22420         $LFS setstripe -c 2 $dom
22421
22422         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
22423                 error "failed to write data into $dom"
22424         local old_md5=$(md5sum $dom)
22425         cancel_lru_locks mdc
22426
22427         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
22428                 error "failed migrating to the DOM file"
22429
22430         cancel_lru_locks mdc
22431         local new_md5=$(md5sum $dom)
22432         [ "$old_md5" != "$new_md5" ] &&
22433                 error "$old_md5 != $new_md5"
22434
22435         return 0
22436 }
22437 run_test 272f "DoM migration: OST-striped file to DOM file"
22438
22439 test_273a() {
22440         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22441                 skip "Need MDS version at least 2.11.50"
22442
22443         # Layout swap cannot be done if either file has DOM component,
22444         # this will never be supported, migration should be used instead
22445
22446         local dom=$DIR/$tdir/$tfile
22447         mkdir -p $DIR/$tdir
22448
22449         $LFS setstripe -c2 ${dom}_plain
22450         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
22451         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
22452                 error "can swap layout with DoM component"
22453         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
22454                 error "can swap layout with DoM component"
22455
22456         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
22457         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
22458                 error "can swap layout with DoM component"
22459         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
22460                 error "can swap layout with DoM component"
22461         return 0
22462 }
22463 run_test 273a "DoM: layout swapping should fail with DOM"
22464
22465 test_273b() {
22466         mkdir -p $DIR/$tdir
22467         $LFS setstripe -E 1M -L mdt -E -1 -c -1 $DIR/$tdir
22468
22469 #define OBD_FAIL_MDS_COMMITRW_DELAY      0x16b
22470         do_facet mds1 $LCTL set_param fail_loc=0x8000016b fail_val=2
22471
22472         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
22473 }
22474 run_test 273b "DoM: race writeback and object destroy"
22475
22476 test_275() {
22477         remote_ost_nodsh && skip "remote OST with nodsh"
22478         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
22479                 skip "Need OST version >= 2.10.57"
22480
22481         local file=$DIR/$tfile
22482         local oss
22483
22484         oss=$(comma_list $(osts_nodes))
22485
22486         dd if=/dev/urandom of=$file bs=1M count=2 ||
22487                 error "failed to create a file"
22488         cancel_lru_locks osc
22489
22490         #lock 1
22491         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
22492                 error "failed to read a file"
22493
22494 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
22495         $LCTL set_param fail_loc=0x8000031f
22496
22497         cancel_lru_locks osc &
22498         sleep 1
22499
22500 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
22501         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
22502         #IO takes another lock, but matches the PENDING one
22503         #and places it to the IO RPC
22504         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
22505                 error "failed to read a file with PENDING lock"
22506 }
22507 run_test 275 "Read on a canceled duplicate lock"
22508
22509 test_276() {
22510         remote_ost_nodsh && skip "remote OST with nodsh"
22511         local pid
22512
22513         do_facet ost1 "(while true; do \
22514                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
22515                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
22516         pid=$!
22517
22518         for LOOP in $(seq 20); do
22519                 stop ost1
22520                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
22521         done
22522         kill -9 $pid
22523         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
22524                 rm $TMP/sanity_276_pid"
22525 }
22526 run_test 276 "Race between mount and obd_statfs"
22527
22528 test_277() {
22529         $LCTL set_param ldlm.namespaces.*.lru_size=0
22530         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22531         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
22532                         grep ^used_mb | awk '{print $2}')
22533         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
22534         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
22535                 oflag=direct conv=notrunc
22536         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
22537                         grep ^used_mb | awk '{print $2}')
22538         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
22539 }
22540 run_test 277 "Direct IO shall drop page cache"
22541
22542 test_278() {
22543         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
22544         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
22545         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
22546                 skip "needs the same host for mdt1 mdt2" && return
22547
22548         local pid1
22549         local pid2
22550
22551 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
22552         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
22553         stop mds2 &
22554         pid2=$!
22555
22556         stop mds1
22557
22558         echo "Starting MDTs"
22559         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22560         wait $pid2
22561 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
22562 #will return NULL
22563         do_facet mds2 $LCTL set_param fail_loc=0
22564
22565         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
22566         wait_recovery_complete mds2
22567 }
22568 run_test 278 "Race starting MDS between MDTs stop/start"
22569
22570 test_280() {
22571         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
22572                 skip "Need MGS version at least 2.13.52"
22573         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22574         combined_mgs_mds || skip "needs combined MGS/MDT"
22575
22576         umount_client $MOUNT
22577 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
22578         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
22579
22580         mount_client $MOUNT &
22581         sleep 1
22582         stop mgs || error "stop mgs failed"
22583         #for a race mgs would crash
22584         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
22585         # make sure we unmount client before remounting
22586         wait
22587         umount_client $MOUNT
22588         mount_client $MOUNT || error "mount client failed"
22589 }
22590 run_test 280 "Race between MGS umount and client llog processing"
22591
22592 cleanup_test_300() {
22593         trap 0
22594         umask $SAVE_UMASK
22595 }
22596 test_striped_dir() {
22597         local mdt_index=$1
22598         local stripe_count
22599         local stripe_index
22600
22601         mkdir -p $DIR/$tdir
22602
22603         SAVE_UMASK=$(umask)
22604         trap cleanup_test_300 RETURN EXIT
22605
22606         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
22607                                                 $DIR/$tdir/striped_dir ||
22608                 error "set striped dir error"
22609
22610         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
22611         [ "$mode" = "755" ] || error "expect 755 got $mode"
22612
22613         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
22614                 error "getdirstripe failed"
22615         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
22616         if [ "$stripe_count" != "2" ]; then
22617                 error "1:stripe_count is $stripe_count, expect 2"
22618         fi
22619         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
22620         if [ "$stripe_count" != "2" ]; then
22621                 error "2:stripe_count is $stripe_count, expect 2"
22622         fi
22623
22624         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
22625         if [ "$stripe_index" != "$mdt_index" ]; then
22626                 error "stripe_index is $stripe_index, expect $mdt_index"
22627         fi
22628
22629         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
22630                 error "nlink error after create striped dir"
22631
22632         mkdir $DIR/$tdir/striped_dir/a
22633         mkdir $DIR/$tdir/striped_dir/b
22634
22635         stat $DIR/$tdir/striped_dir/a ||
22636                 error "create dir under striped dir failed"
22637         stat $DIR/$tdir/striped_dir/b ||
22638                 error "create dir under striped dir failed"
22639
22640         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
22641                 error "nlink error after mkdir"
22642
22643         rmdir $DIR/$tdir/striped_dir/a
22644         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
22645                 error "nlink error after rmdir"
22646
22647         rmdir $DIR/$tdir/striped_dir/b
22648         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
22649                 error "nlink error after rmdir"
22650
22651         chattr +i $DIR/$tdir/striped_dir
22652         createmany -o $DIR/$tdir/striped_dir/f 10 &&
22653                 error "immutable flags not working under striped dir!"
22654         chattr -i $DIR/$tdir/striped_dir
22655
22656         rmdir $DIR/$tdir/striped_dir ||
22657                 error "rmdir striped dir error"
22658
22659         cleanup_test_300
22660
22661         true
22662 }
22663
22664 test_300a() {
22665         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22666                 skip "skipped for lustre < 2.7.0"
22667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22668         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22669
22670         test_striped_dir 0 || error "failed on striped dir on MDT0"
22671         test_striped_dir 1 || error "failed on striped dir on MDT0"
22672 }
22673 run_test 300a "basic striped dir sanity test"
22674
22675 test_300b() {
22676         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22677                 skip "skipped for lustre < 2.7.0"
22678         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22679         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22680
22681         local i
22682         local mtime1
22683         local mtime2
22684         local mtime3
22685
22686         test_mkdir $DIR/$tdir || error "mkdir fail"
22687         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22688                 error "set striped dir error"
22689         for i in {0..9}; do
22690                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
22691                 sleep 1
22692                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
22693                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
22694                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
22695                 sleep 1
22696                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
22697                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
22698                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
22699         done
22700         true
22701 }
22702 run_test 300b "check ctime/mtime for striped dir"
22703
22704 test_300c() {
22705         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22706                 skip "skipped for lustre < 2.7.0"
22707         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22708         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22709
22710         local file_count
22711
22712         mkdir_on_mdt0 $DIR/$tdir
22713         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
22714                 error "set striped dir error"
22715
22716         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
22717                 error "chown striped dir failed"
22718
22719         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
22720                 error "create 5k files failed"
22721
22722         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
22723
22724         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
22725
22726         rm -rf $DIR/$tdir
22727 }
22728 run_test 300c "chown && check ls under striped directory"
22729
22730 test_300d() {
22731         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22732                 skip "skipped for lustre < 2.7.0"
22733         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22734         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22735
22736         local stripe_count
22737         local file
22738
22739         mkdir -p $DIR/$tdir
22740         $LFS setstripe -c 2 $DIR/$tdir
22741
22742         #local striped directory
22743         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22744                 error "set striped dir error"
22745         #look at the directories for debug purposes
22746         ls -l $DIR/$tdir
22747         $LFS getdirstripe $DIR/$tdir
22748         ls -l $DIR/$tdir/striped_dir
22749         $LFS getdirstripe $DIR/$tdir/striped_dir
22750         createmany -o $DIR/$tdir/striped_dir/f 10 ||
22751                 error "create 10 files failed"
22752
22753         #remote striped directory
22754         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
22755                 error "set striped dir error"
22756         #look at the directories for debug purposes
22757         ls -l $DIR/$tdir
22758         $LFS getdirstripe $DIR/$tdir
22759         ls -l $DIR/$tdir/remote_striped_dir
22760         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
22761         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
22762                 error "create 10 files failed"
22763
22764         for file in $(find $DIR/$tdir); do
22765                 stripe_count=$($LFS getstripe -c $file)
22766                 [ $stripe_count -eq 2 ] ||
22767                         error "wrong stripe $stripe_count for $file"
22768         done
22769
22770         rm -rf $DIR/$tdir
22771 }
22772 run_test 300d "check default stripe under striped directory"
22773
22774 test_300e() {
22775         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22776                 skip "Need MDS version at least 2.7.55"
22777         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22778         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22779
22780         local stripe_count
22781         local file
22782
22783         mkdir -p $DIR/$tdir
22784
22785         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22786                 error "set striped dir error"
22787
22788         touch $DIR/$tdir/striped_dir/a
22789         touch $DIR/$tdir/striped_dir/b
22790         touch $DIR/$tdir/striped_dir/c
22791
22792         mkdir $DIR/$tdir/striped_dir/dir_a
22793         mkdir $DIR/$tdir/striped_dir/dir_b
22794         mkdir $DIR/$tdir/striped_dir/dir_c
22795
22796         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
22797                 error "set striped adir under striped dir error"
22798
22799         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
22800                 error "set striped bdir under striped dir error"
22801
22802         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
22803                 error "set striped cdir under striped dir error"
22804
22805         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
22806                 error "rename dir under striped dir fails"
22807
22808         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
22809                 error "rename dir under different stripes fails"
22810
22811         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
22812                 error "rename file under striped dir should succeed"
22813
22814         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
22815                 error "rename dir under striped dir should succeed"
22816
22817         rm -rf $DIR/$tdir
22818 }
22819 run_test 300e "check rename under striped directory"
22820
22821 test_300f() {
22822         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22823         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22824         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22825                 skip "Need MDS version at least 2.7.55"
22826
22827         local stripe_count
22828         local file
22829
22830         rm -rf $DIR/$tdir
22831         mkdir -p $DIR/$tdir
22832
22833         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22834                 error "set striped dir error"
22835
22836         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
22837                 error "set striped dir error"
22838
22839         touch $DIR/$tdir/striped_dir/a
22840         mkdir $DIR/$tdir/striped_dir/dir_a
22841         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
22842                 error "create striped dir under striped dir fails"
22843
22844         touch $DIR/$tdir/striped_dir1/b
22845         mkdir $DIR/$tdir/striped_dir1/dir_b
22846         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
22847                 error "create striped dir under striped dir fails"
22848
22849         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
22850                 error "rename dir under different striped dir should fail"
22851
22852         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
22853                 error "rename striped dir under diff striped dir should fail"
22854
22855         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
22856                 error "rename file under diff striped dirs fails"
22857
22858         rm -rf $DIR/$tdir
22859 }
22860 run_test 300f "check rename cross striped directory"
22861
22862 test_300_check_default_striped_dir()
22863 {
22864         local dirname=$1
22865         local default_count=$2
22866         local default_index=$3
22867         local stripe_count
22868         local stripe_index
22869         local dir_stripe_index
22870         local dir
22871
22872         echo "checking $dirname $default_count $default_index"
22873         $LFS setdirstripe -D -c $default_count -i $default_index \
22874                                 -H all_char $DIR/$tdir/$dirname ||
22875                 error "set default stripe on striped dir error"
22876         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
22877         [ $stripe_count -eq $default_count ] ||
22878                 error "expect $default_count get $stripe_count for $dirname"
22879
22880         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
22881         [ $stripe_index -eq $default_index ] ||
22882                 error "expect $default_index get $stripe_index for $dirname"
22883
22884         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
22885                                                 error "create dirs failed"
22886
22887         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
22888         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
22889         for dir in $(find $DIR/$tdir/$dirname/*); do
22890                 stripe_count=$($LFS getdirstripe -c $dir)
22891                 (( $stripe_count == $default_count )) ||
22892                 (( $stripe_count == $MDSCOUNT && $default_count == -1 )) ||
22893                 (( $stripe_count == 0 )) || (( $default_count == 1 )) ||
22894                 error "stripe count $default_count != $stripe_count for $dir"
22895
22896                 stripe_index=$($LFS getdirstripe -i $dir)
22897                 [ $default_index -eq -1 ] ||
22898                         [ $stripe_index -eq $default_index ] ||
22899                         error "$stripe_index != $default_index for $dir"
22900
22901                 #check default stripe
22902                 stripe_count=$($LFS getdirstripe -D -c $dir)
22903                 [ $stripe_count -eq $default_count ] ||
22904                 error "default count $default_count != $stripe_count for $dir"
22905
22906                 stripe_index=$($LFS getdirstripe -D -i $dir)
22907                 [ $stripe_index -eq $default_index ] ||
22908                 error "default index $default_index != $stripe_index for $dir"
22909         done
22910         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
22911 }
22912
22913 test_300g() {
22914         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22915         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22916                 skip "Need MDS version at least 2.7.55"
22917
22918         local dir
22919         local stripe_count
22920         local stripe_index
22921
22922         mkdir_on_mdt0 $DIR/$tdir
22923         mkdir $DIR/$tdir/normal_dir
22924
22925         #Checking when client cache stripe index
22926         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22927         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
22928                 error "create striped_dir failed"
22929
22930         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
22931                 error "create dir0 fails"
22932         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
22933         [ $stripe_index -eq 0 ] ||
22934                 error "dir0 expect index 0 got $stripe_index"
22935
22936         mkdir $DIR/$tdir/striped_dir/dir1 ||
22937                 error "create dir1 fails"
22938         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
22939         [ $stripe_index -eq 1 ] ||
22940                 error "dir1 expect index 1 got $stripe_index"
22941
22942         #check default stripe count/stripe index
22943         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
22944         test_300_check_default_striped_dir normal_dir 1 0
22945         test_300_check_default_striped_dir normal_dir -1 1
22946         test_300_check_default_striped_dir normal_dir 2 -1
22947
22948         #delete default stripe information
22949         echo "delete default stripeEA"
22950         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
22951                 error "set default stripe on striped dir error"
22952
22953         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
22954         for dir in $(find $DIR/$tdir/normal_dir/*); do
22955                 stripe_count=$($LFS getdirstripe -c $dir)
22956                 [ $stripe_count -eq 0 ] ||
22957                         error "expect 1 get $stripe_count for $dir"
22958         done
22959 }
22960 run_test 300g "check default striped directory for normal directory"
22961
22962 test_300h() {
22963         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22964         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22965                 skip "Need MDS version at least 2.7.55"
22966
22967         local dir
22968         local stripe_count
22969
22970         mkdir $DIR/$tdir
22971         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
22972                 error "set striped dir error"
22973
22974         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
22975         test_300_check_default_striped_dir striped_dir 1 0
22976         test_300_check_default_striped_dir striped_dir -1 1
22977         test_300_check_default_striped_dir striped_dir 2 -1
22978
22979         #delete default stripe information
22980         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
22981                 error "set default stripe on striped dir error"
22982
22983         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
22984         for dir in $(find $DIR/$tdir/striped_dir/*); do
22985                 stripe_count=$($LFS getdirstripe -c $dir)
22986                 [ $stripe_count -eq 0 ] ||
22987                         error "expect 1 get $stripe_count for $dir"
22988         done
22989 }
22990 run_test 300h "check default striped directory for striped directory"
22991
22992 test_300i() {
22993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22994         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22995         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22996                 skip "Need MDS version at least 2.7.55"
22997
22998         local stripe_count
22999         local file
23000
23001         mkdir $DIR/$tdir
23002
23003         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23004                 error "set striped dir error"
23005
23006         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
23007                 error "create files under striped dir failed"
23008
23009         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
23010                 error "set striped hashdir error"
23011
23012         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
23013                 error "create dir0 under hash dir failed"
23014         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
23015                 error "create dir1 under hash dir failed"
23016         $LFS setdirstripe -i0 -c$MDSCOUNT -H crush $DIR/$tdir/hashdir/d2 ||
23017                 error "create dir2 under hash dir failed"
23018
23019         # unfortunately, we need to umount to clear dir layout cache for now
23020         # once we fully implement dir layout, we can drop this
23021         umount_client $MOUNT || error "umount failed"
23022         mount_client $MOUNT || error "mount failed"
23023
23024         $LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir
23025         local dircnt=$($LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir | wc -l)
23026         [ $dircnt -eq 2 ] || error "lfs find striped dir got:$dircnt,except:1"
23027
23028         #set the stripe to be unknown hash type
23029         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
23030         $LCTL set_param fail_loc=0x1901
23031         for ((i = 0; i < 10; i++)); do
23032                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
23033                         error "stat f-$i failed"
23034                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
23035         done
23036
23037         touch $DIR/$tdir/striped_dir/f0 &&
23038                 error "create under striped dir with unknown hash should fail"
23039
23040         $LCTL set_param fail_loc=0
23041
23042         umount_client $MOUNT || error "umount failed"
23043         mount_client $MOUNT || error "mount failed"
23044
23045         return 0
23046 }
23047 run_test 300i "client handle unknown hash type striped directory"
23048
23049 test_300j() {
23050         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23051         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23052         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23053                 skip "Need MDS version at least 2.7.55"
23054
23055         local stripe_count
23056         local file
23057
23058         mkdir $DIR/$tdir
23059
23060         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
23061         $LCTL set_param fail_loc=0x1702
23062         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23063                 error "set striped dir error"
23064
23065         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
23066                 error "create files under striped dir failed"
23067
23068         $LCTL set_param fail_loc=0
23069
23070         rm -rf $DIR/$tdir || error "unlink striped dir fails"
23071
23072         return 0
23073 }
23074 run_test 300j "test large update record"
23075
23076 test_300k() {
23077         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23078         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23079         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23080                 skip "Need MDS version at least 2.7.55"
23081
23082         # this test needs a huge transaction
23083         local kb
23084         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23085              osd*.$FSNAME-MDT0000.kbytestotal")
23086         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
23087
23088         local stripe_count
23089         local file
23090
23091         mkdir $DIR/$tdir
23092
23093         #define OBD_FAIL_LARGE_STRIPE   0x1703
23094         $LCTL set_param fail_loc=0x1703
23095         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
23096                 error "set striped dir error"
23097         $LCTL set_param fail_loc=0
23098
23099         $LFS getdirstripe $DIR/$tdir/striped_dir ||
23100                 error "getstripeddir fails"
23101         rm -rf $DIR/$tdir/striped_dir ||
23102                 error "unlink striped dir fails"
23103
23104         return 0
23105 }
23106 run_test 300k "test large striped directory"
23107
23108 test_300l() {
23109         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23110         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23111         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23112                 skip "Need MDS version at least 2.7.55"
23113
23114         local stripe_index
23115
23116         test_mkdir -p $DIR/$tdir/striped_dir
23117         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
23118                         error "chown $RUNAS_ID failed"
23119         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
23120                 error "set default striped dir failed"
23121
23122         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
23123         $LCTL set_param fail_loc=0x80000158
23124         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
23125
23126         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
23127         [ $stripe_index -eq 1 ] ||
23128                 error "expect 1 get $stripe_index for $dir"
23129 }
23130 run_test 300l "non-root user to create dir under striped dir with stale layout"
23131
23132 test_300m() {
23133         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23134         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
23135         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23136                 skip "Need MDS version at least 2.7.55"
23137
23138         mkdir -p $DIR/$tdir/striped_dir
23139         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
23140                 error "set default stripes dir error"
23141
23142         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
23143
23144         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
23145         [ $stripe_count -eq 0 ] ||
23146                         error "expect 0 get $stripe_count for a"
23147
23148         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
23149                 error "set default stripes dir error"
23150
23151         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
23152
23153         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
23154         [ $stripe_count -eq 0 ] ||
23155                         error "expect 0 get $stripe_count for b"
23156
23157         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
23158                 error "set default stripes dir error"
23159
23160         mkdir $DIR/$tdir/striped_dir/c &&
23161                 error "default stripe_index is invalid, mkdir c should fails"
23162
23163         rm -rf $DIR/$tdir || error "rmdir fails"
23164 }
23165 run_test 300m "setstriped directory on single MDT FS"
23166
23167 cleanup_300n() {
23168         local list=$(comma_list $(mdts_nodes))
23169
23170         trap 0
23171         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
23172 }
23173
23174 test_300n() {
23175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23176         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23177         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23178                 skip "Need MDS version at least 2.7.55"
23179         remote_mds_nodsh && skip "remote MDS with nodsh"
23180
23181         local stripe_index
23182         local list=$(comma_list $(mdts_nodes))
23183
23184         trap cleanup_300n RETURN EXIT
23185         mkdir -p $DIR/$tdir
23186         chmod 777 $DIR/$tdir
23187         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
23188                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
23189                 error "create striped dir succeeds with gid=0"
23190
23191         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
23192         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
23193                 error "create striped dir fails with gid=-1"
23194
23195         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
23196         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
23197                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
23198                 error "set default striped dir succeeds with gid=0"
23199
23200
23201         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
23202         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
23203                 error "set default striped dir fails with gid=-1"
23204
23205
23206         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
23207         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
23208                                         error "create test_dir fails"
23209         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
23210                                         error "create test_dir1 fails"
23211         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
23212                                         error "create test_dir2 fails"
23213         cleanup_300n
23214 }
23215 run_test 300n "non-root user to create dir under striped dir with default EA"
23216
23217 test_300o() {
23218         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23219         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23220         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23221                 skip "Need MDS version at least 2.7.55"
23222
23223         local numfree1
23224         local numfree2
23225
23226         mkdir -p $DIR/$tdir
23227
23228         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
23229         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
23230         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
23231                 skip "not enough free inodes $numfree1 $numfree2"
23232         fi
23233
23234         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
23235         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
23236         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
23237                 skip "not enough free space $numfree1 $numfree2"
23238         fi
23239
23240         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
23241                 error "setdirstripe fails"
23242
23243         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
23244                 error "create dirs fails"
23245
23246         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
23247         ls $DIR/$tdir/striped_dir > /dev/null ||
23248                 error "ls striped dir fails"
23249         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
23250                 error "unlink big striped dir fails"
23251 }
23252 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
23253
23254 test_300p() {
23255         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23256         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23257         remote_mds_nodsh && skip "remote MDS with nodsh"
23258
23259         mkdir_on_mdt0 $DIR/$tdir
23260
23261         #define OBD_FAIL_OUT_ENOSPC     0x1704
23262         do_facet mds2 lctl set_param fail_loc=0x80001704
23263         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
23264                  && error "create striped directory should fail"
23265
23266         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
23267
23268         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
23269         true
23270 }
23271 run_test 300p "create striped directory without space"
23272
23273 test_300q() {
23274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23275         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23276
23277         local fd=$(free_fd)
23278         local cmd="exec $fd<$tdir"
23279         cd $DIR
23280         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
23281         eval $cmd
23282         cmd="exec $fd<&-"
23283         trap "eval $cmd" EXIT
23284         cd $tdir || error "cd $tdir fails"
23285         rmdir  ../$tdir || error "rmdir $tdir fails"
23286         mkdir local_dir && error "create dir succeeds"
23287         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
23288         eval $cmd
23289         return 0
23290 }
23291 run_test 300q "create remote directory under orphan directory"
23292
23293 test_300r() {
23294         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23295                 skip "Need MDS version at least 2.7.55" && return
23296         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23297
23298         mkdir $DIR/$tdir
23299
23300         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
23301                 error "set striped dir error"
23302
23303         $LFS getdirstripe $DIR/$tdir/striped_dir ||
23304                 error "getstripeddir fails"
23305
23306         local stripe_count
23307         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
23308                       awk '/lmv_stripe_count:/ { print $2 }')
23309
23310         [ $MDSCOUNT -ne $stripe_count ] &&
23311                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
23312
23313         rm -rf $DIR/$tdir/striped_dir ||
23314                 error "unlink striped dir fails"
23315 }
23316 run_test 300r "test -1 striped directory"
23317
23318 test_300s_helper() {
23319         local count=$1
23320
23321         local stripe_dir=$DIR/$tdir/striped_dir.$count
23322
23323         $LFS mkdir -c $count $stripe_dir ||
23324                 error "lfs mkdir -c error"
23325
23326         $LFS getdirstripe $stripe_dir ||
23327                 error "lfs getdirstripe fails"
23328
23329         local stripe_count
23330         stripe_count=$($LFS getdirstripe $stripe_dir |
23331                       awk '/lmv_stripe_count:/ { print $2 }')
23332
23333         [ $count -ne $stripe_count ] &&
23334                 error_noexit "bad stripe count $stripe_count expected $count"
23335
23336         local dupe_stripes
23337         dupe_stripes=$($LFS getdirstripe $stripe_dir |
23338                 awk '/0x/ {count[$1] += 1}; END {
23339                         for (idx in count) {
23340                                 if (count[idx]>1) {
23341                                         print "index " idx " count " count[idx]
23342                                 }
23343                         }
23344                 }')
23345
23346         if [[ -n "$dupe_stripes" ]] ; then
23347                 lfs getdirstripe $stripe_dir
23348                 error_noexit "Dupe MDT above: $dupe_stripes "
23349         fi
23350
23351         rm -rf $stripe_dir ||
23352                 error_noexit "unlink $stripe_dir fails"
23353 }
23354
23355 test_300s() {
23356         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23357                 skip "Need MDS version at least 2.7.55" && return
23358         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23359
23360         mkdir $DIR/$tdir
23361         for count in $(seq 2 $MDSCOUNT); do
23362                 test_300s_helper $count
23363         done
23364 }
23365 run_test 300s "test lfs mkdir -c without -i"
23366
23367
23368 prepare_remote_file() {
23369         mkdir $DIR/$tdir/src_dir ||
23370                 error "create remote source failed"
23371
23372         cp /etc/hosts $DIR/$tdir/src_dir/a ||
23373                  error "cp to remote source failed"
23374         touch $DIR/$tdir/src_dir/a
23375
23376         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
23377                 error "create remote target dir failed"
23378
23379         touch $DIR/$tdir/tgt_dir/b
23380
23381         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
23382                 error "rename dir cross MDT failed!"
23383
23384         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
23385                 error "src_child still exists after rename"
23386
23387         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
23388                 error "missing file(a) after rename"
23389
23390         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
23391                 error "diff after rename"
23392 }
23393
23394 test_310a() {
23395         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
23396         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23397
23398         local remote_file=$DIR/$tdir/tgt_dir/b
23399
23400         mkdir -p $DIR/$tdir
23401
23402         prepare_remote_file || error "prepare remote file failed"
23403
23404         #open-unlink file
23405         $OPENUNLINK $remote_file $remote_file ||
23406                 error "openunlink $remote_file failed"
23407         $CHECKSTAT -a $remote_file || error "$remote_file exists"
23408 }
23409 run_test 310a "open unlink remote file"
23410
23411 test_310b() {
23412         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
23413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23414
23415         local remote_file=$DIR/$tdir/tgt_dir/b
23416
23417         mkdir -p $DIR/$tdir
23418
23419         prepare_remote_file || error "prepare remote file failed"
23420
23421         ln $remote_file $DIR/$tfile || error "link failed for remote file"
23422         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
23423         $CHECKSTAT -t file $remote_file || error "check file failed"
23424 }
23425 run_test 310b "unlink remote file with multiple links while open"
23426
23427 test_310c() {
23428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23429         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
23430
23431         local remote_file=$DIR/$tdir/tgt_dir/b
23432
23433         mkdir -p $DIR/$tdir
23434
23435         prepare_remote_file || error "prepare remote file failed"
23436
23437         ln $remote_file $DIR/$tfile || error "link failed for remote file"
23438         multiop_bg_pause $remote_file O_uc ||
23439                         error "mulitop failed for remote file"
23440         MULTIPID=$!
23441         $MULTIOP $DIR/$tfile Ouc
23442         kill -USR1 $MULTIPID
23443         wait $MULTIPID
23444 }
23445 run_test 310c "open-unlink remote file with multiple links"
23446
23447 #LU-4825
23448 test_311() {
23449         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23450         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
23451         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
23452                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
23453         remote_mds_nodsh && skip "remote MDS with nodsh"
23454
23455         local old_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
23456         local mdts=$(comma_list $(mdts_nodes))
23457
23458         mkdir -p $DIR/$tdir
23459         $LFS setstripe -i 0 -c 1 $DIR/$tdir
23460         createmany -o $DIR/$tdir/$tfile. 1000
23461
23462         # statfs data is not real time, let's just calculate it
23463         old_iused=$((old_iused + 1000))
23464
23465         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23466                         osp.*OST0000*MDT0000.create_count")
23467         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23468                                 osp.*OST0000*MDT0000.max_create_count")
23469         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
23470
23471         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
23472         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
23473         [ $index -ne 0 ] || error "$tfile stripe index is 0"
23474
23475         unlinkmany $DIR/$tdir/$tfile. 1000
23476
23477         do_nodes $mdts "$LCTL set_param -n \
23478                         osp.*OST0000*.max_create_count=$max_count"
23479         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
23480                 do_nodes $mdts "$LCTL set_param -n \
23481                                 osp.*OST0000*.create_count=$count"
23482         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
23483                         grep "=0" && error "create_count is zero"
23484
23485         local new_iused
23486         for i in $(seq 120); do
23487                 new_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
23488                 # system may be too busy to destroy all objs in time, use
23489                 # a somewhat small value to not fail autotest
23490                 [ $((old_iused - new_iused)) -gt 400 ] && break
23491                 sleep 1
23492         done
23493
23494         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
23495         [ $((old_iused - new_iused)) -gt 400 ] ||
23496                 error "objs not destroyed after unlink"
23497 }
23498 run_test 311 "disable OSP precreate, and unlink should destroy objs"
23499
23500 zfs_oid_to_objid()
23501 {
23502         local ost=$1
23503         local objid=$2
23504
23505         local vdevdir=$(dirname $(facet_vdevice $ost))
23506         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
23507         local zfs_zapid=$(do_facet $ost $cmd |
23508                           grep -w "/O/0/d$((objid%32))" -C 5 |
23509                           awk '/Object/{getline; print $1}')
23510         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
23511                           awk "/$objid = /"'{printf $3}')
23512
23513         echo $zfs_objid
23514 }
23515
23516 zfs_object_blksz() {
23517         local ost=$1
23518         local objid=$2
23519
23520         local vdevdir=$(dirname $(facet_vdevice $ost))
23521         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
23522         local blksz=$(do_facet $ost $cmd $objid |
23523                       awk '/dblk/{getline; printf $4}')
23524
23525         case "${blksz: -1}" in
23526                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
23527                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
23528                 *) ;;
23529         esac
23530
23531         echo $blksz
23532 }
23533
23534 test_312() { # LU-4856
23535         remote_ost_nodsh && skip "remote OST with nodsh"
23536         [ "$ost1_FSTYPE" = "zfs" ] ||
23537                 skip_env "the test only applies to zfs"
23538
23539         local max_blksz=$(do_facet ost1 \
23540                           $ZFS get -p recordsize $(facet_device ost1) |
23541                           awk '!/VALUE/{print $3}')
23542
23543         # to make life a little bit easier
23544         $LFS mkdir -c 1 -i 0 $DIR/$tdir
23545         $LFS setstripe -c 1 -i 0 $DIR/$tdir
23546
23547         local tf=$DIR/$tdir/$tfile
23548         touch $tf
23549         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23550
23551         # Get ZFS object id
23552         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23553         # block size change by sequential overwrite
23554         local bs
23555
23556         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
23557                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
23558
23559                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
23560                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
23561         done
23562         rm -f $tf
23563
23564         # block size change by sequential append write
23565         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
23566         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23567         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23568         local count
23569
23570         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
23571                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
23572                         oflag=sync conv=notrunc
23573
23574                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
23575                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
23576                         error "blksz error, actual $blksz, " \
23577                                 "expected: 2 * $count * $PAGE_SIZE"
23578         done
23579         rm -f $tf
23580
23581         # random write
23582         touch $tf
23583         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23584         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23585
23586         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
23587         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23588         [ $blksz -eq $PAGE_SIZE ] ||
23589                 error "blksz error: $blksz, expected: $PAGE_SIZE"
23590
23591         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
23592         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23593         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
23594
23595         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
23596         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23597         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
23598 }
23599 run_test 312 "make sure ZFS adjusts its block size by write pattern"
23600
23601 test_313() {
23602         remote_ost_nodsh && skip "remote OST with nodsh"
23603
23604         local file=$DIR/$tfile
23605
23606         rm -f $file
23607         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
23608
23609         # define OBD_FAIL_TGT_RCVD_EIO           0x720
23610         do_facet ost1 "$LCTL set_param fail_loc=0x720"
23611         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
23612                 error "write should failed"
23613         do_facet ost1 "$LCTL set_param fail_loc=0"
23614         rm -f $file
23615 }
23616 run_test 313 "io should fail after last_rcvd update fail"
23617
23618 test_314() {
23619         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
23620
23621         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
23622         do_facet ost1 "$LCTL set_param fail_loc=0x720"
23623         rm -f $DIR/$tfile
23624         wait_delete_completed
23625         do_facet ost1 "$LCTL set_param fail_loc=0"
23626 }
23627 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
23628
23629 test_315() { # LU-618
23630         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
23631
23632         local file=$DIR/$tfile
23633         rm -f $file
23634
23635         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
23636                 error "multiop file write failed"
23637         $MULTIOP $file oO_RDONLY:r4063232_c &
23638         PID=$!
23639
23640         sleep 2
23641
23642         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
23643         kill -USR1 $PID
23644
23645         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
23646         rm -f $file
23647 }
23648 run_test 315 "read should be accounted"
23649
23650 test_316() {
23651         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
23652         large_xattr_enabled || skip_env "ea_inode feature disabled"
23653
23654         rm -rf $DIR/$tdir/d
23655         mkdir -p $DIR/$tdir/d
23656         chown nobody $DIR/$tdir/d
23657         touch $DIR/$tdir/d/file
23658
23659         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
23660 }
23661 run_test 316 "lfs mv"
23662
23663 test_317() {
23664         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
23665                 skip "Need MDS version at least 2.11.53"
23666         if [ "$ost1_FSTYPE" == "zfs" ]; then
23667                 skip "LU-10370: no implementation for ZFS"
23668         fi
23669
23670         local trunc_sz
23671         local grant_blk_size
23672
23673         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
23674                         awk '/grant_block_size:/ { print $2; exit; }')
23675         #
23676         # Create File of size 5M. Truncate it to below size's and verify
23677         # blocks count.
23678         #
23679         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
23680                 error "Create file $DIR/$tfile failed"
23681         stack_trap "rm -f $DIR/$tfile" EXIT
23682
23683         for trunc_sz in 2097152 4097 4000 509 0; do
23684                 $TRUNCATE $DIR/$tfile $trunc_sz ||
23685                         error "truncate $tfile to $trunc_sz failed"
23686                 local sz=$(stat --format=%s $DIR/$tfile)
23687                 local blk=$(stat --format=%b $DIR/$tfile)
23688                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
23689                                      grant_blk_size) * 8))
23690
23691                 if [[ $blk -ne $trunc_blk ]]; then
23692                         $(which stat) $DIR/$tfile
23693                         error "Expected Block $trunc_blk got $blk for $tfile"
23694                 fi
23695
23696                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
23697                         error "Expected Size $trunc_sz got $sz for $tfile"
23698         done
23699
23700         #
23701         # sparse file test
23702         # Create file with a hole and write actual two blocks. Block count
23703         # must be 16.
23704         #
23705         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
23706                 conv=fsync || error "Create file : $DIR/$tfile"
23707
23708         # Calculate the final truncate size.
23709         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
23710
23711         #
23712         # truncate to size $trunc_sz bytes. Strip the last block
23713         # The block count must drop to 8
23714         #
23715         $TRUNCATE $DIR/$tfile $trunc_sz ||
23716                 error "truncate $tfile to $trunc_sz failed"
23717
23718         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
23719         sz=$(stat --format=%s $DIR/$tfile)
23720         blk=$(stat --format=%b $DIR/$tfile)
23721
23722         if [[ $blk -ne $trunc_bsz ]]; then
23723                 $(which stat) $DIR/$tfile
23724                 error "Expected Block $trunc_bsz got $blk for $tfile"
23725         fi
23726
23727         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
23728                 error "Expected Size $trunc_sz got $sz for $tfile"
23729 }
23730 run_test 317 "Verify blocks get correctly update after truncate"
23731
23732 test_318() {
23733         local llite_name="llite.$($LFS getname $MOUNT | awk '{print $1}')"
23734         local old_max_active=$($LCTL get_param -n \
23735                             ${llite_name}.max_read_ahead_async_active \
23736                             2>/dev/null)
23737
23738         $LCTL set_param llite.*.max_read_ahead_async_active=256
23739         local max_active=$($LCTL get_param -n \
23740                            ${llite_name}.max_read_ahead_async_active \
23741                            2>/dev/null)
23742         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
23743
23744         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
23745                 error "set max_read_ahead_async_active should succeed"
23746
23747         $LCTL set_param llite.*.max_read_ahead_async_active=512
23748         max_active=$($LCTL get_param -n \
23749                      ${llite_name}.max_read_ahead_async_active 2>/dev/null)
23750         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
23751
23752         # restore @max_active
23753         [ $old_max_active -ne 0 ] && $LCTL set_param \
23754                 llite.*.max_read_ahead_async_active=$old_max_active
23755
23756         local old_threshold=$($LCTL get_param -n \
23757                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
23758         local max_per_file_mb=$($LCTL get_param -n \
23759                 ${llite_name}.max_read_ahead_per_file_mb 2>/dev/null)
23760
23761         local invalid=$(($max_per_file_mb + 1))
23762         $LCTL set_param \
23763                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
23764                         && error "set $invalid should fail"
23765
23766         local valid=$(($invalid - 1))
23767         $LCTL set_param \
23768                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
23769                         error "set $valid should succeed"
23770         local threshold=$($LCTL get_param -n \
23771                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
23772         [ $threshold -eq $valid ] || error \
23773                 "expect threshold $valid got $threshold"
23774         $LCTL set_param \
23775                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
23776 }
23777 run_test 318 "Verify async readahead tunables"
23778
23779 test_319() {
23780         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
23781
23782         local before=$(date +%s)
23783         local evict
23784         local mdir=$DIR/$tdir
23785         local file=$mdir/xxx
23786
23787         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
23788         touch $file
23789
23790 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
23791         $LCTL set_param fail_val=5 fail_loc=0x8000032c
23792         $LFS mv -m1 $file &
23793
23794         sleep 1
23795         dd if=$file of=/dev/null
23796         wait
23797         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
23798           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
23799
23800         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
23801 }
23802 run_test 319 "lost lease lock on migrate error"
23803
23804 test_398a() { # LU-4198
23805         local ost1_imp=$(get_osc_import_name client ost1)
23806         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23807                          cut -d'.' -f2)
23808
23809         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23810         $LCTL set_param ldlm.namespaces.*.lru_size=clear
23811
23812         # request a new lock on client
23813         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23814
23815         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
23816         local lock_count=$($LCTL get_param -n \
23817                            ldlm.namespaces.$imp_name.lru_size)
23818         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
23819
23820         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
23821
23822         # no lock cached, should use lockless IO and not enqueue new lock
23823         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
23824         lock_count=$($LCTL get_param -n \
23825                      ldlm.namespaces.$imp_name.lru_size)
23826         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
23827 }
23828 run_test 398a "direct IO should cancel lock otherwise lockless"
23829
23830 test_398b() { # LU-4198
23831         which fio || skip_env "no fio installed"
23832         $LFS setstripe -c -1 $DIR/$tfile
23833
23834         local size=12
23835         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
23836
23837         local njobs=4
23838         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
23839         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
23840                 --numjobs=$njobs --fallocate=none \
23841                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23842                 --filename=$DIR/$tfile &
23843         bg_pid=$!
23844
23845         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
23846         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
23847                 --numjobs=$njobs --fallocate=none \
23848                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23849                 --filename=$DIR/$tfile || true
23850         wait $bg_pid
23851
23852         rm -f $DIR/$tfile
23853 }
23854 run_test 398b "DIO and buffer IO race"
23855
23856 test_398c() { # LU-4198
23857         local ost1_imp=$(get_osc_import_name client ost1)
23858         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23859                          cut -d'.' -f2)
23860
23861         which fio || skip_env "no fio installed"
23862
23863         saved_debug=$($LCTL get_param -n debug)
23864         $LCTL set_param debug=0
23865
23866         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
23867         ((size /= 1024)) # by megabytes
23868         ((size /= 2)) # write half of the OST at most
23869         [ $size -gt 40 ] && size=40 #reduce test time anyway
23870
23871         $LFS setstripe -c 1 $DIR/$tfile
23872
23873         # it seems like ldiskfs reserves more space than necessary if the
23874         # writing blocks are not mapped, so it extends the file firstly
23875         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
23876         cancel_lru_locks osc
23877
23878         # clear and verify rpc_stats later
23879         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
23880
23881         local njobs=4
23882         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
23883         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
23884                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
23885                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23886                 --filename=$DIR/$tfile
23887         [ $? -eq 0 ] || error "fio write error"
23888
23889         [ $($LCTL get_param -n ldlm.namespaces.$imp_name.lock_count) -eq 0 ] ||
23890                 error "Locks were requested while doing AIO"
23891
23892         # get the percentage of 1-page I/O
23893         pct=$($LCTL get_param osc.${imp_name}.rpc_stats |
23894                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
23895                 awk '{print $7}')
23896         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
23897
23898         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
23899         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
23900                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
23901                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23902                 --filename=$DIR/$tfile
23903         [ $? -eq 0 ] || error "fio mixed read write error"
23904
23905         echo "AIO with large block size ${size}M"
23906         fio --name=rand-rw --rw=randrw --bs=${size}M --direct=1 \
23907                 --numjobs=1 --fallocate=none --ioengine=libaio \
23908                 --iodepth=16 --allow_file_create=0 --size=${size}M \
23909                 --filename=$DIR/$tfile
23910         [ $? -eq 0 ] || error "fio large block size failed"
23911
23912         rm -f $DIR/$tfile
23913         $LCTL set_param debug="$saved_debug"
23914 }
23915 run_test 398c "run fio to test AIO"
23916
23917 test_398d() { #  LU-13846
23918         which aiocp || skip_env "no aiocp installed"
23919         local aio_file=$DIR/$tfile.aio
23920
23921         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
23922
23923         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=64
23924         aiocp -a $PAGE_SIZE -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file
23925         stack_trap "rm -f $DIR/$tfile $aio_file"
23926
23927         diff $DIR/$tfile $aio_file || error "file diff after aiocp"
23928
23929         # make sure we don't crash and fail properly
23930         aiocp -a 512 -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
23931                 error "aio not aligned with PAGE SIZE should fail"
23932
23933         rm -f $DIR/$tfile $aio_file
23934 }
23935 run_test 398d "run aiocp to verify block size > stripe size"
23936
23937 test_398e() {
23938         dd if=/dev/zero of=$DIR/$tfile bs=1234 count=1
23939         touch $DIR/$tfile.new
23940         dd if=$DIR/$tfile of=$DIR/$tfile.new bs=1M count=1 oflag=direct
23941 }
23942 run_test 398e "O_Direct open cleared by fcntl doesn't cause hang"
23943
23944 test_398f() { #  LU-14687
23945         which aiocp || skip_env "no aiocp installed"
23946         local aio_file=$DIR/$tfile.aio
23947
23948         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
23949
23950         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
23951         stack_trap "rm -f $DIR/$tfile $aio_file"
23952
23953         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
23954         $LCTL set_param fail_loc=0x1418
23955         # make sure we don't crash and fail properly
23956         aiocp -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
23957                 error "aio with page allocation failure succeeded"
23958         $LCTL set_param fail_loc=0
23959         diff $DIR/$tfile $aio_file
23960         [[ $? != 0 ]] || error "no diff after failed aiocp"
23961 }
23962 run_test 398f "verify aio handles ll_direct_rw_pages errors correctly"
23963
23964 # NB: To get the parallel DIO behavior in LU-13798, there must be > 1
23965 # stripe and i/o size must be > stripe size
23966 # Old style synchronous DIO waits after submitting each chunk, resulting in a
23967 # single RPC in flight.  This test shows async DIO submission is working by
23968 # showing multiple RPCs in flight.
23969 test_398g() { #  LU-13798
23970         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
23971
23972         # We need to do some i/o first to acquire enough grant to put our RPCs
23973         # in flight; otherwise a new connection may not have enough grant
23974         # available
23975         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
23976                 error "parallel dio failed"
23977         stack_trap "rm -f $DIR/$tfile"
23978
23979         # Reduce RPC size to 1M to avoid combination in to larger RPCs
23980         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
23981         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
23982         stack_trap "$LCTL set_param -n $pages_per_rpc"
23983
23984         # Recreate file so it's empty
23985         rm -f $DIR/$tfile
23986         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
23987         #Pause rpc completion to guarantee we see multiple rpcs in flight
23988         #define OBD_FAIL_OST_BRW_PAUSE_BULK
23989         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
23990         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
23991
23992         # Clear rpc stats
23993         $LCTL set_param osc.*.rpc_stats=c
23994
23995         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
23996                 error "parallel dio failed"
23997         stack_trap "rm -f $DIR/$tfile"
23998
23999         $LCTL get_param osc.*-OST0000-*.rpc_stats
24000         pct=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
24001                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
24002                 grep "8:" | awk '{print $8}')
24003         # We look at the "8 rpcs in flight" field, and verify A) it is present
24004         # and B) it includes all RPCs.  This proves we had 8 RPCs in flight,
24005         # as expected for an 8M DIO to a file with 1M stripes.
24006         [ $pct -eq 100 ] || error "we should see 8 RPCs in flight"
24007
24008         # Verify turning off parallel dio works as expected
24009         # Clear rpc stats
24010         $LCTL set_param osc.*.rpc_stats=c
24011         $LCTL set_param llite.*.parallel_dio=0
24012         stack_trap '$LCTL set_param llite.*.parallel_dio=1'
24013
24014         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24015                 error "dio with parallel dio disabled failed"
24016
24017         # Ideally, we would see only one RPC in flight here, but there is an
24018         # unavoidable race between i/o completion and RPC in flight counting,
24019         # so while only 1 i/o is in flight at a time, the RPC in flight counter
24020         # will sometimes exceed 1 (3 or 4 is not rare on VM testing).
24021         # So instead we just verify it's always < 8.
24022         $LCTL get_param osc.*-OST0000-*.rpc_stats
24023         ret=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
24024                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
24025                 grep '^$' -B1 | grep . | awk '{print $1}')
24026         [ $ret != "8:" ] ||
24027                 error "we should see fewer than 8 RPCs in flight (saw $ret)"
24028 }
24029 run_test 398g "verify parallel dio async RPC submission"
24030
24031 test_398h() { #  LU-13798
24032         local dio_file=$DIR/$tfile.dio
24033
24034         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
24035
24036         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24037         stack_trap "rm -f $DIR/$tfile $dio_file"
24038
24039         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct ||
24040                 error "parallel dio failed"
24041         diff $DIR/$tfile $dio_file
24042         [[ $? == 0 ]] || error "file diff after aiocp"
24043 }
24044 run_test 398h "verify correctness of read & write with i/o size >> stripe size"
24045
24046 test_398i() { #  LU-13798
24047         local dio_file=$DIR/$tfile.dio
24048
24049         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
24050
24051         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24052         stack_trap "rm -f $DIR/$tfile $dio_file"
24053
24054         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
24055         $LCTL set_param fail_loc=0x1418
24056         # make sure we don't crash and fail properly
24057         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct &&
24058                 error "parallel dio page allocation failure succeeded"
24059         diff $DIR/$tfile $dio_file
24060         [[ $? != 0 ]] || error "no diff after failed aiocp"
24061 }
24062 run_test 398i "verify parallel dio handles ll_direct_rw_pages errors correctly"
24063
24064 test_398j() { #  LU-13798
24065         # Stripe size > RPC size but less than i/o size tests split across
24066         # stripes and RPCs for individual i/o op
24067         $LFS setstripe -o 0,0 -S 4M $DIR/$tfile $DIR/$tfile.2
24068
24069         # Reduce RPC size to 1M to guarantee split to multiple RPCs per stripe
24070         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
24071         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
24072         stack_trap "$LCTL set_param -n $pages_per_rpc"
24073
24074         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
24075                 error "parallel dio write failed"
24076         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.2"
24077
24078         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct ||
24079                 error "parallel dio read failed"
24080         diff $DIR/$tfile $DIR/$tfile.2
24081         [[ $? == 0 ]] || error "file diff after parallel dio read"
24082 }
24083 run_test 398j "test parallel dio where stripe size > rpc_size"
24084
24085 test_398k() { #  LU-13798
24086         wait_delete_completed
24087         wait_mds_ost_sync
24088
24089         # 4 stripe file; we will cause out of space on OST0
24090         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
24091
24092         # Fill OST0 (if it's not too large)
24093         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
24094                    head -n1)
24095         if [[ $ORIGFREE -gt $MAXFREE ]]; then
24096                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
24097         fi
24098         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
24099         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
24100                 error "dd should fill OST0"
24101         stack_trap "rm -f $DIR/$tfile.1"
24102
24103         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24104         err=$?
24105
24106         ls -la $DIR/$tfile
24107         $CHECKSTAT -t file -s 0 $DIR/$tfile ||
24108                 error "file is not 0 bytes in size"
24109
24110         # dd above should not succeed, but don't error until here so we can
24111         # get debug info above
24112         [[ $err != 0 ]] ||
24113                 error "parallel dio write with enospc succeeded"
24114         stack_trap "rm -f $DIR/$tfile"
24115 }
24116 run_test 398k "test enospc on first stripe"
24117
24118 test_398l() { #  LU-13798
24119         wait_delete_completed
24120         wait_mds_ost_sync
24121
24122         # 4 stripe file; we will cause out of space on OST0
24123         # Note the 1M stripe size and the > 1M i/o size mean this ENOSPC
24124         # happens on the second i/o chunk we issue
24125         $LFS setstripe -o 1,0,1,0 -S 1M $DIR/$tfile $DIR/$tfile.2
24126
24127         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=2 oflag=direct
24128         stack_trap "rm -f $DIR/$tfile"
24129
24130         # Fill OST0 (if it's not too large)
24131         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
24132                    head -n1)
24133         if [[ $ORIGFREE -gt $MAXFREE ]]; then
24134                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
24135         fi
24136         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
24137         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
24138                 error "dd should fill OST0"
24139         stack_trap "rm -f $DIR/$tfile.1"
24140
24141         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 oflag=direct
24142         err=$?
24143         stack_trap "rm -f $DIR/$tfile.2"
24144
24145         # Check that short write completed as expected
24146         ls -la $DIR/$tfile.2
24147         $CHECKSTAT -t file -s 1048576 $DIR/$tfile.2 ||
24148                 error "file is not 1M in size"
24149
24150         # dd above should not succeed, but don't error until here so we can
24151         # get debug info above
24152         [[ $err != 0 ]] ||
24153                 error "parallel dio write with enospc succeeded"
24154
24155         # Truncate source file to same length as output file and diff them
24156         $TRUNCATE $DIR/$tfile 1048576
24157         diff $DIR/$tfile $DIR/$tfile.2
24158         [[ $? == 0 ]] || error "data incorrect after short write"
24159 }
24160 run_test 398l "test enospc on intermediate stripe/RPC"
24161
24162 test_398m() { #  LU-13798
24163         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
24164
24165         # Set up failure on OST0, the first stripe:
24166         #define OBD_FAIL_OST_BRW_WRITE_BULK     0x20e
24167         #NB: Fail val is ost # + 1, because we cannot use cfs_fail_val = 0
24168         # So this fail_val specifies OST0
24169         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=1
24170         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
24171
24172         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
24173                 error "parallel dio write with failure on first stripe succeeded"
24174         stack_trap "rm -f $DIR/$tfile"
24175         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
24176
24177         # Place data in file for read
24178         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
24179                 error "parallel dio write failed"
24180
24181         # Fail read on OST0, first stripe
24182         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
24183         do_facet ost1 $LCTL set_param fail_loc=0x20f fail_val=1
24184         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
24185                 error "parallel dio read with error on first stripe succeeded"
24186         rm -f $DIR/$tfile.2
24187         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
24188
24189         # Switch to testing on OST1, second stripe
24190         # Clear file contents, maintain striping
24191         echo > $DIR/$tfile
24192         # Set up failure on OST1, second stripe:
24193         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=2
24194         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
24195
24196         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
24197                 error "parallel dio write with failure on first stripe succeeded"
24198         stack_trap "rm -f $DIR/$tfile"
24199         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
24200
24201         # Place data in file for read
24202         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
24203                 error "parallel dio write failed"
24204
24205         # Fail read on OST1, second stripe
24206         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
24207         do_facet ost2 $LCTL set_param fail_loc=0x20f fail_val=2
24208         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
24209                 error "parallel dio read with error on first stripe succeeded"
24210         rm -f $DIR/$tfile.2
24211         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
24212 }
24213 run_test 398m "test RPC failures with parallel dio"
24214
24215 # Parallel submission of DIO should not cause problems for append, but it's
24216 # important to verify.
24217 test_398n() { #  LU-13798
24218         $LFS setstripe -C 2 -S 1M $DIR/$tfile
24219
24220         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 ||
24221                 error "dd to create source file failed"
24222         stack_trap "rm -f $DIR/$tfile"
24223
24224         dd if=$DIR/$tfile of=$DIR/$tfile.1 bs=8M count=8 oflag=direct oflag=append ||
24225                 error "parallel dio write with failure on second stripe succeeded"
24226         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.1"
24227         diff $DIR/$tfile $DIR/$tfile.1
24228         [[ $? == 0 ]] || error "data incorrect after append"
24229
24230 }
24231 run_test 398n "test append with parallel DIO"
24232
24233 test_fake_rw() {
24234         local read_write=$1
24235         if [ "$read_write" = "write" ]; then
24236                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
24237         elif [ "$read_write" = "read" ]; then
24238                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
24239         else
24240                 error "argument error"
24241         fi
24242
24243         # turn off debug for performance testing
24244         local saved_debug=$($LCTL get_param -n debug)
24245         $LCTL set_param debug=0
24246
24247         $LFS setstripe -c 1 -i 0 $DIR/$tfile
24248
24249         # get ost1 size - $FSNAME-OST0000
24250         local ost1_avail_size=$($LFS df $DIR | awk /${ost1_svc}/'{ print $4 }')
24251         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
24252         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
24253
24254         if [ "$read_write" = "read" ]; then
24255                 $TRUNCATE $DIR/$tfile $(expr 1048576 \* $blocks)
24256         fi
24257
24258         local start_time=$(date +%s.%N)
24259         $dd_cmd bs=1M count=$blocks oflag=sync ||
24260                 error "real dd $read_write error"
24261         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
24262
24263         if [ "$read_write" = "write" ]; then
24264                 rm -f $DIR/$tfile
24265         fi
24266
24267         # define OBD_FAIL_OST_FAKE_RW           0x238
24268         do_facet ost1 $LCTL set_param fail_loc=0x238
24269
24270         local start_time=$(date +%s.%N)
24271         $dd_cmd bs=1M count=$blocks oflag=sync ||
24272                 error "fake dd $read_write error"
24273         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
24274
24275         if [ "$read_write" = "write" ]; then
24276                 # verify file size
24277                 cancel_lru_locks osc
24278                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
24279                         error "$tfile size not $blocks MB"
24280         fi
24281         do_facet ost1 $LCTL set_param fail_loc=0
24282
24283         echo "fake $read_write $duration_fake vs. normal $read_write" \
24284                 "$duration in seconds"
24285         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
24286                 error_not_in_vm "fake write is slower"
24287
24288         $LCTL set_param -n debug="$saved_debug"
24289         rm -f $DIR/$tfile
24290 }
24291 test_399a() { # LU-7655 for OST fake write
24292         remote_ost_nodsh && skip "remote OST with nodsh"
24293
24294         test_fake_rw write
24295 }
24296 run_test 399a "fake write should not be slower than normal write"
24297
24298 test_399b() { # LU-8726 for OST fake read
24299         remote_ost_nodsh && skip "remote OST with nodsh"
24300         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
24301                 skip_env "ldiskfs only test"
24302         fi
24303
24304         test_fake_rw read
24305 }
24306 run_test 399b "fake read should not be slower than normal read"
24307
24308 test_400a() { # LU-1606, was conf-sanity test_74
24309         if ! which $CC > /dev/null 2>&1; then
24310                 skip_env "$CC is not installed"
24311         fi
24312
24313         local extra_flags=''
24314         local out=$TMP/$tfile
24315         local prefix=/usr/include/lustre
24316         local prog
24317
24318         # Oleg removes c files in his test rig so test if any c files exist
24319         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
24320                 skip_env "Needed c test files are missing"
24321
24322         if ! [[ -d $prefix ]]; then
24323                 # Assume we're running in tree and fixup the include path.
24324                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
24325                 extra_flags+=" -L$LUSTRE/utils/.lib"
24326         fi
24327
24328         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
24329                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
24330                         error "client api broken"
24331         done
24332         rm -f $out
24333 }
24334 run_test 400a "Lustre client api program can compile and link"
24335
24336 test_400b() { # LU-1606, LU-5011
24337         local header
24338         local out=$TMP/$tfile
24339         local prefix=/usr/include/linux/lustre
24340
24341         # We use a hard coded prefix so that this test will not fail
24342         # when run in tree. There are headers in lustre/include/lustre/
24343         # that are not packaged (like lustre_idl.h) and have more
24344         # complicated include dependencies (like config.h and lnet/types.h).
24345         # Since this test about correct packaging we just skip them when
24346         # they don't exist (see below) rather than try to fixup cppflags.
24347
24348         if ! which $CC > /dev/null 2>&1; then
24349                 skip_env "$CC is not installed"
24350         fi
24351
24352         for header in $prefix/*.h; do
24353                 if ! [[ -f "$header" ]]; then
24354                         continue
24355                 fi
24356
24357                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
24358                         continue # lustre_ioctl.h is internal header
24359                 fi
24360
24361                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
24362                         error "cannot compile '$header'"
24363         done
24364         rm -f $out
24365 }
24366 run_test 400b "packaged headers can be compiled"
24367
24368 test_401a() { #LU-7437
24369         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
24370         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
24371
24372         #count the number of parameters by "list_param -R"
24373         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
24374         #count the number of parameters by listing proc files
24375         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
24376         echo "proc_dirs='$proc_dirs'"
24377         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
24378         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
24379                       sort -u | wc -l)
24380
24381         [ $params -eq $procs ] ||
24382                 error "found $params parameters vs. $procs proc files"
24383
24384         # test the list_param -D option only returns directories
24385         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
24386         #count the number of parameters by listing proc directories
24387         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
24388                 sort -u | wc -l)
24389
24390         [ $params -eq $procs ] ||
24391                 error "found $params parameters vs. $procs proc files"
24392 }
24393 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
24394
24395 test_401b() {
24396         # jobid_var may not allow arbitrary values, so use jobid_name
24397         # if available
24398         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24399                 local testname=jobid_name tmp='testing%p'
24400         else
24401                 local testname=jobid_var tmp=testing
24402         fi
24403
24404         local save=$($LCTL get_param -n $testname)
24405
24406         $LCTL set_param foo=bar $testname=$tmp bar=baz &&
24407                 error "no error returned when setting bad parameters"
24408
24409         local jobid_new=$($LCTL get_param -n foe $testname baz)
24410         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
24411
24412         $LCTL set_param -n fog=bam $testname=$save bat=fog
24413         local jobid_old=$($LCTL get_param -n foe $testname bag)
24414         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
24415 }
24416 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
24417
24418 test_401c() {
24419         # jobid_var may not allow arbitrary values, so use jobid_name
24420         # if available
24421         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24422                 local testname=jobid_name
24423         else
24424                 local testname=jobid_var
24425         fi
24426
24427         local jobid_var_old=$($LCTL get_param -n $testname)
24428         local jobid_var_new
24429
24430         $LCTL set_param $testname= &&
24431                 error "no error returned for 'set_param a='"
24432
24433         jobid_var_new=$($LCTL get_param -n $testname)
24434         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
24435                 error "$testname was changed by setting without value"
24436
24437         $LCTL set_param $testname &&
24438                 error "no error returned for 'set_param a'"
24439
24440         jobid_var_new=$($LCTL get_param -n $testname)
24441         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
24442                 error "$testname was changed by setting without value"
24443 }
24444 run_test 401c "Verify 'lctl set_param' without value fails in either format."
24445
24446 test_401d() {
24447         # jobid_var may not allow arbitrary values, so use jobid_name
24448         # if available
24449         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24450                 local testname=jobid_name new_value='foo=bar%p'
24451         else
24452                 local testname=jobid_var new_valuie=foo=bar
24453         fi
24454
24455         local jobid_var_old=$($LCTL get_param -n $testname)
24456         local jobid_var_new
24457
24458         $LCTL set_param $testname=$new_value ||
24459                 error "'set_param a=b' did not accept a value containing '='"
24460
24461         jobid_var_new=$($LCTL get_param -n $testname)
24462         [[ "$jobid_var_new" == "$new_value" ]] ||
24463                 error "'set_param a=b' failed on a value containing '='"
24464
24465         # Reset the $testname to test the other format
24466         $LCTL set_param $testname=$jobid_var_old
24467         jobid_var_new=$($LCTL get_param -n $testname)
24468         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
24469                 error "failed to reset $testname"
24470
24471         $LCTL set_param $testname $new_value ||
24472                 error "'set_param a b' did not accept a value containing '='"
24473
24474         jobid_var_new=$($LCTL get_param -n $testname)
24475         [[ "$jobid_var_new" == "$new_value" ]] ||
24476                 error "'set_param a b' failed on a value containing '='"
24477
24478         $LCTL set_param $testname $jobid_var_old
24479         jobid_var_new=$($LCTL get_param -n $testname)
24480         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
24481                 error "failed to reset $testname"
24482 }
24483 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
24484
24485 test_401e() { # LU-14779
24486         $LCTL list_param -R "ldlm.namespaces.MGC*" ||
24487                 error "lctl list_param MGC* failed"
24488         $LCTL get_param "ldlm.namespaces.MGC*" || error "lctl get_param failed"
24489         $LCTL get_param "ldlm.namespaces.MGC*.lru_size" ||
24490                 error "lctl get_param lru_size failed"
24491 }
24492 run_test 401e "verify 'lctl get_param' works with NID in parameter"
24493
24494 test_402() {
24495         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
24496         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
24497                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
24498         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
24499                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
24500                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
24501         remote_mds_nodsh && skip "remote MDS with nodsh"
24502
24503         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
24504 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
24505         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
24506         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
24507                 echo "Touch failed - OK"
24508 }
24509 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
24510
24511 test_403() {
24512         local file1=$DIR/$tfile.1
24513         local file2=$DIR/$tfile.2
24514         local tfile=$TMP/$tfile
24515
24516         rm -f $file1 $file2 $tfile
24517
24518         touch $file1
24519         ln $file1 $file2
24520
24521         # 30 sec OBD_TIMEOUT in ll_getattr()
24522         # right before populating st_nlink
24523         $LCTL set_param fail_loc=0x80001409
24524         stat -c %h $file1 > $tfile &
24525
24526         # create an alias, drop all locks and reclaim the dentry
24527         < $file2
24528         cancel_lru_locks mdc
24529         cancel_lru_locks osc
24530         sysctl -w vm.drop_caches=2
24531
24532         wait
24533
24534         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
24535
24536         rm -f $tfile $file1 $file2
24537 }
24538 run_test 403 "i_nlink should not drop to zero due to aliasing"
24539
24540 test_404() { # LU-6601
24541         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
24542                 skip "Need server version newer than 2.8.52"
24543         remote_mds_nodsh && skip "remote MDS with nodsh"
24544
24545         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
24546                 awk '/osp .*-osc-MDT/ { print $4}')
24547
24548         local osp
24549         for osp in $mosps; do
24550                 echo "Deactivate: " $osp
24551                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
24552                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
24553                         awk -vp=$osp '$4 == p { print $2 }')
24554                 [ $stat = IN ] || {
24555                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
24556                         error "deactivate error"
24557                 }
24558                 echo "Activate: " $osp
24559                 do_facet $SINGLEMDS $LCTL --device %$osp activate
24560                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
24561                         awk -vp=$osp '$4 == p { print $2 }')
24562                 [ $stat = UP ] || {
24563                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
24564                         error "activate error"
24565                 }
24566         done
24567 }
24568 run_test 404 "validate manual {de}activated works properly for OSPs"
24569
24570 test_405() {
24571         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
24572         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
24573                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
24574                         skip "Layout swap lock is not supported"
24575
24576         check_swap_layouts_support
24577         check_swap_layout_no_dom $DIR
24578
24579         test_mkdir $DIR/$tdir
24580         swap_lock_test -d $DIR/$tdir ||
24581                 error "One layout swap locked test failed"
24582 }
24583 run_test 405 "Various layout swap lock tests"
24584
24585 test_406() {
24586         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24587         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
24588         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
24589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24590         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
24591                 skip "Need MDS version at least 2.8.50"
24592
24593         local def_stripe_size=$($LFS getstripe -S $MOUNT)
24594         local test_pool=$TESTNAME
24595
24596         pool_add $test_pool || error "pool_add failed"
24597         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
24598                 error "pool_add_targets failed"
24599
24600         save_layout_restore_at_exit $MOUNT
24601
24602         # parent set default stripe count only, child will stripe from both
24603         # parent and fs default
24604         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
24605                 error "setstripe $MOUNT failed"
24606         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
24607         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
24608         for i in $(seq 10); do
24609                 local f=$DIR/$tdir/$tfile.$i
24610                 touch $f || error "touch failed"
24611                 local count=$($LFS getstripe -c $f)
24612                 [ $count -eq $OSTCOUNT ] ||
24613                         error "$f stripe count $count != $OSTCOUNT"
24614                 local offset=$($LFS getstripe -i $f)
24615                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
24616                 local size=$($LFS getstripe -S $f)
24617                 [ $size -eq $((def_stripe_size * 2)) ] ||
24618                         error "$f stripe size $size != $((def_stripe_size * 2))"
24619                 local pool=$($LFS getstripe -p $f)
24620                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
24621         done
24622
24623         # change fs default striping, delete parent default striping, now child
24624         # will stripe from new fs default striping only
24625         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
24626                 error "change $MOUNT default stripe failed"
24627         $LFS setstripe -c 0 $DIR/$tdir ||
24628                 error "delete $tdir default stripe failed"
24629         for i in $(seq 11 20); do
24630                 local f=$DIR/$tdir/$tfile.$i
24631                 touch $f || error "touch $f failed"
24632                 local count=$($LFS getstripe -c $f)
24633                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
24634                 local offset=$($LFS getstripe -i $f)
24635                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
24636                 local size=$($LFS getstripe -S $f)
24637                 [ $size -eq $def_stripe_size ] ||
24638                         error "$f stripe size $size != $def_stripe_size"
24639                 local pool=$($LFS getstripe -p $f)
24640                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
24641         done
24642
24643         unlinkmany $DIR/$tdir/$tfile. 1 20
24644
24645         local f=$DIR/$tdir/$tfile
24646         pool_remove_all_targets $test_pool $f
24647         pool_remove $test_pool $f
24648 }
24649 run_test 406 "DNE support fs default striping"
24650
24651 test_407() {
24652         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24653         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
24654                 skip "Need MDS version at least 2.8.55"
24655         remote_mds_nodsh && skip "remote MDS with nodsh"
24656
24657         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
24658                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
24659         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
24660                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
24661         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
24662
24663         #define OBD_FAIL_DT_TXN_STOP    0x2019
24664         for idx in $(seq $MDSCOUNT); do
24665                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
24666         done
24667         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
24668         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
24669                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
24670         true
24671 }
24672 run_test 407 "transaction fail should cause operation fail"
24673
24674 test_408() {
24675         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
24676
24677         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
24678         lctl set_param fail_loc=0x8000040a
24679         # let ll_prepare_partial_page() fail
24680         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
24681
24682         rm -f $DIR/$tfile
24683
24684         # create at least 100 unused inodes so that
24685         # shrink_icache_memory(0) should not return 0
24686         touch $DIR/$tfile-{0..100}
24687         rm -f $DIR/$tfile-{0..100}
24688         sync
24689
24690         echo 2 > /proc/sys/vm/drop_caches
24691 }
24692 run_test 408 "drop_caches should not hang due to page leaks"
24693
24694 test_409()
24695 {
24696         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
24697
24698         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
24699         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
24700         touch $DIR/$tdir/guard || error "(2) Fail to create"
24701
24702         local PREFIX=$(str_repeat 'A' 128)
24703         echo "Create 1K hard links start at $(date)"
24704         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
24705                 error "(3) Fail to hard link"
24706
24707         echo "Links count should be right although linkEA overflow"
24708         stat $DIR/$tdir/guard || error "(4) Fail to stat"
24709         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
24710         [ $linkcount -eq 1001 ] ||
24711                 error "(5) Unexpected hard links count: $linkcount"
24712
24713         echo "List all links start at $(date)"
24714         ls -l $DIR/$tdir/foo > /dev/null ||
24715                 error "(6) Fail to list $DIR/$tdir/foo"
24716
24717         echo "Unlink hard links start at $(date)"
24718         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
24719                 error "(7) Fail to unlink"
24720         echo "Unlink hard links finished at $(date)"
24721 }
24722 run_test 409 "Large amount of cross-MDTs hard links on the same file"
24723
24724 test_410()
24725 {
24726         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
24727                 skip "Need client version at least 2.9.59"
24728         [ -f $LUSTRE/tests/kernel/kinode.ko ] ||
24729                 skip "Need MODULES build"
24730
24731         # Create a file, and stat it from the kernel
24732         local testfile=$DIR/$tfile
24733         touch $testfile
24734
24735         local run_id=$RANDOM
24736         local my_ino=$(stat --format "%i" $testfile)
24737
24738         # Try to insert the module. This will always fail as the
24739         # module is designed to not be inserted.
24740         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
24741             &> /dev/null
24742
24743         # Anything but success is a test failure
24744         dmesg | grep -q \
24745             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
24746             error "no inode match"
24747 }
24748 run_test 410 "Test inode number returned from kernel thread"
24749
24750 cleanup_test411_cgroup() {
24751         trap 0
24752         rmdir "$1"
24753 }
24754
24755 test_411() {
24756         local cg_basedir=/sys/fs/cgroup/memory
24757         # LU-9966
24758         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
24759                 skip "no setup for cgroup"
24760
24761         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
24762                 error "test file creation failed"
24763         cancel_lru_locks osc
24764
24765         # Create a very small memory cgroup to force a slab allocation error
24766         local cgdir=$cg_basedir/osc_slab_alloc
24767         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
24768         trap "cleanup_test411_cgroup $cgdir" EXIT
24769         echo 2M > $cgdir/memory.kmem.limit_in_bytes
24770         echo 1M > $cgdir/memory.limit_in_bytes
24771
24772         # Should not LBUG, just be killed by oom-killer
24773         # dd will return 0 even allocation failure in some environment.
24774         # So don't check return value
24775         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
24776         cleanup_test411_cgroup $cgdir
24777
24778         return 0
24779 }
24780 run_test 411 "Slab allocation error with cgroup does not LBUG"
24781
24782 test_412() {
24783         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24784         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
24785                 skip "Need server version at least 2.10.55"
24786         fi
24787
24788         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
24789                 error "mkdir failed"
24790         $LFS getdirstripe $DIR/$tdir
24791         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
24792         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
24793                 error "expect $((MDSCOUT - 1)) get $stripe_index"
24794         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
24795         [ $stripe_count -eq 2 ] ||
24796                 error "expect 2 get $stripe_count"
24797 }
24798 run_test 412 "mkdir on specific MDTs"
24799
24800 generate_uneven_mdts() {
24801         local threshold=$1
24802         local ffree
24803         local bavail
24804         local max
24805         local min
24806         local max_index
24807         local min_index
24808         local tmp
24809         local i
24810
24811         echo
24812         echo "Check for uneven MDTs: "
24813
24814         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
24815         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
24816         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
24817
24818         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24819         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24820         max_index=0
24821         min_index=0
24822         for ((i = 1; i < ${#ffree[@]}; i++)); do
24823                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
24824                 if [ $tmp -gt $max ]; then
24825                         max=$tmp
24826                         max_index=$i
24827                 fi
24828                 if [ $tmp -lt $min ]; then
24829                         min=$tmp
24830                         min_index=$i
24831                 fi
24832         done
24833
24834         # Check if we need to generate uneven MDTs
24835         local diff=$(((max - min) * 100 / min))
24836         local testdir=$DIR/$tdir-fillmdt
24837
24838         mkdir -p $testdir
24839
24840         i=0
24841         while (( diff < threshold )); do
24842                 # generate uneven MDTs, create till $threshold% diff
24843                 echo -n "weight diff=$diff% must be > $threshold% ..."
24844                 echo "Fill MDT$min_index with 100 files: loop $i"
24845                 testdir=$DIR/$tdir-fillmdt/$i
24846                 [ -d $testdir ] || $LFS mkdir -i $min_index $testdir ||
24847                         error "mkdir $testdir failed"
24848                 $LFS setstripe -E 1M -L mdt $testdir ||
24849                         error "setstripe $testdir failed"
24850                 for F in f.{0..99}; do
24851                         dd if=/dev/zero of=$testdir/$F bs=1M count=1 > \
24852                                 /dev/null 2>&1 || error "dd $F failed"
24853                 done
24854
24855                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
24856                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
24857                 max=$(((${ffree[max_index]} >> 8) * \
24858                         (${bavail[max_index]} * bsize >> 16)))
24859                 min=$(((${ffree[min_index]} >> 8) * \
24860                         (${bavail[min_index]} * bsize >> 16)))
24861                 diff=$(((max - min) * 100 / min))
24862                 i=$((i + 1))
24863         done
24864
24865         echo "MDT filesfree available: ${ffree[@]}"
24866         echo "MDT blocks available: ${bavail[@]}"
24867         echo "weight diff=$diff%"
24868 }
24869
24870 test_qos_mkdir() {
24871         local mkdir_cmd=$1
24872         local stripe_count=$2
24873         local mdts=$(comma_list $(mdts_nodes))
24874
24875         local testdir
24876         local lmv_qos_prio_free
24877         local lmv_qos_threshold_rr
24878         local lmv_qos_maxage
24879         local lod_qos_prio_free
24880         local lod_qos_threshold_rr
24881         local lod_qos_maxage
24882         local count
24883         local i
24884
24885         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
24886         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
24887         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
24888                 head -n1)
24889         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
24890         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
24891         stack_trap "$LCTL set_param \
24892                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null"
24893         stack_trap "$LCTL set_param \
24894                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null"
24895         stack_trap "$LCTL set_param \
24896                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
24897
24898         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
24899                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
24900         lod_qos_prio_free=${lod_qos_prio_free%%%}
24901         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
24902                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
24903         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
24904         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
24905                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
24906         stack_trap "do_nodes $mdts $LCTL set_param \
24907                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null"
24908         stack_trap "do_nodes $mdts $LCTL set_param \
24909                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null"
24910         stack_trap "do_nodes $mdts $LCTL set_param \
24911                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null"
24912
24913         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
24914         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
24915
24916         testdir=$DIR/$tdir-s$stripe_count/rr
24917
24918         local stripe_index=$($LFS getstripe -m $testdir)
24919         local test_mkdir_rr=true
24920
24921         echo "dirstripe: '$($LFS getdirstripe $testdir)'"
24922         getfattr -d -m dmv -e hex $testdir | grep dmv
24923         if (( $? == 0 && $MDS1_VERSION >= $(version_code 2.14.51) )); then
24924                 echo "defstripe: '$($LFS getdirstripe -D $testdir)'"
24925                 (( $($LFS getdirstripe -D --max-inherit-rr $testdir) == 0 )) &&
24926                         test_mkdir_rr=false
24927         fi
24928
24929         echo
24930         $test_mkdir_rr &&
24931                 echo "Mkdir (stripe_count $stripe_count) roundrobin:" ||
24932                 echo "Mkdir (stripe_count $stripe_count) on stripe $stripe_index"
24933
24934         stack_trap "unlinkmany -d $testdir/subdir $((100 * MDSCOUNT))"
24935         for (( i = 0; i < 100 * MDSCOUNT; i++ )); do
24936                 eval $mkdir_cmd $testdir/subdir$i ||
24937                         error "$mkdir_cmd subdir$i failed"
24938         done
24939
24940         for (( i = 0; i < $MDSCOUNT; i++ )); do
24941                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
24942                 echo "$count directories created on MDT$i"
24943                 if $test_mkdir_rr; then
24944                         (( $count == 100 )) ||
24945                                 error "subdirs are not evenly distributed"
24946                 elif (( $i == $stripe_index )); then
24947                         (( $count == 100 * MDSCOUNT )) ||
24948                                 error "$count subdirs created on MDT$i"
24949                 else
24950                         (( $count == 0 )) ||
24951                                 error "$count subdirs created on MDT$i"
24952                 fi
24953
24954                 if $test_mkdir_rr && [ $stripe_count -gt 1 ]; then
24955                         count=$($LFS getdirstripe $testdir/* |
24956                                 grep -c -P "^\s+$i\t")
24957                         echo "$count stripes created on MDT$i"
24958                         # deviation should < 5% of average
24959                         (( $count >= 95 * stripe_count &&
24960                            $count <= 105 * stripe_count)) ||
24961                                 error "stripes are not evenly distributed"
24962                 fi
24963         done
24964
24965         echo
24966         echo "Check for uneven MDTs: "
24967
24968         local ffree
24969         local bavail
24970         local max
24971         local min
24972         local max_index
24973         local min_index
24974         local tmp
24975
24976         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
24977         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
24978         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
24979
24980         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24981         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24982         max_index=0
24983         min_index=0
24984         for ((i = 1; i < ${#ffree[@]}; i++)); do
24985                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
24986                 if [ $tmp -gt $max ]; then
24987                         max=$tmp
24988                         max_index=$i
24989                 fi
24990                 if [ $tmp -lt $min ]; then
24991                         min=$tmp
24992                         min_index=$i
24993                 fi
24994         done
24995
24996         (( ${ffree[min_index]} > 0 )) ||
24997                 skip "no free files in MDT$min_index"
24998         (( ${ffree[min_index]} < 100000000 )) ||
24999                 skip "too many free files in MDT$min_index"
25000
25001         echo "MDT filesfree available: ${ffree[@]}"
25002         echo "MDT blocks available: ${bavail[@]}"
25003         echo "weight diff=$(((max - min) * 100 / min))%"
25004         echo
25005         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
25006
25007         $LCTL set_param lmv.*.qos_threshold_rr=0 > /dev/null
25008         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
25009         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=0 > /dev/null
25010         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
25011         # decrease statfs age, so that it can be updated in time
25012         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
25013         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
25014
25015         sleep 1
25016
25017         testdir=$DIR/$tdir-s$stripe_count/qos
25018         local num=200
25019
25020         stack_trap "unlinkmany -d $testdir/subdir $((num * MDSCOUNT))"
25021         for (( i = 0; i < num * MDSCOUNT; i++ )); do
25022                 eval $mkdir_cmd $testdir/subdir$i ||
25023                         error "$mkdir_cmd subdir$i failed"
25024         done
25025
25026         for (( i = 0; i < $MDSCOUNT; i++ )); do
25027                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
25028                 echo "$count directories created on MDT$i"
25029
25030                 if [ $stripe_count -gt 1 ]; then
25031                         count=$($LFS getdirstripe $testdir/* |
25032                                 grep -c -P "^\s+$i\t")
25033                         echo "$count stripes created on MDT$i"
25034                 fi
25035         done
25036
25037         max=$($LFS getdirstripe -i $testdir/* | grep -c "^$max_index$")
25038         min=$($LFS getdirstripe -i $testdir/* | grep -c "^$min_index$")
25039
25040         # D-value should > 10% of averge
25041         (( max - min >= num / 10 )) ||
25042                 error "subdirs shouldn't be evenly distributed: $max - $min < $((num / 10))"
25043
25044         # 5% for stripes
25045         if (( stripe_count > 1 )); then
25046                 max=$($LFS getdirstripe $testdir/* |
25047                       grep -c -P "^\s+$max_index\t")
25048                 min=$($LFS getdirstripe $testdir/* |
25049                         grep -c -P "^\s+$min_index\t")
25050                 (( max - min >= num * stripe_count / 20 )) ||
25051                         error "stripes shouldn't be evenly distributed: $max - $min < $((num / 20)) * $stripe_count"
25052         fi
25053 }
25054
25055 most_full_mdt() {
25056         local ffree
25057         local bavail
25058         local bsize
25059         local min
25060         local min_index
25061         local tmp
25062
25063         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
25064         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
25065         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
25066
25067         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25068         min_index=0
25069         for ((i = 1; i < ${#ffree[@]}; i++)); do
25070                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
25071                 (( tmp < min )) && min=$tmp && min_index=$i
25072         done
25073
25074         echo -n $min_index
25075 }
25076
25077 test_413a() {
25078         [ $MDSCOUNT -lt 2 ] &&
25079                 skip "We need at least 2 MDTs for this test"
25080
25081         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
25082                 skip "Need server version at least 2.12.52"
25083
25084         local stripe_count
25085
25086         generate_uneven_mdts 100
25087         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
25088                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
25089                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
25090                 $LFS mkdir -i $(most_full_mdt) $DIR/$tdir-s$stripe_count/qos ||
25091                         error "mkdir failed"
25092                 test_qos_mkdir "$LFS mkdir -i -1 -c $stripe_count" $stripe_count
25093         done
25094 }
25095 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
25096
25097 test_413b() {
25098         [ $MDSCOUNT -lt 2 ] &&
25099                 skip "We need at least 2 MDTs for this test"
25100
25101         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
25102                 skip "Need server version at least 2.12.52"
25103
25104         local testdir
25105         local stripe_count
25106
25107         generate_uneven_mdts 100
25108         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
25109                 testdir=$DIR/$tdir-s$stripe_count
25110                 mkdir $testdir || error "mkdir $testdir failed"
25111                 mkdir $testdir/rr || error "mkdir rr failed"
25112                 $LFS mkdir -i $(most_full_mdt) $testdir/qos ||
25113                         error "mkdir qos failed"
25114                 $LFS setdirstripe -D -c $stripe_count --max-inherit-rr 2 \
25115                         $testdir/rr || error "setdirstripe rr failed"
25116                 $LFS setdirstripe -D -c $stripe_count $testdir/qos ||
25117                         error "setdirstripe failed"
25118                 test_qos_mkdir "mkdir" $stripe_count
25119         done
25120 }
25121 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
25122
25123 test_413c() {
25124         (( $MDSCOUNT >= 2 )) ||
25125                 skip "We need at least 2 MDTs for this test"
25126
25127         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
25128                 skip "Need server version at least 2.14.51"
25129
25130         local testdir
25131         local inherit
25132         local inherit_rr
25133
25134         testdir=$DIR/${tdir}-s1
25135         mkdir $testdir || error "mkdir $testdir failed"
25136         mkdir $testdir/rr || error "mkdir rr failed"
25137         $LFS mkdir -i $(most_full_mdt) $testdir/qos || error "mkdir qos failed"
25138         # default max_inherit is -1, default max_inherit_rr is 0
25139         $LFS setdirstripe -D -c 1 $testdir/rr ||
25140                 error "setdirstripe rr failed"
25141         $LFS setdirstripe -D -c 1 -i -1 -X 2 --max-inherit-rr 1 $testdir/qos ||
25142                 error "setdirstripe qos failed"
25143         test_qos_mkdir "mkdir" 1
25144
25145         mkdir $testdir/rr/level1 || error "mkdir rr/level1 failed"
25146         inherit=$($LFS getdirstripe -D -X $testdir/rr/level1)
25147         (( $inherit == -1 )) || error "rr/level1 inherit $inherit != -1"
25148         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/rr/level1)
25149         (( $inherit_rr == 0 )) || error "rr/level1 inherit-rr $inherit_rr != 0"
25150
25151         mkdir $testdir/qos/level1 || error "mkdir qos/level1 failed"
25152         inherit=$($LFS getdirstripe -D -X $testdir/qos/level1)
25153         (( $inherit == 1 )) || error "qos/level1 inherit $inherit != 1"
25154         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/qos/level1)
25155         (( $inherit_rr == 0 )) || error "qos/level1 inherit-rr $inherit_rr != 0"
25156         mkdir $testdir/qos/level1/level2 || error "mkdir level2 failed"
25157         getfattr -d -m dmv -e hex $testdir/qos/level1/level2 | grep dmv &&
25158                 error "level2 shouldn't have default LMV" || true
25159 }
25160 run_test 413c "mkdir with default LMV max inherit rr"
25161
25162 test_413d() {
25163         (( MDSCOUNT >= 2 )) ||
25164                 skip "We need at least 2 MDTs for this test"
25165
25166         (( MDS1_VERSION >= $(version_code 2.14.51) )) ||
25167                 skip "Need server version at least 2.14.51"
25168
25169         local lmv_qos_threshold_rr
25170
25171         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
25172                 head -n1)
25173         stack_trap "$LCTL set_param \
25174                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
25175
25176         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
25177         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
25178         getfattr -d -m dmv -e hex $DIR/$tdir | grep dmv &&
25179                 error "$tdir shouldn't have default LMV"
25180         createmany -d $DIR/$tdir/sub $((100 * MDSCOUNT)) ||
25181                 error "mkdir sub failed"
25182
25183         local count=$($LFS getstripe -m $DIR/$tdir/* | grep -c ^0)
25184
25185         (( count == 100 )) || error "$count subdirs on MDT0"
25186 }
25187 run_test 413d "inherit ROOT default LMV"
25188
25189 test_413z() {
25190         local pids=""
25191         local subdir
25192         local pid
25193
25194         for subdir in $(\ls -1 -d $DIR/d413*-fillmdt/*); do
25195                 unlinkmany $subdir/f. 100 &
25196                 pids="$pids $!"
25197         done
25198
25199         for pid in $pids; do
25200                 wait $pid
25201         done
25202 }
25203 run_test 413z "413 test cleanup"
25204
25205 test_414() {
25206 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
25207         $LCTL set_param fail_loc=0x80000521
25208         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
25209         rm -f $DIR/$tfile
25210 }
25211 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
25212
25213 test_415() {
25214         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25215         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
25216                 skip "Need server version at least 2.11.52"
25217
25218         # LU-11102
25219         local total
25220         local setattr_pid
25221         local start_time
25222         local end_time
25223         local duration
25224
25225         total=500
25226         # this test may be slow on ZFS
25227         [ "$mds1_FSTYPE" == "zfs" ] && total=100
25228
25229         # though this test is designed for striped directory, let's test normal
25230         # directory too since lock is always saved as CoS lock.
25231         test_mkdir $DIR/$tdir || error "mkdir $tdir"
25232         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
25233
25234         (
25235                 while true; do
25236                         touch $DIR/$tdir
25237                 done
25238         ) &
25239         setattr_pid=$!
25240
25241         start_time=$(date +%s)
25242         for i in $(seq $total); do
25243                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
25244                         > /dev/null
25245         done
25246         end_time=$(date +%s)
25247         duration=$((end_time - start_time))
25248
25249         kill -9 $setattr_pid
25250
25251         echo "rename $total files took $duration sec"
25252         [ $duration -lt 100 ] || error "rename took $duration sec"
25253 }
25254 run_test 415 "lock revoke is not missing"
25255
25256 test_416() {
25257         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
25258                 skip "Need server version at least 2.11.55"
25259
25260         # define OBD_FAIL_OSD_TXN_START    0x19a
25261         do_facet mds1 lctl set_param fail_loc=0x19a
25262
25263         lfs mkdir -c $MDSCOUNT $DIR/$tdir
25264
25265         true
25266 }
25267 run_test 416 "transaction start failure won't cause system hung"
25268
25269 cleanup_417() {
25270         trap 0
25271         do_nodes $(comma_list $(mdts_nodes)) \
25272                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
25273         do_nodes $(comma_list $(mdts_nodes)) \
25274                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
25275         do_nodes $(comma_list $(mdts_nodes)) \
25276                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
25277 }
25278
25279 test_417() {
25280         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25281         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
25282                 skip "Need MDS version at least 2.11.56"
25283
25284         trap cleanup_417 RETURN EXIT
25285
25286         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
25287         do_nodes $(comma_list $(mdts_nodes)) \
25288                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
25289         $LFS migrate -m 0 $DIR/$tdir.1 &&
25290                 error "migrate dir $tdir.1 should fail"
25291
25292         do_nodes $(comma_list $(mdts_nodes)) \
25293                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
25294         $LFS mkdir -i 1 $DIR/$tdir.2 &&
25295                 error "create remote dir $tdir.2 should fail"
25296
25297         do_nodes $(comma_list $(mdts_nodes)) \
25298                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
25299         $LFS mkdir -c 2 $DIR/$tdir.3 &&
25300                 error "create striped dir $tdir.3 should fail"
25301         true
25302 }
25303 run_test 417 "disable remote dir, striped dir and dir migration"
25304
25305 # Checks that the outputs of df [-i] and lfs df [-i] match
25306 #
25307 # usage: check_lfs_df <blocks | inodes> <mountpoint>
25308 check_lfs_df() {
25309         local dir=$2
25310         local inodes
25311         local df_out
25312         local lfs_df_out
25313         local count
25314         local passed=false
25315
25316         # blocks or inodes
25317         [ "$1" == "blocks" ] && inodes= || inodes="-i"
25318
25319         for count in {1..100}; do
25320                 do_rpc_nodes "$CLIENTS" cancel_lru_locks
25321                 sync; sleep 0.2
25322
25323                 # read the lines of interest
25324                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
25325                         error "df $inodes $dir | tail -n +2 failed"
25326                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
25327                         error "lfs df $inodes $dir | grep summary: failed"
25328
25329                 # skip first substrings of each output as they are different
25330                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
25331                 # compare the two outputs
25332                 passed=true
25333                 for i in {1..5}; do
25334                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
25335                 done
25336                 $passed && break
25337         done
25338
25339         if ! $passed; then
25340                 df -P $inodes $dir
25341                 echo
25342                 lfs df $inodes $dir
25343                 error "df and lfs df $1 output mismatch: "      \
25344                       "df ${inodes}: ${df_out[*]}, "            \
25345                       "lfs df ${inodes}: ${lfs_df_out[*]}"
25346         fi
25347 }
25348
25349 test_418() {
25350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25351
25352         local dir=$DIR/$tdir
25353         local numfiles=$((RANDOM % 4096 + 2))
25354         local numblocks=$((RANDOM % 256 + 1))
25355
25356         wait_delete_completed
25357         test_mkdir $dir
25358
25359         # check block output
25360         check_lfs_df blocks $dir
25361         # check inode output
25362         check_lfs_df inodes $dir
25363
25364         # create a single file and retest
25365         echo "Creating a single file and testing"
25366         createmany -o $dir/$tfile- 1 &>/dev/null ||
25367                 error "creating 1 file in $dir failed"
25368         check_lfs_df blocks $dir
25369         check_lfs_df inodes $dir
25370
25371         # create a random number of files
25372         echo "Creating $((numfiles - 1)) files and testing"
25373         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
25374                 error "creating $((numfiles - 1)) files in $dir failed"
25375
25376         # write a random number of blocks to the first test file
25377         echo "Writing $numblocks 4K blocks and testing"
25378         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
25379                 count=$numblocks &>/dev/null ||
25380                 error "dd to $dir/${tfile}-0 failed"
25381
25382         # retest
25383         check_lfs_df blocks $dir
25384         check_lfs_df inodes $dir
25385
25386         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
25387                 error "unlinking $numfiles files in $dir failed"
25388 }
25389 run_test 418 "df and lfs df outputs match"
25390
25391 test_419()
25392 {
25393         local dir=$DIR/$tdir
25394
25395         mkdir -p $dir
25396         touch $dir/file
25397
25398         cancel_lru_locks mdc
25399
25400         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
25401         $LCTL set_param fail_loc=0x1410
25402         cat $dir/file
25403         $LCTL set_param fail_loc=0
25404         rm -rf $dir
25405 }
25406 run_test 419 "Verify open file by name doesn't crash kernel"
25407
25408 test_420()
25409 {
25410         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
25411                 skip "Need MDS version at least 2.12.53"
25412
25413         local SAVE_UMASK=$(umask)
25414         local dir=$DIR/$tdir
25415         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
25416
25417         mkdir -p $dir
25418         umask 0000
25419         mkdir -m03777 $dir/testdir
25420         ls -dn $dir/testdir
25421         # Need to remove trailing '.' when SELinux is enabled
25422         local dirperms=$(ls -dn $dir/testdir |
25423                          awk '{ sub(/\.$/, "", $1); print $1}')
25424         [ $dirperms == "drwxrwsrwt" ] ||
25425                 error "incorrect perms on $dir/testdir"
25426
25427         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
25428                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
25429         ls -n $dir/testdir/testfile
25430         local fileperms=$(ls -n $dir/testdir/testfile |
25431                           awk '{ sub(/\.$/, "", $1); print $1}')
25432         [ $fileperms == "-rwxr-xr-x" ] ||
25433                 error "incorrect perms on $dir/testdir/testfile"
25434
25435         umask $SAVE_UMASK
25436 }
25437 run_test 420 "clear SGID bit on non-directories for non-members"
25438
25439 test_421a() {
25440         local cnt
25441         local fid1
25442         local fid2
25443
25444         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25445                 skip "Need MDS version at least 2.12.54"
25446
25447         test_mkdir $DIR/$tdir
25448         createmany -o $DIR/$tdir/f 3
25449         cnt=$(ls -1 $DIR/$tdir | wc -l)
25450         [ $cnt != 3 ] && error "unexpected #files: $cnt"
25451
25452         fid1=$(lfs path2fid $DIR/$tdir/f1)
25453         fid2=$(lfs path2fid $DIR/$tdir/f2)
25454         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
25455
25456         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
25457         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
25458
25459         cnt=$(ls -1 $DIR/$tdir | wc -l)
25460         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
25461
25462         rm -f $DIR/$tdir/f3 || error "can't remove f3"
25463         createmany -o $DIR/$tdir/f 3
25464         cnt=$(ls -1 $DIR/$tdir | wc -l)
25465         [ $cnt != 3 ] && error "unexpected #files: $cnt"
25466
25467         fid1=$(lfs path2fid $DIR/$tdir/f1)
25468         fid2=$(lfs path2fid $DIR/$tdir/f2)
25469         echo "remove using fsname $FSNAME"
25470         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
25471
25472         cnt=$(ls -1 $DIR/$tdir | wc -l)
25473         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
25474 }
25475 run_test 421a "simple rm by fid"
25476
25477 test_421b() {
25478         local cnt
25479         local FID1
25480         local FID2
25481
25482         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25483                 skip "Need MDS version at least 2.12.54"
25484
25485         test_mkdir $DIR/$tdir
25486         createmany -o $DIR/$tdir/f 3
25487         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
25488         MULTIPID=$!
25489
25490         FID1=$(lfs path2fid $DIR/$tdir/f1)
25491         FID2=$(lfs path2fid $DIR/$tdir/f2)
25492         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
25493
25494         kill -USR1 $MULTIPID
25495         wait
25496
25497         cnt=$(ls $DIR/$tdir | wc -l)
25498         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
25499 }
25500 run_test 421b "rm by fid on open file"
25501
25502 test_421c() {
25503         local cnt
25504         local FIDS
25505
25506         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25507                 skip "Need MDS version at least 2.12.54"
25508
25509         test_mkdir $DIR/$tdir
25510         createmany -o $DIR/$tdir/f 3
25511         touch $DIR/$tdir/$tfile
25512         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
25513         cnt=$(ls -1 $DIR/$tdir | wc -l)
25514         [ $cnt != 184 ] && error "unexpected #files: $cnt"
25515
25516         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
25517         $LFS rmfid $DIR $FID1 || error "rmfid failed"
25518
25519         cnt=$(ls $DIR/$tdir | wc -l)
25520         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
25521 }
25522 run_test 421c "rm by fid against hardlinked files"
25523
25524 test_421d() {
25525         local cnt
25526         local FIDS
25527
25528         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25529                 skip "Need MDS version at least 2.12.54"
25530
25531         test_mkdir $DIR/$tdir
25532         createmany -o $DIR/$tdir/f 4097
25533         cnt=$(ls -1 $DIR/$tdir | wc -l)
25534         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
25535
25536         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
25537         $LFS rmfid $DIR $FIDS || error "rmfid failed"
25538
25539         cnt=$(ls $DIR/$tdir | wc -l)
25540         rm -rf $DIR/$tdir
25541         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25542 }
25543 run_test 421d "rmfid en masse"
25544
25545 test_421e() {
25546         local cnt
25547         local FID
25548
25549         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25550         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25551                 skip "Need MDS version at least 2.12.54"
25552
25553         mkdir -p $DIR/$tdir
25554         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
25555         createmany -o $DIR/$tdir/striped_dir/f 512
25556         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25557         [ $cnt != 512 ] && error "unexpected #files: $cnt"
25558
25559         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
25560                 sed "s/[/][^:]*://g")
25561         $LFS rmfid $DIR $FIDS || error "rmfid failed"
25562
25563         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
25564         rm -rf $DIR/$tdir
25565         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25566 }
25567 run_test 421e "rmfid in DNE"
25568
25569 test_421f() {
25570         local cnt
25571         local FID
25572
25573         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25574                 skip "Need MDS version at least 2.12.54"
25575
25576         test_mkdir $DIR/$tdir
25577         touch $DIR/$tdir/f
25578         cnt=$(ls -1 $DIR/$tdir | wc -l)
25579         [ $cnt != 1 ] && error "unexpected #files: $cnt"
25580
25581         FID=$(lfs path2fid $DIR/$tdir/f)
25582         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
25583         # rmfid should fail
25584         cnt=$(ls -1 $DIR/$tdir | wc -l)
25585         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
25586
25587         chmod a+rw $DIR/$tdir
25588         ls -la $DIR/$tdir
25589         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
25590         # rmfid should fail
25591         cnt=$(ls -1 $DIR/$tdir | wc -l)
25592         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
25593
25594         rm -f $DIR/$tdir/f
25595         $RUNAS touch $DIR/$tdir/f
25596         FID=$(lfs path2fid $DIR/$tdir/f)
25597         echo "rmfid as root"
25598         $LFS rmfid $DIR $FID || error "rmfid as root failed"
25599         cnt=$(ls -1 $DIR/$tdir | wc -l)
25600         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
25601
25602         rm -f $DIR/$tdir/f
25603         $RUNAS touch $DIR/$tdir/f
25604         cnt=$(ls -1 $DIR/$tdir | wc -l)
25605         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
25606         FID=$(lfs path2fid $DIR/$tdir/f)
25607         # rmfid w/o user_fid2path mount option should fail
25608         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
25609         cnt=$(ls -1 $DIR/$tdir | wc -l)
25610         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
25611
25612         tmpdir=$(mktemp -d /tmp/lustre-XXXXXX)
25613         stack_trap "rmdir $tmpdir"
25614         mount_client $tmpdir "$MOUNT_OPTS,user_fid2path" ||
25615                 error "failed to mount client'"
25616         stack_trap "umount_client $tmpdir"
25617
25618         $RUNAS $LFS rmfid $tmpdir $FID || error "rmfid failed"
25619         # rmfid should succeed
25620         cnt=$(ls -1 $tmpdir/$tdir | wc -l)
25621         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
25622
25623         # rmfid shouldn't allow to remove files due to dir's permission
25624         chmod a+rwx $tmpdir/$tdir
25625         touch $tmpdir/$tdir/f
25626         ls -la $tmpdir/$tdir
25627         FID=$(lfs path2fid $tmpdir/$tdir/f)
25628         $RUNAS $LFS rmfid $tmpdir $FID && error "rmfid didn't fail"
25629         return 0
25630 }
25631 run_test 421f "rmfid checks permissions"
25632
25633 test_421g() {
25634         local cnt
25635         local FIDS
25636
25637         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25638         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25639                 skip "Need MDS version at least 2.12.54"
25640
25641         mkdir -p $DIR/$tdir
25642         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
25643         createmany -o $DIR/$tdir/striped_dir/f 512
25644         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25645         [ $cnt != 512 ] && error "unexpected #files: $cnt"
25646
25647         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
25648                 sed "s/[/][^:]*://g")
25649
25650         rm -f $DIR/$tdir/striped_dir/f1*
25651         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25652         removed=$((512 - cnt))
25653
25654         # few files have been just removed, so we expect
25655         # rmfid to fail on their fids
25656         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
25657         [ $removed != $errors ] && error "$errors != $removed"
25658
25659         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
25660         rm -rf $DIR/$tdir
25661         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25662 }
25663 run_test 421g "rmfid to return errors properly"
25664
25665 test_422() {
25666         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
25667         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
25668         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
25669         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
25670         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
25671
25672         local amc=$(at_max_get client)
25673         local amo=$(at_max_get mds1)
25674         local timeout=`lctl get_param -n timeout`
25675
25676         at_max_set 0 client
25677         at_max_set 0 mds1
25678
25679 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
25680         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
25681                         fail_val=$(((2*timeout + 10)*1000))
25682         touch $DIR/$tdir/d3/file &
25683         sleep 2
25684 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
25685         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
25686                         fail_val=$((2*timeout + 5))
25687         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
25688         local pid=$!
25689         sleep 1
25690         kill -9 $pid
25691         sleep $((2 * timeout))
25692         echo kill $pid
25693         kill -9 $pid
25694         lctl mark touch
25695         touch $DIR/$tdir/d2/file3
25696         touch $DIR/$tdir/d2/file4
25697         touch $DIR/$tdir/d2/file5
25698
25699         wait
25700         at_max_set $amc client
25701         at_max_set $amo mds1
25702
25703         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
25704         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
25705                 error "Watchdog is always throttled"
25706 }
25707 run_test 422 "kill a process with RPC in progress"
25708
25709 stat_test() {
25710     df -h $MOUNT &
25711     df -h $MOUNT &
25712     df -h $MOUNT &
25713     df -h $MOUNT &
25714     df -h $MOUNT &
25715     df -h $MOUNT &
25716 }
25717
25718 test_423() {
25719     local _stats
25720     # ensure statfs cache is expired
25721     sleep 2;
25722
25723     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
25724     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
25725
25726     return 0
25727 }
25728 run_test 423 "statfs should return a right data"
25729
25730 test_424() {
25731 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
25732         $LCTL set_param fail_loc=0x80000522
25733         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
25734         rm -f $DIR/$tfile
25735 }
25736 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
25737
25738 test_425() {
25739         test_mkdir -c -1 $DIR/$tdir
25740         $LFS setstripe -c -1 $DIR/$tdir
25741
25742         lru_resize_disable "" 100
25743         stack_trap "lru_resize_enable" EXIT
25744
25745         sleep 5
25746
25747         for i in $(seq $((MDSCOUNT * 125))); do
25748                 local t=$DIR/$tdir/$tfile_$i
25749
25750                 dd if=/dev/zero of=$t bs=4K count=1 > /dev/null 2>&1 ||
25751                         error_noexit "Create file $t"
25752         done
25753         stack_trap "rm -rf $DIR/$tdir" EXIT
25754
25755         for oscparam in $($LCTL list_param ldlm.namespaces.*osc-[-0-9a-f]*); do
25756                 local lru_size=$($LCTL get_param -n $oscparam.lru_size)
25757                 local lock_count=$($LCTL get_param -n $oscparam.lock_count)
25758
25759                 [ $lock_count -le $lru_size ] ||
25760                         error "osc lock count $lock_count > lru size $lru_size"
25761         done
25762
25763         for mdcparam in $($LCTL list_param ldlm.namespaces.*mdc-*); do
25764                 local lru_size=$($LCTL get_param -n $mdcparam.lru_size)
25765                 local lock_count=$($LCTL get_param -n $mdcparam.lock_count)
25766
25767                 [ $lock_count -le $lru_size ] ||
25768                         error "mdc lock count $lock_count > lru size $lru_size"
25769         done
25770 }
25771 run_test 425 "lock count should not exceed lru size"
25772
25773 test_426() {
25774         splice-test -r $DIR/$tfile
25775         splice-test -rd $DIR/$tfile
25776         splice-test $DIR/$tfile
25777         splice-test -d $DIR/$tfile
25778 }
25779 run_test 426 "splice test on Lustre"
25780
25781 test_427() {
25782         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
25783         (( $MDS1_VERSION >= $(version_code 2.12.4) )) ||
25784                 skip "Need MDS version at least 2.12.4"
25785         local log
25786
25787         mkdir $DIR/$tdir
25788         mkdir $DIR/$tdir/1
25789         mkdir $DIR/$tdir/2
25790         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/1/dir
25791         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/2/dir2
25792
25793         $LFS getdirstripe $DIR/$tdir/1/dir
25794
25795         #first setfattr for creating updatelog
25796         setfattr -n user.attr0 -v "some text" $DIR/$tdir/1/dir
25797
25798 #define OBD_FAIL_OUT_OBJECT_MISS        0x1708
25799         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x80001708
25800         setfattr -n user.attr1 -v "some text" $DIR/$tdir/1/dir &
25801         setfattr -n user.attr2 -v "another attr"  $DIR/$tdir/2/dir2 &
25802
25803         sleep 2
25804         fail mds2
25805         wait_recovery_complete mds2 $((2*TIMEOUT))
25806
25807         log=$(do_facet mds1 dmesg | tac | sed "/${TESTNAME//_/ }/,$ d")
25808         echo $log | grep "get update log failed" &&
25809                 error "update log corruption is detected" || true
25810 }
25811 run_test 427 "Failed DNE2 update request shouldn't corrupt updatelog"
25812
25813 test_428() {
25814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25815         local cache_limit=$CACHE_MAX
25816
25817         stack_trap "$LCTL set_param -n llite.*.max_cached_mb=$cache_limit"
25818         $LCTL set_param -n llite.*.max_cached_mb=64
25819
25820         mkdir $DIR/$tdir
25821         $LFS setstripe -c 1 $DIR/$tdir
25822         eval touch $DIR/$tdir/$tfile.{1..$OSTCOUNT}
25823         stack_trap "rm -f $DIR/$tdir/$tfile.*"
25824         #test write
25825         for f in $(seq 4); do
25826                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$f bs=128M count=1 &
25827         done
25828         wait
25829
25830         cancel_lru_locks osc
25831         # Test read
25832         for f in $(seq 4); do
25833                 dd if=$DIR/$tdir/$tfile.$f of=/dev/null bs=128M count=1 &
25834         done
25835         wait
25836 }
25837 run_test 428 "large block size IO should not hang"
25838
25839 test_429() { # LU-7915 / LU-10948
25840         local ll_opencache_threshold_count="llite.*.opencache_threshold_count"
25841         local testfile=$DIR/$tfile
25842         local mdc_rpcstats="mdc.$FSNAME-MDT0000-*.stats"
25843         local new_flag=1
25844         local first_rpc
25845         local second_rpc
25846         local third_rpc
25847
25848         $LCTL get_param $ll_opencache_threshold_count ||
25849                 skip "client does not have opencache parameter"
25850
25851         set_opencache $new_flag
25852         stack_trap "restore_opencache"
25853         [ $($LCTL get_param -n $ll_opencache_threshold_count) == $new_flag ] ||
25854                 error "enable opencache failed"
25855         touch $testfile
25856         # drop MDC DLM locks
25857         cancel_lru_locks mdc
25858         # clear MDC RPC stats counters
25859         $LCTL set_param $mdc_rpcstats=clear
25860
25861         # According to the current implementation, we need to run 3 times
25862         # open & close file to verify if opencache is enabled correctly.
25863         # 1st, RPCs are sent for lookup/open and open handle is released on
25864         #      close finally.
25865         # 2nd, RPC is sent for open, MDS_OPEN_LOCK is fetched automatically,
25866         #      so open handle won't be released thereafter.
25867         # 3rd, No RPC is sent out.
25868         $MULTIOP $testfile oc || error "multiop failed"
25869         first_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25870         echo "1st: $first_rpc RPCs in flight"
25871
25872         $MULTIOP $testfile oc || error "multiop failed"
25873         second_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25874         echo "2nd: $second_rpc RPCs in flight"
25875
25876         $MULTIOP $testfile oc || error "multiop failed"
25877         third_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25878         echo "3rd: $third_rpc RPCs in flight"
25879
25880         #verify no MDC RPC is sent
25881         [[ $second_rpc == $third_rpc ]] || error "MDC RPC is still sent"
25882 }
25883 run_test 429 "verify if opencache flag on client side does work"
25884
25885 lseek_test_430() {
25886         local offset
25887         local file=$1
25888
25889         # data at [200K, 400K)
25890         dd if=/dev/urandom of=$file bs=256K count=1 seek=1 ||
25891                 error "256K->512K dd fails"
25892         # data at [2M, 3M)
25893         dd if=/dev/urandom of=$file bs=1M count=1 seek=2 ||
25894                 error "2M->3M dd fails"
25895         # data at [4M, 5M)
25896         dd if=/dev/urandom of=$file bs=1M count=1 seek=4 ||
25897                 error "4M->5M dd fails"
25898         echo "Data at 256K...512K, 2M...3M and 4M...5M"
25899         # start at first component hole #1
25900         printf "Seeking hole from 1000 ... "
25901         offset=$(lseek_test -l 1000 $file)
25902         echo $offset
25903         [[ $offset == 1000 ]] || error "offset $offset != 1000"
25904         printf "Seeking data from 1000 ... "
25905         offset=$(lseek_test -d 1000 $file)
25906         echo $offset
25907         [[ $offset == 262144 ]] || error "offset $offset != 262144"
25908
25909         # start at first component data block
25910         printf "Seeking hole from 300000 ... "
25911         offset=$(lseek_test -l 300000 $file)
25912         echo $offset
25913         [[ $offset == 524288 ]] || error "offset $offset != 524288"
25914         printf "Seeking data from 300000 ... "
25915         offset=$(lseek_test -d 300000 $file)
25916         echo $offset
25917         [[ $offset == 300000 ]] || error "offset $offset != 300000"
25918
25919         # start at the first component but beyond end of object size
25920         printf "Seeking hole from 1000000 ... "
25921         offset=$(lseek_test -l 1000000 $file)
25922         echo $offset
25923         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
25924         printf "Seeking data from 1000000 ... "
25925         offset=$(lseek_test -d 1000000 $file)
25926         echo $offset
25927         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
25928
25929         # start at second component stripe 2 (empty file)
25930         printf "Seeking hole from 1500000 ... "
25931         offset=$(lseek_test -l 1500000 $file)
25932         echo $offset
25933         [[ $offset == 1500000 ]] || error "offset $offset != 1500000"
25934         printf "Seeking data from 1500000 ... "
25935         offset=$(lseek_test -d 1500000 $file)
25936         echo $offset
25937         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
25938
25939         # start at second component stripe 1 (all data)
25940         printf "Seeking hole from 3000000 ... "
25941         offset=$(lseek_test -l 3000000 $file)
25942         echo $offset
25943         [[ $offset == 3145728 ]] || error "offset $offset != 3145728"
25944         printf "Seeking data from 3000000 ... "
25945         offset=$(lseek_test -d 3000000 $file)
25946         echo $offset
25947         [[ $offset == 3000000 ]] || error "offset $offset != 3000000"
25948
25949         dd if=/dev/urandom of=$file bs=640K count=1 seek=1 ||
25950                 error "2nd dd fails"
25951         echo "Add data block at 640K...1280K"
25952
25953         # start at before new data block, in hole
25954         printf "Seeking hole from 600000 ... "
25955         offset=$(lseek_test -l 600000 $file)
25956         echo $offset
25957         [[ $offset == 600000 ]] || error "offset $offset != 600000"
25958         printf "Seeking data from 600000 ... "
25959         offset=$(lseek_test -d 600000 $file)
25960         echo $offset
25961         [[ $offset == 655360 ]] || error "offset $offset != 655360"
25962
25963         # start at the first component new data block
25964         printf "Seeking hole from 1000000 ... "
25965         offset=$(lseek_test -l 1000000 $file)
25966         echo $offset
25967         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
25968         printf "Seeking data from 1000000 ... "
25969         offset=$(lseek_test -d 1000000 $file)
25970         echo $offset
25971         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
25972
25973         # start at second component stripe 2, new data
25974         printf "Seeking hole from 1200000 ... "
25975         offset=$(lseek_test -l 1200000 $file)
25976         echo $offset
25977         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
25978         printf "Seeking data from 1200000 ... "
25979         offset=$(lseek_test -d 1200000 $file)
25980         echo $offset
25981         [[ $offset == 1200000 ]] || error "offset $offset != 1200000"
25982
25983         # start beyond file end
25984         printf "Using offset > filesize ... "
25985         lseek_test -l 4000000 $file && error "lseek should fail"
25986         printf "Using offset > filesize ... "
25987         lseek_test -d 4000000 $file && error "lseek should fail"
25988
25989         printf "Done\n\n"
25990 }
25991
25992 test_430a() {
25993         $LCTL get_param mdc.*.import | grep -q 'connect_flags:.*seek' ||
25994                 skip "MDT does not support SEEK_HOLE"
25995
25996         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
25997                 skip "OST does not support SEEK_HOLE"
25998
25999         local file=$DIR/$tdir/$tfile
26000
26001         mkdir -p $DIR/$tdir
26002
26003         $LFS setstripe -E 1M -L mdt -E eof -c2 $file
26004         # OST stripe #1 will have continuous data at [1M, 3M)
26005         # OST stripe #2 is empty
26006         echo "Component #1: 1M DoM, component #2: EOF, 2 stripes 1M"
26007         lseek_test_430 $file
26008         rm $file
26009         $LFS setstripe -E 1M -c2 -S 64K -E 10M -c2 -S 1M $file
26010         echo "Component #1: 1M, 2 stripes 64K, component #2: EOF, 2 stripes 1M"
26011         lseek_test_430 $file
26012         rm $file
26013         $LFS setstripe -c2 -S 512K $file
26014         echo "Two stripes, stripe size 512K"
26015         lseek_test_430 $file
26016         rm $file
26017         # FLR with stale mirror
26018         $LFS setstripe -N -E 512K -c1 -S 64K -E eof -c2 -S 512K \
26019                        -N -c2 -S 1M $file
26020         echo "Mirrored file:"
26021         echo "Component #1: 512K, stripe 64K, component #2: EOF, 2 stripes 512K"
26022         echo "Plain 2 stripes 1M"
26023         lseek_test_430 $file
26024         rm $file
26025 }
26026 run_test 430a "lseek: SEEK_DATA/SEEK_HOLE basic functionality"
26027
26028 test_430b() {
26029         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
26030                 skip "OST does not support SEEK_HOLE"
26031
26032         local offset
26033         local file=$DIR/$tdir/$tfile
26034
26035         mkdir -p $DIR/$tdir
26036         # Empty layout lseek should fail
26037         $MCREATE $file
26038         # seek from 0
26039         printf "Seeking hole from 0 ... "
26040         lseek_test -l 0 $file && error "lseek should fail"
26041         printf "Seeking data from 0 ... "
26042         lseek_test -d 0 $file && error "lseek should fail"
26043         rm $file
26044
26045         # 1M-hole file
26046         $LFS setstripe -E 1M -c2 -E eof $file
26047         $TRUNCATE $file 1048576
26048         printf "Seeking hole from 1000000 ... "
26049         offset=$(lseek_test -l 1000000 $file)
26050         echo $offset
26051         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
26052         printf "Seeking data from 1000000 ... "
26053         lseek_test -d 1000000 $file && error "lseek should fail"
26054         rm $file
26055
26056         # full component followed by non-inited one
26057         $LFS setstripe -E 1M -c2 -E eof $file
26058         dd if=/dev/urandom of=$file bs=1M count=1
26059         printf "Seeking hole from 1000000 ... "
26060         offset=$(lseek_test -l 1000000 $file)
26061         echo $offset
26062         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
26063         printf "Seeking hole from 1048576 ... "
26064         lseek_test -l 1048576 $file && error "lseek should fail"
26065         # init second component and truncate back
26066         echo "123" >> $file
26067         $TRUNCATE $file 1048576
26068         printf "Seeking hole from 1000000 ... "
26069         offset=$(lseek_test -l 1000000 $file)
26070         echo $offset
26071         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
26072         printf "Seeking hole from 1048576 ... "
26073         lseek_test -l 1048576 $file && error "lseek should fail"
26074         # boundary checks for big values
26075         dd if=/dev/urandom of=$file.10g bs=1 count=1 seek=10G
26076         offset=$(lseek_test -d 0 $file.10g)
26077         [[ $offset == 10737418240 ]] || error "offset $offset != 10737418240"
26078         dd if=/dev/urandom of=$file.100g bs=1 count=1 seek=100G
26079         offset=$(lseek_test -d 0 $file.100g)
26080         [[ $offset == 107374182400 ]] || error "offset $offset != 107374182400"
26081         return 0
26082 }
26083 run_test 430b "lseek: SEEK_DATA/SEEK_HOLE special cases"
26084
26085 test_430c() {
26086         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
26087                 skip "OST does not support SEEK_HOLE"
26088
26089         local file=$DIR/$tdir/$tfile
26090         local start
26091
26092         mkdir -p $DIR/$tdir
26093         dd if=/dev/urandom of=$file bs=1k count=1 seek=5M
26094
26095         # cp version 8.33+ prefers lseek over fiemap
26096         if [[ $(cp --version | head -n1 | sed "s/[^0-9]//g") -ge 833 ]]; then
26097                 start=$SECONDS
26098                 time cp $file /dev/null
26099                 (( SECONDS - start < 5 )) ||
26100                         error "cp: too long runtime $((SECONDS - start))"
26101
26102         fi
26103         # tar version 1.29+ supports SEEK_HOLE/DATA
26104         if [[ $(tar --version | head -n1 | sed "s/[^0-9]//g") -ge 129 ]]; then
26105                 start=$SECONDS
26106                 time tar cS $file - | cat > /dev/null
26107                 (( SECONDS - start < 5 )) ||
26108                         error "tar: too long runtime $((SECONDS - start))"
26109         fi
26110 }
26111 run_test 430c "lseek: external tools check"
26112
26113 test_431() { # LU-14187
26114         local file=$DIR/$tdir/$tfile
26115
26116         mkdir -p $DIR/$tdir
26117         $LFS setstripe -c 1 -i 0 $file || error "lfs setstripe failed"
26118         dd if=/dev/urandom of=$file bs=4k count=1
26119         dd if=/dev/urandom of=$file bs=4k count=1 seek=10 conv=notrunc
26120         dd if=/dev/urandom of=$file bs=4k count=1 seek=12 conv=notrunc
26121         #define OBD_FAIL_OST_RESTART_IO 0x251
26122         do_facet ost1 "$LCTL set_param fail_loc=0x251"
26123         $LFS setstripe -c 1 -i 0 $file.0 || error "lfs setstripe failed"
26124         cp $file $file.0
26125         cancel_lru_locks
26126         sync_all_data
26127         echo 3 > /proc/sys/vm/drop_caches
26128         diff  $file $file.0 || error "data diff"
26129 }
26130 run_test 431 "Restart transaction for IO"
26131
26132 cleanup_test_432() {
26133         do_facet mgs $LCTL nodemap_activate 0
26134         wait_nm_sync active
26135 }
26136
26137 test_432() {
26138         local tmpdir=$TMP/dir432
26139
26140         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
26141                 skip "Need MDS version at least 2.14.52"
26142
26143         stack_trap cleanup_test_432 EXIT
26144         mkdir $DIR/$tdir
26145         mkdir $tmpdir
26146
26147         do_facet mgs $LCTL nodemap_activate 1
26148         wait_nm_sync active
26149         do_facet mgs $LCTL nodemap_modify --name default \
26150                 --property admin --value 1
26151         do_facet mgs $LCTL nodemap_modify --name default \
26152                 --property trusted --value 1
26153         cancel_lru_locks mdc
26154         wait_nm_sync default admin_nodemap
26155         wait_nm_sync default trusted_nodemap
26156
26157         if [ $(mv $tmpdir $DIR/$tdir/ 2>&1 |
26158                grep -ci "Operation not permitted") -ne 0 ]; then
26159                 error "mv $tmpdir $DIR/$tdir/ hits 'Operation not permitted'"
26160         fi
26161 }
26162 run_test 432 "mv dir from outside Lustre"
26163
26164 prep_801() {
26165         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
26166         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
26167                 skip "Need server version at least 2.9.55"
26168
26169         start_full_debug_logging
26170 }
26171
26172 post_801() {
26173         stop_full_debug_logging
26174 }
26175
26176 barrier_stat() {
26177         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
26178                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
26179                            awk '/The barrier for/ { print $7 }')
26180                 echo $st
26181         else
26182                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
26183                 echo \'$st\'
26184         fi
26185 }
26186
26187 barrier_expired() {
26188         local expired
26189
26190         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
26191                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
26192                           awk '/will be expired/ { print $7 }')
26193         else
26194                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
26195         fi
26196
26197         echo $expired
26198 }
26199
26200 test_801a() {
26201         prep_801
26202
26203         echo "Start barrier_freeze at: $(date)"
26204         #define OBD_FAIL_BARRIER_DELAY          0x2202
26205         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
26206         # Do not reduce barrier time - See LU-11873
26207         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
26208
26209         sleep 2
26210         local b_status=$(barrier_stat)
26211         echo "Got barrier status at: $(date)"
26212         [ "$b_status" = "'freezing_p1'" ] ||
26213                 error "(1) unexpected barrier status $b_status"
26214
26215         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
26216         wait
26217         b_status=$(barrier_stat)
26218         [ "$b_status" = "'frozen'" ] ||
26219                 error "(2) unexpected barrier status $b_status"
26220
26221         local expired=$(barrier_expired)
26222         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
26223         sleep $((expired + 3))
26224
26225         b_status=$(barrier_stat)
26226         [ "$b_status" = "'expired'" ] ||
26227                 error "(3) unexpected barrier status $b_status"
26228
26229         # Do not reduce barrier time - See LU-11873
26230         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
26231                 error "(4) fail to freeze barrier"
26232
26233         b_status=$(barrier_stat)
26234         [ "$b_status" = "'frozen'" ] ||
26235                 error "(5) unexpected barrier status $b_status"
26236
26237         echo "Start barrier_thaw at: $(date)"
26238         #define OBD_FAIL_BARRIER_DELAY          0x2202
26239         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
26240         do_facet mgs $LCTL barrier_thaw $FSNAME &
26241
26242         sleep 2
26243         b_status=$(barrier_stat)
26244         echo "Got barrier status at: $(date)"
26245         [ "$b_status" = "'thawing'" ] ||
26246                 error "(6) unexpected barrier status $b_status"
26247
26248         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
26249         wait
26250         b_status=$(barrier_stat)
26251         [ "$b_status" = "'thawed'" ] ||
26252                 error "(7) unexpected barrier status $b_status"
26253
26254         #define OBD_FAIL_BARRIER_FAILURE        0x2203
26255         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
26256         do_facet mgs $LCTL barrier_freeze $FSNAME
26257
26258         b_status=$(barrier_stat)
26259         [ "$b_status" = "'failed'" ] ||
26260                 error "(8) unexpected barrier status $b_status"
26261
26262         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
26263         do_facet mgs $LCTL barrier_thaw $FSNAME
26264
26265         post_801
26266 }
26267 run_test 801a "write barrier user interfaces and stat machine"
26268
26269 test_801b() {
26270         prep_801
26271
26272         mkdir $DIR/$tdir || error "(1) fail to mkdir"
26273         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
26274         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
26275         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
26276         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
26277
26278         cancel_lru_locks mdc
26279
26280         # 180 seconds should be long enough
26281         do_facet mgs $LCTL barrier_freeze $FSNAME 180
26282
26283         local b_status=$(barrier_stat)
26284         [ "$b_status" = "'frozen'" ] ||
26285                 error "(6) unexpected barrier status $b_status"
26286
26287         mkdir $DIR/$tdir/d0/d10 &
26288         mkdir_pid=$!
26289
26290         touch $DIR/$tdir/d1/f13 &
26291         touch_pid=$!
26292
26293         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
26294         ln_pid=$!
26295
26296         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
26297         mv_pid=$!
26298
26299         rm -f $DIR/$tdir/d4/f12 &
26300         rm_pid=$!
26301
26302         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
26303
26304         # To guarantee taht the 'stat' is not blocked
26305         b_status=$(barrier_stat)
26306         [ "$b_status" = "'frozen'" ] ||
26307                 error "(8) unexpected barrier status $b_status"
26308
26309         # let above commands to run at background
26310         sleep 5
26311
26312         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
26313         ps -p $touch_pid || error "(10) touch should be blocked"
26314         ps -p $ln_pid || error "(11) link should be blocked"
26315         ps -p $mv_pid || error "(12) rename should be blocked"
26316         ps -p $rm_pid || error "(13) unlink should be blocked"
26317
26318         b_status=$(barrier_stat)
26319         [ "$b_status" = "'frozen'" ] ||
26320                 error "(14) unexpected barrier status $b_status"
26321
26322         do_facet mgs $LCTL barrier_thaw $FSNAME
26323         b_status=$(barrier_stat)
26324         [ "$b_status" = "'thawed'" ] ||
26325                 error "(15) unexpected barrier status $b_status"
26326
26327         wait $mkdir_pid || error "(16) mkdir should succeed"
26328         wait $touch_pid || error "(17) touch should succeed"
26329         wait $ln_pid || error "(18) link should succeed"
26330         wait $mv_pid || error "(19) rename should succeed"
26331         wait $rm_pid || error "(20) unlink should succeed"
26332
26333         post_801
26334 }
26335 run_test 801b "modification will be blocked by write barrier"
26336
26337 test_801c() {
26338         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26339
26340         prep_801
26341
26342         stop mds2 || error "(1) Fail to stop mds2"
26343
26344         do_facet mgs $LCTL barrier_freeze $FSNAME 30
26345
26346         local b_status=$(barrier_stat)
26347         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
26348                 do_facet mgs $LCTL barrier_thaw $FSNAME
26349                 error "(2) unexpected barrier status $b_status"
26350         }
26351
26352         do_facet mgs $LCTL barrier_rescan $FSNAME ||
26353                 error "(3) Fail to rescan barrier bitmap"
26354
26355         # Do not reduce barrier time - See LU-11873
26356         do_facet mgs $LCTL barrier_freeze $FSNAME 20
26357
26358         b_status=$(barrier_stat)
26359         [ "$b_status" = "'frozen'" ] ||
26360                 error "(4) unexpected barrier status $b_status"
26361
26362         do_facet mgs $LCTL barrier_thaw $FSNAME
26363         b_status=$(barrier_stat)
26364         [ "$b_status" = "'thawed'" ] ||
26365                 error "(5) unexpected barrier status $b_status"
26366
26367         local devname=$(mdsdevname 2)
26368
26369         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
26370
26371         do_facet mgs $LCTL barrier_rescan $FSNAME ||
26372                 error "(7) Fail to rescan barrier bitmap"
26373
26374         post_801
26375 }
26376 run_test 801c "rescan barrier bitmap"
26377
26378 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
26379 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
26380 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
26381 saved_MOUNT_OPTS=$MOUNT_OPTS
26382
26383 cleanup_802a() {
26384         trap 0
26385
26386         stopall
26387         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
26388         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
26389         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
26390         MOUNT_OPTS=$saved_MOUNT_OPTS
26391         setupall
26392 }
26393
26394 test_802a() {
26395         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
26396         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
26397         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
26398                 skip "Need server version at least 2.9.55"
26399
26400         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
26401
26402         mkdir $DIR/$tdir || error "(1) fail to mkdir"
26403
26404         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
26405                 error "(2) Fail to copy"
26406
26407         trap cleanup_802a EXIT
26408
26409         # sync by force before remount as readonly
26410         sync; sync_all_data; sleep 3; sync_all_data
26411
26412         stopall
26413
26414         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
26415         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
26416         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
26417
26418         echo "Mount the server as read only"
26419         setupall server_only || error "(3) Fail to start servers"
26420
26421         echo "Mount client without ro should fail"
26422         mount_client $MOUNT &&
26423                 error "(4) Mount client without 'ro' should fail"
26424
26425         echo "Mount client with ro should succeed"
26426         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
26427         mount_client $MOUNT ||
26428                 error "(5) Mount client with 'ro' should succeed"
26429
26430         echo "Modify should be refused"
26431         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
26432
26433         echo "Read should be allowed"
26434         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
26435                 error "(7) Read should succeed under ro mode"
26436
26437         cleanup_802a
26438 }
26439 run_test 802a "simulate readonly device"
26440
26441 test_802b() {
26442         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26443         remote_mds_nodsh && skip "remote MDS with nodsh"
26444
26445         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
26446                 skip "readonly option not available"
26447
26448         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
26449
26450         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
26451                 error "(2) Fail to copy"
26452
26453         # write back all cached data before setting MDT to readonly
26454         cancel_lru_locks
26455         sync_all_data
26456
26457         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
26458         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
26459
26460         echo "Modify should be refused"
26461         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
26462
26463         echo "Read should be allowed"
26464         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
26465                 error "(7) Read should succeed under ro mode"
26466
26467         # disable readonly
26468         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
26469 }
26470 run_test 802b "be able to set MDTs to readonly"
26471
26472 test_803a() {
26473         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26474         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
26475                 skip "MDS needs to be newer than 2.10.54"
26476
26477         mkdir_on_mdt0 $DIR/$tdir
26478         # Create some objects on all MDTs to trigger related logs objects
26479         for idx in $(seq $MDSCOUNT); do
26480                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
26481                         $DIR/$tdir/dir${idx} ||
26482                         error "Fail to create $DIR/$tdir/dir${idx}"
26483         done
26484
26485         sync; sleep 3
26486         wait_delete_completed # ensure old test cleanups are finished
26487         echo "before create:"
26488         $LFS df -i $MOUNT
26489         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26490
26491         for i in {1..10}; do
26492                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
26493                         error "Fail to create $DIR/$tdir/foo$i"
26494         done
26495
26496         sync; sleep 3
26497         echo "after create:"
26498         $LFS df -i $MOUNT
26499         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26500
26501         # allow for an llog to be cleaned up during the test
26502         [ $after_used -ge $((before_used + 10 - 1)) ] ||
26503                 error "before ($before_used) + 10 > after ($after_used)"
26504
26505         for i in {1..10}; do
26506                 rm -rf $DIR/$tdir/foo$i ||
26507                         error "Fail to remove $DIR/$tdir/foo$i"
26508         done
26509
26510         sleep 3 # avoid MDT return cached statfs
26511         wait_delete_completed
26512         echo "after unlink:"
26513         $LFS df -i $MOUNT
26514         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26515
26516         # allow for an llog to be created during the test
26517         [ $after_used -le $((before_used + 1)) ] ||
26518                 error "after ($after_used) > before ($before_used) + 1"
26519 }
26520 run_test 803a "verify agent object for remote object"
26521
26522 test_803b() {
26523         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26524         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
26525                 skip "MDS needs to be newer than 2.13.56"
26526         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26527
26528         for i in $(seq 0 $((MDSCOUNT - 1))); do
26529                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
26530         done
26531
26532         local before=0
26533         local after=0
26534
26535         local tmp
26536
26537         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
26538         for i in $(seq 0 $((MDSCOUNT - 1))); do
26539                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
26540                         awk '/getattr/ { print $2 }')
26541                 before=$((before + tmp))
26542         done
26543         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
26544         for i in $(seq 0 $((MDSCOUNT - 1))); do
26545                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
26546                         awk '/getattr/ { print $2 }')
26547                 after=$((after + tmp))
26548         done
26549
26550         [ $before -eq $after ] || error "getattr count $before != $after"
26551 }
26552 run_test 803b "remote object can getattr from cache"
26553
26554 test_804() {
26555         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26556         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
26557                 skip "MDS needs to be newer than 2.10.54"
26558         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
26559
26560         mkdir -p $DIR/$tdir
26561         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
26562                 error "Fail to create $DIR/$tdir/dir0"
26563
26564         local fid=$($LFS path2fid $DIR/$tdir/dir0)
26565         local dev=$(mdsdevname 2)
26566
26567         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26568                 grep ${fid} || error "NOT found agent entry for dir0"
26569
26570         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
26571                 error "Fail to create $DIR/$tdir/dir1"
26572
26573         touch $DIR/$tdir/dir1/foo0 ||
26574                 error "Fail to create $DIR/$tdir/dir1/foo0"
26575         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
26576         local rc=0
26577
26578         for idx in $(seq $MDSCOUNT); do
26579                 dev=$(mdsdevname $idx)
26580                 do_facet mds${idx} \
26581                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26582                         grep ${fid} && rc=$idx
26583         done
26584
26585         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
26586                 error "Fail to rename foo0 to foo1"
26587         if [ $rc -eq 0 ]; then
26588                 for idx in $(seq $MDSCOUNT); do
26589                         dev=$(mdsdevname $idx)
26590                         do_facet mds${idx} \
26591                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26592                         grep ${fid} && rc=$idx
26593                 done
26594         fi
26595
26596         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
26597                 error "Fail to rename foo1 to foo2"
26598         if [ $rc -eq 0 ]; then
26599                 for idx in $(seq $MDSCOUNT); do
26600                         dev=$(mdsdevname $idx)
26601                         do_facet mds${idx} \
26602                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26603                         grep ${fid} && rc=$idx
26604                 done
26605         fi
26606
26607         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
26608
26609         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
26610                 error "Fail to link to $DIR/$tdir/dir1/foo2"
26611         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
26612                 error "Fail to rename foo2 to foo0"
26613         unlink $DIR/$tdir/dir1/foo0 ||
26614                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
26615         rm -rf $DIR/$tdir/dir0 ||
26616                 error "Fail to rm $DIR/$tdir/dir0"
26617
26618         for idx in $(seq $MDSCOUNT); do
26619                 dev=$(mdsdevname $idx)
26620                 rc=0
26621
26622                 stop mds${idx}
26623                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
26624                         rc=$?
26625                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
26626                         error "mount mds$idx failed"
26627                 df $MOUNT > /dev/null 2>&1
26628
26629                 # e2fsck should not return error
26630                 [ $rc -eq 0 ] ||
26631                         error "e2fsck detected error on MDT${idx}: rc=$rc"
26632         done
26633 }
26634 run_test 804 "verify agent entry for remote entry"
26635
26636 cleanup_805() {
26637         do_facet $SINGLEMDS zfs set quota=$old $fsset
26638         unlinkmany $DIR/$tdir/f- 1000000
26639         trap 0
26640 }
26641
26642 test_805() {
26643         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
26644         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
26645         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
26646                 skip "netfree not implemented before 0.7"
26647         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
26648                 skip "Need MDS version at least 2.10.57"
26649
26650         local fsset
26651         local freekb
26652         local usedkb
26653         local old
26654         local quota
26655         local pref="osd-zfs.$FSNAME-MDT0000."
26656
26657         # limit available space on MDS dataset to meet nospace issue
26658         # quickly. then ZFS 0.7.2 can use reserved space if asked
26659         # properly (using netfree flag in osd_declare_destroy()
26660         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
26661         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
26662                 gawk '{print $3}')
26663         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
26664         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
26665         let "usedkb=usedkb-freekb"
26666         let "freekb=freekb/2"
26667         if let "freekb > 5000"; then
26668                 let "freekb=5000"
26669         fi
26670         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
26671         trap cleanup_805 EXIT
26672         mkdir_on_mdt0 $DIR/$tdir
26673         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
26674                 error "Can't set PFL layout"
26675         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
26676         rm -rf $DIR/$tdir || error "not able to remove"
26677         do_facet $SINGLEMDS zfs set quota=$old $fsset
26678         trap 0
26679 }
26680 run_test 805 "ZFS can remove from full fs"
26681
26682 # Size-on-MDS test
26683 check_lsom_data()
26684 {
26685         local file=$1
26686         local expect=$(stat -c %s $file)
26687
26688         check_lsom_size $1 $expect
26689
26690         local blocks=$($LFS getsom -b $file)
26691         expect=$(stat -c %b $file)
26692         [[ $blocks == $expect ]] ||
26693                 error "$file expected blocks: $expect, got: $blocks"
26694 }
26695
26696 check_lsom_size()
26697 {
26698         local size
26699         local expect=$2
26700
26701         cancel_lru_locks mdc
26702
26703         size=$($LFS getsom -s $1)
26704         [[ $size == $expect ]] ||
26705                 error "$file expected size: $expect, got: $size"
26706 }
26707
26708 test_806() {
26709         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26710                 skip "Need MDS version at least 2.11.52"
26711
26712         local bs=1048576
26713
26714         touch $DIR/$tfile || error "touch $tfile failed"
26715
26716         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
26717         save_lustre_params client "llite.*.xattr_cache" > $save
26718         lctl set_param llite.*.xattr_cache=0
26719         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
26720
26721         # single-threaded write
26722         echo "Test SOM for single-threaded write"
26723         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
26724                 error "write $tfile failed"
26725         check_lsom_size $DIR/$tfile $bs
26726
26727         local num=32
26728         local size=$(($num * $bs))
26729         local offset=0
26730         local i
26731
26732         echo "Test SOM for single client multi-threaded($num) write"
26733         $TRUNCATE $DIR/$tfile 0
26734         for ((i = 0; i < $num; i++)); do
26735                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26736                 local pids[$i]=$!
26737                 offset=$((offset + $bs))
26738         done
26739         for (( i=0; i < $num; i++ )); do
26740                 wait ${pids[$i]}
26741         done
26742         check_lsom_size $DIR/$tfile $size
26743
26744         $TRUNCATE $DIR/$tfile 0
26745         for ((i = 0; i < $num; i++)); do
26746                 offset=$((offset - $bs))
26747                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26748                 local pids[$i]=$!
26749         done
26750         for (( i=0; i < $num; i++ )); do
26751                 wait ${pids[$i]}
26752         done
26753         check_lsom_size $DIR/$tfile $size
26754
26755         # multi-client writes
26756         num=$(get_node_count ${CLIENTS//,/ })
26757         size=$(($num * $bs))
26758         offset=0
26759         i=0
26760
26761         echo "Test SOM for multi-client ($num) writes"
26762         $TRUNCATE $DIR/$tfile 0
26763         for client in ${CLIENTS//,/ }; do
26764                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26765                 local pids[$i]=$!
26766                 i=$((i + 1))
26767                 offset=$((offset + $bs))
26768         done
26769         for (( i=0; i < $num; i++ )); do
26770                 wait ${pids[$i]}
26771         done
26772         check_lsom_size $DIR/$tfile $offset
26773
26774         i=0
26775         $TRUNCATE $DIR/$tfile 0
26776         for client in ${CLIENTS//,/ }; do
26777                 offset=$((offset - $bs))
26778                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26779                 local pids[$i]=$!
26780                 i=$((i + 1))
26781         done
26782         for (( i=0; i < $num; i++ )); do
26783                 wait ${pids[$i]}
26784         done
26785         check_lsom_size $DIR/$tfile $size
26786
26787         # verify truncate
26788         echo "Test SOM for truncate"
26789         $TRUNCATE $DIR/$tfile 1048576
26790         check_lsom_size $DIR/$tfile 1048576
26791         $TRUNCATE $DIR/$tfile 1234
26792         check_lsom_size $DIR/$tfile 1234
26793
26794         # verify SOM blocks count
26795         echo "Verify SOM block count"
26796         $TRUNCATE $DIR/$tfile 0
26797         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
26798                 error "failed to write file $tfile"
26799         check_lsom_data $DIR/$tfile
26800 }
26801 run_test 806 "Verify Lazy Size on MDS"
26802
26803 test_807() {
26804         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
26805         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26806                 skip "Need MDS version at least 2.11.52"
26807
26808         # Registration step
26809         changelog_register || error "changelog_register failed"
26810         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
26811         changelog_users $SINGLEMDS | grep -q $cl_user ||
26812                 error "User $cl_user not found in changelog_users"
26813
26814         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
26815         save_lustre_params client "llite.*.xattr_cache" > $save
26816         lctl set_param llite.*.xattr_cache=0
26817         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
26818
26819         rm -rf $DIR/$tdir || error "rm $tdir failed"
26820         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
26821         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
26822         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
26823         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
26824                 error "truncate $tdir/trunc failed"
26825
26826         local bs=1048576
26827         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
26828                 error "write $tfile failed"
26829
26830         # multi-client wirtes
26831         local num=$(get_node_count ${CLIENTS//,/ })
26832         local offset=0
26833         local i=0
26834
26835         echo "Test SOM for multi-client ($num) writes"
26836         touch $DIR/$tfile || error "touch $tfile failed"
26837         $TRUNCATE $DIR/$tfile 0
26838         for client in ${CLIENTS//,/ }; do
26839                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26840                 local pids[$i]=$!
26841                 i=$((i + 1))
26842                 offset=$((offset + $bs))
26843         done
26844         for (( i=0; i < $num; i++ )); do
26845                 wait ${pids[$i]}
26846         done
26847
26848         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
26849         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
26850         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
26851         check_lsom_data $DIR/$tdir/trunc
26852         check_lsom_data $DIR/$tdir/single_dd
26853         check_lsom_data $DIR/$tfile
26854
26855         rm -rf $DIR/$tdir
26856         # Deregistration step
26857         changelog_deregister || error "changelog_deregister failed"
26858 }
26859 run_test 807 "verify LSOM syncing tool"
26860
26861 check_som_nologged()
26862 {
26863         local lines=$($LFS changelog $FSNAME-MDT0000 |
26864                 grep 'x=trusted.som' | wc -l)
26865         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
26866 }
26867
26868 test_808() {
26869         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
26870                 skip "Need MDS version at least 2.11.55"
26871
26872         # Registration step
26873         changelog_register || error "changelog_register failed"
26874
26875         touch $DIR/$tfile || error "touch $tfile failed"
26876         check_som_nologged
26877
26878         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
26879                 error "write $tfile failed"
26880         check_som_nologged
26881
26882         $TRUNCATE $DIR/$tfile 1234
26883         check_som_nologged
26884
26885         $TRUNCATE $DIR/$tfile 1048576
26886         check_som_nologged
26887
26888         # Deregistration step
26889         changelog_deregister || error "changelog_deregister failed"
26890 }
26891 run_test 808 "Check trusted.som xattr not logged in Changelogs"
26892
26893 check_som_nodata()
26894 {
26895         $LFS getsom $1
26896         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
26897 }
26898
26899 test_809() {
26900         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
26901                 skip "Need MDS version at least 2.11.56"
26902
26903         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
26904                 error "failed to create DoM-only file $DIR/$tfile"
26905         touch $DIR/$tfile || error "touch $tfile failed"
26906         check_som_nodata $DIR/$tfile
26907
26908         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
26909                 error "write $tfile failed"
26910         check_som_nodata $DIR/$tfile
26911
26912         $TRUNCATE $DIR/$tfile 1234
26913         check_som_nodata $DIR/$tfile
26914
26915         $TRUNCATE $DIR/$tfile 4097
26916         check_som_nodata $DIR/$file
26917 }
26918 run_test 809 "Verify no SOM xattr store for DoM-only files"
26919
26920 test_810() {
26921         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26922         $GSS && skip_env "could not run with gss"
26923         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
26924                 skip "OST < 2.12.58 doesn't align checksum"
26925
26926         set_checksums 1
26927         stack_trap "set_checksums $ORIG_CSUM" EXIT
26928         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
26929
26930         local csum
26931         local before
26932         local after
26933         for csum in $CKSUM_TYPES; do
26934                 #define OBD_FAIL_OSC_NO_GRANT   0x411
26935                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
26936                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
26937                         eval set -- $i
26938                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
26939                         before=$(md5sum $DIR/$tfile)
26940                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
26941                         after=$(md5sum $DIR/$tfile)
26942                         [ "$before" == "$after" ] ||
26943                                 error "$csum: $before != $after bs=$1 seek=$2"
26944                 done
26945         done
26946 }
26947 run_test 810 "partial page writes on ZFS (LU-11663)"
26948
26949 test_812a() {
26950         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
26951                 skip "OST < 2.12.51 doesn't support this fail_loc"
26952
26953         $LFS setstripe -c 1 -i 0 $DIR/$tfile
26954         # ensure ost1 is connected
26955         stat $DIR/$tfile >/dev/null || error "can't stat"
26956         wait_osc_import_state client ost1 FULL
26957         # no locks, no reqs to let the connection idle
26958         cancel_lru_locks osc
26959
26960         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
26961 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
26962         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
26963         wait_osc_import_state client ost1 CONNECTING
26964         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
26965
26966         stat $DIR/$tfile >/dev/null || error "can't stat file"
26967 }
26968 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
26969
26970 test_812b() { # LU-12378
26971         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
26972                 skip "OST < 2.12.51 doesn't support this fail_loc"
26973
26974         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
26975         # ensure ost1 is connected
26976         stat $DIR/$tfile >/dev/null || error "can't stat"
26977         wait_osc_import_state client ost1 FULL
26978         # no locks, no reqs to let the connection idle
26979         cancel_lru_locks osc
26980
26981         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
26982 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
26983         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
26984         wait_osc_import_state client ost1 CONNECTING
26985         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
26986
26987         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
26988         wait_osc_import_state client ost1 IDLE
26989 }
26990 run_test 812b "do not drop no resend request for idle connect"
26991
26992 test_812c() {
26993         local old
26994
26995         old=$($LCTL get_param -n osc.*.idle_timeout | head -n 1)
26996
26997         $LFS setstripe -c 1 -o 0 $DIR/$tfile
26998         $LFS getstripe $DIR/$tfile
26999         $LCTL set_param osc.*.idle_timeout=10
27000         stack_trap "$LCTL set_param osc.*.idle_timeout=$old" EXIT
27001         # ensure ost1 is connected
27002         stat $DIR/$tfile >/dev/null || error "can't stat"
27003         wait_osc_import_state client ost1 FULL
27004         # no locks, no reqs to let the connection idle
27005         cancel_lru_locks osc
27006
27007 #define OBD_FAIL_PTLRPC_IDLE_RACE        0x533
27008         $LCTL set_param fail_loc=0x80000533
27009         sleep 15
27010         dd if=/dev/zero of=$DIR/$tfile count=1 conv=sync || error "dd failed"
27011 }
27012 run_test 812c "idle import vs lock enqueue race"
27013
27014 test_813() {
27015         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
27016         [ -z "$file_heat_sav" ] && skip "no file heat support"
27017
27018         local readsample
27019         local writesample
27020         local readbyte
27021         local writebyte
27022         local readsample1
27023         local writesample1
27024         local readbyte1
27025         local writebyte1
27026
27027         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
27028         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
27029
27030         $LCTL set_param -n llite.*.file_heat=1
27031         echo "Turn on file heat"
27032         echo "Period second: $period_second, Decay percentage: $decay_pct"
27033
27034         echo "QQQQ" > $DIR/$tfile
27035         echo "QQQQ" > $DIR/$tfile
27036         echo "QQQQ" > $DIR/$tfile
27037         cat $DIR/$tfile > /dev/null
27038         cat $DIR/$tfile > /dev/null
27039         cat $DIR/$tfile > /dev/null
27040         cat $DIR/$tfile > /dev/null
27041
27042         local out=$($LFS heat_get $DIR/$tfile)
27043
27044         $LFS heat_get $DIR/$tfile
27045         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27046         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27047         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27048         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27049
27050         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
27051         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
27052         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
27053         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
27054
27055         sleep $((period_second + 3))
27056         echo "Sleep $((period_second + 3)) seconds..."
27057         # The recursion formula to calculate the heat of the file f is as
27058         # follow:
27059         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
27060         # Where Hi is the heat value in the period between time points i*I and
27061         # (i+1)*I; Ci is the access count in the period; the symbol P refers
27062         # to the weight of Ci.
27063         out=$($LFS heat_get $DIR/$tfile)
27064         $LFS heat_get $DIR/$tfile
27065         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27066         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27067         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27068         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27069
27070         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
27071                 error "read sample ($readsample) is wrong"
27072         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
27073                 error "write sample ($writesample) is wrong"
27074         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
27075                 error "read bytes ($readbyte) is wrong"
27076         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
27077                 error "write bytes ($writebyte) is wrong"
27078
27079         echo "QQQQ" > $DIR/$tfile
27080         echo "QQQQ" > $DIR/$tfile
27081         echo "QQQQ" > $DIR/$tfile
27082         cat $DIR/$tfile > /dev/null
27083         cat $DIR/$tfile > /dev/null
27084         cat $DIR/$tfile > /dev/null
27085         cat $DIR/$tfile > /dev/null
27086
27087         sleep $((period_second + 3))
27088         echo "Sleep $((period_second + 3)) seconds..."
27089
27090         out=$($LFS heat_get $DIR/$tfile)
27091         $LFS heat_get $DIR/$tfile
27092         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27093         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27094         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27095         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27096
27097         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
27098                 4 * $decay_pct) / 100") -eq 1 ] ||
27099                 error "read sample ($readsample1) is wrong"
27100         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
27101                 3 * $decay_pct) / 100") -eq 1 ] ||
27102                 error "write sample ($writesample1) is wrong"
27103         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
27104                 20 * $decay_pct) / 100") -eq 1 ] ||
27105                 error "read bytes ($readbyte1) is wrong"
27106         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
27107                 15 * $decay_pct) / 100") -eq 1 ] ||
27108                 error "write bytes ($writebyte1) is wrong"
27109
27110         echo "Turn off file heat for the file $DIR/$tfile"
27111         $LFS heat_set -o $DIR/$tfile
27112
27113         echo "QQQQ" > $DIR/$tfile
27114         echo "QQQQ" > $DIR/$tfile
27115         echo "QQQQ" > $DIR/$tfile
27116         cat $DIR/$tfile > /dev/null
27117         cat $DIR/$tfile > /dev/null
27118         cat $DIR/$tfile > /dev/null
27119         cat $DIR/$tfile > /dev/null
27120
27121         out=$($LFS heat_get $DIR/$tfile)
27122         $LFS heat_get $DIR/$tfile
27123         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27124         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27125         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27126         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27127
27128         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
27129         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
27130         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
27131         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
27132
27133         echo "Trun on file heat for the file $DIR/$tfile"
27134         $LFS heat_set -O $DIR/$tfile
27135
27136         echo "QQQQ" > $DIR/$tfile
27137         echo "QQQQ" > $DIR/$tfile
27138         echo "QQQQ" > $DIR/$tfile
27139         cat $DIR/$tfile > /dev/null
27140         cat $DIR/$tfile > /dev/null
27141         cat $DIR/$tfile > /dev/null
27142         cat $DIR/$tfile > /dev/null
27143
27144         out=$($LFS heat_get $DIR/$tfile)
27145         $LFS heat_get $DIR/$tfile
27146         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27147         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27148         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27149         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27150
27151         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
27152         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
27153         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
27154         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
27155
27156         $LFS heat_set -c $DIR/$tfile
27157         $LCTL set_param -n llite.*.file_heat=0
27158         echo "Turn off file heat support for the Lustre filesystem"
27159
27160         echo "QQQQ" > $DIR/$tfile
27161         echo "QQQQ" > $DIR/$tfile
27162         echo "QQQQ" > $DIR/$tfile
27163         cat $DIR/$tfile > /dev/null
27164         cat $DIR/$tfile > /dev/null
27165         cat $DIR/$tfile > /dev/null
27166         cat $DIR/$tfile > /dev/null
27167
27168         out=$($LFS heat_get $DIR/$tfile)
27169         $LFS heat_get $DIR/$tfile
27170         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27171         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27172         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27173         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27174
27175         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
27176         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
27177         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
27178         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
27179
27180         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
27181         rm -f $DIR/$tfile
27182 }
27183 run_test 813 "File heat verfication"
27184
27185 test_814()
27186 {
27187         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
27188         echo -n y >> $DIR/$tfile
27189         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
27190         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
27191 }
27192 run_test 814 "sparse cp works as expected (LU-12361)"
27193
27194 test_815()
27195 {
27196         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
27197         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
27198 }
27199 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
27200
27201 test_816() {
27202         local ost1_imp=$(get_osc_import_name client ost1)
27203         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
27204                          cut -d'.' -f2)
27205
27206         $LFS setstripe -c 1 -i 0 $DIR/$tfile
27207         # ensure ost1 is connected
27208
27209         stat $DIR/$tfile >/dev/null || error "can't stat"
27210         wait_osc_import_state client ost1 FULL
27211         # no locks, no reqs to let the connection idle
27212         cancel_lru_locks osc
27213         lru_resize_disable osc
27214         local before
27215         local now
27216         before=$($LCTL get_param -n \
27217                  ldlm.namespaces.$imp_name.lru_size)
27218
27219         wait_osc_import_state client ost1 IDLE
27220         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
27221         now=$($LCTL get_param -n \
27222               ldlm.namespaces.$imp_name.lru_size)
27223         [ $before == $now ] || error "lru_size changed $before != $now"
27224 }
27225 run_test 816 "do not reset lru_resize on idle reconnect"
27226
27227 cleanup_817() {
27228         umount $tmpdir
27229         exportfs -u localhost:$DIR/nfsexp
27230         rm -rf $DIR/nfsexp
27231 }
27232
27233 test_817() {
27234         systemctl restart nfs-server.service || skip "failed to restart nfsd"
27235
27236         mkdir -p $DIR/nfsexp
27237         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
27238                 error "failed to export nfs"
27239
27240         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
27241         stack_trap cleanup_817 EXIT
27242
27243         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
27244                 error "failed to mount nfs to $tmpdir"
27245
27246         cp /bin/true $tmpdir
27247         $DIR/nfsexp/true || error "failed to execute 'true' command"
27248 }
27249 run_test 817 "nfsd won't cache write lock for exec file"
27250
27251 test_818() {
27252         mkdir $DIR/$tdir
27253         $LFS setstripe -c1 -i0 $DIR/$tfile
27254         $LFS setstripe -c1 -i1 $DIR/$tfile
27255         stop $SINGLEMDS
27256         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
27257         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
27258         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
27259                 error "start $SINGLEMDS failed"
27260         rm -rf $DIR/$tdir
27261 }
27262 run_test 818 "unlink with failed llog"
27263
27264 test_819a() {
27265         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27266         cancel_lru_locks osc
27267         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
27268         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
27269         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
27270         rm -f $TDIR/$tfile
27271 }
27272 run_test 819a "too big niobuf in read"
27273
27274 test_819b() {
27275         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
27276         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
27277         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27278         cancel_lru_locks osc
27279         sleep 1
27280         rm -f $TDIR/$tfile
27281 }
27282 run_test 819b "too big niobuf in write"
27283
27284
27285 function test_820_start_ost() {
27286         sleep 5
27287
27288         for num in $(seq $OSTCOUNT); do
27289                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
27290         done
27291 }
27292
27293 test_820() {
27294         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27295
27296         mkdir $DIR/$tdir
27297         umount_client $MOUNT || error "umount failed"
27298         for num in $(seq $OSTCOUNT); do
27299                 stop ost$num
27300         done
27301
27302         # mount client with no active OSTs
27303         # so that the client can't initialize max LOV EA size
27304         # from OSC notifications
27305         mount_client $MOUNT || error "mount failed"
27306         # delay OST starting to keep this 0 max EA size for a while
27307         test_820_start_ost &
27308
27309         # create a directory on MDS2
27310         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
27311                 error "Failed to create directory"
27312         # open intent should update default EA size
27313         # see mdc_update_max_ea_from_body()
27314         # notice this is the very first RPC to MDS2
27315         out=$(cp /etc/services $DIR/$tdir/mds2 2>&1)
27316         ret=$?
27317         echo $out
27318         # With SSK, this situation can lead to -EPERM being returned.
27319         # In that case, simply retry.
27320         if [ $ret -ne 0 ] && $SHARED_KEY; then
27321                 if echo "$out" | grep -q "not permitted"; then
27322                         cp /etc/services $DIR/$tdir/mds2
27323                         ret=$?
27324                 fi
27325         fi
27326         [ $ret -eq 0 ] || error "Failed to copy files to mds$n"
27327 }
27328 run_test 820 "update max EA from open intent"
27329
27330 test_822() {
27331         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
27332
27333         save_lustre_params mds1 \
27334                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
27335         do_facet $SINGLEMDS "$LCTL set_param -n \
27336                         osp.$FSNAME-OST*MDT0000.max_create_count=0"
27337         do_facet $SINGLEMDS "$LCTL set_param -n \
27338                         osp.$FSNAME-OST0000*MDT0000.max_create_count=20000"
27339
27340         # wait for statfs update to clear OS_STATFS_NOPRECREATE
27341         local maxage=$(do_facet mds1 $LCTL get_param -n \
27342                        osp.$FSNAME-OST0000*MDT0000.maxage)
27343         sleep $((maxage + 1))
27344
27345         #define OBD_FAIL_NET_ERROR_RPC          0x532
27346         do_facet mds1 "$LCTL set_param fail_loc=0x80000532 fail_val=5"
27347
27348         stack_trap "restore_lustre_params < $p; rm $p"
27349
27350         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
27351                       osp.$FSNAME-OST0000*MDT0000.create_count")
27352         for i in $(seq 1 $count); do
27353                 touch $DIR/$tfile.${i} || error "touch failed"
27354         done
27355 }
27356 run_test 822 "test precreate failure"
27357
27358 #
27359 # tests that do cleanup/setup should be run at the end
27360 #
27361
27362 test_900() {
27363         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27364         local ls
27365
27366         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
27367         $LCTL set_param fail_loc=0x903
27368
27369         cancel_lru_locks MGC
27370
27371         FAIL_ON_ERROR=true cleanup
27372         FAIL_ON_ERROR=true setup
27373 }
27374 run_test 900 "umount should not race with any mgc requeue thread"
27375
27376 # LUS-6253/LU-11185
27377 test_901() {
27378         local oldc
27379         local newc
27380         local olds
27381         local news
27382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27383
27384         # some get_param have a bug to handle dot in param name
27385         cancel_lru_locks MGC
27386         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
27387         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
27388         umount_client $MOUNT || error "umount failed"
27389         mount_client $MOUNT || error "mount failed"
27390         cancel_lru_locks MGC
27391         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
27392         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
27393
27394         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
27395         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
27396
27397         return 0
27398 }
27399 run_test 901 "don't leak a mgc lock on client umount"
27400
27401 # LU-13377
27402 test_902() {
27403         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
27404                 skip "client does not have LU-13377 fix"
27405         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
27406         $LCTL set_param fail_loc=0x1415
27407         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27408         cancel_lru_locks osc
27409         rm -f $DIR/$tfile
27410 }
27411 run_test 902 "test short write doesn't hang lustre"
27412
27413 # LU-14711
27414 test_903() {
27415         $LFS setstripe -i 0 -c 1 $DIR/$tfile $DIR/${tfile}-2
27416         echo "blah" > $DIR/${tfile}-2
27417         dd if=/dev/zero of=$DIR/$tfile bs=1M count=6 conv=fsync
27418         #define OBD_FAIL_OSC_SLOW_PAGE_EVICT 0x417
27419         $LCTL set_param fail_loc=0x417 fail_val=20
27420
27421         mv $DIR/${tfile}-2 $DIR/$tfile # Destroys the big object
27422         sleep 1 # To start the destroy
27423         wait_destroy_complete 150 || error "Destroy taking too long"
27424         cat $DIR/$tfile > /dev/null || error "Evicted"
27425 }
27426 run_test 903 "Test long page discard does not cause evictions"
27427
27428 complete $SECONDS
27429 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
27430 check_and_cleanup_lustre
27431 if [ "$I_MOUNTED" != "yes" ]; then
27432         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
27433 fi
27434 exit_status