Whamcloud - gitweb
LU-14448 lod: verify LOV before set/inherit
[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         local qos_old
5686
5687         test_mkdir $DIR/$tdir
5688         $LFS setstripe -c $OSTCOUNT $DIR/$tdir
5689
5690         qos_old=$(do_facet mds1 \
5691                 "$LCTL get_param -n lod.$FSNAME-*.qos_threshold_rr" | head -n 1)
5692         do_nodes $(comma_list $(mdts_nodes)) \
5693                 "$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=100"
5694         stack_trap "do_nodes $(comma_list $(mdts_nodes)) \
5695                 '$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=${qos_old%%%}'"
5696
5697         createmany -o $DIR/$tdir/t- 1000
5698         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5699         for ((n = 0; n < $OSTCOUNT; n++)); do
5700                 objs[$n]=$(awk -vobjs=0 '($1 == '$n') { objs += 1 } \
5701                            END { printf("%0.0f", objs) }' $TMP/$tfile)
5702                 objs0[$n]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5703                             '($1 == '$n') { objs += 1 } \
5704                             END { printf("%0.0f", objs) }')
5705                 log "OST$n has ${objs[$n]} objects, ${objs0[$n]} are index 0"
5706         done
5707         unlinkmany $DIR/$tdir/t- 1000
5708
5709         nlast=0
5710         for ((n = 0; n < $OSTCOUNT; n++)); do
5711                 (( ${objs[$n]} > ${objs[$nlast]} * 4 / 5 )) ||
5712                         { $LFS df && $LFS df -i &&
5713                         error "OST $n has fewer objects vs. OST $nlast" \
5714                               " (${objs[$n]} < ${objs[$nlast]}"; }
5715                 (( ${objs[$n]} < ${objs[$nlast]} * 5 / 4 )) ||
5716                         { $LFS df && $LFS df -i &&
5717                         error "OST $n has fewer objects vs. OST $nlast" \
5718                               " (${objs[$n]} < ${objs[$nlast]}"; }
5719
5720                 (( ${objs0[$n]} > ${objs0[$nlast]} * 4 / 5 )) ||
5721                         { $LFS df && $LFS df -i &&
5722                         error "OST $n has fewer #0 objects vs. OST $nlast" \
5723                               " (${objs0[$n]} < ${objs0[$nlast]}"; }
5724                 (( ${objs0[$n]} < ${objs0[$nlast]} * 5 / 4 )) ||
5725                         { $LFS df && $LFS df -i &&
5726                         error "OST $n has fewer #0 objects vs. OST $nlast" \
5727                               " (${objs0[$n]} < ${objs0[$nlast]}"; }
5728                 nlast=$n
5729         done
5730 }
5731 run_test 51d "check object distribution"
5732
5733 test_51e() {
5734         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5735                 skip_env "ldiskfs only test"
5736         fi
5737
5738         test_mkdir -c1 $DIR/$tdir
5739         test_mkdir -c1 $DIR/$tdir/d0
5740
5741         touch $DIR/$tdir/d0/foo
5742         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5743                 error "file exceed 65000 nlink limit!"
5744         unlinkmany $DIR/$tdir/d0/f- 65001
5745         return 0
5746 }
5747 run_test 51e "check file nlink limit"
5748
5749 test_51f() {
5750         test_mkdir $DIR/$tdir
5751
5752         local max=100000
5753         local ulimit_old=$(ulimit -n)
5754         local spare=20 # number of spare fd's for scripts/libraries, etc.
5755         local mdt=$($LFS getstripe -m $DIR/$tdir)
5756         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5757
5758         echo "MDT$mdt numfree=$numfree, max=$max"
5759         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5760         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5761                 while ! ulimit -n $((numfree + spare)); do
5762                         numfree=$((numfree * 3 / 4))
5763                 done
5764                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5765         else
5766                 echo "left ulimit at $ulimit_old"
5767         fi
5768
5769         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5770                 unlinkmany $DIR/$tdir/f $numfree
5771                 error "create+open $numfree files in $DIR/$tdir failed"
5772         }
5773         ulimit -n $ulimit_old
5774
5775         # if createmany exits at 120s there will be fewer than $numfree files
5776         unlinkmany $DIR/$tdir/f $numfree || true
5777 }
5778 run_test 51f "check many open files limit"
5779
5780 test_52a() {
5781         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5782         test_mkdir $DIR/$tdir
5783         touch $DIR/$tdir/foo
5784         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5785         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5786         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5787         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5788         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5789                                         error "link worked"
5790         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5791         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5792         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5793                                                      error "lsattr"
5794         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5795         cp -r $DIR/$tdir $TMP/
5796         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5797 }
5798 run_test 52a "append-only flag test (should return errors)"
5799
5800 test_52b() {
5801         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5802         test_mkdir $DIR/$tdir
5803         touch $DIR/$tdir/foo
5804         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5805         cat test > $DIR/$tdir/foo && error "cat test worked"
5806         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5807         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5808         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5809                                         error "link worked"
5810         echo foo >> $DIR/$tdir/foo && error "echo worked"
5811         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5812         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5813         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5814         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5815                                                         error "lsattr"
5816         chattr -i $DIR/$tdir/foo || error "chattr failed"
5817
5818         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5819 }
5820 run_test 52b "immutable flag test (should return errors) ======="
5821
5822 test_53() {
5823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5824         remote_mds_nodsh && skip "remote MDS with nodsh"
5825         remote_ost_nodsh && skip "remote OST with nodsh"
5826
5827         local param
5828         local param_seq
5829         local ostname
5830         local mds_last
5831         local mds_last_seq
5832         local ost_last
5833         local ost_last_seq
5834         local ost_last_id
5835         local ostnum
5836         local node
5837         local found=false
5838         local support_last_seq=true
5839
5840         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5841                 support_last_seq=false
5842
5843         # only test MDT0000
5844         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5845         local value
5846         for value in $(do_facet $SINGLEMDS \
5847                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5848                 param=$(echo ${value[0]} | cut -d "=" -f1)
5849                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5850
5851                 if $support_last_seq; then
5852                         param_seq=$(echo $param |
5853                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5854                         mds_last_seq=$(do_facet $SINGLEMDS \
5855                                        $LCTL get_param -n $param_seq)
5856                 fi
5857                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5858
5859                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5860                 node=$(facet_active_host ost$((ostnum+1)))
5861                 param="obdfilter.$ostname.last_id"
5862                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5863                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5864                         ost_last_id=$ost_last
5865
5866                         if $support_last_seq; then
5867                                 ost_last_id=$(echo $ost_last |
5868                                               awk -F':' '{print $2}' |
5869                                               sed -e "s/^0x//g")
5870                                 ost_last_seq=$(echo $ost_last |
5871                                                awk -F':' '{print $1}')
5872                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5873                         fi
5874
5875                         if [[ $ost_last_id != $mds_last ]]; then
5876                                 error "$ost_last_id != $mds_last"
5877                         else
5878                                 found=true
5879                                 break
5880                         fi
5881                 done
5882         done
5883         $found || error "can not match last_seq/last_id for $mdtosc"
5884         return 0
5885 }
5886 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5887
5888 test_54a() {
5889         perl -MSocket -e ';' || skip "no Socket perl module installed"
5890
5891         $SOCKETSERVER $DIR/socket ||
5892                 error "$SOCKETSERVER $DIR/socket failed: $?"
5893         $SOCKETCLIENT $DIR/socket ||
5894                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5895         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5896 }
5897 run_test 54a "unix domain socket test =========================="
5898
5899 test_54b() {
5900         f="$DIR/f54b"
5901         mknod $f c 1 3
5902         chmod 0666 $f
5903         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5904 }
5905 run_test 54b "char device works in lustre ======================"
5906
5907 find_loop_dev() {
5908         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5909         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5910         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5911
5912         for i in $(seq 3 7); do
5913                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5914                 LOOPDEV=$LOOPBASE$i
5915                 LOOPNUM=$i
5916                 break
5917         done
5918 }
5919
5920 cleanup_54c() {
5921         local rc=0
5922         loopdev="$DIR/loop54c"
5923
5924         trap 0
5925         $UMOUNT $DIR/$tdir || rc=$?
5926         losetup -d $loopdev || true
5927         losetup -d $LOOPDEV || true
5928         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5929         return $rc
5930 }
5931
5932 test_54c() {
5933         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5934
5935         loopdev="$DIR/loop54c"
5936
5937         find_loop_dev
5938         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5939         trap cleanup_54c EXIT
5940         mknod $loopdev b 7 $LOOPNUM
5941         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5942         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5943         losetup $loopdev $DIR/$tfile ||
5944                 error "can't set up $loopdev for $DIR/$tfile"
5945         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5946         test_mkdir $DIR/$tdir
5947         mount -t ext2 $loopdev $DIR/$tdir ||
5948                 error "error mounting $loopdev on $DIR/$tdir"
5949         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5950                 error "dd write"
5951         df $DIR/$tdir
5952         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5953                 error "dd read"
5954         cleanup_54c
5955 }
5956 run_test 54c "block device works in lustre ====================="
5957
5958 test_54d() {
5959         f="$DIR/f54d"
5960         string="aaaaaa"
5961         mknod $f p
5962         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5963 }
5964 run_test 54d "fifo device works in lustre ======================"
5965
5966 test_54e() {
5967         f="$DIR/f54e"
5968         string="aaaaaa"
5969         cp -aL /dev/console $f
5970         echo $string > $f || error "echo $string to $f failed"
5971 }
5972 run_test 54e "console/tty device works in lustre ======================"
5973
5974 test_56a() {
5975         local numfiles=3
5976         local numdirs=2
5977         local dir=$DIR/$tdir
5978
5979         rm -rf $dir
5980         test_mkdir -p $dir/dir
5981         for i in $(seq $numfiles); do
5982                 touch $dir/file$i
5983                 touch $dir/dir/file$i
5984         done
5985
5986         local numcomp=$($LFS getstripe --component-count $dir)
5987
5988         [[ $numcomp == 0 ]] && numcomp=1
5989
5990         # test lfs getstripe with --recursive
5991         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5992
5993         [[ $filenum -eq $((numfiles * 2)) ]] ||
5994                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5995         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5996         [[ $filenum -eq $numfiles ]] ||
5997                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5998         echo "$LFS getstripe showed obdidx or l_ost_idx"
5999
6000         # test lfs getstripe with file instead of dir
6001         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
6002         [[ $filenum -eq 1 ]] ||
6003                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
6004         echo "$LFS getstripe file1 passed"
6005
6006         #test lfs getstripe with --verbose
6007         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
6008         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
6009                 error "$LFS getstripe --verbose $dir: "\
6010                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
6011         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
6012                 error "$LFS getstripe $dir: showed lmm_magic"
6013
6014         #test lfs getstripe with -v prints lmm_fid
6015         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
6016         local countfids=$((numdirs + numfiles * numcomp))
6017         [[ $filenum -eq $countfids ]] ||
6018                 error "$LFS getstripe -v $dir: "\
6019                       "got $filenum want $countfids lmm_fid"
6020         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
6021                 error "$LFS getstripe $dir: showed lmm_fid by default"
6022         echo "$LFS getstripe --verbose passed"
6023
6024         #check for FID information
6025         local fid1=$($LFS getstripe --fid $dir/file1)
6026         local fid2=$($LFS getstripe --verbose $dir/file1 |
6027                      awk '/lmm_fid: / { print $2; exit; }')
6028         local fid3=$($LFS path2fid $dir/file1)
6029
6030         [ "$fid1" != "$fid2" ] &&
6031                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
6032         [ "$fid1" != "$fid3" ] &&
6033                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
6034         echo "$LFS getstripe --fid passed"
6035
6036         #test lfs getstripe with --obd
6037         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
6038                 error "$LFS getstripe --obd wrong_uuid: should return error"
6039
6040         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6041
6042         local ostidx=1
6043         local obduuid=$(ostuuid_from_index $ostidx)
6044         local found=$($LFS getstripe -r --obd $obduuid $dir |
6045                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
6046
6047         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
6048         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
6049                 ((filenum--))
6050         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
6051                 ((filenum--))
6052
6053         [[ $found -eq $filenum ]] ||
6054                 error "$LFS getstripe --obd: found $found expect $filenum"
6055         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
6056                 sed '/^[         ]*'${ostidx}'[  ]/d' |
6057                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
6058                 error "$LFS getstripe --obd: should not show file on other obd"
6059         echo "$LFS getstripe --obd passed"
6060 }
6061 run_test 56a "check $LFS getstripe"
6062
6063 test_56b() {
6064         local dir=$DIR/$tdir
6065         local numdirs=3
6066
6067         test_mkdir $dir
6068         for i in $(seq $numdirs); do
6069                 test_mkdir $dir/dir$i
6070         done
6071
6072         # test lfs getdirstripe default mode is non-recursion, which is
6073         # different from lfs getstripe
6074         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
6075
6076         [[ $dircnt -eq 1 ]] ||
6077                 error "$LFS getdirstripe: found $dircnt, not 1"
6078         dircnt=$($LFS getdirstripe --recursive $dir |
6079                 grep -c lmv_stripe_count)
6080         [[ $dircnt -eq $((numdirs + 1)) ]] ||
6081                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
6082 }
6083 run_test 56b "check $LFS getdirstripe"
6084
6085 test_56c() {
6086         remote_ost_nodsh && skip "remote OST with nodsh"
6087
6088         local ost_idx=0
6089         local ost_name=$(ostname_from_index $ost_idx)
6090         local old_status=$(ost_dev_status $ost_idx)
6091         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6092
6093         [[ -z "$old_status" ]] ||
6094                 skip_env "OST $ost_name is in $old_status status"
6095
6096         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
6097         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6098                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
6099         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6100                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
6101                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
6102         fi
6103
6104         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
6105                 error "$LFS df -v showing inactive devices"
6106         sleep_maxage
6107
6108         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
6109
6110         [[ "$new_status" =~ "D" ]] ||
6111                 error "$ost_name status is '$new_status', missing 'D'"
6112         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
6113                 [[ "$new_status" =~ "N" ]] ||
6114                         error "$ost_name status is '$new_status', missing 'N'"
6115         fi
6116         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6117                 [[ "$new_status" =~ "f" ]] ||
6118                         error "$ost_name status is '$new_status', missing 'f'"
6119         fi
6120
6121         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
6122         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6123                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
6124         [[ -z "$p" ]] && restore_lustre_params < $p || true
6125         sleep_maxage
6126
6127         new_status=$(ost_dev_status $ost_idx)
6128         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
6129                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
6130         # can't check 'f' as devices may actually be on flash
6131 }
6132 run_test 56c "check 'lfs df' showing device status"
6133
6134 test_56d() {
6135         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
6136         local osts=$($LFS df -v $MOUNT | grep -c OST)
6137
6138         $LFS df $MOUNT
6139
6140         (( mdts == MDSCOUNT )) ||
6141                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
6142         (( osts == OSTCOUNT )) ||
6143                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
6144 }
6145 run_test 56d "'lfs df -v' prints only configured devices"
6146
6147 test_56e() {
6148         err_enoent=2 # No such file or directory
6149         err_eopnotsupp=95 # Operation not supported
6150
6151         enoent_mnt=/pmt1 # Invalid dentry. Path not present
6152         notsup_mnt=/tmp  # Valid dentry, but Not a lustreFS
6153
6154         # Check for handling of path not exists
6155         output=$($LFS df $enoent_mnt 2>&1)
6156         ret=$?
6157
6158         fs=$(echo $output | awk -F: '{print $2}' | awk '{print $3}' | tr -d \')
6159         [[ $fs = $enoent_mnt && $ret -eq $err_enoent ]] ||
6160                 error "expect failure $err_enoent, not $ret"
6161
6162         # Check for handling of non-Lustre FS
6163         output=$($LFS df $notsup_mnt)
6164         ret=$?
6165
6166         fs=$(echo $output | awk '{print $1}' | awk -F: '{print $2}')
6167         [[ $fs = $notsup_mnt && $ret -eq $err_eopnotsupp ]] ||
6168                 error "expect success $err_eopnotsupp, not $ret"
6169
6170         # Check for multiple LustreFS argument
6171         output=$($LFS df $MOUNT $MOUNT $MOUNT | grep -c "filesystem_summary:")
6172         ret=$?
6173
6174         [[ $output -eq 3 && $ret -eq 0 ]] ||
6175                 error "expect success 3, not $output, rc = $ret"
6176
6177         # Check for correct non-Lustre FS handling among multiple
6178         # LustreFS argument
6179         output=$($LFS df $MOUNT $notsup_mnt $MOUNT |
6180                 grep -c "filesystem_summary:"; exit ${PIPESTATUS[0]})
6181         ret=$?
6182
6183         [[ $output -eq 2 && $ret -eq $err_eopnotsupp ]] ||
6184                 error "expect success 2, not $output, rc = $ret"
6185 }
6186 run_test 56e "'lfs df' Handle non LustreFS & multiple LustreFS"
6187
6188 NUMFILES=3
6189 NUMDIRS=3
6190 setup_56() {
6191         local local_tdir="$1"
6192         local local_numfiles="$2"
6193         local local_numdirs="$3"
6194         local dir_params="$4"
6195         local dir_stripe_params="$5"
6196
6197         if [ ! -d "$local_tdir" ] ; then
6198                 test_mkdir -p $dir_stripe_params $local_tdir
6199                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
6200                 for i in $(seq $local_numfiles) ; do
6201                         touch $local_tdir/file$i
6202                 done
6203                 for i in $(seq $local_numdirs) ; do
6204                         test_mkdir $dir_stripe_params $local_tdir/dir$i
6205                         for j in $(seq $local_numfiles) ; do
6206                                 touch $local_tdir/dir$i/file$j
6207                         done
6208                 done
6209         fi
6210 }
6211
6212 setup_56_special() {
6213         local local_tdir=$1
6214         local local_numfiles=$2
6215         local local_numdirs=$3
6216
6217         setup_56 $local_tdir $local_numfiles $local_numdirs
6218
6219         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
6220                 for i in $(seq $local_numfiles) ; do
6221                         mknod $local_tdir/loop${i}b b 7 $i
6222                         mknod $local_tdir/null${i}c c 1 3
6223                         ln -s $local_tdir/file1 $local_tdir/link${i}
6224                 done
6225                 for i in $(seq $local_numdirs) ; do
6226                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
6227                         mknod $local_tdir/dir$i/null${i}c c 1 3
6228                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
6229                 done
6230         fi
6231 }
6232
6233 test_56g() {
6234         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6235         local expected=$(($NUMDIRS + 2))
6236
6237         setup_56 $dir $NUMFILES $NUMDIRS
6238
6239         # test lfs find with -name
6240         for i in $(seq $NUMFILES) ; do
6241                 local nums=$($LFS find -name "*$i" $dir | wc -l)
6242
6243                 [ $nums -eq $expected ] ||
6244                         error "lfs find -name '*$i' $dir wrong: "\
6245                               "found $nums, expected $expected"
6246         done
6247 }
6248 run_test 56g "check lfs find -name"
6249
6250 test_56h() {
6251         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6252         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
6253
6254         setup_56 $dir $NUMFILES $NUMDIRS
6255
6256         # test lfs find with ! -name
6257         for i in $(seq $NUMFILES) ; do
6258                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
6259
6260                 [ $nums -eq $expected ] ||
6261                         error "lfs find ! -name '*$i' $dir wrong: "\
6262                               "found $nums, expected $expected"
6263         done
6264 }
6265 run_test 56h "check lfs find ! -name"
6266
6267 test_56i() {
6268         local dir=$DIR/$tdir
6269
6270         test_mkdir $dir
6271
6272         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
6273         local out=$($cmd)
6274
6275         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
6276 }
6277 run_test 56i "check 'lfs find -ost UUID' skips directories"
6278
6279 test_56j() {
6280         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6281
6282         setup_56_special $dir $NUMFILES $NUMDIRS
6283
6284         local expected=$((NUMDIRS + 1))
6285         local cmd="$LFS find -type d $dir"
6286         local nums=$($cmd | wc -l)
6287
6288         [ $nums -eq $expected ] ||
6289                 error "'$cmd' wrong: found $nums, expected $expected"
6290 }
6291 run_test 56j "check lfs find -type d"
6292
6293 test_56k() {
6294         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6295
6296         setup_56_special $dir $NUMFILES $NUMDIRS
6297
6298         local expected=$(((NUMDIRS + 1) * NUMFILES))
6299         local cmd="$LFS find -type f $dir"
6300         local nums=$($cmd | wc -l)
6301
6302         [ $nums -eq $expected ] ||
6303                 error "'$cmd' wrong: found $nums, expected $expected"
6304 }
6305 run_test 56k "check lfs find -type f"
6306
6307 test_56l() {
6308         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6309
6310         setup_56_special $dir $NUMFILES $NUMDIRS
6311
6312         local expected=$((NUMDIRS + NUMFILES))
6313         local cmd="$LFS find -type b $dir"
6314         local nums=$($cmd | wc -l)
6315
6316         [ $nums -eq $expected ] ||
6317                 error "'$cmd' wrong: found $nums, expected $expected"
6318 }
6319 run_test 56l "check lfs find -type b"
6320
6321 test_56m() {
6322         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6323
6324         setup_56_special $dir $NUMFILES $NUMDIRS
6325
6326         local expected=$((NUMDIRS + NUMFILES))
6327         local cmd="$LFS find -type c $dir"
6328         local nums=$($cmd | wc -l)
6329         [ $nums -eq $expected ] ||
6330                 error "'$cmd' wrong: found $nums, expected $expected"
6331 }
6332 run_test 56m "check lfs find -type c"
6333
6334 test_56n() {
6335         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6336         setup_56_special $dir $NUMFILES $NUMDIRS
6337
6338         local expected=$((NUMDIRS + NUMFILES))
6339         local cmd="$LFS find -type l $dir"
6340         local nums=$($cmd | wc -l)
6341
6342         [ $nums -eq $expected ] ||
6343                 error "'$cmd' wrong: found $nums, expected $expected"
6344 }
6345 run_test 56n "check lfs find -type l"
6346
6347 test_56o() {
6348         local dir=$DIR/$tdir
6349
6350         setup_56 $dir $NUMFILES $NUMDIRS
6351         utime $dir/file1 > /dev/null || error "utime (1)"
6352         utime $dir/file2 > /dev/null || error "utime (2)"
6353         utime $dir/dir1 > /dev/null || error "utime (3)"
6354         utime $dir/dir2 > /dev/null || error "utime (4)"
6355         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
6356         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
6357
6358         local expected=4
6359         local nums=$($LFS find -mtime +0 $dir | wc -l)
6360
6361         [ $nums -eq $expected ] ||
6362                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
6363
6364         expected=12
6365         cmd="$LFS find -mtime 0 $dir"
6366         nums=$($cmd | wc -l)
6367         [ $nums -eq $expected ] ||
6368                 error "'$cmd' wrong: found $nums, expected $expected"
6369 }
6370 run_test 56o "check lfs find -mtime for old files"
6371
6372 test_56ob() {
6373         local dir=$DIR/$tdir
6374         local expected=1
6375         local count=0
6376
6377         # just to make sure there is something that won't be found
6378         test_mkdir $dir
6379         touch $dir/$tfile.now
6380
6381         for age in year week day hour min; do
6382                 count=$((count + 1))
6383
6384                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
6385                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
6386                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
6387
6388                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
6389                 local nums=$($cmd | wc -l)
6390                 [ $nums -eq $expected ] ||
6391                         error "'$cmd' wrong: found $nums, expected $expected"
6392
6393                 cmd="$LFS find $dir -atime $count${age:0:1}"
6394                 nums=$($cmd | wc -l)
6395                 [ $nums -eq $expected ] ||
6396                         error "'$cmd' wrong: found $nums, expected $expected"
6397         done
6398
6399         sleep 2
6400         cmd="$LFS find $dir -ctime +1s -type f"
6401         nums=$($cmd | wc -l)
6402         (( $nums == $count * 2 + 1)) ||
6403                 error "'$cmd' wrong: found $nums, expected $((count * 2 + 1))"
6404 }
6405 run_test 56ob "check lfs find -atime -mtime -ctime with units"
6406
6407 test_newerXY_base() {
6408         local x=$1
6409         local y=$2
6410         local dir=$DIR/$tdir
6411         local ref
6412         local negref
6413
6414         if [ $y == "t" ]; then
6415                 if [ $x == "b" ]; then
6416                         ref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6417                 else
6418                         ref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6419                 fi
6420         else
6421                 ref=$DIR/$tfile.newer.$x$y
6422                 touch $ref || error "touch $ref failed"
6423         fi
6424
6425         echo "before = $ref"
6426         sleep 2
6427         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6428         sleep 2
6429         if [ $y == "t" ]; then
6430                 if [ $x == "b" ]; then
6431                         negref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6432                 else
6433                         negref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6434                 fi
6435         else
6436                 negref=$DIR/$tfile.negnewer.$x$y
6437                 touch $negref || error "touch $negref failed"
6438         fi
6439
6440         echo "after = $negref"
6441         local cmd="$LFS find $dir -newer$x$y $ref"
6442         local nums=$(eval $cmd | wc -l)
6443         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
6444
6445         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6446                 error "'$cmd' wrong: found $nums newer, expected $expected"  ; }
6447
6448         cmd="$LFS find $dir ! -newer$x$y $negref"
6449         nums=$(eval $cmd | wc -l)
6450         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6451                 error "'$cmd' wrong: found $nums older, expected $expected"  ; }
6452
6453         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
6454         nums=$(eval $cmd | wc -l)
6455         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6456                 error "'$cmd' wrong: found $nums between, expected $expected"; }
6457
6458         rm -rf $DIR/*
6459 }
6460
6461 test_56oc() {
6462         test_newerXY_base "a" "a"
6463         test_newerXY_base "a" "m"
6464         test_newerXY_base "a" "c"
6465         test_newerXY_base "m" "a"
6466         test_newerXY_base "m" "m"
6467         test_newerXY_base "m" "c"
6468         test_newerXY_base "c" "a"
6469         test_newerXY_base "c" "m"
6470         test_newerXY_base "c" "c"
6471
6472         [[ -n "$sles_version" ]] &&
6473                 echo "skip timestamp tests on SLES, LU-13665" && return 0
6474
6475         test_newerXY_base "a" "t"
6476         test_newerXY_base "m" "t"
6477         test_newerXY_base "c" "t"
6478
6479         [[ $MDS1_VERSION -lt $(version_code 2.13.54) ||
6480            $CLIENT_VERSION -lt $(version_code 2.13.54) ]] &&
6481                 ! btime_supported && echo "btime unsupported" && return 0
6482
6483         test_newerXY_base "b" "b"
6484         test_newerXY_base "b" "t"
6485 }
6486 run_test 56oc "check lfs find -newerXY work"
6487
6488 btime_supported() {
6489         local dir=$DIR/$tdir
6490         local rc
6491
6492         mkdir -p $dir
6493         touch $dir/$tfile
6494         $LFS find $dir -btime -1d -type f
6495         rc=$?
6496         rm -rf $dir
6497         return $rc
6498 }
6499
6500 test_56od() {
6501         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
6502                 ! btime_supported && skip "btime unsupported on MDS"
6503
6504         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6505                 ! btime_supported && skip "btime unsupported on clients"
6506
6507         local dir=$DIR/$tdir
6508         local ref=$DIR/$tfile.ref
6509         local negref=$DIR/$tfile.negref
6510
6511         mkdir $dir || error "mkdir $dir failed"
6512         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6513         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6514         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6515         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6516         touch $ref || error "touch $ref failed"
6517         # sleep 3 seconds at least
6518         sleep 3
6519
6520         local before=$(do_facet mds1 date +%s)
6521         local skew=$(($(date +%s) - before + 1))
6522
6523         if (( skew < 0 && skew > -5 )); then
6524                 sleep $((0 - skew + 1))
6525                 skew=0
6526         fi
6527
6528         # Set the dir stripe params to limit files all on MDT0,
6529         # otherwise we need to calc the max clock skew between
6530         # the client and MDTs.
6531         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6532         sleep 2
6533         touch $negref || error "touch $negref failed"
6534
6535         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6536         local nums=$($cmd | wc -l)
6537         local expected=$(((NUMFILES + 1) * NUMDIRS))
6538
6539         [ $nums -eq $expected ] ||
6540                 error "'$cmd' wrong: found $nums, expected $expected"
6541
6542         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6543         nums=$($cmd | wc -l)
6544         expected=$((NUMFILES + 1))
6545         [ $nums -eq $expected ] ||
6546                 error "'$cmd' wrong: found $nums, expected $expected"
6547
6548         [ $skew -lt 0 ] && return
6549
6550         local after=$(do_facet mds1 date +%s)
6551         local age=$((after - before + 1 + skew))
6552
6553         cmd="$LFS find $dir -btime -${age}s -type f"
6554         nums=$($cmd | wc -l)
6555         expected=$(((NUMFILES + 1) * NUMDIRS))
6556
6557         echo "Clock skew between client and server: $skew, age:$age"
6558         [ $nums -eq $expected ] ||
6559                 error "'$cmd' wrong: found $nums, expected $expected"
6560
6561         expected=$(($NUMDIRS + 1))
6562         cmd="$LFS find $dir -btime -${age}s -type d"
6563         nums=$($cmd | wc -l)
6564         [ $nums -eq $expected ] ||
6565                 error "'$cmd' wrong: found $nums, expected $expected"
6566         rm -f $ref $negref || error "Failed to remove $ref $negref"
6567 }
6568 run_test 56od "check lfs find -btime with units"
6569
6570 test_56p() {
6571         [ $RUNAS_ID -eq $UID ] &&
6572                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6573
6574         local dir=$DIR/$tdir
6575
6576         setup_56 $dir $NUMFILES $NUMDIRS
6577         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6578
6579         local expected=$NUMFILES
6580         local cmd="$LFS find -uid $RUNAS_ID $dir"
6581         local nums=$($cmd | wc -l)
6582
6583         [ $nums -eq $expected ] ||
6584                 error "'$cmd' wrong: found $nums, expected $expected"
6585
6586         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6587         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6588         nums=$($cmd | wc -l)
6589         [ $nums -eq $expected ] ||
6590                 error "'$cmd' wrong: found $nums, expected $expected"
6591 }
6592 run_test 56p "check lfs find -uid and ! -uid"
6593
6594 test_56q() {
6595         [ $RUNAS_ID -eq $UID ] &&
6596                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6597
6598         local dir=$DIR/$tdir
6599
6600         setup_56 $dir $NUMFILES $NUMDIRS
6601         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6602
6603         local expected=$NUMFILES
6604         local cmd="$LFS find -gid $RUNAS_GID $dir"
6605         local nums=$($cmd | wc -l)
6606
6607         [ $nums -eq $expected ] ||
6608                 error "'$cmd' wrong: found $nums, expected $expected"
6609
6610         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6611         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6612         nums=$($cmd | wc -l)
6613         [ $nums -eq $expected ] ||
6614                 error "'$cmd' wrong: found $nums, expected $expected"
6615 }
6616 run_test 56q "check lfs find -gid and ! -gid"
6617
6618 test_56r() {
6619         local dir=$DIR/$tdir
6620
6621         setup_56 $dir $NUMFILES $NUMDIRS
6622
6623         local expected=12
6624         local cmd="$LFS find -size 0 -type f -lazy $dir"
6625         local nums=$($cmd | wc -l)
6626
6627         [ $nums -eq $expected ] ||
6628                 error "'$cmd' wrong: found $nums, expected $expected"
6629         cmd="$LFS find -size 0 -type f $dir"
6630         nums=$($cmd | wc -l)
6631         [ $nums -eq $expected ] ||
6632                 error "'$cmd' wrong: found $nums, expected $expected"
6633
6634         expected=0
6635         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6636         nums=$($cmd | wc -l)
6637         [ $nums -eq $expected ] ||
6638                 error "'$cmd' wrong: found $nums, expected $expected"
6639         cmd="$LFS find ! -size 0 -type f $dir"
6640         nums=$($cmd | wc -l)
6641         [ $nums -eq $expected ] ||
6642                 error "'$cmd' wrong: found $nums, expected $expected"
6643
6644         echo "test" > $dir/$tfile
6645         echo "test2" > $dir/$tfile.2 && sync
6646         expected=1
6647         cmd="$LFS find -size 5 -type f -lazy $dir"
6648         nums=$($cmd | wc -l)
6649         [ $nums -eq $expected ] ||
6650                 error "'$cmd' wrong: found $nums, expected $expected"
6651         cmd="$LFS find -size 5 -type f $dir"
6652         nums=$($cmd | wc -l)
6653         [ $nums -eq $expected ] ||
6654                 error "'$cmd' wrong: found $nums, expected $expected"
6655
6656         expected=1
6657         cmd="$LFS find -size +5 -type f -lazy $dir"
6658         nums=$($cmd | wc -l)
6659         [ $nums -eq $expected ] ||
6660                 error "'$cmd' wrong: found $nums, expected $expected"
6661         cmd="$LFS find -size +5 -type f $dir"
6662         nums=$($cmd | wc -l)
6663         [ $nums -eq $expected ] ||
6664                 error "'$cmd' wrong: found $nums, expected $expected"
6665
6666         expected=2
6667         cmd="$LFS find -size +0 -type f -lazy $dir"
6668         nums=$($cmd | wc -l)
6669         [ $nums -eq $expected ] ||
6670                 error "'$cmd' wrong: found $nums, expected $expected"
6671         cmd="$LFS find -size +0 -type f $dir"
6672         nums=$($cmd | wc -l)
6673         [ $nums -eq $expected ] ||
6674                 error "'$cmd' wrong: found $nums, expected $expected"
6675
6676         expected=2
6677         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6678         nums=$($cmd | wc -l)
6679         [ $nums -eq $expected ] ||
6680                 error "'$cmd' wrong: found $nums, expected $expected"
6681         cmd="$LFS find ! -size -5 -type f $dir"
6682         nums=$($cmd | wc -l)
6683         [ $nums -eq $expected ] ||
6684                 error "'$cmd' wrong: found $nums, expected $expected"
6685
6686         expected=12
6687         cmd="$LFS find -size -5 -type f -lazy $dir"
6688         nums=$($cmd | wc -l)
6689         [ $nums -eq $expected ] ||
6690                 error "'$cmd' wrong: found $nums, expected $expected"
6691         cmd="$LFS find -size -5 -type f $dir"
6692         nums=$($cmd | wc -l)
6693         [ $nums -eq $expected ] ||
6694                 error "'$cmd' wrong: found $nums, expected $expected"
6695 }
6696 run_test 56r "check lfs find -size works"
6697
6698 test_56ra_sub() {
6699         local expected=$1
6700         local glimpses=$2
6701         local cmd="$3"
6702
6703         cancel_lru_locks $OSC
6704
6705         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6706         local nums=$($cmd | wc -l)
6707
6708         [ $nums -eq $expected ] ||
6709                 error "'$cmd' wrong: found $nums, expected $expected"
6710
6711         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6712
6713         if (( rpcs_before + glimpses != rpcs_after )); then
6714                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6715                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6716
6717                 if [[ $glimpses == 0 ]]; then
6718                         error "'$cmd' should not send glimpse RPCs to OST"
6719                 else
6720                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6721                 fi
6722         fi
6723 }
6724
6725 test_56ra() {
6726         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6727                 skip "MDS < 2.12.58 doesn't return LSOM data"
6728         local dir=$DIR/$tdir
6729         local old_agl=$($LCTL get_param -n llite.*.statahead_agl)
6730
6731         [[ $OSC == "mdc" ]] && skip "statahead not needed for DoM files"
6732
6733         # statahead_agl may cause extra glimpse which confuses results. LU-13017
6734         $LCTL set_param -n llite.*.statahead_agl=0
6735         stack_trap "$LCTL set_param -n llite.*.statahead_agl=$old_agl"
6736
6737         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6738         # open and close all files to ensure LSOM is updated
6739         cancel_lru_locks $OSC
6740         find $dir -type f | xargs cat > /dev/null
6741
6742         #   expect_found  glimpse_rpcs  command_to_run
6743         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6744         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6745         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6746         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6747
6748         echo "test" > $dir/$tfile
6749         echo "test2" > $dir/$tfile.2 && sync
6750         cancel_lru_locks $OSC
6751         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6752
6753         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6754         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6755         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6756         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6757
6758         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6759         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6760         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6761         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6762         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6763         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6764 }
6765 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6766
6767 test_56rb() {
6768         local dir=$DIR/$tdir
6769         local tmp=$TMP/$tfile.log
6770         local mdt_idx;
6771
6772         test_mkdir -p $dir || error "failed to mkdir $dir"
6773         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6774                 error "failed to setstripe $dir/$tfile"
6775         mdt_idx=$($LFS getdirstripe -i $dir)
6776         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6777
6778         stack_trap "rm -f $tmp" EXIT
6779         $LFS find --size +100K --ost 0 $dir |& tee $tmp
6780         ! grep -q obd_uuid $tmp ||
6781                 error "failed to find --size +100K --ost 0 $dir"
6782         $LFS find --size +100K --mdt $mdt_idx $dir |& tee $tmp
6783         ! grep -q obd_uuid $tmp ||
6784                 error "failed to find --size +100K --mdt $mdt_idx $dir"
6785 }
6786 run_test 56rb "check lfs find --size --ost/--mdt works"
6787
6788 test_56rc() {
6789         (( MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
6790         local dir=$DIR/$tdir
6791         local found
6792
6793         test_mkdir -c 2 -H all_char $dir || error "failed to mkdir $dir"
6794         $LFS mkdir -c 2 --mdt-hash all_char $dir/$tdir-all{1..10}
6795         (( $MDSCOUNT > 2 )) &&
6796                 $LFS mkdir -c 3 --mdt-hash fnv_1a_64 $dir/$tdir-fnv{1..10}
6797         mkdir $dir/$tdir-{1..10}
6798         touch $dir/$tfile-{1..10}
6799
6800         found=$($LFS find $dir --mdt-count 2 | wc -l)
6801         expect=11
6802         (( $found == $expect )) || error "found $found 2-stripe, expect $expect"
6803
6804         found=$($LFS find $dir -T +1 | wc -l)
6805         (( $MDSCOUNT > 2 )) && expect=$((expect + 10))
6806         (( $found == $expect )) || error "found $found 2+stripe, expect $expect"
6807
6808         found=$($LFS find $dir --mdt-hash all_char | wc -l)
6809         expect=11
6810         (( $found == $expect )) || error "found $found all_char, expect $expect"
6811
6812         found=$($LFS find $dir --mdt-hash fnv_1a_64 | wc -l)
6813         (( $MDSCOUNT > 2 )) && expect=10 || expect=0
6814         (( $found == $expect )) || error "found $found all_char, expect $expect"
6815 }
6816 run_test 56rc "check lfs find --mdt-count/--mdt-hash works"
6817
6818 test_56s() { # LU-611 #LU-9369
6819         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6820
6821         local dir=$DIR/$tdir
6822         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6823
6824         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6825         for i in $(seq $NUMDIRS); do
6826                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6827         done
6828
6829         local expected=$NUMDIRS
6830         local cmd="$LFS find -c $OSTCOUNT $dir"
6831         local nums=$($cmd | wc -l)
6832
6833         [ $nums -eq $expected ] || {
6834                 $LFS getstripe -R $dir
6835                 error "'$cmd' wrong: found $nums, expected $expected"
6836         }
6837
6838         expected=$((NUMDIRS + onestripe))
6839         cmd="$LFS find -stripe-count +0 -type f $dir"
6840         nums=$($cmd | wc -l)
6841         [ $nums -eq $expected ] || {
6842                 $LFS getstripe -R $dir
6843                 error "'$cmd' wrong: found $nums, expected $expected"
6844         }
6845
6846         expected=$onestripe
6847         cmd="$LFS find -stripe-count 1 -type f $dir"
6848         nums=$($cmd | wc -l)
6849         [ $nums -eq $expected ] || {
6850                 $LFS getstripe -R $dir
6851                 error "'$cmd' wrong: found $nums, expected $expected"
6852         }
6853
6854         cmd="$LFS find -stripe-count -2 -type f $dir"
6855         nums=$($cmd | wc -l)
6856         [ $nums -eq $expected ] || {
6857                 $LFS getstripe -R $dir
6858                 error "'$cmd' wrong: found $nums, expected $expected"
6859         }
6860
6861         expected=0
6862         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6863         nums=$($cmd | wc -l)
6864         [ $nums -eq $expected ] || {
6865                 $LFS getstripe -R $dir
6866                 error "'$cmd' wrong: found $nums, expected $expected"
6867         }
6868 }
6869 run_test 56s "check lfs find -stripe-count works"
6870
6871 test_56t() { # LU-611 #LU-9369
6872         local dir=$DIR/$tdir
6873
6874         setup_56 $dir 0 $NUMDIRS
6875         for i in $(seq $NUMDIRS); do
6876                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6877         done
6878
6879         local expected=$NUMDIRS
6880         local cmd="$LFS find -S 8M $dir"
6881         local nums=$($cmd | wc -l)
6882
6883         [ $nums -eq $expected ] || {
6884                 $LFS getstripe -R $dir
6885                 error "'$cmd' wrong: found $nums, expected $expected"
6886         }
6887         rm -rf $dir
6888
6889         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6890
6891         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6892
6893         expected=$(((NUMDIRS + 1) * NUMFILES))
6894         cmd="$LFS find -stripe-size 512k -type f $dir"
6895         nums=$($cmd | wc -l)
6896         [ $nums -eq $expected ] ||
6897                 error "'$cmd' wrong: found $nums, expected $expected"
6898
6899         cmd="$LFS find -stripe-size +320k -type f $dir"
6900         nums=$($cmd | wc -l)
6901         [ $nums -eq $expected ] ||
6902                 error "'$cmd' wrong: found $nums, expected $expected"
6903
6904         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6905         cmd="$LFS find -stripe-size +200k -type f $dir"
6906         nums=$($cmd | wc -l)
6907         [ $nums -eq $expected ] ||
6908                 error "'$cmd' wrong: found $nums, expected $expected"
6909
6910         cmd="$LFS find -stripe-size -640k -type f $dir"
6911         nums=$($cmd | wc -l)
6912         [ $nums -eq $expected ] ||
6913                 error "'$cmd' wrong: found $nums, expected $expected"
6914
6915         expected=4
6916         cmd="$LFS find -stripe-size 256k -type f $dir"
6917         nums=$($cmd | wc -l)
6918         [ $nums -eq $expected ] ||
6919                 error "'$cmd' wrong: found $nums, expected $expected"
6920
6921         cmd="$LFS find -stripe-size -320k -type f $dir"
6922         nums=$($cmd | wc -l)
6923         [ $nums -eq $expected ] ||
6924                 error "'$cmd' wrong: found $nums, expected $expected"
6925
6926         expected=0
6927         cmd="$LFS find -stripe-size 1024k -type f $dir"
6928         nums=$($cmd | wc -l)
6929         [ $nums -eq $expected ] ||
6930                 error "'$cmd' wrong: found $nums, expected $expected"
6931 }
6932 run_test 56t "check lfs find -stripe-size works"
6933
6934 test_56u() { # LU-611
6935         local dir=$DIR/$tdir
6936
6937         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6938
6939         if [[ $OSTCOUNT -gt 1 ]]; then
6940                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6941                 onestripe=4
6942         else
6943                 onestripe=0
6944         fi
6945
6946         local expected=$(((NUMDIRS + 1) * NUMFILES))
6947         local cmd="$LFS find -stripe-index 0 -type f $dir"
6948         local nums=$($cmd | wc -l)
6949
6950         [ $nums -eq $expected ] ||
6951                 error "'$cmd' wrong: found $nums, expected $expected"
6952
6953         expected=$onestripe
6954         cmd="$LFS find -stripe-index 1 -type f $dir"
6955         nums=$($cmd | wc -l)
6956         [ $nums -eq $expected ] ||
6957                 error "'$cmd' wrong: found $nums, expected $expected"
6958
6959         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6960         nums=$($cmd | wc -l)
6961         [ $nums -eq $expected ] ||
6962                 error "'$cmd' wrong: found $nums, expected $expected"
6963
6964         expected=0
6965         # This should produce an error and not return any files
6966         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6967         nums=$($cmd 2>/dev/null | wc -l)
6968         [ $nums -eq $expected ] ||
6969                 error "'$cmd' wrong: found $nums, expected $expected"
6970
6971         if [[ $OSTCOUNT -gt 1 ]]; then
6972                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6973                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6974                 nums=$($cmd | wc -l)
6975                 [ $nums -eq $expected ] ||
6976                         error "'$cmd' wrong: found $nums, expected $expected"
6977         fi
6978 }
6979 run_test 56u "check lfs find -stripe-index works"
6980
6981 test_56v() {
6982         local mdt_idx=0
6983         local dir=$DIR/$tdir
6984
6985         setup_56 $dir $NUMFILES $NUMDIRS
6986
6987         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6988         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6989
6990         for file in $($LFS find -m $UUID $dir); do
6991                 file_midx=$($LFS getstripe -m $file)
6992                 [ $file_midx -eq $mdt_idx ] ||
6993                         error "lfs find -m $UUID != getstripe -m $file_midx"
6994         done
6995 }
6996 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6997
6998 test_56w() {
6999         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7000         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7001
7002         local dir=$DIR/$tdir
7003
7004         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
7005
7006         local stripe_size=$($LFS getstripe -S -d $dir) ||
7007                 error "$LFS getstripe -S -d $dir failed"
7008         stripe_size=${stripe_size%% *}
7009
7010         local file_size=$((stripe_size * OSTCOUNT))
7011         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
7012         local required_space=$((file_num * file_size))
7013         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
7014                            head -n1)
7015         [[ $free_space -le $((required_space / 1024)) ]] &&
7016                 skip_env "need $required_space, have $free_space kbytes"
7017
7018         local dd_bs=65536
7019         local dd_count=$((file_size / dd_bs))
7020
7021         # write data into the files
7022         local i
7023         local j
7024         local file
7025
7026         for i in $(seq $NUMFILES); do
7027                 file=$dir/file$i
7028                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7029                         error "write data into $file failed"
7030         done
7031         for i in $(seq $NUMDIRS); do
7032                 for j in $(seq $NUMFILES); do
7033                         file=$dir/dir$i/file$j
7034                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7035                                 error "write data into $file failed"
7036                 done
7037         done
7038
7039         # $LFS_MIGRATE will fail if hard link migration is unsupported
7040         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
7041                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
7042                         error "creating links to $dir/dir1/file1 failed"
7043         fi
7044
7045         local expected=-1
7046
7047         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
7048
7049         # lfs_migrate file
7050         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
7051
7052         echo "$cmd"
7053         eval $cmd || error "$cmd failed"
7054
7055         check_stripe_count $dir/file1 $expected
7056
7057         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
7058         then
7059                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
7060                 # OST 1 if it is on OST 0. This file is small enough to
7061                 # be on only one stripe.
7062                 file=$dir/migr_1_ost
7063                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
7064                         error "write data into $file failed"
7065                 local obdidx=$($LFS getstripe -i $file)
7066                 local oldmd5=$(md5sum $file)
7067                 local newobdidx=0
7068
7069                 [[ $obdidx -eq 0 ]] && newobdidx=1
7070                 cmd="$LFS migrate -i $newobdidx $file"
7071                 echo $cmd
7072                 eval $cmd || error "$cmd failed"
7073
7074                 local realobdix=$($LFS getstripe -i $file)
7075                 local newmd5=$(md5sum $file)
7076
7077                 [[ $newobdidx -ne $realobdix ]] &&
7078                         error "new OST is different (was=$obdidx, "\
7079                               "wanted=$newobdidx, got=$realobdix)"
7080                 [[ "$oldmd5" != "$newmd5" ]] &&
7081                         error "md5sum differ: $oldmd5, $newmd5"
7082         fi
7083
7084         # lfs_migrate dir
7085         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
7086         echo "$cmd"
7087         eval $cmd || error "$cmd failed"
7088
7089         for j in $(seq $NUMFILES); do
7090                 check_stripe_count $dir/dir1/file$j $expected
7091         done
7092
7093         # lfs_migrate works with lfs find
7094         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
7095              $LFS_MIGRATE -y -c $expected"
7096         echo "$cmd"
7097         eval $cmd || error "$cmd failed"
7098
7099         for i in $(seq 2 $NUMFILES); do
7100                 check_stripe_count $dir/file$i $expected
7101         done
7102         for i in $(seq 2 $NUMDIRS); do
7103                 for j in $(seq $NUMFILES); do
7104                 check_stripe_count $dir/dir$i/file$j $expected
7105                 done
7106         done
7107 }
7108 run_test 56w "check lfs_migrate -c stripe_count works"
7109
7110 test_56wb() {
7111         local file1=$DIR/$tdir/file1
7112         local create_pool=false
7113         local initial_pool=$($LFS getstripe -p $DIR)
7114         local pool_list=()
7115         local pool=""
7116
7117         echo -n "Creating test dir..."
7118         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7119         echo "done."
7120
7121         echo -n "Creating test file..."
7122         touch $file1 || error "cannot create file"
7123         echo "done."
7124
7125         echo -n "Detecting existing pools..."
7126         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
7127
7128         if [ ${#pool_list[@]} -gt 0 ]; then
7129                 echo "${pool_list[@]}"
7130                 for thispool in "${pool_list[@]}"; do
7131                         if [[ -z "$initial_pool" ||
7132                               "$initial_pool" != "$thispool" ]]; then
7133                                 pool="$thispool"
7134                                 echo "Using existing pool '$pool'"
7135                                 break
7136                         fi
7137                 done
7138         else
7139                 echo "none detected."
7140         fi
7141         if [ -z "$pool" ]; then
7142                 pool=${POOL:-testpool}
7143                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
7144                 echo -n "Creating pool '$pool'..."
7145                 create_pool=true
7146                 pool_add $pool &> /dev/null ||
7147                         error "pool_add failed"
7148                 echo "done."
7149
7150                 echo -n "Adding target to pool..."
7151                 pool_add_targets $pool 0 0 1 &> /dev/null ||
7152                         error "pool_add_targets failed"
7153                 echo "done."
7154         fi
7155
7156         echo -n "Setting pool using -p option..."
7157         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
7158                 error "migrate failed rc = $?"
7159         echo "done."
7160
7161         echo -n "Verifying test file is in pool after migrating..."
7162         [ "$($LFS getstripe -p $file1)" = $pool ] ||
7163                 error "file was not migrated to pool $pool"
7164         echo "done."
7165
7166         echo -n "Removing test file from pool '$pool'..."
7167         # "lfs migrate $file" won't remove the file from the pool
7168         # until some striping information is changed.
7169         $LFS migrate -c 1 $file1 &> /dev/null ||
7170                 error "cannot remove from pool"
7171         [ "$($LFS getstripe -p $file1)" ] &&
7172                 error "pool still set"
7173         echo "done."
7174
7175         echo -n "Setting pool using --pool option..."
7176         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
7177                 error "migrate failed rc = $?"
7178         echo "done."
7179
7180         # Clean up
7181         rm -f $file1
7182         if $create_pool; then
7183                 destroy_test_pools 2> /dev/null ||
7184                         error "destroy test pools failed"
7185         fi
7186 }
7187 run_test 56wb "check lfs_migrate pool support"
7188
7189 test_56wc() {
7190         local file1="$DIR/$tdir/file1"
7191         local parent_ssize
7192         local parent_scount
7193         local cur_ssize
7194         local cur_scount
7195         local orig_ssize
7196
7197         echo -n "Creating test dir..."
7198         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7199         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
7200                 error "cannot set stripe by '-S 1M -c 1'"
7201         echo "done"
7202
7203         echo -n "Setting initial stripe for test file..."
7204         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
7205                 error "cannot set stripe"
7206         cur_ssize=$($LFS getstripe -S "$file1")
7207         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
7208         echo "done."
7209
7210         # File currently set to -S 512K -c 1
7211
7212         # Ensure -c and -S options are rejected when -R is set
7213         echo -n "Verifying incompatible options are detected..."
7214         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
7215                 error "incompatible -c and -R options not detected"
7216         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
7217                 error "incompatible -S and -R options not detected"
7218         echo "done."
7219
7220         # Ensure unrecognized options are passed through to 'lfs migrate'
7221         echo -n "Verifying -S option is passed through to lfs migrate..."
7222         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
7223                 error "migration failed"
7224         cur_ssize=$($LFS getstripe -S "$file1")
7225         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
7226         echo "done."
7227
7228         # File currently set to -S 1M -c 1
7229
7230         # Ensure long options are supported
7231         echo -n "Verifying long options supported..."
7232         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
7233                 error "long option without argument not supported"
7234         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
7235                 error "long option with argument not supported"
7236         cur_ssize=$($LFS getstripe -S "$file1")
7237         [ $cur_ssize -eq 524288 ] ||
7238                 error "migrate --stripe-size $cur_ssize != 524288"
7239         echo "done."
7240
7241         # File currently set to -S 512K -c 1
7242
7243         if [ "$OSTCOUNT" -gt 1 ]; then
7244                 echo -n "Verifying explicit stripe count can be set..."
7245                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
7246                         error "migrate failed"
7247                 cur_scount=$($LFS getstripe -c "$file1")
7248                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
7249                 echo "done."
7250         fi
7251
7252         # File currently set to -S 512K -c 1 or -S 512K -c 2
7253
7254         # Ensure parent striping is used if -R is set, and no stripe
7255         # count or size is specified
7256         echo -n "Setting stripe for parent directory..."
7257         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7258                 error "cannot set stripe '-S 2M -c 1'"
7259         echo "done."
7260
7261         echo -n "Verifying restripe option uses parent stripe settings..."
7262         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
7263         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
7264         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
7265                 error "migrate failed"
7266         cur_ssize=$($LFS getstripe -S "$file1")
7267         [ $cur_ssize -eq $parent_ssize ] ||
7268                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
7269         cur_scount=$($LFS getstripe -c "$file1")
7270         [ $cur_scount -eq $parent_scount ] ||
7271                 error "migrate -R stripe_count $cur_scount != $parent_scount"
7272         echo "done."
7273
7274         # File currently set to -S 1M -c 1
7275
7276         # Ensure striping is preserved if -R is not set, and no stripe
7277         # count or size is specified
7278         echo -n "Verifying striping size preserved when not specified..."
7279         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
7280         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7281                 error "cannot set stripe on parent directory"
7282         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7283                 error "migrate failed"
7284         cur_ssize=$($LFS getstripe -S "$file1")
7285         [ $cur_ssize -eq $orig_ssize ] ||
7286                 error "migrate by default $cur_ssize != $orig_ssize"
7287         echo "done."
7288
7289         # Ensure file name properly detected when final option has no argument
7290         echo -n "Verifying file name properly detected..."
7291         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7292                 error "file name interpreted as option argument"
7293         echo "done."
7294
7295         # Clean up
7296         rm -f "$file1"
7297 }
7298 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
7299
7300 test_56wd() {
7301         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7302
7303         local file1=$DIR/$tdir/file1
7304
7305         echo -n "Creating test dir..."
7306         test_mkdir $DIR/$tdir || error "cannot create dir"
7307         echo "done."
7308
7309         echo -n "Creating test file..."
7310         touch $file1
7311         echo "done."
7312
7313         # Ensure 'lfs migrate' will fail by using a non-existent option,
7314         # and make sure rsync is not called to recover
7315         echo -n "Make sure --no-rsync option works..."
7316         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
7317                 grep -q 'refusing to fall back to rsync' ||
7318                 error "rsync was called with --no-rsync set"
7319         echo "done."
7320
7321         # Ensure rsync is called without trying 'lfs migrate' first
7322         echo -n "Make sure --rsync option works..."
7323         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
7324                 grep -q 'falling back to rsync' &&
7325                 error "lfs migrate was called with --rsync set"
7326         echo "done."
7327
7328         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
7329         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
7330                 grep -q 'at the same time' ||
7331                 error "--rsync and --no-rsync accepted concurrently"
7332         echo "done."
7333
7334         # Clean up
7335         rm -f $file1
7336 }
7337 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
7338
7339 test_56we() {
7340         local td=$DIR/$tdir
7341         local tf=$td/$tfile
7342
7343         test_mkdir $td || error "cannot create $td"
7344         touch $tf || error "cannot touch $tf"
7345
7346         echo -n "Make sure --non-direct|-D works..."
7347         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
7348                 grep -q "lfs migrate --non-direct" ||
7349                 error "--non-direct option cannot work correctly"
7350         $LFS_MIGRATE -y -D -v $tf 2>&1 |
7351                 grep -q "lfs migrate -D" ||
7352                 error "-D option cannot work correctly"
7353         echo "done."
7354 }
7355 run_test 56we "check lfs_migrate --non-direct|-D support"
7356
7357 test_56x() {
7358         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7359         check_swap_layouts_support
7360
7361         local dir=$DIR/$tdir
7362         local ref1=/etc/passwd
7363         local file1=$dir/file1
7364
7365         test_mkdir $dir || error "creating dir $dir"
7366         $LFS setstripe -c 2 $file1
7367         cp $ref1 $file1
7368         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
7369         stripe=$($LFS getstripe -c $file1)
7370         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7371         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7372
7373         # clean up
7374         rm -f $file1
7375 }
7376 run_test 56x "lfs migration support"
7377
7378 test_56xa() {
7379         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7380         check_swap_layouts_support
7381
7382         local dir=$DIR/$tdir/$testnum
7383
7384         test_mkdir -p $dir
7385
7386         local ref1=/etc/passwd
7387         local file1=$dir/file1
7388
7389         $LFS setstripe -c 2 $file1
7390         cp $ref1 $file1
7391         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
7392
7393         local stripe=$($LFS getstripe -c $file1)
7394
7395         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7396         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7397
7398         # clean up
7399         rm -f $file1
7400 }
7401 run_test 56xa "lfs migration --block support"
7402
7403 check_migrate_links() {
7404         local dir="$1"
7405         local file1="$dir/file1"
7406         local begin="$2"
7407         local count="$3"
7408         local runas="$4"
7409         local total_count=$(($begin + $count - 1))
7410         local symlink_count=10
7411         local uniq_count=10
7412
7413         if [ ! -f "$file1" ]; then
7414                 echo -n "creating initial file..."
7415                 $LFS setstripe -c 1 -S "512k" "$file1" ||
7416                         error "cannot setstripe initial file"
7417                 echo "done"
7418
7419                 echo -n "creating symlinks..."
7420                 for s in $(seq 1 $symlink_count); do
7421                         ln -s "$file1" "$dir/slink$s" ||
7422                                 error "cannot create symlinks"
7423                 done
7424                 echo "done"
7425
7426                 echo -n "creating nonlinked files..."
7427                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
7428                         error "cannot create nonlinked files"
7429                 echo "done"
7430         fi
7431
7432         # create hard links
7433         if [ ! -f "$dir/file$total_count" ]; then
7434                 echo -n "creating hard links $begin:$total_count..."
7435                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
7436                         /dev/null || error "cannot create hard links"
7437                 echo "done"
7438         fi
7439
7440         echo -n "checking number of hard links listed in xattrs..."
7441         local fid=$($LFS getstripe -F "$file1")
7442         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
7443
7444         echo "${#paths[*]}"
7445         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
7446                         skip "hard link list has unexpected size, skipping test"
7447         fi
7448         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
7449                         error "link names should exceed xattrs size"
7450         fi
7451
7452         echo -n "migrating files..."
7453         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
7454         local rc=$?
7455         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
7456         echo "done"
7457
7458         # make sure all links have been properly migrated
7459         echo -n "verifying files..."
7460         fid=$($LFS getstripe -F "$file1") ||
7461                 error "cannot get fid for file $file1"
7462         for i in $(seq 2 $total_count); do
7463                 local fid2=$($LFS getstripe -F $dir/file$i)
7464
7465                 [ "$fid2" == "$fid" ] ||
7466                         error "migrated hard link has mismatched FID"
7467         done
7468
7469         # make sure hard links were properly detected, and migration was
7470         # performed only once for the entire link set; nonlinked files should
7471         # also be migrated
7472         local actual=$(grep -c 'done' <<< "$migrate_out")
7473         local expected=$(($uniq_count + 1))
7474
7475         [ "$actual" -eq  "$expected" ] ||
7476                 error "hard links individually migrated ($actual != $expected)"
7477
7478         # make sure the correct number of hard links are present
7479         local hardlinks=$(stat -c '%h' "$file1")
7480
7481         [ $hardlinks -eq $total_count ] ||
7482                 error "num hard links $hardlinks != $total_count"
7483         echo "done"
7484
7485         return 0
7486 }
7487
7488 test_56xb() {
7489         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
7490                 skip "Need MDS version at least 2.10.55"
7491
7492         local dir="$DIR/$tdir"
7493
7494         test_mkdir "$dir" || error "cannot create dir $dir"
7495
7496         echo "testing lfs migrate mode when all links fit within xattrs"
7497         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
7498
7499         echo "testing rsync mode when all links fit within xattrs"
7500         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
7501
7502         echo "testing lfs migrate mode when all links do not fit within xattrs"
7503         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
7504
7505         echo "testing rsync mode when all links do not fit within xattrs"
7506         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
7507
7508         chown -R $RUNAS_ID $dir
7509         echo "testing non-root lfs migrate mode when not all links are in xattr"
7510         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
7511
7512         # clean up
7513         rm -rf $dir
7514 }
7515 run_test 56xb "lfs migration hard link support"
7516
7517 test_56xc() {
7518         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7519
7520         local dir="$DIR/$tdir"
7521
7522         test_mkdir "$dir" || error "cannot create dir $dir"
7523
7524         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7525         echo -n "Setting initial stripe for 20MB test file..."
7526         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7527                 error "cannot setstripe 20MB file"
7528         echo "done"
7529         echo -n "Sizing 20MB test file..."
7530         $TRUNCATE "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7531         echo "done"
7532         echo -n "Verifying small file autostripe count is 1..."
7533         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7534                 error "cannot migrate 20MB file"
7535         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7536                 error "cannot get stripe for $dir/20mb"
7537         [ $stripe_count -eq 1 ] ||
7538                 error "unexpected stripe count $stripe_count for 20MB file"
7539         rm -f "$dir/20mb"
7540         echo "done"
7541
7542         # Test 2: File is small enough to fit within the available space on
7543         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7544         # have at least an additional 1KB for each desired stripe for test 3
7545         echo -n "Setting stripe for 1GB test file..."
7546         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7547         echo "done"
7548         echo -n "Sizing 1GB test file..."
7549         # File size is 1GB + 3KB
7550         $TRUNCATE "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7551         echo "done"
7552
7553         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7554         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7555         if (( avail > 524288 * OSTCOUNT )); then
7556                 echo -n "Migrating 1GB file..."
7557                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7558                         error "cannot migrate 1GB file"
7559                 echo "done"
7560                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7561                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7562                         error "cannot getstripe for 1GB file"
7563                 [ $stripe_count -eq 2 ] ||
7564                         error "unexpected stripe count $stripe_count != 2"
7565                 echo "done"
7566         fi
7567
7568         # Test 3: File is too large to fit within the available space on
7569         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7570         if [ $OSTCOUNT -ge 3 ]; then
7571                 # The required available space is calculated as
7572                 # file size (1GB + 3KB) / OST count (3).
7573                 local kb_per_ost=349526
7574
7575                 echo -n "Migrating 1GB file with limit..."
7576                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7577                         error "cannot migrate 1GB file with limit"
7578                 echo "done"
7579
7580                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7581                 echo -n "Verifying 1GB autostripe count with limited space..."
7582                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7583                         error "unexpected stripe count $stripe_count (min 3)"
7584                 echo "done"
7585         fi
7586
7587         # clean up
7588         rm -rf $dir
7589 }
7590 run_test 56xc "lfs migration autostripe"
7591
7592 test_56xd() {
7593         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7594
7595         local dir=$DIR/$tdir
7596         local f_mgrt=$dir/$tfile.mgrt
7597         local f_yaml=$dir/$tfile.yaml
7598         local f_copy=$dir/$tfile.copy
7599         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7600         local layout_copy="-c 2 -S 2M -i 1"
7601         local yamlfile=$dir/yamlfile
7602         local layout_before;
7603         local layout_after;
7604
7605         test_mkdir "$dir" || error "cannot create dir $dir"
7606         $LFS setstripe $layout_yaml $f_yaml ||
7607                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7608         $LFS getstripe --yaml $f_yaml > $yamlfile
7609         $LFS setstripe $layout_copy $f_copy ||
7610                 error "cannot setstripe $f_copy with layout $layout_copy"
7611         touch $f_mgrt
7612         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7613
7614         # 1. test option --yaml
7615         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7616                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7617         layout_before=$(get_layout_param $f_yaml)
7618         layout_after=$(get_layout_param $f_mgrt)
7619         [ "$layout_after" == "$layout_before" ] ||
7620                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7621
7622         # 2. test option --copy
7623         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7624                 error "cannot migrate $f_mgrt with --copy $f_copy"
7625         layout_before=$(get_layout_param $f_copy)
7626         layout_after=$(get_layout_param $f_mgrt)
7627         [ "$layout_after" == "$layout_before" ] ||
7628                 error "lfs_migrate --copy: $layout_after != $layout_before"
7629 }
7630 run_test 56xd "check lfs_migrate --yaml and --copy support"
7631
7632 test_56xe() {
7633         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7634
7635         local dir=$DIR/$tdir
7636         local f_comp=$dir/$tfile
7637         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7638         local layout_before=""
7639         local layout_after=""
7640
7641         test_mkdir "$dir" || error "cannot create dir $dir"
7642         $LFS setstripe $layout $f_comp ||
7643                 error "cannot setstripe $f_comp with layout $layout"
7644         layout_before=$(get_layout_param $f_comp)
7645         dd if=/dev/zero of=$f_comp bs=1M count=4
7646
7647         # 1. migrate a comp layout file by lfs_migrate
7648         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7649         layout_after=$(get_layout_param $f_comp)
7650         [ "$layout_before" == "$layout_after" ] ||
7651                 error "lfs_migrate: $layout_before != $layout_after"
7652
7653         # 2. migrate a comp layout file by lfs migrate
7654         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7655         layout_after=$(get_layout_param $f_comp)
7656         [ "$layout_before" == "$layout_after" ] ||
7657                 error "lfs migrate: $layout_before != $layout_after"
7658 }
7659 run_test 56xe "migrate a composite layout file"
7660
7661 test_56xf() {
7662         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7663
7664         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7665                 skip "Need server version at least 2.13.53"
7666
7667         local dir=$DIR/$tdir
7668         local f_comp=$dir/$tfile
7669         local layout="-E 1M -c1 -E -1 -c2"
7670         local fid_before=""
7671         local fid_after=""
7672
7673         test_mkdir "$dir" || error "cannot create dir $dir"
7674         $LFS setstripe $layout $f_comp ||
7675                 error "cannot setstripe $f_comp with layout $layout"
7676         fid_before=$($LFS getstripe --fid $f_comp)
7677         dd if=/dev/zero of=$f_comp bs=1M count=4
7678
7679         # 1. migrate a comp layout file to a comp layout
7680         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7681         fid_after=$($LFS getstripe --fid $f_comp)
7682         [ "$fid_before" == "$fid_after" ] ||
7683                 error "comp-to-comp migrate: $fid_before != $fid_after"
7684
7685         # 2. migrate a comp layout file to a plain layout
7686         $LFS migrate -c2 $f_comp ||
7687                 error "cannot migrate $f_comp by lfs migrate"
7688         fid_after=$($LFS getstripe --fid $f_comp)
7689         [ "$fid_before" == "$fid_after" ] ||
7690                 error "comp-to-plain migrate: $fid_before != $fid_after"
7691
7692         # 3. migrate a plain layout file to a comp layout
7693         $LFS migrate $layout $f_comp ||
7694                 error "cannot migrate $f_comp by lfs migrate"
7695         fid_after=$($LFS getstripe --fid $f_comp)
7696         [ "$fid_before" == "$fid_after" ] ||
7697                 error "plain-to-comp migrate: $fid_before != $fid_after"
7698 }
7699 run_test 56xf "FID is not lost during migration of a composite layout file"
7700
7701 check_file_ost_range() {
7702         local file="$1"
7703         shift
7704         local range="$*"
7705         local -a file_range
7706         local idx
7707
7708         file_range=($($LFS getstripe -y "$file" |
7709                 awk '/l_ost_idx:/ { print $NF }'))
7710
7711         if [[ "${#file_range[@]}" = 0 ]]; then
7712                 echo "No osts found for $file"
7713                 return 1
7714         fi
7715
7716         for idx in "${file_range[@]}"; do
7717                 [[ " $range " =~ " $idx " ]] ||
7718                         return 1
7719         done
7720
7721         return 0
7722 }
7723
7724 sub_test_56xg() {
7725         local stripe_opt="$1"
7726         local pool="$2"
7727         shift 2
7728         local pool_ostidx="$(seq $* | tr '\n' ' ')"
7729
7730         $LFS migrate $stripe_opt -p $pool $DIR/$tfile ||
7731                 error "Fail to migrate $tfile on $pool"
7732         [[ "$($LFS getstripe -p $DIR/$tfile)" = "$pool" ]] ||
7733                 error "$tfile is not in pool $pool"
7734         check_file_ost_range "$DIR/$tfile" $pool_ostidx ||
7735                 error "$tfile osts mismatch with pool $pool (osts $pool_ostidx)"
7736 }
7737
7738 test_56xg() {
7739         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7740         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
7741         [[ $MDS1_VERSION -gt $(version_code 2.14.52) ]] ||
7742                 skip "Need MDS version newer than 2.14.52"
7743
7744         local -a pool_names=("${TESTNAME}_0" "${TESTNAME}_1" "${TESTNAME}_2")
7745         local -a pool_ranges=("0 0" "1 1" "0 1")
7746
7747         # init pools
7748         for i in "${!pool_names[@]}"; do
7749                 pool_add ${pool_names[$i]} ||
7750                         error "pool_add failed (pool: ${pool_names[$i]})"
7751                 pool_add_targets ${pool_names[$i]} ${pool_ranges[$i]} ||
7752                         error "pool_add_targets failed (pool: ${pool_names[$i]})"
7753         done
7754
7755         # init the file to migrate
7756         $LFS setstripe -c1 -i1 $DIR/$tfile ||
7757                 error "Unable to create $tfile on OST1"
7758         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=4 status=none ||
7759                 error "Unable to write on $tfile"
7760
7761         echo "1. migrate $tfile on pool ${pool_names[0]}"
7762         sub_test_56xg "-c-1" "${pool_names[0]}" ${pool_ranges[0]}
7763
7764         echo "2. migrate $tfile on pool ${pool_names[2]}"
7765         sub_test_56xg "-c-1 -S2M" "${pool_names[2]}" ${pool_ranges[2]}
7766
7767         echo "3. migrate $tfile on pool ${pool_names[1]}"
7768         sub_test_56xg "-n -c-1" "${pool_names[1]}" ${pool_ranges[1]}
7769
7770         echo "4. migrate $tfile on pool ${pool_names[2]} with default stripe parameters"
7771         sub_test_56xg "" "${pool_names[2]}" ${pool_ranges[2]}
7772         echo
7773
7774         # Clean pools
7775         destroy_test_pools ||
7776                 error "pool_destroy failed"
7777 }
7778 run_test 56xg "lfs migrate pool support"
7779
7780 test_56y() {
7781         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7782                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7783
7784         local res=""
7785         local dir=$DIR/$tdir
7786         local f1=$dir/file1
7787         local f2=$dir/file2
7788
7789         test_mkdir -p $dir || error "creating dir $dir"
7790         touch $f1 || error "creating std file $f1"
7791         $MULTIOP $f2 H2c || error "creating released file $f2"
7792
7793         # a directory can be raid0, so ask only for files
7794         res=$($LFS find $dir -L raid0 -type f | wc -l)
7795         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7796
7797         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7798         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7799
7800         # only files can be released, so no need to force file search
7801         res=$($LFS find $dir -L released)
7802         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7803
7804         res=$($LFS find $dir -type f \! -L released)
7805         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7806 }
7807 run_test 56y "lfs find -L raid0|released"
7808
7809 test_56z() { # LU-4824
7810         # This checks to make sure 'lfs find' continues after errors
7811         # There are two classes of errors that should be caught:
7812         # - If multiple paths are provided, all should be searched even if one
7813         #   errors out
7814         # - If errors are encountered during the search, it should not terminate
7815         #   early
7816         local dir=$DIR/$tdir
7817         local i
7818
7819         test_mkdir $dir
7820         for i in d{0..9}; do
7821                 test_mkdir $dir/$i
7822                 touch $dir/$i/$tfile
7823         done
7824         $LFS find $DIR/non_existent_dir $dir &&
7825                 error "$LFS find did not return an error"
7826         # Make a directory unsearchable. This should NOT be the last entry in
7827         # directory order.  Arbitrarily pick the 6th entry
7828         chmod 700 $($LFS find $dir -type d | sed '6!d')
7829
7830         $RUNAS $LFS find $DIR/non_existent $dir
7831         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7832
7833         # The user should be able to see 10 directories and 9 files
7834         (( count == 19 )) ||
7835                 error "$LFS find found $count != 19 entries after error"
7836 }
7837 run_test 56z "lfs find should continue after an error"
7838
7839 test_56aa() { # LU-5937
7840         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7841
7842         local dir=$DIR/$tdir
7843
7844         mkdir $dir
7845         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7846
7847         createmany -o $dir/striped_dir/${tfile}- 1024
7848         local dirs=$($LFS find --size +8k $dir/)
7849
7850         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7851 }
7852 run_test 56aa "lfs find --size under striped dir"
7853
7854 test_56ab() { # LU-10705
7855         test_mkdir $DIR/$tdir
7856         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7857         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7858         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7859         # Flush writes to ensure valid blocks.  Need to be more thorough for
7860         # ZFS, since blocks are not allocated/returned to client immediately.
7861         sync_all_data
7862         wait_zfs_commit ost1 2
7863         cancel_lru_locks osc
7864         ls -ls $DIR/$tdir
7865
7866         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7867
7868         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7869
7870         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7871         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7872
7873         rm -f $DIR/$tdir/$tfile.[123]
7874 }
7875 run_test 56ab "lfs find --blocks"
7876
7877 # LU-11188
7878 test_56aca() {
7879         local dir="$DIR/$tdir"
7880         local perms=(001 002 003 004 005 006 007
7881                      010 020 030 040 050 060 070
7882                      100 200 300 400 500 600 700
7883                      111 222 333 444 555 666 777)
7884         local perm_minus=(8 8 4 8 4 4 2
7885                           8 8 4 8 4 4 2
7886                           8 8 4 8 4 4 2
7887                           4 4 2 4 2 2 1)
7888         local perm_slash=(8  8 12  8 12 12 14
7889                           8  8 12  8 12 12 14
7890                           8  8 12  8 12 12 14
7891                          16 16 24 16 24 24 28)
7892
7893         test_mkdir "$dir"
7894         for perm in ${perms[*]}; do
7895                 touch "$dir/$tfile.$perm"
7896                 chmod $perm "$dir/$tfile.$perm"
7897         done
7898
7899         for ((i = 0; i < ${#perms[*]}; i++)); do
7900                 local num=$($LFS find $dir -perm ${perms[i]} | wc -l)
7901                 (( $num == 1 )) ||
7902                         error "lfs find -perm ${perms[i]}:"\
7903                               "$num != 1"
7904
7905                 num=$($LFS find $dir -perm -${perms[i]} -type f| wc -l)
7906                 (( $num == ${perm_minus[i]} )) ||
7907                         error "lfs find -perm -${perms[i]}:"\
7908                               "$num != ${perm_minus[i]}"
7909
7910                 num=$($LFS find $dir -perm /${perms[i]} -type f| wc -l)
7911                 (( $num == ${perm_slash[i]} )) ||
7912                         error "lfs find -perm /${perms[i]}:"\
7913                               "$num != ${perm_slash[i]}"
7914         done
7915 }
7916 run_test 56aca "check lfs find -perm with octal representation"
7917
7918 test_56acb() {
7919         local dir=$DIR/$tdir
7920         # p is the permission of write and execute for user, group and other
7921         # without the umask. It is used to test +wx.
7922         local p=$(printf "%o" "$((0333 & ~$(umask)))")
7923         local perms=(1000 000 2000 4000 $p 644 111 110 100 004)
7924         local symbolic=(+t  a+t u+t g+t o+t
7925                         g+s u+s o+s +s o+sr
7926                         o=r,ug+o,u+w
7927                         u+ g+ o+ a+ ugo+
7928                         u- g- o- a- ugo-
7929                         u= g= o= a= ugo=
7930                         o=r,ug+o,u+w u=r,a+u,u+w
7931                         g=r,ugo=g,u+w u+x,+X +X
7932                         u+x,u+X u+X u+x,g+X o+r,+X
7933                         u+x,go+X +wx +rwx)
7934
7935         test_mkdir $dir
7936         for perm in ${perms[*]}; do
7937                 touch "$dir/$tfile.$perm"
7938                 chmod $perm "$dir/$tfile.$perm"
7939         done
7940
7941         for (( i = 0; i < ${#symbolic[*]}; i++ )); do
7942                 local num=$($LFS find $dir -perm ${symbolic[i]} | wc -l)
7943
7944                 (( $num == 1 )) ||
7945                         error "lfs find $dir -perm ${symbolic[i]}: $num != 1"
7946         done
7947 }
7948 run_test 56acb "check lfs find -perm with symbolic representation"
7949
7950 test_56acc() {
7951         local dir=$DIR/$tdir
7952         local tests="17777 787 789 abcd
7953                 ug=uu ug=a ug=gu uo=ou urw
7954                 u+xg+x a=r,u+x,"
7955
7956         test_mkdir $dir
7957         for err in $tests; do
7958                 if $LFS find $dir -perm $err 2>/dev/null; then
7959                         error "lfs find -perm $err: parsing should have failed"
7960                 fi
7961         done
7962 }
7963 run_test 56acc "check parsing error for lfs find -perm"
7964
7965 test_56ba() {
7966         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7967                 skip "Need MDS version at least 2.10.50"
7968
7969         # Create composite files with one component
7970         local dir=$DIR/$tdir
7971
7972         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7973         # Create composite files with three components
7974         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7975         # Create non-composite files
7976         createmany -o $dir/${tfile}- 10
7977
7978         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7979
7980         [[ $nfiles == 10 ]] ||
7981                 error "lfs find -E 1M found $nfiles != 10 files"
7982
7983         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7984         [[ $nfiles == 25 ]] ||
7985                 error "lfs find ! -E 1M found $nfiles != 25 files"
7986
7987         # All files have a component that starts at 0
7988         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7989         [[ $nfiles == 35 ]] ||
7990                 error "lfs find --component-start 0 - $nfiles != 35 files"
7991
7992         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7993         [[ $nfiles == 15 ]] ||
7994                 error "lfs find --component-start 2M - $nfiles != 15 files"
7995
7996         # All files created here have a componenet that does not starts at 2M
7997         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7998         [[ $nfiles == 35 ]] ||
7999                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
8000
8001         # Find files with a specified number of components
8002         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
8003         [[ $nfiles == 15 ]] ||
8004                 error "lfs find --component-count 3 - $nfiles != 15 files"
8005
8006         # Remember non-composite files have a component count of zero
8007         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
8008         [[ $nfiles == 10 ]] ||
8009                 error "lfs find --component-count 0 - $nfiles != 10 files"
8010
8011         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
8012         [[ $nfiles == 20 ]] ||
8013                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
8014
8015         # All files have a flag called "init"
8016         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
8017         [[ $nfiles == 35 ]] ||
8018                 error "lfs find --component-flags init - $nfiles != 35 files"
8019
8020         # Multi-component files will have a component not initialized
8021         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
8022         [[ $nfiles == 15 ]] ||
8023                 error "lfs find !--component-flags init - $nfiles != 15 files"
8024
8025         rm -rf $dir
8026
8027 }
8028 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
8029
8030 test_56ca() {
8031         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
8032                 skip "Need MDS version at least 2.10.57"
8033
8034         local td=$DIR/$tdir
8035         local tf=$td/$tfile
8036         local dir
8037         local nfiles
8038         local cmd
8039         local i
8040         local j
8041
8042         # create mirrored directories and mirrored files
8043         mkdir $td || error "mkdir $td failed"
8044         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
8045         createmany -o $tf- 10 || error "create $tf- failed"
8046
8047         for i in $(seq 2); do
8048                 dir=$td/dir$i
8049                 mkdir $dir || error "mkdir $dir failed"
8050                 $LFS mirror create -N$((3 + i)) $dir ||
8051                         error "create mirrored dir $dir failed"
8052                 createmany -o $dir/$tfile- 10 ||
8053                         error "create $dir/$tfile- failed"
8054         done
8055
8056         # change the states of some mirrored files
8057         echo foo > $tf-6
8058         for i in $(seq 2); do
8059                 dir=$td/dir$i
8060                 for j in $(seq 4 9); do
8061                         echo foo > $dir/$tfile-$j
8062                 done
8063         done
8064
8065         # find mirrored files with specific mirror count
8066         cmd="$LFS find --mirror-count 3 --type f $td"
8067         nfiles=$($cmd | wc -l)
8068         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
8069
8070         cmd="$LFS find ! --mirror-count 3 --type f $td"
8071         nfiles=$($cmd | wc -l)
8072         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
8073
8074         cmd="$LFS find --mirror-count +2 --type f $td"
8075         nfiles=$($cmd | wc -l)
8076         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8077
8078         cmd="$LFS find --mirror-count -6 --type f $td"
8079         nfiles=$($cmd | wc -l)
8080         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8081
8082         # find mirrored files with specific file state
8083         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
8084         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
8085
8086         cmd="$LFS find --mirror-state=ro --type f $td"
8087         nfiles=$($cmd | wc -l)
8088         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
8089
8090         cmd="$LFS find ! --mirror-state=ro --type f $td"
8091         nfiles=$($cmd | wc -l)
8092         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8093
8094         cmd="$LFS find --mirror-state=wp --type f $td"
8095         nfiles=$($cmd | wc -l)
8096         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8097
8098         cmd="$LFS find ! --mirror-state=sp --type f $td"
8099         nfiles=$($cmd | wc -l)
8100         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8101 }
8102 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
8103
8104 test_56da() { # LU-14179
8105         local path=$DIR/$tdir
8106
8107         test_mkdir $path
8108         cd $path
8109
8110         local longdir=$(str_repeat 'a' 255)
8111
8112         for i in {1..15}; do
8113                 path=$path/$longdir
8114                 test_mkdir $longdir
8115                 cd $longdir
8116         done
8117
8118         local len=${#path}
8119         local lastdir=$(str_repeat 'a' $((4096 - 1 - $len - 1)))
8120
8121         test_mkdir $lastdir
8122         cd $lastdir
8123         # PATH_MAX-1
8124         (( ${#PWD} == 4095 )) || error "bad PWD length ${#PWD}, expect 4095"
8125
8126         # NAME_MAX
8127         touch $(str_repeat 'f' 255)
8128
8129         $LFS find $DIR/$tdir --type d |& grep "lfs find: error" &&
8130                 error "lfs find reported an error"
8131
8132         rm -rf $DIR/$tdir
8133 }
8134 run_test 56da "test lfs find with long paths"
8135
8136 test_57a() {
8137         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8138         # note test will not do anything if MDS is not local
8139         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8140                 skip_env "ldiskfs only test"
8141         fi
8142         remote_mds_nodsh && skip "remote MDS with nodsh"
8143
8144         local MNTDEV="osd*.*MDT*.mntdev"
8145         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
8146         [ -z "$DEV" ] && error "can't access $MNTDEV"
8147         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
8148                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
8149                         error "can't access $DEV"
8150                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
8151                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
8152                 rm $TMP/t57a.dump
8153         done
8154 }
8155 run_test 57a "verify MDS filesystem created with large inodes =="
8156
8157 test_57b() {
8158         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8159         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8160                 skip_env "ldiskfs only test"
8161         fi
8162         remote_mds_nodsh && skip "remote MDS with nodsh"
8163
8164         local dir=$DIR/$tdir
8165         local filecount=100
8166         local file1=$dir/f1
8167         local fileN=$dir/f$filecount
8168
8169         rm -rf $dir || error "removing $dir"
8170         test_mkdir -c1 $dir
8171         local mdtidx=$($LFS getstripe -m $dir)
8172         local mdtname=MDT$(printf %04x $mdtidx)
8173         local facet=mds$((mdtidx + 1))
8174
8175         echo "mcreating $filecount files"
8176         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
8177
8178         # verify that files do not have EAs yet
8179         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
8180                 error "$file1 has an EA"
8181         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
8182                 error "$fileN has an EA"
8183
8184         sync
8185         sleep 1
8186         df $dir  #make sure we get new statfs data
8187         local mdsfree=$(do_facet $facet \
8188                         lctl get_param -n osd*.*$mdtname.kbytesfree)
8189         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8190         local file
8191
8192         echo "opening files to create objects/EAs"
8193         for file in $(seq -f $dir/f%g 1 $filecount); do
8194                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
8195                         error "opening $file"
8196         done
8197
8198         # verify that files have EAs now
8199         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
8200         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
8201
8202         sleep 1  #make sure we get new statfs data
8203         df $dir
8204         local mdsfree2=$(do_facet $facet \
8205                          lctl get_param -n osd*.*$mdtname.kbytesfree)
8206         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8207
8208         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
8209                 if [ "$mdsfree" != "$mdsfree2" ]; then
8210                         error "MDC before $mdcfree != after $mdcfree2"
8211                 else
8212                         echo "MDC before $mdcfree != after $mdcfree2"
8213                         echo "unable to confirm if MDS has large inodes"
8214                 fi
8215         fi
8216         rm -rf $dir
8217 }
8218 run_test 57b "default LOV EAs are stored inside large inodes ==="
8219
8220 test_58() {
8221         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8222         [ -z "$(which wiretest 2>/dev/null)" ] &&
8223                         skip_env "could not find wiretest"
8224
8225         wiretest
8226 }
8227 run_test 58 "verify cross-platform wire constants =============="
8228
8229 test_59() {
8230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8231
8232         echo "touch 130 files"
8233         createmany -o $DIR/f59- 130
8234         echo "rm 130 files"
8235         unlinkmany $DIR/f59- 130
8236         sync
8237         # wait for commitment of removal
8238         wait_delete_completed
8239 }
8240 run_test 59 "verify cancellation of llog records async ========="
8241
8242 TEST60_HEAD="test_60 run $RANDOM"
8243 test_60a() {
8244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8245         remote_mgs_nodsh && skip "remote MGS with nodsh"
8246         do_facet mgs "! which run-llog.sh &> /dev/null" &&
8247                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
8248                         skip_env "missing subtest run-llog.sh"
8249
8250         log "$TEST60_HEAD - from kernel mode"
8251         do_facet mgs "$LCTL dk > /dev/null"
8252         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
8253         do_facet mgs $LCTL dk > $TMP/$tfile
8254
8255         # LU-6388: test llog_reader
8256         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
8257         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
8258         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
8259                         skip_env "missing llog_reader"
8260         local fstype=$(facet_fstype mgs)
8261         [ $fstype != ldiskfs -a $fstype != zfs ] &&
8262                 skip_env "Only for ldiskfs or zfs type mgs"
8263
8264         local mntpt=$(facet_mntpt mgs)
8265         local mgsdev=$(mgsdevname 1)
8266         local fid_list
8267         local fid
8268         local rec_list
8269         local rec
8270         local rec_type
8271         local obj_file
8272         local path
8273         local seq
8274         local oid
8275         local pass=true
8276
8277         #get fid and record list
8278         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
8279                 tail -n 4))
8280         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
8281                 tail -n 4))
8282         #remount mgs as ldiskfs or zfs type
8283         stop mgs || error "stop mgs failed"
8284         mount_fstype mgs || error "remount mgs failed"
8285         for ((i = 0; i < ${#fid_list[@]}; i++)); do
8286                 fid=${fid_list[i]}
8287                 rec=${rec_list[i]}
8288                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
8289                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
8290                 oid=$((16#$oid))
8291
8292                 case $fstype in
8293                         ldiskfs )
8294                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
8295                         zfs )
8296                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
8297                 esac
8298                 echo "obj_file is $obj_file"
8299                 do_facet mgs $llog_reader $obj_file
8300
8301                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
8302                         awk '{ print $3 }' | sed -e "s/^type=//g")
8303                 if [ $rec_type != $rec ]; then
8304                         echo "FAILED test_60a wrong record type $rec_type," \
8305                               "should be $rec"
8306                         pass=false
8307                         break
8308                 fi
8309
8310                 #check obj path if record type is LLOG_LOGID_MAGIC
8311                 if [ "$rec" == "1064553b" ]; then
8312                         path=$(do_facet mgs $llog_reader $obj_file |
8313                                 grep "path=" | awk '{ print $NF }' |
8314                                 sed -e "s/^path=//g")
8315                         if [ $obj_file != $mntpt/$path ]; then
8316                                 echo "FAILED test_60a wrong obj path" \
8317                                       "$montpt/$path, should be $obj_file"
8318                                 pass=false
8319                                 break
8320                         fi
8321                 fi
8322         done
8323         rm -f $TMP/$tfile
8324         #restart mgs before "error", otherwise it will block the next test
8325         stop mgs || error "stop mgs failed"
8326         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
8327         $pass || error "test failed, see FAILED test_60a messages for specifics"
8328 }
8329 run_test 60a "llog_test run from kernel module and test llog_reader"
8330
8331 test_60b() { # bug 6411
8332         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8333
8334         dmesg > $DIR/$tfile
8335         LLOG_COUNT=$(do_facet mgs dmesg |
8336                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
8337                           /llog_[a-z]*.c:[0-9]/ {
8338                                 if (marker)
8339                                         from_marker++
8340                                 from_begin++
8341                           }
8342                           END {
8343                                 if (marker)
8344                                         print from_marker
8345                                 else
8346                                         print from_begin
8347                           }")
8348
8349         [[ $LLOG_COUNT -gt 120 ]] &&
8350                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
8351 }
8352 run_test 60b "limit repeated messages from CERROR/CWARN"
8353
8354 test_60c() {
8355         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8356
8357         echo "create 5000 files"
8358         createmany -o $DIR/f60c- 5000
8359 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
8360         lctl set_param fail_loc=0x80000137
8361         unlinkmany $DIR/f60c- 5000
8362         lctl set_param fail_loc=0
8363 }
8364 run_test 60c "unlink file when mds full"
8365
8366 test_60d() {
8367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8368
8369         SAVEPRINTK=$(lctl get_param -n printk)
8370         # verify "lctl mark" is even working"
8371         MESSAGE="test message ID $RANDOM $$"
8372         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8373         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
8374
8375         lctl set_param printk=0 || error "set lnet.printk failed"
8376         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
8377         MESSAGE="new test message ID $RANDOM $$"
8378         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
8379         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8380         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
8381
8382         lctl set_param -n printk="$SAVEPRINTK"
8383 }
8384 run_test 60d "test printk console message masking"
8385
8386 test_60e() {
8387         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8388         remote_mds_nodsh && skip "remote MDS with nodsh"
8389
8390         touch $DIR/$tfile
8391 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
8392         do_facet mds1 lctl set_param fail_loc=0x15b
8393         rm $DIR/$tfile
8394 }
8395 run_test 60e "no space while new llog is being created"
8396
8397 test_60f() {
8398         local old_path=$($LCTL get_param -n debug_path)
8399
8400         stack_trap "$LCTL set_param debug_path=$old_path"
8401         stack_trap "rm -f $TMP/$tfile*"
8402         rm -f $TMP/$tfile* 2> /dev/null
8403         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8404         $LCTL set_param debug_path=$TMP/$tfile fail_loc=0x8000050e
8405         test_mkdir $DIR/$tdir
8406         # retry in case the open is cached and not released
8407         for (( i = 0; i < 100 && $(ls $TMP/$tfile* | wc -l) == 0; i++ )); do
8408                 echo $i > $DIR/$tdir/$tfile.$i && cat $DIR/$tdir/$tfile.$i
8409                 sleep 0.1
8410         done
8411         ls $TMP/$tfile*
8412         (( $(ls $TMP/$tfile* | wc -l) > 0 )) || error "$TMP/$tfile not dumped"
8413 }
8414 run_test 60f "change debug_path works"
8415
8416 test_60g() {
8417         local pid
8418         local i
8419
8420         test_mkdir -c $MDSCOUNT $DIR/$tdir
8421
8422         (
8423                 local index=0
8424                 while true; do
8425                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
8426                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
8427                                 2>/dev/null
8428                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
8429                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
8430                         index=$((index + 1))
8431                 done
8432         ) &
8433
8434         pid=$!
8435
8436         for i in {0..100}; do
8437                 # define OBD_FAIL_OSD_TXN_START    0x19a
8438                 local index=$((i % MDSCOUNT + 1))
8439
8440                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
8441                         > /dev/null
8442                 sleep 0.01
8443         done
8444
8445         kill -9 $pid
8446
8447         for i in $(seq $MDSCOUNT); do
8448                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
8449         done
8450
8451         mkdir $DIR/$tdir/new || error "mkdir failed"
8452         rmdir $DIR/$tdir/new || error "rmdir failed"
8453
8454         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
8455                 -t namespace
8456         for i in $(seq $MDSCOUNT); do
8457                 wait_update_facet mds$i "$LCTL get_param -n \
8458                         mdd.$(facet_svc mds$i).lfsck_namespace |
8459                         awk '/^status/ { print \\\$2 }'" "completed"
8460         done
8461
8462         ls -R $DIR/$tdir || error "ls failed"
8463         rm -rf $DIR/$tdir || error "rmdir failed"
8464 }
8465 run_test 60g "transaction abort won't cause MDT hung"
8466
8467 test_60h() {
8468         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
8469                 skip "Need MDS version at least 2.12.52"
8470         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
8471
8472         local f
8473
8474         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
8475         #define OBD_FAIL_MDS_STRIPE_FID          0x189
8476         for fail_loc in 0x80000188 0x80000189; do
8477                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
8478                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
8479                         error "mkdir $dir-$fail_loc failed"
8480                 for i in {0..10}; do
8481                         # create may fail on missing stripe
8482                         echo $i > $DIR/$tdir-$fail_loc/$i
8483                 done
8484                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8485                         error "getdirstripe $tdir-$fail_loc failed"
8486                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
8487                         error "migrate $tdir-$fail_loc failed"
8488                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8489                         error "getdirstripe $tdir-$fail_loc failed"
8490                 pushd $DIR/$tdir-$fail_loc
8491                 for f in *; do
8492                         echo $f | cmp $f - || error "$f data mismatch"
8493                 done
8494                 popd
8495                 rm -rf $DIR/$tdir-$fail_loc
8496         done
8497 }
8498 run_test 60h "striped directory with missing stripes can be accessed"
8499
8500 function t60i_load() {
8501         mkdir $DIR/$tdir
8502         #define OBD_FAIL_LLOG_PAUSE_AFTER_PAD               0x131c
8503         $LCTL set_param fail_loc=0x131c fail_val=1
8504         for ((i=0; i<5000; i++)); do
8505                 touch $DIR/$tdir/f$i
8506         done
8507 }
8508
8509 test_60i() {
8510         changelog_register || error "changelog_register failed"
8511         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
8512         changelog_users $SINGLEMDS | grep -q $cl_user ||
8513                 error "User $cl_user not found in changelog_users"
8514         changelog_chmask "ALL"
8515         t60i_load &
8516         local PID=$!
8517         for((i=0; i<100; i++)); do
8518                 changelog_dump >/dev/null ||
8519                         error "can't read changelog"
8520         done
8521         kill $PID
8522         wait $PID
8523         changelog_deregister || error "changelog_deregister failed"
8524         $LCTL set_param fail_loc=0
8525 }
8526 run_test 60i "llog: new record vs reader race"
8527
8528 test_61a() {
8529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8530
8531         f="$DIR/f61"
8532         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
8533         cancel_lru_locks osc
8534         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
8535         sync
8536 }
8537 run_test 61a "mmap() writes don't make sync hang ================"
8538
8539 test_61b() {
8540         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
8541 }
8542 run_test 61b "mmap() of unstriped file is successful"
8543
8544 # bug 2330 - insufficient obd_match error checking causes LBUG
8545 test_62() {
8546         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8547
8548         f="$DIR/f62"
8549         echo foo > $f
8550         cancel_lru_locks osc
8551         lctl set_param fail_loc=0x405
8552         cat $f && error "cat succeeded, expect -EIO"
8553         lctl set_param fail_loc=0
8554 }
8555 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
8556 # match every page all of the time.
8557 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
8558
8559 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
8560 # Though this test is irrelevant anymore, it helped to reveal some
8561 # other grant bugs (LU-4482), let's keep it.
8562 test_63a() {   # was test_63
8563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8564
8565         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
8566
8567         for i in `seq 10` ; do
8568                 dd if=/dev/zero of=$DIR/f63 bs=8k &
8569                 sleep 5
8570                 kill $!
8571                 sleep 1
8572         done
8573
8574         rm -f $DIR/f63 || true
8575 }
8576 run_test 63a "Verify oig_wait interruption does not crash ======="
8577
8578 # bug 2248 - async write errors didn't return to application on sync
8579 # bug 3677 - async write errors left page locked
8580 test_63b() {
8581         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8582
8583         debugsave
8584         lctl set_param debug=-1
8585
8586         # ensure we have a grant to do async writes
8587         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
8588         rm $DIR/$tfile
8589
8590         sync    # sync lest earlier test intercept the fail_loc
8591
8592         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8593         lctl set_param fail_loc=0x80000406
8594         $MULTIOP $DIR/$tfile Owy && \
8595                 error "sync didn't return ENOMEM"
8596         sync; sleep 2; sync     # do a real sync this time to flush page
8597         lctl get_param -n llite.*.dump_page_cache | grep locked && \
8598                 error "locked page left in cache after async error" || true
8599         debugrestore
8600 }
8601 run_test 63b "async write errors should be returned to fsync ==="
8602
8603 test_64a () {
8604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8605
8606         lfs df $DIR
8607         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
8608 }
8609 run_test 64a "verify filter grant calculations (in kernel) ====="
8610
8611 test_64b () {
8612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8613
8614         sh oos.sh $MOUNT || error "oos.sh failed: $?"
8615 }
8616 run_test 64b "check out-of-space detection on client"
8617
8618 test_64c() {
8619         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
8620 }
8621 run_test 64c "verify grant shrink"
8622
8623 import_param() {
8624         local tgt=$1
8625         local param=$2
8626
8627         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
8628 }
8629
8630 # this does exactly what osc_request.c:osc_announce_cached() does in
8631 # order to calculate max amount of grants to ask from server
8632 want_grant() {
8633         local tgt=$1
8634
8635         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
8636         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
8637
8638         ((rpc_in_flight++));
8639         nrpages=$((nrpages * rpc_in_flight))
8640
8641         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
8642
8643         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
8644
8645         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
8646         local undirty=$((nrpages * PAGE_SIZE))
8647
8648         local max_extent_pages
8649         max_extent_pages=$(import_param $tgt grant_max_extent_size)
8650         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
8651         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
8652         local grant_extent_tax
8653         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8654
8655         undirty=$((undirty + nrextents * grant_extent_tax))
8656
8657         echo $undirty
8658 }
8659
8660 # this is size of unit for grant allocation. It should be equal to
8661 # what tgt_grant.c:tgt_grant_chunk() calculates
8662 grant_chunk() {
8663         local tgt=$1
8664         local max_brw_size
8665         local grant_extent_tax
8666
8667         max_brw_size=$(import_param $tgt max_brw_size)
8668
8669         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8670
8671         echo $(((max_brw_size + grant_extent_tax) * 2))
8672 }
8673
8674 test_64d() {
8675         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
8676                 skip "OST < 2.10.55 doesn't limit grants enough"
8677
8678         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
8679
8680         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
8681                 skip "no grant_param connect flag"
8682
8683         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8684
8685         $LCTL set_param -n -n debug="$OLDDEBUG" || true
8686         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8687
8688
8689         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
8690         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
8691
8692         $LFS setstripe $DIR/$tfile -i 0 -c 1
8693         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
8694         ddpid=$!
8695
8696         while kill -0 $ddpid; do
8697                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8698
8699                 if [[ $cur_grant -gt $max_cur_granted ]]; then
8700                         kill $ddpid
8701                         error "cur_grant $cur_grant > $max_cur_granted"
8702                 fi
8703
8704                 sleep 1
8705         done
8706 }
8707 run_test 64d "check grant limit exceed"
8708
8709 check_grants() {
8710         local tgt=$1
8711         local expected=$2
8712         local msg=$3
8713         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8714
8715         ((cur_grants == expected)) ||
8716                 error "$msg: grants mismatch: $cur_grants, expected $expected"
8717 }
8718
8719 round_up_p2() {
8720         echo $((($1 + $2 - 1) & ~($2 - 1)))
8721 }
8722
8723 test_64e() {
8724         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8725         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
8726                 skip "Need OSS version at least 2.11.56"
8727
8728         # Remount client to reset grant
8729         remount_client $MOUNT || error "failed to remount client"
8730         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8731
8732         local init_grants=$(import_param $osc_tgt initial_grant)
8733
8734         check_grants $osc_tgt $init_grants "init grants"
8735
8736         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8737         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8738         local gbs=$(import_param $osc_tgt grant_block_size)
8739
8740         # write random number of bytes from max_brw_size / 4 to max_brw_size
8741         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
8742         # align for direct io
8743         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
8744         # round to grant consumption unit
8745         local wb_round_up=$(round_up_p2 $write_bytes gbs)
8746
8747         local grants=$((wb_round_up + extent_tax))
8748
8749         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
8750
8751         # define OBD_FAIL_TGT_NO_GRANT 0x725
8752         # make the server not grant more back
8753         do_facet ost1 $LCTL set_param fail_loc=0x725
8754         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
8755
8756         do_facet ost1 $LCTL set_param fail_loc=0
8757
8758         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
8759
8760         rm -f $DIR/$tfile || error "rm failed"
8761
8762         # Remount client to reset grant
8763         remount_client $MOUNT || error "failed to remount client"
8764         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8765
8766         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8767
8768         # define OBD_FAIL_TGT_NO_GRANT 0x725
8769         # make the server not grant more back
8770         do_facet ost1 $LCTL set_param fail_loc=0x725
8771         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
8772         do_facet ost1 $LCTL set_param fail_loc=0
8773
8774         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
8775 }
8776 run_test 64e "check grant consumption (no grant allocation)"
8777
8778 test_64f() {
8779         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8780
8781         # Remount client to reset grant
8782         remount_client $MOUNT || error "failed to remount client"
8783         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8784
8785         local init_grants=$(import_param $osc_tgt initial_grant)
8786         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8787         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8788         local gbs=$(import_param $osc_tgt grant_block_size)
8789         local chunk=$(grant_chunk $osc_tgt)
8790
8791         # write random number of bytes from max_brw_size / 4 to max_brw_size
8792         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
8793         # align for direct io
8794         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
8795         # round to grant consumption unit
8796         local wb_round_up=$(round_up_p2 $write_bytes gbs)
8797
8798         local grants=$((wb_round_up + extent_tax))
8799
8800         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8801         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
8802                 error "error writing to $DIR/$tfile"
8803
8804         check_grants $osc_tgt $((init_grants - grants + chunk)) \
8805                 "direct io with grant allocation"
8806
8807         rm -f $DIR/$tfile || error "rm failed"
8808
8809         # Remount client to reset grant
8810         remount_client $MOUNT || error "failed to remount client"
8811         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8812
8813         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
8814
8815         local cmd="oO_WRONLY:w${write_bytes}_yc"
8816
8817         $MULTIOP $DIR/$tfile $cmd &
8818         MULTIPID=$!
8819         sleep 1
8820
8821         check_grants $osc_tgt $((init_grants - grants)) \
8822                 "buffered io, not write rpc"
8823
8824         kill -USR1 $MULTIPID
8825         wait
8826
8827         check_grants $osc_tgt $((init_grants - grants + chunk)) \
8828                 "buffered io, one RPC"
8829 }
8830 run_test 64f "check grant consumption (with grant allocation)"
8831
8832 test_64g() {
8833         #[ $MDS1_VERSION -lt $(version_code 2.14.54) ] &&
8834         #       skip "Need MDS version at least 2.14.54"
8835
8836         local mdts=$(comma_list $(mdts_nodes))
8837
8838         local old=$($LCTL get_param mdc.$FSNAME-*.grant_shrink_interval |
8839                         tr '\n' ' ')
8840         stack_trap "$LCTL set_param $old"
8841
8842         # generate dirty pages and increase dirty granted on MDT
8843         stack_trap "rm -f $DIR/$tfile-*"
8844         for (( i = 0; i < 10; i++)); do
8845                 $LFS setstripe -E 1M -L mdt $DIR/$tfile-$i ||
8846                         error "can't set stripe"
8847                 dd if=/dev/zero of=$DIR/$tfile-$i bs=128k count=1 ||
8848                         error "can't dd"
8849                 $LFS getstripe $DIR/$tfile-$i | grep -q pattern.*mdt || {
8850                         $LFS getstripe $DIR/$tfile-$i
8851                         error "not DoM file"
8852                 }
8853         done
8854
8855         # flush dirty pages
8856         sync
8857
8858         # wait until grant shrink reset grant dirty on MDTs
8859         for ((i = 0; i < 120; i++)); do
8860                 grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
8861                         awk '{sum=sum+$1} END {print sum}')
8862                 vm_dirty=$(awk '/Dirty:/{print $2}' /proc/meminfo)
8863                 echo "$grant_dirty grants, $vm_dirty pages"
8864                 (( grant_dirty + vm_dirty == 0 )) && break
8865                 (( i == 3 )) && sync &&
8866                         $LCTL set_param mdc.$FSNAME-*.grant_shrink_interval=5
8867                 sleep 1
8868         done
8869
8870         grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
8871                 awk '{sum=sum+$1} END {print sum}')
8872         (( grant_dirty == 0 )) || error "$grant_dirty on MDT"
8873 }
8874 run_test 64g "grant shrink on MDT"
8875
8876 # bug 1414 - set/get directories' stripe info
8877 test_65a() {
8878         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8879
8880         test_mkdir $DIR/$tdir
8881         touch $DIR/$tdir/f1
8882         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
8883 }
8884 run_test 65a "directory with no stripe info"
8885
8886 test_65b() {
8887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8888
8889         test_mkdir $DIR/$tdir
8890         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8891
8892         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8893                                                 error "setstripe"
8894         touch $DIR/$tdir/f2
8895         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
8896 }
8897 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
8898
8899 test_65c() {
8900         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8901         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
8902
8903         test_mkdir $DIR/$tdir
8904         local stripesize=$($LFS getstripe -S $DIR/$tdir)
8905
8906         $LFS setstripe -S $((stripesize * 4)) -i 1 \
8907                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
8908         touch $DIR/$tdir/f3
8909         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
8910 }
8911 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
8912
8913 test_65d() {
8914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8915
8916         test_mkdir $DIR/$tdir
8917         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
8918         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8919
8920         if [[ $STRIPECOUNT -le 0 ]]; then
8921                 sc=1
8922         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
8923                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
8924                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
8925         else
8926                 sc=$(($STRIPECOUNT - 1))
8927         fi
8928         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
8929         touch $DIR/$tdir/f4 $DIR/$tdir/f5
8930         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
8931                 error "lverify failed"
8932 }
8933 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
8934
8935 test_65e() {
8936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8937
8938         test_mkdir $DIR/$tdir
8939
8940         $LFS setstripe $DIR/$tdir || error "setstripe"
8941         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8942                                         error "no stripe info failed"
8943         touch $DIR/$tdir/f6
8944         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
8945 }
8946 run_test 65e "directory setstripe defaults"
8947
8948 test_65f() {
8949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8950
8951         test_mkdir $DIR/${tdir}f
8952         $RUNAS $LFS setstripe $DIR/${tdir}f &&
8953                 error "setstripe succeeded" || true
8954 }
8955 run_test 65f "dir setstripe permission (should return error) ==="
8956
8957 test_65g() {
8958         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8959
8960         test_mkdir $DIR/$tdir
8961         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8962
8963         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8964                 error "setstripe -S failed"
8965         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
8966         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8967                 error "delete default stripe failed"
8968 }
8969 run_test 65g "directory setstripe -d"
8970
8971 test_65h() {
8972         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8973
8974         test_mkdir $DIR/$tdir
8975         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8976
8977         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8978                 error "setstripe -S failed"
8979         test_mkdir $DIR/$tdir/dd1
8980         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
8981                 error "stripe info inherit failed"
8982 }
8983 run_test 65h "directory stripe info inherit ===================="
8984
8985 test_65i() {
8986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8987
8988         save_layout_restore_at_exit $MOUNT
8989
8990         # bug6367: set non-default striping on root directory
8991         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
8992
8993         # bug12836: getstripe on -1 default directory striping
8994         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
8995
8996         # bug12836: getstripe -v on -1 default directory striping
8997         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
8998
8999         # bug12836: new find on -1 default directory striping
9000         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
9001 }
9002 run_test 65i "various tests to set root directory striping"
9003
9004 test_65j() { # bug6367
9005         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9006
9007         sync; sleep 1
9008
9009         # if we aren't already remounting for each test, do so for this test
9010         if [ "$I_MOUNTED" = "yes" ]; then
9011                 cleanup || error "failed to unmount"
9012                 setup
9013         fi
9014
9015         save_layout_restore_at_exit $MOUNT
9016
9017         $LFS setstripe -d $MOUNT || error "setstripe failed"
9018 }
9019 run_test 65j "set default striping on root directory (bug 6367)="
9020
9021 cleanup_65k() {
9022         rm -rf $DIR/$tdir
9023         wait_delete_completed
9024         do_facet $SINGLEMDS "lctl set_param -n \
9025                 osp.$ost*MDT0000.max_create_count=$max_count"
9026         do_facet $SINGLEMDS "lctl set_param -n \
9027                 osp.$ost*MDT0000.create_count=$count"
9028         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9029         echo $INACTIVE_OSC "is Activate"
9030
9031         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9032 }
9033
9034 test_65k() { # bug11679
9035         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9036         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9037         remote_mds_nodsh && skip "remote MDS with nodsh"
9038
9039         local disable_precreate=true
9040         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
9041                 disable_precreate=false
9042
9043         echo "Check OST status: "
9044         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
9045                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
9046
9047         for OSC in $MDS_OSCS; do
9048                 echo $OSC "is active"
9049                 do_facet $SINGLEMDS lctl --device %$OSC activate
9050         done
9051
9052         for INACTIVE_OSC in $MDS_OSCS; do
9053                 local ost=$(osc_to_ost $INACTIVE_OSC)
9054                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
9055                                lov.*md*.target_obd |
9056                                awk -F: /$ost/'{ print $1 }' | head -n 1)
9057
9058                 mkdir -p $DIR/$tdir
9059                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
9060                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
9061
9062                 echo "Deactivate: " $INACTIVE_OSC
9063                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
9064
9065                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
9066                               osp.$ost*MDT0000.create_count")
9067                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
9068                                   osp.$ost*MDT0000.max_create_count")
9069                 $disable_precreate &&
9070                         do_facet $SINGLEMDS "lctl set_param -n \
9071                                 osp.$ost*MDT0000.max_create_count=0"
9072
9073                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
9074                         [ -f $DIR/$tdir/$idx ] && continue
9075                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
9076                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
9077                                 { cleanup_65k;
9078                                   error "setstripe $idx should succeed"; }
9079                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
9080                 done
9081                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
9082                 rmdir $DIR/$tdir
9083
9084                 do_facet $SINGLEMDS "lctl set_param -n \
9085                         osp.$ost*MDT0000.max_create_count=$max_count"
9086                 do_facet $SINGLEMDS "lctl set_param -n \
9087                         osp.$ost*MDT0000.create_count=$count"
9088                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9089                 echo $INACTIVE_OSC "is Activate"
9090
9091                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9092         done
9093 }
9094 run_test 65k "validate manual striping works properly with deactivated OSCs"
9095
9096 test_65l() { # bug 12836
9097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9098
9099         test_mkdir -p $DIR/$tdir/test_dir
9100         $LFS setstripe -c -1 $DIR/$tdir/test_dir
9101         $LFS find -mtime -1 $DIR/$tdir >/dev/null
9102 }
9103 run_test 65l "lfs find on -1 stripe dir ========================"
9104
9105 test_65m() {
9106         local layout=$(save_layout $MOUNT)
9107         $RUNAS $LFS setstripe -c 2 $MOUNT && {
9108                 restore_layout $MOUNT $layout
9109                 error "setstripe should fail by non-root users"
9110         }
9111         true
9112 }
9113 run_test 65m "normal user can't set filesystem default stripe"
9114
9115 test_65n() {
9116         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
9117         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
9118                 skip "Need MDS version at least 2.12.50"
9119         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
9120
9121         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
9122         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
9123         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
9124
9125         save_layout_restore_at_exit $MOUNT
9126
9127         # new subdirectory under root directory should not inherit
9128         # the default layout from root
9129         local dir1=$MOUNT/$tdir-1
9130         mkdir $dir1 || error "mkdir $dir1 failed"
9131         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
9132                 error "$dir1 shouldn't have LOV EA"
9133
9134         # delete the default layout on root directory
9135         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
9136
9137         local dir2=$MOUNT/$tdir-2
9138         mkdir $dir2 || error "mkdir $dir2 failed"
9139         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
9140                 error "$dir2 shouldn't have LOV EA"
9141
9142         # set a new striping pattern on root directory
9143         local def_stripe_size=$($LFS getstripe -S $MOUNT)
9144         local new_def_stripe_size=$((def_stripe_size * 2))
9145         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
9146                 error "set stripe size on $MOUNT failed"
9147
9148         # new file created in $dir2 should inherit the new stripe size from
9149         # the filesystem default
9150         local file2=$dir2/$tfile-2
9151         touch $file2 || error "touch $file2 failed"
9152
9153         local file2_stripe_size=$($LFS getstripe -S $file2)
9154         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
9155         {
9156                 echo "file2_stripe_size: '$file2_stripe_size'"
9157                 echo "new_def_stripe_size: '$new_def_stripe_size'"
9158                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
9159         }
9160
9161         local dir3=$MOUNT/$tdir-3
9162         mkdir $dir3 || error "mkdir $dir3 failed"
9163         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
9164         # the root layout, which is the actual default layout that will be used
9165         # when new files are created in $dir3.
9166         local dir3_layout=$(get_layout_param $dir3)
9167         local root_dir_layout=$(get_layout_param $MOUNT)
9168         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
9169         {
9170                 echo "dir3_layout: '$dir3_layout'"
9171                 echo "root_dir_layout: '$root_dir_layout'"
9172                 error "$dir3 should show the default layout from $MOUNT"
9173         }
9174
9175         # set OST pool on root directory
9176         local pool=$TESTNAME
9177         pool_add $pool || error "add $pool failed"
9178         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
9179                 error "add targets to $pool failed"
9180
9181         $LFS setstripe -p $pool $MOUNT ||
9182                 error "set OST pool on $MOUNT failed"
9183
9184         # new file created in $dir3 should inherit the pool from
9185         # the filesystem default
9186         local file3=$dir3/$tfile-3
9187         touch $file3 || error "touch $file3 failed"
9188
9189         local file3_pool=$($LFS getstripe -p $file3)
9190         [[ "$file3_pool" = "$pool" ]] ||
9191                 error "$file3 ('$file3_pool') didn't inherit OST pool '$pool'"
9192
9193         local dir4=$MOUNT/$tdir-4
9194         mkdir $dir4 || error "mkdir $dir4 failed"
9195         local dir4_layout=$(get_layout_param $dir4)
9196         root_dir_layout=$(get_layout_param $MOUNT)
9197         echo "$LFS getstripe -d $dir4"
9198         $LFS getstripe -d $dir4
9199         echo "$LFS getstripe -d $MOUNT"
9200         $LFS getstripe -d $MOUNT
9201         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
9202         {
9203                 echo "dir4_layout: '$dir4_layout'"
9204                 echo "root_dir_layout: '$root_dir_layout'"
9205                 error "$dir4 should show the default layout from $MOUNT"
9206         }
9207
9208         # new file created in $dir4 should inherit the pool from
9209         # the filesystem default
9210         local file4=$dir4/$tfile-4
9211         touch $file4 || error "touch $file4 failed"
9212
9213         local file4_pool=$($LFS getstripe -p $file4)
9214         [[ "$file4_pool" = "$pool" ]] ||
9215                 error "$file4 ('$file4_pool') didn't inherit OST pool $pool"
9216
9217         # new subdirectory under non-root directory should inherit
9218         # the default layout from its parent directory
9219         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
9220                 error "set directory layout on $dir4 failed"
9221
9222         local dir5=$dir4/$tdir-5
9223         mkdir $dir5 || error "mkdir $dir5 failed"
9224
9225         dir4_layout=$(get_layout_param $dir4)
9226         local dir5_layout=$(get_layout_param $dir5)
9227         [[ "$dir4_layout" = "$dir5_layout" ]] ||
9228         {
9229                 echo "dir4_layout: '$dir4_layout'"
9230                 echo "dir5_layout: '$dir5_layout'"
9231                 error "$dir5 should inherit the default layout from $dir4"
9232         }
9233
9234         # though subdir under ROOT doesn't inherit default layout, but
9235         # its sub dir/file should be created with default layout.
9236         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
9237         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
9238                 skip "Need MDS version at least 2.12.59"
9239
9240         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
9241         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
9242         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
9243
9244         if [ $default_lmv_hash == "none" ]; then
9245                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
9246         else
9247                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
9248                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
9249         fi
9250
9251         $LFS setdirstripe -D -c 2 $MOUNT ||
9252                 error "setdirstripe -D -c 2 failed"
9253         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
9254         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
9255         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
9256 }
9257 run_test 65n "don't inherit default layout from root for new subdirectories"
9258
9259 # bug 2543 - update blocks count on client
9260 test_66() {
9261         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9262
9263         COUNT=${COUNT:-8}
9264         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
9265         sync; sync_all_data; sync; sync_all_data
9266         cancel_lru_locks osc
9267         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
9268         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
9269 }
9270 run_test 66 "update inode blocks count on client ==============="
9271
9272 meminfo() {
9273         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
9274 }
9275
9276 swap_used() {
9277         swapon -s | awk '($1 == "'$1'") { print $4 }'
9278 }
9279
9280 # bug5265, obdfilter oa2dentry return -ENOENT
9281 # #define OBD_FAIL_SRV_ENOENT 0x217
9282 test_69() {
9283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9284         remote_ost_nodsh && skip "remote OST with nodsh"
9285
9286         f="$DIR/$tfile"
9287         $LFS setstripe -c 1 -i 0 $f
9288
9289         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
9290
9291         do_facet ost1 lctl set_param fail_loc=0x217
9292         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
9293         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
9294
9295         do_facet ost1 lctl set_param fail_loc=0
9296         $DIRECTIO write $f 0 2 || error "write error"
9297
9298         cancel_lru_locks osc
9299         $DIRECTIO read $f 0 1 || error "read error"
9300
9301         do_facet ost1 lctl set_param fail_loc=0x217
9302         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
9303
9304         do_facet ost1 lctl set_param fail_loc=0
9305         rm -f $f
9306 }
9307 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
9308
9309 test_71() {
9310         test_mkdir $DIR/$tdir
9311         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
9312         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
9313 }
9314 run_test 71 "Running dbench on lustre (don't segment fault) ===="
9315
9316 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
9317         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9318         [ "$RUNAS_ID" = "$UID" ] &&
9319                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9320         # Check that testing environment is properly set up. Skip if not
9321         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
9322                 skip_env "User $RUNAS_ID does not exist - skipping"
9323
9324         touch $DIR/$tfile
9325         chmod 777 $DIR/$tfile
9326         chmod ug+s $DIR/$tfile
9327         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
9328                 error "$RUNAS dd $DIR/$tfile failed"
9329         # See if we are still setuid/sgid
9330         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9331                 error "S/gid is not dropped on write"
9332         # Now test that MDS is updated too
9333         cancel_lru_locks mdc
9334         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9335                 error "S/gid is not dropped on MDS"
9336         rm -f $DIR/$tfile
9337 }
9338 run_test 72a "Test that remove suid works properly (bug5695) ===="
9339
9340 test_72b() { # bug 24226 -- keep mode setting when size is not changing
9341         local perm
9342
9343         [ "$RUNAS_ID" = "$UID" ] &&
9344                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9345         [ "$RUNAS_ID" -eq 0 ] &&
9346                 skip_env "RUNAS_ID = 0 -- skipping"
9347         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9348         # Check that testing environment is properly set up. Skip if not
9349         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
9350                 skip_env "User $RUNAS_ID does not exist - skipping"
9351
9352         touch $DIR/${tfile}-f{g,u}
9353         test_mkdir $DIR/${tfile}-dg
9354         test_mkdir $DIR/${tfile}-du
9355         chmod 770 $DIR/${tfile}-{f,d}{g,u}
9356         chmod g+s $DIR/${tfile}-{f,d}g
9357         chmod u+s $DIR/${tfile}-{f,d}u
9358         for perm in 777 2777 4777; do
9359                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
9360                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
9361                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
9362                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
9363         done
9364         true
9365 }
9366 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
9367
9368 # bug 3462 - multiple simultaneous MDC requests
9369 test_73() {
9370         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9371
9372         test_mkdir $DIR/d73-1
9373         test_mkdir $DIR/d73-2
9374         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
9375         pid1=$!
9376
9377         lctl set_param fail_loc=0x80000129
9378         $MULTIOP $DIR/d73-1/f73-2 Oc &
9379         sleep 1
9380         lctl set_param fail_loc=0
9381
9382         $MULTIOP $DIR/d73-2/f73-3 Oc &
9383         pid3=$!
9384
9385         kill -USR1 $pid1
9386         wait $pid1 || return 1
9387
9388         sleep 25
9389
9390         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
9391         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
9392         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
9393
9394         rm -rf $DIR/d73-*
9395 }
9396 run_test 73 "multiple MDC requests (should not deadlock)"
9397
9398 test_74a() { # bug 6149, 6184
9399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9400
9401         touch $DIR/f74a
9402         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9403         #
9404         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9405         # will spin in a tight reconnection loop
9406         $LCTL set_param fail_loc=0x8000030e
9407         # get any lock that won't be difficult - lookup works.
9408         ls $DIR/f74a
9409         $LCTL set_param fail_loc=0
9410         rm -f $DIR/f74a
9411         true
9412 }
9413 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
9414
9415 test_74b() { # bug 13310
9416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9417
9418         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9419         #
9420         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9421         # will spin in a tight reconnection loop
9422         $LCTL set_param fail_loc=0x8000030e
9423         # get a "difficult" lock
9424         touch $DIR/f74b
9425         $LCTL set_param fail_loc=0
9426         rm -f $DIR/f74b
9427         true
9428 }
9429 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
9430
9431 test_74c() {
9432         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9433
9434         #define OBD_FAIL_LDLM_NEW_LOCK
9435         $LCTL set_param fail_loc=0x319
9436         touch $DIR/$tfile && error "touch successful"
9437         $LCTL set_param fail_loc=0
9438         true
9439 }
9440 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
9441
9442 slab_lic=/sys/kernel/slab/lustre_inode_cache
9443 num_objects() {
9444         [ -f $slab_lic/shrink ] && echo 1 > $slab_lic/shrink
9445         [ -f $slab_lic/objects ] && awk '{ print $1 }' $slab_lic/objects ||
9446                 awk '/lustre_inode_cache/ { print $2; exit }' /proc/slabinfo
9447 }
9448
9449 test_76a() { # Now for b=20433, added originally in b=1443
9450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9451
9452         cancel_lru_locks osc
9453         # there may be some slab objects cached per core
9454         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
9455         local before=$(num_objects)
9456         local count=$((512 * cpus))
9457         [ "$SLOW" = "no" ] && count=$((128 * cpus))
9458         local margin=$((count / 10))
9459         if [[ -f $slab_lic/aliases ]]; then
9460                 local aliases=$(cat $slab_lic/aliases)
9461                 (( aliases > 0 )) && margin=$((margin * aliases))
9462         fi
9463
9464         echo "before slab objects: $before"
9465         for i in $(seq $count); do
9466                 touch $DIR/$tfile
9467                 rm -f $DIR/$tfile
9468         done
9469         cancel_lru_locks osc
9470         local after=$(num_objects)
9471         echo "created: $count, after slab objects: $after"
9472         # shared slab counts are not very accurate, allow significant margin
9473         # the main goal is that the cache growth is not permanently > $count
9474         while (( after > before + margin )); do
9475                 sleep 1
9476                 after=$(num_objects)
9477                 wait=$((wait + 1))
9478                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9479                 if (( wait > 60 )); then
9480                         error "inode slab grew from $before+$margin to $after"
9481                 fi
9482         done
9483 }
9484 run_test 76a "confirm clients recycle inodes properly ===="
9485
9486 test_76b() {
9487         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9488         [ $CLIENT_VERSION -ge $(version_code 2.13.55) ] || skip "not supported"
9489
9490         local count=512
9491         local before=$(num_objects)
9492
9493         for i in $(seq $count); do
9494                 mkdir $DIR/$tdir
9495                 rmdir $DIR/$tdir
9496         done
9497
9498         local after=$(num_objects)
9499         local wait=0
9500
9501         while (( after > before )); do
9502                 sleep 1
9503                 after=$(num_objects)
9504                 wait=$((wait + 1))
9505                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9506                 if (( wait > 60 )); then
9507                         error "inode slab grew from $before to $after"
9508                 fi
9509         done
9510
9511         echo "slab objects before: $before, after: $after"
9512 }
9513 run_test 76b "confirm clients recycle directory inodes properly ===="
9514
9515 export ORIG_CSUM=""
9516 set_checksums()
9517 {
9518         # Note: in sptlrpc modes which enable its own bulk checksum, the
9519         # original crc32_le bulk checksum will be automatically disabled,
9520         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
9521         # will be checked by sptlrpc code against sptlrpc bulk checksum.
9522         # In this case set_checksums() will not be no-op, because sptlrpc
9523         # bulk checksum will be enabled all through the test.
9524
9525         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
9526         lctl set_param -n osc.*.checksums $1
9527         return 0
9528 }
9529
9530 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9531                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
9532 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9533                              tr -d [] | head -n1)}
9534 set_checksum_type()
9535 {
9536         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
9537         rc=$?
9538         log "set checksum type to $1, rc = $rc"
9539         return $rc
9540 }
9541
9542 get_osc_checksum_type()
9543 {
9544         # arugment 1: OST name, like OST0000
9545         ost=$1
9546         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
9547                         sed 's/.*\[\(.*\)\].*/\1/g')
9548         rc=$?
9549         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
9550         echo $checksum_type
9551 }
9552
9553 F77_TMP=$TMP/f77-temp
9554 F77SZ=8
9555 setup_f77() {
9556         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
9557                 error "error writing to $F77_TMP"
9558 }
9559
9560 test_77a() { # bug 10889
9561         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9562         $GSS && skip_env "could not run with gss"
9563
9564         [ ! -f $F77_TMP ] && setup_f77
9565         set_checksums 1
9566         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
9567         set_checksums 0
9568         rm -f $DIR/$tfile
9569 }
9570 run_test 77a "normal checksum read/write operation"
9571
9572 test_77b() { # bug 10889
9573         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9574         $GSS && skip_env "could not run with gss"
9575
9576         [ ! -f $F77_TMP ] && setup_f77
9577         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9578         $LCTL set_param fail_loc=0x80000409
9579         set_checksums 1
9580
9581         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9582                 error "dd error: $?"
9583         $LCTL set_param fail_loc=0
9584
9585         for algo in $CKSUM_TYPES; do
9586                 cancel_lru_locks osc
9587                 set_checksum_type $algo
9588                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9589                 $LCTL set_param fail_loc=0x80000408
9590                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
9591                 $LCTL set_param fail_loc=0
9592         done
9593         set_checksums 0
9594         set_checksum_type $ORIG_CSUM_TYPE
9595         rm -f $DIR/$tfile
9596 }
9597 run_test 77b "checksum error on client write, read"
9598
9599 cleanup_77c() {
9600         trap 0
9601         set_checksums 0
9602         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
9603         $check_ost &&
9604                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
9605         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
9606         $check_ost && [ -n "$ost_file_prefix" ] &&
9607                 do_facet ost1 rm -f ${ost_file_prefix}\*
9608 }
9609
9610 test_77c() {
9611         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9612         $GSS && skip_env "could not run with gss"
9613         remote_ost_nodsh && skip "remote OST with nodsh"
9614
9615         local bad1
9616         local osc_file_prefix
9617         local osc_file
9618         local check_ost=false
9619         local ost_file_prefix
9620         local ost_file
9621         local orig_cksum
9622         local dump_cksum
9623         local fid
9624
9625         # ensure corruption will occur on first OSS/OST
9626         $LFS setstripe -i 0 $DIR/$tfile
9627
9628         [ ! -f $F77_TMP ] && setup_f77
9629         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9630                 error "dd write error: $?"
9631         fid=$($LFS path2fid $DIR/$tfile)
9632
9633         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
9634         then
9635                 check_ost=true
9636                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
9637                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
9638         else
9639                 echo "OSS do not support bulk pages dump upon error"
9640         fi
9641
9642         osc_file_prefix=$($LCTL get_param -n debug_path)
9643         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
9644
9645         trap cleanup_77c EXIT
9646
9647         set_checksums 1
9648         # enable bulk pages dump upon error on Client
9649         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
9650         # enable bulk pages dump upon error on OSS
9651         $check_ost &&
9652                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
9653
9654         # flush Client cache to allow next read to reach OSS
9655         cancel_lru_locks osc
9656
9657         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
9658         $LCTL set_param fail_loc=0x80000408
9659         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
9660         $LCTL set_param fail_loc=0
9661
9662         rm -f $DIR/$tfile
9663
9664         # check cksum dump on Client
9665         osc_file=$(ls ${osc_file_prefix}*)
9666         [ -n "$osc_file" ] || error "no checksum dump file on Client"
9667         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
9668         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
9669         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
9670         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
9671                      cksum)
9672         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
9673         [[ "$orig_cksum" == "$dump_cksum" ]] ||
9674                 error "dump content does not match on Client"
9675
9676         $check_ost || skip "No need to check cksum dump on OSS"
9677
9678         # check cksum dump on OSS
9679         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
9680         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
9681         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
9682         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
9683         [[ "$orig_cksum" == "$dump_cksum" ]] ||
9684                 error "dump content does not match on OSS"
9685
9686         cleanup_77c
9687 }
9688 run_test 77c "checksum error on client read with debug"
9689
9690 test_77d() { # bug 10889
9691         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9692         $GSS && skip_env "could not run with gss"
9693
9694         stack_trap "rm -f $DIR/$tfile"
9695         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9696         $LCTL set_param fail_loc=0x80000409
9697         set_checksums 1
9698         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
9699                 error "direct write: rc=$?"
9700         $LCTL set_param fail_loc=0
9701         set_checksums 0
9702
9703         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9704         $LCTL set_param fail_loc=0x80000408
9705         set_checksums 1
9706         cancel_lru_locks osc
9707         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
9708                 error "direct read: rc=$?"
9709         $LCTL set_param fail_loc=0
9710         set_checksums 0
9711 }
9712 run_test 77d "checksum error on OST direct write, read"
9713
9714 test_77f() { # bug 10889
9715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9716         $GSS && skip_env "could not run with gss"
9717
9718         set_checksums 1
9719         stack_trap "rm -f $DIR/$tfile"
9720         for algo in $CKSUM_TYPES; do
9721                 cancel_lru_locks osc
9722                 set_checksum_type $algo
9723                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9724                 $LCTL set_param fail_loc=0x409
9725                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
9726                         error "direct write succeeded"
9727                 $LCTL set_param fail_loc=0
9728         done
9729         set_checksum_type $ORIG_CSUM_TYPE
9730         set_checksums 0
9731 }
9732 run_test 77f "repeat checksum error on write (expect error)"
9733
9734 test_77g() { # bug 10889
9735         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9736         $GSS && skip_env "could not run with gss"
9737         remote_ost_nodsh && skip "remote OST with nodsh"
9738
9739         [ ! -f $F77_TMP ] && setup_f77
9740
9741         local file=$DIR/$tfile
9742         stack_trap "rm -f $file" EXIT
9743
9744         $LFS setstripe -c 1 -i 0 $file
9745         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
9746         do_facet ost1 lctl set_param fail_loc=0x8000021a
9747         set_checksums 1
9748         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
9749                 error "write error: rc=$?"
9750         do_facet ost1 lctl set_param fail_loc=0
9751         set_checksums 0
9752
9753         cancel_lru_locks osc
9754         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
9755         do_facet ost1 lctl set_param fail_loc=0x8000021b
9756         set_checksums 1
9757         cmp $F77_TMP $file || error "file compare failed"
9758         do_facet ost1 lctl set_param fail_loc=0
9759         set_checksums 0
9760 }
9761 run_test 77g "checksum error on OST write, read"
9762
9763 test_77k() { # LU-10906
9764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9765         $GSS && skip_env "could not run with gss"
9766
9767         local cksum_param="osc.$FSNAME*.checksums"
9768         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
9769         local checksum
9770         local i
9771
9772         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
9773         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM || true"
9774         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM"
9775
9776         for i in 0 1; do
9777                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
9778                         error "failed to set checksum=$i on MGS"
9779                 wait_update $HOSTNAME "$get_checksum" $i
9780                 #remount
9781                 echo "remount client, checksum should be $i"
9782                 remount_client $MOUNT || error "failed to remount client"
9783                 checksum=$(eval $get_checksum)
9784                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
9785         done
9786         # remove persistent param to avoid races with checksum mountopt below
9787         do_facet mgs $LCTL set_param -P -d $cksum_param ||
9788                 error "failed to delete checksum on MGS"
9789
9790         for opt in "checksum" "nochecksum"; do
9791                 #remount with mount option
9792                 echo "remount client with option $opt, checksum should be $i"
9793                 umount_client $MOUNT || error "failed to umount client"
9794                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
9795                         error "failed to mount client with option '$opt'"
9796                 checksum=$(eval $get_checksum)
9797                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
9798                 i=$((i - 1))
9799         done
9800
9801         remount_client $MOUNT || error "failed to remount client"
9802 }
9803 run_test 77k "enable/disable checksum correctly"
9804
9805 test_77l() {
9806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9807         $GSS && skip_env "could not run with gss"
9808
9809         set_checksums 1
9810         stack_trap "set_checksums $ORIG_CSUM" EXIT
9811         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
9812
9813         set_checksum_type invalid && error "unexpected success of invalid checksum type"
9814
9815         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9816         for algo in $CKSUM_TYPES; do
9817                 set_checksum_type $algo || error "fail to set checksum type $algo"
9818                 osc_algo=$(get_osc_checksum_type OST0000)
9819                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
9820
9821                 # no locks, no reqs to let the connection idle
9822                 cancel_lru_locks osc
9823                 lru_resize_disable osc
9824                 wait_osc_import_state client ost1 IDLE
9825
9826                 # ensure ost1 is connected
9827                 stat $DIR/$tfile >/dev/null || error "can't stat"
9828                 wait_osc_import_state client ost1 FULL
9829
9830                 osc_algo=$(get_osc_checksum_type OST0000)
9831                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
9832         done
9833         return 0
9834 }
9835 run_test 77l "preferred checksum type is remembered after reconnected"
9836
9837 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
9838 rm -f $F77_TMP
9839 unset F77_TMP
9840
9841 test_77m() {
9842         (( $CLIENT_VERSION >= $(version_code 2.14.52) )) ||
9843                 skip "Need at least version 2.14.52"
9844         local param=checksum_speed
9845
9846         $LCTL get_param $param || error "reading $param failed"
9847
9848         csum_speeds=$($LCTL get_param -n $param)
9849
9850         [[ "$csum_speeds" =~ "adler32" && "$csum_speeds" =~ "crc32" ]] ||
9851                 error "known checksum types are missing"
9852 }
9853 run_test 77m "Verify checksum_speed is correctly read"
9854
9855 check_filefrag_77n() {
9856         local nr_ext=0
9857         local starts=()
9858         local ends=()
9859
9860         while read extidx a b start end rest; do
9861                 if [[ "${extidx}" =~ ^[0-9]+: ]]; then
9862                         nr_ext=$(( $nr_ext + 1 ))
9863                         starts+=( ${start%..} )
9864                         ends+=( ${end%:} )
9865                 fi
9866         done < <( filefrag -sv $1 )
9867
9868         [[ $nr_ext -eq 2 ]] && [[ "${starts[-1]}" == $(( ${ends[0]} + 1 )) ]] && return 0
9869         return 1
9870 }
9871
9872 test_77n() {
9873         [[ "$CKSUM_TYPES" =~ t10 ]] || skip "no T10 checksum support on osc"
9874
9875         touch $DIR/$tfile
9876         $TRUNCATE $DIR/$tfile 0
9877         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=0
9878         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=2
9879         check_filefrag_77n $DIR/$tfile ||
9880                 skip "$tfile blocks not contiguous around hole"
9881
9882         set_checksums 1
9883         stack_trap "set_checksums $ORIG_CSUM" EXIT
9884         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
9885         stack_trap "rm -f $DIR/$tfile"
9886
9887         for algo in $CKSUM_TYPES; do
9888                 if [[ "$algo" =~ ^t10 ]]; then
9889                         set_checksum_type $algo ||
9890                                 error "fail to set checksum type $algo"
9891                         dd if=$DIR/$tfile of=/dev/null bs=12k count=1 iflag=direct ||
9892                                 error "fail to read $tfile with $algo"
9893                 fi
9894         done
9895         rm -f $DIR/$tfile
9896         return 0
9897 }
9898 run_test 77n "Verify read from a hole inside contiguous blocks with T10PI"
9899
9900 test_77o() {
9901         (( $CLIENT_VERSION >= $(version_code 2.14.54) )) ||
9902                 skip "Need at least version 2.14.54"
9903         local ofd=obdfilter
9904         local mdt=mdt
9905
9906         # print OST checksum_type
9907         echo "$ofd.$FSNAME-*.checksum_type:"
9908         do_nodes $(comma_list $(osts_nodes)) \
9909                 $LCTL get_param -n $ofd.$FSNAME-*.checksum_type
9910
9911         # print MDT checksum_type
9912         echo "$mdt.$FSNAME-*.checksum_type:"
9913         do_nodes $(comma_list $(mdts_nodes)) \
9914                 $LCTL get_param -n $mdt.$FSNAME-*.checksum_type
9915
9916         local o_count=$(do_nodes $(comma_list $(osts_nodes)) \
9917                    $LCTL get_param -n $ofd.$FSNAME-*.checksum_type | wc -l)
9918
9919         (( $o_count == $OSTCOUNT )) ||
9920                 error "found $o_count checksums, not \$MDSCOUNT=$OSTCOUNT"
9921
9922         local m_count=$(do_nodes $(comma_list $(mdts_nodes)) \
9923                    $LCTL get_param -n $mdt.$FSNAME-*.checksum_type | wc -l)
9924
9925         (( $m_count == $MDSCOUNT )) ||
9926                 error "found $m_count checksums, not \$MDSCOUNT=$MDSCOUNT"
9927 }
9928 run_test 77o "Verify checksum_type for server (mdt and ofd(obdfilter))"
9929
9930 cleanup_test_78() {
9931         trap 0
9932         rm -f $DIR/$tfile
9933 }
9934
9935 test_78() { # bug 10901
9936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9937         remote_ost || skip_env "local OST"
9938
9939         NSEQ=5
9940         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
9941         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
9942         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
9943         echo "MemTotal: $MEMTOTAL"
9944
9945         # reserve 256MB of memory for the kernel and other running processes,
9946         # and then take 1/2 of the remaining memory for the read/write buffers.
9947         if [ $MEMTOTAL -gt 512 ] ;then
9948                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
9949         else
9950                 # for those poor memory-starved high-end clusters...
9951                 MEMTOTAL=$((MEMTOTAL / 2))
9952         fi
9953         echo "Mem to use for directio: $MEMTOTAL"
9954
9955         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
9956         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
9957         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
9958         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
9959                 head -n1)
9960         echo "Smallest OST: $SMALLESTOST"
9961         [[ $SMALLESTOST -lt 10240 ]] &&
9962                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
9963
9964         trap cleanup_test_78 EXIT
9965
9966         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
9967                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
9968
9969         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
9970         echo "File size: $F78SIZE"
9971         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
9972         for i in $(seq 1 $NSEQ); do
9973                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
9974                 echo directIO rdwr round $i of $NSEQ
9975                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
9976         done
9977
9978         cleanup_test_78
9979 }
9980 run_test 78 "handle large O_DIRECT writes correctly ============"
9981
9982 test_79() { # bug 12743
9983         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9984
9985         wait_delete_completed
9986
9987         BKTOTAL=$(calc_osc_kbytes kbytestotal)
9988         BKFREE=$(calc_osc_kbytes kbytesfree)
9989         BKAVAIL=$(calc_osc_kbytes kbytesavail)
9990
9991         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
9992         DFTOTAL=`echo $STRING | cut -d, -f1`
9993         DFUSED=`echo $STRING  | cut -d, -f2`
9994         DFAVAIL=`echo $STRING | cut -d, -f3`
9995         DFFREE=$(($DFTOTAL - $DFUSED))
9996
9997         ALLOWANCE=$((64 * $OSTCOUNT))
9998
9999         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
10000            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
10001                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
10002         fi
10003         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
10004            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
10005                 error "df free($DFFREE) mismatch OST free($BKFREE)"
10006         fi
10007         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
10008            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
10009                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
10010         fi
10011 }
10012 run_test 79 "df report consistency check ======================="
10013
10014 test_80() { # bug 10718
10015         remote_ost_nodsh && skip "remote OST with nodsh"
10016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10017
10018         # relax strong synchronous semantics for slow backends like ZFS
10019         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
10020                 local soc="obdfilter.*.sync_lock_cancel"
10021                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10022
10023                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
10024                 if [ -z "$save" ]; then
10025                         soc="obdfilter.*.sync_on_lock_cancel"
10026                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10027                 fi
10028
10029                 if [ "$save" != "never" ]; then
10030                         local hosts=$(comma_list $(osts_nodes))
10031
10032                         do_nodes $hosts $LCTL set_param $soc=never
10033                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
10034                 fi
10035         fi
10036
10037         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
10038         sync; sleep 1; sync
10039         local before=$(date +%s)
10040         cancel_lru_locks osc
10041         local after=$(date +%s)
10042         local diff=$((after - before))
10043         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
10044
10045         rm -f $DIR/$tfile
10046 }
10047 run_test 80 "Page eviction is equally fast at high offsets too"
10048
10049 test_81a() { # LU-456
10050         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10051         remote_ost_nodsh && skip "remote OST with nodsh"
10052
10053         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10054         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
10055         do_facet ost1 lctl set_param fail_loc=0x80000228
10056
10057         # write should trigger a retry and success
10058         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10059         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10060         RC=$?
10061         if [ $RC -ne 0 ] ; then
10062                 error "write should success, but failed for $RC"
10063         fi
10064 }
10065 run_test 81a "OST should retry write when get -ENOSPC ==============="
10066
10067 test_81b() { # LU-456
10068         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10069         remote_ost_nodsh && skip "remote OST with nodsh"
10070
10071         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10072         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
10073         do_facet ost1 lctl set_param fail_loc=0x228
10074
10075         # write should retry several times and return -ENOSPC finally
10076         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10077         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10078         RC=$?
10079         ENOSPC=28
10080         if [ $RC -ne $ENOSPC ] ; then
10081                 error "dd should fail for -ENOSPC, but succeed."
10082         fi
10083 }
10084 run_test 81b "OST should return -ENOSPC when retry still fails ======="
10085
10086 test_99() {
10087         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
10088
10089         test_mkdir $DIR/$tdir.cvsroot
10090         chown $RUNAS_ID $DIR/$tdir.cvsroot
10091
10092         cd $TMP
10093         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
10094
10095         cd /etc/init.d
10096         # some versions of cvs import exit(1) when asked to import links or
10097         # files they can't read.  ignore those files.
10098         local toignore=$(find . -type l -printf '-I %f\n' -o \
10099                          ! -perm /4 -printf '-I %f\n')
10100         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
10101                 $tdir.reposname vtag rtag
10102
10103         cd $DIR
10104         test_mkdir $DIR/$tdir.reposname
10105         chown $RUNAS_ID $DIR/$tdir.reposname
10106         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
10107
10108         cd $DIR/$tdir.reposname
10109         $RUNAS touch foo99
10110         $RUNAS cvs add -m 'addmsg' foo99
10111         $RUNAS cvs update
10112         $RUNAS cvs commit -m 'nomsg' foo99
10113         rm -fr $DIR/$tdir.cvsroot
10114 }
10115 run_test 99 "cvs strange file/directory operations"
10116
10117 test_100() {
10118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10119         [[ "$NETTYPE" =~ tcp ]] ||
10120                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
10121         remote_ost_nodsh && skip "remote OST with nodsh"
10122         remote_mds_nodsh && skip "remote MDS with nodsh"
10123         remote_servers ||
10124                 skip "useless for local single node setup"
10125
10126         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
10127                 [ "$PROT" != "tcp" ] && continue
10128                 RPORT=$(echo $REMOTE | cut -d: -f2)
10129                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
10130
10131                 rc=0
10132                 LPORT=`echo $LOCAL | cut -d: -f2`
10133                 if [ $LPORT -ge 1024 ]; then
10134                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
10135                         netstat -tna
10136                         error_exit "local: $LPORT > 1024, remote: $RPORT"
10137                 fi
10138         done
10139         [ "$rc" = 0 ] || error_exit "privileged port not found" )
10140 }
10141 run_test 100 "check local port using privileged port ==========="
10142
10143 function get_named_value()
10144 {
10145     local tag=$1
10146
10147     grep -w "$tag" | sed "s/^$tag  *\([0-9]*\)  *.*/\1/"
10148 }
10149
10150 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
10151                    awk '/^max_cached_mb/ { print $2 }')
10152
10153 cleanup_101a() {
10154         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
10155         trap 0
10156 }
10157
10158 test_101a() {
10159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10160
10161         local s
10162         local discard
10163         local nreads=10000
10164         local cache_limit=32
10165
10166         $LCTL set_param -n osc.*-osc*.rpc_stats=0
10167         trap cleanup_101a EXIT
10168         $LCTL set_param -n llite.*.read_ahead_stats=0
10169         $LCTL set_param -n llite.*.max_cached_mb=$cache_limit
10170
10171         #
10172         # randomly read 10000 of 64K chunks from file 3x 32MB in size
10173         #
10174         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
10175         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
10176
10177         discard=0
10178         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
10179                    get_named_value 'read.but.discarded'); do
10180                         discard=$(($discard + $s))
10181         done
10182         cleanup_101a
10183
10184         $LCTL get_param osc.*-osc*.rpc_stats
10185         $LCTL get_param llite.*.read_ahead_stats
10186
10187         # Discard is generally zero, but sometimes a few random reads line up
10188         # and trigger larger readahead, which is wasted & leads to discards.
10189         if [[ $(($discard)) -gt $nreads ]]; then
10190                 error "too many ($discard) discarded pages"
10191         fi
10192         rm -f $DIR/$tfile || true
10193 }
10194 run_test 101a "check read-ahead for random reads"
10195
10196 setup_test101bc() {
10197         test_mkdir $DIR/$tdir
10198         local ssize=$1
10199         local FILE_LENGTH=$2
10200         STRIPE_OFFSET=0
10201
10202         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
10203
10204         local list=$(comma_list $(osts_nodes))
10205         set_osd_param $list '' read_cache_enable 0
10206         set_osd_param $list '' writethrough_cache_enable 0
10207
10208         trap cleanup_test101bc EXIT
10209         # prepare the read-ahead file
10210         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
10211
10212         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
10213                                 count=$FILE_SIZE_MB 2> /dev/null
10214
10215 }
10216
10217 cleanup_test101bc() {
10218         trap 0
10219         rm -rf $DIR/$tdir
10220         rm -f $DIR/$tfile
10221
10222         local list=$(comma_list $(osts_nodes))
10223         set_osd_param $list '' read_cache_enable 1
10224         set_osd_param $list '' writethrough_cache_enable 1
10225 }
10226
10227 calc_total() {
10228         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
10229 }
10230
10231 ra_check_101() {
10232         local READ_SIZE=$1
10233         local STRIPE_SIZE=$2
10234         local FILE_LENGTH=$3
10235         local RA_INC=1048576
10236         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
10237         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
10238                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
10239         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
10240                   get_named_value 'read.but.discarded' | calc_total)
10241         if [[ $DISCARD -gt $discard_limit ]]; then
10242                 $LCTL get_param llite.*.read_ahead_stats
10243                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
10244         else
10245                 echo "Read-ahead success for size ${READ_SIZE}"
10246         fi
10247 }
10248
10249 test_101b() {
10250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10251         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10252
10253         local STRIPE_SIZE=1048576
10254         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
10255
10256         if [ $SLOW == "yes" ]; then
10257                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
10258         else
10259                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
10260         fi
10261
10262         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
10263
10264         # prepare the read-ahead file
10265         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10266         cancel_lru_locks osc
10267         for BIDX in 2 4 8 16 32 64 128 256
10268         do
10269                 local BSIZE=$((BIDX*4096))
10270                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
10271                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
10272                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
10273                 $LCTL set_param -n llite.*.read_ahead_stats=0
10274                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
10275                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
10276                 cancel_lru_locks osc
10277                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
10278         done
10279         cleanup_test101bc
10280         true
10281 }
10282 run_test 101b "check stride-io mode read-ahead ================="
10283
10284 test_101c() {
10285         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10286
10287         local STRIPE_SIZE=1048576
10288         local FILE_LENGTH=$((STRIPE_SIZE*100))
10289         local nreads=10000
10290         local rsize=65536
10291         local osc_rpc_stats
10292
10293         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10294
10295         cancel_lru_locks osc
10296         $LCTL set_param osc.*.rpc_stats=0
10297         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
10298         $LCTL get_param osc.*.rpc_stats
10299         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
10300                 local stats=$($LCTL get_param -n $osc_rpc_stats)
10301                 local lines=$(echo "$stats" | awk 'END {print NR;}')
10302                 local size
10303
10304                 if [ $lines -le 20 ]; then
10305                         echo "continue debug"
10306                         continue
10307                 fi
10308                 for size in 1 2 4 8; do
10309                         local rpc=$(echo "$stats" |
10310                                     awk '($1 == "'$size':") {print $2; exit; }')
10311                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
10312                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
10313                 done
10314                 echo "$osc_rpc_stats check passed!"
10315         done
10316         cleanup_test101bc
10317         true
10318 }
10319 run_test 101c "check stripe_size aligned read-ahead"
10320
10321 test_101d() {
10322         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10323
10324         local file=$DIR/$tfile
10325         local sz_MB=${FILESIZE_101d:-80}
10326         local ra_MB=${READAHEAD_MB:-40}
10327
10328         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
10329         [ $free_MB -lt $sz_MB ] &&
10330                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
10331
10332         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
10333         $LFS setstripe -c -1 $file || error "setstripe failed"
10334
10335         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
10336         echo Cancel LRU locks on lustre client to flush the client cache
10337         cancel_lru_locks osc
10338
10339         echo Disable read-ahead
10340         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10341         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10342         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb=$old_RA" EXIT
10343         $LCTL get_param -n llite.*.max_read_ahead_mb
10344
10345         echo "Reading the test file $file with read-ahead disabled"
10346         local sz_KB=$((sz_MB * 1024 / 4))
10347         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
10348         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
10349         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10350                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10351
10352         echo "Cancel LRU locks on lustre client to flush the client cache"
10353         cancel_lru_locks osc
10354         echo Enable read-ahead with ${ra_MB}MB
10355         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
10356
10357         echo "Reading the test file $file with read-ahead enabled"
10358         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10359                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10360
10361         echo "read-ahead disabled time read $raOFF"
10362         echo "read-ahead enabled time read $raON"
10363
10364         rm -f $file
10365         wait_delete_completed
10366
10367         # use awk for this check instead of bash because it handles decimals
10368         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
10369                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
10370 }
10371 run_test 101d "file read with and without read-ahead enabled"
10372
10373 test_101e() {
10374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10375
10376         local file=$DIR/$tfile
10377         local size_KB=500  #KB
10378         local count=100
10379         local bsize=1024
10380
10381         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
10382         local need_KB=$((count * size_KB))
10383         [[ $free_KB -le $need_KB ]] &&
10384                 skip_env "Need free space $need_KB, have $free_KB"
10385
10386         echo "Creating $count ${size_KB}K test files"
10387         for ((i = 0; i < $count; i++)); do
10388                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
10389         done
10390
10391         echo "Cancel LRU locks on lustre client to flush the client cache"
10392         cancel_lru_locks $OSC
10393
10394         echo "Reset readahead stats"
10395         $LCTL set_param -n llite.*.read_ahead_stats=0
10396
10397         for ((i = 0; i < $count; i++)); do
10398                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
10399         done
10400
10401         $LCTL get_param llite.*.max_cached_mb
10402         $LCTL get_param llite.*.read_ahead_stats
10403         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10404                      get_named_value 'misses' | calc_total)
10405
10406         for ((i = 0; i < $count; i++)); do
10407                 rm -rf $file.$i 2>/dev/null
10408         done
10409
10410         #10000 means 20% reads are missing in readahead
10411         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
10412 }
10413 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
10414
10415 test_101f() {
10416         which iozone || skip_env "no iozone installed"
10417
10418         local old_debug=$($LCTL get_param debug)
10419         old_debug=${old_debug#*=}
10420         $LCTL set_param debug="reada mmap"
10421
10422         # create a test file
10423         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
10424
10425         echo Cancel LRU locks on lustre client to flush the client cache
10426         cancel_lru_locks osc
10427
10428         echo Reset readahead stats
10429         $LCTL set_param -n llite.*.read_ahead_stats=0
10430
10431         echo mmap read the file with small block size
10432         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
10433                 > /dev/null 2>&1
10434
10435         echo checking missing pages
10436         $LCTL get_param llite.*.read_ahead_stats
10437         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10438                         get_named_value 'misses' | calc_total)
10439
10440         $LCTL set_param debug="$old_debug"
10441         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
10442         rm -f $DIR/$tfile
10443 }
10444 run_test 101f "check mmap read performance"
10445
10446 test_101g_brw_size_test() {
10447         local mb=$1
10448         local pages=$((mb * 1048576 / PAGE_SIZE))
10449         local file=$DIR/$tfile
10450
10451         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
10452                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
10453         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
10454                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
10455                         return 2
10456         done
10457
10458         stack_trap "rm -f $file" EXIT
10459         $LCTL set_param -n osc.*.rpc_stats=0
10460
10461         # 10 RPCs should be enough for the test
10462         local count=10
10463         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
10464                 { error "dd write ${mb} MB blocks failed"; return 3; }
10465         cancel_lru_locks osc
10466         dd of=/dev/null if=$file bs=${mb}M count=$count ||
10467                 { error "dd write ${mb} MB blocks failed"; return 4; }
10468
10469         # calculate number of full-sized read and write RPCs
10470         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
10471                 sed -n '/pages per rpc/,/^$/p' |
10472                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
10473                 END { print reads,writes }'))
10474         # allow one extra full-sized read RPC for async readahead
10475         [[ ${rpcs[0]} == $count || ${rpcs[0]} == $((count + 1)) ]] ||
10476                 { error "${rpcs[0]} != $count read RPCs"; return 5; }
10477         [[ ${rpcs[1]} == $count ]] ||
10478                 { error "${rpcs[1]} != $count write RPCs"; return 6; }
10479 }
10480
10481 test_101g() {
10482         remote_ost_nodsh && skip "remote OST with nodsh"
10483
10484         local rpcs
10485         local osts=$(get_facets OST)
10486         local list=$(comma_list $(osts_nodes))
10487         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10488         local brw_size="obdfilter.*.brw_size"
10489
10490         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10491
10492         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
10493
10494         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
10495                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
10496                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
10497            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
10498                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
10499                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
10500
10501                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
10502                         suffix="M"
10503
10504                 if [[ $orig_mb -lt 16 ]]; then
10505                         save_lustre_params $osts "$brw_size" > $p
10506                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
10507                                 error "set 16MB RPC size failed"
10508
10509                         echo "remount client to enable new RPC size"
10510                         remount_client $MOUNT || error "remount_client failed"
10511                 fi
10512
10513                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
10514                 # should be able to set brw_size=12, but no rpc_stats for that
10515                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
10516         fi
10517
10518         test_101g_brw_size_test 4 || error "4MB RPC test failed"
10519
10520         if [[ $orig_mb -lt 16 ]]; then
10521                 restore_lustre_params < $p
10522                 remount_client $MOUNT || error "remount_client restore failed"
10523         fi
10524
10525         rm -f $p $DIR/$tfile
10526 }
10527 run_test 101g "Big bulk(4/16 MiB) readahead"
10528
10529 test_101h() {
10530         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10531
10532         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
10533                 error "dd 70M file failed"
10534         echo Cancel LRU locks on lustre client to flush the client cache
10535         cancel_lru_locks osc
10536
10537         echo "Reset readahead stats"
10538         $LCTL set_param -n llite.*.read_ahead_stats 0
10539
10540         echo "Read 10M of data but cross 64M bundary"
10541         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
10542         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10543                      get_named_value 'misses' | calc_total)
10544         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
10545         rm -f $p $DIR/$tfile
10546 }
10547 run_test 101h "Readahead should cover current read window"
10548
10549 test_101i() {
10550         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
10551                 error "dd 10M file failed"
10552
10553         local max_per_file_mb=$($LCTL get_param -n \
10554                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
10555         cancel_lru_locks osc
10556         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
10557         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
10558                 error "set max_read_ahead_per_file_mb to 1 failed"
10559
10560         echo "Reset readahead stats"
10561         $LCTL set_param llite.*.read_ahead_stats=0
10562
10563         dd if=$DIR/$tfile of=/dev/null bs=2M
10564
10565         $LCTL get_param llite.*.read_ahead_stats
10566         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10567                      awk '/misses/ { print $2 }')
10568         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
10569         rm -f $DIR/$tfile
10570 }
10571 run_test 101i "allow current readahead to exceed reservation"
10572
10573 test_101j() {
10574         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
10575                 error "setstripe $DIR/$tfile failed"
10576         local file_size=$((1048576 * 16))
10577         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10578         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
10579
10580         echo Disable read-ahead
10581         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10582
10583         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
10584         for blk in $PAGE_SIZE 1048576 $file_size; do
10585                 cancel_lru_locks osc
10586                 echo "Reset readahead stats"
10587                 $LCTL set_param -n llite.*.read_ahead_stats=0
10588                 local count=$(($file_size / $blk))
10589                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
10590                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10591                              get_named_value 'failed.to.fast.read' | calc_total)
10592                 $LCTL get_param -n llite.*.read_ahead_stats
10593                 [ $miss -eq $count ] || error "expected $count got $miss"
10594         done
10595
10596         rm -f $p $DIR/$tfile
10597 }
10598 run_test 101j "A complete read block should be submitted when no RA"
10599
10600 setup_test102() {
10601         test_mkdir $DIR/$tdir
10602         chown $RUNAS_ID $DIR/$tdir
10603         STRIPE_SIZE=65536
10604         STRIPE_OFFSET=1
10605         STRIPE_COUNT=$OSTCOUNT
10606         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
10607
10608         trap cleanup_test102 EXIT
10609         cd $DIR
10610         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
10611         cd $DIR/$tdir
10612         for num in 1 2 3 4; do
10613                 for count in $(seq 1 $STRIPE_COUNT); do
10614                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
10615                                 local size=`expr $STRIPE_SIZE \* $num`
10616                                 local file=file"$num-$idx-$count"
10617                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
10618                         done
10619                 done
10620         done
10621
10622         cd $DIR
10623         $1 tar cf $TMP/f102.tar $tdir --xattrs
10624 }
10625
10626 cleanup_test102() {
10627         trap 0
10628         rm -f $TMP/f102.tar
10629         rm -rf $DIR/d0.sanity/d102
10630 }
10631
10632 test_102a() {
10633         [ "$UID" != 0 ] && skip "must run as root"
10634         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
10635                 skip_env "must have user_xattr"
10636
10637         [ -z "$(which setfattr 2>/dev/null)" ] &&
10638                 skip_env "could not find setfattr"
10639
10640         local testfile=$DIR/$tfile
10641
10642         touch $testfile
10643         echo "set/get xattr..."
10644         setfattr -n trusted.name1 -v value1 $testfile ||
10645                 error "setfattr -n trusted.name1=value1 $testfile failed"
10646         getfattr -n trusted.name1 $testfile 2> /dev/null |
10647           grep "trusted.name1=.value1" ||
10648                 error "$testfile missing trusted.name1=value1"
10649
10650         setfattr -n user.author1 -v author1 $testfile ||
10651                 error "setfattr -n user.author1=author1 $testfile failed"
10652         getfattr -n user.author1 $testfile 2> /dev/null |
10653           grep "user.author1=.author1" ||
10654                 error "$testfile missing trusted.author1=author1"
10655
10656         echo "listxattr..."
10657         setfattr -n trusted.name2 -v value2 $testfile ||
10658                 error "$testfile unable to set trusted.name2"
10659         setfattr -n trusted.name3 -v value3 $testfile ||
10660                 error "$testfile unable to set trusted.name3"
10661         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
10662             grep "trusted.name" | wc -l) -eq 3 ] ||
10663                 error "$testfile missing 3 trusted.name xattrs"
10664
10665         setfattr -n user.author2 -v author2 $testfile ||
10666                 error "$testfile unable to set user.author2"
10667         setfattr -n user.author3 -v author3 $testfile ||
10668                 error "$testfile unable to set user.author3"
10669         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
10670             grep "user.author" | wc -l) -eq 3 ] ||
10671                 error "$testfile missing 3 user.author xattrs"
10672
10673         echo "remove xattr..."
10674         setfattr -x trusted.name1 $testfile ||
10675                 error "$testfile error deleting trusted.name1"
10676         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
10677                 error "$testfile did not delete trusted.name1 xattr"
10678
10679         setfattr -x user.author1 $testfile ||
10680                 error "$testfile error deleting user.author1"
10681         echo "set lustre special xattr ..."
10682         $LFS setstripe -c1 $testfile
10683         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
10684                 awk -F "=" '/trusted.lov/ { print $2 }' )
10685         setfattr -n "trusted.lov" -v $lovea $testfile ||
10686                 error "$testfile doesn't ignore setting trusted.lov again"
10687         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
10688                 error "$testfile allow setting invalid trusted.lov"
10689         rm -f $testfile
10690 }
10691 run_test 102a "user xattr test =================================="
10692
10693 check_102b_layout() {
10694         local layout="$*"
10695         local testfile=$DIR/$tfile
10696
10697         echo "test layout '$layout'"
10698         $LFS setstripe $layout $testfile || error "setstripe failed"
10699         $LFS getstripe -y $testfile
10700
10701         echo "get/set/list trusted.lov xattr ..." # b=10930
10702         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
10703         [[ "$value" =~ "trusted.lov" ]] ||
10704                 error "can't get trusted.lov from $testfile"
10705         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
10706                 error "getstripe failed"
10707
10708         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
10709
10710         value=$(cut -d= -f2 <<<$value)
10711         # LU-13168: truncated xattr should fail if short lov_user_md header
10712         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
10713                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
10714         for len in $lens; do
10715                 echo "setfattr $len $testfile.2"
10716                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
10717                         [ $len -lt 66 ] && error "short xattr len=$len worked"
10718         done
10719         local stripe_size=$($LFS getstripe -S $testfile.2)
10720         local stripe_count=$($LFS getstripe -c $testfile.2)
10721         [[ $stripe_size -eq 65536 ]] ||
10722                 error "stripe size $stripe_size != 65536"
10723         [[ $stripe_count -eq $stripe_count_orig ]] ||
10724                 error "stripe count $stripe_count != $stripe_count_orig"
10725         rm $testfile $testfile.2
10726 }
10727
10728 test_102b() {
10729         [ -z "$(which setfattr 2>/dev/null)" ] &&
10730                 skip_env "could not find setfattr"
10731         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10732
10733         # check plain layout
10734         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
10735
10736         # and also check composite layout
10737         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
10738
10739 }
10740 run_test 102b "getfattr/setfattr for trusted.lov EAs"
10741
10742 test_102c() {
10743         [ -z "$(which setfattr 2>/dev/null)" ] &&
10744                 skip_env "could not find setfattr"
10745         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10746
10747         # b10930: get/set/list lustre.lov xattr
10748         echo "get/set/list lustre.lov xattr ..."
10749         test_mkdir $DIR/$tdir
10750         chown $RUNAS_ID $DIR/$tdir
10751         local testfile=$DIR/$tdir/$tfile
10752         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
10753                 error "setstripe failed"
10754         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
10755                 error "getstripe failed"
10756         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
10757         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
10758
10759         local testfile2=${testfile}2
10760         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
10761                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
10762
10763         $RUNAS $MCREATE $testfile2
10764         $RUNAS setfattr -n lustre.lov -v $value $testfile2
10765         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
10766         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
10767         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
10768         [ $stripe_count -eq $STRIPECOUNT ] ||
10769                 error "stripe count $stripe_count != $STRIPECOUNT"
10770 }
10771 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
10772
10773 compare_stripe_info1() {
10774         local stripe_index_all_zero=true
10775
10776         for num in 1 2 3 4; do
10777                 for count in $(seq 1 $STRIPE_COUNT); do
10778                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
10779                                 local size=$((STRIPE_SIZE * num))
10780                                 local file=file"$num-$offset-$count"
10781                                 stripe_size=$($LFS getstripe -S $PWD/$file)
10782                                 [[ $stripe_size -ne $size ]] &&
10783                                     error "$file: size $stripe_size != $size"
10784                                 stripe_count=$($LFS getstripe -c $PWD/$file)
10785                                 # allow fewer stripes to be created, ORI-601
10786                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
10787                                     error "$file: count $stripe_count != $count"
10788                                 stripe_index=$($LFS getstripe -i $PWD/$file)
10789                                 [[ $stripe_index -ne 0 ]] &&
10790                                         stripe_index_all_zero=false
10791                         done
10792                 done
10793         done
10794         $stripe_index_all_zero &&
10795                 error "all files are being extracted starting from OST index 0"
10796         return 0
10797 }
10798
10799 have_xattrs_include() {
10800         tar --help | grep -q xattrs-include &&
10801                 echo --xattrs-include="lustre.*"
10802 }
10803
10804 test_102d() {
10805         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10806         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10807
10808         XINC=$(have_xattrs_include)
10809         setup_test102
10810         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
10811         cd $DIR/$tdir/$tdir
10812         compare_stripe_info1
10813 }
10814 run_test 102d "tar restore stripe info from tarfile,not keep osts"
10815
10816 test_102f() {
10817         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10818         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10819
10820         XINC=$(have_xattrs_include)
10821         setup_test102
10822         test_mkdir $DIR/$tdir.restore
10823         cd $DIR
10824         tar cf - --xattrs $tdir | tar xf - \
10825                 -C $DIR/$tdir.restore --xattrs $XINC
10826         cd $DIR/$tdir.restore/$tdir
10827         compare_stripe_info1
10828 }
10829 run_test 102f "tar copy files, not keep osts"
10830
10831 grow_xattr() {
10832         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
10833                 skip "must have user_xattr"
10834         [ -z "$(which setfattr 2>/dev/null)" ] &&
10835                 skip_env "could not find setfattr"
10836         [ -z "$(which getfattr 2>/dev/null)" ] &&
10837                 skip_env "could not find getfattr"
10838
10839         local xsize=${1:-1024}  # in bytes
10840         local file=$DIR/$tfile
10841         local value="$(generate_string $xsize)"
10842         local xbig=trusted.big
10843         local toobig=$2
10844
10845         touch $file
10846         log "save $xbig on $file"
10847         if [ -z "$toobig" ]
10848         then
10849                 setfattr -n $xbig -v $value $file ||
10850                         error "saving $xbig on $file failed"
10851         else
10852                 setfattr -n $xbig -v $value $file &&
10853                         error "saving $xbig on $file succeeded"
10854                 return 0
10855         fi
10856
10857         local orig=$(get_xattr_value $xbig $file)
10858         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
10859
10860         local xsml=trusted.sml
10861         log "save $xsml on $file"
10862         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
10863
10864         local new=$(get_xattr_value $xbig $file)
10865         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
10866
10867         log "grow $xsml on $file"
10868         setfattr -n $xsml -v "$value" $file ||
10869                 error "growing $xsml on $file failed"
10870
10871         new=$(get_xattr_value $xbig $file)
10872         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
10873         log "$xbig still valid after growing $xsml"
10874
10875         rm -f $file
10876 }
10877
10878 test_102h() { # bug 15777
10879         grow_xattr 1024
10880 }
10881 run_test 102h "grow xattr from inside inode to external block"
10882
10883 test_102ha() {
10884         large_xattr_enabled || skip_env "ea_inode feature disabled"
10885
10886         echo "setting xattr of max xattr size: $(max_xattr_size)"
10887         grow_xattr $(max_xattr_size)
10888
10889         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
10890         echo "This should fail:"
10891         grow_xattr $(($(max_xattr_size) + 10)) 1
10892 }
10893 run_test 102ha "grow xattr from inside inode to external inode"
10894
10895 test_102i() { # bug 17038
10896         [ -z "$(which getfattr 2>/dev/null)" ] &&
10897                 skip "could not find getfattr"
10898
10899         touch $DIR/$tfile
10900         ln -s $DIR/$tfile $DIR/${tfile}link
10901         getfattr -n trusted.lov $DIR/$tfile ||
10902                 error "lgetxattr on $DIR/$tfile failed"
10903         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
10904                 grep -i "no such attr" ||
10905                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
10906         rm -f $DIR/$tfile $DIR/${tfile}link
10907 }
10908 run_test 102i "lgetxattr test on symbolic link ============"
10909
10910 test_102j() {
10911         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10912         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10913
10914         XINC=$(have_xattrs_include)
10915         setup_test102 "$RUNAS"
10916         chown $RUNAS_ID $DIR/$tdir
10917         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
10918         cd $DIR/$tdir/$tdir
10919         compare_stripe_info1 "$RUNAS"
10920 }
10921 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
10922
10923 test_102k() {
10924         [ -z "$(which setfattr 2>/dev/null)" ] &&
10925                 skip "could not find setfattr"
10926
10927         touch $DIR/$tfile
10928         # b22187 just check that does not crash for regular file.
10929         setfattr -n trusted.lov $DIR/$tfile
10930         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
10931         local test_kdir=$DIR/$tdir
10932         test_mkdir $test_kdir
10933         local default_size=$($LFS getstripe -S $test_kdir)
10934         local default_count=$($LFS getstripe -c $test_kdir)
10935         local default_offset=$($LFS getstripe -i $test_kdir)
10936         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
10937                 error 'dir setstripe failed'
10938         setfattr -n trusted.lov $test_kdir
10939         local stripe_size=$($LFS getstripe -S $test_kdir)
10940         local stripe_count=$($LFS getstripe -c $test_kdir)
10941         local stripe_offset=$($LFS getstripe -i $test_kdir)
10942         [ $stripe_size -eq $default_size ] ||
10943                 error "stripe size $stripe_size != $default_size"
10944         [ $stripe_count -eq $default_count ] ||
10945                 error "stripe count $stripe_count != $default_count"
10946         [ $stripe_offset -eq $default_offset ] ||
10947                 error "stripe offset $stripe_offset != $default_offset"
10948         rm -rf $DIR/$tfile $test_kdir
10949 }
10950 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
10951
10952 test_102l() {
10953         [ -z "$(which getfattr 2>/dev/null)" ] &&
10954                 skip "could not find getfattr"
10955
10956         # LU-532 trusted. xattr is invisible to non-root
10957         local testfile=$DIR/$tfile
10958
10959         touch $testfile
10960
10961         echo "listxattr as user..."
10962         chown $RUNAS_ID $testfile
10963         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
10964             grep -q "trusted" &&
10965                 error "$testfile trusted xattrs are user visible"
10966
10967         return 0;
10968 }
10969 run_test 102l "listxattr size test =================================="
10970
10971 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
10972         local path=$DIR/$tfile
10973         touch $path
10974
10975         listxattr_size_check $path || error "listattr_size_check $path failed"
10976 }
10977 run_test 102m "Ensure listxattr fails on small bufffer ========"
10978
10979 cleanup_test102
10980
10981 getxattr() { # getxattr path name
10982         # Return the base64 encoding of the value of xattr name on path.
10983         local path=$1
10984         local name=$2
10985
10986         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
10987         # file: $path
10988         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
10989         #
10990         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
10991
10992         getfattr --absolute-names --encoding=base64 --name=$name $path |
10993                 awk -F= -v name=$name '$1 == name {
10994                         print substr($0, index($0, "=") + 1);
10995         }'
10996 }
10997
10998 test_102n() { # LU-4101 mdt: protect internal xattrs
10999         [ -z "$(which setfattr 2>/dev/null)" ] &&
11000                 skip "could not find setfattr"
11001         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
11002         then
11003                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
11004         fi
11005
11006         local file0=$DIR/$tfile.0
11007         local file1=$DIR/$tfile.1
11008         local xattr0=$TMP/$tfile.0
11009         local xattr1=$TMP/$tfile.1
11010         local namelist="lov lma lmv link fid version som hsm"
11011         local name
11012         local value
11013
11014         rm -rf $file0 $file1 $xattr0 $xattr1
11015         touch $file0 $file1
11016
11017         # Get 'before' xattrs of $file1.
11018         getfattr --absolute-names --dump --match=- $file1 > $xattr0
11019
11020         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
11021                 namelist+=" lfsck_namespace"
11022         for name in $namelist; do
11023                 # Try to copy xattr from $file0 to $file1.
11024                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11025
11026                 setfattr --name=trusted.$name --value="$value" $file1 ||
11027                         error "setxattr 'trusted.$name' failed"
11028
11029                 # Try to set a garbage xattr.
11030                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11031
11032                 if [[ x$name == "xlov" ]]; then
11033                         setfattr --name=trusted.lov --value="$value" $file1 &&
11034                         error "setxattr invalid 'trusted.lov' success"
11035                 else
11036                         setfattr --name=trusted.$name --value="$value" $file1 ||
11037                                 error "setxattr invalid 'trusted.$name' failed"
11038                 fi
11039
11040                 # Try to remove the xattr from $file1. We don't care if this
11041                 # appears to succeed or fail, we just don't want there to be
11042                 # any changes or crashes.
11043                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11044         done
11045
11046         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
11047         then
11048                 name="lfsck_ns"
11049                 # Try to copy xattr from $file0 to $file1.
11050                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11051
11052                 setfattr --name=trusted.$name --value="$value" $file1 ||
11053                         error "setxattr 'trusted.$name' failed"
11054
11055                 # Try to set a garbage xattr.
11056                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11057
11058                 setfattr --name=trusted.$name --value="$value" $file1 ||
11059                         error "setxattr 'trusted.$name' failed"
11060
11061                 # Try to remove the xattr from $file1. We don't care if this
11062                 # appears to succeed or fail, we just don't want there to be
11063                 # any changes or crashes.
11064                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11065         fi
11066
11067         # Get 'after' xattrs of file1.
11068         getfattr --absolute-names --dump --match=- $file1 > $xattr1
11069
11070         if ! diff $xattr0 $xattr1; then
11071                 error "before and after xattrs of '$file1' differ"
11072         fi
11073
11074         rm -rf $file0 $file1 $xattr0 $xattr1
11075
11076         return 0
11077 }
11078 run_test 102n "silently ignore setxattr on internal trusted xattrs"
11079
11080 test_102p() { # LU-4703 setxattr did not check ownership
11081         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
11082                 skip "MDS needs to be at least 2.5.56"
11083
11084         local testfile=$DIR/$tfile
11085
11086         touch $testfile
11087
11088         echo "setfacl as user..."
11089         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
11090         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
11091
11092         echo "setfattr as user..."
11093         setfacl -m "u:$RUNAS_ID:---" $testfile
11094         $RUNAS setfattr -x system.posix_acl_access $testfile
11095         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
11096 }
11097 run_test 102p "check setxattr(2) correctly fails without permission"
11098
11099 test_102q() {
11100         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
11101                 skip "MDS needs to be at least 2.6.92"
11102
11103         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
11104 }
11105 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
11106
11107 test_102r() {
11108         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
11109                 skip "MDS needs to be at least 2.6.93"
11110
11111         touch $DIR/$tfile || error "touch"
11112         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
11113         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
11114         rm $DIR/$tfile || error "rm"
11115
11116         #normal directory
11117         mkdir -p $DIR/$tdir || error "mkdir"
11118         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11119         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11120         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11121                 error "$testfile error deleting user.author1"
11122         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11123                 grep "user.$(basename $tdir)" &&
11124                 error "$tdir did not delete user.$(basename $tdir)"
11125         rmdir $DIR/$tdir || error "rmdir"
11126
11127         #striped directory
11128         test_mkdir $DIR/$tdir
11129         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11130         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11131         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11132                 error "$testfile error deleting user.author1"
11133         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11134                 grep "user.$(basename $tdir)" &&
11135                 error "$tdir did not delete user.$(basename $tdir)"
11136         rmdir $DIR/$tdir || error "rm striped dir"
11137 }
11138 run_test 102r "set EAs with empty values"
11139
11140 test_102s() {
11141         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11142                 skip "MDS needs to be at least 2.11.52"
11143
11144         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11145
11146         save_lustre_params client "llite.*.xattr_cache" > $save
11147
11148         for cache in 0 1; do
11149                 lctl set_param llite.*.xattr_cache=$cache
11150
11151                 rm -f $DIR/$tfile
11152                 touch $DIR/$tfile || error "touch"
11153                 for prefix in lustre security system trusted user; do
11154                         # Note getxattr() may fail with 'Operation not
11155                         # supported' or 'No such attribute' depending
11156                         # on prefix and cache.
11157                         getfattr -n $prefix.n102s $DIR/$tfile &&
11158                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
11159                 done
11160         done
11161
11162         restore_lustre_params < $save
11163 }
11164 run_test 102s "getting nonexistent xattrs should fail"
11165
11166 test_102t() {
11167         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11168                 skip "MDS needs to be at least 2.11.52"
11169
11170         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11171
11172         save_lustre_params client "llite.*.xattr_cache" > $save
11173
11174         for cache in 0 1; do
11175                 lctl set_param llite.*.xattr_cache=$cache
11176
11177                 for buf_size in 0 256; do
11178                         rm -f $DIR/$tfile
11179                         touch $DIR/$tfile || error "touch"
11180                         setfattr -n user.multiop $DIR/$tfile
11181                         $MULTIOP $DIR/$tfile oa$buf_size ||
11182                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
11183                 done
11184         done
11185
11186         restore_lustre_params < $save
11187 }
11188 run_test 102t "zero length xattr values handled correctly"
11189
11190 run_acl_subtest()
11191 {
11192     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
11193     return $?
11194 }
11195
11196 test_103a() {
11197         [ "$UID" != 0 ] && skip "must run as root"
11198         $GSS && skip_env "could not run under gss"
11199         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
11200                 skip_env "must have acl enabled"
11201         [ -z "$(which setfacl 2>/dev/null)" ] &&
11202                 skip_env "could not find setfacl"
11203         remote_mds_nodsh && skip "remote MDS with nodsh"
11204
11205         gpasswd -a daemon bin                           # LU-5641
11206         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
11207
11208         declare -a identity_old
11209
11210         for num in $(seq $MDSCOUNT); do
11211                 switch_identity $num true || identity_old[$num]=$?
11212         done
11213
11214         SAVE_UMASK=$(umask)
11215         umask 0022
11216         mkdir -p $DIR/$tdir
11217         cd $DIR/$tdir
11218
11219         echo "performing cp ..."
11220         run_acl_subtest cp || error "run_acl_subtest cp failed"
11221         echo "performing getfacl-noacl..."
11222         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
11223         echo "performing misc..."
11224         run_acl_subtest misc || error  "misc test failed"
11225         echo "performing permissions..."
11226         run_acl_subtest permissions || error "permissions failed"
11227         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
11228         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
11229                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
11230                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
11231         then
11232                 echo "performing permissions xattr..."
11233                 run_acl_subtest permissions_xattr ||
11234                         error "permissions_xattr failed"
11235         fi
11236         echo "performing setfacl..."
11237         run_acl_subtest setfacl || error  "setfacl test failed"
11238
11239         # inheritance test got from HP
11240         echo "performing inheritance..."
11241         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
11242         chmod +x make-tree || error "chmod +x failed"
11243         run_acl_subtest inheritance || error "inheritance test failed"
11244         rm -f make-tree
11245
11246         echo "LU-974 ignore umask when acl is enabled..."
11247         run_acl_subtest 974 || error "LU-974 umask test failed"
11248         if [ $MDSCOUNT -ge 2 ]; then
11249                 run_acl_subtest 974_remote ||
11250                         error "LU-974 umask test failed under remote dir"
11251         fi
11252
11253         echo "LU-2561 newly created file is same size as directory..."
11254         if [ "$mds1_FSTYPE" != "zfs" ]; then
11255                 run_acl_subtest 2561 || error "LU-2561 test failed"
11256         else
11257                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
11258         fi
11259
11260         run_acl_subtest 4924 || error "LU-4924 test failed"
11261
11262         cd $SAVE_PWD
11263         umask $SAVE_UMASK
11264
11265         for num in $(seq $MDSCOUNT); do
11266                 if [ "${identity_old[$num]}" = 1 ]; then
11267                         switch_identity $num false || identity_old[$num]=$?
11268                 fi
11269         done
11270 }
11271 run_test 103a "acl test"
11272
11273 test_103b() {
11274         declare -a pids
11275         local U
11276
11277         for U in {0..511}; do
11278                 {
11279                 local O=$(printf "%04o" $U)
11280
11281                 umask $(printf "%04o" $((511 ^ $O)))
11282                 $LFS setstripe -c 1 $DIR/$tfile.s$O
11283                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
11284
11285                 (( $S == ($O & 0666) )) ||
11286                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
11287
11288                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
11289                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
11290                 (( $S == ($O & 0666) )) ||
11291                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
11292
11293                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
11294                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
11295                 (( $S == ($O & 0666) )) ||
11296                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
11297                 rm -f $DIR/$tfile.[smp]$0
11298                 } &
11299                 local pid=$!
11300
11301                 # limit the concurrently running threads to 64. LU-11878
11302                 local idx=$((U % 64))
11303                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
11304                 pids[idx]=$pid
11305         done
11306         wait
11307 }
11308 run_test 103b "umask lfs setstripe"
11309
11310 test_103c() {
11311         mkdir -p $DIR/$tdir
11312         cp -rp $DIR/$tdir $DIR/$tdir.bak
11313
11314         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
11315                 error "$DIR/$tdir shouldn't contain default ACL"
11316         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
11317                 error "$DIR/$tdir.bak shouldn't contain default ACL"
11318         true
11319 }
11320 run_test 103c "'cp -rp' won't set empty acl"
11321
11322 test_103e() {
11323         local numacl
11324         local fileacl
11325         local saved_debug=$($LCTL get_param -n debug)
11326
11327         (( $MDS1_VERSION >= $(version_code 2.14.0) )) ||
11328                 skip "MDS needs to be at least 2.14.0"
11329
11330         large_xattr_enabled || skip_env "ea_inode feature disabled"
11331
11332         mkdir -p $DIR/$tdir
11333         # add big LOV EA to cause reply buffer overflow earlier
11334         $LFS setstripe -C 1000 $DIR/$tdir
11335         lctl set_param mdc.*-mdc*.stats=clear
11336
11337         $LCTL set_param debug=0
11338         stack_trap "$LCTL set_param debug=\"$saved_debug\"" EXIT
11339         stack_trap "$LCTL get_param mdc.*-mdc*.stats" EXIT
11340
11341         # add a large number of default ACLs (expect 8000+ for 2.13+)
11342         for U in {2..7000}; do
11343                 setfacl -d -m user:$U:rwx $DIR/$tdir ||
11344                         error "Able to add just $U default ACLs"
11345         done
11346         numacl=$(getfacl $DIR/$tdir |& grep -c "default:user")
11347         echo "$numacl default ACLs created"
11348
11349         stat $DIR/$tdir || error "Cannot stat directory"
11350         # check file creation
11351         touch $DIR/$tdir/$tfile ||
11352                 error "failed to create $tfile with $numacl default ACLs"
11353         stat $DIR/$tdir/$tfile  || error "Cannot stat file"
11354         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11355         echo "$fileacl ACLs were inherited"
11356         (( $fileacl == $numacl )) ||
11357                 error "Not all default ACLs were inherited: $numacl != $fileacl"
11358         # check that new ACLs creation adds new ACLs to inherited ACLs
11359         setfacl -m user:19000:rwx $DIR/$tdir/$tfile ||
11360                 error "Cannot set new ACL"
11361         numacl=$((numacl + 1))
11362         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11363         (( $fileacl == $numacl )) ||
11364                 error "failed to add new ACL: $fileacl != $numacl as expected"
11365         # adds more ACLs to a file to reach their maximum at 8000+
11366         numacl=0
11367         for U in {20000..25000}; do
11368                 setfacl -m user:$U:rwx $DIR/$tdir/$tfile || break
11369                 numacl=$((numacl + 1))
11370         done
11371         echo "Added $numacl more ACLs to the file"
11372         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11373         echo "Total $fileacl ACLs in file"
11374         stat $DIR/$tdir/$tfile > /dev/null || error "Cannot stat file"
11375         rm -f $DIR/$tdir/$tfile || error "Cannot remove file"
11376         rmdir $DIR/$tdir || error "Cannot remove directory"
11377 }
11378 run_test 103e "inheritance of big amount of default ACLs"
11379
11380 test_103f() {
11381         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
11382                 skip "MDS needs to be at least 2.14.51"
11383
11384         large_xattr_enabled || skip_env "ea_inode feature disabled"
11385
11386         # enable changelog to consume more internal MDD buffers
11387         changelog_register
11388
11389         mkdir -p $DIR/$tdir
11390         # add big LOV EA
11391         $LFS setstripe -C 1000 $DIR/$tdir
11392         setfacl -d -m user:$U:rwx $DIR/$tdir || error "Cannot add default ACLs"
11393         mkdir $DIR/$tdir/inherited || error "failed to create subdirectory"
11394         rmdir $DIR/$tdir/inherited || error "Cannot remove subdirectory"
11395         rmdir $DIR/$tdir || error "Cannot remove directory"
11396 }
11397 run_test 103f "changelog doesn't interfere with default ACLs buffers"
11398
11399 test_104a() {
11400         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11401
11402         touch $DIR/$tfile
11403         lfs df || error "lfs df failed"
11404         lfs df -ih || error "lfs df -ih failed"
11405         lfs df -h $DIR || error "lfs df -h $DIR failed"
11406         lfs df -i $DIR || error "lfs df -i $DIR failed"
11407         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
11408         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
11409
11410         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
11411         lctl --device %$OSC deactivate
11412         lfs df || error "lfs df with deactivated OSC failed"
11413         lctl --device %$OSC activate
11414         # wait the osc back to normal
11415         wait_osc_import_ready client ost
11416
11417         lfs df || error "lfs df with reactivated OSC failed"
11418         rm -f $DIR/$tfile
11419 }
11420 run_test 104a "lfs df [-ih] [path] test ========================="
11421
11422 test_104b() {
11423         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11424         [ $RUNAS_ID -eq $UID ] &&
11425                 skip_env "RUNAS_ID = UID = $UID -- skipping"
11426
11427         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
11428                         grep "Permission denied" | wc -l)))
11429         if [ $denied_cnt -ne 0 ]; then
11430                 error "lfs check servers test failed"
11431         fi
11432 }
11433 run_test 104b "$RUNAS lfs check servers test ===================="
11434
11435 #
11436 # Verify $1 is within range of $2.
11437 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
11438 # $1 is <= 2% of $2. Else Fail.
11439 #
11440 value_in_range() {
11441         # Strip all units (M, G, T)
11442         actual=$(echo $1 | tr -d A-Z)
11443         expect=$(echo $2 | tr -d A-Z)
11444
11445         expect_lo=$(($expect * 98 / 100)) # 2% below
11446         expect_hi=$(($expect * 102 / 100)) # 2% above
11447
11448         # permit 2% drift above and below
11449         (( $actual >= $expect_lo && $actual <= $expect_hi ))
11450 }
11451
11452 test_104c() {
11453         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11454         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
11455
11456         local ost_param="osd-zfs.$FSNAME-OST0000."
11457         local mdt_param="osd-zfs.$FSNAME-MDT0000."
11458         local ofacets=$(get_facets OST)
11459         local mfacets=$(get_facets MDS)
11460         local saved_ost_blocks=
11461         local saved_mdt_blocks=
11462
11463         echo "Before recordsize change"
11464         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
11465         df=($(df -h | grep "/mnt/lustre"$))
11466
11467         # For checking.
11468         echo "lfs output : ${lfs_df[*]}"
11469         echo "df  output : ${df[*]}"
11470
11471         for facet in ${ofacets//,/ }; do
11472                 if [ -z $saved_ost_blocks ]; then
11473                         saved_ost_blocks=$(do_facet $facet \
11474                                 lctl get_param -n $ost_param.blocksize)
11475                         echo "OST Blocksize: $saved_ost_blocks"
11476                 fi
11477                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11478                 do_facet $facet zfs set recordsize=32768 $ost
11479         done
11480
11481         # BS too small. Sufficient for functional testing.
11482         for facet in ${mfacets//,/ }; do
11483                 if [ -z $saved_mdt_blocks ]; then
11484                         saved_mdt_blocks=$(do_facet $facet \
11485                                 lctl get_param -n $mdt_param.blocksize)
11486                         echo "MDT Blocksize: $saved_mdt_blocks"
11487                 fi
11488                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11489                 do_facet $facet zfs set recordsize=32768 $mdt
11490         done
11491
11492         # Give new values chance to reflect change
11493         sleep 2
11494
11495         echo "After recordsize change"
11496         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
11497         df_after=($(df -h | grep "/mnt/lustre"$))
11498
11499         # For checking.
11500         echo "lfs output : ${lfs_df_after[*]}"
11501         echo "df  output : ${df_after[*]}"
11502
11503         # Verify lfs df
11504         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
11505                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
11506         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
11507                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
11508         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
11509                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
11510
11511         # Verify df
11512         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
11513                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
11514         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
11515                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
11516         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
11517                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
11518
11519         # Restore MDT recordize back to original
11520         for facet in ${mfacets//,/ }; do
11521                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11522                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
11523         done
11524
11525         # Restore OST recordize back to original
11526         for facet in ${ofacets//,/ }; do
11527                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11528                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
11529         done
11530
11531         return 0
11532 }
11533 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
11534
11535 test_105a() {
11536         # doesn't work on 2.4 kernels
11537         touch $DIR/$tfile
11538         if $(flock_is_enabled); then
11539                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
11540         else
11541                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
11542         fi
11543         rm -f $DIR/$tfile
11544 }
11545 run_test 105a "flock when mounted without -o flock test ========"
11546
11547 test_105b() {
11548         touch $DIR/$tfile
11549         if $(flock_is_enabled); then
11550                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
11551         else
11552                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
11553         fi
11554         rm -f $DIR/$tfile
11555 }
11556 run_test 105b "fcntl when mounted without -o flock test ========"
11557
11558 test_105c() {
11559         touch $DIR/$tfile
11560         if $(flock_is_enabled); then
11561                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
11562         else
11563                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
11564         fi
11565         rm -f $DIR/$tfile
11566 }
11567 run_test 105c "lockf when mounted without -o flock test"
11568
11569 test_105d() { # bug 15924
11570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11571
11572         test_mkdir $DIR/$tdir
11573         flock_is_enabled || skip_env "mount w/o flock enabled"
11574         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
11575         $LCTL set_param fail_loc=0x80000315
11576         flocks_test 2 $DIR/$tdir
11577 }
11578 run_test 105d "flock race (should not freeze) ========"
11579
11580 test_105e() { # bug 22660 && 22040
11581         flock_is_enabled || skip_env "mount w/o flock enabled"
11582
11583         touch $DIR/$tfile
11584         flocks_test 3 $DIR/$tfile
11585 }
11586 run_test 105e "Two conflicting flocks from same process"
11587
11588 test_106() { #bug 10921
11589         test_mkdir $DIR/$tdir
11590         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
11591         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
11592 }
11593 run_test 106 "attempt exec of dir followed by chown of that dir"
11594
11595 test_107() {
11596         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11597
11598         CDIR=`pwd`
11599         local file=core
11600
11601         cd $DIR
11602         rm -f $file
11603
11604         local save_pattern=$(sysctl -n kernel.core_pattern)
11605         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
11606         sysctl -w kernel.core_pattern=$file
11607         sysctl -w kernel.core_uses_pid=0
11608
11609         ulimit -c unlimited
11610         sleep 60 &
11611         SLEEPPID=$!
11612
11613         sleep 1
11614
11615         kill -s 11 $SLEEPPID
11616         wait $SLEEPPID
11617         if [ -e $file ]; then
11618                 size=`stat -c%s $file`
11619                 [ $size -eq 0 ] && error "Fail to create core file $file"
11620         else
11621                 error "Fail to create core file $file"
11622         fi
11623         rm -f $file
11624         sysctl -w kernel.core_pattern=$save_pattern
11625         sysctl -w kernel.core_uses_pid=$save_uses_pid
11626         cd $CDIR
11627 }
11628 run_test 107 "Coredump on SIG"
11629
11630 test_110() {
11631         test_mkdir $DIR/$tdir
11632         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
11633         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
11634                 error "mkdir with 256 char should fail, but did not"
11635         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
11636                 error "create with 255 char failed"
11637         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
11638                 error "create with 256 char should fail, but did not"
11639
11640         ls -l $DIR/$tdir
11641         rm -rf $DIR/$tdir
11642 }
11643 run_test 110 "filename length checking"
11644
11645 #
11646 # Purpose: To verify dynamic thread (OSS) creation.
11647 #
11648 test_115() {
11649         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11650         remote_ost_nodsh && skip "remote OST with nodsh"
11651
11652         # Lustre does not stop service threads once they are started.
11653         # Reset number of running threads to default.
11654         stopall
11655         setupall
11656
11657         local OSTIO_pre
11658         local save_params="$TMP/sanity-$TESTNAME.parameters"
11659
11660         # Get ll_ost_io count before I/O
11661         OSTIO_pre=$(do_facet ost1 \
11662                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
11663         # Exit if lustre is not running (ll_ost_io not running).
11664         [ -z "$OSTIO_pre" ] && error "no OSS threads"
11665
11666         echo "Starting with $OSTIO_pre threads"
11667         local thread_max=$((OSTIO_pre * 2))
11668         local rpc_in_flight=$((thread_max * 2))
11669         # Number of I/O Process proposed to be started.
11670         local nfiles
11671         local facets=$(get_facets OST)
11672
11673         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
11674         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
11675
11676         # Set in_flight to $rpc_in_flight
11677         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
11678                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
11679         nfiles=${rpc_in_flight}
11680         # Set ost thread_max to $thread_max
11681         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
11682
11683         # 5 Minutes should be sufficient for max number of OSS
11684         # threads(thread_max) to be created.
11685         local timeout=300
11686
11687         # Start I/O.
11688         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
11689         test_mkdir $DIR/$tdir
11690         for i in $(seq $nfiles); do
11691                 local file=$DIR/$tdir/${tfile}-$i
11692                 $LFS setstripe -c -1 -i 0 $file
11693                 ($WTL $file $timeout)&
11694         done
11695
11696         # I/O Started - Wait for thread_started to reach thread_max or report
11697         # error if thread_started is more than thread_max.
11698         echo "Waiting for thread_started to reach thread_max"
11699         local thread_started=0
11700         local end_time=$((SECONDS + timeout))
11701
11702         while [ $SECONDS -le $end_time ] ; do
11703                 echo -n "."
11704                 # Get ost i/o thread_started count.
11705                 thread_started=$(do_facet ost1 \
11706                         "$LCTL get_param \
11707                         ost.OSS.ost_io.threads_started | cut -d= -f2")
11708                 # Break out if thread_started is equal/greater than thread_max
11709                 if [[ $thread_started -ge $thread_max ]]; then
11710                         echo ll_ost_io thread_started $thread_started, \
11711                                 equal/greater than thread_max $thread_max
11712                         break
11713                 fi
11714                 sleep 1
11715         done
11716
11717         # Cleanup - We have the numbers, Kill i/o jobs if running.
11718         jobcount=($(jobs -p))
11719         for i in $(seq 0 $((${#jobcount[@]}-1)))
11720         do
11721                 kill -9 ${jobcount[$i]}
11722                 if [ $? -ne 0 ] ; then
11723                         echo Warning: \
11724                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
11725                 fi
11726         done
11727
11728         # Cleanup files left by WTL binary.
11729         for i in $(seq $nfiles); do
11730                 local file=$DIR/$tdir/${tfile}-$i
11731                 rm -rf $file
11732                 if [ $? -ne 0 ] ; then
11733                         echo "Warning: Failed to delete file $file"
11734                 fi
11735         done
11736
11737         restore_lustre_params <$save_params
11738         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
11739
11740         # Error out if no new thread has started or Thread started is greater
11741         # than thread max.
11742         if [[ $thread_started -le $OSTIO_pre ||
11743                         $thread_started -gt $thread_max ]]; then
11744                 error "ll_ost_io: thread_started $thread_started" \
11745                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
11746                       "No new thread started or thread started greater " \
11747                       "than thread_max."
11748         fi
11749 }
11750 run_test 115 "verify dynamic thread creation===================="
11751
11752 free_min_max () {
11753         wait_delete_completed
11754         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
11755         echo "OST kbytes available: ${AVAIL[@]}"
11756         MAXV=${AVAIL[0]}
11757         MAXI=0
11758         MINV=${AVAIL[0]}
11759         MINI=0
11760         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
11761                 #echo OST $i: ${AVAIL[i]}kb
11762                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
11763                         MAXV=${AVAIL[i]}
11764                         MAXI=$i
11765                 fi
11766                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
11767                         MINV=${AVAIL[i]}
11768                         MINI=$i
11769                 fi
11770         done
11771         echo "Min free space: OST $MINI: $MINV"
11772         echo "Max free space: OST $MAXI: $MAXV"
11773 }
11774
11775 test_116a() { # was previously test_116()
11776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11777         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11778         remote_mds_nodsh && skip "remote MDS with nodsh"
11779
11780         echo -n "Free space priority "
11781         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
11782                 head -n1
11783         declare -a AVAIL
11784         free_min_max
11785
11786         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
11787         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
11788         trap simple_cleanup_common EXIT
11789
11790         # Check if we need to generate uneven OSTs
11791         test_mkdir -p $DIR/$tdir/OST${MINI}
11792         local FILL=$((MINV / 4))
11793         local DIFF=$((MAXV - MINV))
11794         local DIFF2=$((DIFF * 100 / MINV))
11795
11796         local threshold=$(do_facet $SINGLEMDS \
11797                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
11798         threshold=${threshold%%%}
11799         echo -n "Check for uneven OSTs: "
11800         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
11801
11802         if [[ $DIFF2 -gt $threshold ]]; then
11803                 echo "ok"
11804                 echo "Don't need to fill OST$MINI"
11805         else
11806                 # generate uneven OSTs. Write 2% over the QOS threshold value
11807                 echo "no"
11808                 DIFF=$((threshold - DIFF2 + 2))
11809                 DIFF2=$((MINV * DIFF / 100))
11810                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
11811                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
11812                         error "setstripe failed"
11813                 DIFF=$((DIFF2 / 2048))
11814                 i=0
11815                 while [ $i -lt $DIFF ]; do
11816                         i=$((i + 1))
11817                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
11818                                 bs=2M count=1 2>/dev/null
11819                         echo -n .
11820                 done
11821                 echo .
11822                 sync
11823                 sleep_maxage
11824                 free_min_max
11825         fi
11826
11827         DIFF=$((MAXV - MINV))
11828         DIFF2=$((DIFF * 100 / MINV))
11829         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
11830         if [ $DIFF2 -gt $threshold ]; then
11831                 echo "ok"
11832         else
11833                 echo "failed - QOS mode won't be used"
11834                 simple_cleanup_common
11835                 skip "QOS imbalance criteria not met"
11836         fi
11837
11838         MINI1=$MINI
11839         MINV1=$MINV
11840         MAXI1=$MAXI
11841         MAXV1=$MAXV
11842
11843         # now fill using QOS
11844         $LFS setstripe -c 1 $DIR/$tdir
11845         FILL=$((FILL / 200))
11846         if [ $FILL -gt 600 ]; then
11847                 FILL=600
11848         fi
11849         echo "writing $FILL files to QOS-assigned OSTs"
11850         i=0
11851         while [ $i -lt $FILL ]; do
11852                 i=$((i + 1))
11853                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
11854                         count=1 2>/dev/null
11855                 echo -n .
11856         done
11857         echo "wrote $i 200k files"
11858         sync
11859         sleep_maxage
11860
11861         echo "Note: free space may not be updated, so measurements might be off"
11862         free_min_max
11863         DIFF2=$((MAXV - MINV))
11864         echo "free space delta: orig $DIFF final $DIFF2"
11865         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
11866         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
11867         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
11868         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
11869         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
11870         if [[ $DIFF -gt 0 ]]; then
11871                 FILL=$((DIFF2 * 100 / DIFF - 100))
11872                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
11873         fi
11874
11875         # Figure out which files were written where
11876         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
11877                awk '/'$MINI1': / {print $2; exit}')
11878         echo $UUID
11879         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
11880         echo "$MINC files created on smaller OST $MINI1"
11881         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
11882                awk '/'$MAXI1': / {print $2; exit}')
11883         echo $UUID
11884         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
11885         echo "$MAXC files created on larger OST $MAXI1"
11886         if [[ $MINC -gt 0 ]]; then
11887                 FILL=$((MAXC * 100 / MINC - 100))
11888                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
11889         fi
11890         [[ $MAXC -gt $MINC ]] ||
11891                 error_ignore LU-9 "stripe QOS didn't balance free space"
11892         simple_cleanup_common
11893 }
11894 run_test 116a "stripe QOS: free space balance ==================="
11895
11896 test_116b() { # LU-2093
11897         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11898         remote_mds_nodsh && skip "remote MDS with nodsh"
11899
11900 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
11901         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
11902                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
11903         [ -z "$old_rr" ] && skip "no QOS"
11904         do_facet $SINGLEMDS lctl set_param \
11905                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
11906         mkdir -p $DIR/$tdir
11907         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
11908         createmany -o $DIR/$tdir/f- 20 || error "can't create"
11909         do_facet $SINGLEMDS lctl set_param fail_loc=0
11910         rm -rf $DIR/$tdir
11911         do_facet $SINGLEMDS lctl set_param \
11912                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
11913 }
11914 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
11915
11916 test_117() # bug 10891
11917 {
11918         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11919
11920         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
11921         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
11922         lctl set_param fail_loc=0x21e
11923         > $DIR/$tfile || error "truncate failed"
11924         lctl set_param fail_loc=0
11925         echo "Truncate succeeded."
11926         rm -f $DIR/$tfile
11927 }
11928 run_test 117 "verify osd extend =========="
11929
11930 NO_SLOW_RESENDCOUNT=4
11931 export OLD_RESENDCOUNT=""
11932 set_resend_count () {
11933         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
11934         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
11935         lctl set_param -n $PROC_RESENDCOUNT $1
11936         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
11937 }
11938
11939 # for reduce test_118* time (b=14842)
11940 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
11941
11942 # Reset async IO behavior after error case
11943 reset_async() {
11944         FILE=$DIR/reset_async
11945
11946         # Ensure all OSCs are cleared
11947         $LFS setstripe -c -1 $FILE
11948         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
11949         sync
11950         rm $FILE
11951 }
11952
11953 test_118a() #bug 11710
11954 {
11955         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11956
11957         reset_async
11958
11959         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11960         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11961         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11962
11963         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11964                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11965                 return 1;
11966         fi
11967         rm -f $DIR/$tfile
11968 }
11969 run_test 118a "verify O_SYNC works =========="
11970
11971 test_118b()
11972 {
11973         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11974         remote_ost_nodsh && skip "remote OST with nodsh"
11975
11976         reset_async
11977
11978         #define OBD_FAIL_SRV_ENOENT 0x217
11979         set_nodes_failloc "$(osts_nodes)" 0x217
11980         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11981         RC=$?
11982         set_nodes_failloc "$(osts_nodes)" 0
11983         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11984         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
11985                     grep -c writeback)
11986
11987         if [[ $RC -eq 0 ]]; then
11988                 error "Must return error due to dropped pages, rc=$RC"
11989                 return 1;
11990         fi
11991
11992         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11993                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11994                 return 1;
11995         fi
11996
11997         echo "Dirty pages not leaked on ENOENT"
11998
11999         # Due to the above error the OSC will issue all RPCs syncronously
12000         # until a subsequent RPC completes successfully without error.
12001         $MULTIOP $DIR/$tfile Ow4096yc
12002         rm -f $DIR/$tfile
12003
12004         return 0
12005 }
12006 run_test 118b "Reclaim dirty pages on fatal error =========="
12007
12008 test_118c()
12009 {
12010         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12011
12012         # for 118c, restore the original resend count, LU-1940
12013         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
12014                                 set_resend_count $OLD_RESENDCOUNT
12015         remote_ost_nodsh && skip "remote OST with nodsh"
12016
12017         reset_async
12018
12019         #define OBD_FAIL_OST_EROFS               0x216
12020         set_nodes_failloc "$(osts_nodes)" 0x216
12021
12022         # multiop should block due to fsync until pages are written
12023         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12024         MULTIPID=$!
12025         sleep 1
12026
12027         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12028                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12029         fi
12030
12031         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12032                     grep -c writeback)
12033         if [[ $WRITEBACK -eq 0 ]]; then
12034                 error "No page in writeback, writeback=$WRITEBACK"
12035         fi
12036
12037         set_nodes_failloc "$(osts_nodes)" 0
12038         wait $MULTIPID
12039         RC=$?
12040         if [[ $RC -ne 0 ]]; then
12041                 error "Multiop fsync failed, rc=$RC"
12042         fi
12043
12044         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12045         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12046                     grep -c writeback)
12047         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12048                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12049         fi
12050
12051         rm -f $DIR/$tfile
12052         echo "Dirty pages flushed via fsync on EROFS"
12053         return 0
12054 }
12055 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
12056
12057 # continue to use small resend count to reduce test_118* time (b=14842)
12058 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12059
12060 test_118d()
12061 {
12062         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12063         remote_ost_nodsh && skip "remote OST with nodsh"
12064
12065         reset_async
12066
12067         #define OBD_FAIL_OST_BRW_PAUSE_BULK
12068         set_nodes_failloc "$(osts_nodes)" 0x214
12069         # multiop should block due to fsync until pages are written
12070         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12071         MULTIPID=$!
12072         sleep 1
12073
12074         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12075                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12076         fi
12077
12078         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12079                     grep -c writeback)
12080         if [[ $WRITEBACK -eq 0 ]]; then
12081                 error "No page in writeback, writeback=$WRITEBACK"
12082         fi
12083
12084         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
12085         set_nodes_failloc "$(osts_nodes)" 0
12086
12087         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12088         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12089                     grep -c writeback)
12090         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12091                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12092         fi
12093
12094         rm -f $DIR/$tfile
12095         echo "Dirty pages gaurenteed flushed via fsync"
12096         return 0
12097 }
12098 run_test 118d "Fsync validation inject a delay of the bulk =========="
12099
12100 test_118f() {
12101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12102
12103         reset_async
12104
12105         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
12106         lctl set_param fail_loc=0x8000040a
12107
12108         # Should simulate EINVAL error which is fatal
12109         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12110         RC=$?
12111         if [[ $RC -eq 0 ]]; then
12112                 error "Must return error due to dropped pages, rc=$RC"
12113         fi
12114
12115         lctl set_param fail_loc=0x0
12116
12117         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12118         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12119         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12120                     grep -c writeback)
12121         if [[ $LOCKED -ne 0 ]]; then
12122                 error "Locked pages remain in cache, locked=$LOCKED"
12123         fi
12124
12125         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12126                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12127         fi
12128
12129         rm -f $DIR/$tfile
12130         echo "No pages locked after fsync"
12131
12132         reset_async
12133         return 0
12134 }
12135 run_test 118f "Simulate unrecoverable OSC side error =========="
12136
12137 test_118g() {
12138         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12139
12140         reset_async
12141
12142         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
12143         lctl set_param fail_loc=0x406
12144
12145         # simulate local -ENOMEM
12146         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12147         RC=$?
12148
12149         lctl set_param fail_loc=0
12150         if [[ $RC -eq 0 ]]; then
12151                 error "Must return error due to dropped pages, rc=$RC"
12152         fi
12153
12154         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12155         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12156         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12157                         grep -c writeback)
12158         if [[ $LOCKED -ne 0 ]]; then
12159                 error "Locked pages remain in cache, locked=$LOCKED"
12160         fi
12161
12162         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12163                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12164         fi
12165
12166         rm -f $DIR/$tfile
12167         echo "No pages locked after fsync"
12168
12169         reset_async
12170         return 0
12171 }
12172 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
12173
12174 test_118h() {
12175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12176         remote_ost_nodsh && skip "remote OST with nodsh"
12177
12178         reset_async
12179
12180         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12181         set_nodes_failloc "$(osts_nodes)" 0x20e
12182         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12183         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12184         RC=$?
12185
12186         set_nodes_failloc "$(osts_nodes)" 0
12187         if [[ $RC -eq 0 ]]; then
12188                 error "Must return error due to dropped pages, rc=$RC"
12189         fi
12190
12191         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12192         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12193         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12194                     grep -c writeback)
12195         if [[ $LOCKED -ne 0 ]]; then
12196                 error "Locked pages remain in cache, locked=$LOCKED"
12197         fi
12198
12199         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12200                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12201         fi
12202
12203         rm -f $DIR/$tfile
12204         echo "No pages locked after fsync"
12205
12206         return 0
12207 }
12208 run_test 118h "Verify timeout in handling recoverables errors  =========="
12209
12210 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12211
12212 test_118i() {
12213         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12214         remote_ost_nodsh && skip "remote OST with nodsh"
12215
12216         reset_async
12217
12218         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12219         set_nodes_failloc "$(osts_nodes)" 0x20e
12220
12221         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12222         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12223         PID=$!
12224         sleep 5
12225         set_nodes_failloc "$(osts_nodes)" 0
12226
12227         wait $PID
12228         RC=$?
12229         if [[ $RC -ne 0 ]]; then
12230                 error "got error, but should be not, rc=$RC"
12231         fi
12232
12233         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12234         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12235         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12236         if [[ $LOCKED -ne 0 ]]; then
12237                 error "Locked pages remain in cache, locked=$LOCKED"
12238         fi
12239
12240         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12241                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12242         fi
12243
12244         rm -f $DIR/$tfile
12245         echo "No pages locked after fsync"
12246
12247         return 0
12248 }
12249 run_test 118i "Fix error before timeout in recoverable error  =========="
12250
12251 [ "$SLOW" = "no" ] && set_resend_count 4
12252
12253 test_118j() {
12254         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12255         remote_ost_nodsh && skip "remote OST with nodsh"
12256
12257         reset_async
12258
12259         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
12260         set_nodes_failloc "$(osts_nodes)" 0x220
12261
12262         # return -EIO from OST
12263         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12264         RC=$?
12265         set_nodes_failloc "$(osts_nodes)" 0x0
12266         if [[ $RC -eq 0 ]]; then
12267                 error "Must return error due to dropped pages, rc=$RC"
12268         fi
12269
12270         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12271         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12272         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12273         if [[ $LOCKED -ne 0 ]]; then
12274                 error "Locked pages remain in cache, locked=$LOCKED"
12275         fi
12276
12277         # in recoverable error on OST we want resend and stay until it finished
12278         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12279                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12280         fi
12281
12282         rm -f $DIR/$tfile
12283         echo "No pages locked after fsync"
12284
12285         return 0
12286 }
12287 run_test 118j "Simulate unrecoverable OST side error =========="
12288
12289 test_118k()
12290 {
12291         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12292         remote_ost_nodsh && skip "remote OSTs with nodsh"
12293
12294         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12295         set_nodes_failloc "$(osts_nodes)" 0x20e
12296         test_mkdir $DIR/$tdir
12297
12298         for ((i=0;i<10;i++)); do
12299                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
12300                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
12301                 SLEEPPID=$!
12302                 sleep 0.500s
12303                 kill $SLEEPPID
12304                 wait $SLEEPPID
12305         done
12306
12307         set_nodes_failloc "$(osts_nodes)" 0
12308         rm -rf $DIR/$tdir
12309 }
12310 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
12311
12312 test_118l() # LU-646
12313 {
12314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12315
12316         test_mkdir $DIR/$tdir
12317         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
12318         rm -rf $DIR/$tdir
12319 }
12320 run_test 118l "fsync dir"
12321
12322 test_118m() # LU-3066
12323 {
12324         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12325
12326         test_mkdir $DIR/$tdir
12327         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
12328         rm -rf $DIR/$tdir
12329 }
12330 run_test 118m "fdatasync dir ========="
12331
12332 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12333
12334 test_118n()
12335 {
12336         local begin
12337         local end
12338
12339         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12340         remote_ost_nodsh && skip "remote OSTs with nodsh"
12341
12342         # Sleep to avoid a cached response.
12343         #define OBD_STATFS_CACHE_SECONDS 1
12344         sleep 2
12345
12346         # Inject a 10 second delay in the OST_STATFS handler.
12347         #define OBD_FAIL_OST_STATFS_DELAY 0x242
12348         set_nodes_failloc "$(osts_nodes)" 0x242
12349
12350         begin=$SECONDS
12351         stat --file-system $MOUNT > /dev/null
12352         end=$SECONDS
12353
12354         set_nodes_failloc "$(osts_nodes)" 0
12355
12356         if ((end - begin > 20)); then
12357             error "statfs took $((end - begin)) seconds, expected 10"
12358         fi
12359 }
12360 run_test 118n "statfs() sends OST_STATFS requests in parallel"
12361
12362 test_119a() # bug 11737
12363 {
12364         BSIZE=$((512 * 1024))
12365         directio write $DIR/$tfile 0 1 $BSIZE
12366         # We ask to read two blocks, which is more than a file size.
12367         # directio will indicate an error when requested and actual
12368         # sizes aren't equeal (a normal situation in this case) and
12369         # print actual read amount.
12370         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
12371         if [ "$NOB" != "$BSIZE" ]; then
12372                 error "read $NOB bytes instead of $BSIZE"
12373         fi
12374         rm -f $DIR/$tfile
12375 }
12376 run_test 119a "Short directIO read must return actual read amount"
12377
12378 test_119b() # bug 11737
12379 {
12380         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12381
12382         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
12383         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
12384         sync
12385         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
12386                 error "direct read failed"
12387         rm -f $DIR/$tfile
12388 }
12389 run_test 119b "Sparse directIO read must return actual read amount"
12390
12391 test_119c() # bug 13099
12392 {
12393         BSIZE=1048576
12394         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
12395         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
12396         rm -f $DIR/$tfile
12397 }
12398 run_test 119c "Testing for direct read hitting hole"
12399
12400 test_119d() # bug 15950
12401 {
12402         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12403
12404         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
12405         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
12406         BSIZE=1048576
12407         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
12408         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
12409         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
12410         lctl set_param fail_loc=0x40d
12411         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
12412         pid_dio=$!
12413         sleep 1
12414         cat $DIR/$tfile > /dev/null &
12415         lctl set_param fail_loc=0
12416         pid_reads=$!
12417         wait $pid_dio
12418         log "the DIO writes have completed, now wait for the reads (should not block very long)"
12419         sleep 2
12420         [ -n "`ps h -p $pid_reads -o comm`" ] && \
12421         error "the read rpcs have not completed in 2s"
12422         rm -f $DIR/$tfile
12423         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
12424 }
12425 run_test 119d "The DIO path should try to send a new rpc once one is completed"
12426
12427 test_120a() {
12428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12429         remote_mds_nodsh && skip "remote MDS with nodsh"
12430         test_mkdir -i0 -c1 $DIR/$tdir
12431         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12432                 skip_env "no early lock cancel on server"
12433
12434         lru_resize_disable mdc
12435         lru_resize_disable osc
12436         cancel_lru_locks mdc
12437         # asynchronous object destroy at MDT could cause bl ast to client
12438         cancel_lru_locks osc
12439
12440         stat $DIR/$tdir > /dev/null
12441         can1=$(do_facet mds1 \
12442                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12443                awk '/ldlm_cancel/ {print $2}')
12444         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12445                awk '/ldlm_bl_callback/ {print $2}')
12446         test_mkdir -i0 -c1 $DIR/$tdir/d1
12447         can2=$(do_facet mds1 \
12448                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12449                awk '/ldlm_cancel/ {print $2}')
12450         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12451                awk '/ldlm_bl_callback/ {print $2}')
12452         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12453         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12454         lru_resize_enable mdc
12455         lru_resize_enable osc
12456 }
12457 run_test 120a "Early Lock Cancel: mkdir test"
12458
12459 test_120b() {
12460         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12461         remote_mds_nodsh && skip "remote MDS with nodsh"
12462         test_mkdir $DIR/$tdir
12463         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12464                 skip_env "no early lock cancel on server"
12465
12466         lru_resize_disable mdc
12467         lru_resize_disable osc
12468         cancel_lru_locks mdc
12469         stat $DIR/$tdir > /dev/null
12470         can1=$(do_facet $SINGLEMDS \
12471                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12472                awk '/ldlm_cancel/ {print $2}')
12473         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12474                awk '/ldlm_bl_callback/ {print $2}')
12475         touch $DIR/$tdir/f1
12476         can2=$(do_facet $SINGLEMDS \
12477                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12478                awk '/ldlm_cancel/ {print $2}')
12479         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12480                awk '/ldlm_bl_callback/ {print $2}')
12481         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12482         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12483         lru_resize_enable mdc
12484         lru_resize_enable osc
12485 }
12486 run_test 120b "Early Lock Cancel: create test"
12487
12488 test_120c() {
12489         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12490         remote_mds_nodsh && skip "remote MDS with nodsh"
12491         test_mkdir -i0 -c1 $DIR/$tdir
12492         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12493                 skip "no early lock cancel on server"
12494
12495         lru_resize_disable mdc
12496         lru_resize_disable osc
12497         test_mkdir -i0 -c1 $DIR/$tdir/d1
12498         test_mkdir -i0 -c1 $DIR/$tdir/d2
12499         touch $DIR/$tdir/d1/f1
12500         cancel_lru_locks mdc
12501         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
12502         can1=$(do_facet mds1 \
12503                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12504                awk '/ldlm_cancel/ {print $2}')
12505         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12506                awk '/ldlm_bl_callback/ {print $2}')
12507         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12508         can2=$(do_facet mds1 \
12509                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12510                awk '/ldlm_cancel/ {print $2}')
12511         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12512                awk '/ldlm_bl_callback/ {print $2}')
12513         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12514         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12515         lru_resize_enable mdc
12516         lru_resize_enable osc
12517 }
12518 run_test 120c "Early Lock Cancel: link test"
12519
12520 test_120d() {
12521         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12522         remote_mds_nodsh && skip "remote MDS with nodsh"
12523         test_mkdir -i0 -c1 $DIR/$tdir
12524         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12525                 skip_env "no early lock cancel on server"
12526
12527         lru_resize_disable mdc
12528         lru_resize_disable osc
12529         touch $DIR/$tdir
12530         cancel_lru_locks mdc
12531         stat $DIR/$tdir > /dev/null
12532         can1=$(do_facet mds1 \
12533                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12534                awk '/ldlm_cancel/ {print $2}')
12535         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12536                awk '/ldlm_bl_callback/ {print $2}')
12537         chmod a+x $DIR/$tdir
12538         can2=$(do_facet mds1 \
12539                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12540                awk '/ldlm_cancel/ {print $2}')
12541         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12542                awk '/ldlm_bl_callback/ {print $2}')
12543         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12544         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12545         lru_resize_enable mdc
12546         lru_resize_enable osc
12547 }
12548 run_test 120d "Early Lock Cancel: setattr test"
12549
12550 test_120e() {
12551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12552         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12553                 skip_env "no early lock cancel on server"
12554         remote_mds_nodsh && skip "remote MDS with nodsh"
12555
12556         local dlmtrace_set=false
12557
12558         test_mkdir -i0 -c1 $DIR/$tdir
12559         lru_resize_disable mdc
12560         lru_resize_disable osc
12561         ! $LCTL get_param debug | grep -q dlmtrace &&
12562                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
12563         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
12564         cancel_lru_locks mdc
12565         cancel_lru_locks osc
12566         dd if=$DIR/$tdir/f1 of=/dev/null
12567         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
12568         # XXX client can not do early lock cancel of OST lock
12569         # during unlink (LU-4206), so cancel osc lock now.
12570         sleep 2
12571         cancel_lru_locks osc
12572         can1=$(do_facet mds1 \
12573                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12574                awk '/ldlm_cancel/ {print $2}')
12575         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12576                awk '/ldlm_bl_callback/ {print $2}')
12577         unlink $DIR/$tdir/f1
12578         sleep 5
12579         can2=$(do_facet mds1 \
12580                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12581                awk '/ldlm_cancel/ {print $2}')
12582         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12583                awk '/ldlm_bl_callback/ {print $2}')
12584         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
12585                 $LCTL dk $TMP/cancel.debug.txt
12586         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
12587                 $LCTL dk $TMP/blocking.debug.txt
12588         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
12589         lru_resize_enable mdc
12590         lru_resize_enable osc
12591 }
12592 run_test 120e "Early Lock Cancel: unlink test"
12593
12594 test_120f() {
12595         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12596         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12597                 skip_env "no early lock cancel on server"
12598         remote_mds_nodsh && skip "remote MDS with nodsh"
12599
12600         test_mkdir -i0 -c1 $DIR/$tdir
12601         lru_resize_disable mdc
12602         lru_resize_disable osc
12603         test_mkdir -i0 -c1 $DIR/$tdir/d1
12604         test_mkdir -i0 -c1 $DIR/$tdir/d2
12605         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
12606         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
12607         cancel_lru_locks mdc
12608         cancel_lru_locks osc
12609         dd if=$DIR/$tdir/d1/f1 of=/dev/null
12610         dd if=$DIR/$tdir/d2/f2 of=/dev/null
12611         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
12612         # XXX client can not do early lock cancel of OST lock
12613         # during rename (LU-4206), so cancel osc lock now.
12614         sleep 2
12615         cancel_lru_locks osc
12616         can1=$(do_facet mds1 \
12617                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12618                awk '/ldlm_cancel/ {print $2}')
12619         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12620                awk '/ldlm_bl_callback/ {print $2}')
12621         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12622         sleep 5
12623         can2=$(do_facet mds1 \
12624                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12625                awk '/ldlm_cancel/ {print $2}')
12626         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12627                awk '/ldlm_bl_callback/ {print $2}')
12628         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12629         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12630         lru_resize_enable mdc
12631         lru_resize_enable osc
12632 }
12633 run_test 120f "Early Lock Cancel: rename test"
12634
12635 test_120g() {
12636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12637         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12638                 skip_env "no early lock cancel on server"
12639         remote_mds_nodsh && skip "remote MDS with nodsh"
12640
12641         lru_resize_disable mdc
12642         lru_resize_disable osc
12643         count=10000
12644         echo create $count files
12645         test_mkdir $DIR/$tdir
12646         cancel_lru_locks mdc
12647         cancel_lru_locks osc
12648         t0=$(date +%s)
12649
12650         can0=$(do_facet $SINGLEMDS \
12651                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12652                awk '/ldlm_cancel/ {print $2}')
12653         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12654                awk '/ldlm_bl_callback/ {print $2}')
12655         createmany -o $DIR/$tdir/f $count
12656         sync
12657         can1=$(do_facet $SINGLEMDS \
12658                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12659                awk '/ldlm_cancel/ {print $2}')
12660         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12661                awk '/ldlm_bl_callback/ {print $2}')
12662         t1=$(date +%s)
12663         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
12664         echo rm $count files
12665         rm -r $DIR/$tdir
12666         sync
12667         can2=$(do_facet $SINGLEMDS \
12668                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12669                awk '/ldlm_cancel/ {print $2}')
12670         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12671                awk '/ldlm_bl_callback/ {print $2}')
12672         t2=$(date +%s)
12673         echo total: $count removes in $((t2-t1))
12674         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
12675         sleep 2
12676         # wait for commitment of removal
12677         lru_resize_enable mdc
12678         lru_resize_enable osc
12679 }
12680 run_test 120g "Early Lock Cancel: performance test"
12681
12682 test_121() { #bug #10589
12683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12684
12685         rm -rf $DIR/$tfile
12686         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
12687 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
12688         lctl set_param fail_loc=0x310
12689         cancel_lru_locks osc > /dev/null
12690         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
12691         lctl set_param fail_loc=0
12692         [[ $reads -eq $writes ]] ||
12693                 error "read $reads blocks, must be $writes blocks"
12694 }
12695 run_test 121 "read cancel race ========="
12696
12697 test_123a_base() { # was test 123, statahead(bug 11401)
12698         local lsx="$1"
12699
12700         SLOWOK=0
12701         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
12702                 log "testing UP system. Performance may be lower than expected."
12703                 SLOWOK=1
12704         fi
12705
12706         rm -rf $DIR/$tdir
12707         test_mkdir $DIR/$tdir
12708         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
12709         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
12710         MULT=10
12711         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
12712                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
12713
12714                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
12715                 lctl set_param -n llite.*.statahead_max 0
12716                 lctl get_param llite.*.statahead_max
12717                 cancel_lru_locks mdc
12718                 cancel_lru_locks osc
12719                 stime=$(date +%s)
12720                 time $lsx $DIR/$tdir | wc -l
12721                 etime=$(date +%s)
12722                 delta=$((etime - stime))
12723                 log "$lsx $i files without statahead: $delta sec"
12724                 lctl set_param llite.*.statahead_max=$max
12725
12726                 swrong=$(lctl get_param -n llite.*.statahead_stats |
12727                         grep "statahead wrong:" | awk '{print $3}')
12728                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
12729                 cancel_lru_locks mdc
12730                 cancel_lru_locks osc
12731                 stime=$(date +%s)
12732                 time $lsx $DIR/$tdir | wc -l
12733                 etime=$(date +%s)
12734                 delta_sa=$((etime - stime))
12735                 log "$lsx $i files with statahead: $delta_sa sec"
12736                 lctl get_param -n llite.*.statahead_stats
12737                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
12738                         grep "statahead wrong:" | awk '{print $3}')
12739
12740                 [[ $swrong -lt $ewrong ]] &&
12741                         log "statahead was stopped, maybe too many locks held!"
12742                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
12743
12744                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
12745                         max=$(lctl get_param -n llite.*.statahead_max |
12746                                 head -n 1)
12747                         lctl set_param -n llite.*.statahead_max 0
12748                         lctl get_param llite.*.statahead_max
12749                         cancel_lru_locks mdc
12750                         cancel_lru_locks osc
12751                         stime=$(date +%s)
12752                         time $lsx $DIR/$tdir | wc -l
12753                         etime=$(date +%s)
12754                         delta=$((etime - stime))
12755                         log "$lsx $i files again without statahead: $delta sec"
12756                         lctl set_param llite.*.statahead_max=$max
12757                         if [ $((delta_sa * 100 > delta * 105 && delta_sa > delta + 2)) ]; then
12758                                 if [  $SLOWOK -eq 0 ]; then
12759                                         error "$lsx $i files is slower with statahead!"
12760                                 else
12761                                         log "$lsx $i files is slower with statahead!"
12762                                 fi
12763                                 break
12764                         fi
12765                 fi
12766
12767                 [ $delta -gt 20 ] && break
12768                 [ $delta -gt 8 ] && MULT=$((50 / delta))
12769                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
12770         done
12771         log "$lsx done"
12772
12773         stime=$(date +%s)
12774         rm -r $DIR/$tdir
12775         sync
12776         etime=$(date +%s)
12777         delta=$((etime - stime))
12778         log "rm -r $DIR/$tdir/: $delta seconds"
12779         log "rm done"
12780         lctl get_param -n llite.*.statahead_stats
12781 }
12782
12783 test_123aa() {
12784         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12785
12786         test_123a_base "ls -l"
12787 }
12788 run_test 123aa "verify statahead work"
12789
12790 test_123ab() {
12791         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12792
12793         statx_supported || skip_env "Test must be statx() syscall supported"
12794
12795         test_123a_base "$STATX -l"
12796 }
12797 run_test 123ab "verify statahead work by using statx"
12798
12799 test_123ac() {
12800         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12801
12802         statx_supported || skip_env "Test must be statx() syscall supported"
12803
12804         local rpcs_before
12805         local rpcs_after
12806         local agl_before
12807         local agl_after
12808
12809         cancel_lru_locks $OSC
12810         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
12811         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
12812                 awk '/agl.total:/ {print $3}')
12813         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
12814         test_123a_base "$STATX --cached=always -D"
12815         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
12816                 awk '/agl.total:/ {print $3}')
12817         [ $agl_before -eq $agl_after ] ||
12818                 error "Should not trigger AGL thread - $agl_before:$agl_after"
12819         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
12820         [ $rpcs_after -eq $rpcs_before ] ||
12821                 error "$STATX should not send glimpse RPCs to $OSC"
12822 }
12823 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
12824
12825 test_123b () { # statahead(bug 15027)
12826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12827
12828         test_mkdir $DIR/$tdir
12829         createmany -o $DIR/$tdir/$tfile-%d 1000
12830
12831         cancel_lru_locks mdc
12832         cancel_lru_locks osc
12833
12834 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
12835         lctl set_param fail_loc=0x80000803
12836         ls -lR $DIR/$tdir > /dev/null
12837         log "ls done"
12838         lctl set_param fail_loc=0x0
12839         lctl get_param -n llite.*.statahead_stats
12840         rm -r $DIR/$tdir
12841         sync
12842
12843 }
12844 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
12845
12846 test_123c() {
12847         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
12848
12849         test_mkdir -i 0 -c 1 $DIR/$tdir.0
12850         test_mkdir -i 1 -c 1 $DIR/$tdir.1
12851         touch $DIR/$tdir.1/{1..3}
12852         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
12853
12854         remount_client $MOUNT
12855
12856         $MULTIOP $DIR/$tdir.0 Q
12857
12858         # let statahead to complete
12859         ls -l $DIR/$tdir.0 > /dev/null
12860
12861         testid=$(echo $TESTNAME | tr '_' ' ')
12862         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
12863                 error "statahead warning" || true
12864 }
12865 run_test 123c "Can not initialize inode warning on DNE statahead"
12866
12867 test_124a() {
12868         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12869         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12870                 skip_env "no lru resize on server"
12871
12872         local NR=2000
12873
12874         test_mkdir $DIR/$tdir
12875
12876         log "create $NR files at $DIR/$tdir"
12877         createmany -o $DIR/$tdir/f $NR ||
12878                 error "failed to create $NR files in $DIR/$tdir"
12879
12880         cancel_lru_locks mdc
12881         ls -l $DIR/$tdir > /dev/null
12882
12883         local NSDIR=""
12884         local LRU_SIZE=0
12885         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
12886                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
12887                 LRU_SIZE=$($LCTL get_param -n $PARAM)
12888                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
12889                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
12890                         log "NSDIR=$NSDIR"
12891                         log "NS=$(basename $NSDIR)"
12892                         break
12893                 fi
12894         done
12895
12896         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
12897                 skip "Not enough cached locks created!"
12898         fi
12899         log "LRU=$LRU_SIZE"
12900
12901         local SLEEP=30
12902
12903         # We know that lru resize allows one client to hold $LIMIT locks
12904         # for 10h. After that locks begin to be killed by client.
12905         local MAX_HRS=10
12906         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
12907         log "LIMIT=$LIMIT"
12908         if [ $LIMIT -lt $LRU_SIZE ]; then
12909                 skip "Limit is too small $LIMIT"
12910         fi
12911
12912         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
12913         # killing locks. Some time was spent for creating locks. This means
12914         # that up to the moment of sleep finish we must have killed some of
12915         # them (10-100 locks). This depends on how fast ther were created.
12916         # Many of them were touched in almost the same moment and thus will
12917         # be killed in groups.
12918         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE * 100))
12919
12920         # Use $LRU_SIZE_B here to take into account real number of locks
12921         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
12922         local LRU_SIZE_B=$LRU_SIZE
12923         log "LVF=$LVF"
12924         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
12925         log "OLD_LVF=$OLD_LVF"
12926         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
12927
12928         # Let's make sure that we really have some margin. Client checks
12929         # cached locks every 10 sec.
12930         SLEEP=$((SLEEP+20))
12931         log "Sleep ${SLEEP} sec"
12932         local SEC=0
12933         while ((SEC<$SLEEP)); do
12934                 echo -n "..."
12935                 sleep 5
12936                 SEC=$((SEC+5))
12937                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
12938                 echo -n "$LRU_SIZE"
12939         done
12940         echo ""
12941         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
12942         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
12943
12944         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
12945                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
12946                 unlinkmany $DIR/$tdir/f $NR
12947                 return
12948         }
12949
12950         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
12951         log "unlink $NR files at $DIR/$tdir"
12952         unlinkmany $DIR/$tdir/f $NR
12953 }
12954 run_test 124a "lru resize ======================================="
12955
12956 get_max_pool_limit()
12957 {
12958         local limit=$($LCTL get_param \
12959                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
12960         local max=0
12961         for l in $limit; do
12962                 if [[ $l -gt $max ]]; then
12963                         max=$l
12964                 fi
12965         done
12966         echo $max
12967 }
12968
12969 test_124b() {
12970         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12971         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
12972                 skip_env "no lru resize on server"
12973
12974         LIMIT=$(get_max_pool_limit)
12975
12976         NR=$(($(default_lru_size)*20))
12977         if [[ $NR -gt $LIMIT ]]; then
12978                 log "Limit lock number by $LIMIT locks"
12979                 NR=$LIMIT
12980         fi
12981
12982         IFree=$(mdsrate_inodes_available)
12983         if [ $IFree -lt $NR ]; then
12984                 log "Limit lock number by $IFree inodes"
12985                 NR=$IFree
12986         fi
12987
12988         lru_resize_disable mdc
12989         test_mkdir -p $DIR/$tdir/disable_lru_resize
12990
12991         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
12992         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
12993         cancel_lru_locks mdc
12994         stime=`date +%s`
12995         PID=""
12996         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
12997         PID="$PID $!"
12998         sleep 2
12999         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13000         PID="$PID $!"
13001         sleep 2
13002         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13003         PID="$PID $!"
13004         wait $PID
13005         etime=`date +%s`
13006         nolruresize_delta=$((etime-stime))
13007         log "ls -la time: $nolruresize_delta seconds"
13008         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
13009         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
13010
13011         lru_resize_enable mdc
13012         test_mkdir -p $DIR/$tdir/enable_lru_resize
13013
13014         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
13015         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
13016         cancel_lru_locks mdc
13017         stime=`date +%s`
13018         PID=""
13019         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13020         PID="$PID $!"
13021         sleep 2
13022         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13023         PID="$PID $!"
13024         sleep 2
13025         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13026         PID="$PID $!"
13027         wait $PID
13028         etime=`date +%s`
13029         lruresize_delta=$((etime-stime))
13030         log "ls -la time: $lruresize_delta seconds"
13031         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
13032
13033         if [ $lruresize_delta -gt $nolruresize_delta ]; then
13034                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
13035         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
13036                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
13037         else
13038                 log "lru resize performs the same with no lru resize"
13039         fi
13040         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
13041 }
13042 run_test 124b "lru resize (performance test) ======================="
13043
13044 test_124c() {
13045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13046         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13047                 skip_env "no lru resize on server"
13048
13049         # cache ununsed locks on client
13050         local nr=100
13051         cancel_lru_locks mdc
13052         test_mkdir $DIR/$tdir
13053         createmany -o $DIR/$tdir/f $nr ||
13054                 error "failed to create $nr files in $DIR/$tdir"
13055         ls -l $DIR/$tdir > /dev/null
13056
13057         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13058         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13059         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13060         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13061         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13062
13063         # set lru_max_age to 1 sec
13064         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13065         echo "sleep $((recalc_p * 2)) seconds..."
13066         sleep $((recalc_p * 2))
13067
13068         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13069         # restore lru_max_age
13070         $LCTL set_param -n $nsdir.lru_max_age $max_age
13071         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13072         unlinkmany $DIR/$tdir/f $nr
13073 }
13074 run_test 124c "LRUR cancel very aged locks"
13075
13076 test_124d() {
13077         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13078         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13079                 skip_env "no lru resize on server"
13080
13081         # cache ununsed locks on client
13082         local nr=100
13083
13084         lru_resize_disable mdc
13085         stack_trap "lru_resize_enable mdc" EXIT
13086
13087         cancel_lru_locks mdc
13088
13089         # asynchronous object destroy at MDT could cause bl ast to client
13090         test_mkdir $DIR/$tdir
13091         createmany -o $DIR/$tdir/f $nr ||
13092                 error "failed to create $nr files in $DIR/$tdir"
13093         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
13094
13095         ls -l $DIR/$tdir > /dev/null
13096
13097         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13098         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13099         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13100         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13101
13102         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13103
13104         # set lru_max_age to 1 sec
13105         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13106         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
13107
13108         echo "sleep $((recalc_p * 2)) seconds..."
13109         sleep $((recalc_p * 2))
13110
13111         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13112
13113         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13114 }
13115 run_test 124d "cancel very aged locks if lru-resize diasbaled"
13116
13117 test_125() { # 13358
13118         $LCTL get_param -n llite.*.client_type | grep -q local ||
13119                 skip "must run as local client"
13120         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
13121                 skip_env "must have acl enabled"
13122         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13123
13124         test_mkdir $DIR/$tdir
13125         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
13126         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
13127         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
13128 }
13129 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
13130
13131 test_126() { # bug 12829/13455
13132         $GSS && skip_env "must run as gss disabled"
13133         $LCTL get_param -n llite.*.client_type | grep -q local ||
13134                 skip "must run as local client"
13135         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
13136
13137         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
13138         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
13139         rm -f $DIR/$tfile
13140         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
13141 }
13142 run_test 126 "check that the fsgid provided by the client is taken into account"
13143
13144 test_127a() { # bug 15521
13145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13146         local name count samp unit min max sum sumsq
13147
13148         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
13149         echo "stats before reset"
13150         $LCTL get_param osc.*.stats
13151         $LCTL set_param osc.*.stats=0
13152         local fsize=$((2048 * 1024))
13153
13154         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
13155         cancel_lru_locks osc
13156         dd if=$DIR/$tfile of=/dev/null bs=$fsize
13157
13158         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
13159         stack_trap "rm -f $TMP/$tfile.tmp"
13160         while read name count samp unit min max sum sumsq; do
13161                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13162                 [ ! $min ] && error "Missing min value for $name proc entry"
13163                 eval $name=$count || error "Wrong proc format"
13164
13165                 case $name in
13166                 read_bytes|write_bytes)
13167                         [[ "$unit" =~ "bytes" ]] ||
13168                                 error "unit is not 'bytes': $unit"
13169                         (( $min >= 4096 )) || error "min is too small: $min"
13170                         (( $min <= $fsize )) || error "min is too big: $min"
13171                         (( $max >= 4096 )) || error "max is too small: $max"
13172                         (( $max <= $fsize )) || error "max is too big: $max"
13173                         (( $sum == $fsize )) || error "sum is wrong: $sum"
13174                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
13175                                 error "sumsquare is too small: $sumsq"
13176                         (( $sumsq <= $fsize * $fsize )) ||
13177                                 error "sumsquare is too big: $sumsq"
13178                         ;;
13179                 ost_read|ost_write)
13180                         [[ "$unit" =~ "usec" ]] ||
13181                                 error "unit is not 'usec': $unit"
13182                         ;;
13183                 *)      ;;
13184                 esac
13185         done < $DIR/$tfile.tmp
13186
13187         #check that we actually got some stats
13188         [ "$read_bytes" ] || error "Missing read_bytes stats"
13189         [ "$write_bytes" ] || error "Missing write_bytes stats"
13190         [ "$read_bytes" != 0 ] || error "no read done"
13191         [ "$write_bytes" != 0 ] || error "no write done"
13192 }
13193 run_test 127a "verify the client stats are sane"
13194
13195 test_127b() { # bug LU-333
13196         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13197         local name count samp unit min max sum sumsq
13198
13199         echo "stats before reset"
13200         $LCTL get_param llite.*.stats
13201         $LCTL set_param llite.*.stats=0
13202
13203         # perform 2 reads and writes so MAX is different from SUM.
13204         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13205         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13206         cancel_lru_locks osc
13207         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13208         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13209
13210         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
13211         stack_trap "rm -f $TMP/$tfile.tmp"
13212         while read name count samp unit min max sum sumsq; do
13213                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13214                 eval $name=$count || error "Wrong proc format"
13215
13216                 case $name in
13217                 read_bytes|write_bytes)
13218                         [[ "$unit" =~ "bytes" ]] ||
13219                                 error "unit is not 'bytes': $unit"
13220                         (( $count == 2 )) || error "count is not 2: $count"
13221                         (( $min == $PAGE_SIZE )) ||
13222                                 error "min is not $PAGE_SIZE: $min"
13223                         (( $max == $PAGE_SIZE )) ||
13224                                 error "max is not $PAGE_SIZE: $max"
13225                         (( $sum == $PAGE_SIZE * 2 )) ||
13226                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
13227                         ;;
13228                 read|write)
13229                         [[ "$unit" =~ "usec" ]] ||
13230                                 error "unit is not 'usec': $unit"
13231                         ;;
13232                 *)      ;;
13233                 esac
13234         done < $TMP/$tfile.tmp
13235
13236         #check that we actually got some stats
13237         [ "$read_bytes" ] || error "Missing read_bytes stats"
13238         [ "$write_bytes" ] || error "Missing write_bytes stats"
13239         [ "$read_bytes" != 0 ] || error "no read done"
13240         [ "$write_bytes" != 0 ] || error "no write done"
13241 }
13242 run_test 127b "verify the llite client stats are sane"
13243
13244 test_127c() { # LU-12394
13245         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13246         local size
13247         local bsize
13248         local reads
13249         local writes
13250         local count
13251
13252         $LCTL set_param llite.*.extents_stats=1
13253         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
13254
13255         # Use two stripes so there is enough space in default config
13256         $LFS setstripe -c 2 $DIR/$tfile
13257
13258         # Extent stats start at 0-4K and go in power of two buckets
13259         # LL_HIST_START = 12 --> 2^12 = 4K
13260         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
13261         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
13262         # small configs
13263         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
13264                 do
13265                 # Write and read, 2x each, second time at a non-zero offset
13266                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
13267                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
13268                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
13269                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
13270                 rm -f $DIR/$tfile
13271         done
13272
13273         $LCTL get_param llite.*.extents_stats
13274
13275         count=2
13276         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
13277                 do
13278                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
13279                                 grep -m 1 $bsize)
13280                 reads=$(echo $bucket | awk '{print $5}')
13281                 writes=$(echo $bucket | awk '{print $9}')
13282                 [ "$reads" -eq $count ] ||
13283                         error "$reads reads in < $bsize bucket, expect $count"
13284                 [ "$writes" -eq $count ] ||
13285                         error "$writes writes in < $bsize bucket, expect $count"
13286         done
13287
13288         # Test mmap write and read
13289         $LCTL set_param llite.*.extents_stats=c
13290         size=512
13291         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
13292         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
13293         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
13294
13295         $LCTL get_param llite.*.extents_stats
13296
13297         count=$(((size*1024) / PAGE_SIZE))
13298
13299         bsize=$((2 * PAGE_SIZE / 1024))K
13300
13301         bucket=$($LCTL get_param -n llite.*.extents_stats |
13302                         grep -m 1 $bsize)
13303         reads=$(echo $bucket | awk '{print $5}')
13304         writes=$(echo $bucket | awk '{print $9}')
13305         # mmap writes fault in the page first, creating an additonal read
13306         [ "$reads" -eq $((2 * count)) ] ||
13307                 error "$reads reads in < $bsize bucket, expect $count"
13308         [ "$writes" -eq $count ] ||
13309                 error "$writes writes in < $bsize bucket, expect $count"
13310 }
13311 run_test 127c "test llite extent stats with regular & mmap i/o"
13312
13313 test_128() { # bug 15212
13314         touch $DIR/$tfile
13315         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
13316                 find $DIR/$tfile
13317                 find $DIR/$tfile
13318         EOF
13319
13320         result=$(grep error $TMP/$tfile.log)
13321         rm -f $DIR/$tfile $TMP/$tfile.log
13322         [ -z "$result" ] ||
13323                 error "consecutive find's under interactive lfs failed"
13324 }
13325 run_test 128 "interactive lfs for 2 consecutive find's"
13326
13327 set_dir_limits () {
13328         local mntdev
13329         local canondev
13330         local node
13331
13332         local ldproc=/proc/fs/ldiskfs
13333         local facets=$(get_facets MDS)
13334
13335         for facet in ${facets//,/ }; do
13336                 canondev=$(ldiskfs_canon \
13337                            *.$(convert_facet2label $facet).mntdev $facet)
13338                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
13339                         ldproc=/sys/fs/ldiskfs
13340                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
13341                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
13342         done
13343 }
13344
13345 check_mds_dmesg() {
13346         local facets=$(get_facets MDS)
13347         for facet in ${facets//,/ }; do
13348                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
13349         done
13350         return 1
13351 }
13352
13353 test_129() {
13354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13355         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
13356                 skip "Need MDS version with at least 2.5.56"
13357         if [ "$mds1_FSTYPE" != ldiskfs ]; then
13358                 skip_env "ldiskfs only test"
13359         fi
13360         remote_mds_nodsh && skip "remote MDS with nodsh"
13361
13362         local ENOSPC=28
13363         local has_warning=false
13364
13365         rm -rf $DIR/$tdir
13366         mkdir -p $DIR/$tdir
13367
13368         # block size of mds1
13369         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
13370         set_dir_limits $maxsize $((maxsize * 6 / 8))
13371         stack_trap "set_dir_limits 0 0"
13372         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
13373         local dirsize=$(stat -c%s "$DIR/$tdir")
13374         local nfiles=0
13375         while (( $dirsize <= $maxsize )); do
13376                 $MCREATE $DIR/$tdir/file_base_$nfiles
13377                 rc=$?
13378                 # check two errors:
13379                 # ENOSPC for ext4 max_dir_size, which has been used since
13380                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
13381                 if (( rc == ENOSPC )); then
13382                         set_dir_limits 0 0
13383                         echo "rc=$rc returned as expected after $nfiles files"
13384
13385                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
13386                                 error "create failed w/o dir size limit"
13387
13388                         # messages may be rate limited if test is run repeatedly
13389                         check_mds_dmesg '"is approaching max"' ||
13390                                 echo "warning message should be output"
13391                         check_mds_dmesg '"has reached max"' ||
13392                                 echo "reached message should be output"
13393
13394                         dirsize=$(stat -c%s "$DIR/$tdir")
13395
13396                         [[ $dirsize -ge $maxsize ]] && return 0
13397                         error "dirsize $dirsize < $maxsize after $nfiles files"
13398                 elif (( rc != 0 )); then
13399                         break
13400                 fi
13401                 nfiles=$((nfiles + 1))
13402                 dirsize=$(stat -c%s "$DIR/$tdir")
13403         done
13404
13405         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
13406 }
13407 run_test 129 "test directory size limit ========================"
13408
13409 OLDIFS="$IFS"
13410 cleanup_130() {
13411         trap 0
13412         IFS="$OLDIFS"
13413 }
13414
13415 test_130a() {
13416         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13417         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13418
13419         trap cleanup_130 EXIT RETURN
13420
13421         local fm_file=$DIR/$tfile
13422         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
13423         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
13424                 error "dd failed for $fm_file"
13425
13426         # LU-1795: test filefrag/FIEMAP once, even if unsupported
13427         filefrag -ves $fm_file
13428         RC=$?
13429         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13430                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13431         [ $RC != 0 ] && error "filefrag $fm_file failed"
13432
13433         filefrag_op=$(filefrag -ve -k $fm_file |
13434                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13435         lun=$($LFS getstripe -i $fm_file)
13436
13437         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
13438         IFS=$'\n'
13439         tot_len=0
13440         for line in $filefrag_op
13441         do
13442                 frag_lun=`echo $line | cut -d: -f5`
13443                 ext_len=`echo $line | cut -d: -f4`
13444                 if (( $frag_lun != $lun )); then
13445                         cleanup_130
13446                         error "FIEMAP on 1-stripe file($fm_file) failed"
13447                         return
13448                 fi
13449                 (( tot_len += ext_len ))
13450         done
13451
13452         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
13453                 cleanup_130
13454                 error "FIEMAP on 1-stripe file($fm_file) failed;"
13455                 return
13456         fi
13457
13458         cleanup_130
13459
13460         echo "FIEMAP on single striped file succeeded"
13461 }
13462 run_test 130a "FIEMAP (1-stripe file)"
13463
13464 test_130b() {
13465         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13466
13467         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13468         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13469
13470         trap cleanup_130 EXIT RETURN
13471
13472         local fm_file=$DIR/$tfile
13473         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13474                         error "setstripe on $fm_file"
13475         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13476                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13477
13478         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
13479                 error "dd failed on $fm_file"
13480
13481         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13482         filefrag_op=$(filefrag -ve -k $fm_file |
13483                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13484
13485         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13486                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13487
13488         IFS=$'\n'
13489         tot_len=0
13490         num_luns=1
13491         for line in $filefrag_op
13492         do
13493                 frag_lun=$(echo $line | cut -d: -f5 |
13494                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13495                 ext_len=$(echo $line | cut -d: -f4)
13496                 if (( $frag_lun != $last_lun )); then
13497                         if (( tot_len != 1024 )); then
13498                                 cleanup_130
13499                                 error "FIEMAP on $fm_file failed; returned " \
13500                                 "len $tot_len for OST $last_lun instead of 1024"
13501                                 return
13502                         else
13503                                 (( num_luns += 1 ))
13504                                 tot_len=0
13505                         fi
13506                 fi
13507                 (( tot_len += ext_len ))
13508                 last_lun=$frag_lun
13509         done
13510         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
13511                 cleanup_130
13512                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13513                         "luns or wrong len for OST $last_lun"
13514                 return
13515         fi
13516
13517         cleanup_130
13518
13519         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
13520 }
13521 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
13522
13523 test_130c() {
13524         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13525
13526         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13527         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13528
13529         trap cleanup_130 EXIT RETURN
13530
13531         local fm_file=$DIR/$tfile
13532         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
13533         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13534                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13535
13536         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
13537                         error "dd failed on $fm_file"
13538
13539         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13540         filefrag_op=$(filefrag -ve -k $fm_file |
13541                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13542
13543         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13544                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13545
13546         IFS=$'\n'
13547         tot_len=0
13548         num_luns=1
13549         for line in $filefrag_op
13550         do
13551                 frag_lun=$(echo $line | cut -d: -f5 |
13552                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13553                 ext_len=$(echo $line | cut -d: -f4)
13554                 if (( $frag_lun != $last_lun )); then
13555                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
13556                         if (( logical != 512 )); then
13557                                 cleanup_130
13558                                 error "FIEMAP on $fm_file failed; returned " \
13559                                 "logical start for lun $logical instead of 512"
13560                                 return
13561                         fi
13562                         if (( tot_len != 512 )); then
13563                                 cleanup_130
13564                                 error "FIEMAP on $fm_file failed; returned " \
13565                                 "len $tot_len for OST $last_lun instead of 1024"
13566                                 return
13567                         else
13568                                 (( num_luns += 1 ))
13569                                 tot_len=0
13570                         fi
13571                 fi
13572                 (( tot_len += ext_len ))
13573                 last_lun=$frag_lun
13574         done
13575         if (( num_luns != 2 || tot_len != 512 )); then
13576                 cleanup_130
13577                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13578                         "luns or wrong len for OST $last_lun"
13579                 return
13580         fi
13581
13582         cleanup_130
13583
13584         echo "FIEMAP on 2-stripe file with hole succeeded"
13585 }
13586 run_test 130c "FIEMAP (2-stripe file with hole)"
13587
13588 test_130d() {
13589         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
13590
13591         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13592         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13593
13594         trap cleanup_130 EXIT RETURN
13595
13596         local fm_file=$DIR/$tfile
13597         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13598                         error "setstripe on $fm_file"
13599         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
13600                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
13601
13602         local actual_stripe_count=$($LFS getstripe -c $fm_file)
13603         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
13604                 error "dd failed on $fm_file"
13605
13606         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13607         filefrag_op=$(filefrag -ve -k $fm_file |
13608                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13609
13610         last_lun=$(echo $filefrag_op | cut -d: -f5 |
13611                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13612
13613         IFS=$'\n'
13614         tot_len=0
13615         num_luns=1
13616         for line in $filefrag_op
13617         do
13618                 frag_lun=$(echo $line | cut -d: -f5 |
13619                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13620                 ext_len=$(echo $line | cut -d: -f4)
13621                 if (( $frag_lun != $last_lun )); then
13622                         if (( tot_len != 1024 )); then
13623                                 cleanup_130
13624                                 error "FIEMAP on $fm_file failed; returned " \
13625                                 "len $tot_len for OST $last_lun instead of 1024"
13626                                 return
13627                         else
13628                                 (( num_luns += 1 ))
13629                                 tot_len=0
13630                         fi
13631                 fi
13632                 (( tot_len += ext_len ))
13633                 last_lun=$frag_lun
13634         done
13635         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
13636                 cleanup_130
13637                 error "FIEMAP on $fm_file failed; returned wrong number of " \
13638                         "luns or wrong len for OST $last_lun"
13639                 return
13640         fi
13641
13642         cleanup_130
13643
13644         echo "FIEMAP on N-stripe file succeeded"
13645 }
13646 run_test 130d "FIEMAP (N-stripe file)"
13647
13648 test_130e() {
13649         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
13650
13651         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13652         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
13653
13654         trap cleanup_130 EXIT RETURN
13655
13656         local fm_file=$DIR/$tfile
13657         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
13658
13659         NUM_BLKS=512
13660         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
13661         for ((i = 0; i < $NUM_BLKS; i++)); do
13662                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) \
13663                         conv=notrunc > /dev/null 2>&1
13664         done
13665
13666         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13667         filefrag_op=$(filefrag -ve -k $fm_file |
13668                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13669
13670         last_lun=$(echo $filefrag_op | cut -d: -f5)
13671
13672         IFS=$'\n'
13673         tot_len=0
13674         num_luns=1
13675         for line in $filefrag_op; do
13676                 frag_lun=$(echo $line | cut -d: -f5)
13677                 ext_len=$(echo $line | cut -d: -f4)
13678                 if [[ "$frag_lun" != "$last_lun" ]]; then
13679                         if (( tot_len != $EXPECTED_LEN )); then
13680                                 cleanup_130
13681                                 error "OST$last_lun $tot_len != $EXPECTED_LEN"
13682                         else
13683                                 (( num_luns += 1 ))
13684                                 tot_len=0
13685                         fi
13686                 fi
13687                 (( tot_len += ext_len ))
13688                 last_lun=$frag_lun
13689         done
13690         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
13691                 cleanup_130
13692                 error "OST$last_lun $num_luns != 2, $tot_len != $EXPECTED_LEN"
13693         fi
13694
13695         echo "FIEMAP with continuation calls succeeded"
13696 }
13697 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
13698
13699 test_130f() {
13700         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13701         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
13702
13703         local fm_file=$DIR/$tfile
13704         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
13705                 error "multiop create with lov_delay_create on $fm_file"
13706
13707         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13708         filefrag_extents=$(filefrag -vek $fm_file |
13709                            awk '/extents? found/ { print $2 }')
13710         if [[ "$filefrag_extents" != "0" ]]; then
13711                 error "$fm_file: filefrag_extents=$filefrag_extents != 0"
13712         fi
13713
13714         rm -f $fm_file
13715 }
13716 run_test 130f "FIEMAP (unstriped file)"
13717
13718 test_130g() {
13719         local file=$DIR/$tfile
13720         local nr=$((OSTCOUNT * 100))
13721
13722         $LFS setstripe -C $nr $file ||
13723                 error "failed to setstripe -C $nr $file"
13724
13725         dd if=/dev/zero of=$file count=$nr bs=1M
13726         sync
13727         nr=$($LFS getstripe -c $file)
13728
13729         local extents=$(filefrag -v $file |
13730                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}' | wc -l)
13731
13732         echo "filefrag list $extents extents in file with stripecount $nr"
13733         if (( extents < nr )); then
13734                 $LFS getstripe $file
13735                 filefrag -v $file
13736                 error "filefrag printed $extents < $nr extents"
13737         fi
13738
13739         rm -f $file
13740 }
13741 run_test 130g "FIEMAP (overstripe file)"
13742
13743 # Test for writev/readv
13744 test_131a() {
13745         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
13746                 error "writev test failed"
13747         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
13748                 error "readv failed"
13749         rm -f $DIR/$tfile
13750 }
13751 run_test 131a "test iov's crossing stripe boundary for writev/readv"
13752
13753 test_131b() {
13754         local fsize=$((524288 + 1048576 + 1572864))
13755         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
13756                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
13757                         error "append writev test failed"
13758
13759         ((fsize += 1572864 + 1048576))
13760         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
13761                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
13762                         error "append writev test failed"
13763         rm -f $DIR/$tfile
13764 }
13765 run_test 131b "test append writev"
13766
13767 test_131c() {
13768         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
13769         error "NOT PASS"
13770 }
13771 run_test 131c "test read/write on file w/o objects"
13772
13773 test_131d() {
13774         rwv -f $DIR/$tfile -w -n 1 1572864
13775         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
13776         if [ "$NOB" != 1572864 ]; then
13777                 error "Short read filed: read $NOB bytes instead of 1572864"
13778         fi
13779         rm -f $DIR/$tfile
13780 }
13781 run_test 131d "test short read"
13782
13783 test_131e() {
13784         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
13785         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
13786         error "read hitting hole failed"
13787         rm -f $DIR/$tfile
13788 }
13789 run_test 131e "test read hitting hole"
13790
13791 check_stats() {
13792         local facet=$1
13793         local op=$2
13794         local want=${3:-0}
13795         local res
13796
13797         case $facet in
13798         mds*) res=$(do_facet $facet \
13799                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
13800                  ;;
13801         ost*) res=$(do_facet $facet \
13802                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
13803                  ;;
13804         *) error "Wrong facet '$facet'" ;;
13805         esac
13806         [ "$res" ] || error "The counter for $op on $facet was not incremented"
13807         # if the argument $3 is zero, it means any stat increment is ok.
13808         if [[ $want -gt 0 ]]; then
13809                 local count=$(echo $res | awk '{ print $2 }')
13810                 [[ $count -ne $want ]] &&
13811                         error "The $op counter on $facet is $count, not $want"
13812         fi
13813 }
13814
13815 test_133a() {
13816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13817         remote_ost_nodsh && skip "remote OST with nodsh"
13818         remote_mds_nodsh && skip "remote MDS with nodsh"
13819         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
13820                 skip_env "MDS doesn't support rename stats"
13821
13822         local testdir=$DIR/${tdir}/stats_testdir
13823
13824         mkdir -p $DIR/${tdir}
13825
13826         # clear stats.
13827         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13828         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13829
13830         # verify mdt stats first.
13831         mkdir ${testdir} || error "mkdir failed"
13832         check_stats $SINGLEMDS "mkdir" 1
13833         touch ${testdir}/${tfile} || error "touch failed"
13834         check_stats $SINGLEMDS "open" 1
13835         check_stats $SINGLEMDS "close" 1
13836         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
13837                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
13838                 check_stats $SINGLEMDS "mknod" 2
13839         }
13840         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
13841         check_stats $SINGLEMDS "unlink" 1
13842         rm -f ${testdir}/${tfile} || error "file remove failed"
13843         check_stats $SINGLEMDS "unlink" 2
13844
13845         # remove working dir and check mdt stats again.
13846         rmdir ${testdir} || error "rmdir failed"
13847         check_stats $SINGLEMDS "rmdir" 1
13848
13849         local testdir1=$DIR/${tdir}/stats_testdir1
13850         mkdir -p ${testdir}
13851         mkdir -p ${testdir1}
13852         touch ${testdir1}/test1
13853         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
13854         check_stats $SINGLEMDS "crossdir_rename" 1
13855
13856         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
13857         check_stats $SINGLEMDS "samedir_rename" 1
13858
13859         rm -rf $DIR/${tdir}
13860 }
13861 run_test 133a "Verifying MDT stats ========================================"
13862
13863 test_133b() {
13864         local res
13865
13866         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13867         remote_ost_nodsh && skip "remote OST with nodsh"
13868         remote_mds_nodsh && skip "remote MDS with nodsh"
13869
13870         local testdir=$DIR/${tdir}/stats_testdir
13871
13872         mkdir -p ${testdir} || error "mkdir failed"
13873         touch ${testdir}/${tfile} || error "touch failed"
13874         cancel_lru_locks mdc
13875
13876         # clear stats.
13877         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13878         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13879
13880         # extra mdt stats verification.
13881         chmod 444 ${testdir}/${tfile} || error "chmod failed"
13882         check_stats $SINGLEMDS "setattr" 1
13883         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13884         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
13885         then            # LU-1740
13886                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
13887                 check_stats $SINGLEMDS "getattr" 1
13888         fi
13889         rm -rf $DIR/${tdir}
13890
13891         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
13892         # so the check below is not reliable
13893         [ $MDSCOUNT -eq 1 ] || return 0
13894
13895         # Sleep to avoid a cached response.
13896         #define OBD_STATFS_CACHE_SECONDS 1
13897         sleep 2
13898         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13899         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
13900         $LFS df || error "lfs failed"
13901         check_stats $SINGLEMDS "statfs" 1
13902
13903         # check aggregated statfs (LU-10018)
13904         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
13905                 return 0
13906         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
13907                 return 0
13908         sleep 2
13909         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13910         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
13911         df $DIR
13912         check_stats $SINGLEMDS "statfs" 1
13913
13914         # We want to check that the client didn't send OST_STATFS to
13915         # ost1 but the MDT also uses OST_STATFS for precreate. So some
13916         # extra care is needed here.
13917         if remote_mds; then
13918                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
13919                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
13920
13921                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
13922                 [ "$res" ] && error "OST got STATFS"
13923         fi
13924
13925         return 0
13926 }
13927 run_test 133b "Verifying extra MDT stats =================================="
13928
13929 test_133c() {
13930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13931         remote_ost_nodsh && skip "remote OST with nodsh"
13932         remote_mds_nodsh && skip "remote MDS with nodsh"
13933
13934         local testdir=$DIR/$tdir/stats_testdir
13935
13936         test_mkdir -p $testdir
13937
13938         # verify obdfilter stats.
13939         $LFS setstripe -c 1 -i 0 $testdir/$tfile
13940         sync
13941         cancel_lru_locks osc
13942         wait_delete_completed
13943
13944         # clear stats.
13945         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
13946         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
13947
13948         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
13949                 error "dd failed"
13950         sync
13951         cancel_lru_locks osc
13952         check_stats ost1 "write" 1
13953
13954         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
13955         check_stats ost1 "read" 1
13956
13957         > $testdir/$tfile || error "truncate failed"
13958         check_stats ost1 "punch" 1
13959
13960         rm -f $testdir/$tfile || error "file remove failed"
13961         wait_delete_completed
13962         check_stats ost1 "destroy" 1
13963
13964         rm -rf $DIR/$tdir
13965 }
13966 run_test 133c "Verifying OST stats ========================================"
13967
13968 order_2() {
13969         local value=$1
13970         local orig=$value
13971         local order=1
13972
13973         while [ $value -ge 2 ]; do
13974                 order=$((order*2))
13975                 value=$((value/2))
13976         done
13977
13978         if [ $orig -gt $order ]; then
13979                 order=$((order*2))
13980         fi
13981         echo $order
13982 }
13983
13984 size_in_KMGT() {
13985     local value=$1
13986     local size=('K' 'M' 'G' 'T');
13987     local i=0
13988     local size_string=$value
13989
13990     while [ $value -ge 1024 ]; do
13991         if [ $i -gt 3 ]; then
13992             #T is the biggest unit we get here, if that is bigger,
13993             #just return XXXT
13994             size_string=${value}T
13995             break
13996         fi
13997         value=$((value >> 10))
13998         if [ $value -lt 1024 ]; then
13999             size_string=${value}${size[$i]}
14000             break
14001         fi
14002         i=$((i + 1))
14003     done
14004
14005     echo $size_string
14006 }
14007
14008 get_rename_size() {
14009         local size=$1
14010         local context=${2:-.}
14011         local sample=$(do_facet $SINGLEMDS $LCTL \
14012                 get_param mdt.$FSNAME-MDT0000.rename_stats |
14013                 grep -A1 $context |
14014                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
14015         echo $sample
14016 }
14017
14018 test_133d() {
14019         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14020         remote_ost_nodsh && skip "remote OST with nodsh"
14021         remote_mds_nodsh && skip "remote MDS with nodsh"
14022         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
14023                 skip_env "MDS doesn't support rename stats"
14024
14025         local testdir1=$DIR/${tdir}/stats_testdir1
14026         local testdir2=$DIR/${tdir}/stats_testdir2
14027         mkdir -p $DIR/${tdir}
14028
14029         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14030
14031         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
14032         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
14033
14034         createmany -o $testdir1/test 512 || error "createmany failed"
14035
14036         # check samedir rename size
14037         mv ${testdir1}/test0 ${testdir1}/test_0
14038
14039         local testdir1_size=$(ls -l $DIR/${tdir} |
14040                 awk '/stats_testdir1/ {print $5}')
14041         local testdir2_size=$(ls -l $DIR/${tdir} |
14042                 awk '/stats_testdir2/ {print $5}')
14043
14044         testdir1_size=$(order_2 $testdir1_size)
14045         testdir2_size=$(order_2 $testdir2_size)
14046
14047         testdir1_size=$(size_in_KMGT $testdir1_size)
14048         testdir2_size=$(size_in_KMGT $testdir2_size)
14049
14050         echo "source rename dir size: ${testdir1_size}"
14051         echo "target rename dir size: ${testdir2_size}"
14052
14053         local cmd="do_facet $SINGLEMDS $LCTL "
14054         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
14055
14056         eval $cmd || error "$cmd failed"
14057         local samedir=$($cmd | grep 'same_dir')
14058         local same_sample=$(get_rename_size $testdir1_size)
14059         [ -z "$samedir" ] && error "samedir_rename_size count error"
14060         [[ $same_sample -eq 1 ]] ||
14061                 error "samedir_rename_size error $same_sample"
14062         echo "Check same dir rename stats success"
14063
14064         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14065
14066         # check crossdir rename size
14067         mv ${testdir1}/test_0 ${testdir2}/test_0
14068
14069         testdir1_size=$(ls -l $DIR/${tdir} |
14070                 awk '/stats_testdir1/ {print $5}')
14071         testdir2_size=$(ls -l $DIR/${tdir} |
14072                 awk '/stats_testdir2/ {print $5}')
14073
14074         testdir1_size=$(order_2 $testdir1_size)
14075         testdir2_size=$(order_2 $testdir2_size)
14076
14077         testdir1_size=$(size_in_KMGT $testdir1_size)
14078         testdir2_size=$(size_in_KMGT $testdir2_size)
14079
14080         echo "source rename dir size: ${testdir1_size}"
14081         echo "target rename dir size: ${testdir2_size}"
14082
14083         eval $cmd || error "$cmd failed"
14084         local crossdir=$($cmd | grep 'crossdir')
14085         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
14086         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
14087         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
14088         [[ $src_sample -eq 1 ]] ||
14089                 error "crossdir_rename_size error $src_sample"
14090         [[ $tgt_sample -eq 1 ]] ||
14091                 error "crossdir_rename_size error $tgt_sample"
14092         echo "Check cross dir rename stats success"
14093         rm -rf $DIR/${tdir}
14094 }
14095 run_test 133d "Verifying rename_stats ========================================"
14096
14097 test_133e() {
14098         remote_mds_nodsh && skip "remote MDS with nodsh"
14099         remote_ost_nodsh && skip "remote OST with nodsh"
14100         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14101
14102         local testdir=$DIR/${tdir}/stats_testdir
14103         local ctr f0 f1 bs=32768 count=42 sum
14104
14105         mkdir -p ${testdir} || error "mkdir failed"
14106
14107         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
14108
14109         for ctr in {write,read}_bytes; do
14110                 sync
14111                 cancel_lru_locks osc
14112
14113                 do_facet ost1 $LCTL set_param -n \
14114                         "obdfilter.*.exports.clear=clear"
14115
14116                 if [ $ctr = write_bytes ]; then
14117                         f0=/dev/zero
14118                         f1=${testdir}/${tfile}
14119                 else
14120                         f0=${testdir}/${tfile}
14121                         f1=/dev/null
14122                 fi
14123
14124                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
14125                         error "dd failed"
14126                 sync
14127                 cancel_lru_locks osc
14128
14129                 sum=$(do_facet ost1 $LCTL get_param \
14130                         "obdfilter.*.exports.*.stats" |
14131                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
14132                                 $1 == ctr { sum += $7 }
14133                                 END { printf("%0.0f", sum) }')
14134
14135                 if ((sum != bs * count)); then
14136                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
14137                 fi
14138         done
14139
14140         rm -rf $DIR/${tdir}
14141 }
14142 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
14143
14144 test_133f() {
14145         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
14146                 skip "too old lustre for get_param -R ($facet_ver)"
14147
14148         # verifying readability.
14149         $LCTL get_param -R '*' &> /dev/null
14150
14151         # Verifing writability with badarea_io.
14152         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14153         local skipped_params='force_lbug|changelog_mask|daemon_file'
14154         $LCTL list_param -FR '*' | grep '=' | tr -d = |
14155                 egrep -v "$skipped_params" |
14156                 xargs -n 1 find $proc_dirs -name |
14157                 xargs -n 1 badarea_io ||
14158                 error "client badarea_io failed"
14159
14160         # remount the FS in case writes/reads /proc break the FS
14161         cleanup || error "failed to unmount"
14162         setup || error "failed to setup"
14163 }
14164 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
14165
14166 test_133g() {
14167         remote_mds_nodsh && skip "remote MDS with nodsh"
14168         remote_ost_nodsh && skip "remote OST with nodsh"
14169
14170         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14171         local skipped_params="'force_lbug|changelog_mask|daemon_file'"
14172         local facet
14173         for facet in mds1 ost1; do
14174                 local facet_ver=$(lustre_version_code $facet)
14175                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
14176                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
14177                 else
14178                         log "$facet: too old lustre for get_param -R"
14179                 fi
14180                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
14181                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
14182                                 tr -d = | egrep -v $skipped_params |
14183                                 xargs -n 1 find $proc_dirs -name |
14184                                 xargs -n 1 badarea_io" ||
14185                                         error "$facet badarea_io failed"
14186                 else
14187                         skip_noexit "$facet: too old lustre for get_param -R"
14188                 fi
14189         done
14190
14191         # remount the FS in case writes/reads /proc break the FS
14192         cleanup || error "failed to unmount"
14193         setup || error "failed to setup"
14194 }
14195 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
14196
14197 test_133h() {
14198         remote_mds_nodsh && skip "remote MDS with nodsh"
14199         remote_ost_nodsh && skip "remote OST with nodsh"
14200         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
14201                 skip "Need MDS version at least 2.9.54"
14202
14203         local facet
14204         for facet in client mds1 ost1; do
14205                 # Get the list of files that are missing the terminating newline
14206                 local plist=$(do_facet $facet
14207                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
14208                 local ent
14209                 for ent in $plist; do
14210                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
14211                                 awk -v FS='\v' -v RS='\v\v' \
14212                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
14213                                         print FILENAME}'" 2>/dev/null)
14214                         [ -z $missing ] || {
14215                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
14216                                 error "file does not end with newline: $facet-$ent"
14217                         }
14218                 done
14219         done
14220 }
14221 run_test 133h "Proc files should end with newlines"
14222
14223 test_134a() {
14224         remote_mds_nodsh && skip "remote MDS with nodsh"
14225         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14226                 skip "Need MDS version at least 2.7.54"
14227
14228         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14229         cancel_lru_locks mdc
14230
14231         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
14232         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14233         [ $unused -eq 0 ] || error "$unused locks are not cleared"
14234
14235         local nr=1000
14236         createmany -o $DIR/$tdir/f $nr ||
14237                 error "failed to create $nr files in $DIR/$tdir"
14238         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14239
14240         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
14241         do_facet mds1 $LCTL set_param fail_loc=0x327
14242         do_facet mds1 $LCTL set_param fail_val=500
14243         touch $DIR/$tdir/m
14244
14245         echo "sleep 10 seconds ..."
14246         sleep 10
14247         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
14248
14249         do_facet mds1 $LCTL set_param fail_loc=0
14250         do_facet mds1 $LCTL set_param fail_val=0
14251         [ $lck_cnt -lt $unused ] ||
14252                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
14253
14254         rm $DIR/$tdir/m
14255         unlinkmany $DIR/$tdir/f $nr
14256 }
14257 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
14258
14259 test_134b() {
14260         remote_mds_nodsh && skip "remote MDS with nodsh"
14261         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14262                 skip "Need MDS version at least 2.7.54"
14263
14264         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14265         cancel_lru_locks mdc
14266
14267         local low_wm=$(do_facet mds1 $LCTL get_param -n \
14268                         ldlm.lock_reclaim_threshold_mb)
14269         # disable reclaim temporarily
14270         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
14271
14272         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
14273         do_facet mds1 $LCTL set_param fail_loc=0x328
14274         do_facet mds1 $LCTL set_param fail_val=500
14275
14276         $LCTL set_param debug=+trace
14277
14278         local nr=600
14279         createmany -o $DIR/$tdir/f $nr &
14280         local create_pid=$!
14281
14282         echo "Sleep $TIMEOUT seconds ..."
14283         sleep $TIMEOUT
14284         if ! ps -p $create_pid  > /dev/null 2>&1; then
14285                 do_facet mds1 $LCTL set_param fail_loc=0
14286                 do_facet mds1 $LCTL set_param fail_val=0
14287                 do_facet mds1 $LCTL set_param \
14288                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
14289                 error "createmany finished incorrectly!"
14290         fi
14291         do_facet mds1 $LCTL set_param fail_loc=0
14292         do_facet mds1 $LCTL set_param fail_val=0
14293         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
14294         wait $create_pid || return 1
14295
14296         unlinkmany $DIR/$tdir/f $nr
14297 }
14298 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
14299
14300 test_135() {
14301         remote_mds_nodsh && skip "remote MDS with nodsh"
14302         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14303                 skip "Need MDS version at least 2.13.50"
14304         local fname
14305
14306         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14307
14308 #define OBD_FAIL_PLAIN_RECORDS 0x1319
14309         #set only one record at plain llog
14310         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
14311
14312         #fill already existed plain llog each 64767
14313         #wrapping whole catalog
14314         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14315
14316         createmany -o $DIR/$tdir/$tfile_ 64700
14317         for (( i = 0; i < 64700; i = i + 2 ))
14318         do
14319                 rm $DIR/$tdir/$tfile_$i &
14320                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14321                 local pid=$!
14322                 wait $pid
14323         done
14324
14325         #waiting osp synchronization
14326         wait_delete_completed
14327 }
14328 run_test 135 "Race catalog processing"
14329
14330 test_136() {
14331         remote_mds_nodsh && skip "remote MDS with nodsh"
14332         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14333                 skip "Need MDS version at least 2.13.50"
14334         local fname
14335
14336         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14337         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
14338         #set only one record at plain llog
14339 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
14340         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
14341
14342         #fill already existed 2 plain llogs each 64767
14343         #wrapping whole catalog
14344         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14345         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
14346         wait_delete_completed
14347
14348         createmany -o $DIR/$tdir/$tfile_ 10
14349         sleep 25
14350
14351         do_facet $SINGLEMDS $LCTL set_param fail_val=3
14352         for (( i = 0; i < 10; i = i + 3 ))
14353         do
14354                 rm $DIR/$tdir/$tfile_$i &
14355                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14356                 local pid=$!
14357                 wait $pid
14358                 sleep 7
14359                 rm $DIR/$tdir/$tfile_$((i + 2)) &
14360         done
14361
14362         #waiting osp synchronization
14363         wait_delete_completed
14364 }
14365 run_test 136 "Race catalog processing 2"
14366
14367 test_140() { #bug-17379
14368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14369
14370         test_mkdir $DIR/$tdir
14371         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
14372         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
14373
14374         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
14375         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
14376         local i=0
14377         while i=$((i + 1)); do
14378                 test_mkdir $i
14379                 cd $i || error "Changing to $i"
14380                 ln -s ../stat stat || error "Creating stat symlink"
14381                 # Read the symlink until ELOOP present,
14382                 # not LBUGing the system is considered success,
14383                 # we didn't overrun the stack.
14384                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
14385                 if [ $ret -ne 0 ]; then
14386                         if [ $ret -eq 40 ]; then
14387                                 break  # -ELOOP
14388                         else
14389                                 error "Open stat symlink"
14390                                         return
14391                         fi
14392                 fi
14393         done
14394         i=$((i - 1))
14395         echo "The symlink depth = $i"
14396         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
14397                 error "Invalid symlink depth"
14398
14399         # Test recursive symlink
14400         ln -s symlink_self symlink_self
14401         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
14402         echo "open symlink_self returns $ret"
14403         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
14404 }
14405 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
14406
14407 test_150a() {
14408         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14409
14410         local TF="$TMP/$tfile"
14411
14412         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14413         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14414         cp $TF $DIR/$tfile
14415         cancel_lru_locks $OSC
14416         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
14417         remount_client $MOUNT
14418         df -P $MOUNT
14419         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
14420
14421         $TRUNCATE $TF 6000
14422         $TRUNCATE $DIR/$tfile 6000
14423         cancel_lru_locks $OSC
14424         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
14425
14426         echo "12345" >>$TF
14427         echo "12345" >>$DIR/$tfile
14428         cancel_lru_locks $OSC
14429         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
14430
14431         echo "12345" >>$TF
14432         echo "12345" >>$DIR/$tfile
14433         cancel_lru_locks $OSC
14434         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
14435 }
14436 run_test 150a "truncate/append tests"
14437
14438 test_150b() {
14439         check_set_fallocate_or_skip
14440
14441         touch $DIR/$tfile
14442         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14443         check_fallocate $DIR/$tfile || error "fallocate failed"
14444 }
14445 run_test 150b "Verify fallocate (prealloc) functionality"
14446
14447 test_150bb() {
14448         check_set_fallocate_or_skip
14449
14450         touch $DIR/$tfile
14451         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14452         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=20 || error "dd failed"
14453         > $DIR/$tfile
14454         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14455         # precomputed md5sum for 20MB of zeroes
14456         local expect="8f4e33f3dc3e414ff94e5fb6905cba8c"
14457         local sum=($(md5sum $DIR/$tfile))
14458
14459         [[ "${sum[0]}" == "$expect" ]] || error "fallocate unwritten is not zero"
14460
14461         check_set_fallocate 1
14462
14463         > $DIR/$tfile
14464         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14465         sum=($(md5sum $DIR/$tfile))
14466
14467         [[ "${sum[0]}" == "$expect" ]] || error "fallocate zero is not zero"
14468 }
14469 run_test 150bb "Verify fallocate modes both zero space"
14470
14471 test_150c() {
14472         check_set_fallocate_or_skip
14473         local striping="-c2"
14474
14475         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14476         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tfile || error "setstripe failed"
14477         fallocate -l ${OSTCOUNT}m $DIR/$tfile || error "fallocate failed"
14478         local bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14479         local want=$((OSTCOUNT * 1048576))
14480
14481         # Must allocate all requested space, not more than 5% extra
14482         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14483                 error "bytes $bytes is not $want"
14484
14485         rm -f $DIR/$tfile
14486
14487         echo "verify fallocate on PFL file"
14488
14489         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14490
14491         $LFS setstripe -E1M $striping -E16M -c3 -Eeof -c 4 $DIR/$tfile ||
14492                 error "Create $DIR/$tfile failed"
14493         fallocate -l $((1048576 * 512)) $DIR/$tfile ||
14494                         error "fallocate failed"
14495         bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14496         want=$((512 * 1048576))
14497
14498         # Must allocate all requested space, not more than 5% extra
14499         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14500                 error "bytes $bytes is not $want"
14501 }
14502 run_test 150c "Verify fallocate Size and Blocks"
14503
14504 test_150d() {
14505         check_set_fallocate_or_skip
14506         local striping="-c2"
14507
14508         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14509
14510         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14511         $LFS setstripe -E1M $striping -E eof -c $OSTCOUNT -S1M $DIR/$tdir ||
14512                 error "setstripe failed"
14513         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
14514         local bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
14515         local want=$((OSTCOUNT * 1048576))
14516
14517         # Must allocate all requested space, not more than 5% extra
14518         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14519                 error "bytes $bytes is not $want"
14520 }
14521 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
14522
14523 test_150e() {
14524         check_set_fallocate_or_skip
14525
14526         echo "df before:"
14527         $LFS df
14528         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14529         $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14530                 error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14531
14532         # Find OST with Minimum Size
14533         min_size_ost=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14534                        sort -un | head -1)
14535
14536         # Get 100MB per OST of the available space to reduce run time
14537         # else 60% of the available space if we are running SLOW tests
14538         if [ $SLOW == "no" ]; then
14539                 local space=$((1024 * 100 * OSTCOUNT))
14540         else
14541                 local space=$(((min_size_ost * 60)/100 * OSTCOUNT))
14542         fi
14543
14544         fallocate -l${space}k $DIR/$tfile ||
14545                 error "fallocate ${space}k $DIR/$tfile failed"
14546         echo "'fallocate -l ${space}k $DIR/$tfile' succeeded"
14547
14548         # get size immediately after fallocate. This should be correctly
14549         # updated
14550         local size=$(stat -c '%s' $DIR/$tfile)
14551         local used=$(( $(stat -c '%b * %B' $DIR/$tfile) / 1024))
14552
14553         # Sleep for a while for statfs to get updated. And not pull from cache.
14554         sleep 2
14555
14556         echo "df after fallocate:"
14557         $LFS df
14558
14559         (( size / 1024 == space )) || error "size $size != requested $space"
14560         [ "$ost1_FSTYPE" != ldiskfs ] || (( used >= space )) ||
14561                 error "used $used < space $space"
14562
14563         rm $DIR/$tfile || error "rm failed"
14564         sync
14565         wait_delete_completed
14566
14567         echo "df after unlink:"
14568         $LFS df
14569 }
14570 run_test 150e "Verify 60% of available OST space consumed by fallocate"
14571
14572 test_150f() {
14573         local size
14574         local blocks
14575         local want_size_before=20480 # in bytes
14576         local want_blocks_before=40 # 512 sized blocks
14577         local want_blocks_after=24  # 512 sized blocks
14578         local length=$(((want_blocks_before - want_blocks_after) * 512))
14579
14580         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
14581                 skip "need at least 2.14.0 for fallocate punch"
14582
14583         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
14584                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
14585         fi
14586
14587         check_set_fallocate_or_skip
14588         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14589
14590         [[ "x$DOM" == "xyes" ]] &&
14591                 $LFS setstripe -E1M -L mdt -E eof $DIR/$tfile
14592
14593         echo "Verify fallocate punch: Range within the file range"
14594         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
14595                 error "dd failed for bs 4096 and count 5"
14596
14597         # Call fallocate with punch range which is within the file range
14598         fallocate -p --offset 4096 -l $length $DIR/$tfile ||
14599                 error "fallocate failed: offset 4096 and length $length"
14600         # client must see changes immediately after fallocate
14601         size=$(stat -c '%s' $DIR/$tfile)
14602         blocks=$(stat -c '%b' $DIR/$tfile)
14603
14604         # Verify punch worked.
14605         (( blocks == want_blocks_after )) ||
14606                 error "punch failed: blocks $blocks != $want_blocks_after"
14607
14608         (( size == want_size_before )) ||
14609                 error "punch failed: size $size != $want_size_before"
14610
14611         # Verify there is hole in file
14612         local data_off=$(lseek_test -d 4096 $DIR/$tfile)
14613         # precomputed md5sum
14614         local expect="4a9a834a2db02452929c0a348273b4aa"
14615
14616         cksum=($(md5sum $DIR/$tfile))
14617         [[ "${cksum[0]}" == "$expect" ]] ||
14618                 error "unexpected MD5SUM after punch: ${cksum[0]}"
14619
14620         # Start second sub-case for fallocate punch.
14621         echo "Verify fallocate punch: Range overlapping and less than blocksize"
14622         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
14623                 error "dd failed for bs 4096 and count 5"
14624
14625         # Punch range less than block size will have no change in block count
14626         want_blocks_after=40  # 512 sized blocks
14627
14628         # Punch overlaps two blocks and less than blocksize
14629         fallocate -p --offset 4000 -l 3000 $DIR/$tfile ||
14630                 error "fallocate failed: offset 4000 length 3000"
14631         size=$(stat -c '%s' $DIR/$tfile)
14632         blocks=$(stat -c '%b' $DIR/$tfile)
14633
14634         # Verify punch worked.
14635         (( blocks == want_blocks_after )) ||
14636                 error "punch failed: blocks $blocks != $want_blocks_after"
14637
14638         (( size == want_size_before )) ||
14639                 error "punch failed: size $size != $want_size_before"
14640
14641         # Verify if range is really zero'ed out. We expect Zeros.
14642         # precomputed md5sum
14643         expect="c57ec5d769c3dbe3426edc3f7d7e11d3"
14644         cksum=($(md5sum $DIR/$tfile))
14645         [[ "${cksum[0]}" == "$expect" ]] ||
14646                 error "unexpected MD5SUM after punch: ${cksum[0]}"
14647 }
14648 run_test 150f "Verify fallocate punch functionality"
14649
14650 test_150g() {
14651         local space
14652         local size
14653         local blocks
14654         local blocks_after
14655         local size_after
14656         local BS=4096 # Block size in bytes
14657
14658         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
14659                 skip "need at least 2.14.0 for fallocate punch"
14660
14661         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
14662                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
14663         fi
14664
14665         check_set_fallocate_or_skip
14666         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14667
14668         if [[ "x$DOM" == "xyes" ]]; then
14669                 $LFS setstripe -E2M -L mdt -E eof -c${OSTCOUNT} $DIR/$tfile ||
14670                         error "$LFS setstripe DoM + ${OSTCOUNT} OST failed"
14671         else
14672                 $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14673                         error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14674         fi
14675
14676         # Get 100MB per OST of the available space to reduce run time
14677         # else 60% of the available space if we are running SLOW tests
14678         if [ $SLOW == "no" ]; then
14679                 space=$((1024 * 100 * OSTCOUNT))
14680         else
14681                 # Find OST with Minimum Size
14682                 space=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14683                         sort -un | head -1)
14684                 echo "min size OST: $space"
14685                 space=$(((space * 60)/100 * OSTCOUNT))
14686         fi
14687         # space in 1k units, round to 4k blocks
14688         local blkcount=$((space * 1024 / $BS))
14689
14690         echo "Verify fallocate punch: Very large Range"
14691         fallocate -l${space}k $DIR/$tfile ||
14692                 error "fallocate ${space}k $DIR/$tfile failed"
14693         # write 1M at the end, start and in the middle
14694         yes 'A' | dd of=$DIR/$tfile bs=$BS count=256 ||
14695                 error "dd failed: bs $BS count 256"
14696         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount - 256)) count=256 ||
14697                 error "dd failed: bs $BS count 256 seek $((blkcount - 256))"
14698         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount / 2)) count=1024 ||
14699                 error "dd failed: bs $BS count 256 seek $((blkcount / 2))"
14700
14701         # Gather stats.
14702         size=$(stat -c '%s' $DIR/$tfile)
14703
14704         # gather punch length.
14705         local punch_size=$((size - (BS * 2)))
14706
14707         echo "punch_size = $punch_size"
14708         echo "size - punch_size: $((size - punch_size))"
14709         echo "size - punch_size in blocks: $(((size - punch_size)/BS))"
14710
14711         # Call fallocate to punch all except 2 blocks. We leave the
14712         # first and the last block
14713         echo "fallocate -p --offset $BS -l $punch_size $DIR/$tfile"
14714         fallocate -p --offset $BS -l $punch_size $DIR/$tfile ||
14715                 error "fallocate failed: offset $BS length $punch_size"
14716
14717         size_after=$(stat -c '%s' $DIR/$tfile)
14718         blocks_after=$(stat -c '%b' $DIR/$tfile)
14719
14720         # Verify punch worked.
14721         # Size should be kept
14722         (( size == size_after )) ||
14723                 error "punch failed: size $size != $size_after"
14724
14725         # two 4k data blocks to remain plus possible 1 extra extent block
14726         (( blocks_after <= ((BS / 512) * 3) )) ||
14727                 error "too many blocks remains: $blocks_after"
14728
14729         # Verify that file has hole between the first and the last blocks
14730         local hole_start=$(lseek_test -l 0 $DIR/$tfile)
14731         local hole_end=$(lseek_test -d $BS $DIR/$tfile)
14732
14733         echo "Hole at [$hole_start, $hole_end)"
14734         (( hole_start == BS )) ||
14735                 error "no hole at offset $BS after punch"
14736
14737         (( hole_end == BS + punch_size )) ||
14738                 error "data at offset $hole_end < $((BS + punch_size))"
14739 }
14740 run_test 150g "Verify fallocate punch on large range"
14741
14742 #LU-2902 roc_hit was not able to read all values from lproc
14743 function roc_hit_init() {
14744         local list=$(comma_list $(osts_nodes))
14745         local dir=$DIR/$tdir-check
14746         local file=$dir/$tfile
14747         local BEFORE
14748         local AFTER
14749         local idx
14750
14751         test_mkdir $dir
14752         #use setstripe to do a write to every ost
14753         for i in $(seq 0 $((OSTCOUNT-1))); do
14754                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
14755                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
14756                 idx=$(printf %04x $i)
14757                 BEFORE=$(get_osd_param $list *OST*$idx stats |
14758                         awk '$1 == "cache_access" {sum += $7}
14759                                 END { printf("%0.0f", sum) }')
14760
14761                 cancel_lru_locks osc
14762                 cat $file >/dev/null
14763
14764                 AFTER=$(get_osd_param $list *OST*$idx stats |
14765                         awk '$1 == "cache_access" {sum += $7}
14766                                 END { printf("%0.0f", sum) }')
14767
14768                 echo BEFORE:$BEFORE AFTER:$AFTER
14769                 if ! let "AFTER - BEFORE == 4"; then
14770                         rm -rf $dir
14771                         error "roc_hit is not safe to use"
14772                 fi
14773                 rm $file
14774         done
14775
14776         rm -rf $dir
14777 }
14778
14779 function roc_hit() {
14780         local list=$(comma_list $(osts_nodes))
14781         echo $(get_osd_param $list '' stats |
14782                 awk '$1 == "cache_hit" {sum += $7}
14783                         END { printf("%0.0f", sum) }')
14784 }
14785
14786 function set_cache() {
14787         local on=1
14788
14789         if [ "$2" == "off" ]; then
14790                 on=0;
14791         fi
14792         local list=$(comma_list $(osts_nodes))
14793         set_osd_param $list '' $1_cache_enable $on
14794
14795         cancel_lru_locks osc
14796 }
14797
14798 test_151() {
14799         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14800         remote_ost_nodsh && skip "remote OST with nodsh"
14801
14802         local CPAGES=3
14803         local list=$(comma_list $(osts_nodes))
14804
14805         # check whether obdfilter is cache capable at all
14806         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
14807                 skip "not cache-capable obdfilter"
14808         fi
14809
14810         # check cache is enabled on all obdfilters
14811         if get_osd_param $list '' read_cache_enable | grep 0; then
14812                 skip "oss cache is disabled"
14813         fi
14814
14815         set_osd_param $list '' writethrough_cache_enable 1
14816
14817         # check write cache is enabled on all obdfilters
14818         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
14819                 skip "oss write cache is NOT enabled"
14820         fi
14821
14822         roc_hit_init
14823
14824         #define OBD_FAIL_OBD_NO_LRU  0x609
14825         do_nodes $list $LCTL set_param fail_loc=0x609
14826
14827         # pages should be in the case right after write
14828         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
14829                 error "dd failed"
14830
14831         local BEFORE=$(roc_hit)
14832         cancel_lru_locks osc
14833         cat $DIR/$tfile >/dev/null
14834         local AFTER=$(roc_hit)
14835
14836         do_nodes $list $LCTL set_param fail_loc=0
14837
14838         if ! let "AFTER - BEFORE == CPAGES"; then
14839                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
14840         fi
14841
14842         cancel_lru_locks osc
14843         # invalidates OST cache
14844         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
14845         set_osd_param $list '' read_cache_enable 0
14846         cat $DIR/$tfile >/dev/null
14847
14848         # now data shouldn't be found in the cache
14849         BEFORE=$(roc_hit)
14850         cancel_lru_locks osc
14851         cat $DIR/$tfile >/dev/null
14852         AFTER=$(roc_hit)
14853         if let "AFTER - BEFORE != 0"; then
14854                 error "IN CACHE: before: $BEFORE, after: $AFTER"
14855         fi
14856
14857         set_osd_param $list '' read_cache_enable 1
14858         rm -f $DIR/$tfile
14859 }
14860 run_test 151 "test cache on oss and controls ==============================="
14861
14862 test_152() {
14863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14864
14865         local TF="$TMP/$tfile"
14866
14867         # simulate ENOMEM during write
14868 #define OBD_FAIL_OST_NOMEM      0x226
14869         lctl set_param fail_loc=0x80000226
14870         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14871         cp $TF $DIR/$tfile
14872         sync || error "sync failed"
14873         lctl set_param fail_loc=0
14874
14875         # discard client's cache
14876         cancel_lru_locks osc
14877
14878         # simulate ENOMEM during read
14879         lctl set_param fail_loc=0x80000226
14880         cmp $TF $DIR/$tfile || error "cmp failed"
14881         lctl set_param fail_loc=0
14882
14883         rm -f $TF
14884 }
14885 run_test 152 "test read/write with enomem ============================"
14886
14887 test_153() {
14888         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
14889 }
14890 run_test 153 "test if fdatasync does not crash ======================="
14891
14892 dot_lustre_fid_permission_check() {
14893         local fid=$1
14894         local ffid=$MOUNT/.lustre/fid/$fid
14895         local test_dir=$2
14896
14897         echo "stat fid $fid"
14898         stat $ffid > /dev/null || error "stat $ffid failed."
14899         echo "touch fid $fid"
14900         touch $ffid || error "touch $ffid failed."
14901         echo "write to fid $fid"
14902         cat /etc/hosts > $ffid || error "write $ffid failed."
14903         echo "read fid $fid"
14904         diff /etc/hosts $ffid || error "read $ffid failed."
14905         echo "append write to fid $fid"
14906         cat /etc/hosts >> $ffid || error "append write $ffid failed."
14907         echo "rename fid $fid"
14908         mv $ffid $test_dir/$tfile.1 &&
14909                 error "rename $ffid to $tfile.1 should fail."
14910         touch $test_dir/$tfile.1
14911         mv $test_dir/$tfile.1 $ffid &&
14912                 error "rename $tfile.1 to $ffid should fail."
14913         rm -f $test_dir/$tfile.1
14914         echo "truncate fid $fid"
14915         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
14916         echo "link fid $fid"
14917         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
14918         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
14919                 echo "setfacl fid $fid"
14920                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
14921                 echo "getfacl fid $fid"
14922                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
14923         fi
14924         echo "unlink fid $fid"
14925         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
14926         echo "mknod fid $fid"
14927         mknod $ffid c 1 3 && error "mknod $ffid should fail."
14928
14929         fid=[0xf00000400:0x1:0x0]
14930         ffid=$MOUNT/.lustre/fid/$fid
14931
14932         echo "stat non-exist fid $fid"
14933         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
14934         echo "write to non-exist fid $fid"
14935         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
14936         echo "link new fid $fid"
14937         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
14938
14939         mkdir -p $test_dir/$tdir
14940         touch $test_dir/$tdir/$tfile
14941         fid=$($LFS path2fid $test_dir/$tdir)
14942         rc=$?
14943         [ $rc -ne 0 ] &&
14944                 error "error: could not get fid for $test_dir/$dir/$tfile."
14945
14946         ffid=$MOUNT/.lustre/fid/$fid
14947
14948         echo "ls $fid"
14949         ls $ffid > /dev/null || error "ls $ffid failed."
14950         echo "touch $fid/$tfile.1"
14951         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
14952
14953         echo "touch $MOUNT/.lustre/fid/$tfile"
14954         touch $MOUNT/.lustre/fid/$tfile && \
14955                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
14956
14957         echo "setxattr to $MOUNT/.lustre/fid"
14958         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
14959
14960         echo "listxattr for $MOUNT/.lustre/fid"
14961         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
14962
14963         echo "delxattr from $MOUNT/.lustre/fid"
14964         setfattr -x trusted.name1 $MOUNT/.lustre/fid
14965
14966         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
14967         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
14968                 error "touch invalid fid should fail."
14969
14970         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
14971         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
14972                 error "touch non-normal fid should fail."
14973
14974         echo "rename $tdir to $MOUNT/.lustre/fid"
14975         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
14976                 error "rename to $MOUNT/.lustre/fid should fail."
14977
14978         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
14979         then            # LU-3547
14980                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
14981                 local new_obf_mode=777
14982
14983                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
14984                 chmod $new_obf_mode $DIR/.lustre/fid ||
14985                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
14986
14987                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
14988                 [ $obf_mode -eq $new_obf_mode ] ||
14989                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
14990
14991                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
14992                 chmod $old_obf_mode $DIR/.lustre/fid ||
14993                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
14994         fi
14995
14996         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
14997         fid=$($LFS path2fid $test_dir/$tfile-2)
14998
14999         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
15000         then # LU-5424
15001                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
15002                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
15003                         error "create lov data thru .lustre failed"
15004         fi
15005         echo "cp /etc/passwd $test_dir/$tfile-2"
15006         cp /etc/passwd $test_dir/$tfile-2 ||
15007                 error "copy to $test_dir/$tfile-2 failed."
15008         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
15009         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
15010                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
15011
15012         rm -rf $test_dir/tfile.lnk
15013         rm -rf $test_dir/$tfile-2
15014 }
15015
15016 test_154A() {
15017         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15018                 skip "Need MDS version at least 2.4.1"
15019
15020         local tf=$DIR/$tfile
15021         touch $tf
15022
15023         local fid=$($LFS path2fid $tf)
15024         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
15025
15026         # check that we get the same pathname back
15027         local rootpath
15028         local found
15029         for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do
15030                 echo "$rootpath $fid"
15031                 found=$($LFS fid2path $rootpath "$fid")
15032                 [ -z "$found" ] && error "fid2path unable to get '$fid' path"
15033                 [ "$found" == "$tf" ] || error "fid2path $found != $tf"
15034         done
15035
15036         # check wrong root path format
15037         rootpath=$MOUNT"_wrong"
15038         found=$($LFS fid2path $rootpath "$fid")
15039         [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)"
15040 }
15041 run_test 154A "lfs path2fid and fid2path basic checks"
15042
15043 test_154B() {
15044         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15045                 skip "Need MDS version at least 2.4.1"
15046
15047         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
15048         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
15049         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
15050         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
15051
15052         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
15053         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
15054
15055         # check that we get the same pathname
15056         echo "PFID: $PFID, name: $name"
15057         local FOUND=$($LFS fid2path $MOUNT "$PFID")
15058         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
15059         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
15060                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
15061
15062         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
15063 }
15064 run_test 154B "verify the ll_decode_linkea tool"
15065
15066 test_154a() {
15067         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15068         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15069         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
15070                 skip "Need MDS version at least 2.2.51"
15071         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
15072
15073         cp /etc/hosts $DIR/$tfile
15074
15075         fid=$($LFS path2fid $DIR/$tfile)
15076         rc=$?
15077         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
15078
15079         dot_lustre_fid_permission_check "$fid" $DIR ||
15080                 error "dot lustre permission check $fid failed"
15081
15082         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
15083
15084         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
15085
15086         touch $MOUNT/.lustre/file &&
15087                 error "creation is not allowed under .lustre"
15088
15089         mkdir $MOUNT/.lustre/dir &&
15090                 error "mkdir is not allowed under .lustre"
15091
15092         rm -rf $DIR/$tfile
15093 }
15094 run_test 154a "Open-by-FID"
15095
15096 test_154b() {
15097         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15098         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15099         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15100         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
15101                 skip "Need MDS version at least 2.2.51"
15102
15103         local remote_dir=$DIR/$tdir/remote_dir
15104         local MDTIDX=1
15105         local rc=0
15106
15107         mkdir -p $DIR/$tdir
15108         $LFS mkdir -i $MDTIDX $remote_dir ||
15109                 error "create remote directory failed"
15110
15111         cp /etc/hosts $remote_dir/$tfile
15112
15113         fid=$($LFS path2fid $remote_dir/$tfile)
15114         rc=$?
15115         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
15116
15117         dot_lustre_fid_permission_check "$fid" $remote_dir ||
15118                 error "dot lustre permission check $fid failed"
15119         rm -rf $DIR/$tdir
15120 }
15121 run_test 154b "Open-by-FID for remote directory"
15122
15123 test_154c() {
15124         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15125                 skip "Need MDS version at least 2.4.1"
15126
15127         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
15128         local FID1=$($LFS path2fid $DIR/$tfile.1)
15129         local FID2=$($LFS path2fid $DIR/$tfile.2)
15130         local FID3=$($LFS path2fid $DIR/$tfile.3)
15131
15132         local N=1
15133         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
15134                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
15135                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
15136                 local want=FID$N
15137                 [ "$FID" = "${!want}" ] ||
15138                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
15139                 N=$((N + 1))
15140         done
15141
15142         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
15143         do
15144                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
15145                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
15146                 N=$((N + 1))
15147         done
15148 }
15149 run_test 154c "lfs path2fid and fid2path multiple arguments"
15150
15151 test_154d() {
15152         remote_mds_nodsh && skip "remote MDS with nodsh"
15153         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
15154                 skip "Need MDS version at least 2.5.53"
15155
15156         if remote_mds; then
15157                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
15158         else
15159                 nid="0@lo"
15160         fi
15161         local proc_ofile="mdt.*.exports.'$nid'.open_files"
15162         local fd
15163         local cmd
15164
15165         rm -f $DIR/$tfile
15166         touch $DIR/$tfile
15167
15168         local fid=$($LFS path2fid $DIR/$tfile)
15169         # Open the file
15170         fd=$(free_fd)
15171         cmd="exec $fd<$DIR/$tfile"
15172         eval $cmd
15173         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
15174         echo "$fid_list" | grep "$fid"
15175         rc=$?
15176
15177         cmd="exec $fd>/dev/null"
15178         eval $cmd
15179         if [ $rc -ne 0 ]; then
15180                 error "FID $fid not found in open files list $fid_list"
15181         fi
15182 }
15183 run_test 154d "Verify open file fid"
15184
15185 test_154e()
15186 {
15187         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
15188                 skip "Need MDS version at least 2.6.50"
15189
15190         if ls -a $MOUNT | grep -q '^\.lustre$'; then
15191                 error ".lustre returned by readdir"
15192         fi
15193 }
15194 run_test 154e ".lustre is not returned by readdir"
15195
15196 test_154f() {
15197         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15198
15199         # create parent directory on a single MDT to avoid cross-MDT hardlinks
15200         mkdir_on_mdt0 $DIR/$tdir
15201         # test dirs inherit from its stripe
15202         mkdir -p $DIR/$tdir/foo1 || error "mkdir error"
15203         mkdir -p $DIR/$tdir/foo2 || error "mkdir error"
15204         cp /etc/hosts $DIR/$tdir/foo1/$tfile
15205         ln $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/link
15206         touch $DIR/f
15207
15208         # get fid of parents
15209         local FID0=$($LFS path2fid $DIR/$tdir)
15210         local FID1=$($LFS path2fid $DIR/$tdir/foo1)
15211         local FID2=$($LFS path2fid $DIR/$tdir/foo2)
15212         local FID3=$($LFS path2fid $DIR)
15213
15214         # check that path2fid --parents returns expected <parent_fid>/name
15215         # 1) test for a directory (single parent)
15216         local parent=$($LFS path2fid --parents $DIR/$tdir/foo1)
15217         [ "$parent" == "$FID0/foo1" ] ||
15218                 error "expected parent: $FID0/foo1, got: $parent"
15219
15220         # 2) test for a file with nlink > 1 (multiple parents)
15221         parent=$($LFS path2fid --parents $DIR/$tdir/foo1/$tfile)
15222         echo "$parent" | grep -F "$FID1/$tfile" ||
15223                 error "$FID1/$tfile not returned in parent list"
15224         echo "$parent" | grep -F "$FID2/link" ||
15225                 error "$FID2/link not returned in parent list"
15226
15227         # 3) get parent by fid
15228         local file_fid=$($LFS path2fid $DIR/$tdir/foo1/$tfile)
15229         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15230         echo "$parent" | grep -F "$FID1/$tfile" ||
15231                 error "$FID1/$tfile not returned in parent list (by fid)"
15232         echo "$parent" | grep -F "$FID2/link" ||
15233                 error "$FID2/link not returned in parent list (by fid)"
15234
15235         # 4) test for entry in root directory
15236         parent=$($LFS path2fid --parents $DIR/f)
15237         echo "$parent" | grep -F "$FID3/f" ||
15238                 error "$FID3/f not returned in parent list"
15239
15240         # 5) test it on root directory
15241         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
15242                 error "$MOUNT should not have parents"
15243
15244         # enable xattr caching and check that linkea is correctly updated
15245         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
15246         save_lustre_params client "llite.*.xattr_cache" > $save
15247         lctl set_param llite.*.xattr_cache 1
15248
15249         # 6.1) linkea update on rename
15250         mv $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/$tfile.moved
15251
15252         # get parents by fid
15253         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15254         # foo1 should no longer be returned in parent list
15255         echo "$parent" | grep -F "$FID1" &&
15256                 error "$FID1 should no longer be in parent list"
15257         # the new path should appear
15258         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
15259                 error "$FID2/$tfile.moved is not in parent list"
15260
15261         # 6.2) linkea update on unlink
15262         rm -f $DIR/$tdir/foo2/link
15263         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15264         # foo2/link should no longer be returned in parent list
15265         echo "$parent" | grep -F "$FID2/link" &&
15266                 error "$FID2/link should no longer be in parent list"
15267         true
15268
15269         rm -f $DIR/f
15270         restore_lustre_params < $save
15271         rm -f $save
15272 }
15273 run_test 154f "get parent fids by reading link ea"
15274
15275 test_154g()
15276 {
15277         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15278         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
15279            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
15280                 skip "Need MDS version at least 2.6.92"
15281
15282         mkdir_on_mdt0 $DIR/$tdir
15283         llapi_fid_test -d $DIR/$tdir
15284 }
15285 run_test 154g "various llapi FID tests"
15286
15287 test_155_small_load() {
15288     local temp=$TMP/$tfile
15289     local file=$DIR/$tfile
15290
15291     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
15292         error "dd of=$temp bs=6096 count=1 failed"
15293     cp $temp $file
15294     cancel_lru_locks $OSC
15295     cmp $temp $file || error "$temp $file differ"
15296
15297     $TRUNCATE $temp 6000
15298     $TRUNCATE $file 6000
15299     cmp $temp $file || error "$temp $file differ (truncate1)"
15300
15301     echo "12345" >>$temp
15302     echo "12345" >>$file
15303     cmp $temp $file || error "$temp $file differ (append1)"
15304
15305     echo "12345" >>$temp
15306     echo "12345" >>$file
15307     cmp $temp $file || error "$temp $file differ (append2)"
15308
15309     rm -f $temp $file
15310     true
15311 }
15312
15313 test_155_big_load() {
15314         remote_ost_nodsh && skip "remote OST with nodsh"
15315
15316         local temp=$TMP/$tfile
15317         local file=$DIR/$tfile
15318
15319         free_min_max
15320         local cache_size=$(do_facet ost$((MAXI+1)) \
15321                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
15322         local large_file_size=$((cache_size * 2))
15323
15324         echo "OSS cache size: $cache_size KB"
15325         echo "Large file size: $large_file_size KB"
15326
15327         [ $MAXV -le $large_file_size ] &&
15328                 skip_env "max available OST size needs > $large_file_size KB"
15329
15330         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
15331
15332         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
15333                 error "dd of=$temp bs=$large_file_size count=1k failed"
15334         cp $temp $file
15335         ls -lh $temp $file
15336         cancel_lru_locks osc
15337         cmp $temp $file || error "$temp $file differ"
15338
15339         rm -f $temp $file
15340         true
15341 }
15342
15343 save_writethrough() {
15344         local facets=$(get_facets OST)
15345
15346         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
15347 }
15348
15349 test_155a() {
15350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15351
15352         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15353
15354         save_writethrough $p
15355
15356         set_cache read on
15357         set_cache writethrough on
15358         test_155_small_load
15359         restore_lustre_params < $p
15360         rm -f $p
15361 }
15362 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
15363
15364 test_155b() {
15365         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15366
15367         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15368
15369         save_writethrough $p
15370
15371         set_cache read on
15372         set_cache writethrough off
15373         test_155_small_load
15374         restore_lustre_params < $p
15375         rm -f $p
15376 }
15377 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
15378
15379 test_155c() {
15380         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15381
15382         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15383
15384         save_writethrough $p
15385
15386         set_cache read off
15387         set_cache writethrough on
15388         test_155_small_load
15389         restore_lustre_params < $p
15390         rm -f $p
15391 }
15392 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
15393
15394 test_155d() {
15395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15396
15397         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15398
15399         save_writethrough $p
15400
15401         set_cache read off
15402         set_cache writethrough off
15403         test_155_small_load
15404         restore_lustre_params < $p
15405         rm -f $p
15406 }
15407 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
15408
15409 test_155e() {
15410         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15411
15412         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15413
15414         save_writethrough $p
15415
15416         set_cache read on
15417         set_cache writethrough on
15418         test_155_big_load
15419         restore_lustre_params < $p
15420         rm -f $p
15421 }
15422 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
15423
15424 test_155f() {
15425         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15426
15427         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15428
15429         save_writethrough $p
15430
15431         set_cache read on
15432         set_cache writethrough off
15433         test_155_big_load
15434         restore_lustre_params < $p
15435         rm -f $p
15436 }
15437 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
15438
15439 test_155g() {
15440         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15441
15442         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15443
15444         save_writethrough $p
15445
15446         set_cache read off
15447         set_cache writethrough on
15448         test_155_big_load
15449         restore_lustre_params < $p
15450         rm -f $p
15451 }
15452 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
15453
15454 test_155h() {
15455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15456
15457         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15458
15459         save_writethrough $p
15460
15461         set_cache read off
15462         set_cache writethrough off
15463         test_155_big_load
15464         restore_lustre_params < $p
15465         rm -f $p
15466 }
15467 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
15468
15469 test_156() {
15470         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15471         remote_ost_nodsh && skip "remote OST with nodsh"
15472         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
15473                 skip "stats not implemented on old servers"
15474         [ "$ost1_FSTYPE" = "zfs" ] &&
15475                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
15476
15477         local CPAGES=3
15478         local BEFORE
15479         local AFTER
15480         local file="$DIR/$tfile"
15481         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15482
15483         save_writethrough $p
15484         roc_hit_init
15485
15486         log "Turn on read and write cache"
15487         set_cache read on
15488         set_cache writethrough on
15489
15490         log "Write data and read it back."
15491         log "Read should be satisfied from the cache."
15492         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15493         BEFORE=$(roc_hit)
15494         cancel_lru_locks osc
15495         cat $file >/dev/null
15496         AFTER=$(roc_hit)
15497         if ! let "AFTER - BEFORE == CPAGES"; then
15498                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
15499         else
15500                 log "cache hits: before: $BEFORE, after: $AFTER"
15501         fi
15502
15503         log "Read again; it should be satisfied from the cache."
15504         BEFORE=$AFTER
15505         cancel_lru_locks osc
15506         cat $file >/dev/null
15507         AFTER=$(roc_hit)
15508         if ! let "AFTER - BEFORE == CPAGES"; then
15509                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
15510         else
15511                 log "cache hits:: before: $BEFORE, after: $AFTER"
15512         fi
15513
15514         log "Turn off the read cache and turn on the write cache"
15515         set_cache read off
15516         set_cache writethrough on
15517
15518         log "Read again; it should be satisfied from the cache."
15519         BEFORE=$(roc_hit)
15520         cancel_lru_locks osc
15521         cat $file >/dev/null
15522         AFTER=$(roc_hit)
15523         if ! let "AFTER - BEFORE == CPAGES"; then
15524                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
15525         else
15526                 log "cache hits:: before: $BEFORE, after: $AFTER"
15527         fi
15528
15529         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15530                 # > 2.12.56 uses pagecache if cached
15531                 log "Read again; it should not be satisfied from the cache."
15532                 BEFORE=$AFTER
15533                 cancel_lru_locks osc
15534                 cat $file >/dev/null
15535                 AFTER=$(roc_hit)
15536                 if ! let "AFTER - BEFORE == 0"; then
15537                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
15538                 else
15539                         log "cache hits:: before: $BEFORE, after: $AFTER"
15540                 fi
15541         fi
15542
15543         log "Write data and read it back."
15544         log "Read should be satisfied from the cache."
15545         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15546         BEFORE=$(roc_hit)
15547         cancel_lru_locks osc
15548         cat $file >/dev/null
15549         AFTER=$(roc_hit)
15550         if ! let "AFTER - BEFORE == CPAGES"; then
15551                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
15552         else
15553                 log "cache hits:: before: $BEFORE, after: $AFTER"
15554         fi
15555
15556         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15557                 # > 2.12.56 uses pagecache if cached
15558                 log "Read again; it should not be satisfied from the cache."
15559                 BEFORE=$AFTER
15560                 cancel_lru_locks osc
15561                 cat $file >/dev/null
15562                 AFTER=$(roc_hit)
15563                 if ! let "AFTER - BEFORE == 0"; then
15564                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
15565                 else
15566                         log "cache hits:: before: $BEFORE, after: $AFTER"
15567                 fi
15568         fi
15569
15570         log "Turn off read and write cache"
15571         set_cache read off
15572         set_cache writethrough off
15573
15574         log "Write data and read it back"
15575         log "It should not be satisfied from the cache."
15576         rm -f $file
15577         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15578         cancel_lru_locks osc
15579         BEFORE=$(roc_hit)
15580         cat $file >/dev/null
15581         AFTER=$(roc_hit)
15582         if ! let "AFTER - BEFORE == 0"; then
15583                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
15584         else
15585                 log "cache hits:: before: $BEFORE, after: $AFTER"
15586         fi
15587
15588         log "Turn on the read cache and turn off the write cache"
15589         set_cache read on
15590         set_cache writethrough off
15591
15592         log "Write data and read it back"
15593         log "It should not be satisfied from the cache."
15594         rm -f $file
15595         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15596         BEFORE=$(roc_hit)
15597         cancel_lru_locks osc
15598         cat $file >/dev/null
15599         AFTER=$(roc_hit)
15600         if ! let "AFTER - BEFORE == 0"; then
15601                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
15602         else
15603                 log "cache hits:: before: $BEFORE, after: $AFTER"
15604         fi
15605
15606         log "Read again; it should be satisfied from the cache."
15607         BEFORE=$(roc_hit)
15608         cancel_lru_locks osc
15609         cat $file >/dev/null
15610         AFTER=$(roc_hit)
15611         if ! let "AFTER - BEFORE == CPAGES"; then
15612                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
15613         else
15614                 log "cache hits:: before: $BEFORE, after: $AFTER"
15615         fi
15616
15617         restore_lustre_params < $p
15618         rm -f $p $file
15619 }
15620 run_test 156 "Verification of tunables"
15621
15622 test_160a() {
15623         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15624         remote_mds_nodsh && skip "remote MDS with nodsh"
15625         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
15626                 skip "Need MDS version at least 2.2.0"
15627
15628         changelog_register || error "changelog_register failed"
15629         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
15630         changelog_users $SINGLEMDS | grep -q $cl_user ||
15631                 error "User $cl_user not found in changelog_users"
15632
15633         mkdir_on_mdt0 $DIR/$tdir
15634
15635         # change something
15636         test_mkdir -p $DIR/$tdir/pics/2008/zachy
15637         changelog_clear 0 || error "changelog_clear failed"
15638         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
15639         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
15640         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
15641         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
15642         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
15643         rm $DIR/$tdir/pics/desktop.jpg
15644
15645         echo "verifying changelog mask"
15646         changelog_chmask "-MKDIR"
15647         changelog_chmask "-CLOSE"
15648
15649         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
15650         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
15651
15652         changelog_chmask "+MKDIR"
15653         changelog_chmask "+CLOSE"
15654
15655         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
15656         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
15657
15658         MKDIRS=$(changelog_dump | grep -c "MKDIR")
15659         CLOSES=$(changelog_dump | grep -c "CLOSE")
15660         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
15661         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
15662
15663         # verify contents
15664         echo "verifying target fid"
15665         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
15666         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
15667         [ "$fidc" == "$fidf" ] ||
15668                 error "changelog '$tfile' fid $fidc != file fid $fidf"
15669         echo "verifying parent fid"
15670         # The FID returned from the Changelog may be the directory shard on
15671         # a different MDT, and not the FID returned by path2fid on the parent.
15672         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
15673         # since this is what will matter when recreating this file in the tree.
15674         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
15675         local pathp=$($LFS fid2path $MOUNT "$fidp")
15676         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
15677                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
15678
15679         echo "getting records for $cl_user"
15680         changelog_users $SINGLEMDS
15681         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
15682         local nclr=3
15683         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
15684                 error "changelog_clear failed"
15685         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
15686         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
15687         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
15688                 error "user index expect $user_rec1 + $nclr != $user_rec2"
15689
15690         local min0_rec=$(changelog_users $SINGLEMDS |
15691                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
15692         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
15693                           awk '{ print $1; exit; }')
15694
15695         changelog_dump | tail -n 5
15696         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
15697         [ $first_rec == $((min0_rec + 1)) ] ||
15698                 error "first index should be $min0_rec + 1 not $first_rec"
15699
15700         # LU-3446 changelog index reset on MDT restart
15701         local cur_rec1=$(changelog_users $SINGLEMDS |
15702                          awk '/^current.index:/ { print $NF }')
15703         changelog_clear 0 ||
15704                 error "clear all changelog records for $cl_user failed"
15705         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
15706         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
15707                 error "Fail to start $SINGLEMDS"
15708         local cur_rec2=$(changelog_users $SINGLEMDS |
15709                          awk '/^current.index:/ { print $NF }')
15710         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
15711         [ $cur_rec1 == $cur_rec2 ] ||
15712                 error "current index should be $cur_rec1 not $cur_rec2"
15713
15714         echo "verifying users from this test are deregistered"
15715         changelog_deregister || error "changelog_deregister failed"
15716         changelog_users $SINGLEMDS | grep -q $cl_user &&
15717                 error "User '$cl_user' still in changelog_users"
15718
15719         # lctl get_param -n mdd.*.changelog_users
15720         # current_index: 144
15721         # ID    index (idle seconds)
15722         # cl3   144   (2) mask=<list>
15723         if [ -z "$(changelog_users $SINGLEMDS | grep -v current.index)" ]; then
15724                 # this is the normal case where all users were deregistered
15725                 # make sure no new records are added when no users are present
15726                 local last_rec1=$(changelog_users $SINGLEMDS |
15727                                   awk '/^current.index:/ { print $NF }')
15728                 touch $DIR/$tdir/chloe
15729                 local last_rec2=$(changelog_users $SINGLEMDS |
15730                                   awk '/^current.index:/ { print $NF }')
15731                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
15732                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
15733         else
15734                 # any changelog users must be leftovers from a previous test
15735                 changelog_users $SINGLEMDS
15736                 echo "other changelog users; can't verify off"
15737         fi
15738 }
15739 run_test 160a "changelog sanity"
15740
15741 test_160b() { # LU-3587
15742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15743         remote_mds_nodsh && skip "remote MDS with nodsh"
15744         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
15745                 skip "Need MDS version at least 2.2.0"
15746
15747         changelog_register || error "changelog_register failed"
15748         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
15749         changelog_users $SINGLEMDS | grep -q $cl_user ||
15750                 error "User '$cl_user' not found in changelog_users"
15751
15752         local longname1=$(str_repeat a 255)
15753         local longname2=$(str_repeat b 255)
15754
15755         cd $DIR
15756         echo "creating very long named file"
15757         touch $longname1 || error "create of '$longname1' failed"
15758         echo "renaming very long named file"
15759         mv $longname1 $longname2
15760
15761         changelog_dump | grep RENME | tail -n 5
15762         rm -f $longname2
15763 }
15764 run_test 160b "Verify that very long rename doesn't crash in changelog"
15765
15766 test_160c() {
15767         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15768         remote_mds_nodsh && skip "remote MDS with nodsh"
15769
15770         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
15771                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
15772                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
15773                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
15774
15775         local rc=0
15776
15777         # Registration step
15778         changelog_register || error "changelog_register failed"
15779
15780         rm -rf $DIR/$tdir
15781         mkdir -p $DIR/$tdir
15782         $MCREATE $DIR/$tdir/foo_160c
15783         changelog_chmask "-TRUNC"
15784         $TRUNCATE $DIR/$tdir/foo_160c 200
15785         changelog_chmask "+TRUNC"
15786         $TRUNCATE $DIR/$tdir/foo_160c 199
15787         changelog_dump | tail -n 5
15788         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
15789         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
15790 }
15791 run_test 160c "verify that changelog log catch the truncate event"
15792
15793 test_160d() {
15794         remote_mds_nodsh && skip "remote MDS with nodsh"
15795         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15797         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
15798                 skip "Need MDS version at least 2.7.60"
15799
15800         # Registration step
15801         changelog_register || error "changelog_register failed"
15802
15803         mkdir -p $DIR/$tdir/migrate_dir
15804         changelog_clear 0 || error "changelog_clear failed"
15805
15806         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
15807         changelog_dump | tail -n 5
15808         local migrates=$(changelog_dump | grep -c "MIGRT")
15809         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
15810 }
15811 run_test 160d "verify that changelog log catch the migrate event"
15812
15813 test_160e() {
15814         remote_mds_nodsh && skip "remote MDS with nodsh"
15815
15816         # Create a user
15817         changelog_register || error "changelog_register failed"
15818
15819         local MDT0=$(facet_svc $SINGLEMDS)
15820         local rc
15821
15822         # No user (expect fail)
15823         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister
15824         rc=$?
15825         if [ $rc -eq 0 ]; then
15826                 error "Should fail without user"
15827         elif [ $rc -ne 4 ]; then
15828                 error "changelog_deregister failed with $rc, expect 4(CMD_HELP)"
15829         fi
15830
15831         # Delete a future user (expect fail)
15832         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
15833         rc=$?
15834         if [ $rc -eq 0 ]; then
15835                 error "Deleted non-existant user cl77"
15836         elif [ $rc -ne 2 ]; then
15837                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
15838         fi
15839
15840         # Clear to a bad index (1 billion should be safe)
15841         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
15842         rc=$?
15843
15844         if [ $rc -eq 0 ]; then
15845                 error "Successfully cleared to invalid CL index"
15846         elif [ $rc -ne 22 ]; then
15847                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
15848         fi
15849 }
15850 run_test 160e "changelog negative testing (should return errors)"
15851
15852 test_160f() {
15853         remote_mds_nodsh && skip "remote MDS with nodsh" && return
15854         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15855                 skip "Need MDS version at least 2.10.56"
15856
15857         local mdts=$(comma_list $(mdts_nodes))
15858
15859         # Create a user
15860         changelog_register || error "first changelog_register failed"
15861         changelog_register || error "second changelog_register failed"
15862         local cl_users
15863         declare -A cl_user1
15864         declare -A cl_user2
15865         local user_rec1
15866         local user_rec2
15867         local i
15868
15869         # generate some changelog records to accumulate on each MDT
15870         # use all_char because created files should be evenly distributed
15871         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
15872                 error "test_mkdir $tdir failed"
15873         log "$(date +%s): creating first files"
15874         for ((i = 0; i < MDSCOUNT * 2; i++)); do
15875                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT)) ||
15876                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT)) failed"
15877         done
15878
15879         # check changelogs have been generated
15880         local start=$SECONDS
15881         local idle_time=$((MDSCOUNT * 5 + 5))
15882         local nbcl=$(changelog_dump | wc -l)
15883         [[ $nbcl -eq 0 ]] && error "no changelogs found"
15884
15885         for param in "changelog_max_idle_time=$idle_time" \
15886                      "changelog_gc=1" \
15887                      "changelog_min_gc_interval=2" \
15888                      "changelog_min_free_cat_entries=3"; do
15889                 local MDT0=$(facet_svc $SINGLEMDS)
15890                 local var="${param%=*}"
15891                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
15892
15893                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
15894                 do_nodes $mdts $LCTL set_param mdd.*.$param
15895         done
15896
15897         # force cl_user2 to be idle (1st part), but also cancel the
15898         # cl_user1 records so that it is not evicted later in the test.
15899         local sleep1=$((idle_time / 2))
15900         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
15901         sleep $sleep1
15902
15903         # simulate changelog catalog almost full
15904         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
15905         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
15906
15907         for i in $(seq $MDSCOUNT); do
15908                 cl_users=(${CL_USERS[mds$i]})
15909                 cl_user1[mds$i]="${cl_users[0]}"
15910                 cl_user2[mds$i]="${cl_users[1]}"
15911
15912                 [ -n "${cl_user1[mds$i]}" ] ||
15913                         error "mds$i: no user registered"
15914                 [ -n "${cl_user2[mds$i]}" ] ||
15915                         error "mds$i: only ${cl_user2[mds$i]} is registered"
15916
15917                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15918                 [ -n "$user_rec1" ] ||
15919                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15920                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
15921                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15922                 [ -n "$user_rec2" ] ||
15923                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15924                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
15925                      "$user_rec1 + 2 == $user_rec2"
15926                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
15927                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
15928                               "$user_rec1 + 2, but is $user_rec2"
15929                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
15930                 [ -n "$user_rec2" ] ||
15931                         error "mds$i: User ${cl_user2[mds$i]} not registered"
15932                 [ $user_rec1 == $user_rec2 ] ||
15933                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
15934                               "$user_rec1, but is $user_rec2"
15935         done
15936
15937         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
15938         local sleep2=$((idle_time - (SECONDS - start) + 1))
15939         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
15940         sleep $sleep2
15941
15942         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
15943         # cl_user1 should be OK because it recently processed records.
15944         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
15945         for ((i = 0; i < MDSCOUNT * 2; i++)); do
15946                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT+2))||
15947                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT+2)) failed"
15948         done
15949
15950         # ensure gc thread is done
15951         for i in $(mdts_nodes); do
15952                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
15953                         error "$i: GC-thread not done"
15954         done
15955
15956         local first_rec
15957         for (( i = 1; i <= MDSCOUNT; i++ )); do
15958                 # check cl_user1 still registered
15959                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
15960                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15961                 # check cl_user2 unregistered
15962                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
15963                         error "mds$i: User ${cl_user2[mds$i]} still registered"
15964
15965                 # check changelogs are present and starting at $user_rec1 + 1
15966                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
15967                 [ -n "$user_rec1" ] ||
15968                         error "mds$i: User ${cl_user1[mds$i]} not registered"
15969                 first_rec=$($LFS changelog $(facet_svc mds$i) |
15970                             awk '{ print $1; exit; }')
15971
15972                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
15973                 [ $((user_rec1 + 1)) == $first_rec ] ||
15974                         error "mds$i: rec $first_rec != $user_rec1 + 1"
15975         done
15976 }
15977 run_test 160f "changelog garbage collect (timestamped users)"
15978
15979 test_160g() {
15980         remote_mds_nodsh && skip "remote MDS with nodsh"
15981         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
15982                 skip "Need MDS version at least 2.10.56"
15983
15984         local mdts=$(comma_list $(mdts_nodes))
15985
15986         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
15987         do_nodes $mdts $LCTL set_param fail_loc=0x1314
15988
15989         # Create a user
15990         changelog_register || error "first changelog_register failed"
15991         changelog_register || error "second changelog_register failed"
15992         local cl_users
15993         declare -A cl_user1
15994         declare -A cl_user2
15995         local user_rec1
15996         local user_rec2
15997         local i
15998
15999         # generate some changelog records to accumulate on each MDT
16000         # use all_char because created files should be evenly distributed
16001         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16002                 error "test_mkdir $tdir failed"
16003         for ((i = 0; i < MDSCOUNT; i++)); do
16004                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16005                         error "create $DIR/$tdir/d$i.1 failed"
16006         done
16007
16008         # check changelogs have been generated
16009         local nbcl=$(changelog_dump | wc -l)
16010         (( $nbcl > 0 )) || error "no changelogs found"
16011
16012         # reduce the max_idle_indexes value to make sure we exceed it
16013         for param in "changelog_max_idle_indexes=1" \
16014                      "changelog_gc=1" \
16015                      "changelog_min_gc_interval=2" \
16016                      "changelog_min_free_cat_entries=3"; do
16017                 local MDT0=$(facet_svc $SINGLEMDS)
16018                 local var="${param%=*}"
16019                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16020
16021                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16022                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
16023                         error "unable to set mdd.*.$param"
16024         done
16025
16026         # simulate changelog catalog almost full
16027         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
16028         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
16029
16030         local start=$SECONDS
16031         for i in $(seq $MDSCOUNT); do
16032                 cl_users=(${CL_USERS[mds$i]})
16033                 cl_user1[mds$i]="${cl_users[0]}"
16034                 cl_user2[mds$i]="${cl_users[1]}"
16035
16036                 [ -n "${cl_user1[mds$i]}" ] ||
16037                         error "mds$i: no user registered"
16038                 [ -n "${cl_user2[mds$i]}" ] ||
16039                         error "mds$i: only ${cl_user1[mds$i]} is registered"
16040
16041                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16042                 [ -n "$user_rec1" ] ||
16043                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16044                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16045                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16046                 [ -n "$user_rec2" ] ||
16047                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16048                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16049                      "$user_rec1 + 2 == $user_rec2"
16050                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16051                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16052                               "$user_rec1 + 2, but is $user_rec2"
16053                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16054                 [ -n "$user_rec2" ] ||
16055                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16056                 [ $user_rec1 == $user_rec2 ] ||
16057                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16058                               "$user_rec1, but is $user_rec2"
16059         done
16060
16061         # ensure we are past the previous changelog_min_gc_interval set above
16062         local sleep2=$((start + 2 - SECONDS))
16063         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
16064
16065         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
16066         # cl_user1 should be OK because it recently processed records.
16067         for ((i = 0; i < MDSCOUNT; i++)); do
16068                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 $DIR/$tdir/d$i.4 ||
16069                         error "create $DIR/$tdir/d$i.3 failed"
16070         done
16071
16072         # ensure gc thread is done
16073         for i in $(mdts_nodes); do
16074                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
16075                         error "$i: GC-thread not done"
16076         done
16077
16078         local first_rec
16079         for (( i = 1; i <= MDSCOUNT; i++ )); do
16080                 # check cl_user1 still registered
16081                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16082                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16083                 # check cl_user2 unregistered
16084                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16085                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16086
16087                 # check changelogs are present and starting at $user_rec1 + 1
16088                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16089                 [ -n "$user_rec1" ] ||
16090                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16091                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16092                             awk '{ print $1; exit; }')
16093
16094                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
16095                 [ $((user_rec1 + 1)) == $first_rec ] ||
16096                         error "mds$i: rec $first_rec != $user_rec1 + 1"
16097         done
16098 }
16099 run_test 160g "changelog garbage collect (old users)"
16100
16101 test_160h() {
16102         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16103         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16104                 skip "Need MDS version at least 2.10.56"
16105
16106         local mdts=$(comma_list $(mdts_nodes))
16107
16108         # Create a user
16109         changelog_register || error "first changelog_register failed"
16110         changelog_register || error "second changelog_register failed"
16111         local cl_users
16112         declare -A cl_user1
16113         declare -A cl_user2
16114         local user_rec1
16115         local user_rec2
16116         local i
16117
16118         # generate some changelog records to accumulate on each MDT
16119         # use all_char because created files should be evenly distributed
16120         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16121                 error "test_mkdir $tdir failed"
16122         for ((i = 0; i < MDSCOUNT; i++)); do
16123                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16124                         error "create $DIR/$tdir/d$i.1 failed"
16125         done
16126
16127         # check changelogs have been generated
16128         local nbcl=$(changelog_dump | wc -l)
16129         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16130
16131         for param in "changelog_max_idle_time=10" \
16132                      "changelog_gc=1" \
16133                      "changelog_min_gc_interval=2"; do
16134                 local MDT0=$(facet_svc $SINGLEMDS)
16135                 local var="${param%=*}"
16136                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16137
16138                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16139                 do_nodes $mdts $LCTL set_param mdd.*.$param
16140         done
16141
16142         # force cl_user2 to be idle (1st part)
16143         sleep 9
16144
16145         for i in $(seq $MDSCOUNT); do
16146                 cl_users=(${CL_USERS[mds$i]})
16147                 cl_user1[mds$i]="${cl_users[0]}"
16148                 cl_user2[mds$i]="${cl_users[1]}"
16149
16150                 [ -n "${cl_user1[mds$i]}" ] ||
16151                         error "mds$i: no user registered"
16152                 [ -n "${cl_user2[mds$i]}" ] ||
16153                         error "mds$i: only ${cl_user2[mds$i]} is registered"
16154
16155                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16156                 [ -n "$user_rec1" ] ||
16157                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16158                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16159                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16160                 [ -n "$user_rec2" ] ||
16161                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16162                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16163                      "$user_rec1 + 2 == $user_rec2"
16164                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16165                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16166                               "$user_rec1 + 2, but is $user_rec2"
16167                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16168                 [ -n "$user_rec2" ] ||
16169                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16170                 [ $user_rec1 == $user_rec2 ] ||
16171                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16172                               "$user_rec1, but is $user_rec2"
16173         done
16174
16175         # force cl_user2 to be idle (2nd part) and to reach
16176         # changelog_max_idle_time
16177         sleep 2
16178
16179         # force each GC-thread start and block then
16180         # one per MDT/MDD, set fail_val accordingly
16181         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
16182         do_nodes $mdts $LCTL set_param fail_loc=0x1316
16183
16184         # generate more changelogs to trigger fail_loc
16185         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16186                 error "create $DIR/$tdir/${tfile}bis failed"
16187
16188         # stop MDT to stop GC-thread, should be done in back-ground as it will
16189         # block waiting for the thread to be released and exit
16190         declare -A stop_pids
16191         for i in $(seq $MDSCOUNT); do
16192                 stop mds$i &
16193                 stop_pids[mds$i]=$!
16194         done
16195
16196         for i in $(mdts_nodes); do
16197                 local facet
16198                 local nb=0
16199                 local facets=$(facets_up_on_host $i)
16200
16201                 for facet in ${facets//,/ }; do
16202                         if [[ $facet == mds* ]]; then
16203                                 nb=$((nb + 1))
16204                         fi
16205                 done
16206                 # ensure each MDS's gc threads are still present and all in "R"
16207                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
16208                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
16209                         error "$i: expected $nb GC-thread"
16210                 wait_update $i \
16211                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
16212                         "R" 20 ||
16213                         error "$i: GC-thread not found in R-state"
16214                 # check umounts of each MDT on MDS have reached kthread_stop()
16215                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
16216                         error "$i: expected $nb umount"
16217                 wait_update $i \
16218                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
16219                         error "$i: umount not found in D-state"
16220         done
16221
16222         # release all GC-threads
16223         do_nodes $mdts $LCTL set_param fail_loc=0
16224
16225         # wait for MDT stop to complete
16226         for i in $(seq $MDSCOUNT); do
16227                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
16228         done
16229
16230         # XXX
16231         # may try to check if any orphan changelog records are present
16232         # via ldiskfs/zfs and llog_reader...
16233
16234         # re-start/mount MDTs
16235         for i in $(seq $MDSCOUNT); do
16236                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
16237                         error "Fail to start mds$i"
16238         done
16239
16240         local first_rec
16241         for i in $(seq $MDSCOUNT); do
16242                 # check cl_user1 still registered
16243                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16244                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16245                 # check cl_user2 unregistered
16246                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16247                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16248
16249                 # check changelogs are present and starting at $user_rec1 + 1
16250                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16251                 [ -n "$user_rec1" ] ||
16252                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16253                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16254                             awk '{ print $1; exit; }')
16255
16256                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
16257                 [ $((user_rec1 + 1)) == $first_rec ] ||
16258                         error "mds$i: first index should be $user_rec1 + 1, " \
16259                               "but is $first_rec"
16260         done
16261 }
16262 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
16263               "during mount"
16264
16265 test_160i() {
16266
16267         local mdts=$(comma_list $(mdts_nodes))
16268
16269         changelog_register || error "first changelog_register failed"
16270
16271         # generate some changelog records to accumulate on each MDT
16272         # use all_char because created files should be evenly distributed
16273         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16274                 error "test_mkdir $tdir failed"
16275         for ((i = 0; i < MDSCOUNT; i++)); do
16276                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16277                         error "create $DIR/$tdir/d$i.1 failed"
16278         done
16279
16280         # check changelogs have been generated
16281         local nbcl=$(changelog_dump | wc -l)
16282         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16283
16284         # simulate race between register and unregister
16285         # XXX as fail_loc is set per-MDS, with DNE configs the race
16286         # simulation will only occur for one MDT per MDS and for the
16287         # others the normal race scenario will take place
16288         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
16289         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
16290         do_nodes $mdts $LCTL set_param fail_val=1
16291
16292         # unregister 1st user
16293         changelog_deregister &
16294         local pid1=$!
16295         # wait some time for deregister work to reach race rdv
16296         sleep 2
16297         # register 2nd user
16298         changelog_register || error "2nd user register failed"
16299
16300         wait $pid1 || error "1st user deregister failed"
16301
16302         local i
16303         local last_rec
16304         declare -A LAST_REC
16305         for i in $(seq $MDSCOUNT); do
16306                 if changelog_users mds$i | grep "^cl"; then
16307                         # make sure new records are added with one user present
16308                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
16309                                           awk '/^current.index:/ { print $NF }')
16310                 else
16311                         error "mds$i has no user registered"
16312                 fi
16313         done
16314
16315         # generate more changelog records to accumulate on each MDT
16316         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16317                 error "create $DIR/$tdir/${tfile}bis failed"
16318
16319         for i in $(seq $MDSCOUNT); do
16320                 last_rec=$(changelog_users $SINGLEMDS |
16321                            awk '/^current.index:/ { print $NF }')
16322                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
16323                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
16324                         error "changelogs are off on mds$i"
16325         done
16326 }
16327 run_test 160i "changelog user register/unregister race"
16328
16329 test_160j() {
16330         remote_mds_nodsh && skip "remote MDS with nodsh"
16331         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
16332                 skip "Need MDS version at least 2.12.56"
16333
16334         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
16335         stack_trap "umount $MOUNT2" EXIT
16336
16337         changelog_register || error "first changelog_register failed"
16338         stack_trap "changelog_deregister" EXIT
16339
16340         # generate some changelog
16341         # use all_char because created files should be evenly distributed
16342         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16343                 error "mkdir $tdir failed"
16344         for ((i = 0; i < MDSCOUNT; i++)); do
16345                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16346                         error "create $DIR/$tdir/d$i.1 failed"
16347         done
16348
16349         # open the changelog device
16350         exec 3>/dev/changelog-$FSNAME-MDT0000
16351         stack_trap "exec 3>&-" EXIT
16352         exec 4</dev/changelog-$FSNAME-MDT0000
16353         stack_trap "exec 4<&-" EXIT
16354
16355         # umount the first lustre mount
16356         umount $MOUNT
16357         stack_trap "mount_client $MOUNT" EXIT
16358
16359         # read changelog, which may or may not fail, but should not crash
16360         cat <&4 >/dev/null
16361
16362         # clear changelog
16363         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16364         changelog_users $SINGLEMDS | grep -q $cl_user ||
16365                 error "User $cl_user not found in changelog_users"
16366
16367         printf 'clear:'$cl_user':0' >&3
16368 }
16369 run_test 160j "client can be umounted while its chanangelog is being used"
16370
16371 test_160k() {
16372         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16373         remote_mds_nodsh && skip "remote MDS with nodsh"
16374
16375         mkdir -p $DIR/$tdir/1/1
16376
16377         changelog_register || error "changelog_register failed"
16378         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16379
16380         changelog_users $SINGLEMDS | grep -q $cl_user ||
16381                 error "User '$cl_user' not found in changelog_users"
16382 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
16383         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
16384         rmdir $DIR/$tdir/1/1 & sleep 1
16385         mkdir $DIR/$tdir/2
16386         touch $DIR/$tdir/2/2
16387         rm -rf $DIR/$tdir/2
16388
16389         wait
16390         sleep 4
16391
16392         changelog_dump | grep rmdir || error "rmdir not recorded"
16393 }
16394 run_test 160k "Verify that changelog records are not lost"
16395
16396 # Verifies that a file passed as a parameter has recently had an operation
16397 # performed on it that has generated an MTIME changelog which contains the
16398 # correct parent FID. As files might reside on a different MDT from the
16399 # parent directory in DNE configurations, the FIDs are translated to paths
16400 # before being compared, which should be identical
16401 compare_mtime_changelog() {
16402         local file="${1}"
16403         local mdtidx
16404         local mtime
16405         local cl_fid
16406         local pdir
16407         local dir
16408
16409         mdtidx=$($LFS getstripe --mdt-index $file)
16410         mdtidx=$(printf "%04x" $mdtidx)
16411
16412         # Obtain the parent FID from the MTIME changelog
16413         mtime=$($LFS changelog $FSNAME-MDT$mdtidx | tail -n 1 | grep MTIME)
16414         [ -z "$mtime" ] && error "MTIME changelog not recorded"
16415
16416         cl_fid=$(sed -e 's/.* p=//' -e 's/ .*//' <<<$mtime)
16417         [ -z "$cl_fid" ] && error "parent FID not present"
16418
16419         # Verify that the path for the parent FID is the same as the path for
16420         # the test directory
16421         pdir=$($LFS fid2path $MOUNT "$cl_fid")
16422
16423         dir=$(dirname $1)
16424
16425         [[ "${pdir%/}" == "$dir" ]] ||
16426                 error "MTIME changelog parent FID is wrong, expected $dir, got $pdir"
16427 }
16428
16429 test_160l() {
16430         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16431
16432         remote_mds_nodsh && skip "remote MDS with nodsh"
16433         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
16434                 skip "Need MDS version at least 2.13.55"
16435
16436         local cl_user
16437
16438         changelog_register || error "changelog_register failed"
16439         cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16440
16441         changelog_users $SINGLEMDS | grep -q $cl_user ||
16442                 error "User '$cl_user' not found in changelog_users"
16443
16444         # Clear some types so that MTIME changelogs are generated
16445         changelog_chmask "-CREAT"
16446         changelog_chmask "-CLOSE"
16447
16448         test_mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
16449
16450         # Test CL_MTIME during setattr
16451         touch $DIR/$tdir/$tfile
16452         compare_mtime_changelog $DIR/$tdir/$tfile
16453
16454         # Test CL_MTIME during close
16455         $MULTIOP $DIR/$tdir/${tfile}_2 O_2w4096c || error "multiop failed"
16456         compare_mtime_changelog $DIR/$tdir/${tfile}_2
16457 }
16458 run_test 160l "Verify that MTIME changelog records contain the parent FID"
16459
16460 test_160m() {
16461         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16462         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16463                 skip "Need MDS version at least 2.14.51"
16464         local cl_users
16465         local cl_user1
16466         local cl_user2
16467         local pid1
16468
16469         # Create a user
16470         changelog_register || error "first changelog_register failed"
16471         changelog_register || error "second changelog_register failed"
16472
16473         cl_users=(${CL_USERS[mds1]})
16474         cl_user1="${cl_users[0]}"
16475         cl_user2="${cl_users[1]}"
16476         # generate some changelog records to accumulate on MDT0
16477         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16478         createmany -m $DIR/$tdir/$tfile 50 ||
16479                 error "create $DIR/$tdir/$tfile failed"
16480         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16481         rm -f $DIR/$tdir
16482
16483         # check changelogs have been generated
16484         local nbcl=$(changelog_dump | wc -l)
16485         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16486
16487 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
16488         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
16489
16490         __changelog_clear mds1 $cl_user1 +10
16491         __changelog_clear mds1 $cl_user2 0 &
16492         pid1=$!
16493         sleep 2
16494         __changelog_clear mds1 $cl_user1 0 ||
16495                 error "fail to cancel record for $cl_user1"
16496         wait $pid1
16497         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16498 }
16499 run_test 160m "Changelog clear race"
16500
16501 test_160n() {
16502         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16503         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16504                 skip "Need MDS version at least 2.14.51"
16505         local cl_users
16506         local cl_user1
16507         local cl_user2
16508         local pid1
16509         local first_rec
16510         local last_rec=0
16511
16512         # Create a user
16513         changelog_register || error "first changelog_register failed"
16514
16515         cl_users=(${CL_USERS[mds1]})
16516         cl_user1="${cl_users[0]}"
16517
16518         # generate some changelog records to accumulate on MDT0
16519         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16520         first_rec=$(changelog_users $SINGLEMDS |
16521                         awk '/^current.index:/ { print $NF }')
16522         while (( last_rec < (( first_rec + 65000)) )); do
16523                 createmany -m $DIR/$tdir/$tfile 10000 ||
16524                         error "create $DIR/$tdir/$tfile failed"
16525
16526                 for i in $(seq 0 10000); do
16527                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
16528                                 > /dev/null
16529                 done
16530
16531                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
16532                         error "unlinkmany failed unlink"
16533                 last_rec=$(changelog_users $SINGLEMDS |
16534                         awk '/^current.index:/ { print $NF }')
16535                 echo last record $last_rec
16536                 (( last_rec == 0 )) && error "no changelog found"
16537         done
16538
16539 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
16540         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
16541
16542         __changelog_clear mds1 $cl_user1 0 &
16543         pid1=$!
16544         sleep 2
16545         __changelog_clear mds1 $cl_user1 0 ||
16546                 error "fail to cancel record for $cl_user1"
16547         wait $pid1
16548         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16549 }
16550 run_test 160n "Changelog destroy race"
16551
16552 test_160o() {
16553         local mdt="$(facet_svc $SINGLEMDS)"
16554
16555         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16556         remote_mds_nodsh && skip "remote MDS with nodsh"
16557         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
16558                 skip "Need MDS version at least 2.14.52"
16559
16560         changelog_register --user test_160o -m unlnk+close+open ||
16561                 error "changelog_register failed"
16562
16563         do_facet $SINGLEMDS $LCTL --device $mdt \
16564                                 changelog_register -u "Tt3_-#" &&
16565                 error "bad symbols in name should fail"
16566
16567         do_facet $SINGLEMDS $LCTL --device $mdt \
16568                                 changelog_register -u test_160o &&
16569                 error "the same name registration should fail"
16570
16571         do_facet $SINGLEMDS $LCTL --device $mdt \
16572                         changelog_register -u test_160toolongname &&
16573                 error "too long name registration should fail"
16574
16575         changelog_chmask "MARK+HSM"
16576         lctl get_param mdd.*.changelog*mask
16577         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16578         changelog_users $SINGLEMDS | grep -q $cl_user ||
16579                 error "User $cl_user not found in changelog_users"
16580         #verify username
16581         echo $cl_user | grep -q test_160o ||
16582                 error "User $cl_user has no specific name 'test160o'"
16583
16584         # change something
16585         changelog_clear 0 || error "changelog_clear failed"
16586         # generate some changelog records to accumulate on MDT0
16587         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16588         touch $DIR/$tdir/$tfile                 # open 1
16589
16590         OPENS=$(changelog_dump | grep -c "OPEN")
16591         [[ $OPENS -eq 1 ]] || error "OPEN changelog mask count $OPENS != 1"
16592
16593         # must be no MKDIR it wasn't set as user mask
16594         MKDIR=$(changelog_dump | grep -c "MKDIR")
16595         [[ $MKDIR -eq 0 ]] || error "MKDIR changelog mask found $MKDIR > 0"
16596
16597         oldmask=$(do_facet $SINGLEMDS $LCTL get_param \
16598                                 mdd.$mdt.changelog_current_mask -n)
16599         # register maskless user
16600         changelog_register || error "changelog_register failed"
16601         # effective mask should be not changed because it is not minimal
16602         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16603                                 mdd.$mdt.changelog_current_mask -n)
16604         [[ $mask == $oldmask ]] || error "mask was changed: $mask vs $oldmask"
16605         # set server mask to minimal value
16606         changelog_chmask "MARK"
16607         # check effective mask again, should be treated as DEFMASK now
16608         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16609                                 mdd.$mdt.changelog_current_mask -n)
16610         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
16611
16612         do_facet $SINGLEMDS $LCTL --device $mdt \
16613                                 changelog_deregister -u test_160o ||
16614                 error "cannot deregister by name"
16615 }
16616 run_test 160o "changelog user name and mask"
16617
16618 test_160p() {
16619         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16620         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16621                 skip "Need MDS version at least 2.14.51"
16622         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
16623         local cl_users
16624         local cl_user1
16625         local entry_count
16626
16627         # Create a user
16628         changelog_register || error "first changelog_register failed"
16629
16630         cl_users=(${CL_USERS[mds1]})
16631         cl_user1="${cl_users[0]}"
16632
16633         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16634         createmany -m $DIR/$tdir/$tfile 50 ||
16635                 error "create $DIR/$tdir/$tfile failed"
16636         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16637         rm -rf $DIR/$tdir
16638
16639         # check changelogs have been generated
16640         entry_count=$(changelog_dump | wc -l)
16641         ((entry_count != 0)) || error "no changelog entries found"
16642
16643         # remove changelog_users and check that orphan entries are removed
16644         stop mds1
16645         do_facet mds1 "$DEBUGFS -w -R 'rm changelog_users' $(mdsdevname 1)"
16646         start mds1 || error "cannot start mdt"
16647         entry_count=$(changelog_dump | wc -l)
16648         ((entry_count == 0)) ||
16649                 error "found $entry_count changelog entries, expected none"
16650 }
16651 run_test 160p "Changelog orphan cleanup with no users"
16652
16653 test_160q() {
16654         local mdt="$(facet_svc $SINGLEMDS)"
16655         local clu
16656
16657         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
16658         remote_mds_nodsh && skip "remote MDS with nodsh"
16659         [ $MDS1_VERSION -ge $(version_code 2.14.54) ] ||
16660                 skip "Need MDS version at least 2.14.54"
16661
16662         # set server mask to minimal value like server init does
16663         changelog_chmask "MARK"
16664         clu=$(do_facet $SINGLEMDS $LCTL --device $mdt changelog_register -n) ||
16665                 error "changelog_register failed"
16666         # check effective mask again, should be treated as DEFMASK now
16667         mask=$(do_facet $SINGLEMDS $LCTL get_param \
16668                                 mdd.$mdt.changelog_current_mask -n)
16669         do_facet $SINGLEMDS $LCTL --device $mdt changelog_deregister $clu ||
16670                 error "changelog_deregister failed"
16671         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
16672 }
16673 run_test 160q "changelog effective mask is DEFMASK if not set"
16674
16675 test_161a() {
16676         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16677
16678         test_mkdir -c1 $DIR/$tdir
16679         cp /etc/hosts $DIR/$tdir/$tfile
16680         test_mkdir -c1 $DIR/$tdir/foo1
16681         test_mkdir -c1 $DIR/$tdir/foo2
16682         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
16683         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
16684         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
16685         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
16686         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
16687         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
16688                 $LFS fid2path $DIR $FID
16689                 error "bad link ea"
16690         fi
16691         # middle
16692         rm $DIR/$tdir/foo2/zachary
16693         # last
16694         rm $DIR/$tdir/foo2/thor
16695         # first
16696         rm $DIR/$tdir/$tfile
16697         # rename
16698         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
16699         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
16700                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
16701         rm $DIR/$tdir/foo2/maggie
16702
16703         # overflow the EA
16704         local longname=$tfile.avg_len_is_thirty_two_
16705         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
16706                 error_noexit 'failed to unlink many hardlinks'" EXIT
16707         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
16708                 error "failed to hardlink many files"
16709         links=$($LFS fid2path $DIR $FID | wc -l)
16710         echo -n "${links}/1000 links in link EA"
16711         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
16712 }
16713 run_test 161a "link ea sanity"
16714
16715 test_161b() {
16716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16717         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
16718
16719         local MDTIDX=1
16720         local remote_dir=$DIR/$tdir/remote_dir
16721
16722         mkdir -p $DIR/$tdir
16723         $LFS mkdir -i $MDTIDX $remote_dir ||
16724                 error "create remote directory failed"
16725
16726         cp /etc/hosts $remote_dir/$tfile
16727         mkdir -p $remote_dir/foo1
16728         mkdir -p $remote_dir/foo2
16729         ln $remote_dir/$tfile $remote_dir/foo1/sofia
16730         ln $remote_dir/$tfile $remote_dir/foo2/zachary
16731         ln $remote_dir/$tfile $remote_dir/foo1/luna
16732         ln $remote_dir/$tfile $remote_dir/foo2/thor
16733
16734         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
16735                      tr -d ']')
16736         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
16737                 $LFS fid2path $DIR $FID
16738                 error "bad link ea"
16739         fi
16740         # middle
16741         rm $remote_dir/foo2/zachary
16742         # last
16743         rm $remote_dir/foo2/thor
16744         # first
16745         rm $remote_dir/$tfile
16746         # rename
16747         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
16748         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
16749         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
16750                 $LFS fid2path $DIR $FID
16751                 error "bad link rename"
16752         fi
16753         rm $remote_dir/foo2/maggie
16754
16755         # overflow the EA
16756         local longname=filename_avg_len_is_thirty_two_
16757         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
16758                 error "failed to hardlink many files"
16759         links=$($LFS fid2path $DIR $FID | wc -l)
16760         echo -n "${links}/1000 links in link EA"
16761         [[ ${links} -gt 60 ]] ||
16762                 error "expected at least 60 links in link EA"
16763         unlinkmany $remote_dir/foo2/$longname 1000 ||
16764         error "failed to unlink many hardlinks"
16765 }
16766 run_test 161b "link ea sanity under remote directory"
16767
16768 test_161c() {
16769         remote_mds_nodsh && skip "remote MDS with nodsh"
16770         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16771         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
16772                 skip "Need MDS version at least 2.1.5"
16773
16774         # define CLF_RENAME_LAST 0x0001
16775         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
16776         changelog_register || error "changelog_register failed"
16777
16778         rm -rf $DIR/$tdir
16779         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
16780         touch $DIR/$tdir/foo_161c
16781         touch $DIR/$tdir/bar_161c
16782         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
16783         changelog_dump | grep RENME | tail -n 5
16784         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
16785         changelog_clear 0 || error "changelog_clear failed"
16786         if [ x$flags != "x0x1" ]; then
16787                 error "flag $flags is not 0x1"
16788         fi
16789
16790         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
16791         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
16792         touch $DIR/$tdir/foo_161c
16793         touch $DIR/$tdir/bar_161c
16794         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
16795         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
16796         changelog_dump | grep RENME | tail -n 5
16797         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
16798         changelog_clear 0 || error "changelog_clear failed"
16799         if [ x$flags != "x0x0" ]; then
16800                 error "flag $flags is not 0x0"
16801         fi
16802         echo "rename overwrite a target having nlink > 1," \
16803                 "changelog record has flags of $flags"
16804
16805         # rename doesn't overwrite a target (changelog flag 0x0)
16806         touch $DIR/$tdir/foo_161c
16807         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
16808         changelog_dump | grep RENME | tail -n 5
16809         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
16810         changelog_clear 0 || error "changelog_clear failed"
16811         if [ x$flags != "x0x0" ]; then
16812                 error "flag $flags is not 0x0"
16813         fi
16814         echo "rename doesn't overwrite a target," \
16815                 "changelog record has flags of $flags"
16816
16817         # define CLF_UNLINK_LAST 0x0001
16818         # unlink a file having nlink = 1 (changelog flag 0x1)
16819         rm -f $DIR/$tdir/foo2_161c
16820         changelog_dump | grep UNLNK | tail -n 5
16821         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
16822         changelog_clear 0 || error "changelog_clear failed"
16823         if [ x$flags != "x0x1" ]; then
16824                 error "flag $flags is not 0x1"
16825         fi
16826         echo "unlink a file having nlink = 1," \
16827                 "changelog record has flags of $flags"
16828
16829         # unlink a file having nlink > 1 (changelog flag 0x0)
16830         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
16831         rm -f $DIR/$tdir/foobar_161c
16832         changelog_dump | grep UNLNK | tail -n 5
16833         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
16834         changelog_clear 0 || error "changelog_clear failed"
16835         if [ x$flags != "x0x0" ]; then
16836                 error "flag $flags is not 0x0"
16837         fi
16838         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
16839 }
16840 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
16841
16842 test_161d() {
16843         remote_mds_nodsh && skip "remote MDS with nodsh"
16844         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
16845
16846         local pid
16847         local fid
16848
16849         changelog_register || error "changelog_register failed"
16850
16851         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
16852         # interfer with $MOUNT/.lustre/fid/ access
16853         mkdir $DIR/$tdir
16854         [[ $? -eq 0 ]] || error "mkdir failed"
16855
16856         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
16857         $LCTL set_param fail_loc=0x8000140c
16858         # 5s pause
16859         $LCTL set_param fail_val=5
16860
16861         # create file
16862         echo foofoo > $DIR/$tdir/$tfile &
16863         pid=$!
16864
16865         # wait for create to be delayed
16866         sleep 2
16867
16868         ps -p $pid
16869         [[ $? -eq 0 ]] || error "create should be blocked"
16870
16871         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
16872         stack_trap "rm -f $tempfile"
16873         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
16874         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
16875         # some delay may occur during ChangeLog publishing and file read just
16876         # above, that could allow file write to happen finally
16877         [[ -s $tempfile ]] && echo "file should be empty"
16878
16879         $LCTL set_param fail_loc=0
16880
16881         wait $pid
16882         [[ $? -eq 0 ]] || error "create failed"
16883 }
16884 run_test 161d "create with concurrent .lustre/fid access"
16885
16886 check_path() {
16887         local expected="$1"
16888         shift
16889         local fid="$2"
16890
16891         local path
16892         path=$($LFS fid2path "$@")
16893         local rc=$?
16894
16895         if [ $rc -ne 0 ]; then
16896                 error "path looked up of '$expected' failed: rc=$rc"
16897         elif [ "$path" != "$expected" ]; then
16898                 error "path looked up '$path' instead of '$expected'"
16899         else
16900                 echo "FID '$fid' resolves to path '$path' as expected"
16901         fi
16902 }
16903
16904 test_162a() { # was test_162
16905         test_mkdir -p -c1 $DIR/$tdir/d2
16906         touch $DIR/$tdir/d2/$tfile
16907         touch $DIR/$tdir/d2/x1
16908         touch $DIR/$tdir/d2/x2
16909         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
16910         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
16911         # regular file
16912         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
16913         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
16914
16915         # softlink
16916         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
16917         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
16918         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
16919
16920         # softlink to wrong file
16921         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
16922         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
16923         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
16924
16925         # hardlink
16926         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
16927         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
16928         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
16929         # fid2path dir/fsname should both work
16930         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
16931         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
16932
16933         # hardlink count: check that there are 2 links
16934         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
16935         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
16936
16937         # hardlink indexing: remove the first link
16938         rm $DIR/$tdir/d2/p/q/r/hlink
16939         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
16940 }
16941 run_test 162a "path lookup sanity"
16942
16943 test_162b() {
16944         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16945         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16946
16947         mkdir $DIR/$tdir
16948         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
16949                                 error "create striped dir failed"
16950
16951         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
16952                                         tail -n 1 | awk '{print $2}')
16953         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
16954
16955         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
16956         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
16957
16958         # regular file
16959         for ((i=0;i<5;i++)); do
16960                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
16961                         error "get fid for f$i failed"
16962                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
16963
16964                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
16965                         error "get fid for d$i failed"
16966                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
16967         done
16968
16969         return 0
16970 }
16971 run_test 162b "striped directory path lookup sanity"
16972
16973 # LU-4239: Verify fid2path works with paths 100 or more directories deep
16974 test_162c() {
16975         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
16976                 skip "Need MDS version at least 2.7.51"
16977
16978         local lpath=$tdir.local
16979         local rpath=$tdir.remote
16980
16981         test_mkdir $DIR/$lpath
16982         test_mkdir $DIR/$rpath
16983
16984         for ((i = 0; i <= 101; i++)); do
16985                 lpath="$lpath/$i"
16986                 mkdir $DIR/$lpath
16987                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
16988                         error "get fid for local directory $DIR/$lpath failed"
16989                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
16990
16991                 rpath="$rpath/$i"
16992                 test_mkdir $DIR/$rpath
16993                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
16994                         error "get fid for remote directory $DIR/$rpath failed"
16995                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
16996         done
16997
16998         return 0
16999 }
17000 run_test 162c "fid2path works with paths 100 or more directories deep"
17001
17002 oalr_event_count() {
17003         local event="${1}"
17004         local trace="${2}"
17005
17006         awk -v name="${FSNAME}-OST0000" \
17007             -v event="${event}" \
17008             '$1 == "TRACE" && $2 == event && $3 == name' \
17009             "${trace}" |
17010         wc -l
17011 }
17012
17013 oalr_expect_event_count() {
17014         local event="${1}"
17015         local trace="${2}"
17016         local expect="${3}"
17017         local count
17018
17019         count=$(oalr_event_count "${event}" "${trace}")
17020         if ((count == expect)); then
17021                 return 0
17022         fi
17023
17024         error_noexit "${event} event count was '${count}', expected ${expect}"
17025         cat "${trace}" >&2
17026         exit 1
17027 }
17028
17029 cleanup_165() {
17030         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
17031         stop ost1
17032         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
17033 }
17034
17035 setup_165() {
17036         sync # Flush previous IOs so we can count log entries.
17037         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
17038         stack_trap cleanup_165 EXIT
17039 }
17040
17041 test_165a() {
17042         local trace="/tmp/${tfile}.trace"
17043         local rc
17044         local count
17045
17046         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17047                 skip "OFD access log unsupported"
17048
17049         setup_165
17050         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17051         sleep 5
17052
17053         do_facet ost1 ofd_access_log_reader --list
17054         stop ost1
17055
17056         do_facet ost1 killall -TERM ofd_access_log_reader
17057         wait
17058         rc=$?
17059
17060         if ((rc != 0)); then
17061                 error "ofd_access_log_reader exited with rc = '${rc}'"
17062         fi
17063
17064         # Parse trace file for discovery events:
17065         oalr_expect_event_count alr_log_add "${trace}" 1
17066         oalr_expect_event_count alr_log_eof "${trace}" 1
17067         oalr_expect_event_count alr_log_free "${trace}" 1
17068 }
17069 run_test 165a "ofd access log discovery"
17070
17071 test_165b() {
17072         local trace="/tmp/${tfile}.trace"
17073         local file="${DIR}/${tfile}"
17074         local pfid1
17075         local pfid2
17076         local -a entry
17077         local rc
17078         local count
17079         local size
17080         local flags
17081
17082         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17083                 skip "OFD access log unsupported"
17084
17085         setup_165
17086         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17087         sleep 5
17088
17089         do_facet ost1 ofd_access_log_reader --list
17090
17091         lfs setstripe -c 1 -i 0 "${file}"
17092         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17093                 error "cannot create '${file}'"
17094
17095         sleep 5
17096         do_facet ost1 killall -TERM ofd_access_log_reader
17097         wait
17098         rc=$?
17099
17100         if ((rc != 0)); then
17101                 error "ofd_access_log_reader exited with rc = '${rc}'"
17102         fi
17103
17104         oalr_expect_event_count alr_log_entry "${trace}" 1
17105
17106         pfid1=$($LFS path2fid "${file}")
17107
17108         # 1     2             3   4    5     6   7    8    9     10
17109         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
17110         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17111
17112         echo "entry = '${entry[*]}'" >&2
17113
17114         pfid2=${entry[4]}
17115         if [[ "${pfid1}" != "${pfid2}" ]]; then
17116                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17117         fi
17118
17119         size=${entry[8]}
17120         if ((size != 1048576)); then
17121                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
17122         fi
17123
17124         flags=${entry[10]}
17125         if [[ "${flags}" != "w" ]]; then
17126                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
17127         fi
17128
17129         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17130         sleep 5
17131
17132         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c ||
17133                 error "cannot read '${file}'"
17134         sleep 5
17135
17136         do_facet ost1 killall -TERM ofd_access_log_reader
17137         wait
17138         rc=$?
17139
17140         if ((rc != 0)); then
17141                 error "ofd_access_log_reader exited with rc = '${rc}'"
17142         fi
17143
17144         oalr_expect_event_count alr_log_entry "${trace}" 1
17145
17146         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17147         echo "entry = '${entry[*]}'" >&2
17148
17149         pfid2=${entry[4]}
17150         if [[ "${pfid1}" != "${pfid2}" ]]; then
17151                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17152         fi
17153
17154         size=${entry[8]}
17155         if ((size != 524288)); then
17156                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
17157         fi
17158
17159         flags=${entry[10]}
17160         if [[ "${flags}" != "r" ]]; then
17161                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
17162         fi
17163 }
17164 run_test 165b "ofd access log entries are produced and consumed"
17165
17166 test_165c() {
17167         local trace="/tmp/${tfile}.trace"
17168         local file="${DIR}/${tdir}/${tfile}"
17169
17170         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17171                 skip "OFD access log unsupported"
17172
17173         test_mkdir "${DIR}/${tdir}"
17174
17175         setup_165
17176         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17177         sleep 5
17178
17179         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
17180
17181         # 4096 / 64 = 64. Create twice as many entries.
17182         for ((i = 0; i < 128; i++)); do
17183                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c ||
17184                         error "cannot create file"
17185         done
17186
17187         sync
17188
17189         do_facet ost1 killall -TERM ofd_access_log_reader
17190         wait
17191         rc=$?
17192         if ((rc != 0)); then
17193                 error "ofd_access_log_reader exited with rc = '${rc}'"
17194         fi
17195
17196         unlinkmany  "${file}-%d" 128
17197 }
17198 run_test 165c "full ofd access logs do not block IOs"
17199
17200 oal_get_read_count() {
17201         local stats="$1"
17202
17203         # STATS lustre-OST0001 alr_read_count 1
17204
17205         do_facet ost1 cat "${stats}" |
17206         awk '$1 == "STATS" && $3 == "alr_read_count" { count = $4; }
17207              END { print count; }'
17208 }
17209
17210 oal_expect_read_count() {
17211         local stats="$1"
17212         local count
17213         local expect="$2"
17214
17215         # Ask ofd_access_log_reader to write stats.
17216         do_facet ost1 killall -USR1 ofd_access_log_reader
17217
17218         # Allow some time for things to happen.
17219         sleep 1
17220
17221         count=$(oal_get_read_count "${stats}")
17222         if ((count == expect)); then
17223                 return 0
17224         fi
17225
17226         error_noexit "bad read count, got ${count}, expected ${expect}"
17227         do_facet ost1 cat "${stats}" >&2
17228         exit 1
17229 }
17230
17231 test_165d() {
17232         local stats="/tmp/${tfile}.stats"
17233         local file="${DIR}/${tdir}/${tfile}"
17234         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
17235
17236         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17237                 skip "OFD access log unsupported"
17238
17239         test_mkdir "${DIR}/${tdir}"
17240
17241         setup_165
17242         do_facet ost1 ofd_access_log_reader --stats="${stats}" &
17243         sleep 5
17244
17245         lfs setstripe -c 1 -i 0 "${file}"
17246
17247         do_facet ost1 lctl set_param "${param}=rw"
17248         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17249                 error "cannot create '${file}'"
17250         oal_expect_read_count "${stats}" 1
17251
17252         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17253                 error "cannot read '${file}'"
17254         oal_expect_read_count "${stats}" 2
17255
17256         do_facet ost1 lctl set_param "${param}=r"
17257         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17258                 error "cannot create '${file}'"
17259         oal_expect_read_count "${stats}" 2
17260
17261         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17262                 error "cannot read '${file}'"
17263         oal_expect_read_count "${stats}" 3
17264
17265         do_facet ost1 lctl set_param "${param}=w"
17266         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17267                 error "cannot create '${file}'"
17268         oal_expect_read_count "${stats}" 4
17269
17270         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17271                 error "cannot read '${file}'"
17272         oal_expect_read_count "${stats}" 4
17273
17274         do_facet ost1 lctl set_param "${param}=0"
17275         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17276                 error "cannot create '${file}'"
17277         oal_expect_read_count "${stats}" 4
17278
17279         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17280                 error "cannot read '${file}'"
17281         oal_expect_read_count "${stats}" 4
17282
17283         do_facet ost1 killall -TERM ofd_access_log_reader
17284         wait
17285         rc=$?
17286         if ((rc != 0)); then
17287                 error "ofd_access_log_reader exited with rc = '${rc}'"
17288         fi
17289 }
17290 run_test 165d "ofd_access_log mask works"
17291
17292 test_165e() {
17293         local stats="/tmp/${tfile}.stats"
17294         local file0="${DIR}/${tdir}-0/${tfile}"
17295         local file1="${DIR}/${tdir}-1/${tfile}"
17296
17297         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17298                 skip "OFD access log unsupported"
17299
17300         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
17301
17302         test_mkdir -c 1 -i 0 "${DIR}/${tdir}-0"
17303         test_mkdir -c 1 -i 1 "${DIR}/${tdir}-1"
17304
17305         lfs setstripe -c 1 -i 0 "${file0}"
17306         lfs setstripe -c 1 -i 0 "${file1}"
17307
17308         setup_165
17309         do_facet ost1 ofd_access_log_reader -I 1 --stats="${stats}" &
17310         sleep 5
17311
17312         $MULTIOP "${file0}" oO_CREAT:O_WRONLY:w512c ||
17313                 error "cannot create '${file0}'"
17314         sync
17315         oal_expect_read_count "${stats}" 0
17316
17317         $MULTIOP "${file1}" oO_CREAT:O_WRONLY:w512c ||
17318                 error "cannot create '${file1}'"
17319         sync
17320         oal_expect_read_count "${stats}" 1
17321
17322         do_facet ost1 killall -TERM ofd_access_log_reader
17323         wait
17324         rc=$?
17325         if ((rc != 0)); then
17326                 error "ofd_access_log_reader exited with rc = '${rc}'"
17327         fi
17328 }
17329 run_test 165e "ofd_access_log MDT index filter works"
17330
17331 test_165f() {
17332         local trace="/tmp/${tfile}.trace"
17333         local rc
17334         local count
17335
17336         setup_165
17337         do_facet ost1 timeout 60 ofd_access_log_reader \
17338                 --exit-on-close --debug=- --trace=- > "${trace}" &
17339         sleep 5
17340         stop ost1
17341
17342         wait
17343         rc=$?
17344
17345         if ((rc != 0)); then
17346                 error_noexit "ofd_access_log_reader exited with rc = '${rc}'"
17347                 cat "${trace}"
17348                 exit 1
17349         fi
17350 }
17351 run_test 165f "ofd_access_log_reader --exit-on-close works"
17352
17353 test_169() {
17354         # do directio so as not to populate the page cache
17355         log "creating a 10 Mb file"
17356         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
17357                 error "multiop failed while creating a file"
17358         log "starting reads"
17359         dd if=$DIR/$tfile of=/dev/null bs=4096 &
17360         log "truncating the file"
17361         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
17362                 error "multiop failed while truncating the file"
17363         log "killing dd"
17364         kill %+ || true # reads might have finished
17365         echo "wait until dd is finished"
17366         wait
17367         log "removing the temporary file"
17368         rm -rf $DIR/$tfile || error "tmp file removal failed"
17369 }
17370 run_test 169 "parallel read and truncate should not deadlock"
17371
17372 test_170() {
17373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17374
17375         $LCTL clear     # bug 18514
17376         $LCTL debug_daemon start $TMP/${tfile}_log_good
17377         touch $DIR/$tfile
17378         $LCTL debug_daemon stop
17379         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
17380                 error "sed failed to read log_good"
17381
17382         $LCTL debug_daemon start $TMP/${tfile}_log_good
17383         rm -rf $DIR/$tfile
17384         $LCTL debug_daemon stop
17385
17386         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
17387                error "lctl df log_bad failed"
17388
17389         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17390         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17391
17392         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
17393         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
17394
17395         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
17396                 error "bad_line good_line1 good_line2 are empty"
17397
17398         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
17399         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
17400         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
17401
17402         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
17403         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17404         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17405
17406         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
17407                 error "bad_line_new good_line_new are empty"
17408
17409         local expected_good=$((good_line1 + good_line2*2))
17410
17411         rm -f $TMP/${tfile}*
17412         # LU-231, short malformed line may not be counted into bad lines
17413         if [ $bad_line -ne $bad_line_new ] &&
17414                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
17415                 error "expected $bad_line bad lines, but got $bad_line_new"
17416                 return 1
17417         fi
17418
17419         if [ $expected_good -ne $good_line_new ]; then
17420                 error "expected $expected_good good lines, but got $good_line_new"
17421                 return 2
17422         fi
17423         true
17424 }
17425 run_test 170 "test lctl df to handle corrupted log ====================="
17426
17427 test_171() { # bug20592
17428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17429
17430         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
17431         $LCTL set_param fail_loc=0x50e
17432         $LCTL set_param fail_val=3000
17433         multiop_bg_pause $DIR/$tfile O_s || true
17434         local MULTIPID=$!
17435         kill -USR1 $MULTIPID
17436         # cause log dump
17437         sleep 3
17438         wait $MULTIPID
17439         if dmesg | grep "recursive fault"; then
17440                 error "caught a recursive fault"
17441         fi
17442         $LCTL set_param fail_loc=0
17443         true
17444 }
17445 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
17446
17447 # it would be good to share it with obdfilter-survey/iokit-libecho code
17448 setup_obdecho_osc () {
17449         local rc=0
17450         local ost_nid=$1
17451         local obdfilter_name=$2
17452         echo "Creating new osc for $obdfilter_name on $ost_nid"
17453         # make sure we can find loopback nid
17454         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
17455
17456         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
17457                            ${obdfilter_name}_osc_UUID || rc=2; }
17458         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
17459                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
17460         return $rc
17461 }
17462
17463 cleanup_obdecho_osc () {
17464         local obdfilter_name=$1
17465         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
17466         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
17467         return 0
17468 }
17469
17470 obdecho_test() {
17471         local OBD=$1
17472         local node=$2
17473         local pages=${3:-64}
17474         local rc=0
17475         local id
17476
17477         local count=10
17478         local obd_size=$(get_obd_size $node $OBD)
17479         local page_size=$(get_page_size $node)
17480         if [[ -n "$obd_size" ]]; then
17481                 local new_count=$((obd_size / (pages * page_size / 1024)))
17482                 [[ $new_count -ge $count ]] || count=$new_count
17483         fi
17484
17485         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
17486         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
17487                            rc=2; }
17488         if [ $rc -eq 0 ]; then
17489             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
17490             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
17491         fi
17492         echo "New object id is $id"
17493         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
17494                            rc=4; }
17495         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
17496                            "test_brw $count w v $pages $id" || rc=4; }
17497         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
17498                            rc=4; }
17499         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
17500                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
17501         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
17502                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
17503         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
17504         return $rc
17505 }
17506
17507 test_180a() {
17508         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17509
17510         if ! [ -d /sys/fs/lustre/echo_client ] &&
17511            ! module_loaded obdecho; then
17512                 load_module obdecho/obdecho &&
17513                         stack_trap "rmmod obdecho" EXIT ||
17514                         error "unable to load obdecho on client"
17515         fi
17516
17517         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
17518         local host=$($LCTL get_param -n osc.$osc.import |
17519                      awk '/current_connection:/ { print $2 }' )
17520         local target=$($LCTL get_param -n osc.$osc.import |
17521                        awk '/target:/ { print $2 }' )
17522         target=${target%_UUID}
17523
17524         if [ -n "$target" ]; then
17525                 setup_obdecho_osc $host $target &&
17526                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
17527                         { error "obdecho setup failed with $?"; return; }
17528
17529                 obdecho_test ${target}_osc client ||
17530                         error "obdecho_test failed on ${target}_osc"
17531         else
17532                 $LCTL get_param osc.$osc.import
17533                 error "there is no osc.$osc.import target"
17534         fi
17535 }
17536 run_test 180a "test obdecho on osc"
17537
17538 test_180b() {
17539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17540         remote_ost_nodsh && skip "remote OST with nodsh"
17541
17542         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
17543                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
17544                 error "failed to load module obdecho"
17545
17546         local target=$(do_facet ost1 $LCTL dl |
17547                        awk '/obdfilter/ { print $4; exit; }')
17548
17549         if [ -n "$target" ]; then
17550                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
17551         else
17552                 do_facet ost1 $LCTL dl
17553                 error "there is no obdfilter target on ost1"
17554         fi
17555 }
17556 run_test 180b "test obdecho directly on obdfilter"
17557
17558 test_180c() { # LU-2598
17559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17560         remote_ost_nodsh && skip "remote OST with nodsh"
17561         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
17562                 skip "Need MDS version at least 2.4.0"
17563
17564         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
17565                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
17566                 error "failed to load module obdecho"
17567
17568         local target=$(do_facet ost1 $LCTL dl |
17569                        awk '/obdfilter/ { print $4; exit; }')
17570
17571         if [ -n "$target" ]; then
17572                 local pages=16384 # 64MB bulk I/O RPC size
17573
17574                 obdecho_test "$target" ost1 "$pages" ||
17575                         error "obdecho_test with pages=$pages failed with $?"
17576         else
17577                 do_facet ost1 $LCTL dl
17578                 error "there is no obdfilter target on ost1"
17579         fi
17580 }
17581 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
17582
17583 test_181() { # bug 22177
17584         test_mkdir $DIR/$tdir
17585         # create enough files to index the directory
17586         createmany -o $DIR/$tdir/foobar 4000
17587         # print attributes for debug purpose
17588         lsattr -d .
17589         # open dir
17590         multiop_bg_pause $DIR/$tdir D_Sc || return 1
17591         MULTIPID=$!
17592         # remove the files & current working dir
17593         unlinkmany $DIR/$tdir/foobar 4000
17594         rmdir $DIR/$tdir
17595         kill -USR1 $MULTIPID
17596         wait $MULTIPID
17597         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
17598         return 0
17599 }
17600 run_test 181 "Test open-unlinked dir ========================"
17601
17602 test_182() {
17603         local fcount=1000
17604         local tcount=10
17605
17606         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
17607
17608         $LCTL set_param mdc.*.rpc_stats=clear
17609
17610         for (( i = 0; i < $tcount; i++ )) ; do
17611                 mkdir $DIR/$tdir/$i
17612         done
17613
17614         for (( i = 0; i < $tcount; i++ )) ; do
17615                 createmany -o $DIR/$tdir/$i/f- $fcount &
17616         done
17617         wait
17618
17619         for (( i = 0; i < $tcount; i++ )) ; do
17620                 unlinkmany $DIR/$tdir/$i/f- $fcount &
17621         done
17622         wait
17623
17624         $LCTL get_param mdc.*.rpc_stats
17625
17626         rm -rf $DIR/$tdir
17627 }
17628 run_test 182 "Test parallel modify metadata operations ================"
17629
17630 test_183() { # LU-2275
17631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17632         remote_mds_nodsh && skip "remote MDS with nodsh"
17633         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
17634                 skip "Need MDS version at least 2.3.56"
17635
17636         mkdir_on_mdt0 $DIR/$tdir || error "creating dir $DIR/$tdir"
17637         echo aaa > $DIR/$tdir/$tfile
17638
17639 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
17640         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
17641
17642         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
17643         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
17644
17645         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
17646
17647         # Flush negative dentry cache
17648         touch $DIR/$tdir/$tfile
17649
17650         # We are not checking for any leaked references here, they'll
17651         # become evident next time we do cleanup with module unload.
17652         rm -rf $DIR/$tdir
17653 }
17654 run_test 183 "No crash or request leak in case of strange dispositions ========"
17655
17656 # test suite 184 is for LU-2016, LU-2017
17657 test_184a() {
17658         check_swap_layouts_support
17659
17660         dir0=$DIR/$tdir/$testnum
17661         test_mkdir -p -c1 $dir0
17662         ref1=/etc/passwd
17663         ref2=/etc/group
17664         file1=$dir0/f1
17665         file2=$dir0/f2
17666         $LFS setstripe -c1 $file1
17667         cp $ref1 $file1
17668         $LFS setstripe -c2 $file2
17669         cp $ref2 $file2
17670         gen1=$($LFS getstripe -g $file1)
17671         gen2=$($LFS getstripe -g $file2)
17672
17673         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
17674         gen=$($LFS getstripe -g $file1)
17675         [[ $gen1 != $gen ]] ||
17676                 "Layout generation on $file1 does not change"
17677         gen=$($LFS getstripe -g $file2)
17678         [[ $gen2 != $gen ]] ||
17679                 "Layout generation on $file2 does not change"
17680
17681         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
17682         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
17683
17684         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
17685 }
17686 run_test 184a "Basic layout swap"
17687
17688 test_184b() {
17689         check_swap_layouts_support
17690
17691         dir0=$DIR/$tdir/$testnum
17692         mkdir -p $dir0 || error "creating dir $dir0"
17693         file1=$dir0/f1
17694         file2=$dir0/f2
17695         file3=$dir0/f3
17696         dir1=$dir0/d1
17697         dir2=$dir0/d2
17698         mkdir $dir1 $dir2
17699         $LFS setstripe -c1 $file1
17700         $LFS setstripe -c2 $file2
17701         $LFS setstripe -c1 $file3
17702         chown $RUNAS_ID $file3
17703         gen1=$($LFS getstripe -g $file1)
17704         gen2=$($LFS getstripe -g $file2)
17705
17706         $LFS swap_layouts $dir1 $dir2 &&
17707                 error "swap of directories layouts should fail"
17708         $LFS swap_layouts $dir1 $file1 &&
17709                 error "swap of directory and file layouts should fail"
17710         $RUNAS $LFS swap_layouts $file1 $file2 &&
17711                 error "swap of file we cannot write should fail"
17712         $LFS swap_layouts $file1 $file3 &&
17713                 error "swap of file with different owner should fail"
17714         /bin/true # to clear error code
17715 }
17716 run_test 184b "Forbidden layout swap (will generate errors)"
17717
17718 test_184c() {
17719         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
17720         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
17721         check_swap_layouts_support
17722         check_swap_layout_no_dom $DIR
17723
17724         local dir0=$DIR/$tdir/$testnum
17725         mkdir -p $dir0 || error "creating dir $dir0"
17726
17727         local ref1=$dir0/ref1
17728         local ref2=$dir0/ref2
17729         local file1=$dir0/file1
17730         local file2=$dir0/file2
17731         # create a file large enough for the concurrent test
17732         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
17733         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
17734         echo "ref file size: ref1($(stat -c %s $ref1))," \
17735              "ref2($(stat -c %s $ref2))"
17736
17737         cp $ref2 $file2
17738         dd if=$ref1 of=$file1 bs=16k &
17739         local DD_PID=$!
17740
17741         # Make sure dd starts to copy file, but wait at most 5 seconds
17742         local loops=0
17743         while [ ! -s $file1 -a $((loops++)) -lt 50 ]; do sleep 0.1; done
17744
17745         $LFS swap_layouts $file1 $file2
17746         local rc=$?
17747         wait $DD_PID
17748         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
17749         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
17750
17751         # how many bytes copied before swapping layout
17752         local copied=$(stat -c %s $file2)
17753         local remaining=$(stat -c %s $ref1)
17754         remaining=$((remaining - copied))
17755         echo "Copied $copied bytes before swapping layout..."
17756
17757         cmp -n $copied $file1 $ref2 | grep differ &&
17758                 error "Content mismatch [0, $copied) of ref2 and file1"
17759         cmp -n $copied $file2 $ref1 ||
17760                 error "Content mismatch [0, $copied) of ref1 and file2"
17761         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
17762                 error "Content mismatch [$copied, EOF) of ref1 and file1"
17763
17764         # clean up
17765         rm -f $ref1 $ref2 $file1 $file2
17766 }
17767 run_test 184c "Concurrent write and layout swap"
17768
17769 test_184d() {
17770         check_swap_layouts_support
17771         check_swap_layout_no_dom $DIR
17772         [ -z "$(which getfattr 2>/dev/null)" ] &&
17773                 skip_env "no getfattr command"
17774
17775         local file1=$DIR/$tdir/$tfile-1
17776         local file2=$DIR/$tdir/$tfile-2
17777         local file3=$DIR/$tdir/$tfile-3
17778         local lovea1
17779         local lovea2
17780
17781         mkdir -p $DIR/$tdir
17782         touch $file1 || error "create $file1 failed"
17783         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
17784                 error "create $file2 failed"
17785         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
17786                 error "create $file3 failed"
17787         lovea1=$(get_layout_param $file1)
17788
17789         $LFS swap_layouts $file2 $file3 ||
17790                 error "swap $file2 $file3 layouts failed"
17791         $LFS swap_layouts $file1 $file2 ||
17792                 error "swap $file1 $file2 layouts failed"
17793
17794         lovea2=$(get_layout_param $file2)
17795         echo "$lovea1"
17796         echo "$lovea2"
17797         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
17798
17799         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
17800         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
17801 }
17802 run_test 184d "allow stripeless layouts swap"
17803
17804 test_184e() {
17805         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
17806                 skip "Need MDS version at least 2.6.94"
17807         check_swap_layouts_support
17808         check_swap_layout_no_dom $DIR
17809         [ -z "$(which getfattr 2>/dev/null)" ] &&
17810                 skip_env "no getfattr command"
17811
17812         local file1=$DIR/$tdir/$tfile-1
17813         local file2=$DIR/$tdir/$tfile-2
17814         local file3=$DIR/$tdir/$tfile-3
17815         local lovea
17816
17817         mkdir -p $DIR/$tdir
17818         touch $file1 || error "create $file1 failed"
17819         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
17820                 error "create $file2 failed"
17821         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
17822                 error "create $file3 failed"
17823
17824         $LFS swap_layouts $file1 $file2 ||
17825                 error "swap $file1 $file2 layouts failed"
17826
17827         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
17828         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
17829
17830         echo 123 > $file1 || error "Should be able to write into $file1"
17831
17832         $LFS swap_layouts $file1 $file3 ||
17833                 error "swap $file1 $file3 layouts failed"
17834
17835         echo 123 > $file1 || error "Should be able to write into $file1"
17836
17837         rm -rf $file1 $file2 $file3
17838 }
17839 run_test 184e "Recreate layout after stripeless layout swaps"
17840
17841 test_184f() {
17842         # Create a file with name longer than sizeof(struct stat) ==
17843         # 144 to see if we can get chars from the file name to appear
17844         # in the returned striping. Note that 'f' == 0x66.
17845         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
17846
17847         mkdir -p $DIR/$tdir
17848         mcreate $DIR/$tdir/$file
17849         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
17850                 error "IOC_MDC_GETFILEINFO returned garbage striping"
17851         fi
17852 }
17853 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
17854
17855 test_185() { # LU-2441
17856         # LU-3553 - no volatile file support in old servers
17857         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
17858                 skip "Need MDS version at least 2.3.60"
17859
17860         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
17861         touch $DIR/$tdir/spoo
17862         local mtime1=$(stat -c "%Y" $DIR/$tdir)
17863         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
17864                 error "cannot create/write a volatile file"
17865         [ "$FILESET" == "" ] &&
17866         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
17867                 error "FID is still valid after close"
17868
17869         multiop_bg_pause $DIR/$tdir vVw4096_c
17870         local multi_pid=$!
17871
17872         local OLD_IFS=$IFS
17873         IFS=":"
17874         local fidv=($fid)
17875         IFS=$OLD_IFS
17876         # assume that the next FID for this client is sequential, since stdout
17877         # is unfortunately eaten by multiop_bg_pause
17878         local n=$((${fidv[1]} + 1))
17879         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
17880         if [ "$FILESET" == "" ]; then
17881                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
17882                         error "FID is missing before close"
17883         fi
17884         kill -USR1 $multi_pid
17885         # 1 second delay, so if mtime change we will see it
17886         sleep 1
17887         local mtime2=$(stat -c "%Y" $DIR/$tdir)
17888         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
17889 }
17890 run_test 185 "Volatile file support"
17891
17892 function create_check_volatile() {
17893         local idx=$1
17894         local tgt
17895
17896         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
17897         local PID=$!
17898         sleep 1
17899         local FID=$(cat /tmp/${tfile}.fid)
17900         [ "$FID" == "" ] && error "can't get FID for volatile"
17901         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
17902         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
17903         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
17904         kill -USR1 $PID
17905         wait
17906         sleep 1
17907         cancel_lru_locks mdc # flush opencache
17908         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
17909         return 0
17910 }
17911
17912 test_185a(){
17913         # LU-12516 - volatile creation via .lustre
17914         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
17915                 skip "Need MDS version at least 2.3.55"
17916
17917         create_check_volatile 0
17918         [ $MDSCOUNT -lt 2 ] && return 0
17919
17920         # DNE case
17921         create_check_volatile 1
17922
17923         return 0
17924 }
17925 run_test 185a "Volatile file creation in .lustre/fid/"
17926
17927 test_187a() {
17928         remote_mds_nodsh && skip "remote MDS with nodsh"
17929         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
17930                 skip "Need MDS version at least 2.3.0"
17931
17932         local dir0=$DIR/$tdir/$testnum
17933         mkdir -p $dir0 || error "creating dir $dir0"
17934
17935         local file=$dir0/file1
17936         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
17937         local dv1=$($LFS data_version $file)
17938         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
17939         local dv2=$($LFS data_version $file)
17940         [[ $dv1 != $dv2 ]] ||
17941                 error "data version did not change on write $dv1 == $dv2"
17942
17943         # clean up
17944         rm -f $file1
17945 }
17946 run_test 187a "Test data version change"
17947
17948 test_187b() {
17949         remote_mds_nodsh && skip "remote MDS with nodsh"
17950         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
17951                 skip "Need MDS version at least 2.3.0"
17952
17953         local dir0=$DIR/$tdir/$testnum
17954         mkdir -p $dir0 || error "creating dir $dir0"
17955
17956         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
17957         [[ ${DV[0]} != ${DV[1]} ]] ||
17958                 error "data version did not change on write"\
17959                       " ${DV[0]} == ${DV[1]}"
17960
17961         # clean up
17962         rm -f $file1
17963 }
17964 run_test 187b "Test data version change on volatile file"
17965
17966 test_200() {
17967         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17968         remote_mgs_nodsh && skip "remote MGS with nodsh"
17969         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
17970
17971         local POOL=${POOL:-cea1}
17972         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
17973         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
17974         # Pool OST targets
17975         local first_ost=0
17976         local last_ost=$(($OSTCOUNT - 1))
17977         local ost_step=2
17978         local ost_list=$(seq $first_ost $ost_step $last_ost)
17979         local ost_range="$first_ost $last_ost $ost_step"
17980         local test_path=$POOL_ROOT/$POOL_DIR_NAME
17981         local file_dir=$POOL_ROOT/file_tst
17982         local subdir=$test_path/subdir
17983         local rc=0
17984
17985         while : ; do
17986                 # former test_200a test_200b
17987                 pool_add $POOL                          || { rc=$? ; break; }
17988                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
17989                 # former test_200c test_200d
17990                 mkdir -p $test_path
17991                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
17992                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
17993                 mkdir -p $subdir
17994                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
17995                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
17996                                                         || { rc=$? ; break; }
17997                 # former test_200e test_200f
17998                 local files=$((OSTCOUNT*3))
17999                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
18000                                                         || { rc=$? ; break; }
18001                 pool_create_files $POOL $file_dir $files "$ost_list" \
18002                                                         || { rc=$? ; break; }
18003                 # former test_200g test_200h
18004                 pool_lfs_df $POOL                       || { rc=$? ; break; }
18005                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
18006
18007                 # former test_201a test_201b test_201c
18008                 pool_remove_first_target $POOL          || { rc=$? ; break; }
18009
18010                 local f=$test_path/$tfile
18011                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
18012                 pool_remove $POOL $f                    || { rc=$? ; break; }
18013                 break
18014         done
18015
18016         destroy_test_pools
18017
18018         return $rc
18019 }
18020 run_test 200 "OST pools"
18021
18022 # usage: default_attr <count | size | offset>
18023 default_attr() {
18024         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
18025 }
18026
18027 # usage: check_default_stripe_attr
18028 check_default_stripe_attr() {
18029         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
18030         case $1 in
18031         --stripe-count|-c)
18032                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
18033         --stripe-size|-S)
18034                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
18035         --stripe-index|-i)
18036                 EXPECTED=-1;;
18037         *)
18038                 error "unknown getstripe attr '$1'"
18039         esac
18040
18041         [ $ACTUAL == $EXPECTED ] ||
18042                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
18043 }
18044
18045 test_204a() {
18046         test_mkdir $DIR/$tdir
18047         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
18048
18049         check_default_stripe_attr --stripe-count
18050         check_default_stripe_attr --stripe-size
18051         check_default_stripe_attr --stripe-index
18052 }
18053 run_test 204a "Print default stripe attributes"
18054
18055 test_204b() {
18056         test_mkdir $DIR/$tdir
18057         $LFS setstripe --stripe-count 1 $DIR/$tdir
18058
18059         check_default_stripe_attr --stripe-size
18060         check_default_stripe_attr --stripe-index
18061 }
18062 run_test 204b "Print default stripe size and offset"
18063
18064 test_204c() {
18065         test_mkdir $DIR/$tdir
18066         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18067
18068         check_default_stripe_attr --stripe-count
18069         check_default_stripe_attr --stripe-index
18070 }
18071 run_test 204c "Print default stripe count and offset"
18072
18073 test_204d() {
18074         test_mkdir $DIR/$tdir
18075         $LFS setstripe --stripe-index 0 $DIR/$tdir
18076
18077         check_default_stripe_attr --stripe-count
18078         check_default_stripe_attr --stripe-size
18079 }
18080 run_test 204d "Print default stripe count and size"
18081
18082 test_204e() {
18083         test_mkdir $DIR/$tdir
18084         $LFS setstripe -d $DIR/$tdir
18085
18086         check_default_stripe_attr --stripe-count --raw
18087         check_default_stripe_attr --stripe-size --raw
18088         check_default_stripe_attr --stripe-index --raw
18089 }
18090 run_test 204e "Print raw stripe attributes"
18091
18092 test_204f() {
18093         test_mkdir $DIR/$tdir
18094         $LFS setstripe --stripe-count 1 $DIR/$tdir
18095
18096         check_default_stripe_attr --stripe-size --raw
18097         check_default_stripe_attr --stripe-index --raw
18098 }
18099 run_test 204f "Print raw stripe size and offset"
18100
18101 test_204g() {
18102         test_mkdir $DIR/$tdir
18103         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18104
18105         check_default_stripe_attr --stripe-count --raw
18106         check_default_stripe_attr --stripe-index --raw
18107 }
18108 run_test 204g "Print raw stripe count and offset"
18109
18110 test_204h() {
18111         test_mkdir $DIR/$tdir
18112         $LFS setstripe --stripe-index 0 $DIR/$tdir
18113
18114         check_default_stripe_attr --stripe-count --raw
18115         check_default_stripe_attr --stripe-size --raw
18116 }
18117 run_test 204h "Print raw stripe count and size"
18118
18119 # Figure out which job scheduler is being used, if any,
18120 # or use a fake one
18121 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
18122         JOBENV=SLURM_JOB_ID
18123 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
18124         JOBENV=LSB_JOBID
18125 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
18126         JOBENV=PBS_JOBID
18127 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
18128         JOBENV=LOADL_STEP_ID
18129 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
18130         JOBENV=JOB_ID
18131 else
18132         $LCTL list_param jobid_name > /dev/null 2>&1
18133         if [ $? -eq 0 ]; then
18134                 JOBENV=nodelocal
18135         else
18136                 JOBENV=FAKE_JOBID
18137         fi
18138 fi
18139 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
18140
18141 verify_jobstats() {
18142         local cmd=($1)
18143         shift
18144         local facets="$@"
18145
18146 # we don't really need to clear the stats for this test to work, since each
18147 # command has a unique jobid, but it makes debugging easier if needed.
18148 #       for facet in $facets; do
18149 #               local dev=$(convert_facet2label $facet)
18150 #               # clear old jobstats
18151 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
18152 #       done
18153
18154         # use a new JobID for each test, or we might see an old one
18155         [ "$JOBENV" = "FAKE_JOBID" ] &&
18156                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
18157
18158         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
18159
18160         [ "$JOBENV" = "nodelocal" ] && {
18161                 FAKE_JOBID=id.$testnum.%e.$RANDOM
18162                 $LCTL set_param jobid_name=$FAKE_JOBID
18163                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
18164         }
18165
18166         log "Test: ${cmd[*]}"
18167         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
18168
18169         if [ $JOBENV = "FAKE_JOBID" ]; then
18170                 FAKE_JOBID=$JOBVAL ${cmd[*]}
18171         else
18172                 ${cmd[*]}
18173         fi
18174
18175         # all files are created on OST0000
18176         for facet in $facets; do
18177                 local stats="*.$(convert_facet2label $facet).job_stats"
18178
18179                 # strip out libtool wrappers for in-tree executables
18180                 if [ $(do_facet $facet lctl get_param $stats |
18181                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
18182                         do_facet $facet lctl get_param $stats
18183                         error "No jobstats for $JOBVAL found on $facet::$stats"
18184                 fi
18185         done
18186 }
18187
18188 jobstats_set() {
18189         local new_jobenv=$1
18190
18191         set_persistent_param_and_check client "jobid_var" \
18192                 "$FSNAME.sys.jobid_var" $new_jobenv
18193 }
18194
18195 test_205a() { # Job stats
18196         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18197         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
18198                 skip "Need MDS version with at least 2.7.1"
18199         remote_mgs_nodsh && skip "remote MGS with nodsh"
18200         remote_mds_nodsh && skip "remote MDS with nodsh"
18201         remote_ost_nodsh && skip "remote OST with nodsh"
18202         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
18203                 skip "Server doesn't support jobstats"
18204         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
18205
18206         local old_jobenv=$($LCTL get_param -n jobid_var)
18207         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
18208
18209         if [[ $PERM_CMD == *"set_param -P"* ]]; then
18210                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
18211         else
18212                 stack_trap "do_facet mgs $PERM_CMD \
18213                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
18214         fi
18215         changelog_register
18216
18217         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
18218                                 mdt.*.job_cleanup_interval | head -n 1)
18219         local new_interval=5
18220         do_facet $SINGLEMDS \
18221                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
18222         stack_trap "do_facet $SINGLEMDS \
18223                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
18224         local start=$SECONDS
18225
18226         local cmd
18227         # mkdir
18228         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir"
18229         verify_jobstats "$cmd" "$SINGLEMDS"
18230         # rmdir
18231         cmd="rmdir $DIR/$tdir"
18232         verify_jobstats "$cmd" "$SINGLEMDS"
18233         # mkdir on secondary MDT
18234         if [ $MDSCOUNT -gt 1 ]; then
18235                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
18236                 verify_jobstats "$cmd" "mds2"
18237         fi
18238         # mknod
18239         cmd="mknod $DIR/$tfile c 1 3"
18240         verify_jobstats "$cmd" "$SINGLEMDS"
18241         # unlink
18242         cmd="rm -f $DIR/$tfile"
18243         verify_jobstats "$cmd" "$SINGLEMDS"
18244         # create all files on OST0000 so verify_jobstats can find OST stats
18245         # open & close
18246         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
18247         verify_jobstats "$cmd" "$SINGLEMDS"
18248         # setattr
18249         cmd="touch $DIR/$tfile"
18250         verify_jobstats "$cmd" "$SINGLEMDS ost1"
18251         # write
18252         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
18253         verify_jobstats "$cmd" "ost1"
18254         # read
18255         cancel_lru_locks osc
18256         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
18257         verify_jobstats "$cmd" "ost1"
18258         # truncate
18259         cmd="$TRUNCATE $DIR/$tfile 0"
18260         verify_jobstats "$cmd" "$SINGLEMDS ost1"
18261         # rename
18262         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
18263         verify_jobstats "$cmd" "$SINGLEMDS"
18264         # jobstats expiry - sleep until old stats should be expired
18265         local left=$((new_interval + 5 - (SECONDS - start)))
18266         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
18267                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
18268                         "0" $left
18269         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir.expire"
18270         verify_jobstats "$cmd" "$SINGLEMDS"
18271         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
18272             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
18273
18274         # Ensure that jobid are present in changelog (if supported by MDS)
18275         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
18276                 changelog_dump | tail -10
18277                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
18278                 [ $jobids -eq 9 ] ||
18279                         error "Wrong changelog jobid count $jobids != 9"
18280
18281                 # LU-5862
18282                 JOBENV="disable"
18283                 jobstats_set $JOBENV
18284                 touch $DIR/$tfile
18285                 changelog_dump | grep $tfile
18286                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
18287                 [ $jobids -eq 0 ] ||
18288                         error "Unexpected jobids when jobid_var=$JOBENV"
18289         fi
18290
18291         # test '%j' access to environment variable - if supported
18292         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"; then
18293                 JOBENV="JOBCOMPLEX"
18294                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18295
18296                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18297         fi
18298
18299         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%H.E"; then
18300                 JOBENV="JOBCOMPLEX"
18301                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname -s).E"
18302
18303                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18304         fi
18305
18306         # test '%j' access to per-session jobid - if supported
18307         if lctl list_param jobid_this_session > /dev/null 2>&1
18308         then
18309                 lctl set_param jobid_var=session jobid_name="S.%j.%e.%u.%h.E"
18310                 lctl set_param jobid_this_session=$USER
18311
18312                 JOBENV="JOBCOMPLEX"
18313                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18314
18315                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18316         fi
18317 }
18318 run_test 205a "Verify job stats"
18319
18320 # LU-13117, LU-13597
18321 test_205b() {
18322         (( $MDS1_VERSION >= $(version_code 2.13.54.91) )) ||
18323                 skip "Need MDS version at least 2.13.54.91"
18324
18325         job_stats="mdt.*.job_stats"
18326         $LCTL set_param $job_stats=clear
18327         # Setting jobid_var to USER might not be supported
18328         $LCTL set_param jobid_var=USER || true
18329         $LCTL set_param jobid_name="%e.%u"
18330         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
18331         do_facet $SINGLEMDS $LCTL get_param $job_stats |
18332                 grep "job_id:.*foolish" &&
18333                         error "Unexpected jobid found"
18334         do_facet $SINGLEMDS $LCTL get_param $job_stats |
18335                 grep "open:.*min.*max.*sum" ||
18336                         error "wrong job_stats format found"
18337 }
18338 run_test 205b "Verify job stats jobid and output format"
18339
18340 # LU-13733
18341 test_205c() {
18342         $LCTL set_param llite.*.stats=0
18343         dd if=/dev/zero of=$DIR/$tfile.1 bs=4k count=1
18344         $LCTL get_param llite.*.stats
18345         $LCTL get_param llite.*.stats | grep \
18346                 "write_bytes *1 samples \[bytes\] 4096 4096 4096 16777216" ||
18347                         error "wrong client stats format found"
18348 }
18349 run_test 205c "Verify client stats format"
18350
18351 # LU-1480, LU-1773 and LU-1657
18352 test_206() {
18353         mkdir -p $DIR/$tdir
18354         $LFS setstripe -c -1 $DIR/$tdir
18355 #define OBD_FAIL_LOV_INIT 0x1403
18356         $LCTL set_param fail_loc=0xa0001403
18357         $LCTL set_param fail_val=1
18358         touch $DIR/$tdir/$tfile || true
18359 }
18360 run_test 206 "fail lov_init_raid0() doesn't lbug"
18361
18362 test_207a() {
18363         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
18364         local fsz=`stat -c %s $DIR/$tfile`
18365         cancel_lru_locks mdc
18366
18367         # do not return layout in getattr intent
18368 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
18369         $LCTL set_param fail_loc=0x170
18370         local sz=`stat -c %s $DIR/$tfile`
18371
18372         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
18373
18374         rm -rf $DIR/$tfile
18375 }
18376 run_test 207a "can refresh layout at glimpse"
18377
18378 test_207b() {
18379         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
18380         local cksum=`md5sum $DIR/$tfile`
18381         local fsz=`stat -c %s $DIR/$tfile`
18382         cancel_lru_locks mdc
18383         cancel_lru_locks osc
18384
18385         # do not return layout in getattr intent
18386 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
18387         $LCTL set_param fail_loc=0x171
18388
18389         # it will refresh layout after the file is opened but before read issues
18390         echo checksum is "$cksum"
18391         echo "$cksum" |md5sum -c --quiet || error "file differs"
18392
18393         rm -rf $DIR/$tfile
18394 }
18395 run_test 207b "can refresh layout at open"
18396
18397 test_208() {
18398         # FIXME: in this test suite, only RD lease is used. This is okay
18399         # for now as only exclusive open is supported. After generic lease
18400         # is done, this test suite should be revised. - Jinshan
18401
18402         remote_mds_nodsh && skip "remote MDS with nodsh"
18403         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
18404                 skip "Need MDS version at least 2.4.52"
18405
18406         echo "==== test 1: verify get lease work"
18407         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
18408
18409         echo "==== test 2: verify lease can be broken by upcoming open"
18410         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
18411         local PID=$!
18412         sleep 1
18413
18414         $MULTIOP $DIR/$tfile oO_RDWR:c
18415         kill -USR1 $PID && wait $PID || error "break lease error"
18416
18417         echo "==== test 3: verify lease can't be granted if an open already exists"
18418         $MULTIOP $DIR/$tfile oO_RDWR:_c &
18419         local PID=$!
18420         sleep 1
18421
18422         $MULTIOP $DIR/$tfile oO_RDWR:eReUc && error "apply lease should fail"
18423         kill -USR1 $PID && wait $PID || error "open file error"
18424
18425         echo "==== test 4: lease can sustain over recovery"
18426         $MULTIOP $DIR/$tfile oO_RDWR:eR_E+eUc &
18427         PID=$!
18428         sleep 1
18429
18430         fail mds1
18431
18432         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
18433
18434         echo "==== test 5: lease broken can't be regained by replay"
18435         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
18436         PID=$!
18437         sleep 1
18438
18439         # open file to break lease and then recovery
18440         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
18441         fail mds1
18442
18443         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
18444
18445         rm -f $DIR/$tfile
18446 }
18447 run_test 208 "Exclusive open"
18448
18449 test_209() {
18450         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
18451                 skip_env "must have disp_stripe"
18452
18453         touch $DIR/$tfile
18454         sync; sleep 5; sync;
18455
18456         echo 3 > /proc/sys/vm/drop_caches
18457         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
18458                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
18459         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
18460
18461         # open/close 500 times
18462         for i in $(seq 500); do
18463                 cat $DIR/$tfile
18464         done
18465
18466         echo 3 > /proc/sys/vm/drop_caches
18467         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
18468                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
18469         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
18470
18471         echo "before: $req_before, after: $req_after"
18472         [ $((req_after - req_before)) -ge 300 ] &&
18473                 error "open/close requests are not freed"
18474         return 0
18475 }
18476 run_test 209 "read-only open/close requests should be freed promptly"
18477
18478 test_210() {
18479         local pid
18480
18481         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
18482         pid=$!
18483         sleep 1
18484
18485         $LFS getstripe $DIR/$tfile
18486         kill -USR1 $pid
18487         wait $pid || error "multiop failed"
18488
18489         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
18490         pid=$!
18491         sleep 1
18492
18493         $LFS getstripe $DIR/$tfile
18494         kill -USR1 $pid
18495         wait $pid || error "multiop failed"
18496 }
18497 run_test 210 "lfs getstripe does not break leases"
18498
18499 test_212() {
18500         size=`date +%s`
18501         size=$((size % 8192 + 1))
18502         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
18503         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
18504         rm -f $DIR/f212 $DIR/f212.xyz
18505 }
18506 run_test 212 "Sendfile test ============================================"
18507
18508 test_213() {
18509         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
18510         cancel_lru_locks osc
18511         lctl set_param fail_loc=0x8000040f
18512         # generate a read lock
18513         cat $DIR/$tfile > /dev/null
18514         # write to the file, it will try to cancel the above read lock.
18515         cat /etc/hosts >> $DIR/$tfile
18516 }
18517 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
18518
18519 test_214() { # for bug 20133
18520         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
18521         for (( i=0; i < 340; i++ )) ; do
18522                 touch $DIR/$tdir/d214c/a$i
18523         done
18524
18525         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
18526         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
18527         ls $DIR/d214c || error "ls $DIR/d214c failed"
18528         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
18529         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
18530 }
18531 run_test 214 "hash-indexed directory test - bug 20133"
18532
18533 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
18534 create_lnet_proc_files() {
18535         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
18536 }
18537
18538 # counterpart of create_lnet_proc_files
18539 remove_lnet_proc_files() {
18540         rm -f $TMP/lnet_$1.sys
18541 }
18542
18543 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
18544 # 3rd arg as regexp for body
18545 check_lnet_proc_stats() {
18546         local l=$(cat "$TMP/lnet_$1" |wc -l)
18547         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
18548
18549         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
18550 }
18551
18552 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
18553 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
18554 # optional and can be regexp for 2nd line (lnet.routes case)
18555 check_lnet_proc_entry() {
18556         local blp=2          # blp stands for 'position of 1st line of body'
18557         [ -z "$5" ] || blp=3 # lnet.routes case
18558
18559         local l=$(cat "$TMP/lnet_$1" |wc -l)
18560         # subtracting one from $blp because the body can be empty
18561         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
18562
18563         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
18564                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
18565
18566         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
18567                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
18568
18569         # bail out if any unexpected line happened
18570         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
18571         [ "$?" != 0 ] || error "$2 misformatted"
18572 }
18573
18574 test_215() { # for bugs 18102, 21079, 21517
18575         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18576
18577         local N='(0|[1-9][0-9]*)'       # non-negative numeric
18578         local P='[1-9][0-9]*'           # positive numeric
18579         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
18580         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
18581         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
18582         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
18583
18584         local L1 # regexp for 1st line
18585         local L2 # regexp for 2nd line (optional)
18586         local BR # regexp for the rest (body)
18587
18588         # lnet.stats should look as 11 space-separated non-negative numerics
18589         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
18590         create_lnet_proc_files "stats"
18591         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
18592         remove_lnet_proc_files "stats"
18593
18594         # lnet.routes should look like this:
18595         # Routing disabled/enabled
18596         # net hops priority state router
18597         # where net is a string like tcp0, hops > 0, priority >= 0,
18598         # state is up/down,
18599         # router is a string like 192.168.1.1@tcp2
18600         L1="^Routing (disabled|enabled)$"
18601         L2="^net +hops +priority +state +router$"
18602         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
18603         create_lnet_proc_files "routes"
18604         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
18605         remove_lnet_proc_files "routes"
18606
18607         # lnet.routers should look like this:
18608         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
18609         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
18610         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
18611         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
18612         L1="^ref +rtr_ref +alive +router$"
18613         BR="^$P +$P +(up|down) +$NID$"
18614         create_lnet_proc_files "routers"
18615         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
18616         remove_lnet_proc_files "routers"
18617
18618         # lnet.peers should look like this:
18619         # nid refs state last max rtr min tx min queue
18620         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
18621         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
18622         # numeric (0 or >0 or <0), queue >= 0.
18623         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
18624         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
18625         create_lnet_proc_files "peers"
18626         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
18627         remove_lnet_proc_files "peers"
18628
18629         # lnet.buffers  should look like this:
18630         # pages count credits min
18631         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
18632         L1="^pages +count +credits +min$"
18633         BR="^ +$N +$N +$I +$I$"
18634         create_lnet_proc_files "buffers"
18635         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
18636         remove_lnet_proc_files "buffers"
18637
18638         # lnet.nis should look like this:
18639         # nid status alive refs peer rtr max tx min
18640         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
18641         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
18642         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
18643         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
18644         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
18645         create_lnet_proc_files "nis"
18646         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
18647         remove_lnet_proc_files "nis"
18648
18649         # can we successfully write to lnet.stats?
18650         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
18651 }
18652 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
18653
18654 test_216() { # bug 20317
18655         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18656         remote_ost_nodsh && skip "remote OST with nodsh"
18657
18658         local node
18659         local facets=$(get_facets OST)
18660         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
18661
18662         save_lustre_params client "osc.*.contention_seconds" > $p
18663         save_lustre_params $facets \
18664                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
18665         save_lustre_params $facets \
18666                 "ldlm.namespaces.filter-*.contended_locks" >> $p
18667         save_lustre_params $facets \
18668                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
18669         clear_stats osc.*.osc_stats
18670
18671         # agressive lockless i/o settings
18672         do_nodes $(comma_list $(osts_nodes)) \
18673                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
18674                         ldlm.namespaces.filter-*.contended_locks=0 \
18675                         ldlm.namespaces.filter-*.contention_seconds=60"
18676         lctl set_param -n osc.*.contention_seconds=60
18677
18678         $DIRECTIO write $DIR/$tfile 0 10 4096
18679         $CHECKSTAT -s 40960 $DIR/$tfile
18680
18681         # disable lockless i/o
18682         do_nodes $(comma_list $(osts_nodes)) \
18683                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
18684                         ldlm.namespaces.filter-*.contended_locks=32 \
18685                         ldlm.namespaces.filter-*.contention_seconds=0"
18686         lctl set_param -n osc.*.contention_seconds=0
18687         clear_stats osc.*.osc_stats
18688
18689         dd if=/dev/zero of=$DIR/$tfile count=0
18690         $CHECKSTAT -s 0 $DIR/$tfile
18691
18692         restore_lustre_params <$p
18693         rm -f $p
18694         rm $DIR/$tfile
18695 }
18696 run_test 216 "check lockless direct write updates file size and kms correctly"
18697
18698 test_217() { # bug 22430
18699         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18700
18701         local node
18702         local nid
18703
18704         for node in $(nodes_list); do
18705                 nid=$(host_nids_address $node $NETTYPE)
18706                 if [[ $nid = *-* ]] ; then
18707                         echo "lctl ping $(h2nettype $nid)"
18708                         lctl ping $(h2nettype $nid)
18709                 else
18710                         echo "skipping $node (no hyphen detected)"
18711                 fi
18712         done
18713 }
18714 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
18715
18716 test_218() {
18717        # do directio so as not to populate the page cache
18718        log "creating a 10 Mb file"
18719        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
18720        log "starting reads"
18721        dd if=$DIR/$tfile of=/dev/null bs=4096 &
18722        log "truncating the file"
18723        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
18724        log "killing dd"
18725        kill %+ || true # reads might have finished
18726        echo "wait until dd is finished"
18727        wait
18728        log "removing the temporary file"
18729        rm -rf $DIR/$tfile || error "tmp file removal failed"
18730 }
18731 run_test 218 "parallel read and truncate should not deadlock"
18732
18733 test_219() {
18734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18735
18736         # write one partial page
18737         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
18738         # set no grant so vvp_io_commit_write will do sync write
18739         $LCTL set_param fail_loc=0x411
18740         # write a full page at the end of file
18741         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
18742
18743         $LCTL set_param fail_loc=0
18744         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
18745         $LCTL set_param fail_loc=0x411
18746         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
18747
18748         # LU-4201
18749         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
18750         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
18751 }
18752 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
18753
18754 test_220() { #LU-325
18755         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18756         remote_ost_nodsh && skip "remote OST with nodsh"
18757         remote_mds_nodsh && skip "remote MDS with nodsh"
18758         remote_mgs_nodsh && skip "remote MGS with nodsh"
18759
18760         local OSTIDX=0
18761
18762         # create on MDT0000 so the last_id and next_id are correct
18763         mkdir_on_mdt0 $DIR/$tdir
18764         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
18765         OST=${OST%_UUID}
18766
18767         # on the mdt's osc
18768         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
18769         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
18770                         osp.$mdtosc_proc1.prealloc_last_id)
18771         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
18772                         osp.$mdtosc_proc1.prealloc_next_id)
18773
18774         $LFS df -i
18775
18776         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
18777         #define OBD_FAIL_OST_ENOINO              0x229
18778         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
18779         create_pool $FSNAME.$TESTNAME || return 1
18780         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
18781
18782         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
18783
18784         MDSOBJS=$((last_id - next_id))
18785         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
18786
18787         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
18788         echo "OST still has $count kbytes free"
18789
18790         echo "create $MDSOBJS files @next_id..."
18791         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
18792
18793         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
18794                         osp.$mdtosc_proc1.prealloc_last_id)
18795         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
18796                         osp.$mdtosc_proc1.prealloc_next_id)
18797
18798         echo "after creation, last_id=$last_id2, next_id=$next_id2"
18799         $LFS df -i
18800
18801         echo "cleanup..."
18802
18803         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
18804         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
18805
18806         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
18807                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
18808         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
18809                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
18810         echo "unlink $MDSOBJS files @$next_id..."
18811         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
18812 }
18813 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
18814
18815 test_221() {
18816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18817
18818         dd if=`which date` of=$MOUNT/date oflag=sync
18819         chmod +x $MOUNT/date
18820
18821         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
18822         $LCTL set_param fail_loc=0x80001401
18823
18824         $MOUNT/date > /dev/null
18825         rm -f $MOUNT/date
18826 }
18827 run_test 221 "make sure fault and truncate race to not cause OOM"
18828
18829 test_222a () {
18830         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18831
18832         rm -rf $DIR/$tdir
18833         test_mkdir $DIR/$tdir
18834         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18835         createmany -o $DIR/$tdir/$tfile 10
18836         cancel_lru_locks mdc
18837         cancel_lru_locks osc
18838         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
18839         $LCTL set_param fail_loc=0x31a
18840         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
18841         $LCTL set_param fail_loc=0
18842         rm -r $DIR/$tdir
18843 }
18844 run_test 222a "AGL for ls should not trigger CLIO lock failure"
18845
18846 test_222b () {
18847         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18848
18849         rm -rf $DIR/$tdir
18850         test_mkdir $DIR/$tdir
18851         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18852         createmany -o $DIR/$tdir/$tfile 10
18853         cancel_lru_locks mdc
18854         cancel_lru_locks osc
18855         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
18856         $LCTL set_param fail_loc=0x31a
18857         rm -r $DIR/$tdir || error "AGL for rmdir failed"
18858         $LCTL set_param fail_loc=0
18859 }
18860 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
18861
18862 test_223 () {
18863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18864
18865         rm -rf $DIR/$tdir
18866         test_mkdir $DIR/$tdir
18867         $LFS setstripe -c 1 -i 0 $DIR/$tdir
18868         createmany -o $DIR/$tdir/$tfile 10
18869         cancel_lru_locks mdc
18870         cancel_lru_locks osc
18871         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
18872         $LCTL set_param fail_loc=0x31b
18873         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
18874         $LCTL set_param fail_loc=0
18875         rm -r $DIR/$tdir
18876 }
18877 run_test 223 "osc reenqueue if without AGL lock granted ======================="
18878
18879 test_224a() { # LU-1039, MRP-303
18880         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18881         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
18882         $LCTL set_param fail_loc=0x508
18883         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 conv=fsync
18884         $LCTL set_param fail_loc=0
18885         df $DIR
18886 }
18887 run_test 224a "Don't panic on bulk IO failure"
18888
18889 test_224bd_sub() { # LU-1039, MRP-303
18890         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18891         local timeout=$1
18892
18893         shift
18894         dd if=/dev/urandom of=$TMP/$tfile bs=1M count=1
18895
18896         $LFS setstripe -c 1 -i 0 $DIR/$tfile
18897
18898         dd if=$TMP/$tfile of=$DIR/$tfile bs=1M count=1
18899         cancel_lru_locks osc
18900         set_checksums 0
18901         stack_trap "set_checksums $ORIG_CSUM" EXIT
18902         local at_max_saved=0
18903
18904         # adaptive timeouts may prevent seeing the issue
18905         if at_is_enabled; then
18906                 at_max_saved=$(at_max_get mds)
18907                 at_max_set 0 mds client
18908                 stack_trap "at_max_set $at_max_saved mds client" EXIT
18909         fi
18910
18911         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
18912         do_facet ost1 $LCTL set_param fail_val=$timeout fail_loc=0x80000515
18913         dd of=$TMP/$tfile.new if=$DIR/$tfile bs=1M count=1 || "$@"
18914
18915         do_facet ost1 $LCTL set_param fail_loc=0
18916         cmp $TMP/$tfile $TMP/$tfile.new || error "file contents wrong"
18917         df $DIR
18918 }
18919
18920 test_224b() {
18921         test_224bd_sub 3 error "dd failed"
18922 }
18923 run_test 224b "Don't panic on bulk IO failure"
18924
18925 test_224c() { # LU-6441
18926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18927         remote_mds_nodsh && skip "remote MDS with nodsh"
18928
18929         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
18930         save_writethrough $p
18931         set_cache writethrough on
18932
18933         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
18934         local at_max=$($LCTL get_param -n at_max)
18935         local timeout=$($LCTL get_param -n timeout)
18936         local test_at="at_max"
18937         local param_at="$FSNAME.sys.at_max"
18938         local test_timeout="timeout"
18939         local param_timeout="$FSNAME.sys.timeout"
18940
18941         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
18942
18943         set_persistent_param_and_check client "$test_at" "$param_at" 0
18944         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
18945
18946         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
18947         do_facet ost1 "$LCTL set_param fail_loc=0x520"
18948         $LFS setstripe -c 1 -i 0 $DIR/$tfile
18949         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
18950         sync
18951         do_facet ost1 "$LCTL set_param fail_loc=0"
18952
18953         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
18954         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
18955                 $timeout
18956
18957         $LCTL set_param -n $pages_per_rpc
18958         restore_lustre_params < $p
18959         rm -f $p
18960 }
18961 run_test 224c "Don't hang if one of md lost during large bulk RPC"
18962
18963 test_224d() { # LU-11169
18964         test_224bd_sub $((TIMEOUT + 2)) error "dd failed"
18965 }
18966 run_test 224d "Don't corrupt data on bulk IO timeout"
18967
18968 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
18969 test_225a () {
18970         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18971         if [ -z ${MDSSURVEY} ]; then
18972                 skip_env "mds-survey not found"
18973         fi
18974         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
18975                 skip "Need MDS version at least 2.2.51"
18976
18977         local mds=$(facet_host $SINGLEMDS)
18978         local target=$(do_nodes $mds 'lctl dl' |
18979                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
18980
18981         local cmd1="file_count=1000 thrhi=4"
18982         local cmd2="dir_count=2 layer=mdd stripe_count=0"
18983         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
18984         local cmd="$cmd1 $cmd2 $cmd3"
18985
18986         rm -f ${TMP}/mds_survey*
18987         echo + $cmd
18988         eval $cmd || error "mds-survey with zero-stripe failed"
18989         cat ${TMP}/mds_survey*
18990         rm -f ${TMP}/mds_survey*
18991 }
18992 run_test 225a "Metadata survey sanity with zero-stripe"
18993
18994 test_225b () {
18995         if [ -z ${MDSSURVEY} ]; then
18996                 skip_env "mds-survey not found"
18997         fi
18998         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
18999                 skip "Need MDS version at least 2.2.51"
19000         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19001         remote_mds_nodsh && skip "remote MDS with nodsh"
19002         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
19003                 skip_env "Need to mount OST to test"
19004         fi
19005
19006         local mds=$(facet_host $SINGLEMDS)
19007         local target=$(do_nodes $mds 'lctl dl' |
19008                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
19009
19010         local cmd1="file_count=1000 thrhi=4"
19011         local cmd2="dir_count=2 layer=mdd stripe_count=1"
19012         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
19013         local cmd="$cmd1 $cmd2 $cmd3"
19014
19015         rm -f ${TMP}/mds_survey*
19016         echo + $cmd
19017         eval $cmd || error "mds-survey with stripe_count failed"
19018         cat ${TMP}/mds_survey*
19019         rm -f ${TMP}/mds_survey*
19020 }
19021 run_test 225b "Metadata survey sanity with stripe_count = 1"
19022
19023 mcreate_path2fid () {
19024         local mode=$1
19025         local major=$2
19026         local minor=$3
19027         local name=$4
19028         local desc=$5
19029         local path=$DIR/$tdir/$name
19030         local fid
19031         local rc
19032         local fid_path
19033
19034         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
19035                 error "cannot create $desc"
19036
19037         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
19038         rc=$?
19039         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
19040
19041         fid_path=$($LFS fid2path $MOUNT $fid)
19042         rc=$?
19043         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
19044
19045         [ "$path" == "$fid_path" ] ||
19046                 error "fid2path returned $fid_path, expected $path"
19047
19048         echo "pass with $path and $fid"
19049 }
19050
19051 test_226a () {
19052         rm -rf $DIR/$tdir
19053         mkdir -p $DIR/$tdir
19054
19055         mcreate_path2fid 0010666 0 0 fifo "FIFO"
19056         mcreate_path2fid 0020666 1 3 null "character special file (null)"
19057         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
19058         mcreate_path2fid 0040666 0 0 dir "directory"
19059         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
19060         mcreate_path2fid 0100666 0 0 file "regular file"
19061         mcreate_path2fid 0120666 0 0 link "symbolic link"
19062         mcreate_path2fid 0140666 0 0 sock "socket"
19063 }
19064 run_test 226a "call path2fid and fid2path on files of all type"
19065
19066 test_226b () {
19067         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19068
19069         local MDTIDX=1
19070
19071         rm -rf $DIR/$tdir
19072         mkdir -p $DIR/$tdir
19073         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
19074                 error "create remote directory failed"
19075         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
19076         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
19077                                 "character special file (null)"
19078         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
19079                                 "character special file (no device)"
19080         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
19081         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
19082                                 "block special file (loop)"
19083         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
19084         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
19085         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
19086 }
19087 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
19088
19089 test_226c () {
19090         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19091         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
19092                 skip "Need MDS version at least 2.13.55"
19093
19094         local submnt=/mnt/submnt
19095         local srcfile=/etc/passwd
19096         local dstfile=$submnt/passwd
19097         local path
19098         local fid
19099
19100         rm -rf $DIR/$tdir
19101         rm -rf $submnt
19102         $LFS setdirstripe -c -1 -i 1 $DIR/$tdir ||
19103                 error "create remote directory failed"
19104         mkdir -p $submnt || error "create $submnt failed"
19105         $MOUNT_CMD $MGSNID:/$FSNAME/$tdir $submnt ||
19106                 error "mount $submnt failed"
19107         stack_trap "umount $submnt" EXIT
19108
19109         cp $srcfile $dstfile
19110         fid=$($LFS path2fid $dstfile)
19111         path=$($LFS fid2path $submnt "$fid")
19112         [ "$path" = "$dstfile" ] ||
19113                 error "fid2path $submnt $fid failed ($path != $dstfile)"
19114 }
19115 run_test 226c "call path2fid and fid2path under remote dir with subdir mount"
19116
19117 # LU-1299 Executing or running ldd on a truncated executable does not
19118 # cause an out-of-memory condition.
19119 test_227() {
19120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19121         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
19122
19123         dd if=$(which date) of=$MOUNT/date bs=1k count=1
19124         chmod +x $MOUNT/date
19125
19126         $MOUNT/date > /dev/null
19127         ldd $MOUNT/date > /dev/null
19128         rm -f $MOUNT/date
19129 }
19130 run_test 227 "running truncated executable does not cause OOM"
19131
19132 # LU-1512 try to reuse idle OI blocks
19133 test_228a() {
19134         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19135         remote_mds_nodsh && skip "remote MDS with nodsh"
19136         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19137
19138         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19139         local myDIR=$DIR/$tdir
19140
19141         mkdir -p $myDIR
19142         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19143         $LCTL set_param fail_loc=0x80001002
19144         createmany -o $myDIR/t- 10000
19145         $LCTL set_param fail_loc=0
19146         # The guard is current the largest FID holder
19147         touch $myDIR/guard
19148         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19149                     tr -d '[')
19150         local IDX=$(($SEQ % 64))
19151
19152         do_facet $SINGLEMDS sync
19153         # Make sure journal flushed.
19154         sleep 6
19155         local blk1=$(do_facet $SINGLEMDS \
19156                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19157                      grep Blockcount | awk '{print $4}')
19158
19159         # Remove old files, some OI blocks will become idle.
19160         unlinkmany $myDIR/t- 10000
19161         # Create new files, idle OI blocks should be reused.
19162         createmany -o $myDIR/t- 2000
19163         do_facet $SINGLEMDS sync
19164         # Make sure journal flushed.
19165         sleep 6
19166         local blk2=$(do_facet $SINGLEMDS \
19167                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19168                      grep Blockcount | awk '{print $4}')
19169
19170         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19171 }
19172 run_test 228a "try to reuse idle OI blocks"
19173
19174 test_228b() {
19175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19176         remote_mds_nodsh && skip "remote MDS with nodsh"
19177         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19178
19179         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19180         local myDIR=$DIR/$tdir
19181
19182         mkdir -p $myDIR
19183         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19184         $LCTL set_param fail_loc=0x80001002
19185         createmany -o $myDIR/t- 10000
19186         $LCTL set_param fail_loc=0
19187         # The guard is current the largest FID holder
19188         touch $myDIR/guard
19189         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19190                     tr -d '[')
19191         local IDX=$(($SEQ % 64))
19192
19193         do_facet $SINGLEMDS sync
19194         # Make sure journal flushed.
19195         sleep 6
19196         local blk1=$(do_facet $SINGLEMDS \
19197                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19198                      grep Blockcount | awk '{print $4}')
19199
19200         # Remove old files, some OI blocks will become idle.
19201         unlinkmany $myDIR/t- 10000
19202
19203         # stop the MDT
19204         stop $SINGLEMDS || error "Fail to stop MDT."
19205         # remount the MDT
19206         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
19207
19208         df $MOUNT || error "Fail to df."
19209         # Create new files, idle OI blocks should be reused.
19210         createmany -o $myDIR/t- 2000
19211         do_facet $SINGLEMDS sync
19212         # Make sure journal flushed.
19213         sleep 6
19214         local blk2=$(do_facet $SINGLEMDS \
19215                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19216                      grep Blockcount | awk '{print $4}')
19217
19218         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19219 }
19220 run_test 228b "idle OI blocks can be reused after MDT restart"
19221
19222 #LU-1881
19223 test_228c() {
19224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19225         remote_mds_nodsh && skip "remote MDS with nodsh"
19226         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19227
19228         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19229         local myDIR=$DIR/$tdir
19230
19231         mkdir -p $myDIR
19232         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19233         $LCTL set_param fail_loc=0x80001002
19234         # 20000 files can guarantee there are index nodes in the OI file
19235         createmany -o $myDIR/t- 20000
19236         $LCTL set_param fail_loc=0
19237         # The guard is current the largest FID holder
19238         touch $myDIR/guard
19239         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19240                     tr -d '[')
19241         local IDX=$(($SEQ % 64))
19242
19243         do_facet $SINGLEMDS sync
19244         # Make sure journal flushed.
19245         sleep 6
19246         local blk1=$(do_facet $SINGLEMDS \
19247                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19248                      grep Blockcount | awk '{print $4}')
19249
19250         # Remove old files, some OI blocks will become idle.
19251         unlinkmany $myDIR/t- 20000
19252         rm -f $myDIR/guard
19253         # The OI file should become empty now
19254
19255         # Create new files, idle OI blocks should be reused.
19256         createmany -o $myDIR/t- 2000
19257         do_facet $SINGLEMDS sync
19258         # Make sure journal flushed.
19259         sleep 6
19260         local blk2=$(do_facet $SINGLEMDS \
19261                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19262                      grep Blockcount | awk '{print $4}')
19263
19264         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19265 }
19266 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
19267
19268 test_229() { # LU-2482, LU-3448
19269         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19270         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19271         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
19272                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
19273
19274         rm -f $DIR/$tfile
19275
19276         # Create a file with a released layout and stripe count 2.
19277         $MULTIOP $DIR/$tfile H2c ||
19278                 error "failed to create file with released layout"
19279
19280         $LFS getstripe -v $DIR/$tfile
19281
19282         local pattern=$($LFS getstripe -L $DIR/$tfile)
19283         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
19284
19285         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
19286                 error "getstripe"
19287         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
19288         stat $DIR/$tfile || error "failed to stat released file"
19289
19290         chown $RUNAS_ID $DIR/$tfile ||
19291                 error "chown $RUNAS_ID $DIR/$tfile failed"
19292
19293         chgrp $RUNAS_ID $DIR/$tfile ||
19294                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
19295
19296         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
19297         rm $DIR/$tfile || error "failed to remove released file"
19298 }
19299 run_test 229 "getstripe/stat/rm/attr changes work on released files"
19300
19301 test_230a() {
19302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19303         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19304         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19305                 skip "Need MDS version at least 2.11.52"
19306
19307         local MDTIDX=1
19308
19309         test_mkdir $DIR/$tdir
19310         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
19311         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
19312         [ $mdt_idx -ne 0 ] &&
19313                 error "create local directory on wrong MDT $mdt_idx"
19314
19315         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
19316                         error "create remote directory failed"
19317         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
19318         [ $mdt_idx -ne $MDTIDX ] &&
19319                 error "create remote directory on wrong MDT $mdt_idx"
19320
19321         createmany -o $DIR/$tdir/test_230/t- 10 ||
19322                 error "create files on remote directory failed"
19323         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
19324         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
19325         rm -r $DIR/$tdir || error "unlink remote directory failed"
19326 }
19327 run_test 230a "Create remote directory and files under the remote directory"
19328
19329 test_230b() {
19330         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19331         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19332         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19333                 skip "Need MDS version at least 2.11.52"
19334
19335         local MDTIDX=1
19336         local mdt_index
19337         local i
19338         local file
19339         local pid
19340         local stripe_count
19341         local migrate_dir=$DIR/$tdir/migrate_dir
19342         local other_dir=$DIR/$tdir/other_dir
19343
19344         test_mkdir $DIR/$tdir
19345         test_mkdir -i0 -c1 $migrate_dir
19346         test_mkdir -i0 -c1 $other_dir
19347         for ((i=0; i<10; i++)); do
19348                 mkdir -p $migrate_dir/dir_${i}
19349                 createmany -o $migrate_dir/dir_${i}/f 10 ||
19350                         error "create files under remote dir failed $i"
19351         done
19352
19353         cp /etc/passwd $migrate_dir/$tfile
19354         cp /etc/passwd $other_dir/$tfile
19355         chattr +SAD $migrate_dir
19356         chattr +SAD $migrate_dir/$tfile
19357
19358         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19359         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19360         local old_dir_mode=$(stat -c%f $migrate_dir)
19361         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
19362
19363         mkdir -p $migrate_dir/dir_default_stripe2
19364         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
19365         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
19366
19367         mkdir -p $other_dir
19368         ln $migrate_dir/$tfile $other_dir/luna
19369         ln $migrate_dir/$tfile $migrate_dir/sofia
19370         ln $other_dir/$tfile $migrate_dir/david
19371         ln -s $migrate_dir/$tfile $other_dir/zachary
19372         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
19373         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
19374
19375         local len
19376         local lnktgt
19377
19378         # inline symlink
19379         for len in 58 59 60; do
19380                 lnktgt=$(str_repeat 'l' $len)
19381                 touch $migrate_dir/$lnktgt
19382                 ln -s $lnktgt $migrate_dir/${len}char_ln
19383         done
19384
19385         # PATH_MAX
19386         for len in 4094 4095; do
19387                 lnktgt=$(str_repeat 'l' $len)
19388                 ln -s $lnktgt $migrate_dir/${len}char_ln
19389         done
19390
19391         # NAME_MAX
19392         for len in 254 255; do
19393                 touch $migrate_dir/$(str_repeat 'l' $len)
19394         done
19395
19396         $LFS migrate -m $MDTIDX $migrate_dir ||
19397                 error "fails on migrating remote dir to MDT1"
19398
19399         echo "migratate to MDT1, then checking.."
19400         for ((i = 0; i < 10; i++)); do
19401                 for file in $(find $migrate_dir/dir_${i}); do
19402                         mdt_index=$($LFS getstripe -m $file)
19403                         # broken symlink getstripe will fail
19404                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
19405                                 error "$file is not on MDT${MDTIDX}"
19406                 done
19407         done
19408
19409         # the multiple link file should still in MDT0
19410         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
19411         [ $mdt_index == 0 ] ||
19412                 error "$file is not on MDT${MDTIDX}"
19413
19414         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19415         [ "$old_dir_flag" = "$new_dir_flag" ] ||
19416                 error " expect $old_dir_flag get $new_dir_flag"
19417
19418         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19419         [ "$old_file_flag" = "$new_file_flag" ] ||
19420                 error " expect $old_file_flag get $new_file_flag"
19421
19422         local new_dir_mode=$(stat -c%f $migrate_dir)
19423         [ "$old_dir_mode" = "$new_dir_mode" ] ||
19424                 error "expect mode $old_dir_mode get $new_dir_mode"
19425
19426         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
19427         [ "$old_file_mode" = "$new_file_mode" ] ||
19428                 error "expect mode $old_file_mode get $new_file_mode"
19429
19430         diff /etc/passwd $migrate_dir/$tfile ||
19431                 error "$tfile different after migration"
19432
19433         diff /etc/passwd $other_dir/luna ||
19434                 error "luna different after migration"
19435
19436         diff /etc/passwd $migrate_dir/sofia ||
19437                 error "sofia different after migration"
19438
19439         diff /etc/passwd $migrate_dir/david ||
19440                 error "david different after migration"
19441
19442         diff /etc/passwd $other_dir/zachary ||
19443                 error "zachary different after migration"
19444
19445         diff /etc/passwd $migrate_dir/${tfile}_ln ||
19446                 error "${tfile}_ln different after migration"
19447
19448         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
19449                 error "${tfile}_ln_other different after migration"
19450
19451         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
19452         [ $stripe_count = 2 ] ||
19453                 error "dir strpe_count $d != 2 after migration."
19454
19455         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
19456         [ $stripe_count = 2 ] ||
19457                 error "file strpe_count $d != 2 after migration."
19458
19459         #migrate back to MDT0
19460         MDTIDX=0
19461
19462         $LFS migrate -m $MDTIDX $migrate_dir ||
19463                 error "fails on migrating remote dir to MDT0"
19464
19465         echo "migrate back to MDT0, checking.."
19466         for file in $(find $migrate_dir); do
19467                 mdt_index=$($LFS getstripe -m $file)
19468                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
19469                         error "$file is not on MDT${MDTIDX}"
19470         done
19471
19472         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
19473         [ "$old_dir_flag" = "$new_dir_flag" ] ||
19474                 error " expect $old_dir_flag get $new_dir_flag"
19475
19476         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
19477         [ "$old_file_flag" = "$new_file_flag" ] ||
19478                 error " expect $old_file_flag get $new_file_flag"
19479
19480         local new_dir_mode=$(stat -c%f $migrate_dir)
19481         [ "$old_dir_mode" = "$new_dir_mode" ] ||
19482                 error "expect mode $old_dir_mode get $new_dir_mode"
19483
19484         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
19485         [ "$old_file_mode" = "$new_file_mode" ] ||
19486                 error "expect mode $old_file_mode get $new_file_mode"
19487
19488         diff /etc/passwd ${migrate_dir}/$tfile ||
19489                 error "$tfile different after migration"
19490
19491         diff /etc/passwd ${other_dir}/luna ||
19492                 error "luna different after migration"
19493
19494         diff /etc/passwd ${migrate_dir}/sofia ||
19495                 error "sofia different after migration"
19496
19497         diff /etc/passwd ${other_dir}/zachary ||
19498                 error "zachary different after migration"
19499
19500         diff /etc/passwd $migrate_dir/${tfile}_ln ||
19501                 error "${tfile}_ln different after migration"
19502
19503         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
19504                 error "${tfile}_ln_other different after migration"
19505
19506         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
19507         [ $stripe_count = 2 ] ||
19508                 error "dir strpe_count $d != 2 after migration."
19509
19510         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
19511         [ $stripe_count = 2 ] ||
19512                 error "file strpe_count $d != 2 after migration."
19513
19514         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19515 }
19516 run_test 230b "migrate directory"
19517
19518 test_230c() {
19519         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19520         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19521         remote_mds_nodsh && skip "remote MDS with nodsh"
19522         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19523                 skip "Need MDS version at least 2.11.52"
19524
19525         local MDTIDX=1
19526         local total=3
19527         local mdt_index
19528         local file
19529         local migrate_dir=$DIR/$tdir/migrate_dir
19530
19531         #If migrating directory fails in the middle, all entries of
19532         #the directory is still accessiable.
19533         test_mkdir $DIR/$tdir
19534         test_mkdir -i0 -c1 $migrate_dir
19535         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
19536         stat $migrate_dir
19537         createmany -o $migrate_dir/f $total ||
19538                 error "create files under ${migrate_dir} failed"
19539
19540         # fail after migrating top dir, and this will fail only once, so the
19541         # first sub file migration will fail (currently f3), others succeed.
19542         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
19543         do_facet mds1 lctl set_param fail_loc=0x1801
19544         local t=$(ls $migrate_dir | wc -l)
19545         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
19546                 error "migrate should fail"
19547         local u=$(ls $migrate_dir | wc -l)
19548         [ "$u" == "$t" ] || error "$u != $t during migration"
19549
19550         # add new dir/file should succeed
19551         mkdir $migrate_dir/dir ||
19552                 error "mkdir failed under migrating directory"
19553         touch $migrate_dir/file ||
19554                 error "create file failed under migrating directory"
19555
19556         # add file with existing name should fail
19557         for file in $migrate_dir/f*; do
19558                 stat $file > /dev/null || error "stat $file failed"
19559                 $OPENFILE -f O_CREAT:O_EXCL $file &&
19560                         error "open(O_CREAT|O_EXCL) $file should fail"
19561                 $MULTIOP $file m && error "create $file should fail"
19562                 touch $DIR/$tdir/remote_dir/$tfile ||
19563                         error "touch $tfile failed"
19564                 ln $DIR/$tdir/remote_dir/$tfile $file &&
19565                         error "link $file should fail"
19566                 mdt_index=$($LFS getstripe -m $file)
19567                 if [ $mdt_index == 0 ]; then
19568                         # file failed to migrate is not allowed to rename to
19569                         mv $DIR/$tdir/remote_dir/$tfile $file &&
19570                                 error "rename to $file should fail"
19571                 else
19572                         mv $DIR/$tdir/remote_dir/$tfile $file ||
19573                                 error "rename to $file failed"
19574                 fi
19575                 echo hello >> $file || error "write $file failed"
19576         done
19577
19578         # resume migration with different options should fail
19579         $LFS migrate -m 0 $migrate_dir &&
19580                 error "migrate -m 0 $migrate_dir should fail"
19581
19582         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
19583                 error "migrate -c 2 $migrate_dir should fail"
19584
19585         # resume migration should succeed
19586         $LFS migrate -m $MDTIDX $migrate_dir ||
19587                 error "migrate $migrate_dir failed"
19588
19589         echo "Finish migration, then checking.."
19590         for file in $(find $migrate_dir); do
19591                 mdt_index=$($LFS getstripe -m $file)
19592                 [ $mdt_index == $MDTIDX ] ||
19593                         error "$file is not on MDT${MDTIDX}"
19594         done
19595
19596         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19597 }
19598 run_test 230c "check directory accessiblity if migration failed"
19599
19600 test_230d() {
19601         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19602         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19603         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19604                 skip "Need MDS version at least 2.11.52"
19605         # LU-11235
19606         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
19607
19608         local migrate_dir=$DIR/$tdir/migrate_dir
19609         local old_index
19610         local new_index
19611         local old_count
19612         local new_count
19613         local new_hash
19614         local mdt_index
19615         local i
19616         local j
19617
19618         old_index=$((RANDOM % MDSCOUNT))
19619         old_count=$((MDSCOUNT - old_index))
19620         new_index=$((RANDOM % MDSCOUNT))
19621         new_count=$((MDSCOUNT - new_index))
19622         new_hash=1 # for all_char
19623
19624         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
19625         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
19626
19627         test_mkdir $DIR/$tdir
19628         test_mkdir -i $old_index -c $old_count $migrate_dir
19629
19630         for ((i=0; i<100; i++)); do
19631                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
19632                 createmany -o $migrate_dir/dir_${i}/f 100 ||
19633                         error "create files under remote dir failed $i"
19634         done
19635
19636         echo -n "Migrate from MDT$old_index "
19637         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
19638         echo -n "to MDT$new_index"
19639         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
19640         echo
19641
19642         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
19643         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
19644                 error "migrate remote dir error"
19645
19646         echo "Finish migration, then checking.."
19647         for file in $(find $migrate_dir); do
19648                 mdt_index=$($LFS getstripe -m $file)
19649                 if [ $mdt_index -lt $new_index ] ||
19650                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
19651                         error "$file is on MDT$mdt_index"
19652                 fi
19653         done
19654
19655         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19656 }
19657 run_test 230d "check migrate big directory"
19658
19659 test_230e() {
19660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19661         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19662         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19663                 skip "Need MDS version at least 2.11.52"
19664
19665         local i
19666         local j
19667         local a_fid
19668         local b_fid
19669
19670         mkdir_on_mdt0 $DIR/$tdir
19671         mkdir $DIR/$tdir/migrate_dir
19672         mkdir $DIR/$tdir/other_dir
19673         touch $DIR/$tdir/migrate_dir/a
19674         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
19675         ls $DIR/$tdir/other_dir
19676
19677         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19678                 error "migrate dir fails"
19679
19680         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
19681         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
19682
19683         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19684         [ $mdt_index == 0 ] || error "a is not on MDT0"
19685
19686         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
19687                 error "migrate dir fails"
19688
19689         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
19690         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
19691
19692         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19693         [ $mdt_index == 1 ] || error "a is not on MDT1"
19694
19695         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
19696         [ $mdt_index == 1 ] || error "b is not on MDT1"
19697
19698         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
19699         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
19700
19701         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
19702
19703         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19704 }
19705 run_test 230e "migrate mulitple local link files"
19706
19707 test_230f() {
19708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19709         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19710         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19711                 skip "Need MDS version at least 2.11.52"
19712
19713         local a_fid
19714         local ln_fid
19715
19716         mkdir -p $DIR/$tdir
19717         mkdir $DIR/$tdir/migrate_dir
19718         $LFS mkdir -i1 $DIR/$tdir/other_dir
19719         touch $DIR/$tdir/migrate_dir/a
19720         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
19721         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
19722         ls $DIR/$tdir/other_dir
19723
19724         # a should be migrated to MDT1, since no other links on MDT0
19725         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19726                 error "#1 migrate dir fails"
19727         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
19728         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
19729         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19730         [ $mdt_index == 1 ] || error "a is not on MDT1"
19731
19732         # a should stay on MDT1, because it is a mulitple link file
19733         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
19734                 error "#2 migrate dir fails"
19735         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19736         [ $mdt_index == 1 ] || error "a is not on MDT1"
19737
19738         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
19739                 error "#3 migrate dir fails"
19740
19741         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
19742         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
19743         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
19744
19745         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
19746         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
19747
19748         # a should be migrated to MDT0, since no other links on MDT1
19749         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
19750                 error "#4 migrate dir fails"
19751         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
19752         [ $mdt_index == 0 ] || error "a is not on MDT0"
19753
19754         rm -rf $DIR/$tdir || error "rm dir failed after migration"
19755 }
19756 run_test 230f "migrate mulitple remote link files"
19757
19758 test_230g() {
19759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19760         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19761         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19762                 skip "Need MDS version at least 2.11.52"
19763
19764         mkdir -p $DIR/$tdir/migrate_dir
19765
19766         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
19767                 error "migrating dir to non-exist MDT succeeds"
19768         true
19769 }
19770 run_test 230g "migrate dir to non-exist MDT"
19771
19772 test_230h() {
19773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19774         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19775         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19776                 skip "Need MDS version at least 2.11.52"
19777
19778         local mdt_index
19779
19780         mkdir -p $DIR/$tdir/migrate_dir
19781
19782         $LFS migrate -m1 $DIR &&
19783                 error "migrating mountpoint1 should fail"
19784
19785         $LFS migrate -m1 $DIR/$tdir/.. &&
19786                 error "migrating mountpoint2 should fail"
19787
19788         # same as mv
19789         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
19790                 error "migrating $tdir/migrate_dir/.. should fail"
19791
19792         true
19793 }
19794 run_test 230h "migrate .. and root"
19795
19796 test_230i() {
19797         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19798         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19799         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19800                 skip "Need MDS version at least 2.11.52"
19801
19802         mkdir -p $DIR/$tdir/migrate_dir
19803
19804         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
19805                 error "migration fails with a tailing slash"
19806
19807         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
19808                 error "migration fails with two tailing slashes"
19809 }
19810 run_test 230i "lfs migrate -m tolerates trailing slashes"
19811
19812 test_230j() {
19813         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19814         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
19815                 skip "Need MDS version at least 2.11.52"
19816
19817         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
19818         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
19819                 error "create $tfile failed"
19820         cat /etc/passwd > $DIR/$tdir/$tfile
19821
19822         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
19823
19824         cmp /etc/passwd $DIR/$tdir/$tfile ||
19825                 error "DoM file mismatch after migration"
19826 }
19827 run_test 230j "DoM file data not changed after dir migration"
19828
19829 test_230k() {
19830         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
19831         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19832                 skip "Need MDS version at least 2.11.56"
19833
19834         local total=20
19835         local files_on_starting_mdt=0
19836
19837         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
19838         $LFS getdirstripe $DIR/$tdir
19839         for i in $(seq $total); do
19840                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
19841                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
19842                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19843         done
19844
19845         echo "$files_on_starting_mdt files on MDT0"
19846
19847         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
19848         $LFS getdirstripe $DIR/$tdir
19849
19850         files_on_starting_mdt=0
19851         for i in $(seq $total); do
19852                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
19853                         error "file $tfile.$i mismatch after migration"
19854                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
19855                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19856         done
19857
19858         echo "$files_on_starting_mdt files on MDT1 after migration"
19859         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
19860
19861         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
19862         $LFS getdirstripe $DIR/$tdir
19863
19864         files_on_starting_mdt=0
19865         for i in $(seq $total); do
19866                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
19867                         error "file $tfile.$i mismatch after 2nd migration"
19868                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
19869                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
19870         done
19871
19872         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
19873         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
19874
19875         true
19876 }
19877 run_test 230k "file data not changed after dir migration"
19878
19879 test_230l() {
19880         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19881         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19882                 skip "Need MDS version at least 2.11.56"
19883
19884         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
19885         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
19886                 error "create files under remote dir failed $i"
19887         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
19888 }
19889 run_test 230l "readdir between MDTs won't crash"
19890
19891 test_230m() {
19892         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19893         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
19894                 skip "Need MDS version at least 2.11.56"
19895
19896         local MDTIDX=1
19897         local mig_dir=$DIR/$tdir/migrate_dir
19898         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
19899         local shortstr="b"
19900         local val
19901
19902         echo "Creating files and dirs with xattrs"
19903         test_mkdir $DIR/$tdir
19904         test_mkdir -i0 -c1 $mig_dir
19905         mkdir $mig_dir/dir
19906         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
19907                 error "cannot set xattr attr1 on dir"
19908         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
19909                 error "cannot set xattr attr2 on dir"
19910         touch $mig_dir/dir/f0
19911         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
19912                 error "cannot set xattr attr1 on file"
19913         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
19914                 error "cannot set xattr attr2 on file"
19915         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
19916         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
19917         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
19918         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
19919         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
19920         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
19921         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
19922         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
19923         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
19924
19925         echo "Migrating to MDT1"
19926         $LFS migrate -m $MDTIDX $mig_dir ||
19927                 error "fails on migrating dir to MDT1"
19928
19929         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
19930         echo "Checking xattrs"
19931         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
19932         [ "$val" = $longstr ] ||
19933                 error "expecting xattr1 $longstr on dir, found $val"
19934         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
19935         [ "$val" = $shortstr ] ||
19936                 error "expecting xattr2 $shortstr on dir, found $val"
19937         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
19938         [ "$val" = $longstr ] ||
19939                 error "expecting xattr1 $longstr on file, found $val"
19940         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
19941         [ "$val" = $shortstr ] ||
19942                 error "expecting xattr2 $shortstr on file, found $val"
19943 }
19944 run_test 230m "xattrs not changed after dir migration"
19945
19946 test_230n() {
19947         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
19948         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
19949                 skip "Need MDS version at least 2.13.53"
19950
19951         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
19952         cat /etc/hosts > $DIR/$tdir/$tfile
19953         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
19954         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
19955
19956         cmp /etc/hosts $DIR/$tdir/$tfile ||
19957                 error "File data mismatch after migration"
19958 }
19959 run_test 230n "Dir migration with mirrored file"
19960
19961 test_230o() {
19962         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
19963         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
19964                 skip "Need MDS version at least 2.13.52"
19965
19966         local mdts=$(comma_list $(mdts_nodes))
19967         local timeout=100
19968         local restripe_status
19969         local delta
19970         local i
19971
19972         [[ $mds1_FSTYPE == zfs ]] && timeout=300
19973
19974         # in case "crush" hash type is not set
19975         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
19976
19977         restripe_status=$(do_facet mds1 $LCTL get_param -n \
19978                            mdt.*MDT0000.enable_dir_restripe)
19979         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
19980         stack_trap "do_nodes $mdts $LCTL set_param \
19981                     mdt.*.enable_dir_restripe=$restripe_status"
19982
19983         mkdir $DIR/$tdir
19984         createmany -m $DIR/$tdir/f 100 ||
19985                 error "create files under remote dir failed $i"
19986         createmany -d $DIR/$tdir/d 100 ||
19987                 error "create dirs under remote dir failed $i"
19988
19989         for i in $(seq 2 $MDSCOUNT); do
19990                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
19991                 $LFS setdirstripe -c $i $DIR/$tdir ||
19992                         error "split -c $i $tdir failed"
19993                 wait_update $HOSTNAME \
19994                         "$LFS getdirstripe -H $DIR/$tdir" "crush" $timeout ||
19995                         error "dir split not finished"
19996                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
19997                         awk '/migrate/ {sum += $2} END { print sum }')
19998                 echo "$delta migrated when dir split $((i - 1)) to $i stripes"
19999                 # delta is around total_files/stripe_count
20000                 (( $delta < 200 / (i - 1) + 4 )) ||
20001                         error "$delta files migrated >= $((200 / (i - 1) + 4))"
20002         done
20003 }
20004 run_test 230o "dir split"
20005
20006 test_230p() {
20007         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
20008         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
20009                 skip "Need MDS version at least 2.13.52"
20010
20011         local mdts=$(comma_list $(mdts_nodes))
20012         local timeout=100
20013         local restripe_status
20014         local delta
20015         local c
20016
20017         [[ $mds1_FSTYPE == zfs ]] && timeout=300
20018
20019         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20020
20021         restripe_status=$(do_facet mds1 $LCTL get_param -n \
20022                            mdt.*MDT0000.enable_dir_restripe)
20023         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
20024         stack_trap "do_nodes $mdts $LCTL set_param \
20025                     mdt.*.enable_dir_restripe=$restripe_status"
20026
20027         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
20028         createmany -m $DIR/$tdir/f 100 ||
20029                 error "create files under remote dir failed"
20030         createmany -d $DIR/$tdir/d 100 ||
20031                 error "create dirs under remote dir failed"
20032
20033         for c in $(seq $((MDSCOUNT - 1)) -1 1); do
20034                 local mdt_hash="crush"
20035
20036                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
20037                 $LFS setdirstripe -c $c $DIR/$tdir ||
20038                         error "split -c $c $tdir failed"
20039                 if (( MDS1_VERSION >= $(version_code 2.14.51) )); then
20040                         mdt_hash="$mdt_hash,fixed"
20041                 elif [ $c -eq 1 ]; then
20042                         mdt_hash="none"
20043                 fi
20044                 wait_update $HOSTNAME \
20045                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash $timeout ||
20046                         error "dir merge not finished"
20047                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
20048                         awk '/migrate/ {sum += $2} END { print sum }')
20049                 echo "$delta migrated when dir merge $((c + 1)) to $c stripes"
20050                 # delta is around total_files/stripe_count
20051                 (( delta < 200 / c + 4 )) ||
20052                         error "$delta files migrated >= $((200 / c + 4))"
20053         done
20054 }
20055 run_test 230p "dir merge"
20056
20057 test_230q() {
20058         (( MDSCOUNT > 1)) || skip "needs >= 2 MDTs"
20059         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
20060                 skip "Need MDS version at least 2.13.52"
20061
20062         local mdts=$(comma_list $(mdts_nodes))
20063         local saved_threshold=$(do_facet mds1 \
20064                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
20065         local saved_delta=$(do_facet mds1 \
20066                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
20067         local threshold=100
20068         local delta=2
20069         local total=0
20070         local stripe_count=0
20071         local stripe_index
20072         local nr_files
20073         local create
20074
20075         # test with fewer files on ZFS
20076         [ "$mds1_FSTYPE" == "zfs" ] && threshold=40
20077
20078         stack_trap "do_nodes $mdts $LCTL set_param \
20079                     mdt.*.dir_split_count=$saved_threshold"
20080         stack_trap "do_nodes $mdts $LCTL set_param \
20081                     mdt.*.dir_split_delta=$saved_delta"
20082         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
20083         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
20084         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
20085         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
20086         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
20087         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20088
20089         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
20090         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20091
20092         create=$((threshold * 3 / 2))
20093         while [ $stripe_count -lt $MDSCOUNT ]; do
20094                 createmany -m $DIR/$tdir/f $total $create ||
20095                         error "create sub files failed"
20096                 stat $DIR/$tdir > /dev/null
20097                 total=$((total + create))
20098                 stripe_count=$((stripe_count + delta))
20099                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
20100
20101                 wait_update $HOSTNAME \
20102                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
20103                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
20104
20105                 wait_update $HOSTNAME \
20106                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
20107                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
20108
20109                 nr_files=$($LFS find -m 1 $DIR/$tdir | grep -c -w $stripe_index)
20110                 echo "$nr_files/$total files on MDT$stripe_index after split"
20111                 # allow 10% margin of imbalance with crush hash
20112                 (( $nr_files <= $total / $stripe_count + $create / 10)) ||
20113                         error "$nr_files files on MDT$stripe_index after split"
20114
20115                 nr_files=$($LFS find -type f $DIR/$tdir | wc -l)
20116                 [ $nr_files -eq $total ] ||
20117                         error "total sub files $nr_files != $total"
20118         done
20119
20120         (( MDS1_VERSION >= $(version_code 2.14.51) )) || return 0
20121
20122         echo "fixed layout directory won't auto split"
20123         $LFS migrate -m 0 $DIR/$tdir || error "migrate $tdir failed"
20124         wait_update $HOSTNAME "$LFS getdirstripe -H $DIR/$tdir" "crush,fixed" \
20125                 10 || error "stripe hash $($LFS getdirstripe -H $DIR/$tdir)"
20126         wait_update $HOSTNAME "$LFS getdirstripe -c $DIR/$tdir" 1 10 ||
20127                 error "stripe count $($LFS getdirstripe -c $DIR/$tdir)"
20128 }
20129 run_test 230q "dir auto split"
20130
20131 test_230r() {
20132         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
20133         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20134         [[ $MDS1_VERSION -ge $(version_code 2.13.54) ]] ||
20135                 skip "Need MDS version at least 2.13.54"
20136
20137         # maximum amount of local locks:
20138         # parent striped dir - 2 locks
20139         # new stripe in parent to migrate to - 1 lock
20140         # source and target - 2 locks
20141         # Total 5 locks for regular file
20142         mkdir -p $DIR/$tdir
20143         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
20144         touch $DIR/$tdir/dir1/eee
20145
20146         # create 4 hardlink for 4 more locks
20147         # Total: 9 locks > RS_MAX_LOCKS (8)
20148         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
20149         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
20150         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
20151         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
20152         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
20153         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
20154         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
20155         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
20156
20157         cancel_lru_locks mdc
20158
20159         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
20160                 error "migrate dir fails"
20161
20162         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20163 }
20164 run_test 230r "migrate with too many local locks"
20165
20166 test_230s() {
20167         [ $MDS1_VERSION -ge $(version_code 2.13.57) ] ||
20168                 skip "Need MDS version at least 2.13.57"
20169
20170         local mdts=$(comma_list $(mdts_nodes))
20171         local restripe_status=$(do_facet mds1 $LCTL get_param -n \
20172                                 mdt.*MDT0000.enable_dir_restripe)
20173
20174         stack_trap "do_nodes $mdts $LCTL set_param \
20175                     mdt.*.enable_dir_restripe=$restripe_status"
20176
20177         local st
20178         for st in 0 1; do
20179                 do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=$st"
20180                 test_mkdir $DIR/$tdir
20181                 $LFS mkdir $DIR/$tdir |& grep "File exists" ||
20182                         error "$LFS mkdir doesn't return -EEXIST if target exists"
20183                 rmdir $DIR/$tdir
20184         done
20185 }
20186 run_test 230s "lfs mkdir should return -EEXIST if target exists"
20187
20188 test_230t()
20189 {
20190         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20191         [[ $MDS1_VERSION -ge $(version_code 2.14.50) ]] ||
20192                 skip "Need MDS version at least 2.14.50"
20193
20194         test_mkdir $DIR/$tdir || error "mkdir $tdir failed"
20195         test_mkdir $DIR/$tdir/subdir || error "mkdir subdir failed"
20196         $LFS project -p 1 -s $DIR/$tdir ||
20197                 error "set $tdir project id failed"
20198         $LFS project -p 2 -s $DIR/$tdir/subdir ||
20199                 error "set subdir project id failed"
20200         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir || error "migrate failed"
20201 }
20202 run_test 230t "migrate directory with project ID set"
20203
20204 test_231a()
20205 {
20206         # For simplicity this test assumes that max_pages_per_rpc
20207         # is the same across all OSCs
20208         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
20209         local bulk_size=$((max_pages * PAGE_SIZE))
20210         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
20211                                        head -n 1)
20212
20213         mkdir -p $DIR/$tdir
20214         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
20215                 error "failed to set stripe with -S ${brw_size}M option"
20216
20217         # clear the OSC stats
20218         $LCTL set_param osc.*.stats=0 &>/dev/null
20219         stop_writeback
20220
20221         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
20222         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
20223                 oflag=direct &>/dev/null || error "dd failed"
20224
20225         sync; sleep 1; sync # just to be safe
20226         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
20227         if [ x$nrpcs != "x1" ]; then
20228                 $LCTL get_param osc.*.stats
20229                 error "found $nrpcs ost_write RPCs, not 1 as expected"
20230         fi
20231
20232         start_writeback
20233         # Drop the OSC cache, otherwise we will read from it
20234         cancel_lru_locks osc
20235
20236         # clear the OSC stats
20237         $LCTL set_param osc.*.stats=0 &>/dev/null
20238
20239         # Client reads $bulk_size.
20240         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
20241                 iflag=direct &>/dev/null || error "dd failed"
20242
20243         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
20244         if [ x$nrpcs != "x1" ]; then
20245                 $LCTL get_param osc.*.stats
20246                 error "found $nrpcs ost_read RPCs, not 1 as expected"
20247         fi
20248 }
20249 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
20250
20251 test_231b() {
20252         mkdir -p $DIR/$tdir
20253         local i
20254         for i in {0..1023}; do
20255                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
20256                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
20257                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
20258         done
20259         sync
20260 }
20261 run_test 231b "must not assert on fully utilized OST request buffer"
20262
20263 test_232a() {
20264         mkdir -p $DIR/$tdir
20265         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
20266
20267         #define OBD_FAIL_LDLM_OST_LVB            0x31c
20268         do_facet ost1 $LCTL set_param fail_loc=0x31c
20269
20270         # ignore dd failure
20271         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
20272
20273         do_facet ost1 $LCTL set_param fail_loc=0
20274         umount_client $MOUNT || error "umount failed"
20275         mount_client $MOUNT || error "mount failed"
20276         stop ost1 || error "cannot stop ost1"
20277         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
20278 }
20279 run_test 232a "failed lock should not block umount"
20280
20281 test_232b() {
20282         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
20283                 skip "Need MDS version at least 2.10.58"
20284
20285         mkdir -p $DIR/$tdir
20286         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
20287         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
20288         sync
20289         cancel_lru_locks osc
20290
20291         #define OBD_FAIL_LDLM_OST_LVB            0x31c
20292         do_facet ost1 $LCTL set_param fail_loc=0x31c
20293
20294         # ignore failure
20295         $LFS data_version $DIR/$tdir/$tfile || true
20296
20297         do_facet ost1 $LCTL set_param fail_loc=0
20298         umount_client $MOUNT || error "umount failed"
20299         mount_client $MOUNT || error "mount failed"
20300         stop ost1 || error "cannot stop ost1"
20301         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
20302 }
20303 run_test 232b "failed data version lock should not block umount"
20304
20305 test_233a() {
20306         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
20307                 skip "Need MDS version at least 2.3.64"
20308         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
20309
20310         local fid=$($LFS path2fid $MOUNT)
20311
20312         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20313                 error "cannot access $MOUNT using its FID '$fid'"
20314 }
20315 run_test 233a "checking that OBF of the FS root succeeds"
20316
20317 test_233b() {
20318         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
20319                 skip "Need MDS version at least 2.5.90"
20320         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
20321
20322         local fid=$($LFS path2fid $MOUNT/.lustre)
20323
20324         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20325                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
20326
20327         fid=$($LFS path2fid $MOUNT/.lustre/fid)
20328         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
20329                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
20330 }
20331 run_test 233b "checking that OBF of the FS .lustre succeeds"
20332
20333 test_234() {
20334         local p="$TMP/sanityN-$TESTNAME.parameters"
20335         save_lustre_params client "llite.*.xattr_cache" > $p
20336         lctl set_param llite.*.xattr_cache 1 ||
20337                 skip_env "xattr cache is not supported"
20338
20339         mkdir -p $DIR/$tdir || error "mkdir failed"
20340         touch $DIR/$tdir/$tfile || error "touch failed"
20341         # OBD_FAIL_LLITE_XATTR_ENOMEM
20342         $LCTL set_param fail_loc=0x1405
20343         getfattr -n user.attr $DIR/$tdir/$tfile &&
20344                 error "getfattr should have failed with ENOMEM"
20345         $LCTL set_param fail_loc=0x0
20346         rm -rf $DIR/$tdir
20347
20348         restore_lustre_params < $p
20349         rm -f $p
20350 }
20351 run_test 234 "xattr cache should not crash on ENOMEM"
20352
20353 test_235() {
20354         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
20355                 skip "Need MDS version at least 2.4.52"
20356
20357         flock_deadlock $DIR/$tfile
20358         local RC=$?
20359         case $RC in
20360                 0)
20361                 ;;
20362                 124) error "process hangs on a deadlock"
20363                 ;;
20364                 *) error "error executing flock_deadlock $DIR/$tfile"
20365                 ;;
20366         esac
20367 }
20368 run_test 235 "LU-1715: flock deadlock detection does not work properly"
20369
20370 #LU-2935
20371 test_236() {
20372         check_swap_layouts_support
20373
20374         local ref1=/etc/passwd
20375         local ref2=/etc/group
20376         local file1=$DIR/$tdir/f1
20377         local file2=$DIR/$tdir/f2
20378
20379         test_mkdir -c1 $DIR/$tdir
20380         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
20381         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
20382         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
20383         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
20384         local fd=$(free_fd)
20385         local cmd="exec $fd<>$file2"
20386         eval $cmd
20387         rm $file2
20388         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
20389                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
20390         cmd="exec $fd>&-"
20391         eval $cmd
20392         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
20393
20394         #cleanup
20395         rm -rf $DIR/$tdir
20396 }
20397 run_test 236 "Layout swap on open unlinked file"
20398
20399 # LU-4659 linkea consistency
20400 test_238() {
20401         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
20402                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
20403                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
20404                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
20405
20406         touch $DIR/$tfile
20407         ln $DIR/$tfile $DIR/$tfile.lnk
20408         touch $DIR/$tfile.new
20409         mv $DIR/$tfile.new $DIR/$tfile
20410         local fid1=$($LFS path2fid $DIR/$tfile)
20411         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
20412         local path1=$($LFS fid2path $FSNAME "$fid1")
20413         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
20414         local path2=$($LFS fid2path $FSNAME "$fid2")
20415         [ $tfile.lnk == $path2 ] ||
20416                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
20417         rm -f $DIR/$tfile*
20418 }
20419 run_test 238 "Verify linkea consistency"
20420
20421 test_239A() { # was test_239
20422         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
20423                 skip "Need MDS version at least 2.5.60"
20424
20425         local list=$(comma_list $(mdts_nodes))
20426
20427         mkdir -p $DIR/$tdir
20428         createmany -o $DIR/$tdir/f- 5000
20429         unlinkmany $DIR/$tdir/f- 5000
20430         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
20431                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
20432         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
20433                         osp.*MDT*.sync_in_flight" | calc_sum)
20434         [ "$changes" -eq 0 ] || error "$changes not synced"
20435 }
20436 run_test 239A "osp_sync test"
20437
20438 test_239a() { #LU-5297
20439         remote_mds_nodsh && skip "remote MDS with nodsh"
20440
20441         touch $DIR/$tfile
20442         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
20443         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
20444         chgrp $RUNAS_GID $DIR/$tfile
20445         wait_delete_completed
20446 }
20447 run_test 239a "process invalid osp sync record correctly"
20448
20449 test_239b() { #LU-5297
20450         remote_mds_nodsh && skip "remote MDS with nodsh"
20451
20452         touch $DIR/$tfile1
20453         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
20454         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
20455         chgrp $RUNAS_GID $DIR/$tfile1
20456         wait_delete_completed
20457         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
20458         touch $DIR/$tfile2
20459         chgrp $RUNAS_GID $DIR/$tfile2
20460         wait_delete_completed
20461 }
20462 run_test 239b "process osp sync record with ENOMEM error correctly"
20463
20464 test_240() {
20465         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20466         remote_mds_nodsh && skip "remote MDS with nodsh"
20467
20468         mkdir -p $DIR/$tdir
20469
20470         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
20471                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
20472         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
20473                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
20474
20475         umount_client $MOUNT || error "umount failed"
20476         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
20477         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
20478         mount_client $MOUNT || error "failed to mount client"
20479
20480         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
20481         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
20482 }
20483 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
20484
20485 test_241_bio() {
20486         local count=$1
20487         local bsize=$2
20488
20489         for LOOP in $(seq $count); do
20490                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
20491                 cancel_lru_locks $OSC || true
20492         done
20493 }
20494
20495 test_241_dio() {
20496         local count=$1
20497         local bsize=$2
20498
20499         for LOOP in $(seq $1); do
20500                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
20501                         2>/dev/null
20502         done
20503 }
20504
20505 test_241a() { # was test_241
20506         local bsize=$PAGE_SIZE
20507
20508         (( bsize < 40960 )) && bsize=40960
20509         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
20510         ls -la $DIR/$tfile
20511         cancel_lru_locks $OSC
20512         test_241_bio 1000 $bsize &
20513         PID=$!
20514         test_241_dio 1000 $bsize
20515         wait $PID
20516 }
20517 run_test 241a "bio vs dio"
20518
20519 test_241b() {
20520         local bsize=$PAGE_SIZE
20521
20522         (( bsize < 40960 )) && bsize=40960
20523         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
20524         ls -la $DIR/$tfile
20525         test_241_dio 1000 $bsize &
20526         PID=$!
20527         test_241_dio 1000 $bsize
20528         wait $PID
20529 }
20530 run_test 241b "dio vs dio"
20531
20532 test_242() {
20533         remote_mds_nodsh && skip "remote MDS with nodsh"
20534
20535         mkdir_on_mdt0 $DIR/$tdir
20536         touch $DIR/$tdir/$tfile
20537
20538         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
20539         do_facet mds1 lctl set_param fail_loc=0x105
20540         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
20541
20542         do_facet mds1 lctl set_param fail_loc=0
20543         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
20544 }
20545 run_test 242 "mdt_readpage failure should not cause directory unreadable"
20546
20547 test_243()
20548 {
20549         test_mkdir $DIR/$tdir
20550         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
20551 }
20552 run_test 243 "various group lock tests"
20553
20554 test_244a()
20555 {
20556         test_mkdir $DIR/$tdir
20557         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
20558         sendfile_grouplock $DIR/$tdir/$tfile || \
20559                 error "sendfile+grouplock failed"
20560         rm -rf $DIR/$tdir
20561 }
20562 run_test 244a "sendfile with group lock tests"
20563
20564 test_244b()
20565 {
20566         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
20567
20568         local threads=50
20569         local size=$((1024*1024))
20570
20571         test_mkdir $DIR/$tdir
20572         for i in $(seq 1 $threads); do
20573                 local file=$DIR/$tdir/file_$((i / 10))
20574                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
20575                 local pids[$i]=$!
20576         done
20577         for i in $(seq 1 $threads); do
20578                 wait ${pids[$i]}
20579         done
20580 }
20581 run_test 244b "multi-threaded write with group lock"
20582
20583 test_245() {
20584         local flagname="multi_mod_rpcs"
20585         local connect_data_name="max_mod_rpcs"
20586         local out
20587
20588         # check if multiple modify RPCs flag is set
20589         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
20590                 grep "connect_flags:")
20591         echo "$out"
20592
20593         echo "$out" | grep -qw $flagname
20594         if [ $? -ne 0 ]; then
20595                 echo "connect flag $flagname is not set"
20596                 return
20597         fi
20598
20599         # check if multiple modify RPCs data is set
20600         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
20601         echo "$out"
20602
20603         echo "$out" | grep -qw $connect_data_name ||
20604                 error "import should have connect data $connect_data_name"
20605 }
20606 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
20607
20608 cleanup_247() {
20609         local submount=$1
20610
20611         trap 0
20612         umount_client $submount
20613         rmdir $submount
20614 }
20615
20616 test_247a() {
20617         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20618                 grep -q subtree ||
20619                 skip_env "Fileset feature is not supported"
20620
20621         local submount=${MOUNT}_$tdir
20622
20623         mkdir $MOUNT/$tdir
20624         mkdir -p $submount || error "mkdir $submount failed"
20625         FILESET="$FILESET/$tdir" mount_client $submount ||
20626                 error "mount $submount failed"
20627         trap "cleanup_247 $submount" EXIT
20628         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
20629         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
20630                 error "read $MOUNT/$tdir/$tfile failed"
20631         cleanup_247 $submount
20632 }
20633 run_test 247a "mount subdir as fileset"
20634
20635 test_247b() {
20636         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20637                 skip_env "Fileset feature is not supported"
20638
20639         local submount=${MOUNT}_$tdir
20640
20641         rm -rf $MOUNT/$tdir
20642         mkdir -p $submount || error "mkdir $submount failed"
20643         SKIP_FILESET=1
20644         FILESET="$FILESET/$tdir" mount_client $submount &&
20645                 error "mount $submount should fail"
20646         rmdir $submount
20647 }
20648 run_test 247b "mount subdir that dose not exist"
20649
20650 test_247c() {
20651         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20652                 skip_env "Fileset feature is not supported"
20653
20654         local submount=${MOUNT}_$tdir
20655
20656         mkdir -p $MOUNT/$tdir/dir1
20657         mkdir -p $submount || error "mkdir $submount failed"
20658         trap "cleanup_247 $submount" EXIT
20659         FILESET="$FILESET/$tdir" mount_client $submount ||
20660                 error "mount $submount failed"
20661         local fid=$($LFS path2fid $MOUNT/)
20662         $LFS fid2path $submount $fid && error "fid2path should fail"
20663         cleanup_247 $submount
20664 }
20665 run_test 247c "running fid2path outside subdirectory root"
20666
20667 test_247d() {
20668         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
20669                 skip "Fileset feature is not supported"
20670
20671         local submount=${MOUNT}_$tdir
20672
20673         mkdir -p $MOUNT/$tdir/dir1
20674         mkdir -p $submount || error "mkdir $submount failed"
20675         FILESET="$FILESET/$tdir" mount_client $submount ||
20676                 error "mount $submount failed"
20677         trap "cleanup_247 $submount" EXIT
20678
20679         local td=$submount/dir1
20680         local fid=$($LFS path2fid $td)
20681         [ -z "$fid" ] && error "path2fid unable to get $td FID"
20682
20683         # check that we get the same pathname back
20684         local rootpath
20685         local found
20686         for rootpath in "$submount" "$submount///" "$submount/dir1"; do
20687                 echo "$rootpath $fid"
20688                 found=$($LFS fid2path $rootpath "$fid")
20689                 [ -n "found" ] || error "fid2path should succeed"
20690                 [ "$found" == "$td" ] || error "fid2path $found != $td"
20691         done
20692         # check wrong root path format
20693         rootpath=$submount"_wrong"
20694         found=$($LFS fid2path $rootpath "$fid")
20695         [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)"
20696
20697         cleanup_247 $submount
20698 }
20699 run_test 247d "running fid2path inside subdirectory root"
20700
20701 # LU-8037
20702 test_247e() {
20703         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20704                 grep -q subtree ||
20705                 skip "Fileset feature is not supported"
20706
20707         local submount=${MOUNT}_$tdir
20708
20709         mkdir $MOUNT/$tdir
20710         mkdir -p $submount || error "mkdir $submount failed"
20711         FILESET="$FILESET/.." mount_client $submount &&
20712                 error "mount $submount should fail"
20713         rmdir $submount
20714 }
20715 run_test 247e "mount .. as fileset"
20716
20717 test_247f() {
20718         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20719         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
20720                 skip "Need at least version 2.13.52"
20721         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
20722                 skip "Need at least version 2.14.50"
20723         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
20724                 grep -q subtree ||
20725                 skip "Fileset feature is not supported"
20726
20727         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
20728         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
20729                 error "mkdir remote failed"
20730         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote/subdir ||
20731                 error "mkdir remote/subdir failed"
20732         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
20733                 error "mkdir striped failed"
20734         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
20735
20736         local submount=${MOUNT}_$tdir
20737
20738         mkdir -p $submount || error "mkdir $submount failed"
20739         stack_trap "rmdir $submount"
20740
20741         local dir
20742         local stat
20743         local fileset=$FILESET
20744         local mdts=$(comma_list $(mdts_nodes))
20745
20746         stat=$(do_facet mds1 $LCTL get_param -n \
20747                 mdt.*MDT0000.enable_remote_subdir_mount)
20748         stack_trap "do_nodes $mdts $LCTL set_param \
20749                 mdt.*.enable_remote_subdir_mount=$stat"
20750
20751         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
20752         stack_trap "umount_client $submount"
20753         FILESET="$fileset/$tdir/remote" mount_client $submount &&
20754                 error "mount remote dir $dir should fail"
20755
20756         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
20757                 $tdir/striped/. ; do
20758                 FILESET="$fileset/$dir" mount_client $submount ||
20759                         error "mount $dir failed"
20760                 umount_client $submount
20761         done
20762
20763         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
20764         FILESET="$fileset/$tdir/remote" mount_client $submount ||
20765                 error "mount $tdir/remote failed"
20766 }
20767 run_test 247f "mount striped or remote directory as fileset"
20768
20769 test_247g() {
20770         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
20771         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
20772                 skip "Need at least version 2.14.50"
20773
20774         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
20775                 error "mkdir $tdir failed"
20776         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
20777
20778         local submount=${MOUNT}_$tdir
20779
20780         mkdir -p $submount || error "mkdir $submount failed"
20781         stack_trap "rmdir $submount"
20782
20783         FILESET="$fileset/$tdir" mount_client $submount ||
20784                 error "mount $dir failed"
20785         stack_trap "umount $submount"
20786
20787         local mdts=$(comma_list $(mdts_nodes))
20788
20789         local nrpcs
20790
20791         stat $submount > /dev/null
20792         cancel_lru_locks $MDC
20793         stat $submount > /dev/null
20794         stat $submount/$tfile > /dev/null
20795         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
20796         stat $submount/$tfile > /dev/null
20797         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
20798                 awk '/getattr/ {sum += $2} END {print sum}')
20799
20800         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
20801 }
20802 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
20803
20804 test_248a() {
20805         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
20806         [ -z "$fast_read_sav" ] && skip "no fast read support"
20807
20808         # create a large file for fast read verification
20809         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
20810
20811         # make sure the file is created correctly
20812         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
20813                 { rm -f $DIR/$tfile; skip "file creation error"; }
20814
20815         echo "Test 1: verify that fast read is 4 times faster on cache read"
20816
20817         # small read with fast read enabled
20818         $LCTL set_param -n llite.*.fast_read=1
20819         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
20820                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20821                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20822         # small read with fast read disabled
20823         $LCTL set_param -n llite.*.fast_read=0
20824         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
20825                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20826                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20827
20828         # verify that fast read is 4 times faster for cache read
20829         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
20830                 error_not_in_vm "fast read was not 4 times faster: " \
20831                            "$t_fast vs $t_slow"
20832
20833         echo "Test 2: verify the performance between big and small read"
20834         $LCTL set_param -n llite.*.fast_read=1
20835
20836         # 1k non-cache read
20837         cancel_lru_locks osc
20838         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
20839                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20840                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20841
20842         # 1M non-cache read
20843         cancel_lru_locks osc
20844         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
20845                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
20846                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
20847
20848         # verify that big IO is not 4 times faster than small IO
20849         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
20850                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
20851
20852         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
20853         rm -f $DIR/$tfile
20854 }
20855 run_test 248a "fast read verification"
20856
20857 test_248b() {
20858         # Default short_io_bytes=16384, try both smaller and larger sizes.
20859         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
20860         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
20861         echo "bs=53248 count=113 normal buffered write"
20862         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
20863                 error "dd of initial data file failed"
20864         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
20865
20866         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
20867         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
20868                 error "dd with sync normal writes failed"
20869         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
20870
20871         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
20872         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
20873                 error "dd with sync small writes failed"
20874         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
20875
20876         cancel_lru_locks osc
20877
20878         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
20879         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
20880         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
20881         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
20882                 iflag=direct || error "dd with O_DIRECT small read failed"
20883         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
20884         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
20885                 error "compare $TMP/$tfile.1 failed"
20886
20887         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
20888         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
20889
20890         # just to see what the maximum tunable value is, and test parsing
20891         echo "test invalid parameter 2MB"
20892         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
20893                 error "too-large short_io_bytes allowed"
20894         echo "test maximum parameter 512KB"
20895         # if we can set a larger short_io_bytes, run test regardless of version
20896         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
20897                 # older clients may not allow setting it this large, that's OK
20898                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
20899                         skip "Need at least client version 2.13.50"
20900                 error "medium short_io_bytes failed"
20901         fi
20902         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
20903         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
20904
20905         echo "test large parameter 64KB"
20906         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
20907         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
20908
20909         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
20910         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
20911                 error "dd with sync large writes failed"
20912         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
20913
20914         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
20915         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
20916         num=$((113 * 4096 / PAGE_SIZE))
20917         echo "bs=$size count=$num oflag=direct large write $tfile.3"
20918         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
20919                 error "dd with O_DIRECT large writes failed"
20920         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
20921                 error "compare $DIR/$tfile.3 failed"
20922
20923         cancel_lru_locks osc
20924
20925         echo "bs=$size count=$num iflag=direct large read $tfile.2"
20926         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
20927                 error "dd with O_DIRECT large read failed"
20928         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
20929                 error "compare $TMP/$tfile.2 failed"
20930
20931         echo "bs=$size count=$num iflag=direct large read $tfile.3"
20932         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
20933                 error "dd with O_DIRECT large read failed"
20934         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
20935                 error "compare $TMP/$tfile.3 failed"
20936 }
20937 run_test 248b "test short_io read and write for both small and large sizes"
20938
20939 test_249() { # LU-7890
20940         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
20941                 skip "Need at least version 2.8.54"
20942
20943         rm -f $DIR/$tfile
20944         $LFS setstripe -c 1 $DIR/$tfile
20945         # Offset 2T == 4k * 512M
20946         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
20947                 error "dd to 2T offset failed"
20948 }
20949 run_test 249 "Write above 2T file size"
20950
20951 test_250() {
20952         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
20953          && skip "no 16TB file size limit on ZFS"
20954
20955         $LFS setstripe -c 1 $DIR/$tfile
20956         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
20957         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
20958         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
20959         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
20960                 conv=notrunc,fsync && error "append succeeded"
20961         return 0
20962 }
20963 run_test 250 "Write above 16T limit"
20964
20965 test_251() {
20966         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
20967
20968         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
20969         #Skip once - writing the first stripe will succeed
20970         $LCTL set_param fail_loc=0xa0001407 fail_val=1
20971         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
20972                 error "short write happened"
20973
20974         $LCTL set_param fail_loc=0xa0001407 fail_val=1
20975         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
20976                 error "short read happened"
20977
20978         rm -f $DIR/$tfile
20979 }
20980 run_test 251 "Handling short read and write correctly"
20981
20982 test_252() {
20983         remote_mds_nodsh && skip "remote MDS with nodsh"
20984         remote_ost_nodsh && skip "remote OST with nodsh"
20985         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
20986                 skip_env "ldiskfs only test"
20987         fi
20988
20989         local tgt
20990         local dev
20991         local out
20992         local uuid
20993         local num
20994         local gen
20995
20996         # check lr_reader on OST0000
20997         tgt=ost1
20998         dev=$(facet_device $tgt)
20999         out=$(do_facet $tgt $LR_READER $dev)
21000         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21001         echo "$out"
21002         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
21003         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
21004                 error "Invalid uuid returned by $LR_READER on target $tgt"
21005         echo -e "uuid returned by $LR_READER is '$uuid'\n"
21006
21007         # check lr_reader -c on MDT0000
21008         tgt=mds1
21009         dev=$(facet_device $tgt)
21010         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
21011                 skip "$LR_READER does not support additional options"
21012         fi
21013         out=$(do_facet $tgt $LR_READER -c $dev)
21014         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21015         echo "$out"
21016         num=$(echo "$out" | grep -c "mdtlov")
21017         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
21018                 error "Invalid number of mdtlov clients returned by $LR_READER"
21019         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
21020
21021         # check lr_reader -cr on MDT0000
21022         out=$(do_facet $tgt $LR_READER -cr $dev)
21023         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21024         echo "$out"
21025         echo "$out" | grep -q "^reply_data:$" ||
21026                 error "$LR_READER should have returned 'reply_data' section"
21027         num=$(echo "$out" | grep -c "client_generation")
21028         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
21029 }
21030 run_test 252 "check lr_reader tool"
21031
21032 test_253() {
21033         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21034         remote_mds_nodsh && skip "remote MDS with nodsh"
21035         remote_mgs_nodsh && skip "remote MGS with nodsh"
21036
21037         local ostidx=0
21038         local rc=0
21039         local ost_name=$(ostname_from_index $ostidx)
21040
21041         # on the mdt's osc
21042         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
21043         do_facet $SINGLEMDS $LCTL get_param -n \
21044                 osp.$mdtosc_proc1.reserved_mb_high ||
21045                 skip  "remote MDS does not support reserved_mb_high"
21046
21047         rm -rf $DIR/$tdir
21048         wait_mds_ost_sync
21049         wait_delete_completed
21050         mkdir $DIR/$tdir
21051
21052         pool_add $TESTNAME || error "Pool creation failed"
21053         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
21054
21055         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
21056                 error "Setstripe failed"
21057
21058         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
21059
21060         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
21061                     grep "watermarks")
21062         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
21063
21064         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
21065                         osp.$mdtosc_proc1.prealloc_status)
21066         echo "prealloc_status $oa_status"
21067
21068         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
21069                 error "File creation should fail"
21070
21071         #object allocation was stopped, but we still able to append files
21072         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
21073                 oflag=append || error "Append failed"
21074
21075         rm -f $DIR/$tdir/$tfile.0
21076
21077         # For this test, we want to delete the files we created to go out of
21078         # space but leave the watermark, so we remain nearly out of space
21079         ost_watermarks_enospc_delete_files $tfile $ostidx
21080
21081         wait_delete_completed
21082
21083         sleep_maxage
21084
21085         for i in $(seq 10 12); do
21086                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
21087                         2>/dev/null || error "File creation failed after rm"
21088         done
21089
21090         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
21091                         osp.$mdtosc_proc1.prealloc_status)
21092         echo "prealloc_status $oa_status"
21093
21094         if (( oa_status != 0 )); then
21095                 error "Object allocation still disable after rm"
21096         fi
21097 }
21098 run_test 253 "Check object allocation limit"
21099
21100 test_254() {
21101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21102         remote_mds_nodsh && skip "remote MDS with nodsh"
21103         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
21104                 skip "MDS does not support changelog_size"
21105
21106         local cl_user
21107         local MDT0=$(facet_svc $SINGLEMDS)
21108
21109         changelog_register || error "changelog_register failed"
21110
21111         changelog_clear 0 || error "changelog_clear failed"
21112
21113         local size1=$(do_facet $SINGLEMDS \
21114                       $LCTL get_param -n mdd.$MDT0.changelog_size)
21115         echo "Changelog size $size1"
21116
21117         rm -rf $DIR/$tdir
21118         $LFS mkdir -i 0 $DIR/$tdir
21119         # change something
21120         mkdir -p $DIR/$tdir/pics/2008/zachy
21121         touch $DIR/$tdir/pics/2008/zachy/timestamp
21122         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
21123         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
21124         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
21125         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
21126         rm $DIR/$tdir/pics/desktop.jpg
21127
21128         local size2=$(do_facet $SINGLEMDS \
21129                       $LCTL get_param -n mdd.$MDT0.changelog_size)
21130         echo "Changelog size after work $size2"
21131
21132         (( $size2 > $size1 )) ||
21133                 error "new Changelog size=$size2 less than old size=$size1"
21134 }
21135 run_test 254 "Check changelog size"
21136
21137 ladvise_no_type()
21138 {
21139         local type=$1
21140         local file=$2
21141
21142         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
21143                 awk -F: '{print $2}' | grep $type > /dev/null
21144         if [ $? -ne 0 ]; then
21145                 return 0
21146         fi
21147         return 1
21148 }
21149
21150 ladvise_no_ioctl()
21151 {
21152         local file=$1
21153
21154         lfs ladvise -a willread $file > /dev/null 2>&1
21155         if [ $? -eq 0 ]; then
21156                 return 1
21157         fi
21158
21159         lfs ladvise -a willread $file 2>&1 |
21160                 grep "Inappropriate ioctl for device" > /dev/null
21161         if [ $? -eq 0 ]; then
21162                 return 0
21163         fi
21164         return 1
21165 }
21166
21167 percent() {
21168         bc <<<"scale=2; ($1 - $2) * 100 / $2"
21169 }
21170
21171 # run a random read IO workload
21172 # usage: random_read_iops <filename> <filesize> <iosize>
21173 random_read_iops() {
21174         local file=$1
21175         local fsize=$2
21176         local iosize=${3:-4096}
21177
21178         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
21179                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
21180 }
21181
21182 drop_file_oss_cache() {
21183         local file="$1"
21184         local nodes="$2"
21185
21186         $LFS ladvise -a dontneed $file 2>/dev/null ||
21187                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
21188 }
21189
21190 ladvise_willread_performance()
21191 {
21192         local repeat=10
21193         local average_origin=0
21194         local average_cache=0
21195         local average_ladvise=0
21196
21197         for ((i = 1; i <= $repeat; i++)); do
21198                 echo "Iter $i/$repeat: reading without willread hint"
21199                 cancel_lru_locks osc
21200                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
21201                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
21202                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
21203                 average_origin=$(bc <<<"$average_origin + $speed_origin")
21204
21205                 cancel_lru_locks osc
21206                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
21207                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
21208                 average_cache=$(bc <<<"$average_cache + $speed_cache")
21209
21210                 cancel_lru_locks osc
21211                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
21212                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
21213                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
21214                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
21215                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
21216         done
21217         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
21218         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
21219         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
21220
21221         speedup_cache=$(percent $average_cache $average_origin)
21222         speedup_ladvise=$(percent $average_ladvise $average_origin)
21223
21224         echo "Average uncached read: $average_origin"
21225         echo "Average speedup with OSS cached read: " \
21226                 "$average_cache = +$speedup_cache%"
21227         echo "Average speedup with ladvise willread: " \
21228                 "$average_ladvise = +$speedup_ladvise%"
21229
21230         local lowest_speedup=20
21231         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
21232                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
21233                         "got $average_cache%. Skipping ladvise willread check."
21234                 return 0
21235         fi
21236
21237         # the test won't work on ZFS until it supports 'ladvise dontneed', but
21238         # it is still good to run until then to exercise 'ladvise willread'
21239         ! $LFS ladvise -a dontneed $DIR/$tfile &&
21240                 [ "$ost1_FSTYPE" = "zfs" ] &&
21241                 echo "osd-zfs does not support dontneed or drop_caches" &&
21242                 return 0
21243
21244         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
21245         [[ ${average_ladvise%.*} > $lowest_speedup ]] ||
21246                 error_not_in_vm "Speedup with willread is less than " \
21247                         "$lowest_speedup%, got $average_ladvise%"
21248 }
21249
21250 test_255a() {
21251         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
21252                 skip "lustre < 2.8.54 does not support ladvise "
21253         remote_ost_nodsh && skip "remote OST with nodsh"
21254
21255         stack_trap "rm -f $DIR/$tfile"
21256         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
21257
21258         ladvise_no_type willread $DIR/$tfile &&
21259                 skip "willread ladvise is not supported"
21260
21261         ladvise_no_ioctl $DIR/$tfile &&
21262                 skip "ladvise ioctl is not supported"
21263
21264         local size_mb=100
21265         local size=$((size_mb * 1048576))
21266         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
21267                 error "dd to $DIR/$tfile failed"
21268
21269         lfs ladvise -a willread $DIR/$tfile ||
21270                 error "Ladvise failed with no range argument"
21271
21272         lfs ladvise -a willread -s 0 $DIR/$tfile ||
21273                 error "Ladvise failed with no -l or -e argument"
21274
21275         lfs ladvise -a willread -e 1 $DIR/$tfile ||
21276                 error "Ladvise failed with only -e argument"
21277
21278         lfs ladvise -a willread -l 1 $DIR/$tfile ||
21279                 error "Ladvise failed with only -l argument"
21280
21281         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
21282                 error "End offset should not be smaller than start offset"
21283
21284         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
21285                 error "End offset should not be equal to start offset"
21286
21287         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
21288                 error "Ladvise failed with overflowing -s argument"
21289
21290         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
21291                 error "Ladvise failed with overflowing -e argument"
21292
21293         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
21294                 error "Ladvise failed with overflowing -l argument"
21295
21296         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
21297                 error "Ladvise succeeded with conflicting -l and -e arguments"
21298
21299         echo "Synchronous ladvise should wait"
21300         local delay=4
21301 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
21302         do_nodes $(comma_list $(osts_nodes)) \
21303                 $LCTL set_param fail_val=$delay fail_loc=0x237
21304
21305         local start_ts=$SECONDS
21306         lfs ladvise -a willread $DIR/$tfile ||
21307                 error "Ladvise failed with no range argument"
21308         local end_ts=$SECONDS
21309         local inteval_ts=$((end_ts - start_ts))
21310
21311         if [ $inteval_ts -lt $(($delay - 1)) ]; then
21312                 error "Synchronous advice didn't wait reply"
21313         fi
21314
21315         echo "Asynchronous ladvise shouldn't wait"
21316         local start_ts=$SECONDS
21317         lfs ladvise -a willread -b $DIR/$tfile ||
21318                 error "Ladvise failed with no range argument"
21319         local end_ts=$SECONDS
21320         local inteval_ts=$((end_ts - start_ts))
21321
21322         if [ $inteval_ts -gt $(($delay / 2)) ]; then
21323                 error "Asynchronous advice blocked"
21324         fi
21325
21326         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
21327         ladvise_willread_performance
21328 }
21329 run_test 255a "check 'lfs ladvise -a willread'"
21330
21331 facet_meminfo() {
21332         local facet=$1
21333         local info=$2
21334
21335         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
21336 }
21337
21338 test_255b() {
21339         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
21340                 skip "lustre < 2.8.54 does not support ladvise "
21341         remote_ost_nodsh && skip "remote OST with nodsh"
21342
21343         stack_trap "rm -f $DIR/$tfile"
21344         lfs setstripe -c 1 -i 0 $DIR/$tfile
21345
21346         ladvise_no_type dontneed $DIR/$tfile &&
21347                 skip "dontneed ladvise is not supported"
21348
21349         ladvise_no_ioctl $DIR/$tfile &&
21350                 skip "ladvise ioctl is not supported"
21351
21352         ! $LFS ladvise -a dontneed $DIR/$tfile &&
21353                 [ "$ost1_FSTYPE" = "zfs" ] &&
21354                 skip "zfs-osd does not support 'ladvise dontneed'"
21355
21356         local size_mb=100
21357         local size=$((size_mb * 1048576))
21358         # In order to prevent disturbance of other processes, only check 3/4
21359         # of the memory usage
21360         local kibibytes=$((size_mb * 1024 * 3 / 4))
21361
21362         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
21363                 error "dd to $DIR/$tfile failed"
21364
21365         #force write to complete before dropping OST cache & checking memory
21366         sync
21367
21368         local total=$(facet_meminfo ost1 MemTotal)
21369         echo "Total memory: $total KiB"
21370
21371         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
21372         local before_read=$(facet_meminfo ost1 Cached)
21373         echo "Cache used before read: $before_read KiB"
21374
21375         lfs ladvise -a willread $DIR/$tfile ||
21376                 error "Ladvise willread failed"
21377         local after_read=$(facet_meminfo ost1 Cached)
21378         echo "Cache used after read: $after_read KiB"
21379
21380         lfs ladvise -a dontneed $DIR/$tfile ||
21381                 error "Ladvise dontneed again failed"
21382         local no_read=$(facet_meminfo ost1 Cached)
21383         echo "Cache used after dontneed ladvise: $no_read KiB"
21384
21385         if [ $total -lt $((before_read + kibibytes)) ]; then
21386                 echo "Memory is too small, abort checking"
21387                 return 0
21388         fi
21389
21390         if [ $((before_read + kibibytes)) -gt $after_read ]; then
21391                 error "Ladvise willread should use more memory" \
21392                         "than $kibibytes KiB"
21393         fi
21394
21395         if [ $((no_read + kibibytes)) -gt $after_read ]; then
21396                 error "Ladvise dontneed should release more memory" \
21397                         "than $kibibytes KiB"
21398         fi
21399 }
21400 run_test 255b "check 'lfs ladvise -a dontneed'"
21401
21402 test_255c() {
21403         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
21404                 skip "lustre < 2.10.50 does not support lockahead"
21405
21406         local ost1_imp=$(get_osc_import_name client ost1)
21407         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
21408                          cut -d'.' -f2)
21409         local count
21410         local new_count
21411         local difference
21412         local i
21413         local rc
21414
21415         test_mkdir -p $DIR/$tdir
21416         $LFS setstripe -i 0 -c 1 $DIR/$tdir
21417
21418         #test 10 returns only success/failure
21419         i=10
21420         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21421         rc=$?
21422         if [ $rc -eq 255 ]; then
21423                 error "Ladvise test${i} failed, ${rc}"
21424         fi
21425
21426         #test 11 counts lock enqueue requests, all others count new locks
21427         i=11
21428         count=$(do_facet ost1 \
21429                 $LCTL get_param -n ost.OSS.ost.stats)
21430         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
21431
21432         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21433         rc=$?
21434         if [ $rc -eq 255 ]; then
21435                 error "Ladvise test${i} failed, ${rc}"
21436         fi
21437
21438         new_count=$(do_facet ost1 \
21439                 $LCTL get_param -n ost.OSS.ost.stats)
21440         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
21441                    awk '{ print $2 }')
21442
21443         difference="$((new_count - count))"
21444         if [ $difference -ne $rc ]; then
21445                 error "Ladvise test${i}, bad enqueue count, returned " \
21446                       "${rc}, actual ${difference}"
21447         fi
21448
21449         for i in $(seq 12 21); do
21450                 # If we do not do this, we run the risk of having too many
21451                 # locks and starting lock cancellation while we are checking
21452                 # lock counts.
21453                 cancel_lru_locks osc
21454
21455                 count=$($LCTL get_param -n \
21456                        ldlm.namespaces.$imp_name.lock_unused_count)
21457
21458                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
21459                 rc=$?
21460                 if [ $rc -eq 255 ]; then
21461                         error "Ladvise test ${i} failed, ${rc}"
21462                 fi
21463
21464                 new_count=$($LCTL get_param -n \
21465                        ldlm.namespaces.$imp_name.lock_unused_count)
21466                 difference="$((new_count - count))"
21467
21468                 # Test 15 output is divided by 100 to map down to valid return
21469                 if [ $i -eq 15 ]; then
21470                         rc="$((rc * 100))"
21471                 fi
21472
21473                 if [ $difference -ne $rc ]; then
21474                         error "Ladvise test ${i}, bad lock count, returned " \
21475                               "${rc}, actual ${difference}"
21476                 fi
21477         done
21478
21479         #test 22 returns only success/failure
21480         i=22
21481         lockahead_test -d $DIR/$tdir -t $i -f $tfile
21482         rc=$?
21483         if [ $rc -eq 255 ]; then
21484                 error "Ladvise test${i} failed, ${rc}"
21485         fi
21486 }
21487 run_test 255c "suite of ladvise lockahead tests"
21488
21489 test_256() {
21490         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21491         remote_mds_nodsh && skip "remote MDS with nodsh"
21492         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
21493         changelog_users $SINGLEMDS | grep "^cl" &&
21494                 skip "active changelog user"
21495
21496         local cl_user
21497         local cat_sl
21498         local mdt_dev
21499
21500         mdt_dev=$(mdsdevname 1)
21501         echo $mdt_dev
21502
21503         changelog_register || error "changelog_register failed"
21504
21505         rm -rf $DIR/$tdir
21506         mkdir_on_mdt0 $DIR/$tdir
21507
21508         changelog_clear 0 || error "changelog_clear failed"
21509
21510         # change something
21511         touch $DIR/$tdir/{1..10}
21512
21513         # stop the MDT
21514         stop $SINGLEMDS || error "Fail to stop MDT"
21515
21516         # remount the MDT
21517
21518         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
21519
21520         #after mount new plainllog is used
21521         touch $DIR/$tdir/{11..19}
21522         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
21523         stack_trap "rm -f $tmpfile"
21524         cat_sl=$(do_facet $SINGLEMDS "sync; \
21525                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
21526                  llog_reader $tmpfile | grep -c type=1064553b")
21527         do_facet $SINGLEMDS llog_reader $tmpfile
21528
21529         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
21530
21531         changelog_clear 0 || error "changelog_clear failed"
21532
21533         cat_sl=$(do_facet $SINGLEMDS "sync; \
21534                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
21535                  llog_reader $tmpfile | grep -c type=1064553b")
21536
21537         if (( cat_sl == 2 )); then
21538                 error "Empty plain llog was not deleted from changelog catalog"
21539         elif (( cat_sl != 1 )); then
21540                 error "Active plain llog shouldn't be deleted from catalog"
21541         fi
21542 }
21543 run_test 256 "Check llog delete for empty and not full state"
21544
21545 test_257() {
21546         remote_mds_nodsh && skip "remote MDS with nodsh"
21547         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21548                 skip "Need MDS version at least 2.8.55"
21549
21550         test_mkdir $DIR/$tdir
21551
21552         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
21553                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
21554         stat $DIR/$tdir
21555
21556 #define OBD_FAIL_MDS_XATTR_REP                  0x161
21557         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
21558         local facet=mds$((mdtidx + 1))
21559         set_nodes_failloc $(facet_active_host $facet) 0x80000161
21560         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
21561
21562         stop $facet || error "stop MDS failed"
21563         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
21564                 error "start MDS fail"
21565         wait_recovery_complete $facet
21566 }
21567 run_test 257 "xattr locks are not lost"
21568
21569 # Verify we take the i_mutex when security requires it
21570 test_258a() {
21571 #define OBD_FAIL_IMUTEX_SEC 0x141c
21572         $LCTL set_param fail_loc=0x141c
21573         touch $DIR/$tfile
21574         chmod u+s $DIR/$tfile
21575         chmod a+rwx $DIR/$tfile
21576         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
21577         RC=$?
21578         if [ $RC -ne 0 ]; then
21579                 error "error, failed to take i_mutex, rc=$?"
21580         fi
21581         rm -f $DIR/$tfile
21582 }
21583 run_test 258a "verify i_mutex security behavior when suid attributes is set"
21584
21585 # Verify we do NOT take the i_mutex in the normal case
21586 test_258b() {
21587 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
21588         $LCTL set_param fail_loc=0x141d
21589         touch $DIR/$tfile
21590         chmod a+rwx $DIR
21591         chmod a+rw $DIR/$tfile
21592         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
21593         RC=$?
21594         if [ $RC -ne 0 ]; then
21595                 error "error, took i_mutex unnecessarily, rc=$?"
21596         fi
21597         rm -f $DIR/$tfile
21598
21599 }
21600 run_test 258b "verify i_mutex security behavior"
21601
21602 test_259() {
21603         local file=$DIR/$tfile
21604         local before
21605         local after
21606
21607         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
21608
21609         stack_trap "rm -f $file" EXIT
21610
21611         wait_delete_completed
21612         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21613         echo "before: $before"
21614
21615         $LFS setstripe -i 0 -c 1 $file
21616         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
21617         sync_all_data
21618         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21619         echo "after write: $after"
21620
21621 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
21622         do_facet ost1 $LCTL set_param fail_loc=0x2301
21623         $TRUNCATE $file 0
21624         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21625         echo "after truncate: $after"
21626
21627         stop ost1
21628         do_facet ost1 $LCTL set_param fail_loc=0
21629         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21630         sleep 2
21631         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
21632         echo "after restart: $after"
21633         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
21634                 error "missing truncate?"
21635
21636         return 0
21637 }
21638 run_test 259 "crash at delayed truncate"
21639
21640 test_260() {
21641 #define OBD_FAIL_MDC_CLOSE               0x806
21642         $LCTL set_param fail_loc=0x80000806
21643         touch $DIR/$tfile
21644
21645 }
21646 run_test 260 "Check mdc_close fail"
21647
21648 ### Data-on-MDT sanity tests ###
21649 test_270a() {
21650         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21651                 skip "Need MDS version at least 2.10.55 for DoM"
21652
21653         # create DoM file
21654         local dom=$DIR/$tdir/dom_file
21655         local tmp=$DIR/$tdir/tmp_file
21656
21657         mkdir_on_mdt0 $DIR/$tdir
21658
21659         # basic checks for DoM component creation
21660         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
21661                 error "Can set MDT layout to non-first entry"
21662
21663         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
21664                 error "Can define multiple entries as MDT layout"
21665
21666         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
21667
21668         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
21669         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
21670         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
21671
21672         local mdtidx=$($LFS getstripe -m $dom)
21673         local mdtname=MDT$(printf %04x $mdtidx)
21674         local facet=mds$((mdtidx + 1))
21675         local space_check=1
21676
21677         # Skip free space checks with ZFS
21678         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
21679
21680         # write
21681         sync
21682         local size_tmp=$((65536 * 3))
21683         local mdtfree1=$(do_facet $facet \
21684                          lctl get_param -n osd*.*$mdtname.kbytesfree)
21685
21686         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
21687         # check also direct IO along write
21688         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
21689         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
21690         sync
21691         cmp $tmp $dom || error "file data is different"
21692         [ $(stat -c%s $dom) == $size_tmp ] ||
21693                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
21694         if [ $space_check == 1 ]; then
21695                 local mdtfree2=$(do_facet $facet \
21696                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
21697
21698                 # increase in usage from by $size_tmp
21699                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
21700                         error "MDT free space wrong after write: " \
21701                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
21702         fi
21703
21704         # truncate
21705         local size_dom=10000
21706
21707         $TRUNCATE $dom $size_dom
21708         [ $(stat -c%s $dom) == $size_dom ] ||
21709                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
21710         if [ $space_check == 1 ]; then
21711                 mdtfree1=$(do_facet $facet \
21712                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21713                 # decrease in usage from $size_tmp to new $size_dom
21714                 [ $(($mdtfree1 - $mdtfree2)) -ge \
21715                   $(((size_tmp - size_dom) / 1024)) ] ||
21716                         error "MDT free space is wrong after truncate: " \
21717                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
21718         fi
21719
21720         # append
21721         cat $tmp >> $dom
21722         sync
21723         size_dom=$((size_dom + size_tmp))
21724         [ $(stat -c%s $dom) == $size_dom ] ||
21725                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
21726         if [ $space_check == 1 ]; then
21727                 mdtfree2=$(do_facet $facet \
21728                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21729                 # increase in usage by $size_tmp from previous
21730                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
21731                         error "MDT free space is wrong after append: " \
21732                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
21733         fi
21734
21735         # delete
21736         rm $dom
21737         if [ $space_check == 1 ]; then
21738                 mdtfree1=$(do_facet $facet \
21739                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
21740                 # decrease in usage by $size_dom from previous
21741                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
21742                         error "MDT free space is wrong after removal: " \
21743                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
21744         fi
21745
21746         # combined striping
21747         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
21748                 error "Can't create DoM + OST striping"
21749
21750         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
21751         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
21752         # check also direct IO along write
21753         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
21754         sync
21755         cmp $tmp $dom || error "file data is different"
21756         [ $(stat -c%s $dom) == $size_tmp ] ||
21757                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
21758         rm $dom $tmp
21759
21760         return 0
21761 }
21762 run_test 270a "DoM: basic functionality tests"
21763
21764 test_270b() {
21765         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21766                 skip "Need MDS version at least 2.10.55"
21767
21768         local dom=$DIR/$tdir/dom_file
21769         local max_size=1048576
21770
21771         mkdir -p $DIR/$tdir
21772         $LFS setstripe -E $max_size -L mdt $dom
21773
21774         # truncate over the limit
21775         $TRUNCATE $dom $(($max_size + 1)) &&
21776                 error "successful truncate over the maximum size"
21777         # write over the limit
21778         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
21779                 error "successful write over the maximum size"
21780         # append over the limit
21781         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
21782         echo "12345" >> $dom && error "successful append over the maximum size"
21783         rm $dom
21784
21785         return 0
21786 }
21787 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
21788
21789 test_270c() {
21790         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21791                 skip "Need MDS version at least 2.10.55"
21792
21793         mkdir -p $DIR/$tdir
21794         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21795
21796         # check files inherit DoM EA
21797         touch $DIR/$tdir/first
21798         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
21799                 error "bad pattern"
21800         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
21801                 error "bad stripe count"
21802         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
21803                 error "bad stripe size"
21804
21805         # check directory inherits DoM EA and uses it as default
21806         mkdir $DIR/$tdir/subdir
21807         touch $DIR/$tdir/subdir/second
21808         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
21809                 error "bad pattern in sub-directory"
21810         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
21811                 error "bad stripe count in sub-directory"
21812         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
21813                 error "bad stripe size in sub-directory"
21814         return 0
21815 }
21816 run_test 270c "DoM: DoM EA inheritance tests"
21817
21818 test_270d() {
21819         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21820                 skip "Need MDS version at least 2.10.55"
21821
21822         mkdir -p $DIR/$tdir
21823         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
21824
21825         # inherit default DoM striping
21826         mkdir $DIR/$tdir/subdir
21827         touch $DIR/$tdir/subdir/f1
21828
21829         # change default directory striping
21830         $LFS setstripe -c 1 $DIR/$tdir/subdir
21831         touch $DIR/$tdir/subdir/f2
21832         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
21833                 error "wrong default striping in file 2"
21834         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
21835                 error "bad pattern in file 2"
21836         return 0
21837 }
21838 run_test 270d "DoM: change striping from DoM to RAID0"
21839
21840 test_270e() {
21841         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21842                 skip "Need MDS version at least 2.10.55"
21843
21844         mkdir -p $DIR/$tdir/dom
21845         mkdir -p $DIR/$tdir/norm
21846         DOMFILES=20
21847         NORMFILES=10
21848         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
21849         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
21850
21851         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
21852         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
21853
21854         # find DoM files by layout
21855         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
21856         [ $NUM -eq  $DOMFILES ] ||
21857                 error "lfs find -L: found $NUM, expected $DOMFILES"
21858         echo "Test 1: lfs find 20 DOM files by layout: OK"
21859
21860         # there should be 1 dir with default DOM striping
21861         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
21862         [ $NUM -eq  1 ] ||
21863                 error "lfs find -L: found $NUM, expected 1 dir"
21864         echo "Test 2: lfs find 1 DOM dir by layout: OK"
21865
21866         # find DoM files by stripe size
21867         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
21868         [ $NUM -eq  $DOMFILES ] ||
21869                 error "lfs find -S: found $NUM, expected $DOMFILES"
21870         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
21871
21872         # find files by stripe offset except DoM files
21873         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
21874         [ $NUM -eq  $NORMFILES ] ||
21875                 error "lfs find -i: found $NUM, expected $NORMFILES"
21876         echo "Test 5: lfs find no DOM files by stripe index: OK"
21877         return 0
21878 }
21879 run_test 270e "DoM: lfs find with DoM files test"
21880
21881 test_270f() {
21882         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
21883                 skip "Need MDS version at least 2.10.55"
21884
21885         local mdtname=${FSNAME}-MDT0000-mdtlov
21886         local dom=$DIR/$tdir/dom_file
21887         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
21888                                                 lod.$mdtname.dom_stripesize)
21889         local dom_limit=131072
21890
21891         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
21892         local dom_current=$(do_facet mds1 $LCTL get_param -n \
21893                                                 lod.$mdtname.dom_stripesize)
21894         [ ${dom_limit} -eq ${dom_current} ] ||
21895                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
21896
21897         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21898         $LFS setstripe -d $DIR/$tdir
21899         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
21900                 error "Can't set directory default striping"
21901
21902         # exceed maximum stripe size
21903         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
21904                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
21905         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
21906                 error "Able to create DoM component size more than LOD limit"
21907
21908         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
21909         dom_current=$(do_facet mds1 $LCTL get_param -n \
21910                                                 lod.$mdtname.dom_stripesize)
21911         [ 0 -eq ${dom_current} ] ||
21912                 error "Can't set zero DoM stripe limit"
21913         rm $dom
21914
21915         # attempt to create DoM file on server with disabled DoM should
21916         # remove DoM entry from layout and be succeed
21917         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
21918                 error "Can't create DoM file (DoM is disabled)"
21919         [ $($LFS getstripe -L $dom) == "mdt" ] &&
21920                 error "File has DoM component while DoM is disabled"
21921         rm $dom
21922
21923         # attempt to create DoM file with only DoM stripe should return error
21924         $LFS setstripe -E $dom_limit -L mdt $dom &&
21925                 error "Able to create DoM-only file while DoM is disabled"
21926
21927         # too low values to be aligned with smallest stripe size 64K
21928         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
21929         dom_current=$(do_facet mds1 $LCTL get_param -n \
21930                                                 lod.$mdtname.dom_stripesize)
21931         [ 30000 -eq ${dom_current} ] &&
21932                 error "Can set too small DoM stripe limit"
21933
21934         # 64K is a minimal stripe size in Lustre, expect limit of that size
21935         [ 65536 -eq ${dom_current} ] ||
21936                 error "Limit is not set to 64K but ${dom_current}"
21937
21938         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
21939         dom_current=$(do_facet mds1 $LCTL get_param -n \
21940                                                 lod.$mdtname.dom_stripesize)
21941         echo $dom_current
21942         [ 2147483648 -eq ${dom_current} ] &&
21943                 error "Can set too large DoM stripe limit"
21944
21945         do_facet mds1 $LCTL set_param -n \
21946                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
21947         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
21948                 error "Can't create DoM component size after limit change"
21949         do_facet mds1 $LCTL set_param -n \
21950                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
21951         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
21952                 error "Can't create DoM file after limit decrease"
21953         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
21954                 error "Can create big DoM component after limit decrease"
21955         touch ${dom}_def ||
21956                 error "Can't create file with old default layout"
21957
21958         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
21959         return 0
21960 }
21961 run_test 270f "DoM: maximum DoM stripe size checks"
21962
21963 test_270g() {
21964         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
21965                 skip "Need MDS version at least 2.13.52"
21966         local dom=$DIR/$tdir/$tfile
21967
21968         $LFS mkdir -i 0 -c 1 $DIR/$tdir
21969         local lodname=${FSNAME}-MDT0000-mdtlov
21970
21971         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
21972         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
21973         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
21974         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
21975
21976         local dom_limit=1024
21977         local dom_threshold="50%"
21978
21979         $LFS setstripe -d $DIR/$tdir
21980         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
21981                 error "Can't set directory default striping"
21982
21983         do_facet mds1 $LCTL set_param -n \
21984                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
21985         # set 0 threshold and create DOM file to change tunable stripesize
21986         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
21987         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
21988                 error "Failed to create $dom file"
21989         # now tunable dom_cur_stripesize should reach maximum
21990         local dom_current=$(do_facet mds1 $LCTL get_param -n \
21991                                         lod.${lodname}.dom_stripesize_cur_kb)
21992         [[ $dom_current == $dom_limit ]] ||
21993                 error "Current DOM stripesize is not maximum"
21994         rm $dom
21995
21996         # set threshold for further tests
21997         do_facet mds1 $LCTL set_param -n \
21998                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
21999         echo "DOM threshold is $dom_threshold free space"
22000         local dom_def
22001         local dom_set
22002         # Spoof bfree to exceed threshold
22003         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
22004         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
22005         for spfree in 40 20 0 15 30 55; do
22006                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
22007                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
22008                         error "Failed to create $dom file"
22009                 dom_def=$(do_facet mds1 $LCTL get_param -n \
22010                                         lod.${lodname}.dom_stripesize_cur_kb)
22011                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
22012                 [[ $dom_def != $dom_current ]] ||
22013                         error "Default stripe size was not changed"
22014                 if [[ $spfree > 0 ]] ; then
22015                         dom_set=$($LFS getstripe -S $dom)
22016                         [[ $dom_set == $((dom_def * 1024)) ]] ||
22017                                 error "DOM component size is still old"
22018                 else
22019                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
22020                                 error "DoM component is set with no free space"
22021                 fi
22022                 rm $dom
22023                 dom_current=$dom_def
22024         done
22025 }
22026 run_test 270g "DoM: default DoM stripe size depends on free space"
22027
22028 test_270h() {
22029         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
22030                 skip "Need MDS version at least 2.13.53"
22031
22032         local mdtname=${FSNAME}-MDT0000-mdtlov
22033         local dom=$DIR/$tdir/$tfile
22034         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22035
22036         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
22037         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22038
22039         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22040         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
22041                 error "can't create OST file"
22042         # mirrored file with DOM entry in the second mirror
22043         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
22044                 error "can't create mirror with DoM component"
22045
22046         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
22047
22048         # DOM component in the middle and has other enries in the same mirror,
22049         # should succeed but lost DoM component
22050         $LFS setstripe --copy=${dom}_1 $dom ||
22051                 error "Can't create file from OST|DOM mirror layout"
22052         # check new file has no DoM layout after all
22053         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
22054                 error "File has DoM component while DoM is disabled"
22055 }
22056 run_test 270h "DoM: DoM stripe removal when disabled on server"
22057
22058 test_270i() {
22059         (( $MDS1_VERSION >= $(version_code 2.14.54) )) ||
22060                 skip "Need MDS version at least 2.14.54"
22061
22062         mkdir $DIR/$tdir
22063         $LFS setstripe -L mdt -S 128k -c -1 $DIR/$tdir &&
22064                 error "setstripe should fail" || true
22065 }
22066 run_test 270i "DoM: setting invalid DoM striping should fail"
22067
22068 test_271a() {
22069         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22070                 skip "Need MDS version at least 2.10.55"
22071
22072         local dom=$DIR/$tdir/dom
22073
22074         mkdir -p $DIR/$tdir
22075
22076         $LFS setstripe -E 1024K -L mdt $dom
22077
22078         lctl set_param -n mdc.*.stats=clear
22079         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
22080         cat $dom > /dev/null
22081         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
22082         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
22083         ls $dom
22084         rm -f $dom
22085 }
22086 run_test 271a "DoM: data is cached for read after write"
22087
22088 test_271b() {
22089         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22090                 skip "Need MDS version at least 2.10.55"
22091
22092         local dom=$DIR/$tdir/dom
22093
22094         mkdir -p $DIR/$tdir
22095
22096         $LFS setstripe -E 1024K -L mdt -E EOF $dom
22097
22098         lctl set_param -n mdc.*.stats=clear
22099         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
22100         cancel_lru_locks mdc
22101         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
22102         # second stat to check size is cached on client
22103         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
22104         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
22105         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
22106         rm -f $dom
22107 }
22108 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
22109
22110 test_271ba() {
22111         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22112                 skip "Need MDS version at least 2.10.55"
22113
22114         local dom=$DIR/$tdir/dom
22115
22116         mkdir -p $DIR/$tdir
22117
22118         $LFS setstripe -E 1024K -L mdt -E EOF $dom
22119
22120         lctl set_param -n mdc.*.stats=clear
22121         lctl set_param -n osc.*.stats=clear
22122         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
22123         cancel_lru_locks mdc
22124         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
22125         # second stat to check size is cached on client
22126         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
22127         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
22128         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
22129         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
22130         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
22131         rm -f $dom
22132 }
22133 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
22134
22135
22136 get_mdc_stats() {
22137         local mdtidx=$1
22138         local param=$2
22139         local mdt=MDT$(printf %04x $mdtidx)
22140
22141         if [ -z $param ]; then
22142                 lctl get_param -n mdc.*$mdt*.stats
22143         else
22144                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
22145         fi
22146 }
22147
22148 test_271c() {
22149         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22150                 skip "Need MDS version at least 2.10.55"
22151
22152         local dom=$DIR/$tdir/dom
22153
22154         mkdir -p $DIR/$tdir
22155
22156         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22157
22158         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
22159         local facet=mds$((mdtidx + 1))
22160
22161         cancel_lru_locks mdc
22162         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
22163         createmany -o $dom 1000
22164         lctl set_param -n mdc.*.stats=clear
22165         smalliomany -w $dom 1000 200
22166         get_mdc_stats $mdtidx
22167         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
22168         # Each file has 1 open, 1 IO enqueues, total 2000
22169         # but now we have also +1 getxattr for security.capability, total 3000
22170         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
22171         unlinkmany $dom 1000
22172
22173         cancel_lru_locks mdc
22174         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
22175         createmany -o $dom 1000
22176         lctl set_param -n mdc.*.stats=clear
22177         smalliomany -w $dom 1000 200
22178         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
22179         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
22180         # for OPEN and IO lock.
22181         [ $((enq - enq_2)) -ge 1000 ] ||
22182                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
22183         unlinkmany $dom 1000
22184         return 0
22185 }
22186 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
22187
22188 cleanup_271def_tests() {
22189         trap 0
22190         rm -f $1
22191 }
22192
22193 test_271d() {
22194         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
22195                 skip "Need MDS version at least 2.10.57"
22196
22197         local dom=$DIR/$tdir/dom
22198         local tmp=$TMP/$tfile
22199         trap "cleanup_271def_tests $tmp" EXIT
22200
22201         mkdir -p $DIR/$tdir
22202
22203         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22204
22205         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
22206
22207         cancel_lru_locks mdc
22208         dd if=/dev/urandom of=$tmp bs=1000 count=1
22209         dd if=$tmp of=$dom bs=1000 count=1
22210         cancel_lru_locks mdc
22211
22212         cat /etc/hosts >> $tmp
22213         lctl set_param -n mdc.*.stats=clear
22214
22215         # append data to the same file it should update local page
22216         echo "Append to the same page"
22217         cat /etc/hosts >> $dom
22218         local num=$(get_mdc_stats $mdtidx ost_read)
22219         local ra=$(get_mdc_stats $mdtidx req_active)
22220         local rw=$(get_mdc_stats $mdtidx req_waittime)
22221
22222         [ -z $num ] || error "$num READ RPC occured"
22223         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22224         echo "... DONE"
22225
22226         # compare content
22227         cmp $tmp $dom || error "file miscompare"
22228
22229         cancel_lru_locks mdc
22230         lctl set_param -n mdc.*.stats=clear
22231
22232         echo "Open and read file"
22233         cat $dom > /dev/null
22234         local num=$(get_mdc_stats $mdtidx ost_read)
22235         local ra=$(get_mdc_stats $mdtidx req_active)
22236         local rw=$(get_mdc_stats $mdtidx req_waittime)
22237
22238         [ -z $num ] || error "$num READ RPC occured"
22239         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22240         echo "... DONE"
22241
22242         # compare content
22243         cmp $tmp $dom || error "file miscompare"
22244
22245         return 0
22246 }
22247 run_test 271d "DoM: read on open (1K file in reply buffer)"
22248
22249 test_271f() {
22250         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
22251                 skip "Need MDS version at least 2.10.57"
22252
22253         local dom=$DIR/$tdir/dom
22254         local tmp=$TMP/$tfile
22255         trap "cleanup_271def_tests $tmp" EXIT
22256
22257         mkdir -p $DIR/$tdir
22258
22259         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22260
22261         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
22262
22263         cancel_lru_locks mdc
22264         dd if=/dev/urandom of=$tmp bs=265000 count=1
22265         dd if=$tmp of=$dom bs=265000 count=1
22266         cancel_lru_locks mdc
22267         cat /etc/hosts >> $tmp
22268         lctl set_param -n mdc.*.stats=clear
22269
22270         echo "Append to the same page"
22271         cat /etc/hosts >> $dom
22272         local num=$(get_mdc_stats $mdtidx ost_read)
22273         local ra=$(get_mdc_stats $mdtidx req_active)
22274         local rw=$(get_mdc_stats $mdtidx req_waittime)
22275
22276         [ -z $num ] || error "$num READ RPC occured"
22277         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22278         echo "... DONE"
22279
22280         # compare content
22281         cmp $tmp $dom || error "file miscompare"
22282
22283         cancel_lru_locks mdc
22284         lctl set_param -n mdc.*.stats=clear
22285
22286         echo "Open and read file"
22287         cat $dom > /dev/null
22288         local num=$(get_mdc_stats $mdtidx ost_read)
22289         local ra=$(get_mdc_stats $mdtidx req_active)
22290         local rw=$(get_mdc_stats $mdtidx req_waittime)
22291
22292         [ -z $num ] && num=0
22293         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
22294         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
22295         echo "... DONE"
22296
22297         # compare content
22298         cmp $tmp $dom || error "file miscompare"
22299
22300         return 0
22301 }
22302 run_test 271f "DoM: read on open (200K file and read tail)"
22303
22304 test_271g() {
22305         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
22306                 skip "Skipping due to old client or server version"
22307
22308         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
22309         # to get layout
22310         $CHECKSTAT -t file $DIR1/$tfile
22311
22312         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
22313         MULTIOP_PID=$!
22314         sleep 1
22315         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
22316         $LCTL set_param fail_loc=0x80000314
22317         rm $DIR1/$tfile || error "Unlink fails"
22318         RC=$?
22319         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
22320         [ $RC -eq 0 ] || error "Failed write to stale object"
22321 }
22322 run_test 271g "Discard DoM data vs client flush race"
22323
22324 test_272a() {
22325         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22326                 skip "Need MDS version at least 2.11.50"
22327
22328         local dom=$DIR/$tdir/dom
22329         mkdir -p $DIR/$tdir
22330
22331         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
22332         dd if=/dev/urandom of=$dom bs=512K count=1 ||
22333                 error "failed to write data into $dom"
22334         local old_md5=$(md5sum $dom)
22335
22336         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
22337                 error "failed to migrate to the same DoM component"
22338
22339         local new_md5=$(md5sum $dom)
22340
22341         [ "$old_md5" == "$new_md5" ] ||
22342                 error "md5sum differ: $old_md5, $new_md5"
22343
22344         [ $($LFS getstripe -c $dom) -eq 2 ] ||
22345                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
22346 }
22347 run_test 272a "DoM migration: new layout with the same DOM component"
22348
22349 test_272b() {
22350         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22351                 skip "Need MDS version at least 2.11.50"
22352
22353         local dom=$DIR/$tdir/dom
22354         mkdir -p $DIR/$tdir
22355         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22356
22357         local mdtidx=$($LFS getstripe -m $dom)
22358         local mdtname=MDT$(printf %04x $mdtidx)
22359         local facet=mds$((mdtidx + 1))
22360
22361         local mdtfree1=$(do_facet $facet \
22362                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22363         dd if=/dev/urandom of=$dom bs=2M count=1 ||
22364                 error "failed to write data into $dom"
22365         local old_md5=$(md5sum $dom)
22366         cancel_lru_locks mdc
22367         local mdtfree1=$(do_facet $facet \
22368                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22369
22370         $LFS migrate -c2 $dom ||
22371                 error "failed to migrate to the new composite layout"
22372         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22373                 error "MDT stripe was not removed"
22374
22375         cancel_lru_locks mdc
22376         local new_md5=$(md5sum $dom)
22377         [ "$old_md5" == "$new_md5" ] ||
22378                 error "$old_md5 != $new_md5"
22379
22380         # Skip free space checks with ZFS
22381         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22382                 local mdtfree2=$(do_facet $facet \
22383                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22384                 [ $mdtfree2 -gt $mdtfree1 ] ||
22385                         error "MDT space is not freed after migration"
22386         fi
22387         return 0
22388 }
22389 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
22390
22391 test_272c() {
22392         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22393                 skip "Need MDS version at least 2.11.50"
22394
22395         local dom=$DIR/$tdir/$tfile
22396         mkdir -p $DIR/$tdir
22397         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22398
22399         local mdtidx=$($LFS getstripe -m $dom)
22400         local mdtname=MDT$(printf %04x $mdtidx)
22401         local facet=mds$((mdtidx + 1))
22402
22403         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
22404                 error "failed to write data into $dom"
22405         local old_md5=$(md5sum $dom)
22406         cancel_lru_locks mdc
22407         local mdtfree1=$(do_facet $facet \
22408                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22409
22410         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
22411                 error "failed to migrate to the new composite layout"
22412         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
22413                 error "MDT stripe was not removed"
22414
22415         cancel_lru_locks mdc
22416         local new_md5=$(md5sum $dom)
22417         [ "$old_md5" == "$new_md5" ] ||
22418                 error "$old_md5 != $new_md5"
22419
22420         # Skip free space checks with ZFS
22421         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22422                 local mdtfree2=$(do_facet $facet \
22423                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22424                 [ $mdtfree2 -gt $mdtfree1 ] ||
22425                         error "MDS space is not freed after migration"
22426         fi
22427         return 0
22428 }
22429 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
22430
22431 test_272d() {
22432         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22433                 skip "Need MDS version at least 2.12.55"
22434
22435         local dom=$DIR/$tdir/$tfile
22436         mkdir -p $DIR/$tdir
22437         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
22438
22439         local mdtidx=$($LFS getstripe -m $dom)
22440         local mdtname=MDT$(printf %04x $mdtidx)
22441         local facet=mds$((mdtidx + 1))
22442
22443         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
22444                 error "failed to write data into $dom"
22445         local old_md5=$(md5sum $dom)
22446         cancel_lru_locks mdc
22447         local mdtfree1=$(do_facet $facet \
22448                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22449
22450         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
22451                 error "failed mirroring to the new composite layout"
22452         $LFS mirror resync $dom ||
22453                 error "failed mirror resync"
22454         $LFS mirror split --mirror-id 1 -d $dom ||
22455                 error "failed mirror split"
22456
22457         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22458                 error "MDT stripe was not removed"
22459
22460         cancel_lru_locks mdc
22461         local new_md5=$(md5sum $dom)
22462         [ "$old_md5" == "$new_md5" ] ||
22463                 error "$old_md5 != $new_md5"
22464
22465         # Skip free space checks with ZFS
22466         if [ "$(facet_fstype $facet)" != "zfs" ]; then
22467                 local mdtfree2=$(do_facet $facet \
22468                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22469                 [ $mdtfree2 -gt $mdtfree1 ] ||
22470                         error "MDS space is not freed after DOM mirror deletion"
22471         fi
22472         return 0
22473 }
22474 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
22475
22476 test_272e() {
22477         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22478                 skip "Need MDS version at least 2.12.55"
22479
22480         local dom=$DIR/$tdir/$tfile
22481         mkdir -p $DIR/$tdir
22482         $LFS setstripe -c 2 $dom
22483
22484         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
22485                 error "failed to write data into $dom"
22486         local old_md5=$(md5sum $dom)
22487         cancel_lru_locks mdc
22488
22489         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
22490                 error "failed mirroring to the DOM layout"
22491         $LFS mirror resync $dom ||
22492                 error "failed mirror resync"
22493         $LFS mirror split --mirror-id 1 -d $dom ||
22494                 error "failed mirror split"
22495
22496         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
22497                 error "MDT stripe was not removed"
22498
22499         cancel_lru_locks mdc
22500         local new_md5=$(md5sum $dom)
22501         [ "$old_md5" == "$new_md5" ] ||
22502                 error "$old_md5 != $new_md5"
22503
22504         return 0
22505 }
22506 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
22507
22508 test_272f() {
22509         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
22510                 skip "Need MDS version at least 2.12.55"
22511
22512         local dom=$DIR/$tdir/$tfile
22513         mkdir -p $DIR/$tdir
22514         $LFS setstripe -c 2 $dom
22515
22516         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
22517                 error "failed to write data into $dom"
22518         local old_md5=$(md5sum $dom)
22519         cancel_lru_locks mdc
22520
22521         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
22522                 error "failed migrating to the DOM file"
22523
22524         cancel_lru_locks mdc
22525         local new_md5=$(md5sum $dom)
22526         [ "$old_md5" != "$new_md5" ] &&
22527                 error "$old_md5 != $new_md5"
22528
22529         return 0
22530 }
22531 run_test 272f "DoM migration: OST-striped file to DOM file"
22532
22533 test_273a() {
22534         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
22535                 skip "Need MDS version at least 2.11.50"
22536
22537         # Layout swap cannot be done if either file has DOM component,
22538         # this will never be supported, migration should be used instead
22539
22540         local dom=$DIR/$tdir/$tfile
22541         mkdir -p $DIR/$tdir
22542
22543         $LFS setstripe -c2 ${dom}_plain
22544         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
22545         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
22546                 error "can swap layout with DoM component"
22547         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
22548                 error "can swap layout with DoM component"
22549
22550         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
22551         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
22552                 error "can swap layout with DoM component"
22553         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
22554                 error "can swap layout with DoM component"
22555         return 0
22556 }
22557 run_test 273a "DoM: layout swapping should fail with DOM"
22558
22559 test_273b() {
22560         mkdir -p $DIR/$tdir
22561         $LFS setstripe -E 1M -L mdt -E -1 -c -1 $DIR/$tdir
22562
22563 #define OBD_FAIL_MDS_COMMITRW_DELAY      0x16b
22564         do_facet mds1 $LCTL set_param fail_loc=0x8000016b fail_val=2
22565
22566         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
22567 }
22568 run_test 273b "DoM: race writeback and object destroy"
22569
22570 test_275() {
22571         remote_ost_nodsh && skip "remote OST with nodsh"
22572         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
22573                 skip "Need OST version >= 2.10.57"
22574
22575         local file=$DIR/$tfile
22576         local oss
22577
22578         oss=$(comma_list $(osts_nodes))
22579
22580         dd if=/dev/urandom of=$file bs=1M count=2 ||
22581                 error "failed to create a file"
22582         cancel_lru_locks osc
22583
22584         #lock 1
22585         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
22586                 error "failed to read a file"
22587
22588 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
22589         $LCTL set_param fail_loc=0x8000031f
22590
22591         cancel_lru_locks osc &
22592         sleep 1
22593
22594 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
22595         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
22596         #IO takes another lock, but matches the PENDING one
22597         #and places it to the IO RPC
22598         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
22599                 error "failed to read a file with PENDING lock"
22600 }
22601 run_test 275 "Read on a canceled duplicate lock"
22602
22603 test_276() {
22604         remote_ost_nodsh && skip "remote OST with nodsh"
22605         local pid
22606
22607         do_facet ost1 "(while true; do \
22608                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
22609                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
22610         pid=$!
22611
22612         for LOOP in $(seq 20); do
22613                 stop ost1
22614                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
22615         done
22616         kill -9 $pid
22617         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
22618                 rm $TMP/sanity_276_pid"
22619 }
22620 run_test 276 "Race between mount and obd_statfs"
22621
22622 test_277() {
22623         $LCTL set_param ldlm.namespaces.*.lru_size=0
22624         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
22625         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
22626                         grep ^used_mb | awk '{print $2}')
22627         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
22628         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
22629                 oflag=direct conv=notrunc
22630         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
22631                         grep ^used_mb | awk '{print $2}')
22632         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
22633 }
22634 run_test 277 "Direct IO shall drop page cache"
22635
22636 test_278() {
22637         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
22638         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
22639         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
22640                 skip "needs the same host for mdt1 mdt2" && return
22641
22642         local pid1
22643         local pid2
22644
22645 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
22646         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
22647         stop mds2 &
22648         pid2=$!
22649
22650         stop mds1
22651
22652         echo "Starting MDTs"
22653         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
22654         wait $pid2
22655 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
22656 #will return NULL
22657         do_facet mds2 $LCTL set_param fail_loc=0
22658
22659         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
22660         wait_recovery_complete mds2
22661 }
22662 run_test 278 "Race starting MDS between MDTs stop/start"
22663
22664 test_280() {
22665         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
22666                 skip "Need MGS version at least 2.13.52"
22667         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22668         combined_mgs_mds || skip "needs combined MGS/MDT"
22669
22670         umount_client $MOUNT
22671 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
22672         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
22673
22674         mount_client $MOUNT &
22675         sleep 1
22676         stop mgs || error "stop mgs failed"
22677         #for a race mgs would crash
22678         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
22679         # make sure we unmount client before remounting
22680         wait
22681         umount_client $MOUNT
22682         mount_client $MOUNT || error "mount client failed"
22683 }
22684 run_test 280 "Race between MGS umount and client llog processing"
22685
22686 cleanup_test_300() {
22687         trap 0
22688         umask $SAVE_UMASK
22689 }
22690 test_striped_dir() {
22691         local mdt_index=$1
22692         local stripe_count
22693         local stripe_index
22694
22695         mkdir -p $DIR/$tdir
22696
22697         SAVE_UMASK=$(umask)
22698         trap cleanup_test_300 RETURN EXIT
22699
22700         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
22701                                                 $DIR/$tdir/striped_dir ||
22702                 error "set striped dir error"
22703
22704         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
22705         [ "$mode" = "755" ] || error "expect 755 got $mode"
22706
22707         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
22708                 error "getdirstripe failed"
22709         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
22710         if [ "$stripe_count" != "2" ]; then
22711                 error "1:stripe_count is $stripe_count, expect 2"
22712         fi
22713         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
22714         if [ "$stripe_count" != "2" ]; then
22715                 error "2:stripe_count is $stripe_count, expect 2"
22716         fi
22717
22718         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
22719         if [ "$stripe_index" != "$mdt_index" ]; then
22720                 error "stripe_index is $stripe_index, expect $mdt_index"
22721         fi
22722
22723         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
22724                 error "nlink error after create striped dir"
22725
22726         mkdir $DIR/$tdir/striped_dir/a
22727         mkdir $DIR/$tdir/striped_dir/b
22728
22729         stat $DIR/$tdir/striped_dir/a ||
22730                 error "create dir under striped dir failed"
22731         stat $DIR/$tdir/striped_dir/b ||
22732                 error "create dir under striped dir failed"
22733
22734         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
22735                 error "nlink error after mkdir"
22736
22737         rmdir $DIR/$tdir/striped_dir/a
22738         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
22739                 error "nlink error after rmdir"
22740
22741         rmdir $DIR/$tdir/striped_dir/b
22742         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
22743                 error "nlink error after rmdir"
22744
22745         chattr +i $DIR/$tdir/striped_dir
22746         createmany -o $DIR/$tdir/striped_dir/f 10 &&
22747                 error "immutable flags not working under striped dir!"
22748         chattr -i $DIR/$tdir/striped_dir
22749
22750         rmdir $DIR/$tdir/striped_dir ||
22751                 error "rmdir striped dir error"
22752
22753         cleanup_test_300
22754
22755         true
22756 }
22757
22758 test_300a() {
22759         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22760                 skip "skipped for lustre < 2.7.0"
22761         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22762         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22763
22764         test_striped_dir 0 || error "failed on striped dir on MDT0"
22765         test_striped_dir 1 || error "failed on striped dir on MDT0"
22766 }
22767 run_test 300a "basic striped dir sanity test"
22768
22769 test_300b() {
22770         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22771                 skip "skipped for lustre < 2.7.0"
22772         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22773         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22774
22775         local i
22776         local mtime1
22777         local mtime2
22778         local mtime3
22779
22780         test_mkdir $DIR/$tdir || error "mkdir fail"
22781         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22782                 error "set striped dir error"
22783         for i in {0..9}; do
22784                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
22785                 sleep 1
22786                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
22787                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
22788                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
22789                 sleep 1
22790                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
22791                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
22792                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
22793         done
22794         true
22795 }
22796 run_test 300b "check ctime/mtime for striped dir"
22797
22798 test_300c() {
22799         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22800                 skip "skipped for lustre < 2.7.0"
22801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22802         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22803
22804         local file_count
22805
22806         mkdir_on_mdt0 $DIR/$tdir
22807         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
22808                 error "set striped dir error"
22809
22810         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
22811                 error "chown striped dir failed"
22812
22813         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
22814                 error "create 5k files failed"
22815
22816         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
22817
22818         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
22819
22820         rm -rf $DIR/$tdir
22821 }
22822 run_test 300c "chown && check ls under striped directory"
22823
22824 test_300d() {
22825         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
22826                 skip "skipped for lustre < 2.7.0"
22827         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22828         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22829
22830         local stripe_count
22831         local file
22832
22833         mkdir -p $DIR/$tdir
22834         $LFS setstripe -c 2 $DIR/$tdir
22835
22836         #local striped directory
22837         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22838                 error "set striped dir error"
22839         #look at the directories for debug purposes
22840         ls -l $DIR/$tdir
22841         $LFS getdirstripe $DIR/$tdir
22842         ls -l $DIR/$tdir/striped_dir
22843         $LFS getdirstripe $DIR/$tdir/striped_dir
22844         createmany -o $DIR/$tdir/striped_dir/f 10 ||
22845                 error "create 10 files failed"
22846
22847         #remote striped directory
22848         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
22849                 error "set striped dir error"
22850         #look at the directories for debug purposes
22851         ls -l $DIR/$tdir
22852         $LFS getdirstripe $DIR/$tdir
22853         ls -l $DIR/$tdir/remote_striped_dir
22854         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
22855         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
22856                 error "create 10 files failed"
22857
22858         for file in $(find $DIR/$tdir); do
22859                 stripe_count=$($LFS getstripe -c $file)
22860                 [ $stripe_count -eq 2 ] ||
22861                         error "wrong stripe $stripe_count for $file"
22862         done
22863
22864         rm -rf $DIR/$tdir
22865 }
22866 run_test 300d "check default stripe under striped directory"
22867
22868 test_300e() {
22869         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22870                 skip "Need MDS version at least 2.7.55"
22871         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22872         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22873
22874         local stripe_count
22875         local file
22876
22877         mkdir -p $DIR/$tdir
22878
22879         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22880                 error "set striped dir error"
22881
22882         touch $DIR/$tdir/striped_dir/a
22883         touch $DIR/$tdir/striped_dir/b
22884         touch $DIR/$tdir/striped_dir/c
22885
22886         mkdir $DIR/$tdir/striped_dir/dir_a
22887         mkdir $DIR/$tdir/striped_dir/dir_b
22888         mkdir $DIR/$tdir/striped_dir/dir_c
22889
22890         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
22891                 error "set striped adir under striped dir error"
22892
22893         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
22894                 error "set striped bdir under striped dir error"
22895
22896         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
22897                 error "set striped cdir under striped dir error"
22898
22899         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
22900                 error "rename dir under striped dir fails"
22901
22902         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
22903                 error "rename dir under different stripes fails"
22904
22905         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
22906                 error "rename file under striped dir should succeed"
22907
22908         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
22909                 error "rename dir under striped dir should succeed"
22910
22911         rm -rf $DIR/$tdir
22912 }
22913 run_test 300e "check rename under striped directory"
22914
22915 test_300f() {
22916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22917         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22918         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
22919                 skip "Need MDS version at least 2.7.55"
22920
22921         local stripe_count
22922         local file
22923
22924         rm -rf $DIR/$tdir
22925         mkdir -p $DIR/$tdir
22926
22927         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
22928                 error "set striped dir error"
22929
22930         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
22931                 error "set striped dir error"
22932
22933         touch $DIR/$tdir/striped_dir/a
22934         mkdir $DIR/$tdir/striped_dir/dir_a
22935         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
22936                 error "create striped dir under striped dir fails"
22937
22938         touch $DIR/$tdir/striped_dir1/b
22939         mkdir $DIR/$tdir/striped_dir1/dir_b
22940         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
22941                 error "create striped dir under striped dir fails"
22942
22943         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
22944                 error "rename dir under different striped dir should fail"
22945
22946         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
22947                 error "rename striped dir under diff striped dir should fail"
22948
22949         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
22950                 error "rename file under diff striped dirs fails"
22951
22952         rm -rf $DIR/$tdir
22953 }
22954 run_test 300f "check rename cross striped directory"
22955
22956 test_300_check_default_striped_dir()
22957 {
22958         local dirname=$1
22959         local default_count=$2
22960         local default_index=$3
22961         local stripe_count
22962         local stripe_index
22963         local dir_stripe_index
22964         local dir
22965
22966         echo "checking $dirname $default_count $default_index"
22967         $LFS setdirstripe -D -c $default_count -i $default_index \
22968                                 -H all_char $DIR/$tdir/$dirname ||
22969                 error "set default stripe on striped dir error"
22970         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
22971         [ $stripe_count -eq $default_count ] ||
22972                 error "expect $default_count get $stripe_count for $dirname"
22973
22974         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
22975         [ $stripe_index -eq $default_index ] ||
22976                 error "expect $default_index get $stripe_index for $dirname"
22977
22978         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
22979                                                 error "create dirs failed"
22980
22981         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
22982         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
22983         for dir in $(find $DIR/$tdir/$dirname/*); do
22984                 stripe_count=$($LFS getdirstripe -c $dir)
22985                 (( $stripe_count == $default_count )) ||
22986                 (( $stripe_count == $MDSCOUNT && $default_count == -1 )) ||
22987                 (( $stripe_count == 0 )) || (( $default_count == 1 )) ||
22988                 error "stripe count $default_count != $stripe_count for $dir"
22989
22990                 stripe_index=$($LFS getdirstripe -i $dir)
22991                 [ $default_index -eq -1 ] ||
22992                         [ $stripe_index -eq $default_index ] ||
22993                         error "$stripe_index != $default_index for $dir"
22994
22995                 #check default stripe
22996                 stripe_count=$($LFS getdirstripe -D -c $dir)
22997                 [ $stripe_count -eq $default_count ] ||
22998                 error "default count $default_count != $stripe_count for $dir"
22999
23000                 stripe_index=$($LFS getdirstripe -D -i $dir)
23001                 [ $stripe_index -eq $default_index ] ||
23002                 error "default index $default_index != $stripe_index for $dir"
23003         done
23004         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
23005 }
23006
23007 test_300g() {
23008         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23009         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23010                 skip "Need MDS version at least 2.7.55"
23011
23012         local dir
23013         local stripe_count
23014         local stripe_index
23015
23016         mkdir_on_mdt0 $DIR/$tdir
23017         mkdir $DIR/$tdir/normal_dir
23018
23019         #Checking when client cache stripe index
23020         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
23021         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
23022                 error "create striped_dir failed"
23023
23024         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
23025                 error "create dir0 fails"
23026         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
23027         [ $stripe_index -eq 0 ] ||
23028                 error "dir0 expect index 0 got $stripe_index"
23029
23030         mkdir $DIR/$tdir/striped_dir/dir1 ||
23031                 error "create dir1 fails"
23032         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
23033         [ $stripe_index -eq 1 ] ||
23034                 error "dir1 expect index 1 got $stripe_index"
23035
23036         #check default stripe count/stripe index
23037         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
23038         test_300_check_default_striped_dir normal_dir 1 0
23039         test_300_check_default_striped_dir normal_dir -1 1
23040         test_300_check_default_striped_dir normal_dir 2 -1
23041
23042         #delete default stripe information
23043         echo "delete default stripeEA"
23044         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
23045                 error "set default stripe on striped dir error"
23046
23047         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
23048         for dir in $(find $DIR/$tdir/normal_dir/*); do
23049                 stripe_count=$($LFS getdirstripe -c $dir)
23050                 [ $stripe_count -eq 0 ] ||
23051                         error "expect 1 get $stripe_count for $dir"
23052         done
23053 }
23054 run_test 300g "check default striped directory for normal directory"
23055
23056 test_300h() {
23057         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23058         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23059                 skip "Need MDS version at least 2.7.55"
23060
23061         local dir
23062         local stripe_count
23063
23064         mkdir $DIR/$tdir
23065         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23066                 error "set striped dir error"
23067
23068         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
23069         test_300_check_default_striped_dir striped_dir 1 0
23070         test_300_check_default_striped_dir striped_dir -1 1
23071         test_300_check_default_striped_dir striped_dir 2 -1
23072
23073         #delete default stripe information
23074         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
23075                 error "set default stripe on striped dir error"
23076
23077         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
23078         for dir in $(find $DIR/$tdir/striped_dir/*); do
23079                 stripe_count=$($LFS getdirstripe -c $dir)
23080                 [ $stripe_count -eq 0 ] ||
23081                         error "expect 1 get $stripe_count for $dir"
23082         done
23083 }
23084 run_test 300h "check default striped directory for striped directory"
23085
23086 test_300i() {
23087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23088         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23089         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23090                 skip "Need MDS version at least 2.7.55"
23091
23092         local stripe_count
23093         local file
23094
23095         mkdir $DIR/$tdir
23096
23097         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23098                 error "set striped dir error"
23099
23100         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
23101                 error "create files under striped dir failed"
23102
23103         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
23104                 error "set striped hashdir error"
23105
23106         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
23107                 error "create dir0 under hash dir failed"
23108         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
23109                 error "create dir1 under hash dir failed"
23110         $LFS setdirstripe -i0 -c$MDSCOUNT -H crush $DIR/$tdir/hashdir/d2 ||
23111                 error "create dir2 under hash dir failed"
23112
23113         # unfortunately, we need to umount to clear dir layout cache for now
23114         # once we fully implement dir layout, we can drop this
23115         umount_client $MOUNT || error "umount failed"
23116         mount_client $MOUNT || error "mount failed"
23117
23118         $LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir
23119         local dircnt=$($LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir | wc -l)
23120         [ $dircnt -eq 2 ] || error "lfs find striped dir got:$dircnt,except:1"
23121
23122         #set the stripe to be unknown hash type
23123         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
23124         $LCTL set_param fail_loc=0x1901
23125         for ((i = 0; i < 10; i++)); do
23126                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
23127                         error "stat f-$i failed"
23128                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
23129         done
23130
23131         touch $DIR/$tdir/striped_dir/f0 &&
23132                 error "create under striped dir with unknown hash should fail"
23133
23134         $LCTL set_param fail_loc=0
23135
23136         umount_client $MOUNT || error "umount failed"
23137         mount_client $MOUNT || error "mount failed"
23138
23139         return 0
23140 }
23141 run_test 300i "client handle unknown hash type striped directory"
23142
23143 test_300j() {
23144         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23145         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23146         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23147                 skip "Need MDS version at least 2.7.55"
23148
23149         local stripe_count
23150         local file
23151
23152         mkdir $DIR/$tdir
23153
23154         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
23155         $LCTL set_param fail_loc=0x1702
23156         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23157                 error "set striped dir error"
23158
23159         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
23160                 error "create files under striped dir failed"
23161
23162         $LCTL set_param fail_loc=0
23163
23164         rm -rf $DIR/$tdir || error "unlink striped dir fails"
23165
23166         return 0
23167 }
23168 run_test 300j "test large update record"
23169
23170 test_300k() {
23171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23172         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23173         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23174                 skip "Need MDS version at least 2.7.55"
23175
23176         # this test needs a huge transaction
23177         local kb
23178         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23179              osd*.$FSNAME-MDT0000.kbytestotal")
23180         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
23181
23182         local stripe_count
23183         local file
23184
23185         mkdir $DIR/$tdir
23186
23187         #define OBD_FAIL_LARGE_STRIPE   0x1703
23188         $LCTL set_param fail_loc=0x1703
23189         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
23190                 error "set striped dir error"
23191         $LCTL set_param fail_loc=0
23192
23193         $LFS getdirstripe $DIR/$tdir/striped_dir ||
23194                 error "getstripeddir fails"
23195         rm -rf $DIR/$tdir/striped_dir ||
23196                 error "unlink striped dir fails"
23197
23198         return 0
23199 }
23200 run_test 300k "test large striped directory"
23201
23202 test_300l() {
23203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23204         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23205         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23206                 skip "Need MDS version at least 2.7.55"
23207
23208         local stripe_index
23209
23210         test_mkdir -p $DIR/$tdir/striped_dir
23211         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
23212                         error "chown $RUNAS_ID failed"
23213         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
23214                 error "set default striped dir failed"
23215
23216         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
23217         $LCTL set_param fail_loc=0x80000158
23218         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
23219
23220         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
23221         [ $stripe_index -eq 1 ] ||
23222                 error "expect 1 get $stripe_index for $dir"
23223 }
23224 run_test 300l "non-root user to create dir under striped dir with stale layout"
23225
23226 test_300m() {
23227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23228         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
23229         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23230                 skip "Need MDS version at least 2.7.55"
23231
23232         mkdir -p $DIR/$tdir/striped_dir
23233         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
23234                 error "set default stripes dir error"
23235
23236         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
23237
23238         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
23239         [ $stripe_count -eq 0 ] ||
23240                         error "expect 0 get $stripe_count for a"
23241
23242         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
23243                 error "set default stripes dir error"
23244
23245         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
23246
23247         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
23248         [ $stripe_count -eq 0 ] ||
23249                         error "expect 0 get $stripe_count for b"
23250
23251         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
23252                 error "set default stripes dir error"
23253
23254         mkdir $DIR/$tdir/striped_dir/c &&
23255                 error "default stripe_index is invalid, mkdir c should fails"
23256
23257         rm -rf $DIR/$tdir || error "rmdir fails"
23258 }
23259 run_test 300m "setstriped directory on single MDT FS"
23260
23261 cleanup_300n() {
23262         local list=$(comma_list $(mdts_nodes))
23263
23264         trap 0
23265         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
23266 }
23267
23268 test_300n() {
23269         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23270         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23271         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23272                 skip "Need MDS version at least 2.7.55"
23273         remote_mds_nodsh && skip "remote MDS with nodsh"
23274
23275         local stripe_index
23276         local list=$(comma_list $(mdts_nodes))
23277
23278         trap cleanup_300n RETURN EXIT
23279         mkdir -p $DIR/$tdir
23280         chmod 777 $DIR/$tdir
23281         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
23282                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
23283                 error "create striped dir succeeds with gid=0"
23284
23285         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
23286         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
23287                 error "create striped dir fails with gid=-1"
23288
23289         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
23290         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
23291                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
23292                 error "set default striped dir succeeds with gid=0"
23293
23294
23295         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
23296         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
23297                 error "set default striped dir fails with gid=-1"
23298
23299
23300         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
23301         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
23302                                         error "create test_dir fails"
23303         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
23304                                         error "create test_dir1 fails"
23305         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
23306                                         error "create test_dir2 fails"
23307         cleanup_300n
23308 }
23309 run_test 300n "non-root user to create dir under striped dir with default EA"
23310
23311 test_300o() {
23312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23313         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23314         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23315                 skip "Need MDS version at least 2.7.55"
23316
23317         local numfree1
23318         local numfree2
23319
23320         mkdir -p $DIR/$tdir
23321
23322         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
23323         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
23324         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
23325                 skip "not enough free inodes $numfree1 $numfree2"
23326         fi
23327
23328         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
23329         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
23330         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
23331                 skip "not enough free space $numfree1 $numfree2"
23332         fi
23333
23334         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
23335                 error "setdirstripe fails"
23336
23337         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
23338                 error "create dirs fails"
23339
23340         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
23341         ls $DIR/$tdir/striped_dir > /dev/null ||
23342                 error "ls striped dir fails"
23343         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
23344                 error "unlink big striped dir fails"
23345 }
23346 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
23347
23348 test_300p() {
23349         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23350         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23351         remote_mds_nodsh && skip "remote MDS with nodsh"
23352
23353         mkdir_on_mdt0 $DIR/$tdir
23354
23355         #define OBD_FAIL_OUT_ENOSPC     0x1704
23356         do_facet mds2 lctl set_param fail_loc=0x80001704
23357         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
23358                  && error "create striped directory should fail"
23359
23360         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
23361
23362         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
23363         true
23364 }
23365 run_test 300p "create striped directory without space"
23366
23367 test_300q() {
23368         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23369         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23370
23371         local fd=$(free_fd)
23372         local cmd="exec $fd<$tdir"
23373         cd $DIR
23374         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
23375         eval $cmd
23376         cmd="exec $fd<&-"
23377         trap "eval $cmd" EXIT
23378         cd $tdir || error "cd $tdir fails"
23379         rmdir  ../$tdir || error "rmdir $tdir fails"
23380         mkdir local_dir && error "create dir succeeds"
23381         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
23382         eval $cmd
23383         return 0
23384 }
23385 run_test 300q "create remote directory under orphan directory"
23386
23387 test_300r() {
23388         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23389                 skip "Need MDS version at least 2.7.55" && return
23390         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23391
23392         mkdir $DIR/$tdir
23393
23394         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
23395                 error "set striped dir error"
23396
23397         $LFS getdirstripe $DIR/$tdir/striped_dir ||
23398                 error "getstripeddir fails"
23399
23400         local stripe_count
23401         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
23402                       awk '/lmv_stripe_count:/ { print $2 }')
23403
23404         [ $MDSCOUNT -ne $stripe_count ] &&
23405                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
23406
23407         rm -rf $DIR/$tdir/striped_dir ||
23408                 error "unlink striped dir fails"
23409 }
23410 run_test 300r "test -1 striped directory"
23411
23412 test_300s_helper() {
23413         local count=$1
23414
23415         local stripe_dir=$DIR/$tdir/striped_dir.$count
23416
23417         $LFS mkdir -c $count $stripe_dir ||
23418                 error "lfs mkdir -c error"
23419
23420         $LFS getdirstripe $stripe_dir ||
23421                 error "lfs getdirstripe fails"
23422
23423         local stripe_count
23424         stripe_count=$($LFS getdirstripe $stripe_dir |
23425                       awk '/lmv_stripe_count:/ { print $2 }')
23426
23427         [ $count -ne $stripe_count ] &&
23428                 error_noexit "bad stripe count $stripe_count expected $count"
23429
23430         local dupe_stripes
23431         dupe_stripes=$($LFS getdirstripe $stripe_dir |
23432                 awk '/0x/ {count[$1] += 1}; END {
23433                         for (idx in count) {
23434                                 if (count[idx]>1) {
23435                                         print "index " idx " count " count[idx]
23436                                 }
23437                         }
23438                 }')
23439
23440         if [[ -n "$dupe_stripes" ]] ; then
23441                 lfs getdirstripe $stripe_dir
23442                 error_noexit "Dupe MDT above: $dupe_stripes "
23443         fi
23444
23445         rm -rf $stripe_dir ||
23446                 error_noexit "unlink $stripe_dir fails"
23447 }
23448
23449 test_300s() {
23450         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23451                 skip "Need MDS version at least 2.7.55" && return
23452         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23453
23454         mkdir $DIR/$tdir
23455         for count in $(seq 2 $MDSCOUNT); do
23456                 test_300s_helper $count
23457         done
23458 }
23459 run_test 300s "test lfs mkdir -c without -i"
23460
23461
23462 prepare_remote_file() {
23463         mkdir $DIR/$tdir/src_dir ||
23464                 error "create remote source failed"
23465
23466         cp /etc/hosts $DIR/$tdir/src_dir/a ||
23467                  error "cp to remote source failed"
23468         touch $DIR/$tdir/src_dir/a
23469
23470         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
23471                 error "create remote target dir failed"
23472
23473         touch $DIR/$tdir/tgt_dir/b
23474
23475         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
23476                 error "rename dir cross MDT failed!"
23477
23478         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
23479                 error "src_child still exists after rename"
23480
23481         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
23482                 error "missing file(a) after rename"
23483
23484         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
23485                 error "diff after rename"
23486 }
23487
23488 test_310a() {
23489         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
23490         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23491
23492         local remote_file=$DIR/$tdir/tgt_dir/b
23493
23494         mkdir -p $DIR/$tdir
23495
23496         prepare_remote_file || error "prepare remote file failed"
23497
23498         #open-unlink file
23499         $OPENUNLINK $remote_file $remote_file ||
23500                 error "openunlink $remote_file failed"
23501         $CHECKSTAT -a $remote_file || error "$remote_file exists"
23502 }
23503 run_test 310a "open unlink remote file"
23504
23505 test_310b() {
23506         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
23507         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23508
23509         local remote_file=$DIR/$tdir/tgt_dir/b
23510
23511         mkdir -p $DIR/$tdir
23512
23513         prepare_remote_file || error "prepare remote file failed"
23514
23515         ln $remote_file $DIR/$tfile || error "link failed for remote file"
23516         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
23517         $CHECKSTAT -t file $remote_file || error "check file failed"
23518 }
23519 run_test 310b "unlink remote file with multiple links while open"
23520
23521 test_310c() {
23522         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23523         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
23524
23525         local remote_file=$DIR/$tdir/tgt_dir/b
23526
23527         mkdir -p $DIR/$tdir
23528
23529         prepare_remote_file || error "prepare remote file failed"
23530
23531         ln $remote_file $DIR/$tfile || error "link failed for remote file"
23532         multiop_bg_pause $remote_file O_uc ||
23533                         error "mulitop failed for remote file"
23534         MULTIPID=$!
23535         $MULTIOP $DIR/$tfile Ouc
23536         kill -USR1 $MULTIPID
23537         wait $MULTIPID
23538 }
23539 run_test 310c "open-unlink remote file with multiple links"
23540
23541 #LU-4825
23542 test_311() {
23543         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23544         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
23545         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
23546                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
23547         remote_mds_nodsh && skip "remote MDS with nodsh"
23548
23549         local old_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
23550         local mdts=$(comma_list $(mdts_nodes))
23551
23552         mkdir -p $DIR/$tdir
23553         $LFS setstripe -i 0 -c 1 $DIR/$tdir
23554         createmany -o $DIR/$tdir/$tfile. 1000
23555
23556         # statfs data is not real time, let's just calculate it
23557         old_iused=$((old_iused + 1000))
23558
23559         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23560                         osp.*OST0000*MDT0000.create_count")
23561         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23562                                 osp.*OST0000*MDT0000.max_create_count")
23563         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
23564
23565         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
23566         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
23567         [ $index -ne 0 ] || error "$tfile stripe index is 0"
23568
23569         unlinkmany $DIR/$tdir/$tfile. 1000
23570
23571         do_nodes $mdts "$LCTL set_param -n \
23572                         osp.*OST0000*.max_create_count=$max_count"
23573         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
23574                 do_nodes $mdts "$LCTL set_param -n \
23575                                 osp.*OST0000*.create_count=$count"
23576         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
23577                         grep "=0" && error "create_count is zero"
23578
23579         local new_iused
23580         for i in $(seq 120); do
23581                 new_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
23582                 # system may be too busy to destroy all objs in time, use
23583                 # a somewhat small value to not fail autotest
23584                 [ $((old_iused - new_iused)) -gt 400 ] && break
23585                 sleep 1
23586         done
23587
23588         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
23589         [ $((old_iused - new_iused)) -gt 400 ] ||
23590                 error "objs not destroyed after unlink"
23591 }
23592 run_test 311 "disable OSP precreate, and unlink should destroy objs"
23593
23594 zfs_oid_to_objid()
23595 {
23596         local ost=$1
23597         local objid=$2
23598
23599         local vdevdir=$(dirname $(facet_vdevice $ost))
23600         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
23601         local zfs_zapid=$(do_facet $ost $cmd |
23602                           grep -w "/O/0/d$((objid%32))" -C 5 |
23603                           awk '/Object/{getline; print $1}')
23604         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
23605                           awk "/$objid = /"'{printf $3}')
23606
23607         echo $zfs_objid
23608 }
23609
23610 zfs_object_blksz() {
23611         local ost=$1
23612         local objid=$2
23613
23614         local vdevdir=$(dirname $(facet_vdevice $ost))
23615         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
23616         local blksz=$(do_facet $ost $cmd $objid |
23617                       awk '/dblk/{getline; printf $4}')
23618
23619         case "${blksz: -1}" in
23620                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
23621                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
23622                 *) ;;
23623         esac
23624
23625         echo $blksz
23626 }
23627
23628 test_312() { # LU-4856
23629         remote_ost_nodsh && skip "remote OST with nodsh"
23630         [ "$ost1_FSTYPE" = "zfs" ] ||
23631                 skip_env "the test only applies to zfs"
23632
23633         local max_blksz=$(do_facet ost1 \
23634                           $ZFS get -p recordsize $(facet_device ost1) |
23635                           awk '!/VALUE/{print $3}')
23636
23637         # to make life a little bit easier
23638         $LFS mkdir -c 1 -i 0 $DIR/$tdir
23639         $LFS setstripe -c 1 -i 0 $DIR/$tdir
23640
23641         local tf=$DIR/$tdir/$tfile
23642         touch $tf
23643         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23644
23645         # Get ZFS object id
23646         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23647         # block size change by sequential overwrite
23648         local bs
23649
23650         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
23651                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
23652
23653                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
23654                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
23655         done
23656         rm -f $tf
23657
23658         # block size change by sequential append write
23659         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
23660         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23661         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23662         local count
23663
23664         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
23665                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
23666                         oflag=sync conv=notrunc
23667
23668                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
23669                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
23670                         error "blksz error, actual $blksz, " \
23671                                 "expected: 2 * $count * $PAGE_SIZE"
23672         done
23673         rm -f $tf
23674
23675         # random write
23676         touch $tf
23677         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
23678         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
23679
23680         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
23681         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23682         [ $blksz -eq $PAGE_SIZE ] ||
23683                 error "blksz error: $blksz, expected: $PAGE_SIZE"
23684
23685         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
23686         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23687         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
23688
23689         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
23690         blksz=$(zfs_object_blksz ost1 $zfs_objid)
23691         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
23692 }
23693 run_test 312 "make sure ZFS adjusts its block size by write pattern"
23694
23695 test_313() {
23696         remote_ost_nodsh && skip "remote OST with nodsh"
23697
23698         local file=$DIR/$tfile
23699
23700         rm -f $file
23701         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
23702
23703         # define OBD_FAIL_TGT_RCVD_EIO           0x720
23704         do_facet ost1 "$LCTL set_param fail_loc=0x720"
23705         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
23706                 error "write should failed"
23707         do_facet ost1 "$LCTL set_param fail_loc=0"
23708         rm -f $file
23709 }
23710 run_test 313 "io should fail after last_rcvd update fail"
23711
23712 test_314() {
23713         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
23714
23715         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
23716         do_facet ost1 "$LCTL set_param fail_loc=0x720"
23717         rm -f $DIR/$tfile
23718         wait_delete_completed
23719         do_facet ost1 "$LCTL set_param fail_loc=0"
23720 }
23721 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
23722
23723 test_315() { # LU-618
23724         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
23725
23726         local file=$DIR/$tfile
23727         rm -f $file
23728
23729         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
23730                 error "multiop file write failed"
23731         $MULTIOP $file oO_RDONLY:r4063232_c &
23732         PID=$!
23733
23734         sleep 2
23735
23736         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
23737         kill -USR1 $PID
23738
23739         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
23740         rm -f $file
23741 }
23742 run_test 315 "read should be accounted"
23743
23744 test_316() {
23745         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
23746         large_xattr_enabled || skip_env "ea_inode feature disabled"
23747
23748         rm -rf $DIR/$tdir/d
23749         mkdir -p $DIR/$tdir/d
23750         chown nobody $DIR/$tdir/d
23751         touch $DIR/$tdir/d/file
23752
23753         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
23754 }
23755 run_test 316 "lfs mv"
23756
23757 test_317() {
23758         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
23759                 skip "Need MDS version at least 2.11.53"
23760         if [ "$ost1_FSTYPE" == "zfs" ]; then
23761                 skip "LU-10370: no implementation for ZFS"
23762         fi
23763
23764         local trunc_sz
23765         local grant_blk_size
23766
23767         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
23768                         awk '/grant_block_size:/ { print $2; exit; }')
23769         #
23770         # Create File of size 5M. Truncate it to below size's and verify
23771         # blocks count.
23772         #
23773         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
23774                 error "Create file $DIR/$tfile failed"
23775         stack_trap "rm -f $DIR/$tfile" EXIT
23776
23777         for trunc_sz in 2097152 4097 4000 509 0; do
23778                 $TRUNCATE $DIR/$tfile $trunc_sz ||
23779                         error "truncate $tfile to $trunc_sz failed"
23780                 local sz=$(stat --format=%s $DIR/$tfile)
23781                 local blk=$(stat --format=%b $DIR/$tfile)
23782                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
23783                                      grant_blk_size) * 8))
23784
23785                 if [[ $blk -ne $trunc_blk ]]; then
23786                         $(which stat) $DIR/$tfile
23787                         error "Expected Block $trunc_blk got $blk for $tfile"
23788                 fi
23789
23790                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
23791                         error "Expected Size $trunc_sz got $sz for $tfile"
23792         done
23793
23794         #
23795         # sparse file test
23796         # Create file with a hole and write actual two blocks. Block count
23797         # must be 16.
23798         #
23799         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
23800                 conv=fsync || error "Create file : $DIR/$tfile"
23801
23802         # Calculate the final truncate size.
23803         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
23804
23805         #
23806         # truncate to size $trunc_sz bytes. Strip the last block
23807         # The block count must drop to 8
23808         #
23809         $TRUNCATE $DIR/$tfile $trunc_sz ||
23810                 error "truncate $tfile to $trunc_sz failed"
23811
23812         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
23813         sz=$(stat --format=%s $DIR/$tfile)
23814         blk=$(stat --format=%b $DIR/$tfile)
23815
23816         if [[ $blk -ne $trunc_bsz ]]; then
23817                 $(which stat) $DIR/$tfile
23818                 error "Expected Block $trunc_bsz got $blk for $tfile"
23819         fi
23820
23821         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
23822                 error "Expected Size $trunc_sz got $sz for $tfile"
23823 }
23824 run_test 317 "Verify blocks get correctly update after truncate"
23825
23826 test_318() {
23827         local llite_name="llite.$($LFS getname $MOUNT | awk '{print $1}')"
23828         local old_max_active=$($LCTL get_param -n \
23829                             ${llite_name}.max_read_ahead_async_active \
23830                             2>/dev/null)
23831
23832         $LCTL set_param llite.*.max_read_ahead_async_active=256
23833         local max_active=$($LCTL get_param -n \
23834                            ${llite_name}.max_read_ahead_async_active \
23835                            2>/dev/null)
23836         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
23837
23838         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
23839                 error "set max_read_ahead_async_active should succeed"
23840
23841         $LCTL set_param llite.*.max_read_ahead_async_active=512
23842         max_active=$($LCTL get_param -n \
23843                      ${llite_name}.max_read_ahead_async_active 2>/dev/null)
23844         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
23845
23846         # restore @max_active
23847         [ $old_max_active -ne 0 ] && $LCTL set_param \
23848                 llite.*.max_read_ahead_async_active=$old_max_active
23849
23850         local old_threshold=$($LCTL get_param -n \
23851                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
23852         local max_per_file_mb=$($LCTL get_param -n \
23853                 ${llite_name}.max_read_ahead_per_file_mb 2>/dev/null)
23854
23855         local invalid=$(($max_per_file_mb + 1))
23856         $LCTL set_param \
23857                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
23858                         && error "set $invalid should fail"
23859
23860         local valid=$(($invalid - 1))
23861         $LCTL set_param \
23862                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
23863                         error "set $valid should succeed"
23864         local threshold=$($LCTL get_param -n \
23865                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
23866         [ $threshold -eq $valid ] || error \
23867                 "expect threshold $valid got $threshold"
23868         $LCTL set_param \
23869                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
23870 }
23871 run_test 318 "Verify async readahead tunables"
23872
23873 test_319() {
23874         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
23875
23876         local before=$(date +%s)
23877         local evict
23878         local mdir=$DIR/$tdir
23879         local file=$mdir/xxx
23880
23881         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
23882         touch $file
23883
23884 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
23885         $LCTL set_param fail_val=5 fail_loc=0x8000032c
23886         $LFS mv -m1 $file &
23887
23888         sleep 1
23889         dd if=$file of=/dev/null
23890         wait
23891         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
23892           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
23893
23894         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
23895 }
23896 run_test 319 "lost lease lock on migrate error"
23897
23898 test_398a() { # LU-4198
23899         local ost1_imp=$(get_osc_import_name client ost1)
23900         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23901                          cut -d'.' -f2)
23902
23903         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23904         $LCTL set_param ldlm.namespaces.*.lru_size=clear
23905
23906         # request a new lock on client
23907         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23908
23909         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
23910         local lock_count=$($LCTL get_param -n \
23911                            ldlm.namespaces.$imp_name.lru_size)
23912         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
23913
23914         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
23915
23916         # no lock cached, should use lockless IO and not enqueue new lock
23917         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
23918         lock_count=$($LCTL get_param -n \
23919                      ldlm.namespaces.$imp_name.lru_size)
23920         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
23921 }
23922 run_test 398a "direct IO should cancel lock otherwise lockless"
23923
23924 test_398b() { # LU-4198
23925         which fio || skip_env "no fio installed"
23926         $LFS setstripe -c -1 $DIR/$tfile
23927
23928         local size=12
23929         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
23930
23931         local njobs=4
23932         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
23933         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
23934                 --numjobs=$njobs --fallocate=none \
23935                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23936                 --filename=$DIR/$tfile &
23937         bg_pid=$!
23938
23939         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
23940         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
23941                 --numjobs=$njobs --fallocate=none \
23942                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23943                 --filename=$DIR/$tfile || true
23944         wait $bg_pid
23945
23946         rm -f $DIR/$tfile
23947 }
23948 run_test 398b "DIO and buffer IO race"
23949
23950 test_398c() { # LU-4198
23951         local ost1_imp=$(get_osc_import_name client ost1)
23952         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
23953                          cut -d'.' -f2)
23954
23955         which fio || skip_env "no fio installed"
23956
23957         saved_debug=$($LCTL get_param -n debug)
23958         $LCTL set_param debug=0
23959
23960         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
23961         ((size /= 1024)) # by megabytes
23962         ((size /= 2)) # write half of the OST at most
23963         [ $size -gt 40 ] && size=40 #reduce test time anyway
23964
23965         $LFS setstripe -c 1 $DIR/$tfile
23966
23967         # it seems like ldiskfs reserves more space than necessary if the
23968         # writing blocks are not mapped, so it extends the file firstly
23969         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
23970         cancel_lru_locks osc
23971
23972         # clear and verify rpc_stats later
23973         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
23974
23975         local njobs=4
23976         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
23977         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
23978                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
23979                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23980                 --filename=$DIR/$tfile
23981         [ $? -eq 0 ] || error "fio write error"
23982
23983         [ $($LCTL get_param -n ldlm.namespaces.$imp_name.lock_count) -eq 0 ] ||
23984                 error "Locks were requested while doing AIO"
23985
23986         # get the percentage of 1-page I/O
23987         pct=$($LCTL get_param osc.${imp_name}.rpc_stats |
23988                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
23989                 awk '{print $7}')
23990         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
23991
23992         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
23993         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
23994                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
23995                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
23996                 --filename=$DIR/$tfile
23997         [ $? -eq 0 ] || error "fio mixed read write error"
23998
23999         echo "AIO with large block size ${size}M"
24000         fio --name=rand-rw --rw=randrw --bs=${size}M --direct=1 \
24001                 --numjobs=1 --fallocate=none --ioengine=libaio \
24002                 --iodepth=16 --allow_file_create=0 --size=${size}M \
24003                 --filename=$DIR/$tfile
24004         [ $? -eq 0 ] || error "fio large block size failed"
24005
24006         rm -f $DIR/$tfile
24007         $LCTL set_param debug="$saved_debug"
24008 }
24009 run_test 398c "run fio to test AIO"
24010
24011 test_398d() { #  LU-13846
24012         which aiocp || skip_env "no aiocp installed"
24013         local aio_file=$DIR/$tfile.aio
24014
24015         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
24016
24017         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=64
24018         aiocp -a $PAGE_SIZE -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file
24019         stack_trap "rm -f $DIR/$tfile $aio_file"
24020
24021         diff $DIR/$tfile $aio_file || error "file diff after aiocp"
24022
24023         # make sure we don't crash and fail properly
24024         aiocp -a 512 -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
24025                 error "aio not aligned with PAGE SIZE should fail"
24026
24027         rm -f $DIR/$tfile $aio_file
24028 }
24029 run_test 398d "run aiocp to verify block size > stripe size"
24030
24031 test_398e() {
24032         dd if=/dev/zero of=$DIR/$tfile bs=1234 count=1
24033         touch $DIR/$tfile.new
24034         dd if=$DIR/$tfile of=$DIR/$tfile.new bs=1M count=1 oflag=direct
24035 }
24036 run_test 398e "O_Direct open cleared by fcntl doesn't cause hang"
24037
24038 test_398f() { #  LU-14687
24039         which aiocp || skip_env "no aiocp installed"
24040         local aio_file=$DIR/$tfile.aio
24041
24042         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
24043
24044         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
24045         stack_trap "rm -f $DIR/$tfile $aio_file"
24046
24047         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
24048         $LCTL set_param fail_loc=0x1418
24049         # make sure we don't crash and fail properly
24050         aiocp -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
24051                 error "aio with page allocation failure succeeded"
24052         $LCTL set_param fail_loc=0
24053         diff $DIR/$tfile $aio_file
24054         [[ $? != 0 ]] || error "no diff after failed aiocp"
24055 }
24056 run_test 398f "verify aio handles ll_direct_rw_pages errors correctly"
24057
24058 # NB: To get the parallel DIO behavior in LU-13798, there must be > 1
24059 # stripe and i/o size must be > stripe size
24060 # Old style synchronous DIO waits after submitting each chunk, resulting in a
24061 # single RPC in flight.  This test shows async DIO submission is working by
24062 # showing multiple RPCs in flight.
24063 test_398g() { #  LU-13798
24064         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
24065
24066         # We need to do some i/o first to acquire enough grant to put our RPCs
24067         # in flight; otherwise a new connection may not have enough grant
24068         # available
24069         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24070                 error "parallel dio failed"
24071         stack_trap "rm -f $DIR/$tfile"
24072
24073         # Reduce RPC size to 1M to avoid combination in to larger RPCs
24074         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
24075         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
24076         stack_trap "$LCTL set_param -n $pages_per_rpc"
24077
24078         # Recreate file so it's empty
24079         rm -f $DIR/$tfile
24080         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
24081         #Pause rpc completion to guarantee we see multiple rpcs in flight
24082         #define OBD_FAIL_OST_BRW_PAUSE_BULK
24083         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
24084         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
24085
24086         # Clear rpc stats
24087         $LCTL set_param osc.*.rpc_stats=c
24088
24089         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24090                 error "parallel dio failed"
24091         stack_trap "rm -f $DIR/$tfile"
24092
24093         $LCTL get_param osc.*-OST0000-*.rpc_stats
24094         pct=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
24095                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
24096                 grep "8:" | awk '{print $8}')
24097         # We look at the "8 rpcs in flight" field, and verify A) it is present
24098         # and B) it includes all RPCs.  This proves we had 8 RPCs in flight,
24099         # as expected for an 8M DIO to a file with 1M stripes.
24100         [ $pct -eq 100 ] || error "we should see 8 RPCs in flight"
24101
24102         # Verify turning off parallel dio works as expected
24103         # Clear rpc stats
24104         $LCTL set_param osc.*.rpc_stats=c
24105         $LCTL set_param llite.*.parallel_dio=0
24106         stack_trap '$LCTL set_param llite.*.parallel_dio=1'
24107
24108         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24109                 error "dio with parallel dio disabled failed"
24110
24111         # Ideally, we would see only one RPC in flight here, but there is an
24112         # unavoidable race between i/o completion and RPC in flight counting,
24113         # so while only 1 i/o is in flight at a time, the RPC in flight counter
24114         # will sometimes exceed 1 (3 or 4 is not rare on VM testing).
24115         # So instead we just verify it's always < 8.
24116         $LCTL get_param osc.*-OST0000-*.rpc_stats
24117         ret=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
24118                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
24119                 grep '^$' -B1 | grep . | awk '{print $1}')
24120         [ $ret != "8:" ] ||
24121                 error "we should see fewer than 8 RPCs in flight (saw $ret)"
24122 }
24123 run_test 398g "verify parallel dio async RPC submission"
24124
24125 test_398h() { #  LU-13798
24126         local dio_file=$DIR/$tfile.dio
24127
24128         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
24129
24130         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24131         stack_trap "rm -f $DIR/$tfile $dio_file"
24132
24133         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct ||
24134                 error "parallel dio failed"
24135         diff $DIR/$tfile $dio_file
24136         [[ $? == 0 ]] || error "file diff after aiocp"
24137 }
24138 run_test 398h "verify correctness of read & write with i/o size >> stripe size"
24139
24140 test_398i() { #  LU-13798
24141         local dio_file=$DIR/$tfile.dio
24142
24143         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
24144
24145         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24146         stack_trap "rm -f $DIR/$tfile $dio_file"
24147
24148         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
24149         $LCTL set_param fail_loc=0x1418
24150         # make sure we don't crash and fail properly
24151         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct &&
24152                 error "parallel dio page allocation failure succeeded"
24153         diff $DIR/$tfile $dio_file
24154         [[ $? != 0 ]] || error "no diff after failed aiocp"
24155 }
24156 run_test 398i "verify parallel dio handles ll_direct_rw_pages errors correctly"
24157
24158 test_398j() { #  LU-13798
24159         # Stripe size > RPC size but less than i/o size tests split across
24160         # stripes and RPCs for individual i/o op
24161         $LFS setstripe -o 0,0 -S 4M $DIR/$tfile $DIR/$tfile.2
24162
24163         # Reduce RPC size to 1M to guarantee split to multiple RPCs per stripe
24164         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
24165         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
24166         stack_trap "$LCTL set_param -n $pages_per_rpc"
24167
24168         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
24169                 error "parallel dio write failed"
24170         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.2"
24171
24172         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct ||
24173                 error "parallel dio read failed"
24174         diff $DIR/$tfile $DIR/$tfile.2
24175         [[ $? == 0 ]] || error "file diff after parallel dio read"
24176 }
24177 run_test 398j "test parallel dio where stripe size > rpc_size"
24178
24179 test_398k() { #  LU-13798
24180         wait_delete_completed
24181         wait_mds_ost_sync
24182
24183         # 4 stripe file; we will cause out of space on OST0
24184         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
24185
24186         # Fill OST0 (if it's not too large)
24187         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
24188                    head -n1)
24189         if [[ $ORIGFREE -gt $MAXFREE ]]; then
24190                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
24191         fi
24192         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
24193         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
24194                 error "dd should fill OST0"
24195         stack_trap "rm -f $DIR/$tfile.1"
24196
24197         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24198         err=$?
24199
24200         ls -la $DIR/$tfile
24201         $CHECKSTAT -t file -s 0 $DIR/$tfile ||
24202                 error "file is not 0 bytes in size"
24203
24204         # dd above should not succeed, but don't error until here so we can
24205         # get debug info above
24206         [[ $err != 0 ]] ||
24207                 error "parallel dio write with enospc succeeded"
24208         stack_trap "rm -f $DIR/$tfile"
24209 }
24210 run_test 398k "test enospc on first stripe"
24211
24212 test_398l() { #  LU-13798
24213         wait_delete_completed
24214         wait_mds_ost_sync
24215
24216         # 4 stripe file; we will cause out of space on OST0
24217         # Note the 1M stripe size and the > 1M i/o size mean this ENOSPC
24218         # happens on the second i/o chunk we issue
24219         $LFS setstripe -o 1,0,1,0 -S 1M $DIR/$tfile $DIR/$tfile.2
24220
24221         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=2 oflag=direct
24222         stack_trap "rm -f $DIR/$tfile"
24223
24224         # Fill OST0 (if it's not too large)
24225         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
24226                    head -n1)
24227         if [[ $ORIGFREE -gt $MAXFREE ]]; then
24228                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
24229         fi
24230         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
24231         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
24232                 error "dd should fill OST0"
24233         stack_trap "rm -f $DIR/$tfile.1"
24234
24235         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 oflag=direct
24236         err=$?
24237         stack_trap "rm -f $DIR/$tfile.2"
24238
24239         # Check that short write completed as expected
24240         ls -la $DIR/$tfile.2
24241         $CHECKSTAT -t file -s 1048576 $DIR/$tfile.2 ||
24242                 error "file is not 1M in size"
24243
24244         # dd above should not succeed, but don't error until here so we can
24245         # get debug info above
24246         [[ $err != 0 ]] ||
24247                 error "parallel dio write with enospc succeeded"
24248
24249         # Truncate source file to same length as output file and diff them
24250         $TRUNCATE $DIR/$tfile 1048576
24251         diff $DIR/$tfile $DIR/$tfile.2
24252         [[ $? == 0 ]] || error "data incorrect after short write"
24253 }
24254 run_test 398l "test enospc on intermediate stripe/RPC"
24255
24256 test_398m() { #  LU-13798
24257         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
24258
24259         # Set up failure on OST0, the first stripe:
24260         #define OBD_FAIL_OST_BRW_WRITE_BULK     0x20e
24261         #NB: Fail val is ost # + 1, because we cannot use cfs_fail_val = 0
24262         # So this fail_val specifies OST0
24263         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=1
24264         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
24265
24266         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
24267                 error "parallel dio write with failure on first stripe succeeded"
24268         stack_trap "rm -f $DIR/$tfile"
24269         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
24270
24271         # Place data in file for read
24272         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
24273                 error "parallel dio write failed"
24274
24275         # Fail read on OST0, first stripe
24276         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
24277         do_facet ost1 $LCTL set_param fail_loc=0x20f fail_val=1
24278         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
24279                 error "parallel dio read with error on first stripe succeeded"
24280         rm -f $DIR/$tfile.2
24281         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
24282
24283         # Switch to testing on OST1, second stripe
24284         # Clear file contents, maintain striping
24285         echo > $DIR/$tfile
24286         # Set up failure on OST1, second stripe:
24287         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=2
24288         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
24289
24290         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
24291                 error "parallel dio write with failure on first stripe succeeded"
24292         stack_trap "rm -f $DIR/$tfile"
24293         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
24294
24295         # Place data in file for read
24296         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
24297                 error "parallel dio write failed"
24298
24299         # Fail read on OST1, second stripe
24300         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
24301         do_facet ost2 $LCTL set_param fail_loc=0x20f fail_val=2
24302         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
24303                 error "parallel dio read with error on first stripe succeeded"
24304         rm -f $DIR/$tfile.2
24305         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
24306 }
24307 run_test 398m "test RPC failures with parallel dio"
24308
24309 # Parallel submission of DIO should not cause problems for append, but it's
24310 # important to verify.
24311 test_398n() { #  LU-13798
24312         $LFS setstripe -C 2 -S 1M $DIR/$tfile
24313
24314         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 ||
24315                 error "dd to create source file failed"
24316         stack_trap "rm -f $DIR/$tfile"
24317
24318         dd if=$DIR/$tfile of=$DIR/$tfile.1 bs=8M count=8 oflag=direct oflag=append ||
24319                 error "parallel dio write with failure on second stripe succeeded"
24320         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.1"
24321         diff $DIR/$tfile $DIR/$tfile.1
24322         [[ $? == 0 ]] || error "data incorrect after append"
24323
24324 }
24325 run_test 398n "test append with parallel DIO"
24326
24327 test_fake_rw() {
24328         local read_write=$1
24329         if [ "$read_write" = "write" ]; then
24330                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
24331         elif [ "$read_write" = "read" ]; then
24332                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
24333         else
24334                 error "argument error"
24335         fi
24336
24337         # turn off debug for performance testing
24338         local saved_debug=$($LCTL get_param -n debug)
24339         $LCTL set_param debug=0
24340
24341         $LFS setstripe -c 1 -i 0 $DIR/$tfile
24342
24343         # get ost1 size - $FSNAME-OST0000
24344         local ost1_avail_size=$($LFS df $DIR | awk /${ost1_svc}/'{ print $4 }')
24345         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
24346         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
24347
24348         if [ "$read_write" = "read" ]; then
24349                 $TRUNCATE $DIR/$tfile $(expr 1048576 \* $blocks)
24350         fi
24351
24352         local start_time=$(date +%s.%N)
24353         $dd_cmd bs=1M count=$blocks oflag=sync ||
24354                 error "real dd $read_write error"
24355         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
24356
24357         if [ "$read_write" = "write" ]; then
24358                 rm -f $DIR/$tfile
24359         fi
24360
24361         # define OBD_FAIL_OST_FAKE_RW           0x238
24362         do_facet ost1 $LCTL set_param fail_loc=0x238
24363
24364         local start_time=$(date +%s.%N)
24365         $dd_cmd bs=1M count=$blocks oflag=sync ||
24366                 error "fake dd $read_write error"
24367         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
24368
24369         if [ "$read_write" = "write" ]; then
24370                 # verify file size
24371                 cancel_lru_locks osc
24372                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
24373                         error "$tfile size not $blocks MB"
24374         fi
24375         do_facet ost1 $LCTL set_param fail_loc=0
24376
24377         echo "fake $read_write $duration_fake vs. normal $read_write" \
24378                 "$duration in seconds"
24379         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
24380                 error_not_in_vm "fake write is slower"
24381
24382         $LCTL set_param -n debug="$saved_debug"
24383         rm -f $DIR/$tfile
24384 }
24385 test_399a() { # LU-7655 for OST fake write
24386         remote_ost_nodsh && skip "remote OST with nodsh"
24387
24388         test_fake_rw write
24389 }
24390 run_test 399a "fake write should not be slower than normal write"
24391
24392 test_399b() { # LU-8726 for OST fake read
24393         remote_ost_nodsh && skip "remote OST with nodsh"
24394         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
24395                 skip_env "ldiskfs only test"
24396         fi
24397
24398         test_fake_rw read
24399 }
24400 run_test 399b "fake read should not be slower than normal read"
24401
24402 test_400a() { # LU-1606, was conf-sanity test_74
24403         if ! which $CC > /dev/null 2>&1; then
24404                 skip_env "$CC is not installed"
24405         fi
24406
24407         local extra_flags=''
24408         local out=$TMP/$tfile
24409         local prefix=/usr/include/lustre
24410         local prog
24411
24412         # Oleg removes c files in his test rig so test if any c files exist
24413         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
24414                 skip_env "Needed c test files are missing"
24415
24416         if ! [[ -d $prefix ]]; then
24417                 # Assume we're running in tree and fixup the include path.
24418                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
24419                 extra_flags+=" -L$LUSTRE/utils/.lib"
24420         fi
24421
24422         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
24423                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
24424                         error "client api broken"
24425         done
24426         rm -f $out
24427 }
24428 run_test 400a "Lustre client api program can compile and link"
24429
24430 test_400b() { # LU-1606, LU-5011
24431         local header
24432         local out=$TMP/$tfile
24433         local prefix=/usr/include/linux/lustre
24434
24435         # We use a hard coded prefix so that this test will not fail
24436         # when run in tree. There are headers in lustre/include/lustre/
24437         # that are not packaged (like lustre_idl.h) and have more
24438         # complicated include dependencies (like config.h and lnet/types.h).
24439         # Since this test about correct packaging we just skip them when
24440         # they don't exist (see below) rather than try to fixup cppflags.
24441
24442         if ! which $CC > /dev/null 2>&1; then
24443                 skip_env "$CC is not installed"
24444         fi
24445
24446         for header in $prefix/*.h; do
24447                 if ! [[ -f "$header" ]]; then
24448                         continue
24449                 fi
24450
24451                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
24452                         continue # lustre_ioctl.h is internal header
24453                 fi
24454
24455                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
24456                         error "cannot compile '$header'"
24457         done
24458         rm -f $out
24459 }
24460 run_test 400b "packaged headers can be compiled"
24461
24462 test_401a() { #LU-7437
24463         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
24464         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
24465
24466         #count the number of parameters by "list_param -R"
24467         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
24468         #count the number of parameters by listing proc files
24469         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
24470         echo "proc_dirs='$proc_dirs'"
24471         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
24472         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
24473                       sort -u | wc -l)
24474
24475         [ $params -eq $procs ] ||
24476                 error "found $params parameters vs. $procs proc files"
24477
24478         # test the list_param -D option only returns directories
24479         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
24480         #count the number of parameters by listing proc directories
24481         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
24482                 sort -u | wc -l)
24483
24484         [ $params -eq $procs ] ||
24485                 error "found $params parameters vs. $procs proc files"
24486 }
24487 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
24488
24489 test_401b() {
24490         # jobid_var may not allow arbitrary values, so use jobid_name
24491         # if available
24492         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24493                 local testname=jobid_name tmp='testing%p'
24494         else
24495                 local testname=jobid_var tmp=testing
24496         fi
24497
24498         local save=$($LCTL get_param -n $testname)
24499
24500         $LCTL set_param foo=bar $testname=$tmp bar=baz &&
24501                 error "no error returned when setting bad parameters"
24502
24503         local jobid_new=$($LCTL get_param -n foe $testname baz)
24504         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
24505
24506         $LCTL set_param -n fog=bam $testname=$save bat=fog
24507         local jobid_old=$($LCTL get_param -n foe $testname bag)
24508         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
24509 }
24510 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
24511
24512 test_401c() {
24513         # jobid_var may not allow arbitrary values, so use jobid_name
24514         # if available
24515         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24516                 local testname=jobid_name
24517         else
24518                 local testname=jobid_var
24519         fi
24520
24521         local jobid_var_old=$($LCTL get_param -n $testname)
24522         local jobid_var_new
24523
24524         $LCTL set_param $testname= &&
24525                 error "no error returned for 'set_param a='"
24526
24527         jobid_var_new=$($LCTL get_param -n $testname)
24528         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
24529                 error "$testname was changed by setting without value"
24530
24531         $LCTL set_param $testname &&
24532                 error "no error returned for 'set_param a'"
24533
24534         jobid_var_new=$($LCTL get_param -n $testname)
24535         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
24536                 error "$testname was changed by setting without value"
24537 }
24538 run_test 401c "Verify 'lctl set_param' without value fails in either format."
24539
24540 test_401d() {
24541         # jobid_var may not allow arbitrary values, so use jobid_name
24542         # if available
24543         if $LCTL list_param jobid_name > /dev/null 2>&1; then
24544                 local testname=jobid_name new_value='foo=bar%p'
24545         else
24546                 local testname=jobid_var new_valuie=foo=bar
24547         fi
24548
24549         local jobid_var_old=$($LCTL get_param -n $testname)
24550         local jobid_var_new
24551
24552         $LCTL set_param $testname=$new_value ||
24553                 error "'set_param a=b' did not accept a value containing '='"
24554
24555         jobid_var_new=$($LCTL get_param -n $testname)
24556         [[ "$jobid_var_new" == "$new_value" ]] ||
24557                 error "'set_param a=b' failed on a value containing '='"
24558
24559         # Reset the $testname to test the other format
24560         $LCTL set_param $testname=$jobid_var_old
24561         jobid_var_new=$($LCTL get_param -n $testname)
24562         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
24563                 error "failed to reset $testname"
24564
24565         $LCTL set_param $testname $new_value ||
24566                 error "'set_param a b' did not accept a value containing '='"
24567
24568         jobid_var_new=$($LCTL get_param -n $testname)
24569         [[ "$jobid_var_new" == "$new_value" ]] ||
24570                 error "'set_param a b' failed on a value containing '='"
24571
24572         $LCTL set_param $testname $jobid_var_old
24573         jobid_var_new=$($LCTL get_param -n $testname)
24574         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
24575                 error "failed to reset $testname"
24576 }
24577 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
24578
24579 test_401e() { # LU-14779
24580         $LCTL list_param -R "ldlm.namespaces.MGC*" ||
24581                 error "lctl list_param MGC* failed"
24582         $LCTL get_param "ldlm.namespaces.MGC*" || error "lctl get_param failed"
24583         $LCTL get_param "ldlm.namespaces.MGC*.lru_size" ||
24584                 error "lctl get_param lru_size failed"
24585 }
24586 run_test 401e "verify 'lctl get_param' works with NID in parameter"
24587
24588 test_402() {
24589         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
24590         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
24591                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
24592         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
24593                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
24594                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
24595         remote_mds_nodsh && skip "remote MDS with nodsh"
24596
24597         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
24598 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
24599         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
24600         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
24601                 echo "Touch failed - OK"
24602 }
24603 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
24604
24605 test_403() {
24606         local file1=$DIR/$tfile.1
24607         local file2=$DIR/$tfile.2
24608         local tfile=$TMP/$tfile
24609
24610         rm -f $file1 $file2 $tfile
24611
24612         touch $file1
24613         ln $file1 $file2
24614
24615         # 30 sec OBD_TIMEOUT in ll_getattr()
24616         # right before populating st_nlink
24617         $LCTL set_param fail_loc=0x80001409
24618         stat -c %h $file1 > $tfile &
24619
24620         # create an alias, drop all locks and reclaim the dentry
24621         < $file2
24622         cancel_lru_locks mdc
24623         cancel_lru_locks osc
24624         sysctl -w vm.drop_caches=2
24625
24626         wait
24627
24628         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
24629
24630         rm -f $tfile $file1 $file2
24631 }
24632 run_test 403 "i_nlink should not drop to zero due to aliasing"
24633
24634 test_404() { # LU-6601
24635         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
24636                 skip "Need server version newer than 2.8.52"
24637         remote_mds_nodsh && skip "remote MDS with nodsh"
24638
24639         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
24640                 awk '/osp .*-osc-MDT/ { print $4}')
24641
24642         local osp
24643         for osp in $mosps; do
24644                 echo "Deactivate: " $osp
24645                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
24646                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
24647                         awk -vp=$osp '$4 == p { print $2 }')
24648                 [ $stat = IN ] || {
24649                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
24650                         error "deactivate error"
24651                 }
24652                 echo "Activate: " $osp
24653                 do_facet $SINGLEMDS $LCTL --device %$osp activate
24654                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
24655                         awk -vp=$osp '$4 == p { print $2 }')
24656                 [ $stat = UP ] || {
24657                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
24658                         error "activate error"
24659                 }
24660         done
24661 }
24662 run_test 404 "validate manual {de}activated works properly for OSPs"
24663
24664 test_405() {
24665         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
24666         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
24667                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
24668                         skip "Layout swap lock is not supported"
24669
24670         check_swap_layouts_support
24671         check_swap_layout_no_dom $DIR
24672
24673         test_mkdir $DIR/$tdir
24674         swap_lock_test -d $DIR/$tdir ||
24675                 error "One layout swap locked test failed"
24676 }
24677 run_test 405 "Various layout swap lock tests"
24678
24679 test_406() {
24680         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24681         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
24682         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
24683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24684         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
24685                 skip "Need MDS version at least 2.8.50"
24686
24687         local def_stripe_size=$($LFS getstripe -S $MOUNT)
24688         local test_pool=$TESTNAME
24689
24690         pool_add $test_pool || error "pool_add failed"
24691         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
24692                 error "pool_add_targets failed"
24693
24694         save_layout_restore_at_exit $MOUNT
24695
24696         # parent set default stripe count only, child will stripe from both
24697         # parent and fs default
24698         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
24699                 error "setstripe $MOUNT failed"
24700         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
24701         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
24702         for i in $(seq 10); do
24703                 local f=$DIR/$tdir/$tfile.$i
24704                 touch $f || error "touch failed"
24705                 local count=$($LFS getstripe -c $f)
24706                 [ $count -eq $OSTCOUNT ] ||
24707                         error "$f stripe count $count != $OSTCOUNT"
24708                 local offset=$($LFS getstripe -i $f)
24709                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
24710                 local size=$($LFS getstripe -S $f)
24711                 [ $size -eq $((def_stripe_size * 2)) ] ||
24712                         error "$f stripe size $size != $((def_stripe_size * 2))"
24713                 local pool=$($LFS getstripe -p $f)
24714                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
24715         done
24716
24717         # change fs default striping, delete parent default striping, now child
24718         # will stripe from new fs default striping only
24719         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
24720                 error "change $MOUNT default stripe failed"
24721         $LFS setstripe -c 0 $DIR/$tdir ||
24722                 error "delete $tdir default stripe failed"
24723         for i in $(seq 11 20); do
24724                 local f=$DIR/$tdir/$tfile.$i
24725                 touch $f || error "touch $f failed"
24726                 local count=$($LFS getstripe -c $f)
24727                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
24728                 local offset=$($LFS getstripe -i $f)
24729                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
24730                 local size=$($LFS getstripe -S $f)
24731                 [ $size -eq $def_stripe_size ] ||
24732                         error "$f stripe size $size != $def_stripe_size"
24733                 local pool=$($LFS getstripe -p $f)
24734                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
24735         done
24736
24737         unlinkmany $DIR/$tdir/$tfile. 1 20
24738
24739         local f=$DIR/$tdir/$tfile
24740         pool_remove_all_targets $test_pool $f
24741         pool_remove $test_pool $f
24742 }
24743 run_test 406 "DNE support fs default striping"
24744
24745 test_407() {
24746         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24747         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
24748                 skip "Need MDS version at least 2.8.55"
24749         remote_mds_nodsh && skip "remote MDS with nodsh"
24750
24751         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
24752                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
24753         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
24754                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
24755         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
24756
24757         #define OBD_FAIL_DT_TXN_STOP    0x2019
24758         for idx in $(seq $MDSCOUNT); do
24759                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
24760         done
24761         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
24762         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
24763                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
24764         true
24765 }
24766 run_test 407 "transaction fail should cause operation fail"
24767
24768 test_408() {
24769         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
24770
24771         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
24772         lctl set_param fail_loc=0x8000040a
24773         # let ll_prepare_partial_page() fail
24774         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
24775
24776         rm -f $DIR/$tfile
24777
24778         # create at least 100 unused inodes so that
24779         # shrink_icache_memory(0) should not return 0
24780         touch $DIR/$tfile-{0..100}
24781         rm -f $DIR/$tfile-{0..100}
24782         sync
24783
24784         echo 2 > /proc/sys/vm/drop_caches
24785 }
24786 run_test 408 "drop_caches should not hang due to page leaks"
24787
24788 test_409()
24789 {
24790         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
24791
24792         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
24793         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
24794         touch $DIR/$tdir/guard || error "(2) Fail to create"
24795
24796         local PREFIX=$(str_repeat 'A' 128)
24797         echo "Create 1K hard links start at $(date)"
24798         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
24799                 error "(3) Fail to hard link"
24800
24801         echo "Links count should be right although linkEA overflow"
24802         stat $DIR/$tdir/guard || error "(4) Fail to stat"
24803         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
24804         [ $linkcount -eq 1001 ] ||
24805                 error "(5) Unexpected hard links count: $linkcount"
24806
24807         echo "List all links start at $(date)"
24808         ls -l $DIR/$tdir/foo > /dev/null ||
24809                 error "(6) Fail to list $DIR/$tdir/foo"
24810
24811         echo "Unlink hard links start at $(date)"
24812         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
24813                 error "(7) Fail to unlink"
24814         echo "Unlink hard links finished at $(date)"
24815 }
24816 run_test 409 "Large amount of cross-MDTs hard links on the same file"
24817
24818 test_410()
24819 {
24820         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
24821                 skip "Need client version at least 2.9.59"
24822         [ -f $LUSTRE/tests/kernel/kinode.ko ] ||
24823                 skip "Need MODULES build"
24824
24825         # Create a file, and stat it from the kernel
24826         local testfile=$DIR/$tfile
24827         touch $testfile
24828
24829         local run_id=$RANDOM
24830         local my_ino=$(stat --format "%i" $testfile)
24831
24832         # Try to insert the module. This will always fail as the
24833         # module is designed to not be inserted.
24834         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
24835             &> /dev/null
24836
24837         # Anything but success is a test failure
24838         dmesg | grep -q \
24839             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
24840             error "no inode match"
24841 }
24842 run_test 410 "Test inode number returned from kernel thread"
24843
24844 cleanup_test411_cgroup() {
24845         trap 0
24846         rmdir "$1"
24847 }
24848
24849 test_411() {
24850         local cg_basedir=/sys/fs/cgroup/memory
24851         # LU-9966
24852         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
24853                 skip "no setup for cgroup"
24854
24855         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
24856                 error "test file creation failed"
24857         cancel_lru_locks osc
24858
24859         # Create a very small memory cgroup to force a slab allocation error
24860         local cgdir=$cg_basedir/osc_slab_alloc
24861         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
24862         trap "cleanup_test411_cgroup $cgdir" EXIT
24863         echo 2M > $cgdir/memory.kmem.limit_in_bytes
24864         echo 1M > $cgdir/memory.limit_in_bytes
24865
24866         # Should not LBUG, just be killed by oom-killer
24867         # dd will return 0 even allocation failure in some environment.
24868         # So don't check return value
24869         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
24870         cleanup_test411_cgroup $cgdir
24871
24872         return 0
24873 }
24874 run_test 411 "Slab allocation error with cgroup does not LBUG"
24875
24876 test_412() {
24877         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24878         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
24879                 skip "Need server version at least 2.10.55"
24880         fi
24881
24882         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
24883                 error "mkdir failed"
24884         $LFS getdirstripe $DIR/$tdir
24885         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
24886         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
24887                 error "expect $((MDSCOUT - 1)) get $stripe_index"
24888         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
24889         [ $stripe_count -eq 2 ] ||
24890                 error "expect 2 get $stripe_count"
24891 }
24892 run_test 412 "mkdir on specific MDTs"
24893
24894 generate_uneven_mdts() {
24895         local threshold=$1
24896         local lmv_qos_maxage
24897         local lod_qos_maxage
24898         local ffree
24899         local bavail
24900         local max
24901         local min
24902         local max_index
24903         local min_index
24904         local tmp
24905         local i
24906
24907         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
24908         $LCTL set_param lmv.*.qos_maxage=1
24909         stack_trap "$LCTL set_param \
24910                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" RETURN
24911         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
24912                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
24913         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
24914                 lod.*.mdt_qos_maxage=1
24915         stack_trap "do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
24916                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" RETURN
24917
24918         echo
24919         echo "Check for uneven MDTs: "
24920
24921         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
24922         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
24923         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
24924
24925         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24926         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
24927         max_index=0
24928         min_index=0
24929         for ((i = 1; i < ${#ffree[@]}; i++)); do
24930                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
24931                 if [ $tmp -gt $max ]; then
24932                         max=$tmp
24933                         max_index=$i
24934                 fi
24935                 if [ $tmp -lt $min ]; then
24936                         min=$tmp
24937                         min_index=$i
24938                 fi
24939         done
24940
24941         (( ${ffree[min_index]} > 0 )) ||
24942                 skip "no free files in MDT$min_index"
24943         (( ${ffree[min_index]} < 10000000 )) ||
24944                 skip "too many free files in MDT$min_index"
24945
24946         # Check if we need to generate uneven MDTs
24947         local diff=$(((max - min) * 100 / min))
24948         local testdir=$DIR/$tdir-fillmdt
24949         local start
24950
24951         mkdir -p $testdir
24952
24953         i=0
24954         while (( diff < threshold )); do
24955                 # generate uneven MDTs, create till $threshold% diff
24956                 echo -n "weight diff=$diff% must be > $threshold% ..."
24957                 echo "Fill MDT$min_index with 1000 files: loop $i"
24958                 testdir=$DIR/$tdir-fillmdt/$i
24959                 [ -d $testdir ] || $LFS mkdir -i $min_index $testdir ||
24960                         error "mkdir $testdir failed"
24961                 $LFS setstripe -E 1M -L mdt $testdir ||
24962                         error "setstripe $testdir failed"
24963                 start=$SECONDS
24964                 for F in f.{0..999}; do
24965                         dd if=/dev/zero of=$testdir/$F bs=64K count=1 > \
24966                                 /dev/null 2>&1 || error "dd $F failed"
24967                 done
24968
24969                 # wait for QOS to update
24970                 (( SECONDS < start + 1 )) && sleep $((start + 1 - SECONDS))
24971
24972                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
24973                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
24974                 max=$(((${ffree[max_index]} >> 8) * \
24975                         (${bavail[max_index]} * bsize >> 16)))
24976                 min=$(((${ffree[min_index]} >> 8) * \
24977                         (${bavail[min_index]} * bsize >> 16)))
24978                 diff=$(((max - min) * 100 / min))
24979                 i=$((i + 1))
24980         done
24981
24982         echo "MDT filesfree available: ${ffree[@]}"
24983         echo "MDT blocks available: ${bavail[@]}"
24984         echo "weight diff=$diff%"
24985 }
24986
24987 test_qos_mkdir() {
24988         local mkdir_cmd=$1
24989         local stripe_count=$2
24990         local mdts=$(comma_list $(mdts_nodes))
24991
24992         local testdir
24993         local lmv_qos_prio_free
24994         local lmv_qos_threshold_rr
24995         local lmv_qos_maxage
24996         local lod_qos_prio_free
24997         local lod_qos_threshold_rr
24998         local lod_qos_maxage
24999         local count
25000         local i
25001
25002         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
25003         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
25004         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
25005                 head -n1)
25006         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
25007         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
25008         stack_trap "$LCTL set_param \
25009                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null"
25010         stack_trap "$LCTL set_param \
25011                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null"
25012         stack_trap "$LCTL set_param \
25013                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
25014
25015         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
25016                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
25017         lod_qos_prio_free=${lod_qos_prio_free%%%}
25018         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
25019                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
25020         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
25021         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
25022                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
25023         stack_trap "do_nodes $mdts $LCTL set_param \
25024                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null"
25025         stack_trap "do_nodes $mdts $LCTL set_param \
25026                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null"
25027         stack_trap "do_nodes $mdts $LCTL set_param \
25028                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null"
25029
25030         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
25031         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
25032
25033         testdir=$DIR/$tdir-s$stripe_count/rr
25034
25035         local stripe_index=$($LFS getstripe -m $testdir)
25036         local test_mkdir_rr=true
25037
25038         getfattr -d -m dmv -e hex $testdir | grep dmv
25039         if (( $? == 0 && $MDS1_VERSION >= $(version_code 2.14.51) )); then
25040                 echo "defstripe: '$($LFS getdirstripe -D $testdir)'"
25041                 (( $($LFS getdirstripe -D --max-inherit-rr $testdir) == 0 )) &&
25042                         test_mkdir_rr=false
25043         fi
25044
25045         echo
25046         $test_mkdir_rr &&
25047                 echo "Mkdir (stripe_count $stripe_count) roundrobin:" ||
25048                 echo "Mkdir (stripe_count $stripe_count) on stripe $stripe_index"
25049
25050         stack_trap "unlinkmany -d $testdir/subdir $((100 * MDSCOUNT))"
25051         for (( i = 0; i < 100 * MDSCOUNT; i++ )); do
25052                 eval $mkdir_cmd $testdir/subdir$i ||
25053                         error "$mkdir_cmd subdir$i failed"
25054         done
25055
25056         for (( i = 0; i < $MDSCOUNT; i++ )); do
25057                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
25058                 echo "$count directories created on MDT$i"
25059                 if $test_mkdir_rr; then
25060                         (( $count == 100 )) ||
25061                                 error "subdirs are not evenly distributed"
25062                 elif (( $i == $stripe_index )); then
25063                         (( $count == 100 * MDSCOUNT )) ||
25064                                 error "$count subdirs created on MDT$i"
25065                 else
25066                         (( $count == 0 )) ||
25067                                 error "$count subdirs created on MDT$i"
25068                 fi
25069
25070                 if $test_mkdir_rr && [ $stripe_count -gt 1 ]; then
25071                         count=$($LFS getdirstripe $testdir/* |
25072                                 grep -c -P "^\s+$i\t")
25073                         echo "$count stripes created on MDT$i"
25074                         # deviation should < 5% of average
25075                         (( $count >= 95 * stripe_count &&
25076                            $count <= 105 * stripe_count)) ||
25077                                 error "stripes are not evenly distributed"
25078                 fi
25079         done
25080
25081         echo
25082         echo "Check for uneven MDTs: "
25083
25084         local ffree
25085         local bavail
25086         local max
25087         local min
25088         local max_index
25089         local min_index
25090         local tmp
25091
25092         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
25093         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
25094         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
25095
25096         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25097         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25098         max_index=0
25099         min_index=0
25100         for ((i = 1; i < ${#ffree[@]}; i++)); do
25101                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
25102                 if [ $tmp -gt $max ]; then
25103                         max=$tmp
25104                         max_index=$i
25105                 fi
25106                 if [ $tmp -lt $min ]; then
25107                         min=$tmp
25108                         min_index=$i
25109                 fi
25110         done
25111
25112         (( ${ffree[min_index]} > 0 )) ||
25113                 skip "no free files in MDT$min_index"
25114         (( ${ffree[min_index]} < 10000000 )) ||
25115                 skip "too many free files in MDT$min_index"
25116
25117         echo "MDT filesfree available: ${ffree[@]}"
25118         echo "MDT blocks available: ${bavail[@]}"
25119         echo "weight diff=$(((max - min) * 100 / min))%"
25120         echo
25121         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
25122
25123         $LCTL set_param lmv.*.qos_threshold_rr=0 > /dev/null
25124         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
25125         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=0 > /dev/null
25126         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
25127         # decrease statfs age, so that it can be updated in time
25128         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
25129         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
25130
25131         sleep 1
25132
25133         testdir=$DIR/$tdir-s$stripe_count/qos
25134         local num=200
25135
25136         stack_trap "unlinkmany -d $testdir/subdir $((num * MDSCOUNT))"
25137         for (( i = 0; i < num * MDSCOUNT; i++ )); do
25138                 eval $mkdir_cmd $testdir/subdir$i ||
25139                         error "$mkdir_cmd subdir$i failed"
25140         done
25141
25142         max=0
25143         for (( i = 0; i < $MDSCOUNT; i++ )); do
25144                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
25145                 (( count > max )) && max=$count
25146                 echo "$count directories created on MDT$i"
25147         done
25148
25149         min=$($LFS getdirstripe -i $testdir/* | grep -c "^$min_index$")
25150
25151         # D-value should > 10% of averge
25152         (( max - min > num / 10 )) ||
25153                 error "subdirs shouldn't be evenly distributed: $max - $min < $((num / 10))"
25154
25155         # ditto for stripes
25156         if (( stripe_count > 1 )); then
25157                 max=0
25158                 for (( i = 0; i < $MDSCOUNT; i++ )); do
25159                         count=$($LFS getdirstripe $testdir/* |
25160                                 grep -c -P "^\s+$i\t")
25161                         (( count > max )) && max=$count
25162                         echo "$count stripes created on MDT$i"
25163                 done
25164
25165                 min=$($LFS getdirstripe $testdir/* |
25166                         grep -c -P "^\s+$min_index\t")
25167                 (( max - min > num * stripe_count / 10 )) ||
25168                         error "stripes shouldn't be evenly distributed: $max - $min < $((num / 10)) * $stripe_count"
25169         fi
25170 }
25171
25172 most_full_mdt() {
25173         local ffree
25174         local bavail
25175         local bsize
25176         local min
25177         local min_index
25178         local tmp
25179
25180         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
25181         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
25182         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
25183
25184         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25185         min_index=0
25186         for ((i = 1; i < ${#ffree[@]}; i++)); do
25187                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
25188                 (( tmp < min )) && min=$tmp && min_index=$i
25189         done
25190
25191         echo -n $min_index
25192 }
25193
25194 test_413a() {
25195         [ $MDSCOUNT -lt 2 ] &&
25196                 skip "We need at least 2 MDTs for this test"
25197
25198         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
25199                 skip "Need server version at least 2.12.52"
25200
25201         local stripe_count
25202
25203         generate_uneven_mdts 100
25204         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
25205                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
25206                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
25207                 $LFS mkdir -i $(most_full_mdt) $DIR/$tdir-s$stripe_count/qos ||
25208                         error "mkdir failed"
25209                 test_qos_mkdir "$LFS mkdir -i -1 -c $stripe_count" $stripe_count
25210         done
25211 }
25212 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
25213
25214 test_413b() {
25215         [ $MDSCOUNT -lt 2 ] &&
25216                 skip "We need at least 2 MDTs for this test"
25217
25218         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
25219                 skip "Need server version at least 2.12.52"
25220
25221         local testdir
25222         local stripe_count
25223
25224         generate_uneven_mdts 100
25225         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
25226                 testdir=$DIR/$tdir-s$stripe_count
25227                 mkdir $testdir || error "mkdir $testdir failed"
25228                 mkdir $testdir/rr || error "mkdir rr failed"
25229                 $LFS mkdir -i $(most_full_mdt) $testdir/qos ||
25230                         error "mkdir qos failed"
25231                 $LFS setdirstripe -D -c $stripe_count --max-inherit-rr 2 \
25232                         $testdir/rr || error "setdirstripe rr failed"
25233                 $LFS setdirstripe -D -c $stripe_count $testdir/qos ||
25234                         error "setdirstripe failed"
25235                 test_qos_mkdir "mkdir" $stripe_count
25236         done
25237 }
25238 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
25239
25240 test_413c() {
25241         (( $MDSCOUNT >= 2 )) ||
25242                 skip "We need at least 2 MDTs for this test"
25243
25244         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
25245                 skip "Need server version at least 2.14.51"
25246
25247         local testdir
25248         local inherit
25249         local inherit_rr
25250
25251         testdir=$DIR/${tdir}-s1
25252         mkdir $testdir || error "mkdir $testdir failed"
25253         mkdir $testdir/rr || error "mkdir rr failed"
25254         $LFS mkdir -i $(most_full_mdt) $testdir/qos || error "mkdir qos failed"
25255         # default max_inherit is -1, default max_inherit_rr is 0
25256         $LFS setdirstripe -D -c 1 $testdir/rr ||
25257                 error "setdirstripe rr failed"
25258         $LFS setdirstripe -D -c 1 -i -1 -X 2 --max-inherit-rr 1 $testdir/qos ||
25259                 error "setdirstripe qos failed"
25260         test_qos_mkdir "mkdir" 1
25261
25262         mkdir $testdir/rr/level1 || error "mkdir rr/level1 failed"
25263         inherit=$($LFS getdirstripe -D -X $testdir/rr/level1)
25264         (( $inherit == -1 )) || error "rr/level1 inherit $inherit != -1"
25265         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/rr/level1)
25266         (( $inherit_rr == 0 )) || error "rr/level1 inherit-rr $inherit_rr != 0"
25267
25268         mkdir $testdir/qos/level1 || error "mkdir qos/level1 failed"
25269         inherit=$($LFS getdirstripe -D -X $testdir/qos/level1)
25270         (( $inherit == 1 )) || error "qos/level1 inherit $inherit != 1"
25271         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/qos/level1)
25272         (( $inherit_rr == 0 )) || error "qos/level1 inherit-rr $inherit_rr != 0"
25273         mkdir $testdir/qos/level1/level2 || error "mkdir level2 failed"
25274         getfattr -d -m dmv -e hex $testdir/qos/level1/level2 | grep dmv &&
25275                 error "level2 shouldn't have default LMV" || true
25276 }
25277 run_test 413c "mkdir with default LMV max inherit rr"
25278
25279 test_413d() {
25280         (( MDSCOUNT >= 2 )) ||
25281                 skip "We need at least 2 MDTs for this test"
25282
25283         (( MDS1_VERSION >= $(version_code 2.14.51) )) ||
25284                 skip "Need server version at least 2.14.51"
25285
25286         local lmv_qos_threshold_rr
25287
25288         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
25289                 head -n1)
25290         stack_trap "$LCTL set_param \
25291                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
25292
25293         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
25294         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
25295         getfattr -d -m dmv -e hex $DIR/$tdir | grep dmv &&
25296                 error "$tdir shouldn't have default LMV"
25297         createmany -d $DIR/$tdir/sub $((100 * MDSCOUNT)) ||
25298                 error "mkdir sub failed"
25299
25300         local count=$($LFS getstripe -m $DIR/$tdir/* | grep -c ^0)
25301
25302         (( count == 100 )) || error "$count subdirs on MDT0"
25303 }
25304 run_test 413d "inherit ROOT default LMV"
25305
25306 test_413z() {
25307         local pids=""
25308         local subdir
25309         local pid
25310
25311         for subdir in $(\ls -1 -d $DIR/d413*-fillmdt/*); do
25312                 unlinkmany $subdir/f. 1000 &
25313                 pids="$pids $!"
25314         done
25315
25316         for pid in $pids; do
25317                 wait $pid
25318         done
25319 }
25320 run_test 413z "413 test cleanup"
25321
25322 test_414() {
25323 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
25324         $LCTL set_param fail_loc=0x80000521
25325         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
25326         rm -f $DIR/$tfile
25327 }
25328 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
25329
25330 test_415() {
25331         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25332         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
25333                 skip "Need server version at least 2.11.52"
25334
25335         # LU-11102
25336         local total
25337         local setattr_pid
25338         local start_time
25339         local end_time
25340         local duration
25341
25342         total=500
25343         # this test may be slow on ZFS
25344         [ "$mds1_FSTYPE" == "zfs" ] && total=100
25345
25346         # though this test is designed for striped directory, let's test normal
25347         # directory too since lock is always saved as CoS lock.
25348         test_mkdir $DIR/$tdir || error "mkdir $tdir"
25349         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
25350
25351         (
25352                 while true; do
25353                         touch $DIR/$tdir
25354                 done
25355         ) &
25356         setattr_pid=$!
25357
25358         start_time=$(date +%s)
25359         for i in $(seq $total); do
25360                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
25361                         > /dev/null
25362         done
25363         end_time=$(date +%s)
25364         duration=$((end_time - start_time))
25365
25366         kill -9 $setattr_pid
25367
25368         echo "rename $total files took $duration sec"
25369         [ $duration -lt 100 ] || error "rename took $duration sec"
25370 }
25371 run_test 415 "lock revoke is not missing"
25372
25373 test_416() {
25374         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
25375                 skip "Need server version at least 2.11.55"
25376
25377         # define OBD_FAIL_OSD_TXN_START    0x19a
25378         do_facet mds1 lctl set_param fail_loc=0x19a
25379
25380         lfs mkdir -c $MDSCOUNT $DIR/$tdir
25381
25382         true
25383 }
25384 run_test 416 "transaction start failure won't cause system hung"
25385
25386 cleanup_417() {
25387         trap 0
25388         do_nodes $(comma_list $(mdts_nodes)) \
25389                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
25390         do_nodes $(comma_list $(mdts_nodes)) \
25391                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
25392         do_nodes $(comma_list $(mdts_nodes)) \
25393                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
25394 }
25395
25396 test_417() {
25397         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25398         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
25399                 skip "Need MDS version at least 2.11.56"
25400
25401         trap cleanup_417 RETURN EXIT
25402
25403         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
25404         do_nodes $(comma_list $(mdts_nodes)) \
25405                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
25406         $LFS migrate -m 0 $DIR/$tdir.1 &&
25407                 error "migrate dir $tdir.1 should fail"
25408
25409         do_nodes $(comma_list $(mdts_nodes)) \
25410                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
25411         $LFS mkdir -i 1 $DIR/$tdir.2 &&
25412                 error "create remote dir $tdir.2 should fail"
25413
25414         do_nodes $(comma_list $(mdts_nodes)) \
25415                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
25416         $LFS mkdir -c 2 $DIR/$tdir.3 &&
25417                 error "create striped dir $tdir.3 should fail"
25418         true
25419 }
25420 run_test 417 "disable remote dir, striped dir and dir migration"
25421
25422 # Checks that the outputs of df [-i] and lfs df [-i] match
25423 #
25424 # usage: check_lfs_df <blocks | inodes> <mountpoint>
25425 check_lfs_df() {
25426         local dir=$2
25427         local inodes
25428         local df_out
25429         local lfs_df_out
25430         local count
25431         local passed=false
25432
25433         # blocks or inodes
25434         [ "$1" == "blocks" ] && inodes= || inodes="-i"
25435
25436         for count in {1..100}; do
25437                 do_rpc_nodes "$CLIENTS" cancel_lru_locks
25438                 sync; sleep 0.2
25439
25440                 # read the lines of interest
25441                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
25442                         error "df $inodes $dir | tail -n +2 failed"
25443                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
25444                         error "lfs df $inodes $dir | grep summary: failed"
25445
25446                 # skip first substrings of each output as they are different
25447                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
25448                 # compare the two outputs
25449                 passed=true
25450                 for i in {1..5}; do
25451                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
25452                 done
25453                 $passed && break
25454         done
25455
25456         if ! $passed; then
25457                 df -P $inodes $dir
25458                 echo
25459                 lfs df $inodes $dir
25460                 error "df and lfs df $1 output mismatch: "      \
25461                       "df ${inodes}: ${df_out[*]}, "            \
25462                       "lfs df ${inodes}: ${lfs_df_out[*]}"
25463         fi
25464 }
25465
25466 test_418() {
25467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25468
25469         local dir=$DIR/$tdir
25470         local numfiles=$((RANDOM % 4096 + 2))
25471         local numblocks=$((RANDOM % 256 + 1))
25472
25473         wait_delete_completed
25474         test_mkdir $dir
25475
25476         # check block output
25477         check_lfs_df blocks $dir
25478         # check inode output
25479         check_lfs_df inodes $dir
25480
25481         # create a single file and retest
25482         echo "Creating a single file and testing"
25483         createmany -o $dir/$tfile- 1 &>/dev/null ||
25484                 error "creating 1 file in $dir failed"
25485         check_lfs_df blocks $dir
25486         check_lfs_df inodes $dir
25487
25488         # create a random number of files
25489         echo "Creating $((numfiles - 1)) files and testing"
25490         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
25491                 error "creating $((numfiles - 1)) files in $dir failed"
25492
25493         # write a random number of blocks to the first test file
25494         echo "Writing $numblocks 4K blocks and testing"
25495         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
25496                 count=$numblocks &>/dev/null ||
25497                 error "dd to $dir/${tfile}-0 failed"
25498
25499         # retest
25500         check_lfs_df blocks $dir
25501         check_lfs_df inodes $dir
25502
25503         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
25504                 error "unlinking $numfiles files in $dir failed"
25505 }
25506 run_test 418 "df and lfs df outputs match"
25507
25508 test_419()
25509 {
25510         local dir=$DIR/$tdir
25511
25512         mkdir -p $dir
25513         touch $dir/file
25514
25515         cancel_lru_locks mdc
25516
25517         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
25518         $LCTL set_param fail_loc=0x1410
25519         cat $dir/file
25520         $LCTL set_param fail_loc=0
25521         rm -rf $dir
25522 }
25523 run_test 419 "Verify open file by name doesn't crash kernel"
25524
25525 test_420()
25526 {
25527         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
25528                 skip "Need MDS version at least 2.12.53"
25529
25530         local SAVE_UMASK=$(umask)
25531         local dir=$DIR/$tdir
25532         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
25533
25534         mkdir -p $dir
25535         umask 0000
25536         mkdir -m03777 $dir/testdir
25537         ls -dn $dir/testdir
25538         # Need to remove trailing '.' when SELinux is enabled
25539         local dirperms=$(ls -dn $dir/testdir |
25540                          awk '{ sub(/\.$/, "", $1); print $1}')
25541         [ $dirperms == "drwxrwsrwt" ] ||
25542                 error "incorrect perms on $dir/testdir"
25543
25544         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
25545                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
25546         ls -n $dir/testdir/testfile
25547         local fileperms=$(ls -n $dir/testdir/testfile |
25548                           awk '{ sub(/\.$/, "", $1); print $1}')
25549         [ $fileperms == "-rwxr-xr-x" ] ||
25550                 error "incorrect perms on $dir/testdir/testfile"
25551
25552         umask $SAVE_UMASK
25553 }
25554 run_test 420 "clear SGID bit on non-directories for non-members"
25555
25556 test_421a() {
25557         local cnt
25558         local fid1
25559         local fid2
25560
25561         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25562                 skip "Need MDS version at least 2.12.54"
25563
25564         test_mkdir $DIR/$tdir
25565         createmany -o $DIR/$tdir/f 3
25566         cnt=$(ls -1 $DIR/$tdir | wc -l)
25567         [ $cnt != 3 ] && error "unexpected #files: $cnt"
25568
25569         fid1=$(lfs path2fid $DIR/$tdir/f1)
25570         fid2=$(lfs path2fid $DIR/$tdir/f2)
25571         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
25572
25573         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
25574         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
25575
25576         cnt=$(ls -1 $DIR/$tdir | wc -l)
25577         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
25578
25579         rm -f $DIR/$tdir/f3 || error "can't remove f3"
25580         createmany -o $DIR/$tdir/f 3
25581         cnt=$(ls -1 $DIR/$tdir | wc -l)
25582         [ $cnt != 3 ] && error "unexpected #files: $cnt"
25583
25584         fid1=$(lfs path2fid $DIR/$tdir/f1)
25585         fid2=$(lfs path2fid $DIR/$tdir/f2)
25586         echo "remove using fsname $FSNAME"
25587         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
25588
25589         cnt=$(ls -1 $DIR/$tdir | wc -l)
25590         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
25591 }
25592 run_test 421a "simple rm by fid"
25593
25594 test_421b() {
25595         local cnt
25596         local FID1
25597         local FID2
25598
25599         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25600                 skip "Need MDS version at least 2.12.54"
25601
25602         test_mkdir $DIR/$tdir
25603         createmany -o $DIR/$tdir/f 3
25604         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
25605         MULTIPID=$!
25606
25607         FID1=$(lfs path2fid $DIR/$tdir/f1)
25608         FID2=$(lfs path2fid $DIR/$tdir/f2)
25609         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
25610
25611         kill -USR1 $MULTIPID
25612         wait
25613
25614         cnt=$(ls $DIR/$tdir | wc -l)
25615         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
25616 }
25617 run_test 421b "rm by fid on open file"
25618
25619 test_421c() {
25620         local cnt
25621         local FIDS
25622
25623         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25624                 skip "Need MDS version at least 2.12.54"
25625
25626         test_mkdir $DIR/$tdir
25627         createmany -o $DIR/$tdir/f 3
25628         touch $DIR/$tdir/$tfile
25629         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
25630         cnt=$(ls -1 $DIR/$tdir | wc -l)
25631         [ $cnt != 184 ] && error "unexpected #files: $cnt"
25632
25633         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
25634         $LFS rmfid $DIR $FID1 || error "rmfid failed"
25635
25636         cnt=$(ls $DIR/$tdir | wc -l)
25637         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
25638 }
25639 run_test 421c "rm by fid against hardlinked files"
25640
25641 test_421d() {
25642         local cnt
25643         local FIDS
25644
25645         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25646                 skip "Need MDS version at least 2.12.54"
25647
25648         test_mkdir $DIR/$tdir
25649         createmany -o $DIR/$tdir/f 4097
25650         cnt=$(ls -1 $DIR/$tdir | wc -l)
25651         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
25652
25653         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
25654         $LFS rmfid $DIR $FIDS || error "rmfid failed"
25655
25656         cnt=$(ls $DIR/$tdir | wc -l)
25657         rm -rf $DIR/$tdir
25658         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25659 }
25660 run_test 421d "rmfid en masse"
25661
25662 test_421e() {
25663         local cnt
25664         local FID
25665
25666         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25667         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25668                 skip "Need MDS version at least 2.12.54"
25669
25670         mkdir -p $DIR/$tdir
25671         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
25672         createmany -o $DIR/$tdir/striped_dir/f 512
25673         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25674         [ $cnt != 512 ] && error "unexpected #files: $cnt"
25675
25676         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
25677                 sed "s/[/][^:]*://g")
25678         $LFS rmfid $DIR $FIDS || error "rmfid failed"
25679
25680         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
25681         rm -rf $DIR/$tdir
25682         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25683 }
25684 run_test 421e "rmfid in DNE"
25685
25686 test_421f() {
25687         local cnt
25688         local FID
25689
25690         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25691                 skip "Need MDS version at least 2.12.54"
25692
25693         test_mkdir $DIR/$tdir
25694         touch $DIR/$tdir/f
25695         cnt=$(ls -1 $DIR/$tdir | wc -l)
25696         [ $cnt != 1 ] && error "unexpected #files: $cnt"
25697
25698         FID=$(lfs path2fid $DIR/$tdir/f)
25699         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
25700         # rmfid should fail
25701         cnt=$(ls -1 $DIR/$tdir | wc -l)
25702         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
25703
25704         chmod a+rw $DIR/$tdir
25705         ls -la $DIR/$tdir
25706         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
25707         # rmfid should fail
25708         cnt=$(ls -1 $DIR/$tdir | wc -l)
25709         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
25710
25711         rm -f $DIR/$tdir/f
25712         $RUNAS touch $DIR/$tdir/f
25713         FID=$(lfs path2fid $DIR/$tdir/f)
25714         echo "rmfid as root"
25715         $LFS rmfid $DIR $FID || error "rmfid as root failed"
25716         cnt=$(ls -1 $DIR/$tdir | wc -l)
25717         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
25718
25719         rm -f $DIR/$tdir/f
25720         $RUNAS touch $DIR/$tdir/f
25721         cnt=$(ls -1 $DIR/$tdir | wc -l)
25722         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
25723         FID=$(lfs path2fid $DIR/$tdir/f)
25724         # rmfid w/o user_fid2path mount option should fail
25725         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
25726         cnt=$(ls -1 $DIR/$tdir | wc -l)
25727         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
25728
25729         tmpdir=$(mktemp -d /tmp/lustre-XXXXXX)
25730         stack_trap "rmdir $tmpdir"
25731         mount_client $tmpdir "$MOUNT_OPTS,user_fid2path" ||
25732                 error "failed to mount client'"
25733         stack_trap "umount_client $tmpdir"
25734
25735         $RUNAS $LFS rmfid $tmpdir $FID || error "rmfid failed"
25736         # rmfid should succeed
25737         cnt=$(ls -1 $tmpdir/$tdir | wc -l)
25738         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
25739
25740         # rmfid shouldn't allow to remove files due to dir's permission
25741         chmod a+rwx $tmpdir/$tdir
25742         touch $tmpdir/$tdir/f
25743         ls -la $tmpdir/$tdir
25744         FID=$(lfs path2fid $tmpdir/$tdir/f)
25745         $RUNAS $LFS rmfid $tmpdir $FID && error "rmfid didn't fail"
25746         return 0
25747 }
25748 run_test 421f "rmfid checks permissions"
25749
25750 test_421g() {
25751         local cnt
25752         local FIDS
25753
25754         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25755         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
25756                 skip "Need MDS version at least 2.12.54"
25757
25758         mkdir -p $DIR/$tdir
25759         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
25760         createmany -o $DIR/$tdir/striped_dir/f 512
25761         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25762         [ $cnt != 512 ] && error "unexpected #files: $cnt"
25763
25764         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
25765                 sed "s/[/][^:]*://g")
25766
25767         rm -f $DIR/$tdir/striped_dir/f1*
25768         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
25769         removed=$((512 - cnt))
25770
25771         # few files have been just removed, so we expect
25772         # rmfid to fail on their fids
25773         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
25774         [ $removed != $errors ] && error "$errors != $removed"
25775
25776         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
25777         rm -rf $DIR/$tdir
25778         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
25779 }
25780 run_test 421g "rmfid to return errors properly"
25781
25782 test_422() {
25783         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
25784         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
25785         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
25786         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
25787         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
25788
25789         local amc=$(at_max_get client)
25790         local amo=$(at_max_get mds1)
25791         local timeout=`lctl get_param -n timeout`
25792
25793         at_max_set 0 client
25794         at_max_set 0 mds1
25795
25796 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
25797         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
25798                         fail_val=$(((2*timeout + 10)*1000))
25799         touch $DIR/$tdir/d3/file &
25800         sleep 2
25801 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
25802         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
25803                         fail_val=$((2*timeout + 5))
25804         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
25805         local pid=$!
25806         sleep 1
25807         kill -9 $pid
25808         sleep $((2 * timeout))
25809         echo kill $pid
25810         kill -9 $pid
25811         lctl mark touch
25812         touch $DIR/$tdir/d2/file3
25813         touch $DIR/$tdir/d2/file4
25814         touch $DIR/$tdir/d2/file5
25815
25816         wait
25817         at_max_set $amc client
25818         at_max_set $amo mds1
25819
25820         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
25821         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
25822                 error "Watchdog is always throttled"
25823 }
25824 run_test 422 "kill a process with RPC in progress"
25825
25826 stat_test() {
25827     df -h $MOUNT &
25828     df -h $MOUNT &
25829     df -h $MOUNT &
25830     df -h $MOUNT &
25831     df -h $MOUNT &
25832     df -h $MOUNT &
25833 }
25834
25835 test_423() {
25836     local _stats
25837     # ensure statfs cache is expired
25838     sleep 2;
25839
25840     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
25841     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
25842
25843     return 0
25844 }
25845 run_test 423 "statfs should return a right data"
25846
25847 test_424() {
25848 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
25849         $LCTL set_param fail_loc=0x80000522
25850         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
25851         rm -f $DIR/$tfile
25852 }
25853 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
25854
25855 test_425() {
25856         test_mkdir -c -1 $DIR/$tdir
25857         $LFS setstripe -c -1 $DIR/$tdir
25858
25859         lru_resize_disable "" 100
25860         stack_trap "lru_resize_enable" EXIT
25861
25862         sleep 5
25863
25864         for i in $(seq $((MDSCOUNT * 125))); do
25865                 local t=$DIR/$tdir/$tfile_$i
25866
25867                 dd if=/dev/zero of=$t bs=4K count=1 > /dev/null 2>&1 ||
25868                         error_noexit "Create file $t"
25869         done
25870         stack_trap "rm -rf $DIR/$tdir" EXIT
25871
25872         for oscparam in $($LCTL list_param ldlm.namespaces.*osc-[-0-9a-f]*); do
25873                 local lru_size=$($LCTL get_param -n $oscparam.lru_size)
25874                 local lock_count=$($LCTL get_param -n $oscparam.lock_count)
25875
25876                 [ $lock_count -le $lru_size ] ||
25877                         error "osc lock count $lock_count > lru size $lru_size"
25878         done
25879
25880         for mdcparam in $($LCTL list_param ldlm.namespaces.*mdc-*); do
25881                 local lru_size=$($LCTL get_param -n $mdcparam.lru_size)
25882                 local lock_count=$($LCTL get_param -n $mdcparam.lock_count)
25883
25884                 [ $lock_count -le $lru_size ] ||
25885                         error "mdc lock count $lock_count > lru size $lru_size"
25886         done
25887 }
25888 run_test 425 "lock count should not exceed lru size"
25889
25890 test_426() {
25891         splice-test -r $DIR/$tfile
25892         splice-test -rd $DIR/$tfile
25893         splice-test $DIR/$tfile
25894         splice-test -d $DIR/$tfile
25895 }
25896 run_test 426 "splice test on Lustre"
25897
25898 test_427() {
25899         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
25900         (( $MDS1_VERSION >= $(version_code 2.12.4) )) ||
25901                 skip "Need MDS version at least 2.12.4"
25902         local log
25903
25904         mkdir $DIR/$tdir
25905         mkdir $DIR/$tdir/1
25906         mkdir $DIR/$tdir/2
25907         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/1/dir
25908         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/2/dir2
25909
25910         $LFS getdirstripe $DIR/$tdir/1/dir
25911
25912         #first setfattr for creating updatelog
25913         setfattr -n user.attr0 -v "some text" $DIR/$tdir/1/dir
25914
25915 #define OBD_FAIL_OUT_OBJECT_MISS        0x1708
25916         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x80001708
25917         setfattr -n user.attr1 -v "some text" $DIR/$tdir/1/dir &
25918         setfattr -n user.attr2 -v "another attr"  $DIR/$tdir/2/dir2 &
25919
25920         sleep 2
25921         fail mds2
25922         wait_recovery_complete mds2 $((2*TIMEOUT))
25923
25924         log=$(do_facet mds1 dmesg | tac | sed "/${TESTNAME//_/ }/,$ d")
25925         echo $log | grep "get update log failed" &&
25926                 error "update log corruption is detected" || true
25927 }
25928 run_test 427 "Failed DNE2 update request shouldn't corrupt updatelog"
25929
25930 test_428() {
25931         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25932         local cache_limit=$CACHE_MAX
25933
25934         stack_trap "$LCTL set_param -n llite.*.max_cached_mb=$cache_limit"
25935         $LCTL set_param -n llite.*.max_cached_mb=64
25936
25937         mkdir $DIR/$tdir
25938         $LFS setstripe -c 1 $DIR/$tdir
25939         eval touch $DIR/$tdir/$tfile.{1..$OSTCOUNT}
25940         stack_trap "rm -f $DIR/$tdir/$tfile.*"
25941         #test write
25942         for f in $(seq 4); do
25943                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$f bs=128M count=1 &
25944         done
25945         wait
25946
25947         cancel_lru_locks osc
25948         # Test read
25949         for f in $(seq 4); do
25950                 dd if=$DIR/$tdir/$tfile.$f of=/dev/null bs=128M count=1 &
25951         done
25952         wait
25953 }
25954 run_test 428 "large block size IO should not hang"
25955
25956 test_429() { # LU-7915 / LU-10948
25957         local ll_opencache_threshold_count="llite.*.opencache_threshold_count"
25958         local testfile=$DIR/$tfile
25959         local mdc_rpcstats="mdc.$FSNAME-MDT0000-*.stats"
25960         local new_flag=1
25961         local first_rpc
25962         local second_rpc
25963         local third_rpc
25964
25965         $LCTL get_param $ll_opencache_threshold_count ||
25966                 skip "client does not have opencache parameter"
25967
25968         set_opencache $new_flag
25969         stack_trap "restore_opencache"
25970         [ $($LCTL get_param -n $ll_opencache_threshold_count) == $new_flag ] ||
25971                 error "enable opencache failed"
25972         touch $testfile
25973         # drop MDC DLM locks
25974         cancel_lru_locks mdc
25975         # clear MDC RPC stats counters
25976         $LCTL set_param $mdc_rpcstats=clear
25977
25978         # According to the current implementation, we need to run 3 times
25979         # open & close file to verify if opencache is enabled correctly.
25980         # 1st, RPCs are sent for lookup/open and open handle is released on
25981         #      close finally.
25982         # 2nd, RPC is sent for open, MDS_OPEN_LOCK is fetched automatically,
25983         #      so open handle won't be released thereafter.
25984         # 3rd, No RPC is sent out.
25985         $MULTIOP $testfile oc || error "multiop failed"
25986         first_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25987         echo "1st: $first_rpc RPCs in flight"
25988
25989         $MULTIOP $testfile oc || error "multiop failed"
25990         second_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25991         echo "2nd: $second_rpc RPCs in flight"
25992
25993         $MULTIOP $testfile oc || error "multiop failed"
25994         third_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
25995         echo "3rd: $third_rpc RPCs in flight"
25996
25997         #verify no MDC RPC is sent
25998         [[ $second_rpc == $third_rpc ]] || error "MDC RPC is still sent"
25999 }
26000 run_test 429 "verify if opencache flag on client side does work"
26001
26002 lseek_test_430() {
26003         local offset
26004         local file=$1
26005
26006         # data at [200K, 400K)
26007         dd if=/dev/urandom of=$file bs=256K count=1 seek=1 ||
26008                 error "256K->512K dd fails"
26009         # data at [2M, 3M)
26010         dd if=/dev/urandom of=$file bs=1M count=1 seek=2 ||
26011                 error "2M->3M dd fails"
26012         # data at [4M, 5M)
26013         dd if=/dev/urandom of=$file bs=1M count=1 seek=4 ||
26014                 error "4M->5M dd fails"
26015         echo "Data at 256K...512K, 2M...3M and 4M...5M"
26016         # start at first component hole #1
26017         printf "Seeking hole from 1000 ... "
26018         offset=$(lseek_test -l 1000 $file)
26019         echo $offset
26020         [[ $offset == 1000 ]] || error "offset $offset != 1000"
26021         printf "Seeking data from 1000 ... "
26022         offset=$(lseek_test -d 1000 $file)
26023         echo $offset
26024         [[ $offset == 262144 ]] || error "offset $offset != 262144"
26025
26026         # start at first component data block
26027         printf "Seeking hole from 300000 ... "
26028         offset=$(lseek_test -l 300000 $file)
26029         echo $offset
26030         [[ $offset == 524288 ]] || error "offset $offset != 524288"
26031         printf "Seeking data from 300000 ... "
26032         offset=$(lseek_test -d 300000 $file)
26033         echo $offset
26034         [[ $offset == 300000 ]] || error "offset $offset != 300000"
26035
26036         # start at the first component but beyond end of object size
26037         printf "Seeking hole from 1000000 ... "
26038         offset=$(lseek_test -l 1000000 $file)
26039         echo $offset
26040         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
26041         printf "Seeking data from 1000000 ... "
26042         offset=$(lseek_test -d 1000000 $file)
26043         echo $offset
26044         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
26045
26046         # start at second component stripe 2 (empty file)
26047         printf "Seeking hole from 1500000 ... "
26048         offset=$(lseek_test -l 1500000 $file)
26049         echo $offset
26050         [[ $offset == 1500000 ]] || error "offset $offset != 1500000"
26051         printf "Seeking data from 1500000 ... "
26052         offset=$(lseek_test -d 1500000 $file)
26053         echo $offset
26054         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
26055
26056         # start at second component stripe 1 (all data)
26057         printf "Seeking hole from 3000000 ... "
26058         offset=$(lseek_test -l 3000000 $file)
26059         echo $offset
26060         [[ $offset == 3145728 ]] || error "offset $offset != 3145728"
26061         printf "Seeking data from 3000000 ... "
26062         offset=$(lseek_test -d 3000000 $file)
26063         echo $offset
26064         [[ $offset == 3000000 ]] || error "offset $offset != 3000000"
26065
26066         dd if=/dev/urandom of=$file bs=640K count=1 seek=1 ||
26067                 error "2nd dd fails"
26068         echo "Add data block at 640K...1280K"
26069
26070         # start at before new data block, in hole
26071         printf "Seeking hole from 600000 ... "
26072         offset=$(lseek_test -l 600000 $file)
26073         echo $offset
26074         [[ $offset == 600000 ]] || error "offset $offset != 600000"
26075         printf "Seeking data from 600000 ... "
26076         offset=$(lseek_test -d 600000 $file)
26077         echo $offset
26078         [[ $offset == 655360 ]] || error "offset $offset != 655360"
26079
26080         # start at the first component new data block
26081         printf "Seeking hole from 1000000 ... "
26082         offset=$(lseek_test -l 1000000 $file)
26083         echo $offset
26084         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
26085         printf "Seeking data from 1000000 ... "
26086         offset=$(lseek_test -d 1000000 $file)
26087         echo $offset
26088         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
26089
26090         # start at second component stripe 2, new data
26091         printf "Seeking hole from 1200000 ... "
26092         offset=$(lseek_test -l 1200000 $file)
26093         echo $offset
26094         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
26095         printf "Seeking data from 1200000 ... "
26096         offset=$(lseek_test -d 1200000 $file)
26097         echo $offset
26098         [[ $offset == 1200000 ]] || error "offset $offset != 1200000"
26099
26100         # start beyond file end
26101         printf "Using offset > filesize ... "
26102         lseek_test -l 4000000 $file && error "lseek should fail"
26103         printf "Using offset > filesize ... "
26104         lseek_test -d 4000000 $file && error "lseek should fail"
26105
26106         printf "Done\n\n"
26107 }
26108
26109 test_430a() {
26110         $LCTL get_param mdc.*.import | grep -q 'connect_flags:.*seek' ||
26111                 skip "MDT does not support SEEK_HOLE"
26112
26113         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
26114                 skip "OST does not support SEEK_HOLE"
26115
26116         local file=$DIR/$tdir/$tfile
26117
26118         mkdir -p $DIR/$tdir
26119
26120         $LFS setstripe -E 1M -L mdt -E eof -c2 $file
26121         # OST stripe #1 will have continuous data at [1M, 3M)
26122         # OST stripe #2 is empty
26123         echo "Component #1: 1M DoM, component #2: EOF, 2 stripes 1M"
26124         lseek_test_430 $file
26125         rm $file
26126         $LFS setstripe -E 1M -c2 -S 64K -E 10M -c2 -S 1M $file
26127         echo "Component #1: 1M, 2 stripes 64K, component #2: EOF, 2 stripes 1M"
26128         lseek_test_430 $file
26129         rm $file
26130         $LFS setstripe -c2 -S 512K $file
26131         echo "Two stripes, stripe size 512K"
26132         lseek_test_430 $file
26133         rm $file
26134         # FLR with stale mirror
26135         $LFS setstripe -N -E 512K -c1 -S 64K -E eof -c2 -S 512K \
26136                        -N -c2 -S 1M $file
26137         echo "Mirrored file:"
26138         echo "Component #1: 512K, stripe 64K, component #2: EOF, 2 stripes 512K"
26139         echo "Plain 2 stripes 1M"
26140         lseek_test_430 $file
26141         rm $file
26142 }
26143 run_test 430a "lseek: SEEK_DATA/SEEK_HOLE basic functionality"
26144
26145 test_430b() {
26146         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
26147                 skip "OST does not support SEEK_HOLE"
26148
26149         local offset
26150         local file=$DIR/$tdir/$tfile
26151
26152         mkdir -p $DIR/$tdir
26153         # Empty layout lseek should fail
26154         $MCREATE $file
26155         # seek from 0
26156         printf "Seeking hole from 0 ... "
26157         lseek_test -l 0 $file && error "lseek should fail"
26158         printf "Seeking data from 0 ... "
26159         lseek_test -d 0 $file && error "lseek should fail"
26160         rm $file
26161
26162         # 1M-hole file
26163         $LFS setstripe -E 1M -c2 -E eof $file
26164         $TRUNCATE $file 1048576
26165         printf "Seeking hole from 1000000 ... "
26166         offset=$(lseek_test -l 1000000 $file)
26167         echo $offset
26168         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
26169         printf "Seeking data from 1000000 ... "
26170         lseek_test -d 1000000 $file && error "lseek should fail"
26171         rm $file
26172
26173         # full component followed by non-inited one
26174         $LFS setstripe -E 1M -c2 -E eof $file
26175         dd if=/dev/urandom of=$file bs=1M count=1
26176         printf "Seeking hole from 1000000 ... "
26177         offset=$(lseek_test -l 1000000 $file)
26178         echo $offset
26179         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
26180         printf "Seeking hole from 1048576 ... "
26181         lseek_test -l 1048576 $file && error "lseek should fail"
26182         # init second component and truncate back
26183         echo "123" >> $file
26184         $TRUNCATE $file 1048576
26185         printf "Seeking hole from 1000000 ... "
26186         offset=$(lseek_test -l 1000000 $file)
26187         echo $offset
26188         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
26189         printf "Seeking hole from 1048576 ... "
26190         lseek_test -l 1048576 $file && error "lseek should fail"
26191         # boundary checks for big values
26192         dd if=/dev/urandom of=$file.10g bs=1 count=1 seek=10G
26193         offset=$(lseek_test -d 0 $file.10g)
26194         [[ $offset == 10737418240 ]] || error "offset $offset != 10737418240"
26195         dd if=/dev/urandom of=$file.100g bs=1 count=1 seek=100G
26196         offset=$(lseek_test -d 0 $file.100g)
26197         [[ $offset == 107374182400 ]] || error "offset $offset != 107374182400"
26198         return 0
26199 }
26200 run_test 430b "lseek: SEEK_DATA/SEEK_HOLE special cases"
26201
26202 test_430c() {
26203         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
26204                 skip "OST does not support SEEK_HOLE"
26205
26206         local file=$DIR/$tdir/$tfile
26207         local start
26208
26209         mkdir -p $DIR/$tdir
26210         dd if=/dev/urandom of=$file bs=1k count=1 seek=5M
26211
26212         # cp version 8.33+ prefers lseek over fiemap
26213         if [[ $(cp --version | head -n1 | sed "s/[^0-9]//g") -ge 833 ]]; then
26214                 start=$SECONDS
26215                 time cp $file /dev/null
26216                 (( SECONDS - start < 5 )) ||
26217                         error "cp: too long runtime $((SECONDS - start))"
26218
26219         fi
26220         # tar version 1.29+ supports SEEK_HOLE/DATA
26221         if [[ $(tar --version | head -n1 | sed "s/[^0-9]//g") -ge 129 ]]; then
26222                 start=$SECONDS
26223                 time tar cS $file - | cat > /dev/null
26224                 (( SECONDS - start < 5 )) ||
26225                         error "tar: too long runtime $((SECONDS - start))"
26226         fi
26227 }
26228 run_test 430c "lseek: external tools check"
26229
26230 test_431() { # LU-14187
26231         local file=$DIR/$tdir/$tfile
26232
26233         mkdir -p $DIR/$tdir
26234         $LFS setstripe -c 1 -i 0 $file || error "lfs setstripe failed"
26235         dd if=/dev/urandom of=$file bs=4k count=1
26236         dd if=/dev/urandom of=$file bs=4k count=1 seek=10 conv=notrunc
26237         dd if=/dev/urandom of=$file bs=4k count=1 seek=12 conv=notrunc
26238         #define OBD_FAIL_OST_RESTART_IO 0x251
26239         do_facet ost1 "$LCTL set_param fail_loc=0x251"
26240         $LFS setstripe -c 1 -i 0 $file.0 || error "lfs setstripe failed"
26241         cp $file $file.0
26242         cancel_lru_locks
26243         sync_all_data
26244         echo 3 > /proc/sys/vm/drop_caches
26245         diff  $file $file.0 || error "data diff"
26246 }
26247 run_test 431 "Restart transaction for IO"
26248
26249 cleanup_test_432() {
26250         do_facet mgs $LCTL nodemap_activate 0
26251         wait_nm_sync active
26252 }
26253
26254 test_432() {
26255         local tmpdir=$TMP/dir432
26256
26257         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
26258                 skip "Need MDS version at least 2.14.52"
26259
26260         stack_trap cleanup_test_432 EXIT
26261         mkdir $DIR/$tdir
26262         mkdir $tmpdir
26263
26264         do_facet mgs $LCTL nodemap_activate 1
26265         wait_nm_sync active
26266         do_facet mgs $LCTL nodemap_modify --name default \
26267                 --property admin --value 1
26268         do_facet mgs $LCTL nodemap_modify --name default \
26269                 --property trusted --value 1
26270         cancel_lru_locks mdc
26271         wait_nm_sync default admin_nodemap
26272         wait_nm_sync default trusted_nodemap
26273
26274         if [ $(mv $tmpdir $DIR/$tdir/ 2>&1 |
26275                grep -ci "Operation not permitted") -ne 0 ]; then
26276                 error "mv $tmpdir $DIR/$tdir/ hits 'Operation not permitted'"
26277         fi
26278 }
26279 run_test 432 "mv dir from outside Lustre"
26280
26281 prep_801() {
26282         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
26283         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
26284                 skip "Need server version at least 2.9.55"
26285
26286         start_full_debug_logging
26287 }
26288
26289 post_801() {
26290         stop_full_debug_logging
26291 }
26292
26293 barrier_stat() {
26294         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
26295                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
26296                            awk '/The barrier for/ { print $7 }')
26297                 echo $st
26298         else
26299                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
26300                 echo \'$st\'
26301         fi
26302 }
26303
26304 barrier_expired() {
26305         local expired
26306
26307         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
26308                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
26309                           awk '/will be expired/ { print $7 }')
26310         else
26311                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
26312         fi
26313
26314         echo $expired
26315 }
26316
26317 test_801a() {
26318         prep_801
26319
26320         echo "Start barrier_freeze at: $(date)"
26321         #define OBD_FAIL_BARRIER_DELAY          0x2202
26322         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
26323         # Do not reduce barrier time - See LU-11873
26324         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
26325
26326         sleep 2
26327         local b_status=$(barrier_stat)
26328         echo "Got barrier status at: $(date)"
26329         [ "$b_status" = "'freezing_p1'" ] ||
26330                 error "(1) unexpected barrier status $b_status"
26331
26332         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
26333         wait
26334         b_status=$(barrier_stat)
26335         [ "$b_status" = "'frozen'" ] ||
26336                 error "(2) unexpected barrier status $b_status"
26337
26338         local expired=$(barrier_expired)
26339         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
26340         sleep $((expired + 3))
26341
26342         b_status=$(barrier_stat)
26343         [ "$b_status" = "'expired'" ] ||
26344                 error "(3) unexpected barrier status $b_status"
26345
26346         # Do not reduce barrier time - See LU-11873
26347         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
26348                 error "(4) fail to freeze barrier"
26349
26350         b_status=$(barrier_stat)
26351         [ "$b_status" = "'frozen'" ] ||
26352                 error "(5) unexpected barrier status $b_status"
26353
26354         echo "Start barrier_thaw at: $(date)"
26355         #define OBD_FAIL_BARRIER_DELAY          0x2202
26356         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
26357         do_facet mgs $LCTL barrier_thaw $FSNAME &
26358
26359         sleep 2
26360         b_status=$(barrier_stat)
26361         echo "Got barrier status at: $(date)"
26362         [ "$b_status" = "'thawing'" ] ||
26363                 error "(6) unexpected barrier status $b_status"
26364
26365         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
26366         wait
26367         b_status=$(barrier_stat)
26368         [ "$b_status" = "'thawed'" ] ||
26369                 error "(7) unexpected barrier status $b_status"
26370
26371         #define OBD_FAIL_BARRIER_FAILURE        0x2203
26372         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
26373         do_facet mgs $LCTL barrier_freeze $FSNAME
26374
26375         b_status=$(barrier_stat)
26376         [ "$b_status" = "'failed'" ] ||
26377                 error "(8) unexpected barrier status $b_status"
26378
26379         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
26380         do_facet mgs $LCTL barrier_thaw $FSNAME
26381
26382         post_801
26383 }
26384 run_test 801a "write barrier user interfaces and stat machine"
26385
26386 test_801b() {
26387         prep_801
26388
26389         mkdir $DIR/$tdir || error "(1) fail to mkdir"
26390         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
26391         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
26392         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
26393         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
26394
26395         cancel_lru_locks mdc
26396
26397         # 180 seconds should be long enough
26398         do_facet mgs $LCTL barrier_freeze $FSNAME 180
26399
26400         local b_status=$(barrier_stat)
26401         [ "$b_status" = "'frozen'" ] ||
26402                 error "(6) unexpected barrier status $b_status"
26403
26404         mkdir $DIR/$tdir/d0/d10 &
26405         mkdir_pid=$!
26406
26407         touch $DIR/$tdir/d1/f13 &
26408         touch_pid=$!
26409
26410         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
26411         ln_pid=$!
26412
26413         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
26414         mv_pid=$!
26415
26416         rm -f $DIR/$tdir/d4/f12 &
26417         rm_pid=$!
26418
26419         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
26420
26421         # To guarantee taht the 'stat' is not blocked
26422         b_status=$(barrier_stat)
26423         [ "$b_status" = "'frozen'" ] ||
26424                 error "(8) unexpected barrier status $b_status"
26425
26426         # let above commands to run at background
26427         sleep 5
26428
26429         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
26430         ps -p $touch_pid || error "(10) touch should be blocked"
26431         ps -p $ln_pid || error "(11) link should be blocked"
26432         ps -p $mv_pid || error "(12) rename should be blocked"
26433         ps -p $rm_pid || error "(13) unlink should be blocked"
26434
26435         b_status=$(barrier_stat)
26436         [ "$b_status" = "'frozen'" ] ||
26437                 error "(14) unexpected barrier status $b_status"
26438
26439         do_facet mgs $LCTL barrier_thaw $FSNAME
26440         b_status=$(barrier_stat)
26441         [ "$b_status" = "'thawed'" ] ||
26442                 error "(15) unexpected barrier status $b_status"
26443
26444         wait $mkdir_pid || error "(16) mkdir should succeed"
26445         wait $touch_pid || error "(17) touch should succeed"
26446         wait $ln_pid || error "(18) link should succeed"
26447         wait $mv_pid || error "(19) rename should succeed"
26448         wait $rm_pid || error "(20) unlink should succeed"
26449
26450         post_801
26451 }
26452 run_test 801b "modification will be blocked by write barrier"
26453
26454 test_801c() {
26455         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26456
26457         prep_801
26458
26459         stop mds2 || error "(1) Fail to stop mds2"
26460
26461         do_facet mgs $LCTL barrier_freeze $FSNAME 30
26462
26463         local b_status=$(barrier_stat)
26464         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
26465                 do_facet mgs $LCTL barrier_thaw $FSNAME
26466                 error "(2) unexpected barrier status $b_status"
26467         }
26468
26469         do_facet mgs $LCTL barrier_rescan $FSNAME ||
26470                 error "(3) Fail to rescan barrier bitmap"
26471
26472         # Do not reduce barrier time - See LU-11873
26473         do_facet mgs $LCTL barrier_freeze $FSNAME 20
26474
26475         b_status=$(barrier_stat)
26476         [ "$b_status" = "'frozen'" ] ||
26477                 error "(4) unexpected barrier status $b_status"
26478
26479         do_facet mgs $LCTL barrier_thaw $FSNAME
26480         b_status=$(barrier_stat)
26481         [ "$b_status" = "'thawed'" ] ||
26482                 error "(5) unexpected barrier status $b_status"
26483
26484         local devname=$(mdsdevname 2)
26485
26486         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
26487
26488         do_facet mgs $LCTL barrier_rescan $FSNAME ||
26489                 error "(7) Fail to rescan barrier bitmap"
26490
26491         post_801
26492 }
26493 run_test 801c "rescan barrier bitmap"
26494
26495 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
26496 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
26497 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
26498 saved_MOUNT_OPTS=$MOUNT_OPTS
26499
26500 cleanup_802a() {
26501         trap 0
26502
26503         stopall
26504         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
26505         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
26506         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
26507         MOUNT_OPTS=$saved_MOUNT_OPTS
26508         setupall
26509 }
26510
26511 test_802a() {
26512         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
26513         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
26514         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
26515                 skip "Need server version at least 2.9.55"
26516
26517         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
26518
26519         mkdir $DIR/$tdir || error "(1) fail to mkdir"
26520
26521         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
26522                 error "(2) Fail to copy"
26523
26524         trap cleanup_802a EXIT
26525
26526         # sync by force before remount as readonly
26527         sync; sync_all_data; sleep 3; sync_all_data
26528
26529         stopall
26530
26531         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
26532         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
26533         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
26534
26535         echo "Mount the server as read only"
26536         setupall server_only || error "(3) Fail to start servers"
26537
26538         echo "Mount client without ro should fail"
26539         mount_client $MOUNT &&
26540                 error "(4) Mount client without 'ro' should fail"
26541
26542         echo "Mount client with ro should succeed"
26543         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
26544         mount_client $MOUNT ||
26545                 error "(5) Mount client with 'ro' should succeed"
26546
26547         echo "Modify should be refused"
26548         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
26549
26550         echo "Read should be allowed"
26551         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
26552                 error "(7) Read should succeed under ro mode"
26553
26554         cleanup_802a
26555 }
26556 run_test 802a "simulate readonly device"
26557
26558 test_802b() {
26559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26560         remote_mds_nodsh && skip "remote MDS with nodsh"
26561
26562         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
26563                 skip "readonly option not available"
26564
26565         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
26566
26567         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
26568                 error "(2) Fail to copy"
26569
26570         # write back all cached data before setting MDT to readonly
26571         cancel_lru_locks
26572         sync_all_data
26573
26574         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
26575         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
26576
26577         echo "Modify should be refused"
26578         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
26579
26580         echo "Read should be allowed"
26581         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
26582                 error "(7) Read should succeed under ro mode"
26583
26584         # disable readonly
26585         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
26586 }
26587 run_test 802b "be able to set MDTs to readonly"
26588
26589 test_803a() {
26590         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26591         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
26592                 skip "MDS needs to be newer than 2.10.54"
26593
26594         mkdir_on_mdt0 $DIR/$tdir
26595         # Create some objects on all MDTs to trigger related logs objects
26596         for idx in $(seq $MDSCOUNT); do
26597                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
26598                         $DIR/$tdir/dir${idx} ||
26599                         error "Fail to create $DIR/$tdir/dir${idx}"
26600         done
26601
26602         sync; sleep 3
26603         wait_delete_completed # ensure old test cleanups are finished
26604         echo "before create:"
26605         $LFS df -i $MOUNT
26606         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26607
26608         for i in {1..10}; do
26609                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
26610                         error "Fail to create $DIR/$tdir/foo$i"
26611         done
26612
26613         sync; sleep 3
26614         echo "after create:"
26615         $LFS df -i $MOUNT
26616         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26617
26618         # allow for an llog to be cleaned up during the test
26619         [ $after_used -ge $((before_used + 10 - 1)) ] ||
26620                 error "before ($before_used) + 10 > after ($after_used)"
26621
26622         for i in {1..10}; do
26623                 rm -rf $DIR/$tdir/foo$i ||
26624                         error "Fail to remove $DIR/$tdir/foo$i"
26625         done
26626
26627         sleep 3 # avoid MDT return cached statfs
26628         wait_delete_completed
26629         echo "after unlink:"
26630         $LFS df -i $MOUNT
26631         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
26632
26633         # allow for an llog to be created during the test
26634         [ $after_used -le $((before_used + 1)) ] ||
26635                 error "after ($after_used) > before ($before_used) + 1"
26636 }
26637 run_test 803a "verify agent object for remote object"
26638
26639 test_803b() {
26640         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26641         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
26642                 skip "MDS needs to be newer than 2.13.56"
26643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26644
26645         for i in $(seq 0 $((MDSCOUNT - 1))); do
26646                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
26647         done
26648
26649         local before=0
26650         local after=0
26651
26652         local tmp
26653
26654         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
26655         for i in $(seq 0 $((MDSCOUNT - 1))); do
26656                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
26657                         awk '/getattr/ { print $2 }')
26658                 before=$((before + tmp))
26659         done
26660         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
26661         for i in $(seq 0 $((MDSCOUNT - 1))); do
26662                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
26663                         awk '/getattr/ { print $2 }')
26664                 after=$((after + tmp))
26665         done
26666
26667         [ $before -eq $after ] || error "getattr count $before != $after"
26668 }
26669 run_test 803b "remote object can getattr from cache"
26670
26671 test_804() {
26672         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
26673         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
26674                 skip "MDS needs to be newer than 2.10.54"
26675         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
26676
26677         mkdir -p $DIR/$tdir
26678         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
26679                 error "Fail to create $DIR/$tdir/dir0"
26680
26681         local fid=$($LFS path2fid $DIR/$tdir/dir0)
26682         local dev=$(mdsdevname 2)
26683
26684         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26685                 grep ${fid} || error "NOT found agent entry for dir0"
26686
26687         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
26688                 error "Fail to create $DIR/$tdir/dir1"
26689
26690         touch $DIR/$tdir/dir1/foo0 ||
26691                 error "Fail to create $DIR/$tdir/dir1/foo0"
26692         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
26693         local rc=0
26694
26695         for idx in $(seq $MDSCOUNT); do
26696                 dev=$(mdsdevname $idx)
26697                 do_facet mds${idx} \
26698                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26699                         grep ${fid} && rc=$idx
26700         done
26701
26702         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
26703                 error "Fail to rename foo0 to foo1"
26704         if [ $rc -eq 0 ]; then
26705                 for idx in $(seq $MDSCOUNT); do
26706                         dev=$(mdsdevname $idx)
26707                         do_facet mds${idx} \
26708                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26709                         grep ${fid} && rc=$idx
26710                 done
26711         fi
26712
26713         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
26714                 error "Fail to rename foo1 to foo2"
26715         if [ $rc -eq 0 ]; then
26716                 for idx in $(seq $MDSCOUNT); do
26717                         dev=$(mdsdevname $idx)
26718                         do_facet mds${idx} \
26719                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
26720                         grep ${fid} && rc=$idx
26721                 done
26722         fi
26723
26724         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
26725
26726         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
26727                 error "Fail to link to $DIR/$tdir/dir1/foo2"
26728         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
26729                 error "Fail to rename foo2 to foo0"
26730         unlink $DIR/$tdir/dir1/foo0 ||
26731                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
26732         rm -rf $DIR/$tdir/dir0 ||
26733                 error "Fail to rm $DIR/$tdir/dir0"
26734
26735         for idx in $(seq $MDSCOUNT); do
26736                 dev=$(mdsdevname $idx)
26737                 rc=0
26738
26739                 stop mds${idx}
26740                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
26741                         rc=$?
26742                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
26743                         error "mount mds$idx failed"
26744                 df $MOUNT > /dev/null 2>&1
26745
26746                 # e2fsck should not return error
26747                 [ $rc -eq 0 ] ||
26748                         error "e2fsck detected error on MDT${idx}: rc=$rc"
26749         done
26750 }
26751 run_test 804 "verify agent entry for remote entry"
26752
26753 cleanup_805() {
26754         do_facet $SINGLEMDS zfs set quota=$old $fsset
26755         unlinkmany $DIR/$tdir/f- 1000000
26756         trap 0
26757 }
26758
26759 test_805() {
26760         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
26761         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
26762         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
26763                 skip "netfree not implemented before 0.7"
26764         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
26765                 skip "Need MDS version at least 2.10.57"
26766
26767         local fsset
26768         local freekb
26769         local usedkb
26770         local old
26771         local quota
26772         local pref="osd-zfs.$FSNAME-MDT0000."
26773
26774         # limit available space on MDS dataset to meet nospace issue
26775         # quickly. then ZFS 0.7.2 can use reserved space if asked
26776         # properly (using netfree flag in osd_declare_destroy()
26777         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
26778         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
26779                 gawk '{print $3}')
26780         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
26781         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
26782         let "usedkb=usedkb-freekb"
26783         let "freekb=freekb/2"
26784         if let "freekb > 5000"; then
26785                 let "freekb=5000"
26786         fi
26787         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
26788         trap cleanup_805 EXIT
26789         mkdir_on_mdt0 $DIR/$tdir
26790         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
26791                 error "Can't set PFL layout"
26792         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
26793         rm -rf $DIR/$tdir || error "not able to remove"
26794         do_facet $SINGLEMDS zfs set quota=$old $fsset
26795         trap 0
26796 }
26797 run_test 805 "ZFS can remove from full fs"
26798
26799 # Size-on-MDS test
26800 check_lsom_data()
26801 {
26802         local file=$1
26803         local expect=$(stat -c %s $file)
26804
26805         check_lsom_size $1 $expect
26806
26807         local blocks=$($LFS getsom -b $file)
26808         expect=$(stat -c %b $file)
26809         [[ $blocks == $expect ]] ||
26810                 error "$file expected blocks: $expect, got: $blocks"
26811 }
26812
26813 check_lsom_size()
26814 {
26815         local size
26816         local expect=$2
26817
26818         cancel_lru_locks mdc
26819
26820         size=$($LFS getsom -s $1)
26821         [[ $size == $expect ]] ||
26822                 error "$file expected size: $expect, got: $size"
26823 }
26824
26825 test_806() {
26826         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26827                 skip "Need MDS version at least 2.11.52"
26828
26829         local bs=1048576
26830
26831         touch $DIR/$tfile || error "touch $tfile failed"
26832
26833         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
26834         save_lustre_params client "llite.*.xattr_cache" > $save
26835         lctl set_param llite.*.xattr_cache=0
26836         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
26837
26838         # single-threaded write
26839         echo "Test SOM for single-threaded write"
26840         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
26841                 error "write $tfile failed"
26842         check_lsom_size $DIR/$tfile $bs
26843
26844         local num=32
26845         local size=$(($num * $bs))
26846         local offset=0
26847         local i
26848
26849         echo "Test SOM for single client multi-threaded($num) write"
26850         $TRUNCATE $DIR/$tfile 0
26851         for ((i = 0; i < $num; i++)); do
26852                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26853                 local pids[$i]=$!
26854                 offset=$((offset + $bs))
26855         done
26856         for (( i=0; i < $num; i++ )); do
26857                 wait ${pids[$i]}
26858         done
26859         check_lsom_size $DIR/$tfile $size
26860
26861         $TRUNCATE $DIR/$tfile 0
26862         for ((i = 0; i < $num; i++)); do
26863                 offset=$((offset - $bs))
26864                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26865                 local pids[$i]=$!
26866         done
26867         for (( i=0; i < $num; i++ )); do
26868                 wait ${pids[$i]}
26869         done
26870         check_lsom_size $DIR/$tfile $size
26871
26872         # multi-client writes
26873         num=$(get_node_count ${CLIENTS//,/ })
26874         size=$(($num * $bs))
26875         offset=0
26876         i=0
26877
26878         echo "Test SOM for multi-client ($num) writes"
26879         $TRUNCATE $DIR/$tfile 0
26880         for client in ${CLIENTS//,/ }; do
26881                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26882                 local pids[$i]=$!
26883                 i=$((i + 1))
26884                 offset=$((offset + $bs))
26885         done
26886         for (( i=0; i < $num; i++ )); do
26887                 wait ${pids[$i]}
26888         done
26889         check_lsom_size $DIR/$tfile $offset
26890
26891         i=0
26892         $TRUNCATE $DIR/$tfile 0
26893         for client in ${CLIENTS//,/ }; do
26894                 offset=$((offset - $bs))
26895                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26896                 local pids[$i]=$!
26897                 i=$((i + 1))
26898         done
26899         for (( i=0; i < $num; i++ )); do
26900                 wait ${pids[$i]}
26901         done
26902         check_lsom_size $DIR/$tfile $size
26903
26904         # verify truncate
26905         echo "Test SOM for truncate"
26906         $TRUNCATE $DIR/$tfile 1048576
26907         check_lsom_size $DIR/$tfile 1048576
26908         $TRUNCATE $DIR/$tfile 1234
26909         check_lsom_size $DIR/$tfile 1234
26910
26911         # verify SOM blocks count
26912         echo "Verify SOM block count"
26913         $TRUNCATE $DIR/$tfile 0
26914         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
26915                 error "failed to write file $tfile"
26916         check_lsom_data $DIR/$tfile
26917 }
26918 run_test 806 "Verify Lazy Size on MDS"
26919
26920 test_807() {
26921         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
26922         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26923                 skip "Need MDS version at least 2.11.52"
26924
26925         # Registration step
26926         changelog_register || error "changelog_register failed"
26927         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
26928         changelog_users $SINGLEMDS | grep -q $cl_user ||
26929                 error "User $cl_user not found in changelog_users"
26930
26931         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
26932         save_lustre_params client "llite.*.xattr_cache" > $save
26933         lctl set_param llite.*.xattr_cache=0
26934         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
26935
26936         rm -rf $DIR/$tdir || error "rm $tdir failed"
26937         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
26938         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
26939         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
26940         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
26941                 error "truncate $tdir/trunc failed"
26942
26943         local bs=1048576
26944         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
26945                 error "write $tfile failed"
26946
26947         # multi-client wirtes
26948         local num=$(get_node_count ${CLIENTS//,/ })
26949         local offset=0
26950         local i=0
26951
26952         echo "Test SOM for multi-client ($num) writes"
26953         touch $DIR/$tfile || error "touch $tfile failed"
26954         $TRUNCATE $DIR/$tfile 0
26955         for client in ${CLIENTS//,/ }; do
26956                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
26957                 local pids[$i]=$!
26958                 i=$((i + 1))
26959                 offset=$((offset + $bs))
26960         done
26961         for (( i=0; i < $num; i++ )); do
26962                 wait ${pids[$i]}
26963         done
26964
26965         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
26966         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
26967         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
26968         check_lsom_data $DIR/$tdir/trunc
26969         check_lsom_data $DIR/$tdir/single_dd
26970         check_lsom_data $DIR/$tfile
26971
26972         rm -rf $DIR/$tdir
26973         # Deregistration step
26974         changelog_deregister || error "changelog_deregister failed"
26975 }
26976 run_test 807 "verify LSOM syncing tool"
26977
26978 check_som_nologged()
26979 {
26980         local lines=$($LFS changelog $FSNAME-MDT0000 |
26981                 grep 'x=trusted.som' | wc -l)
26982         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
26983 }
26984
26985 test_808() {
26986         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
26987                 skip "Need MDS version at least 2.11.55"
26988
26989         # Registration step
26990         changelog_register || error "changelog_register failed"
26991
26992         touch $DIR/$tfile || error "touch $tfile failed"
26993         check_som_nologged
26994
26995         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
26996                 error "write $tfile failed"
26997         check_som_nologged
26998
26999         $TRUNCATE $DIR/$tfile 1234
27000         check_som_nologged
27001
27002         $TRUNCATE $DIR/$tfile 1048576
27003         check_som_nologged
27004
27005         # Deregistration step
27006         changelog_deregister || error "changelog_deregister failed"
27007 }
27008 run_test 808 "Check trusted.som xattr not logged in Changelogs"
27009
27010 check_som_nodata()
27011 {
27012         $LFS getsom $1
27013         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
27014 }
27015
27016 test_809() {
27017         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
27018                 skip "Need MDS version at least 2.11.56"
27019
27020         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
27021                 error "failed to create DoM-only file $DIR/$tfile"
27022         touch $DIR/$tfile || error "touch $tfile failed"
27023         check_som_nodata $DIR/$tfile
27024
27025         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
27026                 error "write $tfile failed"
27027         check_som_nodata $DIR/$tfile
27028
27029         $TRUNCATE $DIR/$tfile 1234
27030         check_som_nodata $DIR/$tfile
27031
27032         $TRUNCATE $DIR/$tfile 4097
27033         check_som_nodata $DIR/$file
27034 }
27035 run_test 809 "Verify no SOM xattr store for DoM-only files"
27036
27037 test_810() {
27038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27039         $GSS && skip_env "could not run with gss"
27040         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
27041                 skip "OST < 2.12.58 doesn't align checksum"
27042
27043         set_checksums 1
27044         stack_trap "set_checksums $ORIG_CSUM" EXIT
27045         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
27046
27047         local csum
27048         local before
27049         local after
27050         for csum in $CKSUM_TYPES; do
27051                 #define OBD_FAIL_OSC_NO_GRANT   0x411
27052                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
27053                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
27054                         eval set -- $i
27055                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
27056                         before=$(md5sum $DIR/$tfile)
27057                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
27058                         after=$(md5sum $DIR/$tfile)
27059                         [ "$before" == "$after" ] ||
27060                                 error "$csum: $before != $after bs=$1 seek=$2"
27061                 done
27062         done
27063 }
27064 run_test 810 "partial page writes on ZFS (LU-11663)"
27065
27066 test_812a() {
27067         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
27068                 skip "OST < 2.12.51 doesn't support this fail_loc"
27069
27070         $LFS setstripe -c 1 -i 0 $DIR/$tfile
27071         # ensure ost1 is connected
27072         stat $DIR/$tfile >/dev/null || error "can't stat"
27073         wait_osc_import_state client ost1 FULL
27074         # no locks, no reqs to let the connection idle
27075         cancel_lru_locks osc
27076
27077         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
27078 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
27079         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
27080         wait_osc_import_state client ost1 CONNECTING
27081         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
27082
27083         stat $DIR/$tfile >/dev/null || error "can't stat file"
27084 }
27085 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
27086
27087 test_812b() { # LU-12378
27088         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
27089                 skip "OST < 2.12.51 doesn't support this fail_loc"
27090
27091         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
27092         # ensure ost1 is connected
27093         stat $DIR/$tfile >/dev/null || error "can't stat"
27094         wait_osc_import_state client ost1 FULL
27095         # no locks, no reqs to let the connection idle
27096         cancel_lru_locks osc
27097
27098         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
27099 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
27100         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
27101         wait_osc_import_state client ost1 CONNECTING
27102         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
27103
27104         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
27105         wait_osc_import_state client ost1 IDLE
27106 }
27107 run_test 812b "do not drop no resend request for idle connect"
27108
27109 test_812c() {
27110         local old
27111
27112         old=$($LCTL get_param -n osc.*.idle_timeout | head -n 1)
27113
27114         $LFS setstripe -c 1 -o 0 $DIR/$tfile
27115         $LFS getstripe $DIR/$tfile
27116         $LCTL set_param osc.*.idle_timeout=10
27117         stack_trap "$LCTL set_param osc.*.idle_timeout=$old" EXIT
27118         # ensure ost1 is connected
27119         stat $DIR/$tfile >/dev/null || error "can't stat"
27120         wait_osc_import_state client ost1 FULL
27121         # no locks, no reqs to let the connection idle
27122         cancel_lru_locks osc
27123
27124 #define OBD_FAIL_PTLRPC_IDLE_RACE        0x533
27125         $LCTL set_param fail_loc=0x80000533
27126         sleep 15
27127         dd if=/dev/zero of=$DIR/$tfile count=1 conv=sync || error "dd failed"
27128 }
27129 run_test 812c "idle import vs lock enqueue race"
27130
27131 test_813() {
27132         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
27133         [ -z "$file_heat_sav" ] && skip "no file heat support"
27134
27135         local readsample
27136         local writesample
27137         local readbyte
27138         local writebyte
27139         local readsample1
27140         local writesample1
27141         local readbyte1
27142         local writebyte1
27143
27144         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
27145         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
27146
27147         $LCTL set_param -n llite.*.file_heat=1
27148         echo "Turn on file heat"
27149         echo "Period second: $period_second, Decay percentage: $decay_pct"
27150
27151         echo "QQQQ" > $DIR/$tfile
27152         echo "QQQQ" > $DIR/$tfile
27153         echo "QQQQ" > $DIR/$tfile
27154         cat $DIR/$tfile > /dev/null
27155         cat $DIR/$tfile > /dev/null
27156         cat $DIR/$tfile > /dev/null
27157         cat $DIR/$tfile > /dev/null
27158
27159         local out=$($LFS heat_get $DIR/$tfile)
27160
27161         $LFS heat_get $DIR/$tfile
27162         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27163         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27164         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27165         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27166
27167         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
27168         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
27169         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
27170         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
27171
27172         sleep $((period_second + 3))
27173         echo "Sleep $((period_second + 3)) seconds..."
27174         # The recursion formula to calculate the heat of the file f is as
27175         # follow:
27176         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
27177         # Where Hi is the heat value in the period between time points i*I and
27178         # (i+1)*I; Ci is the access count in the period; the symbol P refers
27179         # to the weight of Ci.
27180         out=$($LFS heat_get $DIR/$tfile)
27181         $LFS heat_get $DIR/$tfile
27182         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27183         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27184         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27185         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27186
27187         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
27188                 error "read sample ($readsample) is wrong"
27189         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
27190                 error "write sample ($writesample) is wrong"
27191         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
27192                 error "read bytes ($readbyte) is wrong"
27193         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
27194                 error "write bytes ($writebyte) is wrong"
27195
27196         echo "QQQQ" > $DIR/$tfile
27197         echo "QQQQ" > $DIR/$tfile
27198         echo "QQQQ" > $DIR/$tfile
27199         cat $DIR/$tfile > /dev/null
27200         cat $DIR/$tfile > /dev/null
27201         cat $DIR/$tfile > /dev/null
27202         cat $DIR/$tfile > /dev/null
27203
27204         sleep $((period_second + 3))
27205         echo "Sleep $((period_second + 3)) seconds..."
27206
27207         out=$($LFS heat_get $DIR/$tfile)
27208         $LFS heat_get $DIR/$tfile
27209         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27210         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27211         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27212         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27213
27214         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
27215                 4 * $decay_pct) / 100") -eq 1 ] ||
27216                 error "read sample ($readsample1) is wrong"
27217         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
27218                 3 * $decay_pct) / 100") -eq 1 ] ||
27219                 error "write sample ($writesample1) is wrong"
27220         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
27221                 20 * $decay_pct) / 100") -eq 1 ] ||
27222                 error "read bytes ($readbyte1) is wrong"
27223         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
27224                 15 * $decay_pct) / 100") -eq 1 ] ||
27225                 error "write bytes ($writebyte1) is wrong"
27226
27227         echo "Turn off file heat for the file $DIR/$tfile"
27228         $LFS heat_set -o $DIR/$tfile
27229
27230         echo "QQQQ" > $DIR/$tfile
27231         echo "QQQQ" > $DIR/$tfile
27232         echo "QQQQ" > $DIR/$tfile
27233         cat $DIR/$tfile > /dev/null
27234         cat $DIR/$tfile > /dev/null
27235         cat $DIR/$tfile > /dev/null
27236         cat $DIR/$tfile > /dev/null
27237
27238         out=$($LFS heat_get $DIR/$tfile)
27239         $LFS heat_get $DIR/$tfile
27240         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27241         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27242         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27243         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27244
27245         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
27246         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
27247         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
27248         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
27249
27250         echo "Trun on file heat for the file $DIR/$tfile"
27251         $LFS heat_set -O $DIR/$tfile
27252
27253         echo "QQQQ" > $DIR/$tfile
27254         echo "QQQQ" > $DIR/$tfile
27255         echo "QQQQ" > $DIR/$tfile
27256         cat $DIR/$tfile > /dev/null
27257         cat $DIR/$tfile > /dev/null
27258         cat $DIR/$tfile > /dev/null
27259         cat $DIR/$tfile > /dev/null
27260
27261         out=$($LFS heat_get $DIR/$tfile)
27262         $LFS heat_get $DIR/$tfile
27263         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27264         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27265         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27266         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27267
27268         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
27269         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
27270         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
27271         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
27272
27273         $LFS heat_set -c $DIR/$tfile
27274         $LCTL set_param -n llite.*.file_heat=0
27275         echo "Turn off file heat support for the Lustre filesystem"
27276
27277         echo "QQQQ" > $DIR/$tfile
27278         echo "QQQQ" > $DIR/$tfile
27279         echo "QQQQ" > $DIR/$tfile
27280         cat $DIR/$tfile > /dev/null
27281         cat $DIR/$tfile > /dev/null
27282         cat $DIR/$tfile > /dev/null
27283         cat $DIR/$tfile > /dev/null
27284
27285         out=$($LFS heat_get $DIR/$tfile)
27286         $LFS heat_get $DIR/$tfile
27287         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
27288         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
27289         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
27290         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
27291
27292         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
27293         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
27294         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
27295         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
27296
27297         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
27298         rm -f $DIR/$tfile
27299 }
27300 run_test 813 "File heat verfication"
27301
27302 test_814()
27303 {
27304         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
27305         echo -n y >> $DIR/$tfile
27306         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
27307         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
27308 }
27309 run_test 814 "sparse cp works as expected (LU-12361)"
27310
27311 test_815()
27312 {
27313         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
27314         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
27315 }
27316 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
27317
27318 test_816() {
27319         local ost1_imp=$(get_osc_import_name client ost1)
27320         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
27321                          cut -d'.' -f2)
27322
27323         $LFS setstripe -c 1 -i 0 $DIR/$tfile
27324         # ensure ost1 is connected
27325
27326         stat $DIR/$tfile >/dev/null || error "can't stat"
27327         wait_osc_import_state client ost1 FULL
27328         # no locks, no reqs to let the connection idle
27329         cancel_lru_locks osc
27330         lru_resize_disable osc
27331         local before
27332         local now
27333         before=$($LCTL get_param -n \
27334                  ldlm.namespaces.$imp_name.lru_size)
27335
27336         wait_osc_import_state client ost1 IDLE
27337         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
27338         now=$($LCTL get_param -n \
27339               ldlm.namespaces.$imp_name.lru_size)
27340         [ $before == $now ] || error "lru_size changed $before != $now"
27341 }
27342 run_test 816 "do not reset lru_resize on idle reconnect"
27343
27344 cleanup_817() {
27345         umount $tmpdir
27346         exportfs -u localhost:$DIR/nfsexp
27347         rm -rf $DIR/nfsexp
27348 }
27349
27350 test_817() {
27351         systemctl restart nfs-server.service || skip "failed to restart nfsd"
27352
27353         mkdir -p $DIR/nfsexp
27354         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
27355                 error "failed to export nfs"
27356
27357         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
27358         stack_trap cleanup_817 EXIT
27359
27360         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
27361                 error "failed to mount nfs to $tmpdir"
27362
27363         cp /bin/true $tmpdir
27364         $DIR/nfsexp/true || error "failed to execute 'true' command"
27365 }
27366 run_test 817 "nfsd won't cache write lock for exec file"
27367
27368 test_818() {
27369         mkdir $DIR/$tdir
27370         $LFS setstripe -c1 -i0 $DIR/$tfile
27371         $LFS setstripe -c1 -i1 $DIR/$tfile
27372         stop $SINGLEMDS
27373         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
27374         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
27375         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
27376                 error "start $SINGLEMDS failed"
27377         rm -rf $DIR/$tdir
27378 }
27379 run_test 818 "unlink with failed llog"
27380
27381 test_819a() {
27382         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27383         cancel_lru_locks osc
27384         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
27385         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
27386         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
27387         rm -f $TDIR/$tfile
27388 }
27389 run_test 819a "too big niobuf in read"
27390
27391 test_819b() {
27392         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
27393         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
27394         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27395         cancel_lru_locks osc
27396         sleep 1
27397         rm -f $TDIR/$tfile
27398 }
27399 run_test 819b "too big niobuf in write"
27400
27401
27402 function test_820_start_ost() {
27403         sleep 5
27404
27405         for num in $(seq $OSTCOUNT); do
27406                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
27407         done
27408 }
27409
27410 test_820() {
27411         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27412
27413         mkdir $DIR/$tdir
27414         umount_client $MOUNT || error "umount failed"
27415         for num in $(seq $OSTCOUNT); do
27416                 stop ost$num
27417         done
27418
27419         # mount client with no active OSTs
27420         # so that the client can't initialize max LOV EA size
27421         # from OSC notifications
27422         mount_client $MOUNT || error "mount failed"
27423         # delay OST starting to keep this 0 max EA size for a while
27424         test_820_start_ost &
27425
27426         # create a directory on MDS2
27427         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
27428                 error "Failed to create directory"
27429         # open intent should update default EA size
27430         # see mdc_update_max_ea_from_body()
27431         # notice this is the very first RPC to MDS2
27432         out=$(cp /etc/services $DIR/$tdir/mds2 2>&1)
27433         ret=$?
27434         echo $out
27435         # With SSK, this situation can lead to -EPERM being returned.
27436         # In that case, simply retry.
27437         if [ $ret -ne 0 ] && $SHARED_KEY; then
27438                 if echo "$out" | grep -q "not permitted"; then
27439                         cp /etc/services $DIR/$tdir/mds2
27440                         ret=$?
27441                 fi
27442         fi
27443         [ $ret -eq 0 ] || error "Failed to copy files to mds$n"
27444 }
27445 run_test 820 "update max EA from open intent"
27446
27447 test_822() {
27448         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
27449
27450         save_lustre_params mds1 \
27451                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
27452         do_facet $SINGLEMDS "$LCTL set_param -n \
27453                         osp.$FSNAME-OST*MDT0000.max_create_count=0"
27454         do_facet $SINGLEMDS "$LCTL set_param -n \
27455                         osp.$FSNAME-OST0000*MDT0000.max_create_count=20000"
27456
27457         # wait for statfs update to clear OS_STATFS_NOPRECREATE
27458         local maxage=$(do_facet mds1 $LCTL get_param -n \
27459                        osp.$FSNAME-OST0000*MDT0000.maxage)
27460         sleep $((maxage + 1))
27461
27462         #define OBD_FAIL_NET_ERROR_RPC          0x532
27463         do_facet mds1 "$LCTL set_param fail_loc=0x80000532 fail_val=5"
27464
27465         stack_trap "restore_lustre_params < $p; rm $p"
27466
27467         local count=$(do_facet $SINGLEMDS "lctl get_param -n \
27468                       osp.$FSNAME-OST0000*MDT0000.create_count")
27469         for i in $(seq 1 $count); do
27470                 touch $DIR/$tfile.${i} || error "touch failed"
27471         done
27472 }
27473 run_test 822 "test precreate failure"
27474
27475 test_823() {
27476         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
27477         local OST_MAX_PRECREATE=20000
27478
27479         save_lustre_params mds1 \
27480                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
27481         do_facet $SINGLEMDS "$LCTL set_param -n \
27482                 osp.$FSNAME-OST*MDT0000.max_create_count=0"
27483         do_facet $SINGLEMDS "$LCTL set_param -n \
27484                 osp.$FSNAME-OST0000*MDT0000.max_create_count=$OST_MAX_PRECREATE"
27485
27486         stack_trap "restore_lustre_params < $p; rm $p"
27487
27488         do_facet $SINGLEMDS "$LCTL set_param -n \
27489                 osp.$FSNAME-OST*-osc-MDT*.create_count=100200"
27490
27491         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
27492                       osp.$FSNAME-OST0000*MDT0000.create_count")
27493         local max=$(do_facet $SINGLEMDS "$LCTL get_param -n \
27494                     osp.$FSNAME-OST0000*MDT0000.max_create_count")
27495         local expect_count=$(((($max/2)/256) * 256))
27496
27497         log "setting create_count to 100200:"
27498         log " -result- count: $count with max: $max, expecting: $expect_count"
27499
27500         [[ $count -eq expect_count ]] ||
27501                 error "Create count not set to max precreate."
27502 }
27503 run_test 823 "Setting create_count > OST_MAX_PRECREATE is lowered to maximum"
27504
27505 #
27506 # tests that do cleanup/setup should be run at the end
27507 #
27508
27509 test_900() {
27510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27511         local ls
27512
27513         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
27514         $LCTL set_param fail_loc=0x903
27515
27516         cancel_lru_locks MGC
27517
27518         FAIL_ON_ERROR=true cleanup
27519         FAIL_ON_ERROR=true setup
27520 }
27521 run_test 900 "umount should not race with any mgc requeue thread"
27522
27523 # LUS-6253/LU-11185
27524 test_901() {
27525         local oldc
27526         local newc
27527         local olds
27528         local news
27529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27530
27531         # some get_param have a bug to handle dot in param name
27532         cancel_lru_locks MGC
27533         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
27534         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
27535         umount_client $MOUNT || error "umount failed"
27536         mount_client $MOUNT || error "mount failed"
27537         cancel_lru_locks MGC
27538         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
27539         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
27540
27541         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
27542         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
27543
27544         return 0
27545 }
27546 run_test 901 "don't leak a mgc lock on client umount"
27547
27548 # LU-13377
27549 test_902() {
27550         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
27551                 skip "client does not have LU-13377 fix"
27552         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
27553         $LCTL set_param fail_loc=0x1415
27554         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
27555         cancel_lru_locks osc
27556         rm -f $DIR/$tfile
27557 }
27558 run_test 902 "test short write doesn't hang lustre"
27559
27560 # LU-14711
27561 test_903() {
27562         $LFS setstripe -i 0 -c 1 $DIR/$tfile $DIR/${tfile}-2
27563         echo "blah" > $DIR/${tfile}-2
27564         dd if=/dev/zero of=$DIR/$tfile bs=1M count=6 conv=fsync
27565         #define OBD_FAIL_OSC_SLOW_PAGE_EVICT 0x417
27566         $LCTL set_param fail_loc=0x417 fail_val=20
27567
27568         mv $DIR/${tfile}-2 $DIR/$tfile # Destroys the big object
27569         sleep 1 # To start the destroy
27570         wait_destroy_complete 150 || error "Destroy taking too long"
27571         cat $DIR/$tfile > /dev/null || error "Evicted"
27572 }
27573 run_test 903 "Test long page discard does not cause evictions"
27574
27575 complete $SECONDS
27576 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
27577 check_and_cleanup_lustre
27578 if [ "$I_MOUNTED" != "yes" ]; then
27579         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
27580 fi
27581 exit_status