Whamcloud - gitweb
LU-14992 tests: sanity/replay-vbr mkdir on MDT0
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13
14 OSC=${OSC:-"osc"}
15
16 CC=${CC:-cc}
17 CREATETEST=${CREATETEST:-createtest}
18 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
19 OPENFILE=${OPENFILE:-openfile}
20 OPENUNLINK=${OPENUNLINK:-openunlink}
21 READS=${READS:-"reads"}
22 MUNLINK=${MUNLINK:-munlink}
23 SOCKETSERVER=${SOCKETSERVER:-socketserver}
24 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
25 MEMHOG=${MEMHOG:-memhog}
26 DIRECTIO=${DIRECTIO:-directio}
27 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
28 DEF_STRIPE_COUNT=-1
29 CHECK_GRANT=${CHECK_GRANT:-"yes"}
30 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
31
32 TRACE=${TRACE:-""}
33 LUSTRE=${LUSTRE:-$(dirname $0)/..}
34 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
35 . $LUSTRE/tests/test-framework.sh
36 init_test_env "$@"
37
38 init_logging
39
40 ALWAYS_EXCEPT="$SANITY_EXCEPT "
41 always_except LU-9693  42a 42c
42 always_except LU-6493  42b
43 always_except LU-14541 277
44 always_except LU-9054  312
45 always_except LU-8411  407
46
47 if $SHARED_KEY; then
48         always_except LU-14181 64e 64f
49 fi
50
51 # skip the grant tests for ARM until they are fixed
52 if [[ $(uname -m) = aarch64 ]]; then
53         always_except LU-11671 45
54 fi
55
56 # skip nfs tests on kernels >= 4.12.0 until they are fixed
57 if [ $LINUX_VERSION_CODE -ge $(version_code 4.12.0) ]; then
58         always_except LU-12661 817
59 fi
60 # skip cgroup tests on RHEL8.1 kernels until they are fixed
61 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
62       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
63         always_except LU-13063 411
64 fi
65
66 #                                  5              12     8   12  15   (min)"
67 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 60i 64b 68 71 115 135 136 230d 300o"
68
69 if [ "$mds1_FSTYPE" = "zfs" ]; then
70         #                                               13    (min)"
71         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
72 fi
73
74 if [ "$ost1_FSTYPE" = "zfs" ]; then
75         always_except LU-1941 130b 130c 130d 130e 130f 130g
76 fi
77
78 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
79
80 # Get the SLES distro version
81 #
82 # Returns a version string that should only be used in comparing
83 # strings returned by version_code()
84 sles_version_code()
85 {
86         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
87
88         # All SuSE Linux versions have one decimal. version_code expects two
89         local sles_version=$version.0
90         version_code $sles_version
91 }
92
93 # Check if we are running on Ubuntu or SLES so we can make decisions on
94 # what tests to run
95 if [ -r /etc/SuSE-release ]; then
96         sles_version=$(sles_version_code)
97         [ $sles_version -lt $(version_code 11.4.0) ] &&
98                 always_except LU-4341 170
99
100         [ $sles_version -lt $(version_code 12.0.0) ] &&
101                 always_except LU-3703 234
102 elif [ -r /etc/os-release ]; then
103         if grep -qi ubuntu /etc/os-release; then
104                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
105                                                 -e 's/^VERSION=//p' \
106                                                 /etc/os-release |
107                                                 awk '{ print $1 }'))
108
109                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
110                         always_except LU-10366 410
111                 fi
112         fi
113 fi
114
115 build_test_filter
116 FAIL_ON_ERROR=false
117
118 cleanup() {
119         echo -n "cln.."
120         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
121         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
122 }
123 setup() {
124         echo -n "mnt.."
125         load_modules
126         setupall || exit 10
127         echo "done"
128 }
129
130 check_swap_layouts_support()
131 {
132         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
133                 skip "Does not support layout lock."
134 }
135
136 check_swap_layout_no_dom()
137 {
138         local FOLDER=$1
139         local SUPP=$(lfs getstripe $FOLDER | grep "pattern:       mdt" | wc -l)
140         [ $SUPP -eq 0 ] || skip "layout swap does not support DOM files so far"
141 }
142
143 check_and_setup_lustre
144 DIR=${DIR:-$MOUNT}
145 assert_DIR
146
147 MAXFREE=${MAXFREE:-$((300000 * $OSTCOUNT))}
148
149 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
150 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
151 rm -rf $DIR/[Rdfs][0-9]*
152
153 # $RUNAS_ID may get set incorrectly somewhere else
154 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
155         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
156
157 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
158
159 if [ "${ONLY}" = "MOUNT" ] ; then
160         echo "Lustre is up, please go on"
161         exit
162 fi
163
164 echo "preparing for tests involving mounts"
165 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
166 touch $EXT2_DEV
167 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
168 echo # add a newline after mke2fs.
169
170 umask 077
171
172 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
173 lctl set_param debug=-1 2> /dev/null || true
174 test_0a() {
175         touch $DIR/$tfile
176         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
177         rm $DIR/$tfile
178         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
179 }
180 run_test 0a "touch; rm ====================="
181
182 test_0b() {
183         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
184         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
185 }
186 run_test 0b "chmod 0755 $DIR ============================="
187
188 test_0c() {
189         $LCTL get_param mdc.*.import | grep "state: FULL" ||
190                 error "import not FULL"
191         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
192                 error "bad target"
193 }
194 run_test 0c "check import proc"
195
196 test_0d() { # LU-3397
197         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
198                 skip "proc exports not supported before 2.10.57"
199
200         local mgs_exp="mgs.MGS.exports"
201         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
202         local exp_client_nid
203         local exp_client_version
204         local exp_val
205         local imp_val
206         local temp_imp=$DIR/$tfile.import
207         local temp_exp=$DIR/$tfile.export
208
209         # save mgc import file to $temp_imp
210         $LCTL get_param mgc.*.import | tee $temp_imp
211         # Check if client uuid is found in MGS export
212         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
213                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
214                         $client_uuid ] &&
215                         break;
216         done
217         # save mgs export file to $temp_exp
218         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
219
220         # Compare the value of field "connect_flags"
221         imp_val=$(grep "connect_flags" $temp_imp)
222         exp_val=$(grep "connect_flags" $temp_exp)
223         [ "$exp_val" == "$imp_val" ] ||
224                 error "export flags '$exp_val' != import flags '$imp_val'"
225
226         # Compare client versions.  Only compare top-3 fields for compatibility
227         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
228         exp_val=$(version_code $(cut -d. -f1,2,3 <<<$exp_client_version))
229         imp_val=$(version_code $(lustre_build_version client | cut -d. -f1,2,3))
230         [ "$exp_val" == "$imp_val" ] ||
231                 error "exp version '$exp_client_version'($exp_val) != " \
232                         "'$(lustre_build_version client)'($imp_val)"
233 }
234 run_test 0d "check export proc ============================="
235
236 test_0e() { # LU-13417
237         (( $MDSCOUNT > 1 )) ||
238                 skip "We need at least 2 MDTs for this test"
239
240         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
241                 skip "Need server version at least 2.14.51"
242
243         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
244         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
245
246         [ $default_lmv_count -eq 1 ] ||
247                 error "$MOUNT default stripe count $default_lmv_count"
248
249         [ $default_lmv_index -eq -1 ] ||
250                 error "$MOUNT default stripe index $default_lmv_index"
251
252         mkdir $MOUNT/$tdir.1 || error "mkdir $MOUNT/$tdir.1 failed"
253         mkdir $MOUNT/$tdir.2 || error "mkdir $MOUNT/$tdir.2 failed"
254
255         local mdt_index1=$($LFS getdirstripe -i $MOUNT/$tdir.1)
256         local mdt_index2=$($LFS getdirstripe -i $MOUNT/$tdir.2)
257
258         [ $mdt_index1 -eq $mdt_index2 ] &&
259                 error "directories are on the same MDT $mdt_index1=$mdt_index2"
260
261         rmdir $MOUNT/$tdir.1 $MOUNT/$tdir.2
262 }
263 run_test 0e "Enable DNE MDT balancing for mkdir in the ROOT"
264
265 test_1() {
266         test_mkdir $DIR/$tdir
267         test_mkdir $DIR/$tdir/d2
268         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
269         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
270         rmdir $DIR/$tdir/d2
271         rmdir $DIR/$tdir
272         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
273 }
274 run_test 1 "mkdir; remkdir; rmdir"
275
276 test_2() {
277         test_mkdir $DIR/$tdir
278         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
279         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
280         rm -r $DIR/$tdir
281         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
282 }
283 run_test 2 "mkdir; touch; rmdir; check file"
284
285 test_3() {
286         test_mkdir $DIR/$tdir
287         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
288         touch $DIR/$tdir/$tfile
289         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
290         rm -r $DIR/$tdir
291         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
292 }
293 run_test 3 "mkdir; touch; rmdir; check dir"
294
295 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
296 test_4() {
297         test_mkdir -i 1 $DIR/$tdir
298
299         touch $DIR/$tdir/$tfile ||
300                 error "Create file under remote directory failed"
301
302         rmdir $DIR/$tdir &&
303                 error "Expect error removing in-use dir $DIR/$tdir"
304
305         test -d $DIR/$tdir || error "Remote directory disappeared"
306
307         rm -rf $DIR/$tdir || error "remove remote dir error"
308 }
309 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
310
311 test_5() {
312         test_mkdir $DIR/$tdir
313         test_mkdir $DIR/$tdir/d2
314         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
315         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
316         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
317 }
318 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
319
320 test_6a() {
321         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
322         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
323         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
324                 error "$tfile does not have perm 0666 or UID $UID"
325         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
326         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
327                 error "$tfile should be 0666 and owned by UID $UID"
328 }
329 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
330
331 test_6c() {
332         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
333
334         touch $DIR/$tfile
335         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
336         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
337                 error "$tfile should be owned by UID $RUNAS_ID"
338         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
339         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
340                 error "$tfile should be owned by UID $RUNAS_ID"
341 }
342 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
343
344 test_6e() {
345         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
346
347         touch $DIR/$tfile
348         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
349         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
350                 error "$tfile should be owned by GID $UID"
351         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
352         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
353                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
354 }
355 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
356
357 test_6g() {
358         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
359
360         test_mkdir $DIR/$tdir
361         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
362         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
363         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
364         test_mkdir $DIR/$tdir/d/subdir
365         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
366                 error "$tdir/d/subdir should be GID $RUNAS_GID"
367         if [[ $MDSCOUNT -gt 1 ]]; then
368                 # check remote dir sgid inherite
369                 $LFS mkdir -i 0 $DIR/$tdir.local ||
370                         error "mkdir $tdir.local failed"
371                 chmod g+s $DIR/$tdir.local ||
372                         error "chmod $tdir.local failed"
373                 chgrp $RUNAS_GID $DIR/$tdir.local ||
374                         error "chgrp $tdir.local failed"
375                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
376                         error "mkdir $tdir.remote failed"
377                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
378                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
379                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
380                         error "$tdir.remote should be mode 02755"
381         fi
382 }
383 run_test 6g "verify new dir in sgid dir inherits group"
384
385 test_6h() { # bug 7331
386         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
387
388         touch $DIR/$tfile || error "touch failed"
389         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
390         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
391                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
392         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
393                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
394 }
395 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
396
397 test_7a() {
398         test_mkdir $DIR/$tdir
399         $MCREATE $DIR/$tdir/$tfile
400         chmod 0666 $DIR/$tdir/$tfile
401         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
402                 error "$tdir/$tfile should be mode 0666"
403 }
404 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
405
406 test_7b() {
407         if [ ! -d $DIR/$tdir ]; then
408                 test_mkdir $DIR/$tdir
409         fi
410         $MCREATE $DIR/$tdir/$tfile
411         echo -n foo > $DIR/$tdir/$tfile
412         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
413         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
414 }
415 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
416
417 test_8() {
418         test_mkdir $DIR/$tdir
419         touch $DIR/$tdir/$tfile
420         chmod 0666 $DIR/$tdir/$tfile
421         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
422                 error "$tfile mode not 0666"
423 }
424 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
425
426 test_9() {
427         test_mkdir $DIR/$tdir
428         test_mkdir $DIR/$tdir/d2
429         test_mkdir $DIR/$tdir/d2/d3
430         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
431 }
432 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
433
434 test_10() {
435         test_mkdir $DIR/$tdir
436         test_mkdir $DIR/$tdir/d2
437         touch $DIR/$tdir/d2/$tfile
438         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
439                 error "$tdir/d2/$tfile not a file"
440 }
441 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
442
443 test_11() {
444         test_mkdir $DIR/$tdir
445         test_mkdir $DIR/$tdir/d2
446         chmod 0666 $DIR/$tdir/d2
447         chmod 0705 $DIR/$tdir/d2
448         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
449                 error "$tdir/d2 mode not 0705"
450 }
451 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
452
453 test_12() {
454         test_mkdir $DIR/$tdir
455         touch $DIR/$tdir/$tfile
456         chmod 0666 $DIR/$tdir/$tfile
457         chmod 0654 $DIR/$tdir/$tfile
458         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
459                 error "$tdir/d2 mode not 0654"
460 }
461 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
462
463 test_13() {
464         test_mkdir $DIR/$tdir
465         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
466         >  $DIR/$tdir/$tfile
467         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
468                 error "$tdir/$tfile size not 0 after truncate"
469 }
470 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
471
472 test_14() {
473         test_mkdir $DIR/$tdir
474         touch $DIR/$tdir/$tfile
475         rm $DIR/$tdir/$tfile
476         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
477 }
478 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
479
480 test_15() {
481         test_mkdir $DIR/$tdir
482         touch $DIR/$tdir/$tfile
483         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
484         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
485                 error "$tdir/${tfile_2} not a file after rename"
486         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
487 }
488 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
489
490 test_16() {
491         test_mkdir $DIR/$tdir
492         touch $DIR/$tdir/$tfile
493         rm -rf $DIR/$tdir/$tfile
494         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
495 }
496 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
497
498 test_17a() {
499         test_mkdir $DIR/$tdir
500         touch $DIR/$tdir/$tfile
501         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
502         ls -l $DIR/$tdir
503         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
504                 error "$tdir/l-exist not a symlink"
505         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
506                 error "$tdir/l-exist not referencing a file"
507         rm -f $DIR/$tdir/l-exist
508         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
509 }
510 run_test 17a "symlinks: create, remove (real)"
511
512 test_17b() {
513         test_mkdir $DIR/$tdir
514         ln -s no-such-file $DIR/$tdir/l-dangle
515         ls -l $DIR/$tdir
516         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
517                 error "$tdir/l-dangle not referencing no-such-file"
518         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
519                 error "$tdir/l-dangle not referencing non-existent file"
520         rm -f $DIR/$tdir/l-dangle
521         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
522 }
523 run_test 17b "symlinks: create, remove (dangling)"
524
525 test_17c() { # bug 3440 - don't save failed open RPC for replay
526         test_mkdir $DIR/$tdir
527         ln -s foo $DIR/$tdir/$tfile
528         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
529 }
530 run_test 17c "symlinks: open dangling (should return error)"
531
532 test_17d() {
533         test_mkdir $DIR/$tdir
534         ln -s foo $DIR/$tdir/$tfile
535         touch $DIR/$tdir/$tfile || error "creating to new symlink"
536 }
537 run_test 17d "symlinks: create dangling"
538
539 test_17e() {
540         test_mkdir $DIR/$tdir
541         local foo=$DIR/$tdir/$tfile
542         ln -s $foo $foo || error "create symlink failed"
543         ls -l $foo || error "ls -l failed"
544         ls $foo && error "ls not failed" || true
545 }
546 run_test 17e "symlinks: create recursive symlink (should return error)"
547
548 test_17f() {
549         test_mkdir $DIR/$tdir
550         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
551         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
552         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
553         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
554         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
555         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
556         ls -l  $DIR/$tdir
557 }
558 run_test 17f "symlinks: long and very long symlink name"
559
560 # str_repeat(S, N) generate a string that is string S repeated N times
561 str_repeat() {
562         local s=$1
563         local n=$2
564         local ret=''
565         while [ $((n -= 1)) -ge 0 ]; do
566                 ret=$ret$s
567         done
568         echo $ret
569 }
570
571 # Long symlinks and LU-2241
572 test_17g() {
573         test_mkdir $DIR/$tdir
574         local TESTS="59 60 61 4094 4095"
575
576         # Fix for inode size boundary in 2.1.4
577         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
578                 TESTS="4094 4095"
579
580         # Patch not applied to 2.2 or 2.3 branches
581         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
582         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
583                 TESTS="4094 4095"
584
585         for i in $TESTS; do
586                 local SYMNAME=$(str_repeat 'x' $i)
587                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
588                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
589         done
590 }
591 run_test 17g "symlinks: really long symlink name and inode boundaries"
592
593 test_17h() { #bug 17378
594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
595         remote_mds_nodsh && skip "remote MDS with nodsh"
596
597         local mdt_idx
598
599         test_mkdir $DIR/$tdir
600         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
601         $LFS setstripe -c -1 $DIR/$tdir
602         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
603         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
604         touch $DIR/$tdir/$tfile || true
605 }
606 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
607
608 test_17i() { #bug 20018
609         [ $PARALLEL == "yes" ] && skip "skip parallel run"
610         remote_mds_nodsh && skip "remote MDS with nodsh"
611
612         local foo=$DIR/$tdir/$tfile
613         local mdt_idx
614
615         test_mkdir -c1 $DIR/$tdir
616         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
617         ln -s $foo $foo || error "create symlink failed"
618 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
619         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
620         ls -l $foo && error "error not detected"
621         return 0
622 }
623 run_test 17i "don't panic on short symlink (should return error)"
624
625 test_17k() { #bug 22301
626         [ $PARALLEL == "yes" ] && skip "skip parallel run"
627         [[ -z "$(which rsync 2>/dev/null)" ]] &&
628                 skip "no rsync command"
629         rsync --help | grep -q xattr ||
630                 skip_env "$(rsync --version | head -n1) does not support xattrs"
631         test_mkdir $DIR/$tdir
632         test_mkdir $DIR/$tdir.new
633         touch $DIR/$tdir/$tfile
634         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
635         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
636                 error "rsync failed with xattrs enabled"
637 }
638 run_test 17k "symlinks: rsync with xattrs enabled"
639
640 test_17l() { # LU-279
641         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
642                 skip "no getfattr command"
643
644         test_mkdir $DIR/$tdir
645         touch $DIR/$tdir/$tfile
646         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
647         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
648                 # -h to not follow symlinks. -m '' to list all the xattrs.
649                 # grep to remove first line: '# file: $path'.
650                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
651                 do
652                         lgetxattr_size_check $path $xattr ||
653                                 error "lgetxattr_size_check $path $xattr failed"
654                 done
655         done
656 }
657 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
658
659 # LU-1540
660 test_17m() {
661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
662         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
663         remote_mds_nodsh && skip "remote MDS with nodsh"
664         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
665         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
666                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
667
668         local short_sym="0123456789"
669         local wdir=$DIR/$tdir
670         local i
671
672         test_mkdir $wdir
673         long_sym=$short_sym
674         # create a long symlink file
675         for ((i = 0; i < 4; ++i)); do
676                 long_sym=${long_sym}${long_sym}
677         done
678
679         echo "create 512 short and long symlink files under $wdir"
680         for ((i = 0; i < 256; ++i)); do
681                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
682                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
683         done
684
685         echo "erase them"
686         rm -f $wdir/*
687         sync
688         wait_delete_completed
689
690         echo "recreate the 512 symlink files with a shorter string"
691         for ((i = 0; i < 512; ++i)); do
692                 # rewrite the symlink file with a shorter string
693                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
694                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
695         done
696
697         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
698
699         echo "stop and checking mds${mds_index}:"
700         # e2fsck should not return error
701         stop mds${mds_index}
702         local devname=$(mdsdevname $mds_index)
703         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
704         rc=$?
705
706         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
707                 error "start mds${mds_index} failed"
708         df $MOUNT > /dev/null 2>&1
709         [ $rc -eq 0 ] ||
710                 error "e2fsck detected error for short/long symlink: rc=$rc"
711         rm -f $wdir/*
712 }
713 run_test 17m "run e2fsck against MDT which contains short/long symlink"
714
715 check_fs_consistency_17n() {
716         local mdt_index
717         local rc=0
718
719         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
720         # so it only check MDT1/MDT2 instead of all of MDTs.
721         for mdt_index in 1 2; do
722                 # e2fsck should not return error
723                 stop mds${mdt_index}
724                 local devname=$(mdsdevname $mdt_index)
725                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
726                         rc=$((rc + $?))
727
728                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
729                         error "mount mds$mdt_index failed"
730                 df $MOUNT > /dev/null 2>&1
731         done
732         return $rc
733 }
734
735 test_17n() {
736         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
737         [ $PARALLEL == "yes" ] && skip "skip parallel run"
738         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
739         remote_mds_nodsh && skip "remote MDS with nodsh"
740         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
741         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
742                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
743
744         local i
745
746         test_mkdir $DIR/$tdir
747         for ((i=0; i<10; i++)); do
748                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
749                         error "create remote dir error $i"
750                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
751                         error "create files under remote dir failed $i"
752         done
753
754         check_fs_consistency_17n ||
755                 error "e2fsck report error after create files under remote dir"
756
757         for ((i = 0; i < 10; i++)); do
758                 rm -rf $DIR/$tdir/remote_dir_${i} ||
759                         error "destroy remote dir error $i"
760         done
761
762         check_fs_consistency_17n ||
763                 error "e2fsck report error after unlink files under remote dir"
764
765         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
766                 skip "lustre < 2.4.50 does not support migrate mv"
767
768         for ((i = 0; i < 10; i++)); do
769                 mkdir -p $DIR/$tdir/remote_dir_${i}
770                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
771                         error "create files under remote dir failed $i"
772                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
773                         error "migrate remote dir error $i"
774         done
775         check_fs_consistency_17n || error "e2fsck report error after migration"
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 || error "e2fsck report error after unlink"
783 }
784 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
785
786 test_17o() {
787         remote_mds_nodsh && skip "remote MDS with nodsh"
788         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
789                 skip "Need MDS version at least 2.3.64"
790
791         local wdir=$DIR/${tdir}o
792         local mdt_index
793         local rc=0
794
795         test_mkdir $wdir
796         touch $wdir/$tfile
797         mdt_index=$($LFS getstripe -m $wdir/$tfile)
798         mdt_index=$((mdt_index + 1))
799
800         cancel_lru_locks mdc
801         #fail mds will wait the failover finish then set
802         #following fail_loc to avoid interfer the recovery process.
803         fail mds${mdt_index}
804
805         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
806         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
807         ls -l $wdir/$tfile && rc=1
808         do_facet mds${mdt_index} lctl set_param fail_loc=0
809         [[ $rc -eq 0 ]] || error "stat file should fail"
810 }
811 run_test 17o "stat file with incompat LMA feature"
812
813 test_18() {
814         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
815         ls $DIR || error "Failed to ls $DIR: $?"
816 }
817 run_test 18 "touch .../f ; ls ... =============================="
818
819 test_19a() {
820         touch $DIR/$tfile
821         ls -l $DIR
822         rm $DIR/$tfile
823         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
824 }
825 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
826
827 test_19b() {
828         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
829 }
830 run_test 19b "ls -l .../f19 (should return error) =============="
831
832 test_19c() {
833         [ $RUNAS_ID -eq $UID ] &&
834                 skip_env "RUNAS_ID = UID = $UID -- skipping"
835
836         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
837 }
838 run_test 19c "$RUNAS touch .../f19 (should return error) =="
839
840 test_19d() {
841         cat $DIR/f19 && error || true
842 }
843 run_test 19d "cat .../f19 (should return error) =============="
844
845 test_20() {
846         touch $DIR/$tfile
847         rm $DIR/$tfile
848         touch $DIR/$tfile
849         rm $DIR/$tfile
850         touch $DIR/$tfile
851         rm $DIR/$tfile
852         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
853 }
854 run_test 20 "touch .../f ; ls -l ..."
855
856 test_21() {
857         test_mkdir $DIR/$tdir
858         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
859         ln -s dangle $DIR/$tdir/link
860         echo foo >> $DIR/$tdir/link
861         cat $DIR/$tdir/dangle
862         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
863         $CHECKSTAT -f -t file $DIR/$tdir/link ||
864                 error "$tdir/link not linked to a file"
865 }
866 run_test 21 "write to dangling link"
867
868 test_22() {
869         local wdir=$DIR/$tdir
870         test_mkdir $wdir
871         chown $RUNAS_ID:$RUNAS_GID $wdir
872         (cd $wdir || error "cd $wdir failed";
873                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
874                 $RUNAS tar xf -)
875         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
876         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
877         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
878                 error "checkstat -u failed"
879 }
880 run_test 22 "unpack tar archive as non-root user"
881
882 # was test_23
883 test_23a() {
884         test_mkdir $DIR/$tdir
885         local file=$DIR/$tdir/$tfile
886
887         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
888         openfile -f O_CREAT:O_EXCL $file &&
889                 error "$file recreate succeeded" || true
890 }
891 run_test 23a "O_CREAT|O_EXCL in subdir"
892
893 test_23b() { # bug 18988
894         test_mkdir $DIR/$tdir
895         local file=$DIR/$tdir/$tfile
896
897         rm -f $file
898         echo foo > $file || error "write filed"
899         echo bar >> $file || error "append filed"
900         $CHECKSTAT -s 8 $file || error "wrong size"
901         rm $file
902 }
903 run_test 23b "O_APPEND check"
904
905 # LU-9409, size with O_APPEND and tiny writes
906 test_23c() {
907         local file=$DIR/$tfile
908
909         # single dd
910         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
911         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
912         rm -f $file
913
914         # racing tiny writes
915         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
916         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
917         wait
918         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
919         rm -f $file
920
921         #racing tiny & normal writes
922         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
923         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
924         wait
925         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
926         rm -f $file
927
928         #racing tiny & normal writes 2, ugly numbers
929         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
930         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
931         wait
932         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
933         rm -f $file
934 }
935 run_test 23c "O_APPEND size checks for tiny writes"
936
937 # LU-11069 file offset is correct after appending writes
938 test_23d() {
939         local file=$DIR/$tfile
940         local offset
941
942         echo CentaurHauls > $file
943         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
944         if ((offset != 26)); then
945                 error "wrong offset, expected 26, got '$offset'"
946         fi
947 }
948 run_test 23d "file offset is correct after appending writes"
949
950 # rename sanity
951 test_24a() {
952         echo '-- same directory rename'
953         test_mkdir $DIR/$tdir
954         touch $DIR/$tdir/$tfile.1
955         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
956         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
957 }
958 run_test 24a "rename file to non-existent target"
959
960 test_24b() {
961         test_mkdir $DIR/$tdir
962         touch $DIR/$tdir/$tfile.{1,2}
963         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
964         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
965         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
966 }
967 run_test 24b "rename file to existing target"
968
969 test_24c() {
970         test_mkdir $DIR/$tdir
971         test_mkdir $DIR/$tdir/d$testnum.1
972         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
973         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
974         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
975 }
976 run_test 24c "rename directory to non-existent target"
977
978 test_24d() {
979         test_mkdir -c1 $DIR/$tdir
980         test_mkdir -c1 $DIR/$tdir/d$testnum.1
981         test_mkdir -c1 $DIR/$tdir/d$testnum.2
982         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
983         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
984         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
985 }
986 run_test 24d "rename directory to existing target"
987
988 test_24e() {
989         echo '-- cross directory renames --'
990         test_mkdir $DIR/R5a
991         test_mkdir $DIR/R5b
992         touch $DIR/R5a/f
993         mv $DIR/R5a/f $DIR/R5b/g
994         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
995         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
996 }
997 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
998
999 test_24f() {
1000         test_mkdir $DIR/R6a
1001         test_mkdir $DIR/R6b
1002         touch $DIR/R6a/f $DIR/R6b/g
1003         mv $DIR/R6a/f $DIR/R6b/g
1004         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
1005         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
1006 }
1007 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
1008
1009 test_24g() {
1010         test_mkdir $DIR/R7a
1011         test_mkdir $DIR/R7b
1012         test_mkdir $DIR/R7a/d
1013         mv $DIR/R7a/d $DIR/R7b/e
1014         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
1015         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
1016 }
1017 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
1018
1019 test_24h() {
1020         test_mkdir -c1 $DIR/R8a
1021         test_mkdir -c1 $DIR/R8b
1022         test_mkdir -c1 $DIR/R8a/d
1023         test_mkdir -c1 $DIR/R8b/e
1024         mrename $DIR/R8a/d $DIR/R8b/e
1025         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1026         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1027 }
1028 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1029
1030 test_24i() {
1031         echo "-- rename error cases"
1032         test_mkdir $DIR/R9
1033         test_mkdir $DIR/R9/a
1034         touch $DIR/R9/f
1035         mrename $DIR/R9/f $DIR/R9/a
1036         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1037         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1038         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1039 }
1040 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1041
1042 test_24j() {
1043         test_mkdir $DIR/R10
1044         mrename $DIR/R10/f $DIR/R10/g
1045         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1046         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1047         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1048 }
1049 run_test 24j "source does not exist ============================"
1050
1051 test_24k() {
1052         test_mkdir $DIR/R11a
1053         test_mkdir $DIR/R11a/d
1054         touch $DIR/R11a/f
1055         mv $DIR/R11a/f $DIR/R11a/d
1056         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1057         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1058 }
1059 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1060
1061 # bug 2429 - rename foo foo foo creates invalid file
1062 test_24l() {
1063         f="$DIR/f24l"
1064         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1065 }
1066 run_test 24l "Renaming a file to itself ========================"
1067
1068 test_24m() {
1069         f="$DIR/f24m"
1070         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1071         # on ext3 this does not remove either the source or target files
1072         # though the "expected" operation would be to remove the source
1073         $CHECKSTAT -t file ${f} || error "${f} missing"
1074         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1075 }
1076 run_test 24m "Renaming a file to a hard link to itself ========="
1077
1078 test_24n() {
1079     f="$DIR/f24n"
1080     # this stats the old file after it was renamed, so it should fail
1081     touch ${f}
1082     $CHECKSTAT ${f} || error "${f} missing"
1083     mv ${f} ${f}.rename
1084     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1085     $CHECKSTAT -a ${f} || error "${f} exists"
1086 }
1087 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1088
1089 test_24o() {
1090         test_mkdir $DIR/$tdir
1091         rename_many -s random -v -n 10 $DIR/$tdir
1092 }
1093 run_test 24o "rename of files during htree split"
1094
1095 test_24p() {
1096         test_mkdir $DIR/R12a
1097         test_mkdir $DIR/R12b
1098         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1099         mrename $DIR/R12a $DIR/R12b
1100         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1101         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1102         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1103         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1104 }
1105 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1106
1107 cleanup_multiop_pause() {
1108         trap 0
1109         kill -USR1 $MULTIPID
1110 }
1111
1112 test_24q() {
1113         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1114
1115         test_mkdir $DIR/R13a
1116         test_mkdir $DIR/R13b
1117         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1118         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1119         MULTIPID=$!
1120
1121         trap cleanup_multiop_pause EXIT
1122         mrename $DIR/R13a $DIR/R13b
1123         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1124         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1125         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1126         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1127         cleanup_multiop_pause
1128         wait $MULTIPID || error "multiop close failed"
1129 }
1130 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1131
1132 test_24r() { #bug 3789
1133         test_mkdir $DIR/R14a
1134         test_mkdir $DIR/R14a/b
1135         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1136         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1137         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1138 }
1139 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1140
1141 test_24s() {
1142         test_mkdir $DIR/R15a
1143         test_mkdir $DIR/R15a/b
1144         test_mkdir $DIR/R15a/b/c
1145         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1146         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1147         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1148 }
1149 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1150 test_24t() {
1151         test_mkdir $DIR/R16a
1152         test_mkdir $DIR/R16a/b
1153         test_mkdir $DIR/R16a/b/c
1154         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1155         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1156         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1157 }
1158 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1159
1160 test_24u() { # bug12192
1161         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1162         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1163 }
1164 run_test 24u "create stripe file"
1165
1166 simple_cleanup_common() {
1167         local createmany=$1
1168         local rc=0
1169
1170         [[ -z "$DIR" || -z "$tdir" || ! -d "$DIR/$tdir" ]] && return 0
1171
1172         local start=$SECONDS
1173
1174         [[ -n "$createmany" ]] && unlinkmany $DIR/$tdir/$tfile $createmany
1175         rm -rf $DIR/$tdir || error "cleanup $DIR/$tdir failed"
1176         rc=$?
1177         wait_delete_completed
1178         echo "cleanup time $((SECONDS - start))"
1179         return $rc
1180 }
1181
1182 max_pages_per_rpc() {
1183         local mdtname="$(printf "MDT%04x" ${1:-0})"
1184         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1185 }
1186
1187 test_24v() {
1188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1189
1190         local nrfiles=${COUNT:-100000}
1191         local fname="$DIR/$tdir/$tfile"
1192
1193         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1194         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1195
1196         test_mkdir "$(dirname $fname)"
1197         # assume MDT0000 has the fewest inodes
1198         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1199         local free_inodes=$(($(mdt_free_inodes 0) * ${stripes/#0/1}))
1200         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1201
1202         stack_trap "simple_cleanup_common $nrfiles"
1203
1204         createmany -m "$fname" $nrfiles
1205
1206         cancel_lru_locks mdc
1207         lctl set_param mdc.*.stats clear
1208
1209         # was previously test_24D: LU-6101
1210         # readdir() returns correct number of entries after cursor reload
1211         local num_ls=$(ls $DIR/$tdir | wc -l)
1212         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1213         local num_all=$(ls -a $DIR/$tdir | wc -l)
1214         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1215                 [ $num_all -ne $((nrfiles + 2)) ]; then
1216                         error "Expected $nrfiles files, got $num_ls " \
1217                                 "($num_uniq unique $num_all .&..)"
1218         fi
1219         # LU-5 large readdir
1220         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1221         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1222         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1223         # take into account of overhead in lu_dirpage header and end mark in
1224         # each page, plus one in rpc_num calculation.
1225         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1226         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1227         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1228         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1229         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1230         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1231         echo "readpages: $mds_readpage rpc_max: $rpc_max-2/+1"
1232         (( $mds_readpage >= $rpc_max - 2 && $mds_readpage <= $rpc_max + 1)) ||
1233                 error "large readdir doesn't take effect: " \
1234                       "$mds_readpage should be about $rpc_max"
1235 }
1236 run_test 24v "list large directory (test hash collision, b=17560)"
1237
1238 test_24w() { # bug21506
1239         SZ1=234852
1240         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1241         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1242         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1243         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1244         [[ "$SZ1" -eq "$SZ2" ]] ||
1245                 error "Error reading at the end of the file $tfile"
1246 }
1247 run_test 24w "Reading a file larger than 4Gb"
1248
1249 test_24x() {
1250         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1252         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1253                 skip "Need MDS version at least 2.7.56"
1254
1255         local MDTIDX=1
1256         local remote_dir=$DIR/$tdir/remote_dir
1257
1258         test_mkdir $DIR/$tdir
1259         $LFS mkdir -i $MDTIDX $remote_dir ||
1260                 error "create remote directory failed"
1261
1262         test_mkdir $DIR/$tdir/src_dir
1263         touch $DIR/$tdir/src_file
1264         test_mkdir $remote_dir/tgt_dir
1265         touch $remote_dir/tgt_file
1266
1267         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1268                 error "rename dir cross MDT failed!"
1269
1270         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1271                 error "rename file cross MDT failed!"
1272
1273         touch $DIR/$tdir/ln_file
1274         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1275                 error "ln file cross MDT failed"
1276
1277         rm -rf $DIR/$tdir || error "Can not delete directories"
1278 }
1279 run_test 24x "cross MDT rename/link"
1280
1281 test_24y() {
1282         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1283         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1284
1285         local remote_dir=$DIR/$tdir/remote_dir
1286         local mdtidx=1
1287
1288         test_mkdir $DIR/$tdir
1289         $LFS mkdir -i $mdtidx $remote_dir ||
1290                 error "create remote directory failed"
1291
1292         test_mkdir $remote_dir/src_dir
1293         touch $remote_dir/src_file
1294         test_mkdir $remote_dir/tgt_dir
1295         touch $remote_dir/tgt_file
1296
1297         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1298                 error "rename subdir in the same remote dir failed!"
1299
1300         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1301                 error "rename files in the same remote dir failed!"
1302
1303         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1304                 error "link files in the same remote dir failed!"
1305
1306         rm -rf $DIR/$tdir || error "Can not delete directories"
1307 }
1308 run_test 24y "rename/link on the same dir should succeed"
1309
1310 test_24z() {
1311         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1312         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1313                 skip "Need MDS version at least 2.12.51"
1314
1315         local index
1316
1317         for index in 0 1; do
1318                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1319                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1320         done
1321
1322         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1323
1324         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1325         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1326
1327         local mdts=$(comma_list $(mdts_nodes))
1328
1329         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1330         stack_trap "do_nodes $mdts $LCTL \
1331                 set_param mdt.*.enable_remote_rename=1" EXIT
1332
1333         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1334
1335         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1336         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1337 }
1338 run_test 24z "cross-MDT rename is done as cp"
1339
1340 test_24A() { # LU-3182
1341         local NFILES=5000
1342
1343         test_mkdir $DIR/$tdir
1344         stack_trap "simple_cleanup_common $NFILES"
1345         createmany -m $DIR/$tdir/$tfile $NFILES
1346         local t=$(ls $DIR/$tdir | wc -l)
1347         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1348         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1349
1350         (( $t == $NFILES && $u == $NFILES && $v == NFILES + 2 )) ||
1351                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1352 }
1353 run_test 24A "readdir() returns correct number of entries."
1354
1355 test_24B() { # LU-4805
1356         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1357
1358         local count
1359
1360         test_mkdir $DIR/$tdir
1361         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1362                 error "create striped dir failed"
1363
1364         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1365         [ $count -eq 2 ] || error "Expected 2, got $count"
1366
1367         touch $DIR/$tdir/striped_dir/a
1368
1369         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1370         [ $count -eq 3 ] || error "Expected 3, got $count"
1371
1372         touch $DIR/$tdir/striped_dir/.f
1373
1374         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1375         [ $count -eq 4 ] || error "Expected 4, got $count"
1376
1377         rm -rf $DIR/$tdir || error "Can not delete directories"
1378 }
1379 run_test 24B "readdir for striped dir return correct number of entries"
1380
1381 test_24C() {
1382         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1383
1384         mkdir $DIR/$tdir
1385         mkdir $DIR/$tdir/d0
1386         mkdir $DIR/$tdir/d1
1387
1388         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1389                 error "create striped dir failed"
1390
1391         cd $DIR/$tdir/d0/striped_dir
1392
1393         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1394         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1395         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1396
1397         [ "$d0_ino" = "$parent_ino" ] ||
1398                 error ".. wrong, expect $d0_ino, get $parent_ino"
1399
1400         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1401                 error "mv striped dir failed"
1402
1403         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1404
1405         [ "$d1_ino" = "$parent_ino" ] ||
1406                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1407 }
1408 run_test 24C "check .. in striped dir"
1409
1410 test_24E() {
1411         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1413
1414         mkdir -p $DIR/$tdir
1415         mkdir $DIR/$tdir/src_dir
1416         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1417                 error "create remote source failed"
1418
1419         touch $DIR/$tdir/src_dir/src_child/a
1420
1421         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1422                 error "create remote target dir failed"
1423
1424         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1425                 error "create remote target child failed"
1426
1427         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1428                 error "rename dir cross MDT failed!"
1429
1430         find $DIR/$tdir
1431
1432         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1433                 error "src_child still exists after rename"
1434
1435         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1436                 error "missing file(a) after rename"
1437
1438         rm -rf $DIR/$tdir || error "Can not delete directories"
1439 }
1440 run_test 24E "cross MDT rename/link"
1441
1442 test_24F () {
1443         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1444
1445         local repeats=1000
1446         [ "$SLOW" = "no" ] && repeats=100
1447
1448         mkdir -p $DIR/$tdir
1449
1450         echo "$repeats repeats"
1451         for ((i = 0; i < repeats; i++)); do
1452                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1453                 touch $DIR/$tdir/test/a || error "touch fails"
1454                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1455                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1456         done
1457
1458         true
1459 }
1460 run_test 24F "hash order vs readdir (LU-11330)"
1461
1462 test_24G () {
1463         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1464
1465         local ino1
1466         local ino2
1467
1468         $LFS mkdir -i 0 $DIR/$tdir-0 || error "mkdir $tdir-0"
1469         $LFS mkdir -i 1 $DIR/$tdir-1 || error "mkdir $tdir-1"
1470         touch $DIR/$tdir-0/f1 || error "touch f1"
1471         ln -s $DIR/$tdir-0/f1 $DIR/$tdir-0/s1 || error "ln s1"
1472         ino1=$(stat -c%i $DIR/$tdir-0/s1)
1473         mv $DIR/$tdir-0/s1 $DIR/$tdir-1 || error "mv s1"
1474         ino2=$(stat -c%i $DIR/$tdir-1/s1)
1475         [ $ino1 -ne $ino2 ] || error "s1 should be migrated"
1476 }
1477 run_test 24G "migrate symlink in rename"
1478
1479 test_24H() {
1480         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
1481         [[ $(hostname) != $(facet_active_host mds2) ]] ||
1482                 skip "MDT1 should be on another node"
1483
1484         test_mkdir -i 1 -c 1 $DIR/$tdir
1485 #define OBD_FAIL_FLD_QUERY_REQ           0x1103
1486         do_facet mds2 $LCTL set_param fail_loc=0x80001103
1487         touch $DIR/$tdir/$tfile || error "touch failed"
1488 }
1489 run_test 24H "repeat FLD_QUERY rpc"
1490
1491 test_25a() {
1492         echo '== symlink sanity ============================================='
1493
1494         test_mkdir $DIR/d25
1495         ln -s d25 $DIR/s25
1496         touch $DIR/s25/foo ||
1497                 error "File creation in symlinked directory failed"
1498 }
1499 run_test 25a "create file in symlinked directory ==============="
1500
1501 test_25b() {
1502         [ ! -d $DIR/d25 ] && test_25a
1503         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1504 }
1505 run_test 25b "lookup file in symlinked directory ==============="
1506
1507 test_26a() {
1508         test_mkdir $DIR/d26
1509         test_mkdir $DIR/d26/d26-2
1510         ln -s d26/d26-2 $DIR/s26
1511         touch $DIR/s26/foo || error "File creation failed"
1512 }
1513 run_test 26a "multiple component symlink ======================="
1514
1515 test_26b() {
1516         test_mkdir -p $DIR/$tdir/d26-2
1517         ln -s $tdir/d26-2/foo $DIR/s26-2
1518         touch $DIR/s26-2 || error "File creation failed"
1519 }
1520 run_test 26b "multiple component symlink at end of lookup ======"
1521
1522 test_26c() {
1523         test_mkdir $DIR/d26.2
1524         touch $DIR/d26.2/foo
1525         ln -s d26.2 $DIR/s26.2-1
1526         ln -s s26.2-1 $DIR/s26.2-2
1527         ln -s s26.2-2 $DIR/s26.2-3
1528         chmod 0666 $DIR/s26.2-3/foo
1529 }
1530 run_test 26c "chain of symlinks"
1531
1532 # recursive symlinks (bug 439)
1533 test_26d() {
1534         ln -s d26-3/foo $DIR/d26-3
1535 }
1536 run_test 26d "create multiple component recursive symlink"
1537
1538 test_26e() {
1539         [ ! -h $DIR/d26-3 ] && test_26d
1540         rm $DIR/d26-3
1541 }
1542 run_test 26e "unlink multiple component recursive symlink"
1543
1544 # recursive symlinks (bug 7022)
1545 test_26f() {
1546         test_mkdir $DIR/$tdir
1547         test_mkdir $DIR/$tdir/$tfile
1548         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1549         test_mkdir -p lndir/bar1
1550         test_mkdir $DIR/$tdir/$tfile/$tfile
1551         cd $tfile                || error "cd $tfile failed"
1552         ln -s .. dotdot          || error "ln dotdot failed"
1553         ln -s dotdot/lndir lndir || error "ln lndir failed"
1554         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1555         output=`ls $tfile/$tfile/lndir/bar1`
1556         [ "$output" = bar1 ] && error "unexpected output"
1557         rm -r $tfile             || error "rm $tfile failed"
1558         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1559 }
1560 run_test 26f "rm -r of a directory which has recursive symlink"
1561
1562 test_27a() {
1563         test_mkdir $DIR/$tdir
1564         $LFS getstripe $DIR/$tdir
1565         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1566         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1567         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1568 }
1569 run_test 27a "one stripe file"
1570
1571 test_27b() {
1572         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1573
1574         test_mkdir $DIR/$tdir
1575         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1576         $LFS getstripe -c $DIR/$tdir/$tfile
1577         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1578                 error "two-stripe file doesn't have two stripes"
1579
1580         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1581 }
1582 run_test 27b "create and write to two stripe file"
1583
1584 # 27c family tests specific striping, setstripe -o
1585 test_27ca() {
1586         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1587         test_mkdir -p $DIR/$tdir
1588         local osts="1"
1589
1590         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1591         $LFS getstripe -i $DIR/$tdir/$tfile
1592         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1593                 error "stripe not on specified OST"
1594
1595         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1596 }
1597 run_test 27ca "one stripe on specified OST"
1598
1599 test_27cb() {
1600         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1601         test_mkdir -p $DIR/$tdir
1602         local osts="1,0"
1603         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1604         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1605         echo "$getstripe"
1606
1607         # Strip getstripe output to a space separated list of OSTs
1608         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1609                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1610         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1611                 error "stripes not on specified OSTs"
1612
1613         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1614 }
1615 run_test 27cb "two stripes on specified OSTs"
1616
1617 test_27cc() {
1618         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1619         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1620                 skip "server does not support overstriping"
1621
1622         test_mkdir -p $DIR/$tdir
1623         local osts="0,0"
1624         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1625         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1626         echo "$getstripe"
1627
1628         # Strip getstripe output to a space separated list of OSTs
1629         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1630                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1631         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1632                 error "stripes not on specified OSTs"
1633
1634         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1635 }
1636 run_test 27cc "two stripes on the same OST"
1637
1638 test_27cd() {
1639         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1640         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1641                 skip "server does not support overstriping"
1642         test_mkdir -p $DIR/$tdir
1643         local osts="0,1,1,0"
1644         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1645         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1646         echo "$getstripe"
1647
1648         # Strip getstripe output to a space separated list of OSTs
1649         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1650                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1651         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1652                 error "stripes not on specified OSTs"
1653
1654         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1655 }
1656 run_test 27cd "four stripes on two OSTs"
1657
1658 test_27ce() {
1659         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1660                 skip_env "too many osts, skipping"
1661         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1662                 skip "server does not support overstriping"
1663         # We do one more stripe than we have OSTs
1664         [ $OSTCOUNT -lt 159 ] || large_xattr_enabled ||
1665                 skip_env "ea_inode feature disabled"
1666
1667         test_mkdir -p $DIR/$tdir
1668         local osts=""
1669         for i in $(seq 0 $OSTCOUNT);
1670         do
1671                 osts=$osts"0"
1672                 if [ $i -ne $OSTCOUNT ]; then
1673                         osts=$osts","
1674                 fi
1675         done
1676         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1677         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1678         echo "$getstripe"
1679
1680         # Strip getstripe output to a space separated list of OSTs
1681         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1682                 awk '{print $1}' | tr '\n' ' ' | sed -e 's/[[:space:]]*$//')
1683         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1684                 error "stripes not on specified OSTs"
1685
1686         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1687 }
1688 run_test 27ce "more stripes than OSTs with -o"
1689
1690 test_27cf() {
1691         local osp_proc="osp.$FSNAME-OST0000-osc-MDT000*.active"
1692         local pid=0
1693
1694         test_mkdir -p $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
1695         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=0"
1696         stack_trap "do_facet $SINGLEMDS $LCTL set_param -n $osp_proc=1" EXIT
1697         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 1" ||
1698                 error "failed to set $osp_proc=0"
1699
1700         $LFS setstripe -o 0 $DIR/$tdir/$tfile &
1701         pid=$!
1702         sleep 1
1703         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=1"
1704         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 0" ||
1705                 error "failed to set $osp_proc=1"
1706         wait $pid
1707         [[ $pid -ne 0 ]] ||
1708                 error "should return error due to $osp_proc=0"
1709 }
1710 run_test 27cf "'setstripe -o' on inactive OSTs should return error"
1711
1712 test_27d() {
1713         test_mkdir $DIR/$tdir
1714         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1715                 error "setstripe failed"
1716         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1717         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1718 }
1719 run_test 27d "create file with default settings"
1720
1721 test_27e() {
1722         # LU-5839 adds check for existed layout before setting it
1723         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1724                 skip "Need MDS version at least 2.7.56"
1725
1726         test_mkdir $DIR/$tdir
1727         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1728         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1729         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1730 }
1731 run_test 27e "setstripe existing file (should return error)"
1732
1733 test_27f() {
1734         test_mkdir $DIR/$tdir
1735         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1736                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1737         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1738                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1739         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1740         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1741 }
1742 run_test 27f "setstripe with bad stripe size (should return error)"
1743
1744 test_27g() {
1745         test_mkdir $DIR/$tdir
1746         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1747         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1748                 error "$DIR/$tdir/$tfile has object"
1749 }
1750 run_test 27g "$LFS getstripe with no objects"
1751
1752 test_27ga() {
1753         test_mkdir $DIR/$tdir
1754         touch $DIR/$tdir/$tfile || error "touch failed"
1755         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1756         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1757         local rc=$?
1758         (( rc == 2 )) || error "getstripe did not return ENOENT"
1759 }
1760 run_test 27ga "$LFS getstripe with missing file (should return error)"
1761
1762 test_27i() {
1763         test_mkdir $DIR/$tdir
1764         touch $DIR/$tdir/$tfile || error "touch failed"
1765         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1766                 error "missing objects"
1767 }
1768 run_test 27i "$LFS getstripe with some objects"
1769
1770 test_27j() {
1771         test_mkdir $DIR/$tdir
1772         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1773                 error "setstripe failed" || true
1774 }
1775 run_test 27j "setstripe with bad stripe offset (should return error)"
1776
1777 test_27k() { # bug 2844
1778         test_mkdir $DIR/$tdir
1779         local file=$DIR/$tdir/$tfile
1780         local ll_max_blksize=$((4 * 1024 * 1024))
1781         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1782         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1783         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1784         dd if=/dev/zero of=$file bs=4k count=1
1785         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1786         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1787 }
1788 run_test 27k "limit i_blksize for broken user apps"
1789
1790 test_27l() {
1791         mcreate $DIR/$tfile || error "creating file"
1792         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1793                 error "setstripe should have failed" || true
1794 }
1795 run_test 27l "check setstripe permissions (should return error)"
1796
1797 test_27m() {
1798         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1799
1800         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1801                 skip_env "multiple clients -- skipping"
1802
1803         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1804                    head -n1)
1805         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1806                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1807         fi
1808         stack_trap simple_cleanup_common
1809         test_mkdir $DIR/$tdir
1810         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1811         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1812                 error "dd should fill OST0"
1813         i=2
1814         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1815                 i=$((i + 1))
1816                 [ $i -gt 256 ] && break
1817         done
1818         i=$((i + 1))
1819         touch $DIR/$tdir/$tfile.$i
1820         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1821             awk '{print $1}'| grep -w "0") ] &&
1822                 error "OST0 was full but new created file still use it"
1823         i=$((i + 1))
1824         touch $DIR/$tdir/$tfile.$i
1825         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1826             awk '{print $1}'| grep -w "0") ] &&
1827                 error "OST0 was full but new created file still use it" || true
1828 }
1829 run_test 27m "create file while OST0 was full"
1830
1831 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1832 # if the OST isn't full anymore.
1833 reset_enospc() {
1834         local ostidx=${1:-""}
1835         local delay
1836         local ready
1837         local get_prealloc
1838
1839         local list=$(comma_list $(osts_nodes))
1840         [ "$ostidx" ] && list=$(facet_host ost$((ostidx + 1)))
1841
1842         do_nodes $list lctl set_param fail_loc=0
1843         wait_delete_completed   # initiate all OST_DESTROYs from MDS to OST
1844         delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1845                 awk '{print $1 * 2;exit;}')
1846         get_prealloc="$LCTL get_param -n osc.*MDT*.prealloc_status |
1847                         grep -v \"^0$\""
1848         wait_update_facet $SINGLEMDS "$get_prealloc" "" $delay
1849 }
1850
1851 test_27n() {
1852         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1854         remote_mds_nodsh && skip "remote MDS with nodsh"
1855         remote_ost_nodsh && skip "remote OST with nodsh"
1856
1857         reset_enospc
1858         rm -f $DIR/$tdir/$tfile
1859         exhaust_precreations 0 0x80000215
1860         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1861         touch $DIR/$tdir/$tfile || error "touch failed"
1862         $LFS getstripe $DIR/$tdir/$tfile
1863         reset_enospc
1864 }
1865 run_test 27n "create file with some full OSTs"
1866
1867 test_27o() {
1868         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1870         remote_mds_nodsh && skip "remote MDS with nodsh"
1871         remote_ost_nodsh && skip "remote OST with nodsh"
1872
1873         reset_enospc
1874         rm -f $DIR/$tdir/$tfile
1875         exhaust_all_precreations 0x215
1876
1877         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1878
1879         reset_enospc
1880         rm -rf $DIR/$tdir/*
1881 }
1882 run_test 27o "create file with all full OSTs (should error)"
1883
1884 function create_and_checktime() {
1885         local fname=$1
1886         local loops=$2
1887         local i
1888
1889         for ((i=0; i < $loops; i++)); do
1890                 local start=$SECONDS
1891                 multiop $fname-$i Oc
1892                 ((SECONDS-start < TIMEOUT)) ||
1893                         error "creation took " $((SECONDS-$start)) && return 1
1894         done
1895 }
1896
1897 test_27oo() {
1898         local mdts=$(comma_list $(mdts_nodes))
1899
1900         [ $MDS1_VERSION -lt $(version_code 2.13.57) ] &&
1901                 skip "Need MDS version at least 2.13.57"
1902
1903         local f0=$DIR/${tfile}-0
1904         local f1=$DIR/${tfile}-1
1905
1906         wait_delete_completed
1907
1908         # refill precreated objects
1909         $LFS setstripe -i0 -c1 $f0
1910
1911         saved=$(do_facet mds1 $LCTL get_param -n lov.*0000*.qos_threshold_rr)
1912         # force QoS allocation policy
1913         do_nodes $mdts $LCTL set_param lov.*.qos_threshold_rr=0%
1914         stack_trap "do_nodes $mdts $LCTL set_param \
1915                 lov.*.qos_threshold_rr=$saved" EXIT
1916         sleep_maxage
1917
1918         # one OST is unavailable, but still have few objects preallocated
1919         stop ost1
1920         stack_trap "start ost1 $(ostdevname 1) $OST_MOUNT_OPTS; \
1921                 rm -rf $f1 $DIR/$tdir*" EXIT
1922
1923         for ((i=0; i < 7; i++)); do
1924                 mkdir $DIR/$tdir$i || error "can't create dir"
1925                 $LFS setstripe -c$((OSTCOUNT-1)) $DIR/$tdir$i ||
1926                         error "can't set striping"
1927         done
1928         for ((i=0; i < 7; i++)); do
1929                 create_and_checktime $DIR/$tdir$i/$tfile 100 &
1930         done
1931         wait
1932 }
1933 run_test 27oo "don't let few threads to reserve too many objects"
1934
1935 test_27p() {
1936         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1938         remote_mds_nodsh && skip "remote MDS with nodsh"
1939         remote_ost_nodsh && skip "remote OST with nodsh"
1940
1941         reset_enospc
1942         rm -f $DIR/$tdir/$tfile
1943         test_mkdir $DIR/$tdir
1944
1945         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1946         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1947         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1948
1949         exhaust_precreations 0 0x80000215
1950         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1951         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1952         $LFS getstripe $DIR/$tdir/$tfile
1953
1954         reset_enospc
1955 }
1956 run_test 27p "append to a truncated file with some full OSTs"
1957
1958 test_27q() {
1959         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1961         remote_mds_nodsh && skip "remote MDS with nodsh"
1962         remote_ost_nodsh && skip "remote OST with nodsh"
1963
1964         reset_enospc
1965         rm -f $DIR/$tdir/$tfile
1966
1967         mkdir_on_mdt0 $DIR/$tdir
1968         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1969         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1970                 error "truncate $DIR/$tdir/$tfile failed"
1971         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1972
1973         exhaust_all_precreations 0x215
1974
1975         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1976         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1977
1978         reset_enospc
1979 }
1980 run_test 27q "append to truncated file with all OSTs full (should error)"
1981
1982 test_27r() {
1983         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1985         remote_mds_nodsh && skip "remote MDS with nodsh"
1986         remote_ost_nodsh && skip "remote OST with nodsh"
1987
1988         reset_enospc
1989         rm -f $DIR/$tdir/$tfile
1990         exhaust_precreations 0 0x80000215
1991
1992         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1993
1994         reset_enospc
1995 }
1996 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1997
1998 test_27s() { # bug 10725
1999         test_mkdir $DIR/$tdir
2000         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
2001         local stripe_count=0
2002         [ $OSTCOUNT -eq 1 ] || stripe_count=2
2003         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
2004                 error "stripe width >= 2^32 succeeded" || true
2005
2006 }
2007 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
2008
2009 test_27t() { # bug 10864
2010         WDIR=$(pwd)
2011         WLFS=$(which lfs)
2012         cd $DIR
2013         touch $tfile
2014         $WLFS getstripe $tfile
2015         cd $WDIR
2016 }
2017 run_test 27t "check that utils parse path correctly"
2018
2019 test_27u() { # bug 4900
2020         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2021         remote_mds_nodsh && skip "remote MDS with nodsh"
2022
2023         local index
2024         local list=$(comma_list $(mdts_nodes))
2025
2026 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
2027         do_nodes $list $LCTL set_param fail_loc=0x139
2028         test_mkdir -p $DIR/$tdir
2029         stack_trap "simple_cleanup_common 1000"
2030         createmany -o $DIR/$tdir/$tfile 1000
2031         do_nodes $list $LCTL set_param fail_loc=0
2032
2033         TLOG=$TMP/$tfile.getstripe
2034         $LFS getstripe $DIR/$tdir > $TLOG
2035         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
2036         [[ $OBJS -gt 0 ]] &&
2037                 error "$OBJS objects created on OST-0. See $TLOG" ||
2038                 rm -f $TLOG
2039 }
2040 run_test 27u "skip object creation on OSC w/o objects"
2041
2042 test_27v() { # bug 4900
2043         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2044         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2045         remote_mds_nodsh && skip "remote MDS with nodsh"
2046         remote_ost_nodsh && skip "remote OST with nodsh"
2047
2048         exhaust_all_precreations 0x215
2049         reset_enospc
2050
2051         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
2052
2053         touch $DIR/$tdir/$tfile
2054         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
2055         # all except ost1
2056         for (( i=1; i < OSTCOUNT; i++ )); do
2057                 do_facet ost$i lctl set_param fail_loc=0x705
2058         done
2059         local START=`date +%s`
2060         createmany -o $DIR/$tdir/$tfile 32
2061
2062         local FINISH=`date +%s`
2063         local TIMEOUT=`lctl get_param -n timeout`
2064         local PROCESS=$((FINISH - START))
2065         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
2066                error "$FINISH - $START >= $TIMEOUT / 2"
2067         sleep $((TIMEOUT / 2 - PROCESS))
2068         reset_enospc
2069 }
2070 run_test 27v "skip object creation on slow OST"
2071
2072 test_27w() { # bug 10997
2073         test_mkdir $DIR/$tdir
2074         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
2075         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
2076                 error "stripe size $size != 65536" || true
2077         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
2078                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
2079 }
2080 run_test 27w "check $LFS setstripe -S and getstrip -d options"
2081
2082 test_27wa() {
2083         [[ $OSTCOUNT -lt 2 ]] &&
2084                 skip_env "skipping multiple stripe count/offset test"
2085
2086         test_mkdir $DIR/$tdir
2087         for i in $(seq 1 $OSTCOUNT); do
2088                 offset=$((i - 1))
2089                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2090                         error "setstripe -c $i -i $offset failed"
2091                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2092                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2093                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2094                 [ $index -ne $offset ] &&
2095                         error "stripe offset $index != $offset" || true
2096         done
2097 }
2098 run_test 27wa "check $LFS setstripe -c -i options"
2099
2100 test_27x() {
2101         remote_ost_nodsh && skip "remote OST with nodsh"
2102         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2103         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2104
2105         OFFSET=$(($OSTCOUNT - 1))
2106         OSTIDX=0
2107         local OST=$(ostname_from_index $OSTIDX)
2108
2109         test_mkdir $DIR/$tdir
2110         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2111         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2112         sleep_maxage
2113         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2114         for i in $(seq 0 $OFFSET); do
2115                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2116                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2117                 error "OST0 was degraded but new created file still use it"
2118         done
2119         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2120 }
2121 run_test 27x "create files while OST0 is degraded"
2122
2123 test_27y() {
2124         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2125         remote_mds_nodsh && skip "remote MDS with nodsh"
2126         remote_ost_nodsh && skip "remote OST with nodsh"
2127         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2128
2129         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2130         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2131                 osp.$mdtosc.prealloc_last_id)
2132         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2133                 osp.$mdtosc.prealloc_next_id)
2134         local fcount=$((last_id - next_id))
2135         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2136         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2137
2138         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2139                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2140         local OST_DEACTIVE_IDX=-1
2141         local OSC
2142         local OSTIDX
2143         local OST
2144
2145         for OSC in $MDS_OSCS; do
2146                 OST=$(osc_to_ost $OSC)
2147                 OSTIDX=$(index_from_ostuuid $OST)
2148                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2149                         OST_DEACTIVE_IDX=$OSTIDX
2150                 fi
2151                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2152                         echo $OSC "is Deactivated:"
2153                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2154                 fi
2155         done
2156
2157         OSTIDX=$(index_from_ostuuid $OST)
2158         test_mkdir $DIR/$tdir
2159         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2160
2161         for OSC in $MDS_OSCS; do
2162                 OST=$(osc_to_ost $OSC)
2163                 OSTIDX=$(index_from_ostuuid $OST)
2164                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2165                         echo $OST "is degraded:"
2166                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2167                                                 obdfilter.$OST.degraded=1
2168                 fi
2169         done
2170
2171         sleep_maxage
2172         createmany -o $DIR/$tdir/$tfile $fcount
2173
2174         for OSC in $MDS_OSCS; do
2175                 OST=$(osc_to_ost $OSC)
2176                 OSTIDX=$(index_from_ostuuid $OST)
2177                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2178                         echo $OST "is recovered from degraded:"
2179                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2180                                                 obdfilter.$OST.degraded=0
2181                 else
2182                         do_facet $SINGLEMDS lctl --device %$OSC activate
2183                 fi
2184         done
2185
2186         # all osp devices get activated, hence -1 stripe count restored
2187         local stripe_count=0
2188
2189         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2190         # devices get activated.
2191         sleep_maxage
2192         $LFS setstripe -c -1 $DIR/$tfile
2193         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2194         rm -f $DIR/$tfile
2195         [ $stripe_count -ne $OSTCOUNT ] &&
2196                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2197         return 0
2198 }
2199 run_test 27y "create files while OST0 is degraded and the rest inactive"
2200
2201 check_seq_oid()
2202 {
2203         log "check file $1"
2204
2205         lmm_count=$($LFS getstripe -c $1)
2206         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2207         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2208
2209         local old_ifs="$IFS"
2210         IFS=$'[:]'
2211         fid=($($LFS path2fid $1))
2212         IFS="$old_ifs"
2213
2214         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2215         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2216
2217         # compare lmm_seq and lu_fid->f_seq
2218         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2219         # compare lmm_object_id and lu_fid->oid
2220         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2221
2222         # check the trusted.fid attribute of the OST objects of the file
2223         local have_obdidx=false
2224         local stripe_nr=0
2225         $LFS getstripe $1 | while read obdidx oid hex seq; do
2226                 # skip lines up to and including "obdidx"
2227                 [ -z "$obdidx" ] && break
2228                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2229                 $have_obdidx || continue
2230
2231                 local ost=$((obdidx + 1))
2232                 local dev=$(ostdevname $ost)
2233                 local oid_hex
2234
2235                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2236
2237                 seq=$(echo $seq | sed -e "s/^0x//g")
2238                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2239                         oid_hex=$(echo $oid)
2240                 else
2241                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2242                 fi
2243                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2244
2245                 local ff=""
2246                 #
2247                 # Don't unmount/remount the OSTs if we don't need to do that.
2248                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2249                 # update too, until that use mount/ll_decode_filter_fid/mount.
2250                 # Re-enable when debugfs will understand new filter_fid.
2251                 #
2252                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2253                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2254                                 $dev 2>/dev/null" | grep "parent=")
2255                 fi
2256                 if [ -z "$ff" ]; then
2257                         stop ost$ost
2258                         mount_fstype ost$ost
2259                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2260                                 $(facet_mntpt ost$ost)/$obj_file)
2261                         unmount_fstype ost$ost
2262                         start ost$ost $dev $OST_MOUNT_OPTS
2263                         clients_up
2264                 fi
2265
2266                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2267
2268                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2269
2270                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2271                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2272                 #
2273                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2274                 #       stripe_size=1048576 component_id=1 component_start=0 \
2275                 #       component_end=33554432
2276                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2277                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2278                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2279                 local ff_pstripe
2280                 if grep -q 'stripe=' <<<$ff; then
2281                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2282                 else
2283                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2284                         # into f_ver in this case.  See comment on ff_parent.
2285                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2286                 fi
2287
2288                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2289                 [ $ff_pseq = $lmm_seq ] ||
2290                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2291                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2292                 [ $ff_poid = $lmm_oid ] ||
2293                         error "FF parent OID $ff_poid != $lmm_oid"
2294                 (($ff_pstripe == $stripe_nr)) ||
2295                         error "FF stripe $ff_pstripe != $stripe_nr"
2296
2297                 stripe_nr=$((stripe_nr + 1))
2298                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2299                         continue
2300                 if grep -q 'stripe_count=' <<<$ff; then
2301                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2302                                             -e 's/ .*//' <<<$ff)
2303                         [ $lmm_count = $ff_scnt ] ||
2304                                 error "FF stripe count $lmm_count != $ff_scnt"
2305                 fi
2306         done
2307 }
2308
2309 test_27z() {
2310         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2311         remote_ost_nodsh && skip "remote OST with nodsh"
2312
2313         test_mkdir $DIR/$tdir
2314         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2315                 { error "setstripe -c -1 failed"; return 1; }
2316         # We need to send a write to every object to get parent FID info set.
2317         # This _should_ also work for setattr, but does not currently.
2318         # touch $DIR/$tdir/$tfile-1 ||
2319         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2320                 { error "dd $tfile-1 failed"; return 2; }
2321         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2322                 { error "setstripe -c -1 failed"; return 3; }
2323         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2324                 { error "dd $tfile-2 failed"; return 4; }
2325
2326         # make sure write RPCs have been sent to OSTs
2327         sync; sleep 5; sync
2328
2329         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2330         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2331 }
2332 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2333
2334 test_27A() { # b=19102
2335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2336
2337         save_layout_restore_at_exit $MOUNT
2338         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2339         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2340                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2341         local default_size=$($LFS getstripe -S $MOUNT)
2342         local default_offset=$($LFS getstripe -i $MOUNT)
2343         local dsize=$(do_facet $SINGLEMDS \
2344                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2345         [ $default_size -eq $dsize ] ||
2346                 error "stripe size $default_size != $dsize"
2347         [ $default_offset -eq -1 ] ||
2348                 error "stripe offset $default_offset != -1"
2349 }
2350 run_test 27A "check filesystem-wide default LOV EA values"
2351
2352 test_27B() { # LU-2523
2353         test_mkdir $DIR/$tdir
2354         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2355         touch $DIR/$tdir/f0
2356         # open f1 with O_LOV_DELAY_CREATE
2357         # rename f0 onto f1
2358         # call setstripe ioctl on open file descriptor for f1
2359         # close
2360         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2361                 $DIR/$tdir/f0
2362
2363         rm -f $DIR/$tdir/f1
2364         # open f1 with O_LOV_DELAY_CREATE
2365         # unlink f1
2366         # call setstripe ioctl on open file descriptor for f1
2367         # close
2368         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2369
2370         # Allow multiop to fail in imitation of NFS's busted semantics.
2371         true
2372 }
2373 run_test 27B "call setstripe on open unlinked file/rename victim"
2374
2375 # 27C family tests full striping and overstriping
2376 test_27Ca() { #LU-2871
2377         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2378
2379         declare -a ost_idx
2380         local index
2381         local found
2382         local i
2383         local j
2384
2385         test_mkdir $DIR/$tdir
2386         cd $DIR/$tdir
2387         for i in $(seq 0 $((OSTCOUNT - 1))); do
2388                 # set stripe across all OSTs starting from OST$i
2389                 $LFS setstripe -i $i -c -1 $tfile$i
2390                 # get striping information
2391                 ost_idx=($($LFS getstripe $tfile$i |
2392                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2393                 echo "OST Index: ${ost_idx[*]}"
2394
2395                 # check the layout
2396                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2397                         error "${#ost_idx[@]} != $OSTCOUNT"
2398
2399                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2400                         found=0
2401                         for j in "${ost_idx[@]}"; do
2402                                 if [ $index -eq $j ]; then
2403                                         found=1
2404                                         break
2405                                 fi
2406                         done
2407                         [ $found = 1 ] ||
2408                                 error "Can not find $index in ${ost_idx[*]}"
2409                 done
2410         done
2411 }
2412 run_test 27Ca "check full striping across all OSTs"
2413
2414 test_27Cb() {
2415         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2416                 skip "server does not support overstriping"
2417         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2418                 skip_env "too many osts, skipping"
2419
2420         test_mkdir -p $DIR/$tdir
2421         local setcount=$(($OSTCOUNT * 2))
2422         [ $setcount -lt 160 ] || large_xattr_enabled ||
2423                 skip_env "ea_inode feature disabled"
2424
2425         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2426                 error "setstripe failed"
2427
2428         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2429         [ $count -eq $setcount ] ||
2430                 error "stripe count $count, should be $setcount"
2431
2432         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2433                 error "overstriped should be set in pattern"
2434
2435         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2436                 error "dd failed"
2437 }
2438 run_test 27Cb "more stripes than OSTs with -C"
2439
2440 test_27Cc() {
2441         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2442                 skip "server does not support overstriping"
2443         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2444
2445         test_mkdir -p $DIR/$tdir
2446         local setcount=$(($OSTCOUNT - 1))
2447
2448         [ $setcount -lt 160 ] || large_xattr_enabled ||
2449                 skip_env "ea_inode feature disabled"
2450
2451         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2452                 error "setstripe failed"
2453
2454         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2455         [ $count -eq $setcount ] ||
2456                 error "stripe count $count, should be $setcount"
2457
2458         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2459                 error "overstriped should not be set in pattern"
2460
2461         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2462                 error "dd failed"
2463 }
2464 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2465
2466 test_27Cd() {
2467         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2468                 skip "server does not support overstriping"
2469         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2470         large_xattr_enabled || skip_env "ea_inode feature disabled"
2471
2472         test_mkdir -p $DIR/$tdir
2473         local setcount=$LOV_MAX_STRIPE_COUNT
2474
2475         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2476                 error "setstripe failed"
2477
2478         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2479         [ $count -eq $setcount ] ||
2480                 error "stripe count $count, should be $setcount"
2481
2482         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2483                 error "overstriped should be set in pattern"
2484
2485         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2486                 error "dd failed"
2487
2488         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2489 }
2490 run_test 27Cd "test maximum stripe count"
2491
2492 test_27Ce() {
2493         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2494                 skip "server does not support overstriping"
2495         test_mkdir -p $DIR/$tdir
2496
2497         pool_add $TESTNAME || error "Pool creation failed"
2498         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2499
2500         local setcount=8
2501
2502         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2503                 error "setstripe failed"
2504
2505         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2506         [ $count -eq $setcount ] ||
2507                 error "stripe count $count, should be $setcount"
2508
2509         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2510                 error "overstriped should be set in pattern"
2511
2512         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2513                 error "dd failed"
2514
2515         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2516 }
2517 run_test 27Ce "test pool with overstriping"
2518
2519 test_27Cf() {
2520         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2521                 skip "server does not support overstriping"
2522         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2523                 skip_env "too many osts, skipping"
2524
2525         test_mkdir -p $DIR/$tdir
2526
2527         local setcount=$(($OSTCOUNT * 2))
2528         [ $setcount -lt 160 ] || large_xattr_enabled ||
2529                 skip_env "ea_inode feature disabled"
2530
2531         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2532                 error "setstripe failed"
2533
2534         echo 1 > $DIR/$tdir/$tfile
2535
2536         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2537         [ $count -eq $setcount ] ||
2538                 error "stripe count $count, should be $setcount"
2539
2540         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2541                 error "overstriped should be set in pattern"
2542
2543         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2544                 error "dd failed"
2545
2546         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2547 }
2548 run_test 27Cf "test default inheritance with overstriping"
2549
2550 test_27D() {
2551         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2552         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2553         remote_mds_nodsh && skip "remote MDS with nodsh"
2554
2555         local POOL=${POOL:-testpool}
2556         local first_ost=0
2557         local last_ost=$(($OSTCOUNT - 1))
2558         local ost_step=1
2559         local ost_list=$(seq $first_ost $ost_step $last_ost)
2560         local ost_range="$first_ost $last_ost $ost_step"
2561
2562         test_mkdir $DIR/$tdir
2563         pool_add $POOL || error "pool_add failed"
2564         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2565
2566         local skip27D
2567         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2568                 skip27D+="-s 29"
2569         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2570                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2571                         skip27D+=" -s 30,31"
2572         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2573           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2574                 skip27D+=" -s 32,33"
2575         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2576                 skip27D+=" -s 34"
2577         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2578                 error "llapi_layout_test failed"
2579
2580         destroy_test_pools || error "destroy test pools failed"
2581 }
2582 run_test 27D "validate llapi_layout API"
2583
2584 # Verify that default_easize is increased from its initial value after
2585 # accessing a widely striped file.
2586 test_27E() {
2587         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2588         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2589                 skip "client does not have LU-3338 fix"
2590
2591         # 72 bytes is the minimum space required to store striping
2592         # information for a file striped across one OST:
2593         # (sizeof(struct lov_user_md_v3) +
2594         #  sizeof(struct lov_user_ost_data_v1))
2595         local min_easize=72
2596         $LCTL set_param -n llite.*.default_easize $min_easize ||
2597                 error "lctl set_param failed"
2598         local easize=$($LCTL get_param -n llite.*.default_easize)
2599
2600         [ $easize -eq $min_easize ] ||
2601                 error "failed to set default_easize"
2602
2603         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2604                 error "setstripe failed"
2605         # In order to ensure stat() call actually talks to MDS we need to
2606         # do something drastic to this file to shake off all lock, e.g.
2607         # rename it (kills lookup lock forcing cache cleaning)
2608         mv $DIR/$tfile $DIR/${tfile}-1
2609         ls -l $DIR/${tfile}-1
2610         rm $DIR/${tfile}-1
2611
2612         easize=$($LCTL get_param -n llite.*.default_easize)
2613
2614         [ $easize -gt $min_easize ] ||
2615                 error "default_easize not updated"
2616 }
2617 run_test 27E "check that default extended attribute size properly increases"
2618
2619 test_27F() { # LU-5346/LU-7975
2620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2621         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2622         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2623                 skip "Need MDS version at least 2.8.51"
2624         remote_ost_nodsh && skip "remote OST with nodsh"
2625
2626         test_mkdir $DIR/$tdir
2627         rm -f $DIR/$tdir/f0
2628         $LFS setstripe -c 2 $DIR/$tdir
2629
2630         # stop all OSTs to reproduce situation for LU-7975 ticket
2631         for num in $(seq $OSTCOUNT); do
2632                 stop ost$num
2633         done
2634
2635         # open/create f0 with O_LOV_DELAY_CREATE
2636         # truncate f0 to a non-0 size
2637         # close
2638         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2639
2640         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2641         # open/write it again to force delayed layout creation
2642         cat /etc/hosts > $DIR/$tdir/f0 &
2643         catpid=$!
2644
2645         # restart OSTs
2646         for num in $(seq $OSTCOUNT); do
2647                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2648                         error "ost$num failed to start"
2649         done
2650
2651         wait $catpid || error "cat failed"
2652
2653         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2654         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2655                 error "wrong stripecount"
2656
2657 }
2658 run_test 27F "Client resend delayed layout creation with non-zero size"
2659
2660 test_27G() { #LU-10629
2661         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2662                 skip "Need MDS version at least 2.11.51"
2663         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2664         remote_mds_nodsh && skip "remote MDS with nodsh"
2665         local POOL=${POOL:-testpool}
2666         local ostrange="0 0 1"
2667
2668         test_mkdir $DIR/$tdir
2669         touch $DIR/$tdir/$tfile.nopool
2670         pool_add $POOL || error "pool_add failed"
2671         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2672         $LFS setstripe -p $POOL $DIR/$tdir
2673
2674         local pool=$($LFS getstripe -p $DIR/$tdir)
2675
2676         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2677         touch $DIR/$tdir/$tfile.default
2678         $LFS setstripe -E 1M --pool $POOL -c 1 -E eof -c 1 $DIR/$tdir/$tfile.pfl
2679         $LFS find $DIR/$tdir -type f --pool $POOL
2680         local found=$($LFS find $DIR/$tdir -type f --pool $POOL | wc -l)
2681         [[ "$found" == "2" ]] ||
2682                 error "found $found != 2 files in '$DIR/$tdir' in '$POOL'"
2683
2684         $LFS setstripe -d $DIR/$tdir
2685
2686         pool=$($LFS getstripe -p -d $DIR/$tdir)
2687
2688         [[ "$pool" != "$POOL" ]] || error "$DIR/$tdir is still '$pool'"
2689 }
2690 run_test 27G "Clear OST pool from stripe"
2691
2692 test_27H() {
2693         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2694                 skip "Need MDS version newer than 2.11.54"
2695         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2696         test_mkdir $DIR/$tdir
2697         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2698         touch $DIR/$tdir/$tfile
2699         $LFS getstripe -c $DIR/$tdir/$tfile
2700         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2701                 error "two-stripe file doesn't have two stripes"
2702
2703         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2704         $LFS getstripe -y $DIR/$tdir/$tfile
2705         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2706              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2707                 error "expected l_ost_idx: [02]$ not matched"
2708
2709         # make sure ost list has been cleared
2710         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2711         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2712                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2713         touch $DIR/$tdir/f3
2714         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2715 }
2716 run_test 27H "Set specific OSTs stripe"
2717
2718 test_27I() {
2719         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2720         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2721         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2722                 skip "Need MDS version newer than 2.12.52"
2723         local pool=$TESTNAME
2724         local ostrange="1 1 1"
2725
2726         save_layout_restore_at_exit $MOUNT
2727         $LFS setstripe -c 2 -i 0 $MOUNT
2728         pool_add $pool || error "pool_add failed"
2729         pool_add_targets $pool $ostrange ||
2730                 error "pool_add_targets failed"
2731         test_mkdir $DIR/$tdir
2732         $LFS setstripe -p $pool $DIR/$tdir
2733         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2734         $LFS getstripe $DIR/$tdir/$tfile
2735 }
2736 run_test 27I "check that root dir striping does not break parent dir one"
2737
2738 test_27J() {
2739         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2740                 skip "Need MDS version newer than 2.12.51"
2741
2742         test_mkdir $DIR/$tdir
2743         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2744         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2745
2746         # create foreign file (raw way)
2747         ! $LFS setstripe --flags 0xda08 $DIR/$tdir/$tfile ||
2748                 error "creating $tfile w/ hex flags w/o --foreign should fail"
2749
2750         ! $LFS setstripe --foreign --flags foo \
2751                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tfile ||
2752                         error "creating $tfile with '--flags foo' should fail"
2753
2754         ! $LFS setstripe --foreign --flags 0xffffffff \
2755                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tfile ||
2756                         error "creating $tfile w/ 0xffffffff flags should fail"
2757
2758         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2759                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2760
2761         # verify foreign file (raw way)
2762         parse_foreign_file -f $DIR/$tdir/$tfile |
2763                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2764                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2765         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2766                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2767         parse_foreign_file -f $DIR/$tdir/$tfile |
2768                 grep "lov_foreign_size: 73" ||
2769                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2770         parse_foreign_file -f $DIR/$tdir/$tfile |
2771                 grep "lov_foreign_type: 1" ||
2772                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2773         parse_foreign_file -f $DIR/$tdir/$tfile |
2774                 grep "lov_foreign_flags: 0x0000DA08" ||
2775                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2776         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2777                 grep "lov_foreign_value: 0x" |
2778                 sed -e 's/lov_foreign_value: 0x//')
2779         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2780         [[ $lov = ${lov2// /} ]] ||
2781                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2782
2783         # create foreign file (lfs + API)
2784         $LFS setstripe --foreign=none --flags 0xda08 \
2785                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2786                 error "$DIR/$tdir/${tfile}2: create failed"
2787
2788         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2789                 grep "lfm_magic:.*0x0BD70BD0" ||
2790                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2791         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2792         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2793                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2794         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*none" ||
2795                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2796         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2797                 grep "lfm_flags:.*0x0000DA08" ||
2798                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2799         $LFS getstripe $DIR/$tdir/${tfile}2 |
2800                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2801                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2802
2803         # modify striping should fail
2804         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2805                 error "$DIR/$tdir/$tfile: setstripe should fail"
2806         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2807                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2808
2809         # R/W should fail
2810         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2811         cat $DIR/$tdir/${tfile}2 &&
2812                 error "$DIR/$tdir/${tfile}2: read should fail"
2813         cat /etc/passwd > $DIR/$tdir/$tfile &&
2814                 error "$DIR/$tdir/$tfile: write should fail"
2815         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2816                 error "$DIR/$tdir/${tfile}2: write should fail"
2817
2818         # chmod should work
2819         chmod 222 $DIR/$tdir/$tfile ||
2820                 error "$DIR/$tdir/$tfile: chmod failed"
2821         chmod 222 $DIR/$tdir/${tfile}2 ||
2822                 error "$DIR/$tdir/${tfile}2: chmod failed"
2823
2824         # chown should work
2825         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2826                 error "$DIR/$tdir/$tfile: chown failed"
2827         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2828                 error "$DIR/$tdir/${tfile}2: chown failed"
2829
2830         # rename should work
2831         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2832                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2833         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2834                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2835
2836         #remove foreign file
2837         rm $DIR/$tdir/${tfile}.new ||
2838                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2839         rm $DIR/$tdir/${tfile}2.new ||
2840                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2841 }
2842 run_test 27J "basic ops on file with foreign LOV"
2843
2844 test_27K() {
2845         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2846                 skip "Need MDS version newer than 2.12.49"
2847
2848         test_mkdir $DIR/$tdir
2849         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2850         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2851
2852         # create foreign dir (raw way)
2853         ! $LFS setdirstripe --flags 0xda08 $DIR/$tdir/$tdir ||
2854                 error "creating $tdir w/ hex flags w/o --foreign should fail"
2855
2856         ! $LFS setdirstripe --foreign --flags foo \
2857                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tdir ||
2858                         error "creating $tdir with '--flags foo' should fail"
2859
2860         ! $LFS setdirstripe --foreign --flags 0xffffffff \
2861                 --xattr ${uuid1}@${uuid2} $DIR/$tdir/$tdir ||
2862                         error "creating $tdir w/ 0xffffffff flags should fail"
2863
2864         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2865                 error "create_foreign_dir FAILED"
2866
2867         # verify foreign dir (raw way)
2868         parse_foreign_dir -d $DIR/$tdir/$tdir |
2869                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2870                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2871         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2872                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2873         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2874                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2875         parse_foreign_dir -d $DIR/$tdir/$tdir |
2876                 grep "lmv_foreign_flags: 55813$" ||
2877                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2878         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2879                 grep "lmv_foreign_value: 0x" |
2880                 sed 's/lmv_foreign_value: 0x//')
2881         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2882                 sed 's/ //g')
2883         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2884
2885         # create foreign dir (lfs + API)
2886         $LFS mkdir --foreign=none --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2887                 $DIR/$tdir/${tdir}2 ||
2888                 error "$DIR/$tdir/${tdir}2: create failed"
2889
2890         $LFS getdirstripe -v $DIR/$tdir/${tdir}2
2891
2892         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2893                 grep "lfm_magic:.*0x0CD50CD0" ||
2894                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2895         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2896         # - sizeof(lfm_type) - sizeof(lfm_flags)
2897         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2898                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2899         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*none" ||
2900                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2901         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2902                 grep "lfm_flags:.*0x0000DA05" ||
2903                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2904         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2905                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2906                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2907
2908         # file create in dir should fail
2909         touch $DIR/$tdir/$tdir/$tfile && error "$DIR/$tdir: file create should fail"
2910         touch $DIR/$tdir/${tdir}2/$tfile &&
2911                 error "$DIR/${tdir}2: file create should fail"
2912
2913         # chmod should work
2914         chmod 777 $DIR/$tdir/$tdir ||
2915                 error "$DIR/$tdir: chmod failed"
2916         chmod 777 $DIR/$tdir/${tdir}2 ||
2917                 error "$DIR/${tdir}2: chmod failed"
2918
2919         # chown should work
2920         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2921                 error "$DIR/$tdir: chown failed"
2922         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2923                 error "$DIR/${tdir}2: chown failed"
2924
2925         # rename should work
2926         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2927                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2928         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2929                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2930
2931         #remove foreign dir
2932         rmdir $DIR/$tdir/${tdir}.new ||
2933                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2934         rmdir $DIR/$tdir/${tdir}2.new ||
2935                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2936 }
2937 run_test 27K "basic ops on dir with foreign LMV"
2938
2939 test_27L() {
2940         remote_mds_nodsh && skip "remote MDS with nodsh"
2941
2942         local POOL=${POOL:-$TESTNAME}
2943
2944         pool_add $POOL || error "pool_add failed"
2945
2946         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2947                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2948                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2949 }
2950 run_test 27L "lfs pool_list gives correct pool name"
2951
2952 test_27M() {
2953         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2954                 skip "Need MDS version >= than 2.12.57"
2955         remote_mds_nodsh && skip "remote MDS with nodsh"
2956         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2957
2958         # Set default striping on directory
2959         local setcount=4
2960         local stripe_opt
2961         local mdts=$(comma_list $(mdts_nodes))
2962
2963         # if we run against a 2.12 server which lacks overstring support
2964         # then the connect_flag will not report overstriping, even if client
2965         # is 2.14+
2966         if [[ $($LCTL get_param mdc.*.connect_flags) =~ overstriping ]]; then
2967                 stripe_opt="-C $setcount"
2968         elif (( $OSTCOUNT >= $setcount )); then
2969                 stripe_opt="-c $setcount"
2970         else
2971                 skip "server does not support overstriping"
2972         fi
2973
2974         test_mkdir $DIR/$tdir
2975
2976         # Validate existing append_* params and ensure restore
2977         local pool=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_pool)
2978         [[ "$pool" == "" ]] || error "expected append_pool == '', got '$pool'"
2979         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool=none"
2980
2981         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2982         ((orig_count == 1)) || error "expected append_stripe_count == 1, got $orig_count"
2983         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1"
2984
2985         $LFS setstripe $stripe_opt $DIR/$tdir
2986
2987         echo 1 > $DIR/$tdir/${tfile}.1
2988         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2989         [ $count -eq $setcount ] ||
2990                 error "(1) stripe count $count, should be $setcount"
2991
2992         local appendcount=$orig_count
2993         echo 1 >> $DIR/$tdir/${tfile}.2_append
2994         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2995         [ $count -eq $appendcount ] ||
2996                 error "(2)stripe count $count, should be $appendcount for append"
2997
2998         # Disable O_APPEND striping, verify it works
2999         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
3000
3001         # Should now get the default striping, which is 4
3002         setcount=4
3003         echo 1 >> $DIR/$tdir/${tfile}.3_append
3004         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
3005         [ $count -eq $setcount ] ||
3006                 error "(3) stripe count $count, should be $setcount"
3007
3008         # Try changing the stripe count for append files
3009         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
3010
3011         # Append striping is now 2 (directory default is still 4)
3012         appendcount=2
3013         echo 1 >> $DIR/$tdir/${tfile}.4_append
3014         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
3015         [ $count -eq $appendcount ] ||
3016                 error "(4) stripe count $count, should be $appendcount for append"
3017
3018         # Test append stripe count of -1
3019         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
3020         appendcount=$OSTCOUNT
3021         echo 1 >> $DIR/$tdir/${tfile}.5
3022         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
3023         [ $count -eq $appendcount ] ||
3024                 error "(5) stripe count $count, should be $appendcount for append"
3025
3026         # Set append striping back to default of 1
3027         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
3028
3029         # Try a new default striping, PFL + DOM
3030         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
3031
3032         # Create normal DOM file, DOM returns stripe count == 0
3033         setcount=0
3034         touch $DIR/$tdir/${tfile}.6
3035         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
3036         [ $count -eq $setcount ] ||
3037                 error "(6) stripe count $count, should be $setcount"
3038
3039         # Show
3040         appendcount=1
3041         echo 1 >> $DIR/$tdir/${tfile}.7_append
3042         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
3043         [ $count -eq $appendcount ] ||
3044                 error "(7) stripe count $count, should be $appendcount for append"
3045
3046         # Clean up DOM layout
3047         $LFS setstripe -d $DIR/$tdir
3048
3049         save_layout_restore_at_exit $MOUNT
3050         # Now test that append striping works when layout is from root
3051         $LFS setstripe -c 2 $MOUNT
3052         # Make a special directory for this
3053         mkdir $DIR/${tdir}/${tdir}.2
3054
3055         # Verify for normal file
3056         setcount=2
3057         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
3058         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
3059         [ $count -eq $setcount ] ||
3060                 error "(8) stripe count $count, should be $setcount"
3061
3062         appendcount=1
3063         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
3064         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
3065         [ $count -eq $appendcount ] ||
3066                 error "(9) stripe count $count, should be $appendcount for append"
3067
3068         # Now test O_APPEND striping with pools
3069         pool_add $TESTNAME || error "pool creation failed"
3070         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
3071         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
3072
3073         echo 1 >> $DIR/$tdir/${tfile}.10_append
3074
3075         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
3076         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
3077
3078         # Check that count is still correct
3079         appendcount=1
3080         echo 1 >> $DIR/$tdir/${tfile}.11_append
3081         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
3082         [ $count -eq $appendcount ] ||
3083                 error "(11) stripe count $count, should be $appendcount for append"
3084
3085         # Disable O_APPEND stripe count, verify pool works separately
3086         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
3087
3088         echo 1 >> $DIR/$tdir/${tfile}.12_append
3089
3090         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
3091         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
3092
3093         # Remove pool setting, verify it's not applied
3094         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
3095
3096         echo 1 >> $DIR/$tdir/${tfile}.13_append
3097
3098         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
3099         [ "$pool" = "" ] || error "(13) pool found: $pool"
3100 }
3101 run_test 27M "test O_APPEND striping"
3102
3103 test_27N() {
3104         combined_mgs_mds && skip "needs separate MGS/MDT"
3105
3106         pool_add $TESTNAME || error "pool_add failed"
3107         do_facet mgs "$LCTL pool_list $FSNAME" |
3108                 grep -Fx "${FSNAME}.${TESTNAME}" ||
3109                 error "lctl pool_list on MGS failed"
3110 }
3111 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
3112
3113 clean_foreign_symlink() {
3114         trap 0
3115         lctl set_param llite/$FSNAME-*/foreign_symlink_enable=0
3116         for i in $DIR/$tdir/* ; do
3117                 $LFS unlink_foreign $i || true
3118         done
3119 }
3120
3121 test_27O() {
3122         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.51) ]] &&
3123                 skip "Need MDS version newer than 2.12.51"
3124
3125         test_mkdir $DIR/$tdir
3126         local uuid1=$(cat /proc/sys/kernel/random/uuid)
3127         local uuid2=$(cat /proc/sys/kernel/random/uuid)
3128
3129         trap clean_foreign_symlink EXIT
3130
3131         # enable foreign_symlink behaviour
3132         $LCTL set_param llite/$FSNAME-*/foreign_symlink_enable=1
3133
3134         # foreign symlink LOV format is a partial path by default
3135
3136         # create foreign file (lfs + API)
3137         $LFS setstripe --foreign=symlink --flags 0xda05 \
3138                 -x "${uuid1}/${uuid2}" --mode 0600 $DIR/$tdir/${tfile} ||
3139                 error "$DIR/$tdir/${tfile}: create failed"
3140
3141         $LFS getstripe -v $DIR/$tdir/${tfile} |
3142                 grep "lfm_magic:.*0x0BD70BD0" ||
3143                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign magic"
3144         $LFS getstripe -v $DIR/$tdir/${tfile} | grep "lfm_type:.*symlink" ||
3145                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign type"
3146         $LFS getstripe -v $DIR/$tdir/${tfile} |
3147                 grep "lfm_flags:.*0x0000DA05" ||
3148                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign flags"
3149         $LFS getstripe $DIR/$tdir/${tfile} |
3150                 grep "lfm_value:.*${uuid1}/${uuid2}" ||
3151                 error "$DIR/$tdir/${tfile}: invalid LOV EA foreign value"
3152
3153         # modify striping should fail
3154         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
3155                 error "$DIR/$tdir/$tfile: setstripe should fail"
3156
3157         # R/W should fail ("/{foreign_symlink_prefix}/${uuid1}/" missing)
3158         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
3159         cat /etc/passwd > $DIR/$tdir/$tfile &&
3160                 error "$DIR/$tdir/$tfile: write should fail"
3161
3162         # rename should succeed
3163         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
3164                 error "$DIR/$tdir/$tfile: rename has failed"
3165
3166         #remove foreign_symlink file should fail
3167         rm $DIR/$tdir/${tfile}.new &&
3168                 error "$DIR/$tdir/${tfile}.new: remove of foreign_symlink file should fail"
3169
3170         #test fake symlink
3171         mkdir /tmp/${uuid1} ||
3172                 error "/tmp/${uuid1}: mkdir has failed"
3173         echo FOOFOO > /tmp/${uuid1}/${uuid2} ||
3174                 error "/tmp/${uuid1}/${uuid2}: echo has failed"
3175         $LCTL set_param llite/$FSNAME-*/foreign_symlink_prefix=/tmp/
3176         $CHECKSTAT -t link -l /tmp/${uuid1}/${uuid2} $DIR/$tdir/${tfile}.new ||
3177                 error "$DIR/$tdir/${tfile}.new: not seen as a symlink"
3178         #read should succeed now
3179         cat $DIR/$tdir/${tfile}.new | grep FOOFOO ||
3180                 error "$DIR/$tdir/${tfile}.new: symlink resolution has failed"
3181         #write should succeed now
3182         cat /etc/passwd > $DIR/$tdir/${tfile}.new ||
3183                 error "$DIR/$tdir/${tfile}.new: write should succeed"
3184         diff /etc/passwd $DIR/$tdir/${tfile}.new ||
3185                 error "$DIR/$tdir/${tfile}.new: diff has failed"
3186         diff /etc/passwd /tmp/${uuid1}/${uuid2} ||
3187                 error "/tmp/${uuid1}/${uuid2}: diff has failed"
3188
3189         #check that getstripe still works
3190         $LFS getstripe $DIR/$tdir/${tfile}.new ||
3191                 error "$DIR/$tdir/${tfile}.new: getstripe should still work with foreign_symlink enabled"
3192
3193         # chmod should still succeed
3194         chmod 644 $DIR/$tdir/${tfile}.new ||
3195                 error "$DIR/$tdir/${tfile}.new: chmod has failed"
3196
3197         # chown should still succeed
3198         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}.new ||
3199                 error "$DIR/$tdir/${tfile}.new: chown has failed"
3200
3201         # rename should still succeed
3202         mv $DIR/$tdir/${tfile}.new $DIR/$tdir/${tfile} ||
3203                 error "$DIR/$tdir/${tfile}.new: rename has failed"
3204
3205         #remove foreign_symlink file should still fail
3206         rm $DIR/$tdir/${tfile} &&
3207                 error "$DIR/$tdir/${tfile}: remove of foreign_symlink file should fail"
3208
3209         #use special ioctl() to unlink foreign_symlink file
3210         $LFS unlink_foreign $DIR/$tdir/${tfile} ||
3211                 error "$DIR/$tdir/$tfile: unlink/ioctl failed"
3212
3213 }
3214 run_test 27O "basic ops on foreign file of symlink type"
3215
3216 test_27P() {
3217         [[ $(lustre_version_code $SINGLEMDS) -le $(version_code 2.12.49) ]] &&
3218                 skip "Need MDS version newer than 2.12.49"
3219
3220         test_mkdir $DIR/$tdir
3221         local uuid1=$(cat /proc/sys/kernel/random/uuid)
3222         local uuid2=$(cat /proc/sys/kernel/random/uuid)
3223
3224         trap clean_foreign_symlink EXIT
3225
3226         # enable foreign_symlink behaviour
3227         $LCTL set_param llite/$FSNAME-*/foreign_symlink_enable=1
3228
3229         # foreign symlink LMV format is a partial path by default
3230
3231         # create foreign dir (lfs + API)
3232         $LFS mkdir --foreign=symlink --xattr="${uuid1}/${uuid2}" \
3233                 --flags=0xda05 --mode 0750 $DIR/$tdir/${tdir} ||
3234                 error "$DIR/$tdir/${tdir}: create failed"
3235
3236         $LFS getdirstripe -v $DIR/$tdir/${tdir}
3237
3238         $LFS getdirstripe -v $DIR/$tdir/${tdir} |
3239                 grep "lfm_magic:.*0x0CD50CD0" ||
3240                 error "$DIR/$tdir/${tdir}: invalid LMV EA magic"
3241         $LFS getdirstripe -v $DIR/$tdir/${tdir} | grep "lfm_type:.*symlink" ||
3242                 error "$DIR/$tdir/${tdir}: invalid LMV EA type"
3243         $LFS getdirstripe -v $DIR/$tdir/${tdir} |
3244                 grep "lfm_flags:.*0x0000DA05" ||
3245                 error "$DIR/$tdir/${tdir}: invalid LMV EA flags"
3246         $LFS getdirstripe $DIR/$tdir/${tdir} |
3247                 grep "lfm_value.*${uuid1}/${uuid2}" ||
3248                 error "$DIR/$tdir/${tdir}: invalid LMV EA value"
3249
3250         # file create in dir should fail
3251         # ("/{foreign_symlink_prefix}/${uuid1}/${uuid2}/" missing)
3252         touch $DIR/$tdir/$tdir/$tfile && error "$DIR/$tdir: file create should fail"
3253
3254         # rename should succeed
3255         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
3256                 error "$DIR/$tdir/$tdir: rename of foreign_symlink dir has failed"
3257
3258         #remove foreign_symlink dir should fail
3259         rmdir $DIR/$tdir/${tdir}.new &&
3260                 error "$DIR/$tdir/${tdir}.new: remove of foreign_symlink dir should fail"
3261
3262         #test fake symlink
3263         mkdir -p /tmp/${uuid1}/${uuid2} ||
3264                 error "/tmp/${uuid1}/${uuid2}: mkdir has failed"
3265         echo FOOFOO > /tmp/${uuid1}/${uuid2}/foo ||
3266                 error "/tmp/${uuid1}/${uuid2}/foo: echo has failed"
3267         $LCTL set_param llite/$FSNAME-*/foreign_symlink_prefix=/tmp/
3268         $CHECKSTAT -t link -l /tmp/${uuid1}/${uuid2} $DIR/$tdir/${tdir}.new ||
3269                 error "$DIR/$tdir/${tdir}.new: not seen as a symlink"
3270         cat $DIR/$tdir/${tdir}.new/foo | grep FOOFOO ||
3271                 error "$DIR/$tdir/${tdir}.new: symlink resolution has failed"
3272
3273         #check that getstripe fails now that foreign_symlink enabled
3274         $LFS getdirstripe $DIR/$tdir/${tdir}.new ||
3275                 error "$DIR/$tdir/${tdir}.new: getdirstripe should still work with foreign_symlink enabled"
3276
3277         # file create in dir should work now
3278         cp /etc/passwd $DIR/$tdir/${tdir}.new/$tfile ||
3279                 error "$DIR/$tdir/${tdir}.new/$tfile: file create should fail"
3280         diff /etc/passwd $DIR/$tdir/${tdir}.new/$tfile ||
3281                 error "$DIR/$tdir/${tdir}.new/$tfile: diff has failed"
3282         diff /etc/passwd /tmp/${uuid1}/${uuid2}/$tfile ||
3283                 error "/tmp/${uuid1}/${uuid2}/$tfile: diff has failed"
3284
3285         # chmod should still succeed
3286         chmod 755 $DIR/$tdir/${tdir}.new ||
3287                 error "$DIR/$tdir/${tdir}.new: chmod has failed"
3288
3289         # chown should still succeed
3290         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}.new ||
3291                 error "$DIR/$tdir/${tdir}.new: chown has failed"
3292
3293         # rename should still succeed
3294         mv $DIR/$tdir/${tdir}.new $DIR/$tdir/${tdir} ||
3295                 error "$DIR/$tdir/${tdir}.new: rename of foreign_symlink dir has failed"
3296
3297         #remove foreign_symlink dir should still fail
3298         rmdir $DIR/$tdir/${tdir} &&
3299                 error "$DIR/$tdir/${tdir}: remove of foreign_symlink dir should fail"
3300
3301         #use special ioctl() to unlink foreign_symlink file
3302         $LFS unlink_foreign $DIR/$tdir/${tdir} ||
3303                 error "$DIR/$tdir/$tdir: unlink/ioctl failed"
3304
3305         #created file should still exist
3306         [[ -f /tmp/${uuid1}/${uuid2}/$tfile ]] ||
3307                 error "/tmp/${uuid1}/${uuid2}/$tfile has been removed"
3308         diff /etc/passwd /tmp/${uuid1}/${uuid2}/$tfile ||
3309                 error "/tmp/${uuid1}/${uuid2}/$tfile: diff has failed"
3310 }
3311 run_test 27P "basic ops on foreign dir of foreign_symlink type"
3312
3313 test_27Q() {
3314         rm -f $TMP/$tfile $TMP/$tfile.loop $TMP/$tfile.none $TMP/$tfile.broken
3315         stack_trap "rm -f $TMP/$tfile*"
3316
3317         test_mkdir $DIR/$tdir-1
3318         test_mkdir $DIR/$tdir-2
3319
3320         echo 'It is what it is' > $DIR/$tdir-1/$tfile
3321         lov_getstripe_old $DIR/$tdir-1/$tfile || error "$DIR/$tdir-1/$tfile: rc = $?"
3322
3323         ln -s $DIR/$tdir-1/$tfile $DIR/$tdir-2/$tfile
3324         lov_getstripe_old $DIR/$tdir-2/$tfile || error "$DIR/$tdir-2/$tfile: rc = $?"
3325
3326         ln -s $DIR/$tdir-1/$tfile $TMP/$tfile
3327         lov_getstripe_old $TMP/$tfile || error "$TMP/$tfile: rc = $?"
3328
3329         # Create some bad symlinks and ensure that we don't loop
3330         # forever or something. These should return ELOOP (40) and
3331         # ENOENT (2) but I don't want to test for that because there's
3332         # always some weirdo architecture that needs to ruin
3333         # everything by defining these error numbers differently.
3334
3335         ln -s $TMP/$tfile.loop $TMP/$tfile.loop
3336         lov_getstripe_old $TMP/$tfile.loop && error "$TMP/$tfile.loop: rc = $?"
3337
3338         ln -s $TMP/$tfile.none $TMP/$tfile.broken
3339         lov_getstripe_old $TMP/$tfile.broken && error "$TMP/$tfile.broken: rc = $?"
3340
3341         return 0
3342 }
3343 run_test 27Q "llapi_file_get_stripe() works on symlinks"
3344
3345 test_27R() {
3346         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
3347                 skip "need MDS 2.14.55 or later"
3348         (( $OSTCOUNT >= 2 )) || skip_env "needs at least 2 OSTs"
3349
3350         local testdir="$DIR/$tdir"
3351         test_mkdir -p $testdir
3352         stack_trap "rm -rf $testdir"
3353         $LFS setstripe -c -1 $testdir || error "setstripe failed"
3354
3355         local f1="$testdir/f1"
3356         touch $f1 || error "failed to touch $f1"
3357         local count=$($LFS getstripe -c $f1)
3358         (( $count == $OSTCOUNT )) || error "wrong stripe count"
3359
3360         do_facet $SINGLEMDS $LCTL set_param lod.*.max_stripecount=-1
3361         (( $? == 34 )) || error "setting max_stripecount to -1 should fail and return ERANGE"
3362
3363         local maxcount=$(($OSTCOUNT - 1))
3364         local mdts=$(comma_list $(mdts_nodes))
3365         do_nodes $mdts $LCTL set_param lod.*.max_stripecount=$maxcount
3366         stack_trap "do_nodes $mdts $LCTL set_param lod.*.max_stripecount=0"
3367
3368         local f2="$testdir/f2"
3369         touch $f2 || error "failed to touch $f2"
3370         local count=$($LFS getstripe -c $f2)
3371         (( $count == $maxcount )) || error "wrong stripe count"
3372 }
3373 run_test 27R "test max_stripecount limitation when stripe count is set to -1"
3374
3375 test_27T() {
3376         [ $(facet_host client) == $(facet_host ost1) ] &&
3377                 skip "need ost1 and client on different nodes"
3378
3379 #define OBD_FAIL_OSC_NO_GRANT            0x411
3380         $LCTL set_param fail_loc=0x20000411 fail_val=1
3381 #define OBD_FAIL_OST_ENOSPC              0x215
3382         do_facet ost1 "$LCTL set_param fail_loc=0x80000215"
3383         $LFS setstripe -i 0 -c 1 $DIR/$tfile
3384         $MULTIOP $DIR/$tfile oO_WRONLY:P$((4 * 1024 * 1024 + 10 * 4096))c ||
3385                 error "multiop failed"
3386 }
3387 run_test 27T "no eio on close on partial write due to enosp"
3388
3389 test_27U() {
3390         local dir=$DIR/$tdir
3391         local file=$dir/$tfile
3392         local append_pool=${TESTNAME}-append
3393         local normal_pool=${TESTNAME}-normal
3394         local pool
3395         local stripe_count
3396         local stripe_count2
3397         local mdts=$(comma_list $(mdts_nodes))
3398
3399         # FIMXE
3400         # (( $MDS1_VERSION >= $(version_code 2.15.42) )) ||
3401         #       skip "Need MDS version at least 2.15.42"
3402
3403         # Validate existing append_* params and ensure restore
3404         pool=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_pool)
3405         [[ "$pool" == "" ]] || error "expected append_pool == '', got '$pool'"
3406         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool=none"
3407
3408         stripe_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
3409         ((stripe_count == 1)) || error "expected append_stripe_count != 0, got $stripe_count"
3410         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$stripe_count"
3411
3412         pool_add $append_pool || error "pool creation failed"
3413         pool_add_targets $append_pool 0 1 || error "Pool add targets failed"
3414
3415         pool_add $normal_pool || error "pool creation failed"
3416         pool_add_targets $normal_pool 0 1 || error "Pool add targets failed"
3417
3418         test_mkdir $dir
3419         $LFS setstripe -E 1M -c 1 -p $normal_pool -E 2M -c 2 -p $normal_pool -E eof -c -1 $dir
3420
3421         echo XXX >> $file.1
3422         $LFS getstripe $file.1
3423
3424         pool=$($LFS getstripe -p $file.1)
3425         [[ "$pool" == "$normal_pool" ]] || error "got pool '$pool', expected '$normal_pool'"
3426
3427         stripe_count2=$($LFS getstripe -c $file.1)
3428         ((stripe_count2 == stripe_count)) ||
3429                 error "got stripe_count '$stripe_count2', expected '$stripe_count'"
3430
3431         do_nodes $mdts $LCTL set_param mdd.*.append_pool=$append_pool
3432
3433         echo XXX >> $file.2
3434         $LFS getstripe $file.2
3435
3436         pool=$($LFS getstripe -p $file.2)
3437         [[ "$pool" == "$append_pool" ]] || error "got pool '$pool', expected '$append_pool'"
3438
3439         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
3440
3441         echo XXX >> $file.3
3442         $LFS getstripe $file.3
3443
3444         stripe_count2=$($LFS getstripe -c $file.3)
3445         ((stripe_count2 == 2)) || error "got stripe_count '$stripe_count2', expected 2"
3446 }
3447 run_test 27U "append pool and stripe count work with composite default layout"
3448
3449 # createtest also checks that device nodes are created and
3450 # then visible correctly (#2091)
3451 test_28() { # bug 2091
3452         test_mkdir $DIR/d28
3453         $CREATETEST $DIR/d28/ct || error "createtest failed"
3454 }
3455 run_test 28 "create/mknod/mkdir with bad file types ============"
3456
3457 test_29() {
3458         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3459
3460         [ $MDS1_VERSION -ge $(version_code 2.14.51) ] && {
3461                 disable_opencache
3462                 stack_trap "restore_opencache"
3463         }
3464
3465         sync; sleep 1; sync # flush out any dirty pages from previous tests
3466         cancel_lru_locks
3467         test_mkdir $DIR/d29
3468         touch $DIR/d29/foo
3469         log 'first d29'
3470         ls -l $DIR/d29
3471
3472         declare -i LOCKCOUNTORIG=0
3473         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3474                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3475         done
3476         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3477
3478         declare -i LOCKUNUSEDCOUNTORIG=0
3479         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3480                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3481         done
3482
3483         log 'second d29'
3484         ls -l $DIR/d29
3485         log 'done'
3486
3487         declare -i LOCKCOUNTCURRENT=0
3488         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3489                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3490         done
3491
3492         declare -i LOCKUNUSEDCOUNTCURRENT=0
3493         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3494                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3495         done
3496
3497         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3498                 $LCTL set_param -n ldlm.dump_namespaces ""
3499                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3500                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3501                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3502                 return 2
3503         fi
3504         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3505                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3506                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3507                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3508                 return 3
3509         fi
3510 }
3511 run_test 29 "IT_GETATTR regression  ============================"
3512
3513 test_30a() { # was test_30
3514         cp $(which ls) $DIR || cp /bin/ls $DIR
3515         $DIR/ls / || error "Can't execute binary from lustre"
3516         rm $DIR/ls
3517 }
3518 run_test 30a "execute binary from Lustre (execve) =============="
3519
3520 test_30b() {
3521         cp `which ls` $DIR || cp /bin/ls $DIR
3522         chmod go+rx $DIR/ls
3523         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3524         rm $DIR/ls
3525 }
3526 run_test 30b "execute binary from Lustre as non-root ==========="
3527
3528 test_30c() { # b=22376
3529         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3530
3531         cp $(which ls) $DIR || cp /bin/ls $DIR
3532         chmod a-rw $DIR/ls
3533         cancel_lru_locks mdc
3534         cancel_lru_locks osc
3535         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3536         rm -f $DIR/ls
3537 }
3538 run_test 30c "execute binary from Lustre without read perms ===="
3539
3540 test_30d() {
3541         cp $(which dd) $DIR || error "failed to copy dd to $DIR/dd"
3542
3543         for i in {1..10}; do
3544                 $DIR/dd bs=1M count=128 if=/dev/zero of=$DIR/$tfile &
3545                 local PID=$!
3546                 sleep 1
3547                 $LCTL set_param ldlm.namespaces.*MDT*.lru_size=clear
3548                 wait $PID || error "executing dd from Lustre failed"
3549                 rm -f $DIR/$tfile
3550         done
3551
3552         rm -f $DIR/dd
3553 }
3554 run_test 30d "execute binary from Lustre while clear locks"
3555
3556 test_31a() {
3557         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3558         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3559 }
3560 run_test 31a "open-unlink file =================================="
3561
3562 test_31b() {
3563         touch $DIR/f31 || error "touch $DIR/f31 failed"
3564         ln $DIR/f31 $DIR/f31b || error "ln failed"
3565         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3566         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3567 }
3568 run_test 31b "unlink file with multiple links while open ======="
3569
3570 test_31c() {
3571         touch $DIR/f31 || error "touch $DIR/f31 failed"
3572         ln $DIR/f31 $DIR/f31c || error "ln failed"
3573         multiop_bg_pause $DIR/f31 O_uc ||
3574                 error "multiop_bg_pause for $DIR/f31 failed"
3575         MULTIPID=$!
3576         $MULTIOP $DIR/f31c Ouc
3577         kill -USR1 $MULTIPID
3578         wait $MULTIPID
3579 }
3580 run_test 31c "open-unlink file with multiple links ============="
3581
3582 test_31d() {
3583         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3584         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3585 }
3586 run_test 31d "remove of open directory ========================="
3587
3588 test_31e() { # bug 2904
3589         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3590 }
3591 run_test 31e "remove of open non-empty directory ==============="
3592
3593 test_31f() { # bug 4554
3594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3595
3596         set -vx
3597         test_mkdir $DIR/d31f
3598         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3599         cp /etc/hosts $DIR/d31f
3600         ls -l $DIR/d31f
3601         $LFS getstripe $DIR/d31f/hosts
3602         multiop_bg_pause $DIR/d31f D_c || return 1
3603         MULTIPID=$!
3604
3605         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3606         test_mkdir $DIR/d31f
3607         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3608         cp /etc/hosts $DIR/d31f
3609         ls -l $DIR/d31f
3610         $LFS getstripe $DIR/d31f/hosts
3611         multiop_bg_pause $DIR/d31f D_c || return 1
3612         MULTIPID2=$!
3613
3614         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3615         wait $MULTIPID || error "first opendir $MULTIPID failed"
3616
3617         sleep 6
3618
3619         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3620         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3621         set +vx
3622 }
3623 run_test 31f "remove of open directory with open-unlink file ==="
3624
3625 test_31g() {
3626         echo "-- cross directory link --"
3627         test_mkdir -c1 $DIR/${tdir}ga
3628         test_mkdir -c1 $DIR/${tdir}gb
3629         touch $DIR/${tdir}ga/f
3630         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3631         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3632         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3633         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3634         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3635 }
3636 run_test 31g "cross directory link==============="
3637
3638 test_31h() {
3639         echo "-- cross directory link --"
3640         test_mkdir -c1 $DIR/${tdir}
3641         test_mkdir -c1 $DIR/${tdir}/dir
3642         touch $DIR/${tdir}/f
3643         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3644         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3645         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3646         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3647         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3648 }
3649 run_test 31h "cross directory link under child==============="
3650
3651 test_31i() {
3652         echo "-- cross directory link --"
3653         test_mkdir -c1 $DIR/$tdir
3654         test_mkdir -c1 $DIR/$tdir/dir
3655         touch $DIR/$tdir/dir/f
3656         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3657         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3658         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3659         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3660         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3661 }
3662 run_test 31i "cross directory link under parent==============="
3663
3664 test_31j() {
3665         test_mkdir -c1 -p $DIR/$tdir
3666         test_mkdir -c1 -p $DIR/$tdir/dir1
3667         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3668         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3669         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3670         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3671         return 0
3672 }
3673 run_test 31j "link for directory==============="
3674
3675 test_31k() {
3676         test_mkdir -c1 -p $DIR/$tdir
3677         touch $DIR/$tdir/s
3678         touch $DIR/$tdir/exist
3679         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3680         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3681         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3682         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3683         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3684         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3685         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3686         return 0
3687 }
3688 run_test 31k "link to file: the same, non-existing, dir==============="
3689
3690 test_31m() {
3691         mkdir $DIR/d31m
3692         touch $DIR/d31m/s
3693         mkdir $DIR/d31m2
3694         touch $DIR/d31m2/exist
3695         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3696         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3697         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3698         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3699         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3700         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3701         return 0
3702 }
3703 run_test 31m "link to file: the same, non-existing, dir==============="
3704
3705 test_31n() {
3706         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3707         nlink=$(stat --format=%h $DIR/$tfile)
3708         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3709         local fd=$(free_fd)
3710         local cmd="exec $fd<$DIR/$tfile"
3711         eval $cmd
3712         cmd="exec $fd<&-"
3713         trap "eval $cmd" EXIT
3714         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3715         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3716         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3717         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3718         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3719         eval $cmd
3720 }
3721 run_test 31n "check link count of unlinked file"
3722
3723 link_one() {
3724         local tempfile=$(mktemp $1_XXXXXX)
3725         mlink $tempfile $1 2> /dev/null &&
3726                 echo "$BASHPID: link $tempfile to $1 succeeded"
3727         munlink $tempfile
3728 }
3729
3730 test_31o() { # LU-2901
3731         test_mkdir $DIR/$tdir
3732         for LOOP in $(seq 100); do
3733                 rm -f $DIR/$tdir/$tfile*
3734                 for THREAD in $(seq 8); do
3735                         link_one $DIR/$tdir/$tfile.$LOOP &
3736                 done
3737                 wait
3738                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3739                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3740                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3741                         break || true
3742         done
3743 }
3744 run_test 31o "duplicate hard links with same filename"
3745
3746 test_31p() {
3747         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3748
3749         test_mkdir $DIR/$tdir
3750         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3751         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3752
3753         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3754                 error "open unlink test1 failed"
3755         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3756                 error "open unlink test2 failed"
3757
3758         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3759                 error "test1 still exists"
3760         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3761                 error "test2 still exists"
3762 }
3763 run_test 31p "remove of open striped directory"
3764
3765 test_31q() {
3766         [ $MDSCOUNT -lt 3 ] && skip_env "needs >= 3 MDTs"
3767
3768         $LFS mkdir -i 3,1 $DIR/$tdir || error "mkdir failed"
3769         index=$($LFS getdirstripe -i $DIR/$tdir)
3770         [ $index -eq 3 ] || error "first stripe index $index != 3"
3771         index=$($LFS getdirstripe $DIR/$tdir | tail -1 | awk '{print $1}')
3772         [ $index -eq 1 ] || error "second stripe index $index != 1"
3773
3774         # when "-c <stripe_count>" is set, the number of MDTs specified after
3775         # "-i" should equal to the stripe count
3776         $LFS mkdir -i 3,1 -c 3 $DIR/$tdir.2 && error "mkdir should fail" || true
3777 }
3778 run_test 31q "create striped directory on specific MDTs"
3779
3780 #LU-14949
3781 test_31r() {
3782         touch $DIR/$tfile.target
3783         touch $DIR/$tfile.source
3784
3785         #OBD_FAIL_LLITE_OPEN_DELAY 0x1419
3786         $LCTL set_param fail_loc=0x1419 fail_val=3
3787         cat $DIR/$tfile.target &
3788         CATPID=$!
3789
3790         # Guarantee open is waiting before we get here
3791         sleep 1
3792         mv $DIR/$tfile.source $DIR/$tfile.target
3793
3794         wait $CATPID
3795         RC=$?
3796         if [[ $RC -ne 0 ]]; then
3797                 error "open with cat failed, rc=$RC"
3798         fi
3799 }
3800 run_test 31r "open-rename(replace) race"
3801
3802 cleanup_test32_mount() {
3803         local rc=0
3804         trap 0
3805         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3806         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3807         losetup -d $loopdev || true
3808         rm -rf $DIR/$tdir
3809         return $rc
3810 }
3811
3812 test_32a() {
3813         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3814
3815         echo "== more mountpoints and symlinks ================="
3816         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3817         trap cleanup_test32_mount EXIT
3818         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3819         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3820                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3821         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3822                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3823         cleanup_test32_mount
3824 }
3825 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3826
3827 test_32b() {
3828         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3829
3830         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3831         trap cleanup_test32_mount EXIT
3832         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3833         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3834                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3835         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3836                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3837         cleanup_test32_mount
3838 }
3839 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3840
3841 test_32c() {
3842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3843
3844         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3845         trap cleanup_test32_mount EXIT
3846         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3847         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3848                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3849         test_mkdir -p $DIR/$tdir/d2/test_dir
3850         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3851                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3852         cleanup_test32_mount
3853 }
3854 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3855
3856 test_32d() {
3857         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3858
3859         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3860         trap cleanup_test32_mount EXIT
3861         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3862         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3863                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3864         test_mkdir -p $DIR/$tdir/d2/test_dir
3865         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3866                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3867         cleanup_test32_mount
3868 }
3869 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3870
3871 test_32e() {
3872         rm -fr $DIR/$tdir
3873         test_mkdir -p $DIR/$tdir/tmp
3874         local tmp_dir=$DIR/$tdir/tmp
3875         ln -s $DIR/$tdir $tmp_dir/symlink11
3876         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3877         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3878         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3879 }
3880 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3881
3882 test_32f() {
3883         rm -fr $DIR/$tdir
3884         test_mkdir -p $DIR/$tdir/tmp
3885         local tmp_dir=$DIR/$tdir/tmp
3886         ln -s $DIR/$tdir $tmp_dir/symlink11
3887         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3888         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3889         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3890 }
3891 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3892
3893 test_32g() {
3894         local tmp_dir=$DIR/$tdir/tmp
3895         test_mkdir -p $tmp_dir
3896         test_mkdir $DIR/${tdir}2
3897         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3898         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3899         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3900         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3901         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3902         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3903 }
3904 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3905
3906 test_32h() {
3907         rm -fr $DIR/$tdir $DIR/${tdir}2
3908         tmp_dir=$DIR/$tdir/tmp
3909         test_mkdir -p $tmp_dir
3910         test_mkdir $DIR/${tdir}2
3911         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3912         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3913         ls $tmp_dir/symlink12 || error "listing symlink12"
3914         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3915 }
3916 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3917
3918 test_32i() {
3919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3920
3921         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3922         trap cleanup_test32_mount EXIT
3923         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3924         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3925                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3926         touch $DIR/$tdir/test_file
3927         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3928                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3929         cleanup_test32_mount
3930 }
3931 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3932
3933 test_32j() {
3934         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3935
3936         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3937         trap cleanup_test32_mount EXIT
3938         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3939         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3940                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3941         touch $DIR/$tdir/test_file
3942         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3943                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3944         cleanup_test32_mount
3945 }
3946 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3947
3948 test_32k() {
3949         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3950
3951         rm -fr $DIR/$tdir
3952         trap cleanup_test32_mount EXIT
3953         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3954         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3955                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3956         test_mkdir -p $DIR/$tdir/d2
3957         touch $DIR/$tdir/d2/test_file || error "touch failed"
3958         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3959                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3960         cleanup_test32_mount
3961 }
3962 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3963
3964 test_32l() {
3965         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3966
3967         rm -fr $DIR/$tdir
3968         trap cleanup_test32_mount EXIT
3969         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3970         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3971                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3972         test_mkdir -p $DIR/$tdir/d2
3973         touch $DIR/$tdir/d2/test_file || error "touch failed"
3974         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3975                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3976         cleanup_test32_mount
3977 }
3978 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3979
3980 test_32m() {
3981         rm -fr $DIR/d32m
3982         test_mkdir -p $DIR/d32m/tmp
3983         TMP_DIR=$DIR/d32m/tmp
3984         ln -s $DIR $TMP_DIR/symlink11
3985         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3986         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3987                 error "symlink11 not a link"
3988         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3989                 error "symlink01 not a link"
3990 }
3991 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3992
3993 test_32n() {
3994         rm -fr $DIR/d32n
3995         test_mkdir -p $DIR/d32n/tmp
3996         TMP_DIR=$DIR/d32n/tmp
3997         ln -s $DIR $TMP_DIR/symlink11
3998         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3999         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
4000         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
4001 }
4002 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
4003
4004 test_32o() {
4005         touch $DIR/$tfile
4006         test_mkdir -p $DIR/d32o/tmp
4007         TMP_DIR=$DIR/d32o/tmp
4008         ln -s $DIR/$tfile $TMP_DIR/symlink12
4009         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
4010         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
4011                 error "symlink12 not a link"
4012         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
4013         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
4014                 error "$DIR/d32o/tmp/symlink12 not file type"
4015         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
4016                 error "$DIR/d32o/symlink02 not file type"
4017 }
4018 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
4019
4020 test_32p() {
4021         log 32p_1
4022         rm -fr $DIR/d32p
4023         log 32p_2
4024         rm -f $DIR/$tfile
4025         log 32p_3
4026         touch $DIR/$tfile
4027         log 32p_4
4028         test_mkdir -p $DIR/d32p/tmp
4029         log 32p_5
4030         TMP_DIR=$DIR/d32p/tmp
4031         log 32p_6
4032         ln -s $DIR/$tfile $TMP_DIR/symlink12
4033         log 32p_7
4034         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
4035         log 32p_8
4036         cat $DIR/d32p/tmp/symlink12 ||
4037                 error "Can't open $DIR/d32p/tmp/symlink12"
4038         log 32p_9
4039         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
4040         log 32p_10
4041 }
4042 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
4043
4044 test_32q() {
4045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4046
4047         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
4048         trap cleanup_test32_mount EXIT
4049         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4050         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
4051         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4052                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4053         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
4054         cleanup_test32_mount
4055 }
4056 run_test 32q "stat follows mountpoints in Lustre (should return error)"
4057
4058 test_32r() {
4059         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4060
4061         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
4062         trap cleanup_test32_mount EXIT
4063         test_mkdir -p $DIR/$tdir/ext2-mountpoint
4064         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
4065         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
4066                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
4067         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
4068         cleanup_test32_mount
4069 }
4070 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
4071
4072 test_33aa() {
4073         rm -f $DIR/$tfile
4074         touch $DIR/$tfile
4075         chmod 444 $DIR/$tfile
4076         chown $RUNAS_ID $DIR/$tfile
4077         log 33_1
4078         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4079         log 33_2
4080 }
4081 run_test 33aa "write file with mode 444 (should return error)"
4082
4083 test_33a() {
4084         rm -fr $DIR/$tdir
4085         test_mkdir $DIR/$tdir
4086         chown $RUNAS_ID $DIR/$tdir
4087         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
4088                 error "$RUNAS create $tdir/$tfile failed"
4089         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
4090                 error "open RDWR" || true
4091 }
4092 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
4093
4094 test_33b() {
4095         rm -fr $DIR/$tdir
4096         test_mkdir $DIR/$tdir
4097         chown $RUNAS_ID $DIR/$tdir
4098         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
4099 }
4100 run_test 33b "test open file with malformed flags (No panic)"
4101
4102 test_33c() {
4103         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4104         remote_ost_nodsh && skip "remote OST with nodsh"
4105
4106         local ostnum
4107         local ostname
4108         local write_bytes
4109         local all_zeros
4110
4111         all_zeros=true
4112         test_mkdir $DIR/$tdir
4113         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
4114
4115         sync
4116         for ostnum in $(seq $OSTCOUNT); do
4117                 # test-framework's OST numbering is one-based, while Lustre's
4118                 # is zero-based
4119                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4120                 # check if at least some write_bytes stats are counted
4121                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4122                               obdfilter.$ostname.stats |
4123                               awk '/^write_bytes/ {print $7}' )
4124                 echo "baseline_write_bytes@ost$ostnum/$ostname=$write_bytes"
4125                 if (( ${write_bytes:-0} > 0 )); then
4126                         all_zeros=false
4127                         break
4128                 fi
4129         done
4130
4131         $all_zeros || return 0
4132
4133         # Write four bytes
4134         echo foo > $DIR/$tdir/bar
4135         # Really write them
4136         sync
4137
4138         # Total up write_bytes after writing.  We'd better find non-zeros.
4139         for ostnum in $(seq $OSTCOUNT); do
4140                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4141                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
4142                               obdfilter/$ostname/stats |
4143                               awk '/^write_bytes/ {print $7}' )
4144                 echo "write_bytes@ost$ostnum/$ostname=$write_bytes"
4145                 if (( ${write_bytes:-0} > 0 )); then
4146                         all_zeros=false
4147                         break
4148                 fi
4149         done
4150
4151         if $all_zeros; then
4152                 for ostnum in $(seq $OSTCOUNT); do
4153                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
4154                         echo "Check write_bytes is in obdfilter.*.stats:"
4155                         do_facet ost$ostnum lctl get_param -n \
4156                                 obdfilter.$ostname.stats
4157                 done
4158                 error "OST not keeping write_bytes stats (b=22312)"
4159         fi
4160 }
4161 run_test 33c "test write_bytes stats"
4162
4163 test_33d() {
4164         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4166
4167         local MDTIDX=1
4168         local remote_dir=$DIR/$tdir/remote_dir
4169
4170         test_mkdir $DIR/$tdir
4171         $LFS mkdir -i $MDTIDX $remote_dir ||
4172                 error "create remote directory failed"
4173
4174         touch $remote_dir/$tfile
4175         chmod 444 $remote_dir/$tfile
4176         chown $RUNAS_ID $remote_dir/$tfile
4177
4178         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
4179
4180         chown $RUNAS_ID $remote_dir
4181         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
4182                                         error "create" || true
4183         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
4184                                     error "open RDWR" || true
4185         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
4186 }
4187 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
4188
4189 test_33e() {
4190         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4191
4192         mkdir $DIR/$tdir
4193
4194         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4195         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4196         mkdir $DIR/$tdir/local_dir
4197
4198         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4199         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4200         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4201
4202         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4203                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
4204
4205         rmdir $DIR/$tdir/* || error "rmdir failed"
4206
4207         umask 777
4208         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4209         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4210         mkdir $DIR/$tdir/local_dir
4211
4212         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4213         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4214         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4215
4216         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4217                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
4218
4219         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
4220
4221         umask 000
4222         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
4223         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
4224         mkdir $DIR/$tdir/local_dir
4225
4226         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
4227         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
4228         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
4229
4230         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
4231                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
4232 }
4233 run_test 33e "mkdir and striped directory should have same mode"
4234
4235 cleanup_33f() {
4236         trap 0
4237         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
4238 }
4239
4240 test_33f() {
4241         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4242         remote_mds_nodsh && skip "remote MDS with nodsh"
4243
4244         mkdir $DIR/$tdir
4245         chmod go+rwx $DIR/$tdir
4246         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
4247         trap cleanup_33f EXIT
4248
4249         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
4250                 error "cannot create striped directory"
4251
4252         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
4253                 error "cannot create files in striped directory"
4254
4255         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
4256                 error "cannot remove files in striped directory"
4257
4258         $RUNAS rmdir $DIR/$tdir/striped_dir ||
4259                 error "cannot remove striped directory"
4260
4261         cleanup_33f
4262 }
4263 run_test 33f "nonroot user can create, access, and remove a striped directory"
4264
4265 test_33g() {
4266         mkdir -p $DIR/$tdir/dir2
4267
4268         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
4269         echo $err
4270         [[ $err =~ "exists" ]] || error "Not exists error"
4271 }
4272 run_test 33g "nonroot user create already existing root created file"
4273
4274 sub_33h() {
4275         local hash_type=$1
4276         local count=250
4277
4278         test_mkdir -c $MDSCOUNT -H $hash_type $DIR/$tdir ||
4279                 error "lfs mkdir -H $hash_type $tdir failed"
4280         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
4281
4282         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
4283         local index2
4284         local fname
4285
4286         for fname in $DIR/$tdir/$tfile.bak \
4287                      $DIR/$tdir/$tfile.SAV \
4288                      $DIR/$tdir/$tfile.orig \
4289                      $DIR/$tdir/$tfile~; do
4290                 touch $fname || error "touch $fname failed"
4291                 index2=$($LFS getstripe -m $fname)
4292                 (( $index == $index2 )) ||
4293                         error "$fname MDT index mismatch $index != $index2"
4294         done
4295
4296         local failed=0
4297         local patterns=(".$tfile.XXXXXX" "$tfile.XXXXXXXX")
4298         local pattern
4299
4300         for pattern in ${patterns[*]}; do
4301                 echo "pattern $pattern"
4302                 fname=$DIR/$tdir/$pattern
4303                 for (( i = 0; i < $count; i++ )); do
4304                         fname=$(mktemp $DIR/$tdir/$pattern) ||
4305                                 error "mktemp $DIR/$tdir/$pattern failed"
4306                         index2=$($LFS getstripe -m $fname)
4307                         (( $index == $index2 )) && continue
4308
4309                         failed=$((failed + 1))
4310                         echo "$fname MDT index mismatch $index != $index2"
4311                 done
4312         done
4313
4314         echo "$failed/$count MDT index mismatches, expect ~2-4"
4315         (( failed < 10 )) || error "MDT index mismatch $failed/$count times"
4316
4317         local same=0
4318         local expect
4319
4320         # verify that "crush" is still broken with all files on same MDT,
4321         # crush2 should have about 1/MDSCOUNT files on each MDT, with margin
4322         [[ "$hash_type" == "crush" ]] && expect=$count ||
4323                 expect=$((count / MDSCOUNT))
4324
4325         # crush2 doesn't put all-numeric suffixes on the same MDT,
4326         # filename like $tfile.12345678 should *not* be considered temp
4327         for pattern in ${patterns[*]}; do
4328                 local base=${pattern%%X*}
4329                 local suff=${pattern#$base}
4330
4331                 echo "pattern $pattern"
4332                 for (( i = 0; i < $count; i++ )); do
4333                         fname=$DIR/$tdir/$base$((${suff//X/1} + i))
4334                         touch $fname || error "touch $fname failed"
4335                         index2=$($LFS getstripe -m $fname)
4336                         (( $index != $index2 )) && continue
4337
4338                         same=$((same + 1))
4339                 done
4340         done
4341
4342         echo "$((same/${#patterns[*]}))/$count matches, expect ~$expect for $1"
4343         (( same / ${#patterns[*]} < expect * 5 / 4 &&
4344            same / ${#patterns[*]} > expect * 4 / 5 )) ||
4345                 error "MDT index match $((same / ${#patterns[*]}))/$count times"
4346         same=0
4347
4348         # crush2 doesn't put suffixes with special characters on the same MDT
4349         # filename like $tfile.txt.1234 should *not* be considered temp
4350         for pattern in ${patterns[*]}; do
4351                 local base=${pattern%%X*}
4352                 local suff=${pattern#$base}
4353
4354                 pattern=$base...${suff/XXX}
4355                 echo "pattern=$pattern"
4356                 for (( i = 0; i < $count; i++ )); do
4357                         fname=$(mktemp $DIR/$tdir/$pattern) ||
4358                                 error "touch $fname failed"
4359                         index2=$($LFS getstripe -m $fname)
4360                         (( $index != $index2 )) && continue
4361
4362                         same=$((same + 1))
4363                 done
4364         done
4365
4366         echo "$((same/${#patterns[*]}))/$count matches, expect ~$expect for $1"
4367         (( same / ${#patterns[*]} < expect * 5 / 4 &&
4368            same / ${#patterns[*]} > expect * 4 / 5 )) ||
4369                 error "MDT index match $((same / ${#patterns[*]}))/$count times"
4370 }
4371
4372 test_33h() {
4373         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4374         (( $MDS1_VERSION >= $(version_code 2.13.50) )) ||
4375                 skip "Need MDS version at least 2.13.50"
4376
4377         sub_33h crush
4378 }
4379 run_test 33h "temp file is located on the same MDT as target (crush)"
4380
4381 test_33hh() {
4382         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4383         echo "MDS1_VERSION=$MDS1_VERSION version_code=$(version_code 2.15.0)"
4384         (( $MDS1_VERSION > $(version_code 2.15.0) )) ||
4385                 skip "Need MDS version at least 2.15.0 for crush2"
4386
4387         sub_33h crush2
4388 }
4389 run_test 33hh "temp file is located on the same MDT as target (crush2)"
4390
4391 test_33i()
4392 {
4393         (( MDSCOUNT < 2 )) && skip "needs >= 2 MDTs"
4394
4395         local FNAME=$(str_repeat 'f' 250)
4396
4397         test_mkdir -i 0 -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
4398         createmany -o $DIR/$tdir/$FNAME 1000 || error "createmany failed"
4399
4400         local count
4401         local total
4402
4403         count=$($LFS getstripe -m $DIR/$tdir/* | grep -cw 1)
4404
4405         local MDC=$(lctl dl | awk '/MDT0001-mdc-[^M]/ { print $4 }')
4406
4407         lctl --device %$MDC deactivate
4408         stack_trap "lctl --device %$MDC activate"
4409         ls $DIR/$tdir > /dev/null && error "ls should return an error"
4410         total=$(\ls -l $DIR/$tdir | wc -l)
4411         # "ls -l" will list total in the first line
4412         total=$((total - 1))
4413         (( total + count == 1000 )) ||
4414                 error "ls list $total files, $count files on MDT1"
4415 }
4416 run_test 33i "striped directory can be accessed when one MDT is down"
4417
4418 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
4419 test_34a() {
4420         rm -f $DIR/f34
4421         $MCREATE $DIR/f34 || error "mcreate failed"
4422         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4423                 error "getstripe failed"
4424         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
4425         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4426                 error "getstripe failed"
4427         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4428                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4429 }
4430 run_test 34a "truncate file that has not been opened ==========="
4431
4432 test_34b() {
4433         [ ! -f $DIR/f34 ] && test_34a
4434         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4435                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4436         $OPENFILE -f O_RDONLY $DIR/f34
4437         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4438                 error "getstripe failed"
4439         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4440                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4441 }
4442 run_test 34b "O_RDONLY opening file doesn't create objects ====="
4443
4444 test_34c() {
4445         [ ! -f $DIR/f34 ] && test_34a
4446         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4447                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4448         $OPENFILE -f O_RDWR $DIR/f34
4449         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
4450                 error "$LFS getstripe failed"
4451         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4452                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4453 }
4454 run_test 34c "O_RDWR opening file-with-size works =============="
4455
4456 test_34d() {
4457         [ ! -f $DIR/f34 ] && test_34a
4458         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
4459                 error "dd failed"
4460         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4461                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4462         rm $DIR/f34
4463 }
4464 run_test 34d "write to sparse file ============================="
4465
4466 test_34e() {
4467         rm -f $DIR/f34e
4468         $MCREATE $DIR/f34e || error "mcreate failed"
4469         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
4470         $CHECKSTAT -s 1000 $DIR/f34e ||
4471                 error "Size of $DIR/f34e not equal to 1000 bytes"
4472         $OPENFILE -f O_RDWR $DIR/f34e
4473         $CHECKSTAT -s 1000 $DIR/f34e ||
4474                 error "Size of $DIR/f34e not equal to 1000 bytes"
4475 }
4476 run_test 34e "create objects, some with size and some without =="
4477
4478 test_34f() { # bug 6242, 6243
4479         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4480
4481         SIZE34F=48000
4482         rm -f $DIR/f34f
4483         $MCREATE $DIR/f34f || error "mcreate failed"
4484         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
4485         dd if=$DIR/f34f of=$TMP/f34f
4486         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
4487         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
4488         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
4489         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
4490         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
4491 }
4492 run_test 34f "read from a file with no objects until EOF ======="
4493
4494 test_34g() {
4495         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4496
4497         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
4498                 error "dd failed"
4499         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
4500         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4501                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
4502         cancel_lru_locks osc
4503         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4504                 error "wrong size after lock cancel"
4505
4506         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
4507         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4508                 error "expanding truncate failed"
4509         cancel_lru_locks osc
4510         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4511                 error "wrong expanded size after lock cancel"
4512 }
4513 run_test 34g "truncate long file ==============================="
4514
4515 test_34h() {
4516         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4517
4518         local gid=10
4519         local sz=1000
4520
4521         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
4522         sync # Flush the cache so that multiop below does not block on cache
4523              # flush when getting the group lock
4524         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
4525         MULTIPID=$!
4526
4527         # Since just timed wait is not good enough, let's do a sync write
4528         # that way we are sure enough time for a roundtrip + processing
4529         # passed + 2 seconds of extra margin.
4530         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
4531         rm $DIR/${tfile}-1
4532         sleep 2
4533
4534         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
4535                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
4536                 kill -9 $MULTIPID
4537         fi
4538         wait $MULTIPID
4539         local nsz=`stat -c %s $DIR/$tfile`
4540         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
4541 }
4542 run_test 34h "ftruncate file under grouplock should not block"
4543
4544 test_35a() {
4545         cp /bin/sh $DIR/f35a
4546         chmod 444 $DIR/f35a
4547         chown $RUNAS_ID $DIR/f35a
4548         $RUNAS $DIR/f35a && error || true
4549         rm $DIR/f35a
4550 }
4551 run_test 35a "exec file with mode 444 (should return and not leak)"
4552
4553 test_36a() {
4554         rm -f $DIR/f36
4555         utime $DIR/f36 || error "utime failed for MDS"
4556 }
4557 run_test 36a "MDS utime check (mknod, utime)"
4558
4559 test_36b() {
4560         echo "" > $DIR/f36
4561         utime $DIR/f36 || error "utime failed for OST"
4562 }
4563 run_test 36b "OST utime check (open, utime)"
4564
4565 test_36c() {
4566         rm -f $DIR/d36/f36
4567         test_mkdir $DIR/d36
4568         chown $RUNAS_ID $DIR/d36
4569         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
4570 }
4571 run_test 36c "non-root MDS utime check (mknod, utime)"
4572
4573 test_36d() {
4574         [ ! -d $DIR/d36 ] && test_36c
4575         echo "" > $DIR/d36/f36
4576         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
4577 }
4578 run_test 36d "non-root OST utime check (open, utime)"
4579
4580 test_36e() {
4581         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
4582
4583         test_mkdir $DIR/$tdir
4584         touch $DIR/$tdir/$tfile
4585         $RUNAS utime $DIR/$tdir/$tfile &&
4586                 error "utime worked, expected failure" || true
4587 }
4588 run_test 36e "utime on non-owned file (should return error)"
4589
4590 subr_36fh() {
4591         local fl="$1"
4592         local LANG_SAVE=$LANG
4593         local LC_LANG_SAVE=$LC_LANG
4594         export LANG=C LC_LANG=C # for date language
4595
4596         DATESTR="Dec 20  2000"
4597         test_mkdir $DIR/$tdir
4598         lctl set_param fail_loc=$fl
4599         date; date +%s
4600         cp /etc/hosts $DIR/$tdir/$tfile
4601         sync & # write RPC generated with "current" inode timestamp, but delayed
4602         sleep 1
4603         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4604         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4605         cancel_lru_locks $OSC
4606         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4607         date; date +%s
4608         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4609                 echo "BEFORE: $LS_BEFORE" && \
4610                 echo "AFTER : $LS_AFTER" && \
4611                 echo "WANT  : $DATESTR" && \
4612                 error "$DIR/$tdir/$tfile timestamps changed" || true
4613
4614         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4615 }
4616
4617 test_36f() {
4618         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4619
4620         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4621         subr_36fh "0x80000214"
4622 }
4623 run_test 36f "utime on file racing with OST BRW write =========="
4624
4625 test_36g() {
4626         remote_ost_nodsh && skip "remote OST with nodsh"
4627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4628         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4629                 skip "Need MDS version at least 2.12.51"
4630
4631         local fmd_max_age
4632         local fmd
4633         local facet="ost1"
4634         local tgt="obdfilter"
4635
4636         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4637
4638         test_mkdir $DIR/$tdir
4639         fmd_max_age=$(do_facet $facet \
4640                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4641                 head -n 1")
4642
4643         echo "FMD max age: ${fmd_max_age}s"
4644         touch $DIR/$tdir/$tfile
4645         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4646                 gawk '{cnt=cnt+$1}  END{print cnt}')
4647         echo "FMD before: $fmd"
4648         [[ $fmd == 0 ]] &&
4649                 error "FMD wasn't create by touch"
4650         sleep $((fmd_max_age + 12))
4651         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4652                 gawk '{cnt=cnt+$1}  END{print cnt}')
4653         echo "FMD after: $fmd"
4654         [[ $fmd == 0 ]] ||
4655                 error "FMD wasn't expired by ping"
4656 }
4657 run_test 36g "FMD cache expiry ====================="
4658
4659 test_36h() {
4660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4661
4662         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4663         subr_36fh "0x80000227"
4664 }
4665 run_test 36h "utime on file racing with OST BRW write =========="
4666
4667 test_36i() {
4668         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4669
4670         test_mkdir $DIR/$tdir
4671         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4672
4673         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4674         local new_mtime=$((mtime + 200))
4675
4676         #change Modify time of striped dir
4677         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4678                         error "change mtime failed"
4679
4680         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4681
4682         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4683 }
4684 run_test 36i "change mtime on striped directory"
4685
4686 # test_37 - duplicate with tests 32q 32r
4687
4688 test_38() {
4689         local file=$DIR/$tfile
4690         touch $file
4691         openfile -f O_DIRECTORY $file
4692         local RC=$?
4693         local ENOTDIR=20
4694         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4695         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4696 }
4697 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4698
4699 test_39a() { # was test_39
4700         touch $DIR/$tfile
4701         touch $DIR/${tfile}2
4702 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4703 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4704 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4705         sleep 2
4706         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4707         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4708                 echo "mtime"
4709                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4710                 echo "atime"
4711                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4712                 echo "ctime"
4713                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4714                 error "O_TRUNC didn't change timestamps"
4715         fi
4716 }
4717 run_test 39a "mtime changed on create"
4718
4719 test_39b() {
4720         test_mkdir -c1 $DIR/$tdir
4721         cp -p /etc/passwd $DIR/$tdir/fopen
4722         cp -p /etc/passwd $DIR/$tdir/flink
4723         cp -p /etc/passwd $DIR/$tdir/funlink
4724         cp -p /etc/passwd $DIR/$tdir/frename
4725         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4726
4727         sleep 1
4728         echo "aaaaaa" >> $DIR/$tdir/fopen
4729         echo "aaaaaa" >> $DIR/$tdir/flink
4730         echo "aaaaaa" >> $DIR/$tdir/funlink
4731         echo "aaaaaa" >> $DIR/$tdir/frename
4732
4733         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4734         local link_new=`stat -c %Y $DIR/$tdir/flink`
4735         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4736         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4737
4738         cat $DIR/$tdir/fopen > /dev/null
4739         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4740         rm -f $DIR/$tdir/funlink2
4741         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4742
4743         for (( i=0; i < 2; i++ )) ; do
4744                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4745                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4746                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4747                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4748
4749                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4750                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4751                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4752                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4753
4754                 cancel_lru_locks $OSC
4755                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4756         done
4757 }
4758 run_test 39b "mtime change on open, link, unlink, rename  ======"
4759
4760 # this should be set to past
4761 TEST_39_MTIME=`date -d "1 year ago" +%s`
4762
4763 # bug 11063
4764 test_39c() {
4765         touch $DIR1/$tfile
4766         sleep 2
4767         local mtime0=`stat -c %Y $DIR1/$tfile`
4768
4769         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4770         local mtime1=`stat -c %Y $DIR1/$tfile`
4771         [ "$mtime1" = $TEST_39_MTIME ] || \
4772                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4773
4774         local d1=`date +%s`
4775         echo hello >> $DIR1/$tfile
4776         local d2=`date +%s`
4777         local mtime2=`stat -c %Y $DIR1/$tfile`
4778         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4779                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4780
4781         mv $DIR1/$tfile $DIR1/$tfile-1
4782
4783         for (( i=0; i < 2; i++ )) ; do
4784                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4785                 [ "$mtime2" = "$mtime3" ] || \
4786                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4787
4788                 cancel_lru_locks $OSC
4789                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4790         done
4791 }
4792 run_test 39c "mtime change on rename ==========================="
4793
4794 # bug 21114
4795 test_39d() {
4796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4797
4798         touch $DIR1/$tfile
4799         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4800
4801         for (( i=0; i < 2; i++ )) ; do
4802                 local mtime=`stat -c %Y $DIR1/$tfile`
4803                 [ $mtime = $TEST_39_MTIME ] || \
4804                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4805
4806                 cancel_lru_locks $OSC
4807                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4808         done
4809 }
4810 run_test 39d "create, utime, stat =============================="
4811
4812 # bug 21114
4813 test_39e() {
4814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4815
4816         touch $DIR1/$tfile
4817         local mtime1=`stat -c %Y $DIR1/$tfile`
4818
4819         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4820
4821         for (( i=0; i < 2; i++ )) ; do
4822                 local mtime2=`stat -c %Y $DIR1/$tfile`
4823                 [ $mtime2 = $TEST_39_MTIME ] || \
4824                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4825
4826                 cancel_lru_locks $OSC
4827                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4828         done
4829 }
4830 run_test 39e "create, stat, utime, stat ========================"
4831
4832 # bug 21114
4833 test_39f() {
4834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4835
4836         touch $DIR1/$tfile
4837         mtime1=`stat -c %Y $DIR1/$tfile`
4838
4839         sleep 2
4840         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4841
4842         for (( i=0; i < 2; i++ )) ; do
4843                 local mtime2=`stat -c %Y $DIR1/$tfile`
4844                 [ $mtime2 = $TEST_39_MTIME ] || \
4845                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4846
4847                 cancel_lru_locks $OSC
4848                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4849         done
4850 }
4851 run_test 39f "create, stat, sleep, utime, stat ================="
4852
4853 # bug 11063
4854 test_39g() {
4855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4856
4857         echo hello >> $DIR1/$tfile
4858         local mtime1=`stat -c %Y $DIR1/$tfile`
4859
4860         sleep 2
4861         chmod o+r $DIR1/$tfile
4862
4863         for (( i=0; i < 2; i++ )) ; do
4864                 local mtime2=`stat -c %Y $DIR1/$tfile`
4865                 [ "$mtime1" = "$mtime2" ] || \
4866                         error "lost mtime: $mtime2, should be $mtime1"
4867
4868                 cancel_lru_locks $OSC
4869                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4870         done
4871 }
4872 run_test 39g "write, chmod, stat ==============================="
4873
4874 # bug 11063
4875 test_39h() {
4876         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4877
4878         touch $DIR1/$tfile
4879         sleep 1
4880
4881         local d1=`date`
4882         echo hello >> $DIR1/$tfile
4883         local mtime1=`stat -c %Y $DIR1/$tfile`
4884
4885         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4886         local d2=`date`
4887         if [ "$d1" != "$d2" ]; then
4888                 echo "write and touch not within one second"
4889         else
4890                 for (( i=0; i < 2; i++ )) ; do
4891                         local mtime2=`stat -c %Y $DIR1/$tfile`
4892                         [ "$mtime2" = $TEST_39_MTIME ] || \
4893                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4894
4895                         cancel_lru_locks $OSC
4896                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4897                 done
4898         fi
4899 }
4900 run_test 39h "write, utime within one second, stat ============="
4901
4902 test_39i() {
4903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4904
4905         touch $DIR1/$tfile
4906         sleep 1
4907
4908         echo hello >> $DIR1/$tfile
4909         local mtime1=`stat -c %Y $DIR1/$tfile`
4910
4911         mv $DIR1/$tfile $DIR1/$tfile-1
4912
4913         for (( i=0; i < 2; i++ )) ; do
4914                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4915
4916                 [ "$mtime1" = "$mtime2" ] || \
4917                         error "lost mtime: $mtime2, should be $mtime1"
4918
4919                 cancel_lru_locks $OSC
4920                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4921         done
4922 }
4923 run_test 39i "write, rename, stat =============================="
4924
4925 test_39j() {
4926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4927
4928         start_full_debug_logging
4929         touch $DIR1/$tfile
4930         sleep 1
4931
4932         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4933         lctl set_param fail_loc=0x80000412
4934         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4935                 error "multiop failed"
4936         local multipid=$!
4937         local mtime1=`stat -c %Y $DIR1/$tfile`
4938
4939         mv $DIR1/$tfile $DIR1/$tfile-1
4940
4941         kill -USR1 $multipid
4942         wait $multipid || error "multiop close failed"
4943
4944         for (( i=0; i < 2; i++ )) ; do
4945                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4946                 [ "$mtime1" = "$mtime2" ] ||
4947                         error "mtime is lost on close: $mtime2, " \
4948                               "should be $mtime1"
4949
4950                 cancel_lru_locks
4951                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4952         done
4953         lctl set_param fail_loc=0
4954         stop_full_debug_logging
4955 }
4956 run_test 39j "write, rename, close, stat ======================="
4957
4958 test_39k() {
4959         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4960
4961         touch $DIR1/$tfile
4962         sleep 1
4963
4964         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4965         local multipid=$!
4966         local mtime1=`stat -c %Y $DIR1/$tfile`
4967
4968         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4969
4970         kill -USR1 $multipid
4971         wait $multipid || error "multiop close failed"
4972
4973         for (( i=0; i < 2; i++ )) ; do
4974                 local mtime2=`stat -c %Y $DIR1/$tfile`
4975
4976                 [ "$mtime2" = $TEST_39_MTIME ] || \
4977                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4978
4979                 cancel_lru_locks
4980                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4981         done
4982 }
4983 run_test 39k "write, utime, close, stat ========================"
4984
4985 # this should be set to future
4986 TEST_39_ATIME=`date -d "1 year" +%s`
4987
4988 test_39l() {
4989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4990         remote_mds_nodsh && skip "remote MDS with nodsh"
4991
4992         local atime_diff=$(do_facet $SINGLEMDS \
4993                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4994         rm -rf $DIR/$tdir
4995         mkdir_on_mdt0 $DIR/$tdir
4996
4997         # test setting directory atime to future
4998         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4999         local atime=$(stat -c %X $DIR/$tdir)
5000         [ "$atime" = $TEST_39_ATIME ] ||
5001                 error "atime is not set to future: $atime, $TEST_39_ATIME"
5002
5003         # test setting directory atime from future to now
5004         local now=$(date +%s)
5005         touch -a -d @$now $DIR/$tdir
5006
5007         atime=$(stat -c %X $DIR/$tdir)
5008         [ "$atime" -eq "$now"  ] ||
5009                 error "atime is not updated from future: $atime, $now"
5010
5011         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
5012         sleep 3
5013
5014         # test setting directory atime when now > dir atime + atime_diff
5015         local d1=$(date +%s)
5016         ls $DIR/$tdir
5017         local d2=$(date +%s)
5018         cancel_lru_locks mdc
5019         atime=$(stat -c %X $DIR/$tdir)
5020         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
5021                 error "atime is not updated  : $atime, should be $d2"
5022
5023         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
5024         sleep 3
5025
5026         # test not setting directory atime when now < dir atime + atime_diff
5027         ls $DIR/$tdir
5028         cancel_lru_locks mdc
5029         atime=$(stat -c %X $DIR/$tdir)
5030         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
5031                 error "atime is updated to $atime, should remain $d1<atime<$d2"
5032
5033         do_facet $SINGLEMDS \
5034                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
5035 }
5036 run_test 39l "directory atime update ==========================="
5037
5038 test_39m() {
5039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5040
5041         touch $DIR1/$tfile
5042         sleep 2
5043         local far_past_mtime=$(date -d "May 29 1953" +%s)
5044         local far_past_atime=$(date -d "Dec 17 1903" +%s)
5045
5046         touch -m -d @$far_past_mtime $DIR1/$tfile
5047         touch -a -d @$far_past_atime $DIR1/$tfile
5048
5049         for (( i=0; i < 2; i++ )) ; do
5050                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
5051                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
5052                         error "atime or mtime set incorrectly"
5053
5054                 cancel_lru_locks $OSC
5055                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
5056         done
5057 }
5058 run_test 39m "test atime and mtime before 1970"
5059
5060 test_39n() { # LU-3832
5061         remote_mds_nodsh && skip "remote MDS with nodsh"
5062
5063         local atime_diff=$(do_facet $SINGLEMDS \
5064                 lctl get_param -n mdd.*MDT0000*.atime_diff)
5065         local atime0
5066         local atime1
5067         local atime2
5068
5069         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
5070
5071         rm -rf $DIR/$tfile
5072         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
5073         atime0=$(stat -c %X $DIR/$tfile)
5074
5075         sleep 5
5076         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
5077         atime1=$(stat -c %X $DIR/$tfile)
5078
5079         sleep 5
5080         cancel_lru_locks mdc
5081         cancel_lru_locks osc
5082         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
5083         atime2=$(stat -c %X $DIR/$tfile)
5084
5085         do_facet $SINGLEMDS \
5086                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
5087
5088         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
5089         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
5090 }
5091 run_test 39n "check that O_NOATIME is honored"
5092
5093 test_39o() {
5094         TESTDIR=$DIR/$tdir/$tfile
5095         [ -e $TESTDIR ] && rm -rf $TESTDIR
5096         mkdir -p $TESTDIR
5097         cd $TESTDIR
5098         links1=2
5099         ls
5100         mkdir a b
5101         ls
5102         links2=$(stat -c %h .)
5103         [ $(($links1 + 2)) != $links2 ] &&
5104                 error "wrong links count $(($links1 + 2)) != $links2"
5105         rmdir b
5106         links3=$(stat -c %h .)
5107         [ $(($links1 + 1)) != $links3 ] &&
5108                 error "wrong links count $links1 != $links3"
5109         return 0
5110 }
5111 run_test 39o "directory cached attributes updated after create"
5112
5113 test_39p() {
5114         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
5115
5116         local MDTIDX=1
5117         TESTDIR=$DIR/$tdir/$tdir
5118         [ -e $TESTDIR ] && rm -rf $TESTDIR
5119         test_mkdir -p $TESTDIR
5120         cd $TESTDIR
5121         links1=2
5122         ls
5123         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
5124         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
5125         ls
5126         links2=$(stat -c %h .)
5127         [ $(($links1 + 2)) != $links2 ] &&
5128                 error "wrong links count $(($links1 + 2)) != $links2"
5129         rmdir remote_dir2
5130         links3=$(stat -c %h .)
5131         [ $(($links1 + 1)) != $links3 ] &&
5132                 error "wrong links count $links1 != $links3"
5133         return 0
5134 }
5135 run_test 39p "remote directory cached attributes updated after create ========"
5136
5137 test_39r() {
5138         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
5139                 skip "no atime update on old OST"
5140         if [ "$ost1_FSTYPE" != ldiskfs ]; then
5141                 skip_env "ldiskfs only test"
5142         fi
5143
5144         local saved_adiff
5145         saved_adiff=$(do_facet ost1 \
5146                 lctl get_param -n obdfilter.*OST0000.atime_diff)
5147         stack_trap "do_facet ost1 \
5148                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
5149
5150         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
5151
5152         $LFS setstripe -i 0 $DIR/$tfile
5153         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
5154                 error "can't write initial file"
5155         cancel_lru_locks osc
5156
5157         # exceed atime_diff and access file
5158         sleep 10
5159         dd if=$DIR/$tfile of=/dev/null bs=4k count=1 ||
5160                 error "can't udpate atime"
5161
5162         local atime_cli=$(stat -c %X $DIR/$tfile)
5163         echo "client atime: $atime_cli"
5164         # allow atime update to be written to device
5165         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
5166         sleep 5
5167
5168         local ostdev=$(ostdevname 1)
5169         local fid=($(lfs getstripe -y $DIR/$tfile |
5170                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
5171         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
5172         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
5173
5174         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
5175         local atime_ost=$(do_facet ost1 "$cmd" |&
5176                           awk -F'[: ]' '/atime:/ { print $4 }')
5177         (( atime_cli == atime_ost )) ||
5178                 error "atime on client $atime_cli != ost $atime_ost"
5179 }
5180 run_test 39r "lazy atime update on OST"
5181
5182 test_39q() { # LU-8041
5183         local testdir=$DIR/$tdir
5184         mkdir -p $testdir
5185         multiop_bg_pause $testdir D_c || error "multiop failed"
5186         local multipid=$!
5187         cancel_lru_locks mdc
5188         kill -USR1 $multipid
5189         local atime=$(stat -c %X $testdir)
5190         [ "$atime" -ne 0 ] || error "atime is zero"
5191 }
5192 run_test 39q "close won't zero out atime"
5193
5194 test_40() {
5195         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
5196         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
5197                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
5198         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
5199                 error "$tfile is not 4096 bytes in size"
5200 }
5201 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
5202
5203 test_41() {
5204         # bug 1553
5205         small_write $DIR/f41 18
5206 }
5207 run_test 41 "test small file write + fstat ====================="
5208
5209 count_ost_writes() {
5210         lctl get_param -n ${OSC}.*.stats |
5211                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
5212                         END { printf("%0.0f", writes) }'
5213 }
5214
5215 # decent default
5216 WRITEBACK_SAVE=500
5217 DIRTY_RATIO_SAVE=40
5218 MAX_DIRTY_RATIO=50
5219 BG_DIRTY_RATIO_SAVE=10
5220 MAX_BG_DIRTY_RATIO=25
5221
5222 start_writeback() {
5223         trap 0
5224         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
5225         # dirty_ratio, dirty_background_ratio
5226         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5227                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
5228                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
5229                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
5230         else
5231                 # if file not here, we are a 2.4 kernel
5232                 kill -CONT `pidof kupdated`
5233         fi
5234 }
5235
5236 stop_writeback() {
5237         # setup the trap first, so someone cannot exit the test at the
5238         # exact wrong time and mess up a machine
5239         trap start_writeback EXIT
5240         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
5241         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5242                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
5243                 sysctl -w vm.dirty_writeback_centisecs=0
5244                 sysctl -w vm.dirty_writeback_centisecs=0
5245                 # save and increase /proc/sys/vm/dirty_ratio
5246                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
5247                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
5248                 # save and increase /proc/sys/vm/dirty_background_ratio
5249                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
5250                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
5251         else
5252                 # if file not here, we are a 2.4 kernel
5253                 kill -STOP `pidof kupdated`
5254         fi
5255 }
5256
5257 # ensure that all stripes have some grant before we test client-side cache
5258 setup_test42() {
5259         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
5260                 dd if=/dev/zero of=$i bs=4k count=1
5261                 rm $i
5262         done
5263 }
5264
5265 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
5266 # file truncation, and file removal.
5267 test_42a() {
5268         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5269
5270         setup_test42
5271         cancel_lru_locks $OSC
5272         stop_writeback
5273         sync; sleep 1; sync # just to be safe
5274         BEFOREWRITES=`count_ost_writes`
5275         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
5276         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
5277         AFTERWRITES=`count_ost_writes`
5278         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
5279                 error "$BEFOREWRITES < $AFTERWRITES"
5280         start_writeback
5281 }
5282 run_test 42a "ensure that we don't flush on close"
5283
5284 test_42b() {
5285         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5286
5287         setup_test42
5288         cancel_lru_locks $OSC
5289         stop_writeback
5290         sync
5291         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
5292         BEFOREWRITES=$(count_ost_writes)
5293         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
5294         AFTERWRITES=$(count_ost_writes)
5295         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5296                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
5297         fi
5298         BEFOREWRITES=$(count_ost_writes)
5299         sync || error "sync: $?"
5300         AFTERWRITES=$(count_ost_writes)
5301         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5302                 error "$BEFOREWRITES < $AFTERWRITES on sync"
5303         fi
5304         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
5305         start_writeback
5306         return 0
5307 }
5308 run_test 42b "test destroy of file with cached dirty data ======"
5309
5310 # if these tests just want to test the effect of truncation,
5311 # they have to be very careful.  consider:
5312 # - the first open gets a {0,EOF}PR lock
5313 # - the first write conflicts and gets a {0, count-1}PW
5314 # - the rest of the writes are under {count,EOF}PW
5315 # - the open for truncate tries to match a {0,EOF}PR
5316 #   for the filesize and cancels the PWs.
5317 # any number of fixes (don't get {0,EOF} on open, match
5318 # composite locks, do smarter file size management) fix
5319 # this, but for now we want these tests to verify that
5320 # the cancellation with truncate intent works, so we
5321 # start the file with a full-file pw lock to match against
5322 # until the truncate.
5323 trunc_test() {
5324         test=$1
5325         file=$DIR/$test
5326         offset=$2
5327         cancel_lru_locks $OSC
5328         stop_writeback
5329         # prime the file with 0,EOF PW to match
5330         touch $file
5331         $TRUNCATE $file 0
5332         sync; sync
5333         # now the real test..
5334         dd if=/dev/zero of=$file bs=1024 count=100
5335         BEFOREWRITES=`count_ost_writes`
5336         $TRUNCATE $file $offset
5337         cancel_lru_locks $OSC
5338         AFTERWRITES=`count_ost_writes`
5339         start_writeback
5340 }
5341
5342 test_42c() {
5343         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5344
5345         trunc_test 42c 1024
5346         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
5347                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
5348         rm $file
5349 }
5350 run_test 42c "test partial truncate of file with cached dirty data"
5351
5352 test_42d() {
5353         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5354
5355         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
5356         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
5357         $LCTL set_param debug=+cache
5358
5359         trunc_test 42d 0
5360         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
5361                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
5362         rm $file
5363 }
5364 run_test 42d "test complete truncate of file with cached dirty data"
5365
5366 test_42e() { # bug22074
5367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5368
5369         local TDIR=$DIR/${tdir}e
5370         local pages=16 # hardcoded 16 pages, don't change it.
5371         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
5372         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
5373         local max_dirty_mb
5374         local warmup_files
5375
5376         test_mkdir $DIR/${tdir}e
5377         $LFS setstripe -c 1 $TDIR
5378         createmany -o $TDIR/f $files
5379
5380         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
5381
5382         # we assume that with $OSTCOUNT files, at least one of them will
5383         # be allocated on OST0.
5384         warmup_files=$((OSTCOUNT * max_dirty_mb))
5385         createmany -o $TDIR/w $warmup_files
5386
5387         # write a large amount of data into one file and sync, to get good
5388         # avail_grant number from OST.
5389         for ((i=0; i<$warmup_files; i++)); do
5390                 idx=$($LFS getstripe -i $TDIR/w$i)
5391                 [ $idx -ne 0 ] && continue
5392                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
5393                 break
5394         done
5395         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
5396         sync
5397         $LCTL get_param $proc_osc0/cur_dirty_bytes
5398         $LCTL get_param $proc_osc0/cur_grant_bytes
5399
5400         # create as much dirty pages as we can while not to trigger the actual
5401         # RPCs directly. but depends on the env, VFS may trigger flush during this
5402         # period, hopefully we are good.
5403         for ((i=0; i<$warmup_files; i++)); do
5404                 idx=$($LFS getstripe -i $TDIR/w$i)
5405                 [ $idx -ne 0 ] && continue
5406                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
5407         done
5408         $LCTL get_param $proc_osc0/cur_dirty_bytes
5409         $LCTL get_param $proc_osc0/cur_grant_bytes
5410
5411         # perform the real test
5412         $LCTL set_param $proc_osc0/rpc_stats 0
5413         for ((;i<$files; i++)); do
5414                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
5415                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
5416         done
5417         sync
5418         $LCTL get_param $proc_osc0/rpc_stats
5419
5420         local percent=0
5421         local have_ppr=false
5422         $LCTL get_param $proc_osc0/rpc_stats |
5423                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
5424                         # skip lines until we are at the RPC histogram data
5425                         [ "$PPR" == "pages" ] && have_ppr=true && continue
5426                         $have_ppr || continue
5427
5428                         # we only want the percent stat for < 16 pages
5429                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
5430
5431                         percent=$((percent + WPCT))
5432                         if [[ $percent -gt 15 ]]; then
5433                                 error "less than 16-pages write RPCs" \
5434                                       "$percent% > 15%"
5435                                 break
5436                         fi
5437                 done
5438         rm -rf $TDIR
5439 }
5440 run_test 42e "verify sub-RPC writes are not done synchronously"
5441
5442 test_43A() { # was test_43
5443         test_mkdir $DIR/$tdir
5444         cp -p /bin/ls $DIR/$tdir/$tfile
5445         $MULTIOP $DIR/$tdir/$tfile Ow_c &
5446         pid=$!
5447         # give multiop a chance to open
5448         sleep 1
5449
5450         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
5451         kill -USR1 $pid
5452         # Wait for multiop to exit
5453         wait $pid
5454 }
5455 run_test 43A "execution of file opened for write should return -ETXTBSY"
5456
5457 test_43a() {
5458         test_mkdir $DIR/$tdir
5459         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5460         $DIR/$tdir/sleep 60 &
5461         SLEEP_PID=$!
5462         # Make sure exec of $tdir/sleep wins race with truncate
5463         sleep 1
5464         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
5465         kill $SLEEP_PID
5466 }
5467 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
5468
5469 test_43b() {
5470         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5471
5472         test_mkdir $DIR/$tdir
5473         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5474         $DIR/$tdir/sleep 60 &
5475         SLEEP_PID=$!
5476         # Make sure exec of $tdir/sleep wins race with truncate
5477         sleep 1
5478         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
5479         kill $SLEEP_PID
5480 }
5481 run_test 43b "truncate of file being executed should return -ETXTBSY"
5482
5483 test_43c() {
5484         local testdir="$DIR/$tdir"
5485         test_mkdir $testdir
5486         cp $SHELL $testdir/
5487         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
5488                 ( cd $testdir && md5sum -c )
5489 }
5490 run_test 43c "md5sum of copy into lustre"
5491
5492 test_44A() { # was test_44
5493         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5494
5495         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
5496         dd if=$DIR/f1 bs=4k count=1 > /dev/null
5497 }
5498 run_test 44A "zero length read from a sparse stripe"
5499
5500 test_44a() {
5501         local nstripe=$($LFS getstripe -c -d $DIR)
5502         [ -z "$nstripe" ] && skip "can't get stripe info"
5503         [[ $nstripe -gt $OSTCOUNT ]] &&
5504                 skip "Wrong default stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
5505
5506         local stride=$($LFS getstripe -S -d $DIR)
5507         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
5508                 nstripe=$($LFS df $DIR | grep OST: | wc -l)
5509         fi
5510
5511         OFFSETS="0 $((stride/2)) $((stride-1))"
5512         for offset in $OFFSETS; do
5513                 for i in $(seq 0 $((nstripe-1))); do
5514                         local GLOBALOFFSETS=""
5515                         # size in Bytes
5516                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
5517                         local myfn=$DIR/d44a-$size
5518                         echo "--------writing $myfn at $size"
5519                         ll_sparseness_write $myfn $size ||
5520                                 error "ll_sparseness_write"
5521                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
5522                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5523                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5524
5525                         for j in $(seq 0 $((nstripe-1))); do
5526                                 # size in Bytes
5527                                 size=$((((j + $nstripe )*$stride + $offset)))
5528                                 ll_sparseness_write $myfn $size ||
5529                                         error "ll_sparseness_write"
5530                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
5531                         done
5532                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5533                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5534                         rm -f $myfn
5535                 done
5536         done
5537 }
5538 run_test 44a "test sparse pwrite ==============================="
5539
5540 dirty_osc_total() {
5541         tot=0
5542         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
5543                 tot=$(($tot + $d))
5544         done
5545         echo $tot
5546 }
5547 do_dirty_record() {
5548         before=`dirty_osc_total`
5549         echo executing "\"$*\""
5550         eval $*
5551         after=`dirty_osc_total`
5552         echo before $before, after $after
5553 }
5554 test_45() {
5555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5556
5557         f="$DIR/f45"
5558         # Obtain grants from OST if it supports it
5559         echo blah > ${f}_grant
5560         stop_writeback
5561         sync
5562         do_dirty_record "echo blah > $f"
5563         [[ $before -eq $after ]] && error "write wasn't cached"
5564         do_dirty_record "> $f"
5565         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
5566         do_dirty_record "echo blah > $f"
5567         [[ $before -eq $after ]] && error "write wasn't cached"
5568         do_dirty_record "sync"
5569         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
5570         do_dirty_record "echo blah > $f"
5571         [[ $before -eq $after ]] && error "write wasn't cached"
5572         do_dirty_record "cancel_lru_locks osc"
5573         [[ $before -gt $after ]] ||
5574                 error "lock cancellation didn't lower dirty count"
5575         start_writeback
5576 }
5577 run_test 45 "osc io page accounting ============================"
5578
5579 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
5580 # test tickles a bug where re-dirtying a page was failing to be mapped to the
5581 # objects offset and an assert hit when an rpc was built with 1023's mapped
5582 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
5583 test_46() {
5584         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5585
5586         f="$DIR/f46"
5587         stop_writeback
5588         sync
5589         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5590         sync
5591         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5592         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5593         sync
5594         start_writeback
5595 }
5596 run_test 46 "dirtying a previously written page ================"
5597
5598 # test_47 is removed "Device nodes check" is moved to test_28
5599
5600 test_48a() { # bug 2399
5601         [ "$mds1_FSTYPE" = "zfs" ] &&
5602         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5603                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5604
5605         test_mkdir $DIR/$tdir
5606         cd $DIR/$tdir
5607         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5608         test_mkdir $DIR/$tdir
5609         touch foo || error "'touch foo' failed after recreating cwd"
5610         test_mkdir bar
5611         touch .foo || error "'touch .foo' failed after recreating cwd"
5612         test_mkdir .bar
5613         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5614         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5615         cd . || error "'cd .' failed after recreating cwd"
5616         mkdir . && error "'mkdir .' worked after recreating cwd"
5617         rmdir . && error "'rmdir .' worked after recreating cwd"
5618         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5619         cd .. || error "'cd ..' failed after recreating cwd"
5620 }
5621 run_test 48a "Access renamed working dir (should return errors)="
5622
5623 test_48b() { # bug 2399
5624         rm -rf $DIR/$tdir
5625         test_mkdir $DIR/$tdir
5626         cd $DIR/$tdir
5627         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5628         touch foo && error "'touch foo' worked after removing cwd"
5629         mkdir foo && error "'mkdir foo' worked after removing cwd"
5630         touch .foo && error "'touch .foo' worked after removing cwd"
5631         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5632         ls . > /dev/null && error "'ls .' worked after removing cwd"
5633         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5634         mkdir . && error "'mkdir .' worked after removing cwd"
5635         rmdir . && error "'rmdir .' worked after removing cwd"
5636         ln -s . foo && error "'ln -s .' worked after removing cwd"
5637         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5638 }
5639 run_test 48b "Access removed working dir (should return errors)="
5640
5641 test_48c() { # bug 2350
5642         #lctl set_param debug=-1
5643         #set -vx
5644         rm -rf $DIR/$tdir
5645         test_mkdir -p $DIR/$tdir/dir
5646         cd $DIR/$tdir/dir
5647         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5648         $TRACE touch foo && error "touch foo worked after removing cwd"
5649         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5650         touch .foo && error "touch .foo worked after removing cwd"
5651         mkdir .foo && error "mkdir .foo worked after removing cwd"
5652         $TRACE ls . && error "'ls .' worked after removing cwd"
5653         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5654         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5655         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5656         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5657         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5658 }
5659 run_test 48c "Access removed working subdir (should return errors)"
5660
5661 test_48d() { # bug 2350
5662         #lctl set_param debug=-1
5663         #set -vx
5664         rm -rf $DIR/$tdir
5665         test_mkdir -p $DIR/$tdir/dir
5666         cd $DIR/$tdir/dir
5667         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5668         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5669         $TRACE touch foo && error "'touch foo' worked after removing parent"
5670         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5671         touch .foo && error "'touch .foo' worked after removing parent"
5672         mkdir .foo && error "mkdir .foo worked after removing parent"
5673         $TRACE ls . && error "'ls .' worked after removing parent"
5674         $TRACE ls .. && error "'ls ..' worked after removing parent"
5675         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5676         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5677         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5678         true
5679 }
5680 run_test 48d "Access removed parent subdir (should return errors)"
5681
5682 test_48e() { # bug 4134
5683         #lctl set_param debug=-1
5684         #set -vx
5685         rm -rf $DIR/$tdir
5686         test_mkdir -p $DIR/$tdir/dir
5687         cd $DIR/$tdir/dir
5688         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5689         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5690         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5691         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5692         # On a buggy kernel addition of "touch foo" after cd .. will
5693         # produce kernel oops in lookup_hash_it
5694         touch ../foo && error "'cd ..' worked after recreate parent"
5695         cd $DIR
5696         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5697 }
5698 run_test 48e "Access to recreated parent subdir (should return errors)"
5699
5700 test_48f() {
5701         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
5702                 skip "need MDS >= 2.13.55"
5703         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
5704         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] ||
5705                 skip "needs different host for mdt1 mdt2"
5706         [[ $(facet_fstype mds1) == ldiskfs ]] || skip "ldiskfs only"
5707
5708         $LFS mkdir -i0 $DIR/$tdir
5709         $LFS mkdir -i 1 $DIR/$tdir/sub1 $DIR/$tdir/sub2 $DIR/$tdir/sub3
5710
5711         for d in sub1 sub2 sub3; do
5712                 #define OBD_FAIL_OSD_REF_DEL    0x19c
5713                 do_facet mds1 $LCTL set_param fail_loc=0x8000019c
5714                 rm -rf $DIR/$tdir/$d && error "rm $d should fail"
5715         done
5716
5717         rm -d --interactive=never $DIR/$tdir || error "rm $tdir fail"
5718 }
5719 run_test 48f "non-zero nlink dir unlink won't LBUG()"
5720
5721 test_49() { # LU-1030
5722         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5723         remote_ost_nodsh && skip "remote OST with nodsh"
5724
5725         # get ost1 size - $FSNAME-OST0000
5726         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5727                 awk '{ print $4 }')
5728         # write 800M at maximum
5729         [[ $ost1_size -lt 2 ]] && ost1_size=2
5730         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5731
5732         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5733         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5734         local dd_pid=$!
5735
5736         # change max_pages_per_rpc while writing the file
5737         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5738         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5739         # loop until dd process exits
5740         while ps ax -opid | grep -wq $dd_pid; do
5741                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5742                 sleep $((RANDOM % 5 + 1))
5743         done
5744         # restore original max_pages_per_rpc
5745         $LCTL set_param $osc1_mppc=$orig_mppc
5746         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5747 }
5748 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5749
5750 test_50() {
5751         # bug 1485
5752         test_mkdir $DIR/$tdir
5753         cd $DIR/$tdir
5754         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5755 }
5756 run_test 50 "special situations: /proc symlinks  ==============="
5757
5758 test_51a() {    # was test_51
5759         # bug 1516 - create an empty entry right after ".." then split dir
5760         test_mkdir -c1 $DIR/$tdir
5761         touch $DIR/$tdir/foo
5762         $MCREATE $DIR/$tdir/bar
5763         rm $DIR/$tdir/foo
5764         createmany -m $DIR/$tdir/longfile 201
5765         FNUM=202
5766         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5767                 $MCREATE $DIR/$tdir/longfile$FNUM
5768                 FNUM=$(($FNUM + 1))
5769                 echo -n "+"
5770         done
5771         echo
5772         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5773 }
5774 run_test 51a "special situations: split htree with empty entry =="
5775
5776 cleanup_print_lfs_df () {
5777         trap 0
5778         $LFS df
5779         $LFS df -i
5780 }
5781
5782 test_51b() {
5783         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5784
5785         local dir=$DIR/$tdir
5786         local nrdirs=$((65536 + 100))
5787
5788         # cleanup the directory
5789         rm -fr $dir
5790
5791         mkdir_on_mdt -i $((RANDOM % MDSCOUNT)) $dir
5792
5793         $LFS df
5794         $LFS df -i
5795         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5796         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5797         [[ $numfree -lt $nrdirs ]] &&
5798                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5799
5800         # need to check free space for the directories as well
5801         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5802         numfree=$(( blkfree / $(fs_inode_ksize) ))
5803         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5804
5805         trap cleanup_print_lfs_df EXIT
5806
5807         # create files
5808         createmany -d $dir/d $nrdirs || {
5809                 unlinkmany $dir/d $nrdirs
5810                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5811         }
5812
5813         # really created :
5814         nrdirs=$(ls -U $dir | wc -l)
5815
5816         # unlink all but 100 subdirectories, then check it still works
5817         local left=100
5818         local delete=$((nrdirs - left))
5819
5820         $LFS df
5821         $LFS df -i
5822
5823         # for ldiskfs the nlink count should be 1, but this is OSD specific
5824         # and so this is listed for informational purposes only
5825         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5826         unlinkmany -d $dir/d $delete ||
5827                 error "unlink of first $delete subdirs failed"
5828
5829         echo "nlink between: $(stat -c %h $dir)"
5830         local found=$(ls -U $dir | wc -l)
5831         [ $found -ne $left ] &&
5832                 error "can't find subdirs: found only $found, expected $left"
5833
5834         unlinkmany -d $dir/d $delete $left ||
5835                 error "unlink of second $left subdirs failed"
5836         # regardless of whether the backing filesystem tracks nlink accurately
5837         # or not, the nlink count shouldn't be more than "." and ".." here
5838         local after=$(stat -c %h $dir)
5839         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5840                 echo "nlink after: $after"
5841
5842         cleanup_print_lfs_df
5843 }
5844 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5845
5846 test_51d_sub() {
5847         local stripecount=$1
5848         local nfiles=$2
5849
5850         log "create files with stripecount=$stripecount"
5851         $LFS setstripe -C $stripecount $DIR/$tdir
5852         createmany -o $DIR/$tdir/t- $nfiles
5853         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5854         for ((n = 0; n < $OSTCOUNT; n++)); do
5855                 objs[$n]=$(awk -vobjs=0 '($1 == '$n') { objs += 1 } \
5856                            END { printf("%0.0f", objs) }' $TMP/$tfile)
5857                 objs0[$n]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5858                             '($1 == '$n') { objs += 1 } \
5859                             END { printf("%0.0f", objs) }')
5860                 log "OST$n has ${objs[$n]} objects, ${objs0[$n]} are index 0"
5861         done
5862         unlinkmany $DIR/$tdir/t- $nfiles
5863         rm  -f $TMP/$tfile
5864
5865         local nlast
5866         local min=4
5867         local max=6 # allow variance of (1 - $min/$max) = 33% by default
5868
5869         # For some combinations of stripecount and OSTCOUNT current code
5870         # is not ideal, and allocates 50% fewer *first* objects to some OSTs
5871         # than others. Rather than skipping this test entirely, check that
5872         # and keep testing to ensure imbalance does not get worse. LU-15282
5873         (( (OSTCOUNT == 6 && stripecount == 4) ||
5874            (OSTCOUNT == 10 && (stripecount == 4 || stripecount == 8)) ||
5875            (OSTCOUNT == 12 && (stripecount == 8 || stripecount == 9)))) && max=9
5876         for ((nlast=0, n = 1; n < $OSTCOUNT; nlast=n,n++)); do
5877                 (( ${objs[$n]} > ${objs[$nlast]} * 4 / 5 )) ||
5878                         { $LFS df && $LFS df -i &&
5879                         error "stripecount=$stripecount: " \
5880                               "OST $n has fewer objects vs. OST $nlast " \
5881                               "(${objs[$n]} < ${objs[$nlast]} x 4/5)"; }
5882                 (( ${objs[$n]} < ${objs[$nlast]} * 5 / 4 )) ||
5883                         { $LFS df && $LFS df -i &&
5884                         error "stripecount=$stripecount: " \
5885                               "OST $n has more objects vs. OST $nlast " \
5886                               "(${objs[$n]} > ${objs[$nlast]} x 5/4)"; }
5887
5888                 (( ${objs0[$n]} > ${objs0[$nlast]} * $min / $max )) ||
5889                         { $LFS df && $LFS df -i &&
5890                         error "stripecount=$stripecount: " \
5891                               "OST $n has fewer #0 objects vs. OST $nlast " \
5892                               "(${objs0[$n]} < ${objs0[$nlast]} x $min/$max)"; }
5893                 (( ${objs0[$n]} < ${objs0[$nlast]} * $max / $min )) ||
5894                         { $LFS df && $LFS df -i &&
5895                         error "stripecount=$stripecount: " \
5896                               "OST $n has more #0 objects vs. OST $nlast " \
5897                               "(${objs0[$n]} > ${objs0[$nlast]} x $max/$min)"; }
5898         done
5899 }
5900
5901 test_51d() {
5902         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5903         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5904
5905         local stripecount
5906         local per_ost=100
5907         local nfiles=$((per_ost * OSTCOUNT))
5908         local mdts=$(comma_list $(mdts_nodes))
5909         local param="osp.*.create_count"
5910         local qos_old=$(do_facet mds1 \
5911                 "$LCTL get_param -n lod.$FSNAME-*.qos_threshold_rr" | head -n 1)
5912
5913         do_nodes $mdts \
5914                 "$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=100"
5915         stack_trap "do_nodes $mdts \
5916                 '$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=${qos_old%%%}'"
5917
5918         test_mkdir $DIR/$tdir
5919         local dirstripes=$(lfs getdirstripe -c $DIR/$tdir)
5920         (( dirstripes > 0 )) || dirstripes=1
5921
5922         # Ensure enough OST objects precreated for tests to pass without
5923         # running out of objects.  This is an LOV r-r OST algorithm test,
5924         # not an OST object precreation test.
5925         local old=$(do_facet mds1 "$LCTL get_param -n $param" | head -n 1)
5926         (( old >= nfiles )) ||
5927         {
5928                 local create_count=$((nfiles * OSTCOUNT / dirstripes))
5929
5930                 do_nodes $mdts "$LCTL set_param $param=$create_count"
5931                 stack_trap "do_nodes $mdts $LCTL set_param $param=$old"
5932
5933                 # trigger precreation from all MDTs for all OSTs
5934                 for ((i = 0; i < $MDSCOUNT * 2; i++ )); do
5935                         $LFS setstripe -c -1 $DIR/$tdir/wide.$i
5936                 done
5937         }
5938
5939         for ((stripecount = 3; stripecount <= $OSTCOUNT; stripecount++)); do
5940                 sleep 8  # allow object precreation to catch up
5941                 test_51d_sub $stripecount $nfiles
5942         done
5943 }
5944 run_test 51d "check LOV round-robin OST object distribution"
5945
5946 test_51e() {
5947         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5948                 skip_env "ldiskfs only test"
5949         fi
5950
5951         test_mkdir -c1 $DIR/$tdir
5952         test_mkdir -c1 $DIR/$tdir/d0
5953
5954         touch $DIR/$tdir/d0/foo
5955         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5956                 error "file exceed 65000 nlink limit!"
5957         unlinkmany $DIR/$tdir/d0/f- 65001
5958         return 0
5959 }
5960 run_test 51e "check file nlink limit"
5961
5962 test_51f() {
5963         test_mkdir $DIR/$tdir
5964
5965         local max=100000
5966         local ulimit_old=$(ulimit -n)
5967         local spare=20 # number of spare fd's for scripts/libraries, etc.
5968         local mdt=$($LFS getstripe -m $DIR/$tdir)
5969         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5970
5971         echo "MDT$mdt numfree=$numfree, max=$max"
5972         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5973         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5974                 while ! ulimit -n $((numfree + spare)); do
5975                         numfree=$((numfree * 3 / 4))
5976                 done
5977                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5978         else
5979                 echo "left ulimit at $ulimit_old"
5980         fi
5981
5982         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5983                 unlinkmany $DIR/$tdir/f $numfree
5984                 error "create+open $numfree files in $DIR/$tdir failed"
5985         }
5986         ulimit -n $ulimit_old
5987
5988         # if createmany exits at 120s there will be fewer than $numfree files
5989         unlinkmany $DIR/$tdir/f $numfree || true
5990 }
5991 run_test 51f "check many open files limit"
5992
5993 test_52a() {
5994         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5995         test_mkdir $DIR/$tdir
5996         touch $DIR/$tdir/foo
5997         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5998         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5999         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
6000         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
6001         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
6002                                         error "link worked"
6003         echo foo >> $DIR/$tdir/foo || error "append foo failed"
6004         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
6005         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
6006                                                      error "lsattr"
6007         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
6008         cp -r $DIR/$tdir $TMP/
6009         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
6010 }
6011 run_test 52a "append-only flag test (should return errors)"
6012
6013 test_52b() {
6014         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
6015         test_mkdir $DIR/$tdir
6016         touch $DIR/$tdir/foo
6017         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
6018         cat test > $DIR/$tdir/foo && error "cat test worked"
6019         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
6020         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
6021         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
6022                                         error "link worked"
6023         echo foo >> $DIR/$tdir/foo && error "echo worked"
6024         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
6025         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
6026         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
6027         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
6028                                                         error "lsattr"
6029         chattr -i $DIR/$tdir/foo || error "chattr failed"
6030
6031         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
6032 }
6033 run_test 52b "immutable flag test (should return errors) ======="
6034
6035 test_53() {
6036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6037         remote_mds_nodsh && skip "remote MDS with nodsh"
6038         remote_ost_nodsh && skip "remote OST with nodsh"
6039
6040         local param
6041         local param_seq
6042         local ostname
6043         local mds_last
6044         local mds_last_seq
6045         local ost_last
6046         local ost_last_seq
6047         local ost_last_id
6048         local ostnum
6049         local node
6050         local found=false
6051         local support_last_seq=true
6052
6053         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
6054                 support_last_seq=false
6055
6056         # only test MDT0000
6057         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
6058         local value
6059         for value in $(do_facet $SINGLEMDS \
6060                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
6061                 param=$(echo ${value[0]} | cut -d "=" -f1)
6062                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
6063
6064                 if $support_last_seq; then
6065                         param_seq=$(echo $param |
6066                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
6067                         mds_last_seq=$(do_facet $SINGLEMDS \
6068                                        $LCTL get_param -n $param_seq)
6069                 fi
6070                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
6071
6072                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
6073                 node=$(facet_active_host ost$((ostnum+1)))
6074                 param="obdfilter.$ostname.last_id"
6075                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
6076                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
6077                         ost_last_id=$ost_last
6078
6079                         if $support_last_seq; then
6080                                 ost_last_id=$(echo $ost_last |
6081                                               awk -F':' '{print $2}' |
6082                                               sed -e "s/^0x//g")
6083                                 ost_last_seq=$(echo $ost_last |
6084                                                awk -F':' '{print $1}')
6085                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
6086                         fi
6087
6088                         if [[ $ost_last_id != $mds_last ]]; then
6089                                 error "$ost_last_id != $mds_last"
6090                         else
6091                                 found=true
6092                                 break
6093                         fi
6094                 done
6095         done
6096         $found || error "can not match last_seq/last_id for $mdtosc"
6097         return 0
6098 }
6099 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
6100
6101 test_54a() {
6102         perl -MSocket -e ';' || skip "no Socket perl module installed"
6103
6104         $SOCKETSERVER $DIR/socket ||
6105                 error "$SOCKETSERVER $DIR/socket failed: $?"
6106         $SOCKETCLIENT $DIR/socket ||
6107                 error "$SOCKETCLIENT $DIR/socket failed: $?"
6108         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
6109 }
6110 run_test 54a "unix domain socket test =========================="
6111
6112 test_54b() {
6113         f="$DIR/f54b"
6114         mknod $f c 1 3
6115         chmod 0666 $f
6116         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
6117 }
6118 run_test 54b "char device works in lustre ======================"
6119
6120 find_loop_dev() {
6121         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
6122         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
6123         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
6124
6125         for i in $(seq 3 7); do
6126                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
6127                 LOOPDEV=$LOOPBASE$i
6128                 LOOPNUM=$i
6129                 break
6130         done
6131 }
6132
6133 cleanup_54c() {
6134         local rc=0
6135         loopdev="$DIR/loop54c"
6136
6137         trap 0
6138         $UMOUNT $DIR/$tdir || rc=$?
6139         losetup -d $loopdev || true
6140         losetup -d $LOOPDEV || true
6141         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
6142         return $rc
6143 }
6144
6145 test_54c() {
6146         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6147
6148         loopdev="$DIR/loop54c"
6149
6150         find_loop_dev
6151         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
6152         trap cleanup_54c EXIT
6153         mknod $loopdev b 7 $LOOPNUM
6154         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
6155         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
6156         losetup $loopdev $DIR/$tfile ||
6157                 error "can't set up $loopdev for $DIR/$tfile"
6158         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
6159         test_mkdir $DIR/$tdir
6160         mount -t ext2 $loopdev $DIR/$tdir ||
6161                 error "error mounting $loopdev on $DIR/$tdir"
6162         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
6163                 error "dd write"
6164         df $DIR/$tdir
6165         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
6166                 error "dd read"
6167         cleanup_54c
6168 }
6169 run_test 54c "block device works in lustre ====================="
6170
6171 test_54d() {
6172         local pipe="$DIR/$tfile.pipe"
6173         local string="aaaaaa"
6174
6175         mknod $pipe p
6176         echo -n "$string" > $pipe &
6177         local result=$(cat $pipe)
6178         [[ "$result" == "$string" ]] || error "$result != $string"
6179 }
6180 run_test 54d "fifo device works in lustre ======================"
6181
6182 test_54e() {
6183         f="$DIR/f54e"
6184         string="aaaaaa"
6185         cp -aL /dev/console $f
6186         echo $string > $f || error "echo $string to $f failed"
6187 }
6188 run_test 54e "console/tty device works in lustre ======================"
6189
6190 test_56a() {
6191         local numfiles=3
6192         local numdirs=2
6193         local dir=$DIR/$tdir
6194
6195         rm -rf $dir
6196         test_mkdir -p $dir/dir
6197         for i in $(seq $numfiles); do
6198                 touch $dir/file$i
6199                 touch $dir/dir/file$i
6200         done
6201
6202         local numcomp=$($LFS getstripe --component-count $dir)
6203
6204         [[ $numcomp == 0 ]] && numcomp=1
6205
6206         # test lfs getstripe with --recursive
6207         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
6208
6209         [[ $filenum -eq $((numfiles * 2)) ]] ||
6210                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
6211         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
6212         [[ $filenum -eq $numfiles ]] ||
6213                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
6214         echo "$LFS getstripe showed obdidx or l_ost_idx"
6215
6216         # test lfs getstripe with file instead of dir
6217         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
6218         [[ $filenum -eq 1 ]] ||
6219                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
6220         echo "$LFS getstripe file1 passed"
6221
6222         #test lfs getstripe with --verbose
6223         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
6224         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
6225                 error "$LFS getstripe --verbose $dir: "\
6226                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
6227         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
6228                 error "$LFS getstripe $dir: showed lmm_magic"
6229
6230         #test lfs getstripe with -v prints lmm_fid
6231         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
6232         local countfids=$((numdirs + numfiles * numcomp))
6233         [[ $filenum -eq $countfids ]] ||
6234                 error "$LFS getstripe -v $dir: "\
6235                       "got $filenum want $countfids lmm_fid"
6236         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
6237                 error "$LFS getstripe $dir: showed lmm_fid by default"
6238         echo "$LFS getstripe --verbose passed"
6239
6240         #check for FID information
6241         local fid1=$($LFS getstripe --fid $dir/file1)
6242         local fid2=$($LFS getstripe --verbose $dir/file1 |
6243                      awk '/lmm_fid: / { print $2; exit; }')
6244         local fid3=$($LFS path2fid $dir/file1)
6245
6246         [ "$fid1" != "$fid2" ] &&
6247                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
6248         [ "$fid1" != "$fid3" ] &&
6249                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
6250         echo "$LFS getstripe --fid passed"
6251
6252         #test lfs getstripe with --obd
6253         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
6254                 error "$LFS getstripe --obd wrong_uuid: should return error"
6255
6256         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6257
6258         local ostidx=1
6259         local obduuid=$(ostuuid_from_index $ostidx)
6260         local found=$($LFS getstripe -r --obd $obduuid $dir |
6261                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
6262
6263         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
6264         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
6265                 ((filenum--))
6266         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
6267                 ((filenum--))
6268
6269         [[ $found -eq $filenum ]] ||
6270                 error "$LFS getstripe --obd: found $found expect $filenum"
6271         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
6272                 sed '/^[         ]*'${ostidx}'[  ]/d' |
6273                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
6274                 error "$LFS getstripe --obd: should not show file on other obd"
6275         echo "$LFS getstripe --obd passed"
6276 }
6277 run_test 56a "check $LFS getstripe"
6278
6279 test_56b() {
6280         local dir=$DIR/$tdir
6281         local numdirs=3
6282
6283         test_mkdir $dir
6284         for i in $(seq $numdirs); do
6285                 test_mkdir $dir/dir$i
6286         done
6287
6288         # test lfs getdirstripe default mode is non-recursion, which is
6289         # different from lfs getstripe
6290         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
6291
6292         [[ $dircnt -eq 1 ]] ||
6293                 error "$LFS getdirstripe: found $dircnt, not 1"
6294         dircnt=$($LFS getdirstripe --recursive $dir |
6295                 grep -c lmv_stripe_count)
6296         [[ $dircnt -eq $((numdirs + 1)) ]] ||
6297                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
6298 }
6299 run_test 56b "check $LFS getdirstripe"
6300
6301 test_56c() {
6302         remote_ost_nodsh && skip "remote OST with nodsh"
6303
6304         local ost_idx=0
6305         local ost_name=$(ostname_from_index $ost_idx)
6306         local old_status=$(ost_dev_status $ost_idx)
6307         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6308
6309         [[ -z "$old_status" ]] ||
6310                 skip_env "OST $ost_name is in $old_status status"
6311
6312         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
6313         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6314                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
6315         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6316                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
6317                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
6318         fi
6319
6320         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
6321                 error "$LFS df -v showing inactive devices"
6322         sleep_maxage
6323
6324         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
6325
6326         [[ "$new_status" =~ "D" ]] ||
6327                 error "$ost_name status is '$new_status', missing 'D'"
6328         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
6329                 [[ "$new_status" =~ "N" ]] ||
6330                         error "$ost_name status is '$new_status', missing 'N'"
6331         fi
6332         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6333                 [[ "$new_status" =~ "f" ]] ||
6334                         error "$ost_name status is '$new_status', missing 'f'"
6335         fi
6336
6337         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
6338         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6339                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
6340         [[ -z "$p" ]] && restore_lustre_params < $p || true
6341         sleep_maxage
6342
6343         new_status=$(ost_dev_status $ost_idx)
6344         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
6345                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
6346         # can't check 'f' as devices may actually be on flash
6347 }
6348 run_test 56c "check 'lfs df' showing device status"
6349
6350 test_56d() {
6351         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
6352         local osts=$($LFS df -v $MOUNT | grep -c OST)
6353
6354         $LFS df $MOUNT
6355
6356         (( mdts == MDSCOUNT )) ||
6357                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
6358         (( osts == OSTCOUNT )) ||
6359                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
6360 }
6361 run_test 56d "'lfs df -v' prints only configured devices"
6362
6363 test_56e() {
6364         err_enoent=2 # No such file or directory
6365         err_eopnotsupp=95 # Operation not supported
6366
6367         enoent_mnt=/pmt1 # Invalid dentry. Path not present
6368         notsup_mnt=/tmp  # Valid dentry, but Not a lustreFS
6369
6370         # Check for handling of path not exists
6371         output=$($LFS df $enoent_mnt 2>&1)
6372         ret=$?
6373
6374         fs=$(echo $output | awk -F: '{print $2}' | awk '{print $3}' | tr -d \')
6375         [[ $fs = $enoent_mnt && $ret -eq $err_enoent ]] ||
6376                 error "expect failure $err_enoent, not $ret"
6377
6378         # Check for handling of non-Lustre FS
6379         output=$($LFS df $notsup_mnt)
6380         ret=$?
6381
6382         fs=$(echo $output | awk '{print $1}' | awk -F: '{print $2}')
6383         [[ $fs = $notsup_mnt && $ret -eq $err_eopnotsupp ]] ||
6384                 error "expect success $err_eopnotsupp, not $ret"
6385
6386         # Check for multiple LustreFS argument
6387         output=$($LFS df $MOUNT $MOUNT $MOUNT | grep -c "filesystem_summary:")
6388         ret=$?
6389
6390         [[ $output -eq 3 && $ret -eq 0 ]] ||
6391                 error "expect success 3, not $output, rc = $ret"
6392
6393         # Check for correct non-Lustre FS handling among multiple
6394         # LustreFS argument
6395         output=$($LFS df $MOUNT $notsup_mnt $MOUNT |
6396                 grep -c "filesystem_summary:"; exit ${PIPESTATUS[0]})
6397         ret=$?
6398
6399         [[ $output -eq 2 && $ret -eq $err_eopnotsupp ]] ||
6400                 error "expect success 2, not $output, rc = $ret"
6401 }
6402 run_test 56e "'lfs df' Handle non LustreFS & multiple LustreFS"
6403
6404 NUMFILES=3
6405 NUMDIRS=3
6406 setup_56() {
6407         local local_tdir="$1"
6408         local local_numfiles="$2"
6409         local local_numdirs="$3"
6410         local dir_params="$4"
6411         local dir_stripe_params="$5"
6412
6413         if [ ! -d "$local_tdir" ] ; then
6414                 test_mkdir -p $dir_stripe_params $local_tdir
6415                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
6416                 for i in $(seq $local_numfiles) ; do
6417                         touch $local_tdir/file$i
6418                 done
6419                 for i in $(seq $local_numdirs) ; do
6420                         test_mkdir $dir_stripe_params $local_tdir/dir$i
6421                         for j in $(seq $local_numfiles) ; do
6422                                 touch $local_tdir/dir$i/file$j
6423                         done
6424                 done
6425         fi
6426 }
6427
6428 setup_56_special() {
6429         local local_tdir=$1
6430         local local_numfiles=$2
6431         local local_numdirs=$3
6432
6433         setup_56 $local_tdir $local_numfiles $local_numdirs
6434
6435         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
6436                 for i in $(seq $local_numfiles) ; do
6437                         mknod $local_tdir/loop${i}b b 7 $i
6438                         mknod $local_tdir/null${i}c c 1 3
6439                         ln -s $local_tdir/file1 $local_tdir/link${i}
6440                 done
6441                 for i in $(seq $local_numdirs) ; do
6442                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
6443                         mknod $local_tdir/dir$i/null${i}c c 1 3
6444                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
6445                 done
6446         fi
6447 }
6448
6449 test_56g() {
6450         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6451         local expected=$(($NUMDIRS + 2))
6452
6453         setup_56 $dir $NUMFILES $NUMDIRS
6454
6455         # test lfs find with -name
6456         for i in $(seq $NUMFILES) ; do
6457                 local nums=$($LFS find -name "*$i" $dir | wc -l)
6458
6459                 [ $nums -eq $expected ] ||
6460                         error "lfs find -name '*$i' $dir wrong: "\
6461                               "found $nums, expected $expected"
6462         done
6463 }
6464 run_test 56g "check lfs find -name"
6465
6466 test_56h() {
6467         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6468         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
6469
6470         setup_56 $dir $NUMFILES $NUMDIRS
6471
6472         # test lfs find with ! -name
6473         for i in $(seq $NUMFILES) ; do
6474                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
6475
6476                 [ $nums -eq $expected ] ||
6477                         error "lfs find ! -name '*$i' $dir wrong: "\
6478                               "found $nums, expected $expected"
6479         done
6480 }
6481 run_test 56h "check lfs find ! -name"
6482
6483 test_56i() {
6484         local dir=$DIR/$tdir
6485
6486         test_mkdir $dir
6487
6488         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
6489         local out=$($cmd)
6490
6491         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
6492 }
6493 run_test 56i "check 'lfs find -ost UUID' skips directories"
6494
6495 test_56j() {
6496         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6497
6498         setup_56_special $dir $NUMFILES $NUMDIRS
6499
6500         local expected=$((NUMDIRS + 1))
6501         local cmd="$LFS find -type d $dir"
6502         local nums=$($cmd | wc -l)
6503
6504         [ $nums -eq $expected ] ||
6505                 error "'$cmd' wrong: found $nums, expected $expected"
6506 }
6507 run_test 56j "check lfs find -type d"
6508
6509 test_56k() {
6510         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6511
6512         setup_56_special $dir $NUMFILES $NUMDIRS
6513
6514         local expected=$(((NUMDIRS + 1) * NUMFILES))
6515         local cmd="$LFS find -type f $dir"
6516         local nums=$($cmd | wc -l)
6517
6518         [ $nums -eq $expected ] ||
6519                 error "'$cmd' wrong: found $nums, expected $expected"
6520 }
6521 run_test 56k "check lfs find -type f"
6522
6523 test_56l() {
6524         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6525
6526         setup_56_special $dir $NUMFILES $NUMDIRS
6527
6528         local expected=$((NUMDIRS + NUMFILES))
6529         local cmd="$LFS find -type b $dir"
6530         local nums=$($cmd | wc -l)
6531
6532         [ $nums -eq $expected ] ||
6533                 error "'$cmd' wrong: found $nums, expected $expected"
6534 }
6535 run_test 56l "check lfs find -type b"
6536
6537 test_56m() {
6538         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6539
6540         setup_56_special $dir $NUMFILES $NUMDIRS
6541
6542         local expected=$((NUMDIRS + NUMFILES))
6543         local cmd="$LFS find -type c $dir"
6544         local nums=$($cmd | wc -l)
6545         [ $nums -eq $expected ] ||
6546                 error "'$cmd' wrong: found $nums, expected $expected"
6547 }
6548 run_test 56m "check lfs find -type c"
6549
6550 test_56n() {
6551         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6552         setup_56_special $dir $NUMFILES $NUMDIRS
6553
6554         local expected=$((NUMDIRS + NUMFILES))
6555         local cmd="$LFS find -type l $dir"
6556         local nums=$($cmd | wc -l)
6557
6558         [ $nums -eq $expected ] ||
6559                 error "'$cmd' wrong: found $nums, expected $expected"
6560 }
6561 run_test 56n "check lfs find -type l"
6562
6563 test_56o() {
6564         local dir=$DIR/$tdir
6565
6566         setup_56 $dir $NUMFILES $NUMDIRS
6567         utime $dir/file1 > /dev/null || error "utime (1)"
6568         utime $dir/file2 > /dev/null || error "utime (2)"
6569         utime $dir/dir1 > /dev/null || error "utime (3)"
6570         utime $dir/dir2 > /dev/null || error "utime (4)"
6571         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
6572         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
6573
6574         local expected=4
6575         local nums=$($LFS find -mtime +0 $dir | wc -l)
6576
6577         [ $nums -eq $expected ] ||
6578                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
6579
6580         expected=12
6581         cmd="$LFS find -mtime 0 $dir"
6582         nums=$($cmd | wc -l)
6583         [ $nums -eq $expected ] ||
6584                 error "'$cmd' wrong: found $nums, expected $expected"
6585 }
6586 run_test 56o "check lfs find -mtime for old files"
6587
6588 test_56ob() {
6589         local dir=$DIR/$tdir
6590         local expected=1
6591         local count=0
6592
6593         # just to make sure there is something that won't be found
6594         test_mkdir $dir
6595         touch $dir/$tfile.now
6596
6597         for age in year week day hour min; do
6598                 count=$((count + 1))
6599
6600                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
6601                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
6602                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
6603
6604                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
6605                 local nums=$($cmd | wc -l)
6606                 [ $nums -eq $expected ] ||
6607                         error "'$cmd' wrong: found $nums, expected $expected"
6608
6609                 cmd="$LFS find $dir -atime $count${age:0:1}"
6610                 nums=$($cmd | wc -l)
6611                 [ $nums -eq $expected ] ||
6612                         error "'$cmd' wrong: found $nums, expected $expected"
6613         done
6614
6615         sleep 2
6616         cmd="$LFS find $dir -ctime +1s -type f"
6617         nums=$($cmd | wc -l)
6618         (( $nums == $count * 2 + 1)) ||
6619                 error "'$cmd' wrong: found $nums, expected $((count * 2 + 1))"
6620 }
6621 run_test 56ob "check lfs find -atime -mtime -ctime with units"
6622
6623 test_newerXY_base() {
6624         local x=$1
6625         local y=$2
6626         local dir=$DIR/$tdir
6627         local ref
6628         local negref
6629
6630         if [ $y == "t" ]; then
6631                 if [ $x == "b" ]; then
6632                         ref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6633                 else
6634                         ref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6635                 fi
6636         else
6637                 ref=$DIR/$tfile.newer.$x$y
6638                 touch $ref || error "touch $ref failed"
6639         fi
6640
6641         echo "before = $ref"
6642         sleep 2
6643         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6644         sleep 2
6645         if [ $y == "t" ]; then
6646                 if [ $x == "b" ]; then
6647                         negref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6648                 else
6649                         negref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6650                 fi
6651         else
6652                 negref=$DIR/$tfile.negnewer.$x$y
6653                 touch $negref || error "touch $negref failed"
6654         fi
6655
6656         echo "after = $negref"
6657         local cmd="$LFS find $dir -newer$x$y $ref"
6658         local nums=$(eval $cmd | wc -l)
6659         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
6660
6661         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6662                 error "'$cmd' wrong: found $nums newer, expected $expected"  ; }
6663
6664         cmd="$LFS find $dir ! -newer$x$y $negref"
6665         nums=$(eval $cmd | wc -l)
6666         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6667                 error "'$cmd' wrong: found $nums older, expected $expected"  ; }
6668
6669         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
6670         nums=$(eval $cmd | wc -l)
6671         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6672                 error "'$cmd' wrong: found $nums between, expected $expected"; }
6673
6674         rm -rf $DIR/*
6675 }
6676
6677 test_56oc() {
6678         test_newerXY_base "a" "a"
6679         test_newerXY_base "a" "m"
6680         test_newerXY_base "a" "c"
6681         test_newerXY_base "m" "a"
6682         test_newerXY_base "m" "m"
6683         test_newerXY_base "m" "c"
6684         test_newerXY_base "c" "a"
6685         test_newerXY_base "c" "m"
6686         test_newerXY_base "c" "c"
6687
6688         [[ -n "$sles_version" ]] &&
6689                 echo "skip timestamp tests on SLES, LU-13665" && return 0
6690
6691         test_newerXY_base "a" "t"
6692         test_newerXY_base "m" "t"
6693         test_newerXY_base "c" "t"
6694
6695         [[ $MDS1_VERSION -lt $(version_code 2.13.54) ||
6696            $CLIENT_VERSION -lt $(version_code 2.13.54) ]] &&
6697                 ! btime_supported && echo "btime unsupported" && return 0
6698
6699         test_newerXY_base "b" "b"
6700         test_newerXY_base "b" "t"
6701 }
6702 run_test 56oc "check lfs find -newerXY work"
6703
6704 btime_supported() {
6705         local dir=$DIR/$tdir
6706         local rc
6707
6708         mkdir -p $dir
6709         touch $dir/$tfile
6710         $LFS find $dir -btime -1d -type f
6711         rc=$?
6712         rm -rf $dir
6713         return $rc
6714 }
6715
6716 test_56od() {
6717         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
6718                 ! btime_supported && skip "btime unsupported on MDS"
6719
6720         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6721                 ! btime_supported && skip "btime unsupported on clients"
6722
6723         local dir=$DIR/$tdir
6724         local ref=$DIR/$tfile.ref
6725         local negref=$DIR/$tfile.negref
6726
6727         mkdir $dir || error "mkdir $dir failed"
6728         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6729         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6730         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6731         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6732         touch $ref || error "touch $ref failed"
6733         # sleep 3 seconds at least
6734         sleep 3
6735
6736         local before=$(do_facet mds1 date +%s)
6737         local skew=$(($(date +%s) - before + 1))
6738
6739         if (( skew < 0 && skew > -5 )); then
6740                 sleep $((0 - skew + 1))
6741                 skew=0
6742         fi
6743
6744         # Set the dir stripe params to limit files all on MDT0,
6745         # otherwise we need to calc the max clock skew between
6746         # the client and MDTs.
6747         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6748         sleep 2
6749         touch $negref || error "touch $negref failed"
6750
6751         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6752         local nums=$($cmd | wc -l)
6753         local expected=$(((NUMFILES + 1) * NUMDIRS))
6754
6755         [ $nums -eq $expected ] ||
6756                 error "'$cmd' wrong: found $nums, expected $expected"
6757
6758         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6759         nums=$($cmd | wc -l)
6760         expected=$((NUMFILES + 1))
6761         [ $nums -eq $expected ] ||
6762                 error "'$cmd' wrong: found $nums, expected $expected"
6763
6764         [ $skew -lt 0 ] && return
6765
6766         local after=$(do_facet mds1 date +%s)
6767         local age=$((after - before + 1 + skew))
6768
6769         cmd="$LFS find $dir -btime -${age}s -type f"
6770         nums=$($cmd | wc -l)
6771         expected=$(((NUMFILES + 1) * NUMDIRS))
6772
6773         echo "Clock skew between client and server: $skew, age:$age"
6774         [ $nums -eq $expected ] ||
6775                 error "'$cmd' wrong: found $nums, expected $expected"
6776
6777         expected=$(($NUMDIRS + 1))
6778         cmd="$LFS find $dir -btime -${age}s -type d"
6779         nums=$($cmd | wc -l)
6780         [ $nums -eq $expected ] ||
6781                 error "'$cmd' wrong: found $nums, expected $expected"
6782         rm -f $ref $negref || error "Failed to remove $ref $negref"
6783 }
6784 run_test 56od "check lfs find -btime with units"
6785
6786 test_56p() {
6787         [ $RUNAS_ID -eq $UID ] &&
6788                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6789
6790         local dir=$DIR/$tdir
6791
6792         setup_56 $dir $NUMFILES $NUMDIRS
6793         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6794
6795         local expected=$NUMFILES
6796         local cmd="$LFS find -uid $RUNAS_ID $dir"
6797         local nums=$($cmd | wc -l)
6798
6799         [ $nums -eq $expected ] ||
6800                 error "'$cmd' wrong: found $nums, expected $expected"
6801
6802         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6803         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6804         nums=$($cmd | wc -l)
6805         [ $nums -eq $expected ] ||
6806                 error "'$cmd' wrong: found $nums, expected $expected"
6807 }
6808 run_test 56p "check lfs find -uid and ! -uid"
6809
6810 test_56q() {
6811         [ $RUNAS_ID -eq $UID ] &&
6812                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6813
6814         local dir=$DIR/$tdir
6815
6816         setup_56 $dir $NUMFILES $NUMDIRS
6817         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6818
6819         local expected=$NUMFILES
6820         local cmd="$LFS find -gid $RUNAS_GID $dir"
6821         local nums=$($cmd | wc -l)
6822
6823         [ $nums -eq $expected ] ||
6824                 error "'$cmd' wrong: found $nums, expected $expected"
6825
6826         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6827         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6828         nums=$($cmd | wc -l)
6829         [ $nums -eq $expected ] ||
6830                 error "'$cmd' wrong: found $nums, expected $expected"
6831 }
6832 run_test 56q "check lfs find -gid and ! -gid"
6833
6834 test_56r() {
6835         local dir=$DIR/$tdir
6836
6837         setup_56 $dir $NUMFILES $NUMDIRS
6838
6839         local expected=12
6840         local cmd="$LFS find -size 0 -type f -lazy $dir"
6841         local nums=$($cmd | wc -l)
6842
6843         [ $nums -eq $expected ] ||
6844                 error "'$cmd' wrong: found $nums, expected $expected"
6845         cmd="$LFS find -size 0 -type f $dir"
6846         nums=$($cmd | wc -l)
6847         [ $nums -eq $expected ] ||
6848                 error "'$cmd' wrong: found $nums, expected $expected"
6849
6850         expected=0
6851         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6852         nums=$($cmd | wc -l)
6853         [ $nums -eq $expected ] ||
6854                 error "'$cmd' wrong: found $nums, expected $expected"
6855         cmd="$LFS find ! -size 0 -type f $dir"
6856         nums=$($cmd | wc -l)
6857         [ $nums -eq $expected ] ||
6858                 error "'$cmd' wrong: found $nums, expected $expected"
6859
6860         echo "test" > $dir/$tfile
6861         echo "test2" > $dir/$tfile.2 && sync
6862         expected=1
6863         cmd="$LFS find -size 5 -type f -lazy $dir"
6864         nums=$($cmd | wc -l)
6865         [ $nums -eq $expected ] ||
6866                 error "'$cmd' wrong: found $nums, expected $expected"
6867         cmd="$LFS find -size 5 -type f $dir"
6868         nums=$($cmd | wc -l)
6869         [ $nums -eq $expected ] ||
6870                 error "'$cmd' wrong: found $nums, expected $expected"
6871
6872         expected=1
6873         cmd="$LFS find -size +5 -type f -lazy $dir"
6874         nums=$($cmd | wc -l)
6875         [ $nums -eq $expected ] ||
6876                 error "'$cmd' wrong: found $nums, expected $expected"
6877         cmd="$LFS find -size +5 -type f $dir"
6878         nums=$($cmd | wc -l)
6879         [ $nums -eq $expected ] ||
6880                 error "'$cmd' wrong: found $nums, expected $expected"
6881
6882         expected=2
6883         cmd="$LFS find -size +0 -type f -lazy $dir"
6884         nums=$($cmd | wc -l)
6885         [ $nums -eq $expected ] ||
6886                 error "'$cmd' wrong: found $nums, expected $expected"
6887         cmd="$LFS find -size +0 -type f $dir"
6888         nums=$($cmd | wc -l)
6889         [ $nums -eq $expected ] ||
6890                 error "'$cmd' wrong: found $nums, expected $expected"
6891
6892         expected=2
6893         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6894         nums=$($cmd | wc -l)
6895         [ $nums -eq $expected ] ||
6896                 error "'$cmd' wrong: found $nums, expected $expected"
6897         cmd="$LFS find ! -size -5 -type f $dir"
6898         nums=$($cmd | wc -l)
6899         [ $nums -eq $expected ] ||
6900                 error "'$cmd' wrong: found $nums, expected $expected"
6901
6902         expected=12
6903         cmd="$LFS find -size -5 -type f -lazy $dir"
6904         nums=$($cmd | wc -l)
6905         [ $nums -eq $expected ] ||
6906                 error "'$cmd' wrong: found $nums, expected $expected"
6907         cmd="$LFS find -size -5 -type f $dir"
6908         nums=$($cmd | wc -l)
6909         [ $nums -eq $expected ] ||
6910                 error "'$cmd' wrong: found $nums, expected $expected"
6911 }
6912 run_test 56r "check lfs find -size works"
6913
6914 test_56ra_sub() {
6915         local expected=$1
6916         local glimpses=$2
6917         local cmd="$3"
6918
6919         cancel_lru_locks $OSC
6920
6921         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6922         local nums=$($cmd | wc -l)
6923
6924         [ $nums -eq $expected ] ||
6925                 error "'$cmd' wrong: found $nums, expected $expected"
6926
6927         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6928
6929         if (( rpcs_before + glimpses != rpcs_after )); then
6930                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6931                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6932
6933                 if [[ $glimpses == 0 ]]; then
6934                         error "'$cmd' should not send glimpse RPCs to OST"
6935                 else
6936                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6937                 fi
6938         fi
6939 }
6940
6941 test_56ra() {
6942         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6943                 skip "MDS < 2.12.58 doesn't return LSOM data"
6944         local dir=$DIR/$tdir
6945         local old_agl=$($LCTL get_param -n llite.*.statahead_agl)
6946
6947         [[ $OSC == "mdc" ]] && skip "statahead not needed for DoM files"
6948
6949         # statahead_agl may cause extra glimpse which confuses results. LU-13017
6950         $LCTL set_param -n llite.*.statahead_agl=0
6951         stack_trap "$LCTL set_param -n llite.*.statahead_agl=$old_agl"
6952
6953         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6954         # open and close all files to ensure LSOM is updated
6955         cancel_lru_locks $OSC
6956         find $dir -type f | xargs cat > /dev/null
6957
6958         #   expect_found  glimpse_rpcs  command_to_run
6959         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6960         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6961         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6962         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6963
6964         echo "test" > $dir/$tfile
6965         echo "test2" > $dir/$tfile.2 && sync
6966         cancel_lru_locks $OSC
6967         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6968
6969         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6970         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6971         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6972         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6973
6974         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6975         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6976         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6977         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6978         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6979         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6980 }
6981 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6982
6983 test_56rb() {
6984         local dir=$DIR/$tdir
6985         local tmp=$TMP/$tfile.log
6986         local mdt_idx;
6987
6988         test_mkdir -p $dir || error "failed to mkdir $dir"
6989         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6990                 error "failed to setstripe $dir/$tfile"
6991         mdt_idx=$($LFS getdirstripe -i $dir)
6992         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6993
6994         stack_trap "rm -f $tmp" EXIT
6995         $LFS find --size +100K --ost 0 $dir |& tee $tmp
6996         ! grep -q obd_uuid $tmp ||
6997                 error "failed to find --size +100K --ost 0 $dir"
6998         $LFS find --size +100K --mdt $mdt_idx $dir |& tee $tmp
6999         ! grep -q obd_uuid $tmp ||
7000                 error "failed to find --size +100K --mdt $mdt_idx $dir"
7001 }
7002 run_test 56rb "check lfs find --size --ost/--mdt works"
7003
7004 test_56rc() {
7005         (( MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
7006         local dir=$DIR/$tdir
7007         local found
7008
7009         test_mkdir -c 2 -H all_char $dir || error "failed to mkdir $dir"
7010         $LFS mkdir -c 2 --mdt-hash all_char $dir/$tdir-all{1..10}
7011         (( $MDSCOUNT > 2 )) &&
7012                 $LFS mkdir -c 3 --mdt-hash fnv_1a_64 $dir/$tdir-fnv{1..10}
7013         mkdir $dir/$tdir-{1..10}
7014         touch $dir/$tfile-{1..10}
7015
7016         found=$($LFS find $dir --mdt-count 2 | wc -l)
7017         expect=11
7018         (( $found == $expect )) || error "found $found 2-stripe, expect $expect"
7019
7020         found=$($LFS find $dir -T +1 | wc -l)
7021         (( $MDSCOUNT > 2 )) && expect=$((expect + 10))
7022         (( $found == $expect )) || error "found $found 2+stripe, expect $expect"
7023
7024         found=$($LFS find $dir --mdt-hash all_char | wc -l)
7025         expect=11
7026         (( $found == $expect )) || error "found $found all_char, expect $expect"
7027
7028         found=$($LFS find $dir --mdt-hash fnv_1a_64 | wc -l)
7029         (( $MDSCOUNT > 2 )) && expect=10 || expect=0
7030         (( $found == $expect )) || error "found $found all_char, expect $expect"
7031 }
7032 run_test 56rc "check lfs find --mdt-count/--mdt-hash works"
7033
7034 test_56s() { # LU-611 #LU-9369
7035         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
7036
7037         local dir=$DIR/$tdir
7038         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
7039
7040         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
7041         for i in $(seq $NUMDIRS); do
7042                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
7043         done
7044
7045         local expected=$NUMDIRS
7046         local cmd="$LFS find -c $OSTCOUNT $dir"
7047         local nums=$($cmd | wc -l)
7048
7049         [ $nums -eq $expected ] || {
7050                 $LFS getstripe -R $dir
7051                 error "'$cmd' wrong: found $nums, expected $expected"
7052         }
7053
7054         expected=$((NUMDIRS + onestripe))
7055         cmd="$LFS find -stripe-count +0 -type f $dir"
7056         nums=$($cmd | wc -l)
7057         [ $nums -eq $expected ] || {
7058                 $LFS getstripe -R $dir
7059                 error "'$cmd' wrong: found $nums, expected $expected"
7060         }
7061
7062         expected=$onestripe
7063         cmd="$LFS find -stripe-count 1 -type f $dir"
7064         nums=$($cmd | wc -l)
7065         [ $nums -eq $expected ] || {
7066                 $LFS getstripe -R $dir
7067                 error "'$cmd' wrong: found $nums, expected $expected"
7068         }
7069
7070         cmd="$LFS find -stripe-count -2 -type f $dir"
7071         nums=$($cmd | wc -l)
7072         [ $nums -eq $expected ] || {
7073                 $LFS getstripe -R $dir
7074                 error "'$cmd' wrong: found $nums, expected $expected"
7075         }
7076
7077         expected=0
7078         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
7079         nums=$($cmd | wc -l)
7080         [ $nums -eq $expected ] || {
7081                 $LFS getstripe -R $dir
7082                 error "'$cmd' wrong: found $nums, expected $expected"
7083         }
7084 }
7085 run_test 56s "check lfs find -stripe-count works"
7086
7087 test_56t() { # LU-611 #LU-9369
7088         local dir=$DIR/$tdir
7089
7090         setup_56 $dir 0 $NUMDIRS
7091         for i in $(seq $NUMDIRS); do
7092                 $LFS setstripe -S 8M $dir/dir$i/$tfile
7093         done
7094
7095         local expected=$NUMDIRS
7096         local cmd="$LFS find -S 8M $dir"
7097         local nums=$($cmd | wc -l)
7098
7099         [ $nums -eq $expected ] || {
7100                 $LFS getstripe -R $dir
7101                 error "'$cmd' wrong: found $nums, expected $expected"
7102         }
7103         rm -rf $dir
7104
7105         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
7106
7107         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
7108
7109         expected=$(((NUMDIRS + 1) * NUMFILES))
7110         cmd="$LFS find -stripe-size 512k -type f $dir"
7111         nums=$($cmd | wc -l)
7112         [ $nums -eq $expected ] ||
7113                 error "'$cmd' wrong: found $nums, expected $expected"
7114
7115         cmd="$LFS find -stripe-size +320k -type f $dir"
7116         nums=$($cmd | wc -l)
7117         [ $nums -eq $expected ] ||
7118                 error "'$cmd' wrong: found $nums, expected $expected"
7119
7120         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
7121         cmd="$LFS find -stripe-size +200k -type f $dir"
7122         nums=$($cmd | wc -l)
7123         [ $nums -eq $expected ] ||
7124                 error "'$cmd' wrong: found $nums, expected $expected"
7125
7126         cmd="$LFS find -stripe-size -640k -type f $dir"
7127         nums=$($cmd | wc -l)
7128         [ $nums -eq $expected ] ||
7129                 error "'$cmd' wrong: found $nums, expected $expected"
7130
7131         expected=4
7132         cmd="$LFS find -stripe-size 256k -type f $dir"
7133         nums=$($cmd | wc -l)
7134         [ $nums -eq $expected ] ||
7135                 error "'$cmd' wrong: found $nums, expected $expected"
7136
7137         cmd="$LFS find -stripe-size -320k -type f $dir"
7138         nums=$($cmd | wc -l)
7139         [ $nums -eq $expected ] ||
7140                 error "'$cmd' wrong: found $nums, expected $expected"
7141
7142         expected=0
7143         cmd="$LFS find -stripe-size 1024k -type f $dir"
7144         nums=$($cmd | wc -l)
7145         [ $nums -eq $expected ] ||
7146                 error "'$cmd' wrong: found $nums, expected $expected"
7147 }
7148 run_test 56t "check lfs find -stripe-size works"
7149
7150 test_56u() { # LU-611
7151         local dir=$DIR/$tdir
7152
7153         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
7154
7155         if [[ $OSTCOUNT -gt 1 ]]; then
7156                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
7157                 onestripe=4
7158         else
7159                 onestripe=0
7160         fi
7161
7162         local expected=$(((NUMDIRS + 1) * NUMFILES))
7163         local cmd="$LFS find -stripe-index 0 -type f $dir"
7164         local nums=$($cmd | wc -l)
7165
7166         [ $nums -eq $expected ] ||
7167                 error "'$cmd' wrong: found $nums, expected $expected"
7168
7169         expected=$onestripe
7170         cmd="$LFS find -stripe-index 1 -type f $dir"
7171         nums=$($cmd | wc -l)
7172         [ $nums -eq $expected ] ||
7173                 error "'$cmd' wrong: found $nums, expected $expected"
7174
7175         cmd="$LFS find ! -stripe-index 0 -type f $dir"
7176         nums=$($cmd | wc -l)
7177         [ $nums -eq $expected ] ||
7178                 error "'$cmd' wrong: found $nums, expected $expected"
7179
7180         expected=0
7181         # This should produce an error and not return any files
7182         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
7183         nums=$($cmd 2>/dev/null | wc -l)
7184         [ $nums -eq $expected ] ||
7185                 error "'$cmd' wrong: found $nums, expected $expected"
7186
7187         if [[ $OSTCOUNT -gt 1 ]]; then
7188                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
7189                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
7190                 nums=$($cmd | wc -l)
7191                 [ $nums -eq $expected ] ||
7192                         error "'$cmd' wrong: found $nums, expected $expected"
7193         fi
7194 }
7195 run_test 56u "check lfs find -stripe-index works"
7196
7197 test_56v() {
7198         local mdt_idx=0
7199         local dir=$DIR/$tdir
7200
7201         setup_56 $dir $NUMFILES $NUMDIRS
7202
7203         UUID=$(mdtuuid_from_index $mdt_idx $dir)
7204         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
7205
7206         for file in $($LFS find -m $UUID $dir); do
7207                 file_midx=$($LFS getstripe -m $file)
7208                 [ $file_midx -eq $mdt_idx ] ||
7209                         error "lfs find -m $UUID != getstripe -m $file_midx"
7210         done
7211 }
7212 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
7213
7214 test_56w() {
7215         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7216         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7217
7218         local dir=$DIR/$tdir
7219
7220         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
7221
7222         local stripe_size=$($LFS getstripe -S -d $dir) ||
7223                 error "$LFS getstripe -S -d $dir failed"
7224         stripe_size=${stripe_size%% *}
7225
7226         local file_size=$((stripe_size * OSTCOUNT))
7227         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
7228         local required_space=$((file_num * file_size))
7229         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
7230                            head -n1)
7231         [[ $free_space -le $((required_space / 1024)) ]] &&
7232                 skip_env "need $required_space, have $free_space kbytes"
7233
7234         local dd_bs=65536
7235         local dd_count=$((file_size / dd_bs))
7236
7237         # write data into the files
7238         local i
7239         local j
7240         local file
7241
7242         for i in $(seq $NUMFILES); do
7243                 file=$dir/file$i
7244                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7245                         error "write data into $file failed"
7246         done
7247         for i in $(seq $NUMDIRS); do
7248                 for j in $(seq $NUMFILES); do
7249                         file=$dir/dir$i/file$j
7250                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7251                                 error "write data into $file failed"
7252                 done
7253         done
7254
7255         # $LFS_MIGRATE will fail if hard link migration is unsupported
7256         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
7257                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
7258                         error "creating links to $dir/dir1/file1 failed"
7259         fi
7260
7261         local expected=-1
7262
7263         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
7264
7265         # lfs_migrate file
7266         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
7267
7268         echo "$cmd"
7269         eval $cmd || error "$cmd failed"
7270
7271         check_stripe_count $dir/file1 $expected
7272
7273         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
7274         then
7275                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
7276                 # OST 1 if it is on OST 0. This file is small enough to
7277                 # be on only one stripe.
7278                 file=$dir/migr_1_ost
7279                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
7280                         error "write data into $file failed"
7281                 local obdidx=$($LFS getstripe -i $file)
7282                 local oldmd5=$(md5sum $file)
7283                 local newobdidx=0
7284
7285                 [[ $obdidx -eq 0 ]] && newobdidx=1
7286                 cmd="$LFS migrate -i $newobdidx $file"
7287                 echo $cmd
7288                 eval $cmd || error "$cmd failed"
7289
7290                 local realobdix=$($LFS getstripe -i $file)
7291                 local newmd5=$(md5sum $file)
7292
7293                 [[ $newobdidx -ne $realobdix ]] &&
7294                         error "new OST is different (was=$obdidx, "\
7295                               "wanted=$newobdidx, got=$realobdix)"
7296                 [[ "$oldmd5" != "$newmd5" ]] &&
7297                         error "md5sum differ: $oldmd5, $newmd5"
7298         fi
7299
7300         # lfs_migrate dir
7301         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
7302         echo "$cmd"
7303         eval $cmd || error "$cmd failed"
7304
7305         for j in $(seq $NUMFILES); do
7306                 check_stripe_count $dir/dir1/file$j $expected
7307         done
7308
7309         # lfs_migrate works with lfs find
7310         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
7311              $LFS_MIGRATE -y -c $expected"
7312         echo "$cmd"
7313         eval $cmd || error "$cmd failed"
7314
7315         for i in $(seq 2 $NUMFILES); do
7316                 check_stripe_count $dir/file$i $expected
7317         done
7318         for i in $(seq 2 $NUMDIRS); do
7319                 for j in $(seq $NUMFILES); do
7320                 check_stripe_count $dir/dir$i/file$j $expected
7321                 done
7322         done
7323 }
7324 run_test 56w "check lfs_migrate -c stripe_count works"
7325
7326 test_56wb() {
7327         local file1=$DIR/$tdir/file1
7328         local create_pool=false
7329         local initial_pool=$($LFS getstripe -p $DIR)
7330         local pool_list=()
7331         local pool=""
7332
7333         echo -n "Creating test dir..."
7334         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7335         echo "done."
7336
7337         echo -n "Creating test file..."
7338         touch $file1 || error "cannot create file"
7339         echo "done."
7340
7341         echo -n "Detecting existing pools..."
7342         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
7343
7344         if [ ${#pool_list[@]} -gt 0 ]; then
7345                 echo "${pool_list[@]}"
7346                 for thispool in "${pool_list[@]}"; do
7347                         if [[ -z "$initial_pool" ||
7348                               "$initial_pool" != "$thispool" ]]; then
7349                                 pool="$thispool"
7350                                 echo "Using existing pool '$pool'"
7351                                 break
7352                         fi
7353                 done
7354         else
7355                 echo "none detected."
7356         fi
7357         if [ -z "$pool" ]; then
7358                 pool=${POOL:-testpool}
7359                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
7360                 echo -n "Creating pool '$pool'..."
7361                 create_pool=true
7362                 pool_add $pool &> /dev/null ||
7363                         error "pool_add failed"
7364                 echo "done."
7365
7366                 echo -n "Adding target to pool..."
7367                 pool_add_targets $pool 0 0 1 &> /dev/null ||
7368                         error "pool_add_targets failed"
7369                 echo "done."
7370         fi
7371
7372         echo -n "Setting pool using -p option..."
7373         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
7374                 error "migrate failed rc = $?"
7375         echo "done."
7376
7377         echo -n "Verifying test file is in pool after migrating..."
7378         [ "$($LFS getstripe -p $file1)" = $pool ] ||
7379                 error "file was not migrated to pool $pool"
7380         echo "done."
7381
7382         echo -n "Removing test file from pool '$pool'..."
7383         # "lfs migrate $file" won't remove the file from the pool
7384         # until some striping information is changed.
7385         $LFS migrate -c 1 $file1 &> /dev/null ||
7386                 error "cannot remove from pool"
7387         [ "$($LFS getstripe -p $file1)" ] &&
7388                 error "pool still set"
7389         echo "done."
7390
7391         echo -n "Setting pool using --pool option..."
7392         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
7393                 error "migrate failed rc = $?"
7394         echo "done."
7395
7396         # Clean up
7397         rm -f $file1
7398         if $create_pool; then
7399                 destroy_test_pools 2> /dev/null ||
7400                         error "destroy test pools failed"
7401         fi
7402 }
7403 run_test 56wb "check lfs_migrate pool support"
7404
7405 test_56wc() {
7406         local file1="$DIR/$tdir/file1"
7407         local parent_ssize
7408         local parent_scount
7409         local cur_ssize
7410         local cur_scount
7411         local orig_ssize
7412
7413         echo -n "Creating test dir..."
7414         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7415         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
7416                 error "cannot set stripe by '-S 1M -c 1'"
7417         echo "done"
7418
7419         echo -n "Setting initial stripe for test file..."
7420         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
7421                 error "cannot set stripe"
7422         cur_ssize=$($LFS getstripe -S "$file1")
7423         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
7424         echo "done."
7425
7426         # File currently set to -S 512K -c 1
7427
7428         # Ensure -c and -S options are rejected when -R is set
7429         echo -n "Verifying incompatible options are detected..."
7430         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
7431                 error "incompatible -c and -R options not detected"
7432         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
7433                 error "incompatible -S and -R options not detected"
7434         echo "done."
7435
7436         # Ensure unrecognized options are passed through to 'lfs migrate'
7437         echo -n "Verifying -S option is passed through to lfs migrate..."
7438         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
7439                 error "migration failed"
7440         cur_ssize=$($LFS getstripe -S "$file1")
7441         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
7442         echo "done."
7443
7444         # File currently set to -S 1M -c 1
7445
7446         # Ensure long options are supported
7447         echo -n "Verifying long options supported..."
7448         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
7449                 error "long option without argument not supported"
7450         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
7451                 error "long option with argument not supported"
7452         cur_ssize=$($LFS getstripe -S "$file1")
7453         [ $cur_ssize -eq 524288 ] ||
7454                 error "migrate --stripe-size $cur_ssize != 524288"
7455         echo "done."
7456
7457         # File currently set to -S 512K -c 1
7458
7459         if [ "$OSTCOUNT" -gt 1 ]; then
7460                 echo -n "Verifying explicit stripe count can be set..."
7461                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
7462                         error "migrate failed"
7463                 cur_scount=$($LFS getstripe -c "$file1")
7464                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
7465                 echo "done."
7466         fi
7467
7468         # File currently set to -S 512K -c 1 or -S 512K -c 2
7469
7470         # Ensure parent striping is used if -R is set, and no stripe
7471         # count or size is specified
7472         echo -n "Setting stripe for parent directory..."
7473         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7474                 error "cannot set stripe '-S 2M -c 1'"
7475         echo "done."
7476
7477         echo -n "Verifying restripe option uses parent stripe settings..."
7478         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
7479         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
7480         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
7481                 error "migrate failed"
7482         cur_ssize=$($LFS getstripe -S "$file1")
7483         [ $cur_ssize -eq $parent_ssize ] ||
7484                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
7485         cur_scount=$($LFS getstripe -c "$file1")
7486         [ $cur_scount -eq $parent_scount ] ||
7487                 error "migrate -R stripe_count $cur_scount != $parent_scount"
7488         echo "done."
7489
7490         # File currently set to -S 1M -c 1
7491
7492         # Ensure striping is preserved if -R is not set, and no stripe
7493         # count or size is specified
7494         echo -n "Verifying striping size preserved when not specified..."
7495         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
7496         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7497                 error "cannot set stripe on parent directory"
7498         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7499                 error "migrate failed"
7500         cur_ssize=$($LFS getstripe -S "$file1")
7501         [ $cur_ssize -eq $orig_ssize ] ||
7502                 error "migrate by default $cur_ssize != $orig_ssize"
7503         echo "done."
7504
7505         # Ensure file name properly detected when final option has no argument
7506         echo -n "Verifying file name properly detected..."
7507         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7508                 error "file name interpreted as option argument"
7509         echo "done."
7510
7511         # Clean up
7512         rm -f "$file1"
7513 }
7514 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
7515
7516 test_56wd() {
7517         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7518
7519         local file1=$DIR/$tdir/file1
7520
7521         echo -n "Creating test dir..."
7522         test_mkdir $DIR/$tdir || error "cannot create dir"
7523         echo "done."
7524
7525         echo -n "Creating test file..."
7526         touch $file1
7527         echo "done."
7528
7529         # Ensure 'lfs migrate' will fail by using a non-existent option,
7530         # and make sure rsync is not called to recover
7531         echo -n "Make sure --no-rsync option works..."
7532         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
7533                 grep -q 'refusing to fall back to rsync' ||
7534                 error "rsync was called with --no-rsync set"
7535         echo "done."
7536
7537         # Ensure rsync is called without trying 'lfs migrate' first
7538         echo -n "Make sure --rsync option works..."
7539         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
7540                 grep -q 'falling back to rsync' &&
7541                 error "lfs migrate was called with --rsync set"
7542         echo "done."
7543
7544         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
7545         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
7546                 grep -q 'at the same time' ||
7547                 error "--rsync and --no-rsync accepted concurrently"
7548         echo "done."
7549
7550         # Clean up
7551         rm -f $file1
7552 }
7553 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
7554
7555 test_56we() {
7556         local td=$DIR/$tdir
7557         local tf=$td/$tfile
7558
7559         test_mkdir $td || error "cannot create $td"
7560         touch $tf || error "cannot touch $tf"
7561
7562         echo -n "Make sure --non-direct|-D works..."
7563         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
7564                 grep -q "lfs migrate --non-direct" ||
7565                 error "--non-direct option cannot work correctly"
7566         $LFS_MIGRATE -y -D -v $tf 2>&1 |
7567                 grep -q "lfs migrate -D" ||
7568                 error "-D option cannot work correctly"
7569         echo "done."
7570 }
7571 run_test 56we "check lfs_migrate --non-direct|-D support"
7572
7573 test_56x() {
7574         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7575         check_swap_layouts_support
7576
7577         local dir=$DIR/$tdir
7578         local ref1=/etc/passwd
7579         local file1=$dir/file1
7580
7581         test_mkdir $dir || error "creating dir $dir"
7582         $LFS setstripe -c 2 $file1
7583         cp $ref1 $file1
7584         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
7585         stripe=$($LFS getstripe -c $file1)
7586         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7587         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7588
7589         # clean up
7590         rm -f $file1
7591 }
7592 run_test 56x "lfs migration support"
7593
7594 test_56xa() {
7595         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7596         check_swap_layouts_support
7597
7598         local dir=$DIR/$tdir/$testnum
7599
7600         test_mkdir -p $dir
7601
7602         local ref1=/etc/passwd
7603         local file1=$dir/file1
7604
7605         $LFS setstripe -c 2 $file1
7606         cp $ref1 $file1
7607         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
7608
7609         local stripe=$($LFS getstripe -c $file1)
7610
7611         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7612         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7613
7614         # clean up
7615         rm -f $file1
7616 }
7617 run_test 56xa "lfs migration --block support"
7618
7619 check_migrate_links() {
7620         local dir="$1"
7621         local file1="$dir/file1"
7622         local begin="$2"
7623         local count="$3"
7624         local runas="$4"
7625         local total_count=$(($begin + $count - 1))
7626         local symlink_count=10
7627         local uniq_count=10
7628
7629         if [ ! -f "$file1" ]; then
7630                 echo -n "creating initial file..."
7631                 $LFS setstripe -c 1 -S "512k" "$file1" ||
7632                         error "cannot setstripe initial file"
7633                 echo "done"
7634
7635                 echo -n "creating symlinks..."
7636                 for s in $(seq 1 $symlink_count); do
7637                         ln -s "$file1" "$dir/slink$s" ||
7638                                 error "cannot create symlinks"
7639                 done
7640                 echo "done"
7641
7642                 echo -n "creating nonlinked files..."
7643                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
7644                         error "cannot create nonlinked files"
7645                 echo "done"
7646         fi
7647
7648         # create hard links
7649         if [ ! -f "$dir/file$total_count" ]; then
7650                 echo -n "creating hard links $begin:$total_count..."
7651                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
7652                         /dev/null || error "cannot create hard links"
7653                 echo "done"
7654         fi
7655
7656         echo -n "checking number of hard links listed in xattrs..."
7657         local fid=$($LFS getstripe -F "$file1")
7658         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
7659
7660         echo "${#paths[*]}"
7661         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
7662                         skip "hard link list has unexpected size, skipping test"
7663         fi
7664         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
7665                         error "link names should exceed xattrs size"
7666         fi
7667
7668         echo -n "migrating files..."
7669         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
7670         local rc=$?
7671         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
7672         echo "done"
7673
7674         # make sure all links have been properly migrated
7675         echo -n "verifying files..."
7676         fid=$($LFS getstripe -F "$file1") ||
7677                 error "cannot get fid for file $file1"
7678         for i in $(seq 2 $total_count); do
7679                 local fid2=$($LFS getstripe -F $dir/file$i)
7680
7681                 [ "$fid2" == "$fid" ] ||
7682                         error "migrated hard link has mismatched FID"
7683         done
7684
7685         # make sure hard links were properly detected, and migration was
7686         # performed only once for the entire link set; nonlinked files should
7687         # also be migrated
7688         local actual=$(grep -c 'done' <<< "$migrate_out")
7689         local expected=$(($uniq_count + 1))
7690
7691         [ "$actual" -eq  "$expected" ] ||
7692                 error "hard links individually migrated ($actual != $expected)"
7693
7694         # make sure the correct number of hard links are present
7695         local hardlinks=$(stat -c '%h' "$file1")
7696
7697         [ $hardlinks -eq $total_count ] ||
7698                 error "num hard links $hardlinks != $total_count"
7699         echo "done"
7700
7701         return 0
7702 }
7703
7704 test_56xb() {
7705         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
7706                 skip "Need MDS version at least 2.10.55"
7707
7708         local dir="$DIR/$tdir"
7709
7710         test_mkdir "$dir" || error "cannot create dir $dir"
7711
7712         echo "testing lfs migrate mode when all links fit within xattrs"
7713         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
7714
7715         echo "testing rsync mode when all links fit within xattrs"
7716         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
7717
7718         echo "testing lfs migrate mode when all links do not fit within xattrs"
7719         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
7720
7721         echo "testing rsync mode when all links do not fit within xattrs"
7722         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
7723
7724         chown -R $RUNAS_ID $dir
7725         echo "testing non-root lfs migrate mode when not all links are in xattr"
7726         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
7727
7728         # clean up
7729         rm -rf $dir
7730 }
7731 run_test 56xb "lfs migration hard link support"
7732
7733 test_56xc() {
7734         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7735
7736         local dir="$DIR/$tdir"
7737
7738         test_mkdir "$dir" || error "cannot create dir $dir"
7739
7740         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7741         echo -n "Setting initial stripe for 20MB test file..."
7742         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7743                 error "cannot setstripe 20MB file"
7744         echo "done"
7745         echo -n "Sizing 20MB test file..."
7746         $TRUNCATE "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7747         echo "done"
7748         echo -n "Verifying small file autostripe count is 1..."
7749         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7750                 error "cannot migrate 20MB file"
7751         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7752                 error "cannot get stripe for $dir/20mb"
7753         [ $stripe_count -eq 1 ] ||
7754                 error "unexpected stripe count $stripe_count for 20MB file"
7755         rm -f "$dir/20mb"
7756         echo "done"
7757
7758         # Test 2: File is small enough to fit within the available space on
7759         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7760         # have at least an additional 1KB for each desired stripe for test 3
7761         echo -n "Setting stripe for 1GB test file..."
7762         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7763         echo "done"
7764         echo -n "Sizing 1GB test file..."
7765         # File size is 1GB + 3KB
7766         $TRUNCATE "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7767         echo "done"
7768
7769         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7770         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7771         if (( avail > 524288 * OSTCOUNT )); then
7772                 echo -n "Migrating 1GB file..."
7773                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7774                         error "cannot migrate 1GB file"
7775                 echo "done"
7776                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7777                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7778                         error "cannot getstripe for 1GB file"
7779                 [ $stripe_count -eq 2 ] ||
7780                         error "unexpected stripe count $stripe_count != 2"
7781                 echo "done"
7782         fi
7783
7784         # Test 3: File is too large to fit within the available space on
7785         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7786         if [ $OSTCOUNT -ge 3 ]; then
7787                 # The required available space is calculated as
7788                 # file size (1GB + 3KB) / OST count (3).
7789                 local kb_per_ost=349526
7790
7791                 echo -n "Migrating 1GB file with limit..."
7792                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7793                         error "cannot migrate 1GB file with limit"
7794                 echo "done"
7795
7796                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7797                 echo -n "Verifying 1GB autostripe count with limited space..."
7798                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7799                         error "unexpected stripe count $stripe_count (min 3)"
7800                 echo "done"
7801         fi
7802
7803         # clean up
7804         rm -rf $dir
7805 }
7806 run_test 56xc "lfs migration autostripe"
7807
7808 test_56xd() {
7809         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7810
7811         local dir=$DIR/$tdir
7812         local f_mgrt=$dir/$tfile.mgrt
7813         local f_yaml=$dir/$tfile.yaml
7814         local f_copy=$dir/$tfile.copy
7815         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7816         local layout_copy="-c 2 -S 2M -i 1"
7817         local yamlfile=$dir/yamlfile
7818         local layout_before;
7819         local layout_after;
7820
7821         test_mkdir "$dir" || error "cannot create dir $dir"
7822         $LFS setstripe $layout_yaml $f_yaml ||
7823                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7824         $LFS getstripe --yaml $f_yaml > $yamlfile
7825         $LFS setstripe $layout_copy $f_copy ||
7826                 error "cannot setstripe $f_copy with layout $layout_copy"
7827         touch $f_mgrt
7828         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7829
7830         # 1. test option --yaml
7831         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7832                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7833         layout_before=$(get_layout_param $f_yaml)
7834         layout_after=$(get_layout_param $f_mgrt)
7835         [ "$layout_after" == "$layout_before" ] ||
7836                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7837
7838         # 2. test option --copy
7839         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7840                 error "cannot migrate $f_mgrt with --copy $f_copy"
7841         layout_before=$(get_layout_param $f_copy)
7842         layout_after=$(get_layout_param $f_mgrt)
7843         [ "$layout_after" == "$layout_before" ] ||
7844                 error "lfs_migrate --copy: $layout_after != $layout_before"
7845 }
7846 run_test 56xd "check lfs_migrate --yaml and --copy support"
7847
7848 test_56xe() {
7849         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7850
7851         local dir=$DIR/$tdir
7852         local f_comp=$dir/$tfile
7853         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7854         local layout_before=""
7855         local layout_after=""
7856
7857         test_mkdir "$dir" || error "cannot create dir $dir"
7858         $LFS setstripe $layout $f_comp ||
7859                 error "cannot setstripe $f_comp with layout $layout"
7860         layout_before=$(get_layout_param $f_comp)
7861         dd if=/dev/zero of=$f_comp bs=1M count=4
7862
7863         # 1. migrate a comp layout file by lfs_migrate
7864         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7865         layout_after=$(get_layout_param $f_comp)
7866         [ "$layout_before" == "$layout_after" ] ||
7867                 error "lfs_migrate: $layout_before != $layout_after"
7868
7869         # 2. migrate a comp layout file by lfs migrate
7870         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7871         layout_after=$(get_layout_param $f_comp)
7872         [ "$layout_before" == "$layout_after" ] ||
7873                 error "lfs migrate: $layout_before != $layout_after"
7874 }
7875 run_test 56xe "migrate a composite layout file"
7876
7877 test_56xf() {
7878         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7879
7880         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7881                 skip "Need server version at least 2.13.53"
7882
7883         local dir=$DIR/$tdir
7884         local f_comp=$dir/$tfile
7885         local layout="-E 1M -c1 -E -1 -c2"
7886         local fid_before=""
7887         local fid_after=""
7888
7889         test_mkdir "$dir" || error "cannot create dir $dir"
7890         $LFS setstripe $layout $f_comp ||
7891                 error "cannot setstripe $f_comp with layout $layout"
7892         fid_before=$($LFS getstripe --fid $f_comp)
7893         dd if=/dev/zero of=$f_comp bs=1M count=4
7894
7895         # 1. migrate a comp layout file to a comp layout
7896         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7897         fid_after=$($LFS getstripe --fid $f_comp)
7898         [ "$fid_before" == "$fid_after" ] ||
7899                 error "comp-to-comp migrate: $fid_before != $fid_after"
7900
7901         # 2. migrate a comp layout file to a plain layout
7902         $LFS migrate -c2 $f_comp ||
7903                 error "cannot migrate $f_comp by lfs migrate"
7904         fid_after=$($LFS getstripe --fid $f_comp)
7905         [ "$fid_before" == "$fid_after" ] ||
7906                 error "comp-to-plain migrate: $fid_before != $fid_after"
7907
7908         # 3. migrate a plain layout file to a comp layout
7909         $LFS migrate $layout $f_comp ||
7910                 error "cannot migrate $f_comp by lfs migrate"
7911         fid_after=$($LFS getstripe --fid $f_comp)
7912         [ "$fid_before" == "$fid_after" ] ||
7913                 error "plain-to-comp migrate: $fid_before != $fid_after"
7914 }
7915 run_test 56xf "FID is not lost during migration of a composite layout file"
7916
7917 check_file_ost_range() {
7918         local file="$1"
7919         shift
7920         local range="$*"
7921         local -a file_range
7922         local idx
7923
7924         file_range=($($LFS getstripe -y "$file" |
7925                 awk '/l_ost_idx:/ { print $NF }'))
7926
7927         if [[ "${#file_range[@]}" = 0 ]]; then
7928                 echo "No osts found for $file"
7929                 return 1
7930         fi
7931
7932         for idx in "${file_range[@]}"; do
7933                 [[ " $range " =~ " $idx " ]] ||
7934                         return 1
7935         done
7936
7937         return 0
7938 }
7939
7940 sub_test_56xg() {
7941         local stripe_opt="$1"
7942         local pool="$2"
7943         shift 2
7944         local pool_ostidx="$(seq $* | tr '\n' ' ')"
7945
7946         $LFS migrate $stripe_opt -p $pool $DIR/$tfile ||
7947                 error "Fail to migrate $tfile on $pool"
7948         [[ "$($LFS getstripe -p $DIR/$tfile)" = "$pool" ]] ||
7949                 error "$tfile is not in pool $pool"
7950         check_file_ost_range "$DIR/$tfile" $pool_ostidx ||
7951                 error "$tfile osts mismatch with pool $pool (osts $pool_ostidx)"
7952 }
7953
7954 test_56xg() {
7955         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7956         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
7957         [[ $MDS1_VERSION -gt $(version_code 2.14.52) ]] ||
7958                 skip "Need MDS version newer than 2.14.52"
7959
7960         local -a pool_names=("${TESTNAME}_0" "${TESTNAME}_1" "${TESTNAME}_2")
7961         local -a pool_ranges=("0 0" "1 1" "0 1")
7962
7963         # init pools
7964         for i in "${!pool_names[@]}"; do
7965                 pool_add ${pool_names[$i]} ||
7966                         error "pool_add failed (pool: ${pool_names[$i]})"
7967                 pool_add_targets ${pool_names[$i]} ${pool_ranges[$i]} ||
7968                         error "pool_add_targets failed (pool: ${pool_names[$i]})"
7969         done
7970
7971         # init the file to migrate
7972         $LFS setstripe -c1 -i1 $DIR/$tfile ||
7973                 error "Unable to create $tfile on OST1"
7974         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=4 status=none ||
7975                 error "Unable to write on $tfile"
7976
7977         echo "1. migrate $tfile on pool ${pool_names[0]}"
7978         sub_test_56xg "-c-1" "${pool_names[0]}" ${pool_ranges[0]}
7979
7980         echo "2. migrate $tfile on pool ${pool_names[2]}"
7981         sub_test_56xg "-c-1 -S2M" "${pool_names[2]}" ${pool_ranges[2]}
7982
7983         echo "3. migrate $tfile on pool ${pool_names[1]}"
7984         sub_test_56xg "-n -c-1" "${pool_names[1]}" ${pool_ranges[1]}
7985
7986         echo "4. migrate $tfile on pool ${pool_names[2]} with default stripe parameters"
7987         sub_test_56xg "" "${pool_names[2]}" ${pool_ranges[2]}
7988         echo
7989
7990         # Clean pools
7991         destroy_test_pools ||
7992                 error "pool_destroy failed"
7993 }
7994 run_test 56xg "lfs migrate pool support"
7995
7996 test_56y() {
7997         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7998                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7999
8000         local res=""
8001         local dir=$DIR/$tdir
8002         local f1=$dir/file1
8003         local f2=$dir/file2
8004
8005         test_mkdir -p $dir || error "creating dir $dir"
8006         touch $f1 || error "creating std file $f1"
8007         $MULTIOP $f2 H2c || error "creating released file $f2"
8008
8009         # a directory can be raid0, so ask only for files
8010         res=$($LFS find $dir -L raid0 -type f | wc -l)
8011         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
8012
8013         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
8014         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
8015
8016         # only files can be released, so no need to force file search
8017         res=$($LFS find $dir -L released)
8018         [[ $res == $f2 ]] || error "search released: found $res != $f2"
8019
8020         res=$($LFS find $dir -type f \! -L released)
8021         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
8022 }
8023 run_test 56y "lfs find -L raid0|released"
8024
8025 test_56z() { # LU-4824
8026         # This checks to make sure 'lfs find' continues after errors
8027         # There are two classes of errors that should be caught:
8028         # - If multiple paths are provided, all should be searched even if one
8029         #   errors out
8030         # - If errors are encountered during the search, it should not terminate
8031         #   early
8032         local dir=$DIR/$tdir
8033         local i
8034
8035         test_mkdir $dir
8036         for i in d{0..9}; do
8037                 test_mkdir $dir/$i
8038                 touch $dir/$i/$tfile
8039         done
8040         $LFS find $DIR/non_existent_dir $dir &&
8041                 error "$LFS find did not return an error"
8042         # Make a directory unsearchable. This should NOT be the last entry in
8043         # directory order.  Arbitrarily pick the 6th entry
8044         chmod 700 $($LFS find $dir -type d | sed '6!d')
8045
8046         $RUNAS $LFS find $DIR/non_existent $dir
8047         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
8048
8049         # The user should be able to see 10 directories and 9 files
8050         (( count == 19 )) ||
8051                 error "$LFS find found $count != 19 entries after error"
8052 }
8053 run_test 56z "lfs find should continue after an error"
8054
8055 test_56aa() { # LU-5937
8056         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
8057
8058         local dir=$DIR/$tdir
8059
8060         mkdir $dir
8061         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
8062
8063         createmany -o $dir/striped_dir/${tfile}- 1024
8064         local dirs=$($LFS find --size +8k $dir/)
8065
8066         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
8067 }
8068 run_test 56aa "lfs find --size under striped dir"
8069
8070 test_56ab() { # LU-10705
8071         test_mkdir $DIR/$tdir
8072         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
8073         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
8074         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
8075         # Flush writes to ensure valid blocks.  Need to be more thorough for
8076         # ZFS, since blocks are not allocated/returned to client immediately.
8077         sync_all_data
8078         wait_zfs_commit ost1 2
8079         cancel_lru_locks osc
8080         ls -ls $DIR/$tdir
8081
8082         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
8083
8084         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
8085
8086         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
8087         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
8088
8089         rm -f $DIR/$tdir/$tfile.[123]
8090 }
8091 run_test 56ab "lfs find --blocks"
8092
8093 # LU-11188
8094 test_56aca() {
8095         local dir="$DIR/$tdir"
8096         local perms=(001 002 003 004 005 006 007
8097                      010 020 030 040 050 060 070
8098                      100 200 300 400 500 600 700
8099                      111 222 333 444 555 666 777)
8100         local perm_minus=(8 8 4 8 4 4 2
8101                           8 8 4 8 4 4 2
8102                           8 8 4 8 4 4 2
8103                           4 4 2 4 2 2 1)
8104         local perm_slash=(8  8 12  8 12 12 14
8105                           8  8 12  8 12 12 14
8106                           8  8 12  8 12 12 14
8107                          16 16 24 16 24 24 28)
8108
8109         test_mkdir "$dir"
8110         for perm in ${perms[*]}; do
8111                 touch "$dir/$tfile.$perm"
8112                 chmod $perm "$dir/$tfile.$perm"
8113         done
8114
8115         for ((i = 0; i < ${#perms[*]}; i++)); do
8116                 local num=$($LFS find $dir -perm ${perms[i]} | wc -l)
8117                 (( $num == 1 )) ||
8118                         error "lfs find -perm ${perms[i]}:"\
8119                               "$num != 1"
8120
8121                 num=$($LFS find $dir -perm -${perms[i]} -type f| wc -l)
8122                 (( $num == ${perm_minus[i]} )) ||
8123                         error "lfs find -perm -${perms[i]}:"\
8124                               "$num != ${perm_minus[i]}"
8125
8126                 num=$($LFS find $dir -perm /${perms[i]} -type f| wc -l)
8127                 (( $num == ${perm_slash[i]} )) ||
8128                         error "lfs find -perm /${perms[i]}:"\
8129                               "$num != ${perm_slash[i]}"
8130         done
8131 }
8132 run_test 56aca "check lfs find -perm with octal representation"
8133
8134 test_56acb() {
8135         local dir=$DIR/$tdir
8136         # p is the permission of write and execute for user, group and other
8137         # without the umask. It is used to test +wx.
8138         local p=$(printf "%o" "$((0333 & ~$(umask)))")
8139         local perms=(1000 000 2000 4000 $p 644 111 110 100 004)
8140         local symbolic=(+t  a+t u+t g+t o+t
8141                         g+s u+s o+s +s o+sr
8142                         o=r,ug+o,u+w
8143                         u+ g+ o+ a+ ugo+
8144                         u- g- o- a- ugo-
8145                         u= g= o= a= ugo=
8146                         o=r,ug+o,u+w u=r,a+u,u+w
8147                         g=r,ugo=g,u+w u+x,+X +X
8148                         u+x,u+X u+X u+x,g+X o+r,+X
8149                         u+x,go+X +wx +rwx)
8150
8151         test_mkdir $dir
8152         for perm in ${perms[*]}; do
8153                 touch "$dir/$tfile.$perm"
8154                 chmod $perm "$dir/$tfile.$perm"
8155         done
8156
8157         for (( i = 0; i < ${#symbolic[*]}; i++ )); do
8158                 local num=$($LFS find $dir -perm ${symbolic[i]} | wc -l)
8159
8160                 (( $num == 1 )) ||
8161                         error "lfs find $dir -perm ${symbolic[i]}: $num != 1"
8162         done
8163 }
8164 run_test 56acb "check lfs find -perm with symbolic representation"
8165
8166 test_56acc() {
8167         local dir=$DIR/$tdir
8168         local tests="17777 787 789 abcd
8169                 ug=uu ug=a ug=gu uo=ou urw
8170                 u+xg+x a=r,u+x,"
8171
8172         test_mkdir $dir
8173         for err in $tests; do
8174                 if $LFS find $dir -perm $err 2>/dev/null; then
8175                         error "lfs find -perm $err: parsing should have failed"
8176                 fi
8177         done
8178 }
8179 run_test 56acc "check parsing error for lfs find -perm"
8180
8181 test_56ba() {
8182         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
8183                 skip "Need MDS version at least 2.10.50"
8184
8185         # Create composite files with one component
8186         local dir=$DIR/$tdir
8187
8188         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
8189         # Create composite files with three components
8190         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
8191         # Create non-composite files
8192         createmany -o $dir/${tfile}- 10
8193
8194         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
8195
8196         [[ $nfiles == 10 ]] ||
8197                 error "lfs find -E 1M found $nfiles != 10 files"
8198
8199         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
8200         [[ $nfiles == 25 ]] ||
8201                 error "lfs find ! -E 1M found $nfiles != 25 files"
8202
8203         # All files have a component that starts at 0
8204         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
8205         [[ $nfiles == 35 ]] ||
8206                 error "lfs find --component-start 0 - $nfiles != 35 files"
8207
8208         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
8209         [[ $nfiles == 15 ]] ||
8210                 error "lfs find --component-start 2M - $nfiles != 15 files"
8211
8212         # All files created here have a componenet that does not starts at 2M
8213         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
8214         [[ $nfiles == 35 ]] ||
8215                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
8216
8217         # Find files with a specified number of components
8218         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
8219         [[ $nfiles == 15 ]] ||
8220                 error "lfs find --component-count 3 - $nfiles != 15 files"
8221
8222         # Remember non-composite files have a component count of zero
8223         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
8224         [[ $nfiles == 10 ]] ||
8225                 error "lfs find --component-count 0 - $nfiles != 10 files"
8226
8227         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
8228         [[ $nfiles == 20 ]] ||
8229                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
8230
8231         # All files have a flag called "init"
8232         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
8233         [[ $nfiles == 35 ]] ||
8234                 error "lfs find --component-flags init - $nfiles != 35 files"
8235
8236         # Multi-component files will have a component not initialized
8237         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
8238         [[ $nfiles == 15 ]] ||
8239                 error "lfs find !--component-flags init - $nfiles != 15 files"
8240
8241         rm -rf $dir
8242
8243 }
8244 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
8245
8246 test_56ca() {
8247         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
8248                 skip "Need MDS version at least 2.10.57"
8249
8250         local td=$DIR/$tdir
8251         local tf=$td/$tfile
8252         local dir
8253         local nfiles
8254         local cmd
8255         local i
8256         local j
8257
8258         # create mirrored directories and mirrored files
8259         mkdir $td || error "mkdir $td failed"
8260         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
8261         createmany -o $tf- 10 || error "create $tf- failed"
8262
8263         for i in $(seq 2); do
8264                 dir=$td/dir$i
8265                 mkdir $dir || error "mkdir $dir failed"
8266                 $LFS mirror create -N$((3 + i)) $dir ||
8267                         error "create mirrored dir $dir failed"
8268                 createmany -o $dir/$tfile- 10 ||
8269                         error "create $dir/$tfile- failed"
8270         done
8271
8272         # change the states of some mirrored files
8273         echo foo > $tf-6
8274         for i in $(seq 2); do
8275                 dir=$td/dir$i
8276                 for j in $(seq 4 9); do
8277                         echo foo > $dir/$tfile-$j
8278                 done
8279         done
8280
8281         # find mirrored files with specific mirror count
8282         cmd="$LFS find --mirror-count 3 --type f $td"
8283         nfiles=$($cmd | wc -l)
8284         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
8285
8286         cmd="$LFS find ! --mirror-count 3 --type f $td"
8287         nfiles=$($cmd | wc -l)
8288         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
8289
8290         cmd="$LFS find --mirror-count +2 --type f $td"
8291         nfiles=$($cmd | wc -l)
8292         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8293
8294         cmd="$LFS find --mirror-count -6 --type f $td"
8295         nfiles=$($cmd | wc -l)
8296         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8297
8298         # find mirrored files with specific file state
8299         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
8300         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
8301
8302         cmd="$LFS find --mirror-state=ro --type f $td"
8303         nfiles=$($cmd | wc -l)
8304         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
8305
8306         cmd="$LFS find ! --mirror-state=ro --type f $td"
8307         nfiles=$($cmd | wc -l)
8308         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8309
8310         cmd="$LFS find --mirror-state=wp --type f $td"
8311         nfiles=$($cmd | wc -l)
8312         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8313
8314         cmd="$LFS find ! --mirror-state=sp --type f $td"
8315         nfiles=$($cmd | wc -l)
8316         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8317 }
8318 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
8319
8320 test_56da() { # LU-14179
8321         local path=$DIR/$tdir
8322
8323         test_mkdir $path
8324         cd $path
8325
8326         local longdir=$(str_repeat 'a' 255)
8327
8328         for i in {1..15}; do
8329                 path=$path/$longdir
8330                 test_mkdir $longdir
8331                 cd $longdir
8332         done
8333
8334         local len=${#path}
8335         local lastdir=$(str_repeat 'a' $((4096 - 1 - $len - 1)))
8336
8337         test_mkdir $lastdir
8338         cd $lastdir
8339         # PATH_MAX-1
8340         (( ${#PWD} == 4095 )) || error "bad PWD length ${#PWD}, expect 4095"
8341
8342         # NAME_MAX
8343         touch $(str_repeat 'f' 255)
8344
8345         $LFS find $DIR/$tdir --type d |& grep "lfs find: error" &&
8346                 error "lfs find reported an error"
8347
8348         rm -rf $DIR/$tdir
8349 }
8350 run_test 56da "test lfs find with long paths"
8351
8352 test_56ea() { #LU-10378
8353         local path=$DIR/$tdir
8354         local pool=$TESTNAME
8355
8356         # Create ost pool
8357         pool_add $pool || error "pool_add $pool failed"
8358         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8359                 error "adding targets to $pool failed"
8360
8361         # Set default pool on directory before creating file
8362         mkdir $path || error "mkdir $path failed"
8363         $LFS setstripe -p $pool $path ||
8364                 error "set OST pool on $pool failed"
8365         touch $path/$tfile || error "touch $path/$tfile failed"
8366
8367         # Compare basic file attributes from -printf and stat
8368         local attr_printf=$($LFS find $path/$tfile -printf "%A@ %T@ %C@ %U %G")
8369         local attr_stat=$(stat -c "%X %Y %Z %u %g" $path/$tfile)
8370
8371         [[ "${attr_printf}" == "${attr_stat}" ]] ||
8372                 error "Attrs from lfs find and stat don't match"
8373
8374         # Compare Lustre attributes from lfs find and lfs getstripe
8375         local lattr_printf=$($LFS find $path/$tfile -printf "%Lc %LS %Li %Lp")
8376         local str_cnt=$($LFS getstripe --stripe-count $path/$tfile)
8377         local str_size=$($LFS getstripe --stripe-size $path/$tfile)
8378         local str_idx=$($LFS getstripe --stripe-index $path/$tfile)
8379         local fpool=$($LFS getstripe --pool $path/$tfile)
8380         local lattr_getstr="${str_cnt} ${str_size} ${str_idx} ${fpool}"
8381
8382         [[ "${lattr_printf}" == "${lattr_getstr}" ]] ||
8383                 error "Attrs from lfs find and lfs getstripe don't match"
8384
8385         # Verify behavior for unknown escape/format sequences
8386         local esc_printf=$($LFS find $path/$tfile -printf '\\ %% \Q %Q')
8387
8388         [[ "${esc_printf}" == '\ % \Q %Q' ]] ||
8389                 error "Escape/format codes don't match"
8390 }
8391 run_test 56ea "test lfs find -printf option"
8392
8393 test_57a() {
8394         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8395         # note test will not do anything if MDS is not local
8396         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8397                 skip_env "ldiskfs only test"
8398         fi
8399         remote_mds_nodsh && skip "remote MDS with nodsh"
8400
8401         local MNTDEV="osd*.*MDT*.mntdev"
8402         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
8403         [ -z "$DEV" ] && error "can't access $MNTDEV"
8404         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
8405                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
8406                         error "can't access $DEV"
8407                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
8408                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
8409                 rm $TMP/t57a.dump
8410         done
8411 }
8412 run_test 57a "verify MDS filesystem created with large inodes =="
8413
8414 test_57b() {
8415         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8416         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8417                 skip_env "ldiskfs only test"
8418         fi
8419         remote_mds_nodsh && skip "remote MDS with nodsh"
8420
8421         local dir=$DIR/$tdir
8422         local filecount=100
8423         local file1=$dir/f1
8424         local fileN=$dir/f$filecount
8425
8426         rm -rf $dir || error "removing $dir"
8427         test_mkdir -c1 $dir
8428         local mdtidx=$($LFS getstripe -m $dir)
8429         local mdtname=MDT$(printf %04x $mdtidx)
8430         local facet=mds$((mdtidx + 1))
8431
8432         echo "mcreating $filecount files"
8433         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
8434
8435         # verify that files do not have EAs yet
8436         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
8437                 error "$file1 has an EA"
8438         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
8439                 error "$fileN has an EA"
8440
8441         sync
8442         sleep 1
8443         df $dir  #make sure we get new statfs data
8444         local mdsfree=$(do_facet $facet \
8445                         lctl get_param -n osd*.*$mdtname.kbytesfree)
8446         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8447         local file
8448
8449         echo "opening files to create objects/EAs"
8450         for file in $(seq -f $dir/f%g 1 $filecount); do
8451                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
8452                         error "opening $file"
8453         done
8454
8455         # verify that files have EAs now
8456         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
8457         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
8458
8459         sleep 1  #make sure we get new statfs data
8460         df $dir
8461         local mdsfree2=$(do_facet $facet \
8462                          lctl get_param -n osd*.*$mdtname.kbytesfree)
8463         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8464
8465         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
8466                 if [ "$mdsfree" != "$mdsfree2" ]; then
8467                         error "MDC before $mdcfree != after $mdcfree2"
8468                 else
8469                         echo "MDC before $mdcfree != after $mdcfree2"
8470                         echo "unable to confirm if MDS has large inodes"
8471                 fi
8472         fi
8473         rm -rf $dir
8474 }
8475 run_test 57b "default LOV EAs are stored inside large inodes ==="
8476
8477 test_58() {
8478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8479         [ -z "$(which wiretest 2>/dev/null)" ] &&
8480                         skip_env "could not find wiretest"
8481
8482         wiretest
8483 }
8484 run_test 58 "verify cross-platform wire constants =============="
8485
8486 test_59() {
8487         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8488
8489         echo "touch 130 files"
8490         createmany -o $DIR/f59- 130
8491         echo "rm 130 files"
8492         unlinkmany $DIR/f59- 130
8493         sync
8494         # wait for commitment of removal
8495         wait_delete_completed
8496 }
8497 run_test 59 "verify cancellation of llog records async ========="
8498
8499 TEST60_HEAD="test_60 run $RANDOM"
8500 test_60a() {
8501         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8502         remote_mgs_nodsh && skip "remote MGS with nodsh"
8503         do_facet mgs "! which run-llog.sh &> /dev/null" &&
8504                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
8505                         skip_env "missing subtest run-llog.sh"
8506
8507         log "$TEST60_HEAD - from kernel mode"
8508         do_facet mgs "$LCTL dk > /dev/null"
8509         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
8510         do_facet mgs $LCTL dk > $TMP/$tfile
8511
8512         # LU-6388: test llog_reader
8513         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
8514         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
8515         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
8516                         skip_env "missing llog_reader"
8517         local fstype=$(facet_fstype mgs)
8518         [ $fstype != ldiskfs -a $fstype != zfs ] &&
8519                 skip_env "Only for ldiskfs or zfs type mgs"
8520
8521         local mntpt=$(facet_mntpt mgs)
8522         local mgsdev=$(mgsdevname 1)
8523         local fid_list
8524         local fid
8525         local rec_list
8526         local rec
8527         local rec_type
8528         local obj_file
8529         local path
8530         local seq
8531         local oid
8532         local pass=true
8533
8534         #get fid and record list
8535         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
8536                 tail -n 4))
8537         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
8538                 tail -n 4))
8539         #remount mgs as ldiskfs or zfs type
8540         stop mgs || error "stop mgs failed"
8541         mount_fstype mgs || error "remount mgs failed"
8542         for ((i = 0; i < ${#fid_list[@]}; i++)); do
8543                 fid=${fid_list[i]}
8544                 rec=${rec_list[i]}
8545                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
8546                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
8547                 oid=$((16#$oid))
8548
8549                 case $fstype in
8550                         ldiskfs )
8551                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
8552                         zfs )
8553                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
8554                 esac
8555                 echo "obj_file is $obj_file"
8556                 do_facet mgs $llog_reader $obj_file
8557
8558                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
8559                         awk '{ print $3 }' | sed -e "s/^type=//g")
8560                 if [ $rec_type != $rec ]; then
8561                         echo "FAILED test_60a wrong record type $rec_type," \
8562                               "should be $rec"
8563                         pass=false
8564                         break
8565                 fi
8566
8567                 #check obj path if record type is LLOG_LOGID_MAGIC
8568                 if [ "$rec" == "1064553b" ]; then
8569                         path=$(do_facet mgs $llog_reader $obj_file |
8570                                 grep "path=" | awk '{ print $NF }' |
8571                                 sed -e "s/^path=//g")
8572                         if [ $obj_file != $mntpt/$path ]; then
8573                                 echo "FAILED test_60a wrong obj path" \
8574                                       "$montpt/$path, should be $obj_file"
8575                                 pass=false
8576                                 break
8577                         fi
8578                 fi
8579         done
8580         rm -f $TMP/$tfile
8581         #restart mgs before "error", otherwise it will block the next test
8582         stop mgs || error "stop mgs failed"
8583         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
8584         $pass || error "test failed, see FAILED test_60a messages for specifics"
8585 }
8586 run_test 60a "llog_test run from kernel module and test llog_reader"
8587
8588 test_60b() { # bug 6411
8589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8590
8591         dmesg > $DIR/$tfile
8592         LLOG_COUNT=$(do_facet mgs dmesg |
8593                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
8594                           /llog_[a-z]*.c:[0-9]/ {
8595                                 if (marker)
8596                                         from_marker++
8597                                 from_begin++
8598                           }
8599                           END {
8600                                 if (marker)
8601                                         print from_marker
8602                                 else
8603                                         print from_begin
8604                           }")
8605
8606         [[ $LLOG_COUNT -gt 120 ]] &&
8607                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
8608 }
8609 run_test 60b "limit repeated messages from CERROR/CWARN"
8610
8611 test_60c() {
8612         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8613
8614         echo "create 5000 files"
8615         createmany -o $DIR/f60c- 5000
8616 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
8617         lctl set_param fail_loc=0x80000137
8618         unlinkmany $DIR/f60c- 5000
8619         lctl set_param fail_loc=0
8620 }
8621 run_test 60c "unlink file when mds full"
8622
8623 test_60d() {
8624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8625
8626         SAVEPRINTK=$(lctl get_param -n printk)
8627         # verify "lctl mark" is even working"
8628         MESSAGE="test message ID $RANDOM $$"
8629         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8630         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
8631
8632         lctl set_param printk=0 || error "set lnet.printk failed"
8633         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
8634         MESSAGE="new test message ID $RANDOM $$"
8635         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
8636         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8637         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
8638
8639         lctl set_param -n printk="$SAVEPRINTK"
8640 }
8641 run_test 60d "test printk console message masking"
8642
8643 test_60e() {
8644         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8645         remote_mds_nodsh && skip "remote MDS with nodsh"
8646
8647         touch $DIR/$tfile
8648 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
8649         do_facet mds1 lctl set_param fail_loc=0x15b
8650         rm $DIR/$tfile
8651 }
8652 run_test 60e "no space while new llog is being created"
8653
8654 test_60f() {
8655         local old_path=$($LCTL get_param -n debug_path)
8656
8657         stack_trap "$LCTL set_param debug_path=$old_path"
8658         stack_trap "rm -f $TMP/$tfile*"
8659         rm -f $TMP/$tfile* 2> /dev/null
8660         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8661         $LCTL set_param debug_path=$TMP/$tfile fail_loc=0x8000050e
8662         test_mkdir $DIR/$tdir
8663         # retry in case the open is cached and not released
8664         for (( i = 0; i < 100 && $(ls $TMP/$tfile* | wc -l) == 0; i++ )); do
8665                 echo $i > $DIR/$tdir/$tfile.$i && cat $DIR/$tdir/$tfile.$i
8666                 sleep 0.1
8667         done
8668         ls $TMP/$tfile*
8669         (( $(ls $TMP/$tfile* | wc -l) > 0 )) || error "$TMP/$tfile not dumped"
8670 }
8671 run_test 60f "change debug_path works"
8672
8673 test_60g() {
8674         local pid
8675         local i
8676
8677         test_mkdir -c $MDSCOUNT $DIR/$tdir
8678
8679         (
8680                 local index=0
8681                 while true; do
8682                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
8683                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
8684                                 2>/dev/null
8685                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
8686                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
8687                         index=$((index + 1))
8688                 done
8689         ) &
8690
8691         pid=$!
8692
8693         for i in {0..100}; do
8694                 # define OBD_FAIL_OSD_TXN_START    0x19a
8695                 local index=$((i % MDSCOUNT + 1))
8696
8697                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
8698                         > /dev/null
8699                 sleep 0.01
8700         done
8701
8702         kill -9 $pid
8703
8704         for i in $(seq $MDSCOUNT); do
8705                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
8706         done
8707
8708         mkdir $DIR/$tdir/new || error "mkdir failed"
8709         rmdir $DIR/$tdir/new || error "rmdir failed"
8710
8711         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
8712                 -t namespace
8713         for i in $(seq $MDSCOUNT); do
8714                 wait_update_facet mds$i "$LCTL get_param -n \
8715                         mdd.$(facet_svc mds$i).lfsck_namespace |
8716                         awk '/^status/ { print \\\$2 }'" "completed"
8717         done
8718
8719         ls -R $DIR/$tdir
8720         rm -rf $DIR/$tdir || error "rmdir failed"
8721 }
8722 run_test 60g "transaction abort won't cause MDT hung"
8723
8724 test_60h() {
8725         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
8726                 skip "Need MDS version at least 2.12.52"
8727         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
8728
8729         local f
8730
8731         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
8732         #define OBD_FAIL_MDS_STRIPE_FID          0x189
8733         for fail_loc in 0x80000188 0x80000189; do
8734                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
8735                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
8736                         error "mkdir $dir-$fail_loc failed"
8737                 for i in {0..10}; do
8738                         # create may fail on missing stripe
8739                         echo $i > $DIR/$tdir-$fail_loc/$i
8740                 done
8741                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8742                         error "getdirstripe $tdir-$fail_loc failed"
8743                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
8744                         error "migrate $tdir-$fail_loc failed"
8745                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8746                         error "getdirstripe $tdir-$fail_loc failed"
8747                 pushd $DIR/$tdir-$fail_loc
8748                 for f in *; do
8749                         echo $f | cmp $f - || error "$f data mismatch"
8750                 done
8751                 popd
8752                 rm -rf $DIR/$tdir-$fail_loc
8753         done
8754 }
8755 run_test 60h "striped directory with missing stripes can be accessed"
8756
8757 function t60i_load() {
8758         mkdir $DIR/$tdir
8759         #define OBD_FAIL_LLOG_PAUSE_AFTER_PAD               0x131c
8760         $LCTL set_param fail_loc=0x131c fail_val=1
8761         for ((i=0; i<5000; i++)); do
8762                 touch $DIR/$tdir/f$i
8763         done
8764 }
8765
8766 test_60i() {
8767         changelog_register || error "changelog_register failed"
8768         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
8769         changelog_users $SINGLEMDS | grep -q $cl_user ||
8770                 error "User $cl_user not found in changelog_users"
8771         changelog_chmask "ALL"
8772         t60i_load &
8773         local PID=$!
8774         for((i=0; i<100; i++)); do
8775                 changelog_dump >/dev/null ||
8776                         error "can't read changelog"
8777         done
8778         kill $PID
8779         wait $PID
8780         changelog_deregister || error "changelog_deregister failed"
8781         $LCTL set_param fail_loc=0
8782 }
8783 run_test 60i "llog: new record vs reader race"
8784
8785 test_61a() {
8786         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8787
8788         f="$DIR/f61"
8789         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
8790         cancel_lru_locks osc
8791         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
8792         sync
8793 }
8794 run_test 61a "mmap() writes don't make sync hang ================"
8795
8796 test_61b() {
8797         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
8798 }
8799 run_test 61b "mmap() of unstriped file is successful"
8800
8801 # bug 2330 - insufficient obd_match error checking causes LBUG
8802 test_62() {
8803         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8804
8805         f="$DIR/f62"
8806         echo foo > $f
8807         cancel_lru_locks osc
8808         lctl set_param fail_loc=0x405
8809         cat $f && error "cat succeeded, expect -EIO"
8810         lctl set_param fail_loc=0
8811 }
8812 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
8813 # match every page all of the time.
8814 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
8815
8816 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
8817 # Though this test is irrelevant anymore, it helped to reveal some
8818 # other grant bugs (LU-4482), let's keep it.
8819 test_63a() {   # was test_63
8820         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8821
8822         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
8823
8824         for i in `seq 10` ; do
8825                 dd if=/dev/zero of=$DIR/f63 bs=8k &
8826                 sleep 5
8827                 kill $!
8828                 sleep 1
8829         done
8830
8831         rm -f $DIR/f63 || true
8832 }
8833 run_test 63a "Verify oig_wait interruption does not crash ======="
8834
8835 # bug 2248 - async write errors didn't return to application on sync
8836 # bug 3677 - async write errors left page locked
8837 test_63b() {
8838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8839
8840         debugsave
8841         lctl set_param debug=-1
8842
8843         # ensure we have a grant to do async writes
8844         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
8845         rm $DIR/$tfile
8846
8847         sync    # sync lest earlier test intercept the fail_loc
8848
8849         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8850         lctl set_param fail_loc=0x80000406
8851         $MULTIOP $DIR/$tfile Owy && \
8852                 error "sync didn't return ENOMEM"
8853         sync; sleep 2; sync     # do a real sync this time to flush page
8854         lctl get_param -n llite.*.dump_page_cache | grep locked && \
8855                 error "locked page left in cache after async error" || true
8856         debugrestore
8857 }
8858 run_test 63b "async write errors should be returned to fsync ==="
8859
8860 test_64a () {
8861         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8862
8863         lfs df $DIR
8864         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
8865 }
8866 run_test 64a "verify filter grant calculations (in kernel) ====="
8867
8868 test_64b () {
8869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8870
8871         bash oos.sh $MOUNT || error "oos.sh failed: $?"
8872 }
8873 run_test 64b "check out-of-space detection on client"
8874
8875 test_64c() {
8876         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
8877 }
8878 run_test 64c "verify grant shrink"
8879
8880 import_param() {
8881         local tgt=$1
8882         local param=$2
8883
8884         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
8885 }
8886
8887 # this does exactly what osc_request.c:osc_announce_cached() does in
8888 # order to calculate max amount of grants to ask from server
8889 want_grant() {
8890         local tgt=$1
8891
8892         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
8893         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
8894
8895         ((rpc_in_flight++));
8896         nrpages=$((nrpages * rpc_in_flight))
8897
8898         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
8899
8900         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
8901
8902         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
8903         local undirty=$((nrpages * PAGE_SIZE))
8904
8905         local max_extent_pages
8906         max_extent_pages=$(import_param $tgt grant_max_extent_size)
8907         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
8908         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
8909         local grant_extent_tax
8910         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8911
8912         undirty=$((undirty + nrextents * grant_extent_tax))
8913
8914         echo $undirty
8915 }
8916
8917 # this is size of unit for grant allocation. It should be equal to
8918 # what tgt_grant.c:tgt_grant_chunk() calculates
8919 grant_chunk() {
8920         local tgt=$1
8921         local max_brw_size
8922         local grant_extent_tax
8923
8924         max_brw_size=$(import_param $tgt max_brw_size)
8925
8926         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8927
8928         echo $(((max_brw_size + grant_extent_tax) * 2))
8929 }
8930
8931 test_64d() {
8932         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
8933                 skip "OST < 2.10.55 doesn't limit grants enough"
8934
8935         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
8936
8937         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
8938                 skip "no grant_param connect flag"
8939
8940         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8941
8942         $LCTL set_param -n -n debug="$OLDDEBUG" || true
8943         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8944
8945
8946         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
8947         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
8948
8949         $LFS setstripe $DIR/$tfile -i 0 -c 1
8950         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
8951         ddpid=$!
8952
8953         while kill -0 $ddpid; do
8954                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8955
8956                 if [[ $cur_grant -gt $max_cur_granted ]]; then
8957                         kill $ddpid
8958                         error "cur_grant $cur_grant > $max_cur_granted"
8959                 fi
8960
8961                 sleep 1
8962         done
8963 }
8964 run_test 64d "check grant limit exceed"
8965
8966 check_grants() {
8967         local tgt=$1
8968         local expected=$2
8969         local msg=$3
8970         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8971
8972         ((cur_grants == expected)) ||
8973                 error "$msg: grants mismatch: $cur_grants, expected $expected"
8974 }
8975
8976 round_up_p2() {
8977         echo $((($1 + $2 - 1) & ~($2 - 1)))
8978 }
8979
8980 test_64e() {
8981         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8982         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
8983                 skip "Need OSS version at least 2.11.56"
8984
8985         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8986         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8987         $LCTL set_param debug=+cache
8988
8989         # Remount client to reset grant
8990         remount_client $MOUNT || error "failed to remount client"
8991         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8992
8993         local init_grants=$(import_param $osc_tgt initial_grant)
8994
8995         check_grants $osc_tgt $init_grants "init grants"
8996
8997         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
8998         local max_brw_size=$(import_param $osc_tgt max_brw_size)
8999         local gbs=$(import_param $osc_tgt grant_block_size)
9000
9001         # write random number of bytes from max_brw_size / 4 to max_brw_size
9002         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
9003         # align for direct io
9004         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
9005         # round to grant consumption unit
9006         local wb_round_up=$(round_up_p2 $write_bytes gbs)
9007
9008         local grants=$((wb_round_up + extent_tax))
9009
9010         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
9011
9012         # define OBD_FAIL_TGT_NO_GRANT 0x725
9013         # make the server not grant more back
9014         do_facet ost1 $LCTL set_param fail_loc=0x725
9015         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
9016
9017         do_facet ost1 $LCTL set_param fail_loc=0
9018
9019         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
9020
9021         rm -f $DIR/$tfile || error "rm failed"
9022
9023         # Remount client to reset grant
9024         remount_client $MOUNT || error "failed to remount client"
9025         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9026
9027         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9028
9029         # define OBD_FAIL_TGT_NO_GRANT 0x725
9030         # make the server not grant more back
9031         do_facet ost1 $LCTL set_param fail_loc=0x725
9032         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
9033         do_facet ost1 $LCTL set_param fail_loc=0
9034
9035         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
9036 }
9037 run_test 64e "check grant consumption (no grant allocation)"
9038
9039 test_64f() {
9040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9041
9042         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
9043         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
9044         $LCTL set_param debug=+cache
9045
9046         # Remount client to reset grant
9047         remount_client $MOUNT || error "failed to remount client"
9048         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9049
9050         local init_grants=$(import_param $osc_tgt initial_grant)
9051         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
9052         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9053         local gbs=$(import_param $osc_tgt grant_block_size)
9054         local chunk=$(grant_chunk $osc_tgt)
9055
9056         # write random number of bytes from max_brw_size / 4 to max_brw_size
9057         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
9058         # align for direct io
9059         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
9060         # round to grant consumption unit
9061         local wb_round_up=$(round_up_p2 $write_bytes gbs)
9062
9063         local grants=$((wb_round_up + extent_tax))
9064
9065         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9066         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
9067                 error "error writing to $DIR/$tfile"
9068
9069         check_grants $osc_tgt $((init_grants - grants + chunk)) \
9070                 "direct io with grant allocation"
9071
9072         rm -f $DIR/$tfile || error "rm failed"
9073
9074         # Remount client to reset grant
9075         remount_client $MOUNT || error "failed to remount client"
9076         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9077
9078         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9079
9080         local cmd="oO_WRONLY:w${write_bytes}_yc"
9081
9082         $MULTIOP $DIR/$tfile $cmd &
9083         MULTIPID=$!
9084         sleep 1
9085
9086         check_grants $osc_tgt $((init_grants - grants)) \
9087                 "buffered io, not write rpc"
9088
9089         kill -USR1 $MULTIPID
9090         wait
9091
9092         check_grants $osc_tgt $((init_grants - grants + chunk)) \
9093                 "buffered io, one RPC"
9094 }
9095 run_test 64f "check grant consumption (with grant allocation)"
9096
9097 test_64g() {
9098         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
9099                 skip "Need MDS version at least 2.14.56"
9100
9101         local mdts=$(comma_list $(mdts_nodes))
9102
9103         local old=$($LCTL get_param mdc.$FSNAME-*.grant_shrink_interval |
9104                         tr '\n' ' ')
9105         stack_trap "$LCTL set_param $old"
9106
9107         # generate dirty pages and increase dirty granted on MDT
9108         stack_trap "rm -f $DIR/$tfile-*"
9109         for (( i = 0; i < 10; i++)); do
9110                 $LFS setstripe -E 1M -L mdt $DIR/$tfile-$i ||
9111                         error "can't set stripe"
9112                 dd if=/dev/zero of=$DIR/$tfile-$i bs=128k count=1 ||
9113                         error "can't dd"
9114                 $LFS getstripe $DIR/$tfile-$i | grep -q pattern.*mdt || {
9115                         $LFS getstripe $DIR/$tfile-$i
9116                         error "not DoM file"
9117                 }
9118         done
9119
9120         # flush dirty pages
9121         sync
9122
9123         # wait until grant shrink reset grant dirty on MDTs
9124         for ((i = 0; i < 120; i++)); do
9125                 grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
9126                         awk '{sum=sum+$1} END {print sum}')
9127                 vm_dirty=$(awk '/Dirty:/{print $2}' /proc/meminfo)
9128                 echo "$grant_dirty grants, $vm_dirty pages"
9129                 (( grant_dirty + vm_dirty == 0 )) && break
9130                 (( i == 3 )) && sync &&
9131                         $LCTL set_param mdc.$FSNAME-*.grant_shrink_interval=5
9132                 sleep 1
9133         done
9134
9135         grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
9136                 awk '{sum=sum+$1} END {print sum}')
9137         (( grant_dirty == 0 )) || error "$grant_dirty on MDT"
9138 }
9139 run_test 64g "grant shrink on MDT"
9140
9141 test_64h() {
9142         (( $OST1_VERSION >= $(version_code 2.14.56) )) ||
9143                 skip "need OST at least 2.14.56 to avoid grant shrink on read"
9144
9145         local instance=$($LFS getname -i $DIR)
9146         local osc_tgt="$FSNAME-OST0000-osc-$instance"
9147         local num_exps=$(do_facet ost1 \
9148             $LCTL get_param -n obdfilter.*OST0000*.num_exports)
9149         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9150         local avail=$($LCTL get_param -n osc.*OST0000-osc-$instance.kbytesavail)
9151         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9152
9153         # 10MiB is for file to be written, max_brw_size * 16 *
9154         # num_exps is space reserve so that tgt_grant_shrink() decided
9155         # to not shrink
9156         local expect=$((max_brw_size * 16 * num_exps + 10 * 1048576))
9157         (( avail * 1024 < expect )) &&
9158                 skip "need $expect bytes on ost1, have $(( avail * 1024 )) only"
9159
9160         save_lustre_params client "osc.*OST0000*.grant_shrink" > $p
9161         save_lustre_params client "osc.*OST0000*.grant_shrink_interval" >> $p
9162         stack_trap "restore_lustre_params < $p; rm -f $save" EXIT
9163         $LCTL set_param osc.*OST0000*.grant_shrink=1
9164         $LCTL set_param osc.*OST0000*.grant_shrink_interval=10
9165
9166         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9167         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 oflag=sync
9168
9169         # drop cache so that coming read would do rpc
9170         cancel_lru_locks osc
9171
9172         # shrink interval is set to 10, pause for 7 seconds so that
9173         # grant thread did not wake up yet but coming read entered
9174         # shrink mode for rpc (osc_should_shrink_grant())
9175         sleep 7
9176
9177         declare -a cur_grant_bytes
9178         declare -a tot_granted
9179         cur_grant_bytes[0]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
9180         tot_granted[0]=$(do_facet ost1 \
9181             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
9182
9183         dd if=$DIR/$tfile bs=4K count=1 of=/dev/null
9184
9185         cur_grant_bytes[1]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
9186         tot_granted[1]=$(do_facet ost1 \
9187             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
9188
9189         # grant change should be equal on both sides
9190         (( cur_grant_bytes[0] - cur_grant_bytes[1] ==
9191                 tot_granted[0] - tot_granted[1])) ||
9192                 error "grant change mismatch, "                                \
9193                         "server: ${tot_granted[0]} to ${tot_granted[1]}, "     \
9194                         "client: ${cur_grant_bytes[0]} to ${cur_grant_bytes[1]}"
9195 }
9196 run_test 64h "grant shrink on read"
9197
9198 test_64i() {
9199         (( $OST1_VERSION >= $(version_code 2.14.56) )) ||
9200                 skip "need OST at least 2.14.56 to avoid grant shrink on replay"
9201
9202         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9203         remote_ost_nodsh && skip "remote OSTs with nodsh"
9204
9205         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9206
9207         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
9208
9209         # lustre-ffff9fc75e850800 /mnt/lustre -> ffff9fc75e850800
9210         local instance=$($LFS getname -i $DIR)
9211
9212         local osc_tgt="$FSNAME-OST0000-osc-$instance"
9213         local cgb=$($LCTL get_param -n osc.$osc_tgt.cur_grant_bytes)
9214
9215         # shrink grants and simulate rpc loss
9216         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC     0x513
9217         do_facet ost1 "$LCTL set_param fail_loc=0x80000513 fail_val=17"
9218         $LCTL set_param osc.$osc_tgt.cur_grant_bytes=$((cgb/2))B
9219
9220         fail ost1
9221
9222         dd if=/dev/zero of=$DIR/$tfile oflag=append bs=1M count=8 conv=notrunc
9223
9224         local testid=$(echo $TESTNAME | tr '_' ' ')
9225
9226         do_facet ost1 dmesg | tac | sed "/$testid/,$ d" |
9227                 grep "GRANT, real grant" &&
9228                 error "client has more grants then it owns" || true
9229 }
9230 run_test 64i "shrink on reconnect"
9231
9232 # bug 1414 - set/get directories' stripe info
9233 test_65a() {
9234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9235
9236         test_mkdir $DIR/$tdir
9237         touch $DIR/$tdir/f1
9238         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
9239 }
9240 run_test 65a "directory with no stripe info"
9241
9242 test_65b() {
9243         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9244
9245         test_mkdir $DIR/$tdir
9246         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9247
9248         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9249                                                 error "setstripe"
9250         touch $DIR/$tdir/f2
9251         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
9252 }
9253 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
9254
9255 test_65c() {
9256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9257         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
9258
9259         test_mkdir $DIR/$tdir
9260         local stripesize=$($LFS getstripe -S $DIR/$tdir)
9261
9262         $LFS setstripe -S $((stripesize * 4)) -i 1 \
9263                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
9264         touch $DIR/$tdir/f3
9265         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
9266 }
9267 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
9268
9269 test_65d() {
9270         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9271
9272         test_mkdir $DIR/$tdir
9273         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
9274         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9275
9276         if [[ $STRIPECOUNT -le 0 ]]; then
9277                 sc=1
9278         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
9279                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
9280                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
9281         else
9282                 sc=$(($STRIPECOUNT - 1))
9283         fi
9284         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
9285         touch $DIR/$tdir/f4 $DIR/$tdir/f5
9286         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
9287                 error "lverify failed"
9288 }
9289 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
9290
9291 test_65e() {
9292         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9293
9294         test_mkdir $DIR/$tdir
9295
9296         $LFS setstripe $DIR/$tdir || error "setstripe"
9297         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
9298                                         error "no stripe info failed"
9299         touch $DIR/$tdir/f6
9300         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
9301 }
9302 run_test 65e "directory setstripe defaults"
9303
9304 test_65f() {
9305         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9306
9307         test_mkdir $DIR/${tdir}f
9308         $RUNAS $LFS setstripe $DIR/${tdir}f &&
9309                 error "setstripe succeeded" || true
9310 }
9311 run_test 65f "dir setstripe permission (should return error) ==="
9312
9313 test_65g() {
9314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9315
9316         test_mkdir $DIR/$tdir
9317         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9318
9319         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9320                 error "setstripe -S failed"
9321         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
9322         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
9323                 error "delete default stripe failed"
9324 }
9325 run_test 65g "directory setstripe -d"
9326
9327 test_65h() {
9328         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9329
9330         test_mkdir $DIR/$tdir
9331         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9332
9333         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9334                 error "setstripe -S failed"
9335         test_mkdir $DIR/$tdir/dd1
9336         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
9337                 error "stripe info inherit failed"
9338 }
9339 run_test 65h "directory stripe info inherit ===================="
9340
9341 test_65i() {
9342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9343
9344         save_layout_restore_at_exit $MOUNT
9345
9346         # bug6367: set non-default striping on root directory
9347         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
9348
9349         # bug12836: getstripe on -1 default directory striping
9350         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
9351
9352         # bug12836: getstripe -v on -1 default directory striping
9353         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
9354
9355         # bug12836: new find on -1 default directory striping
9356         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
9357 }
9358 run_test 65i "various tests to set root directory striping"
9359
9360 test_65j() { # bug6367
9361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9362
9363         sync; sleep 1
9364
9365         # if we aren't already remounting for each test, do so for this test
9366         if [ "$I_MOUNTED" = "yes" ]; then
9367                 cleanup || error "failed to unmount"
9368                 setup
9369         fi
9370
9371         save_layout_restore_at_exit $MOUNT
9372
9373         $LFS setstripe -d $MOUNT || error "setstripe failed"
9374 }
9375 run_test 65j "set default striping on root directory (bug 6367)="
9376
9377 cleanup_65k() {
9378         rm -rf $DIR/$tdir
9379         wait_delete_completed
9380         do_facet $SINGLEMDS "lctl set_param -n \
9381                 osp.$ost*MDT0000.max_create_count=$max_count"
9382         do_facet $SINGLEMDS "lctl set_param -n \
9383                 osp.$ost*MDT0000.create_count=$count"
9384         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9385         echo $INACTIVE_OSC "is Activate"
9386
9387         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9388 }
9389
9390 test_65k() { # bug11679
9391         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9392         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9393         remote_mds_nodsh && skip "remote MDS with nodsh"
9394
9395         local disable_precreate=true
9396         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
9397                 disable_precreate=false
9398
9399         echo "Check OST status: "
9400         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
9401                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
9402
9403         for OSC in $MDS_OSCS; do
9404                 echo $OSC "is active"
9405                 do_facet $SINGLEMDS lctl --device %$OSC activate
9406         done
9407
9408         for INACTIVE_OSC in $MDS_OSCS; do
9409                 local ost=$(osc_to_ost $INACTIVE_OSC)
9410                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
9411                                lov.*md*.target_obd |
9412                                awk -F: /$ost/'{ print $1 }' | head -n 1)
9413
9414                 mkdir -p $DIR/$tdir
9415                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
9416                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
9417
9418                 echo "Deactivate: " $INACTIVE_OSC
9419                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
9420
9421                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
9422                               osp.$ost*MDT0000.create_count")
9423                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
9424                                   osp.$ost*MDT0000.max_create_count")
9425                 $disable_precreate &&
9426                         do_facet $SINGLEMDS "lctl set_param -n \
9427                                 osp.$ost*MDT0000.max_create_count=0"
9428
9429                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
9430                         [ -f $DIR/$tdir/$idx ] && continue
9431                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
9432                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
9433                                 { cleanup_65k;
9434                                   error "setstripe $idx should succeed"; }
9435                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
9436                 done
9437                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
9438                 rmdir $DIR/$tdir
9439
9440                 do_facet $SINGLEMDS "lctl set_param -n \
9441                         osp.$ost*MDT0000.max_create_count=$max_count"
9442                 do_facet $SINGLEMDS "lctl set_param -n \
9443                         osp.$ost*MDT0000.create_count=$count"
9444                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9445                 echo $INACTIVE_OSC "is Activate"
9446
9447                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9448         done
9449 }
9450 run_test 65k "validate manual striping works properly with deactivated OSCs"
9451
9452 test_65l() { # bug 12836
9453         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9454
9455         test_mkdir -p $DIR/$tdir/test_dir
9456         $LFS setstripe -c -1 $DIR/$tdir/test_dir
9457         $LFS find -mtime -1 $DIR/$tdir >/dev/null
9458 }
9459 run_test 65l "lfs find on -1 stripe dir ========================"
9460
9461 test_65m() {
9462         local layout=$(save_layout $MOUNT)
9463         $RUNAS $LFS setstripe -c 2 $MOUNT && {
9464                 restore_layout $MOUNT $layout
9465                 error "setstripe should fail by non-root users"
9466         }
9467         true
9468 }
9469 run_test 65m "normal user can't set filesystem default stripe"
9470
9471 test_65n() {
9472         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
9473         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
9474                 skip "Need MDS version at least 2.12.50"
9475         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
9476
9477         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
9478         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
9479         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
9480
9481         save_layout_restore_at_exit $MOUNT
9482
9483         # new subdirectory under root directory should not inherit
9484         # the default layout from root
9485         local dir1=$MOUNT/$tdir-1
9486         mkdir $dir1 || error "mkdir $dir1 failed"
9487         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
9488                 error "$dir1 shouldn't have LOV EA"
9489
9490         # delete the default layout on root directory
9491         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
9492
9493         local dir2=$MOUNT/$tdir-2
9494         mkdir $dir2 || error "mkdir $dir2 failed"
9495         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
9496                 error "$dir2 shouldn't have LOV EA"
9497
9498         # set a new striping pattern on root directory
9499         local def_stripe_size=$($LFS getstripe -S $MOUNT)
9500         local new_def_stripe_size=$((def_stripe_size * 2))
9501         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
9502                 error "set stripe size on $MOUNT failed"
9503
9504         # new file created in $dir2 should inherit the new stripe size from
9505         # the filesystem default
9506         local file2=$dir2/$tfile-2
9507         touch $file2 || error "touch $file2 failed"
9508
9509         local file2_stripe_size=$($LFS getstripe -S $file2)
9510         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
9511         {
9512                 echo "file2_stripe_size: '$file2_stripe_size'"
9513                 echo "new_def_stripe_size: '$new_def_stripe_size'"
9514                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
9515         }
9516
9517         local dir3=$MOUNT/$tdir-3
9518         mkdir $dir3 || error "mkdir $dir3 failed"
9519         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
9520         # the root layout, which is the actual default layout that will be used
9521         # when new files are created in $dir3.
9522         local dir3_layout=$(get_layout_param $dir3)
9523         local root_dir_layout=$(get_layout_param $MOUNT)
9524         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
9525         {
9526                 echo "dir3_layout: '$dir3_layout'"
9527                 echo "root_dir_layout: '$root_dir_layout'"
9528                 error "$dir3 should show the default layout from $MOUNT"
9529         }
9530
9531         # set OST pool on root directory
9532         local pool=$TESTNAME
9533         pool_add $pool || error "add $pool failed"
9534         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
9535                 error "add targets to $pool failed"
9536
9537         $LFS setstripe -p $pool $MOUNT ||
9538                 error "set OST pool on $MOUNT failed"
9539
9540         # new file created in $dir3 should inherit the pool from
9541         # the filesystem default
9542         local file3=$dir3/$tfile-3
9543         touch $file3 || error "touch $file3 failed"
9544
9545         local file3_pool=$($LFS getstripe -p $file3)
9546         [[ "$file3_pool" = "$pool" ]] ||
9547                 error "$file3 ('$file3_pool') didn't inherit OST pool '$pool'"
9548
9549         local dir4=$MOUNT/$tdir-4
9550         mkdir $dir4 || error "mkdir $dir4 failed"
9551         local dir4_layout=$(get_layout_param $dir4)
9552         root_dir_layout=$(get_layout_param $MOUNT)
9553         echo "$LFS getstripe -d $dir4"
9554         $LFS getstripe -d $dir4
9555         echo "$LFS getstripe -d $MOUNT"
9556         $LFS getstripe -d $MOUNT
9557         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
9558         {
9559                 echo "dir4_layout: '$dir4_layout'"
9560                 echo "root_dir_layout: '$root_dir_layout'"
9561                 error "$dir4 should show the default layout from $MOUNT"
9562         }
9563
9564         # new file created in $dir4 should inherit the pool from
9565         # the filesystem default
9566         local file4=$dir4/$tfile-4
9567         touch $file4 || error "touch $file4 failed"
9568
9569         local file4_pool=$($LFS getstripe -p $file4)
9570         [[ "$file4_pool" = "$pool" ]] ||
9571                 error "$file4 ('$file4_pool') didn't inherit OST pool $pool"
9572
9573         # new subdirectory under non-root directory should inherit
9574         # the default layout from its parent directory
9575         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
9576                 error "set directory layout on $dir4 failed"
9577
9578         local dir5=$dir4/$tdir-5
9579         mkdir $dir5 || error "mkdir $dir5 failed"
9580
9581         dir4_layout=$(get_layout_param $dir4)
9582         local dir5_layout=$(get_layout_param $dir5)
9583         [[ "$dir4_layout" = "$dir5_layout" ]] ||
9584         {
9585                 echo "dir4_layout: '$dir4_layout'"
9586                 echo "dir5_layout: '$dir5_layout'"
9587                 error "$dir5 should inherit the default layout from $dir4"
9588         }
9589
9590         # though subdir under ROOT doesn't inherit default layout, but
9591         # its sub dir/file should be created with default layout.
9592         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
9593         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
9594                 skip "Need MDS version at least 2.12.59"
9595
9596         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
9597         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
9598         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
9599
9600         if [ $default_lmv_hash == "none" ]; then
9601                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
9602         else
9603                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
9604                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
9605         fi
9606
9607         $LFS setdirstripe -D -c 2 $MOUNT ||
9608                 error "setdirstripe -D -c 2 failed"
9609         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
9610         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
9611         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
9612
9613         # $dir4 layout includes pool
9614         $LFS setstripe -S $((new_def_stripe_size * 2)) $dir4
9615         [[ "$pool" = $($LFS getstripe -p -d $dir4) ]] ||
9616                 error "pool lost on setstripe"
9617         $LFS setstripe -E -1 -S $new_def_stripe_size $dir4
9618         [[ "$pool" = $($LFS getstripe -p -d $dir4) ]] ||
9619                 error "pool lost on compound layout setstripe"
9620 }
9621 run_test 65n "don't inherit default layout from root for new subdirectories"
9622
9623 # bug 2543 - update blocks count on client
9624 test_66() {
9625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9626
9627         local COUNT=${COUNT:-8}
9628         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
9629         sync; sync_all_data; sync; sync_all_data
9630         cancel_lru_locks osc
9631         local BLOCKS=$(ls -s --block-size=1k $DIR/f66 | awk '{ print $1 }')
9632         (( BLOCKS >= COUNT )) || error "$DIR/f66 blocks $BLOCKS < $COUNT"
9633 }
9634 run_test 66 "update inode blocks count on client ==============="
9635
9636 meminfo() {
9637         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
9638 }
9639
9640 swap_used() {
9641         swapon -s | awk '($1 == "'$1'") { print $4 }'
9642 }
9643
9644 # bug5265, obdfilter oa2dentry return -ENOENT
9645 # #define OBD_FAIL_SRV_ENOENT 0x217
9646 test_69() {
9647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9648         remote_ost_nodsh && skip "remote OST with nodsh"
9649
9650         f="$DIR/$tfile"
9651         $LFS setstripe -c 1 -i 0 $f
9652
9653         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
9654
9655         do_facet ost1 lctl set_param fail_loc=0x217
9656         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
9657         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
9658
9659         do_facet ost1 lctl set_param fail_loc=0
9660         $DIRECTIO write $f 0 2 || error "write error"
9661
9662         cancel_lru_locks osc
9663         $DIRECTIO read $f 0 1 || error "read error"
9664
9665         do_facet ost1 lctl set_param fail_loc=0x217
9666         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
9667
9668         do_facet ost1 lctl set_param fail_loc=0
9669         rm -f $f
9670 }
9671 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
9672
9673 test_71() {
9674         test_mkdir $DIR/$tdir
9675         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
9676         bash rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
9677 }
9678 run_test 71 "Running dbench on lustre (don't segment fault) ===="
9679
9680 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
9681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9682         [ "$RUNAS_ID" = "$UID" ] &&
9683                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9684         # Check that testing environment is properly set up. Skip if not
9685         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
9686                 skip_env "User $RUNAS_ID does not exist - skipping"
9687
9688         touch $DIR/$tfile
9689         chmod 777 $DIR/$tfile
9690         chmod ug+s $DIR/$tfile
9691         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
9692                 error "$RUNAS dd $DIR/$tfile failed"
9693         # See if we are still setuid/sgid
9694         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9695                 error "S/gid is not dropped on write"
9696         # Now test that MDS is updated too
9697         cancel_lru_locks mdc
9698         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9699                 error "S/gid is not dropped on MDS"
9700         rm -f $DIR/$tfile
9701 }
9702 run_test 72a "Test that remove suid works properly (bug5695) ===="
9703
9704 test_72b() { # bug 24226 -- keep mode setting when size is not changing
9705         local perm
9706
9707         [ "$RUNAS_ID" = "$UID" ] &&
9708                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9709         [ "$RUNAS_ID" -eq 0 ] &&
9710                 skip_env "RUNAS_ID = 0 -- skipping"
9711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9712         # Check that testing environment is properly set up. Skip if not
9713         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
9714                 skip_env "User $RUNAS_ID does not exist - skipping"
9715
9716         touch $DIR/${tfile}-f{g,u}
9717         test_mkdir $DIR/${tfile}-dg
9718         test_mkdir $DIR/${tfile}-du
9719         chmod 770 $DIR/${tfile}-{f,d}{g,u}
9720         chmod g+s $DIR/${tfile}-{f,d}g
9721         chmod u+s $DIR/${tfile}-{f,d}u
9722         for perm in 777 2777 4777; do
9723                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
9724                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
9725                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
9726                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
9727         done
9728         true
9729 }
9730 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
9731
9732 # bug 3462 - multiple simultaneous MDC requests
9733 test_73() {
9734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9735
9736         test_mkdir $DIR/d73-1
9737         test_mkdir $DIR/d73-2
9738         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
9739         pid1=$!
9740
9741         lctl set_param fail_loc=0x80000129
9742         $MULTIOP $DIR/d73-1/f73-2 Oc &
9743         sleep 1
9744         lctl set_param fail_loc=0
9745
9746         $MULTIOP $DIR/d73-2/f73-3 Oc &
9747         pid3=$!
9748
9749         kill -USR1 $pid1
9750         wait $pid1 || return 1
9751
9752         sleep 25
9753
9754         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
9755         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
9756         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
9757
9758         rm -rf $DIR/d73-*
9759 }
9760 run_test 73 "multiple MDC requests (should not deadlock)"
9761
9762 test_74a() { # bug 6149, 6184
9763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9764
9765         touch $DIR/f74a
9766         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9767         #
9768         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9769         # will spin in a tight reconnection loop
9770         $LCTL set_param fail_loc=0x8000030e
9771         # get any lock that won't be difficult - lookup works.
9772         ls $DIR/f74a
9773         $LCTL set_param fail_loc=0
9774         rm -f $DIR/f74a
9775         true
9776 }
9777 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
9778
9779 test_74b() { # bug 13310
9780         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9781
9782         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9783         #
9784         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9785         # will spin in a tight reconnection loop
9786         $LCTL set_param fail_loc=0x8000030e
9787         # get a "difficult" lock
9788         touch $DIR/f74b
9789         $LCTL set_param fail_loc=0
9790         rm -f $DIR/f74b
9791         true
9792 }
9793 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
9794
9795 test_74c() {
9796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9797
9798         #define OBD_FAIL_LDLM_NEW_LOCK
9799         $LCTL set_param fail_loc=0x319
9800         touch $DIR/$tfile && error "touch successful"
9801         $LCTL set_param fail_loc=0
9802         true
9803 }
9804 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
9805
9806 slab_lic=/sys/kernel/slab/lustre_inode_cache
9807 num_objects() {
9808         [ -f $slab_lic/shrink ] && echo 1 > $slab_lic/shrink
9809         [ -f $slab_lic/objects ] && awk '{ print $1 }' $slab_lic/objects ||
9810                 awk '/lustre_inode_cache/ { print $2; exit }' /proc/slabinfo
9811 }
9812
9813 test_76a() { # Now for b=20433, added originally in b=1443
9814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9815
9816         cancel_lru_locks osc
9817         # there may be some slab objects cached per core
9818         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
9819         local before=$(num_objects)
9820         local count=$((512 * cpus))
9821         [ "$SLOW" = "no" ] && count=$((128 * cpus))
9822         local margin=$((count / 10))
9823         if [[ -f $slab_lic/aliases ]]; then
9824                 local aliases=$(cat $slab_lic/aliases)
9825                 (( aliases > 0 )) && margin=$((margin * aliases))
9826         fi
9827
9828         echo "before slab objects: $before"
9829         for i in $(seq $count); do
9830                 touch $DIR/$tfile
9831                 rm -f $DIR/$tfile
9832         done
9833         cancel_lru_locks osc
9834         local after=$(num_objects)
9835         echo "created: $count, after slab objects: $after"
9836         # shared slab counts are not very accurate, allow significant margin
9837         # the main goal is that the cache growth is not permanently > $count
9838         while (( after > before + margin )); do
9839                 sleep 1
9840                 after=$(num_objects)
9841                 wait=$((wait + 1))
9842                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9843                 if (( wait > 60 )); then
9844                         error "inode slab grew from $before+$margin to $after"
9845                 fi
9846         done
9847 }
9848 run_test 76a "confirm clients recycle inodes properly ===="
9849
9850 test_76b() {
9851         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9852         [ $CLIENT_VERSION -ge $(version_code 2.13.55) ] || skip "not supported"
9853
9854         local count=512
9855         local before=$(num_objects)
9856
9857         for i in $(seq $count); do
9858                 mkdir $DIR/$tdir
9859                 rmdir $DIR/$tdir
9860         done
9861
9862         local after=$(num_objects)
9863         local wait=0
9864
9865         while (( after > before )); do
9866                 sleep 1
9867                 after=$(num_objects)
9868                 wait=$((wait + 1))
9869                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9870                 if (( wait > 60 )); then
9871                         error "inode slab grew from $before to $after"
9872                 fi
9873         done
9874
9875         echo "slab objects before: $before, after: $after"
9876 }
9877 run_test 76b "confirm clients recycle directory inodes properly ===="
9878
9879 export ORIG_CSUM=""
9880 set_checksums()
9881 {
9882         # Note: in sptlrpc modes which enable its own bulk checksum, the
9883         # original crc32_le bulk checksum will be automatically disabled,
9884         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
9885         # will be checked by sptlrpc code against sptlrpc bulk checksum.
9886         # In this case set_checksums() will not be no-op, because sptlrpc
9887         # bulk checksum will be enabled all through the test.
9888
9889         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
9890         lctl set_param -n osc.*.checksums $1
9891         return 0
9892 }
9893
9894 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9895                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
9896 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9897                              tr -d [] | head -n1)}
9898 set_checksum_type()
9899 {
9900         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
9901         rc=$?
9902         log "set checksum type to $1, rc = $rc"
9903         return $rc
9904 }
9905
9906 get_osc_checksum_type()
9907 {
9908         # arugment 1: OST name, like OST0000
9909         ost=$1
9910         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
9911                         sed 's/.*\[\(.*\)\].*/\1/g')
9912         rc=$?
9913         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
9914         echo $checksum_type
9915 }
9916
9917 F77_TMP=$TMP/f77-temp
9918 F77SZ=8
9919 setup_f77() {
9920         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
9921                 error "error writing to $F77_TMP"
9922 }
9923
9924 test_77a() { # bug 10889
9925         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9926         $GSS && skip_env "could not run with gss"
9927
9928         [ ! -f $F77_TMP ] && setup_f77
9929         set_checksums 1
9930         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
9931         set_checksums 0
9932         rm -f $DIR/$tfile
9933 }
9934 run_test 77a "normal checksum read/write operation"
9935
9936 test_77b() { # bug 10889
9937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9938         $GSS && skip_env "could not run with gss"
9939
9940         [ ! -f $F77_TMP ] && setup_f77
9941         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
9942         $LCTL set_param fail_loc=0x80000409
9943         set_checksums 1
9944
9945         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9946                 error "dd error: $?"
9947         $LCTL set_param fail_loc=0
9948
9949         for algo in $CKSUM_TYPES; do
9950                 cancel_lru_locks osc
9951                 set_checksum_type $algo
9952                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
9953                 $LCTL set_param fail_loc=0x80000408
9954                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
9955                 $LCTL set_param fail_loc=0
9956         done
9957         set_checksums 0
9958         set_checksum_type $ORIG_CSUM_TYPE
9959         rm -f $DIR/$tfile
9960 }
9961 run_test 77b "checksum error on client write, read"
9962
9963 cleanup_77c() {
9964         trap 0
9965         set_checksums 0
9966         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
9967         $check_ost &&
9968                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
9969         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
9970         $check_ost && [ -n "$ost_file_prefix" ] &&
9971                 do_facet ost1 rm -f ${ost_file_prefix}\*
9972 }
9973
9974 test_77c() {
9975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9976         $GSS && skip_env "could not run with gss"
9977         remote_ost_nodsh && skip "remote OST with nodsh"
9978
9979         local bad1
9980         local osc_file_prefix
9981         local osc_file
9982         local check_ost=false
9983         local ost_file_prefix
9984         local ost_file
9985         local orig_cksum
9986         local dump_cksum
9987         local fid
9988
9989         # ensure corruption will occur on first OSS/OST
9990         $LFS setstripe -i 0 $DIR/$tfile
9991
9992         [ ! -f $F77_TMP ] && setup_f77
9993         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
9994                 error "dd write error: $?"
9995         fid=$($LFS path2fid $DIR/$tfile)
9996
9997         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
9998         then
9999                 check_ost=true
10000                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
10001                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
10002         else
10003                 echo "OSS do not support bulk pages dump upon error"
10004         fi
10005
10006         osc_file_prefix=$($LCTL get_param -n debug_path)
10007         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
10008
10009         trap cleanup_77c EXIT
10010
10011         set_checksums 1
10012         # enable bulk pages dump upon error on Client
10013         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
10014         # enable bulk pages dump upon error on OSS
10015         $check_ost &&
10016                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
10017
10018         # flush Client cache to allow next read to reach OSS
10019         cancel_lru_locks osc
10020
10021         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
10022         $LCTL set_param fail_loc=0x80000408
10023         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
10024         $LCTL set_param fail_loc=0
10025
10026         rm -f $DIR/$tfile
10027
10028         # check cksum dump on Client
10029         osc_file=$(ls ${osc_file_prefix}*)
10030         [ -n "$osc_file" ] || error "no checksum dump file on Client"
10031         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
10032         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
10033         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
10034         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
10035                      cksum)
10036         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
10037         [[ "$orig_cksum" == "$dump_cksum" ]] ||
10038                 error "dump content does not match on Client"
10039
10040         $check_ost || skip "No need to check cksum dump on OSS"
10041
10042         # check cksum dump on OSS
10043         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
10044         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
10045         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
10046         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
10047         [[ "$orig_cksum" == "$dump_cksum" ]] ||
10048                 error "dump content does not match on OSS"
10049
10050         cleanup_77c
10051 }
10052 run_test 77c "checksum error on client read with debug"
10053
10054 test_77d() { # bug 10889
10055         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10056         $GSS && skip_env "could not run with gss"
10057
10058         stack_trap "rm -f $DIR/$tfile"
10059         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10060         $LCTL set_param fail_loc=0x80000409
10061         set_checksums 1
10062         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
10063                 error "direct write: rc=$?"
10064         $LCTL set_param fail_loc=0
10065         set_checksums 0
10066
10067         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
10068         $LCTL set_param fail_loc=0x80000408
10069         set_checksums 1
10070         cancel_lru_locks osc
10071         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
10072                 error "direct read: rc=$?"
10073         $LCTL set_param fail_loc=0
10074         set_checksums 0
10075 }
10076 run_test 77d "checksum error on OST direct write, read"
10077
10078 test_77f() { # bug 10889
10079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10080         $GSS && skip_env "could not run with gss"
10081
10082         set_checksums 1
10083         stack_trap "rm -f $DIR/$tfile"
10084         for algo in $CKSUM_TYPES; do
10085                 cancel_lru_locks osc
10086                 set_checksum_type $algo
10087                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10088                 $LCTL set_param fail_loc=0x409
10089                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
10090                         error "direct write succeeded"
10091                 $LCTL set_param fail_loc=0
10092         done
10093         set_checksum_type $ORIG_CSUM_TYPE
10094         set_checksums 0
10095 }
10096 run_test 77f "repeat checksum error on write (expect error)"
10097
10098 test_77g() { # bug 10889
10099         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10100         $GSS && skip_env "could not run with gss"
10101         remote_ost_nodsh && skip "remote OST with nodsh"
10102
10103         [ ! -f $F77_TMP ] && setup_f77
10104
10105         local file=$DIR/$tfile
10106         stack_trap "rm -f $file" EXIT
10107
10108         $LFS setstripe -c 1 -i 0 $file
10109         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
10110         do_facet ost1 lctl set_param fail_loc=0x8000021a
10111         set_checksums 1
10112         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
10113                 error "write error: rc=$?"
10114         do_facet ost1 lctl set_param fail_loc=0
10115         set_checksums 0
10116
10117         cancel_lru_locks osc
10118         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
10119         do_facet ost1 lctl set_param fail_loc=0x8000021b
10120         set_checksums 1
10121         cmp $F77_TMP $file || error "file compare failed"
10122         do_facet ost1 lctl set_param fail_loc=0
10123         set_checksums 0
10124 }
10125 run_test 77g "checksum error on OST write, read"
10126
10127 test_77k() { # LU-10906
10128         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10129         $GSS && skip_env "could not run with gss"
10130
10131         local cksum_param="osc.$FSNAME*.checksums"
10132         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
10133         local checksum
10134         local i
10135
10136         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
10137         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM || true"
10138         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM"
10139
10140         for i in 0 1; do
10141                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
10142                         error "failed to set checksum=$i on MGS"
10143                 wait_update $HOSTNAME "$get_checksum" $i
10144                 #remount
10145                 echo "remount client, checksum should be $i"
10146                 remount_client $MOUNT || error "failed to remount client"
10147                 checksum=$(eval $get_checksum)
10148                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
10149         done
10150         # remove persistent param to avoid races with checksum mountopt below
10151         do_facet mgs $LCTL set_param -P -d $cksum_param ||
10152                 error "failed to delete checksum on MGS"
10153
10154         for opt in "checksum" "nochecksum"; do
10155                 #remount with mount option
10156                 echo "remount client with option $opt, checksum should be $i"
10157                 umount_client $MOUNT || error "failed to umount client"
10158                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
10159                         error "failed to mount client with option '$opt'"
10160                 checksum=$(eval $get_checksum)
10161                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
10162                 i=$((i - 1))
10163         done
10164
10165         remount_client $MOUNT || error "failed to remount client"
10166 }
10167 run_test 77k "enable/disable checksum correctly"
10168
10169 test_77l() {
10170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10171         $GSS && skip_env "could not run with gss"
10172
10173         set_checksums 1
10174         stack_trap "set_checksums $ORIG_CSUM" EXIT
10175         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
10176
10177         set_checksum_type invalid && error "unexpected success of invalid checksum type"
10178
10179         $LFS setstripe -c 1 -i 0 $DIR/$tfile
10180         for algo in $CKSUM_TYPES; do
10181                 set_checksum_type $algo || error "fail to set checksum type $algo"
10182                 osc_algo=$(get_osc_checksum_type OST0000)
10183                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
10184
10185                 # no locks, no reqs to let the connection idle
10186                 cancel_lru_locks osc
10187                 lru_resize_disable osc
10188                 wait_osc_import_state client ost1 IDLE
10189
10190                 # ensure ost1 is connected
10191                 stat $DIR/$tfile >/dev/null || error "can't stat"
10192                 wait_osc_import_state client ost1 FULL
10193
10194                 osc_algo=$(get_osc_checksum_type OST0000)
10195                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
10196         done
10197         return 0
10198 }
10199 run_test 77l "preferred checksum type is remembered after reconnected"
10200
10201 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
10202 rm -f $F77_TMP
10203 unset F77_TMP
10204
10205 test_77m() {
10206         (( $CLIENT_VERSION >= $(version_code 2.14.52) )) ||
10207                 skip "Need at least version 2.14.52"
10208         local param=checksum_speed
10209
10210         $LCTL get_param $param || error "reading $param failed"
10211
10212         csum_speeds=$($LCTL get_param -n $param)
10213
10214         [[ "$csum_speeds" =~ "adler32" && "$csum_speeds" =~ "crc32" ]] ||
10215                 error "known checksum types are missing"
10216 }
10217 run_test 77m "Verify checksum_speed is correctly read"
10218
10219 check_filefrag_77n() {
10220         local nr_ext=0
10221         local starts=()
10222         local ends=()
10223
10224         while read extidx a b start end rest; do
10225                 if [[ "${extidx}" =~ ^[0-9]+: ]]; then
10226                         nr_ext=$(( $nr_ext + 1 ))
10227                         starts+=( ${start%..} )
10228                         ends+=( ${end%:} )
10229                 fi
10230         done < <( filefrag -sv $1 )
10231
10232         [[ $nr_ext -eq 2 ]] && [[ "${starts[-1]}" == $(( ${ends[0]} + 1 )) ]] && return 0
10233         return 1
10234 }
10235
10236 test_77n() {
10237         [[ "$CKSUM_TYPES" =~ t10 ]] || skip "no T10 checksum support on osc"
10238
10239         touch $DIR/$tfile
10240         $TRUNCATE $DIR/$tfile 0
10241         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=0
10242         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=2
10243         check_filefrag_77n $DIR/$tfile ||
10244                 skip "$tfile blocks not contiguous around hole"
10245
10246         set_checksums 1
10247         stack_trap "set_checksums $ORIG_CSUM" EXIT
10248         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
10249         stack_trap "rm -f $DIR/$tfile"
10250
10251         for algo in $CKSUM_TYPES; do
10252                 if [[ "$algo" =~ ^t10 ]]; then
10253                         set_checksum_type $algo ||
10254                                 error "fail to set checksum type $algo"
10255                         dd if=$DIR/$tfile of=/dev/null bs=12k count=1 iflag=direct ||
10256                                 error "fail to read $tfile with $algo"
10257                 fi
10258         done
10259         rm -f $DIR/$tfile
10260         return 0
10261 }
10262 run_test 77n "Verify read from a hole inside contiguous blocks with T10PI"
10263
10264 test_77o() {
10265         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
10266                 skip "Need MDS version at least 2.14.55"
10267         (( $OST1_VERSION >= $(version_code 2.14.55) )) ||
10268                 skip "Need OST version at least 2.14.55"
10269         local ofd=obdfilter
10270         local mdt=mdt
10271
10272         # print OST checksum_type
10273         echo "$ofd.$FSNAME-*.checksum_type:"
10274         do_nodes $(comma_list $(osts_nodes)) \
10275                 $LCTL get_param -n $ofd.$FSNAME-*.checksum_type
10276
10277         # print MDT checksum_type
10278         echo "$mdt.$FSNAME-*.checksum_type:"
10279         do_nodes $(comma_list $(mdts_nodes)) \
10280                 $LCTL get_param -n $mdt.$FSNAME-*.checksum_type
10281
10282         local o_count=$(do_nodes $(comma_list $(osts_nodes)) \
10283                    $LCTL get_param -n $ofd.$FSNAME-*.checksum_type | wc -l)
10284
10285         (( $o_count == $OSTCOUNT )) ||
10286                 error "found $o_count checksums, not \$MDSCOUNT=$OSTCOUNT"
10287
10288         local m_count=$(do_nodes $(comma_list $(mdts_nodes)) \
10289                    $LCTL get_param -n $mdt.$FSNAME-*.checksum_type | wc -l)
10290
10291         (( $m_count == $MDSCOUNT )) ||
10292                 error "found $m_count checksums, not \$MDSCOUNT=$MDSCOUNT"
10293 }
10294 run_test 77o "Verify checksum_type for server (mdt and ofd(obdfilter))"
10295
10296 cleanup_test_78() {
10297         trap 0
10298         rm -f $DIR/$tfile
10299 }
10300
10301 test_78() { # bug 10901
10302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10303         remote_ost || skip_env "local OST"
10304
10305         NSEQ=5
10306         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
10307         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
10308         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
10309         echo "MemTotal: $MEMTOTAL"
10310
10311         # reserve 256MB of memory for the kernel and other running processes,
10312         # and then take 1/2 of the remaining memory for the read/write buffers.
10313         if [ $MEMTOTAL -gt 512 ] ;then
10314                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
10315         else
10316                 # for those poor memory-starved high-end clusters...
10317                 MEMTOTAL=$((MEMTOTAL / 2))
10318         fi
10319         echo "Mem to use for directio: $MEMTOTAL"
10320
10321         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
10322         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
10323         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
10324         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
10325                 head -n1)
10326         echo "Smallest OST: $SMALLESTOST"
10327         [[ $SMALLESTOST -lt 10240 ]] &&
10328                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
10329
10330         trap cleanup_test_78 EXIT
10331
10332         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
10333                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
10334
10335         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
10336         echo "File size: $F78SIZE"
10337         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
10338         for i in $(seq 1 $NSEQ); do
10339                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
10340                 echo directIO rdwr round $i of $NSEQ
10341                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
10342         done
10343
10344         cleanup_test_78
10345 }
10346 run_test 78 "handle large O_DIRECT writes correctly ============"
10347
10348 test_79() { # bug 12743
10349         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10350
10351         wait_delete_completed
10352
10353         BKTOTAL=$(calc_osc_kbytes kbytestotal)
10354         BKFREE=$(calc_osc_kbytes kbytesfree)
10355         BKAVAIL=$(calc_osc_kbytes kbytesavail)
10356
10357         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
10358         DFTOTAL=`echo $STRING | cut -d, -f1`
10359         DFUSED=`echo $STRING  | cut -d, -f2`
10360         DFAVAIL=`echo $STRING | cut -d, -f3`
10361         DFFREE=$(($DFTOTAL - $DFUSED))
10362
10363         ALLOWANCE=$((64 * $OSTCOUNT))
10364
10365         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
10366            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
10367                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
10368         fi
10369         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
10370            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
10371                 error "df free($DFFREE) mismatch OST free($BKFREE)"
10372         fi
10373         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
10374            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
10375                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
10376         fi
10377 }
10378 run_test 79 "df report consistency check ======================="
10379
10380 test_80() { # bug 10718
10381         remote_ost_nodsh && skip "remote OST with nodsh"
10382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10383
10384         # relax strong synchronous semantics for slow backends like ZFS
10385         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
10386                 local soc="obdfilter.*.sync_lock_cancel"
10387                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10388
10389                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
10390                 if [ -z "$save" ]; then
10391                         soc="obdfilter.*.sync_on_lock_cancel"
10392                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10393                 fi
10394
10395                 if [ "$save" != "never" ]; then
10396                         local hosts=$(comma_list $(osts_nodes))
10397
10398                         do_nodes $hosts $LCTL set_param $soc=never
10399                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
10400                 fi
10401         fi
10402
10403         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
10404         sync; sleep 1; sync
10405         local before=$(date +%s)
10406         cancel_lru_locks osc
10407         local after=$(date +%s)
10408         local diff=$((after - before))
10409         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
10410
10411         rm -f $DIR/$tfile
10412 }
10413 run_test 80 "Page eviction is equally fast at high offsets too"
10414
10415 test_81a() { # LU-456
10416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10417         remote_ost_nodsh && skip "remote OST with nodsh"
10418
10419         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10420         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
10421         do_facet ost1 lctl set_param fail_loc=0x80000228
10422
10423         # write should trigger a retry and success
10424         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10425         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10426         RC=$?
10427         if [ $RC -ne 0 ] ; then
10428                 error "write should success, but failed for $RC"
10429         fi
10430 }
10431 run_test 81a "OST should retry write when get -ENOSPC ==============="
10432
10433 test_81b() { # LU-456
10434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10435         remote_ost_nodsh && skip "remote OST with nodsh"
10436
10437         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10438         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
10439         do_facet ost1 lctl set_param fail_loc=0x228
10440
10441         # write should retry several times and return -ENOSPC finally
10442         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10443         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10444         RC=$?
10445         ENOSPC=28
10446         if [ $RC -ne $ENOSPC ] ; then
10447                 error "dd should fail for -ENOSPC, but succeed."
10448         fi
10449 }
10450 run_test 81b "OST should return -ENOSPC when retry still fails ======="
10451
10452 test_99() {
10453         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
10454
10455         test_mkdir $DIR/$tdir.cvsroot
10456         chown $RUNAS_ID $DIR/$tdir.cvsroot
10457
10458         cd $TMP
10459         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
10460
10461         cd /etc/init.d
10462         # some versions of cvs import exit(1) when asked to import links or
10463         # files they can't read.  ignore those files.
10464         local toignore=$(find . -type l -printf '-I %f\n' -o \
10465                          ! -perm /4 -printf '-I %f\n')
10466         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
10467                 $tdir.reposname vtag rtag
10468
10469         cd $DIR
10470         test_mkdir $DIR/$tdir.reposname
10471         chown $RUNAS_ID $DIR/$tdir.reposname
10472         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
10473
10474         cd $DIR/$tdir.reposname
10475         $RUNAS touch foo99
10476         $RUNAS cvs add -m 'addmsg' foo99
10477         $RUNAS cvs update
10478         $RUNAS cvs commit -m 'nomsg' foo99
10479         rm -fr $DIR/$tdir.cvsroot
10480 }
10481 run_test 99 "cvs strange file/directory operations"
10482
10483 test_100() {
10484         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10485         [[ "$NETTYPE" =~ tcp ]] ||
10486                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
10487         remote_ost_nodsh && skip "remote OST with nodsh"
10488         remote_mds_nodsh && skip "remote MDS with nodsh"
10489         remote_servers ||
10490                 skip "useless for local single node setup"
10491
10492         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
10493                 [ "$PROT" != "tcp" ] && continue
10494                 RPORT=$(echo $REMOTE | cut -d: -f2)
10495                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
10496
10497                 rc=0
10498                 LPORT=`echo $LOCAL | cut -d: -f2`
10499                 if [ $LPORT -ge 1024 ]; then
10500                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
10501                         netstat -tna
10502                         error_exit "local: $LPORT > 1024, remote: $RPORT"
10503                 fi
10504         done
10505         [ "$rc" = 0 ] || error_exit "privileged port not found" )
10506 }
10507 run_test 100 "check local port using privileged port ==========="
10508
10509 function get_named_value()
10510 {
10511     local tag=$1
10512
10513     grep -w "$tag" | sed "s/^$tag  *\([0-9]*\)  *.*/\1/"
10514 }
10515
10516 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
10517                    awk '/^max_cached_mb/ { print $2 }')
10518
10519 cleanup_101a() {
10520         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
10521         trap 0
10522 }
10523
10524 test_101a() {
10525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10526
10527         local s
10528         local discard
10529         local nreads=10000
10530         local cache_limit=32
10531
10532         $LCTL set_param -n osc.*-osc*.rpc_stats=0
10533         trap cleanup_101a EXIT
10534         $LCTL set_param -n llite.*.read_ahead_stats=0
10535         $LCTL set_param -n llite.*.max_cached_mb=$cache_limit
10536
10537         #
10538         # randomly read 10000 of 64K chunks from file 3x 32MB in size
10539         #
10540         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
10541         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
10542
10543         discard=0
10544         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
10545                    get_named_value 'read.but.discarded'); do
10546                         discard=$(($discard + $s))
10547         done
10548         cleanup_101a
10549
10550         $LCTL get_param osc.*-osc*.rpc_stats
10551         $LCTL get_param llite.*.read_ahead_stats
10552
10553         # Discard is generally zero, but sometimes a few random reads line up
10554         # and trigger larger readahead, which is wasted & leads to discards.
10555         if [[ $(($discard)) -gt $nreads ]]; then
10556                 error "too many ($discard) discarded pages"
10557         fi
10558         rm -f $DIR/$tfile || true
10559 }
10560 run_test 101a "check read-ahead for random reads"
10561
10562 setup_test101bc() {
10563         test_mkdir $DIR/$tdir
10564         local ssize=$1
10565         local FILE_LENGTH=$2
10566         STRIPE_OFFSET=0
10567
10568         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
10569
10570         local list=$(comma_list $(osts_nodes))
10571         set_osd_param $list '' read_cache_enable 0
10572         set_osd_param $list '' writethrough_cache_enable 0
10573
10574         trap cleanup_test101bc EXIT
10575         # prepare the read-ahead file
10576         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
10577
10578         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
10579                                 count=$FILE_SIZE_MB 2> /dev/null
10580
10581 }
10582
10583 cleanup_test101bc() {
10584         trap 0
10585         rm -rf $DIR/$tdir
10586         rm -f $DIR/$tfile
10587
10588         local list=$(comma_list $(osts_nodes))
10589         set_osd_param $list '' read_cache_enable 1
10590         set_osd_param $list '' writethrough_cache_enable 1
10591 }
10592
10593 calc_total() {
10594         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
10595 }
10596
10597 ra_check_101() {
10598         local read_size=$1
10599         local stripe_size=$2
10600         local stride_length=$((stripe_size / read_size))
10601         local stride_width=$((stride_length * OSTCOUNT))
10602         local discard_limit=$(( ((stride_length - 1) * 3 / stride_width) *
10603                                 (stride_width - stride_length) ))
10604         local discard=$($LCTL get_param -n llite.*.read_ahead_stats |
10605                   get_named_value 'read.but.discarded' | calc_total)
10606
10607         if [[ $discard -gt $discard_limit ]]; then
10608                 $LCTL get_param llite.*.read_ahead_stats
10609                 error "($discard limit ${discard_limit}) discarded pages with size (${read_size})"
10610         else
10611                 echo "Read-ahead success for size ${read_size}"
10612         fi
10613 }
10614
10615 test_101b() {
10616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10617         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10618
10619         local STRIPE_SIZE=1048576
10620         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
10621
10622         if [ $SLOW == "yes" ]; then
10623                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
10624         else
10625                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
10626         fi
10627
10628         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
10629
10630         # prepare the read-ahead file
10631         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10632         cancel_lru_locks osc
10633         for BIDX in 2 4 8 16 32 64 128 256
10634         do
10635                 local BSIZE=$((BIDX*4096))
10636                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
10637                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
10638                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
10639                 $LCTL set_param -n llite.*.read_ahead_stats=0
10640                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
10641                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
10642                 cancel_lru_locks osc
10643                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
10644         done
10645         cleanup_test101bc
10646         true
10647 }
10648 run_test 101b "check stride-io mode read-ahead ================="
10649
10650 test_101c() {
10651         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10652
10653         local STRIPE_SIZE=1048576
10654         local FILE_LENGTH=$((STRIPE_SIZE*100))
10655         local nreads=10000
10656         local rsize=65536
10657         local osc_rpc_stats
10658
10659         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10660
10661         cancel_lru_locks osc
10662         $LCTL set_param osc.*.rpc_stats=0
10663         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
10664         $LCTL get_param osc.*.rpc_stats
10665         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
10666                 local stats=$($LCTL get_param -n $osc_rpc_stats)
10667                 local lines=$(echo "$stats" | awk 'END {print NR;}')
10668                 local size
10669
10670                 if [ $lines -le 20 ]; then
10671                         echo "continue debug"
10672                         continue
10673                 fi
10674                 for size in 1 2 4 8; do
10675                         local rpc=$(echo "$stats" |
10676                                     awk '($1 == "'$size':") {print $2; exit; }')
10677                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
10678                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
10679                 done
10680                 echo "$osc_rpc_stats check passed!"
10681         done
10682         cleanup_test101bc
10683         true
10684 }
10685 run_test 101c "check stripe_size aligned read-ahead"
10686
10687 test_101d() {
10688         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10689
10690         local file=$DIR/$tfile
10691         local sz_MB=${FILESIZE_101d:-80}
10692         local ra_MB=${READAHEAD_MB:-40}
10693
10694         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
10695         [ $free_MB -lt $sz_MB ] &&
10696                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
10697
10698         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
10699         $LFS setstripe -c -1 $file || error "setstripe failed"
10700
10701         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
10702         echo Cancel LRU locks on lustre client to flush the client cache
10703         cancel_lru_locks osc
10704
10705         echo Disable read-ahead
10706         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10707         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10708         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb=$old_RA" EXIT
10709         $LCTL get_param -n llite.*.max_read_ahead_mb
10710
10711         echo "Reading the test file $file with read-ahead disabled"
10712         local sz_KB=$((sz_MB * 1024 / 4))
10713         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
10714         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
10715         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10716                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10717
10718         echo "Cancel LRU locks on lustre client to flush the client cache"
10719         cancel_lru_locks osc
10720         echo Enable read-ahead with ${ra_MB}MB
10721         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
10722
10723         echo "Reading the test file $file with read-ahead enabled"
10724         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10725                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10726
10727         echo "read-ahead disabled time read $raOFF"
10728         echo "read-ahead enabled time read $raON"
10729
10730         rm -f $file
10731         wait_delete_completed
10732
10733         # use awk for this check instead of bash because it handles decimals
10734         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
10735                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
10736 }
10737 run_test 101d "file read with and without read-ahead enabled"
10738
10739 test_101e() {
10740         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10741
10742         local file=$DIR/$tfile
10743         local size_KB=500  #KB
10744         local count=100
10745         local bsize=1024
10746
10747         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
10748         local need_KB=$((count * size_KB))
10749         [[ $free_KB -le $need_KB ]] &&
10750                 skip_env "Need free space $need_KB, have $free_KB"
10751
10752         echo "Creating $count ${size_KB}K test files"
10753         for ((i = 0; i < $count; i++)); do
10754                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
10755         done
10756
10757         echo "Cancel LRU locks on lustre client to flush the client cache"
10758         cancel_lru_locks $OSC
10759
10760         echo "Reset readahead stats"
10761         $LCTL set_param -n llite.*.read_ahead_stats=0
10762
10763         for ((i = 0; i < $count; i++)); do
10764                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
10765         done
10766
10767         $LCTL get_param llite.*.max_cached_mb
10768         $LCTL get_param llite.*.read_ahead_stats
10769         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10770                      get_named_value 'misses' | calc_total)
10771
10772         for ((i = 0; i < $count; i++)); do
10773                 rm -rf $file.$i 2>/dev/null
10774         done
10775
10776         #10000 means 20% reads are missing in readahead
10777         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
10778 }
10779 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
10780
10781 test_101f() {
10782         which iozone || skip_env "no iozone installed"
10783
10784         local old_debug=$($LCTL get_param debug)
10785         old_debug=${old_debug#*=}
10786         $LCTL set_param debug="reada mmap"
10787
10788         # create a test file
10789         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
10790
10791         echo Cancel LRU locks on lustre client to flush the client cache
10792         cancel_lru_locks osc
10793
10794         echo Reset readahead stats
10795         $LCTL set_param -n llite.*.read_ahead_stats=0
10796
10797         echo mmap read the file with small block size
10798         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
10799                 > /dev/null 2>&1
10800
10801         echo checking missing pages
10802         $LCTL get_param llite.*.read_ahead_stats
10803         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10804                         get_named_value 'misses' | calc_total)
10805
10806         $LCTL set_param debug="$old_debug"
10807         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
10808         rm -f $DIR/$tfile
10809 }
10810 run_test 101f "check mmap read performance"
10811
10812 test_101g_brw_size_test() {
10813         local mb=$1
10814         local pages=$((mb * 1048576 / PAGE_SIZE))
10815         local file=$DIR/$tfile
10816
10817         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
10818                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
10819         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
10820                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
10821                         return 2
10822         done
10823
10824         stack_trap "rm -f $file" EXIT
10825         $LCTL set_param -n osc.*.rpc_stats=0
10826
10827         # 10 RPCs should be enough for the test
10828         local count=10
10829         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
10830                 { error "dd write ${mb} MB blocks failed"; return 3; }
10831         cancel_lru_locks osc
10832         dd of=/dev/null if=$file bs=${mb}M count=$count ||
10833                 { error "dd write ${mb} MB blocks failed"; return 4; }
10834
10835         # calculate number of full-sized read and write RPCs
10836         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
10837                 sed -n '/pages per rpc/,/^$/p' |
10838                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
10839                 END { print reads,writes }'))
10840         # allow one extra full-sized read RPC for async readahead
10841         [[ ${rpcs[0]} == $count || ${rpcs[0]} == $((count + 1)) ]] ||
10842                 { error "${rpcs[0]} != $count read RPCs"; return 5; }
10843         [[ ${rpcs[1]} == $count ]] ||
10844                 { error "${rpcs[1]} != $count write RPCs"; return 6; }
10845 }
10846
10847 test_101g() {
10848         remote_ost_nodsh && skip "remote OST with nodsh"
10849
10850         local rpcs
10851         local osts=$(get_facets OST)
10852         local list=$(comma_list $(osts_nodes))
10853         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10854         local brw_size="obdfilter.*.brw_size"
10855
10856         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10857
10858         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
10859
10860         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
10861                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
10862                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
10863            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
10864                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
10865                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
10866
10867                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
10868                         suffix="M"
10869
10870                 if [[ $orig_mb -lt 16 ]]; then
10871                         save_lustre_params $osts "$brw_size" > $p
10872                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
10873                                 error "set 16MB RPC size failed"
10874
10875                         echo "remount client to enable new RPC size"
10876                         remount_client $MOUNT || error "remount_client failed"
10877                 fi
10878
10879                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
10880                 # should be able to set brw_size=12, but no rpc_stats for that
10881                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
10882         fi
10883
10884         test_101g_brw_size_test 4 || error "4MB RPC test failed"
10885
10886         if [[ $orig_mb -lt 16 ]]; then
10887                 restore_lustre_params < $p
10888                 remount_client $MOUNT || error "remount_client restore failed"
10889         fi
10890
10891         rm -f $p $DIR/$tfile
10892 }
10893 run_test 101g "Big bulk(4/16 MiB) readahead"
10894
10895 test_101h() {
10896         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10897
10898         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
10899                 error "dd 70M file failed"
10900         echo Cancel LRU locks on lustre client to flush the client cache
10901         cancel_lru_locks osc
10902
10903         echo "Reset readahead stats"
10904         $LCTL set_param -n llite.*.read_ahead_stats 0
10905
10906         echo "Read 10M of data but cross 64M bundary"
10907         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
10908         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10909                      get_named_value 'misses' | calc_total)
10910         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
10911         rm -f $p $DIR/$tfile
10912 }
10913 run_test 101h "Readahead should cover current read window"
10914
10915 test_101i() {
10916         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
10917                 error "dd 10M file failed"
10918
10919         local max_per_file_mb=$($LCTL get_param -n \
10920                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
10921         cancel_lru_locks osc
10922         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
10923         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
10924                 error "set max_read_ahead_per_file_mb to 1 failed"
10925
10926         echo "Reset readahead stats"
10927         $LCTL set_param llite.*.read_ahead_stats=0
10928
10929         dd if=$DIR/$tfile of=/dev/null bs=2M
10930
10931         $LCTL get_param llite.*.read_ahead_stats
10932         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10933                      awk '/misses/ { print $2 }')
10934         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
10935         rm -f $DIR/$tfile
10936 }
10937 run_test 101i "allow current readahead to exceed reservation"
10938
10939 test_101j() {
10940         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
10941                 error "setstripe $DIR/$tfile failed"
10942         local file_size=$((1048576 * 16))
10943         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10944         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
10945
10946         echo Disable read-ahead
10947         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10948
10949         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
10950         for blk in $PAGE_SIZE 1048576 $file_size; do
10951                 cancel_lru_locks osc
10952                 echo "Reset readahead stats"
10953                 $LCTL set_param -n llite.*.read_ahead_stats=0
10954                 local count=$(($file_size / $blk))
10955                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
10956                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10957                              get_named_value 'failed.to.fast.read' | calc_total)
10958                 $LCTL get_param -n llite.*.read_ahead_stats
10959                 [ $miss -eq $count ] || error "expected $count got $miss"
10960         done
10961
10962         rm -f $p $DIR/$tfile
10963 }
10964 run_test 101j "A complete read block should be submitted when no RA"
10965
10966 setup_test102() {
10967         test_mkdir $DIR/$tdir
10968         chown $RUNAS_ID $DIR/$tdir
10969         STRIPE_SIZE=65536
10970         STRIPE_OFFSET=1
10971         STRIPE_COUNT=$OSTCOUNT
10972         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
10973
10974         trap cleanup_test102 EXIT
10975         cd $DIR
10976         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
10977         cd $DIR/$tdir
10978         for num in 1 2 3 4; do
10979                 for count in $(seq 1 $STRIPE_COUNT); do
10980                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
10981                                 local size=`expr $STRIPE_SIZE \* $num`
10982                                 local file=file"$num-$idx-$count"
10983                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
10984                         done
10985                 done
10986         done
10987
10988         cd $DIR
10989         $1 tar cf $TMP/f102.tar $tdir --xattrs
10990 }
10991
10992 cleanup_test102() {
10993         trap 0
10994         rm -f $TMP/f102.tar
10995         rm -rf $DIR/d0.sanity/d102
10996 }
10997
10998 test_102a() {
10999         [ "$UID" != 0 ] && skip "must run as root"
11000         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
11001                 skip_env "must have user_xattr"
11002
11003         [ -z "$(which setfattr 2>/dev/null)" ] &&
11004                 skip_env "could not find setfattr"
11005
11006         local testfile=$DIR/$tfile
11007
11008         touch $testfile
11009         echo "set/get xattr..."
11010         setfattr -n trusted.name1 -v value1 $testfile ||
11011                 error "setfattr -n trusted.name1=value1 $testfile failed"
11012         getfattr -n trusted.name1 $testfile 2> /dev/null |
11013           grep "trusted.name1=.value1" ||
11014                 error "$testfile missing trusted.name1=value1"
11015
11016         setfattr -n user.author1 -v author1 $testfile ||
11017                 error "setfattr -n user.author1=author1 $testfile failed"
11018         getfattr -n user.author1 $testfile 2> /dev/null |
11019           grep "user.author1=.author1" ||
11020                 error "$testfile missing trusted.author1=author1"
11021
11022         echo "listxattr..."
11023         setfattr -n trusted.name2 -v value2 $testfile ||
11024                 error "$testfile unable to set trusted.name2"
11025         setfattr -n trusted.name3 -v value3 $testfile ||
11026                 error "$testfile unable to set trusted.name3"
11027         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
11028             grep "trusted.name" | wc -l) -eq 3 ] ||
11029                 error "$testfile missing 3 trusted.name xattrs"
11030
11031         setfattr -n user.author2 -v author2 $testfile ||
11032                 error "$testfile unable to set user.author2"
11033         setfattr -n user.author3 -v author3 $testfile ||
11034                 error "$testfile unable to set user.author3"
11035         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
11036             grep "user.author" | wc -l) -eq 3 ] ||
11037                 error "$testfile missing 3 user.author xattrs"
11038
11039         echo "remove xattr..."
11040         setfattr -x trusted.name1 $testfile ||
11041                 error "$testfile error deleting trusted.name1"
11042         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
11043                 error "$testfile did not delete trusted.name1 xattr"
11044
11045         setfattr -x user.author1 $testfile ||
11046                 error "$testfile error deleting user.author1"
11047         echo "set lustre special xattr ..."
11048         $LFS setstripe -c1 $testfile
11049         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
11050                 awk -F "=" '/trusted.lov/ { print $2 }' )
11051         setfattr -n "trusted.lov" -v $lovea $testfile ||
11052                 error "$testfile doesn't ignore setting trusted.lov again"
11053         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
11054                 error "$testfile allow setting invalid trusted.lov"
11055         rm -f $testfile
11056 }
11057 run_test 102a "user xattr test =================================="
11058
11059 check_102b_layout() {
11060         local layout="$*"
11061         local testfile=$DIR/$tfile
11062
11063         echo "test layout '$layout'"
11064         $LFS setstripe $layout $testfile || error "setstripe failed"
11065         $LFS getstripe -y $testfile
11066
11067         echo "get/set/list trusted.lov xattr ..." # b=10930
11068         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
11069         [[ "$value" =~ "trusted.lov" ]] ||
11070                 error "can't get trusted.lov from $testfile"
11071         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
11072                 error "getstripe failed"
11073
11074         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
11075
11076         value=$(cut -d= -f2 <<<$value)
11077         # LU-13168: truncated xattr should fail if short lov_user_md header
11078         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
11079                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
11080         for len in $lens; do
11081                 echo "setfattr $len $testfile.2"
11082                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
11083                         [ $len -lt 66 ] && error "short xattr len=$len worked"
11084         done
11085         local stripe_size=$($LFS getstripe -S $testfile.2)
11086         local stripe_count=$($LFS getstripe -c $testfile.2)
11087         [[ $stripe_size -eq 65536 ]] ||
11088                 error "stripe size $stripe_size != 65536"
11089         [[ $stripe_count -eq $stripe_count_orig ]] ||
11090                 error "stripe count $stripe_count != $stripe_count_orig"
11091         rm $testfile $testfile.2
11092 }
11093
11094 test_102b() {
11095         [ -z "$(which setfattr 2>/dev/null)" ] &&
11096                 skip_env "could not find setfattr"
11097         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11098
11099         # check plain layout
11100         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
11101
11102         # and also check composite layout
11103         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
11104
11105 }
11106 run_test 102b "getfattr/setfattr for trusted.lov EAs"
11107
11108 test_102c() {
11109         [ -z "$(which setfattr 2>/dev/null)" ] &&
11110                 skip_env "could not find setfattr"
11111         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11112
11113         # b10930: get/set/list lustre.lov xattr
11114         echo "get/set/list lustre.lov xattr ..."
11115         test_mkdir $DIR/$tdir
11116         chown $RUNAS_ID $DIR/$tdir
11117         local testfile=$DIR/$tdir/$tfile
11118         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
11119                 error "setstripe failed"
11120         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
11121                 error "getstripe failed"
11122         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
11123         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
11124
11125         local testfile2=${testfile}2
11126         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
11127                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
11128
11129         $RUNAS $MCREATE $testfile2
11130         $RUNAS setfattr -n lustre.lov -v $value $testfile2
11131         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
11132         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
11133         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
11134         [ $stripe_count -eq $STRIPECOUNT ] ||
11135                 error "stripe count $stripe_count != $STRIPECOUNT"
11136 }
11137 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
11138
11139 compare_stripe_info1() {
11140         local stripe_index_all_zero=true
11141
11142         for num in 1 2 3 4; do
11143                 for count in $(seq 1 $STRIPE_COUNT); do
11144                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
11145                                 local size=$((STRIPE_SIZE * num))
11146                                 local file=file"$num-$offset-$count"
11147                                 stripe_size=$($LFS getstripe -S $PWD/$file)
11148                                 [[ $stripe_size -ne $size ]] &&
11149                                     error "$file: size $stripe_size != $size"
11150                                 stripe_count=$($LFS getstripe -c $PWD/$file)
11151                                 # allow fewer stripes to be created, ORI-601
11152                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
11153                                     error "$file: count $stripe_count != $count"
11154                                 stripe_index=$($LFS getstripe -i $PWD/$file)
11155                                 [[ $stripe_index -ne 0 ]] &&
11156                                         stripe_index_all_zero=false
11157                         done
11158                 done
11159         done
11160         $stripe_index_all_zero &&
11161                 error "all files are being extracted starting from OST index 0"
11162         return 0
11163 }
11164
11165 have_xattrs_include() {
11166         tar --help | grep -q xattrs-include &&
11167                 echo --xattrs-include="lustre.*"
11168 }
11169
11170 test_102d() {
11171         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11172         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11173
11174         XINC=$(have_xattrs_include)
11175         setup_test102
11176         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
11177         cd $DIR/$tdir/$tdir
11178         compare_stripe_info1
11179 }
11180 run_test 102d "tar restore stripe info from tarfile,not keep osts"
11181
11182 test_102f() {
11183         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11184         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11185
11186         XINC=$(have_xattrs_include)
11187         setup_test102
11188         test_mkdir $DIR/$tdir.restore
11189         cd $DIR
11190         tar cf - --xattrs $tdir | tar xf - \
11191                 -C $DIR/$tdir.restore --xattrs $XINC
11192         cd $DIR/$tdir.restore/$tdir
11193         compare_stripe_info1
11194 }
11195 run_test 102f "tar copy files, not keep osts"
11196
11197 grow_xattr() {
11198         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
11199                 skip "must have user_xattr"
11200         [ -z "$(which setfattr 2>/dev/null)" ] &&
11201                 skip_env "could not find setfattr"
11202         [ -z "$(which getfattr 2>/dev/null)" ] &&
11203                 skip_env "could not find getfattr"
11204
11205         local xsize=${1:-1024}  # in bytes
11206         local file=$DIR/$tfile
11207         local value="$(generate_string $xsize)"
11208         local xbig=trusted.big
11209         local toobig=$2
11210
11211         touch $file
11212         log "save $xbig on $file"
11213         if [ -z "$toobig" ]
11214         then
11215                 setfattr -n $xbig -v $value $file ||
11216                         error "saving $xbig on $file failed"
11217         else
11218                 setfattr -n $xbig -v $value $file &&
11219                         error "saving $xbig on $file succeeded"
11220                 return 0
11221         fi
11222
11223         local orig=$(get_xattr_value $xbig $file)
11224         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
11225
11226         local xsml=trusted.sml
11227         log "save $xsml on $file"
11228         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
11229
11230         local new=$(get_xattr_value $xbig $file)
11231         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
11232
11233         log "grow $xsml on $file"
11234         setfattr -n $xsml -v "$value" $file ||
11235                 error "growing $xsml on $file failed"
11236
11237         new=$(get_xattr_value $xbig $file)
11238         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
11239         log "$xbig still valid after growing $xsml"
11240
11241         rm -f $file
11242 }
11243
11244 test_102h() { # bug 15777
11245         grow_xattr 1024
11246 }
11247 run_test 102h "grow xattr from inside inode to external block"
11248
11249 test_102ha() {
11250         large_xattr_enabled || skip_env "ea_inode feature disabled"
11251
11252         echo "setting xattr of max xattr size: $(max_xattr_size)"
11253         grow_xattr $(max_xattr_size)
11254
11255         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
11256         echo "This should fail:"
11257         grow_xattr $(($(max_xattr_size) + 10)) 1
11258 }
11259 run_test 102ha "grow xattr from inside inode to external inode"
11260
11261 test_102i() { # bug 17038
11262         [ -z "$(which getfattr 2>/dev/null)" ] &&
11263                 skip "could not find getfattr"
11264
11265         touch $DIR/$tfile
11266         ln -s $DIR/$tfile $DIR/${tfile}link
11267         getfattr -n trusted.lov $DIR/$tfile ||
11268                 error "lgetxattr on $DIR/$tfile failed"
11269         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
11270                 grep -i "no such attr" ||
11271                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
11272         rm -f $DIR/$tfile $DIR/${tfile}link
11273 }
11274 run_test 102i "lgetxattr test on symbolic link ============"
11275
11276 test_102j() {
11277         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11278         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11279
11280         XINC=$(have_xattrs_include)
11281         setup_test102 "$RUNAS"
11282         chown $RUNAS_ID $DIR/$tdir
11283         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
11284         cd $DIR/$tdir/$tdir
11285         compare_stripe_info1 "$RUNAS"
11286 }
11287 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
11288
11289 test_102k() {
11290         [ -z "$(which setfattr 2>/dev/null)" ] &&
11291                 skip "could not find setfattr"
11292
11293         touch $DIR/$tfile
11294         # b22187 just check that does not crash for regular file.
11295         setfattr -n trusted.lov $DIR/$tfile
11296         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
11297         local test_kdir=$DIR/$tdir
11298         test_mkdir $test_kdir
11299         local default_size=$($LFS getstripe -S $test_kdir)
11300         local default_count=$($LFS getstripe -c $test_kdir)
11301         local default_offset=$($LFS getstripe -i $test_kdir)
11302         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
11303                 error 'dir setstripe failed'
11304         setfattr -n trusted.lov $test_kdir
11305         local stripe_size=$($LFS getstripe -S $test_kdir)
11306         local stripe_count=$($LFS getstripe -c $test_kdir)
11307         local stripe_offset=$($LFS getstripe -i $test_kdir)
11308         [ $stripe_size -eq $default_size ] ||
11309                 error "stripe size $stripe_size != $default_size"
11310         [ $stripe_count -eq $default_count ] ||
11311                 error "stripe count $stripe_count != $default_count"
11312         [ $stripe_offset -eq $default_offset ] ||
11313                 error "stripe offset $stripe_offset != $default_offset"
11314         rm -rf $DIR/$tfile $test_kdir
11315 }
11316 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
11317
11318 test_102l() {
11319         [ -z "$(which getfattr 2>/dev/null)" ] &&
11320                 skip "could not find getfattr"
11321
11322         # LU-532 trusted. xattr is invisible to non-root
11323         local testfile=$DIR/$tfile
11324
11325         touch $testfile
11326
11327         echo "listxattr as user..."
11328         chown $RUNAS_ID $testfile
11329         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
11330             grep -q "trusted" &&
11331                 error "$testfile trusted xattrs are user visible"
11332
11333         return 0;
11334 }
11335 run_test 102l "listxattr size test =================================="
11336
11337 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
11338         local path=$DIR/$tfile
11339         touch $path
11340
11341         listxattr_size_check $path || error "listattr_size_check $path failed"
11342 }
11343 run_test 102m "Ensure listxattr fails on small bufffer ========"
11344
11345 cleanup_test102
11346
11347 getxattr() { # getxattr path name
11348         # Return the base64 encoding of the value of xattr name on path.
11349         local path=$1
11350         local name=$2
11351
11352         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
11353         # file: $path
11354         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
11355         #
11356         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
11357
11358         getfattr --absolute-names --encoding=base64 --name=$name $path |
11359                 awk -F= -v name=$name '$1 == name {
11360                         print substr($0, index($0, "=") + 1);
11361         }'
11362 }
11363
11364 test_102n() { # LU-4101 mdt: protect internal xattrs
11365         [ -z "$(which setfattr 2>/dev/null)" ] &&
11366                 skip "could not find setfattr"
11367         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
11368         then
11369                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
11370         fi
11371
11372         local file0=$DIR/$tfile.0
11373         local file1=$DIR/$tfile.1
11374         local xattr0=$TMP/$tfile.0
11375         local xattr1=$TMP/$tfile.1
11376         local namelist="lov lma lmv link fid version som hsm"
11377         local name
11378         local value
11379
11380         rm -rf $file0 $file1 $xattr0 $xattr1
11381         touch $file0 $file1
11382
11383         # Get 'before' xattrs of $file1.
11384         getfattr --absolute-names --dump --match=- $file1 > $xattr0
11385
11386         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
11387                 namelist+=" lfsck_namespace"
11388         for name in $namelist; do
11389                 # Try to copy xattr from $file0 to $file1.
11390                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11391
11392                 setfattr --name=trusted.$name --value="$value" $file1 ||
11393                         error "setxattr 'trusted.$name' failed"
11394
11395                 # Try to set a garbage xattr.
11396                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11397
11398                 if [[ x$name == "xlov" ]]; then
11399                         setfattr --name=trusted.lov --value="$value" $file1 &&
11400                         error "setxattr invalid 'trusted.lov' success"
11401                 else
11402                         setfattr --name=trusted.$name --value="$value" $file1 ||
11403                                 error "setxattr invalid 'trusted.$name' failed"
11404                 fi
11405
11406                 # Try to remove the xattr from $file1. We don't care if this
11407                 # appears to succeed or fail, we just don't want there to be
11408                 # any changes or crashes.
11409                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11410         done
11411
11412         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
11413         then
11414                 name="lfsck_ns"
11415                 # Try to copy xattr from $file0 to $file1.
11416                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11417
11418                 setfattr --name=trusted.$name --value="$value" $file1 ||
11419                         error "setxattr 'trusted.$name' failed"
11420
11421                 # Try to set a garbage xattr.
11422                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11423
11424                 setfattr --name=trusted.$name --value="$value" $file1 ||
11425                         error "setxattr 'trusted.$name' failed"
11426
11427                 # Try to remove the xattr from $file1. We don't care if this
11428                 # appears to succeed or fail, we just don't want there to be
11429                 # any changes or crashes.
11430                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11431         fi
11432
11433         # Get 'after' xattrs of file1.
11434         getfattr --absolute-names --dump --match=- $file1 > $xattr1
11435
11436         if ! diff $xattr0 $xattr1; then
11437                 error "before and after xattrs of '$file1' differ"
11438         fi
11439
11440         rm -rf $file0 $file1 $xattr0 $xattr1
11441
11442         return 0
11443 }
11444 run_test 102n "silently ignore setxattr on internal trusted xattrs"
11445
11446 test_102p() { # LU-4703 setxattr did not check ownership
11447         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
11448                 skip "MDS needs to be at least 2.5.56"
11449
11450         local testfile=$DIR/$tfile
11451
11452         touch $testfile
11453
11454         echo "setfacl as user..."
11455         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
11456         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
11457
11458         echo "setfattr as user..."
11459         setfacl -m "u:$RUNAS_ID:---" $testfile
11460         $RUNAS setfattr -x system.posix_acl_access $testfile
11461         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
11462 }
11463 run_test 102p "check setxattr(2) correctly fails without permission"
11464
11465 test_102q() {
11466         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
11467                 skip "MDS needs to be at least 2.6.92"
11468
11469         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
11470 }
11471 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
11472
11473 test_102r() {
11474         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
11475                 skip "MDS needs to be at least 2.6.93"
11476
11477         touch $DIR/$tfile || error "touch"
11478         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
11479         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
11480         rm $DIR/$tfile || error "rm"
11481
11482         #normal directory
11483         mkdir -p $DIR/$tdir || error "mkdir"
11484         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11485         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11486         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11487                 error "$testfile error deleting user.author1"
11488         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11489                 grep "user.$(basename $tdir)" &&
11490                 error "$tdir did not delete user.$(basename $tdir)"
11491         rmdir $DIR/$tdir || error "rmdir"
11492
11493         #striped directory
11494         test_mkdir $DIR/$tdir
11495         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11496         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11497         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11498                 error "$testfile error deleting user.author1"
11499         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11500                 grep "user.$(basename $tdir)" &&
11501                 error "$tdir did not delete user.$(basename $tdir)"
11502         rmdir $DIR/$tdir || error "rm striped dir"
11503 }
11504 run_test 102r "set EAs with empty values"
11505
11506 test_102s() {
11507         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11508                 skip "MDS needs to be at least 2.11.52"
11509
11510         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11511
11512         save_lustre_params client "llite.*.xattr_cache" > $save
11513
11514         for cache in 0 1; do
11515                 lctl set_param llite.*.xattr_cache=$cache
11516
11517                 rm -f $DIR/$tfile
11518                 touch $DIR/$tfile || error "touch"
11519                 for prefix in lustre security system trusted user; do
11520                         # Note getxattr() may fail with 'Operation not
11521                         # supported' or 'No such attribute' depending
11522                         # on prefix and cache.
11523                         getfattr -n $prefix.n102s $DIR/$tfile &&
11524                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
11525                 done
11526         done
11527
11528         restore_lustre_params < $save
11529 }
11530 run_test 102s "getting nonexistent xattrs should fail"
11531
11532 test_102t() {
11533         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11534                 skip "MDS needs to be at least 2.11.52"
11535
11536         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11537
11538         save_lustre_params client "llite.*.xattr_cache" > $save
11539
11540         for cache in 0 1; do
11541                 lctl set_param llite.*.xattr_cache=$cache
11542
11543                 for buf_size in 0 256; do
11544                         rm -f $DIR/$tfile
11545                         touch $DIR/$tfile || error "touch"
11546                         setfattr -n user.multiop $DIR/$tfile
11547                         $MULTIOP $DIR/$tfile oa$buf_size ||
11548                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
11549                 done
11550         done
11551
11552         restore_lustre_params < $save
11553 }
11554 run_test 102t "zero length xattr values handled correctly"
11555
11556 run_acl_subtest()
11557 {
11558         local test=$LUSTRE/tests/acl/$1.test
11559         local tmp=$(mktemp -t $1-XXXXXX).test
11560         local bin=$2
11561         local dmn=$3
11562         local grp=$4
11563         local nbd=$5
11564         export LANG=C
11565
11566
11567         local sedusers="-e s/bin/$bin/g -e s/daemon/$dmn/g"
11568         local sedgroups="-e s/:users/:$grp/g"
11569         [[ -z "$nbd" ]] || sedusers+=" -e s/nobody/$nbd/g"
11570
11571         sed $sedusers $sedgroups < $test > $tmp
11572         stack_trap "rm -f $tmp"
11573         [[ -s $tmp ]] || error "sed failed to create test script"
11574
11575         echo "performing $1 with bin='$bin' daemon='$dmn' users='$grp'..."
11576         $LUSTRE/tests/acl/run $tmp || error "run_acl_subtest '$1' failed"
11577 }
11578
11579 test_103a() {
11580         [ "$UID" != 0 ] && skip "must run as root"
11581         $GSS && skip_env "could not run under gss"
11582         [[ "$(lctl get_param -n mdc.*-mdc-*.connect_flags)" =~ "acl" ]] ||
11583                 skip_env "must have acl enabled"
11584         which setfacl || skip_env "could not find setfacl"
11585         remote_mds_nodsh && skip "remote MDS with nodsh"
11586
11587         ACLBIN=${ACLBIN:-"bin"}
11588         ACLDMN=${ACLDMN:-"daemon"}
11589         ACLGRP=${ACLGRP:-"users"}
11590         ACLNBD=${ACLNBD:-"nobody"}
11591
11592         if ! id $ACLBIN ||
11593            [[ "$(id -u $ACLBIN)" != "$(do_facet mds1 id -u $ACLBIN)" ]]; then
11594                 echo "bad 'bin' user '$ACLBIN', using '$USER0'"
11595                 ACLBIN=$USER0
11596                 if ! id $ACLBIN ; then
11597                         cat /etc/passwd
11598                         skip_env "can't find suitable ACL 'bin' $ACLBIN"
11599                 fi
11600         fi
11601         if ! id $ACLDMN || (( $(id -u $ACLDMN) < $(id -u $ACLBIN) )) ||
11602            [[ "$(id -u $ACLDMN)" != "$(do_facet mds1 id -u $ACLDMN)" ]]; then
11603                 echo "bad 'daemon' user '$ACLDMN', using '$USER1'"
11604                 ACLDMN=$USER1
11605                 if ! id $ACLDMN ; then
11606                         cat /etc/passwd
11607                         skip_env "can't find suitable ACL 'daemon' $ACLDMN"
11608                 fi
11609         fi
11610         if ! getent group $ACLGRP; then
11611                 echo "missing 'users' group '$ACLGRP', using '$TSTUSR'"
11612                 ACLGRP="$TSTUSR"
11613                 if ! getent group $ACLGRP; then
11614                         echo "cannot find group '$ACLGRP', adding it"
11615                         cat /etc/group
11616                         add_group 60000 $ACLGRP
11617                 fi
11618         fi
11619
11620         local bingid=$(getent group $ACLBIN | cut -d: -f 3)
11621         local dmngid=$(getent group $ACLDMN | cut -d: -f 3)
11622         local grpgid=$(getent group $ACLGRP | cut -d: -f 3)
11623
11624         if (( $bingid > $grpgid || $dmngid > $grpgid )); then
11625                 echo "group '$ACLGRP' has low gid=$grpgid, use '$TSTUSR'"
11626                 ACLGRP="$TSTUSR"
11627                 if ! getent group $ACLGRP; then
11628                         echo "cannot find group '$ACLGRP', adding it"
11629                         cat /etc/group
11630                         add_group 60000 $ACLGRP
11631                 fi
11632                 grpgid=$(getent group $ACLGRP | cut -d: -f 3)
11633                 if (( $bingid > $grpgid || $dmngid > $grpgid )); then
11634                         cat /etc/group
11635                         skip_env "$ACLGRP gid=$grpgid less than $bingid|$dmngid"
11636                 fi
11637         fi
11638
11639         gpasswd -a $ACLDMN $ACLBIN ||
11640                 error "setting client group failed"             # LU-5641
11641         do_facet mds1 gpasswd -a $ACLDMN $ACLBIN ||
11642                 error "setting MDS group failed"                # LU-5641
11643
11644         declare -a identity_old
11645
11646         for num in $(seq $MDSCOUNT); do
11647                 switch_identity $num true || identity_old[$num]=$?
11648         done
11649
11650         SAVE_UMASK=$(umask)
11651         umask 0022
11652         mkdir -p $DIR/$tdir
11653         cd $DIR/$tdir
11654
11655         run_acl_subtest cp $ACLBIN $ACLDMN $ACLGRP
11656         run_acl_subtest getfacl-noacl $ACLBIN $ACLDMN $ACLGRP
11657         run_acl_subtest misc $ACLBIN $ACLDMN $ACLGRP
11658         run_acl_subtest permissions $ACLBIN $ACLDMN $ACLGRP
11659         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
11660         # CentOS7- uses nobody=99, while newer distros use nobody=65534
11661         if ! id -u $ACLNBD ||
11662            (( $(id -u nobody) != $(do_facet mds1 id -u nobody) )); then
11663                 ACLNBD="nfsnobody"
11664                 if ! id -u $ACLNBD; then
11665                         ACLNBD=""
11666                 fi
11667         fi
11668         if [[ -n "$ACLNBD" ]] && ! getent group $ACLNBD; then
11669                 add_group $(id -u $ACLNBD) $ACLNBD
11670                 if ! getent group $ACLNBD; then
11671                         ACLNBD=""
11672                 fi
11673         fi
11674         if (( $MDS1_VERSION > $(version_code 2.8.55) )) &&
11675            [[ -n "$ACLNBD" ]] && which setfattr; then
11676                 run_acl_subtest permissions_xattr \
11677                         $ACLBIN $ACLDMN $ACLGRP $ACLNBD
11678         elif [[ -z "$ACLNBD" ]]; then
11679                 echo "skip 'permission_xattr' test - missing 'nobody' user/grp"
11680         else
11681                 echo "skip 'permission_xattr' test - missing setfattr command"
11682         fi
11683         run_acl_subtest setfacl $ACLBIN $ACLDMN $ACLGRP
11684
11685         # inheritance test got from HP
11686         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
11687         chmod +x make-tree || error "chmod +x failed"
11688         run_acl_subtest inheritance $ACLBIN $ACLDMN $ACLGRP
11689         rm -f make-tree
11690
11691         echo "LU-974 ignore umask when acl is enabled..."
11692         run_acl_subtest 974 $ACLBIN $ACLDMN $ACLGRP
11693         if [ $MDSCOUNT -ge 2 ]; then
11694                 run_acl_subtest 974_remote $ACLBIN $ACLDMN $ACLGRP
11695         fi
11696
11697         echo "LU-2561 newly created file is same size as directory..."
11698         if [ "$mds1_FSTYPE" != "zfs" ]; then
11699                 run_acl_subtest 2561 $ACLBIN $ACLDMN $ACLGRP
11700         else
11701                 run_acl_subtest 2561_zfs $ACLBIN $ACLDMN $ACLGRP
11702         fi
11703
11704         run_acl_subtest 4924 $ACLBIN $ACLDMN $ACLGRP
11705
11706         cd $SAVE_PWD
11707         umask $SAVE_UMASK
11708
11709         for num in $(seq $MDSCOUNT); do
11710                 if [ "${identity_old[$num]}" = 1 ]; then
11711                         switch_identity $num false || identity_old[$num]=$?
11712                 fi
11713         done
11714 }
11715 run_test 103a "acl test"
11716
11717 test_103b() {
11718         declare -a pids
11719         local U
11720
11721         for U in {0..511}; do
11722                 {
11723                 local O=$(printf "%04o" $U)
11724
11725                 umask $(printf "%04o" $((511 ^ $O)))
11726                 $LFS setstripe -c 1 $DIR/$tfile.s$O
11727                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
11728
11729                 (( $S == ($O & 0666) )) ||
11730                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
11731
11732                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
11733                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
11734                 (( $S == ($O & 0666) )) ||
11735                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
11736
11737                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
11738                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
11739                 (( $S == ($O & 0666) )) ||
11740                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
11741                 rm -f $DIR/$tfile.[smp]$0
11742                 } &
11743                 local pid=$!
11744
11745                 # limit the concurrently running threads to 64. LU-11878
11746                 local idx=$((U % 64))
11747                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
11748                 pids[idx]=$pid
11749         done
11750         wait
11751 }
11752 run_test 103b "umask lfs setstripe"
11753
11754 test_103c() {
11755         mkdir -p $DIR/$tdir
11756         cp -rp $DIR/$tdir $DIR/$tdir.bak
11757
11758         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
11759                 error "$DIR/$tdir shouldn't contain default ACL"
11760         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
11761                 error "$DIR/$tdir.bak shouldn't contain default ACL"
11762         true
11763 }
11764 run_test 103c "'cp -rp' won't set empty acl"
11765
11766 test_103e() {
11767         local numacl
11768         local fileacl
11769         local saved_debug=$($LCTL get_param -n debug)
11770
11771         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
11772                 skip "MDS needs to be at least 2.14.52"
11773
11774         large_xattr_enabled || skip_env "ea_inode feature disabled"
11775
11776         mkdir -p $DIR/$tdir
11777         # add big LOV EA to cause reply buffer overflow earlier
11778         $LFS setstripe -C 1000 $DIR/$tdir
11779         lctl set_param mdc.*-mdc*.stats=clear
11780
11781         $LCTL set_param debug=0
11782         stack_trap "$LCTL set_param debug=\"$saved_debug\"" EXIT
11783         stack_trap "$LCTL get_param mdc.*-mdc*.stats" EXIT
11784
11785         # add a large number of default ACLs (expect 8000+ for 2.13+)
11786         for U in {2..7000}; do
11787                 setfacl -d -m user:$U:rwx $DIR/$tdir ||
11788                         error "Able to add just $U default ACLs"
11789         done
11790         numacl=$(getfacl $DIR/$tdir |& grep -c "default:user")
11791         echo "$numacl default ACLs created"
11792
11793         stat $DIR/$tdir || error "Cannot stat directory"
11794         # check file creation
11795         touch $DIR/$tdir/$tfile ||
11796                 error "failed to create $tfile with $numacl default ACLs"
11797         stat $DIR/$tdir/$tfile  || error "Cannot stat file"
11798         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11799         echo "$fileacl ACLs were inherited"
11800         (( $fileacl == $numacl )) ||
11801                 error "Not all default ACLs were inherited: $numacl != $fileacl"
11802         # check that new ACLs creation adds new ACLs to inherited ACLs
11803         setfacl -m user:19000:rwx $DIR/$tdir/$tfile ||
11804                 error "Cannot set new ACL"
11805         numacl=$((numacl + 1))
11806         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11807         (( $fileacl == $numacl )) ||
11808                 error "failed to add new ACL: $fileacl != $numacl as expected"
11809         # adds more ACLs to a file to reach their maximum at 8000+
11810         numacl=0
11811         for U in {20000..25000}; do
11812                 setfacl -m user:$U:rwx $DIR/$tdir/$tfile || break
11813                 numacl=$((numacl + 1))
11814         done
11815         echo "Added $numacl more ACLs to the file"
11816         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11817         echo "Total $fileacl ACLs in file"
11818         stat $DIR/$tdir/$tfile > /dev/null || error "Cannot stat file"
11819         rm -f $DIR/$tdir/$tfile || error "Cannot remove file"
11820         rmdir $DIR/$tdir || error "Cannot remove directory"
11821 }
11822 run_test 103e "inheritance of big amount of default ACLs"
11823
11824 test_103f() {
11825         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
11826                 skip "MDS needs to be at least 2.14.51"
11827
11828         large_xattr_enabled || skip_env "ea_inode feature disabled"
11829
11830         # enable changelog to consume more internal MDD buffers
11831         changelog_register
11832
11833         mkdir -p $DIR/$tdir
11834         # add big LOV EA
11835         $LFS setstripe -C 1000 $DIR/$tdir
11836         setfacl -d -m user:$U:rwx $DIR/$tdir || error "Cannot add default ACLs"
11837         mkdir $DIR/$tdir/inherited || error "failed to create subdirectory"
11838         rmdir $DIR/$tdir/inherited || error "Cannot remove subdirectory"
11839         rmdir $DIR/$tdir || error "Cannot remove directory"
11840 }
11841 run_test 103f "changelog doesn't interfere with default ACLs buffers"
11842
11843 test_104a() {
11844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11845
11846         touch $DIR/$tfile
11847         lfs df || error "lfs df failed"
11848         lfs df -ih || error "lfs df -ih failed"
11849         lfs df -h $DIR || error "lfs df -h $DIR failed"
11850         lfs df -i $DIR || error "lfs df -i $DIR failed"
11851         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
11852         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
11853
11854         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
11855         lctl --device %$OSC deactivate
11856         lfs df || error "lfs df with deactivated OSC failed"
11857         lctl --device %$OSC activate
11858         # wait the osc back to normal
11859         wait_osc_import_ready client ost
11860
11861         lfs df || error "lfs df with reactivated OSC failed"
11862         rm -f $DIR/$tfile
11863 }
11864 run_test 104a "lfs df [-ih] [path] test ========================="
11865
11866 test_104b() {
11867         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11868         [ $RUNAS_ID -eq $UID ] &&
11869                 skip_env "RUNAS_ID = UID = $UID -- skipping"
11870
11871         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
11872                         grep "Permission denied" | wc -l)))
11873         if [ $denied_cnt -ne 0 ]; then
11874                 error "lfs check servers test failed"
11875         fi
11876 }
11877 run_test 104b "$RUNAS lfs check servers test ===================="
11878
11879 #
11880 # Verify $1 is within range of $2.
11881 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
11882 # $1 is <= 2% of $2. Else Fail.
11883 #
11884 value_in_range() {
11885         # Strip all units (M, G, T)
11886         actual=$(echo $1 | tr -d A-Z)
11887         expect=$(echo $2 | tr -d A-Z)
11888
11889         expect_lo=$(($expect * 98 / 100)) # 2% below
11890         expect_hi=$(($expect * 102 / 100)) # 2% above
11891
11892         # permit 2% drift above and below
11893         (( $actual >= $expect_lo && $actual <= $expect_hi ))
11894 }
11895
11896 test_104c() {
11897         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11898         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
11899
11900         local ost_param="osd-zfs.$FSNAME-OST0000."
11901         local mdt_param="osd-zfs.$FSNAME-MDT0000."
11902         local ofacets=$(get_facets OST)
11903         local mfacets=$(get_facets MDS)
11904         local saved_ost_blocks=
11905         local saved_mdt_blocks=
11906
11907         echo "Before recordsize change"
11908         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
11909         df=($(df -h | grep "$MOUNT"$))
11910
11911         # For checking.
11912         echo "lfs output : ${lfs_df[*]}"
11913         echo "df  output : ${df[*]}"
11914
11915         for facet in ${ofacets//,/ }; do
11916                 if [ -z $saved_ost_blocks ]; then
11917                         saved_ost_blocks=$(do_facet $facet \
11918                                 lctl get_param -n $ost_param.blocksize)
11919                         echo "OST Blocksize: $saved_ost_blocks"
11920                 fi
11921                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11922                 do_facet $facet zfs set recordsize=32768 $ost
11923         done
11924
11925         # BS too small. Sufficient for functional testing.
11926         for facet in ${mfacets//,/ }; do
11927                 if [ -z $saved_mdt_blocks ]; then
11928                         saved_mdt_blocks=$(do_facet $facet \
11929                                 lctl get_param -n $mdt_param.blocksize)
11930                         echo "MDT Blocksize: $saved_mdt_blocks"
11931                 fi
11932                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11933                 do_facet $facet zfs set recordsize=32768 $mdt
11934         done
11935
11936         # Give new values chance to reflect change
11937         sleep 2
11938
11939         echo "After recordsize change"
11940         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
11941         df_after=($(df -h | grep "$MOUNT"$))
11942
11943         # For checking.
11944         echo "lfs output : ${lfs_df_after[*]}"
11945         echo "df  output : ${df_after[*]}"
11946
11947         # Verify lfs df
11948         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
11949                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
11950         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
11951                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
11952         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
11953                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
11954
11955         # Verify df
11956         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
11957                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
11958         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
11959                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
11960         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
11961                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
11962
11963         # Restore MDT recordize back to original
11964         for facet in ${mfacets//,/ }; do
11965                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
11966                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
11967         done
11968
11969         # Restore OST recordize back to original
11970         for facet in ${ofacets//,/ }; do
11971                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11972                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
11973         done
11974
11975         return 0
11976 }
11977 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
11978
11979 test_105a() {
11980         # doesn't work on 2.4 kernels
11981         touch $DIR/$tfile
11982         if $(flock_is_enabled); then
11983                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
11984         else
11985                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
11986         fi
11987         rm -f $DIR/$tfile
11988 }
11989 run_test 105a "flock when mounted without -o flock test ========"
11990
11991 test_105b() {
11992         touch $DIR/$tfile
11993         if $(flock_is_enabled); then
11994                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
11995         else
11996                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
11997         fi
11998         rm -f $DIR/$tfile
11999 }
12000 run_test 105b "fcntl when mounted without -o flock test ========"
12001
12002 test_105c() {
12003         touch $DIR/$tfile
12004         if $(flock_is_enabled); then
12005                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
12006         else
12007                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
12008         fi
12009         rm -f $DIR/$tfile
12010 }
12011 run_test 105c "lockf when mounted without -o flock test"
12012
12013 test_105d() { # bug 15924
12014         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12015
12016         test_mkdir $DIR/$tdir
12017         flock_is_enabled || skip_env "mount w/o flock enabled"
12018         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
12019         $LCTL set_param fail_loc=0x80000315
12020         flocks_test 2 $DIR/$tdir
12021 }
12022 run_test 105d "flock race (should not freeze) ========"
12023
12024 test_105e() { # bug 22660 && 22040
12025         flock_is_enabled || skip_env "mount w/o flock enabled"
12026
12027         touch $DIR/$tfile
12028         flocks_test 3 $DIR/$tfile
12029 }
12030 run_test 105e "Two conflicting flocks from same process"
12031
12032 test_106() { #bug 10921
12033         test_mkdir $DIR/$tdir
12034         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
12035         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
12036 }
12037 run_test 106 "attempt exec of dir followed by chown of that dir"
12038
12039 test_107() {
12040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12041
12042         CDIR=`pwd`
12043         local file=core
12044
12045         cd $DIR
12046         rm -f $file
12047
12048         local save_pattern=$(sysctl -n kernel.core_pattern)
12049         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
12050         sysctl -w kernel.core_pattern=$file
12051         sysctl -w kernel.core_uses_pid=0
12052
12053         ulimit -c unlimited
12054         sleep 60 &
12055         SLEEPPID=$!
12056
12057         sleep 1
12058
12059         kill -s 11 $SLEEPPID
12060         wait $SLEEPPID
12061         if [ -e $file ]; then
12062                 size=`stat -c%s $file`
12063                 [ $size -eq 0 ] && error "Fail to create core file $file"
12064         else
12065                 error "Fail to create core file $file"
12066         fi
12067         rm -f $file
12068         sysctl -w kernel.core_pattern=$save_pattern
12069         sysctl -w kernel.core_uses_pid=$save_uses_pid
12070         cd $CDIR
12071 }
12072 run_test 107 "Coredump on SIG"
12073
12074 test_110() {
12075         test_mkdir $DIR/$tdir
12076         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
12077         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
12078                 error "mkdir with 256 char should fail, but did not"
12079         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
12080                 error "create with 255 char failed"
12081         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
12082                 error "create with 256 char should fail, but did not"
12083
12084         ls -l $DIR/$tdir
12085         rm -rf $DIR/$tdir
12086 }
12087 run_test 110 "filename length checking"
12088
12089 #
12090 # Purpose: To verify dynamic thread (OSS) creation.
12091 #
12092 test_115() {
12093         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12094         remote_ost_nodsh && skip "remote OST with nodsh"
12095
12096         # Lustre does not stop service threads once they are started.
12097         # Reset number of running threads to default.
12098         stopall
12099         setupall
12100
12101         local OSTIO_pre
12102         local save_params="$TMP/sanity-$TESTNAME.parameters"
12103
12104         # Get ll_ost_io count before I/O
12105         OSTIO_pre=$(do_facet ost1 \
12106                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
12107         # Exit if lustre is not running (ll_ost_io not running).
12108         [ -z "$OSTIO_pre" ] && error "no OSS threads"
12109
12110         echo "Starting with $OSTIO_pre threads"
12111         local thread_max=$((OSTIO_pre * 2))
12112         local rpc_in_flight=$((thread_max * 2))
12113         # this is limited to OSC_MAX_RIF_MAX (256)
12114         [ $rpc_in_flight -gt 256 ] && rpc_in_flight=256
12115         thread_max=$((rpc_in_flight / 2))
12116         [ $thread_max -le $OSTIO_pre ] && skip "Too many ost_io threads" &&
12117                 return
12118
12119         # Number of I/O Process proposed to be started.
12120         local nfiles
12121         local facets=$(get_facets OST)
12122
12123         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
12124         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
12125
12126         # Set in_flight to $rpc_in_flight
12127         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
12128                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
12129         nfiles=${rpc_in_flight}
12130         # Set ost thread_max to $thread_max
12131         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
12132
12133         # 5 Minutes should be sufficient for max number of OSS
12134         # threads(thread_max) to be created.
12135         local timeout=300
12136
12137         # Start I/O.
12138         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
12139         test_mkdir $DIR/$tdir
12140         for i in $(seq $nfiles); do
12141                 local file=$DIR/$tdir/${tfile}-$i
12142                 $LFS setstripe -c -1 -i 0 $file
12143                 ($WTL $file $timeout)&
12144         done
12145
12146         # I/O Started - Wait for thread_started to reach thread_max or report
12147         # error if thread_started is more than thread_max.
12148         echo "Waiting for thread_started to reach thread_max"
12149         local thread_started=0
12150         local end_time=$((SECONDS + timeout))
12151
12152         while [ $SECONDS -le $end_time ] ; do
12153                 echo -n "."
12154                 # Get ost i/o thread_started count.
12155                 thread_started=$(do_facet ost1 \
12156                         "$LCTL get_param \
12157                         ost.OSS.ost_io.threads_started | cut -d= -f2")
12158                 # Break out if thread_started is equal/greater than thread_max
12159                 if [[ $thread_started -ge $thread_max ]]; then
12160                         echo ll_ost_io thread_started $thread_started, \
12161                                 equal/greater than thread_max $thread_max
12162                         break
12163                 fi
12164                 sleep 1
12165         done
12166
12167         # Cleanup - We have the numbers, Kill i/o jobs if running.
12168         jobcount=($(jobs -p))
12169         for i in $(seq 0 $((${#jobcount[@]}-1)))
12170         do
12171                 kill -9 ${jobcount[$i]}
12172                 if [ $? -ne 0 ] ; then
12173                         echo Warning: \
12174                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
12175                 fi
12176         done
12177
12178         # Cleanup files left by WTL binary.
12179         for i in $(seq $nfiles); do
12180                 local file=$DIR/$tdir/${tfile}-$i
12181                 rm -rf $file
12182                 if [ $? -ne 0 ] ; then
12183                         echo "Warning: Failed to delete file $file"
12184                 fi
12185         done
12186
12187         restore_lustre_params <$save_params
12188         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
12189
12190         # Error out if no new thread has started or Thread started is greater
12191         # than thread max.
12192         if [[ $thread_started -le $OSTIO_pre ||
12193                         $thread_started -gt $thread_max ]]; then
12194                 error "ll_ost_io: thread_started $thread_started" \
12195                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
12196                       "No new thread started or thread started greater " \
12197                       "than thread_max."
12198         fi
12199 }
12200 run_test 115 "verify dynamic thread creation===================="
12201
12202 test_116a() { # was previously test_116()
12203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12204         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12205         remote_mds_nodsh && skip "remote MDS with nodsh"
12206
12207         echo -n "Free space priority "
12208         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
12209                 head -n1
12210         declare -a AVAIL
12211         free_min_max
12212
12213         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
12214         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
12215         stack_trap simple_cleanup_common
12216
12217         # Check if we need to generate uneven OSTs
12218         test_mkdir -p $DIR/$tdir/OST${MINI}
12219         local FILL=$((MINV / 4))
12220         local DIFF=$((MAXV - MINV))
12221         local DIFF2=$((DIFF * 100 / MINV))
12222
12223         local threshold=$(do_facet $SINGLEMDS \
12224                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
12225         threshold=${threshold%%%}
12226         echo -n "Check for uneven OSTs: "
12227         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
12228
12229         if [[ $DIFF2 -gt $threshold ]]; then
12230                 echo "ok"
12231                 echo "Don't need to fill OST$MINI"
12232         else
12233                 # generate uneven OSTs. Write 2% over the QOS threshold value
12234                 echo "no"
12235                 DIFF=$((threshold - DIFF2 + 2))
12236                 DIFF2=$((MINV * DIFF / 100))
12237                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
12238                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
12239                         error "setstripe failed"
12240                 DIFF=$((DIFF2 / 2048))
12241                 i=0
12242                 while [ $i -lt $DIFF ]; do
12243                         i=$((i + 1))
12244                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
12245                                 bs=2M count=1 2>/dev/null
12246                         echo -n .
12247                 done
12248                 echo .
12249                 sync
12250                 sleep_maxage
12251                 free_min_max
12252         fi
12253
12254         DIFF=$((MAXV - MINV))
12255         DIFF2=$((DIFF * 100 / MINV))
12256         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
12257         if [ $DIFF2 -gt $threshold ]; then
12258                 echo "ok"
12259         else
12260                 skip "QOS imbalance criteria not met"
12261         fi
12262
12263         MINI1=$MINI
12264         MINV1=$MINV
12265         MAXI1=$MAXI
12266         MAXV1=$MAXV
12267
12268         # now fill using QOS
12269         $LFS setstripe -c 1 $DIR/$tdir
12270         FILL=$((FILL / 200))
12271         if [ $FILL -gt 600 ]; then
12272                 FILL=600
12273         fi
12274         echo "writing $FILL files to QOS-assigned OSTs"
12275         i=0
12276         while [ $i -lt $FILL ]; do
12277                 i=$((i + 1))
12278                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
12279                         count=1 2>/dev/null
12280                 echo -n .
12281         done
12282         echo "wrote $i 200k files"
12283         sync
12284         sleep_maxage
12285
12286         echo "Note: free space may not be updated, so measurements might be off"
12287         free_min_max
12288         DIFF2=$((MAXV - MINV))
12289         echo "free space delta: orig $DIFF final $DIFF2"
12290         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
12291         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
12292         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
12293         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
12294         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
12295         if [[ $DIFF -gt 0 ]]; then
12296                 FILL=$((DIFF2 * 100 / DIFF - 100))
12297                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
12298         fi
12299
12300         # Figure out which files were written where
12301         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
12302                awk '/'$MINI1': / {print $2; exit}')
12303         echo $UUID
12304         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
12305         echo "$MINC files created on smaller OST $MINI1"
12306         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
12307                awk '/'$MAXI1': / {print $2; exit}')
12308         echo $UUID
12309         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
12310         echo "$MAXC files created on larger OST $MAXI1"
12311         if [[ $MINC -gt 0 ]]; then
12312                 FILL=$((MAXC * 100 / MINC - 100))
12313                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
12314         fi
12315         [[ $MAXC -gt $MINC ]] ||
12316                 error_ignore LU-9 "stripe QOS didn't balance free space"
12317 }
12318 run_test 116a "stripe QOS: free space balance ==================="
12319
12320 test_116b() { # LU-2093
12321         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12322         remote_mds_nodsh && skip "remote MDS with nodsh"
12323
12324 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
12325         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
12326                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
12327         [ -z "$old_rr" ] && skip "no QOS"
12328         do_facet $SINGLEMDS lctl set_param \
12329                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
12330         mkdir -p $DIR/$tdir
12331         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
12332         createmany -o $DIR/$tdir/f- 20 || error "can't create"
12333         do_facet $SINGLEMDS lctl set_param fail_loc=0
12334         rm -rf $DIR/$tdir
12335         do_facet $SINGLEMDS lctl set_param \
12336                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
12337 }
12338 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
12339
12340 test_117() # bug 10891
12341 {
12342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12343
12344         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
12345         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
12346         lctl set_param fail_loc=0x21e
12347         > $DIR/$tfile || error "truncate failed"
12348         lctl set_param fail_loc=0
12349         echo "Truncate succeeded."
12350         rm -f $DIR/$tfile
12351 }
12352 run_test 117 "verify osd extend =========="
12353
12354 NO_SLOW_RESENDCOUNT=4
12355 export OLD_RESENDCOUNT=""
12356 set_resend_count () {
12357         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
12358         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
12359         lctl set_param -n $PROC_RESENDCOUNT $1
12360         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
12361 }
12362
12363 # for reduce test_118* time (b=14842)
12364 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12365
12366 # Reset async IO behavior after error case
12367 reset_async() {
12368         FILE=$DIR/reset_async
12369
12370         # Ensure all OSCs are cleared
12371         $LFS setstripe -c -1 $FILE
12372         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
12373         sync
12374         rm $FILE
12375 }
12376
12377 test_118a() #bug 11710
12378 {
12379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12380
12381         reset_async
12382
12383         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12384         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12385         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12386
12387         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12388                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12389                 return 1;
12390         fi
12391         rm -f $DIR/$tfile
12392 }
12393 run_test 118a "verify O_SYNC works =========="
12394
12395 test_118b()
12396 {
12397         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12398         remote_ost_nodsh && skip "remote OST with nodsh"
12399
12400         reset_async
12401
12402         #define OBD_FAIL_SRV_ENOENT 0x217
12403         set_nodes_failloc "$(osts_nodes)" 0x217
12404         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12405         RC=$?
12406         set_nodes_failloc "$(osts_nodes)" 0
12407         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12408         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12409                     grep -c writeback)
12410
12411         if [[ $RC -eq 0 ]]; then
12412                 error "Must return error due to dropped pages, rc=$RC"
12413                 return 1;
12414         fi
12415
12416         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12417                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12418                 return 1;
12419         fi
12420
12421         echo "Dirty pages not leaked on ENOENT"
12422
12423         # Due to the above error the OSC will issue all RPCs syncronously
12424         # until a subsequent RPC completes successfully without error.
12425         $MULTIOP $DIR/$tfile Ow4096yc
12426         rm -f $DIR/$tfile
12427
12428         return 0
12429 }
12430 run_test 118b "Reclaim dirty pages on fatal error =========="
12431
12432 test_118c()
12433 {
12434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12435
12436         # for 118c, restore the original resend count, LU-1940
12437         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
12438                                 set_resend_count $OLD_RESENDCOUNT
12439         remote_ost_nodsh && skip "remote OST with nodsh"
12440
12441         reset_async
12442
12443         #define OBD_FAIL_OST_EROFS               0x216
12444         set_nodes_failloc "$(osts_nodes)" 0x216
12445
12446         # multiop should block due to fsync until pages are written
12447         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12448         MULTIPID=$!
12449         sleep 1
12450
12451         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12452                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12453         fi
12454
12455         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12456                     grep -c writeback)
12457         if [[ $WRITEBACK -eq 0 ]]; then
12458                 error "No page in writeback, writeback=$WRITEBACK"
12459         fi
12460
12461         set_nodes_failloc "$(osts_nodes)" 0
12462         wait $MULTIPID
12463         RC=$?
12464         if [[ $RC -ne 0 ]]; then
12465                 error "Multiop fsync failed, rc=$RC"
12466         fi
12467
12468         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12469         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12470                     grep -c writeback)
12471         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12472                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12473         fi
12474
12475         rm -f $DIR/$tfile
12476         echo "Dirty pages flushed via fsync on EROFS"
12477         return 0
12478 }
12479 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
12480
12481 # continue to use small resend count to reduce test_118* time (b=14842)
12482 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12483
12484 test_118d()
12485 {
12486         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12487         remote_ost_nodsh && skip "remote OST with nodsh"
12488
12489         reset_async
12490
12491         #define OBD_FAIL_OST_BRW_PAUSE_BULK
12492         set_nodes_failloc "$(osts_nodes)" 0x214
12493         # multiop should block due to fsync until pages are written
12494         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12495         MULTIPID=$!
12496         sleep 1
12497
12498         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12499                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12500         fi
12501
12502         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12503                     grep -c writeback)
12504         if [[ $WRITEBACK -eq 0 ]]; then
12505                 error "No page in writeback, writeback=$WRITEBACK"
12506         fi
12507
12508         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
12509         set_nodes_failloc "$(osts_nodes)" 0
12510
12511         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12512         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12513                     grep -c writeback)
12514         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12515                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12516         fi
12517
12518         rm -f $DIR/$tfile
12519         echo "Dirty pages gaurenteed flushed via fsync"
12520         return 0
12521 }
12522 run_test 118d "Fsync validation inject a delay of the bulk =========="
12523
12524 test_118f() {
12525         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12526
12527         reset_async
12528
12529         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
12530         lctl set_param fail_loc=0x8000040a
12531
12532         # Should simulate EINVAL error which is fatal
12533         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12534         RC=$?
12535         if [[ $RC -eq 0 ]]; then
12536                 error "Must return error due to dropped pages, rc=$RC"
12537         fi
12538
12539         lctl set_param fail_loc=0x0
12540
12541         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12542         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12543         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12544                     grep -c writeback)
12545         if [[ $LOCKED -ne 0 ]]; then
12546                 error "Locked pages remain in cache, locked=$LOCKED"
12547         fi
12548
12549         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12550                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12551         fi
12552
12553         rm -f $DIR/$tfile
12554         echo "No pages locked after fsync"
12555
12556         reset_async
12557         return 0
12558 }
12559 run_test 118f "Simulate unrecoverable OSC side error =========="
12560
12561 test_118g() {
12562         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12563
12564         reset_async
12565
12566         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
12567         lctl set_param fail_loc=0x406
12568
12569         # simulate local -ENOMEM
12570         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12571         RC=$?
12572
12573         lctl set_param fail_loc=0
12574         if [[ $RC -eq 0 ]]; then
12575                 error "Must return error due to dropped pages, rc=$RC"
12576         fi
12577
12578         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12579         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12580         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12581                         grep -c writeback)
12582         if [[ $LOCKED -ne 0 ]]; then
12583                 error "Locked pages remain in cache, locked=$LOCKED"
12584         fi
12585
12586         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12587                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12588         fi
12589
12590         rm -f $DIR/$tfile
12591         echo "No pages locked after fsync"
12592
12593         reset_async
12594         return 0
12595 }
12596 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
12597
12598 test_118h() {
12599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12600         remote_ost_nodsh && skip "remote OST with nodsh"
12601
12602         reset_async
12603
12604         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12605         set_nodes_failloc "$(osts_nodes)" 0x20e
12606         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12607         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12608         RC=$?
12609
12610         set_nodes_failloc "$(osts_nodes)" 0
12611         if [[ $RC -eq 0 ]]; then
12612                 error "Must return error due to dropped pages, rc=$RC"
12613         fi
12614
12615         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12616         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12617         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12618                     grep -c writeback)
12619         if [[ $LOCKED -ne 0 ]]; then
12620                 error "Locked pages remain in cache, locked=$LOCKED"
12621         fi
12622
12623         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12624                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12625         fi
12626
12627         rm -f $DIR/$tfile
12628         echo "No pages locked after fsync"
12629
12630         return 0
12631 }
12632 run_test 118h "Verify timeout in handling recoverables errors  =========="
12633
12634 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12635
12636 test_118i() {
12637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12638         remote_ost_nodsh && skip "remote OST with nodsh"
12639
12640         reset_async
12641
12642         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12643         set_nodes_failloc "$(osts_nodes)" 0x20e
12644
12645         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12646         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12647         PID=$!
12648         sleep 5
12649         set_nodes_failloc "$(osts_nodes)" 0
12650
12651         wait $PID
12652         RC=$?
12653         if [[ $RC -ne 0 ]]; then
12654                 error "got error, but should be not, rc=$RC"
12655         fi
12656
12657         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12658         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12659         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12660         if [[ $LOCKED -ne 0 ]]; then
12661                 error "Locked pages remain in cache, locked=$LOCKED"
12662         fi
12663
12664         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12665                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12666         fi
12667
12668         rm -f $DIR/$tfile
12669         echo "No pages locked after fsync"
12670
12671         return 0
12672 }
12673 run_test 118i "Fix error before timeout in recoverable error  =========="
12674
12675 [ "$SLOW" = "no" ] && set_resend_count 4
12676
12677 test_118j() {
12678         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12679         remote_ost_nodsh && skip "remote OST with nodsh"
12680
12681         reset_async
12682
12683         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
12684         set_nodes_failloc "$(osts_nodes)" 0x220
12685
12686         # return -EIO from OST
12687         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12688         RC=$?
12689         set_nodes_failloc "$(osts_nodes)" 0x0
12690         if [[ $RC -eq 0 ]]; then
12691                 error "Must return error due to dropped pages, rc=$RC"
12692         fi
12693
12694         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12695         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12696         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12697         if [[ $LOCKED -ne 0 ]]; then
12698                 error "Locked pages remain in cache, locked=$LOCKED"
12699         fi
12700
12701         # in recoverable error on OST we want resend and stay until it finished
12702         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12703                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12704         fi
12705
12706         rm -f $DIR/$tfile
12707         echo "No pages locked after fsync"
12708
12709         return 0
12710 }
12711 run_test 118j "Simulate unrecoverable OST side error =========="
12712
12713 test_118k()
12714 {
12715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12716         remote_ost_nodsh && skip "remote OSTs with nodsh"
12717
12718         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12719         set_nodes_failloc "$(osts_nodes)" 0x20e
12720         test_mkdir $DIR/$tdir
12721
12722         for ((i=0;i<10;i++)); do
12723                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
12724                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
12725                 SLEEPPID=$!
12726                 sleep 0.500s
12727                 kill $SLEEPPID
12728                 wait $SLEEPPID
12729         done
12730
12731         set_nodes_failloc "$(osts_nodes)" 0
12732         rm -rf $DIR/$tdir
12733 }
12734 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
12735
12736 test_118l() # LU-646
12737 {
12738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12739
12740         test_mkdir $DIR/$tdir
12741         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
12742         rm -rf $DIR/$tdir
12743 }
12744 run_test 118l "fsync dir"
12745
12746 test_118m() # LU-3066
12747 {
12748         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12749
12750         test_mkdir $DIR/$tdir
12751         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
12752         rm -rf $DIR/$tdir
12753 }
12754 run_test 118m "fdatasync dir ========="
12755
12756 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12757
12758 test_118n()
12759 {
12760         local begin
12761         local end
12762
12763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12764         remote_ost_nodsh && skip "remote OSTs with nodsh"
12765
12766         # Sleep to avoid a cached response.
12767         #define OBD_STATFS_CACHE_SECONDS 1
12768         sleep 2
12769
12770         # Inject a 10 second delay in the OST_STATFS handler.
12771         #define OBD_FAIL_OST_STATFS_DELAY 0x242
12772         set_nodes_failloc "$(osts_nodes)" 0x242
12773
12774         begin=$SECONDS
12775         stat --file-system $MOUNT > /dev/null
12776         end=$SECONDS
12777
12778         set_nodes_failloc "$(osts_nodes)" 0
12779
12780         if ((end - begin > 20)); then
12781             error "statfs took $((end - begin)) seconds, expected 10"
12782         fi
12783 }
12784 run_test 118n "statfs() sends OST_STATFS requests in parallel"
12785
12786 test_119a() # bug 11737
12787 {
12788         BSIZE=$((512 * 1024))
12789         directio write $DIR/$tfile 0 1 $BSIZE
12790         # We ask to read two blocks, which is more than a file size.
12791         # directio will indicate an error when requested and actual
12792         # sizes aren't equeal (a normal situation in this case) and
12793         # print actual read amount.
12794         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
12795         if [ "$NOB" != "$BSIZE" ]; then
12796                 error "read $NOB bytes instead of $BSIZE"
12797         fi
12798         rm -f $DIR/$tfile
12799 }
12800 run_test 119a "Short directIO read must return actual read amount"
12801
12802 test_119b() # bug 11737
12803 {
12804         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12805
12806         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
12807         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
12808         sync
12809         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
12810                 error "direct read failed"
12811         rm -f $DIR/$tfile
12812 }
12813 run_test 119b "Sparse directIO read must return actual read amount"
12814
12815 test_119c() # bug 13099
12816 {
12817         BSIZE=1048576
12818         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
12819         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
12820         rm -f $DIR/$tfile
12821 }
12822 run_test 119c "Testing for direct read hitting hole"
12823
12824 test_119d() # bug 15950
12825 {
12826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12827
12828         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
12829         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
12830         BSIZE=1048576
12831         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
12832         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
12833         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
12834         lctl set_param fail_loc=0x40d
12835         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
12836         pid_dio=$!
12837         sleep 1
12838         cat $DIR/$tfile > /dev/null &
12839         lctl set_param fail_loc=0
12840         pid_reads=$!
12841         wait $pid_dio
12842         log "the DIO writes have completed, now wait for the reads (should not block very long)"
12843         sleep 2
12844         [ -n "`ps h -p $pid_reads -o comm`" ] && \
12845         error "the read rpcs have not completed in 2s"
12846         rm -f $DIR/$tfile
12847         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
12848 }
12849 run_test 119d "The DIO path should try to send a new rpc once one is completed"
12850
12851 test_120a() {
12852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12853         remote_mds_nodsh && skip "remote MDS with nodsh"
12854         test_mkdir -i0 -c1 $DIR/$tdir
12855         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12856                 skip_env "no early lock cancel on server"
12857
12858         lru_resize_disable mdc
12859         lru_resize_disable osc
12860         cancel_lru_locks mdc
12861         # asynchronous object destroy at MDT could cause bl ast to client
12862         cancel_lru_locks osc
12863
12864         stat $DIR/$tdir > /dev/null
12865         can1=$(do_facet mds1 \
12866                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12867                awk '/ldlm_cancel/ {print $2}')
12868         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12869                awk '/ldlm_bl_callback/ {print $2}')
12870         test_mkdir -i0 -c1 $DIR/$tdir/d1
12871         can2=$(do_facet mds1 \
12872                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12873                awk '/ldlm_cancel/ {print $2}')
12874         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12875                awk '/ldlm_bl_callback/ {print $2}')
12876         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12877         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12878         lru_resize_enable mdc
12879         lru_resize_enable osc
12880 }
12881 run_test 120a "Early Lock Cancel: mkdir test"
12882
12883 test_120b() {
12884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12885         remote_mds_nodsh && skip "remote MDS with nodsh"
12886         test_mkdir $DIR/$tdir
12887         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12888                 skip_env "no early lock cancel on server"
12889
12890         lru_resize_disable mdc
12891         lru_resize_disable osc
12892         cancel_lru_locks mdc
12893         stat $DIR/$tdir > /dev/null
12894         can1=$(do_facet $SINGLEMDS \
12895                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12896                awk '/ldlm_cancel/ {print $2}')
12897         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12898                awk '/ldlm_bl_callback/ {print $2}')
12899         touch $DIR/$tdir/f1
12900         can2=$(do_facet $SINGLEMDS \
12901                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12902                awk '/ldlm_cancel/ {print $2}')
12903         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12904                awk '/ldlm_bl_callback/ {print $2}')
12905         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12906         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12907         lru_resize_enable mdc
12908         lru_resize_enable osc
12909 }
12910 run_test 120b "Early Lock Cancel: create test"
12911
12912 test_120c() {
12913         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12914         remote_mds_nodsh && skip "remote MDS with nodsh"
12915         test_mkdir -i0 -c1 $DIR/$tdir
12916         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12917                 skip "no early lock cancel on server"
12918
12919         lru_resize_disable mdc
12920         lru_resize_disable osc
12921         test_mkdir -i0 -c1 $DIR/$tdir/d1
12922         test_mkdir -i0 -c1 $DIR/$tdir/d2
12923         touch $DIR/$tdir/d1/f1
12924         cancel_lru_locks mdc
12925         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
12926         can1=$(do_facet mds1 \
12927                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12928                awk '/ldlm_cancel/ {print $2}')
12929         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12930                awk '/ldlm_bl_callback/ {print $2}')
12931         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
12932         can2=$(do_facet mds1 \
12933                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12934                awk '/ldlm_cancel/ {print $2}')
12935         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12936                awk '/ldlm_bl_callback/ {print $2}')
12937         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12938         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12939         lru_resize_enable mdc
12940         lru_resize_enable osc
12941 }
12942 run_test 120c "Early Lock Cancel: link test"
12943
12944 test_120d() {
12945         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12946         remote_mds_nodsh && skip "remote MDS with nodsh"
12947         test_mkdir -i0 -c1 $DIR/$tdir
12948         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12949                 skip_env "no early lock cancel on server"
12950
12951         lru_resize_disable mdc
12952         lru_resize_disable osc
12953         touch $DIR/$tdir
12954         cancel_lru_locks mdc
12955         stat $DIR/$tdir > /dev/null
12956         can1=$(do_facet mds1 \
12957                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12958                awk '/ldlm_cancel/ {print $2}')
12959         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12960                awk '/ldlm_bl_callback/ {print $2}')
12961         chmod a+x $DIR/$tdir
12962         can2=$(do_facet mds1 \
12963                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12964                awk '/ldlm_cancel/ {print $2}')
12965         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12966                awk '/ldlm_bl_callback/ {print $2}')
12967         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12968         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12969         lru_resize_enable mdc
12970         lru_resize_enable osc
12971 }
12972 run_test 120d "Early Lock Cancel: setattr test"
12973
12974 test_120e() {
12975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12976         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12977                 skip_env "no early lock cancel on server"
12978         remote_mds_nodsh && skip "remote MDS with nodsh"
12979
12980         local dlmtrace_set=false
12981
12982         test_mkdir -i0 -c1 $DIR/$tdir
12983         lru_resize_disable mdc
12984         lru_resize_disable osc
12985         ! $LCTL get_param debug | grep -q dlmtrace &&
12986                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
12987         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
12988         cancel_lru_locks mdc
12989         cancel_lru_locks osc
12990         dd if=$DIR/$tdir/f1 of=/dev/null
12991         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
12992         # XXX client can not do early lock cancel of OST lock
12993         # during unlink (LU-4206), so cancel osc lock now.
12994         sleep 2
12995         cancel_lru_locks osc
12996         can1=$(do_facet mds1 \
12997                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12998                awk '/ldlm_cancel/ {print $2}')
12999         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13000                awk '/ldlm_bl_callback/ {print $2}')
13001         unlink $DIR/$tdir/f1
13002         sleep 5
13003         can2=$(do_facet mds1 \
13004                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13005                awk '/ldlm_cancel/ {print $2}')
13006         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13007                awk '/ldlm_bl_callback/ {print $2}')
13008         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
13009                 $LCTL dk $TMP/cancel.debug.txt
13010         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
13011                 $LCTL dk $TMP/blocking.debug.txt
13012         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
13013         lru_resize_enable mdc
13014         lru_resize_enable osc
13015 }
13016 run_test 120e "Early Lock Cancel: unlink test"
13017
13018 test_120f() {
13019         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13020         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13021                 skip_env "no early lock cancel on server"
13022         remote_mds_nodsh && skip "remote MDS with nodsh"
13023
13024         test_mkdir -i0 -c1 $DIR/$tdir
13025         lru_resize_disable mdc
13026         lru_resize_disable osc
13027         test_mkdir -i0 -c1 $DIR/$tdir/d1
13028         test_mkdir -i0 -c1 $DIR/$tdir/d2
13029         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
13030         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
13031         cancel_lru_locks mdc
13032         cancel_lru_locks osc
13033         dd if=$DIR/$tdir/d1/f1 of=/dev/null
13034         dd if=$DIR/$tdir/d2/f2 of=/dev/null
13035         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
13036         # XXX client can not do early lock cancel of OST lock
13037         # during rename (LU-4206), so cancel osc lock now.
13038         sleep 2
13039         cancel_lru_locks osc
13040         can1=$(do_facet mds1 \
13041                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13042                awk '/ldlm_cancel/ {print $2}')
13043         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13044                awk '/ldlm_bl_callback/ {print $2}')
13045         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
13046         sleep 5
13047         can2=$(do_facet mds1 \
13048                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13049                awk '/ldlm_cancel/ {print $2}')
13050         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13051                awk '/ldlm_bl_callback/ {print $2}')
13052         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13053         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13054         lru_resize_enable mdc
13055         lru_resize_enable osc
13056 }
13057 run_test 120f "Early Lock Cancel: rename test"
13058
13059 test_120g() {
13060         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13061         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13062                 skip_env "no early lock cancel on server"
13063         remote_mds_nodsh && skip "remote MDS with nodsh"
13064
13065         lru_resize_disable mdc
13066         lru_resize_disable osc
13067         count=10000
13068         echo create $count files
13069         test_mkdir $DIR/$tdir
13070         cancel_lru_locks mdc
13071         cancel_lru_locks osc
13072         t0=$(date +%s)
13073
13074         can0=$(do_facet $SINGLEMDS \
13075                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13076                awk '/ldlm_cancel/ {print $2}')
13077         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13078                awk '/ldlm_bl_callback/ {print $2}')
13079         createmany -o $DIR/$tdir/f $count
13080         sync
13081         can1=$(do_facet $SINGLEMDS \
13082                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13083                awk '/ldlm_cancel/ {print $2}')
13084         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13085                awk '/ldlm_bl_callback/ {print $2}')
13086         t1=$(date +%s)
13087         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
13088         echo rm $count files
13089         rm -r $DIR/$tdir
13090         sync
13091         can2=$(do_facet $SINGLEMDS \
13092                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13093                awk '/ldlm_cancel/ {print $2}')
13094         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13095                awk '/ldlm_bl_callback/ {print $2}')
13096         t2=$(date +%s)
13097         echo total: $count removes in $((t2-t1))
13098         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
13099         sleep 2
13100         # wait for commitment of removal
13101         lru_resize_enable mdc
13102         lru_resize_enable osc
13103 }
13104 run_test 120g "Early Lock Cancel: performance test"
13105
13106 test_121() { #bug #10589
13107         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13108
13109         rm -rf $DIR/$tfile
13110         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
13111 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
13112         lctl set_param fail_loc=0x310
13113         cancel_lru_locks osc > /dev/null
13114         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
13115         lctl set_param fail_loc=0
13116         [[ $reads -eq $writes ]] ||
13117                 error "read $reads blocks, must be $writes blocks"
13118 }
13119 run_test 121 "read cancel race ========="
13120
13121 test_123a_base() { # was test 123, statahead(bug 11401)
13122         local lsx="$1"
13123
13124         SLOWOK=0
13125         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
13126                 log "testing UP system. Performance may be lower than expected."
13127                 SLOWOK=1
13128         fi
13129         running_in_vm && SLOWOK=1
13130
13131         rm -rf $DIR/$tdir
13132         test_mkdir $DIR/$tdir
13133         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
13134         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
13135         MULT=10
13136         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
13137                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
13138
13139                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
13140                 lctl set_param -n llite.*.statahead_max 0
13141                 lctl get_param llite.*.statahead_max
13142                 cancel_lru_locks mdc
13143                 cancel_lru_locks osc
13144                 stime=$(date +%s)
13145                 time $lsx $DIR/$tdir | wc -l
13146                 etime=$(date +%s)
13147                 delta=$((etime - stime))
13148                 log "$lsx $i files without statahead: $delta sec"
13149                 lctl set_param llite.*.statahead_max=$max
13150
13151                 swrong=$(lctl get_param -n llite.*.statahead_stats |
13152                          awk '/statahead.wrong:/ { print $NF }')
13153                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
13154                 cancel_lru_locks mdc
13155                 cancel_lru_locks osc
13156                 stime=$(date +%s)
13157                 time $lsx $DIR/$tdir | wc -l
13158                 etime=$(date +%s)
13159                 delta_sa=$((etime - stime))
13160                 log "$lsx $i files with statahead: $delta_sa sec"
13161                 lctl get_param -n llite.*.statahead_stats
13162                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
13163                          awk '/statahead.wrong:/ { print $NF }')
13164
13165                 [[ $swrong -lt $ewrong ]] &&
13166                         log "statahead was stopped, maybe too many locks held!"
13167                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
13168
13169                 if (( $delta_sa*100 > $delta*105 && $delta_sa > $delta+2)); then
13170                         max=$(lctl get_param -n llite.*.statahead_max |
13171                                 head -n 1)
13172                         lctl set_param -n llite.*.statahead_max 0
13173                         lctl get_param llite.*.statahead_max
13174                         cancel_lru_locks mdc
13175                         cancel_lru_locks osc
13176                         stime=$(date +%s)
13177                         time $lsx $DIR/$tdir | wc -l
13178                         etime=$(date +%s)
13179                         delta=$((etime - stime))
13180                         log "$lsx $i files again without statahead: $delta sec"
13181                         lctl set_param llite.*.statahead_max=$max
13182                         if (( $delta_sa*100 > delta*105 && delta_sa > delta+2 )); then
13183                                 if [ $SLOWOK -eq 0 ]; then
13184                                         error "$lsx $i files is slower with statahead!"
13185                                 else
13186                                         log "$lsx $i files is slower with statahead!"
13187                                 fi
13188                                 break
13189                         fi
13190                 fi
13191
13192                 [ $delta -gt 20 ] && break
13193                 [ $delta -gt 8 ] && MULT=$((50 / delta))
13194                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
13195         done
13196         log "$lsx done"
13197
13198         stime=$(date +%s)
13199         rm -r $DIR/$tdir
13200         sync
13201         etime=$(date +%s)
13202         delta=$((etime - stime))
13203         log "rm -r $DIR/$tdir/: $delta seconds"
13204         log "rm done"
13205         lctl get_param -n llite.*.statahead_stats
13206 }
13207
13208 test_123aa() {
13209         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13210
13211         test_123a_base "ls -l"
13212 }
13213 run_test 123aa "verify statahead work"
13214
13215 test_123ab() {
13216         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13217
13218         statx_supported || skip_env "Test must be statx() syscall supported"
13219
13220         test_123a_base "$STATX -l"
13221 }
13222 run_test 123ab "verify statahead work by using statx"
13223
13224 test_123ac() {
13225         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13226
13227         statx_supported || skip_env "Test must be statx() syscall supported"
13228
13229         local rpcs_before
13230         local rpcs_after
13231         local agl_before
13232         local agl_after
13233
13234         cancel_lru_locks $OSC
13235         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
13236         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
13237                      awk '/agl.total:/ { print $NF }')
13238         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
13239         test_123a_base "$STATX --cached=always -D"
13240         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
13241                     awk '/agl.total:/ { print $NF }')
13242         [ $agl_before -eq $agl_after ] ||
13243                 error "Should not trigger AGL thread - $agl_before:$agl_after"
13244         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
13245         [ $rpcs_after -eq $rpcs_before ] ||
13246                 error "$STATX should not send glimpse RPCs to $OSC"
13247 }
13248 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
13249
13250 test_123b () { # statahead(bug 15027)
13251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13252
13253         test_mkdir $DIR/$tdir
13254         createmany -o $DIR/$tdir/$tfile-%d 1000
13255
13256         cancel_lru_locks mdc
13257         cancel_lru_locks osc
13258
13259 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
13260         lctl set_param fail_loc=0x80000803
13261         ls -lR $DIR/$tdir > /dev/null
13262         log "ls done"
13263         lctl set_param fail_loc=0x0
13264         lctl get_param -n llite.*.statahead_stats
13265         rm -r $DIR/$tdir
13266         sync
13267
13268 }
13269 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
13270
13271 test_123c() {
13272         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
13273
13274         test_mkdir -i 0 -c 1 $DIR/$tdir.0
13275         test_mkdir -i 1 -c 1 $DIR/$tdir.1
13276         touch $DIR/$tdir.1/{1..3}
13277         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
13278
13279         remount_client $MOUNT
13280
13281         $MULTIOP $DIR/$tdir.0 Q
13282
13283         # let statahead to complete
13284         ls -l $DIR/$tdir.0 > /dev/null
13285
13286         testid=$(echo $TESTNAME | tr '_' ' ')
13287         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
13288                 error "statahead warning" || true
13289 }
13290 run_test 123c "Can not initialize inode warning on DNE statahead"
13291
13292 test_123d() {
13293         local num=100
13294         local swrong
13295         local ewrong
13296
13297         test_mkdir -c -1 $DIR/$tdir || error "test_mkdir $DIR/$tdir failed"
13298         $LFS setdirstripe -D -c $MDSCOUNT $DIR/$tdir ||
13299                 error "setdirstripe $DIR/$tdir failed"
13300         createmany -d $DIR/$tdir/$tfile $num || error "createmany $num failed"
13301         remount_client $MOUNT
13302         $LCTL get_param llite.*.statahead_max
13303         $LCTL set_param llite.*.statahead_stats=0 ||
13304                 error "clear statahead_stats failed"
13305         swrong=$(lctl get_param -n llite.*.statahead_stats |
13306                  awk '/statahead.wrong:/ { print $NF }')
13307         ls -l $DIR/$tdir || error "ls -l $DIR/$tdir failed"
13308         # wait for statahead thread finished to update hit/miss stats.
13309         sleep 1
13310         $LCTL get_param -n llite.*.statahead_stats
13311         ewrong=$(lctl get_param -n llite.*.statahead_stats |
13312                  awk '/statahead.wrong:/ { print $NF }')
13313         (( $swrong == $ewrong )) ||
13314                 log "statahead was stopped, maybe too many locks held!"
13315 }
13316 run_test 123d "Statahead on striped directories works correctly"
13317
13318 test_124a() {
13319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13320         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13321                 skip_env "no lru resize on server"
13322
13323         local NR=2000
13324
13325         test_mkdir $DIR/$tdir
13326
13327         log "create $NR files at $DIR/$tdir"
13328         createmany -o $DIR/$tdir/f $NR ||
13329                 error "failed to create $NR files in $DIR/$tdir"
13330
13331         cancel_lru_locks mdc
13332         ls -l $DIR/$tdir > /dev/null
13333
13334         local NSDIR=""
13335         local LRU_SIZE=0
13336         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
13337                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
13338                 LRU_SIZE=$($LCTL get_param -n $PARAM)
13339                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
13340                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
13341                         log "NSDIR=$NSDIR"
13342                         log "NS=$(basename $NSDIR)"
13343                         break
13344                 fi
13345         done
13346
13347         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
13348                 skip "Not enough cached locks created!"
13349         fi
13350         log "LRU=$LRU_SIZE"
13351
13352         local SLEEP=30
13353
13354         # We know that lru resize allows one client to hold $LIMIT locks
13355         # for 10h. After that locks begin to be killed by client.
13356         local MAX_HRS=10
13357         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
13358         log "LIMIT=$LIMIT"
13359         if [ $LIMIT -lt $LRU_SIZE ]; then
13360                 skip "Limit is too small $LIMIT"
13361         fi
13362
13363         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
13364         # killing locks. Some time was spent for creating locks. This means
13365         # that up to the moment of sleep finish we must have killed some of
13366         # them (10-100 locks). This depends on how fast ther were created.
13367         # Many of them were touched in almost the same moment and thus will
13368         # be killed in groups.
13369         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE * 100))
13370
13371         # Use $LRU_SIZE_B here to take into account real number of locks
13372         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
13373         local LRU_SIZE_B=$LRU_SIZE
13374         log "LVF=$LVF"
13375         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
13376         log "OLD_LVF=$OLD_LVF"
13377         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
13378
13379         # Let's make sure that we really have some margin. Client checks
13380         # cached locks every 10 sec.
13381         SLEEP=$((SLEEP+20))
13382         log "Sleep ${SLEEP} sec"
13383         local SEC=0
13384         while ((SEC<$SLEEP)); do
13385                 echo -n "..."
13386                 sleep 5
13387                 SEC=$((SEC+5))
13388                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
13389                 echo -n "$LRU_SIZE"
13390         done
13391         echo ""
13392         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
13393         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
13394
13395         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
13396                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
13397                 unlinkmany $DIR/$tdir/f $NR
13398                 return
13399         }
13400
13401         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
13402         log "unlink $NR files at $DIR/$tdir"
13403         unlinkmany $DIR/$tdir/f $NR
13404 }
13405 run_test 124a "lru resize ======================================="
13406
13407 get_max_pool_limit()
13408 {
13409         local limit=$($LCTL get_param \
13410                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
13411         local max=0
13412         for l in $limit; do
13413                 if [[ $l -gt $max ]]; then
13414                         max=$l
13415                 fi
13416         done
13417         echo $max
13418 }
13419
13420 test_124b() {
13421         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13422         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13423                 skip_env "no lru resize on server"
13424
13425         LIMIT=$(get_max_pool_limit)
13426
13427         NR=$(($(default_lru_size)*20))
13428         if [[ $NR -gt $LIMIT ]]; then
13429                 log "Limit lock number by $LIMIT locks"
13430                 NR=$LIMIT
13431         fi
13432
13433         IFree=$(mdsrate_inodes_available)
13434         if [ $IFree -lt $NR ]; then
13435                 log "Limit lock number by $IFree inodes"
13436                 NR=$IFree
13437         fi
13438
13439         lru_resize_disable mdc
13440         test_mkdir -p $DIR/$tdir/disable_lru_resize
13441
13442         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
13443         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
13444         cancel_lru_locks mdc
13445         stime=`date +%s`
13446         PID=""
13447         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13448         PID="$PID $!"
13449         sleep 2
13450         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13451         PID="$PID $!"
13452         sleep 2
13453         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13454         PID="$PID $!"
13455         wait $PID
13456         etime=`date +%s`
13457         nolruresize_delta=$((etime-stime))
13458         log "ls -la time: $nolruresize_delta seconds"
13459         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
13460         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
13461
13462         lru_resize_enable mdc
13463         test_mkdir -p $DIR/$tdir/enable_lru_resize
13464
13465         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
13466         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
13467         cancel_lru_locks mdc
13468         stime=`date +%s`
13469         PID=""
13470         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13471         PID="$PID $!"
13472         sleep 2
13473         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13474         PID="$PID $!"
13475         sleep 2
13476         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13477         PID="$PID $!"
13478         wait $PID
13479         etime=`date +%s`
13480         lruresize_delta=$((etime-stime))
13481         log "ls -la time: $lruresize_delta seconds"
13482         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
13483
13484         if [ $lruresize_delta -gt $nolruresize_delta ]; then
13485                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
13486         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
13487                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
13488         else
13489                 log "lru resize performs the same with no lru resize"
13490         fi
13491         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
13492 }
13493 run_test 124b "lru resize (performance test) ======================="
13494
13495 test_124c() {
13496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13497         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13498                 skip_env "no lru resize on server"
13499
13500         # cache ununsed locks on client
13501         local nr=100
13502         cancel_lru_locks mdc
13503         test_mkdir $DIR/$tdir
13504         createmany -o $DIR/$tdir/f $nr ||
13505                 error "failed to create $nr files in $DIR/$tdir"
13506         ls -l $DIR/$tdir > /dev/null
13507
13508         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13509         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13510         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13511         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13512         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13513
13514         # set lru_max_age to 1 sec
13515         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13516         echo "sleep $((recalc_p * 2)) seconds..."
13517         sleep $((recalc_p * 2))
13518
13519         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13520         # restore lru_max_age
13521         $LCTL set_param -n $nsdir.lru_max_age $max_age
13522         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13523         unlinkmany $DIR/$tdir/f $nr
13524 }
13525 run_test 124c "LRUR cancel very aged locks"
13526
13527 test_124d() {
13528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13529         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13530                 skip_env "no lru resize on server"
13531
13532         # cache ununsed locks on client
13533         local nr=100
13534
13535         lru_resize_disable mdc
13536         stack_trap "lru_resize_enable mdc" EXIT
13537
13538         cancel_lru_locks mdc
13539
13540         # asynchronous object destroy at MDT could cause bl ast to client
13541         test_mkdir $DIR/$tdir
13542         createmany -o $DIR/$tdir/f $nr ||
13543                 error "failed to create $nr files in $DIR/$tdir"
13544         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
13545
13546         ls -l $DIR/$tdir > /dev/null
13547
13548         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13549         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13550         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13551         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13552
13553         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13554
13555         # set lru_max_age to 1 sec
13556         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13557         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
13558
13559         echo "sleep $((recalc_p * 2)) seconds..."
13560         sleep $((recalc_p * 2))
13561
13562         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13563
13564         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13565 }
13566 run_test 124d "cancel very aged locks if lru-resize diasbaled"
13567
13568 test_125() { # 13358
13569         $LCTL get_param -n llite.*.client_type | grep -q local ||
13570                 skip "must run as local client"
13571         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
13572                 skip_env "must have acl enabled"
13573         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13574
13575         test_mkdir $DIR/$tdir
13576         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
13577         setfacl -R -m u:$USER0:rwx $DIR/$tdir ||
13578                 error "setfacl $DIR/$tdir failed"
13579         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
13580 }
13581 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
13582
13583 test_126() { # bug 12829/13455
13584         $GSS && skip_env "must run as gss disabled"
13585         $LCTL get_param -n llite.*.client_type | grep -q local ||
13586                 skip "must run as local client"
13587         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
13588
13589         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
13590         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
13591         rm -f $DIR/$tfile
13592         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
13593 }
13594 run_test 126 "check that the fsgid provided by the client is taken into account"
13595
13596 test_127a() { # bug 15521
13597         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13598         local name count samp unit min max sum sumsq
13599
13600         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
13601         echo "stats before reset"
13602         $LCTL get_param osc.*.stats
13603         $LCTL set_param osc.*.stats=0
13604         local fsize=$((2048 * 1024))
13605
13606         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
13607         cancel_lru_locks osc
13608         dd if=$DIR/$tfile of=/dev/null bs=$fsize
13609
13610         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
13611         stack_trap "rm -f $TMP/$tfile.tmp"
13612         while read name count samp unit min max sum sumsq; do
13613                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13614                 [ ! $min ] && error "Missing min value for $name proc entry"
13615                 eval $name=$count || error "Wrong proc format"
13616
13617                 case $name in
13618                 read_bytes|write_bytes)
13619                         [[ "$unit" =~ "bytes" ]] ||
13620                                 error "unit is not 'bytes': $unit"
13621                         (( $min >= 4096 )) || error "min is too small: $min"
13622                         (( $min <= $fsize )) || error "min is too big: $min"
13623                         (( $max >= 4096 )) || error "max is too small: $max"
13624                         (( $max <= $fsize )) || error "max is too big: $max"
13625                         (( $sum == $fsize )) || error "sum is wrong: $sum"
13626                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
13627                                 error "sumsquare is too small: $sumsq"
13628                         (( $sumsq <= $fsize * $fsize )) ||
13629                                 error "sumsquare is too big: $sumsq"
13630                         ;;
13631                 ost_read|ost_write)
13632                         [[ "$unit" =~ "usec" ]] ||
13633                                 error "unit is not 'usec': $unit"
13634                         ;;
13635                 *)      ;;
13636                 esac
13637         done < $DIR/$tfile.tmp
13638
13639         #check that we actually got some stats
13640         [ "$read_bytes" ] || error "Missing read_bytes stats"
13641         [ "$write_bytes" ] || error "Missing write_bytes stats"
13642         [ "$read_bytes" != 0 ] || error "no read done"
13643         [ "$write_bytes" != 0 ] || error "no write done"
13644 }
13645 run_test 127a "verify the client stats are sane"
13646
13647 test_127b() { # bug LU-333
13648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13649         local name count samp unit min max sum sumsq
13650
13651         echo "stats before reset"
13652         $LCTL get_param llite.*.stats
13653         $LCTL set_param llite.*.stats=0
13654
13655         # perform 2 reads and writes so MAX is different from SUM.
13656         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13657         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13658         cancel_lru_locks osc
13659         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13660         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13661
13662         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
13663         stack_trap "rm -f $TMP/$tfile.tmp"
13664         while read name count samp unit min max sum sumsq; do
13665                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13666                 eval $name=$count || error "Wrong proc format"
13667
13668                 case $name in
13669                 read_bytes|write_bytes)
13670                         [[ "$unit" =~ "bytes" ]] ||
13671                                 error "unit is not 'bytes': $unit"
13672                         (( $count == 2 )) || error "count is not 2: $count"
13673                         (( $min == $PAGE_SIZE )) ||
13674                                 error "min is not $PAGE_SIZE: $min"
13675                         (( $max == $PAGE_SIZE )) ||
13676                                 error "max is not $PAGE_SIZE: $max"
13677                         (( $sum == $PAGE_SIZE * 2 )) ||
13678                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
13679                         ;;
13680                 read|write)
13681                         [[ "$unit" =~ "usec" ]] ||
13682                                 error "unit is not 'usec': $unit"
13683                         ;;
13684                 *)      ;;
13685                 esac
13686         done < $TMP/$tfile.tmp
13687
13688         #check that we actually got some stats
13689         [ "$read_bytes" ] || error "Missing read_bytes stats"
13690         [ "$write_bytes" ] || error "Missing write_bytes stats"
13691         [ "$read_bytes" != 0 ] || error "no read done"
13692         [ "$write_bytes" != 0 ] || error "no write done"
13693 }
13694 run_test 127b "verify the llite client stats are sane"
13695
13696 test_127c() { # LU-12394
13697         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13698         local size
13699         local bsize
13700         local reads
13701         local writes
13702         local count
13703
13704         $LCTL set_param llite.*.extents_stats=1
13705         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
13706
13707         # Use two stripes so there is enough space in default config
13708         $LFS setstripe -c 2 $DIR/$tfile
13709
13710         # Extent stats start at 0-4K and go in power of two buckets
13711         # LL_HIST_START = 12 --> 2^12 = 4K
13712         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
13713         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
13714         # small configs
13715         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
13716                 do
13717                 # Write and read, 2x each, second time at a non-zero offset
13718                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
13719                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
13720                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
13721                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
13722                 rm -f $DIR/$tfile
13723         done
13724
13725         $LCTL get_param llite.*.extents_stats
13726
13727         count=2
13728         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
13729                 do
13730                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
13731                                 grep -m 1 $bsize)
13732                 reads=$(echo $bucket | awk '{print $5}')
13733                 writes=$(echo $bucket | awk '{print $9}')
13734                 [ "$reads" -eq $count ] ||
13735                         error "$reads reads in < $bsize bucket, expect $count"
13736                 [ "$writes" -eq $count ] ||
13737                         error "$writes writes in < $bsize bucket, expect $count"
13738         done
13739
13740         # Test mmap write and read
13741         $LCTL set_param llite.*.extents_stats=c
13742         size=512
13743         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
13744         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
13745         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
13746
13747         $LCTL get_param llite.*.extents_stats
13748
13749         count=$(((size*1024) / PAGE_SIZE))
13750
13751         bsize=$((2 * PAGE_SIZE / 1024))K
13752
13753         bucket=$($LCTL get_param -n llite.*.extents_stats |
13754                         grep -m 1 $bsize)
13755         reads=$(echo $bucket | awk '{print $5}')
13756         writes=$(echo $bucket | awk '{print $9}')
13757         # mmap writes fault in the page first, creating an additonal read
13758         [ "$reads" -eq $((2 * count)) ] ||
13759                 error "$reads reads in < $bsize bucket, expect $count"
13760         [ "$writes" -eq $count ] ||
13761                 error "$writes writes in < $bsize bucket, expect $count"
13762 }
13763 run_test 127c "test llite extent stats with regular & mmap i/o"
13764
13765 test_128() { # bug 15212
13766         touch $DIR/$tfile
13767         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
13768                 find $DIR/$tfile
13769                 find $DIR/$tfile
13770         EOF
13771
13772         result=$(grep error $TMP/$tfile.log)
13773         rm -f $DIR/$tfile $TMP/$tfile.log
13774         [ -z "$result" ] ||
13775                 error "consecutive find's under interactive lfs failed"
13776 }
13777 run_test 128 "interactive lfs for 2 consecutive find's"
13778
13779 set_dir_limits () {
13780         local mntdev
13781         local canondev
13782         local node
13783
13784         local ldproc=/proc/fs/ldiskfs
13785         local facets=$(get_facets MDS)
13786
13787         for facet in ${facets//,/ }; do
13788                 canondev=$(ldiskfs_canon \
13789                            *.$(convert_facet2label $facet).mntdev $facet)
13790                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
13791                         ldproc=/sys/fs/ldiskfs
13792                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
13793                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
13794         done
13795 }
13796
13797 check_mds_dmesg() {
13798         local facets=$(get_facets MDS)
13799         for facet in ${facets//,/ }; do
13800                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
13801         done
13802         return 1
13803 }
13804
13805 test_129() {
13806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13807         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
13808                 skip "Need MDS version with at least 2.5.56"
13809         if [ "$mds1_FSTYPE" != ldiskfs ]; then
13810                 skip_env "ldiskfs only test"
13811         fi
13812         remote_mds_nodsh && skip "remote MDS with nodsh"
13813
13814         local ENOSPC=28
13815         local has_warning=false
13816
13817         rm -rf $DIR/$tdir
13818         mkdir -p $DIR/$tdir
13819
13820         # block size of mds1
13821         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
13822         set_dir_limits $maxsize $((maxsize * 6 / 8))
13823         stack_trap "set_dir_limits 0 0"
13824         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
13825         local dirsize=$(stat -c%s "$DIR/$tdir")
13826         local nfiles=0
13827         while (( $dirsize <= $maxsize )); do
13828                 $MCREATE $DIR/$tdir/file_base_$nfiles
13829                 rc=$?
13830                 # check two errors:
13831                 # ENOSPC for ext4 max_dir_size, which has been used since
13832                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
13833                 if (( rc == ENOSPC )); then
13834                         set_dir_limits 0 0
13835                         echo "rc=$rc returned as expected after $nfiles files"
13836
13837                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
13838                                 error "create failed w/o dir size limit"
13839
13840                         # messages may be rate limited if test is run repeatedly
13841                         check_mds_dmesg '"is approaching max"' ||
13842                                 echo "warning message should be output"
13843                         check_mds_dmesg '"has reached max"' ||
13844                                 echo "reached message should be output"
13845
13846                         dirsize=$(stat -c%s "$DIR/$tdir")
13847
13848                         [[ $dirsize -ge $maxsize ]] && return 0
13849                         error "dirsize $dirsize < $maxsize after $nfiles files"
13850                 elif (( rc != 0 )); then
13851                         break
13852                 fi
13853                 nfiles=$((nfiles + 1))
13854                 dirsize=$(stat -c%s "$DIR/$tdir")
13855         done
13856
13857         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
13858 }
13859 run_test 129 "test directory size limit ========================"
13860
13861 OLDIFS="$IFS"
13862 cleanup_130() {
13863         trap 0
13864         IFS="$OLDIFS"
13865 }
13866
13867 test_130a() {
13868         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13869         [[ -z "$filefrag_op" ]] || skip_env "filefrag does not support FIEMAP"
13870
13871         trap cleanup_130 EXIT RETURN
13872
13873         local fm_file=$DIR/$tfile
13874         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
13875         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
13876                 error "dd failed for $fm_file"
13877
13878         # LU-1795: test filefrag/FIEMAP once, even if unsupported on ZFS
13879         filefrag -ves $fm_file
13880         local rc=$?
13881         [[ "$ost1_FSTYPE" != "zfs" ]] ||
13882                 skip "LU-1941: FIEMAP unimplemented on ZFS"
13883         (( $rc == 0 )) || error "filefrag $fm_file failed"
13884
13885         filefrag_op=$(filefrag -ve -k $fm_file |
13886                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13887         local lun=$($LFS getstripe -i $fm_file)
13888
13889         local start_blk=$(echo $filefrag_op | cut -d: -f2 | cut -d. -f1)
13890         IFS=$'\n'
13891         local tot_len=0
13892         for line in $filefrag_op; do
13893                 local frag_lun=$(echo $line | cut -d: -f5)
13894                 local ext_len=$(echo $line | cut -d: -f4)
13895
13896                 if (( $frag_lun != $lun )); then
13897                         error "FIEMAP on 1-stripe file($fm_file) failed"
13898                         return
13899                 fi
13900                 (( tot_len += ext_len ))
13901         done
13902
13903         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
13904                 error "FIEMAP on 1-stripe file($fm_file) failed"
13905                 return
13906         fi
13907
13908         echo "FIEMAP on single striped file succeeded"
13909 }
13910 run_test 130a "FIEMAP (1-stripe file)"
13911
13912 test_130b() {
13913         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
13914
13915         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
13916         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
13917         [[ "$ost1_FSTYPE" != "zfs" ]] ||
13918                 skip "LU-1941: FIEMAP unimplemented on ZFS"
13919
13920         trap cleanup_130 EXIT RETURN
13921
13922         local fm_file=$DIR/$tfile
13923         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
13924                 error "setstripe on $fm_file"
13925
13926         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
13927                 error "dd failed on $fm_file"
13928
13929         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13930         filefrag_op=$(filefrag -ve -k $fm_file |
13931                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13932
13933         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
13934                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13935
13936         IFS=$'\n'
13937         local tot_len=0
13938         local num_luns=1
13939
13940         for line in $filefrag_op; do
13941                 local frag_lun=$(echo $line | cut -d: -f5 |
13942                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13943                 local ext_len=$(echo $line | cut -d: -f4)
13944                 if (( $frag_lun != $last_lun )); then
13945                         if (( tot_len != 1024 )); then
13946                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
13947                                 return
13948                         else
13949                                 (( num_luns += 1 ))
13950                                 tot_len=0
13951                         fi
13952                 fi
13953                 (( tot_len += ext_len ))
13954                 last_lun=$frag_lun
13955         done
13956         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
13957                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
13958                 return
13959         fi
13960
13961         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
13962 }
13963 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
13964
13965 test_130c() {
13966         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
13967
13968         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
13969         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
13970         [[ "$ost1_FSTYPE" != "zfs" ]] ||
13971                 skip "LU-1941: FIEMAP unimplemented on ZFS"
13972
13973         trap cleanup_130 EXIT RETURN
13974
13975         local fm_file=$DIR/$tfile
13976         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
13977
13978         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
13979                 error "dd failed on $fm_file"
13980
13981         filefrag -ves $fm_file || error "filefrag $fm_file failed"
13982         filefrag_op=$(filefrag -ve -k $fm_file |
13983                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13984
13985         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
13986                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13987
13988         IFS=$'\n'
13989         local tot_len=0
13990         local num_luns=1
13991         for line in $filefrag_op; do
13992                 local frag_lun=$(echo $line | cut -d: -f5 |
13993                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
13994                 local ext_len=$(echo $line | cut -d: -f4)
13995                 if (( $frag_lun != $last_lun )); then
13996                         local logical=$(echo $line | cut -d: -f2 | cut -d. -f1)
13997                         if (( logical != 512 )); then
13998                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
13999                                 return
14000                         fi
14001                         if (( tot_len != 512 )); then
14002                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14003                                 return
14004                         else
14005                                 (( num_luns += 1 ))
14006                                 tot_len=0
14007                         fi
14008                 fi
14009                 (( tot_len += ext_len ))
14010                 last_lun=$frag_lun
14011         done
14012         if (( num_luns != 2 || tot_len != 512 )); then
14013                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14014                 return
14015         fi
14016
14017         echo "FIEMAP on 2-stripe file with hole succeeded"
14018 }
14019 run_test 130c "FIEMAP (2-stripe file with hole)"
14020
14021 test_130d() {
14022         (( $OSTCOUNT >= 3 )) || skip "needs >= 3 OSTs"
14023
14024         filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14025         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14026         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14027                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14028
14029         trap cleanup_130 EXIT RETURN
14030
14031         local fm_file=$DIR/$tfile
14032         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
14033                         error "setstripe on $fm_file"
14034
14035         local actual_stripe_count=$($LFS getstripe -c $fm_file)
14036         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
14037                 error "dd failed on $fm_file"
14038
14039         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14040         filefrag_op=$(filefrag -ve -k $fm_file |
14041                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14042
14043         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14044                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14045
14046         IFS=$'\n'
14047         local tot_len=0
14048         local num_luns=1
14049         for line in $filefrag_op; do
14050                 local frag_lun=$(echo $line | cut -d: -f5 |
14051                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14052                 local ext_len=$(echo $line | cut -d: -f4)
14053                 if (( $frag_lun != $last_lun )); then
14054                         if (( tot_len != 1024 )); then
14055                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14056                                 return
14057                         else
14058                                 (( num_luns += 1 ))
14059                                 local tot_len=0
14060                         fi
14061                 fi
14062                 (( tot_len += ext_len ))
14063                 last_lun=$frag_lun
14064         done
14065         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
14066                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14067                 return
14068         fi
14069
14070         echo "FIEMAP on N-stripe file succeeded"
14071 }
14072 run_test 130d "FIEMAP (N-stripe file)"
14073
14074 test_130e() {
14075         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14076
14077         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14078         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14079         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14080                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14081
14082         trap cleanup_130 EXIT RETURN
14083
14084         local fm_file=$DIR/$tfile
14085         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
14086
14087         local num_blks=512
14088         local expected_len=$(( (num_blks / 2) * 64 ))
14089         for ((i = 0; i < $num_blks; i++)); do
14090                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) \
14091                         conv=notrunc > /dev/null 2>&1
14092         done
14093
14094         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14095         filefrag_op=$(filefrag -ve -k $fm_file |
14096                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14097
14098         local last_lun=$(echo $filefrag_op | cut -d: -f5)
14099
14100         IFS=$'\n'
14101         local tot_len=0
14102         local num_luns=1
14103         for line in $filefrag_op; do
14104                 local frag_lun=$(echo $line | cut -d: -f5)
14105                 local ext_len=$(echo $line | cut -d: -f4)
14106                 if (( $frag_lun != $last_lun )); then
14107                         if (( tot_len != $expected_len )); then
14108                                 error "OST$last_lun $tot_len != $expected_len"
14109                         else
14110                                 (( num_luns += 1 ))
14111                                 tot_len=0
14112                         fi
14113                 fi
14114                 (( tot_len += ext_len ))
14115                 last_lun=$frag_lun
14116         done
14117         if (( num_luns != 2 || tot_len != $expected_len )); then
14118                 error "OST$last_lun $num_luns != 2, $tot_len != $expected_len"
14119         fi
14120
14121         echo "FIEMAP with continuation calls succeeded"
14122 }
14123 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
14124
14125 test_130f() {
14126         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14127         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14128         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14129                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14130
14131         local fm_file=$DIR/$tfile
14132         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
14133                 error "multiop create with lov_delay_create on $fm_file"
14134
14135         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14136         filefrag_extents=$(filefrag -vek $fm_file |
14137                            awk '/extents? found/ { print $2 }')
14138         if (( $filefrag_extents != 0 )); then
14139                 error "$fm_file: filefrag_extents=$filefrag_extents != 0"
14140         fi
14141
14142         rm -f $fm_file
14143 }
14144 run_test 130f "FIEMAP (unstriped file)"
14145
14146 test_130g() {
14147         (( $MDS1_VERSION >= $(version_code 2.12.53) )) ||
14148                 skip "Need MDS version with at least 2.12.53 for overstriping"
14149         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14150         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14151         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14152                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14153
14154         local file=$DIR/$tfile
14155         local nr=$((OSTCOUNT * 100))
14156
14157         $LFS setstripe -C $nr $file || error "failed to setstripe -C $nr $file"
14158
14159         stack_trap "rm -f $file"
14160         dd if=/dev/zero of=$file count=$nr bs=1M
14161         sync
14162         nr=$($LFS getstripe -c $file)
14163
14164         local extents=$(filefrag -v $file |
14165                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}' | wc -l)
14166
14167         echo "filefrag list $extents extents in file with stripecount $nr"
14168         if (( extents < nr )); then
14169                 $LFS getstripe $file
14170                 filefrag -v $file
14171                 error "filefrag printed $extents < $nr extents"
14172         fi
14173 }
14174 run_test 130g "FIEMAP (overstripe file)"
14175
14176 # Test for writev/readv
14177 test_131a() {
14178         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
14179                 error "writev test failed"
14180         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
14181                 error "readv failed"
14182         rm -f $DIR/$tfile
14183 }
14184 run_test 131a "test iov's crossing stripe boundary for writev/readv"
14185
14186 test_131b() {
14187         local fsize=$((524288 + 1048576 + 1572864))
14188         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
14189                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
14190                         error "append writev test failed"
14191
14192         ((fsize += 1572864 + 1048576))
14193         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
14194                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
14195                         error "append writev test failed"
14196         rm -f $DIR/$tfile
14197 }
14198 run_test 131b "test append writev"
14199
14200 test_131c() {
14201         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
14202         error "NOT PASS"
14203 }
14204 run_test 131c "test read/write on file w/o objects"
14205
14206 test_131d() {
14207         rwv -f $DIR/$tfile -w -n 1 1572864
14208         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
14209         if [ "$NOB" != 1572864 ]; then
14210                 error "Short read filed: read $NOB bytes instead of 1572864"
14211         fi
14212         rm -f $DIR/$tfile
14213 }
14214 run_test 131d "test short read"
14215
14216 test_131e() {
14217         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
14218         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
14219         error "read hitting hole failed"
14220         rm -f $DIR/$tfile
14221 }
14222 run_test 131e "test read hitting hole"
14223
14224 check_stats() {
14225         local facet=$1
14226         local op=$2
14227         local want=${3:-0}
14228         local res
14229
14230         # open             11 samples [usecs] 468 4793 13658 35791898
14231         case $facet in
14232         mds*) res=($(do_facet $facet \
14233                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op"))
14234                  ;;
14235         ost*) res=($(do_facet $facet \
14236                   $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op"))
14237                  ;;
14238         *) error "Wrong facet '$facet'" ;;
14239         esac
14240         [[ -n "$res" ]] || error "counter for $op on $facet not incremented"
14241         # if $want is zero, it means any stat increment is ok.
14242         if (( $want > 0 )); then
14243                 local count=${res[1]}
14244
14245                 if (( $count != $want )); then
14246                         if [[ $facet =~ "mds" ]]; then
14247                                 do_nodes $(comma_list $(mdts_nodes)) \
14248                                         $LCTL get_param mdt.*.md_stats
14249                         else
14250                                 do_nodes $(comma_list $(osts-nodes)) \
14251                                         $LCTL get_param obdfilter.*.stats
14252                         fi
14253                         error "The $op counter on $facet is $count, not $want"
14254                 fi
14255         fi
14256 }
14257
14258 test_133a() {
14259         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14260         remote_ost_nodsh && skip "remote OST with nodsh"
14261         remote_mds_nodsh && skip "remote MDS with nodsh"
14262         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
14263                 skip_env "MDS doesn't support rename stats"
14264
14265         local testdir=$DIR/${tdir}/stats_testdir
14266
14267         mkdir -p $DIR/${tdir}
14268
14269         # clear stats.
14270         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14271         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14272
14273         # verify mdt stats first.
14274         mkdir_on_mdt0 ${testdir} || error "mkdir_on_mdt0 failed"
14275         check_stats $SINGLEMDS "mkdir" 1
14276
14277         # clear "open" from "lfs mkdir" above
14278         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14279         touch ${testdir}/${tfile} || error "touch failed"
14280         check_stats $SINGLEMDS "open" 1
14281         check_stats $SINGLEMDS "close" 1
14282         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
14283                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
14284                 check_stats $SINGLEMDS "mknod" 2
14285         }
14286         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
14287         check_stats $SINGLEMDS "unlink" 1
14288         rm -f ${testdir}/${tfile} || error "file remove failed"
14289         check_stats $SINGLEMDS "unlink" 2
14290
14291         # remove working dir and check mdt stats again.
14292         rmdir ${testdir} || error "rmdir failed"
14293         check_stats $SINGLEMDS "rmdir" 1
14294
14295         local testdir1=$DIR/${tdir}/stats_testdir1
14296         mkdir -p ${testdir}
14297         mkdir -p ${testdir1}
14298         touch ${testdir1}/test1
14299         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
14300         check_stats $SINGLEMDS "crossdir_rename" 1
14301
14302         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
14303         check_stats $SINGLEMDS "samedir_rename" 1
14304
14305         rm -rf $DIR/${tdir}
14306 }
14307 run_test 133a "Verifying MDT stats ========================================"
14308
14309 test_133b() {
14310         local res
14311
14312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14313         remote_ost_nodsh && skip "remote OST with nodsh"
14314         remote_mds_nodsh && skip "remote MDS with nodsh"
14315
14316         local testdir=$DIR/${tdir}/stats_testdir
14317
14318         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
14319         mkdir_on_mdt0 ${testdir} || error "mkdir_on_mdt0 failed"
14320         touch ${testdir}/${tfile} || error "touch failed"
14321         cancel_lru_locks mdc
14322
14323         # clear stats.
14324         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14325         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14326
14327         # extra mdt stats verification.
14328         chmod 444 ${testdir}/${tfile} || error "chmod failed"
14329         check_stats $SINGLEMDS "setattr" 1
14330         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14331         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
14332         then            # LU-1740
14333                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
14334                 check_stats $SINGLEMDS "getattr" 1
14335         fi
14336         rm -rf $DIR/${tdir}
14337
14338         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
14339         # so the check below is not reliable
14340         [ $MDSCOUNT -eq 1 ] || return 0
14341
14342         # Sleep to avoid a cached response.
14343         #define OBD_STATFS_CACHE_SECONDS 1
14344         sleep 2
14345         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14346         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
14347         $LFS df || error "lfs failed"
14348         check_stats $SINGLEMDS "statfs" 1
14349
14350         # check aggregated statfs (LU-10018)
14351         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
14352                 return 0
14353         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
14354                 return 0
14355         sleep 2
14356         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14357         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
14358         df $DIR
14359         check_stats $SINGLEMDS "statfs" 1
14360
14361         # We want to check that the client didn't send OST_STATFS to
14362         # ost1 but the MDT also uses OST_STATFS for precreate. So some
14363         # extra care is needed here.
14364         if remote_mds; then
14365                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
14366                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
14367
14368                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
14369                 [ "$res" ] && error "OST got STATFS"
14370         fi
14371
14372         return 0
14373 }
14374 run_test 133b "Verifying extra MDT stats =================================="
14375
14376 test_133c() {
14377         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14378         remote_ost_nodsh && skip "remote OST with nodsh"
14379         remote_mds_nodsh && skip "remote MDS with nodsh"
14380
14381         local testdir=$DIR/$tdir/stats_testdir
14382
14383         test_mkdir -p $testdir
14384
14385         # verify obdfilter stats.
14386         $LFS setstripe -c 1 -i 0 $testdir/$tfile
14387         sync
14388         cancel_lru_locks osc
14389         wait_delete_completed
14390
14391         # clear stats.
14392         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14393         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14394
14395         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
14396                 error "dd failed"
14397         sync
14398         cancel_lru_locks osc
14399         check_stats ost1 "write" 1
14400
14401         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
14402         check_stats ost1 "read" 1
14403
14404         > $testdir/$tfile || error "truncate failed"
14405         check_stats ost1 "punch" 1
14406
14407         rm -f $testdir/$tfile || error "file remove failed"
14408         wait_delete_completed
14409         check_stats ost1 "destroy" 1
14410
14411         rm -rf $DIR/$tdir
14412 }
14413 run_test 133c "Verifying OST stats ========================================"
14414
14415 order_2() {
14416         local value=$1
14417         local orig=$value
14418         local order=1
14419
14420         while [ $value -ge 2 ]; do
14421                 order=$((order*2))
14422                 value=$((value/2))
14423         done
14424
14425         if [ $orig -gt $order ]; then
14426                 order=$((order*2))
14427         fi
14428         echo $order
14429 }
14430
14431 size_in_KMGT() {
14432     local value=$1
14433     local size=('K' 'M' 'G' 'T');
14434     local i=0
14435     local size_string=$value
14436
14437     while [ $value -ge 1024 ]; do
14438         if [ $i -gt 3 ]; then
14439             #T is the biggest unit we get here, if that is bigger,
14440             #just return XXXT
14441             size_string=${value}T
14442             break
14443         fi
14444         value=$((value >> 10))
14445         if [ $value -lt 1024 ]; then
14446             size_string=${value}${size[$i]}
14447             break
14448         fi
14449         i=$((i + 1))
14450     done
14451
14452     echo $size_string
14453 }
14454
14455 get_rename_size() {
14456         local size=$1
14457         local context=${2:-.}
14458         local sample=$(do_facet $SINGLEMDS $LCTL \
14459                 get_param mdt.$FSNAME-MDT0000.rename_stats |
14460                 grep -A1 $context |
14461                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
14462         echo $sample
14463 }
14464
14465 test_133d() {
14466         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14467         remote_ost_nodsh && skip "remote OST with nodsh"
14468         remote_mds_nodsh && skip "remote MDS with nodsh"
14469         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
14470                 skip_env "MDS doesn't support rename stats"
14471
14472         local testdir1=$DIR/${tdir}/stats_testdir1
14473         local testdir2=$DIR/${tdir}/stats_testdir2
14474         mkdir -p $DIR/${tdir} || error "mkdir $tdir failed"
14475
14476         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14477
14478         mkdir_on_mdt0 ${testdir1} || error "mkdir $testdir1 failed"
14479         mkdir_on_mdt0 ${testdir2} || error "mkdir $testdir2 failed"
14480
14481         createmany -o $testdir1/test 512 || error "createmany failed"
14482
14483         # check samedir rename size
14484         mv ${testdir1}/test0 ${testdir1}/test_0
14485
14486         local testdir1_size=$(ls -l $DIR/${tdir} |
14487                 awk '/stats_testdir1/ {print $5}')
14488         local testdir2_size=$(ls -l $DIR/${tdir} |
14489                 awk '/stats_testdir2/ {print $5}')
14490
14491         testdir1_size=$(order_2 $testdir1_size)
14492         testdir2_size=$(order_2 $testdir2_size)
14493
14494         testdir1_size=$(size_in_KMGT $testdir1_size)
14495         testdir2_size=$(size_in_KMGT $testdir2_size)
14496
14497         echo "source rename dir size: ${testdir1_size}"
14498         echo "target rename dir size: ${testdir2_size}"
14499
14500         local cmd="do_facet $SINGLEMDS $LCTL "
14501         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
14502
14503         eval $cmd || error "$cmd failed"
14504         local samedir=$($cmd | grep 'same_dir')
14505         local same_sample=$(get_rename_size $testdir1_size)
14506         [ -z "$samedir" ] && error "samedir_rename_size count error"
14507         [[ $same_sample -eq 1 ]] ||
14508                 error "samedir_rename_size error $same_sample"
14509         echo "Check same dir rename stats success"
14510
14511         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14512
14513         # check crossdir rename size
14514         mv ${testdir1}/test_0 ${testdir2}/test_0
14515
14516         testdir1_size=$(ls -l $DIR/${tdir} |
14517                 awk '/stats_testdir1/ {print $5}')
14518         testdir2_size=$(ls -l $DIR/${tdir} |
14519                 awk '/stats_testdir2/ {print $5}')
14520
14521         testdir1_size=$(order_2 $testdir1_size)
14522         testdir2_size=$(order_2 $testdir2_size)
14523
14524         testdir1_size=$(size_in_KMGT $testdir1_size)
14525         testdir2_size=$(size_in_KMGT $testdir2_size)
14526
14527         echo "source rename dir size: ${testdir1_size}"
14528         echo "target rename dir size: ${testdir2_size}"
14529
14530         eval $cmd || error "$cmd failed"
14531         local crossdir=$($cmd | grep 'crossdir')
14532         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
14533         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
14534         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
14535         [[ $src_sample -eq 1 ]] ||
14536                 error "crossdir_rename_size error $src_sample"
14537         [[ $tgt_sample -eq 1 ]] ||
14538                 error "crossdir_rename_size error $tgt_sample"
14539         echo "Check cross dir rename stats success"
14540         rm -rf $DIR/${tdir}
14541 }
14542 run_test 133d "Verifying rename_stats ========================================"
14543
14544 test_133e() {
14545         remote_mds_nodsh && skip "remote MDS with nodsh"
14546         remote_ost_nodsh && skip "remote OST with nodsh"
14547         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14548
14549         local testdir=$DIR/${tdir}/stats_testdir
14550         local ctr f0 f1 bs=32768 count=42 sum
14551
14552         mkdir -p ${testdir} || error "mkdir failed"
14553
14554         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
14555
14556         for ctr in {write,read}_bytes; do
14557                 sync
14558                 cancel_lru_locks osc
14559
14560                 do_facet ost1 $LCTL set_param -n \
14561                         "obdfilter.*.exports.clear=clear"
14562
14563                 if [ $ctr = write_bytes ]; then
14564                         f0=/dev/zero
14565                         f1=${testdir}/${tfile}
14566                 else
14567                         f0=${testdir}/${tfile}
14568                         f1=/dev/null
14569                 fi
14570
14571                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
14572                         error "dd failed"
14573                 sync
14574                 cancel_lru_locks osc
14575
14576                 sum=$(do_facet ost1 $LCTL get_param \
14577                         "obdfilter.*.exports.*.stats" |
14578                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
14579                                 $1 == ctr { sum += $7 }
14580                                 END { printf("%0.0f", sum) }')
14581
14582                 if ((sum != bs * count)); then
14583                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
14584                 fi
14585         done
14586
14587         rm -rf $DIR/${tdir}
14588 }
14589 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
14590
14591 test_133f() {
14592         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
14593                 skip "too old lustre for get_param -R ($facet_ver)"
14594
14595         # verifying readability.
14596         $LCTL get_param -R '*' &> /dev/null
14597
14598         # Verifing writability with badarea_io.
14599         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14600         local skipped_params='force_lbug|changelog_mask|daemon_file'
14601         $LCTL list_param -FR '*' | grep '=' | tr -d = |
14602                 egrep -v "$skipped_params" |
14603                 xargs -n 1 find $proc_dirs -name |
14604                 xargs -n 1 badarea_io ||
14605                 error "client badarea_io failed"
14606
14607         # remount the FS in case writes/reads /proc break the FS
14608         cleanup || error "failed to unmount"
14609         setup || error "failed to setup"
14610 }
14611 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
14612
14613 test_133g() {
14614         remote_mds_nodsh && skip "remote MDS with nodsh"
14615         remote_ost_nodsh && skip "remote OST with nodsh"
14616
14617         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14618         local skipped_params="'force_lbug|changelog_mask|daemon_file'"
14619         local facet
14620         for facet in mds1 ost1; do
14621                 local facet_ver=$(lustre_version_code $facet)
14622                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
14623                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
14624                 else
14625                         log "$facet: too old lustre for get_param -R"
14626                 fi
14627                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
14628                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
14629                                 tr -d = | egrep -v $skipped_params |
14630                                 xargs -n 1 find $proc_dirs -name |
14631                                 xargs -n 1 badarea_io" ||
14632                                         error "$facet badarea_io failed"
14633                 else
14634                         skip_noexit "$facet: too old lustre for get_param -R"
14635                 fi
14636         done
14637
14638         # remount the FS in case writes/reads /proc break the FS
14639         cleanup || error "failed to unmount"
14640         setup || error "failed to setup"
14641 }
14642 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
14643
14644 test_133h() {
14645         remote_mds_nodsh && skip "remote MDS with nodsh"
14646         remote_ost_nodsh && skip "remote OST with nodsh"
14647         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
14648                 skip "Need MDS version at least 2.9.54"
14649
14650         local facet
14651         for facet in client mds1 ost1; do
14652                 # Get the list of files that are missing the terminating newline
14653                 local plist=$(do_facet $facet
14654                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
14655                 local ent
14656                 for ent in $plist; do
14657                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
14658                                 awk -v FS='\v' -v RS='\v\v' \
14659                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
14660                                         print FILENAME}'" 2>/dev/null)
14661                         [ -z $missing ] || {
14662                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
14663                                 error "file does not end with newline: $facet-$ent"
14664                         }
14665                 done
14666         done
14667 }
14668 run_test 133h "Proc files should end with newlines"
14669
14670 test_134a() {
14671         remote_mds_nodsh && skip "remote MDS with nodsh"
14672         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14673                 skip "Need MDS version at least 2.7.54"
14674
14675         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14676         cancel_lru_locks mdc
14677
14678         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
14679         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14680         [ $unused -eq 0 ] || error "$unused locks are not cleared"
14681
14682         local nr=1000
14683         createmany -o $DIR/$tdir/f $nr ||
14684                 error "failed to create $nr files in $DIR/$tdir"
14685         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14686
14687         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
14688         do_facet mds1 $LCTL set_param fail_loc=0x327
14689         do_facet mds1 $LCTL set_param fail_val=500
14690         touch $DIR/$tdir/m
14691
14692         echo "sleep 10 seconds ..."
14693         sleep 10
14694         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
14695
14696         do_facet mds1 $LCTL set_param fail_loc=0
14697         do_facet mds1 $LCTL set_param fail_val=0
14698         [ $lck_cnt -lt $unused ] ||
14699                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
14700
14701         rm $DIR/$tdir/m
14702         unlinkmany $DIR/$tdir/f $nr
14703 }
14704 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
14705
14706 test_134b() {
14707         remote_mds_nodsh && skip "remote MDS with nodsh"
14708         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14709                 skip "Need MDS version at least 2.7.54"
14710
14711         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14712         cancel_lru_locks mdc
14713
14714         local low_wm=$(do_facet mds1 $LCTL get_param -n \
14715                         ldlm.lock_reclaim_threshold_mb)
14716         # disable reclaim temporarily
14717         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
14718
14719         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
14720         do_facet mds1 $LCTL set_param fail_loc=0x328
14721         do_facet mds1 $LCTL set_param fail_val=500
14722
14723         $LCTL set_param debug=+trace
14724
14725         local nr=600
14726         createmany -o $DIR/$tdir/f $nr &
14727         local create_pid=$!
14728
14729         echo "Sleep $TIMEOUT seconds ..."
14730         sleep $TIMEOUT
14731         if ! ps -p $create_pid  > /dev/null 2>&1; then
14732                 do_facet mds1 $LCTL set_param fail_loc=0
14733                 do_facet mds1 $LCTL set_param fail_val=0
14734                 do_facet mds1 $LCTL set_param \
14735                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
14736                 error "createmany finished incorrectly!"
14737         fi
14738         do_facet mds1 $LCTL set_param fail_loc=0
14739         do_facet mds1 $LCTL set_param fail_val=0
14740         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
14741         wait $create_pid || return 1
14742
14743         unlinkmany $DIR/$tdir/f $nr
14744 }
14745 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
14746
14747 test_135() {
14748         remote_mds_nodsh && skip "remote MDS with nodsh"
14749         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14750                 skip "Need MDS version at least 2.13.50"
14751         local fname
14752
14753         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14754
14755 #define OBD_FAIL_PLAIN_RECORDS 0x1319
14756         #set only one record at plain llog
14757         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
14758
14759         #fill already existed plain llog each 64767
14760         #wrapping whole catalog
14761         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14762
14763         createmany -o $DIR/$tdir/$tfile_ 64700
14764         for (( i = 0; i < 64700; i = i + 2 ))
14765         do
14766                 rm $DIR/$tdir/$tfile_$i &
14767                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14768                 local pid=$!
14769                 wait $pid
14770         done
14771
14772         #waiting osp synchronization
14773         wait_delete_completed
14774 }
14775 run_test 135 "Race catalog processing"
14776
14777 test_136() {
14778         remote_mds_nodsh && skip "remote MDS with nodsh"
14779         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14780                 skip "Need MDS version at least 2.13.50"
14781         local fname
14782
14783         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14784         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
14785         #set only one record at plain llog
14786 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
14787         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
14788
14789         #fill already existed 2 plain llogs each 64767
14790         #wrapping whole catalog
14791         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14792         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
14793         wait_delete_completed
14794
14795         createmany -o $DIR/$tdir/$tfile_ 10
14796         sleep 25
14797
14798         do_facet $SINGLEMDS $LCTL set_param fail_val=3
14799         for (( i = 0; i < 10; i = i + 3 ))
14800         do
14801                 rm $DIR/$tdir/$tfile_$i &
14802                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14803                 local pid=$!
14804                 wait $pid
14805                 sleep 7
14806                 rm $DIR/$tdir/$tfile_$((i + 2)) &
14807         done
14808
14809         #waiting osp synchronization
14810         wait_delete_completed
14811 }
14812 run_test 136 "Race catalog processing 2"
14813
14814 test_140() { #bug-17379
14815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14816
14817         test_mkdir $DIR/$tdir
14818         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
14819         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
14820
14821         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
14822         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
14823         local i=0
14824         while i=$((i + 1)); do
14825                 test_mkdir $i
14826                 cd $i || error "Changing to $i"
14827                 ln -s ../stat stat || error "Creating stat symlink"
14828                 # Read the symlink until ELOOP present,
14829                 # not LBUGing the system is considered success,
14830                 # we didn't overrun the stack.
14831                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
14832                 if [ $ret -ne 0 ]; then
14833                         if [ $ret -eq 40 ]; then
14834                                 break  # -ELOOP
14835                         else
14836                                 error "Open stat symlink"
14837                                         return
14838                         fi
14839                 fi
14840         done
14841         i=$((i - 1))
14842         echo "The symlink depth = $i"
14843         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
14844                 error "Invalid symlink depth"
14845
14846         # Test recursive symlink
14847         ln -s symlink_self symlink_self
14848         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
14849         echo "open symlink_self returns $ret"
14850         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
14851 }
14852 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
14853
14854 test_150a() {
14855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14856
14857         local TF="$TMP/$tfile"
14858
14859         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14860         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14861         cp $TF $DIR/$tfile
14862         cancel_lru_locks $OSC
14863         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
14864         remount_client $MOUNT
14865         df -P $MOUNT
14866         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
14867
14868         $TRUNCATE $TF 6000
14869         $TRUNCATE $DIR/$tfile 6000
14870         cancel_lru_locks $OSC
14871         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
14872
14873         echo "12345" >>$TF
14874         echo "12345" >>$DIR/$tfile
14875         cancel_lru_locks $OSC
14876         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
14877
14878         echo "12345" >>$TF
14879         echo "12345" >>$DIR/$tfile
14880         cancel_lru_locks $OSC
14881         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
14882 }
14883 run_test 150a "truncate/append tests"
14884
14885 test_150b() {
14886         check_set_fallocate_or_skip
14887
14888         touch $DIR/$tfile
14889         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14890         check_fallocate $DIR/$tfile || skip_eopnotsupp "fallocate failed"
14891 }
14892 run_test 150b "Verify fallocate (prealloc) functionality"
14893
14894 test_150bb() {
14895         check_set_fallocate_or_skip
14896
14897         touch $DIR/$tfile
14898         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14899         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=20 || error "dd failed"
14900         > $DIR/$tfile
14901         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14902         # precomputed md5sum for 20MB of zeroes
14903         local expect="8f4e33f3dc3e414ff94e5fb6905cba8c"
14904         local sum=($(md5sum $DIR/$tfile))
14905
14906         [[ "${sum[0]}" == "$expect" ]] || error "fallocate unwritten is not zero"
14907
14908         check_set_fallocate 1
14909
14910         > $DIR/$tfile
14911         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
14912         sum=($(md5sum $DIR/$tfile))
14913
14914         [[ "${sum[0]}" == "$expect" ]] || error "fallocate zero is not zero"
14915 }
14916 run_test 150bb "Verify fallocate modes both zero space"
14917
14918 test_150c() {
14919         check_set_fallocate_or_skip
14920         local striping="-c2"
14921
14922         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14923         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tfile || error "setstripe failed"
14924         fallocate -l ${OSTCOUNT}m $DIR/$tfile || error "fallocate failed"
14925         local bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14926         local want=$((OSTCOUNT * 1048576))
14927
14928         # Must allocate all requested space, not more than 5% extra
14929         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14930                 error "bytes $bytes is not $want"
14931
14932         rm -f $DIR/$tfile
14933
14934         echo "verify fallocate on PFL file"
14935
14936         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14937
14938         $LFS setstripe -E1M $striping -E16M -c3 -Eeof -c 4 $DIR/$tfile ||
14939                 error "Create $DIR/$tfile failed"
14940         fallocate -l $((1048576 * 512)) $DIR/$tfile || error "fallocate failed"
14941         bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
14942         want=$((512 * 1048576))
14943
14944         # Must allocate all requested space, not more than 5% extra
14945         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14946                 error "bytes $bytes is not $want"
14947 }
14948 run_test 150c "Verify fallocate Size and Blocks"
14949
14950 test_150d() {
14951         check_set_fallocate_or_skip
14952         local striping="-c2"
14953
14954         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
14955
14956         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14957         $LFS setstripe -E1M $striping -E eof -c $OSTCOUNT -S1M $DIR/$tdir ||
14958                 error "setstripe failed"
14959         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
14960         local bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
14961         local want=$((OSTCOUNT * 1048576))
14962
14963         # Must allocate all requested space, not more than 5% extra
14964         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
14965                 error "bytes $bytes is not $want"
14966 }
14967 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
14968
14969 test_150e() {
14970         check_set_fallocate_or_skip
14971
14972         echo "df before:"
14973         $LFS df
14974         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14975         $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
14976                 error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
14977
14978         # Find OST with Minimum Size
14979         min_size_ost=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
14980                        sort -un | head -1)
14981
14982         # Get 100MB per OST of the available space to reduce run time
14983         # else 60% of the available space if we are running SLOW tests
14984         if [ $SLOW == "no" ]; then
14985                 local space=$((1024 * 100 * OSTCOUNT))
14986         else
14987                 local space=$(((min_size_ost * 60)/100 * OSTCOUNT))
14988         fi
14989
14990         fallocate -l${space}k $DIR/$tfile ||
14991                 error "fallocate ${space}k $DIR/$tfile failed"
14992         echo "'fallocate -l ${space}k $DIR/$tfile' succeeded"
14993
14994         # get size immediately after fallocate. This should be correctly
14995         # updated
14996         local size=$(stat -c '%s' $DIR/$tfile)
14997         local used=$(( $(stat -c '%b * %B' $DIR/$tfile) / 1024))
14998
14999         # Sleep for a while for statfs to get updated. And not pull from cache.
15000         sleep 2
15001
15002         echo "df after fallocate:"
15003         $LFS df
15004
15005         (( size / 1024 == space )) || error "size $size != requested $space"
15006         [ "$ost1_FSTYPE" != ldiskfs ] || (( used >= space )) ||
15007                 error "used $used < space $space"
15008
15009         rm $DIR/$tfile || error "rm failed"
15010         sync
15011         wait_delete_completed
15012
15013         echo "df after unlink:"
15014         $LFS df
15015 }
15016 run_test 150e "Verify 60% of available OST space consumed by fallocate"
15017
15018 test_150f() {
15019         local size
15020         local blocks
15021         local want_size_before=20480 # in bytes
15022         local want_blocks_before=40 # 512 sized blocks
15023         local want_blocks_after=24  # 512 sized blocks
15024         local length=$(((want_blocks_before - want_blocks_after) * 512))
15025
15026         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
15027                 skip "need at least 2.14.0 for fallocate punch"
15028
15029         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
15030                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
15031         fi
15032
15033         check_set_fallocate_or_skip
15034         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15035
15036         [[ "x$DOM" == "xyes" ]] &&
15037                 $LFS setstripe -E1M -L mdt -E eof $DIR/$tfile
15038
15039         echo "Verify fallocate punch: Range within the file range"
15040         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
15041                 error "dd failed for bs 4096 and count 5"
15042
15043         # Call fallocate with punch range which is within the file range
15044         out=$(fallocate -p --offset 4096 -l $length $DIR/$tfile 2>&1) ||
15045                 skip_eopnotsupp "$out|fallocate: offset 4096 and length $length"
15046         # client must see changes immediately after fallocate
15047         size=$(stat -c '%s' $DIR/$tfile)
15048         blocks=$(stat -c '%b' $DIR/$tfile)
15049
15050         # Verify punch worked.
15051         (( blocks == want_blocks_after )) ||
15052                 error "punch failed: blocks $blocks != $want_blocks_after"
15053
15054         (( size == want_size_before )) ||
15055                 error "punch failed: size $size != $want_size_before"
15056
15057         # Verify there is hole in file
15058         local data_off=$(lseek_test -d 4096 $DIR/$tfile)
15059         # precomputed md5sum
15060         local expect="4a9a834a2db02452929c0a348273b4aa"
15061
15062         cksum=($(md5sum $DIR/$tfile))
15063         [[ "${cksum[0]}" == "$expect" ]] ||
15064                 error "unexpected MD5SUM after punch: ${cksum[0]}"
15065
15066         # Start second sub-case for fallocate punch.
15067         echo "Verify fallocate punch: Range overlapping and less than blocksize"
15068         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
15069                 error "dd failed for bs 4096 and count 5"
15070
15071         # Punch range less than block size will have no change in block count
15072         want_blocks_after=40  # 512 sized blocks
15073
15074         # Punch overlaps two blocks and less than blocksize
15075         out=$(fallocate -p --offset 4000 -l 3000 $DIR/$tfile 2>&1) ||
15076                 skip_eopnotsupp "$out|fallocate: offset 4000 length 3000"
15077         size=$(stat -c '%s' $DIR/$tfile)
15078         blocks=$(stat -c '%b' $DIR/$tfile)
15079
15080         # Verify punch worked.
15081         (( blocks == want_blocks_after )) ||
15082                 error "punch failed: blocks $blocks != $want_blocks_after"
15083
15084         (( size == want_size_before )) ||
15085                 error "punch failed: size $size != $want_size_before"
15086
15087         # Verify if range is really zero'ed out. We expect Zeros.
15088         # precomputed md5sum
15089         expect="c57ec5d769c3dbe3426edc3f7d7e11d3"
15090         cksum=($(md5sum $DIR/$tfile))
15091         [[ "${cksum[0]}" == "$expect" ]] ||
15092                 error "unexpected MD5SUM after punch: ${cksum[0]}"
15093 }
15094 run_test 150f "Verify fallocate punch functionality"
15095
15096 test_150g() {
15097         local space
15098         local size
15099         local blocks
15100         local blocks_after
15101         local size_after
15102         local BS=4096 # Block size in bytes
15103
15104         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
15105                 skip "need at least 2.14.0 for fallocate punch"
15106
15107         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
15108                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
15109         fi
15110
15111         check_set_fallocate_or_skip
15112         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15113
15114         if [[ "x$DOM" == "xyes" ]]; then
15115                 $LFS setstripe -E2M -L mdt -E eof -c${OSTCOUNT} $DIR/$tfile ||
15116                         error "$LFS setstripe DoM + ${OSTCOUNT} OST failed"
15117         else
15118                 $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
15119                         error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
15120         fi
15121
15122         # Get 100MB per OST of the available space to reduce run time
15123         # else 60% of the available space if we are running SLOW tests
15124         if [ $SLOW == "no" ]; then
15125                 space=$((1024 * 100 * OSTCOUNT))
15126         else
15127                 # Find OST with Minimum Size
15128                 space=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
15129                         sort -un | head -1)
15130                 echo "min size OST: $space"
15131                 space=$(((space * 60)/100 * OSTCOUNT))
15132         fi
15133         # space in 1k units, round to 4k blocks
15134         local blkcount=$((space * 1024 / $BS))
15135
15136         echo "Verify fallocate punch: Very large Range"
15137         fallocate -l${space}k $DIR/$tfile ||
15138                 error "fallocate ${space}k $DIR/$tfile failed"
15139         # write 1M at the end, start and in the middle
15140         yes 'A' | dd of=$DIR/$tfile bs=$BS count=256 ||
15141                 error "dd failed: bs $BS count 256"
15142         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount - 256)) count=256 ||
15143                 error "dd failed: bs $BS count 256 seek $((blkcount - 256))"
15144         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount / 2)) count=1024 ||
15145                 error "dd failed: bs $BS count 256 seek $((blkcount / 2))"
15146
15147         # Gather stats.
15148         size=$(stat -c '%s' $DIR/$tfile)
15149
15150         # gather punch length.
15151         local punch_size=$((size - (BS * 2)))
15152
15153         echo "punch_size = $punch_size"
15154         echo "size - punch_size: $((size - punch_size))"
15155         echo "size - punch_size in blocks: $(((size - punch_size)/BS))"
15156
15157         # Call fallocate to punch all except 2 blocks. We leave the
15158         # first and the last block
15159         echo "fallocate -p --offset $BS -l $punch_size $DIR/$tfile"
15160         out=$(fallocate -p --offset $BS -l $punch_size $DIR/$tfile 2>&1) ||
15161                 skip_eopnotsupp "$out|fallocate: offset $BS length $punch_size"
15162
15163         size_after=$(stat -c '%s' $DIR/$tfile)
15164         blocks_after=$(stat -c '%b' $DIR/$tfile)
15165
15166         # Verify punch worked.
15167         # Size should be kept
15168         (( size == size_after )) ||
15169                 error "punch failed: size $size != $size_after"
15170
15171         # two 4k data blocks to remain plus possible 1 extra extent block
15172         (( blocks_after <= ((BS / 512) * 3) )) ||
15173                 error "too many blocks remains: $blocks_after"
15174
15175         # Verify that file has hole between the first and the last blocks
15176         local hole_start=$(lseek_test -l 0 $DIR/$tfile)
15177         local hole_end=$(lseek_test -d $BS $DIR/$tfile)
15178
15179         echo "Hole at [$hole_start, $hole_end)"
15180         (( hole_start == BS )) ||
15181                 error "no hole at offset $BS after punch"
15182
15183         (( hole_end == BS + punch_size )) ||
15184                 error "data at offset $hole_end < $((BS + punch_size))"
15185 }
15186 run_test 150g "Verify fallocate punch on large range"
15187
15188 #LU-2902 roc_hit was not able to read all values from lproc
15189 function roc_hit_init() {
15190         local list=$(comma_list $(osts_nodes))
15191         local dir=$DIR/$tdir-check
15192         local file=$dir/$tfile
15193         local BEFORE
15194         local AFTER
15195         local idx
15196
15197         test_mkdir $dir
15198         #use setstripe to do a write to every ost
15199         for i in $(seq 0 $((OSTCOUNT-1))); do
15200                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
15201                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
15202                 idx=$(printf %04x $i)
15203                 BEFORE=$(get_osd_param $list *OST*$idx stats |
15204                         awk '$1 == "cache_access" {sum += $7}
15205                                 END { printf("%0.0f", sum) }')
15206
15207                 cancel_lru_locks osc
15208                 cat $file >/dev/null
15209
15210                 AFTER=$(get_osd_param $list *OST*$idx stats |
15211                         awk '$1 == "cache_access" {sum += $7}
15212                                 END { printf("%0.0f", sum) }')
15213
15214                 echo BEFORE:$BEFORE AFTER:$AFTER
15215                 if ! let "AFTER - BEFORE == 4"; then
15216                         rm -rf $dir
15217                         error "roc_hit is not safe to use"
15218                 fi
15219                 rm $file
15220         done
15221
15222         rm -rf $dir
15223 }
15224
15225 function roc_hit() {
15226         local list=$(comma_list $(osts_nodes))
15227         echo $(get_osd_param $list '' stats |
15228                 awk '$1 == "cache_hit" {sum += $7}
15229                         END { printf("%0.0f", sum) }')
15230 }
15231
15232 function set_cache() {
15233         local on=1
15234
15235         if [ "$2" == "off" ]; then
15236                 on=0;
15237         fi
15238         local list=$(comma_list $(osts_nodes))
15239         set_osd_param $list '' $1_cache_enable $on
15240
15241         cancel_lru_locks osc
15242 }
15243
15244 test_151() {
15245         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15246         remote_ost_nodsh && skip "remote OST with nodsh"
15247
15248         local CPAGES=3
15249         local list=$(comma_list $(osts_nodes))
15250
15251         # check whether obdfilter is cache capable at all
15252         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
15253                 skip "not cache-capable obdfilter"
15254         fi
15255
15256         # check cache is enabled on all obdfilters
15257         if get_osd_param $list '' read_cache_enable | grep 0; then
15258                 skip "oss cache is disabled"
15259         fi
15260
15261         set_osd_param $list '' writethrough_cache_enable 1
15262
15263         # check write cache is enabled on all obdfilters
15264         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
15265                 skip "oss write cache is NOT enabled"
15266         fi
15267
15268         roc_hit_init
15269
15270         #define OBD_FAIL_OBD_NO_LRU  0x609
15271         do_nodes $list $LCTL set_param fail_loc=0x609
15272
15273         # pages should be in the case right after write
15274         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
15275                 error "dd failed"
15276
15277         local BEFORE=$(roc_hit)
15278         cancel_lru_locks osc
15279         cat $DIR/$tfile >/dev/null
15280         local AFTER=$(roc_hit)
15281
15282         do_nodes $list $LCTL set_param fail_loc=0
15283
15284         if ! let "AFTER - BEFORE == CPAGES"; then
15285                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
15286         fi
15287
15288         cancel_lru_locks osc
15289         # invalidates OST cache
15290         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
15291         set_osd_param $list '' read_cache_enable 0
15292         cat $DIR/$tfile >/dev/null
15293
15294         # now data shouldn't be found in the cache
15295         BEFORE=$(roc_hit)
15296         cancel_lru_locks osc
15297         cat $DIR/$tfile >/dev/null
15298         AFTER=$(roc_hit)
15299         if let "AFTER - BEFORE != 0"; then
15300                 error "IN CACHE: before: $BEFORE, after: $AFTER"
15301         fi
15302
15303         set_osd_param $list '' read_cache_enable 1
15304         rm -f $DIR/$tfile
15305 }
15306 run_test 151 "test cache on oss and controls ==============================="
15307
15308 test_152() {
15309         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15310
15311         local TF="$TMP/$tfile"
15312
15313         # simulate ENOMEM during write
15314 #define OBD_FAIL_OST_NOMEM      0x226
15315         lctl set_param fail_loc=0x80000226
15316         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
15317         cp $TF $DIR/$tfile
15318         sync || error "sync failed"
15319         lctl set_param fail_loc=0
15320
15321         # discard client's cache
15322         cancel_lru_locks osc
15323
15324         # simulate ENOMEM during read
15325         lctl set_param fail_loc=0x80000226
15326         cmp $TF $DIR/$tfile || error "cmp failed"
15327         lctl set_param fail_loc=0
15328
15329         rm -f $TF
15330 }
15331 run_test 152 "test read/write with enomem ============================"
15332
15333 test_153() {
15334         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
15335 }
15336 run_test 153 "test if fdatasync does not crash ======================="
15337
15338 dot_lustre_fid_permission_check() {
15339         local fid=$1
15340         local ffid=$MOUNT/.lustre/fid/$fid
15341         local test_dir=$2
15342
15343         echo "stat fid $fid"
15344         stat $ffid || error "stat $ffid failed."
15345         echo "touch fid $fid"
15346         touch $ffid || error "touch $ffid failed."
15347         echo "write to fid $fid"
15348         cat /etc/hosts > $ffid || error "write $ffid failed."
15349         echo "read fid $fid"
15350         diff /etc/hosts $ffid || error "read $ffid failed."
15351         echo "append write to fid $fid"
15352         cat /etc/hosts >> $ffid || error "append write $ffid failed."
15353         echo "rename fid $fid"
15354         mv $ffid $test_dir/$tfile.1 &&
15355                 error "rename $ffid to $tfile.1 should fail."
15356         touch $test_dir/$tfile.1
15357         mv $test_dir/$tfile.1 $ffid &&
15358                 error "rename $tfile.1 to $ffid should fail."
15359         rm -f $test_dir/$tfile.1
15360         echo "truncate fid $fid"
15361         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
15362         echo "link fid $fid"
15363         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
15364         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
15365                 echo "setfacl fid $fid"
15366                 setfacl -R -m u:$USER0:rwx $ffid ||
15367                         error "setfacl $ffid failed"
15368                 echo "getfacl fid $fid"
15369                 getfacl $ffid || error "getfacl $ffid failed."
15370         fi
15371         echo "unlink fid $fid"
15372         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
15373         echo "mknod fid $fid"
15374         mknod $ffid c 1 3 && error "mknod $ffid should fail."
15375
15376         fid=[0xf00000400:0x1:0x0]
15377         ffid=$MOUNT/.lustre/fid/$fid
15378
15379         echo "stat non-exist fid $fid"
15380         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
15381         echo "write to non-exist fid $fid"
15382         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
15383         echo "link new fid $fid"
15384         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
15385
15386         mkdir -p $test_dir/$tdir
15387         touch $test_dir/$tdir/$tfile
15388         fid=$($LFS path2fid $test_dir/$tdir)
15389         rc=$?
15390         [ $rc -ne 0 ] &&
15391                 error "error: could not get fid for $test_dir/$dir/$tfile."
15392
15393         ffid=$MOUNT/.lustre/fid/$fid
15394
15395         echo "ls $fid"
15396         ls $ffid || error "ls $ffid failed."
15397         echo "touch $fid/$tfile.1"
15398         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
15399
15400         echo "touch $MOUNT/.lustre/fid/$tfile"
15401         touch $MOUNT/.lustre/fid/$tfile && \
15402                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
15403
15404         echo "setxattr to $MOUNT/.lustre/fid"
15405         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
15406
15407         echo "listxattr for $MOUNT/.lustre/fid"
15408         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
15409
15410         echo "delxattr from $MOUNT/.lustre/fid"
15411         setfattr -x trusted.name1 $MOUNT/.lustre/fid
15412
15413         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
15414         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
15415                 error "touch invalid fid should fail."
15416
15417         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
15418         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
15419                 error "touch non-normal fid should fail."
15420
15421         echo "rename $tdir to $MOUNT/.lustre/fid"
15422         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
15423                 error "rename to $MOUNT/.lustre/fid should fail."
15424
15425         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
15426         then            # LU-3547
15427                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
15428                 local new_obf_mode=777
15429
15430                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
15431                 chmod $new_obf_mode $DIR/.lustre/fid ||
15432                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
15433
15434                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
15435                 [ $obf_mode -eq $new_obf_mode ] ||
15436                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
15437
15438                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
15439                 chmod $old_obf_mode $DIR/.lustre/fid ||
15440                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
15441         fi
15442
15443         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
15444         fid=$($LFS path2fid $test_dir/$tfile-2)
15445
15446         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
15447         then # LU-5424
15448                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
15449                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
15450                         error "create lov data thru .lustre failed"
15451         fi
15452         echo "cp /etc/passwd $test_dir/$tfile-2"
15453         cp /etc/passwd $test_dir/$tfile-2 ||
15454                 error "copy to $test_dir/$tfile-2 failed."
15455         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
15456         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
15457                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
15458
15459         rm -rf $test_dir/tfile.lnk
15460         rm -rf $test_dir/$tfile-2
15461 }
15462
15463 test_154A() {
15464         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15465                 skip "Need MDS version at least 2.4.1"
15466
15467         local tf=$DIR/$tfile
15468         touch $tf
15469
15470         local fid=$($LFS path2fid $tf)
15471         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
15472
15473         # check that we get the same pathname back
15474         local rootpath
15475         local found
15476         for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do
15477                 echo "$rootpath $fid"
15478                 found=$($LFS fid2path $rootpath "$fid")
15479                 [ -z "$found" ] && error "fid2path unable to get '$fid' path"
15480                 [ "$found" == "$tf" ] || error "fid2path $found != $tf"
15481         done
15482
15483         # check wrong root path format
15484         rootpath=$MOUNT"_wrong"
15485         found=$($LFS fid2path $rootpath "$fid")
15486         [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)"
15487 }
15488 run_test 154A "lfs path2fid and fid2path basic checks"
15489
15490 test_154B() {
15491         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15492                 skip "Need MDS version at least 2.4.1"
15493
15494         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
15495         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
15496         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
15497         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
15498
15499         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
15500         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
15501
15502         # check that we get the same pathname
15503         echo "PFID: $PFID, name: $name"
15504         local FOUND=$($LFS fid2path $MOUNT "$PFID")
15505         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
15506         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
15507                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
15508
15509         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
15510 }
15511 run_test 154B "verify the ll_decode_linkea tool"
15512
15513 test_154a() {
15514         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15515         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15516         (( $MDS1_VERSION >= $(version_code 2.2.51) )) ||
15517                 skip "Need MDS version at least 2.2.51"
15518         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
15519
15520         cp /etc/hosts $DIR/$tfile
15521
15522         fid=$($LFS path2fid $DIR/$tfile)
15523         rc=$?
15524         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
15525
15526         dot_lustre_fid_permission_check "$fid" $DIR ||
15527                 error "dot lustre permission check $fid failed"
15528
15529         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
15530
15531         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
15532
15533         touch $MOUNT/.lustre/file &&
15534                 error "creation is not allowed under .lustre"
15535
15536         mkdir $MOUNT/.lustre/dir &&
15537                 error "mkdir is not allowed under .lustre"
15538
15539         rm -rf $DIR/$tfile
15540 }
15541 run_test 154a "Open-by-FID"
15542
15543 test_154b() {
15544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15545         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15546         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15547         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
15548                 skip "Need MDS version at least 2.2.51"
15549
15550         local remote_dir=$DIR/$tdir/remote_dir
15551         local MDTIDX=1
15552         local rc=0
15553
15554         mkdir -p $DIR/$tdir
15555         $LFS mkdir -i $MDTIDX $remote_dir ||
15556                 error "create remote directory failed"
15557
15558         cp /etc/hosts $remote_dir/$tfile
15559
15560         fid=$($LFS path2fid $remote_dir/$tfile)
15561         rc=$?
15562         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
15563
15564         dot_lustre_fid_permission_check "$fid" $remote_dir ||
15565                 error "dot lustre permission check $fid failed"
15566         rm -rf $DIR/$tdir
15567 }
15568 run_test 154b "Open-by-FID for remote directory"
15569
15570 test_154c() {
15571         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15572                 skip "Need MDS version at least 2.4.1"
15573
15574         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
15575         local FID1=$($LFS path2fid $DIR/$tfile.1)
15576         local FID2=$($LFS path2fid $DIR/$tfile.2)
15577         local FID3=$($LFS path2fid $DIR/$tfile.3)
15578
15579         local N=1
15580         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
15581                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
15582                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
15583                 local want=FID$N
15584                 [ "$FID" = "${!want}" ] ||
15585                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
15586                 N=$((N + 1))
15587         done
15588
15589         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
15590         do
15591                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
15592                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
15593                 N=$((N + 1))
15594         done
15595 }
15596 run_test 154c "lfs path2fid and fid2path multiple arguments"
15597
15598 test_154d() {
15599         remote_mds_nodsh && skip "remote MDS with nodsh"
15600         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
15601                 skip "Need MDS version at least 2.5.53"
15602
15603         if remote_mds; then
15604                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
15605         else
15606                 nid="0@lo"
15607         fi
15608         local proc_ofile="mdt.*.exports.'$nid'.open_files"
15609         local fd
15610         local cmd
15611
15612         rm -f $DIR/$tfile
15613         touch $DIR/$tfile
15614
15615         local fid=$($LFS path2fid $DIR/$tfile)
15616         # Open the file
15617         fd=$(free_fd)
15618         cmd="exec $fd<$DIR/$tfile"
15619         eval $cmd
15620         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
15621         echo "$fid_list" | grep "$fid"
15622         rc=$?
15623
15624         cmd="exec $fd>/dev/null"
15625         eval $cmd
15626         if [ $rc -ne 0 ]; then
15627                 error "FID $fid not found in open files list $fid_list"
15628         fi
15629 }
15630 run_test 154d "Verify open file fid"
15631
15632 test_154e()
15633 {
15634         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
15635                 skip "Need MDS version at least 2.6.50"
15636
15637         if ls -a $MOUNT | grep -q '^\.lustre$'; then
15638                 error ".lustre returned by readdir"
15639         fi
15640 }
15641 run_test 154e ".lustre is not returned by readdir"
15642
15643 test_154f() {
15644         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15645
15646         # create parent directory on a single MDT to avoid cross-MDT hardlinks
15647         mkdir_on_mdt0 $DIR/$tdir
15648         # test dirs inherit from its stripe
15649         mkdir -p $DIR/$tdir/foo1 || error "mkdir error"
15650         mkdir -p $DIR/$tdir/foo2 || error "mkdir error"
15651         cp /etc/hosts $DIR/$tdir/foo1/$tfile
15652         ln $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/link
15653         touch $DIR/f
15654
15655         # get fid of parents
15656         local FID0=$($LFS path2fid $DIR/$tdir)
15657         local FID1=$($LFS path2fid $DIR/$tdir/foo1)
15658         local FID2=$($LFS path2fid $DIR/$tdir/foo2)
15659         local FID3=$($LFS path2fid $DIR)
15660
15661         # check that path2fid --parents returns expected <parent_fid>/name
15662         # 1) test for a directory (single parent)
15663         local parent=$($LFS path2fid --parents $DIR/$tdir/foo1)
15664         [ "$parent" == "$FID0/foo1" ] ||
15665                 error "expected parent: $FID0/foo1, got: $parent"
15666
15667         # 2) test for a file with nlink > 1 (multiple parents)
15668         parent=$($LFS path2fid --parents $DIR/$tdir/foo1/$tfile)
15669         echo "$parent" | grep -F "$FID1/$tfile" ||
15670                 error "$FID1/$tfile not returned in parent list"
15671         echo "$parent" | grep -F "$FID2/link" ||
15672                 error "$FID2/link not returned in parent list"
15673
15674         # 3) get parent by fid
15675         local file_fid=$($LFS path2fid $DIR/$tdir/foo1/$tfile)
15676         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15677         echo "$parent" | grep -F "$FID1/$tfile" ||
15678                 error "$FID1/$tfile not returned in parent list (by fid)"
15679         echo "$parent" | grep -F "$FID2/link" ||
15680                 error "$FID2/link not returned in parent list (by fid)"
15681
15682         # 4) test for entry in root directory
15683         parent=$($LFS path2fid --parents $DIR/f)
15684         echo "$parent" | grep -F "$FID3/f" ||
15685                 error "$FID3/f not returned in parent list"
15686
15687         # 5) test it on root directory
15688         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
15689                 error "$MOUNT should not have parents"
15690
15691         # enable xattr caching and check that linkea is correctly updated
15692         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
15693         save_lustre_params client "llite.*.xattr_cache" > $save
15694         lctl set_param llite.*.xattr_cache 1
15695
15696         # 6.1) linkea update on rename
15697         mv $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/$tfile.moved
15698
15699         # get parents by fid
15700         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15701         # foo1 should no longer be returned in parent list
15702         echo "$parent" | grep -F "$FID1" &&
15703                 error "$FID1 should no longer be in parent list"
15704         # the new path should appear
15705         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
15706                 error "$FID2/$tfile.moved is not in parent list"
15707
15708         # 6.2) linkea update on unlink
15709         rm -f $DIR/$tdir/foo2/link
15710         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15711         # foo2/link should no longer be returned in parent list
15712         echo "$parent" | grep -F "$FID2/link" &&
15713                 error "$FID2/link should no longer be in parent list"
15714         true
15715
15716         rm -f $DIR/f
15717         restore_lustre_params < $save
15718         rm -f $save
15719 }
15720 run_test 154f "get parent fids by reading link ea"
15721
15722 test_154g()
15723 {
15724         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15725         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
15726            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
15727                 skip "Need MDS version at least 2.6.92"
15728
15729         mkdir_on_mdt0 $DIR/$tdir
15730         llapi_fid_test -d $DIR/$tdir
15731 }
15732 run_test 154g "various llapi FID tests"
15733
15734 test_155_small_load() {
15735     local temp=$TMP/$tfile
15736     local file=$DIR/$tfile
15737
15738     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
15739         error "dd of=$temp bs=6096 count=1 failed"
15740     cp $temp $file
15741     cancel_lru_locks $OSC
15742     cmp $temp $file || error "$temp $file differ"
15743
15744     $TRUNCATE $temp 6000
15745     $TRUNCATE $file 6000
15746     cmp $temp $file || error "$temp $file differ (truncate1)"
15747
15748     echo "12345" >>$temp
15749     echo "12345" >>$file
15750     cmp $temp $file || error "$temp $file differ (append1)"
15751
15752     echo "12345" >>$temp
15753     echo "12345" >>$file
15754     cmp $temp $file || error "$temp $file differ (append2)"
15755
15756     rm -f $temp $file
15757     true
15758 }
15759
15760 test_155_big_load() {
15761         remote_ost_nodsh && skip "remote OST with nodsh"
15762
15763         local temp=$TMP/$tfile
15764         local file=$DIR/$tfile
15765
15766         free_min_max
15767         local cache_size=$(do_facet ost$((MAXI+1)) \
15768                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
15769
15770         # LU-16042: can not get the cache size on Arm64 VM here, fallback to a
15771         # pre-set value
15772         if [ -z "$cache_size" ]; then
15773                 cache_size=256
15774         fi
15775         local large_file_size=$((cache_size * 2))
15776
15777         echo "OSS cache size: $cache_size KB"
15778         echo "Large file size: $large_file_size KB"
15779
15780         [ $MAXV -le $large_file_size ] &&
15781                 skip_env "max available OST size needs > $large_file_size KB"
15782
15783         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
15784
15785         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
15786                 error "dd of=$temp bs=$large_file_size count=1k failed"
15787         cp $temp $file
15788         ls -lh $temp $file
15789         cancel_lru_locks osc
15790         cmp $temp $file || error "$temp $file differ"
15791
15792         rm -f $temp $file
15793         true
15794 }
15795
15796 save_writethrough() {
15797         local facets=$(get_facets OST)
15798
15799         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
15800 }
15801
15802 test_155a() {
15803         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15804
15805         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15806
15807         save_writethrough $p
15808
15809         set_cache read on
15810         set_cache writethrough on
15811         test_155_small_load
15812         restore_lustre_params < $p
15813         rm -f $p
15814 }
15815 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
15816
15817 test_155b() {
15818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15819
15820         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15821
15822         save_writethrough $p
15823
15824         set_cache read on
15825         set_cache writethrough off
15826         test_155_small_load
15827         restore_lustre_params < $p
15828         rm -f $p
15829 }
15830 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
15831
15832 test_155c() {
15833         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15834
15835         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15836
15837         save_writethrough $p
15838
15839         set_cache read off
15840         set_cache writethrough on
15841         test_155_small_load
15842         restore_lustre_params < $p
15843         rm -f $p
15844 }
15845 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
15846
15847 test_155d() {
15848         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15849
15850         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15851
15852         save_writethrough $p
15853
15854         set_cache read off
15855         set_cache writethrough off
15856         test_155_small_load
15857         restore_lustre_params < $p
15858         rm -f $p
15859 }
15860 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
15861
15862 test_155e() {
15863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15864
15865         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15866
15867         save_writethrough $p
15868
15869         set_cache read on
15870         set_cache writethrough on
15871         test_155_big_load
15872         restore_lustre_params < $p
15873         rm -f $p
15874 }
15875 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
15876
15877 test_155f() {
15878         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15879
15880         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15881
15882         save_writethrough $p
15883
15884         set_cache read on
15885         set_cache writethrough off
15886         test_155_big_load
15887         restore_lustre_params < $p
15888         rm -f $p
15889 }
15890 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
15891
15892 test_155g() {
15893         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15894
15895         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15896
15897         save_writethrough $p
15898
15899         set_cache read off
15900         set_cache writethrough on
15901         test_155_big_load
15902         restore_lustre_params < $p
15903         rm -f $p
15904 }
15905 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
15906
15907 test_155h() {
15908         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15909
15910         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15911
15912         save_writethrough $p
15913
15914         set_cache read off
15915         set_cache writethrough off
15916         test_155_big_load
15917         restore_lustre_params < $p
15918         rm -f $p
15919 }
15920 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
15921
15922 test_156() {
15923         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15924         remote_ost_nodsh && skip "remote OST with nodsh"
15925         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
15926                 skip "stats not implemented on old servers"
15927         [ "$ost1_FSTYPE" = "zfs" ] &&
15928                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
15929
15930         local CPAGES=3
15931         local BEFORE
15932         local AFTER
15933         local file="$DIR/$tfile"
15934         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15935
15936         save_writethrough $p
15937         roc_hit_init
15938
15939         log "Turn on read and write cache"
15940         set_cache read on
15941         set_cache writethrough on
15942
15943         log "Write data and read it back."
15944         log "Read should be satisfied from the cache."
15945         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15946         BEFORE=$(roc_hit)
15947         cancel_lru_locks osc
15948         cat $file >/dev/null
15949         AFTER=$(roc_hit)
15950         if ! let "AFTER - BEFORE == CPAGES"; then
15951                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
15952         else
15953                 log "cache hits: before: $BEFORE, after: $AFTER"
15954         fi
15955
15956         log "Read again; it should be satisfied from the cache."
15957         BEFORE=$AFTER
15958         cancel_lru_locks osc
15959         cat $file >/dev/null
15960         AFTER=$(roc_hit)
15961         if ! let "AFTER - BEFORE == CPAGES"; then
15962                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
15963         else
15964                 log "cache hits:: before: $BEFORE, after: $AFTER"
15965         fi
15966
15967         log "Turn off the read cache and turn on the write cache"
15968         set_cache read off
15969         set_cache writethrough on
15970
15971         log "Read again; it should be satisfied from the cache."
15972         BEFORE=$(roc_hit)
15973         cancel_lru_locks osc
15974         cat $file >/dev/null
15975         AFTER=$(roc_hit)
15976         if ! let "AFTER - BEFORE == CPAGES"; then
15977                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
15978         else
15979                 log "cache hits:: before: $BEFORE, after: $AFTER"
15980         fi
15981
15982         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
15983                 # > 2.12.56 uses pagecache if cached
15984                 log "Read again; it should not be satisfied from the cache."
15985                 BEFORE=$AFTER
15986                 cancel_lru_locks osc
15987                 cat $file >/dev/null
15988                 AFTER=$(roc_hit)
15989                 if ! let "AFTER - BEFORE == 0"; then
15990                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
15991                 else
15992                         log "cache hits:: before: $BEFORE, after: $AFTER"
15993                 fi
15994         fi
15995
15996         log "Write data and read it back."
15997         log "Read should be satisfied from the cache."
15998         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
15999         BEFORE=$(roc_hit)
16000         cancel_lru_locks osc
16001         cat $file >/dev/null
16002         AFTER=$(roc_hit)
16003         if ! let "AFTER - BEFORE == CPAGES"; then
16004                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
16005         else
16006                 log "cache hits:: before: $BEFORE, after: $AFTER"
16007         fi
16008
16009         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
16010                 # > 2.12.56 uses pagecache if cached
16011                 log "Read again; it should not be satisfied from the cache."
16012                 BEFORE=$AFTER
16013                 cancel_lru_locks osc
16014                 cat $file >/dev/null
16015                 AFTER=$(roc_hit)
16016                 if ! let "AFTER - BEFORE == 0"; then
16017                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
16018                 else
16019                         log "cache hits:: before: $BEFORE, after: $AFTER"
16020                 fi
16021         fi
16022
16023         log "Turn off read and write cache"
16024         set_cache read off
16025         set_cache writethrough off
16026
16027         log "Write data and read it back"
16028         log "It should not be satisfied from the cache."
16029         rm -f $file
16030         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16031         cancel_lru_locks osc
16032         BEFORE=$(roc_hit)
16033         cat $file >/dev/null
16034         AFTER=$(roc_hit)
16035         if ! let "AFTER - BEFORE == 0"; then
16036                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
16037         else
16038                 log "cache hits:: before: $BEFORE, after: $AFTER"
16039         fi
16040
16041         log "Turn on the read cache and turn off the write cache"
16042         set_cache read on
16043         set_cache writethrough off
16044
16045         log "Write data and read it back"
16046         log "It should not be satisfied from the cache."
16047         rm -f $file
16048         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16049         BEFORE=$(roc_hit)
16050         cancel_lru_locks osc
16051         cat $file >/dev/null
16052         AFTER=$(roc_hit)
16053         if ! let "AFTER - BEFORE == 0"; then
16054                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
16055         else
16056                 log "cache hits:: before: $BEFORE, after: $AFTER"
16057         fi
16058
16059         log "Read again; it should be satisfied from the cache."
16060         BEFORE=$(roc_hit)
16061         cancel_lru_locks osc
16062         cat $file >/dev/null
16063         AFTER=$(roc_hit)
16064         if ! let "AFTER - BEFORE == CPAGES"; then
16065                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
16066         else
16067                 log "cache hits:: before: $BEFORE, after: $AFTER"
16068         fi
16069
16070         restore_lustre_params < $p
16071         rm -f $p $file
16072 }
16073 run_test 156 "Verification of tunables"
16074
16075 test_160a() {
16076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16077         remote_mds_nodsh && skip "remote MDS with nodsh"
16078         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
16079                 skip "Need MDS version at least 2.2.0"
16080
16081         changelog_register || error "changelog_register failed"
16082         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16083         changelog_users $SINGLEMDS | grep -q $cl_user ||
16084                 error "User $cl_user not found in changelog_users"
16085
16086         mkdir_on_mdt0 $DIR/$tdir
16087
16088         # change something
16089         test_mkdir -p $DIR/$tdir/pics/2008/zachy
16090         changelog_clear 0 || error "changelog_clear failed"
16091         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
16092         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
16093         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16094         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16095         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16096         rm $DIR/$tdir/pics/desktop.jpg
16097
16098         echo "verifying changelog mask"
16099         changelog_chmask "-MKDIR"
16100         changelog_chmask "-CLOSE"
16101
16102         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
16103         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
16104
16105         changelog_chmask "+MKDIR"
16106         changelog_chmask "+CLOSE"
16107
16108         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
16109         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
16110
16111         MKDIRS=$(changelog_dump | grep -c "MKDIR")
16112         CLOSES=$(changelog_dump | grep -c "CLOSE")
16113         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
16114         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
16115
16116         # verify contents
16117         echo "verifying target fid"
16118         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
16119         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
16120         [ "$fidc" == "$fidf" ] ||
16121                 error "changelog '$tfile' fid $fidc != file fid $fidf"
16122         echo "verifying parent fid"
16123         # The FID returned from the Changelog may be the directory shard on
16124         # a different MDT, and not the FID returned by path2fid on the parent.
16125         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
16126         # since this is what will matter when recreating this file in the tree.
16127         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
16128         local pathp=$($LFS fid2path $MOUNT "$fidp")
16129         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
16130                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
16131
16132         echo "getting records for $cl_user"
16133         changelog_users $SINGLEMDS
16134         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
16135         local nclr=3
16136         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
16137                 error "changelog_clear failed"
16138         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
16139         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
16140         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
16141                 error "user index expect $user_rec1 + $nclr != $user_rec2"
16142
16143         local min0_rec=$(changelog_users $SINGLEMDS |
16144                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
16145         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
16146                           awk '{ print $1; exit; }')
16147
16148         changelog_dump | tail -n 5
16149         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
16150         [ $first_rec == $((min0_rec + 1)) ] ||
16151                 error "first index should be $min0_rec + 1 not $first_rec"
16152
16153         # LU-3446 changelog index reset on MDT restart
16154         local cur_rec1=$(changelog_users $SINGLEMDS |
16155                          awk '/^current.index:/ { print $NF }')
16156         changelog_clear 0 ||
16157                 error "clear all changelog records for $cl_user failed"
16158         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
16159         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
16160                 error "Fail to start $SINGLEMDS"
16161         local cur_rec2=$(changelog_users $SINGLEMDS |
16162                          awk '/^current.index:/ { print $NF }')
16163         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
16164         [ $cur_rec1 == $cur_rec2 ] ||
16165                 error "current index should be $cur_rec1 not $cur_rec2"
16166
16167         echo "verifying users from this test are deregistered"
16168         changelog_deregister || error "changelog_deregister failed"
16169         changelog_users $SINGLEMDS | grep -q $cl_user &&
16170                 error "User '$cl_user' still in changelog_users"
16171
16172         # lctl get_param -n mdd.*.changelog_users
16173         # current_index: 144
16174         # ID    index (idle seconds)
16175         # cl3   144   (2) mask=<list>
16176         if [ -z "$(changelog_users $SINGLEMDS | grep -v current.index)" ]; then
16177                 # this is the normal case where all users were deregistered
16178                 # make sure no new records are added when no users are present
16179                 local last_rec1=$(changelog_users $SINGLEMDS |
16180                                   awk '/^current.index:/ { print $NF }')
16181                 touch $DIR/$tdir/chloe
16182                 local last_rec2=$(changelog_users $SINGLEMDS |
16183                                   awk '/^current.index:/ { print $NF }')
16184                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
16185                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
16186         else
16187                 # any changelog users must be leftovers from a previous test
16188                 changelog_users $SINGLEMDS
16189                 echo "other changelog users; can't verify off"
16190         fi
16191 }
16192 run_test 160a "changelog sanity"
16193
16194 test_160b() { # LU-3587
16195         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16196         remote_mds_nodsh && skip "remote MDS with nodsh"
16197         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
16198                 skip "Need MDS version at least 2.2.0"
16199
16200         changelog_register || error "changelog_register failed"
16201         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16202         changelog_users $SINGLEMDS | grep -q $cl_user ||
16203                 error "User '$cl_user' not found in changelog_users"
16204
16205         local longname1=$(str_repeat a 255)
16206         local longname2=$(str_repeat b 255)
16207
16208         cd $DIR
16209         echo "creating very long named file"
16210         touch $longname1 || error "create of '$longname1' failed"
16211         echo "renaming very long named file"
16212         mv $longname1 $longname2
16213
16214         changelog_dump | grep RENME | tail -n 5
16215         rm -f $longname2
16216 }
16217 run_test 160b "Verify that very long rename doesn't crash in changelog"
16218
16219 test_160c() {
16220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16221         remote_mds_nodsh && skip "remote MDS with nodsh"
16222
16223         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16224                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
16225                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
16226                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
16227
16228         local rc=0
16229
16230         # Registration step
16231         changelog_register || error "changelog_register failed"
16232
16233         rm -rf $DIR/$tdir
16234         mkdir -p $DIR/$tdir
16235         $MCREATE $DIR/$tdir/foo_160c
16236         changelog_chmask "-TRUNC"
16237         $TRUNCATE $DIR/$tdir/foo_160c 200
16238         changelog_chmask "+TRUNC"
16239         $TRUNCATE $DIR/$tdir/foo_160c 199
16240         changelog_dump | tail -n 5
16241         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
16242         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
16243 }
16244 run_test 160c "verify that changelog log catch the truncate event"
16245
16246 test_160d() {
16247         remote_mds_nodsh && skip "remote MDS with nodsh"
16248         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16249         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16250         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
16251                 skip "Need MDS version at least 2.7.60"
16252
16253         # Registration step
16254         changelog_register || error "changelog_register failed"
16255
16256         mkdir -p $DIR/$tdir/migrate_dir
16257         changelog_clear 0 || error "changelog_clear failed"
16258
16259         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
16260         changelog_dump | tail -n 5
16261         local migrates=$(changelog_dump | grep -c "MIGRT")
16262         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
16263 }
16264 run_test 160d "verify that changelog log catch the migrate event"
16265
16266 test_160e() {
16267         remote_mds_nodsh && skip "remote MDS with nodsh"
16268
16269         # Create a user
16270         changelog_register || error "changelog_register failed"
16271
16272         local MDT0=$(facet_svc $SINGLEMDS)
16273         local rc
16274
16275         # No user (expect fail)
16276         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister
16277         rc=$?
16278         if [ $rc -eq 0 ]; then
16279                 error "Should fail without user"
16280         elif [ $rc -ne 4 ]; then
16281                 error "changelog_deregister failed with $rc, expect 4(CMD_HELP)"
16282         fi
16283
16284         # Delete a future user (expect fail)
16285         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
16286         rc=$?
16287         if [ $rc -eq 0 ]; then
16288                 error "Deleted non-existant user cl77"
16289         elif [ $rc -ne 2 ]; then
16290                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
16291         fi
16292
16293         # Clear to a bad index (1 billion should be safe)
16294         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
16295         rc=$?
16296
16297         if [ $rc -eq 0 ]; then
16298                 error "Successfully cleared to invalid CL index"
16299         elif [ $rc -ne 22 ]; then
16300                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
16301         fi
16302 }
16303 run_test 160e "changelog negative testing (should return errors)"
16304
16305 test_160f() {
16306         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16307         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16308                 skip "Need MDS version at least 2.10.56"
16309
16310         local mdts=$(comma_list $(mdts_nodes))
16311
16312         # Create a user
16313         changelog_register || error "first changelog_register failed"
16314         changelog_register || error "second changelog_register failed"
16315         local cl_users
16316         declare -A cl_user1
16317         declare -A cl_user2
16318         local user_rec1
16319         local user_rec2
16320         local i
16321
16322         # generate some changelog records to accumulate on each MDT
16323         # use all_char because created files should be evenly distributed
16324         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16325                 error "test_mkdir $tdir failed"
16326         log "$(date +%s): creating first files"
16327         for ((i = 0; i < MDSCOUNT * 2; i++)); do
16328                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT)) ||
16329                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT)) failed"
16330         done
16331
16332         # check changelogs have been generated
16333         local start=$SECONDS
16334         local idle_time=$((MDSCOUNT * 5 + 5))
16335         local nbcl=$(changelog_dump | wc -l)
16336         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16337
16338         for param in "changelog_max_idle_time=$idle_time" \
16339                      "changelog_gc=1" \
16340                      "changelog_min_gc_interval=2" \
16341                      "changelog_min_free_cat_entries=3"; do
16342                 local MDT0=$(facet_svc $SINGLEMDS)
16343                 local var="${param%=*}"
16344                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16345
16346                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16347                 do_nodes $mdts $LCTL set_param mdd.*.$param
16348         done
16349
16350         # force cl_user2 to be idle (1st part), but also cancel the
16351         # cl_user1 records so that it is not evicted later in the test.
16352         local sleep1=$((idle_time / 2))
16353         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
16354         sleep $sleep1
16355
16356         # simulate changelog catalog almost full
16357         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
16358         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
16359
16360         for i in $(seq $MDSCOUNT); do
16361                 cl_users=(${CL_USERS[mds$i]})
16362                 cl_user1[mds$i]="${cl_users[0]}"
16363                 cl_user2[mds$i]="${cl_users[1]}"
16364
16365                 [ -n "${cl_user1[mds$i]}" ] ||
16366                         error "mds$i: no user registered"
16367                 [ -n "${cl_user2[mds$i]}" ] ||
16368                         error "mds$i: only ${cl_user2[mds$i]} is registered"
16369
16370                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16371                 [ -n "$user_rec1" ] ||
16372                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16373                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16374                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16375                 [ -n "$user_rec2" ] ||
16376                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16377                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16378                      "$user_rec1 + 2 == $user_rec2"
16379                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16380                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16381                               "$user_rec1 + 2, but is $user_rec2"
16382                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16383                 [ -n "$user_rec2" ] ||
16384                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16385                 [ $user_rec1 == $user_rec2 ] ||
16386                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16387                               "$user_rec1, but is $user_rec2"
16388         done
16389
16390         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
16391         local sleep2=$((idle_time - (SECONDS - start) + 1))
16392         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
16393         sleep $sleep2
16394
16395         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
16396         # cl_user1 should be OK because it recently processed records.
16397         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
16398         for ((i = 0; i < MDSCOUNT * 2; i++)); do
16399                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT+2))||
16400                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT+2)) failed"
16401         done
16402
16403         # ensure gc thread is done
16404         for i in $(mdts_nodes); do
16405                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
16406                         error "$i: GC-thread not done"
16407         done
16408
16409         local first_rec
16410         for (( i = 1; i <= MDSCOUNT; i++ )); do
16411                 # check cl_user1 still registered
16412                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16413                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16414                 # check cl_user2 unregistered
16415                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16416                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16417
16418                 # check changelogs are present and starting at $user_rec1 + 1
16419                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16420                 [ -n "$user_rec1" ] ||
16421                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16422                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16423                             awk '{ print $1; exit; }')
16424
16425                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
16426                 [ $((user_rec1 + 1)) == $first_rec ] ||
16427                         error "mds$i: rec $first_rec != $user_rec1 + 1"
16428         done
16429 }
16430 run_test 160f "changelog garbage collect (timestamped users)"
16431
16432 test_160g() {
16433         remote_mds_nodsh && skip "remote MDS with nodsh"
16434         [[ $MDS1_VERSION -ge $(version_code 2.14.55) ]] ||
16435                 skip "Need MDS version at least 2.14.55"
16436
16437         local mdts=$(comma_list $(mdts_nodes))
16438
16439         # Create a user
16440         changelog_register || error "first changelog_register failed"
16441         changelog_register || error "second changelog_register failed"
16442         local cl_users
16443         declare -A cl_user1
16444         declare -A cl_user2
16445         local user_rec1
16446         local user_rec2
16447         local i
16448
16449         # generate some changelog records to accumulate on each MDT
16450         # use all_char because created files should be evenly distributed
16451         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16452                 error "test_mkdir $tdir failed"
16453         for ((i = 0; i < MDSCOUNT; i++)); do
16454                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16455                         error "create $DIR/$tdir/d$i.1 failed"
16456         done
16457
16458         # check changelogs have been generated
16459         local nbcl=$(changelog_dump | wc -l)
16460         (( $nbcl > 0 )) || error "no changelogs found"
16461
16462         # reduce the max_idle_indexes value to make sure we exceed it
16463         for param in "changelog_max_idle_indexes=2" \
16464                      "changelog_gc=1" \
16465                      "changelog_min_gc_interval=2"; do
16466                 local MDT0=$(facet_svc $SINGLEMDS)
16467                 local var="${param%=*}"
16468                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16469
16470                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16471                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
16472                         error "unable to set mdd.*.$param"
16473         done
16474
16475         local start=$SECONDS
16476         for i in $(seq $MDSCOUNT); do
16477                 cl_users=(${CL_USERS[mds$i]})
16478                 cl_user1[mds$i]="${cl_users[0]}"
16479                 cl_user2[mds$i]="${cl_users[1]}"
16480
16481                 [ -n "${cl_user1[mds$i]}" ] ||
16482                         error "mds$i: user1 is not registered"
16483                 [ -n "${cl_user2[mds$i]}" ] ||
16484                         error "mds$i: only ${cl_user1[mds$i]} is registered"
16485
16486                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16487                 [ -n "$user_rec1" ] ||
16488                         error "mds$i: user1 ${cl_user1[mds$i]} not found"
16489                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16490                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16491                 [ -n "$user_rec2" ] ||
16492                         error "mds$i: user1 ${cl_user1[mds$i]} not found (2)"
16493                 echo "mds$i: verifying user1 ${cl_user1[mds$i]} clear: " \
16494                      "$user_rec1 + 2 == $user_rec2"
16495                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16496                         error "mds$i: user1 ${cl_user1[mds$i]} index " \
16497                               "expected $user_rec1 + 2, but is $user_rec2"
16498                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16499                 [ -n "$user_rec2" ] ||
16500                         error "mds$i: user2 ${cl_user2[mds$i]} not found"
16501                 [ $user_rec1 == $user_rec2 ] ||
16502                         error "mds$i: user2 ${cl_user2[mds$i]} index " \
16503                               "expected $user_rec1, but is $user_rec2"
16504         done
16505
16506         # ensure we are past the previous changelog_min_gc_interval set above
16507         local sleep2=$((start + 2 - SECONDS))
16508         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
16509         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
16510         # cl_user1 should be OK because it recently processed records.
16511         for ((i = 0; i < MDSCOUNT; i++)); do
16512                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 ||
16513                         error "create $DIR/$tdir/d$i.3 failed"
16514         done
16515
16516         # ensure gc thread is done
16517         for i in $(mdts_nodes); do
16518                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
16519                         error "$i: GC-thread not done"
16520         done
16521
16522         local first_rec
16523         for (( i = 1; i <= MDSCOUNT; i++ )); do
16524                 # check cl_user1 still registered
16525                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16526                         error "mds$i: user1 ${cl_user1[mds$i]} not found (3)"
16527                 # check cl_user2 unregistered
16528                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16529                         error "mds$i: user2 ${cl_user2[mds$i]} is registered"
16530
16531                 # check changelogs are present and starting at $user_rec1 + 1
16532                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16533                 [ -n "$user_rec1" ] ||
16534                         error "mds$i: user1 ${cl_user1[mds$i]} not found (4)"
16535                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16536                             awk '{ print $1; exit; }')
16537
16538                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
16539                 [ $((user_rec1 + 1)) == $first_rec ] ||
16540                         error "mds$i: rec $first_rec != $user_rec1 + 1"
16541         done
16542 }
16543 run_test 160g "changelog garbage collect on idle records"
16544
16545 test_160h() {
16546         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16547         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16548                 skip "Need MDS version at least 2.10.56"
16549
16550         local mdts=$(comma_list $(mdts_nodes))
16551
16552         # Create a user
16553         changelog_register || error "first changelog_register failed"
16554         changelog_register || error "second changelog_register failed"
16555         local cl_users
16556         declare -A cl_user1
16557         declare -A cl_user2
16558         local user_rec1
16559         local user_rec2
16560         local i
16561
16562         # generate some changelog records to accumulate on each MDT
16563         # use all_char because created files should be evenly distributed
16564         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16565                 error "test_mkdir $tdir failed"
16566         for ((i = 0; i < MDSCOUNT; i++)); do
16567                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16568                         error "create $DIR/$tdir/d$i.1 failed"
16569         done
16570
16571         # check changelogs have been generated
16572         local nbcl=$(changelog_dump | wc -l)
16573         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16574
16575         for param in "changelog_max_idle_time=10" \
16576                      "changelog_gc=1" \
16577                      "changelog_min_gc_interval=2"; do
16578                 local MDT0=$(facet_svc $SINGLEMDS)
16579                 local var="${param%=*}"
16580                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16581
16582                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16583                 do_nodes $mdts $LCTL set_param mdd.*.$param
16584         done
16585
16586         # force cl_user2 to be idle (1st part)
16587         sleep 9
16588
16589         for i in $(seq $MDSCOUNT); do
16590                 cl_users=(${CL_USERS[mds$i]})
16591                 cl_user1[mds$i]="${cl_users[0]}"
16592                 cl_user2[mds$i]="${cl_users[1]}"
16593
16594                 [ -n "${cl_user1[mds$i]}" ] ||
16595                         error "mds$i: no user registered"
16596                 [ -n "${cl_user2[mds$i]}" ] ||
16597                         error "mds$i: only ${cl_user2[mds$i]} is registered"
16598
16599                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16600                 [ -n "$user_rec1" ] ||
16601                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16602                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16603                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16604                 [ -n "$user_rec2" ] ||
16605                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16606                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16607                      "$user_rec1 + 2 == $user_rec2"
16608                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16609                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16610                               "$user_rec1 + 2, but is $user_rec2"
16611                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16612                 [ -n "$user_rec2" ] ||
16613                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16614                 [ $user_rec1 == $user_rec2 ] ||
16615                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16616                               "$user_rec1, but is $user_rec2"
16617         done
16618
16619         # force cl_user2 to be idle (2nd part) and to reach
16620         # changelog_max_idle_time
16621         sleep 2
16622
16623         # force each GC-thread start and block then
16624         # one per MDT/MDD, set fail_val accordingly
16625         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
16626         do_nodes $mdts $LCTL set_param fail_loc=0x1316
16627
16628         # generate more changelogs to trigger fail_loc
16629         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16630                 error "create $DIR/$tdir/${tfile}bis failed"
16631
16632         # stop MDT to stop GC-thread, should be done in back-ground as it will
16633         # block waiting for the thread to be released and exit
16634         declare -A stop_pids
16635         for i in $(seq $MDSCOUNT); do
16636                 stop mds$i &
16637                 stop_pids[mds$i]=$!
16638         done
16639
16640         for i in $(mdts_nodes); do
16641                 local facet
16642                 local nb=0
16643                 local facets=$(facets_up_on_host $i)
16644
16645                 for facet in ${facets//,/ }; do
16646                         if [[ $facet == mds* ]]; then
16647                                 nb=$((nb + 1))
16648                         fi
16649                 done
16650                 # ensure each MDS's gc threads are still present and all in "R"
16651                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
16652                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
16653                         error "$i: expected $nb GC-thread"
16654                 wait_update $i \
16655                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
16656                         "R" 20 ||
16657                         error "$i: GC-thread not found in R-state"
16658                 # check umounts of each MDT on MDS have reached kthread_stop()
16659                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
16660                         error "$i: expected $nb umount"
16661                 wait_update $i \
16662                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
16663                         error "$i: umount not found in D-state"
16664         done
16665
16666         # release all GC-threads
16667         do_nodes $mdts $LCTL set_param fail_loc=0
16668
16669         # wait for MDT stop to complete
16670         for i in $(seq $MDSCOUNT); do
16671                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
16672         done
16673
16674         # XXX
16675         # may try to check if any orphan changelog records are present
16676         # via ldiskfs/zfs and llog_reader...
16677
16678         # re-start/mount MDTs
16679         for i in $(seq $MDSCOUNT); do
16680                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
16681                         error "Fail to start mds$i"
16682         done
16683
16684         local first_rec
16685         for i in $(seq $MDSCOUNT); do
16686                 # check cl_user1 still registered
16687                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16688                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16689                 # check cl_user2 unregistered
16690                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16691                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16692
16693                 # check changelogs are present and starting at $user_rec1 + 1
16694                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16695                 [ -n "$user_rec1" ] ||
16696                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16697                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16698                             awk '{ print $1; exit; }')
16699
16700                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
16701                 [ $((user_rec1 + 1)) == $first_rec ] ||
16702                         error "mds$i: first index should be $user_rec1 + 1, " \
16703                               "but is $first_rec"
16704         done
16705 }
16706 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
16707               "during mount"
16708
16709 test_160i() {
16710
16711         local mdts=$(comma_list $(mdts_nodes))
16712
16713         changelog_register || error "first changelog_register failed"
16714
16715         # generate some changelog records to accumulate on each MDT
16716         # use all_char because created files should be evenly distributed
16717         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16718                 error "test_mkdir $tdir failed"
16719         for ((i = 0; i < MDSCOUNT; i++)); do
16720                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16721                         error "create $DIR/$tdir/d$i.1 failed"
16722         done
16723
16724         # check changelogs have been generated
16725         local nbcl=$(changelog_dump | wc -l)
16726         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16727
16728         # simulate race between register and unregister
16729         # XXX as fail_loc is set per-MDS, with DNE configs the race
16730         # simulation will only occur for one MDT per MDS and for the
16731         # others the normal race scenario will take place
16732         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
16733         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
16734         do_nodes $mdts $LCTL set_param fail_val=1
16735
16736         # unregister 1st user
16737         changelog_deregister &
16738         local pid1=$!
16739         # wait some time for deregister work to reach race rdv
16740         sleep 2
16741         # register 2nd user
16742         changelog_register || error "2nd user register failed"
16743
16744         wait $pid1 || error "1st user deregister failed"
16745
16746         local i
16747         local last_rec
16748         declare -A LAST_REC
16749         for i in $(seq $MDSCOUNT); do
16750                 if changelog_users mds$i | grep "^cl"; then
16751                         # make sure new records are added with one user present
16752                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
16753                                           awk '/^current.index:/ { print $NF }')
16754                 else
16755                         error "mds$i has no user registered"
16756                 fi
16757         done
16758
16759         # generate more changelog records to accumulate on each MDT
16760         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16761                 error "create $DIR/$tdir/${tfile}bis failed"
16762
16763         for i in $(seq $MDSCOUNT); do
16764                 last_rec=$(changelog_users $SINGLEMDS |
16765                            awk '/^current.index:/ { print $NF }')
16766                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
16767                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
16768                         error "changelogs are off on mds$i"
16769         done
16770 }
16771 run_test 160i "changelog user register/unregister race"
16772
16773 test_160j() {
16774         remote_mds_nodsh && skip "remote MDS with nodsh"
16775         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
16776                 skip "Need MDS version at least 2.12.56"
16777
16778         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
16779         stack_trap "umount $MOUNT2" EXIT
16780
16781         changelog_register || error "first changelog_register failed"
16782         stack_trap "changelog_deregister" EXIT
16783
16784         # generate some changelog
16785         # use all_char because created files should be evenly distributed
16786         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16787                 error "mkdir $tdir failed"
16788         for ((i = 0; i < MDSCOUNT; i++)); do
16789                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16790                         error "create $DIR/$tdir/d$i.1 failed"
16791         done
16792
16793         # open the changelog device
16794         exec 3>/dev/changelog-$FSNAME-MDT0000
16795         stack_trap "exec 3>&-" EXIT
16796         exec 4</dev/changelog-$FSNAME-MDT0000
16797         stack_trap "exec 4<&-" EXIT
16798
16799         # umount the first lustre mount
16800         umount $MOUNT
16801         stack_trap "mount_client $MOUNT" EXIT
16802
16803         # read changelog, which may or may not fail, but should not crash
16804         cat <&4 >/dev/null
16805
16806         # clear changelog
16807         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16808         changelog_users $SINGLEMDS | grep -q $cl_user ||
16809                 error "User $cl_user not found in changelog_users"
16810
16811         printf 'clear:'$cl_user':0' >&3
16812 }
16813 run_test 160j "client can be umounted while its chanangelog is being used"
16814
16815 test_160k() {
16816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16817         remote_mds_nodsh && skip "remote MDS with nodsh"
16818
16819         mkdir -p $DIR/$tdir/1/1
16820
16821         changelog_register || error "changelog_register failed"
16822         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16823
16824         changelog_users $SINGLEMDS | grep -q $cl_user ||
16825                 error "User '$cl_user' not found in changelog_users"
16826 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
16827         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
16828         rmdir $DIR/$tdir/1/1 & sleep 1
16829         mkdir $DIR/$tdir/2
16830         touch $DIR/$tdir/2/2
16831         rm -rf $DIR/$tdir/2
16832
16833         wait
16834         sleep 4
16835
16836         changelog_dump | grep rmdir || error "rmdir not recorded"
16837 }
16838 run_test 160k "Verify that changelog records are not lost"
16839
16840 # Verifies that a file passed as a parameter has recently had an operation
16841 # performed on it that has generated an MTIME changelog which contains the
16842 # correct parent FID. As files might reside on a different MDT from the
16843 # parent directory in DNE configurations, the FIDs are translated to paths
16844 # before being compared, which should be identical
16845 compare_mtime_changelog() {
16846         local file="${1}"
16847         local mdtidx
16848         local mtime
16849         local cl_fid
16850         local pdir
16851         local dir
16852
16853         mdtidx=$($LFS getstripe --mdt-index $file)
16854         mdtidx=$(printf "%04x" $mdtidx)
16855
16856         # Obtain the parent FID from the MTIME changelog
16857         mtime=$($LFS changelog $FSNAME-MDT$mdtidx | tail -n 1 | grep MTIME)
16858         [ -z "$mtime" ] && error "MTIME changelog not recorded"
16859
16860         cl_fid=$(sed -e 's/.* p=//' -e 's/ .*//' <<<$mtime)
16861         [ -z "$cl_fid" ] && error "parent FID not present"
16862
16863         # Verify that the path for the parent FID is the same as the path for
16864         # the test directory
16865         pdir=$($LFS fid2path $MOUNT "$cl_fid")
16866
16867         dir=$(dirname $1)
16868
16869         [[ "${pdir%/}" == "$dir" ]] ||
16870                 error "MTIME changelog parent FID is wrong, expected $dir, got $pdir"
16871 }
16872
16873 test_160l() {
16874         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16875
16876         remote_mds_nodsh && skip "remote MDS with nodsh"
16877         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
16878                 skip "Need MDS version at least 2.13.55"
16879
16880         local cl_user
16881
16882         changelog_register || error "changelog_register failed"
16883         cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16884
16885         changelog_users $SINGLEMDS | grep -q $cl_user ||
16886                 error "User '$cl_user' not found in changelog_users"
16887
16888         # Clear some types so that MTIME changelogs are generated
16889         changelog_chmask "-CREAT"
16890         changelog_chmask "-CLOSE"
16891
16892         test_mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
16893
16894         # Test CL_MTIME during setattr
16895         touch $DIR/$tdir/$tfile
16896         compare_mtime_changelog $DIR/$tdir/$tfile
16897
16898         # Test CL_MTIME during close
16899         $MULTIOP $DIR/$tdir/${tfile}_2 O_2w4096c || error "multiop failed"
16900         compare_mtime_changelog $DIR/$tdir/${tfile}_2
16901 }
16902 run_test 160l "Verify that MTIME changelog records contain the parent FID"
16903
16904 test_160m() {
16905         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16906         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16907                 skip "Need MDS version at least 2.14.51"
16908         local cl_users
16909         local cl_user1
16910         local cl_user2
16911         local pid1
16912
16913         # Create a user
16914         changelog_register || error "first changelog_register failed"
16915         changelog_register || error "second changelog_register failed"
16916
16917         cl_users=(${CL_USERS[mds1]})
16918         cl_user1="${cl_users[0]}"
16919         cl_user2="${cl_users[1]}"
16920         # generate some changelog records to accumulate on MDT0
16921         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16922         createmany -m $DIR/$tdir/$tfile 50 ||
16923                 error "create $DIR/$tdir/$tfile failed"
16924         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
16925         rm -f $DIR/$tdir
16926
16927         # check changelogs have been generated
16928         local nbcl=$(changelog_dump | wc -l)
16929         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16930
16931 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
16932         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
16933
16934         __changelog_clear mds1 $cl_user1 +10
16935         __changelog_clear mds1 $cl_user2 0 &
16936         pid1=$!
16937         sleep 2
16938         __changelog_clear mds1 $cl_user1 0 ||
16939                 error "fail to cancel record for $cl_user1"
16940         wait $pid1
16941         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16942 }
16943 run_test 160m "Changelog clear race"
16944
16945 test_160n() {
16946         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16947         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
16948                 skip "Need MDS version at least 2.14.51"
16949         local cl_users
16950         local cl_user1
16951         local cl_user2
16952         local pid1
16953         local first_rec
16954         local last_rec=0
16955
16956         # Create a user
16957         changelog_register || error "first changelog_register failed"
16958
16959         cl_users=(${CL_USERS[mds1]})
16960         cl_user1="${cl_users[0]}"
16961
16962         # generate some changelog records to accumulate on MDT0
16963         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
16964         first_rec=$(changelog_users $SINGLEMDS |
16965                         awk '/^current.index:/ { print $NF }')
16966         while (( last_rec < (( first_rec + 65000)) )); do
16967                 createmany -m $DIR/$tdir/$tfile 10000 ||
16968                         error "create $DIR/$tdir/$tfile failed"
16969
16970                 for i in $(seq 0 10000); do
16971                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
16972                                 > /dev/null
16973                 done
16974
16975                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
16976                         error "unlinkmany failed unlink"
16977                 last_rec=$(changelog_users $SINGLEMDS |
16978                         awk '/^current.index:/ { print $NF }')
16979                 echo last record $last_rec
16980                 (( last_rec == 0 )) && error "no changelog found"
16981         done
16982
16983 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
16984         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
16985
16986         __changelog_clear mds1 $cl_user1 0 &
16987         pid1=$!
16988         sleep 2
16989         __changelog_clear mds1 $cl_user1 0 ||
16990                 error "fail to cancel record for $cl_user1"
16991         wait $pid1
16992         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
16993 }
16994 run_test 160n "Changelog destroy race"
16995
16996 test_160o() {
16997         local mdt="$(facet_svc $SINGLEMDS)"
16998
16999         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
17000         remote_mds_nodsh && skip "remote MDS with nodsh"
17001         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
17002                 skip "Need MDS version at least 2.14.52"
17003
17004         changelog_register --user test_160o -m unlnk+close+open ||
17005                 error "changelog_register failed"
17006
17007         do_facet $SINGLEMDS $LCTL --device $mdt \
17008                                 changelog_register -u "Tt3_-#" &&
17009                 error "bad symbols in name should fail"
17010
17011         do_facet $SINGLEMDS $LCTL --device $mdt \
17012                                 changelog_register -u test_160o &&
17013                 error "the same name registration should fail"
17014
17015         do_facet $SINGLEMDS $LCTL --device $mdt \
17016                         changelog_register -u test_160toolongname &&
17017                 error "too long name registration should fail"
17018
17019         changelog_chmask "MARK+HSM"
17020         lctl get_param mdd.*.changelog*mask
17021         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
17022         changelog_users $SINGLEMDS | grep -q $cl_user ||
17023                 error "User $cl_user not found in changelog_users"
17024         #verify username
17025         echo $cl_user | grep -q test_160o ||
17026                 error "User $cl_user has no specific name 'test160o'"
17027
17028         # change something
17029         changelog_clear 0 || error "changelog_clear failed"
17030         # generate some changelog records to accumulate on MDT0
17031         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17032         touch $DIR/$tdir/$tfile                 # open 1
17033
17034         OPENS=$(changelog_dump | grep -c "OPEN")
17035         [[ $OPENS -eq 1 ]] || error "OPEN changelog mask count $OPENS != 1"
17036
17037         # must be no MKDIR it wasn't set as user mask
17038         MKDIR=$(changelog_dump | grep -c "MKDIR")
17039         [[ $MKDIR -eq 0 ]] || error "MKDIR changelog mask found $MKDIR > 0"
17040
17041         oldmask=$(do_facet $SINGLEMDS $LCTL get_param \
17042                                 mdd.$mdt.changelog_current_mask -n)
17043         # register maskless user
17044         changelog_register || error "changelog_register failed"
17045         # effective mask should be not changed because it is not minimal
17046         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17047                                 mdd.$mdt.changelog_current_mask -n)
17048         [[ $mask == $oldmask ]] || error "mask was changed: $mask vs $oldmask"
17049         # set server mask to minimal value
17050         changelog_chmask "MARK"
17051         # check effective mask again, should be treated as DEFMASK now
17052         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17053                                 mdd.$mdt.changelog_current_mask -n)
17054         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
17055
17056         do_facet $SINGLEMDS $LCTL --device $mdt \
17057                                 changelog_deregister -u test_160o ||
17058                 error "cannot deregister by name"
17059 }
17060 run_test 160o "changelog user name and mask"
17061
17062 test_160p() {
17063         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17064         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17065                 skip "Need MDS version at least 2.14.51"
17066         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
17067         local cl_users
17068         local cl_user1
17069         local entry_count
17070
17071         # Create a user
17072         changelog_register || error "first changelog_register failed"
17073
17074         cl_users=(${CL_USERS[mds1]})
17075         cl_user1="${cl_users[0]}"
17076
17077         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17078         createmany -m $DIR/$tdir/$tfile 50 ||
17079                 error "create $DIR/$tdir/$tfile failed"
17080         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
17081         rm -rf $DIR/$tdir
17082
17083         # check changelogs have been generated
17084         entry_count=$(changelog_dump | wc -l)
17085         ((entry_count != 0)) || error "no changelog entries found"
17086
17087         # remove changelog_users and check that orphan entries are removed
17088         stop mds1
17089         local dev=$(mdsdevname 1)
17090         do_facet mds1 "$DEBUGFS -w -R 'rm changelog_users' $dev"
17091         start mds1 $dev $MDS_MOUNT_OPTS || error "cannot start mds1"
17092         entry_count=$(changelog_dump | wc -l)
17093         ((entry_count == 0)) ||
17094                 error "found $entry_count changelog entries, expected none"
17095 }
17096 run_test 160p "Changelog orphan cleanup with no users"
17097
17098 test_160q() {
17099         local mdt="$(facet_svc $SINGLEMDS)"
17100         local clu
17101
17102         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
17103         remote_mds_nodsh && skip "remote MDS with nodsh"
17104         [ $MDS1_VERSION -ge $(version_code 2.14.54) ] ||
17105                 skip "Need MDS version at least 2.14.54"
17106
17107         # set server mask to minimal value like server init does
17108         changelog_chmask "MARK"
17109         clu=$(do_facet $SINGLEMDS $LCTL --device $mdt changelog_register -n) ||
17110                 error "changelog_register failed"
17111         # check effective mask again, should be treated as DEFMASK now
17112         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17113                                 mdd.$mdt.changelog_current_mask -n)
17114         do_facet $SINGLEMDS $LCTL --device $mdt changelog_deregister $clu ||
17115                 error "changelog_deregister failed"
17116         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
17117 }
17118 run_test 160q "changelog effective mask is DEFMASK if not set"
17119
17120 test_160s() {
17121         remote_mds_nodsh && skip "remote MDS with nodsh"
17122         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
17123                 skip "Need MDS version at least 2.14.55"
17124
17125         local mdts=$(comma_list $(mdts_nodes))
17126
17127         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
17128         do_nodes $mdts $LCTL set_param fail_loc=0x1314 \
17129                                        fail_val=$((24 * 3600 * 10))
17130
17131         # Create a user which is 10 days old
17132         changelog_register || error "first changelog_register failed"
17133         local cl_users
17134         declare -A cl_user1
17135         local i
17136
17137         # generate some changelog records to accumulate on each MDT
17138         # use all_char because created files should be evenly distributed
17139         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17140                 error "test_mkdir $tdir failed"
17141         for ((i = 0; i < MDSCOUNT; i++)); do
17142                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17143                         error "create $DIR/$tdir/d$i.1 failed"
17144         done
17145
17146         # check changelogs have been generated
17147         local nbcl=$(changelog_dump | wc -l)
17148         (( nbcl > 0 )) || error "no changelogs found"
17149
17150         # reduce the max_idle_indexes value to make sure we exceed it
17151         for param in "changelog_max_idle_indexes=2097446912" \
17152                      "changelog_max_idle_time=2592000" \
17153                      "changelog_gc=1" \
17154                      "changelog_min_gc_interval=2"; do
17155                 local MDT0=$(facet_svc $SINGLEMDS)
17156                 local var="${param%=*}"
17157                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17158
17159                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
17160                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
17161                         error "unable to set mdd.*.$param"
17162         done
17163
17164         local start=$SECONDS
17165         for i in $(seq $MDSCOUNT); do
17166                 cl_users=(${CL_USERS[mds$i]})
17167                 cl_user1[mds$i]="${cl_users[0]}"
17168
17169                 [[ -n "${cl_user1[mds$i]}" ]] ||
17170                         error "mds$i: no user registered"
17171         done
17172
17173         #define OBD_FAIL_MDS_CHANGELOG_IDX_PUMP   0x16d
17174         do_nodes $mdts $LCTL set_param fail_loc=0x16d fail_val=500000000
17175
17176         # ensure we are past the previous changelog_min_gc_interval set above
17177         local sleep2=$((start + 2 - SECONDS))
17178         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17179
17180         # Generate one more changelog to trigger GC
17181         for ((i = 0; i < MDSCOUNT; i++)); do
17182                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 $DIR/$tdir/d$i.4 ||
17183                         error "create $DIR/$tdir/d$i.3 failed"
17184         done
17185
17186         # ensure gc thread is done
17187         for node in $(mdts_nodes); do
17188                 wait_update $node "pgrep chlg_gc_thread" "" 20 ||
17189                         error "$node: GC-thread not done"
17190         done
17191
17192         do_nodes $mdts $LCTL set_param fail_loc=0
17193
17194         for (( i = 1; i <= MDSCOUNT; i++ )); do
17195                 # check cl_user1 is purged
17196                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" &&
17197                         error "mds$i: User ${cl_user1[mds$i]} is registered"
17198         done
17199         return 0
17200 }
17201 run_test 160s "changelog garbage collect on idle records * time"
17202
17203 test_160t() {
17204         remote_mds_nodsh && skip "remote MDS with nodsh"
17205         (( $MDS1_VERSION >= $(version_code 2.15.50) )) ||
17206                 skip "Need MDS version at least 2.15.50"
17207
17208         local MDT0=$(facet_svc $SINGLEMDS)
17209         local cl_users
17210         local cl_user1
17211         local cl_user2
17212         local start
17213
17214         changelog_register --user user1 -m all ||
17215                 error "user1 failed to register"
17216
17217         mkdir_on_mdt0 $DIR/$tdir
17218         # create default overstripe to maximize changelog size
17219         $LFS setstripe  -C 8 $DIR/$tdir || error "setstripe failed"
17220         createmany -o $DIR/$tdir/u1_ 2000 || error "createmany for user1 failed"
17221         llog_size1=$(do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size)
17222
17223         # user2 consumes less records so less space
17224         changelog_register --user user2 || error "user2 failed to register"
17225         createmany -o $DIR/$tdir/u2_ 500 || error "createmany for user2 failed"
17226         llog_size2=$(do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size)
17227
17228         # check changelogs have been generated
17229         local nbcl=$(changelog_dump | wc -l)
17230         (( nbcl > 0 )) || error "no changelogs found"
17231
17232         # reduce the changelog_min_gc_interval to force check
17233         for param in "changelog_gc=1" "changelog_min_gc_interval=2"; do
17234                 local var="${param%=*}"
17235                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17236
17237                 stack_trap "do_facet mds1 $LCTL set_param mdd.$MDT0.$var=$old"
17238                 do_facet mds1 $LCTL set_param mdd.$MDT0.$param ||
17239                         error "unable to set mdd.*.$param"
17240         done
17241
17242         start=$SECONDS
17243         cl_users=(${CL_USERS[mds1]})
17244         cl_user1="${cl_users[0]}"
17245         cl_user2="${cl_users[1]}"
17246
17247         [[ -n $cl_user1 ]] ||
17248                 error "mds1: user #1 isn't registered"
17249         [[ -n $cl_user2 ]] ||
17250                 error "mds1: user #2 isn't registered"
17251
17252         # ensure we are past the previous changelog_min_gc_interval set above
17253         local sleep2=$((start + 2 - SECONDS))
17254         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17255
17256         #define OBD_FAIL_MDS_CHANGELOG_ENOSPC 0x018c
17257         do_facet mds1 $LCTL set_param fail_loc=0x018c \
17258                         fail_val=$(((llog_size1 + llog_size2) / 2))
17259
17260         # Generate more changelog to trigger GC
17261         createmany -o $DIR/$tdir/u3_ 4 ||
17262                 error "create failed for more files"
17263
17264         # ensure gc thread is done
17265         wait_update_facet mds1 "pgrep chlg_gc_thread" "" 20 ||
17266                 error "mds1: GC-thread not done"
17267
17268         do_facet mds1 $LCTL set_param fail_loc=0
17269
17270         # check cl_user1 is purged
17271         changelog_users mds1 | grep -q "$cl_user1" &&
17272                 error "User $cl_user1 is registered"
17273         # check cl_user2 is not purged
17274         changelog_users mds1 | grep -q "$cl_user2" ||
17275                 error "User $cl_user2 is not registered"
17276 }
17277 run_test 160t "changelog garbage collect on lack of space"
17278
17279 test_161a() {
17280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17281
17282         test_mkdir -c1 $DIR/$tdir
17283         cp /etc/hosts $DIR/$tdir/$tfile
17284         test_mkdir -c1 $DIR/$tdir/foo1
17285         test_mkdir -c1 $DIR/$tdir/foo2
17286         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
17287         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
17288         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
17289         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
17290         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
17291         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
17292                 $LFS fid2path $DIR $FID
17293                 error "bad link ea"
17294         fi
17295         # middle
17296         rm $DIR/$tdir/foo2/zachary
17297         # last
17298         rm $DIR/$tdir/foo2/thor
17299         # first
17300         rm $DIR/$tdir/$tfile
17301         # rename
17302         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
17303         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
17304                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
17305         rm $DIR/$tdir/foo2/maggie
17306
17307         # overflow the EA
17308         local longname=$tfile.avg_len_is_thirty_two_
17309         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
17310                 error_noexit 'failed to unlink many hardlinks'" EXIT
17311         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
17312                 error "failed to hardlink many files"
17313         links=$($LFS fid2path $DIR $FID | wc -l)
17314         echo -n "${links}/1000 links in link EA"
17315         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
17316 }
17317 run_test 161a "link ea sanity"
17318
17319 test_161b() {
17320         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17321         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
17322
17323         local MDTIDX=1
17324         local remote_dir=$DIR/$tdir/remote_dir
17325
17326         mkdir -p $DIR/$tdir
17327         $LFS mkdir -i $MDTIDX $remote_dir ||
17328                 error "create remote directory failed"
17329
17330         cp /etc/hosts $remote_dir/$tfile
17331         mkdir -p $remote_dir/foo1
17332         mkdir -p $remote_dir/foo2
17333         ln $remote_dir/$tfile $remote_dir/foo1/sofia
17334         ln $remote_dir/$tfile $remote_dir/foo2/zachary
17335         ln $remote_dir/$tfile $remote_dir/foo1/luna
17336         ln $remote_dir/$tfile $remote_dir/foo2/thor
17337
17338         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
17339                      tr -d ']')
17340         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
17341                 $LFS fid2path $DIR $FID
17342                 error "bad link ea"
17343         fi
17344         # middle
17345         rm $remote_dir/foo2/zachary
17346         # last
17347         rm $remote_dir/foo2/thor
17348         # first
17349         rm $remote_dir/$tfile
17350         # rename
17351         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
17352         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
17353         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
17354                 $LFS fid2path $DIR $FID
17355                 error "bad link rename"
17356         fi
17357         rm $remote_dir/foo2/maggie
17358
17359         # overflow the EA
17360         local longname=filename_avg_len_is_thirty_two_
17361         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
17362                 error "failed to hardlink many files"
17363         links=$($LFS fid2path $DIR $FID | wc -l)
17364         echo -n "${links}/1000 links in link EA"
17365         [[ ${links} -gt 60 ]] ||
17366                 error "expected at least 60 links in link EA"
17367         unlinkmany $remote_dir/foo2/$longname 1000 ||
17368         error "failed to unlink many hardlinks"
17369 }
17370 run_test 161b "link ea sanity under remote directory"
17371
17372 test_161c() {
17373         remote_mds_nodsh && skip "remote MDS with nodsh"
17374         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17375         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
17376                 skip "Need MDS version at least 2.1.5"
17377
17378         # define CLF_RENAME_LAST 0x0001
17379         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
17380         changelog_register || error "changelog_register failed"
17381
17382         rm -rf $DIR/$tdir
17383         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
17384         touch $DIR/$tdir/foo_161c
17385         touch $DIR/$tdir/bar_161c
17386         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
17387         changelog_dump | grep RENME | tail -n 5
17388         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
17389         changelog_clear 0 || error "changelog_clear failed"
17390         if [ x$flags != "x0x1" ]; then
17391                 error "flag $flags is not 0x1"
17392         fi
17393
17394         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
17395         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
17396         touch $DIR/$tdir/foo_161c
17397         touch $DIR/$tdir/bar_161c
17398         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
17399         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
17400         changelog_dump | grep RENME | tail -n 5
17401         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
17402         changelog_clear 0 || error "changelog_clear failed"
17403         if [ x$flags != "x0x0" ]; then
17404                 error "flag $flags is not 0x0"
17405         fi
17406         echo "rename overwrite a target having nlink > 1," \
17407                 "changelog record has flags of $flags"
17408
17409         # rename doesn't overwrite a target (changelog flag 0x0)
17410         touch $DIR/$tdir/foo_161c
17411         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
17412         changelog_dump | grep RENME | tail -n 5
17413         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
17414         changelog_clear 0 || error "changelog_clear failed"
17415         if [ x$flags != "x0x0" ]; then
17416                 error "flag $flags is not 0x0"
17417         fi
17418         echo "rename doesn't overwrite a target," \
17419                 "changelog record has flags of $flags"
17420
17421         # define CLF_UNLINK_LAST 0x0001
17422         # unlink a file having nlink = 1 (changelog flag 0x1)
17423         rm -f $DIR/$tdir/foo2_161c
17424         changelog_dump | grep UNLNK | tail -n 5
17425         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
17426         changelog_clear 0 || error "changelog_clear failed"
17427         if [ x$flags != "x0x1" ]; then
17428                 error "flag $flags is not 0x1"
17429         fi
17430         echo "unlink a file having nlink = 1," \
17431                 "changelog record has flags of $flags"
17432
17433         # unlink a file having nlink > 1 (changelog flag 0x0)
17434         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
17435         rm -f $DIR/$tdir/foobar_161c
17436         changelog_dump | grep UNLNK | tail -n 5
17437         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
17438         changelog_clear 0 || error "changelog_clear failed"
17439         if [ x$flags != "x0x0" ]; then
17440                 error "flag $flags is not 0x0"
17441         fi
17442         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
17443 }
17444 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
17445
17446 test_161d() {
17447         remote_mds_nodsh && skip "remote MDS with nodsh"
17448         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
17449
17450         local pid
17451         local fid
17452
17453         changelog_register || error "changelog_register failed"
17454
17455         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
17456         # interfer with $MOUNT/.lustre/fid/ access
17457         mkdir $DIR/$tdir
17458         [[ $? -eq 0 ]] || error "mkdir failed"
17459
17460         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
17461         $LCTL set_param fail_loc=0x8000140c
17462         # 5s pause
17463         $LCTL set_param fail_val=5
17464
17465         # create file
17466         echo foofoo > $DIR/$tdir/$tfile &
17467         pid=$!
17468
17469         # wait for create to be delayed
17470         sleep 2
17471
17472         ps -p $pid
17473         [[ $? -eq 0 ]] || error "create should be blocked"
17474
17475         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
17476         stack_trap "rm -f $tempfile"
17477         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
17478         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
17479         # some delay may occur during ChangeLog publishing and file read just
17480         # above, that could allow file write to happen finally
17481         [[ -s $tempfile ]] && echo "file should be empty"
17482
17483         $LCTL set_param fail_loc=0
17484
17485         wait $pid
17486         [[ $? -eq 0 ]] || error "create failed"
17487 }
17488 run_test 161d "create with concurrent .lustre/fid access"
17489
17490 check_path() {
17491         local expected="$1"
17492         shift
17493         local fid="$2"
17494
17495         local path
17496         path=$($LFS fid2path "$@")
17497         local rc=$?
17498
17499         if [ $rc -ne 0 ]; then
17500                 error "path looked up of '$expected' failed: rc=$rc"
17501         elif [ "$path" != "$expected" ]; then
17502                 error "path looked up '$path' instead of '$expected'"
17503         else
17504                 echo "FID '$fid' resolves to path '$path' as expected"
17505         fi
17506 }
17507
17508 test_162a() { # was test_162
17509         test_mkdir -p -c1 $DIR/$tdir/d2
17510         touch $DIR/$tdir/d2/$tfile
17511         touch $DIR/$tdir/d2/x1
17512         touch $DIR/$tdir/d2/x2
17513         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
17514         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
17515         # regular file
17516         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
17517         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
17518
17519         # softlink
17520         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
17521         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
17522         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
17523
17524         # softlink to wrong file
17525         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
17526         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
17527         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
17528
17529         # hardlink
17530         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
17531         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
17532         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
17533         # fid2path dir/fsname should both work
17534         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
17535         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
17536
17537         # hardlink count: check that there are 2 links
17538         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
17539         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
17540
17541         # hardlink indexing: remove the first link
17542         rm $DIR/$tdir/d2/p/q/r/hlink
17543         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
17544 }
17545 run_test 162a "path lookup sanity"
17546
17547 test_162b() {
17548         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17549         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17550
17551         mkdir $DIR/$tdir
17552         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17553                                 error "create striped dir failed"
17554
17555         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
17556                                         tail -n 1 | awk '{print $2}')
17557         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
17558
17559         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
17560         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
17561
17562         # regular file
17563         for ((i=0;i<5;i++)); do
17564                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
17565                         error "get fid for f$i failed"
17566                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
17567
17568                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
17569                         error "get fid for d$i failed"
17570                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
17571         done
17572
17573         return 0
17574 }
17575 run_test 162b "striped directory path lookup sanity"
17576
17577 # LU-4239: Verify fid2path works with paths 100 or more directories deep
17578 test_162c() {
17579         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
17580                 skip "Need MDS version at least 2.7.51"
17581
17582         local lpath=$tdir.local
17583         local rpath=$tdir.remote
17584
17585         test_mkdir $DIR/$lpath
17586         test_mkdir $DIR/$rpath
17587
17588         for ((i = 0; i <= 101; i++)); do
17589                 lpath="$lpath/$i"
17590                 mkdir $DIR/$lpath
17591                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
17592                         error "get fid for local directory $DIR/$lpath failed"
17593                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
17594
17595                 rpath="$rpath/$i"
17596                 test_mkdir $DIR/$rpath
17597                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
17598                         error "get fid for remote directory $DIR/$rpath failed"
17599                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
17600         done
17601
17602         return 0
17603 }
17604 run_test 162c "fid2path works with paths 100 or more directories deep"
17605
17606 oalr_event_count() {
17607         local event="${1}"
17608         local trace="${2}"
17609
17610         awk -v name="${FSNAME}-OST0000" \
17611             -v event="${event}" \
17612             '$1 == "TRACE" && $2 == event && $3 == name' \
17613             "${trace}" |
17614         wc -l
17615 }
17616
17617 oalr_expect_event_count() {
17618         local event="${1}"
17619         local trace="${2}"
17620         local expect="${3}"
17621         local count
17622
17623         count=$(oalr_event_count "${event}" "${trace}")
17624         if ((count == expect)); then
17625                 return 0
17626         fi
17627
17628         error_noexit "${event} event count was '${count}', expected ${expect}"
17629         cat "${trace}" >&2
17630         exit 1
17631 }
17632
17633 cleanup_165() {
17634         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
17635         stop ost1
17636         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
17637 }
17638
17639 setup_165() {
17640         sync # Flush previous IOs so we can count log entries.
17641         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
17642         stack_trap cleanup_165 EXIT
17643 }
17644
17645 test_165a() {
17646         local trace="/tmp/${tfile}.trace"
17647         local rc
17648         local count
17649
17650         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17651                 skip "OFD access log unsupported"
17652
17653         setup_165
17654         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17655         sleep 5
17656
17657         do_facet ost1 ofd_access_log_reader --list
17658         stop ost1
17659
17660         do_facet ost1 killall -TERM ofd_access_log_reader
17661         wait
17662         rc=$?
17663
17664         if ((rc != 0)); then
17665                 error "ofd_access_log_reader exited with rc = '${rc}'"
17666         fi
17667
17668         # Parse trace file for discovery events:
17669         oalr_expect_event_count alr_log_add "${trace}" 1
17670         oalr_expect_event_count alr_log_eof "${trace}" 1
17671         oalr_expect_event_count alr_log_free "${trace}" 1
17672 }
17673 run_test 165a "ofd access log discovery"
17674
17675 test_165b() {
17676         local trace="/tmp/${tfile}.trace"
17677         local file="${DIR}/${tfile}"
17678         local pfid1
17679         local pfid2
17680         local -a entry
17681         local rc
17682         local count
17683         local size
17684         local flags
17685
17686         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17687                 skip "OFD access log unsupported"
17688
17689         setup_165
17690         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17691         sleep 5
17692
17693         do_facet ost1 ofd_access_log_reader --list
17694
17695         lfs setstripe -c 1 -i 0 "${file}"
17696         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17697                 error "cannot create '${file}'"
17698
17699         sleep 5
17700         do_facet ost1 killall -TERM ofd_access_log_reader
17701         wait
17702         rc=$?
17703
17704         if ((rc != 0)); then
17705                 error "ofd_access_log_reader exited with rc = '${rc}'"
17706         fi
17707
17708         oalr_expect_event_count alr_log_entry "${trace}" 1
17709
17710         pfid1=$($LFS path2fid "${file}")
17711
17712         # 1     2             3   4    5     6   7    8    9     10
17713         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
17714         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17715
17716         echo "entry = '${entry[*]}'" >&2
17717
17718         pfid2=${entry[4]}
17719         if [[ "${pfid1}" != "${pfid2}" ]]; then
17720                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17721         fi
17722
17723         size=${entry[8]}
17724         if ((size != 1048576)); then
17725                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
17726         fi
17727
17728         flags=${entry[10]}
17729         if [[ "${flags}" != "w" ]]; then
17730                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
17731         fi
17732
17733         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17734         sleep 5
17735
17736         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c ||
17737                 error "cannot read '${file}'"
17738         sleep 5
17739
17740         do_facet ost1 killall -TERM ofd_access_log_reader
17741         wait
17742         rc=$?
17743
17744         if ((rc != 0)); then
17745                 error "ofd_access_log_reader exited with rc = '${rc}'"
17746         fi
17747
17748         oalr_expect_event_count alr_log_entry "${trace}" 1
17749
17750         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17751         echo "entry = '${entry[*]}'" >&2
17752
17753         pfid2=${entry[4]}
17754         if [[ "${pfid1}" != "${pfid2}" ]]; then
17755                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17756         fi
17757
17758         size=${entry[8]}
17759         if ((size != 524288)); then
17760                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
17761         fi
17762
17763         flags=${entry[10]}
17764         if [[ "${flags}" != "r" ]]; then
17765                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
17766         fi
17767 }
17768 run_test 165b "ofd access log entries are produced and consumed"
17769
17770 test_165c() {
17771         local trace="/tmp/${tfile}.trace"
17772         local file="${DIR}/${tdir}/${tfile}"
17773
17774         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17775                 skip "OFD access log unsupported"
17776
17777         test_mkdir "${DIR}/${tdir}"
17778
17779         setup_165
17780         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17781         sleep 5
17782
17783         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
17784
17785         # 4096 / 64 = 64. Create twice as many entries.
17786         for ((i = 0; i < 128; i++)); do
17787                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c ||
17788                         error "cannot create file"
17789         done
17790
17791         sync
17792
17793         do_facet ost1 killall -TERM ofd_access_log_reader
17794         wait
17795         rc=$?
17796         if ((rc != 0)); then
17797                 error "ofd_access_log_reader exited with rc = '${rc}'"
17798         fi
17799
17800         unlinkmany  "${file}-%d" 128
17801 }
17802 run_test 165c "full ofd access logs do not block IOs"
17803
17804 oal_get_read_count() {
17805         local stats="$1"
17806
17807         # STATS lustre-OST0001 alr_read_count 1
17808
17809         do_facet ost1 cat "${stats}" |
17810         awk '$1 == "STATS" && $3 == "alr_read_count" { count = $4; }
17811              END { print count; }'
17812 }
17813
17814 oal_expect_read_count() {
17815         local stats="$1"
17816         local count
17817         local expect="$2"
17818
17819         # Ask ofd_access_log_reader to write stats.
17820         do_facet ost1 killall -USR1 ofd_access_log_reader
17821
17822         # Allow some time for things to happen.
17823         sleep 1
17824
17825         count=$(oal_get_read_count "${stats}")
17826         if ((count == expect)); then
17827                 return 0
17828         fi
17829
17830         error_noexit "bad read count, got ${count}, expected ${expect}"
17831         do_facet ost1 cat "${stats}" >&2
17832         exit 1
17833 }
17834
17835 test_165d() {
17836         local stats="/tmp/${tfile}.stats"
17837         local file="${DIR}/${tdir}/${tfile}"
17838         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
17839
17840         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17841                 skip "OFD access log unsupported"
17842
17843         test_mkdir "${DIR}/${tdir}"
17844
17845         setup_165
17846         do_facet ost1 ofd_access_log_reader --stats="${stats}" &
17847         sleep 5
17848
17849         lfs setstripe -c 1 -i 0 "${file}"
17850
17851         do_facet ost1 lctl set_param "${param}=rw"
17852         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17853                 error "cannot create '${file}'"
17854         oal_expect_read_count "${stats}" 1
17855
17856         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17857                 error "cannot read '${file}'"
17858         oal_expect_read_count "${stats}" 2
17859
17860         do_facet ost1 lctl set_param "${param}=r"
17861         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17862                 error "cannot create '${file}'"
17863         oal_expect_read_count "${stats}" 2
17864
17865         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17866                 error "cannot read '${file}'"
17867         oal_expect_read_count "${stats}" 3
17868
17869         do_facet ost1 lctl set_param "${param}=w"
17870         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17871                 error "cannot create '${file}'"
17872         oal_expect_read_count "${stats}" 4
17873
17874         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17875                 error "cannot read '${file}'"
17876         oal_expect_read_count "${stats}" 4
17877
17878         do_facet ost1 lctl set_param "${param}=0"
17879         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17880                 error "cannot create '${file}'"
17881         oal_expect_read_count "${stats}" 4
17882
17883         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17884                 error "cannot read '${file}'"
17885         oal_expect_read_count "${stats}" 4
17886
17887         do_facet ost1 killall -TERM ofd_access_log_reader
17888         wait
17889         rc=$?
17890         if ((rc != 0)); then
17891                 error "ofd_access_log_reader exited with rc = '${rc}'"
17892         fi
17893 }
17894 run_test 165d "ofd_access_log mask works"
17895
17896 test_165e() {
17897         local stats="/tmp/${tfile}.stats"
17898         local file0="${DIR}/${tdir}-0/${tfile}"
17899         local file1="${DIR}/${tdir}-1/${tfile}"
17900
17901         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17902                 skip "OFD access log unsupported"
17903
17904         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
17905
17906         test_mkdir -c 1 -i 0 "${DIR}/${tdir}-0"
17907         test_mkdir -c 1 -i 1 "${DIR}/${tdir}-1"
17908
17909         lfs setstripe -c 1 -i 0 "${file0}"
17910         lfs setstripe -c 1 -i 0 "${file1}"
17911
17912         setup_165
17913         do_facet ost1 ofd_access_log_reader -I 1 --stats="${stats}" &
17914         sleep 5
17915
17916         $MULTIOP "${file0}" oO_CREAT:O_WRONLY:w512c ||
17917                 error "cannot create '${file0}'"
17918         sync
17919         oal_expect_read_count "${stats}" 0
17920
17921         $MULTIOP "${file1}" oO_CREAT:O_WRONLY:w512c ||
17922                 error "cannot create '${file1}'"
17923         sync
17924         oal_expect_read_count "${stats}" 1
17925
17926         do_facet ost1 killall -TERM ofd_access_log_reader
17927         wait
17928         rc=$?
17929         if ((rc != 0)); then
17930                 error "ofd_access_log_reader exited with rc = '${rc}'"
17931         fi
17932 }
17933 run_test 165e "ofd_access_log MDT index filter works"
17934
17935 test_165f() {
17936         local trace="/tmp/${tfile}.trace"
17937         local rc
17938         local count
17939
17940         setup_165
17941         do_facet ost1 timeout 60 ofd_access_log_reader \
17942                 --exit-on-close --debug=- --trace=- > "${trace}" &
17943         sleep 5
17944         stop ost1
17945
17946         wait
17947         rc=$?
17948
17949         if ((rc != 0)); then
17950                 error_noexit "ofd_access_log_reader exited with rc = '${rc}'"
17951                 cat "${trace}"
17952                 exit 1
17953         fi
17954 }
17955 run_test 165f "ofd_access_log_reader --exit-on-close works"
17956
17957 test_169() {
17958         # do directio so as not to populate the page cache
17959         log "creating a 10 Mb file"
17960         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
17961                 error "multiop failed while creating a file"
17962         log "starting reads"
17963         dd if=$DIR/$tfile of=/dev/null bs=4096 &
17964         log "truncating the file"
17965         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
17966                 error "multiop failed while truncating the file"
17967         log "killing dd"
17968         kill %+ || true # reads might have finished
17969         echo "wait until dd is finished"
17970         wait
17971         log "removing the temporary file"
17972         rm -rf $DIR/$tfile || error "tmp file removal failed"
17973 }
17974 run_test 169 "parallel read and truncate should not deadlock"
17975
17976 test_170() {
17977         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17978
17979         $LCTL clear     # bug 18514
17980         $LCTL debug_daemon start $TMP/${tfile}_log_good
17981         touch $DIR/$tfile
17982         $LCTL debug_daemon stop
17983         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
17984                 error "sed failed to read log_good"
17985
17986         $LCTL debug_daemon start $TMP/${tfile}_log_good
17987         rm -rf $DIR/$tfile
17988         $LCTL debug_daemon stop
17989
17990         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
17991                error "lctl df log_bad failed"
17992
17993         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
17994         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
17995
17996         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
17997         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
17998
17999         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
18000                 error "bad_line good_line1 good_line2 are empty"
18001
18002         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
18003         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
18004         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
18005
18006         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
18007         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
18008         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
18009
18010         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
18011                 error "bad_line_new good_line_new are empty"
18012
18013         local expected_good=$((good_line1 + good_line2*2))
18014
18015         rm -f $TMP/${tfile}*
18016         # LU-231, short malformed line may not be counted into bad lines
18017         if [ $bad_line -ne $bad_line_new ] &&
18018                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
18019                 error "expected $bad_line bad lines, but got $bad_line_new"
18020                 return 1
18021         fi
18022
18023         if [ $expected_good -ne $good_line_new ]; then
18024                 error "expected $expected_good good lines, but got $good_line_new"
18025                 return 2
18026         fi
18027         true
18028 }
18029 run_test 170 "test lctl df to handle corrupted log ====================="
18030
18031 test_171() { # bug20592
18032         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18033
18034         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
18035         $LCTL set_param fail_loc=0x50e
18036         $LCTL set_param fail_val=3000
18037         multiop_bg_pause $DIR/$tfile O_s || true
18038         local MULTIPID=$!
18039         kill -USR1 $MULTIPID
18040         # cause log dump
18041         sleep 3
18042         wait $MULTIPID
18043         if dmesg | grep "recursive fault"; then
18044                 error "caught a recursive fault"
18045         fi
18046         $LCTL set_param fail_loc=0
18047         true
18048 }
18049 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
18050
18051 test_172() {
18052
18053         #define OBD_FAIL_OBD_CLEANUP  0x60e
18054         $LCTL set_param fail_loc=0x60e
18055         umount $MOUNT || error "umount $MOUNT failed"
18056         stack_trap "mount_client $MOUNT"
18057
18058         (( $($LCTL dl | egrep -c " osc | lov | lmv | mdc ") > 0 )) ||
18059                 error "no client OBDs are remained"
18060
18061         $LCTL dl | while read devno state type name foo; do
18062                 case $type in
18063                 lov|osc|lmv|mdc)
18064                         $LCTL --device $name cleanup
18065                         $LCTL --device $name detach
18066                         ;;
18067                 *)
18068                         # skip server devices
18069                         ;;
18070                 esac
18071         done
18072
18073         if (( $($LCTL dl | egrep -c " osc | lov | lmv | mdc ") > 0 )); then
18074                 $LCTL dl | egrep " osc | lov | lmv | mdc "
18075                 error "some client OBDs are still remained"
18076         fi
18077
18078 }
18079 run_test 172 "manual device removal with lctl cleanup/detach ======"
18080
18081 # it would be good to share it with obdfilter-survey/iokit-libecho code
18082 setup_obdecho_osc () {
18083         local rc=0
18084         local ost_nid=$1
18085         local obdfilter_name=$2
18086         echo "Creating new osc for $obdfilter_name on $ost_nid"
18087         # make sure we can find loopback nid
18088         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
18089
18090         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
18091                            ${obdfilter_name}_osc_UUID || rc=2; }
18092         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
18093                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
18094         return $rc
18095 }
18096
18097 cleanup_obdecho_osc () {
18098         local obdfilter_name=$1
18099         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
18100         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
18101         return 0
18102 }
18103
18104 obdecho_test() {
18105         local OBD=$1
18106         local node=$2
18107         local pages=${3:-64}
18108         local rc=0
18109         local id
18110
18111         local count=10
18112         local obd_size=$(get_obd_size $node $OBD)
18113         local page_size=$(get_page_size $node)
18114         if [[ -n "$obd_size" ]]; then
18115                 local new_count=$((obd_size / (pages * page_size / 1024)))
18116                 [[ $new_count -ge $count ]] || count=$new_count
18117         fi
18118
18119         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
18120         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
18121                            rc=2; }
18122         if [ $rc -eq 0 ]; then
18123             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
18124             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
18125         fi
18126         echo "New object id is $id"
18127         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
18128                            rc=4; }
18129         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
18130                            "test_brw $count w v $pages $id" || rc=4; }
18131         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
18132                            rc=4; }
18133         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
18134                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
18135         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
18136                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
18137         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
18138         return $rc
18139 }
18140
18141 test_180a() {
18142         skip "obdecho on osc is no longer supported"
18143 }
18144 run_test 180a "test obdecho on osc"
18145
18146 test_180b() {
18147         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18148         remote_ost_nodsh && skip "remote OST with nodsh"
18149
18150         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
18151                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
18152                 error "failed to load module obdecho"
18153
18154         local target=$(do_facet ost1 $LCTL dl |
18155                        awk '/obdfilter/ { print $4; exit; }')
18156
18157         if [ -n "$target" ]; then
18158                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
18159         else
18160                 do_facet ost1 $LCTL dl
18161                 error "there is no obdfilter target on ost1"
18162         fi
18163 }
18164 run_test 180b "test obdecho directly on obdfilter"
18165
18166 test_180c() { # LU-2598
18167         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18168         remote_ost_nodsh && skip "remote OST with nodsh"
18169         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
18170                 skip "Need MDS version at least 2.4.0"
18171
18172         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
18173                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
18174                 error "failed to load module obdecho"
18175
18176         local target=$(do_facet ost1 $LCTL dl |
18177                        awk '/obdfilter/ { print $4; exit; }')
18178
18179         if [ -n "$target" ]; then
18180                 local pages=16384 # 64MB bulk I/O RPC size
18181
18182                 obdecho_test "$target" ost1 "$pages" ||
18183                         error "obdecho_test with pages=$pages failed with $?"
18184         else
18185                 do_facet ost1 $LCTL dl
18186                 error "there is no obdfilter target on ost1"
18187         fi
18188 }
18189 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
18190
18191 test_181() { # bug 22177
18192         test_mkdir $DIR/$tdir
18193         # create enough files to index the directory
18194         createmany -o $DIR/$tdir/foobar 4000
18195         # print attributes for debug purpose
18196         lsattr -d .
18197         # open dir
18198         multiop_bg_pause $DIR/$tdir D_Sc || return 1
18199         MULTIPID=$!
18200         # remove the files & current working dir
18201         unlinkmany $DIR/$tdir/foobar 4000
18202         rmdir $DIR/$tdir
18203         kill -USR1 $MULTIPID
18204         wait $MULTIPID
18205         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
18206         return 0
18207 }
18208 run_test 181 "Test open-unlinked dir ========================"
18209
18210 test_182a() {
18211         local fcount=1000
18212         local tcount=10
18213
18214         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
18215
18216         $LCTL set_param mdc.*.rpc_stats=clear
18217
18218         for (( i = 0; i < $tcount; i++ )) ; do
18219                 mkdir $DIR/$tdir/$i
18220         done
18221
18222         for (( i = 0; i < $tcount; i++ )) ; do
18223                 createmany -o $DIR/$tdir/$i/f- $fcount &
18224         done
18225         wait
18226
18227         for (( i = 0; i < $tcount; i++ )) ; do
18228                 unlinkmany $DIR/$tdir/$i/f- $fcount &
18229         done
18230         wait
18231
18232         $LCTL get_param mdc.*.rpc_stats
18233
18234         rm -rf $DIR/$tdir
18235 }
18236 run_test 182a "Test parallel modify metadata operations from mdc"
18237
18238 test_182b() {
18239         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18240         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
18241         local dcount=1000
18242         local tcount=10
18243         local stime
18244         local etime
18245         local delta
18246
18247         do_facet mds1 $LCTL list_param \
18248                 osp.$FSNAME-MDT*-osp-MDT*.rpc_stats ||
18249                 skip "MDS lacks parallel RPC handling"
18250
18251         $LFS mkdir -i 0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18252
18253         rpc_count=$(do_facet mds1 $LCTL get_param -n \
18254                     osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight)
18255
18256         stime=$(date +%s)
18257         createmany -i 0 -d $DIR/$tdir/t- $tcount
18258
18259         for (( i = 0; i < $tcount; i++ )) ; do
18260                 createmany -i 0 -d $DIR/$tdir/t-$i/d- 0 $dcount &
18261         done
18262         wait
18263         etime=$(date +%s)
18264         delta=$((etime - stime))
18265         echo "Time for file creation $delta sec for $rpc_count parallel RPCs"
18266
18267         stime=$(date +%s)
18268         for (( i = 0; i < $tcount; i++ )) ; do
18269                 unlinkmany -d $DIR/$tdir/$i/d- $dcount &
18270         done
18271         wait
18272         etime=$(date +%s)
18273         delta=$((etime - stime))
18274         echo "Time for file removal $delta sec for $rpc_count parallel RPCs"
18275
18276         rm -rf $DIR/$tdir
18277
18278         $LFS mkdir -i 0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18279
18280         do_facet mds1 $LCTL set_param osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight=1
18281
18282         stime=$(date +%s)
18283         createmany -i 0 -d $DIR/$tdir/t- $tcount
18284
18285         for (( i = 0; i < $tcount; i++ )) ; do
18286                 createmany -i 0 -d $DIR/$tdir/t-$i/d- 0 $dcount &
18287         done
18288         wait
18289         etime=$(date +%s)
18290         delta=$((etime - stime))
18291         echo "Time for file creation $delta sec for 1 RPC sent at a time"
18292
18293         stime=$(date +%s)
18294         for (( i = 0; i < $tcount; i++ )) ; do
18295                 unlinkmany -d $DIR/$tdir/t-$i/d- $dcount &
18296         done
18297         wait
18298         etime=$(date +%s)
18299         delta=$((etime - stime))
18300         echo "Time for file removal $delta sec for 1 RPC sent at a time"
18301
18302         do_facet mds1 $LCTL set_param osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight=$rpc_count
18303 }
18304 run_test 182b "Test parallel modify metadata operations from osp"
18305
18306 test_183() { # LU-2275
18307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18308         remote_mds_nodsh && skip "remote MDS with nodsh"
18309         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
18310                 skip "Need MDS version at least 2.3.56"
18311
18312         mkdir_on_mdt0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18313         echo aaa > $DIR/$tdir/$tfile
18314
18315 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
18316         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
18317
18318         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
18319         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
18320
18321         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18322
18323         # Flush negative dentry cache
18324         touch $DIR/$tdir/$tfile
18325
18326         # We are not checking for any leaked references here, they'll
18327         # become evident next time we do cleanup with module unload.
18328         rm -rf $DIR/$tdir
18329 }
18330 run_test 183 "No crash or request leak in case of strange dispositions ========"
18331
18332 # test suite 184 is for LU-2016, LU-2017
18333 test_184a() {
18334         check_swap_layouts_support
18335
18336         dir0=$DIR/$tdir/$testnum
18337         test_mkdir -p -c1 $dir0
18338         ref1=/etc/passwd
18339         ref2=/etc/group
18340         file1=$dir0/f1
18341         file2=$dir0/f2
18342         $LFS setstripe -c1 $file1
18343         cp $ref1 $file1
18344         $LFS setstripe -c2 $file2
18345         cp $ref2 $file2
18346         gen1=$($LFS getstripe -g $file1)
18347         gen2=$($LFS getstripe -g $file2)
18348
18349         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
18350         gen=$($LFS getstripe -g $file1)
18351         [[ $gen1 != $gen ]] ||
18352                 error "Layout generation on $file1 does not change"
18353         gen=$($LFS getstripe -g $file2)
18354         [[ $gen2 != $gen ]] ||
18355                 error "Layout generation on $file2 does not change"
18356
18357         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
18358         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
18359
18360         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
18361 }
18362 run_test 184a "Basic layout swap"
18363
18364 test_184b() {
18365         check_swap_layouts_support
18366
18367         dir0=$DIR/$tdir/$testnum
18368         mkdir -p $dir0 || error "creating dir $dir0"
18369         file1=$dir0/f1
18370         file2=$dir0/f2
18371         file3=$dir0/f3
18372         dir1=$dir0/d1
18373         dir2=$dir0/d2
18374         mkdir $dir1 $dir2
18375         $LFS setstripe -c1 $file1
18376         $LFS setstripe -c2 $file2
18377         $LFS setstripe -c1 $file3
18378         chown $RUNAS_ID $file3
18379         gen1=$($LFS getstripe -g $file1)
18380         gen2=$($LFS getstripe -g $file2)
18381
18382         $LFS swap_layouts $dir1 $dir2 &&
18383                 error "swap of directories layouts should fail"
18384         $LFS swap_layouts $dir1 $file1 &&
18385                 error "swap of directory and file layouts should fail"
18386         $RUNAS $LFS swap_layouts $file1 $file2 &&
18387                 error "swap of file we cannot write should fail"
18388         $LFS swap_layouts $file1 $file3 &&
18389                 error "swap of file with different owner should fail"
18390         /bin/true # to clear error code
18391 }
18392 run_test 184b "Forbidden layout swap (will generate errors)"
18393
18394 test_184c() {
18395         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
18396         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
18397         check_swap_layouts_support
18398         check_swap_layout_no_dom $DIR
18399
18400         local dir0=$DIR/$tdir/$testnum
18401         mkdir -p $dir0 || error "creating dir $dir0"
18402
18403         local ref1=$dir0/ref1
18404         local ref2=$dir0/ref2
18405         local file1=$dir0/file1
18406         local file2=$dir0/file2
18407         # create a file large enough for the concurrent test
18408         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
18409         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
18410         echo "ref file size: ref1($(stat -c %s $ref1))," \
18411              "ref2($(stat -c %s $ref2))"
18412
18413         cp $ref2 $file2
18414         dd if=$ref1 of=$file1 bs=16k &
18415         local DD_PID=$!
18416
18417         # Make sure dd starts to copy file, but wait at most 5 seconds
18418         local loops=0
18419         while [ ! -s $file1 -a $((loops++)) -lt 50 ]; do sleep 0.1; done
18420
18421         $LFS swap_layouts $file1 $file2
18422         local rc=$?
18423         wait $DD_PID
18424         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
18425         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
18426
18427         # how many bytes copied before swapping layout
18428         local copied=$(stat -c %s $file2)
18429         local remaining=$(stat -c %s $ref1)
18430         remaining=$((remaining - copied))
18431         echo "Copied $copied bytes before swapping layout..."
18432
18433         cmp -n $copied $file1 $ref2 | grep differ &&
18434                 error "Content mismatch [0, $copied) of ref2 and file1"
18435         cmp -n $copied $file2 $ref1 ||
18436                 error "Content mismatch [0, $copied) of ref1 and file2"
18437         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
18438                 error "Content mismatch [$copied, EOF) of ref1 and file1"
18439
18440         # clean up
18441         rm -f $ref1 $ref2 $file1 $file2
18442 }
18443 run_test 184c "Concurrent write and layout swap"
18444
18445 test_184d() {
18446         check_swap_layouts_support
18447         check_swap_layout_no_dom $DIR
18448         [ -z "$(which getfattr 2>/dev/null)" ] &&
18449                 skip_env "no getfattr command"
18450
18451         local file1=$DIR/$tdir/$tfile-1
18452         local file2=$DIR/$tdir/$tfile-2
18453         local file3=$DIR/$tdir/$tfile-3
18454         local lovea1
18455         local lovea2
18456
18457         mkdir -p $DIR/$tdir
18458         touch $file1 || error "create $file1 failed"
18459         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
18460                 error "create $file2 failed"
18461         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
18462                 error "create $file3 failed"
18463         lovea1=$(get_layout_param $file1)
18464
18465         $LFS swap_layouts $file2 $file3 ||
18466                 error "swap $file2 $file3 layouts failed"
18467         $LFS swap_layouts $file1 $file2 ||
18468                 error "swap $file1 $file2 layouts failed"
18469
18470         lovea2=$(get_layout_param $file2)
18471         echo "$lovea1"
18472         echo "$lovea2"
18473         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
18474
18475         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
18476         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
18477 }
18478 run_test 184d "allow stripeless layouts swap"
18479
18480 test_184e() {
18481         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
18482                 skip "Need MDS version at least 2.6.94"
18483         check_swap_layouts_support
18484         check_swap_layout_no_dom $DIR
18485         [ -z "$(which getfattr 2>/dev/null)" ] &&
18486                 skip_env "no getfattr command"
18487
18488         local file1=$DIR/$tdir/$tfile-1
18489         local file2=$DIR/$tdir/$tfile-2
18490         local file3=$DIR/$tdir/$tfile-3
18491         local lovea
18492
18493         mkdir -p $DIR/$tdir
18494         touch $file1 || error "create $file1 failed"
18495         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
18496                 error "create $file2 failed"
18497         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
18498                 error "create $file3 failed"
18499
18500         $LFS swap_layouts $file1 $file2 ||
18501                 error "swap $file1 $file2 layouts failed"
18502
18503         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
18504         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
18505
18506         echo 123 > $file1 || error "Should be able to write into $file1"
18507
18508         $LFS swap_layouts $file1 $file3 ||
18509                 error "swap $file1 $file3 layouts failed"
18510
18511         echo 123 > $file1 || error "Should be able to write into $file1"
18512
18513         rm -rf $file1 $file2 $file3
18514 }
18515 run_test 184e "Recreate layout after stripeless layout swaps"
18516
18517 test_184f() {
18518         # Create a file with name longer than sizeof(struct stat) ==
18519         # 144 to see if we can get chars from the file name to appear
18520         # in the returned striping. Note that 'f' == 0x66.
18521         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
18522
18523         mkdir -p $DIR/$tdir
18524         mcreate $DIR/$tdir/$file
18525         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
18526                 error "IOC_MDC_GETFILEINFO returned garbage striping"
18527         fi
18528 }
18529 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
18530
18531 test_185() { # LU-2441
18532         # LU-3553 - no volatile file support in old servers
18533         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
18534                 skip "Need MDS version at least 2.3.60"
18535
18536         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
18537         touch $DIR/$tdir/spoo
18538         local mtime1=$(stat -c "%Y" $DIR/$tdir)
18539         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
18540                 error "cannot create/write a volatile file"
18541         [ "$FILESET" == "" ] &&
18542         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
18543                 error "FID is still valid after close"
18544
18545         multiop_bg_pause $DIR/$tdir vVw4096_c
18546         local multi_pid=$!
18547
18548         local OLD_IFS=$IFS
18549         IFS=":"
18550         local fidv=($fid)
18551         IFS=$OLD_IFS
18552         # assume that the next FID for this client is sequential, since stdout
18553         # is unfortunately eaten by multiop_bg_pause
18554         local n=$((${fidv[1]} + 1))
18555         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
18556         if [ "$FILESET" == "" ]; then
18557                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
18558                         error "FID is missing before close"
18559         fi
18560         kill -USR1 $multi_pid
18561         # 1 second delay, so if mtime change we will see it
18562         sleep 1
18563         local mtime2=$(stat -c "%Y" $DIR/$tdir)
18564         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
18565 }
18566 run_test 185 "Volatile file support"
18567
18568 function create_check_volatile() {
18569         local idx=$1
18570         local tgt
18571
18572         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
18573         local PID=$!
18574         sleep 1
18575         local FID=$(cat /tmp/${tfile}.fid)
18576         [ "$FID" == "" ] && error "can't get FID for volatile"
18577         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
18578         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
18579         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
18580         kill -USR1 $PID
18581         wait
18582         sleep 1
18583         cancel_lru_locks mdc # flush opencache
18584         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
18585         return 0
18586 }
18587
18588 test_185a(){
18589         # LU-12516 - volatile creation via .lustre
18590         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
18591                 skip "Need MDS version at least 2.3.55"
18592
18593         create_check_volatile 0
18594         [ $MDSCOUNT -lt 2 ] && return 0
18595
18596         # DNE case
18597         create_check_volatile 1
18598
18599         return 0
18600 }
18601 run_test 185a "Volatile file creation in .lustre/fid/"
18602
18603 test_187a() {
18604         remote_mds_nodsh && skip "remote MDS with nodsh"
18605         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
18606                 skip "Need MDS version at least 2.3.0"
18607
18608         local dir0=$DIR/$tdir/$testnum
18609         mkdir -p $dir0 || error "creating dir $dir0"
18610
18611         local file=$dir0/file1
18612         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
18613         local dv1=$($LFS data_version $file)
18614         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
18615         local dv2=$($LFS data_version $file)
18616         [[ $dv1 != $dv2 ]] ||
18617                 error "data version did not change on write $dv1 == $dv2"
18618
18619         # clean up
18620         rm -f $file1
18621 }
18622 run_test 187a "Test data version change"
18623
18624 test_187b() {
18625         remote_mds_nodsh && skip "remote MDS with nodsh"
18626         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
18627                 skip "Need MDS version at least 2.3.0"
18628
18629         local dir0=$DIR/$tdir/$testnum
18630         mkdir -p $dir0 || error "creating dir $dir0"
18631
18632         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
18633         [[ ${DV[0]} != ${DV[1]} ]] ||
18634                 error "data version did not change on write"\
18635                       " ${DV[0]} == ${DV[1]}"
18636
18637         # clean up
18638         rm -f $file1
18639 }
18640 run_test 187b "Test data version change on volatile file"
18641
18642 test_200() {
18643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18644         remote_mgs_nodsh && skip "remote MGS with nodsh"
18645         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
18646
18647         local POOL=${POOL:-cea1}
18648         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
18649         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
18650         # Pool OST targets
18651         local first_ost=0
18652         local last_ost=$(($OSTCOUNT - 1))
18653         local ost_step=2
18654         local ost_list=$(seq $first_ost $ost_step $last_ost)
18655         local ost_range="$first_ost $last_ost $ost_step"
18656         local test_path=$POOL_ROOT/$POOL_DIR_NAME
18657         local file_dir=$POOL_ROOT/file_tst
18658         local subdir=$test_path/subdir
18659         local rc=0
18660
18661         while : ; do
18662                 # former test_200a test_200b
18663                 pool_add $POOL                          || { rc=$? ; break; }
18664                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
18665                 # former test_200c test_200d
18666                 mkdir -p $test_path
18667                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
18668                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
18669                 mkdir -p $subdir
18670                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
18671                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
18672                                                         || { rc=$? ; break; }
18673                 # former test_200e test_200f
18674                 local files=$((OSTCOUNT*3))
18675                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
18676                                                         || { rc=$? ; break; }
18677                 pool_create_files $POOL $file_dir $files "$ost_list" \
18678                                                         || { rc=$? ; break; }
18679                 # former test_200g test_200h
18680                 pool_lfs_df $POOL                       || { rc=$? ; break; }
18681                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
18682
18683                 # former test_201a test_201b test_201c
18684                 pool_remove_first_target $POOL          || { rc=$? ; break; }
18685
18686                 local f=$test_path/$tfile
18687                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
18688                 pool_remove $POOL $f                    || { rc=$? ; break; }
18689                 break
18690         done
18691
18692         destroy_test_pools
18693
18694         return $rc
18695 }
18696 run_test 200 "OST pools"
18697
18698 # usage: default_attr <count | size | offset>
18699 default_attr() {
18700         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
18701 }
18702
18703 # usage: check_default_stripe_attr
18704 check_default_stripe_attr() {
18705         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
18706         case $1 in
18707         --stripe-count|-c)
18708                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
18709         --stripe-size|-S)
18710                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
18711         --stripe-index|-i)
18712                 EXPECTED=-1;;
18713         *)
18714                 error "unknown getstripe attr '$1'"
18715         esac
18716
18717         [ $ACTUAL == $EXPECTED ] ||
18718                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
18719 }
18720
18721 test_204a() {
18722         test_mkdir $DIR/$tdir
18723         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
18724
18725         check_default_stripe_attr --stripe-count
18726         check_default_stripe_attr --stripe-size
18727         check_default_stripe_attr --stripe-index
18728 }
18729 run_test 204a "Print default stripe attributes"
18730
18731 test_204b() {
18732         test_mkdir $DIR/$tdir
18733         $LFS setstripe --stripe-count 1 $DIR/$tdir
18734
18735         check_default_stripe_attr --stripe-size
18736         check_default_stripe_attr --stripe-index
18737 }
18738 run_test 204b "Print default stripe size and offset"
18739
18740 test_204c() {
18741         test_mkdir $DIR/$tdir
18742         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18743
18744         check_default_stripe_attr --stripe-count
18745         check_default_stripe_attr --stripe-index
18746 }
18747 run_test 204c "Print default stripe count and offset"
18748
18749 test_204d() {
18750         test_mkdir $DIR/$tdir
18751         $LFS setstripe --stripe-index 0 $DIR/$tdir
18752
18753         check_default_stripe_attr --stripe-count
18754         check_default_stripe_attr --stripe-size
18755 }
18756 run_test 204d "Print default stripe count and size"
18757
18758 test_204e() {
18759         test_mkdir $DIR/$tdir
18760         $LFS setstripe -d $DIR/$tdir
18761
18762         check_default_stripe_attr --stripe-count --raw
18763         check_default_stripe_attr --stripe-size --raw
18764         check_default_stripe_attr --stripe-index --raw
18765 }
18766 run_test 204e "Print raw stripe attributes"
18767
18768 test_204f() {
18769         test_mkdir $DIR/$tdir
18770         $LFS setstripe --stripe-count 1 $DIR/$tdir
18771
18772         check_default_stripe_attr --stripe-size --raw
18773         check_default_stripe_attr --stripe-index --raw
18774 }
18775 run_test 204f "Print raw stripe size and offset"
18776
18777 test_204g() {
18778         test_mkdir $DIR/$tdir
18779         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18780
18781         check_default_stripe_attr --stripe-count --raw
18782         check_default_stripe_attr --stripe-index --raw
18783 }
18784 run_test 204g "Print raw stripe count and offset"
18785
18786 test_204h() {
18787         test_mkdir $DIR/$tdir
18788         $LFS setstripe --stripe-index 0 $DIR/$tdir
18789
18790         check_default_stripe_attr --stripe-count --raw
18791         check_default_stripe_attr --stripe-size --raw
18792 }
18793 run_test 204h "Print raw stripe count and size"
18794
18795 # Figure out which job scheduler is being used, if any,
18796 # or use a fake one
18797 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
18798         JOBENV=SLURM_JOB_ID
18799 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
18800         JOBENV=LSB_JOBID
18801 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
18802         JOBENV=PBS_JOBID
18803 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
18804         JOBENV=LOADL_STEP_ID
18805 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
18806         JOBENV=JOB_ID
18807 else
18808         $LCTL list_param jobid_name > /dev/null 2>&1
18809         if [ $? -eq 0 ]; then
18810                 JOBENV=nodelocal
18811         else
18812                 JOBENV=FAKE_JOBID
18813         fi
18814 fi
18815 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
18816
18817 verify_jobstats() {
18818         local cmd=($1)
18819         shift
18820         local facets="$@"
18821
18822 # we don't really need to clear the stats for this test to work, since each
18823 # command has a unique jobid, but it makes debugging easier if needed.
18824 #       for facet in $facets; do
18825 #               local dev=$(convert_facet2label $facet)
18826 #               # clear old jobstats
18827 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
18828 #       done
18829
18830         # use a new JobID for each test, or we might see an old one
18831         [ "$JOBENV" = "FAKE_JOBID" ] &&
18832                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
18833
18834         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
18835
18836         [ "$JOBENV" = "nodelocal" ] && {
18837                 FAKE_JOBID=id.$testnum.%e.$RANDOM
18838                 $LCTL set_param jobid_name=$FAKE_JOBID
18839                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
18840         }
18841
18842         log "Test: ${cmd[*]}"
18843         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
18844
18845         if [ $JOBENV = "FAKE_JOBID" ]; then
18846                 FAKE_JOBID=$JOBVAL ${cmd[*]}
18847         else
18848                 ${cmd[*]}
18849         fi
18850
18851         # all files are created on OST0000
18852         for facet in $facets; do
18853                 local stats="*.$(convert_facet2label $facet).job_stats"
18854
18855                 # strip out libtool wrappers for in-tree executables
18856                 if (( $(do_facet $facet lctl get_param $stats |
18857                         sed -e 's/\.lt-/./' | grep -cw $JOBVAL) != 1 )); then
18858                         do_facet $facet lctl get_param $stats
18859                         error "No jobstats for $JOBVAL found on $facet::$stats"
18860                 fi
18861         done
18862 }
18863
18864 jobstats_set() {
18865         local new_jobenv=$1
18866
18867         set_persistent_param_and_check client "jobid_var" \
18868                 "$FSNAME.sys.jobid_var" $new_jobenv
18869 }
18870
18871 test_205a() { # Job stats
18872         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18873         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
18874                 skip "Need MDS version with at least 2.7.1"
18875         remote_mgs_nodsh && skip "remote MGS with nodsh"
18876         remote_mds_nodsh && skip "remote MDS with nodsh"
18877         remote_ost_nodsh && skip "remote OST with nodsh"
18878         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
18879                 skip "Server doesn't support jobstats"
18880         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
18881
18882         local old_jobenv=$($LCTL get_param -n jobid_var)
18883         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
18884
18885         if [[ $PERM_CMD == *"set_param -P"* ]]; then
18886                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
18887         else
18888                 stack_trap "do_facet mgs $PERM_CMD \
18889                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
18890         fi
18891         changelog_register
18892
18893         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
18894                                 mdt.*.job_cleanup_interval | head -n 1)
18895         local new_interval=5
18896         do_facet $SINGLEMDS \
18897                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
18898         stack_trap "do_facet $SINGLEMDS \
18899                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
18900         local start=$SECONDS
18901
18902         local cmd
18903         # mkdir
18904         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir"
18905         verify_jobstats "$cmd" "$SINGLEMDS"
18906         # rmdir
18907         cmd="rmdir $DIR/$tdir"
18908         verify_jobstats "$cmd" "$SINGLEMDS"
18909         # mkdir on secondary MDT
18910         if [ $MDSCOUNT -gt 1 ]; then
18911                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
18912                 verify_jobstats "$cmd" "mds2"
18913         fi
18914         # mknod
18915         cmd="mknod $DIR/$tfile c 1 3"
18916         verify_jobstats "$cmd" "$SINGLEMDS"
18917         # unlink
18918         cmd="rm -f $DIR/$tfile"
18919         verify_jobstats "$cmd" "$SINGLEMDS"
18920         # create all files on OST0000 so verify_jobstats can find OST stats
18921         # open & close
18922         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
18923         verify_jobstats "$cmd" "$SINGLEMDS"
18924         # setattr
18925         cmd="touch $DIR/$tfile"
18926         verify_jobstats "$cmd" "$SINGLEMDS ost1"
18927         # write
18928         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
18929         verify_jobstats "$cmd" "ost1"
18930         # read
18931         cancel_lru_locks osc
18932         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
18933         verify_jobstats "$cmd" "ost1"
18934         # truncate
18935         cmd="$TRUNCATE $DIR/$tfile 0"
18936         verify_jobstats "$cmd" "$SINGLEMDS ost1"
18937         # rename
18938         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
18939         verify_jobstats "$cmd" "$SINGLEMDS"
18940         # jobstats expiry - sleep until old stats should be expired
18941         local left=$((new_interval + 5 - (SECONDS - start)))
18942         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
18943                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
18944                         "0" $left
18945         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir.expire"
18946         verify_jobstats "$cmd" "$SINGLEMDS"
18947         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
18948             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
18949
18950         # Ensure that jobid are present in changelog (if supported by MDS)
18951         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
18952                 changelog_dump | tail -10
18953                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
18954                 [ $jobids -eq 9 ] ||
18955                         error "Wrong changelog jobid count $jobids != 9"
18956
18957                 # LU-5862
18958                 JOBENV="disable"
18959                 jobstats_set $JOBENV
18960                 touch $DIR/$tfile
18961                 changelog_dump | grep $tfile
18962                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
18963                 [ $jobids -eq 0 ] ||
18964                         error "Unexpected jobids when jobid_var=$JOBENV"
18965         fi
18966
18967         # test '%j' access to environment variable - if supported
18968         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"; then
18969                 JOBENV="JOBCOMPLEX"
18970                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18971
18972                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18973         fi
18974
18975         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%H.E"; then
18976                 JOBENV="JOBCOMPLEX"
18977                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname -s).E"
18978
18979                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18980         fi
18981
18982         # test '%j' access to per-session jobid - if supported
18983         if lctl list_param jobid_this_session > /dev/null 2>&1
18984         then
18985                 lctl set_param jobid_var=session jobid_name="S.%j.%e.%u.%h.E"
18986                 lctl set_param jobid_this_session=$USER
18987
18988                 JOBENV="JOBCOMPLEX"
18989                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
18990
18991                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
18992         fi
18993 }
18994 run_test 205a "Verify job stats"
18995
18996 # LU-13117, LU-13597
18997 test_205b() {
18998         (( $MDS1_VERSION >= $(version_code 2.13.54.91) )) ||
18999                 skip "Need MDS version at least 2.13.54.91"
19000
19001         local job_stats="mdt.*.job_stats"
19002         local old_jobid=$(do_facet mds1 $LCTL get_param jobid_var)
19003
19004         do_facet mds1 $LCTL set_param $job_stats=clear
19005
19006         # Setting jobid_var to USER might not be supported
19007         [[ -n "$old_jobid" ]] && stack_trap "$LCTL set_param $old_jobid"
19008         $LCTL set_param jobid_var=USER || true
19009         stack_trap "$LCTL set_param $($LCTL get_param jobid_name)"
19010         $LCTL set_param jobid_name="%j.%e.%u"
19011
19012         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
19013         do_facet mds1 $LCTL get_param $job_stats | grep "job_id:.*foolish" &&
19014                 { do_facet mds1 $LCTL get_param $job_stats;
19015                   error "Unexpected jobid found"; }
19016         do_facet mds1 $LCTL get_param $job_stats | grep "open:.*min.*max.*sum"||
19017                 { do_facet mds1 $LCTL get_param $job_stats;
19018                   error "wrong job_stats format found"; }
19019
19020         (( $MDS1_VERSION <= $(version_code 2.15.0) )) &&
19021                 echo "MDS does not yet escape jobid" && return 0
19022         $LCTL set_param jobid_var=TEST205b
19023         env -i TEST205b="has sp" touch $DIR/$tfile.2
19024         do_facet mds1 $LCTL get_param $job_stats | grep "has.*x20sp" ||
19025                 { do_facet mds1 $LCTL get_param $job_stats;
19026                   error "jobid not escaped"; }
19027 }
19028 run_test 205b "Verify job stats jobid and output format"
19029
19030 # LU-13733
19031 test_205c() {
19032         $LCTL set_param llite.*.stats=0
19033         dd if=/dev/zero of=$DIR/$tfile.1 bs=4k count=1
19034         $LCTL get_param llite.*.stats
19035         $LCTL get_param llite.*.stats | grep \
19036                 "write_bytes *1 samples \[bytes\] 4096 4096 4096 16777216" ||
19037                         error "wrong client stats format found"
19038 }
19039 run_test 205c "Verify client stats format"
19040
19041 # LU-1480, LU-1773 and LU-1657
19042 test_206() {
19043         mkdir -p $DIR/$tdir
19044         $LFS setstripe -c -1 $DIR/$tdir
19045 #define OBD_FAIL_LOV_INIT 0x1403
19046         $LCTL set_param fail_loc=0xa0001403
19047         $LCTL set_param fail_val=1
19048         touch $DIR/$tdir/$tfile || true
19049 }
19050 run_test 206 "fail lov_init_raid0() doesn't lbug"
19051
19052 test_207a() {
19053         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
19054         local fsz=`stat -c %s $DIR/$tfile`
19055         cancel_lru_locks mdc
19056
19057         # do not return layout in getattr intent
19058 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
19059         $LCTL set_param fail_loc=0x170
19060         local sz=`stat -c %s $DIR/$tfile`
19061
19062         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
19063
19064         rm -rf $DIR/$tfile
19065 }
19066 run_test 207a "can refresh layout at glimpse"
19067
19068 test_207b() {
19069         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
19070         local cksum=`md5sum $DIR/$tfile`
19071         local fsz=`stat -c %s $DIR/$tfile`
19072         cancel_lru_locks mdc
19073         cancel_lru_locks osc
19074
19075         # do not return layout in getattr intent
19076 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
19077         $LCTL set_param fail_loc=0x171
19078
19079         # it will refresh layout after the file is opened but before read issues
19080         echo checksum is "$cksum"
19081         echo "$cksum" |md5sum -c --quiet || error "file differs"
19082
19083         rm -rf $DIR/$tfile
19084 }
19085 run_test 207b "can refresh layout at open"
19086
19087 test_208() {
19088         # FIXME: in this test suite, only RD lease is used. This is okay
19089         # for now as only exclusive open is supported. After generic lease
19090         # is done, this test suite should be revised. - Jinshan
19091
19092         remote_mds_nodsh && skip "remote MDS with nodsh"
19093         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
19094                 skip "Need MDS version at least 2.4.52"
19095
19096         echo "==== test 1: verify get lease work"
19097         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
19098
19099         echo "==== test 2: verify lease can be broken by upcoming open"
19100         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
19101         local PID=$!
19102         sleep 2
19103
19104         $MULTIOP $DIR/$tfile oO_RDWR:c
19105         kill -USR1 $PID && wait $PID || error "break lease error"
19106
19107         echo "==== test 3: verify lease can't be granted if an open already exists"
19108         $MULTIOP $DIR/$tfile oO_RDWR:_c &
19109         local PID=$!
19110         sleep 2
19111
19112         $MULTIOP $DIR/$tfile oO_RDWR:eReUc && error "apply lease should fail"
19113         kill -USR1 $PID && wait $PID || error "open file error"
19114
19115         echo "==== test 4: lease can sustain over recovery"
19116         $MULTIOP $DIR/$tfile oO_RDWR:eR_E+eUc &
19117         PID=$!
19118         sleep 2
19119
19120         fail mds1
19121
19122         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
19123
19124         echo "==== test 5: lease broken can't be regained by replay"
19125         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
19126         PID=$!
19127         sleep 2
19128
19129         # open file to break lease and then recovery
19130         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
19131         fail mds1
19132
19133         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
19134
19135         rm -f $DIR/$tfile
19136 }
19137 run_test 208 "Exclusive open"
19138
19139 test_209() {
19140         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
19141                 skip_env "must have disp_stripe"
19142
19143         touch $DIR/$tfile
19144         sync; sleep 5; sync;
19145
19146         echo 3 > /proc/sys/vm/drop_caches
19147         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
19148                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
19149         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
19150
19151         # open/close 500 times
19152         for i in $(seq 500); do
19153                 cat $DIR/$tfile
19154         done
19155
19156         echo 3 > /proc/sys/vm/drop_caches
19157         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
19158                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
19159         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
19160
19161         echo "before: $req_before, after: $req_after"
19162         [ $((req_after - req_before)) -ge 300 ] &&
19163                 error "open/close requests are not freed"
19164         return 0
19165 }
19166 run_test 209 "read-only open/close requests should be freed promptly"
19167
19168 test_210() {
19169         local pid
19170
19171         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
19172         pid=$!
19173         sleep 1
19174
19175         $LFS getstripe $DIR/$tfile
19176         kill -USR1 $pid
19177         wait $pid || error "multiop failed"
19178
19179         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
19180         pid=$!
19181         sleep 1
19182
19183         $LFS getstripe $DIR/$tfile
19184         kill -USR1 $pid
19185         wait $pid || error "multiop failed"
19186 }
19187 run_test 210 "lfs getstripe does not break leases"
19188
19189 test_212() {
19190         size=`date +%s`
19191         size=$((size % 8192 + 1))
19192         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
19193         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
19194         rm -f $DIR/f212 $DIR/f212.xyz
19195 }
19196 run_test 212 "Sendfile test ============================================"
19197
19198 test_213() {
19199         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
19200         cancel_lru_locks osc
19201         lctl set_param fail_loc=0x8000040f
19202         # generate a read lock
19203         cat $DIR/$tfile > /dev/null
19204         # write to the file, it will try to cancel the above read lock.
19205         cat /etc/hosts >> $DIR/$tfile
19206 }
19207 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
19208
19209 test_214() { # for bug 20133
19210         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
19211         for (( i=0; i < 340; i++ )) ; do
19212                 touch $DIR/$tdir/d214c/a$i
19213         done
19214
19215         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
19216         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
19217         ls $DIR/d214c || error "ls $DIR/d214c failed"
19218         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
19219         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
19220 }
19221 run_test 214 "hash-indexed directory test - bug 20133"
19222
19223 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
19224 create_lnet_proc_files() {
19225         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
19226 }
19227
19228 # counterpart of create_lnet_proc_files
19229 remove_lnet_proc_files() {
19230         rm -f $TMP/lnet_$1.sys
19231 }
19232
19233 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
19234 # 3rd arg as regexp for body
19235 check_lnet_proc_stats() {
19236         local l=$(cat "$TMP/lnet_$1" |wc -l)
19237         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
19238
19239         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
19240 }
19241
19242 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
19243 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
19244 # optional and can be regexp for 2nd line (lnet.routes case)
19245 check_lnet_proc_entry() {
19246         local blp=2          # blp stands for 'position of 1st line of body'
19247         [ -z "$5" ] || blp=3 # lnet.routes case
19248
19249         local l=$(cat "$TMP/lnet_$1" |wc -l)
19250         # subtracting one from $blp because the body can be empty
19251         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
19252
19253         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
19254                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
19255
19256         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
19257                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
19258
19259         # bail out if any unexpected line happened
19260         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
19261         [ "$?" != 0 ] || error "$2 misformatted"
19262 }
19263
19264 test_215() { # for bugs 18102, 21079, 21517
19265         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19266
19267         local N='(0|[1-9][0-9]*)'       # non-negative numeric
19268         local P='[1-9][0-9]*'           # positive numeric
19269         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
19270         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
19271         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
19272         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
19273
19274         local L1 # regexp for 1st line
19275         local L2 # regexp for 2nd line (optional)
19276         local BR # regexp for the rest (body)
19277
19278         # lnet.stats should look as 11 space-separated non-negative numerics
19279         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
19280         create_lnet_proc_files "stats"
19281         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
19282         remove_lnet_proc_files "stats"
19283
19284         # lnet.routes should look like this:
19285         # Routing disabled/enabled
19286         # net hops priority state router
19287         # where net is a string like tcp0, hops > 0, priority >= 0,
19288         # state is up/down,
19289         # router is a string like 192.168.1.1@tcp2
19290         L1="^Routing (disabled|enabled)$"
19291         L2="^net +hops +priority +state +router$"
19292         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
19293         create_lnet_proc_files "routes"
19294         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
19295         remove_lnet_proc_files "routes"
19296
19297         # lnet.routers should look like this:
19298         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
19299         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
19300         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
19301         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
19302         L1="^ref +rtr_ref +alive +router$"
19303         BR="^$P +$P +(up|down) +$NID$"
19304         create_lnet_proc_files "routers"
19305         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
19306         remove_lnet_proc_files "routers"
19307
19308         # lnet.peers should look like this:
19309         # nid refs state last max rtr min tx min queue
19310         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
19311         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
19312         # numeric (0 or >0 or <0), queue >= 0.
19313         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
19314         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
19315         create_lnet_proc_files "peers"
19316         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
19317         remove_lnet_proc_files "peers"
19318
19319         # lnet.buffers  should look like this:
19320         # pages count credits min
19321         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
19322         L1="^pages +count +credits +min$"
19323         BR="^ +$N +$N +$I +$I$"
19324         create_lnet_proc_files "buffers"
19325         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
19326         remove_lnet_proc_files "buffers"
19327
19328         # lnet.nis should look like this:
19329         # nid status alive refs peer rtr max tx min
19330         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
19331         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
19332         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
19333         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
19334         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
19335         create_lnet_proc_files "nis"
19336         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
19337         remove_lnet_proc_files "nis"
19338
19339         # can we successfully write to lnet.stats?
19340         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
19341 }
19342 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
19343
19344 test_216() { # bug 20317
19345         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19346         remote_ost_nodsh && skip "remote OST with nodsh"
19347
19348         local node
19349         local facets=$(get_facets OST)
19350         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
19351
19352         save_lustre_params client "osc.*.contention_seconds" > $p
19353         save_lustre_params $facets \
19354                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
19355         save_lustre_params $facets \
19356                 "ldlm.namespaces.filter-*.contended_locks" >> $p
19357         save_lustre_params $facets \
19358                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
19359         clear_stats osc.*.osc_stats
19360
19361         # agressive lockless i/o settings
19362         do_nodes $(comma_list $(osts_nodes)) \
19363                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
19364                         ldlm.namespaces.filter-*.contended_locks=0 \
19365                         ldlm.namespaces.filter-*.contention_seconds=60"
19366         lctl set_param -n osc.*.contention_seconds=60
19367
19368         $DIRECTIO write $DIR/$tfile 0 10 4096
19369         $CHECKSTAT -s 40960 $DIR/$tfile
19370
19371         # disable lockless i/o
19372         do_nodes $(comma_list $(osts_nodes)) \
19373                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
19374                         ldlm.namespaces.filter-*.contended_locks=32 \
19375                         ldlm.namespaces.filter-*.contention_seconds=0"
19376         lctl set_param -n osc.*.contention_seconds=0
19377         clear_stats osc.*.osc_stats
19378
19379         dd if=/dev/zero of=$DIR/$tfile count=0
19380         $CHECKSTAT -s 0 $DIR/$tfile
19381
19382         restore_lustre_params <$p
19383         rm -f $p
19384         rm $DIR/$tfile
19385 }
19386 run_test 216 "check lockless direct write updates file size and kms correctly"
19387
19388 test_217() { # bug 22430
19389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19390
19391         local node
19392         local nid
19393
19394         for node in $(nodes_list); do
19395                 nid=$(host_nids_address $node $NETTYPE)
19396                 if [[ $nid = *-* ]] ; then
19397                         echo "lctl ping $(h2nettype $nid)"
19398                         lctl ping $(h2nettype $nid)
19399                 else
19400                         echo "skipping $node (no hyphen detected)"
19401                 fi
19402         done
19403 }
19404 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
19405
19406 test_218() {
19407        # do directio so as not to populate the page cache
19408        log "creating a 10 Mb file"
19409        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
19410        log "starting reads"
19411        dd if=$DIR/$tfile of=/dev/null bs=4096 &
19412        log "truncating the file"
19413        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
19414        log "killing dd"
19415        kill %+ || true # reads might have finished
19416        echo "wait until dd is finished"
19417        wait
19418        log "removing the temporary file"
19419        rm -rf $DIR/$tfile || error "tmp file removal failed"
19420 }
19421 run_test 218 "parallel read and truncate should not deadlock"
19422
19423 test_219() {
19424         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19425
19426         # write one partial page
19427         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
19428         # set no grant so vvp_io_commit_write will do sync write
19429         $LCTL set_param fail_loc=0x411
19430         # write a full page at the end of file
19431         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
19432
19433         $LCTL set_param fail_loc=0
19434         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
19435         $LCTL set_param fail_loc=0x411
19436         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
19437
19438         # LU-4201
19439         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
19440         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
19441 }
19442 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
19443
19444 test_220() { #LU-325
19445         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19446         remote_ost_nodsh && skip "remote OST with nodsh"
19447         remote_mds_nodsh && skip "remote MDS with nodsh"
19448         remote_mgs_nodsh && skip "remote MGS with nodsh"
19449
19450         local OSTIDX=0
19451
19452         # create on MDT0000 so the last_id and next_id are correct
19453         mkdir_on_mdt0 $DIR/$tdir
19454         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
19455         OST=${OST%_UUID}
19456
19457         # on the mdt's osc
19458         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
19459         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
19460                         osp.$mdtosc_proc1.prealloc_last_id)
19461         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
19462                         osp.$mdtosc_proc1.prealloc_next_id)
19463
19464         $LFS df -i
19465
19466         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
19467         #define OBD_FAIL_OST_ENOINO              0x229
19468         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
19469         create_pool $FSNAME.$TESTNAME || return 1
19470         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
19471
19472         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
19473
19474         MDSOBJS=$((last_id - next_id))
19475         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
19476
19477         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
19478         echo "OST still has $count kbytes free"
19479
19480         echo "create $MDSOBJS files @next_id..."
19481         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
19482
19483         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
19484                         osp.$mdtosc_proc1.prealloc_last_id)
19485         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
19486                         osp.$mdtosc_proc1.prealloc_next_id)
19487
19488         echo "after creation, last_id=$last_id2, next_id=$next_id2"
19489         $LFS df -i
19490
19491         echo "cleanup..."
19492
19493         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
19494         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
19495
19496         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
19497                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
19498         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
19499                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
19500         echo "unlink $MDSOBJS files @$next_id..."
19501         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
19502 }
19503 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
19504
19505 test_221() {
19506         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19507
19508         dd if=`which date` of=$MOUNT/date oflag=sync
19509         chmod +x $MOUNT/date
19510
19511         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
19512         $LCTL set_param fail_loc=0x80001401
19513
19514         $MOUNT/date > /dev/null
19515         rm -f $MOUNT/date
19516 }
19517 run_test 221 "make sure fault and truncate race to not cause OOM"
19518
19519 test_222a () {
19520         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19521
19522         rm -rf $DIR/$tdir
19523         test_mkdir $DIR/$tdir
19524         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19525         createmany -o $DIR/$tdir/$tfile 10
19526         cancel_lru_locks mdc
19527         cancel_lru_locks osc
19528         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
19529         $LCTL set_param fail_loc=0x31a
19530         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
19531         $LCTL set_param fail_loc=0
19532         rm -r $DIR/$tdir
19533 }
19534 run_test 222a "AGL for ls should not trigger CLIO lock failure"
19535
19536 test_222b () {
19537         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19538
19539         rm -rf $DIR/$tdir
19540         test_mkdir $DIR/$tdir
19541         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19542         createmany -o $DIR/$tdir/$tfile 10
19543         cancel_lru_locks mdc
19544         cancel_lru_locks osc
19545         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
19546         $LCTL set_param fail_loc=0x31a
19547         rm -r $DIR/$tdir || error "AGL for rmdir failed"
19548         $LCTL set_param fail_loc=0
19549 }
19550 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
19551
19552 test_223 () {
19553         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19554
19555         rm -rf $DIR/$tdir
19556         test_mkdir $DIR/$tdir
19557         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19558         createmany -o $DIR/$tdir/$tfile 10
19559         cancel_lru_locks mdc
19560         cancel_lru_locks osc
19561         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
19562         $LCTL set_param fail_loc=0x31b
19563         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
19564         $LCTL set_param fail_loc=0
19565         rm -r $DIR/$tdir
19566 }
19567 run_test 223 "osc reenqueue if without AGL lock granted ======================="
19568
19569 test_224a() { # LU-1039, MRP-303
19570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19571         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
19572         $LCTL set_param fail_loc=0x508
19573         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 conv=fsync
19574         $LCTL set_param fail_loc=0
19575         df $DIR
19576 }
19577 run_test 224a "Don't panic on bulk IO failure"
19578
19579 test_224bd_sub() { # LU-1039, MRP-303
19580         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19581         local timeout=$1
19582
19583         shift
19584         dd if=/dev/urandom of=$TMP/$tfile bs=1M count=1
19585
19586         $LFS setstripe -c 1 -i 0 $DIR/$tfile
19587
19588         dd if=$TMP/$tfile of=$DIR/$tfile bs=1M count=1
19589         cancel_lru_locks osc
19590         set_checksums 0
19591         stack_trap "set_checksums $ORIG_CSUM" EXIT
19592         local at_max_saved=0
19593
19594         # adaptive timeouts may prevent seeing the issue
19595         if at_is_enabled; then
19596                 at_max_saved=$(at_max_get mds)
19597                 at_max_set 0 mds client
19598                 stack_trap "at_max_set $at_max_saved mds client" EXIT
19599         fi
19600
19601         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
19602         do_facet ost1 $LCTL set_param fail_val=$timeout fail_loc=0x80000515
19603         dd of=$TMP/$tfile.new if=$DIR/$tfile bs=1M count=1 || "$@"
19604
19605         do_facet ost1 $LCTL set_param fail_loc=0
19606         cmp $TMP/$tfile $TMP/$tfile.new || error "file contents wrong"
19607         df $DIR
19608 }
19609
19610 test_224b() {
19611         test_224bd_sub 3 error "dd failed"
19612 }
19613 run_test 224b "Don't panic on bulk IO failure"
19614
19615 test_224c() { # LU-6441
19616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19617         remote_mds_nodsh && skip "remote MDS with nodsh"
19618
19619         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
19620         save_writethrough $p
19621         set_cache writethrough on
19622
19623         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
19624         local at_max=$($LCTL get_param -n at_max)
19625         local timeout=$($LCTL get_param -n timeout)
19626         local test_at="at_max"
19627         local param_at="$FSNAME.sys.at_max"
19628         local test_timeout="timeout"
19629         local param_timeout="$FSNAME.sys.timeout"
19630
19631         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
19632
19633         set_persistent_param_and_check client "$test_at" "$param_at" 0
19634         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
19635
19636         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
19637         do_facet ost1 "$LCTL set_param fail_loc=0x520"
19638         $LFS setstripe -c 1 -i 0 $DIR/$tfile
19639         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
19640         sync
19641         do_facet ost1 "$LCTL set_param fail_loc=0"
19642
19643         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
19644         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
19645                 $timeout
19646
19647         $LCTL set_param -n $pages_per_rpc
19648         restore_lustre_params < $p
19649         rm -f $p
19650 }
19651 run_test 224c "Don't hang if one of md lost during large bulk RPC"
19652
19653 test_224d() { # LU-11169
19654         test_224bd_sub $((TIMEOUT + 2)) error "dd failed"
19655 }
19656 run_test 224d "Don't corrupt data on bulk IO timeout"
19657
19658 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
19659 test_225a () {
19660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19661         if [ -z ${MDSSURVEY} ]; then
19662                 skip_env "mds-survey not found"
19663         fi
19664         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
19665                 skip "Need MDS version at least 2.2.51"
19666
19667         local mds=$(facet_host $SINGLEMDS)
19668         local target=$(do_nodes $mds 'lctl dl' |
19669                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
19670
19671         local cmd1="file_count=1000 thrhi=4"
19672         local cmd2="dir_count=2 layer=mdd stripe_count=0"
19673         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
19674         local cmd="$cmd1 $cmd2 $cmd3"
19675
19676         rm -f ${TMP}/mds_survey*
19677         echo + $cmd
19678         eval $cmd || error "mds-survey with zero-stripe failed"
19679         cat ${TMP}/mds_survey*
19680         rm -f ${TMP}/mds_survey*
19681 }
19682 run_test 225a "Metadata survey sanity with zero-stripe"
19683
19684 test_225b () {
19685         if [ -z ${MDSSURVEY} ]; then
19686                 skip_env "mds-survey not found"
19687         fi
19688         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
19689                 skip "Need MDS version at least 2.2.51"
19690         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19691         remote_mds_nodsh && skip "remote MDS with nodsh"
19692         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
19693                 skip_env "Need to mount OST to test"
19694         fi
19695
19696         local mds=$(facet_host $SINGLEMDS)
19697         local target=$(do_nodes $mds 'lctl dl' |
19698                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
19699
19700         local cmd1="file_count=1000 thrhi=4"
19701         local cmd2="dir_count=2 layer=mdd stripe_count=1"
19702         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
19703         local cmd="$cmd1 $cmd2 $cmd3"
19704
19705         rm -f ${TMP}/mds_survey*
19706         echo + $cmd
19707         eval $cmd || error "mds-survey with stripe_count failed"
19708         cat ${TMP}/mds_survey*
19709         rm -f ${TMP}/mds_survey*
19710 }
19711 run_test 225b "Metadata survey sanity with stripe_count = 1"
19712
19713 mcreate_path2fid () {
19714         local mode=$1
19715         local major=$2
19716         local minor=$3
19717         local name=$4
19718         local desc=$5
19719         local path=$DIR/$tdir/$name
19720         local fid
19721         local rc
19722         local fid_path
19723
19724         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
19725                 error "cannot create $desc"
19726
19727         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
19728         rc=$?
19729         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
19730
19731         fid_path=$($LFS fid2path $MOUNT $fid)
19732         rc=$?
19733         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
19734
19735         [ "$path" == "$fid_path" ] ||
19736                 error "fid2path returned $fid_path, expected $path"
19737
19738         echo "pass with $path and $fid"
19739 }
19740
19741 test_226a () {
19742         rm -rf $DIR/$tdir
19743         mkdir -p $DIR/$tdir
19744
19745         mcreate_path2fid 0010666 0 0 fifo "FIFO"
19746         mcreate_path2fid 0020666 1 3 null "character special file (null)"
19747         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
19748         mcreate_path2fid 0040666 0 0 dir "directory"
19749         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
19750         mcreate_path2fid 0100666 0 0 file "regular file"
19751         mcreate_path2fid 0120666 0 0 link "symbolic link"
19752         mcreate_path2fid 0140666 0 0 sock "socket"
19753 }
19754 run_test 226a "call path2fid and fid2path on files of all type"
19755
19756 test_226b () {
19757         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19758
19759         local MDTIDX=1
19760
19761         rm -rf $DIR/$tdir
19762         mkdir -p $DIR/$tdir
19763         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
19764                 error "create remote directory failed"
19765         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
19766         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
19767                                 "character special file (null)"
19768         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
19769                                 "character special file (no device)"
19770         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
19771         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
19772                                 "block special file (loop)"
19773         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
19774         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
19775         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
19776 }
19777 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
19778
19779 test_226c () {
19780         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19781         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
19782                 skip "Need MDS version at least 2.13.55"
19783
19784         local submnt=/mnt/submnt
19785         local srcfile=/etc/passwd
19786         local dstfile=$submnt/passwd
19787         local path
19788         local fid
19789
19790         rm -rf $DIR/$tdir
19791         rm -rf $submnt
19792         $LFS setdirstripe -c -1 -i 1 $DIR/$tdir ||
19793                 error "create remote directory failed"
19794         mkdir -p $submnt || error "create $submnt failed"
19795         $MOUNT_CMD $MGSNID:/$FSNAME/$tdir $submnt ||
19796                 error "mount $submnt failed"
19797         stack_trap "umount $submnt" EXIT
19798
19799         cp $srcfile $dstfile
19800         fid=$($LFS path2fid $dstfile)
19801         path=$($LFS fid2path $submnt "$fid")
19802         [ "$path" = "$dstfile" ] ||
19803                 error "fid2path $submnt $fid failed ($path != $dstfile)"
19804 }
19805 run_test 226c "call path2fid and fid2path under remote dir with subdir mount"
19806
19807 # LU-1299 Executing or running ldd on a truncated executable does not
19808 # cause an out-of-memory condition.
19809 test_227() {
19810         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19811         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
19812
19813         dd if=$(which date) of=$MOUNT/date bs=1k count=1
19814         chmod +x $MOUNT/date
19815
19816         $MOUNT/date > /dev/null
19817         ldd $MOUNT/date > /dev/null
19818         rm -f $MOUNT/date
19819 }
19820 run_test 227 "running truncated executable does not cause OOM"
19821
19822 # LU-1512 try to reuse idle OI blocks
19823 test_228a() {
19824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19825         remote_mds_nodsh && skip "remote MDS with nodsh"
19826         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19827
19828         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19829         local myDIR=$DIR/$tdir
19830
19831         mkdir -p $myDIR
19832         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19833         $LCTL set_param fail_loc=0x80001002
19834         createmany -o $myDIR/t- 10000
19835         $LCTL set_param fail_loc=0
19836         # The guard is current the largest FID holder
19837         touch $myDIR/guard
19838         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19839                     tr -d '[')
19840         local IDX=$(($SEQ % 64))
19841
19842         do_facet $SINGLEMDS sync
19843         # Make sure journal flushed.
19844         sleep 6
19845         local blk1=$(do_facet $SINGLEMDS \
19846                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19847                      grep Blockcount | awk '{print $4}')
19848
19849         # Remove old files, some OI blocks will become idle.
19850         unlinkmany $myDIR/t- 10000
19851         # Create new files, idle OI blocks should be reused.
19852         createmany -o $myDIR/t- 2000
19853         do_facet $SINGLEMDS sync
19854         # Make sure journal flushed.
19855         sleep 6
19856         local blk2=$(do_facet $SINGLEMDS \
19857                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19858                      grep Blockcount | awk '{print $4}')
19859
19860         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19861 }
19862 run_test 228a "try to reuse idle OI blocks"
19863
19864 test_228b() {
19865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19866         remote_mds_nodsh && skip "remote MDS with nodsh"
19867         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19868
19869         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19870         local myDIR=$DIR/$tdir
19871
19872         mkdir -p $myDIR
19873         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19874         $LCTL set_param fail_loc=0x80001002
19875         createmany -o $myDIR/t- 10000
19876         $LCTL set_param fail_loc=0
19877         # The guard is current the largest FID holder
19878         touch $myDIR/guard
19879         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19880                     tr -d '[')
19881         local IDX=$(($SEQ % 64))
19882
19883         do_facet $SINGLEMDS sync
19884         # Make sure journal flushed.
19885         sleep 6
19886         local blk1=$(do_facet $SINGLEMDS \
19887                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19888                      grep Blockcount | awk '{print $4}')
19889
19890         # Remove old files, some OI blocks will become idle.
19891         unlinkmany $myDIR/t- 10000
19892
19893         # stop the MDT
19894         stop $SINGLEMDS || error "Fail to stop MDT."
19895         # remount the MDT
19896         start $SINGLEMDS $(facet_device $SINGLEMDS) $MDS_MOUNT_OPTS ||
19897                 error "Fail to start MDT."
19898
19899         df $MOUNT || error "Fail to df."
19900         # Create new files, idle OI blocks should be reused.
19901         createmany -o $myDIR/t- 2000
19902         do_facet $SINGLEMDS sync
19903         # Make sure journal flushed.
19904         sleep 6
19905         local blk2=$(do_facet $SINGLEMDS \
19906                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19907                      grep Blockcount | awk '{print $4}')
19908
19909         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19910 }
19911 run_test 228b "idle OI blocks can be reused after MDT restart"
19912
19913 #LU-1881
19914 test_228c() {
19915         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19916         remote_mds_nodsh && skip "remote MDS with nodsh"
19917         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19918
19919         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19920         local myDIR=$DIR/$tdir
19921
19922         mkdir -p $myDIR
19923         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19924         $LCTL set_param fail_loc=0x80001002
19925         # 20000 files can guarantee there are index nodes in the OI file
19926         createmany -o $myDIR/t- 20000
19927         $LCTL set_param fail_loc=0
19928         # The guard is current the largest FID holder
19929         touch $myDIR/guard
19930         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19931                     tr -d '[')
19932         local IDX=$(($SEQ % 64))
19933
19934         do_facet $SINGLEMDS sync
19935         # Make sure journal flushed.
19936         sleep 6
19937         local blk1=$(do_facet $SINGLEMDS \
19938                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19939                      grep Blockcount | awk '{print $4}')
19940
19941         # Remove old files, some OI blocks will become idle.
19942         unlinkmany $myDIR/t- 20000
19943         rm -f $myDIR/guard
19944         # The OI file should become empty now
19945
19946         # Create new files, idle OI blocks should be reused.
19947         createmany -o $myDIR/t- 2000
19948         do_facet $SINGLEMDS sync
19949         # Make sure journal flushed.
19950         sleep 6
19951         local blk2=$(do_facet $SINGLEMDS \
19952                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19953                      grep Blockcount | awk '{print $4}')
19954
19955         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19956 }
19957 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
19958
19959 test_229() { # LU-2482, LU-3448
19960         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19961         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
19962         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
19963                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
19964
19965         rm -f $DIR/$tfile
19966
19967         # Create a file with a released layout and stripe count 2.
19968         $MULTIOP $DIR/$tfile H2c ||
19969                 error "failed to create file with released layout"
19970
19971         $LFS getstripe -v $DIR/$tfile
19972
19973         local pattern=$($LFS getstripe -L $DIR/$tfile)
19974         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
19975
19976         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
19977                 error "getstripe"
19978         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
19979         stat $DIR/$tfile || error "failed to stat released file"
19980
19981         chown $RUNAS_ID $DIR/$tfile ||
19982                 error "chown $RUNAS_ID $DIR/$tfile failed"
19983
19984         chgrp $RUNAS_ID $DIR/$tfile ||
19985                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
19986
19987         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
19988         rm $DIR/$tfile || error "failed to remove released file"
19989 }
19990 run_test 229 "getstripe/stat/rm/attr changes work on released files"
19991
19992 test_230a() {
19993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19994         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19995         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
19996                 skip "Need MDS version at least 2.11.52"
19997
19998         local MDTIDX=1
19999
20000         test_mkdir $DIR/$tdir
20001         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
20002         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
20003         [ $mdt_idx -ne 0 ] &&
20004                 error "create local directory on wrong MDT $mdt_idx"
20005
20006         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
20007                         error "create remote directory failed"
20008         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
20009         [ $mdt_idx -ne $MDTIDX ] &&
20010                 error "create remote directory on wrong MDT $mdt_idx"
20011
20012         createmany -o $DIR/$tdir/test_230/t- 10 ||
20013                 error "create files on remote directory failed"
20014         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
20015         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
20016         rm -r $DIR/$tdir || error "unlink remote directory failed"
20017 }
20018 run_test 230a "Create remote directory and files under the remote directory"
20019
20020 test_230b() {
20021         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20022         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20023         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20024                 skip "Need MDS version at least 2.11.52"
20025
20026         local MDTIDX=1
20027         local mdt_index
20028         local i
20029         local file
20030         local pid
20031         local stripe_count
20032         local migrate_dir=$DIR/$tdir/migrate_dir
20033         local other_dir=$DIR/$tdir/other_dir
20034
20035         test_mkdir $DIR/$tdir
20036         test_mkdir -i0 -c1 $migrate_dir
20037         test_mkdir -i0 -c1 $other_dir
20038         for ((i=0; i<10; i++)); do
20039                 mkdir -p $migrate_dir/dir_${i}
20040                 createmany -o $migrate_dir/dir_${i}/f 10 ||
20041                         error "create files under remote dir failed $i"
20042         done
20043
20044         cp /etc/passwd $migrate_dir/$tfile
20045         cp /etc/passwd $other_dir/$tfile
20046         chattr +SAD $migrate_dir
20047         chattr +SAD $migrate_dir/$tfile
20048
20049         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20050         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20051         local old_dir_mode=$(stat -c%f $migrate_dir)
20052         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
20053
20054         mkdir -p $migrate_dir/dir_default_stripe2
20055         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
20056         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
20057
20058         mkdir -p $other_dir
20059         ln $migrate_dir/$tfile $other_dir/luna
20060         ln $migrate_dir/$tfile $migrate_dir/sofia
20061         ln $other_dir/$tfile $migrate_dir/david
20062         ln -s $migrate_dir/$tfile $other_dir/zachary
20063         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
20064         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
20065
20066         local len
20067         local lnktgt
20068
20069         # inline symlink
20070         for len in 58 59 60; do
20071                 lnktgt=$(str_repeat 'l' $len)
20072                 touch $migrate_dir/$lnktgt
20073                 ln -s $lnktgt $migrate_dir/${len}char_ln
20074         done
20075
20076         # PATH_MAX
20077         for len in 4094 4095; do
20078                 lnktgt=$(str_repeat 'l' $len)
20079                 ln -s $lnktgt $migrate_dir/${len}char_ln
20080         done
20081
20082         # NAME_MAX
20083         for len in 254 255; do
20084                 touch $migrate_dir/$(str_repeat 'l' $len)
20085         done
20086
20087         $LFS migrate -m $MDTIDX $migrate_dir ||
20088                 error "fails on migrating remote dir to MDT1"
20089
20090         echo "migratate to MDT1, then checking.."
20091         for ((i = 0; i < 10; i++)); do
20092                 for file in $(find $migrate_dir/dir_${i}); do
20093                         mdt_index=$($LFS getstripe -m $file)
20094                         # broken symlink getstripe will fail
20095                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
20096                                 error "$file is not on MDT${MDTIDX}"
20097                 done
20098         done
20099
20100         # the multiple link file should still in MDT0
20101         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
20102         [ $mdt_index == 0 ] ||
20103                 error "$file is not on MDT${MDTIDX}"
20104
20105         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20106         [ "$old_dir_flag" = "$new_dir_flag" ] ||
20107                 error " expect $old_dir_flag get $new_dir_flag"
20108
20109         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20110         [ "$old_file_flag" = "$new_file_flag" ] ||
20111                 error " expect $old_file_flag get $new_file_flag"
20112
20113         local new_dir_mode=$(stat -c%f $migrate_dir)
20114         [ "$old_dir_mode" = "$new_dir_mode" ] ||
20115                 error "expect mode $old_dir_mode get $new_dir_mode"
20116
20117         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
20118         [ "$old_file_mode" = "$new_file_mode" ] ||
20119                 error "expect mode $old_file_mode get $new_file_mode"
20120
20121         diff /etc/passwd $migrate_dir/$tfile ||
20122                 error "$tfile different after migration"
20123
20124         diff /etc/passwd $other_dir/luna ||
20125                 error "luna different after migration"
20126
20127         diff /etc/passwd $migrate_dir/sofia ||
20128                 error "sofia different after migration"
20129
20130         diff /etc/passwd $migrate_dir/david ||
20131                 error "david different after migration"
20132
20133         diff /etc/passwd $other_dir/zachary ||
20134                 error "zachary different after migration"
20135
20136         diff /etc/passwd $migrate_dir/${tfile}_ln ||
20137                 error "${tfile}_ln different after migration"
20138
20139         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
20140                 error "${tfile}_ln_other different after migration"
20141
20142         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
20143         [ $stripe_count = 2 ] ||
20144                 error "dir strpe_count $d != 2 after migration."
20145
20146         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
20147         [ $stripe_count = 2 ] ||
20148                 error "file strpe_count $d != 2 after migration."
20149
20150         #migrate back to MDT0
20151         MDTIDX=0
20152
20153         $LFS migrate -m $MDTIDX $migrate_dir ||
20154                 error "fails on migrating remote dir to MDT0"
20155
20156         echo "migrate back to MDT0, checking.."
20157         for file in $(find $migrate_dir); do
20158                 mdt_index=$($LFS getstripe -m $file)
20159                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
20160                         error "$file is not on MDT${MDTIDX}"
20161         done
20162
20163         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20164         [ "$old_dir_flag" = "$new_dir_flag" ] ||
20165                 error " expect $old_dir_flag get $new_dir_flag"
20166
20167         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20168         [ "$old_file_flag" = "$new_file_flag" ] ||
20169                 error " expect $old_file_flag get $new_file_flag"
20170
20171         local new_dir_mode=$(stat -c%f $migrate_dir)
20172         [ "$old_dir_mode" = "$new_dir_mode" ] ||
20173                 error "expect mode $old_dir_mode get $new_dir_mode"
20174
20175         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
20176         [ "$old_file_mode" = "$new_file_mode" ] ||
20177                 error "expect mode $old_file_mode get $new_file_mode"
20178
20179         diff /etc/passwd ${migrate_dir}/$tfile ||
20180                 error "$tfile different after migration"
20181
20182         diff /etc/passwd ${other_dir}/luna ||
20183                 error "luna different after migration"
20184
20185         diff /etc/passwd ${migrate_dir}/sofia ||
20186                 error "sofia different after migration"
20187
20188         diff /etc/passwd ${other_dir}/zachary ||
20189                 error "zachary different after migration"
20190
20191         diff /etc/passwd $migrate_dir/${tfile}_ln ||
20192                 error "${tfile}_ln different after migration"
20193
20194         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
20195                 error "${tfile}_ln_other different after migration"
20196
20197         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
20198         [ $stripe_count = 2 ] ||
20199                 error "dir strpe_count $d != 2 after migration."
20200
20201         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
20202         [ $stripe_count = 2 ] ||
20203                 error "file strpe_count $d != 2 after migration."
20204
20205         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20206 }
20207 run_test 230b "migrate directory"
20208
20209 test_230c() {
20210         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20211         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20212         remote_mds_nodsh && skip "remote MDS with nodsh"
20213         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20214                 skip "Need MDS version at least 2.11.52"
20215
20216         local MDTIDX=1
20217         local total=3
20218         local mdt_index
20219         local file
20220         local migrate_dir=$DIR/$tdir/migrate_dir
20221
20222         #If migrating directory fails in the middle, all entries of
20223         #the directory is still accessiable.
20224         test_mkdir $DIR/$tdir
20225         test_mkdir -i0 -c1 $migrate_dir
20226         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
20227         stat $migrate_dir
20228         createmany -o $migrate_dir/f $total ||
20229                 error "create files under ${migrate_dir} failed"
20230
20231         # fail after migrating top dir, and this will fail only once, so the
20232         # first sub file migration will fail (currently f3), others succeed.
20233         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
20234         do_facet mds1 lctl set_param fail_loc=0x1801
20235         local t=$(ls $migrate_dir | wc -l)
20236         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
20237                 error "migrate should fail"
20238         local u=$(ls $migrate_dir | wc -l)
20239         [ "$u" == "$t" ] || error "$u != $t during migration"
20240
20241         # add new dir/file should succeed
20242         mkdir $migrate_dir/dir ||
20243                 error "mkdir failed under migrating directory"
20244         touch $migrate_dir/file ||
20245                 error "create file failed under migrating directory"
20246
20247         # add file with existing name should fail
20248         for file in $migrate_dir/f*; do
20249                 stat $file > /dev/null || error "stat $file failed"
20250                 $OPENFILE -f O_CREAT:O_EXCL $file &&
20251                         error "open(O_CREAT|O_EXCL) $file should fail"
20252                 $MULTIOP $file m && error "create $file should fail"
20253                 touch $DIR/$tdir/remote_dir/$tfile ||
20254                         error "touch $tfile failed"
20255                 ln $DIR/$tdir/remote_dir/$tfile $file &&
20256                         error "link $file should fail"
20257                 mdt_index=$($LFS getstripe -m $file)
20258                 if [ $mdt_index == 0 ]; then
20259                         # file failed to migrate is not allowed to rename to
20260                         mv $DIR/$tdir/remote_dir/$tfile $file &&
20261                                 error "rename to $file should fail"
20262                 else
20263                         mv $DIR/$tdir/remote_dir/$tfile $file ||
20264                                 error "rename to $file failed"
20265                 fi
20266                 echo hello >> $file || error "write $file failed"
20267         done
20268
20269         # resume migration with different options should fail
20270         $LFS migrate -m 0 $migrate_dir &&
20271                 error "migrate -m 0 $migrate_dir should fail"
20272
20273         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
20274                 error "migrate -c 2 $migrate_dir should fail"
20275
20276         # resume migration should succeed
20277         $LFS migrate -m $MDTIDX $migrate_dir ||
20278                 error "migrate $migrate_dir failed"
20279
20280         echo "Finish migration, then checking.."
20281         for file in $(find $migrate_dir); do
20282                 mdt_index=$($LFS getstripe -m $file)
20283                 [ $mdt_index == $MDTIDX ] ||
20284                         error "$file is not on MDT${MDTIDX}"
20285         done
20286
20287         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20288 }
20289 run_test 230c "check directory accessiblity if migration failed"
20290
20291 test_230d() {
20292         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20293         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20294         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20295                 skip "Need MDS version at least 2.11.52"
20296         # LU-11235
20297         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
20298
20299         local migrate_dir=$DIR/$tdir/migrate_dir
20300         local old_index
20301         local new_index
20302         local old_count
20303         local new_count
20304         local new_hash
20305         local mdt_index
20306         local i
20307         local j
20308
20309         old_index=$((RANDOM % MDSCOUNT))
20310         old_count=$((MDSCOUNT - old_index))
20311         new_index=$((RANDOM % MDSCOUNT))
20312         new_count=$((MDSCOUNT - new_index))
20313         new_hash=1 # for all_char
20314
20315         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
20316         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
20317
20318         test_mkdir $DIR/$tdir
20319         test_mkdir -i $old_index -c $old_count $migrate_dir
20320
20321         for ((i=0; i<100; i++)); do
20322                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
20323                 createmany -o $migrate_dir/dir_${i}/f 100 ||
20324                         error "create files under remote dir failed $i"
20325         done
20326
20327         echo -n "Migrate from MDT$old_index "
20328         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
20329         echo -n "to MDT$new_index"
20330         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
20331         echo
20332
20333         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
20334         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
20335                 error "migrate remote dir error"
20336
20337         echo "Finish migration, then checking.."
20338         for file in $(find $migrate_dir -maxdepth 1); do
20339                 mdt_index=$($LFS getstripe -m $file)
20340                 if [ $mdt_index -lt $new_index ] ||
20341                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
20342                         error "$file is on MDT$mdt_index"
20343                 fi
20344         done
20345
20346         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20347 }
20348 run_test 230d "check migrate big directory"
20349
20350 test_230e() {
20351         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20352         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20353         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20354                 skip "Need MDS version at least 2.11.52"
20355
20356         local i
20357         local j
20358         local a_fid
20359         local b_fid
20360
20361         mkdir_on_mdt0 $DIR/$tdir
20362         mkdir $DIR/$tdir/migrate_dir
20363         mkdir $DIR/$tdir/other_dir
20364         touch $DIR/$tdir/migrate_dir/a
20365         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
20366         ls $DIR/$tdir/other_dir
20367
20368         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
20369                 error "migrate dir fails"
20370
20371         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
20372         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
20373
20374         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20375         [ $mdt_index == 0 ] || error "a is not on MDT0"
20376
20377         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
20378                 error "migrate dir fails"
20379
20380         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
20381         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
20382
20383         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20384         [ $mdt_index == 1 ] || error "a is not on MDT1"
20385
20386         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
20387         [ $mdt_index == 1 ] || error "b is not on MDT1"
20388
20389         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
20390         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
20391
20392         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
20393
20394         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20395 }
20396 run_test 230e "migrate mulitple local link files"
20397
20398 test_230f() {
20399         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20400         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20401         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20402                 skip "Need MDS version at least 2.11.52"
20403
20404         local a_fid
20405         local ln_fid
20406
20407         mkdir -p $DIR/$tdir
20408         mkdir $DIR/$tdir/migrate_dir
20409         $LFS mkdir -i1 $DIR/$tdir/other_dir
20410         touch $DIR/$tdir/migrate_dir/a
20411         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
20412         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
20413         ls $DIR/$tdir/other_dir
20414
20415         # a should be migrated to MDT1, since no other links on MDT0
20416         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
20417                 error "#1 migrate dir fails"
20418         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
20419         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
20420         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20421         [ $mdt_index == 1 ] || error "a is not on MDT1"
20422
20423         # a should stay on MDT1, because it is a mulitple link file
20424         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
20425                 error "#2 migrate dir fails"
20426         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20427         [ $mdt_index == 1 ] || error "a is not on MDT1"
20428
20429         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
20430                 error "#3 migrate dir fails"
20431
20432         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
20433         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
20434         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
20435
20436         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
20437         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
20438
20439         # a should be migrated to MDT0, since no other links on MDT1
20440         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
20441                 error "#4 migrate dir fails"
20442         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20443         [ $mdt_index == 0 ] || error "a is not on MDT0"
20444
20445         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20446 }
20447 run_test 230f "migrate mulitple remote link files"
20448
20449 test_230g() {
20450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20451         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20452         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20453                 skip "Need MDS version at least 2.11.52"
20454
20455         mkdir -p $DIR/$tdir/migrate_dir
20456
20457         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
20458                 error "migrating dir to non-exist MDT succeeds"
20459         true
20460 }
20461 run_test 230g "migrate dir to non-exist MDT"
20462
20463 test_230h() {
20464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20465         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20466         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20467                 skip "Need MDS version at least 2.11.52"
20468
20469         local mdt_index
20470
20471         mkdir -p $DIR/$tdir/migrate_dir
20472
20473         $LFS migrate -m1 $DIR &&
20474                 error "migrating mountpoint1 should fail"
20475
20476         $LFS migrate -m1 $DIR/$tdir/.. &&
20477                 error "migrating mountpoint2 should fail"
20478
20479         # same as mv
20480         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
20481                 error "migrating $tdir/migrate_dir/.. should fail"
20482
20483         true
20484 }
20485 run_test 230h "migrate .. and root"
20486
20487 test_230i() {
20488         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20489         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20490         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20491                 skip "Need MDS version at least 2.11.52"
20492
20493         mkdir -p $DIR/$tdir/migrate_dir
20494
20495         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
20496                 error "migration fails with a tailing slash"
20497
20498         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
20499                 error "migration fails with two tailing slashes"
20500 }
20501 run_test 230i "lfs migrate -m tolerates trailing slashes"
20502
20503 test_230j() {
20504         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20505         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
20506                 skip "Need MDS version at least 2.11.52"
20507
20508         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
20509         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
20510                 error "create $tfile failed"
20511         cat /etc/passwd > $DIR/$tdir/$tfile
20512
20513         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
20514
20515         cmp /etc/passwd $DIR/$tdir/$tfile ||
20516                 error "DoM file mismatch after migration"
20517 }
20518 run_test 230j "DoM file data not changed after dir migration"
20519
20520 test_230k() {
20521         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
20522         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20523                 skip "Need MDS version at least 2.11.56"
20524
20525         local total=20
20526         local files_on_starting_mdt=0
20527
20528         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
20529         $LFS getdirstripe $DIR/$tdir
20530         for i in $(seq $total); do
20531                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
20532                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
20533                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
20534         done
20535
20536         echo "$files_on_starting_mdt files on MDT0"
20537
20538         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
20539         $LFS getdirstripe $DIR/$tdir
20540
20541         files_on_starting_mdt=0
20542         for i in $(seq $total); do
20543                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
20544                         error "file $tfile.$i mismatch after migration"
20545                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
20546                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
20547         done
20548
20549         echo "$files_on_starting_mdt files on MDT1 after migration"
20550         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
20551
20552         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
20553         $LFS getdirstripe $DIR/$tdir
20554
20555         files_on_starting_mdt=0
20556         for i in $(seq $total); do
20557                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
20558                         error "file $tfile.$i mismatch after 2nd migration"
20559                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
20560                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
20561         done
20562
20563         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
20564         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
20565
20566         true
20567 }
20568 run_test 230k "file data not changed after dir migration"
20569
20570 test_230l() {
20571         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20572         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20573                 skip "Need MDS version at least 2.11.56"
20574
20575         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
20576         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
20577                 error "create files under remote dir failed $i"
20578         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
20579 }
20580 run_test 230l "readdir between MDTs won't crash"
20581
20582 test_230m() {
20583         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20584         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20585                 skip "Need MDS version at least 2.11.56"
20586
20587         local MDTIDX=1
20588         local mig_dir=$DIR/$tdir/migrate_dir
20589         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20590         local shortstr="b"
20591         local val
20592
20593         echo "Creating files and dirs with xattrs"
20594         test_mkdir $DIR/$tdir
20595         test_mkdir -i0 -c1 $mig_dir
20596         mkdir $mig_dir/dir
20597         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
20598                 error "cannot set xattr attr1 on dir"
20599         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
20600                 error "cannot set xattr attr2 on dir"
20601         touch $mig_dir/dir/f0
20602         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
20603                 error "cannot set xattr attr1 on file"
20604         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
20605                 error "cannot set xattr attr2 on file"
20606         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
20607         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
20608         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
20609         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
20610         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
20611         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
20612         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
20613         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
20614         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
20615
20616         echo "Migrating to MDT1"
20617         $LFS migrate -m $MDTIDX $mig_dir ||
20618                 error "fails on migrating dir to MDT1"
20619
20620         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
20621         echo "Checking xattrs"
20622         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
20623         [ "$val" = $longstr ] ||
20624                 error "expecting xattr1 $longstr on dir, found $val"
20625         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
20626         [ "$val" = $shortstr ] ||
20627                 error "expecting xattr2 $shortstr on dir, found $val"
20628         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
20629         [ "$val" = $longstr ] ||
20630                 error "expecting xattr1 $longstr on file, found $val"
20631         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
20632         [ "$val" = $shortstr ] ||
20633                 error "expecting xattr2 $shortstr on file, found $val"
20634 }
20635 run_test 230m "xattrs not changed after dir migration"
20636
20637 test_230n() {
20638         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20639         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
20640                 skip "Need MDS version at least 2.13.53"
20641
20642         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
20643         cat /etc/hosts > $DIR/$tdir/$tfile
20644         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
20645         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
20646
20647         cmp /etc/hosts $DIR/$tdir/$tfile ||
20648                 error "File data mismatch after migration"
20649 }
20650 run_test 230n "Dir migration with mirrored file"
20651
20652 test_230o() {
20653         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
20654         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
20655                 skip "Need MDS version at least 2.13.52"
20656
20657         local mdts=$(comma_list $(mdts_nodes))
20658         local timeout=100
20659         local restripe_status
20660         local delta
20661         local i
20662
20663         [[ $mds1_FSTYPE == zfs ]] && timeout=300
20664
20665         # in case "crush" hash type is not set
20666         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20667
20668         restripe_status=$(do_facet mds1 $LCTL get_param -n \
20669                            mdt.*MDT0000.enable_dir_restripe)
20670         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
20671         stack_trap "do_nodes $mdts $LCTL set_param \
20672                     mdt.*.enable_dir_restripe=$restripe_status"
20673
20674         mkdir $DIR/$tdir
20675         createmany -m $DIR/$tdir/f 100 ||
20676                 error "create files under remote dir failed $i"
20677         createmany -d $DIR/$tdir/d 100 ||
20678                 error "create dirs under remote dir failed $i"
20679
20680         for i in $(seq 2 $MDSCOUNT); do
20681                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
20682                 $LFS setdirstripe -c $i $DIR/$tdir ||
20683                         error "split -c $i $tdir failed"
20684                 wait_update $HOSTNAME \
20685                         "$LFS getdirstripe -H $DIR/$tdir" "crush" $timeout ||
20686                         error "dir split not finished"
20687                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
20688                         awk '/migrate/ {sum += $2} END { print sum }')
20689                 echo "$delta migrated when dir split $((i - 1)) to $i stripes"
20690                 # delta is around total_files/stripe_count
20691                 (( $delta < 200 / (i - 1) + 4 )) ||
20692                         error "$delta files migrated >= $((200 / (i - 1) + 4))"
20693         done
20694 }
20695 run_test 230o "dir split"
20696
20697 test_230p() {
20698         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
20699         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
20700                 skip "Need MDS version at least 2.13.52"
20701
20702         local mdts=$(comma_list $(mdts_nodes))
20703         local timeout=100
20704         local restripe_status
20705         local delta
20706         local c
20707
20708         [[ $mds1_FSTYPE == zfs ]] && timeout=300
20709
20710         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20711
20712         restripe_status=$(do_facet mds1 $LCTL get_param -n \
20713                            mdt.*MDT0000.enable_dir_restripe)
20714         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
20715         stack_trap "do_nodes $mdts $LCTL set_param \
20716                     mdt.*.enable_dir_restripe=$restripe_status"
20717
20718         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
20719         createmany -m $DIR/$tdir/f 100 ||
20720                 error "create files under remote dir failed"
20721         createmany -d $DIR/$tdir/d 100 ||
20722                 error "create dirs under remote dir failed"
20723
20724         for c in $(seq $((MDSCOUNT - 1)) -1 1); do
20725                 local mdt_hash="crush"
20726
20727                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
20728                 $LFS setdirstripe -c $c $DIR/$tdir ||
20729                         error "split -c $c $tdir failed"
20730                 if (( MDS1_VERSION >= $(version_code 2.14.51) )); then
20731                         mdt_hash="$mdt_hash,fixed"
20732                 elif [ $c -eq 1 ]; then
20733                         mdt_hash="none"
20734                 fi
20735                 wait_update $HOSTNAME \
20736                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash $timeout ||
20737                         error "dir merge not finished"
20738                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
20739                         awk '/migrate/ {sum += $2} END { print sum }')
20740                 echo "$delta migrated when dir merge $((c + 1)) to $c stripes"
20741                 # delta is around total_files/stripe_count
20742                 (( delta < 200 / c + 4 )) ||
20743                         error "$delta files migrated >= $((200 / c + 4))"
20744         done
20745 }
20746 run_test 230p "dir merge"
20747
20748 test_230q() {
20749         (( MDSCOUNT > 1)) || skip "needs >= 2 MDTs"
20750         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
20751                 skip "Need MDS version at least 2.13.52"
20752
20753         local mdts=$(comma_list $(mdts_nodes))
20754         local saved_threshold=$(do_facet mds1 \
20755                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
20756         local saved_delta=$(do_facet mds1 \
20757                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
20758         local threshold=100
20759         local delta=2
20760         local total=0
20761         local stripe_count=0
20762         local stripe_index
20763         local nr_files
20764         local create
20765
20766         # test with fewer files on ZFS
20767         [ "$mds1_FSTYPE" == "zfs" ] && threshold=40
20768
20769         stack_trap "do_nodes $mdts $LCTL set_param \
20770                     mdt.*.dir_split_count=$saved_threshold"
20771         stack_trap "do_nodes $mdts $LCTL set_param \
20772                     mdt.*.dir_split_delta=$saved_delta"
20773         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
20774         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
20775         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
20776         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
20777         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
20778         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20779
20780         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
20781         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20782
20783         create=$((threshold * 3 / 2))
20784         while [ $stripe_count -lt $MDSCOUNT ]; do
20785                 createmany -m $DIR/$tdir/f $total $create ||
20786                         error "create sub files failed"
20787                 stat $DIR/$tdir > /dev/null
20788                 total=$((total + create))
20789                 stripe_count=$((stripe_count + delta))
20790                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
20791
20792                 wait_update $HOSTNAME \
20793                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
20794                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
20795
20796                 wait_update $HOSTNAME \
20797                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
20798                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
20799
20800                 nr_files=$($LFS find -m 1 $DIR/$tdir | grep -c -w $stripe_index)
20801                 echo "$nr_files/$total files on MDT$stripe_index after split"
20802                 # allow 10% margin of imbalance with crush hash
20803                 (( $nr_files <= $total / $stripe_count + $create / 10)) ||
20804                         error "$nr_files files on MDT$stripe_index after split"
20805
20806                 nr_files=$($LFS find -type f $DIR/$tdir | wc -l)
20807                 [ $nr_files -eq $total ] ||
20808                         error "total sub files $nr_files != $total"
20809         done
20810
20811         (( MDS1_VERSION >= $(version_code 2.14.51) )) || return 0
20812
20813         echo "fixed layout directory won't auto split"
20814         $LFS migrate -m 0 $DIR/$tdir || error "migrate $tdir failed"
20815         wait_update $HOSTNAME "$LFS getdirstripe -H $DIR/$tdir" "crush,fixed" \
20816                 10 || error "stripe hash $($LFS getdirstripe -H $DIR/$tdir)"
20817         wait_update $HOSTNAME "$LFS getdirstripe -c $DIR/$tdir" 1 10 ||
20818                 error "stripe count $($LFS getdirstripe -c $DIR/$tdir)"
20819 }
20820 run_test 230q "dir auto split"
20821
20822 test_230r() {
20823         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
20824         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20825         [[ $MDS1_VERSION -ge $(version_code 2.13.54) ]] ||
20826                 skip "Need MDS version at least 2.13.54"
20827
20828         # maximum amount of local locks:
20829         # parent striped dir - 2 locks
20830         # new stripe in parent to migrate to - 1 lock
20831         # source and target - 2 locks
20832         # Total 5 locks for regular file
20833         mkdir -p $DIR/$tdir
20834         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
20835         touch $DIR/$tdir/dir1/eee
20836
20837         # create 4 hardlink for 4 more locks
20838         # Total: 9 locks > RS_MAX_LOCKS (8)
20839         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
20840         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
20841         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
20842         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
20843         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
20844         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
20845         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
20846         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
20847
20848         cancel_lru_locks mdc
20849
20850         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
20851                 error "migrate dir fails"
20852
20853         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20854 }
20855 run_test 230r "migrate with too many local locks"
20856
20857 test_230s() {
20858         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
20859                 skip "Need MDS version at least 2.14.52"
20860
20861         local mdts=$(comma_list $(mdts_nodes))
20862         local restripe_status=$(do_facet mds1 $LCTL get_param -n \
20863                                 mdt.*MDT0000.enable_dir_restripe)
20864
20865         stack_trap "do_nodes $mdts $LCTL set_param \
20866                     mdt.*.enable_dir_restripe=$restripe_status"
20867
20868         local st
20869         for st in 0 1; do
20870                 do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=$st"
20871                 test_mkdir $DIR/$tdir
20872                 $LFS mkdir $DIR/$tdir |& grep "File exists" ||
20873                         error "$LFS mkdir should return EEXIST if target exists"
20874                 rmdir $DIR/$tdir
20875         done
20876 }
20877 run_test 230s "lfs mkdir should return -EEXIST if target exists"
20878
20879 test_230t()
20880 {
20881         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20882         [[ $MDS1_VERSION -ge $(version_code 2.14.50) ]] ||
20883                 skip "Need MDS version at least 2.14.50"
20884
20885         test_mkdir $DIR/$tdir || error "mkdir $tdir failed"
20886         test_mkdir $DIR/$tdir/subdir || error "mkdir subdir failed"
20887         $LFS project -p 1 -s $DIR/$tdir ||
20888                 error "set $tdir project id failed"
20889         $LFS project -p 2 -s $DIR/$tdir/subdir ||
20890                 error "set subdir project id failed"
20891         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir || error "migrate failed"
20892 }
20893 run_test 230t "migrate directory with project ID set"
20894
20895 test_230u()
20896 {
20897         (( MDSCOUNT > 3 )) || skip_env "needs >= 4 MDTs"
20898         (( MDS1_VERSION >= $(version_code 2.14.53) )) ||
20899                 skip "Need MDS version at least 2.14.53"
20900
20901         local count
20902
20903         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
20904         mkdir $DIR/$tdir/sub{0..99} || error "mkdir sub failed"
20905         $LFS migrate -m -1 $DIR/$tdir/sub{0..99} || error "migrate sub failed"
20906         for i in $(seq 0 $((MDSCOUNT - 1))); do
20907                 count=$($LFS getstripe -m $DIR/$tdir/sub* | grep -c ^$i)
20908                 echo "$count dirs migrated to MDT$i"
20909         done
20910         count=$($LFS getstripe -m $DIR/$tdir/sub* | sort -u | wc -l)
20911         (( count >= MDSCOUNT - 1 )) || error "dirs migrated to $count MDTs"
20912 }
20913 run_test 230u "migrate directory by QOS"
20914
20915 test_230v()
20916 {
20917         (( MDSCOUNT > 3 )) || skip_env "needs >= 4 MDTs"
20918         (( MDS1_VERSION >= $(version_code 2.14.53) )) ||
20919                 skip "Need MDS version at least 2.14.53"
20920
20921         local count
20922
20923         mkdir $DIR/$tdir || error "mkdir $tdir failed"
20924         mkdir $DIR/$tdir/sub{0..99} || error "mkdir sub failed"
20925         $LFS migrate -m 0,2,1 $DIR/$tdir || error "migrate $tdir failed"
20926         for i in $(seq 0 $((MDSCOUNT - 1))); do
20927                 count=$($LFS getstripe -m $DIR/$tdir/sub* | grep -c ^$i)
20928                 echo "$count subdirs migrated to MDT$i"
20929                 (( i == 3 )) && (( count > 0 )) &&
20930                         error "subdir shouldn't be migrated to MDT3"
20931         done
20932         count=$($LFS getstripe -m $DIR/$tdir/sub* | sort -u | wc -l)
20933         (( count == 3 )) || error "dirs migrated to $count MDTs"
20934 }
20935 run_test 230v "subdir migrated to the MDT where its parent is located"
20936
20937 test_230w() {
20938         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
20939         (( MDS1_VERSION >= $(version_code 2.15.0) )) ||
20940                 skip "Need MDS version at least 2.15.0"
20941
20942         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
20943         createmany -o $DIR/$tdir/f 10 || error "create files failed"
20944         createmany -d $DIR/$tdir/d 10 || error "create dirs failed"
20945
20946         $LFS migrate -m 1 -c $MDSCOUNT -d $DIR/$tdir ||
20947                 error "migrate failed"
20948
20949         (( $($LFS getdirstripe -c $DIR/$tdir) == MDSCOUNT )) ||
20950                 error "$tdir stripe count mismatch"
20951
20952         for i in $(seq 0 9); do
20953                 (( $($LFS getdirstripe -c $DIR/$tdir/d$i) == 0 )) ||
20954                         error "d$i is striped"
20955         done
20956 }
20957 run_test 230w "non-recursive mode dir migration"
20958
20959 test_231a()
20960 {
20961         # For simplicity this test assumes that max_pages_per_rpc
20962         # is the same across all OSCs
20963         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
20964         local bulk_size=$((max_pages * PAGE_SIZE))
20965         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
20966                                        head -n 1)
20967
20968         mkdir -p $DIR/$tdir
20969         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
20970                 error "failed to set stripe with -S ${brw_size}M option"
20971
20972         # clear the OSC stats
20973         $LCTL set_param osc.*.stats=0 &>/dev/null
20974         stop_writeback
20975
20976         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
20977         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
20978                 oflag=direct &>/dev/null || error "dd failed"
20979
20980         sync; sleep 1; sync # just to be safe
20981         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
20982         if [ x$nrpcs != "x1" ]; then
20983                 $LCTL get_param osc.*.stats
20984                 error "found $nrpcs ost_write RPCs, not 1 as expected"
20985         fi
20986
20987         start_writeback
20988         # Drop the OSC cache, otherwise we will read from it
20989         cancel_lru_locks osc
20990
20991         # clear the OSC stats
20992         $LCTL set_param osc.*.stats=0 &>/dev/null
20993
20994         # Client reads $bulk_size.
20995         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
20996                 iflag=direct &>/dev/null || error "dd failed"
20997
20998         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
20999         if [ x$nrpcs != "x1" ]; then
21000                 $LCTL get_param osc.*.stats
21001                 error "found $nrpcs ost_read RPCs, not 1 as expected"
21002         fi
21003 }
21004 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
21005
21006 test_231b() {
21007         mkdir -p $DIR/$tdir
21008         local i
21009         for i in {0..1023}; do
21010                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
21011                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
21012                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
21013         done
21014         sync
21015 }
21016 run_test 231b "must not assert on fully utilized OST request buffer"
21017
21018 test_232a() {
21019         mkdir -p $DIR/$tdir
21020         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
21021
21022         #define OBD_FAIL_LDLM_OST_LVB            0x31c
21023         do_facet ost1 $LCTL set_param fail_loc=0x31c
21024
21025         # ignore dd failure
21026         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
21027
21028         do_facet ost1 $LCTL set_param fail_loc=0
21029         umount_client $MOUNT || error "umount failed"
21030         mount_client $MOUNT || error "mount failed"
21031         stop ost1 || error "cannot stop ost1"
21032         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21033 }
21034 run_test 232a "failed lock should not block umount"
21035
21036 test_232b() {
21037         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
21038                 skip "Need MDS version at least 2.10.58"
21039
21040         mkdir -p $DIR/$tdir
21041         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
21042         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
21043         sync
21044         cancel_lru_locks osc
21045
21046         #define OBD_FAIL_LDLM_OST_LVB            0x31c
21047         do_facet ost1 $LCTL set_param fail_loc=0x31c
21048
21049         # ignore failure
21050         $LFS data_version $DIR/$tdir/$tfile || true
21051
21052         do_facet ost1 $LCTL set_param fail_loc=0
21053         umount_client $MOUNT || error "umount failed"
21054         mount_client $MOUNT || error "mount failed"
21055         stop ost1 || error "cannot stop ost1"
21056         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21057 }
21058 run_test 232b "failed data version lock should not block umount"
21059
21060 test_233a() {
21061         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
21062                 skip "Need MDS version at least 2.3.64"
21063         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
21064
21065         local fid=$($LFS path2fid $MOUNT)
21066
21067         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21068                 error "cannot access $MOUNT using its FID '$fid'"
21069 }
21070 run_test 233a "checking that OBF of the FS root succeeds"
21071
21072 test_233b() {
21073         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
21074                 skip "Need MDS version at least 2.5.90"
21075         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
21076
21077         local fid=$($LFS path2fid $MOUNT/.lustre)
21078
21079         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21080                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
21081
21082         fid=$($LFS path2fid $MOUNT/.lustre/fid)
21083         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21084                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
21085 }
21086 run_test 233b "checking that OBF of the FS .lustre succeeds"
21087
21088 test_234() {
21089         local p="$TMP/sanityN-$TESTNAME.parameters"
21090         save_lustre_params client "llite.*.xattr_cache" > $p
21091         lctl set_param llite.*.xattr_cache 1 ||
21092                 skip_env "xattr cache is not supported"
21093
21094         mkdir -p $DIR/$tdir || error "mkdir failed"
21095         touch $DIR/$tdir/$tfile || error "touch failed"
21096         # OBD_FAIL_LLITE_XATTR_ENOMEM
21097         $LCTL set_param fail_loc=0x1405
21098         getfattr -n user.attr $DIR/$tdir/$tfile &&
21099                 error "getfattr should have failed with ENOMEM"
21100         $LCTL set_param fail_loc=0x0
21101         rm -rf $DIR/$tdir
21102
21103         restore_lustre_params < $p
21104         rm -f $p
21105 }
21106 run_test 234 "xattr cache should not crash on ENOMEM"
21107
21108 test_235() {
21109         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
21110                 skip "Need MDS version at least 2.4.52"
21111
21112         flock_deadlock $DIR/$tfile
21113         local RC=$?
21114         case $RC in
21115                 0)
21116                 ;;
21117                 124) error "process hangs on a deadlock"
21118                 ;;
21119                 *) error "error executing flock_deadlock $DIR/$tfile"
21120                 ;;
21121         esac
21122 }
21123 run_test 235 "LU-1715: flock deadlock detection does not work properly"
21124
21125 #LU-2935
21126 test_236() {
21127         check_swap_layouts_support
21128
21129         local ref1=/etc/passwd
21130         local ref2=/etc/group
21131         local file1=$DIR/$tdir/f1
21132         local file2=$DIR/$tdir/f2
21133
21134         test_mkdir -c1 $DIR/$tdir
21135         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
21136         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
21137         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
21138         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
21139         local fd=$(free_fd)
21140         local cmd="exec $fd<>$file2"
21141         eval $cmd
21142         rm $file2
21143         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
21144                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
21145         cmd="exec $fd>&-"
21146         eval $cmd
21147         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
21148
21149         #cleanup
21150         rm -rf $DIR/$tdir
21151 }
21152 run_test 236 "Layout swap on open unlinked file"
21153
21154 # LU-4659 linkea consistency
21155 test_238() {
21156         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
21157                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
21158                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
21159                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
21160
21161         touch $DIR/$tfile
21162         ln $DIR/$tfile $DIR/$tfile.lnk
21163         touch $DIR/$tfile.new
21164         mv $DIR/$tfile.new $DIR/$tfile
21165         local fid1=$($LFS path2fid $DIR/$tfile)
21166         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
21167         local path1=$($LFS fid2path $FSNAME "$fid1")
21168         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
21169         local path2=$($LFS fid2path $FSNAME "$fid2")
21170         [ $tfile.lnk == $path2 ] ||
21171                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
21172         rm -f $DIR/$tfile*
21173 }
21174 run_test 238 "Verify linkea consistency"
21175
21176 test_239A() { # was test_239
21177         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
21178                 skip "Need MDS version at least 2.5.60"
21179
21180         local list=$(comma_list $(mdts_nodes))
21181
21182         mkdir -p $DIR/$tdir
21183         createmany -o $DIR/$tdir/f- 5000
21184         unlinkmany $DIR/$tdir/f- 5000
21185         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
21186                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
21187         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
21188                         osp.*MDT*.sync_in_flight" | calc_sum)
21189         [ "$changes" -eq 0 ] || error "$changes not synced"
21190 }
21191 run_test 239A "osp_sync test"
21192
21193 test_239a() { #LU-5297
21194         remote_mds_nodsh && skip "remote MDS with nodsh"
21195
21196         touch $DIR/$tfile
21197         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
21198         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
21199         chgrp $RUNAS_GID $DIR/$tfile
21200         wait_delete_completed
21201 }
21202 run_test 239a "process invalid osp sync record correctly"
21203
21204 test_239b() { #LU-5297
21205         remote_mds_nodsh && skip "remote MDS with nodsh"
21206
21207         touch $DIR/$tfile1
21208         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
21209         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
21210         chgrp $RUNAS_GID $DIR/$tfile1
21211         wait_delete_completed
21212         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21213         touch $DIR/$tfile2
21214         chgrp $RUNAS_GID $DIR/$tfile2
21215         wait_delete_completed
21216 }
21217 run_test 239b "process osp sync record with ENOMEM error correctly"
21218
21219 test_240() {
21220         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21221         remote_mds_nodsh && skip "remote MDS with nodsh"
21222
21223         mkdir -p $DIR/$tdir
21224
21225         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
21226                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
21227         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
21228                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
21229
21230         umount_client $MOUNT || error "umount failed"
21231         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
21232         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
21233         mount_client $MOUNT || error "failed to mount client"
21234
21235         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
21236         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
21237 }
21238 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
21239
21240 test_241_bio() {
21241         local count=$1
21242         local bsize=$2
21243
21244         for LOOP in $(seq $count); do
21245                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
21246                 cancel_lru_locks $OSC || true
21247         done
21248 }
21249
21250 test_241_dio() {
21251         local count=$1
21252         local bsize=$2
21253
21254         for LOOP in $(seq $1); do
21255                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
21256                         2>/dev/null
21257         done
21258 }
21259
21260 test_241a() { # was test_241
21261         local bsize=$PAGE_SIZE
21262
21263         (( bsize < 40960 )) && bsize=40960
21264         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
21265         ls -la $DIR/$tfile
21266         cancel_lru_locks $OSC
21267         test_241_bio 1000 $bsize &
21268         PID=$!
21269         test_241_dio 1000 $bsize
21270         wait $PID
21271 }
21272 run_test 241a "bio vs dio"
21273
21274 test_241b() {
21275         local bsize=$PAGE_SIZE
21276
21277         (( bsize < 40960 )) && bsize=40960
21278         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
21279         ls -la $DIR/$tfile
21280         test_241_dio 1000 $bsize &
21281         PID=$!
21282         test_241_dio 1000 $bsize
21283         wait $PID
21284 }
21285 run_test 241b "dio vs dio"
21286
21287 test_242() {
21288         remote_mds_nodsh && skip "remote MDS with nodsh"
21289
21290         mkdir_on_mdt0 $DIR/$tdir
21291         touch $DIR/$tdir/$tfile
21292
21293         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
21294         do_facet mds1 lctl set_param fail_loc=0x105
21295         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
21296
21297         do_facet mds1 lctl set_param fail_loc=0
21298         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
21299 }
21300 run_test 242 "mdt_readpage failure should not cause directory unreadable"
21301
21302 test_243()
21303 {
21304         test_mkdir $DIR/$tdir
21305         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
21306 }
21307 run_test 243 "various group lock tests"
21308
21309 test_244a()
21310 {
21311         test_mkdir $DIR/$tdir
21312         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
21313         sendfile_grouplock $DIR/$tdir/$tfile || \
21314                 error "sendfile+grouplock failed"
21315         rm -rf $DIR/$tdir
21316 }
21317 run_test 244a "sendfile with group lock tests"
21318
21319 test_244b()
21320 {
21321         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
21322
21323         local threads=50
21324         local size=$((1024*1024))
21325
21326         test_mkdir $DIR/$tdir
21327         for i in $(seq 1 $threads); do
21328                 local file=$DIR/$tdir/file_$((i / 10))
21329                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
21330                 local pids[$i]=$!
21331         done
21332         for i in $(seq 1 $threads); do
21333                 wait ${pids[$i]}
21334         done
21335 }
21336 run_test 244b "multi-threaded write with group lock"
21337
21338 test_245a() {
21339         local flagname="multi_mod_rpcs"
21340         local connect_data_name="max_mod_rpcs"
21341         local out
21342
21343         # check if multiple modify RPCs flag is set
21344         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
21345                 grep "connect_flags:")
21346         echo "$out"
21347
21348         echo "$out" | grep -qw $flagname
21349         if [ $? -ne 0 ]; then
21350                 echo "connect flag $flagname is not set"
21351                 return
21352         fi
21353
21354         # check if multiple modify RPCs data is set
21355         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
21356         echo "$out"
21357
21358         echo "$out" | grep -qw $connect_data_name ||
21359                 error "import should have connect data $connect_data_name"
21360 }
21361 run_test 245a "check mdc connection flag/data: multiple modify RPCs"
21362
21363 test_245b() {
21364         local flagname="multi_mod_rpcs"
21365         local connect_data_name="max_mod_rpcs"
21366         local out
21367
21368         remote_mds_nodsh && skip "remote MDS with nodsh"
21369         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
21370
21371         # check if multiple modify RPCs flag is set
21372         out=$(do_facet mds1 \
21373               $LCTL get_param osp.$FSNAME-MDT0001-osp-MDT0000.import |
21374               grep "connect_flags:")
21375         echo "$out"
21376
21377         [[ "$out" =~ $flagname ]] || skip "connect flag $flagname is not set"
21378
21379         # check if multiple modify RPCs data is set
21380         out=$(do_facet mds1 \
21381               $LCTL get_param osp.$FSNAME-MDT0001-osp-MDT0000.import)
21382
21383         [[ "$out" =~ $connect_data_name ]] ||
21384                 {
21385                         echo "$out"
21386                         error "missing connect data $connect_data_name"
21387                 }
21388 }
21389 run_test 245b "check osp connection flag/data: multiple modify RPCs"
21390
21391 cleanup_247() {
21392         local submount=$1
21393
21394         trap 0
21395         umount_client $submount
21396         rmdir $submount
21397 }
21398
21399 test_247a() {
21400         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
21401                 grep -q subtree ||
21402                 skip_env "Fileset feature is not supported"
21403
21404         local submount=${MOUNT}_$tdir
21405
21406         mkdir $MOUNT/$tdir
21407         mkdir -p $submount || error "mkdir $submount failed"
21408         FILESET="$FILESET/$tdir" mount_client $submount ||
21409                 error "mount $submount failed"
21410         trap "cleanup_247 $submount" EXIT
21411         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
21412         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
21413                 error "read $MOUNT/$tdir/$tfile failed"
21414         cleanup_247 $submount
21415 }
21416 run_test 247a "mount subdir as fileset"
21417
21418 test_247b() {
21419         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
21420                 skip_env "Fileset feature is not supported"
21421
21422         local submount=${MOUNT}_$tdir
21423
21424         rm -rf $MOUNT/$tdir
21425         mkdir -p $submount || error "mkdir $submount failed"
21426         SKIP_FILESET=1
21427         FILESET="$FILESET/$tdir" mount_client $submount &&
21428                 error "mount $submount should fail"
21429         rmdir $submount
21430 }
21431 run_test 247b "mount subdir that dose not exist"
21432
21433 test_247c() {
21434         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
21435                 skip_env "Fileset feature is not supported"
21436
21437         local submount=${MOUNT}_$tdir
21438
21439         mkdir -p $MOUNT/$tdir/dir1
21440         mkdir -p $submount || error "mkdir $submount failed"
21441         trap "cleanup_247 $submount" EXIT
21442         FILESET="$FILESET/$tdir" mount_client $submount ||
21443                 error "mount $submount failed"
21444         local fid=$($LFS path2fid $MOUNT/)
21445         $LFS fid2path $submount $fid && error "fid2path should fail"
21446         cleanup_247 $submount
21447 }
21448 run_test 247c "running fid2path outside subdirectory root"
21449
21450 test_247d() {
21451         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
21452                 skip "Fileset feature is not supported"
21453
21454         local submount=${MOUNT}_$tdir
21455
21456         mkdir -p $MOUNT/$tdir/dir1
21457         mkdir -p $submount || error "mkdir $submount failed"
21458         FILESET="$FILESET/$tdir" mount_client $submount ||
21459                 error "mount $submount failed"
21460         trap "cleanup_247 $submount" EXIT
21461
21462         local td=$submount/dir1
21463         local fid=$($LFS path2fid $td)
21464         [ -z "$fid" ] && error "path2fid unable to get $td FID"
21465
21466         # check that we get the same pathname back
21467         local rootpath
21468         local found
21469         for rootpath in "$submount" "$submount///" "$submount/dir1"; do
21470                 echo "$rootpath $fid"
21471                 found=$($LFS fid2path $rootpath "$fid")
21472                 [ -n "$found" ] || error "fid2path should succeed"
21473                 [ "$found" == "$td" ] || error "fid2path $found != $td"
21474         done
21475         # check wrong root path format
21476         rootpath=$submount"_wrong"
21477         found=$($LFS fid2path $rootpath "$fid")
21478         [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)"
21479
21480         cleanup_247 $submount
21481 }
21482 run_test 247d "running fid2path inside subdirectory root"
21483
21484 # LU-8037
21485 test_247e() {
21486         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
21487                 grep -q subtree ||
21488                 skip "Fileset feature is not supported"
21489
21490         local submount=${MOUNT}_$tdir
21491
21492         mkdir $MOUNT/$tdir
21493         mkdir -p $submount || error "mkdir $submount failed"
21494         FILESET="$FILESET/.." mount_client $submount &&
21495                 error "mount $submount should fail"
21496         rmdir $submount
21497 }
21498 run_test 247e "mount .. as fileset"
21499
21500 test_247f() {
21501         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21502         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
21503                 skip "Need at least version 2.13.52"
21504         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
21505                 skip "Need at least version 2.14.50"
21506         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
21507                 grep -q subtree ||
21508                 skip "Fileset feature is not supported"
21509
21510         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
21511         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
21512                 error "mkdir remote failed"
21513         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote/subdir ||
21514                 error "mkdir remote/subdir failed"
21515         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
21516                 error "mkdir striped failed"
21517         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
21518
21519         local submount=${MOUNT}_$tdir
21520
21521         mkdir -p $submount || error "mkdir $submount failed"
21522         stack_trap "rmdir $submount"
21523
21524         local dir
21525         local stat
21526         local fileset=$FILESET
21527         local mdts=$(comma_list $(mdts_nodes))
21528
21529         stat=$(do_facet mds1 $LCTL get_param -n \
21530                 mdt.*MDT0000.enable_remote_subdir_mount)
21531         stack_trap "do_nodes $mdts $LCTL set_param \
21532                 mdt.*.enable_remote_subdir_mount=$stat"
21533
21534         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
21535         stack_trap "umount_client $submount"
21536         FILESET="$fileset/$tdir/remote" mount_client $submount &&
21537                 error "mount remote dir $dir should fail"
21538
21539         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
21540                 $tdir/striped/. ; do
21541                 FILESET="$fileset/$dir" mount_client $submount ||
21542                         error "mount $dir failed"
21543                 umount_client $submount
21544         done
21545
21546         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
21547         FILESET="$fileset/$tdir/remote" mount_client $submount ||
21548                 error "mount $tdir/remote failed"
21549 }
21550 run_test 247f "mount striped or remote directory as fileset"
21551
21552 test_247g() {
21553         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
21554         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
21555                 skip "Need at least version 2.14.50"
21556
21557         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
21558                 error "mkdir $tdir failed"
21559         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
21560
21561         local submount=${MOUNT}_$tdir
21562
21563         mkdir -p $submount || error "mkdir $submount failed"
21564         stack_trap "rmdir $submount"
21565
21566         FILESET="$fileset/$tdir" mount_client $submount ||
21567                 error "mount $dir failed"
21568         stack_trap "umount $submount"
21569
21570         local mdts=$(comma_list $(mdts_nodes))
21571
21572         local nrpcs
21573
21574         stat $submount > /dev/null
21575         cancel_lru_locks $MDC
21576         stat $submount > /dev/null
21577         stat $submount/$tfile > /dev/null
21578         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
21579         stat $submount/$tfile > /dev/null
21580         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
21581                 awk '/getattr/ {sum += $2} END {print sum}')
21582
21583         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
21584 }
21585 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
21586
21587 test_248a() {
21588         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
21589         [ -z "$fast_read_sav" ] && skip "no fast read support"
21590
21591         # create a large file for fast read verification
21592         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
21593
21594         # make sure the file is created correctly
21595         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
21596                 { rm -f $DIR/$tfile; skip "file creation error"; }
21597
21598         echo "Test 1: verify that fast read is 4 times faster on cache read"
21599
21600         # small read with fast read enabled
21601         $LCTL set_param -n llite.*.fast_read=1
21602         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
21603                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21604                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21605         # small read with fast read disabled
21606         $LCTL set_param -n llite.*.fast_read=0
21607         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
21608                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21609                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21610
21611         # verify that fast read is 4 times faster for cache read
21612         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
21613                 error_not_in_vm "fast read was not 4 times faster: " \
21614                            "$t_fast vs $t_slow"
21615
21616         echo "Test 2: verify the performance between big and small read"
21617         $LCTL set_param -n llite.*.fast_read=1
21618
21619         # 1k non-cache read
21620         cancel_lru_locks osc
21621         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
21622                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21623                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21624
21625         # 1M non-cache read
21626         cancel_lru_locks osc
21627         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
21628                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21629                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21630
21631         # verify that big IO is not 4 times faster than small IO
21632         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
21633                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
21634
21635         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
21636         rm -f $DIR/$tfile
21637 }
21638 run_test 248a "fast read verification"
21639
21640 test_248b() {
21641         # Default short_io_bytes=16384, try both smaller and larger sizes.
21642         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
21643         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
21644         echo "bs=53248 count=113 normal buffered write"
21645         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
21646                 error "dd of initial data file failed"
21647         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
21648
21649         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
21650         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
21651                 error "dd with sync normal writes failed"
21652         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
21653
21654         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
21655         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
21656                 error "dd with sync small writes failed"
21657         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
21658
21659         cancel_lru_locks osc
21660
21661         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
21662         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
21663         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
21664         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
21665                 iflag=direct || error "dd with O_DIRECT small read failed"
21666         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
21667         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
21668                 error "compare $TMP/$tfile.1 failed"
21669
21670         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
21671         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
21672
21673         # just to see what the maximum tunable value is, and test parsing
21674         echo "test invalid parameter 2MB"
21675         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
21676                 error "too-large short_io_bytes allowed"
21677         echo "test maximum parameter 512KB"
21678         # if we can set a larger short_io_bytes, run test regardless of version
21679         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
21680                 # older clients may not allow setting it this large, that's OK
21681                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
21682                         skip "Need at least client version 2.13.50"
21683                 error "medium short_io_bytes failed"
21684         fi
21685         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
21686         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
21687
21688         echo "test large parameter 64KB"
21689         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
21690         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
21691
21692         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
21693         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
21694                 error "dd with sync large writes failed"
21695         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
21696
21697         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
21698         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
21699         num=$((113 * 4096 / PAGE_SIZE))
21700         echo "bs=$size count=$num oflag=direct large write $tfile.3"
21701         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
21702                 error "dd with O_DIRECT large writes failed"
21703         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
21704                 error "compare $DIR/$tfile.3 failed"
21705
21706         cancel_lru_locks osc
21707
21708         echo "bs=$size count=$num iflag=direct large read $tfile.2"
21709         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
21710                 error "dd with O_DIRECT large read failed"
21711         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
21712                 error "compare $TMP/$tfile.2 failed"
21713
21714         echo "bs=$size count=$num iflag=direct large read $tfile.3"
21715         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
21716                 error "dd with O_DIRECT large read failed"
21717         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
21718                 error "compare $TMP/$tfile.3 failed"
21719 }
21720 run_test 248b "test short_io read and write for both small and large sizes"
21721
21722 test_249() { # LU-7890
21723         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
21724                 skip "Need at least version 2.8.54"
21725
21726         rm -f $DIR/$tfile
21727         $LFS setstripe -c 1 $DIR/$tfile
21728         # Offset 2T == 4k * 512M
21729         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
21730                 error "dd to 2T offset failed"
21731 }
21732 run_test 249 "Write above 2T file size"
21733
21734 test_250() {
21735         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
21736          && skip "no 16TB file size limit on ZFS"
21737
21738         $LFS setstripe -c 1 $DIR/$tfile
21739         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
21740         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
21741         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
21742         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
21743                 conv=notrunc,fsync && error "append succeeded"
21744         return 0
21745 }
21746 run_test 250 "Write above 16T limit"
21747
21748 test_251() {
21749         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
21750
21751         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
21752         #Skip once - writing the first stripe will succeed
21753         $LCTL set_param fail_loc=0xa0001407 fail_val=1
21754         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
21755                 error "short write happened"
21756
21757         $LCTL set_param fail_loc=0xa0001407 fail_val=1
21758         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
21759                 error "short read happened"
21760
21761         rm -f $DIR/$tfile
21762 }
21763 run_test 251 "Handling short read and write correctly"
21764
21765 test_252() {
21766         remote_mds_nodsh && skip "remote MDS with nodsh"
21767         remote_ost_nodsh && skip "remote OST with nodsh"
21768         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
21769                 skip_env "ldiskfs only test"
21770         fi
21771
21772         local tgt
21773         local dev
21774         local out
21775         local uuid
21776         local num
21777         local gen
21778
21779         # check lr_reader on OST0000
21780         tgt=ost1
21781         dev=$(facet_device $tgt)
21782         out=$(do_facet $tgt $LR_READER $dev)
21783         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21784         echo "$out"
21785         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
21786         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
21787                 error "Invalid uuid returned by $LR_READER on target $tgt"
21788         echo -e "uuid returned by $LR_READER is '$uuid'\n"
21789
21790         # check lr_reader -c on MDT0000
21791         tgt=mds1
21792         dev=$(facet_device $tgt)
21793         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
21794                 skip "$LR_READER does not support additional options"
21795         fi
21796         out=$(do_facet $tgt $LR_READER -c $dev)
21797         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21798         echo "$out"
21799         num=$(echo "$out" | grep -c "mdtlov")
21800         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
21801                 error "Invalid number of mdtlov clients returned by $LR_READER"
21802         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
21803
21804         # check lr_reader -cr on MDT0000
21805         out=$(do_facet $tgt $LR_READER -cr $dev)
21806         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21807         echo "$out"
21808         echo "$out" | grep -q "^reply_data:$" ||
21809                 error "$LR_READER should have returned 'reply_data' section"
21810         num=$(echo "$out" | grep -c "client_generation")
21811         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
21812 }
21813 run_test 252 "check lr_reader tool"
21814
21815 test_253() {
21816         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21817         remote_mds_nodsh && skip "remote MDS with nodsh"
21818         remote_mgs_nodsh && skip "remote MGS with nodsh"
21819
21820         local ostidx=0
21821         local rc=0
21822         local ost_name=$(ostname_from_index $ostidx)
21823
21824         # on the mdt's osc
21825         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
21826         do_facet $SINGLEMDS $LCTL get_param -n \
21827                 osp.$mdtosc_proc1.reserved_mb_high ||
21828                 skip  "remote MDS does not support reserved_mb_high"
21829
21830         rm -rf $DIR/$tdir
21831         wait_mds_ost_sync
21832         wait_delete_completed
21833         mkdir $DIR/$tdir
21834
21835         pool_add $TESTNAME || error "Pool creation failed"
21836         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
21837
21838         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
21839                 error "Setstripe failed"
21840
21841         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
21842
21843         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
21844                     grep "watermarks")
21845         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
21846
21847         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
21848                         osp.$mdtosc_proc1.prealloc_status)
21849         echo "prealloc_status $oa_status"
21850
21851         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
21852                 error "File creation should fail"
21853
21854         #object allocation was stopped, but we still able to append files
21855         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
21856                 oflag=append || error "Append failed"
21857
21858         rm -f $DIR/$tdir/$tfile.0
21859
21860         # For this test, we want to delete the files we created to go out of
21861         # space but leave the watermark, so we remain nearly out of space
21862         ost_watermarks_enospc_delete_files $tfile $ostidx
21863
21864         wait_delete_completed
21865
21866         sleep_maxage
21867
21868         for i in $(seq 10 12); do
21869                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
21870                         2>/dev/null || error "File creation failed after rm"
21871         done
21872
21873         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
21874                         osp.$mdtosc_proc1.prealloc_status)
21875         echo "prealloc_status $oa_status"
21876
21877         if (( oa_status != 0 )); then
21878                 error "Object allocation still disable after rm"
21879         fi
21880 }
21881 run_test 253 "Check object allocation limit"
21882
21883 test_254() {
21884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21885         remote_mds_nodsh && skip "remote MDS with nodsh"
21886
21887         local mdt=$(facet_svc $SINGLEMDS)
21888
21889         do_facet $SINGLEMDS $LCTL get_param -n mdd.$mdt.changelog_size ||
21890                 skip "MDS does not support changelog_size"
21891
21892         local cl_user
21893
21894         changelog_register || error "changelog_register failed"
21895
21896         changelog_clear 0 || error "changelog_clear failed"
21897
21898         local size1=$(do_facet $SINGLEMDS \
21899                       $LCTL get_param -n mdd.$mdt.changelog_size)
21900         echo "Changelog size $size1"
21901
21902         rm -rf $DIR/$tdir
21903         $LFS mkdir -i 0 $DIR/$tdir
21904         # change something
21905         mkdir -p $DIR/$tdir/pics/2008/zachy
21906         touch $DIR/$tdir/pics/2008/zachy/timestamp
21907         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
21908         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
21909         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
21910         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
21911         rm $DIR/$tdir/pics/desktop.jpg
21912
21913         local size2=$(do_facet $SINGLEMDS \
21914                       $LCTL get_param -n mdd.$mdt.changelog_size)
21915         echo "Changelog size after work $size2"
21916
21917         (( $size2 > $size1 )) ||
21918                 error "new Changelog size=$size2 less than old size=$size1"
21919 }
21920 run_test 254 "Check changelog size"
21921
21922 ladvise_no_type()
21923 {
21924         local type=$1
21925         local file=$2
21926
21927         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
21928                 awk -F: '{print $2}' | grep $type > /dev/null
21929         if [ $? -ne 0 ]; then
21930                 return 0
21931         fi
21932         return 1
21933 }
21934
21935 ladvise_no_ioctl()
21936 {
21937         local file=$1
21938
21939         lfs ladvise -a willread $file > /dev/null 2>&1
21940         if [ $? -eq 0 ]; then
21941                 return 1
21942         fi
21943
21944         lfs ladvise -a willread $file 2>&1 |
21945                 grep "Inappropriate ioctl for device" > /dev/null
21946         if [ $? -eq 0 ]; then
21947                 return 0
21948         fi
21949         return 1
21950 }
21951
21952 percent() {
21953         bc <<<"scale=2; ($1 - $2) * 100 / $2"
21954 }
21955
21956 # run a random read IO workload
21957 # usage: random_read_iops <filename> <filesize> <iosize>
21958 random_read_iops() {
21959         local file=$1
21960         local fsize=$2
21961         local iosize=${3:-4096}
21962
21963         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
21964                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
21965 }
21966
21967 drop_file_oss_cache() {
21968         local file="$1"
21969         local nodes="$2"
21970
21971         $LFS ladvise -a dontneed $file 2>/dev/null ||
21972                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
21973 }
21974
21975 ladvise_willread_performance()
21976 {
21977         local repeat=10
21978         local average_origin=0
21979         local average_cache=0
21980         local average_ladvise=0
21981
21982         for ((i = 1; i <= $repeat; i++)); do
21983                 echo "Iter $i/$repeat: reading without willread hint"
21984                 cancel_lru_locks osc
21985                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
21986                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
21987                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
21988                 average_origin=$(bc <<<"$average_origin + $speed_origin")
21989
21990                 cancel_lru_locks osc
21991                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
21992                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
21993                 average_cache=$(bc <<<"$average_cache + $speed_cache")
21994
21995                 cancel_lru_locks osc
21996                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
21997                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
21998                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
21999                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
22000                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
22001         done
22002         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
22003         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
22004         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
22005
22006         speedup_cache=$(percent $average_cache $average_origin)
22007         speedup_ladvise=$(percent $average_ladvise $average_origin)
22008
22009         echo "Average uncached read: $average_origin"
22010         echo "Average speedup with OSS cached read: " \
22011                 "$average_cache = +$speedup_cache%"
22012         echo "Average speedup with ladvise willread: " \
22013                 "$average_ladvise = +$speedup_ladvise%"
22014
22015         local lowest_speedup=20
22016         if (( ${average_cache%.*} < $lowest_speedup )); then
22017                 echo "Speedup with OSS cached read less than $lowest_speedup%,"\
22018                      " got $average_cache%. Skipping ladvise willread check."
22019                 return 0
22020         fi
22021
22022         # the test won't work on ZFS until it supports 'ladvise dontneed', but
22023         # it is still good to run until then to exercise 'ladvise willread'
22024         ! $LFS ladvise -a dontneed $DIR/$tfile &&
22025                 [ "$ost1_FSTYPE" = "zfs" ] &&
22026                 echo "osd-zfs does not support dontneed or drop_caches" &&
22027                 return 0
22028
22029         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
22030         (( ${average_ladvise%.*} > ${lowest_speedup%.*} )) ||
22031                 error_not_in_vm "Speedup with willread is less than " \
22032                         "$lowest_speedup%, got $average_ladvise%"
22033 }
22034
22035 test_255a() {
22036         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
22037                 skip "lustre < 2.8.54 does not support ladvise "
22038         remote_ost_nodsh && skip "remote OST with nodsh"
22039
22040         stack_trap "rm -f $DIR/$tfile"
22041         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
22042
22043         ladvise_no_type willread $DIR/$tfile &&
22044                 skip "willread ladvise is not supported"
22045
22046         ladvise_no_ioctl $DIR/$tfile &&
22047                 skip "ladvise ioctl is not supported"
22048
22049         local size_mb=100
22050         local size=$((size_mb * 1048576))
22051         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
22052                 error "dd to $DIR/$tfile failed"
22053
22054         lfs ladvise -a willread $DIR/$tfile ||
22055                 error "Ladvise failed with no range argument"
22056
22057         lfs ladvise -a willread -s 0 $DIR/$tfile ||
22058                 error "Ladvise failed with no -l or -e argument"
22059
22060         lfs ladvise -a willread -e 1 $DIR/$tfile ||
22061                 error "Ladvise failed with only -e argument"
22062
22063         lfs ladvise -a willread -l 1 $DIR/$tfile ||
22064                 error "Ladvise failed with only -l argument"
22065
22066         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
22067                 error "End offset should not be smaller than start offset"
22068
22069         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
22070                 error "End offset should not be equal to start offset"
22071
22072         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
22073                 error "Ladvise failed with overflowing -s argument"
22074
22075         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
22076                 error "Ladvise failed with overflowing -e argument"
22077
22078         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
22079                 error "Ladvise failed with overflowing -l argument"
22080
22081         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
22082                 error "Ladvise succeeded with conflicting -l and -e arguments"
22083
22084         echo "Synchronous ladvise should wait"
22085         local delay=4
22086 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
22087         do_nodes $(comma_list $(osts_nodes)) \
22088                 $LCTL set_param fail_val=$delay fail_loc=0x237
22089
22090         local start_ts=$SECONDS
22091         lfs ladvise -a willread $DIR/$tfile ||
22092                 error "Ladvise failed with no range argument"
22093         local end_ts=$SECONDS
22094         local inteval_ts=$((end_ts - start_ts))
22095
22096         if [ $inteval_ts -lt $(($delay - 1)) ]; then
22097                 error "Synchronous advice didn't wait reply"
22098         fi
22099
22100         echo "Asynchronous ladvise shouldn't wait"
22101         local start_ts=$SECONDS
22102         lfs ladvise -a willread -b $DIR/$tfile ||
22103                 error "Ladvise failed with no range argument"
22104         local end_ts=$SECONDS
22105         local inteval_ts=$((end_ts - start_ts))
22106
22107         if [ $inteval_ts -gt $(($delay / 2)) ]; then
22108                 error "Asynchronous advice blocked"
22109         fi
22110
22111         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
22112         ladvise_willread_performance
22113 }
22114 run_test 255a "check 'lfs ladvise -a willread'"
22115
22116 facet_meminfo() {
22117         local facet=$1
22118         local info=$2
22119
22120         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
22121 }
22122
22123 test_255b() {
22124         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
22125                 skip "lustre < 2.8.54 does not support ladvise "
22126         remote_ost_nodsh && skip "remote OST with nodsh"
22127
22128         stack_trap "rm -f $DIR/$tfile"
22129         lfs setstripe -c 1 -i 0 $DIR/$tfile
22130
22131         ladvise_no_type dontneed $DIR/$tfile &&
22132                 skip "dontneed ladvise is not supported"
22133
22134         ladvise_no_ioctl $DIR/$tfile &&
22135                 skip "ladvise ioctl is not supported"
22136
22137         ! $LFS ladvise -a dontneed $DIR/$tfile &&
22138                 [ "$ost1_FSTYPE" = "zfs" ] &&
22139                 skip "zfs-osd does not support 'ladvise dontneed'"
22140
22141         local size_mb=100
22142         local size=$((size_mb * 1048576))
22143         # In order to prevent disturbance of other processes, only check 3/4
22144         # of the memory usage
22145         local kibibytes=$((size_mb * 1024 * 3 / 4))
22146
22147         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
22148                 error "dd to $DIR/$tfile failed"
22149
22150         #force write to complete before dropping OST cache & checking memory
22151         sync
22152
22153         local total=$(facet_meminfo ost1 MemTotal)
22154         echo "Total memory: $total KiB"
22155
22156         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
22157         local before_read=$(facet_meminfo ost1 Cached)
22158         echo "Cache used before read: $before_read KiB"
22159
22160         lfs ladvise -a willread $DIR/$tfile ||
22161                 error "Ladvise willread failed"
22162         local after_read=$(facet_meminfo ost1 Cached)
22163         echo "Cache used after read: $after_read KiB"
22164
22165         lfs ladvise -a dontneed $DIR/$tfile ||
22166                 error "Ladvise dontneed again failed"
22167         local no_read=$(facet_meminfo ost1 Cached)
22168         echo "Cache used after dontneed ladvise: $no_read KiB"
22169
22170         if [ $total -lt $((before_read + kibibytes)) ]; then
22171                 echo "Memory is too small, abort checking"
22172                 return 0
22173         fi
22174
22175         if [ $((before_read + kibibytes)) -gt $after_read ]; then
22176                 error "Ladvise willread should use more memory" \
22177                         "than $kibibytes KiB"
22178         fi
22179
22180         if [ $((no_read + kibibytes)) -gt $after_read ]; then
22181                 error "Ladvise dontneed should release more memory" \
22182                         "than $kibibytes KiB"
22183         fi
22184 }
22185 run_test 255b "check 'lfs ladvise -a dontneed'"
22186
22187 test_255c() {
22188         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
22189                 skip "lustre < 2.10.50 does not support lockahead"
22190
22191         local ost1_imp=$(get_osc_import_name client ost1)
22192         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
22193                          cut -d'.' -f2)
22194         local count
22195         local new_count
22196         local difference
22197         local i
22198         local rc
22199
22200         test_mkdir -p $DIR/$tdir
22201         $LFS setstripe -i 0 -c 1 $DIR/$tdir
22202
22203         #test 10 returns only success/failure
22204         i=10
22205         lockahead_test -d $DIR/$tdir -t $i -f $tfile
22206         rc=$?
22207         if [ $rc -eq 255 ]; then
22208                 error "Ladvise test${i} failed, ${rc}"
22209         fi
22210
22211         #test 11 counts lock enqueue requests, all others count new locks
22212         i=11
22213         count=$(do_facet ost1 \
22214                 $LCTL get_param -n ost.OSS.ost.stats)
22215         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
22216
22217         lockahead_test -d $DIR/$tdir -t $i -f $tfile
22218         rc=$?
22219         if [ $rc -eq 255 ]; then
22220                 error "Ladvise test${i} failed, ${rc}"
22221         fi
22222
22223         new_count=$(do_facet ost1 \
22224                 $LCTL get_param -n ost.OSS.ost.stats)
22225         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
22226                    awk '{ print $2 }')
22227
22228         difference="$((new_count - count))"
22229         if [ $difference -ne $rc ]; then
22230                 error "Ladvise test${i}, bad enqueue count, returned " \
22231                       "${rc}, actual ${difference}"
22232         fi
22233
22234         for i in $(seq 12 21); do
22235                 # If we do not do this, we run the risk of having too many
22236                 # locks and starting lock cancellation while we are checking
22237                 # lock counts.
22238                 cancel_lru_locks osc
22239
22240                 count=$($LCTL get_param -n \
22241                        ldlm.namespaces.$imp_name.lock_unused_count)
22242
22243                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
22244                 rc=$?
22245                 if [ $rc -eq 255 ]; then
22246                         error "Ladvise test ${i} failed, ${rc}"
22247                 fi
22248
22249                 new_count=$($LCTL get_param -n \
22250                        ldlm.namespaces.$imp_name.lock_unused_count)
22251                 difference="$((new_count - count))"
22252
22253                 # Test 15 output is divided by 100 to map down to valid return
22254                 if [ $i -eq 15 ]; then
22255                         rc="$((rc * 100))"
22256                 fi
22257
22258                 if [ $difference -ne $rc ]; then
22259                         error "Ladvise test ${i}, bad lock count, returned " \
22260                               "${rc}, actual ${difference}"
22261                 fi
22262         done
22263
22264         #test 22 returns only success/failure
22265         i=22
22266         lockahead_test -d $DIR/$tdir -t $i -f $tfile
22267         rc=$?
22268         if [ $rc -eq 255 ]; then
22269                 error "Ladvise test${i} failed, ${rc}"
22270         fi
22271 }
22272 run_test 255c "suite of ladvise lockahead tests"
22273
22274 test_256() {
22275         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22276         remote_mds_nodsh && skip "remote MDS with nodsh"
22277         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
22278         changelog_users $SINGLEMDS | grep "^cl" &&
22279                 skip "active changelog user"
22280
22281         local cl_user
22282         local cat_sl
22283         local mdt_dev
22284
22285         mdt_dev=$(facet_device $SINGLEMDS)
22286         echo $mdt_dev
22287
22288         changelog_register || error "changelog_register failed"
22289
22290         rm -rf $DIR/$tdir
22291         mkdir_on_mdt -i$(($(facet_number $SINGLEMDS) - 1)) $DIR/$tdir
22292
22293         changelog_clear 0 || error "changelog_clear failed"
22294
22295         # change something
22296         touch $DIR/$tdir/{1..10}
22297
22298         # stop the MDT
22299         stop $SINGLEMDS || error "Fail to stop MDT"
22300
22301         # remount the MDT
22302         start $SINGLEMDS $(facet_device $SINGLEMDS) $MDS_MOUNT_OPTS ||
22303                 error "Fail to start MDT"
22304
22305         #after mount new plainllog is used
22306         touch $DIR/$tdir/{11..19}
22307         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
22308         stack_trap "rm -f $tmpfile"
22309         cat_sl=$(do_facet $SINGLEMDS "sync; \
22310                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
22311                  llog_reader $tmpfile | grep -c type=1064553b")
22312         do_facet $SINGLEMDS llog_reader $tmpfile
22313
22314         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
22315
22316         changelog_clear 0 || error "changelog_clear failed"
22317
22318         cat_sl=$(do_facet $SINGLEMDS "sync; \
22319                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
22320                  llog_reader $tmpfile | grep -c type=1064553b")
22321
22322         if (( cat_sl == 2 )); then
22323                 error "Empty plain llog was not deleted from changelog catalog"
22324         elif (( cat_sl != 1 )); then
22325                 error "Active plain llog shouldn't be deleted from catalog"
22326         fi
22327 }
22328 run_test 256 "Check llog delete for empty and not full state"
22329
22330 test_257() {
22331         remote_mds_nodsh && skip "remote MDS with nodsh"
22332         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
22333                 skip "Need MDS version at least 2.8.55"
22334
22335         test_mkdir $DIR/$tdir
22336
22337         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
22338                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
22339         stat $DIR/$tdir
22340
22341 #define OBD_FAIL_MDS_XATTR_REP                  0x161
22342         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
22343         local facet=mds$((mdtidx + 1))
22344         set_nodes_failloc $(facet_active_host $facet) 0x80000161
22345         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
22346
22347         stop $facet || error "stop MDS failed"
22348         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
22349                 error "start MDS fail"
22350         wait_recovery_complete $facet
22351 }
22352 run_test 257 "xattr locks are not lost"
22353
22354 # Verify we take the i_mutex when security requires it
22355 test_258a() {
22356 #define OBD_FAIL_IMUTEX_SEC 0x141c
22357         $LCTL set_param fail_loc=0x141c
22358         touch $DIR/$tfile
22359         chmod u+s $DIR/$tfile
22360         chmod a+rwx $DIR/$tfile
22361         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
22362         RC=$?
22363         if [ $RC -ne 0 ]; then
22364                 error "error, failed to take i_mutex, rc=$?"
22365         fi
22366         rm -f $DIR/$tfile
22367 }
22368 run_test 258a "verify i_mutex security behavior when suid attributes is set"
22369
22370 # Verify we do NOT take the i_mutex in the normal case
22371 test_258b() {
22372 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
22373         $LCTL set_param fail_loc=0x141d
22374         touch $DIR/$tfile
22375         chmod a+rwx $DIR
22376         chmod a+rw $DIR/$tfile
22377         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
22378         RC=$?
22379         if [ $RC -ne 0 ]; then
22380                 error "error, took i_mutex unnecessarily, rc=$?"
22381         fi
22382         rm -f $DIR/$tfile
22383
22384 }
22385 run_test 258b "verify i_mutex security behavior"
22386
22387 test_259() {
22388         local file=$DIR/$tfile
22389         local before
22390         local after
22391
22392         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
22393
22394         stack_trap "rm -f $file" EXIT
22395
22396         wait_delete_completed
22397         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22398         echo "before: $before"
22399
22400         $LFS setstripe -i 0 -c 1 $file
22401         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
22402         sync_all_data
22403         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22404         echo "after write: $after"
22405
22406 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
22407         do_facet ost1 $LCTL set_param fail_loc=0x2301
22408         $TRUNCATE $file 0
22409         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22410         echo "after truncate: $after"
22411
22412         stop ost1
22413         do_facet ost1 $LCTL set_param fail_loc=0
22414         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
22415         sleep 2
22416         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22417         echo "after restart: $after"
22418         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
22419                 error "missing truncate?"
22420
22421         return 0
22422 }
22423 run_test 259 "crash at delayed truncate"
22424
22425 test_260() {
22426 #define OBD_FAIL_MDC_CLOSE               0x806
22427         $LCTL set_param fail_loc=0x80000806
22428         touch $DIR/$tfile
22429
22430 }
22431 run_test 260 "Check mdc_close fail"
22432
22433 ### Data-on-MDT sanity tests ###
22434 test_270a() {
22435         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22436                 skip "Need MDS version at least 2.10.55 for DoM"
22437
22438         # create DoM file
22439         local dom=$DIR/$tdir/dom_file
22440         local tmp=$DIR/$tdir/tmp_file
22441
22442         mkdir_on_mdt0 $DIR/$tdir
22443
22444         # basic checks for DoM component creation
22445         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
22446                 error "Can set MDT layout to non-first entry"
22447
22448         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
22449                 error "Can define multiple entries as MDT layout"
22450
22451         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
22452
22453         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
22454         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
22455         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
22456
22457         local mdtidx=$($LFS getstripe -m $dom)
22458         local mdtname=MDT$(printf %04x $mdtidx)
22459         local facet=mds$((mdtidx + 1))
22460         local space_check=1
22461
22462         # Skip free space checks with ZFS
22463         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
22464
22465         # write
22466         sync
22467         local size_tmp=$((65536 * 3))
22468         local mdtfree1=$(do_facet $facet \
22469                          lctl get_param -n osd*.*$mdtname.kbytesfree)
22470
22471         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
22472         # check also direct IO along write
22473         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
22474         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
22475         sync
22476         cmp $tmp $dom || error "file data is different"
22477         [ $(stat -c%s $dom) == $size_tmp ] ||
22478                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
22479         if [ $space_check == 1 ]; then
22480                 local mdtfree2=$(do_facet $facet \
22481                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
22482
22483                 # increase in usage from by $size_tmp
22484                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
22485                         error "MDT free space wrong after write: " \
22486                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
22487         fi
22488
22489         # truncate
22490         local size_dom=10000
22491
22492         $TRUNCATE $dom $size_dom
22493         [ $(stat -c%s $dom) == $size_dom ] ||
22494                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
22495         if [ $space_check == 1 ]; then
22496                 mdtfree1=$(do_facet $facet \
22497                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22498                 # decrease in usage from $size_tmp to new $size_dom
22499                 [ $(($mdtfree1 - $mdtfree2)) -ge \
22500                   $(((size_tmp - size_dom) / 1024)) ] ||
22501                         error "MDT free space is wrong after truncate: " \
22502                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
22503         fi
22504
22505         # append
22506         cat $tmp >> $dom
22507         sync
22508         size_dom=$((size_dom + size_tmp))
22509         [ $(stat -c%s $dom) == $size_dom ] ||
22510                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
22511         if [ $space_check == 1 ]; then
22512                 mdtfree2=$(do_facet $facet \
22513                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22514                 # increase in usage by $size_tmp from previous
22515                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
22516                         error "MDT free space is wrong after append: " \
22517                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
22518         fi
22519
22520         # delete
22521         rm $dom
22522         if [ $space_check == 1 ]; then
22523                 mdtfree1=$(do_facet $facet \
22524                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22525                 # decrease in usage by $size_dom from previous
22526                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
22527                         error "MDT free space is wrong after removal: " \
22528                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
22529         fi
22530
22531         # combined striping
22532         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
22533                 error "Can't create DoM + OST striping"
22534
22535         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
22536         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
22537         # check also direct IO along write
22538         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
22539         sync
22540         cmp $tmp $dom || error "file data is different"
22541         [ $(stat -c%s $dom) == $size_tmp ] ||
22542                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
22543         rm $dom $tmp
22544
22545         return 0
22546 }
22547 run_test 270a "DoM: basic functionality tests"
22548
22549 test_270b() {
22550         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22551                 skip "Need MDS version at least 2.10.55"
22552
22553         local dom=$DIR/$tdir/dom_file
22554         local max_size=1048576
22555
22556         mkdir -p $DIR/$tdir
22557         $LFS setstripe -E $max_size -L mdt $dom
22558
22559         # truncate over the limit
22560         $TRUNCATE $dom $(($max_size + 1)) &&
22561                 error "successful truncate over the maximum size"
22562         # write over the limit
22563         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
22564                 error "successful write over the maximum size"
22565         # append over the limit
22566         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
22567         echo "12345" >> $dom && error "successful append over the maximum size"
22568         rm $dom
22569
22570         return 0
22571 }
22572 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
22573
22574 test_270c() {
22575         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22576                 skip "Need MDS version at least 2.10.55"
22577
22578         mkdir -p $DIR/$tdir
22579         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22580
22581         # check files inherit DoM EA
22582         touch $DIR/$tdir/first
22583         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
22584                 error "bad pattern"
22585         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
22586                 error "bad stripe count"
22587         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
22588                 error "bad stripe size"
22589
22590         # check directory inherits DoM EA and uses it as default
22591         mkdir $DIR/$tdir/subdir
22592         touch $DIR/$tdir/subdir/second
22593         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
22594                 error "bad pattern in sub-directory"
22595         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
22596                 error "bad stripe count in sub-directory"
22597         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
22598                 error "bad stripe size in sub-directory"
22599         return 0
22600 }
22601 run_test 270c "DoM: DoM EA inheritance tests"
22602
22603 test_270d() {
22604         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22605                 skip "Need MDS version at least 2.10.55"
22606
22607         mkdir -p $DIR/$tdir
22608         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22609
22610         # inherit default DoM striping
22611         mkdir $DIR/$tdir/subdir
22612         touch $DIR/$tdir/subdir/f1
22613
22614         # change default directory striping
22615         $LFS setstripe -c 1 $DIR/$tdir/subdir
22616         touch $DIR/$tdir/subdir/f2
22617         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
22618                 error "wrong default striping in file 2"
22619         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
22620                 error "bad pattern in file 2"
22621         return 0
22622 }
22623 run_test 270d "DoM: change striping from DoM to RAID0"
22624
22625 test_270e() {
22626         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22627                 skip "Need MDS version at least 2.10.55"
22628
22629         mkdir -p $DIR/$tdir/dom
22630         mkdir -p $DIR/$tdir/norm
22631         DOMFILES=20
22632         NORMFILES=10
22633         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
22634         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
22635
22636         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
22637         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
22638
22639         # find DoM files by layout
22640         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
22641         [ $NUM -eq  $DOMFILES ] ||
22642                 error "lfs find -L: found $NUM, expected $DOMFILES"
22643         echo "Test 1: lfs find 20 DOM files by layout: OK"
22644
22645         # there should be 1 dir with default DOM striping
22646         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
22647         [ $NUM -eq  1 ] ||
22648                 error "lfs find -L: found $NUM, expected 1 dir"
22649         echo "Test 2: lfs find 1 DOM dir by layout: OK"
22650
22651         # find DoM files by stripe size
22652         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
22653         [ $NUM -eq  $DOMFILES ] ||
22654                 error "lfs find -S: found $NUM, expected $DOMFILES"
22655         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
22656
22657         # find files by stripe offset except DoM files
22658         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
22659         [ $NUM -eq  $NORMFILES ] ||
22660                 error "lfs find -i: found $NUM, expected $NORMFILES"
22661         echo "Test 5: lfs find no DOM files by stripe index: OK"
22662         return 0
22663 }
22664 run_test 270e "DoM: lfs find with DoM files test"
22665
22666 test_270f() {
22667         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22668                 skip "Need MDS version at least 2.10.55"
22669
22670         local mdtname=${FSNAME}-MDT0000-mdtlov
22671         local dom=$DIR/$tdir/dom_file
22672         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
22673                                                 lod.$mdtname.dom_stripesize)
22674         local dom_limit=131072
22675
22676         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
22677         local dom_current=$(do_facet mds1 $LCTL get_param -n \
22678                                                 lod.$mdtname.dom_stripesize)
22679         [ ${dom_limit} -eq ${dom_current} ] ||
22680                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
22681
22682         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22683         $LFS setstripe -d $DIR/$tdir
22684         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
22685                 error "Can't set directory default striping"
22686
22687         # exceed maximum stripe size
22688         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
22689                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
22690         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
22691                 error "Able to create DoM component size more than LOD limit"
22692
22693         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
22694         dom_current=$(do_facet mds1 $LCTL get_param -n \
22695                                                 lod.$mdtname.dom_stripesize)
22696         [ 0 -eq ${dom_current} ] ||
22697                 error "Can't set zero DoM stripe limit"
22698         rm $dom
22699
22700         # attempt to create DoM file on server with disabled DoM should
22701         # remove DoM entry from layout and be succeed
22702         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
22703                 error "Can't create DoM file (DoM is disabled)"
22704         [ $($LFS getstripe -L $dom) == "mdt" ] &&
22705                 error "File has DoM component while DoM is disabled"
22706         rm $dom
22707
22708         # attempt to create DoM file with only DoM stripe should return error
22709         $LFS setstripe -E $dom_limit -L mdt $dom &&
22710                 error "Able to create DoM-only file while DoM is disabled"
22711
22712         # too low values to be aligned with smallest stripe size 64K
22713         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
22714         dom_current=$(do_facet mds1 $LCTL get_param -n \
22715                                                 lod.$mdtname.dom_stripesize)
22716         [ 30000 -eq ${dom_current} ] &&
22717                 error "Can set too small DoM stripe limit"
22718
22719         # 64K is a minimal stripe size in Lustre, expect limit of that size
22720         [ 65536 -eq ${dom_current} ] ||
22721                 error "Limit is not set to 64K but ${dom_current}"
22722
22723         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
22724         dom_current=$(do_facet mds1 $LCTL get_param -n \
22725                                                 lod.$mdtname.dom_stripesize)
22726         echo $dom_current
22727         [ 2147483648 -eq ${dom_current} ] &&
22728                 error "Can set too large DoM stripe limit"
22729
22730         do_facet mds1 $LCTL set_param -n \
22731                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
22732         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
22733                 error "Can't create DoM component size after limit change"
22734         do_facet mds1 $LCTL set_param -n \
22735                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
22736         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
22737                 error "Can't create DoM file after limit decrease"
22738         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
22739                 error "Can create big DoM component after limit decrease"
22740         touch ${dom}_def ||
22741                 error "Can't create file with old default layout"
22742
22743         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
22744         return 0
22745 }
22746 run_test 270f "DoM: maximum DoM stripe size checks"
22747
22748 test_270g() {
22749         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
22750                 skip "Need MDS version at least 2.13.52"
22751         local dom=$DIR/$tdir/$tfile
22752
22753         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22754         local lodname=${FSNAME}-MDT0000-mdtlov
22755
22756         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22757         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
22758         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
22759         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22760
22761         local dom_limit=1024
22762         local dom_threshold="50%"
22763
22764         $LFS setstripe -d $DIR/$tdir
22765         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
22766                 error "Can't set directory default striping"
22767
22768         do_facet mds1 $LCTL set_param -n \
22769                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
22770         # set 0 threshold and create DOM file to change tunable stripesize
22771         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
22772         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
22773                 error "Failed to create $dom file"
22774         # now tunable dom_cur_stripesize should reach maximum
22775         local dom_current=$(do_facet mds1 $LCTL get_param -n \
22776                                         lod.${lodname}.dom_stripesize_cur_kb)
22777         [[ $dom_current == $dom_limit ]] ||
22778                 error "Current DOM stripesize is not maximum"
22779         rm $dom
22780
22781         # set threshold for further tests
22782         do_facet mds1 $LCTL set_param -n \
22783                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
22784         echo "DOM threshold is $dom_threshold free space"
22785         local dom_def
22786         local dom_set
22787         # Spoof bfree to exceed threshold
22788         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
22789         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
22790         for spfree in 40 20 0 15 30 55; do
22791                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
22792                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
22793                         error "Failed to create $dom file"
22794                 dom_def=$(do_facet mds1 $LCTL get_param -n \
22795                                         lod.${lodname}.dom_stripesize_cur_kb)
22796                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
22797                 [[ $dom_def != $dom_current ]] ||
22798                         error "Default stripe size was not changed"
22799                 if (( spfree > 0 )) ; then
22800                         dom_set=$($LFS getstripe -S $dom)
22801                         (( dom_set == dom_def * 1024 )) ||
22802                                 error "DOM component size is still old"
22803                 else
22804                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
22805                                 error "DoM component is set with no free space"
22806                 fi
22807                 rm $dom
22808                 dom_current=$dom_def
22809         done
22810 }
22811 run_test 270g "DoM: default DoM stripe size depends on free space"
22812
22813 test_270h() {
22814         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
22815                 skip "Need MDS version at least 2.13.53"
22816
22817         local mdtname=${FSNAME}-MDT0000-mdtlov
22818         local dom=$DIR/$tdir/$tfile
22819         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22820
22821         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
22822         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22823
22824         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22825         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
22826                 error "can't create OST file"
22827         # mirrored file with DOM entry in the second mirror
22828         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
22829                 error "can't create mirror with DoM component"
22830
22831         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
22832
22833         # DOM component in the middle and has other enries in the same mirror,
22834         # should succeed but lost DoM component
22835         $LFS setstripe --copy=${dom}_1 $dom ||
22836                 error "Can't create file from OST|DOM mirror layout"
22837         # check new file has no DoM layout after all
22838         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
22839                 error "File has DoM component while DoM is disabled"
22840 }
22841 run_test 270h "DoM: DoM stripe removal when disabled on server"
22842
22843 test_270i() {
22844         (( $MDS1_VERSION >= $(version_code 2.14.54) )) ||
22845                 skip "Need MDS version at least 2.14.54"
22846
22847         mkdir $DIR/$tdir
22848         $LFS setstripe -L mdt -S 128k -c -1 $DIR/$tdir &&
22849                 error "setstripe should fail" || true
22850 }
22851 run_test 270i "DoM: setting invalid DoM striping should fail"
22852
22853 test_271a() {
22854         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22855                 skip "Need MDS version at least 2.10.55"
22856
22857         local dom=$DIR/$tdir/dom
22858
22859         mkdir -p $DIR/$tdir
22860
22861         $LFS setstripe -E 1024K -L mdt $dom
22862
22863         lctl set_param -n mdc.*.stats=clear
22864         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
22865         cat $dom > /dev/null
22866         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
22867         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
22868         ls $dom
22869         rm -f $dom
22870 }
22871 run_test 271a "DoM: data is cached for read after write"
22872
22873 test_271b() {
22874         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22875                 skip "Need MDS version at least 2.10.55"
22876
22877         local dom=$DIR/$tdir/dom
22878
22879         mkdir -p $DIR/$tdir
22880
22881         $LFS setstripe -E 1024K -L mdt -E EOF $dom
22882
22883         lctl set_param -n mdc.*.stats=clear
22884         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
22885         cancel_lru_locks mdc
22886         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
22887         # second stat to check size is cached on client
22888         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
22889         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
22890         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
22891         rm -f $dom
22892 }
22893 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
22894
22895 test_271ba() {
22896         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22897                 skip "Need MDS version at least 2.10.55"
22898
22899         local dom=$DIR/$tdir/dom
22900
22901         mkdir -p $DIR/$tdir
22902
22903         $LFS setstripe -E 1024K -L mdt -E EOF $dom
22904
22905         lctl set_param -n mdc.*.stats=clear
22906         lctl set_param -n osc.*.stats=clear
22907         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
22908         cancel_lru_locks mdc
22909         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
22910         # second stat to check size is cached on client
22911         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
22912         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
22913         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
22914         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
22915         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
22916         rm -f $dom
22917 }
22918 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
22919
22920
22921 get_mdc_stats() {
22922         local mdtidx=$1
22923         local param=$2
22924         local mdt=MDT$(printf %04x $mdtidx)
22925
22926         if [ -z $param ]; then
22927                 lctl get_param -n mdc.*$mdt*.stats
22928         else
22929                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
22930         fi
22931 }
22932
22933 test_271c() {
22934         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22935                 skip "Need MDS version at least 2.10.55"
22936
22937         local dom=$DIR/$tdir/dom
22938
22939         mkdir -p $DIR/$tdir
22940
22941         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22942
22943         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
22944         local facet=mds$((mdtidx + 1))
22945
22946         cancel_lru_locks mdc
22947         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
22948         createmany -o $dom 1000
22949         lctl set_param -n mdc.*.stats=clear
22950         smalliomany -w $dom 1000 200
22951         get_mdc_stats $mdtidx
22952         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
22953         # Each file has 1 open, 1 IO enqueues, total 2000
22954         # but now we have also +1 getxattr for security.capability, total 3000
22955         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
22956         unlinkmany $dom 1000
22957
22958         cancel_lru_locks mdc
22959         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
22960         createmany -o $dom 1000
22961         lctl set_param -n mdc.*.stats=clear
22962         smalliomany -w $dom 1000 200
22963         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
22964         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
22965         # for OPEN and IO lock.
22966         [ $((enq - enq_2)) -ge 1000 ] ||
22967                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
22968         unlinkmany $dom 1000
22969         return 0
22970 }
22971 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
22972
22973 cleanup_271def_tests() {
22974         trap 0
22975         rm -f $1
22976 }
22977
22978 test_271d() {
22979         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
22980                 skip "Need MDS version at least 2.10.57"
22981
22982         local dom=$DIR/$tdir/dom
22983         local tmp=$TMP/$tfile
22984         trap "cleanup_271def_tests $tmp" EXIT
22985
22986         mkdir -p $DIR/$tdir
22987
22988         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22989
22990         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
22991
22992         cancel_lru_locks mdc
22993         dd if=/dev/urandom of=$tmp bs=1000 count=1
22994         dd if=$tmp of=$dom bs=1000 count=1
22995         cancel_lru_locks mdc
22996
22997         cat /etc/hosts >> $tmp
22998         lctl set_param -n mdc.*.stats=clear
22999
23000         # append data to the same file it should update local page
23001         echo "Append to the same page"
23002         cat /etc/hosts >> $dom
23003         local num=$(get_mdc_stats $mdtidx ost_read)
23004         local ra=$(get_mdc_stats $mdtidx req_active)
23005         local rw=$(get_mdc_stats $mdtidx req_waittime)
23006
23007         [ -z $num ] || error "$num READ RPC occured"
23008         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23009         echo "... DONE"
23010
23011         # compare content
23012         cmp $tmp $dom || error "file miscompare"
23013
23014         cancel_lru_locks mdc
23015         lctl set_param -n mdc.*.stats=clear
23016
23017         echo "Open and read file"
23018         cat $dom > /dev/null
23019         local num=$(get_mdc_stats $mdtidx ost_read)
23020         local ra=$(get_mdc_stats $mdtidx req_active)
23021         local rw=$(get_mdc_stats $mdtidx req_waittime)
23022
23023         [ -z $num ] || error "$num READ RPC occured"
23024         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23025         echo "... DONE"
23026
23027         # compare content
23028         cmp $tmp $dom || error "file miscompare"
23029
23030         return 0
23031 }
23032 run_test 271d "DoM: read on open (1K file in reply buffer)"
23033
23034 test_271f() {
23035         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
23036                 skip "Need MDS version at least 2.10.57"
23037
23038         local dom=$DIR/$tdir/dom
23039         local tmp=$TMP/$tfile
23040         trap "cleanup_271def_tests $tmp" EXIT
23041
23042         mkdir -p $DIR/$tdir
23043
23044         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23045
23046         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
23047
23048         cancel_lru_locks mdc
23049         dd if=/dev/urandom of=$tmp bs=265000 count=1
23050         dd if=$tmp of=$dom bs=265000 count=1
23051         cancel_lru_locks mdc
23052         cat /etc/hosts >> $tmp
23053         lctl set_param -n mdc.*.stats=clear
23054
23055         echo "Append to the same page"
23056         cat /etc/hosts >> $dom
23057         local num=$(get_mdc_stats $mdtidx ost_read)
23058         local ra=$(get_mdc_stats $mdtidx req_active)
23059         local rw=$(get_mdc_stats $mdtidx req_waittime)
23060
23061         [ -z $num ] || error "$num READ RPC occured"
23062         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23063         echo "... DONE"
23064
23065         # compare content
23066         cmp $tmp $dom || error "file miscompare"
23067
23068         cancel_lru_locks mdc
23069         lctl set_param -n mdc.*.stats=clear
23070
23071         echo "Open and read file"
23072         cat $dom > /dev/null
23073         local num=$(get_mdc_stats $mdtidx ost_read)
23074         local ra=$(get_mdc_stats $mdtidx req_active)
23075         local rw=$(get_mdc_stats $mdtidx req_waittime)
23076
23077         [ -z $num ] && num=0
23078         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
23079         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23080         echo "... DONE"
23081
23082         # compare content
23083         cmp $tmp $dom || error "file miscompare"
23084
23085         return 0
23086 }
23087 run_test 271f "DoM: read on open (200K file and read tail)"
23088
23089 test_271g() {
23090         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
23091                 skip "Skipping due to old client or server version"
23092
23093         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
23094         # to get layout
23095         $CHECKSTAT -t file $DIR1/$tfile
23096
23097         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
23098         MULTIOP_PID=$!
23099         sleep 1
23100         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
23101         $LCTL set_param fail_loc=0x80000314
23102         rm $DIR1/$tfile || error "Unlink fails"
23103         RC=$?
23104         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
23105         [ $RC -eq 0 ] || error "Failed write to stale object"
23106 }
23107 run_test 271g "Discard DoM data vs client flush race"
23108
23109 test_272a() {
23110         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23111                 skip "Need MDS version at least 2.11.50"
23112
23113         local dom=$DIR/$tdir/dom
23114         mkdir -p $DIR/$tdir
23115
23116         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
23117         dd if=/dev/urandom of=$dom bs=512K count=1 ||
23118                 error "failed to write data into $dom"
23119         local old_md5=$(md5sum $dom)
23120
23121         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
23122                 error "failed to migrate to the same DoM component"
23123
23124         local new_md5=$(md5sum $dom)
23125
23126         [ "$old_md5" == "$new_md5" ] ||
23127                 error "md5sum differ: $old_md5, $new_md5"
23128
23129         [ $($LFS getstripe -c $dom) -eq 2 ] ||
23130                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
23131 }
23132 run_test 272a "DoM migration: new layout with the same DOM component"
23133
23134 test_272b() {
23135         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23136                 skip "Need MDS version at least 2.11.50"
23137
23138         local dom=$DIR/$tdir/dom
23139         mkdir -p $DIR/$tdir
23140         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23141
23142         local mdtidx=$($LFS getstripe -m $dom)
23143         local mdtname=MDT$(printf %04x $mdtidx)
23144         local facet=mds$((mdtidx + 1))
23145
23146         local mdtfree1=$(do_facet $facet \
23147                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23148         dd if=/dev/urandom of=$dom bs=2M count=1 ||
23149                 error "failed to write data into $dom"
23150         local old_md5=$(md5sum $dom)
23151         cancel_lru_locks mdc
23152         local mdtfree1=$(do_facet $facet \
23153                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23154
23155         $LFS migrate -c2 $dom ||
23156                 error "failed to migrate to the new composite layout"
23157         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
23158                 error "MDT stripe was not removed"
23159
23160         cancel_lru_locks mdc
23161         local new_md5=$(md5sum $dom)
23162         [ "$old_md5" == "$new_md5" ] ||
23163                 error "$old_md5 != $new_md5"
23164
23165         # Skip free space checks with ZFS
23166         if [ "$(facet_fstype $facet)" != "zfs" ]; then
23167                 local mdtfree2=$(do_facet $facet \
23168                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23169                 [ $mdtfree2 -gt $mdtfree1 ] ||
23170                         error "MDT space is not freed after migration"
23171         fi
23172         return 0
23173 }
23174 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
23175
23176 test_272c() {
23177         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23178                 skip "Need MDS version at least 2.11.50"
23179
23180         local dom=$DIR/$tdir/$tfile
23181         mkdir -p $DIR/$tdir
23182         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23183
23184         local mdtidx=$($LFS getstripe -m $dom)
23185         local mdtname=MDT$(printf %04x $mdtidx)
23186         local facet=mds$((mdtidx + 1))
23187
23188         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23189                 error "failed to write data into $dom"
23190         local old_md5=$(md5sum $dom)
23191         cancel_lru_locks mdc
23192         local mdtfree1=$(do_facet $facet \
23193                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23194
23195         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
23196                 error "failed to migrate to the new composite layout"
23197         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
23198                 error "MDT stripe was not removed"
23199
23200         cancel_lru_locks mdc
23201         local new_md5=$(md5sum $dom)
23202         [ "$old_md5" == "$new_md5" ] ||
23203                 error "$old_md5 != $new_md5"
23204
23205         # Skip free space checks with ZFS
23206         if [ "$(facet_fstype $facet)" != "zfs" ]; then
23207                 local mdtfree2=$(do_facet $facet \
23208                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23209                 [ $mdtfree2 -gt $mdtfree1 ] ||
23210                         error "MDS space is not freed after migration"
23211         fi
23212         return 0
23213 }
23214 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
23215
23216 test_272d() {
23217         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
23218                 skip "Need MDS version at least 2.12.55"
23219
23220         local dom=$DIR/$tdir/$tfile
23221         mkdir -p $DIR/$tdir
23222         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23223
23224         local mdtidx=$($LFS getstripe -m $dom)
23225         local mdtname=MDT$(printf %04x $mdtidx)
23226         local facet=mds$((mdtidx + 1))
23227
23228         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23229                 error "failed to write data into $dom"
23230         local old_md5=$(md5sum $dom)
23231         cancel_lru_locks mdc
23232         local mdtfree1=$(do_facet $facet \
23233                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23234
23235         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
23236                 error "failed mirroring to the new composite layout"
23237         $LFS mirror resync $dom ||
23238                 error "failed mirror resync"
23239         $LFS mirror split --mirror-id 1 -d $dom ||
23240                 error "failed mirror split"
23241
23242         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
23243                 error "MDT stripe was not removed"
23244
23245         cancel_lru_locks mdc
23246         local new_md5=$(md5sum $dom)
23247         [ "$old_md5" == "$new_md5" ] ||
23248                 error "$old_md5 != $new_md5"
23249
23250         # Skip free space checks with ZFS
23251         if [ "$(facet_fstype $facet)" != "zfs" ]; then
23252                 local mdtfree2=$(do_facet $facet \
23253                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23254                 [ $mdtfree2 -gt $mdtfree1 ] ||
23255                         error "MDS space is not freed after DOM mirror deletion"
23256         fi
23257         return 0
23258 }
23259 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
23260
23261 test_272e() {
23262         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
23263                 skip "Need MDS version at least 2.12.55"
23264
23265         local dom=$DIR/$tdir/$tfile
23266         mkdir -p $DIR/$tdir
23267         $LFS setstripe -c 2 $dom
23268
23269         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23270                 error "failed to write data into $dom"
23271         local old_md5=$(md5sum $dom)
23272         cancel_lru_locks
23273
23274         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
23275                 error "failed mirroring to the DOM layout"
23276         $LFS mirror resync $dom ||
23277                 error "failed mirror resync"
23278         $LFS mirror split --mirror-id 1 -d $dom ||
23279                 error "failed mirror split"
23280
23281         [[ $($LFS getstripe -L --component-start=0 $dom) == 'mdt' ]] ||
23282                 error "MDT stripe wasn't set"
23283
23284         cancel_lru_locks
23285         local new_md5=$(md5sum $dom)
23286         [ "$old_md5" == "$new_md5" ] ||
23287                 error "$old_md5 != $new_md5"
23288
23289         return 0
23290 }
23291 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
23292
23293 test_272f() {
23294         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
23295                 skip "Need MDS version at least 2.12.55"
23296
23297         local dom=$DIR/$tdir/$tfile
23298         mkdir -p $DIR/$tdir
23299         $LFS setstripe -c 2 $dom
23300
23301         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23302                 error "failed to write data into $dom"
23303         local old_md5=$(md5sum $dom)
23304         cancel_lru_locks
23305
23306         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
23307                 error "failed migrating to the DOM file"
23308
23309         [[ $($LFS getstripe -L --component-start=0 $dom) == 'mdt' ]] ||
23310                 error "MDT stripe wasn't set"
23311
23312         cancel_lru_locks
23313         local new_md5=$(md5sum $dom)
23314         [ "$old_md5" != "$new_md5" ] &&
23315                 error "$old_md5 != $new_md5"
23316
23317         return 0
23318 }
23319 run_test 272f "DoM migration: OST-striped file to DOM file"
23320
23321 test_273a() {
23322         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23323                 skip "Need MDS version at least 2.11.50"
23324
23325         # Layout swap cannot be done if either file has DOM component,
23326         # this will never be supported, migration should be used instead
23327
23328         local dom=$DIR/$tdir/$tfile
23329         mkdir -p $DIR/$tdir
23330
23331         $LFS setstripe -c2 ${dom}_plain
23332         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
23333         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
23334                 error "can swap layout with DoM component"
23335         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
23336                 error "can swap layout with DoM component"
23337
23338         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
23339         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
23340                 error "can swap layout with DoM component"
23341         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
23342                 error "can swap layout with DoM component"
23343         return 0
23344 }
23345 run_test 273a "DoM: layout swapping should fail with DOM"
23346
23347 test_273b() {
23348         mkdir -p $DIR/$tdir
23349         $LFS setstripe -E 1M -L mdt -E -1 -c -1 $DIR/$tdir
23350
23351 #define OBD_FAIL_MDS_COMMITRW_DELAY      0x16b
23352         do_facet mds1 $LCTL set_param fail_loc=0x8000016b fail_val=2
23353
23354         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
23355 }
23356 run_test 273b "DoM: race writeback and object destroy"
23357
23358 test_275() {
23359         remote_ost_nodsh && skip "remote OST with nodsh"
23360         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
23361                 skip "Need OST version >= 2.10.57"
23362
23363         local file=$DIR/$tfile
23364         local oss
23365
23366         oss=$(comma_list $(osts_nodes))
23367
23368         dd if=/dev/urandom of=$file bs=1M count=2 ||
23369                 error "failed to create a file"
23370         cancel_lru_locks osc
23371
23372         #lock 1
23373         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
23374                 error "failed to read a file"
23375
23376 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
23377         $LCTL set_param fail_loc=0x8000031f
23378
23379         cancel_lru_locks osc &
23380         sleep 1
23381
23382 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
23383         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
23384         #IO takes another lock, but matches the PENDING one
23385         #and places it to the IO RPC
23386         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
23387                 error "failed to read a file with PENDING lock"
23388 }
23389 run_test 275 "Read on a canceled duplicate lock"
23390
23391 test_276() {
23392         remote_ost_nodsh && skip "remote OST with nodsh"
23393         local pid
23394
23395         do_facet ost1 "(while true; do \
23396                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
23397                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
23398         pid=$!
23399
23400         for LOOP in $(seq 20); do
23401                 stop ost1
23402                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
23403         done
23404         kill -9 $pid
23405         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
23406                 rm $TMP/sanity_276_pid"
23407 }
23408 run_test 276 "Race between mount and obd_statfs"
23409
23410 test_277() {
23411         $LCTL set_param ldlm.namespaces.*.lru_size=0
23412         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23413         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
23414                         grep ^used_mb | awk '{print $2}')
23415         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
23416         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
23417                 oflag=direct conv=notrunc
23418         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
23419                         grep ^used_mb | awk '{print $2}')
23420         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
23421 }
23422 run_test 277 "Direct IO shall drop page cache"
23423
23424 test_278() {
23425         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
23426         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23427         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
23428                 skip "needs the same host for mdt1 mdt2" && return
23429
23430         local pid1
23431         local pid2
23432
23433 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
23434         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
23435         stop mds2 &
23436         pid2=$!
23437
23438         stop mds1
23439
23440         echo "Starting MDTs"
23441         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
23442         wait $pid2
23443 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
23444 #will return NULL
23445         do_facet mds2 $LCTL set_param fail_loc=0
23446
23447         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
23448         wait_recovery_complete mds2
23449 }
23450 run_test 278 "Race starting MDS between MDTs stop/start"
23451
23452 test_280() {
23453         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
23454                 skip "Need MGS version at least 2.13.52"
23455         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23456         combined_mgs_mds || skip "needs combined MGS/MDT"
23457
23458         umount_client $MOUNT
23459 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
23460         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
23461
23462         mount_client $MOUNT &
23463         sleep 1
23464         stop mgs || error "stop mgs failed"
23465         #for a race mgs would crash
23466         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
23467         # make sure we unmount client before remounting
23468         wait
23469         umount_client $MOUNT
23470         mount_client $MOUNT || error "mount client failed"
23471 }
23472 run_test 280 "Race between MGS umount and client llog processing"
23473
23474 cleanup_test_300() {
23475         trap 0
23476         umask $SAVE_UMASK
23477 }
23478 test_striped_dir() {
23479         local mdt_index=$1
23480         local stripe_count
23481         local stripe_index
23482
23483         mkdir -p $DIR/$tdir
23484
23485         SAVE_UMASK=$(umask)
23486         trap cleanup_test_300 RETURN EXIT
23487
23488         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
23489                                                 $DIR/$tdir/striped_dir ||
23490                 error "set striped dir error"
23491
23492         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
23493         [ "$mode" = "755" ] || error "expect 755 got $mode"
23494
23495         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
23496                 error "getdirstripe failed"
23497         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
23498         if [ "$stripe_count" != "2" ]; then
23499                 error "1:stripe_count is $stripe_count, expect 2"
23500         fi
23501         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
23502         if [ "$stripe_count" != "2" ]; then
23503                 error "2:stripe_count is $stripe_count, expect 2"
23504         fi
23505
23506         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
23507         if [ "$stripe_index" != "$mdt_index" ]; then
23508                 error "stripe_index is $stripe_index, expect $mdt_index"
23509         fi
23510
23511         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
23512                 error "nlink error after create striped dir"
23513
23514         mkdir $DIR/$tdir/striped_dir/a
23515         mkdir $DIR/$tdir/striped_dir/b
23516
23517         stat $DIR/$tdir/striped_dir/a ||
23518                 error "create dir under striped dir failed"
23519         stat $DIR/$tdir/striped_dir/b ||
23520                 error "create dir under striped dir failed"
23521
23522         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
23523                 error "nlink error after mkdir"
23524
23525         rmdir $DIR/$tdir/striped_dir/a
23526         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
23527                 error "nlink error after rmdir"
23528
23529         rmdir $DIR/$tdir/striped_dir/b
23530         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
23531                 error "nlink error after rmdir"
23532
23533         chattr +i $DIR/$tdir/striped_dir
23534         createmany -o $DIR/$tdir/striped_dir/f 10 &&
23535                 error "immutable flags not working under striped dir!"
23536         chattr -i $DIR/$tdir/striped_dir
23537
23538         rmdir $DIR/$tdir/striped_dir ||
23539                 error "rmdir striped dir error"
23540
23541         cleanup_test_300
23542
23543         true
23544 }
23545
23546 test_300a() {
23547         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23548                 skip "skipped for lustre < 2.7.0"
23549         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23550         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23551
23552         test_striped_dir 0 || error "failed on striped dir on MDT0"
23553         test_striped_dir 1 || error "failed on striped dir on MDT0"
23554 }
23555 run_test 300a "basic striped dir sanity test"
23556
23557 test_300b() {
23558         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23559                 skip "skipped for lustre < 2.7.0"
23560         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23561         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23562
23563         local i
23564         local mtime1
23565         local mtime2
23566         local mtime3
23567
23568         test_mkdir $DIR/$tdir || error "mkdir fail"
23569         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23570                 error "set striped dir error"
23571         for i in {0..9}; do
23572                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
23573                 sleep 1
23574                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
23575                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
23576                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
23577                 sleep 1
23578                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
23579                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
23580                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
23581         done
23582         true
23583 }
23584 run_test 300b "check ctime/mtime for striped dir"
23585
23586 test_300c() {
23587         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23588                 skip "skipped for lustre < 2.7.0"
23589         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23590         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23591
23592         local file_count
23593
23594         mkdir_on_mdt0 $DIR/$tdir
23595         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
23596                 error "set striped dir error"
23597
23598         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
23599                 error "chown striped dir failed"
23600
23601         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
23602                 error "create 5k files failed"
23603
23604         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
23605
23606         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
23607
23608         rm -rf $DIR/$tdir
23609 }
23610 run_test 300c "chown && check ls under striped directory"
23611
23612 test_300d() {
23613         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23614                 skip "skipped for lustre < 2.7.0"
23615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23616         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23617
23618         local stripe_count
23619         local file
23620
23621         mkdir -p $DIR/$tdir
23622         $LFS setstripe -c 2 $DIR/$tdir
23623
23624         #local striped directory
23625         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23626                 error "set striped dir error"
23627         #look at the directories for debug purposes
23628         ls -l $DIR/$tdir
23629         $LFS getdirstripe $DIR/$tdir
23630         ls -l $DIR/$tdir/striped_dir
23631         $LFS getdirstripe $DIR/$tdir/striped_dir
23632         createmany -o $DIR/$tdir/striped_dir/f 10 ||
23633                 error "create 10 files failed"
23634
23635         #remote striped directory
23636         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
23637                 error "set striped dir error"
23638         #look at the directories for debug purposes
23639         ls -l $DIR/$tdir
23640         $LFS getdirstripe $DIR/$tdir
23641         ls -l $DIR/$tdir/remote_striped_dir
23642         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
23643         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
23644                 error "create 10 files failed"
23645
23646         for file in $(find $DIR/$tdir); do
23647                 stripe_count=$($LFS getstripe -c $file)
23648                 [ $stripe_count -eq 2 ] ||
23649                         error "wrong stripe $stripe_count for $file"
23650         done
23651
23652         rm -rf $DIR/$tdir
23653 }
23654 run_test 300d "check default stripe under striped directory"
23655
23656 test_300e() {
23657         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23658                 skip "Need MDS version at least 2.7.55"
23659         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23660         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23661
23662         local stripe_count
23663         local file
23664
23665         mkdir -p $DIR/$tdir
23666
23667         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23668                 error "set striped dir error"
23669
23670         touch $DIR/$tdir/striped_dir/a
23671         touch $DIR/$tdir/striped_dir/b
23672         touch $DIR/$tdir/striped_dir/c
23673
23674         mkdir $DIR/$tdir/striped_dir/dir_a
23675         mkdir $DIR/$tdir/striped_dir/dir_b
23676         mkdir $DIR/$tdir/striped_dir/dir_c
23677
23678         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
23679                 error "set striped adir under striped dir error"
23680
23681         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
23682                 error "set striped bdir under striped dir error"
23683
23684         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
23685                 error "set striped cdir under striped dir error"
23686
23687         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
23688                 error "rename dir under striped dir fails"
23689
23690         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
23691                 error "rename dir under different stripes fails"
23692
23693         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
23694                 error "rename file under striped dir should succeed"
23695
23696         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
23697                 error "rename dir under striped dir should succeed"
23698
23699         rm -rf $DIR/$tdir
23700 }
23701 run_test 300e "check rename under striped directory"
23702
23703 test_300f() {
23704         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23705         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23706         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23707                 skip "Need MDS version at least 2.7.55"
23708
23709         local stripe_count
23710         local file
23711
23712         rm -rf $DIR/$tdir
23713         mkdir -p $DIR/$tdir
23714
23715         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23716                 error "set striped dir error"
23717
23718         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
23719                 error "set striped dir error"
23720
23721         touch $DIR/$tdir/striped_dir/a
23722         mkdir $DIR/$tdir/striped_dir/dir_a
23723         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
23724                 error "create striped dir under striped dir fails"
23725
23726         touch $DIR/$tdir/striped_dir1/b
23727         mkdir $DIR/$tdir/striped_dir1/dir_b
23728         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
23729                 error "create striped dir under striped dir fails"
23730
23731         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
23732                 error "rename dir under different striped dir should fail"
23733
23734         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
23735                 error "rename striped dir under diff striped dir should fail"
23736
23737         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
23738                 error "rename file under diff striped dirs fails"
23739
23740         rm -rf $DIR/$tdir
23741 }
23742 run_test 300f "check rename cross striped directory"
23743
23744 test_300_check_default_striped_dir()
23745 {
23746         local dirname=$1
23747         local default_count=$2
23748         local default_index=$3
23749         local stripe_count
23750         local stripe_index
23751         local dir_stripe_index
23752         local dir
23753
23754         echo "checking $dirname $default_count $default_index"
23755         $LFS setdirstripe -D -c $default_count -i $default_index \
23756                                 -H all_char $DIR/$tdir/$dirname ||
23757                 error "set default stripe on striped dir error"
23758         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
23759         [ $stripe_count -eq $default_count ] ||
23760                 error "expect $default_count get $stripe_count for $dirname"
23761
23762         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
23763         [ $stripe_index -eq $default_index ] ||
23764                 error "expect $default_index get $stripe_index for $dirname"
23765
23766         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
23767                                                 error "create dirs failed"
23768
23769         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
23770         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
23771         for dir in $(find $DIR/$tdir/$dirname/*); do
23772                 stripe_count=$($LFS getdirstripe -c $dir)
23773                 (( $stripe_count == $default_count )) ||
23774                 (( $stripe_count == $MDSCOUNT && $default_count == -1 )) ||
23775                 (( $stripe_count == 0 )) || (( $default_count == 1 )) ||
23776                 error "stripe count $default_count != $stripe_count for $dir"
23777
23778                 stripe_index=$($LFS getdirstripe -i $dir)
23779                 [ $default_index -eq -1 ] ||
23780                         [ $stripe_index -eq $default_index ] ||
23781                         error "$stripe_index != $default_index for $dir"
23782
23783                 #check default stripe
23784                 stripe_count=$($LFS getdirstripe -D -c $dir)
23785                 [ $stripe_count -eq $default_count ] ||
23786                 error "default count $default_count != $stripe_count for $dir"
23787
23788                 stripe_index=$($LFS getdirstripe -D -i $dir)
23789                 [ $stripe_index -eq $default_index ] ||
23790                 error "default index $default_index != $stripe_index for $dir"
23791         done
23792         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
23793 }
23794
23795 test_300g() {
23796         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23797         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23798                 skip "Need MDS version at least 2.7.55"
23799
23800         local dir
23801         local stripe_count
23802         local stripe_index
23803
23804         mkdir_on_mdt0 $DIR/$tdir
23805         mkdir $DIR/$tdir/normal_dir
23806
23807         #Checking when client cache stripe index
23808         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
23809         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
23810                 error "create striped_dir failed"
23811
23812         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
23813                 error "create dir0 fails"
23814         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
23815         [ $stripe_index -eq 0 ] ||
23816                 error "dir0 expect index 0 got $stripe_index"
23817
23818         mkdir $DIR/$tdir/striped_dir/dir1 ||
23819                 error "create dir1 fails"
23820         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
23821         [ $stripe_index -eq 1 ] ||
23822                 error "dir1 expect index 1 got $stripe_index"
23823
23824         #check default stripe count/stripe index
23825         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
23826         test_300_check_default_striped_dir normal_dir 1 0
23827         test_300_check_default_striped_dir normal_dir -1 1
23828         test_300_check_default_striped_dir normal_dir 2 -1
23829
23830         #delete default stripe information
23831         echo "delete default stripeEA"
23832         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
23833                 error "set default stripe on striped dir error"
23834
23835         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
23836         for dir in $(find $DIR/$tdir/normal_dir/*); do
23837                 stripe_count=$($LFS getdirstripe -c $dir)
23838                 [ $stripe_count -eq 0 ] ||
23839                         error "expect 1 get $stripe_count for $dir"
23840         done
23841 }
23842 run_test 300g "check default striped directory for normal directory"
23843
23844 test_300h() {
23845         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23846         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23847                 skip "Need MDS version at least 2.7.55"
23848
23849         local dir
23850         local stripe_count
23851
23852         mkdir $DIR/$tdir
23853         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23854                 error "set striped dir error"
23855
23856         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
23857         test_300_check_default_striped_dir striped_dir 1 0
23858         test_300_check_default_striped_dir striped_dir -1 1
23859         test_300_check_default_striped_dir striped_dir 2 -1
23860
23861         #delete default stripe information
23862         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
23863                 error "set default stripe on striped dir error"
23864
23865         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
23866         for dir in $(find $DIR/$tdir/striped_dir/*); do
23867                 stripe_count=$($LFS getdirstripe -c $dir)
23868                 [ $stripe_count -eq 0 ] ||
23869                         error "expect 1 get $stripe_count for $dir"
23870         done
23871 }
23872 run_test 300h "check default striped directory for striped directory"
23873
23874 test_300i() {
23875         [[ $PARALLEL == "yes" ]] && skip "skip parallel run"
23876         (( $MDSCOUNT >= 2 )) || skip_env "needs >= 2 MDTs"
23877         (( $MDS1_VERSION >= $(version_code 2.7.55) )) ||
23878                 skip "Need MDS version at least 2.7.55"
23879
23880         local stripe_count
23881         local file
23882
23883         mkdir $DIR/$tdir
23884
23885         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23886                 error "set striped dir error"
23887
23888         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
23889                 error "create files under striped dir failed"
23890
23891         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
23892                 error "set striped hashdir error"
23893
23894         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
23895                 error "create dir0 under hash dir failed"
23896         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
23897                 error "create dir1 under hash dir failed"
23898         $LFS setdirstripe -i0 -c$MDSCOUNT -H crush $DIR/$tdir/hashdir/d2 ||
23899                 error "create dir2 under hash dir failed"
23900
23901         # unfortunately, we need to umount to clear dir layout cache for now
23902         # once we fully implement dir layout, we can drop this
23903         umount_client $MOUNT || error "umount failed"
23904         mount_client $MOUNT || error "mount failed"
23905
23906         $LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir
23907         local dircnt=$($LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir | wc -l)
23908         (( $dircnt == 2 )) || error "lfs find striped dir got $dircnt != 2"
23909
23910         if (( $MDS1_VERSION > $(version_code 2.15.0) )); then
23911                 $LFS mkdir -i0 -c$MDSCOUNT -H crush2 $DIR/$tdir/hashdir/d3 ||
23912                         error "create crush2 dir $tdir/hashdir/d3 failed"
23913                 $LFS find -H crush2 $DIR/$tdir/hashdir
23914                 dircnt=$($LFS find -H crush2 $DIR/$tdir/hashdir | wc -l)
23915                 (( $dircnt == 1 )) || error "find crush2 dir got $dircnt != 1"
23916
23917                 # mkdir with an invalid hash type (hash=fail_val) from client
23918                 # should be replaced on MDS with a valid (default) hash type
23919                 #define OBD_FAIL_LMV_UNKNOWN_STRIPE     0x1901
23920                 $LCTL set_param fail_loc=0x1901 fail_val=99
23921                 $LFS mkdir -c2 $DIR/$tdir/hashdir/d99
23922
23923                 local hash=$($LFS getdirstripe -H $DIR/$tdir/hashdir/d99)
23924                 local expect=$(do_facet mds1 \
23925                         $LCTL get_param -n lod.$FSNAME-MDT0000-mdtlov.mdt_hash)
23926                 [[ $hash == $expect ]] ||
23927                         error "d99 hash '$hash' != expected hash '$expect'"
23928         fi
23929
23930         #set the stripe to be unknown hash type on read
23931         #define OBD_FAIL_LMV_UNKNOWN_STRIPE     0x1901
23932         $LCTL set_param fail_loc=0x1901 fail_val=99
23933         for ((i = 0; i < 10; i++)); do
23934                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
23935                         error "stat f-$i failed"
23936                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
23937         done
23938
23939         touch $DIR/$tdir/striped_dir/f0 &&
23940                 error "create under striped dir with unknown hash should fail"
23941
23942         $LCTL set_param fail_loc=0
23943
23944         umount_client $MOUNT || error "umount failed"
23945         mount_client $MOUNT || error "mount failed"
23946
23947         return 0
23948 }
23949 run_test 300i "client handle unknown hash type striped directory"
23950
23951 test_300j() {
23952         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23953         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23954         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23955                 skip "Need MDS version at least 2.7.55"
23956
23957         local stripe_count
23958         local file
23959
23960         mkdir $DIR/$tdir
23961
23962         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
23963         $LCTL set_param fail_loc=0x1702
23964         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
23965                 error "set striped dir error"
23966
23967         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
23968                 error "create files under striped dir failed"
23969
23970         $LCTL set_param fail_loc=0
23971
23972         rm -rf $DIR/$tdir || error "unlink striped dir fails"
23973
23974         return 0
23975 }
23976 run_test 300j "test large update record"
23977
23978 test_300k() {
23979         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23980         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23981         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23982                 skip "Need MDS version at least 2.7.55"
23983
23984         # this test needs a huge transaction
23985         local kb
23986         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
23987              osd*.$FSNAME-MDT0000.kbytestotal")
23988         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
23989
23990         local stripe_count
23991         local file
23992
23993         mkdir $DIR/$tdir
23994
23995         #define OBD_FAIL_LARGE_STRIPE   0x1703
23996         $LCTL set_param fail_loc=0x1703
23997         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
23998                 error "set striped dir error"
23999         $LCTL set_param fail_loc=0
24000
24001         $LFS getdirstripe $DIR/$tdir/striped_dir ||
24002                 error "getstripeddir fails"
24003         rm -rf $DIR/$tdir/striped_dir ||
24004                 error "unlink striped dir fails"
24005
24006         return 0
24007 }
24008 run_test 300k "test large striped directory"
24009
24010 test_300l() {
24011         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24012         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24013         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24014                 skip "Need MDS version at least 2.7.55"
24015
24016         local stripe_index
24017
24018         test_mkdir -p $DIR/$tdir/striped_dir
24019         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
24020                         error "chown $RUNAS_ID failed"
24021         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
24022                 error "set default striped dir failed"
24023
24024         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
24025         $LCTL set_param fail_loc=0x80000158
24026         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
24027
24028         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
24029         [ $stripe_index -eq 1 ] ||
24030                 error "expect 1 get $stripe_index for $dir"
24031 }
24032 run_test 300l "non-root user to create dir under striped dir with stale layout"
24033
24034 test_300m() {
24035         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24036         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
24037         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24038                 skip "Need MDS version at least 2.7.55"
24039
24040         mkdir -p $DIR/$tdir/striped_dir
24041         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
24042                 error "set default stripes dir error"
24043
24044         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
24045
24046         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
24047         [ $stripe_count -eq 0 ] ||
24048                         error "expect 0 get $stripe_count for a"
24049
24050         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
24051                 error "set default stripes dir error"
24052
24053         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
24054
24055         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
24056         [ $stripe_count -eq 0 ] ||
24057                         error "expect 0 get $stripe_count for b"
24058
24059         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
24060                 error "set default stripes dir error"
24061
24062         mkdir $DIR/$tdir/striped_dir/c &&
24063                 error "default stripe_index is invalid, mkdir c should fails"
24064
24065         rm -rf $DIR/$tdir || error "rmdir fails"
24066 }
24067 run_test 300m "setstriped directory on single MDT FS"
24068
24069 cleanup_300n() {
24070         local list=$(comma_list $(mdts_nodes))
24071
24072         trap 0
24073         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24074 }
24075
24076 test_300n() {
24077         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24078         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24079         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24080                 skip "Need MDS version at least 2.7.55"
24081         remote_mds_nodsh && skip "remote MDS with nodsh"
24082
24083         local stripe_index
24084         local list=$(comma_list $(mdts_nodes))
24085
24086         trap cleanup_300n RETURN EXIT
24087         mkdir -p $DIR/$tdir
24088         chmod 777 $DIR/$tdir
24089         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
24090                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
24091                 error "create striped dir succeeds with gid=0"
24092
24093         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
24094         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
24095                 error "create striped dir fails with gid=-1"
24096
24097         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24098         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
24099                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
24100                 error "set default striped dir succeeds with gid=0"
24101
24102
24103         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
24104         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
24105                 error "set default striped dir fails with gid=-1"
24106
24107
24108         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24109         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
24110                                         error "create test_dir fails"
24111         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
24112                                         error "create test_dir1 fails"
24113         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
24114                                         error "create test_dir2 fails"
24115         cleanup_300n
24116 }
24117 run_test 300n "non-root user to create dir under striped dir with default EA"
24118
24119 test_300o() {
24120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24121         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24122         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24123                 skip "Need MDS version at least 2.7.55"
24124
24125         local numfree1
24126         local numfree2
24127
24128         mkdir -p $DIR/$tdir
24129
24130         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
24131         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
24132         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
24133                 skip "not enough free inodes $numfree1 $numfree2"
24134         fi
24135
24136         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
24137         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
24138         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
24139                 skip "not enough free space $numfree1 $numfree2"
24140         fi
24141
24142         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
24143                 error "setdirstripe fails"
24144
24145         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
24146                 error "create dirs fails"
24147
24148         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
24149         ls $DIR/$tdir/striped_dir > /dev/null ||
24150                 error "ls striped dir fails"
24151         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
24152                 error "unlink big striped dir fails"
24153 }
24154 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
24155
24156 test_300p() {
24157         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24158         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24159         remote_mds_nodsh && skip "remote MDS with nodsh"
24160
24161         mkdir_on_mdt0 $DIR/$tdir
24162
24163         #define OBD_FAIL_OUT_ENOSPC     0x1704
24164         do_facet mds2 lctl set_param fail_loc=0x80001704
24165         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
24166                  && error "create striped directory should fail"
24167
24168         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
24169
24170         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
24171         true
24172 }
24173 run_test 300p "create striped directory without space"
24174
24175 test_300q() {
24176         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24177         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24178
24179         local fd=$(free_fd)
24180         local cmd="exec $fd<$tdir"
24181         cd $DIR
24182         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
24183         eval $cmd
24184         cmd="exec $fd<&-"
24185         trap "eval $cmd" EXIT
24186         cd $tdir || error "cd $tdir fails"
24187         rmdir  ../$tdir || error "rmdir $tdir fails"
24188         mkdir local_dir && error "create dir succeeds"
24189         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
24190         eval $cmd
24191         return 0
24192 }
24193 run_test 300q "create remote directory under orphan directory"
24194
24195 test_300r() {
24196         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24197                 skip "Need MDS version at least 2.7.55" && return
24198         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
24199
24200         mkdir $DIR/$tdir
24201
24202         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
24203                 error "set striped dir error"
24204
24205         $LFS getdirstripe $DIR/$tdir/striped_dir ||
24206                 error "getstripeddir fails"
24207
24208         local stripe_count
24209         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
24210                       awk '/lmv_stripe_count:/ { print $2 }')
24211
24212         [ $MDSCOUNT -ne $stripe_count ] &&
24213                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
24214
24215         rm -rf $DIR/$tdir/striped_dir ||
24216                 error "unlink striped dir fails"
24217 }
24218 run_test 300r "test -1 striped directory"
24219
24220 test_300s_helper() {
24221         local count=$1
24222
24223         local stripe_dir=$DIR/$tdir/striped_dir.$count
24224
24225         $LFS mkdir -c $count $stripe_dir ||
24226                 error "lfs mkdir -c error"
24227
24228         $LFS getdirstripe $stripe_dir ||
24229                 error "lfs getdirstripe fails"
24230
24231         local stripe_count
24232         stripe_count=$($LFS getdirstripe $stripe_dir |
24233                       awk '/lmv_stripe_count:/ { print $2 }')
24234
24235         [ $count -ne $stripe_count ] &&
24236                 error_noexit "bad stripe count $stripe_count expected $count"
24237
24238         local dupe_stripes
24239         dupe_stripes=$($LFS getdirstripe $stripe_dir |
24240                 awk '/0x/ {count[$1] += 1}; END {
24241                         for (idx in count) {
24242                                 if (count[idx]>1) {
24243                                         print "index " idx " count " count[idx]
24244                                 }
24245                         }
24246                 }')
24247
24248         if [[ -n "$dupe_stripes" ]] ; then
24249                 lfs getdirstripe $stripe_dir
24250                 error_noexit "Dupe MDT above: $dupe_stripes "
24251         fi
24252
24253         rm -rf $stripe_dir ||
24254                 error_noexit "unlink $stripe_dir fails"
24255 }
24256
24257 test_300s() {
24258         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24259                 skip "Need MDS version at least 2.7.55" && return
24260         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
24261
24262         mkdir $DIR/$tdir
24263         for count in $(seq 2 $MDSCOUNT); do
24264                 test_300s_helper $count
24265         done
24266 }
24267 run_test 300s "test lfs mkdir -c without -i"
24268
24269 test_300t() {
24270         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
24271                 skip "need MDS 2.14.55 or later"
24272         (( $MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
24273
24274         local testdir="$DIR/$tdir/striped_dir"
24275         local dir1=$testdir/dir1
24276         local dir2=$testdir/dir2
24277
24278         mkdir -p $testdir
24279
24280         $LFS setdirstripe -D -c -1 --max-inherit=3 $testdir ||
24281                 error "failed to set default stripe count for $testdir"
24282
24283         mkdir $dir1
24284         local stripe_count=$($LFS getdirstripe -c $dir1)
24285
24286         (( $stripe_count == $MDSCOUNT )) || error "wrong stripe count"
24287
24288         local max_count=$((MDSCOUNT - 1))
24289         local mdts=$(comma_list $(mdts_nodes))
24290
24291         do_nodes $mdts $LCTL set_param lod.*.max_mdt_stripecount=$max_count
24292         stack_trap "do_nodes $mdts $LCTL set_param lod.*.max_mdt_stripecount=0"
24293
24294         mkdir $dir2
24295         stripe_count=$($LFS getdirstripe -c $dir2)
24296
24297         (( $stripe_count == $max_count )) || error "wrong stripe count"
24298 }
24299 run_test 300t "test max_mdt_stripecount"
24300
24301 prepare_remote_file() {
24302         mkdir $DIR/$tdir/src_dir ||
24303                 error "create remote source failed"
24304
24305         cp /etc/hosts $DIR/$tdir/src_dir/a ||
24306                  error "cp to remote source failed"
24307         touch $DIR/$tdir/src_dir/a
24308
24309         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
24310                 error "create remote target dir failed"
24311
24312         touch $DIR/$tdir/tgt_dir/b
24313
24314         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
24315                 error "rename dir cross MDT failed!"
24316
24317         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
24318                 error "src_child still exists after rename"
24319
24320         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
24321                 error "missing file(a) after rename"
24322
24323         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
24324                 error "diff after rename"
24325 }
24326
24327 test_310a() {
24328         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
24329         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24330
24331         local remote_file=$DIR/$tdir/tgt_dir/b
24332
24333         mkdir -p $DIR/$tdir
24334
24335         prepare_remote_file || error "prepare remote file failed"
24336
24337         #open-unlink file
24338         $OPENUNLINK $remote_file $remote_file ||
24339                 error "openunlink $remote_file failed"
24340         $CHECKSTAT -a $remote_file || error "$remote_file exists"
24341 }
24342 run_test 310a "open unlink remote file"
24343
24344 test_310b() {
24345         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
24346         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24347
24348         local remote_file=$DIR/$tdir/tgt_dir/b
24349
24350         mkdir -p $DIR/$tdir
24351
24352         prepare_remote_file || error "prepare remote file failed"
24353
24354         ln $remote_file $DIR/$tfile || error "link failed for remote file"
24355         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
24356         $CHECKSTAT -t file $remote_file || error "check file failed"
24357 }
24358 run_test 310b "unlink remote file with multiple links while open"
24359
24360 test_310c() {
24361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24362         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
24363
24364         local remote_file=$DIR/$tdir/tgt_dir/b
24365
24366         mkdir -p $DIR/$tdir
24367
24368         prepare_remote_file || error "prepare remote file failed"
24369
24370         ln $remote_file $DIR/$tfile || error "link failed for remote file"
24371         multiop_bg_pause $remote_file O_uc ||
24372                         error "mulitop failed for remote file"
24373         MULTIPID=$!
24374         $MULTIOP $DIR/$tfile Ouc
24375         kill -USR1 $MULTIPID
24376         wait $MULTIPID
24377 }
24378 run_test 310c "open-unlink remote file with multiple links"
24379
24380 #LU-4825
24381 test_311() {
24382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24383         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
24384         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
24385                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
24386         remote_mds_nodsh && skip "remote MDS with nodsh"
24387
24388         local old_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
24389         local mdts=$(comma_list $(mdts_nodes))
24390
24391         mkdir -p $DIR/$tdir
24392         $LFS setstripe -i 0 -c 1 $DIR/$tdir
24393         createmany -o $DIR/$tdir/$tfile. 1000
24394
24395         # statfs data is not real time, let's just calculate it
24396         old_iused=$((old_iused + 1000))
24397
24398         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
24399                         osp.*OST0000*MDT0000.create_count")
24400         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
24401                                 osp.*OST0000*MDT0000.max_create_count")
24402         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
24403
24404         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
24405         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
24406         [ $index -ne 0 ] || error "$tfile stripe index is 0"
24407
24408         unlinkmany $DIR/$tdir/$tfile. 1000
24409
24410         do_nodes $mdts "$LCTL set_param -n \
24411                         osp.*OST0000*.max_create_count=$max_count"
24412         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
24413                 do_nodes $mdts "$LCTL set_param -n \
24414                                 osp.*OST0000*.create_count=$count"
24415         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
24416                         grep "=0" && error "create_count is zero"
24417
24418         local new_iused
24419         for i in $(seq 120); do
24420                 new_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
24421                 # system may be too busy to destroy all objs in time, use
24422                 # a somewhat small value to not fail autotest
24423                 [ $((old_iused - new_iused)) -gt 400 ] && break
24424                 sleep 1
24425         done
24426
24427         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
24428         [ $((old_iused - new_iused)) -gt 400 ] ||
24429                 error "objs not destroyed after unlink"
24430 }
24431 run_test 311 "disable OSP precreate, and unlink should destroy objs"
24432
24433 zfs_oid_to_objid()
24434 {
24435         local ost=$1
24436         local objid=$2
24437
24438         local vdevdir=$(dirname $(facet_vdevice $ost))
24439         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
24440         local zfs_zapid=$(do_facet $ost $cmd |
24441                           grep -w "/O/0/d$((objid%32))" -C 5 |
24442                           awk '/Object/{getline; print $1}')
24443         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
24444                           awk "/$objid = /"'{printf $3}')
24445
24446         echo $zfs_objid
24447 }
24448
24449 zfs_object_blksz() {
24450         local ost=$1
24451         local objid=$2
24452
24453         local vdevdir=$(dirname $(facet_vdevice $ost))
24454         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
24455         local blksz=$(do_facet $ost $cmd $objid |
24456                       awk '/dblk/{getline; printf $4}')
24457
24458         case "${blksz: -1}" in
24459                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
24460                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
24461                 *) ;;
24462         esac
24463
24464         echo $blksz
24465 }
24466
24467 test_312() { # LU-4856
24468         remote_ost_nodsh && skip "remote OST with nodsh"
24469         [ "$ost1_FSTYPE" = "zfs" ] ||
24470                 skip_env "the test only applies to zfs"
24471
24472         local max_blksz=$(do_facet ost1 \
24473                           $ZFS get -p recordsize $(facet_device ost1) |
24474                           awk '!/VALUE/{print $3}')
24475
24476         # to make life a little bit easier
24477         $LFS mkdir -c 1 -i 0 $DIR/$tdir
24478         $LFS setstripe -c 1 -i 0 $DIR/$tdir
24479
24480         local tf=$DIR/$tdir/$tfile
24481         touch $tf
24482         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
24483
24484         # Get ZFS object id
24485         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
24486         # block size change by sequential overwrite
24487         local bs
24488
24489         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
24490                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
24491
24492                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
24493                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
24494         done
24495         rm -f $tf
24496
24497         # block size change by sequential append write
24498         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
24499         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
24500         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
24501         local count
24502
24503         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
24504                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
24505                         oflag=sync conv=notrunc
24506
24507                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
24508                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
24509                         error "blksz error, actual $blksz, " \
24510                                 "expected: 2 * $count * $PAGE_SIZE"
24511         done
24512         rm -f $tf
24513
24514         # random write
24515         touch $tf
24516         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
24517         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
24518
24519         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
24520         blksz=$(zfs_object_blksz ost1 $zfs_objid)
24521         [ $blksz -eq $PAGE_SIZE ] ||
24522                 error "blksz error: $blksz, expected: $PAGE_SIZE"
24523
24524         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
24525         blksz=$(zfs_object_blksz ost1 $zfs_objid)
24526         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
24527
24528         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
24529         blksz=$(zfs_object_blksz ost1 $zfs_objid)
24530         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
24531 }
24532 run_test 312 "make sure ZFS adjusts its block size by write pattern"
24533
24534 test_313() {
24535         remote_ost_nodsh && skip "remote OST with nodsh"
24536
24537         local file=$DIR/$tfile
24538
24539         rm -f $file
24540         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
24541
24542         # define OBD_FAIL_TGT_RCVD_EIO           0x720
24543         do_facet ost1 "$LCTL set_param fail_loc=0x720"
24544         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
24545                 error "write should failed"
24546         do_facet ost1 "$LCTL set_param fail_loc=0"
24547         rm -f $file
24548 }
24549 run_test 313 "io should fail after last_rcvd update fail"
24550
24551 test_314() {
24552         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
24553
24554         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
24555         do_facet ost1 "$LCTL set_param fail_loc=0x720"
24556         rm -f $DIR/$tfile
24557         wait_delete_completed
24558         do_facet ost1 "$LCTL set_param fail_loc=0"
24559 }
24560 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
24561
24562 test_315() { # LU-618
24563         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
24564
24565         local file=$DIR/$tfile
24566         rm -f $file
24567
24568         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
24569                 error "multiop file write failed"
24570         $MULTIOP $file oO_RDONLY:r4063232_c &
24571         PID=$!
24572
24573         sleep 2
24574
24575         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
24576         kill -USR1 $PID
24577
24578         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
24579         rm -f $file
24580 }
24581 run_test 315 "read should be accounted"
24582
24583 test_316() {
24584         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
24585         large_xattr_enabled || skip "ea_inode feature disabled"
24586
24587         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
24588         mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
24589         chown nobody $DIR/$tdir/d || error "chown $tdir/d failed"
24590         touch $DIR/$tdir/d/$tfile || error "touch $tdir/d/$tfile failed"
24591
24592         $LFS migrate -m1 $DIR/$tdir/d || error "lfs migrate -m1 failed"
24593 }
24594 run_test 316 "lfs migrate of file with large_xattr enabled"
24595
24596 test_317() {
24597         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
24598                 skip "Need MDS version at least 2.11.53"
24599         if [ "$ost1_FSTYPE" == "zfs" ]; then
24600                 skip "LU-10370: no implementation for ZFS"
24601         fi
24602
24603         local trunc_sz
24604         local grant_blk_size
24605
24606         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
24607                         awk '/grant_block_size:/ { print $2; exit; }')
24608         #
24609         # Create File of size 5M. Truncate it to below size's and verify
24610         # blocks count.
24611         #
24612         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
24613                 error "Create file $DIR/$tfile failed"
24614         stack_trap "rm -f $DIR/$tfile" EXIT
24615
24616         for trunc_sz in 2097152 4097 4000 509 0; do
24617                 $TRUNCATE $DIR/$tfile $trunc_sz ||
24618                         error "truncate $tfile to $trunc_sz failed"
24619                 local sz=$(stat --format=%s $DIR/$tfile)
24620                 local blk=$(stat --format=%b $DIR/$tfile)
24621                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
24622                                      grant_blk_size) * 8))
24623
24624                 if [[ $blk -ne $trunc_blk ]]; then
24625                         $(which stat) $DIR/$tfile
24626                         error "Expected Block $trunc_blk got $blk for $tfile"
24627                 fi
24628
24629                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
24630                         error "Expected Size $trunc_sz got $sz for $tfile"
24631         done
24632
24633         #
24634         # sparse file test
24635         # Create file with a hole and write actual 65536 bytes which aligned
24636         # with 4K and 64K PAGE_SIZE. Block count must be 128.
24637         #
24638         local bs=65536
24639         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 seek=5 conv=fsync ||
24640                 error "Create file : $DIR/$tfile"
24641
24642         #
24643         # Truncate to size $trunc_sz bytes. Strip tail blocks and leave only 8
24644         # blocks. The block count must drop to 8.
24645         #
24646         trunc_sz=$(($(stat --format=%s $DIR/$tfile) -
24647                 ((bs - grant_blk_size) + 1)))
24648         $TRUNCATE $DIR/$tfile $trunc_sz ||
24649                 error "truncate $tfile to $trunc_sz failed"
24650
24651         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
24652         sz=$(stat --format=%s $DIR/$tfile)
24653         blk=$(stat --format=%b $DIR/$tfile)
24654
24655         if [[ $blk -ne $trunc_bsz ]]; then
24656                 $(which stat) $DIR/$tfile
24657                 error "Expected Block $trunc_bsz got $blk for $tfile"
24658         fi
24659
24660         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
24661                 error "Expected Size $trunc_sz got $sz for $tfile"
24662 }
24663 run_test 317 "Verify blocks get correctly update after truncate"
24664
24665 test_318() {
24666         local llite_name="llite.$($LFS getname $MOUNT | awk '{print $1}')"
24667         local old_max_active=$($LCTL get_param -n \
24668                             ${llite_name}.max_read_ahead_async_active \
24669                             2>/dev/null)
24670
24671         $LCTL set_param llite.*.max_read_ahead_async_active=256
24672         local max_active=$($LCTL get_param -n \
24673                            ${llite_name}.max_read_ahead_async_active \
24674                            2>/dev/null)
24675         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
24676
24677         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
24678                 error "set max_read_ahead_async_active should succeed"
24679
24680         $LCTL set_param llite.*.max_read_ahead_async_active=512
24681         max_active=$($LCTL get_param -n \
24682                      ${llite_name}.max_read_ahead_async_active 2>/dev/null)
24683         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
24684
24685         # restore @max_active
24686         [ $old_max_active -ne 0 ] && $LCTL set_param \
24687                 llite.*.max_read_ahead_async_active=$old_max_active
24688
24689         local old_threshold=$($LCTL get_param -n \
24690                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
24691         local max_per_file_mb=$($LCTL get_param -n \
24692                 ${llite_name}.max_read_ahead_per_file_mb 2>/dev/null)
24693
24694         local invalid=$(($max_per_file_mb + 1))
24695         $LCTL set_param \
24696                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
24697                         && error "set $invalid should fail"
24698
24699         local valid=$(($invalid - 1))
24700         $LCTL set_param \
24701                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
24702                         error "set $valid should succeed"
24703         local threshold=$($LCTL get_param -n \
24704                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
24705         [ $threshold -eq $valid ] || error \
24706                 "expect threshold $valid got $threshold"
24707         $LCTL set_param \
24708                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
24709 }
24710 run_test 318 "Verify async readahead tunables"
24711
24712 test_319() {
24713         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
24714
24715         local before=$(date +%s)
24716         local evict
24717         local mdir=$DIR/$tdir
24718         local file=$mdir/xxx
24719
24720         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
24721         touch $file
24722
24723 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
24724         $LCTL set_param fail_val=5 fail_loc=0x8000032c
24725         $LFS migrate -m1 $mdir &
24726
24727         sleep 1
24728         dd if=$file of=/dev/null
24729         wait
24730         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
24731           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
24732
24733         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
24734 }
24735 run_test 319 "lost lease lock on migrate error"
24736
24737 test_398a() { # LU-4198
24738         local ost1_imp=$(get_osc_import_name client ost1)
24739         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
24740                          cut -d'.' -f2)
24741
24742         $LFS setstripe -c 1 -i 0 $DIR/$tfile
24743         $LCTL set_param ldlm.namespaces.*.lru_size=clear
24744
24745         # Disabled: DIO does not push out buffered I/O pages, see LU-12587
24746         # request a new lock on client
24747         #dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
24748
24749         #dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
24750         #local lock_count=$($LCTL get_param -n \
24751         #                  ldlm.namespaces.$imp_name.lru_size)
24752         #[[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
24753
24754         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
24755
24756         # no lock cached, should use lockless DIO and not enqueue new lock
24757         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct \
24758                 conv=notrunc ||
24759                 error "dio write failed"
24760         lock_count=$($LCTL get_param -n \
24761                      ldlm.namespaces.$imp_name.lru_size)
24762         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
24763
24764         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
24765
24766         # no lock cached, should use locked DIO append
24767         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct oflag=append \
24768                 conv=notrunc || error "DIO append failed"
24769         lock_count=$($LCTL get_param -n \
24770                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
24771         [[ $lock_count -ne 0 ]] || error "lock still must be held by DIO append"
24772 }
24773 run_test 398a "direct IO should cancel lock otherwise lockless"
24774
24775 test_398b() { # LU-4198
24776         which fio || skip_env "no fio installed"
24777         $LFS setstripe -c -1 -S 1M $DIR/$tfile
24778
24779         local size=48
24780         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
24781
24782         local njobs=4
24783         # Single page, multiple pages, stripe size, 4*stripe size
24784         for bsize in $(( $PAGE_SIZE )) $(( 4*$PAGE_SIZE )) 1048576 4194304; do
24785                 echo "mix direct rw ${bsize} by fio with $njobs jobs..."
24786                 fio --name=rand-rw --rw=randrw --bs=$bsize --direct=1 \
24787                         --numjobs=$njobs --fallocate=none \
24788                         --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24789                         --filename=$DIR/$tfile &
24790                 bg_pid=$!
24791
24792                 echo "mix buffer rw ${bsize} by fio with $njobs jobs..."
24793                 fio --name=rand-rw --rw=randrw --bs=$bsize \
24794                         --numjobs=$njobs --fallocate=none \
24795                         --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24796                         --filename=$DIR/$tfile || true
24797                 wait $bg_pid
24798         done
24799
24800         evict=$(do_facet client $LCTL get_param \
24801                 osc.$FSNAME-OST*-osc-*/state |
24802             awk -F"[ [,]" '/EVICTED ]$/ { if (t<$5) {t=$5;} } END { print t }')
24803
24804         [ -z "$evict" ] || [[ $evict -le $before ]] ||
24805                 (do_facet client $LCTL get_param \
24806                         osc.$FSNAME-OST*-osc-*/state;
24807                     error "eviction happened: $evict before:$before")
24808
24809         rm -f $DIR/$tfile
24810 }
24811 run_test 398b "DIO and buffer IO race"
24812
24813 test_398c() { # LU-4198
24814         local ost1_imp=$(get_osc_import_name client ost1)
24815         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
24816                          cut -d'.' -f2)
24817
24818         which fio || skip_env "no fio installed"
24819
24820         saved_debug=$($LCTL get_param -n debug)
24821         $LCTL set_param debug=0
24822
24823         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
24824         ((size /= 1024)) # by megabytes
24825         ((size /= 2)) # write half of the OST at most
24826         [ $size -gt 40 ] && size=40 #reduce test time anyway
24827
24828         $LFS setstripe -c 1 $DIR/$tfile
24829
24830         # it seems like ldiskfs reserves more space than necessary if the
24831         # writing blocks are not mapped, so it extends the file firstly
24832         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
24833         cancel_lru_locks osc
24834
24835         # clear and verify rpc_stats later
24836         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
24837
24838         local njobs=4
24839         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
24840         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
24841                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
24842                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24843                 --filename=$DIR/$tfile
24844         [ $? -eq 0 ] || error "fio write error"
24845
24846         [ $($LCTL get_param -n ldlm.namespaces.$imp_name.lock_count) -eq 0 ] ||
24847                 error "Locks were requested while doing AIO"
24848
24849         # get the percentage of 1-page I/O
24850         pct=$($LCTL get_param osc.${imp_name}.rpc_stats |
24851                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
24852                 awk '{print $7}')
24853         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
24854
24855         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
24856         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
24857                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
24858                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24859                 --filename=$DIR/$tfile
24860         [ $? -eq 0 ] || error "fio mixed read write error"
24861
24862         echo "AIO with large block size ${size}M"
24863         fio --name=rand-rw --rw=randrw --bs=${size}M --direct=1 \
24864                 --numjobs=1 --fallocate=none --ioengine=libaio \
24865                 --iodepth=16 --allow_file_create=0 --size=${size}M \
24866                 --filename=$DIR/$tfile
24867         [ $? -eq 0 ] || error "fio large block size failed"
24868
24869         rm -f $DIR/$tfile
24870         $LCTL set_param debug="$saved_debug"
24871 }
24872 run_test 398c "run fio to test AIO"
24873
24874 test_398d() { #  LU-13846
24875         which aiocp || skip_env "no aiocp installed"
24876         local aio_file=$DIR/$tfile.aio
24877
24878         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
24879
24880         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=64
24881         aiocp -a $PAGE_SIZE -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file
24882         stack_trap "rm -f $DIR/$tfile $aio_file"
24883
24884         diff $DIR/$tfile $aio_file || error "file diff after aiocp"
24885
24886         # make sure we don't crash and fail properly
24887         aiocp -a 512 -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
24888                 error "aio not aligned with PAGE SIZE should fail"
24889
24890         rm -f $DIR/$tfile $aio_file
24891 }
24892 run_test 398d "run aiocp to verify block size > stripe size"
24893
24894 test_398e() {
24895         dd if=/dev/zero of=$DIR/$tfile bs=1234 count=1
24896         touch $DIR/$tfile.new
24897         dd if=$DIR/$tfile of=$DIR/$tfile.new bs=1M count=1 oflag=direct
24898 }
24899 run_test 398e "O_Direct open cleared by fcntl doesn't cause hang"
24900
24901 test_398f() { #  LU-14687
24902         which aiocp || skip_env "no aiocp installed"
24903         local aio_file=$DIR/$tfile.aio
24904
24905         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
24906
24907         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
24908         stack_trap "rm -f $DIR/$tfile $aio_file"
24909
24910         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
24911         $LCTL set_param fail_loc=0x1418
24912         # make sure we don't crash and fail properly
24913         aiocp -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
24914                 error "aio with page allocation failure succeeded"
24915         $LCTL set_param fail_loc=0
24916         diff $DIR/$tfile $aio_file
24917         [[ $? != 0 ]] || error "no diff after failed aiocp"
24918 }
24919 run_test 398f "verify aio handles ll_direct_rw_pages errors correctly"
24920
24921 # NB: To get the parallel DIO behavior in LU-13798, there must be > 1
24922 # stripe and i/o size must be > stripe size
24923 # Old style synchronous DIO waits after submitting each chunk, resulting in a
24924 # single RPC in flight.  This test shows async DIO submission is working by
24925 # showing multiple RPCs in flight.
24926 test_398g() { #  LU-13798
24927         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
24928
24929         # We need to do some i/o first to acquire enough grant to put our RPCs
24930         # in flight; otherwise a new connection may not have enough grant
24931         # available
24932         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24933                 error "parallel dio failed"
24934         stack_trap "rm -f $DIR/$tfile"
24935
24936         # Reduce RPC size to 1M to avoid combination in to larger RPCs
24937         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
24938         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
24939         stack_trap "$LCTL set_param -n $pages_per_rpc"
24940
24941         # Recreate file so it's empty
24942         rm -f $DIR/$tfile
24943         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
24944         #Pause rpc completion to guarantee we see multiple rpcs in flight
24945         #define OBD_FAIL_OST_BRW_PAUSE_BULK
24946         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
24947         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
24948
24949         # Clear rpc stats
24950         $LCTL set_param osc.*.rpc_stats=c
24951
24952         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24953                 error "parallel dio failed"
24954         stack_trap "rm -f $DIR/$tfile"
24955
24956         $LCTL get_param osc.*-OST0000-*.rpc_stats
24957         pct=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
24958                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
24959                 grep "8:" | awk '{print $8}')
24960         # We look at the "8 rpcs in flight" field, and verify A) it is present
24961         # and B) it includes all RPCs.  This proves we had 8 RPCs in flight,
24962         # as expected for an 8M DIO to a file with 1M stripes.
24963         [ $pct -eq 100 ] || error "we should see 8 RPCs in flight"
24964
24965         # Verify turning off parallel dio works as expected
24966         # Clear rpc stats
24967         $LCTL set_param osc.*.rpc_stats=c
24968         $LCTL set_param llite.*.parallel_dio=0
24969         stack_trap '$LCTL set_param llite.*.parallel_dio=1'
24970
24971         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
24972                 error "dio with parallel dio disabled failed"
24973
24974         # Ideally, we would see only one RPC in flight here, but there is an
24975         # unavoidable race between i/o completion and RPC in flight counting,
24976         # so while only 1 i/o is in flight at a time, the RPC in flight counter
24977         # will sometimes exceed 1 (3 or 4 is not rare on VM testing).
24978         # So instead we just verify it's always < 8.
24979         $LCTL get_param osc.*-OST0000-*.rpc_stats
24980         ret=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
24981                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
24982                 grep '^$' -B1 | grep . | awk '{print $1}')
24983         [ $ret != "8:" ] ||
24984                 error "we should see fewer than 8 RPCs in flight (saw $ret)"
24985 }
24986 run_test 398g "verify parallel dio async RPC submission"
24987
24988 test_398h() { #  LU-13798
24989         local dio_file=$DIR/$tfile.dio
24990
24991         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
24992
24993         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
24994         stack_trap "rm -f $DIR/$tfile $dio_file"
24995
24996         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct ||
24997                 error "parallel dio failed"
24998         diff $DIR/$tfile $dio_file
24999         [[ $? == 0 ]] || error "file diff after aiocp"
25000 }
25001 run_test 398h "verify correctness of read & write with i/o size >> stripe size"
25002
25003 test_398i() { #  LU-13798
25004         local dio_file=$DIR/$tfile.dio
25005
25006         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
25007
25008         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25009         stack_trap "rm -f $DIR/$tfile $dio_file"
25010
25011         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
25012         $LCTL set_param fail_loc=0x1418
25013         # make sure we don't crash and fail properly
25014         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct &&
25015                 error "parallel dio page allocation failure succeeded"
25016         diff $DIR/$tfile $dio_file
25017         [[ $? != 0 ]] || error "no diff after failed aiocp"
25018 }
25019 run_test 398i "verify parallel dio handles ll_direct_rw_pages errors correctly"
25020
25021 test_398j() { #  LU-13798
25022         # Stripe size > RPC size but less than i/o size tests split across
25023         # stripes and RPCs for individual i/o op
25024         $LFS setstripe -o 0,0 -S 4M $DIR/$tfile $DIR/$tfile.2
25025
25026         # Reduce RPC size to 1M to guarantee split to multiple RPCs per stripe
25027         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
25028         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
25029         stack_trap "$LCTL set_param -n $pages_per_rpc"
25030
25031         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25032                 error "parallel dio write failed"
25033         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.2"
25034
25035         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct ||
25036                 error "parallel dio read failed"
25037         diff $DIR/$tfile $DIR/$tfile.2
25038         [[ $? == 0 ]] || error "file diff after parallel dio read"
25039 }
25040 run_test 398j "test parallel dio where stripe size > rpc_size"
25041
25042 test_398k() { #  LU-13798
25043         wait_delete_completed
25044         wait_mds_ost_sync
25045
25046         # 4 stripe file; we will cause out of space on OST0
25047         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
25048
25049         # Fill OST0 (if it's not too large)
25050         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
25051                    head -n1)
25052         if [[ $ORIGFREE -gt $MAXFREE ]]; then
25053                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
25054         fi
25055         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
25056         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
25057                 error "dd should fill OST0"
25058         stack_trap "rm -f $DIR/$tfile.1"
25059
25060         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25061         err=$?
25062
25063         ls -la $DIR/$tfile
25064         $CHECKSTAT -t file -s 0 $DIR/$tfile ||
25065                 error "file is not 0 bytes in size"
25066
25067         # dd above should not succeed, but don't error until here so we can
25068         # get debug info above
25069         [[ $err != 0 ]] ||
25070                 error "parallel dio write with enospc succeeded"
25071         stack_trap "rm -f $DIR/$tfile"
25072 }
25073 run_test 398k "test enospc on first stripe"
25074
25075 test_398l() { #  LU-13798
25076         wait_delete_completed
25077         wait_mds_ost_sync
25078
25079         # 4 stripe file; we will cause out of space on OST0
25080         # Note the 1M stripe size and the > 1M i/o size mean this ENOSPC
25081         # happens on the second i/o chunk we issue
25082         $LFS setstripe -o 1,0,1,0 -S 1M $DIR/$tfile $DIR/$tfile.2
25083
25084         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=2 oflag=direct
25085         stack_trap "rm -f $DIR/$tfile"
25086
25087         # Fill OST0 (if it's not too large)
25088         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
25089                    head -n1)
25090         if [[ $ORIGFREE -gt $MAXFREE ]]; then
25091                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
25092         fi
25093         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
25094         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
25095                 error "dd should fill OST0"
25096         stack_trap "rm -f $DIR/$tfile.1"
25097
25098         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 oflag=direct
25099         err=$?
25100         stack_trap "rm -f $DIR/$tfile.2"
25101
25102         # Check that short write completed as expected
25103         ls -la $DIR/$tfile.2
25104         $CHECKSTAT -t file -s 1048576 $DIR/$tfile.2 ||
25105                 error "file is not 1M in size"
25106
25107         # dd above should not succeed, but don't error until here so we can
25108         # get debug info above
25109         [[ $err != 0 ]] ||
25110                 error "parallel dio write with enospc succeeded"
25111
25112         # Truncate source file to same length as output file and diff them
25113         $TRUNCATE $DIR/$tfile 1048576
25114         diff $DIR/$tfile $DIR/$tfile.2
25115         [[ $? == 0 ]] || error "data incorrect after short write"
25116 }
25117 run_test 398l "test enospc on intermediate stripe/RPC"
25118
25119 test_398m() { #  LU-13798
25120         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
25121
25122         # Set up failure on OST0, the first stripe:
25123         #define OBD_FAIL_OST_BRW_WRITE_BULK     0x20e
25124         #NB: Fail val is ost # + 1, because we cannot use cfs_fail_val = 0
25125         # So this fail_val specifies OST0
25126         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=1
25127         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25128
25129         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
25130                 error "parallel dio write with failure on first stripe succeeded"
25131         stack_trap "rm -f $DIR/$tfile"
25132         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25133
25134         # Place data in file for read
25135         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25136                 error "parallel dio write failed"
25137
25138         # Fail read on OST0, first stripe
25139         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
25140         do_facet ost1 $LCTL set_param fail_loc=0x20f fail_val=1
25141         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
25142                 error "parallel dio read with error on first stripe succeeded"
25143         rm -f $DIR/$tfile.2
25144         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25145
25146         # Switch to testing on OST1, second stripe
25147         # Clear file contents, maintain striping
25148         echo > $DIR/$tfile
25149         # Set up failure on OST1, second stripe:
25150         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=2
25151         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25152
25153         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
25154                 error "parallel dio write with failure on first stripe succeeded"
25155         stack_trap "rm -f $DIR/$tfile"
25156         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25157
25158         # Place data in file for read
25159         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25160                 error "parallel dio write failed"
25161
25162         # Fail read on OST1, second stripe
25163         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
25164         do_facet ost2 $LCTL set_param fail_loc=0x20f fail_val=2
25165         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
25166                 error "parallel dio read with error on first stripe succeeded"
25167         rm -f $DIR/$tfile.2
25168         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
25169 }
25170 run_test 398m "test RPC failures with parallel dio"
25171
25172 # Parallel submission of DIO should not cause problems for append, but it's
25173 # important to verify.
25174 test_398n() { #  LU-13798
25175         $LFS setstripe -C 2 -S 1M $DIR/$tfile
25176
25177         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 ||
25178                 error "dd to create source file failed"
25179         stack_trap "rm -f $DIR/$tfile"
25180
25181         dd if=$DIR/$tfile of=$DIR/$tfile.1 bs=8M count=8 oflag=direct oflag=append ||
25182                 error "parallel dio write with failure on second stripe succeeded"
25183         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.1"
25184         diff $DIR/$tfile $DIR/$tfile.1
25185         [[ $? == 0 ]] || error "data incorrect after append"
25186
25187 }
25188 run_test 398n "test append with parallel DIO"
25189
25190 test_398o() {
25191         directio rdwr $DIR/$tfile 0 1 1 || error "bad KMS"
25192 }
25193 run_test 398o "right kms with DIO"
25194
25195 test_fake_rw() {
25196         local read_write=$1
25197         if [ "$read_write" = "write" ]; then
25198                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
25199         elif [ "$read_write" = "read" ]; then
25200                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
25201         else
25202                 error "argument error"
25203         fi
25204
25205         # turn off debug for performance testing
25206         local saved_debug=$($LCTL get_param -n debug)
25207         $LCTL set_param debug=0
25208
25209         $LFS setstripe -c 1 -i 0 $DIR/$tfile
25210
25211         # get ost1 size - $FSNAME-OST0000
25212         local ost1_avail_size=$($LFS df $DIR | awk /${ost1_svc}/'{ print $4 }')
25213         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
25214         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
25215
25216         if [ "$read_write" = "read" ]; then
25217                 $TRUNCATE $DIR/$tfile $(expr 1048576 \* $blocks)
25218         fi
25219
25220         local start_time=$(date +%s.%N)
25221         $dd_cmd bs=1M count=$blocks oflag=sync ||
25222                 error "real dd $read_write error"
25223         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
25224
25225         if [ "$read_write" = "write" ]; then
25226                 rm -f $DIR/$tfile
25227         fi
25228
25229         # define OBD_FAIL_OST_FAKE_RW           0x238
25230         do_facet ost1 $LCTL set_param fail_loc=0x238
25231
25232         local start_time=$(date +%s.%N)
25233         $dd_cmd bs=1M count=$blocks oflag=sync ||
25234                 error "fake dd $read_write error"
25235         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
25236
25237         if [ "$read_write" = "write" ]; then
25238                 # verify file size
25239                 cancel_lru_locks osc
25240                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
25241                         error "$tfile size not $blocks MB"
25242         fi
25243         do_facet ost1 $LCTL set_param fail_loc=0
25244
25245         echo "fake $read_write $duration_fake vs. normal $read_write" \
25246                 "$duration in seconds"
25247         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
25248                 error_not_in_vm "fake write is slower"
25249
25250         $LCTL set_param -n debug="$saved_debug"
25251         rm -f $DIR/$tfile
25252 }
25253 test_399a() { # LU-7655 for OST fake write
25254         remote_ost_nodsh && skip "remote OST with nodsh"
25255
25256         test_fake_rw write
25257 }
25258 run_test 399a "fake write should not be slower than normal write"
25259
25260 test_399b() { # LU-8726 for OST fake read
25261         remote_ost_nodsh && skip "remote OST with nodsh"
25262         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
25263                 skip_env "ldiskfs only test"
25264         fi
25265
25266         test_fake_rw read
25267 }
25268 run_test 399b "fake read should not be slower than normal read"
25269
25270 test_400a() { # LU-1606, was conf-sanity test_74
25271         if ! which $CC > /dev/null 2>&1; then
25272                 skip_env "$CC is not installed"
25273         fi
25274
25275         local extra_flags=''
25276         local out=$TMP/$tfile
25277         local prefix=/usr/include/lustre
25278         local prog
25279
25280         # Oleg removes c files in his test rig so test if any c files exist
25281         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
25282                 skip_env "Needed c test files are missing"
25283
25284         if ! [[ -d $prefix ]]; then
25285                 # Assume we're running in tree and fixup the include path.
25286                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
25287                 extra_flags+=" -L$LUSTRE/utils/.lib"
25288         fi
25289
25290         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
25291                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
25292                         error "client api broken"
25293         done
25294         rm -f $out
25295 }
25296 run_test 400a "Lustre client api program can compile and link"
25297
25298 test_400b() { # LU-1606, LU-5011
25299         local header
25300         local out=$TMP/$tfile
25301         local prefix=/usr/include/linux/lustre
25302
25303         # We use a hard coded prefix so that this test will not fail
25304         # when run in tree. There are headers in lustre/include/lustre/
25305         # that are not packaged (like lustre_idl.h) and have more
25306         # complicated include dependencies (like config.h and lnet/types.h).
25307         # Since this test about correct packaging we just skip them when
25308         # they don't exist (see below) rather than try to fixup cppflags.
25309
25310         if ! which $CC > /dev/null 2>&1; then
25311                 skip_env "$CC is not installed"
25312         fi
25313
25314         for header in $prefix/*.h; do
25315                 if ! [[ -f "$header" ]]; then
25316                         continue
25317                 fi
25318
25319                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
25320                         continue # lustre_ioctl.h is internal header
25321                 fi
25322
25323                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
25324                         error "cannot compile '$header'"
25325         done
25326         rm -f $out
25327 }
25328 run_test 400b "packaged headers can be compiled"
25329
25330 test_401a() { #LU-7437
25331         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
25332         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
25333
25334         #count the number of parameters by "list_param -R"
25335         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
25336         #count the number of parameters by listing proc files
25337         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
25338         echo "proc_dirs='$proc_dirs'"
25339         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
25340         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
25341                       sort -u | wc -l)
25342
25343         [ $params -eq $procs ] ||
25344                 error "found $params parameters vs. $procs proc files"
25345
25346         # test the list_param -D option only returns directories
25347         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
25348         #count the number of parameters by listing proc directories
25349         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
25350                 sort -u | wc -l)
25351
25352         [ $params -eq $procs ] ||
25353                 error "found $params parameters vs. $procs proc files"
25354 }
25355 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
25356
25357 test_401b() {
25358         # jobid_var may not allow arbitrary values, so use jobid_name
25359         # if available
25360         if $LCTL list_param jobid_name > /dev/null 2>&1; then
25361                 local testname=jobid_name tmp='testing%p'
25362         else
25363                 local testname=jobid_var tmp=testing
25364         fi
25365
25366         local save=$($LCTL get_param -n $testname)
25367
25368         $LCTL set_param foo=bar $testname=$tmp bar=baz &&
25369                 error "no error returned when setting bad parameters"
25370
25371         local jobid_new=$($LCTL get_param -n foe $testname baz)
25372         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
25373
25374         $LCTL set_param -n fog=bam $testname=$save bat=fog
25375         local jobid_old=$($LCTL get_param -n foe $testname bag)
25376         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
25377 }
25378 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
25379
25380 test_401c() {
25381         # jobid_var may not allow arbitrary values, so use jobid_name
25382         # if available
25383         if $LCTL list_param jobid_name > /dev/null 2>&1; then
25384                 local testname=jobid_name
25385         else
25386                 local testname=jobid_var
25387         fi
25388
25389         local jobid_var_old=$($LCTL get_param -n $testname)
25390         local jobid_var_new
25391
25392         $LCTL set_param $testname= &&
25393                 error "no error returned for 'set_param a='"
25394
25395         jobid_var_new=$($LCTL get_param -n $testname)
25396         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
25397                 error "$testname was changed by setting without value"
25398
25399         $LCTL set_param $testname &&
25400                 error "no error returned for 'set_param a'"
25401
25402         jobid_var_new=$($LCTL get_param -n $testname)
25403         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
25404                 error "$testname was changed by setting without value"
25405 }
25406 run_test 401c "Verify 'lctl set_param' without value fails in either format."
25407
25408 test_401d() {
25409         # jobid_var may not allow arbitrary values, so use jobid_name
25410         # if available
25411         if $LCTL list_param jobid_name > /dev/null 2>&1; then
25412                 local testname=jobid_name new_value='foo=bar%p'
25413         else
25414                 local testname=jobid_var new_valuie=foo=bar
25415         fi
25416
25417         local jobid_var_old=$($LCTL get_param -n $testname)
25418         local jobid_var_new
25419
25420         $LCTL set_param $testname=$new_value ||
25421                 error "'set_param a=b' did not accept a value containing '='"
25422
25423         jobid_var_new=$($LCTL get_param -n $testname)
25424         [[ "$jobid_var_new" == "$new_value" ]] ||
25425                 error "'set_param a=b' failed on a value containing '='"
25426
25427         # Reset the $testname to test the other format
25428         $LCTL set_param $testname=$jobid_var_old
25429         jobid_var_new=$($LCTL get_param -n $testname)
25430         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
25431                 error "failed to reset $testname"
25432
25433         $LCTL set_param $testname $new_value ||
25434                 error "'set_param a b' did not accept a value containing '='"
25435
25436         jobid_var_new=$($LCTL get_param -n $testname)
25437         [[ "$jobid_var_new" == "$new_value" ]] ||
25438                 error "'set_param a b' failed on a value containing '='"
25439
25440         $LCTL set_param $testname $jobid_var_old
25441         jobid_var_new=$($LCTL get_param -n $testname)
25442         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
25443                 error "failed to reset $testname"
25444 }
25445 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
25446
25447 test_401e() { # LU-14779
25448         $LCTL list_param -R "ldlm.namespaces.MGC*" ||
25449                 error "lctl list_param MGC* failed"
25450         $LCTL get_param "ldlm.namespaces.MGC*" || error "lctl get_param failed"
25451         $LCTL get_param "ldlm.namespaces.MGC*.lru_size" ||
25452                 error "lctl get_param lru_size failed"
25453 }
25454 run_test 401e "verify 'lctl get_param' works with NID in parameter"
25455
25456 test_402() {
25457         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
25458         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
25459                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
25460         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
25461                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
25462                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
25463         remote_mds_nodsh && skip "remote MDS with nodsh"
25464
25465         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
25466 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
25467         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
25468         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
25469                 echo "Touch failed - OK"
25470 }
25471 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
25472
25473 test_403() {
25474         local file1=$DIR/$tfile.1
25475         local file2=$DIR/$tfile.2
25476         local tfile=$TMP/$tfile
25477
25478         rm -f $file1 $file2 $tfile
25479
25480         touch $file1
25481         ln $file1 $file2
25482
25483         # 30 sec OBD_TIMEOUT in ll_getattr()
25484         # right before populating st_nlink
25485         $LCTL set_param fail_loc=0x80001409
25486         stat -c %h $file1 > $tfile &
25487
25488         # create an alias, drop all locks and reclaim the dentry
25489         < $file2
25490         cancel_lru_locks mdc
25491         cancel_lru_locks osc
25492         sysctl -w vm.drop_caches=2
25493
25494         wait
25495
25496         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
25497
25498         rm -f $tfile $file1 $file2
25499 }
25500 run_test 403 "i_nlink should not drop to zero due to aliasing"
25501
25502 test_404() { # LU-6601
25503         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
25504                 skip "Need server version newer than 2.8.52"
25505         remote_mds_nodsh && skip "remote MDS with nodsh"
25506
25507         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
25508                 awk '/osp .*-osc-MDT/ { print $4}')
25509
25510         local osp
25511         for osp in $mosps; do
25512                 echo "Deactivate: " $osp
25513                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
25514                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
25515                         awk -vp=$osp '$4 == p { print $2 }')
25516                 [ $stat = IN ] || {
25517                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
25518                         error "deactivate error"
25519                 }
25520                 echo "Activate: " $osp
25521                 do_facet $SINGLEMDS $LCTL --device %$osp activate
25522                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
25523                         awk -vp=$osp '$4 == p { print $2 }')
25524                 [ $stat = UP ] || {
25525                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
25526                         error "activate error"
25527                 }
25528         done
25529 }
25530 run_test 404 "validate manual {de}activated works properly for OSPs"
25531
25532 test_405() {
25533         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
25534         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
25535                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
25536                         skip "Layout swap lock is not supported"
25537
25538         check_swap_layouts_support
25539         check_swap_layout_no_dom $DIR
25540
25541         test_mkdir $DIR/$tdir
25542         swap_lock_test -d $DIR/$tdir ||
25543                 error "One layout swap locked test failed"
25544 }
25545 run_test 405 "Various layout swap lock tests"
25546
25547 test_406() {
25548         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
25549         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
25550         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
25551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25552         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
25553                 skip "Need MDS version at least 2.8.50"
25554
25555         local def_stripe_size=$($LFS getstripe -S $MOUNT)
25556         local test_pool=$TESTNAME
25557
25558         pool_add $test_pool || error "pool_add failed"
25559         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
25560                 error "pool_add_targets failed"
25561
25562         save_layout_restore_at_exit $MOUNT
25563
25564         # parent set default stripe count only, child will stripe from both
25565         # parent and fs default
25566         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
25567                 error "setstripe $MOUNT failed"
25568         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
25569         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
25570         for i in $(seq 10); do
25571                 local f=$DIR/$tdir/$tfile.$i
25572                 touch $f || error "touch failed"
25573                 local count=$($LFS getstripe -c $f)
25574                 [ $count -eq $OSTCOUNT ] ||
25575                         error "$f stripe count $count != $OSTCOUNT"
25576                 local offset=$($LFS getstripe -i $f)
25577                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
25578                 local size=$($LFS getstripe -S $f)
25579                 [ $size -eq $((def_stripe_size * 2)) ] ||
25580                         error "$f stripe size $size != $((def_stripe_size * 2))"
25581                 local pool=$($LFS getstripe -p $f)
25582                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
25583         done
25584
25585         # change fs default striping, delete parent default striping, now child
25586         # will stripe from new fs default striping only
25587         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
25588                 error "change $MOUNT default stripe failed"
25589         $LFS setstripe -c 0 $DIR/$tdir ||
25590                 error "delete $tdir default stripe failed"
25591         for i in $(seq 11 20); do
25592                 local f=$DIR/$tdir/$tfile.$i
25593                 touch $f || error "touch $f failed"
25594                 local count=$($LFS getstripe -c $f)
25595                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
25596                 local offset=$($LFS getstripe -i $f)
25597                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
25598                 local size=$($LFS getstripe -S $f)
25599                 [ $size -eq $def_stripe_size ] ||
25600                         error "$f stripe size $size != $def_stripe_size"
25601                 local pool=$($LFS getstripe -p $f)
25602                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
25603         done
25604
25605         unlinkmany $DIR/$tdir/$tfile. 1 20
25606
25607         local f=$DIR/$tdir/$tfile
25608         pool_remove_all_targets $test_pool $f
25609         pool_remove $test_pool $f
25610 }
25611 run_test 406 "DNE support fs default striping"
25612
25613 test_407() {
25614         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
25615         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
25616                 skip "Need MDS version at least 2.8.55"
25617         remote_mds_nodsh && skip "remote MDS with nodsh"
25618
25619         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
25620                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
25621         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
25622                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
25623         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
25624
25625         #define OBD_FAIL_DT_TXN_STOP    0x2019
25626         for idx in $(seq $MDSCOUNT); do
25627                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
25628         done
25629         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
25630         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
25631                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
25632         true
25633 }
25634 run_test 407 "transaction fail should cause operation fail"
25635
25636 test_408() {
25637         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
25638
25639         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
25640         lctl set_param fail_loc=0x8000040a
25641         # let ll_prepare_partial_page() fail
25642         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
25643
25644         rm -f $DIR/$tfile
25645
25646         # create at least 100 unused inodes so that
25647         # shrink_icache_memory(0) should not return 0
25648         touch $DIR/$tfile-{0..100}
25649         rm -f $DIR/$tfile-{0..100}
25650         sync
25651
25652         echo 2 > /proc/sys/vm/drop_caches
25653 }
25654 run_test 408 "drop_caches should not hang due to page leaks"
25655
25656 test_409()
25657 {
25658         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25659
25660         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
25661         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
25662         touch $DIR/$tdir/guard || error "(2) Fail to create"
25663
25664         local PREFIX=$(str_repeat 'A' 128)
25665         echo "Create 1K hard links start at $(date)"
25666         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
25667                 error "(3) Fail to hard link"
25668
25669         echo "Links count should be right although linkEA overflow"
25670         stat $DIR/$tdir/guard || error "(4) Fail to stat"
25671         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
25672         [ $linkcount -eq 1001 ] ||
25673                 error "(5) Unexpected hard links count: $linkcount"
25674
25675         echo "List all links start at $(date)"
25676         ls -l $DIR/$tdir/foo > /dev/null ||
25677                 error "(6) Fail to list $DIR/$tdir/foo"
25678
25679         echo "Unlink hard links start at $(date)"
25680         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
25681                 error "(7) Fail to unlink"
25682         echo "Unlink hard links finished at $(date)"
25683 }
25684 run_test 409 "Large amount of cross-MDTs hard links on the same file"
25685
25686 test_410()
25687 {
25688         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
25689                 skip "Need client version at least 2.9.59"
25690         [ -f $LUSTRE/tests/kernel/kinode.ko ] ||
25691                 skip "Need MODULES build"
25692
25693         # Create a file, and stat it from the kernel
25694         local testfile=$DIR/$tfile
25695         touch $testfile
25696
25697         local run_id=$RANDOM
25698         local my_ino=$(stat --format "%i" $testfile)
25699
25700         # Try to insert the module. This will always fail as the
25701         # module is designed to not be inserted.
25702         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
25703             &> /dev/null
25704
25705         # Anything but success is a test failure
25706         dmesg | grep -q \
25707             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
25708             error "no inode match"
25709 }
25710 run_test 410 "Test inode number returned from kernel thread"
25711
25712 cleanup_test411_cgroup() {
25713         trap 0
25714         rmdir "$1"
25715 }
25716
25717 test_411() {
25718         local cg_basedir=/sys/fs/cgroup/memory
25719         # LU-9966
25720         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
25721                 skip "no setup for cgroup"
25722
25723         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
25724                 error "test file creation failed"
25725         cancel_lru_locks osc
25726
25727         # Create a very small memory cgroup to force a slab allocation error
25728         local cgdir=$cg_basedir/osc_slab_alloc
25729         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
25730         trap "cleanup_test411_cgroup $cgdir" EXIT
25731         echo 2M > $cgdir/memory.kmem.limit_in_bytes
25732         echo 1M > $cgdir/memory.limit_in_bytes
25733
25734         # Should not LBUG, just be killed by oom-killer
25735         # dd will return 0 even allocation failure in some environment.
25736         # So don't check return value
25737         bash -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
25738         cleanup_test411_cgroup $cgdir
25739
25740         return 0
25741 }
25742 run_test 411 "Slab allocation error with cgroup does not LBUG"
25743
25744 test_412() {
25745         (( $MDSCOUNT > 1 )) || skip_env "needs >= 2 MDTs"
25746         (( $MDS1_VERSION >= $(version_code 2.10.55) )) ||
25747                 skip "Need server version at least 2.10.55"
25748
25749         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
25750                 error "mkdir failed"
25751         $LFS getdirstripe $DIR/$tdir
25752         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
25753         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
25754                 error "expect $((MDSCOUT - 1)) get $stripe_index"
25755         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
25756         [ $stripe_count -eq 2 ] ||
25757                 error "expect 2 get $stripe_count"
25758
25759         (( $MDS1_VERSION >= $(version_code 2.14.55) )) || return 0
25760
25761         local index
25762         local index2
25763
25764         # subdirs should be on the same MDT as parent
25765         for i in $(seq 0 $((MDSCOUNT - 1))); do
25766                 $LFS mkdir -i $i $DIR/$tdir/mdt$i || error "mkdir mdt$i failed"
25767                 mkdir $DIR/$tdir/mdt$i/sub || error "mkdir sub failed"
25768                 index=$($LFS getstripe -m $DIR/$tdir/mdt$i/sub)
25769                 (( index == i )) || error "mdt$i/sub on MDT$index"
25770         done
25771
25772         # stripe offset -1, ditto
25773         for i in {1..10}; do
25774                 $LFS mkdir -i -1 $DIR/$tdir/qos$i || error "mkdir qos$i failed"
25775                 index=$($LFS getstripe -m $DIR/$tdir/qos$i)
25776                 mkdir $DIR/$tdir/qos$i/sub || error "mkdir sub failed"
25777                 index2=$($LFS getstripe -m $DIR/$tdir/qos$i/sub)
25778                 (( index == index2 )) ||
25779                         error "qos$i on MDT$index, sub on MDT$index2"
25780         done
25781
25782         local testdir=$DIR/$tdir/inherit
25783
25784         $LFS mkdir -i 1 --max-inherit=3 $testdir || error "mkdir inherit failed"
25785         # inherit 2 levels
25786         for i in 1 2; do
25787                 testdir=$testdir/s$i
25788                 mkdir $testdir || error "mkdir $testdir failed"
25789                 index=$($LFS getstripe -m $testdir)
25790                 (( index == 1 )) ||
25791                         error "$testdir on MDT$index"
25792         done
25793
25794         # not inherit any more
25795         testdir=$testdir/s3
25796         mkdir $testdir || error "mkdir $testdir failed"
25797         getfattr -d -m dmv $testdir | grep dmv &&
25798                 error "default LMV set on $testdir" || true
25799 }
25800 run_test 412 "mkdir on specific MDTs"
25801
25802 TEST413_COUNT=${TEST413_COUNT:-200}
25803 generate_uneven_mdts() {
25804         local threshold=$1
25805         local lmv_qos_maxage
25806         local lod_qos_maxage
25807         local ffree
25808         local bavail
25809         local max
25810         local min
25811         local max_index
25812         local min_index
25813         local tmp
25814         local i
25815
25816         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
25817         $LCTL set_param lmv.*.qos_maxage=1
25818         stack_trap "$LCTL set_param \
25819                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" RETURN
25820         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
25821                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
25822         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
25823                 lod.*.mdt_qos_maxage=1
25824         stack_trap "do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
25825                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" RETURN
25826
25827         echo
25828         echo "Check for uneven MDTs: "
25829
25830         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
25831         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
25832         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
25833
25834         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25835         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25836         max_index=0
25837         min_index=0
25838         for ((i = 1; i < ${#ffree[@]}; i++)); do
25839                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
25840                 if [ $tmp -gt $max ]; then
25841                         max=$tmp
25842                         max_index=$i
25843                 fi
25844                 if [ $tmp -lt $min ]; then
25845                         min=$tmp
25846                         min_index=$i
25847                 fi
25848         done
25849
25850         (( ${ffree[min_index]} > 0 )) ||
25851                 skip "no free files in MDT$min_index"
25852         (( ${ffree[min_index]} < 10000000 )) ||
25853                 skip "too many free files in MDT$min_index"
25854
25855         # Check if we need to generate uneven MDTs
25856         local diff=$(((max - min) * 100 / min))
25857         local testdir=$DIR/$tdir-fillmdt
25858         local start
25859
25860         i=0
25861         while (( diff < threshold )); do
25862                 mkdir -p $testdir
25863                 # generate uneven MDTs, create till $threshold% diff
25864                 echo -n "weight diff=$diff% must be > $threshold% ..."
25865                 echo "Fill MDT$min_index with $TEST413_COUNT files: loop $i"
25866                 testdir=$DIR/$tdir-fillmdt/$i
25867                 [ -d $testdir ] && continue
25868                 $LFS mkdir -i $min_index $testdir ||
25869                         error "mkdir $testdir failed"
25870                 $LFS setstripe -E 1M -L mdt $testdir ||
25871                         error "setstripe $testdir failed"
25872                 start=$SECONDS
25873                 for ((F=0; F < TEST413_COUNT; F++)); do
25874                         dd if=/dev/zero of=$testdir/f.$F bs=128K count=1 > \
25875                                 /dev/null 2>&1 || error "dd $F failed"
25876                 done
25877                 sync; sleep 1; sync
25878
25879                 # wait for QOS to update
25880                 (( SECONDS < start + 1 )) && sleep $((start + 1 - SECONDS))
25881
25882                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
25883                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
25884                 max=$(((${ffree[max_index]} >> 8) *
25885                         (${bavail[max_index]} * bsize >> 16)))
25886                 min=$(((${ffree[min_index]} >> 8) *
25887                         (${bavail[min_index]} * bsize >> 16)))
25888                 diff=$(((max - min) * 100 / min))
25889                 i=$((i + 1))
25890         done
25891
25892         echo "MDT filesfree available: ${ffree[*]}"
25893         echo "MDT blocks available: ${bavail[*]}"
25894         echo "weight diff=$diff%"
25895 }
25896
25897 test_qos_mkdir() {
25898         local mkdir_cmd=$1
25899         local stripe_count=$2
25900         local mdts=$(comma_list $(mdts_nodes))
25901
25902         local testdir
25903         local lmv_qos_prio_free
25904         local lmv_qos_threshold_rr
25905         local lmv_qos_maxage
25906         local lod_qos_prio_free
25907         local lod_qos_threshold_rr
25908         local lod_qos_maxage
25909         local count
25910         local i
25911
25912         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
25913         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
25914         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
25915                 head -n1)
25916         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
25917         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
25918         stack_trap "$LCTL set_param \
25919                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null"
25920         stack_trap "$LCTL set_param \
25921                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null"
25922         stack_trap "$LCTL set_param \
25923                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
25924
25925         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
25926                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
25927         lod_qos_prio_free=${lod_qos_prio_free%%%}
25928         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
25929                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
25930         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
25931         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
25932                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
25933         stack_trap "do_nodes $mdts $LCTL set_param \
25934                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null"
25935         stack_trap "do_nodes $mdts $LCTL set_param \
25936                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null"
25937         stack_trap "do_nodes $mdts $LCTL set_param \
25938                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null"
25939
25940         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
25941         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
25942
25943         testdir=$DIR/$tdir-s$stripe_count/rr
25944
25945         local stripe_index=$($LFS getstripe -m $testdir)
25946         local test_mkdir_rr=true
25947
25948         getfattr -d -m dmv -e hex $testdir | grep dmv
25949         if (( $? == 0 && $MDS1_VERSION >= $(version_code 2.14.51) )); then
25950                 echo "defstripe: '$($LFS getdirstripe -D $testdir)'"
25951                 (( $($LFS getdirstripe -D --max-inherit-rr $testdir) == 0 )) &&
25952                         test_mkdir_rr=false
25953         fi
25954
25955         echo
25956         $test_mkdir_rr &&
25957                 echo "Mkdir (stripe_count $stripe_count) roundrobin:" ||
25958                 echo "Mkdir (stripe_count $stripe_count) on stripe $stripe_index"
25959
25960         stack_trap "unlinkmany -d $testdir/subdir $((100 * MDSCOUNT))"
25961         for (( i = 0; i < 100 * MDSCOUNT; i++ )); do
25962                 eval $mkdir_cmd $testdir/subdir$i ||
25963                         error "$mkdir_cmd subdir$i failed"
25964         done
25965
25966         for (( i = 0; i < $MDSCOUNT; i++ )); do
25967                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
25968                 echo "$count directories created on MDT$i"
25969                 if $test_mkdir_rr; then
25970                         (( $count == 100 )) ||
25971                                 error "subdirs are not evenly distributed"
25972                 elif (( $i == $stripe_index )); then
25973                         (( $count == 100 * MDSCOUNT )) ||
25974                                 error "$count subdirs created on MDT$i"
25975                 else
25976                         (( $count == 0 )) ||
25977                                 error "$count subdirs created on MDT$i"
25978                 fi
25979
25980                 if $test_mkdir_rr && [ $stripe_count -gt 1 ]; then
25981                         count=$($LFS getdirstripe $testdir/* |
25982                                 grep -c -P "^\s+$i\t")
25983                         echo "$count stripes created on MDT$i"
25984                         # deviation should < 5% of average
25985                         (( $count >= 95 * stripe_count &&
25986                            $count <= 105 * stripe_count)) ||
25987                                 error "stripes are not evenly distributed"
25988                 fi
25989         done
25990
25991         echo
25992         echo "Check for uneven MDTs: "
25993
25994         local ffree
25995         local bavail
25996         local max
25997         local min
25998         local max_index
25999         local min_index
26000         local tmp
26001
26002         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26003         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26004         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26005
26006         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26007         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26008         max_index=0
26009         min_index=0
26010         for ((i = 1; i < ${#ffree[@]}; i++)); do
26011                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26012                 if [ $tmp -gt $max ]; then
26013                         max=$tmp
26014                         max_index=$i
26015                 fi
26016                 if [ $tmp -lt $min ]; then
26017                         min=$tmp
26018                         min_index=$i
26019                 fi
26020         done
26021
26022         (( ${ffree[min_index]} > 0 )) ||
26023                 skip "no free files in MDT$min_index"
26024         (( ${ffree[min_index]} < 10000000 )) ||
26025                 skip "too many free files in MDT$min_index"
26026
26027         echo "MDT filesfree available: ${ffree[*]}"
26028         echo "MDT blocks available: ${bavail[*]}"
26029         echo "weight diff=$(((max - min) * 100 / min))%"
26030         echo
26031         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
26032
26033         $LCTL set_param lmv.*.qos_threshold_rr=0 > /dev/null
26034         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
26035         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=0 > /dev/null
26036         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
26037         # decrease statfs age, so that it can be updated in time
26038         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
26039         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
26040
26041         sleep 1
26042
26043         testdir=$DIR/$tdir-s$stripe_count/qos
26044         local num=200
26045
26046         stack_trap "unlinkmany -d $testdir/subdir $((num * MDSCOUNT))"
26047         for (( i = 0; i < num * MDSCOUNT; i++ )); do
26048                 eval $mkdir_cmd $testdir/subdir$i ||
26049                         error "$mkdir_cmd subdir$i failed"
26050         done
26051
26052         max=0
26053         for (( i = 0; i < $MDSCOUNT; i++ )); do
26054                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
26055                 (( count > max )) && max=$count
26056                 echo "$count directories created on MDT$i"
26057         done
26058
26059         min=$($LFS getdirstripe -i $testdir/* | grep -c "^$min_index$")
26060
26061         # D-value should > 10% of averge
26062         (( max - min > num / 10 )) ||
26063                 error "subdirs shouldn't be evenly distributed: $max - $min < $((num / 10))"
26064
26065         # ditto for stripes
26066         if (( stripe_count > 1 )); then
26067                 max=0
26068                 for (( i = 0; i < $MDSCOUNT; i++ )); do
26069                         count=$($LFS getdirstripe $testdir/* |
26070                                 grep -c -P "^\s+$i\t")
26071                         (( count > max )) && max=$count
26072                         echo "$count stripes created on MDT$i"
26073                 done
26074
26075                 min=$($LFS getdirstripe $testdir/* |
26076                         grep -c -P "^\s+$min_index\t")
26077                 (( max - min > num * stripe_count / 10 )) ||
26078                         error "stripes shouldn't be evenly distributed: $max - $min < $((num / 10)) * $stripe_count"
26079         fi
26080 }
26081
26082 most_full_mdt() {
26083         local ffree
26084         local bavail
26085         local bsize
26086         local min
26087         local min_index
26088         local tmp
26089
26090         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26091         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26092         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26093
26094         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26095         min_index=0
26096         for ((i = 1; i < ${#ffree[@]}; i++)); do
26097                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26098                 (( tmp < min )) && min=$tmp && min_index=$i
26099         done
26100
26101         echo -n $min_index
26102 }
26103
26104 test_413a() {
26105         [ $MDSCOUNT -lt 2 ] &&
26106                 skip "We need at least 2 MDTs for this test"
26107
26108         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
26109                 skip "Need server version at least 2.12.52"
26110
26111         local stripe_count
26112
26113         generate_uneven_mdts 100
26114         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
26115                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
26116                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
26117                 $LFS mkdir -i $(most_full_mdt) $DIR/$tdir-s$stripe_count/qos ||
26118                         error "mkdir failed"
26119                 test_qos_mkdir "$LFS mkdir -i -1 -c $stripe_count" $stripe_count
26120         done
26121 }
26122 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
26123
26124 test_413b() {
26125         [ $MDSCOUNT -lt 2 ] &&
26126                 skip "We need at least 2 MDTs for this test"
26127
26128         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
26129                 skip "Need server version at least 2.12.52"
26130
26131         local testdir
26132         local stripe_count
26133
26134         generate_uneven_mdts 100
26135         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
26136                 testdir=$DIR/$tdir-s$stripe_count
26137                 mkdir $testdir || error "mkdir $testdir failed"
26138                 mkdir $testdir/rr || error "mkdir rr failed"
26139                 $LFS mkdir -i $(most_full_mdt) $testdir/qos ||
26140                         error "mkdir qos failed"
26141                 $LFS setdirstripe -D -c $stripe_count --max-inherit-rr 2 \
26142                         $testdir/rr || error "setdirstripe rr failed"
26143                 $LFS setdirstripe -D -c $stripe_count $testdir/qos ||
26144                         error "setdirstripe failed"
26145                 test_qos_mkdir "mkdir" $stripe_count
26146         done
26147 }
26148 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
26149
26150 test_413c() {
26151         (( $MDSCOUNT >= 2 )) ||
26152                 skip "We need at least 2 MDTs for this test"
26153
26154         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
26155                 skip "Need server version at least 2.14.51"
26156
26157         local testdir
26158         local inherit
26159         local inherit_rr
26160
26161         testdir=$DIR/${tdir}-s1
26162         mkdir $testdir || error "mkdir $testdir failed"
26163         mkdir $testdir/rr || error "mkdir rr failed"
26164         $LFS mkdir -i $(most_full_mdt) $testdir/qos || error "mkdir qos failed"
26165         # default max_inherit is -1, default max_inherit_rr is 0
26166         $LFS setdirstripe -D -c 1 $testdir/rr ||
26167                 error "setdirstripe rr failed"
26168         $LFS setdirstripe -D -c 1 -i -1 -X 2 --max-inherit-rr 1 $testdir/qos ||
26169                 error "setdirstripe qos failed"
26170         test_qos_mkdir "mkdir" 1
26171
26172         mkdir $testdir/rr/level1 || error "mkdir rr/level1 failed"
26173         inherit=$($LFS getdirstripe -D -X $testdir/rr/level1)
26174         (( $inherit == -1 )) || error "rr/level1 inherit $inherit != -1"
26175         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/rr/level1)
26176         (( $inherit_rr == 0 )) || error "rr/level1 inherit-rr $inherit_rr != 0"
26177
26178         mkdir $testdir/qos/level1 || error "mkdir qos/level1 failed"
26179         inherit=$($LFS getdirstripe -D -X $testdir/qos/level1)
26180         (( $inherit == 1 )) || error "qos/level1 inherit $inherit != 1"
26181         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/qos/level1)
26182         (( $inherit_rr == 0 )) || error "qos/level1 inherit-rr $inherit_rr != 0"
26183         mkdir $testdir/qos/level1/level2 || error "mkdir level2 failed"
26184         getfattr -d -m dmv -e hex $testdir/qos/level1/level2 | grep dmv &&
26185                 error "level2 shouldn't have default LMV" || true
26186 }
26187 run_test 413c "mkdir with default LMV max inherit rr"
26188
26189 test_413d() {
26190         (( MDSCOUNT >= 2 )) ||
26191                 skip "We need at least 2 MDTs for this test"
26192
26193         (( MDS1_VERSION >= $(version_code 2.14.51) )) ||
26194                 skip "Need server version at least 2.14.51"
26195
26196         local lmv_qos_threshold_rr
26197
26198         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
26199                 head -n1)
26200         stack_trap "$LCTL set_param \
26201                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
26202
26203         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
26204         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
26205         getfattr -d -m dmv -e hex $DIR/$tdir | grep dmv &&
26206                 error "$tdir shouldn't have default LMV"
26207         createmany -d $DIR/$tdir/sub $((100 * MDSCOUNT)) ||
26208                 error "mkdir sub failed"
26209
26210         local count=$($LFS getstripe -m $DIR/$tdir/* | grep -c ^0)
26211
26212         (( count == 100 )) || error "$count subdirs on MDT0"
26213 }
26214 run_test 413d "inherit ROOT default LMV"
26215
26216 test_413e() {
26217         (( MDSCOUNT >= 2 )) ||
26218                 skip "We need at least 2 MDTs for this test"
26219         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
26220                 skip "Need server version at least 2.14.55"
26221
26222         local testdir=$DIR/$tdir
26223         local tmpfile=$TMP/temp.setdirstripe.stderr.$$
26224         local max_inherit
26225         local sub_max_inherit
26226
26227         mkdir -p $testdir || error "failed to create $testdir"
26228
26229         # set default max-inherit to -1 if stripe count is 0 or 1
26230         $LFS setdirstripe -D -c 1 $testdir ||
26231                 error "failed to set default LMV"
26232         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
26233         (( max_inherit == -1 )) ||
26234                 error "wrong max_inherit value $max_inherit"
26235
26236         # set default max_inherit to a fixed value if stripe count is not 0 or 1
26237         $LFS setdirstripe -D -c -1 $testdir ||
26238                 error "failed to set default LMV"
26239         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
26240         (( max_inherit > 0 )) ||
26241                 error "wrong max_inherit value $max_inherit"
26242
26243         # and the subdir will decrease the max_inherit by 1
26244         mkdir -p $testdir/subdir-1 || error "failed to make subdir"
26245         sub_max_inherit=$($LFS getdirstripe -D --max-inherit $testdir/subdir-1)
26246         (( sub_max_inherit == max_inherit - 1)) ||
26247                 error "wrong max-inherit of subdir $sub_max_inherit"
26248
26249         # check specified --max-inherit and warning message
26250         stack_trap "rm -f $tmpfile"
26251         $LFS setdirstripe -D -c 2 --max-inherit=-1 $testdir 2> $tmpfile ||
26252                 error "failed to set default LMV"
26253         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
26254         (( max_inherit == -1 )) ||
26255                 error "wrong max_inherit value $max_inherit"
26256
26257         # check the warning messages
26258         if ! [[ $(cat $tmpfile) =~ "max-inherit=" ]]; then
26259                 error "failed to detect warning string"
26260         fi
26261 }
26262 run_test 413e "check default max-inherit value"
26263
26264 test_fs_dmv_inherit()
26265 {
26266         local testdir=$DIR/$tdir
26267
26268         local count
26269         local inherit
26270         local inherit_rr
26271
26272         for i in 1 2 3; do
26273                 mkdir $testdir || error "mkdir $testdir failed"
26274                 count=$($LFS getdirstripe -D -c $testdir)
26275                 (( count == 1 )) ||
26276                         error "$testdir default LMV count mismatch $count != 1"
26277                 inherit=$($LFS getdirstripe -D -X $testdir)
26278                 (( inherit == 3 - i )) ||
26279                         error "$testdir default LMV max-inherit $inherit != $((3 - i))"
26280                 inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir)
26281                 (( inherit_rr == 3 - i )) ||
26282                         error "$testdir default LMV max-inherit-rr $inherit_rr != $((3 - i))"
26283                 testdir=$testdir/sub
26284         done
26285
26286         mkdir $testdir || error "mkdir $testdir failed"
26287         count=$($LFS getdirstripe -D -c $testdir)
26288         (( count == 0 )) ||
26289                 error "$testdir default LMV count not zero: $count"
26290 }
26291
26292 test_413f() {
26293         (( MDSCOUNT >= 2 )) || skip "We need at least 2 MDTs for this test"
26294
26295         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
26296                 skip "Need server version at least 2.14.55"
26297
26298         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
26299                 error "dump $DIR default LMV failed"
26300         stack_trap "setfattr --restore=$TMP/dmv.ea"
26301
26302         $LFS setdirstripe -D -i -1 -c 1 -X 3 --max-inherit-rr 3 $DIR ||
26303                 error "set $DIR default LMV failed"
26304
26305         test_fs_dmv_inherit
26306 }
26307 run_test 413f "lfs getdirstripe -D list ROOT default LMV if it's not set on dir"
26308
26309 test_413g() {
26310         (( MDSCOUNT >= 2 )) || skip "We need at least 2 MDTs for this test"
26311
26312         mkdir -p $DIR/$tdir/l2/l3/l4 || error "mkdir $tdir/l1/l2/l3 failed"
26313         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
26314                 error "dump $DIR default LMV failed"
26315         stack_trap "setfattr --restore=$TMP/dmv.ea"
26316
26317         $LFS setdirstripe -D -i -1 -c 1 -X 3 --max-inherit-rr 3 $DIR ||
26318                 error "set $DIR default LMV failed"
26319
26320         FILESET="$FILESET/$tdir/l2/l3/l4" mount_client $MOUNT2 ||
26321                 error "mount $MOUNT2 failed"
26322         stack_trap "umount_client $MOUNT2"
26323
26324         local saved_DIR=$DIR
26325
26326         export DIR=$MOUNT2
26327
26328         stack_trap "export DIR=$saved_DIR"
26329
26330         # first check filesystem-wide default LMV inheritance
26331         test_fs_dmv_inherit || error "incorrect fs default LMV inheritance"
26332
26333         # then check subdirs are spread to all MDTs
26334         createmany -d $DIR/s $((MDSCOUNT * 100)) || error "createmany failed"
26335
26336         local count=$($LFS getstripe -m $DIR/s* | sort -u | wc -l)
26337
26338         (( $count == $MDSCOUNT )) || error "dirs are spread to $count MDTs"
26339 }
26340 run_test 413g "enforce ROOT default LMV on subdir mount"
26341
26342 test_413h() {
26343         (( MDSCOUNT >= 2 )) ||
26344                 skip "We need at least 2 MDTs for this test"
26345
26346         (( MDS1_VERSION >= $(version_code 2.15.50.6) )) ||
26347                 skip "Need server version at least 2.15.50.6"
26348
26349         local lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
26350
26351         stack_trap "$LCTL set_param \
26352                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
26353         $LCTL set_param lmv.*.qos_maxage=1
26354
26355         local depth=5
26356         local rr_depth=4
26357         local dir=$DIR/$tdir/l1/l2/l3/l4/l5
26358         local count=$((MDSCOUNT * 20))
26359
26360         generate_uneven_mdts 50
26361
26362         mkdir -p $dir || error "mkdir $dir failed"
26363         stack_trap "rm -rf $dir"
26364         $LFS setdirstripe -D -c 1 -i -1 --max-inherit=$depth \
26365                 --max-inherit-rr=$rr_depth $dir
26366
26367         for ((d=0; d < depth + 2; d++)); do
26368                 log "dir=$dir:"
26369                 for ((sub=0; sub < count; sub++)); do
26370                         mkdir $dir/d$sub
26371                 done
26372                 $LFS getdirstripe -i $dir/d* | sort | uniq -c | sort -nr
26373                 local num=($($LFS getdirstripe -i $dir/d* | sort | uniq -c))
26374                 # subdirs within $rr_depth should be created round-robin
26375                 if (( d < rr_depth )); then
26376                         (( ${num[0]} != count )) ||
26377                                 error "all objects created on MDT ${num[1]}"
26378                 fi
26379
26380                 dir=$dir/d0
26381         done
26382 }
26383 run_test 413h "don't stick to parent for round-robin dirs"
26384
26385 test_413z() {
26386         local pids=""
26387         local subdir
26388         local pid
26389
26390         for subdir in $(\ls -1 -d $DIR/d413*-fillmdt/*); do
26391                 unlinkmany $subdir/f. $TEST413_COUNT &
26392                 pids="$pids $!"
26393         done
26394
26395         for pid in $pids; do
26396                 wait $pid
26397         done
26398 }
26399 run_test 413z "413 test cleanup"
26400
26401 test_414() {
26402 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
26403         $LCTL set_param fail_loc=0x80000521
26404         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
26405         rm -f $DIR/$tfile
26406 }
26407 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
26408
26409 test_415() {
26410         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26411         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26412                 skip "Need server version at least 2.11.52"
26413
26414         # LU-11102
26415         local total
26416         local setattr_pid
26417         local start_time
26418         local end_time
26419         local duration
26420
26421         total=500
26422         # this test may be slow on ZFS
26423         [ "$mds1_FSTYPE" == "zfs" ] && total=50
26424
26425         # though this test is designed for striped directory, let's test normal
26426         # directory too since lock is always saved as CoS lock.
26427         test_mkdir $DIR/$tdir || error "mkdir $tdir"
26428         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
26429
26430         (
26431                 while true; do
26432                         touch $DIR/$tdir
26433                 done
26434         ) &
26435         setattr_pid=$!
26436
26437         start_time=$(date +%s)
26438         for i in $(seq $total); do
26439                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
26440                         > /dev/null
26441         done
26442         end_time=$(date +%s)
26443         duration=$((end_time - start_time))
26444
26445         kill -9 $setattr_pid
26446
26447         echo "rename $total files took $duration sec"
26448         [ $duration -lt 100 ] || error "rename took $duration sec"
26449 }
26450 run_test 415 "lock revoke is not missing"
26451
26452 test_416() {
26453         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
26454                 skip "Need server version at least 2.11.55"
26455
26456         # define OBD_FAIL_OSD_TXN_START    0x19a
26457         do_facet mds1 lctl set_param fail_loc=0x19a
26458
26459         lfs mkdir -c $MDSCOUNT $DIR/$tdir
26460
26461         true
26462 }
26463 run_test 416 "transaction start failure won't cause system hung"
26464
26465 cleanup_417() {
26466         trap 0
26467         do_nodes $(comma_list $(mdts_nodes)) \
26468                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
26469         do_nodes $(comma_list $(mdts_nodes)) \
26470                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
26471         do_nodes $(comma_list $(mdts_nodes)) \
26472                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
26473 }
26474
26475 test_417() {
26476         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26477         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
26478                 skip "Need MDS version at least 2.11.56"
26479
26480         trap cleanup_417 RETURN EXIT
26481
26482         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
26483         do_nodes $(comma_list $(mdts_nodes)) \
26484                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
26485         $LFS migrate -m 0 $DIR/$tdir.1 &&
26486                 error "migrate dir $tdir.1 should fail"
26487
26488         do_nodes $(comma_list $(mdts_nodes)) \
26489                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
26490         $LFS mkdir -i 1 $DIR/$tdir.2 &&
26491                 error "create remote dir $tdir.2 should fail"
26492
26493         do_nodes $(comma_list $(mdts_nodes)) \
26494                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
26495         $LFS mkdir -c 2 $DIR/$tdir.3 &&
26496                 error "create striped dir $tdir.3 should fail"
26497         true
26498 }
26499 run_test 417 "disable remote dir, striped dir and dir migration"
26500
26501 # Checks that the outputs of df [-i] and lfs df [-i] match
26502 #
26503 # usage: check_lfs_df <blocks | inodes> <mountpoint>
26504 check_lfs_df() {
26505         local dir=$2
26506         local inodes
26507         local df_out
26508         local lfs_df_out
26509         local count
26510         local passed=false
26511
26512         # blocks or inodes
26513         [ "$1" == "blocks" ] && inodes= || inodes="-i"
26514
26515         for count in {1..100}; do
26516                 do_nodes "$CLIENTS" \
26517                         $LCTL set_param ldlm.namespaces.*.lru_size=clear
26518                 sync; sleep 0.2
26519
26520                 # read the lines of interest
26521                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
26522                         error "df $inodes $dir | tail -n +2 failed"
26523                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
26524                         error "lfs df $inodes $dir | grep summary: failed"
26525
26526                 # skip first substrings of each output as they are different
26527                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
26528                 # compare the two outputs
26529                 passed=true
26530                 #  skip "available" on MDT until LU-13997 is fixed.
26531                 #for i in {1..5}; do
26532                 for i in 1 2 4 5; do
26533                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
26534                 done
26535                 $passed && break
26536         done
26537
26538         if ! $passed; then
26539                 df -P $inodes $dir
26540                 echo
26541                 lfs df $inodes $dir
26542                 error "df and lfs df $1 output mismatch: "      \
26543                       "df ${inodes}: ${df_out[*]}, "            \
26544                       "lfs df ${inodes}: ${lfs_df_out[*]}"
26545         fi
26546 }
26547
26548 test_418() {
26549         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26550
26551         local dir=$DIR/$tdir
26552         local numfiles=$((RANDOM % 4096 + 2))
26553         local numblocks=$((RANDOM % 256 + 1))
26554
26555         wait_delete_completed
26556         test_mkdir $dir
26557
26558         # check block output
26559         check_lfs_df blocks $dir
26560         # check inode output
26561         check_lfs_df inodes $dir
26562
26563         # create a single file and retest
26564         echo "Creating a single file and testing"
26565         createmany -o $dir/$tfile- 1 &>/dev/null ||
26566                 error "creating 1 file in $dir failed"
26567         check_lfs_df blocks $dir
26568         check_lfs_df inodes $dir
26569
26570         # create a random number of files
26571         echo "Creating $((numfiles - 1)) files and testing"
26572         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
26573                 error "creating $((numfiles - 1)) files in $dir failed"
26574
26575         # write a random number of blocks to the first test file
26576         echo "Writing $numblocks 4K blocks and testing"
26577         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
26578                 count=$numblocks &>/dev/null ||
26579                 error "dd to $dir/${tfile}-0 failed"
26580
26581         # retest
26582         check_lfs_df blocks $dir
26583         check_lfs_df inodes $dir
26584
26585         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
26586                 error "unlinking $numfiles files in $dir failed"
26587 }
26588 run_test 418 "df and lfs df outputs match"
26589
26590 test_419()
26591 {
26592         local dir=$DIR/$tdir
26593
26594         mkdir -p $dir
26595         touch $dir/file
26596
26597         cancel_lru_locks mdc
26598
26599         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
26600         $LCTL set_param fail_loc=0x1410
26601         cat $dir/file
26602         $LCTL set_param fail_loc=0
26603         rm -rf $dir
26604 }
26605 run_test 419 "Verify open file by name doesn't crash kernel"
26606
26607 test_420()
26608 {
26609         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
26610                 skip "Need MDS version at least 2.12.53"
26611
26612         local SAVE_UMASK=$(umask)
26613         local dir=$DIR/$tdir
26614         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
26615
26616         mkdir -p $dir
26617         umask 0000
26618         mkdir -m03777 $dir/testdir
26619         ls -dn $dir/testdir
26620         # Need to remove trailing '.' when SELinux is enabled
26621         local dirperms=$(ls -dn $dir/testdir |
26622                          awk '{ sub(/\.$/, "", $1); print $1}')
26623         [ $dirperms == "drwxrwsrwt" ] ||
26624                 error "incorrect perms on $dir/testdir"
26625
26626         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
26627                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
26628         ls -n $dir/testdir/testfile
26629         local fileperms=$(ls -n $dir/testdir/testfile |
26630                           awk '{ sub(/\.$/, "", $1); print $1}')
26631         [ $fileperms == "-rwxr-xr-x" ] ||
26632                 error "incorrect perms on $dir/testdir/testfile"
26633
26634         umask $SAVE_UMASK
26635 }
26636 run_test 420 "clear SGID bit on non-directories for non-members"
26637
26638 test_421a() {
26639         local cnt
26640         local fid1
26641         local fid2
26642
26643         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26644                 skip "Need MDS version at least 2.12.54"
26645
26646         test_mkdir $DIR/$tdir
26647         createmany -o $DIR/$tdir/f 3
26648         cnt=$(ls -1 $DIR/$tdir | wc -l)
26649         [ $cnt != 3 ] && error "unexpected #files: $cnt"
26650
26651         fid1=$(lfs path2fid $DIR/$tdir/f1)
26652         fid2=$(lfs path2fid $DIR/$tdir/f2)
26653         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
26654
26655         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
26656         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
26657
26658         cnt=$(ls -1 $DIR/$tdir | wc -l)
26659         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
26660
26661         rm -f $DIR/$tdir/f3 || error "can't remove f3"
26662         createmany -o $DIR/$tdir/f 3
26663         cnt=$(ls -1 $DIR/$tdir | wc -l)
26664         [ $cnt != 3 ] && error "unexpected #files: $cnt"
26665
26666         fid1=$(lfs path2fid $DIR/$tdir/f1)
26667         fid2=$(lfs path2fid $DIR/$tdir/f2)
26668         echo "remove using fsname $FSNAME"
26669         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
26670
26671         cnt=$(ls -1 $DIR/$tdir | wc -l)
26672         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
26673 }
26674 run_test 421a "simple rm by fid"
26675
26676 test_421b() {
26677         local cnt
26678         local FID1
26679         local FID2
26680
26681         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26682                 skip "Need MDS version at least 2.12.54"
26683
26684         test_mkdir $DIR/$tdir
26685         createmany -o $DIR/$tdir/f 3
26686         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
26687         MULTIPID=$!
26688
26689         FID1=$(lfs path2fid $DIR/$tdir/f1)
26690         FID2=$(lfs path2fid $DIR/$tdir/f2)
26691         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
26692
26693         kill -USR1 $MULTIPID
26694         wait
26695
26696         cnt=$(ls $DIR/$tdir | wc -l)
26697         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
26698 }
26699 run_test 421b "rm by fid on open file"
26700
26701 test_421c() {
26702         local cnt
26703         local FIDS
26704
26705         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26706                 skip "Need MDS version at least 2.12.54"
26707
26708         test_mkdir $DIR/$tdir
26709         createmany -o $DIR/$tdir/f 3
26710         touch $DIR/$tdir/$tfile
26711         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
26712         cnt=$(ls -1 $DIR/$tdir | wc -l)
26713         [ $cnt != 184 ] && error "unexpected #files: $cnt"
26714
26715         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
26716         $LFS rmfid $DIR $FID1 || error "rmfid failed"
26717
26718         cnt=$(ls $DIR/$tdir | wc -l)
26719         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
26720 }
26721 run_test 421c "rm by fid against hardlinked files"
26722
26723 test_421d() {
26724         local cnt
26725         local FIDS
26726
26727         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26728                 skip "Need MDS version at least 2.12.54"
26729
26730         test_mkdir $DIR/$tdir
26731         createmany -o $DIR/$tdir/f 4097
26732         cnt=$(ls -1 $DIR/$tdir | wc -l)
26733         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
26734
26735         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
26736         $LFS rmfid $DIR $FIDS || error "rmfid failed"
26737
26738         cnt=$(ls $DIR/$tdir | wc -l)
26739         rm -rf $DIR/$tdir
26740         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
26741 }
26742 run_test 421d "rmfid en masse"
26743
26744 test_421e() {
26745         local cnt
26746         local FID
26747
26748         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26749         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26750                 skip "Need MDS version at least 2.12.54"
26751
26752         mkdir -p $DIR/$tdir
26753         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
26754         createmany -o $DIR/$tdir/striped_dir/f 512
26755         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
26756         [ $cnt != 512 ] && error "unexpected #files: $cnt"
26757
26758         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
26759                 sed "s/[/][^:]*://g")
26760         $LFS rmfid $DIR $FIDS || error "rmfid failed"
26761
26762         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
26763         rm -rf $DIR/$tdir
26764         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
26765 }
26766 run_test 421e "rmfid in DNE"
26767
26768 test_421f() {
26769         local cnt
26770         local FID
26771
26772         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26773                 skip "Need MDS version at least 2.12.54"
26774
26775         test_mkdir $DIR/$tdir
26776         touch $DIR/$tdir/f
26777         cnt=$(ls -1 $DIR/$tdir | wc -l)
26778         [ $cnt != 1 ] && error "unexpected #files: $cnt"
26779
26780         FID=$(lfs path2fid $DIR/$tdir/f)
26781         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
26782         # rmfid should fail
26783         cnt=$(ls -1 $DIR/$tdir | wc -l)
26784         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
26785
26786         chmod a+rw $DIR/$tdir
26787         ls -la $DIR/$tdir
26788         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
26789         # rmfid should fail
26790         cnt=$(ls -1 $DIR/$tdir | wc -l)
26791         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
26792
26793         rm -f $DIR/$tdir/f
26794         $RUNAS touch $DIR/$tdir/f
26795         FID=$(lfs path2fid $DIR/$tdir/f)
26796         echo "rmfid as root"
26797         $LFS rmfid $DIR $FID || error "rmfid as root failed"
26798         cnt=$(ls -1 $DIR/$tdir | wc -l)
26799         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
26800
26801         rm -f $DIR/$tdir/f
26802         $RUNAS touch $DIR/$tdir/f
26803         cnt=$(ls -1 $DIR/$tdir | wc -l)
26804         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
26805         FID=$(lfs path2fid $DIR/$tdir/f)
26806         # rmfid w/o user_fid2path mount option should fail
26807         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
26808         cnt=$(ls -1 $DIR/$tdir | wc -l)
26809         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
26810
26811         tmpdir=$(mktemp -d /tmp/lustre-XXXXXX)
26812         stack_trap "rmdir $tmpdir"
26813         mount_client $tmpdir "$MOUNT_OPTS,user_fid2path" ||
26814                 error "failed to mount client'"
26815         stack_trap "umount_client $tmpdir"
26816
26817         $RUNAS $LFS rmfid $tmpdir $FID || error "rmfid failed"
26818         # rmfid should succeed
26819         cnt=$(ls -1 $tmpdir/$tdir | wc -l)
26820         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
26821
26822         # rmfid shouldn't allow to remove files due to dir's permission
26823         chmod a+rwx $tmpdir/$tdir
26824         touch $tmpdir/$tdir/f
26825         ls -la $tmpdir/$tdir
26826         FID=$(lfs path2fid $tmpdir/$tdir/f)
26827         $RUNAS $LFS rmfid $tmpdir $FID && error "rmfid didn't fail"
26828         return 0
26829 }
26830 run_test 421f "rmfid checks permissions"
26831
26832 test_421g() {
26833         local cnt
26834         local FIDS
26835
26836         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26837         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26838                 skip "Need MDS version at least 2.12.54"
26839
26840         mkdir -p $DIR/$tdir
26841         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
26842         createmany -o $DIR/$tdir/striped_dir/f 512
26843         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
26844         [ $cnt != 512 ] && error "unexpected #files: $cnt"
26845
26846         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
26847                 sed "s/[/][^:]*://g")
26848
26849         rm -f $DIR/$tdir/striped_dir/f1*
26850         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
26851         removed=$((512 - cnt))
26852
26853         # few files have been just removed, so we expect
26854         # rmfid to fail on their fids
26855         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
26856         [ $removed != $errors ] && error "$errors != $removed"
26857
26858         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
26859         rm -rf $DIR/$tdir
26860         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
26861 }
26862 run_test 421g "rmfid to return errors properly"
26863
26864 test_422() {
26865         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
26866         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
26867         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
26868         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
26869         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
26870
26871         local amc=$(at_max_get client)
26872         local amo=$(at_max_get mds1)
26873         local timeout=`lctl get_param -n timeout`
26874
26875         at_max_set 0 client
26876         at_max_set 0 mds1
26877
26878 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
26879         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
26880                         fail_val=$(((2*timeout + 10)*1000))
26881         touch $DIR/$tdir/d3/file &
26882         sleep 2
26883 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
26884         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
26885                         fail_val=$((2*timeout + 5))
26886         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
26887         local pid=$!
26888         sleep 1
26889         kill -9 $pid
26890         sleep $((2 * timeout))
26891         echo kill $pid
26892         kill -9 $pid
26893         lctl mark touch
26894         touch $DIR/$tdir/d2/file3
26895         touch $DIR/$tdir/d2/file4
26896         touch $DIR/$tdir/d2/file5
26897
26898         wait
26899         at_max_set $amc client
26900         at_max_set $amo mds1
26901
26902         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
26903         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
26904                 error "Watchdog is always throttled"
26905 }
26906 run_test 422 "kill a process with RPC in progress"
26907
26908 stat_test() {
26909     df -h $MOUNT &
26910     df -h $MOUNT &
26911     df -h $MOUNT &
26912     df -h $MOUNT &
26913     df -h $MOUNT &
26914     df -h $MOUNT &
26915 }
26916
26917 test_423() {
26918     local _stats
26919     # ensure statfs cache is expired
26920     sleep 2;
26921
26922     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
26923     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
26924
26925     return 0
26926 }
26927 run_test 423 "statfs should return a right data"
26928
26929 test_424() {
26930 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
26931         $LCTL set_param fail_loc=0x80000522
26932         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
26933         rm -f $DIR/$tfile
26934 }
26935 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
26936
26937 test_425() {
26938         test_mkdir -c -1 $DIR/$tdir
26939         $LFS setstripe -c -1 $DIR/$tdir
26940
26941         lru_resize_disable "" 100
26942         stack_trap "lru_resize_enable" EXIT
26943
26944         sleep 5
26945
26946         for i in $(seq $((MDSCOUNT * 125))); do
26947                 local t=$DIR/$tdir/$tfile_$i
26948
26949                 dd if=/dev/zero of=$t bs=4K count=1 > /dev/null 2>&1 ||
26950                         error_noexit "Create file $t"
26951         done
26952         stack_trap "rm -rf $DIR/$tdir" EXIT
26953
26954         for oscparam in $($LCTL list_param ldlm.namespaces.*osc-[-0-9a-f]*); do
26955                 local lru_size=$($LCTL get_param -n $oscparam.lru_size)
26956                 local lock_count=$($LCTL get_param -n $oscparam.lock_count)
26957
26958                 [ $lock_count -le $lru_size ] ||
26959                         error "osc lock count $lock_count > lru size $lru_size"
26960         done
26961
26962         for mdcparam in $($LCTL list_param ldlm.namespaces.*mdc-*); do
26963                 local lru_size=$($LCTL get_param -n $mdcparam.lru_size)
26964                 local lock_count=$($LCTL get_param -n $mdcparam.lock_count)
26965
26966                 [ $lock_count -le $lru_size ] ||
26967                         error "mdc lock count $lock_count > lru size $lru_size"
26968         done
26969 }
26970 run_test 425 "lock count should not exceed lru size"
26971
26972 test_426() {
26973         splice-test -r $DIR/$tfile
26974         splice-test -rd $DIR/$tfile
26975         splice-test $DIR/$tfile
26976         splice-test -d $DIR/$tfile
26977 }
26978 run_test 426 "splice test on Lustre"
26979
26980 test_427() {
26981         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
26982         (( $MDS1_VERSION >= $(version_code 2.12.4) )) ||
26983                 skip "Need MDS version at least 2.12.4"
26984         local log
26985
26986         mkdir $DIR/$tdir
26987         mkdir $DIR/$tdir/1
26988         mkdir $DIR/$tdir/2
26989         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/1/dir
26990         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/2/dir2
26991
26992         $LFS getdirstripe $DIR/$tdir/1/dir
26993
26994         #first setfattr for creating updatelog
26995         setfattr -n user.attr0 -v "some text" $DIR/$tdir/1/dir
26996
26997 #define OBD_FAIL_OUT_OBJECT_MISS        0x1708
26998         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x80001708
26999         setfattr -n user.attr1 -v "some text" $DIR/$tdir/1/dir &
27000         setfattr -n user.attr2 -v "another attr"  $DIR/$tdir/2/dir2 &
27001
27002         sleep 2
27003         fail mds2
27004         wait_recovery_complete mds2 $((2*TIMEOUT))
27005
27006         log=$(do_facet mds1 dmesg | tac | sed "/${TESTNAME//_/ }/,$ d")
27007         echo $log | grep "get update log failed" &&
27008                 error "update log corruption is detected" || true
27009 }
27010 run_test 427 "Failed DNE2 update request shouldn't corrupt updatelog"
27011
27012 test_428() {
27013         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27014         local cache_limit=$CACHE_MAX
27015
27016         stack_trap "$LCTL set_param -n llite.*.max_cached_mb=$cache_limit"
27017         $LCTL set_param -n llite.*.max_cached_mb=64
27018
27019         mkdir $DIR/$tdir
27020         $LFS setstripe -c 1 $DIR/$tdir
27021         eval touch $DIR/$tdir/$tfile.{1..$OSTCOUNT}
27022         stack_trap "rm -f $DIR/$tdir/$tfile.*"
27023         #test write
27024         for f in $(seq 4); do
27025                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$f bs=128M count=1 &
27026         done
27027         wait
27028
27029         cancel_lru_locks osc
27030         # Test read
27031         for f in $(seq 4); do
27032                 dd if=$DIR/$tdir/$tfile.$f of=/dev/null bs=128M count=1 &
27033         done
27034         wait
27035 }
27036 run_test 428 "large block size IO should not hang"
27037
27038 test_429() { # LU-7915 / LU-10948
27039         local ll_opencache_threshold_count="llite.*.opencache_threshold_count"
27040         local testfile=$DIR/$tfile
27041         local mdc_rpcstats="mdc.$FSNAME-MDT0000-*.stats"
27042         local new_flag=1
27043         local first_rpc
27044         local second_rpc
27045         local third_rpc
27046
27047         $LCTL get_param $ll_opencache_threshold_count ||
27048                 skip "client does not have opencache parameter"
27049
27050         set_opencache $new_flag
27051         stack_trap "restore_opencache"
27052         [ $($LCTL get_param -n $ll_opencache_threshold_count) == $new_flag ] ||
27053                 error "enable opencache failed"
27054         touch $testfile
27055         # drop MDC DLM locks
27056         cancel_lru_locks mdc
27057         # clear MDC RPC stats counters
27058         $LCTL set_param $mdc_rpcstats=clear
27059
27060         # According to the current implementation, we need to run 3 times
27061         # open & close file to verify if opencache is enabled correctly.
27062         # 1st, RPCs are sent for lookup/open and open handle is released on
27063         #      close finally.
27064         # 2nd, RPC is sent for open, MDS_OPEN_LOCK is fetched automatically,
27065         #      so open handle won't be released thereafter.
27066         # 3rd, No RPC is sent out.
27067         $MULTIOP $testfile oc || error "multiop failed"
27068         first_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
27069         echo "1st: $first_rpc RPCs in flight"
27070
27071         $MULTIOP $testfile oc || error "multiop failed"
27072         second_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
27073         echo "2nd: $second_rpc RPCs in flight"
27074
27075         $MULTIOP $testfile oc || error "multiop failed"
27076         third_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
27077         echo "3rd: $third_rpc RPCs in flight"
27078
27079         #verify no MDC RPC is sent
27080         [[ $second_rpc == $third_rpc ]] || error "MDC RPC is still sent"
27081 }
27082 run_test 429 "verify if opencache flag on client side does work"
27083
27084 lseek_test_430() {
27085         local offset
27086         local file=$1
27087
27088         # data at [200K, 400K)
27089         dd if=/dev/urandom of=$file bs=256K count=1 seek=1 ||
27090                 error "256K->512K dd fails"
27091         # data at [2M, 3M)
27092         dd if=/dev/urandom of=$file bs=1M count=1 seek=2 ||
27093                 error "2M->3M dd fails"
27094         # data at [4M, 5M)
27095         dd if=/dev/urandom of=$file bs=1M count=1 seek=4 ||
27096                 error "4M->5M dd fails"
27097         echo "Data at 256K...512K, 2M...3M and 4M...5M"
27098         # start at first component hole #1
27099         printf "Seeking hole from 1000 ... "
27100         offset=$(lseek_test -l 1000 $file)
27101         echo $offset
27102         [[ $offset == 1000 ]] || error "offset $offset != 1000"
27103         printf "Seeking data from 1000 ... "
27104         offset=$(lseek_test -d 1000 $file)
27105         echo $offset
27106         [[ $offset == 262144 ]] || error "offset $offset != 262144"
27107
27108         # start at first component data block
27109         printf "Seeking hole from 300000 ... "
27110         offset=$(lseek_test -l 300000 $file)
27111         echo $offset
27112         [[ $offset == 524288 ]] || error "offset $offset != 524288"
27113         printf "Seeking data from 300000 ... "
27114         offset=$(lseek_test -d 300000 $file)
27115         echo $offset
27116         [[ $offset == 300000 ]] || error "offset $offset != 300000"
27117
27118         # start at the first component but beyond end of object size
27119         printf "Seeking hole from 1000000 ... "
27120         offset=$(lseek_test -l 1000000 $file)
27121         echo $offset
27122         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
27123         printf "Seeking data from 1000000 ... "
27124         offset=$(lseek_test -d 1000000 $file)
27125         echo $offset
27126         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
27127
27128         # start at second component stripe 2 (empty file)
27129         printf "Seeking hole from 1500000 ... "
27130         offset=$(lseek_test -l 1500000 $file)
27131         echo $offset
27132         [[ $offset == 1500000 ]] || error "offset $offset != 1500000"
27133         printf "Seeking data from 1500000 ... "
27134         offset=$(lseek_test -d 1500000 $file)
27135         echo $offset
27136         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
27137
27138         # start at second component stripe 1 (all data)
27139         printf "Seeking hole from 3000000 ... "
27140         offset=$(lseek_test -l 3000000 $file)
27141         echo $offset
27142         [[ $offset == 3145728 ]] || error "offset $offset != 3145728"
27143         printf "Seeking data from 3000000 ... "
27144         offset=$(lseek_test -d 3000000 $file)
27145         echo $offset
27146         [[ $offset == 3000000 ]] || error "offset $offset != 3000000"
27147
27148         dd if=/dev/urandom of=$file bs=640K count=1 seek=1 ||
27149                 error "2nd dd fails"
27150         echo "Add data block at 640K...1280K"
27151
27152         # start at before new data block, in hole
27153         printf "Seeking hole from 600000 ... "
27154         offset=$(lseek_test -l 600000 $file)
27155         echo $offset
27156         [[ $offset == 600000 ]] || error "offset $offset != 600000"
27157         printf "Seeking data from 600000 ... "
27158         offset=$(lseek_test -d 600000 $file)
27159         echo $offset
27160         [[ $offset == 655360 ]] || error "offset $offset != 655360"
27161
27162         # start at the first component new data block
27163         printf "Seeking hole from 1000000 ... "
27164         offset=$(lseek_test -l 1000000 $file)
27165         echo $offset
27166         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
27167         printf "Seeking data from 1000000 ... "
27168         offset=$(lseek_test -d 1000000 $file)
27169         echo $offset
27170         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
27171
27172         # start at second component stripe 2, new data
27173         printf "Seeking hole from 1200000 ... "
27174         offset=$(lseek_test -l 1200000 $file)
27175         echo $offset
27176         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
27177         printf "Seeking data from 1200000 ... "
27178         offset=$(lseek_test -d 1200000 $file)
27179         echo $offset
27180         [[ $offset == 1200000 ]] || error "offset $offset != 1200000"
27181
27182         # start beyond file end
27183         printf "Using offset > filesize ... "
27184         lseek_test -l 4000000 $file && error "lseek should fail"
27185         printf "Using offset > filesize ... "
27186         lseek_test -d 4000000 $file && error "lseek should fail"
27187
27188         printf "Done\n\n"
27189 }
27190
27191 test_430a() {
27192         $LCTL get_param mdc.*.import | grep -q 'connect_flags:.*seek' ||
27193                 skip "MDT does not support SEEK_HOLE"
27194
27195         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
27196                 skip "OST does not support SEEK_HOLE"
27197
27198         local file=$DIR/$tdir/$tfile
27199
27200         mkdir -p $DIR/$tdir
27201
27202         $LFS setstripe -E 1M -L mdt -E eof -c2 $file
27203         # OST stripe #1 will have continuous data at [1M, 3M)
27204         # OST stripe #2 is empty
27205         echo "Component #1: 1M DoM, component #2: EOF, 2 stripes 1M"
27206         lseek_test_430 $file
27207         rm $file
27208         $LFS setstripe -E 1M -c2 -S 64K -E 10M -c2 -S 1M $file
27209         echo "Component #1: 1M, 2 stripes 64K, component #2: EOF, 2 stripes 1M"
27210         lseek_test_430 $file
27211         rm $file
27212         $LFS setstripe -c2 -S 512K $file
27213         echo "Two stripes, stripe size 512K"
27214         lseek_test_430 $file
27215         rm $file
27216         # FLR with stale mirror
27217         $LFS setstripe -N -E 512K -c1 -S 64K -E eof -c2 -S 512K \
27218                        -N -c2 -S 1M $file
27219         echo "Mirrored file:"
27220         echo "Component #1: 512K, stripe 64K, component #2: EOF, 2 stripes 512K"
27221         echo "Plain 2 stripes 1M"
27222         lseek_test_430 $file
27223         rm $file
27224 }
27225 run_test 430a "lseek: SEEK_DATA/SEEK_HOLE basic functionality"
27226
27227 test_430b() {
27228         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
27229                 skip "OST does not support SEEK_HOLE"
27230
27231         local offset
27232         local file=$DIR/$tdir/$tfile
27233
27234         mkdir -p $DIR/$tdir
27235         # Empty layout lseek should fail
27236         $MCREATE $file
27237         # seek from 0
27238         printf "Seeking hole from 0 ... "
27239         lseek_test -l 0 $file && error "lseek should fail"
27240         printf "Seeking data from 0 ... "
27241         lseek_test -d 0 $file && error "lseek should fail"
27242         rm $file
27243
27244         # 1M-hole file
27245         $LFS setstripe -E 1M -c2 -E eof $file
27246         $TRUNCATE $file 1048576
27247         printf "Seeking hole from 1000000 ... "
27248         offset=$(lseek_test -l 1000000 $file)
27249         echo $offset
27250         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
27251         printf "Seeking data from 1000000 ... "
27252         lseek_test -d 1000000 $file && error "lseek should fail"
27253         rm $file
27254
27255         # full component followed by non-inited one
27256         $LFS setstripe -E 1M -c2 -E eof $file
27257         dd if=/dev/urandom of=$file bs=1M count=1
27258         printf "Seeking hole from 1000000 ... "
27259         offset=$(lseek_test -l 1000000 $file)
27260         echo $offset
27261         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
27262         printf "Seeking hole from 1048576 ... "
27263         lseek_test -l 1048576 $file && error "lseek should fail"
27264         # init second component and truncate back
27265         echo "123" >> $file
27266         $TRUNCATE $file 1048576
27267         printf "Seeking hole from 1000000 ... "
27268         offset=$(lseek_test -l 1000000 $file)
27269         echo $offset
27270         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
27271         printf "Seeking hole from 1048576 ... "
27272         lseek_test -l 1048576 $file && error "lseek should fail"
27273         # boundary checks for big values
27274         dd if=/dev/urandom of=$file.10g bs=1 count=1 seek=10G
27275         offset=$(lseek_test -d 0 $file.10g)
27276         [[ $offset == 10737418240 ]] || error "offset $offset != 10737418240"
27277         dd if=/dev/urandom of=$file.100g bs=1 count=1 seek=100G
27278         offset=$(lseek_test -d 0 $file.100g)
27279         [[ $offset == 107374182400 ]] || error "offset $offset != 107374182400"
27280         return 0
27281 }
27282 run_test 430b "lseek: SEEK_DATA/SEEK_HOLE special cases"
27283
27284 test_430c() {
27285         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
27286                 skip "OST does not support SEEK_HOLE"
27287
27288         local file=$DIR/$tdir/$tfile
27289         local start
27290
27291         mkdir -p $DIR/$tdir
27292         dd if=/dev/urandom of=$file bs=1k count=1 seek=5M
27293
27294         # cp version 8.33+ prefers lseek over fiemap
27295         if [[ $(cp --version | head -n1 | sed "s/[^0-9]//g") -ge 833 ]]; then
27296                 start=$SECONDS
27297                 time cp $file /dev/null
27298                 (( SECONDS - start < 5 )) ||
27299                         error "cp: too long runtime $((SECONDS - start))"
27300
27301         fi
27302         # tar version 1.29+ supports SEEK_HOLE/DATA
27303         if [[ $(tar --version | head -n1 | sed "s/[^0-9]//g") -ge 129 ]]; then
27304                 start=$SECONDS
27305                 time tar cS $file - | cat > /dev/null
27306                 (( SECONDS - start < 5 )) ||
27307                         error "tar: too long runtime $((SECONDS - start))"
27308         fi
27309 }
27310 run_test 430c "lseek: external tools check"
27311
27312 test_431() { # LU-14187
27313         local file=$DIR/$tdir/$tfile
27314
27315         mkdir -p $DIR/$tdir
27316         $LFS setstripe -c 1 -i 0 $file || error "lfs setstripe failed"
27317         dd if=/dev/urandom of=$file bs=4k count=1
27318         dd if=/dev/urandom of=$file bs=4k count=1 seek=10 conv=notrunc
27319         dd if=/dev/urandom of=$file bs=4k count=1 seek=12 conv=notrunc
27320         #define OBD_FAIL_OST_RESTART_IO 0x251
27321         do_facet ost1 "$LCTL set_param fail_loc=0x251"
27322         $LFS setstripe -c 1 -i 0 $file.0 || error "lfs setstripe failed"
27323         cp $file $file.0
27324         cancel_lru_locks
27325         sync_all_data
27326         echo 3 > /proc/sys/vm/drop_caches
27327         diff  $file $file.0 || error "data diff"
27328 }
27329 run_test 431 "Restart transaction for IO"
27330
27331 cleanup_test_432() {
27332         do_facet mgs $LCTL nodemap_activate 0
27333         wait_nm_sync active
27334 }
27335
27336 test_432() {
27337         local tmpdir=$TMP/dir432
27338
27339         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
27340                 skip "Need MDS version at least 2.14.52"
27341
27342         stack_trap cleanup_test_432 EXIT
27343         mkdir $DIR/$tdir
27344         mkdir $tmpdir
27345
27346         do_facet mgs $LCTL nodemap_activate 1
27347         wait_nm_sync active
27348         do_facet mgs $LCTL nodemap_modify --name default \
27349                 --property admin --value 1
27350         do_facet mgs $LCTL nodemap_modify --name default \
27351                 --property trusted --value 1
27352         cancel_lru_locks mdc
27353         wait_nm_sync default admin_nodemap
27354         wait_nm_sync default trusted_nodemap
27355
27356         if [ $(mv $tmpdir $DIR/$tdir/ 2>&1 |
27357                grep -ci "Operation not permitted") -ne 0 ]; then
27358                 error "mv $tmpdir $DIR/$tdir/ hits 'Operation not permitted'"
27359         fi
27360 }
27361 run_test 432 "mv dir from outside Lustre"
27362
27363 test_433() {
27364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27365
27366         [[ -n "$($LCTL list_param llite.*.inode_cache 2>/dev/null)" ]] ||
27367                 skip "inode cache not supported"
27368
27369         $LCTL set_param llite.*.inode_cache=0
27370         stack_trap "$LCTL set_param llite.*.inode_cache=1"
27371
27372         local count=256
27373         local before
27374         local after
27375
27376         cancel_lru_locks mdc
27377         test_mkdir $DIR/$tdir || error "mkdir $tdir"
27378         createmany -m $DIR/$tdir/f $count
27379         createmany -d $DIR/$tdir/d $count
27380         ls -l $DIR/$tdir > /dev/null
27381         stack_trap "rm -rf $DIR/$tdir"
27382
27383         before=$(num_objects)
27384         cancel_lru_locks mdc
27385         after=$(num_objects)
27386
27387         # sometimes even @before is less than 2 * count
27388         while (( before - after < count )); do
27389                 sleep 1
27390                 after=$(num_objects)
27391                 wait=$((wait + 1))
27392                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
27393                 if (( wait > 60 )); then
27394                         error "inode slab grew from $before to $after"
27395                 fi
27396         done
27397
27398         echo "lustre_inode_cache $before objs before lock cancel, $after after"
27399 }
27400 run_test 433 "ldlm lock cancel releases dentries and inodes"
27401
27402 prep_801() {
27403         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
27404         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
27405                 skip "Need server version at least 2.9.55"
27406
27407         start_full_debug_logging
27408 }
27409
27410 post_801() {
27411         stop_full_debug_logging
27412 }
27413
27414 barrier_stat() {
27415         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
27416                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
27417                            awk '/The barrier for/ { print $7 }')
27418                 echo $st
27419         else
27420                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
27421                 echo \'$st\'
27422         fi
27423 }
27424
27425 barrier_expired() {
27426         local expired
27427
27428         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
27429                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
27430                           awk '/will be expired/ { print $7 }')
27431         else
27432                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
27433         fi
27434
27435         echo $expired
27436 }
27437
27438 test_801a() {
27439         prep_801
27440
27441         echo "Start barrier_freeze at: $(date)"
27442         #define OBD_FAIL_BARRIER_DELAY          0x2202
27443         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
27444         # Do not reduce barrier time - See LU-11873
27445         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
27446
27447         sleep 2
27448         local b_status=$(barrier_stat)
27449         echo "Got barrier status at: $(date)"
27450         [ "$b_status" = "'freezing_p1'" ] ||
27451                 error "(1) unexpected barrier status $b_status"
27452
27453         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
27454         wait
27455         b_status=$(barrier_stat)
27456         [ "$b_status" = "'frozen'" ] ||
27457                 error "(2) unexpected barrier status $b_status"
27458
27459         local expired=$(barrier_expired)
27460         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
27461         sleep $((expired + 3))
27462
27463         b_status=$(barrier_stat)
27464         [ "$b_status" = "'expired'" ] ||
27465                 error "(3) unexpected barrier status $b_status"
27466
27467         # Do not reduce barrier time - See LU-11873
27468         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
27469                 error "(4) fail to freeze barrier"
27470
27471         b_status=$(barrier_stat)
27472         [ "$b_status" = "'frozen'" ] ||
27473                 error "(5) unexpected barrier status $b_status"
27474
27475         echo "Start barrier_thaw at: $(date)"
27476         #define OBD_FAIL_BARRIER_DELAY          0x2202
27477         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
27478         do_facet mgs $LCTL barrier_thaw $FSNAME &
27479
27480         sleep 2
27481         b_status=$(barrier_stat)
27482         echo "Got barrier status at: $(date)"
27483         [ "$b_status" = "'thawing'" ] ||
27484                 error "(6) unexpected barrier status $b_status"
27485
27486         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
27487         wait
27488         b_status=$(barrier_stat)
27489         [ "$b_status" = "'thawed'" ] ||
27490                 error "(7) unexpected barrier status $b_status"
27491
27492         #define OBD_FAIL_BARRIER_FAILURE        0x2203
27493         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
27494         do_facet mgs $LCTL barrier_freeze $FSNAME
27495
27496         b_status=$(barrier_stat)
27497         [ "$b_status" = "'failed'" ] ||
27498                 error "(8) unexpected barrier status $b_status"
27499
27500         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
27501         do_facet mgs $LCTL barrier_thaw $FSNAME
27502
27503         post_801
27504 }
27505 run_test 801a "write barrier user interfaces and stat machine"
27506
27507 test_801b() {
27508         prep_801
27509
27510         mkdir $DIR/$tdir || error "(1) fail to mkdir"
27511         createmany -d $DIR/$tdir/d 6 || error "(2) fail to mkdir"
27512         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
27513         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
27514         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
27515
27516         cancel_lru_locks mdc
27517
27518         # 180 seconds should be long enough
27519         do_facet mgs $LCTL barrier_freeze $FSNAME 180
27520
27521         local b_status=$(barrier_stat)
27522         [ "$b_status" = "'frozen'" ] ||
27523                 error "(6) unexpected barrier status $b_status"
27524
27525         mkdir $DIR/$tdir/d0/d10 &
27526         mkdir_pid=$!
27527
27528         touch $DIR/$tdir/d1/f13 &
27529         touch_pid=$!
27530
27531         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
27532         ln_pid=$!
27533
27534         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
27535         mv_pid=$!
27536
27537         rm -f $DIR/$tdir/d4/f12 &
27538         rm_pid=$!
27539
27540         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
27541
27542         # To guarantee taht the 'stat' is not blocked
27543         b_status=$(barrier_stat)
27544         [ "$b_status" = "'frozen'" ] ||
27545                 error "(8) unexpected barrier status $b_status"
27546
27547         # let above commands to run at background
27548         sleep 5
27549
27550         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
27551         ps -p $touch_pid || error "(10) touch should be blocked"
27552         ps -p $ln_pid || error "(11) link should be blocked"
27553         ps -p $mv_pid || error "(12) rename should be blocked"
27554         ps -p $rm_pid || error "(13) unlink should be blocked"
27555
27556         b_status=$(barrier_stat)
27557         [ "$b_status" = "'frozen'" ] ||
27558                 error "(14) unexpected barrier status $b_status"
27559
27560         do_facet mgs $LCTL barrier_thaw $FSNAME
27561         b_status=$(barrier_stat)
27562         [ "$b_status" = "'thawed'" ] ||
27563                 error "(15) unexpected barrier status $b_status"
27564
27565         wait $mkdir_pid || error "(16) mkdir should succeed"
27566         wait $touch_pid || error "(17) touch should succeed"
27567         wait $ln_pid || error "(18) link should succeed"
27568         wait $mv_pid || error "(19) rename should succeed"
27569         wait $rm_pid || error "(20) unlink should succeed"
27570
27571         post_801
27572 }
27573 run_test 801b "modification will be blocked by write barrier"
27574
27575 test_801c() {
27576         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27577
27578         prep_801
27579
27580         stop mds2 || error "(1) Fail to stop mds2"
27581
27582         do_facet mgs $LCTL barrier_freeze $FSNAME 30
27583
27584         local b_status=$(barrier_stat)
27585         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
27586                 do_facet mgs $LCTL barrier_thaw $FSNAME
27587                 error "(2) unexpected barrier status $b_status"
27588         }
27589
27590         do_facet mgs $LCTL barrier_rescan $FSNAME ||
27591                 error "(3) Fail to rescan barrier bitmap"
27592
27593         # Do not reduce barrier time - See LU-11873
27594         do_facet mgs $LCTL barrier_freeze $FSNAME 20
27595
27596         b_status=$(barrier_stat)
27597         [ "$b_status" = "'frozen'" ] ||
27598                 error "(4) unexpected barrier status $b_status"
27599
27600         do_facet mgs $LCTL barrier_thaw $FSNAME
27601         b_status=$(barrier_stat)
27602         [ "$b_status" = "'thawed'" ] ||
27603                 error "(5) unexpected barrier status $b_status"
27604
27605         local devname=$(mdsdevname 2)
27606
27607         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
27608
27609         do_facet mgs $LCTL barrier_rescan $FSNAME ||
27610                 error "(7) Fail to rescan barrier bitmap"
27611
27612         post_801
27613 }
27614 run_test 801c "rescan barrier bitmap"
27615
27616 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
27617 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
27618 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
27619 saved_MOUNT_OPTS=$MOUNT_OPTS
27620
27621 cleanup_802a() {
27622         trap 0
27623
27624         stopall
27625         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
27626         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
27627         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
27628         MOUNT_OPTS=$saved_MOUNT_OPTS
27629         setupall
27630 }
27631
27632 test_802a() {
27633         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
27634         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
27635         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
27636                 skip "Need server version at least 2.9.55"
27637
27638         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
27639
27640         mkdir $DIR/$tdir || error "(1) fail to mkdir"
27641
27642         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
27643                 error "(2) Fail to copy"
27644
27645         trap cleanup_802a EXIT
27646
27647         # sync by force before remount as readonly
27648         sync; sync_all_data; sleep 3; sync_all_data
27649
27650         stopall
27651
27652         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
27653         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
27654         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
27655
27656         echo "Mount the server as read only"
27657         setupall server_only || error "(3) Fail to start servers"
27658
27659         echo "Mount client without ro should fail"
27660         mount_client $MOUNT &&
27661                 error "(4) Mount client without 'ro' should fail"
27662
27663         echo "Mount client with ro should succeed"
27664         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
27665         mount_client $MOUNT ||
27666                 error "(5) Mount client with 'ro' should succeed"
27667
27668         echo "Modify should be refused"
27669         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
27670
27671         echo "Read should be allowed"
27672         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
27673                 error "(7) Read should succeed under ro mode"
27674
27675         cleanup_802a
27676 }
27677 run_test 802a "simulate readonly device"
27678
27679 test_802b() {
27680         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27681         remote_mds_nodsh && skip "remote MDS with nodsh"
27682
27683         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
27684                 skip "readonly option not available"
27685
27686         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
27687
27688         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
27689                 error "(2) Fail to copy"
27690
27691         # write back all cached data before setting MDT to readonly
27692         cancel_lru_locks
27693         sync_all_data
27694
27695         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
27696         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
27697
27698         echo "Modify should be refused"
27699         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
27700
27701         echo "Read should be allowed"
27702         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
27703                 error "(7) Read should succeed under ro mode"
27704
27705         # disable readonly
27706         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
27707 }
27708 run_test 802b "be able to set MDTs to readonly"
27709
27710 test_803a() {
27711         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27712         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
27713                 skip "MDS needs to be newer than 2.10.54"
27714
27715         mkdir_on_mdt0 $DIR/$tdir
27716         # Create some objects on all MDTs to trigger related logs objects
27717         for idx in $(seq $MDSCOUNT); do
27718                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
27719                         $DIR/$tdir/dir${idx} ||
27720                         error "Fail to create $DIR/$tdir/dir${idx}"
27721         done
27722
27723         sync; sleep 3
27724         wait_delete_completed # ensure old test cleanups are finished
27725         echo "before create:"
27726         $LFS df -i $MOUNT
27727         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
27728
27729         for i in {1..10}; do
27730                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
27731                         error "Fail to create $DIR/$tdir/foo$i"
27732         done
27733
27734         sync; sleep 3
27735         echo "after create:"
27736         $LFS df -i $MOUNT
27737         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
27738
27739         # allow for an llog to be cleaned up during the test
27740         [ $after_used -ge $((before_used + 10 - 1)) ] ||
27741                 error "before ($before_used) + 10 > after ($after_used)"
27742
27743         for i in {1..10}; do
27744                 rm -rf $DIR/$tdir/foo$i ||
27745                         error "Fail to remove $DIR/$tdir/foo$i"
27746         done
27747
27748         sleep 3 # avoid MDT return cached statfs
27749         wait_delete_completed
27750         echo "after unlink:"
27751         $LFS df -i $MOUNT
27752         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
27753
27754         # allow for an llog to be created during the test
27755         [ $after_used -le $((before_used + 1)) ] ||
27756                 error "after ($after_used) > before ($before_used) + 1"
27757 }
27758 run_test 803a "verify agent object for remote object"
27759
27760 test_803b() {
27761         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27762         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
27763                 skip "MDS needs to be newer than 2.13.56"
27764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27765
27766         for i in $(seq 0 $((MDSCOUNT - 1))); do
27767                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
27768         done
27769
27770         local before=0
27771         local after=0
27772
27773         local tmp
27774
27775         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
27776         for i in $(seq 0 $((MDSCOUNT - 1))); do
27777                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
27778                         awk '/getattr/ { print $2 }')
27779                 before=$((before + tmp))
27780         done
27781         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
27782         for i in $(seq 0 $((MDSCOUNT - 1))); do
27783                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
27784                         awk '/getattr/ { print $2 }')
27785                 after=$((after + tmp))
27786         done
27787
27788         [ $before -eq $after ] || error "getattr count $before != $after"
27789 }
27790 run_test 803b "remote object can getattr from cache"
27791
27792 test_804() {
27793         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27794         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
27795                 skip "MDS needs to be newer than 2.10.54"
27796         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
27797
27798         mkdir -p $DIR/$tdir
27799         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
27800                 error "Fail to create $DIR/$tdir/dir0"
27801
27802         local fid=$($LFS path2fid $DIR/$tdir/dir0)
27803         local dev=$(mdsdevname 2)
27804
27805         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27806                 grep ${fid} || error "NOT found agent entry for dir0"
27807
27808         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
27809                 error "Fail to create $DIR/$tdir/dir1"
27810
27811         touch $DIR/$tdir/dir1/foo0 ||
27812                 error "Fail to create $DIR/$tdir/dir1/foo0"
27813         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
27814         local rc=0
27815
27816         for idx in $(seq $MDSCOUNT); do
27817                 dev=$(mdsdevname $idx)
27818                 do_facet mds${idx} \
27819                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27820                         grep ${fid} && rc=$idx
27821         done
27822
27823         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
27824                 error "Fail to rename foo0 to foo1"
27825         if [ $rc -eq 0 ]; then
27826                 for idx in $(seq $MDSCOUNT); do
27827                         dev=$(mdsdevname $idx)
27828                         do_facet mds${idx} \
27829                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27830                         grep ${fid} && rc=$idx
27831                 done
27832         fi
27833
27834         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
27835                 error "Fail to rename foo1 to foo2"
27836         if [ $rc -eq 0 ]; then
27837                 for idx in $(seq $MDSCOUNT); do
27838                         dev=$(mdsdevname $idx)
27839                         do_facet mds${idx} \
27840                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27841                         grep ${fid} && rc=$idx
27842                 done
27843         fi
27844
27845         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
27846
27847         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
27848                 error "Fail to link to $DIR/$tdir/dir1/foo2"
27849         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
27850                 error "Fail to rename foo2 to foo0"
27851         unlink $DIR/$tdir/dir1/foo0 ||
27852                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
27853         rm -rf $DIR/$tdir/dir0 ||
27854                 error "Fail to rm $DIR/$tdir/dir0"
27855
27856         for idx in $(seq $MDSCOUNT); do
27857                 rc=0
27858
27859                 stop mds${idx}
27860                 dev=$(mdsdevname $idx)
27861                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
27862                         rc=$?
27863                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
27864                         error "mount mds$idx failed"
27865                 df $MOUNT > /dev/null 2>&1
27866
27867                 # e2fsck should not return error
27868                 [ $rc -eq 0 ] ||
27869                         error "e2fsck detected error on MDT${idx}: rc=$rc"
27870         done
27871 }
27872 run_test 804 "verify agent entry for remote entry"
27873
27874 cleanup_805() {
27875         do_facet $SINGLEMDS zfs set quota=$old $fsset
27876         unlinkmany $DIR/$tdir/f- 1000000
27877         trap 0
27878 }
27879
27880 test_805() {
27881         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
27882         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
27883         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
27884                 skip "netfree not implemented before 0.7"
27885         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
27886                 skip "Need MDS version at least 2.10.57"
27887
27888         local fsset
27889         local freekb
27890         local usedkb
27891         local old
27892         local quota
27893         local pref="osd-zfs.$FSNAME-MDT0000."
27894
27895         # limit available space on MDS dataset to meet nospace issue
27896         # quickly. then ZFS 0.7.2 can use reserved space if asked
27897         # properly (using netfree flag in osd_declare_destroy()
27898         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
27899         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
27900                 gawk '{print $3}')
27901         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
27902         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
27903         let "usedkb=usedkb-freekb"
27904         let "freekb=freekb/2"
27905         if let "freekb > 5000"; then
27906                 let "freekb=5000"
27907         fi
27908         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
27909         trap cleanup_805 EXIT
27910         mkdir_on_mdt0 $DIR/$tdir
27911         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
27912                 error "Can't set PFL layout"
27913         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
27914         rm -rf $DIR/$tdir || error "not able to remove"
27915         do_facet $SINGLEMDS zfs set quota=$old $fsset
27916         trap 0
27917 }
27918 run_test 805 "ZFS can remove from full fs"
27919
27920 # Size-on-MDS test
27921 check_lsom_data()
27922 {
27923         local file=$1
27924         local expect=$(stat -c %s $file)
27925
27926         check_lsom_size $1 $expect
27927
27928         local blocks=$($LFS getsom -b $file)
27929         expect=$(stat -c %b $file)
27930         [[ $blocks == $expect ]] ||
27931                 error "$file expected blocks: $expect, got: $blocks"
27932 }
27933
27934 check_lsom_size()
27935 {
27936         local size
27937         local expect=$2
27938
27939         cancel_lru_locks mdc
27940
27941         size=$($LFS getsom -s $1)
27942         [[ $size == $expect ]] ||
27943                 error "$file expected size: $expect, got: $size"
27944 }
27945
27946 test_806() {
27947         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
27948                 skip "Need MDS version at least 2.11.52"
27949
27950         local bs=1048576
27951
27952         touch $DIR/$tfile || error "touch $tfile failed"
27953
27954         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
27955         save_lustre_params client "llite.*.xattr_cache" > $save
27956         lctl set_param llite.*.xattr_cache=0
27957         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
27958
27959         # single-threaded write
27960         echo "Test SOM for single-threaded write"
27961         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
27962                 error "write $tfile failed"
27963         check_lsom_size $DIR/$tfile $bs
27964
27965         local num=32
27966         local size=$(($num * $bs))
27967         local offset=0
27968         local i
27969
27970         echo "Test SOM for single client multi-threaded($num) write"
27971         $TRUNCATE $DIR/$tfile 0
27972         for ((i = 0; i < $num; i++)); do
27973                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
27974                 local pids[$i]=$!
27975                 offset=$((offset + $bs))
27976         done
27977         for (( i=0; i < $num; i++ )); do
27978                 wait ${pids[$i]}
27979         done
27980         check_lsom_size $DIR/$tfile $size
27981
27982         $TRUNCATE $DIR/$tfile 0
27983         for ((i = 0; i < $num; i++)); do
27984                 offset=$((offset - $bs))
27985                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
27986                 local pids[$i]=$!
27987         done
27988         for (( i=0; i < $num; i++ )); do
27989                 wait ${pids[$i]}
27990         done
27991         check_lsom_size $DIR/$tfile $size
27992
27993         # multi-client writes
27994         num=$(get_node_count ${CLIENTS//,/ })
27995         size=$(($num * $bs))
27996         offset=0
27997         i=0
27998
27999         echo "Test SOM for multi-client ($num) writes"
28000         $TRUNCATE $DIR/$tfile 0
28001         for client in ${CLIENTS//,/ }; do
28002                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28003                 local pids[$i]=$!
28004                 i=$((i + 1))
28005                 offset=$((offset + $bs))
28006         done
28007         for (( i=0; i < $num; i++ )); do
28008                 wait ${pids[$i]}
28009         done
28010         check_lsom_size $DIR/$tfile $offset
28011
28012         i=0
28013         $TRUNCATE $DIR/$tfile 0
28014         for client in ${CLIENTS//,/ }; do
28015                 offset=$((offset - $bs))
28016                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28017                 local pids[$i]=$!
28018                 i=$((i + 1))
28019         done
28020         for (( i=0; i < $num; i++ )); do
28021                 wait ${pids[$i]}
28022         done
28023         check_lsom_size $DIR/$tfile $size
28024
28025         # verify truncate
28026         echo "Test SOM for truncate"
28027         $TRUNCATE $DIR/$tfile 1048576
28028         check_lsom_size $DIR/$tfile 1048576
28029         $TRUNCATE $DIR/$tfile 1234
28030         check_lsom_size $DIR/$tfile 1234
28031
28032         # verify SOM blocks count
28033         echo "Verify SOM block count"
28034         $TRUNCATE $DIR/$tfile 0
28035         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
28036                 error "failed to write file $tfile"
28037         check_lsom_data $DIR/$tfile
28038 }
28039 run_test 806 "Verify Lazy Size on MDS"
28040
28041 test_807() {
28042         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
28043         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
28044                 skip "Need MDS version at least 2.11.52"
28045
28046         # Registration step
28047         changelog_register || error "changelog_register failed"
28048         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
28049         changelog_users $SINGLEMDS | grep -q $cl_user ||
28050                 error "User $cl_user not found in changelog_users"
28051
28052         rm -rf $DIR/$tdir || error "rm $tdir failed"
28053         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
28054         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
28055         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
28056         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
28057                 error "truncate $tdir/trunc failed"
28058
28059         local bs=1048576
28060         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
28061                 error "write $tfile failed"
28062
28063         # multi-client wirtes
28064         local num=$(get_node_count ${CLIENTS//,/ })
28065         local offset=0
28066         local i=0
28067
28068         echo "Test SOM for multi-client ($num) writes"
28069         touch $DIR/$tfile || error "touch $tfile failed"
28070         $TRUNCATE $DIR/$tfile 0
28071         for client in ${CLIENTS//,/ }; do
28072                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28073                 local pids[$i]=$!
28074                 i=$((i + 1))
28075                 offset=$((offset + $bs))
28076         done
28077         for (( i=0; i < $num; i++ )); do
28078                 wait ${pids[$i]}
28079         done
28080
28081         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
28082         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
28083         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
28084         check_lsom_data $DIR/$tdir/trunc
28085         check_lsom_data $DIR/$tdir/single_dd
28086         check_lsom_data $DIR/$tfile
28087
28088         rm -rf $DIR/$tdir
28089         # Deregistration step
28090         changelog_deregister || error "changelog_deregister failed"
28091 }
28092 run_test 807 "verify LSOM syncing tool"
28093
28094 check_som_nologged()
28095 {
28096         local lines=$($LFS changelog $FSNAME-MDT0000 |
28097                 grep 'x=trusted.som' | wc -l)
28098         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
28099 }
28100
28101 test_808() {
28102         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
28103                 skip "Need MDS version at least 2.11.55"
28104
28105         # Registration step
28106         changelog_register || error "changelog_register failed"
28107
28108         touch $DIR/$tfile || error "touch $tfile failed"
28109         check_som_nologged
28110
28111         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
28112                 error "write $tfile failed"
28113         check_som_nologged
28114
28115         $TRUNCATE $DIR/$tfile 1234
28116         check_som_nologged
28117
28118         $TRUNCATE $DIR/$tfile 1048576
28119         check_som_nologged
28120
28121         # Deregistration step
28122         changelog_deregister || error "changelog_deregister failed"
28123 }
28124 run_test 808 "Check trusted.som xattr not logged in Changelogs"
28125
28126 check_som_nodata()
28127 {
28128         $LFS getsom $1
28129         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
28130 }
28131
28132 test_809() {
28133         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
28134                 skip "Need MDS version at least 2.11.56"
28135
28136         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
28137                 error "failed to create DoM-only file $DIR/$tfile"
28138         touch $DIR/$tfile || error "touch $tfile failed"
28139         check_som_nodata $DIR/$tfile
28140
28141         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
28142                 error "write $tfile failed"
28143         check_som_nodata $DIR/$tfile
28144
28145         $TRUNCATE $DIR/$tfile 1234
28146         check_som_nodata $DIR/$tfile
28147
28148         $TRUNCATE $DIR/$tfile 4097
28149         check_som_nodata $DIR/$file
28150 }
28151 run_test 809 "Verify no SOM xattr store for DoM-only files"
28152
28153 test_810() {
28154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28155         $GSS && skip_env "could not run with gss"
28156         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
28157                 skip "OST < 2.12.58 doesn't align checksum"
28158
28159         set_checksums 1
28160         stack_trap "set_checksums $ORIG_CSUM" EXIT
28161         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
28162
28163         local csum
28164         local before
28165         local after
28166         for csum in $CKSUM_TYPES; do
28167                 #define OBD_FAIL_OSC_NO_GRANT   0x411
28168                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
28169                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
28170                         eval set -- $i
28171                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
28172                         before=$(md5sum $DIR/$tfile)
28173                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
28174                         after=$(md5sum $DIR/$tfile)
28175                         [ "$before" == "$after" ] ||
28176                                 error "$csum: $before != $after bs=$1 seek=$2"
28177                 done
28178         done
28179 }
28180 run_test 810 "partial page writes on ZFS (LU-11663)"
28181
28182 test_812a() {
28183         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
28184                 skip "OST < 2.12.51 doesn't support this fail_loc"
28185
28186         $LFS setstripe -c 1 -i 0 $DIR/$tfile
28187         # ensure ost1 is connected
28188         stat $DIR/$tfile >/dev/null || error "can't stat"
28189         wait_osc_import_state client ost1 FULL
28190         # no locks, no reqs to let the connection idle
28191         cancel_lru_locks osc
28192
28193         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
28194 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
28195         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
28196         wait_osc_import_state client ost1 CONNECTING
28197         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
28198
28199         stat $DIR/$tfile >/dev/null || error "can't stat file"
28200 }
28201 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
28202
28203 test_812b() { # LU-12378
28204         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
28205                 skip "OST < 2.12.51 doesn't support this fail_loc"
28206
28207         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
28208         # ensure ost1 is connected
28209         stat $DIR/$tfile >/dev/null || error "can't stat"
28210         wait_osc_import_state client ost1 FULL
28211         # no locks, no reqs to let the connection idle
28212         cancel_lru_locks osc
28213
28214         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
28215 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
28216         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
28217         wait_osc_import_state client ost1 CONNECTING
28218         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
28219
28220         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
28221         wait_osc_import_state client ost1 IDLE
28222 }
28223 run_test 812b "do not drop no resend request for idle connect"
28224
28225 test_812c() {
28226         local old
28227
28228         old=$($LCTL get_param -n osc.*.idle_timeout | head -n 1)
28229
28230         $LFS setstripe -c 1 -o 0 $DIR/$tfile
28231         $LFS getstripe $DIR/$tfile
28232         $LCTL set_param osc.*.idle_timeout=10
28233         stack_trap "$LCTL set_param osc.*.idle_timeout=$old" EXIT
28234         # ensure ost1 is connected
28235         stat $DIR/$tfile >/dev/null || error "can't stat"
28236         wait_osc_import_state client ost1 FULL
28237         # no locks, no reqs to let the connection idle
28238         cancel_lru_locks osc
28239
28240 #define OBD_FAIL_PTLRPC_IDLE_RACE        0x533
28241         $LCTL set_param fail_loc=0x80000533
28242         sleep 15
28243         dd if=/dev/zero of=$DIR/$tfile count=1 conv=sync || error "dd failed"
28244 }
28245 run_test 812c "idle import vs lock enqueue race"
28246
28247 test_813() {
28248         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
28249         [ -z "$file_heat_sav" ] && skip "no file heat support"
28250
28251         local readsample
28252         local writesample
28253         local readbyte
28254         local writebyte
28255         local readsample1
28256         local writesample1
28257         local readbyte1
28258         local writebyte1
28259
28260         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
28261         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
28262
28263         $LCTL set_param -n llite.*.file_heat=1
28264         echo "Turn on file heat"
28265         echo "Period second: $period_second, Decay percentage: $decay_pct"
28266
28267         echo "QQQQ" > $DIR/$tfile
28268         echo "QQQQ" > $DIR/$tfile
28269         echo "QQQQ" > $DIR/$tfile
28270         cat $DIR/$tfile > /dev/null
28271         cat $DIR/$tfile > /dev/null
28272         cat $DIR/$tfile > /dev/null
28273         cat $DIR/$tfile > /dev/null
28274
28275         local out=$($LFS heat_get $DIR/$tfile)
28276
28277         $LFS heat_get $DIR/$tfile
28278         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28279         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28280         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28281         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28282
28283         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
28284         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
28285         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
28286         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
28287
28288         sleep $((period_second + 3))
28289         echo "Sleep $((period_second + 3)) seconds..."
28290         # The recursion formula to calculate the heat of the file f is as
28291         # follow:
28292         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
28293         # Where Hi is the heat value in the period between time points i*I and
28294         # (i+1)*I; Ci is the access count in the period; the symbol P refers
28295         # to the weight of Ci.
28296         out=$($LFS heat_get $DIR/$tfile)
28297         $LFS heat_get $DIR/$tfile
28298         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28299         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28300         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28301         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28302
28303         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
28304                 error "read sample ($readsample) is wrong"
28305         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
28306                 error "write sample ($writesample) is wrong"
28307         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
28308                 error "read bytes ($readbyte) is wrong"
28309         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
28310                 error "write bytes ($writebyte) is wrong"
28311
28312         echo "QQQQ" > $DIR/$tfile
28313         echo "QQQQ" > $DIR/$tfile
28314         echo "QQQQ" > $DIR/$tfile
28315         cat $DIR/$tfile > /dev/null
28316         cat $DIR/$tfile > /dev/null
28317         cat $DIR/$tfile > /dev/null
28318         cat $DIR/$tfile > /dev/null
28319
28320         sleep $((period_second + 3))
28321         echo "Sleep $((period_second + 3)) seconds..."
28322
28323         out=$($LFS heat_get $DIR/$tfile)
28324         $LFS heat_get $DIR/$tfile
28325         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28326         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28327         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28328         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28329
28330         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
28331                 4 * $decay_pct) / 100") -eq 1 ] ||
28332                 error "read sample ($readsample1) is wrong"
28333         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
28334                 3 * $decay_pct) / 100") -eq 1 ] ||
28335                 error "write sample ($writesample1) is wrong"
28336         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
28337                 20 * $decay_pct) / 100") -eq 1 ] ||
28338                 error "read bytes ($readbyte1) is wrong"
28339         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
28340                 15 * $decay_pct) / 100") -eq 1 ] ||
28341                 error "write bytes ($writebyte1) is wrong"
28342
28343         echo "Turn off file heat for the file $DIR/$tfile"
28344         $LFS heat_set -o $DIR/$tfile
28345
28346         echo "QQQQ" > $DIR/$tfile
28347         echo "QQQQ" > $DIR/$tfile
28348         echo "QQQQ" > $DIR/$tfile
28349         cat $DIR/$tfile > /dev/null
28350         cat $DIR/$tfile > /dev/null
28351         cat $DIR/$tfile > /dev/null
28352         cat $DIR/$tfile > /dev/null
28353
28354         out=$($LFS heat_get $DIR/$tfile)
28355         $LFS heat_get $DIR/$tfile
28356         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28357         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28358         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28359         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28360
28361         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
28362         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
28363         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
28364         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
28365
28366         echo "Trun on file heat for the file $DIR/$tfile"
28367         $LFS heat_set -O $DIR/$tfile
28368
28369         echo "QQQQ" > $DIR/$tfile
28370         echo "QQQQ" > $DIR/$tfile
28371         echo "QQQQ" > $DIR/$tfile
28372         cat $DIR/$tfile > /dev/null
28373         cat $DIR/$tfile > /dev/null
28374         cat $DIR/$tfile > /dev/null
28375         cat $DIR/$tfile > /dev/null
28376
28377         out=$($LFS heat_get $DIR/$tfile)
28378         $LFS heat_get $DIR/$tfile
28379         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28380         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28381         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28382         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28383
28384         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
28385         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
28386         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
28387         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
28388
28389         $LFS heat_set -c $DIR/$tfile
28390         $LCTL set_param -n llite.*.file_heat=0
28391         echo "Turn off file heat support for the Lustre filesystem"
28392
28393         echo "QQQQ" > $DIR/$tfile
28394         echo "QQQQ" > $DIR/$tfile
28395         echo "QQQQ" > $DIR/$tfile
28396         cat $DIR/$tfile > /dev/null
28397         cat $DIR/$tfile > /dev/null
28398         cat $DIR/$tfile > /dev/null
28399         cat $DIR/$tfile > /dev/null
28400
28401         out=$($LFS heat_get $DIR/$tfile)
28402         $LFS heat_get $DIR/$tfile
28403         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28404         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28405         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28406         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28407
28408         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
28409         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
28410         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
28411         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
28412
28413         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
28414         rm -f $DIR/$tfile
28415 }
28416 run_test 813 "File heat verfication"
28417
28418 test_814()
28419 {
28420         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
28421         echo -n y >> $DIR/$tfile
28422         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
28423         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
28424 }
28425 run_test 814 "sparse cp works as expected (LU-12361)"
28426
28427 test_815()
28428 {
28429         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
28430         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
28431 }
28432 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
28433
28434 test_816() {
28435         local ost1_imp=$(get_osc_import_name client ost1)
28436         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
28437                          cut -d'.' -f2)
28438
28439         $LFS setstripe -c 1 -i 0 $DIR/$tfile
28440         # ensure ost1 is connected
28441
28442         stat $DIR/$tfile >/dev/null || error "can't stat"
28443         wait_osc_import_state client ost1 FULL
28444         # no locks, no reqs to let the connection idle
28445         cancel_lru_locks osc
28446         lru_resize_disable osc
28447         local before
28448         local now
28449         before=$($LCTL get_param -n \
28450                  ldlm.namespaces.$imp_name.lru_size)
28451
28452         wait_osc_import_state client ost1 IDLE
28453         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
28454         now=$($LCTL get_param -n \
28455               ldlm.namespaces.$imp_name.lru_size)
28456         [ $before == $now ] || error "lru_size changed $before != $now"
28457 }
28458 run_test 816 "do not reset lru_resize on idle reconnect"
28459
28460 cleanup_817() {
28461         umount $tmpdir
28462         exportfs -u localhost:$DIR/nfsexp
28463         rm -rf $DIR/nfsexp
28464 }
28465
28466 test_817() {
28467         systemctl restart nfs-server.service || skip "failed to restart nfsd"
28468
28469         mkdir -p $DIR/nfsexp
28470         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
28471                 error "failed to export nfs"
28472
28473         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
28474         stack_trap cleanup_817 EXIT
28475
28476         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
28477                 error "failed to mount nfs to $tmpdir"
28478
28479         cp /bin/true $tmpdir
28480         $DIR/nfsexp/true || error "failed to execute 'true' command"
28481 }
28482 run_test 817 "nfsd won't cache write lock for exec file"
28483
28484 test_818() {
28485         test_mkdir -i0 -c1 $DIR/$tdir
28486         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
28487         $LFS setstripe -c1 -i1 $DIR/$tdir/$tfile
28488         stop $SINGLEMDS
28489
28490         # restore osp-syn threads
28491         stack_trap "fail $SINGLEMDS"
28492
28493         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
28494         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
28495         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
28496                 error "start $SINGLEMDS failed"
28497         rm -rf $DIR/$tdir
28498
28499         local testid=$(echo $TESTNAME | tr '_' ' ')
28500
28501         do_facet mds1 dmesg | tac | sed "/$testid/,$ d" |
28502                 grep "run LFSCK" || error "run LFSCK is not suggested"
28503 }
28504 run_test 818 "unlink with failed llog"
28505
28506 test_819a() {
28507         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
28508         cancel_lru_locks osc
28509         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
28510         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
28511         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
28512         rm -f $TDIR/$tfile
28513 }
28514 run_test 819a "too big niobuf in read"
28515
28516 test_819b() {
28517         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
28518         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
28519         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
28520         cancel_lru_locks osc
28521         sleep 1
28522         rm -f $TDIR/$tfile
28523 }
28524 run_test 819b "too big niobuf in write"
28525
28526
28527 function test_820_start_ost() {
28528         sleep 5
28529
28530         for num in $(seq $OSTCOUNT); do
28531                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
28532         done
28533 }
28534
28535 test_820() {
28536         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
28537
28538         mkdir $DIR/$tdir
28539         umount_client $MOUNT || error "umount failed"
28540         for num in $(seq $OSTCOUNT); do
28541                 stop ost$num
28542         done
28543
28544         # mount client with no active OSTs
28545         # so that the client can't initialize max LOV EA size
28546         # from OSC notifications
28547         mount_client $MOUNT || error "mount failed"
28548         # delay OST starting to keep this 0 max EA size for a while
28549         test_820_start_ost &
28550
28551         # create a directory on MDS2
28552         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
28553                 error "Failed to create directory"
28554         # open intent should update default EA size
28555         # see mdc_update_max_ea_from_body()
28556         # notice this is the very first RPC to MDS2
28557         out=$(cp /etc/services $DIR/$tdir/mds2 2>&1)
28558         ret=$?
28559         echo $out
28560         # With SSK, this situation can lead to -EPERM being returned.
28561         # In that case, simply retry.
28562         if [ $ret -ne 0 ] && $SHARED_KEY; then
28563                 if echo "$out" | grep -q "not permitted"; then
28564                         cp /etc/services $DIR/$tdir/mds2
28565                         ret=$?
28566                 fi
28567         fi
28568         [ $ret -eq 0 ] || error "Failed to copy files to mds$n"
28569 }
28570 run_test 820 "update max EA from open intent"
28571
28572 test_823() {
28573         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
28574         local OST_MAX_PRECREATE=20000
28575
28576         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
28577                 skip "Need MDS version at least 2.14.56"
28578
28579         save_lustre_params mds1 \
28580                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
28581         do_facet $SINGLEMDS "$LCTL set_param -n \
28582                 osp.$FSNAME-OST*MDT0000.max_create_count=0"
28583         do_facet $SINGLEMDS "$LCTL set_param -n \
28584                 osp.$FSNAME-OST0000*MDT0000.max_create_count=$OST_MAX_PRECREATE"
28585
28586         stack_trap "restore_lustre_params < $p; rm $p"
28587
28588         do_facet $SINGLEMDS "$LCTL set_param -n \
28589                 osp.$FSNAME-OST*-osc-MDT*.create_count=100200"
28590
28591         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
28592                       osp.$FSNAME-OST0000*MDT0000.create_count")
28593         local max=$(do_facet $SINGLEMDS "$LCTL get_param -n \
28594                     osp.$FSNAME-OST0000*MDT0000.max_create_count")
28595         local expect_count=$(((($max/2)/256) * 256))
28596
28597         log "setting create_count to 100200:"
28598         log " -result- count: $count with max: $max, expecting: $expect_count"
28599
28600         [[ $count -eq expect_count ]] ||
28601                 error "Create count not set to max precreate."
28602 }
28603 run_test 823 "Setting create_count > OST_MAX_PRECREATE is lowered to maximum"
28604
28605 test_831() {
28606         [[ $MDS1_VERSION -lt $(version_code 2.14.56) ]] &&
28607                 skip "Need MDS version 2.14.56"
28608
28609         local sync_changes=$(do_facet $SINGLEMDS \
28610                 $LCTL get_param -n osp.$FSNAME-OST0000-osc-MDT0000.sync_changes)
28611
28612         [ "$sync_changes" -gt 100 ] &&
28613                 skip "Sync changes $sync_changes > 100 already"
28614
28615         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
28616
28617         $LFS mkdir -i 0 $DIR/$tdir
28618         $LFS setstripe -c 1 -i 0 $DIR/$tdir
28619
28620         save_lustre_params mds1 \
28621                 "osp.$FSNAME-OST*-osc-MDT0000.max_sync_changes" > $p
28622         save_lustre_params mds1 \
28623                 "osp.$FSNAME-OST*-osc-MDT0000.max_rpcs_in_progress" >> $p
28624
28625         do_facet mds1 "$LCTL set_param -n \
28626                 osp.$FSNAME-OST*-osc-MDT0000.max_sync_changes=100 \
28627                 osp.$FSNAME-OST*-osc-MDT0000.max_rpcs_in_progress=128"
28628         stack_trap "restore_lustre_params < $p" EXIT
28629
28630         createmany -o $DIR/$tdir/f- 1000
28631         unlinkmany $DIR/$tdir/f- 1000 &
28632         local UNLINK_PID=$!
28633
28634         while sleep 1; do
28635                 sync_changes=$(do_facet mds1 \
28636                 $LCTL get_param -n osp.$FSNAME-OST0000-osc-MDT0000.sync_changes)
28637                 # the check in the code is racy, fail the test
28638                 # if the value above the limit by 10.
28639                 [ $sync_changes -gt 110 ] && {
28640                         kill -2 $UNLINK_PID
28641                         wait
28642                         error "osp changes throttling failed, $sync_changes>110"
28643                 }
28644                 kill -0 $UNLINK_PID 2> /dev/null || break
28645         done
28646         wait
28647 }
28648 run_test 831 "throttling unlink/setattr queuing on OSP"
28649
28650 #
28651 # tests that do cleanup/setup should be run at the end
28652 #
28653
28654 test_900() {
28655         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28656         local ls
28657
28658         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
28659         $LCTL set_param fail_loc=0x903
28660
28661         cancel_lru_locks MGC
28662
28663         FAIL_ON_ERROR=true cleanup
28664         FAIL_ON_ERROR=true setup
28665 }
28666 run_test 900 "umount should not race with any mgc requeue thread"
28667
28668 # LUS-6253/LU-11185
28669 test_901() {
28670         local old
28671         local count
28672         local oldc
28673         local newc
28674         local olds
28675         local news
28676         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28677
28678         # some get_param have a bug to handle dot in param name
28679         cancel_lru_locks MGC
28680         old=$(mount -t lustre | wc -l)
28681         # 1 config+sptlrpc
28682         # 2 params
28683         # 3 nodemap
28684         # 4 IR
28685         old=$((old * 4))
28686         oldc=0
28687         count=0
28688         while [ $old -ne $oldc ]; do
28689                 oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
28690                 sleep 1
28691                 ((count++))
28692                 if [ $count -ge $TIMEOUT ]; then
28693                         error "too large timeout"
28694                 fi
28695         done
28696         umount_client $MOUNT || error "umount failed"
28697         mount_client $MOUNT || error "mount failed"
28698         cancel_lru_locks MGC
28699         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
28700
28701         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
28702
28703         return 0
28704 }
28705 run_test 901 "don't leak a mgc lock on client umount"
28706
28707 # LU-13377
28708 test_902() {
28709         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
28710                 skip "client does not have LU-13377 fix"
28711         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
28712         $LCTL set_param fail_loc=0x1415
28713         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
28714         cancel_lru_locks osc
28715         rm -f $DIR/$tfile
28716 }
28717 run_test 902 "test short write doesn't hang lustre"
28718
28719 # LU-14711
28720 test_903() {
28721         $LFS setstripe -i 0 -c 1 $DIR/$tfile $DIR/${tfile}-2
28722         echo "blah" > $DIR/${tfile}-2
28723         dd if=/dev/zero of=$DIR/$tfile bs=1M count=6 conv=fsync
28724         #define OBD_FAIL_OSC_SLOW_PAGE_EVICT 0x417
28725         $LCTL set_param fail_loc=0x417 fail_val=20
28726
28727         mv $DIR/${tfile}-2 $DIR/$tfile # Destroys the big object
28728         sleep 1 # To start the destroy
28729         wait_destroy_complete 150 || error "Destroy taking too long"
28730         cat $DIR/$tfile > /dev/null || error "Evicted"
28731 }
28732 run_test 903 "Test long page discard does not cause evictions"
28733
28734 test_904() {
28735         [ "$mds1_FSTYPE" == "ldiskfs" ] || skip "ldiskfs only test"
28736         do_facet mds1 $DEBUGFS -R features $(mdsdevname 1) |
28737                 grep -q project || skip "skip project quota not supported"
28738
28739         local testfile="$DIR/$tdir/$tfile"
28740         local xattr="trusted.projid"
28741         local projid
28742         local mdts=$(comma_list $(mdts_nodes))
28743         local saved=$(do_facet mds1 $LCTL get_param -n \
28744                 osd-ldiskfs.*MDT0000.enable_projid_xattr)
28745
28746         do_nodes $mdts $LCTL set_param osd-ldiskfs.*MDT*.enable_projid_xattr=0
28747         stack_trap "do_nodes $mdts $LCTL set_param \
28748                 osd-ldiskfs.*MDT*.enable_projid_xattr=$saved"
28749
28750         mkdir -p $DIR/$tdir
28751         touch $testfile
28752         #hide projid xattr on server
28753         $LFS project -p 1 $testfile ||
28754                 error "set $testfile project id failed"
28755         getfattr -m - $testfile | grep $xattr &&
28756                 error "do not show trusted.projid when disabled on server"
28757         do_nodes $mdts $LCTL set_param osd-ldiskfs.*MDT*.enable_projid_xattr=1
28758         #should be hidden when projid is 0
28759         $LFS project -p 0 $testfile ||
28760                 error "set $testfile project id failed"
28761         getfattr -m - $testfile | grep $xattr &&
28762                 error "do not show trusted.projid with project ID 0"
28763
28764         #still can getxattr explicitly
28765         projid=$(getfattr -n $xattr $testfile |
28766                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
28767         [ $projid == "0" ] ||
28768                 error "projid expected 0 not $projid"
28769
28770         #set the projid via setxattr
28771         setfattr -n $xattr -v "1000" $testfile ||
28772                 error "setattr failed with $?"
28773         projid=($($LFS project $testfile))
28774         [ ${projid[0]} == "1000" ] ||
28775                 error "projid expected 1000 not $projid"
28776
28777         #check the new projid via getxattr
28778         $LFS project -p 1001 $testfile ||
28779                 error "set $testfile project id failed"
28780         getfattr -m - $testfile | grep $xattr ||
28781                 error "should show trusted.projid when project ID != 0"
28782         projid=$(getfattr -n $xattr $testfile |
28783                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
28784         [ $projid == "1001" ] ||
28785                 error "projid expected 1001 not $projid"
28786
28787         #try to set invalid projid
28788         setfattr -n $xattr -v "4294967295" $testfile &&
28789                 error "set invalid projid should fail"
28790
28791         #remove the xattr means setting projid to 0
28792         setfattr -x $xattr $testfile ||
28793                 error "setfattr failed with $?"
28794         projid=($($LFS project $testfile))
28795         [ ${projid[0]} == "0" ] ||
28796                 error "projid expected 0 not $projid"
28797
28798         #should be hidden when parent has inherit flag and same projid
28799         $LFS project -srp 1002 $DIR/$tdir ||
28800                 error "set $tdir project id failed"
28801         getfattr -m - $testfile | grep $xattr &&
28802                 error "do not show trusted.projid with inherit flag"
28803
28804         #still can getxattr explicitly
28805         projid=$(getfattr -n $xattr $testfile |
28806                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
28807         [ $projid == "1002" ] ||
28808                 error "projid expected 1002 not $projid"
28809 }
28810 run_test 904 "virtual project ID xattr"
28811
28812 # LU-8582
28813 test_905() {
28814         (( $OST1_VERSION >= $(version_code 2.8.54) )) ||
28815                 skip "lustre < 2.8.54 does not support ladvise"
28816
28817         remote_ost_nodsh && skip "remote OST with nodsh"
28818         $LFS setstripe -c -1 -i 0 $DIR/$tfile || error "setstripe failed"
28819
28820         $LFS ladvise -a willread $DIR/$tfile || error "ladvise does not work"
28821
28822         #define OBD_FAIL_OST_OPCODE 0x253
28823         # OST_LADVISE = 21
28824         do_facet ost1 "$LCTL set_param fail_val=21 fail_loc=0x0253"
28825         $LFS ladvise -a willread $DIR/$tfile &&
28826                 error "unexpected success of ladvise with fault injection"
28827         $LFS ladvise -a willread $DIR/$tfile |&
28828                 grep -q "Operation not supported"
28829         (( $? == 0 )) || error "unexpected stderr of ladvise with fault injection"
28830 }
28831 run_test 905 "bad or new opcode should not stuck client"
28832
28833 test_906() {
28834         grep -q io_uring_setup /proc/kallsyms ||
28835                 skip "Client OS does not support io_uring I/O engine"
28836         io_uring_probe || skip "kernel does not support io_uring fully"
28837         which fio || skip_env "no fio installed"
28838         fio --enghelp | grep -q io_uring ||
28839                 skip_env "fio does not support io_uring I/O engine"
28840
28841         local file=$DIR/$tfile
28842         local ioengine="io_uring"
28843         local numjobs=2
28844         local size=50M
28845
28846         fio --name=seqwrite --ioengine=$ioengine        \
28847                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
28848                 --iodepth=64 --size=$size --filename=$file --rw=write ||
28849                 error "fio seqwrite $file failed"
28850
28851         fio --name=seqread --ioengine=$ioengine \
28852                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
28853                 --iodepth=64 --size=$size --filename=$file --rw=read ||
28854                 error "fio seqread $file failed"
28855
28856         rm -f $file || error "rm -f $file failed"
28857 }
28858 run_test 906 "Simple test for io_uring I/O engine via fio"
28859
28860 complete $SECONDS
28861 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
28862 check_and_cleanup_lustre
28863 if [ "$I_MOUNTED" != "yes" ]; then
28864         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
28865 fi
28866 exit_status