Whamcloud - gitweb
LU-14736 utils: update leak-finder.pl for new format
[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         # the number of "bad" hashes is random, as it depends on the random
4343         # filenames generated by "mktemp".  Allow some margin in the results.
4344         echo "$((same/${#patterns[*]}))/$count matches, expect ~$expect for $1"
4345         (( same / ${#patterns[*]} <= expect * 9 / 7 &&
4346            same / ${#patterns[*]} > expect * 5 / 7 )) ||
4347                 error "MDT index match $((same / ${#patterns[*]}))/$count times"
4348         same=0
4349
4350         # crush2 doesn't put suffixes with special characters on the same MDT
4351         # filename like $tfile.txt.1234 should *not* be considered temp
4352         for pattern in ${patterns[*]}; do
4353                 local base=${pattern%%X*}
4354                 local suff=${pattern#$base}
4355
4356                 pattern=$base...${suff/XXX}
4357                 echo "pattern=$pattern"
4358                 for (( i = 0; i < $count; i++ )); do
4359                         fname=$(mktemp $DIR/$tdir/$pattern) ||
4360                                 error "touch $fname failed"
4361                         index2=$($LFS getstripe -m $fname)
4362                         (( $index != $index2 )) && continue
4363
4364                         same=$((same + 1))
4365                 done
4366         done
4367
4368         # the number of "bad" hashes is random, as it depends on the random
4369         # filenames generated by "mktemp".  Allow some margin in the results.
4370         echo "$((same/${#patterns[*]}))/$count matches, expect ~$expect for $1"
4371         (( same / ${#patterns[*]} <= expect * 9 / 7 &&
4372            same / ${#patterns[*]} > expect * 5 / 7 )) ||
4373                 error "MDT index match $((same / ${#patterns[*]}))/$count times"
4374 }
4375
4376 test_33h() {
4377         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4378         (( $MDS1_VERSION >= $(version_code 2.13.50) )) ||
4379                 skip "Need MDS version at least 2.13.50"
4380
4381         sub_33h crush
4382 }
4383 run_test 33h "temp file is located on the same MDT as target (crush)"
4384
4385 test_33hh() {
4386         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
4387         echo "MDS1_VERSION=$MDS1_VERSION version_code=$(version_code 2.15.0)"
4388         (( $MDS1_VERSION > $(version_code 2.15.0) )) ||
4389                 skip "Need MDS version at least 2.15.0 for crush2"
4390
4391         sub_33h crush2
4392 }
4393 run_test 33hh "temp file is located on the same MDT as target (crush2)"
4394
4395 test_33i()
4396 {
4397         (( MDSCOUNT < 2 )) && skip "needs >= 2 MDTs"
4398
4399         local FNAME=$(str_repeat 'f' 250)
4400
4401         test_mkdir -i 0 -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
4402         createmany -o $DIR/$tdir/$FNAME 1000 || error "createmany failed"
4403
4404         local count
4405         local total
4406
4407         count=$($LFS getstripe -m $DIR/$tdir/* | grep -cw 1)
4408
4409         local MDC=$(lctl dl | awk '/MDT0001-mdc-[^M]/ { print $4 }')
4410
4411         lctl --device %$MDC deactivate
4412         stack_trap "lctl --device %$MDC activate"
4413         ls $DIR/$tdir > /dev/null && error "ls should return an error"
4414         total=$(\ls -l $DIR/$tdir | wc -l)
4415         # "ls -l" will list total in the first line
4416         total=$((total - 1))
4417         (( total + count == 1000 )) ||
4418                 error "ls list $total files, $count files on MDT1"
4419 }
4420 run_test 33i "striped directory can be accessed when one MDT is down"
4421
4422 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
4423 test_34a() {
4424         rm -f $DIR/f34
4425         $MCREATE $DIR/f34 || error "mcreate failed"
4426         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4427                 error "getstripe failed"
4428         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
4429         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4430                 error "getstripe failed"
4431         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4432                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4433 }
4434 run_test 34a "truncate file that has not been opened ==========="
4435
4436 test_34b() {
4437         [ ! -f $DIR/f34 ] && test_34a
4438         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4439                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4440         $OPENFILE -f O_RDONLY $DIR/f34
4441         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
4442                 error "getstripe failed"
4443         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4444                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4445 }
4446 run_test 34b "O_RDONLY opening file doesn't create objects ====="
4447
4448 test_34c() {
4449         [ ! -f $DIR/f34 ] && test_34a
4450         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4451                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4452         $OPENFILE -f O_RDWR $DIR/f34
4453         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
4454                 error "$LFS getstripe failed"
4455         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4456                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4457 }
4458 run_test 34c "O_RDWR opening file-with-size works =============="
4459
4460 test_34d() {
4461         [ ! -f $DIR/f34 ] && test_34a
4462         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
4463                 error "dd failed"
4464         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
4465                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
4466         rm $DIR/f34
4467 }
4468 run_test 34d "write to sparse file ============================="
4469
4470 test_34e() {
4471         rm -f $DIR/f34e
4472         $MCREATE $DIR/f34e || error "mcreate failed"
4473         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
4474         $CHECKSTAT -s 1000 $DIR/f34e ||
4475                 error "Size of $DIR/f34e not equal to 1000 bytes"
4476         $OPENFILE -f O_RDWR $DIR/f34e
4477         $CHECKSTAT -s 1000 $DIR/f34e ||
4478                 error "Size of $DIR/f34e not equal to 1000 bytes"
4479 }
4480 run_test 34e "create objects, some with size and some without =="
4481
4482 test_34f() { # bug 6242, 6243
4483         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4484
4485         SIZE34F=48000
4486         rm -f $DIR/f34f
4487         $MCREATE $DIR/f34f || error "mcreate failed"
4488         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
4489         dd if=$DIR/f34f of=$TMP/f34f
4490         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
4491         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
4492         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
4493         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
4494         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
4495 }
4496 run_test 34f "read from a file with no objects until EOF ======="
4497
4498 test_34g() {
4499         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4500
4501         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
4502                 error "dd failed"
4503         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
4504         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4505                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
4506         cancel_lru_locks osc
4507         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
4508                 error "wrong size after lock cancel"
4509
4510         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
4511         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4512                 error "expanding truncate failed"
4513         cancel_lru_locks osc
4514         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
4515                 error "wrong expanded size after lock cancel"
4516 }
4517 run_test 34g "truncate long file ==============================="
4518
4519 test_34h() {
4520         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4521
4522         local gid=10
4523         local sz=1000
4524
4525         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
4526         sync # Flush the cache so that multiop below does not block on cache
4527              # flush when getting the group lock
4528         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
4529         MULTIPID=$!
4530
4531         # Since just timed wait is not good enough, let's do a sync write
4532         # that way we are sure enough time for a roundtrip + processing
4533         # passed + 2 seconds of extra margin.
4534         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
4535         rm $DIR/${tfile}-1
4536         sleep 2
4537
4538         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
4539                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
4540                 kill -9 $MULTIPID
4541         fi
4542         wait $MULTIPID
4543         local nsz=`stat -c %s $DIR/$tfile`
4544         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
4545 }
4546 run_test 34h "ftruncate file under grouplock should not block"
4547
4548 test_35a() {
4549         cp /bin/sh $DIR/f35a
4550         chmod 444 $DIR/f35a
4551         chown $RUNAS_ID $DIR/f35a
4552         $RUNAS $DIR/f35a && error || true
4553         rm $DIR/f35a
4554 }
4555 run_test 35a "exec file with mode 444 (should return and not leak)"
4556
4557 test_36a() {
4558         rm -f $DIR/f36
4559         utime $DIR/f36 || error "utime failed for MDS"
4560 }
4561 run_test 36a "MDS utime check (mknod, utime)"
4562
4563 test_36b() {
4564         echo "" > $DIR/f36
4565         utime $DIR/f36 || error "utime failed for OST"
4566 }
4567 run_test 36b "OST utime check (open, utime)"
4568
4569 test_36c() {
4570         rm -f $DIR/d36/f36
4571         test_mkdir $DIR/d36
4572         chown $RUNAS_ID $DIR/d36
4573         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
4574 }
4575 run_test 36c "non-root MDS utime check (mknod, utime)"
4576
4577 test_36d() {
4578         [ ! -d $DIR/d36 ] && test_36c
4579         echo "" > $DIR/d36/f36
4580         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
4581 }
4582 run_test 36d "non-root OST utime check (open, utime)"
4583
4584 test_36e() {
4585         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
4586
4587         test_mkdir $DIR/$tdir
4588         touch $DIR/$tdir/$tfile
4589         $RUNAS utime $DIR/$tdir/$tfile &&
4590                 error "utime worked, expected failure" || true
4591 }
4592 run_test 36e "utime on non-owned file (should return error)"
4593
4594 subr_36fh() {
4595         local fl="$1"
4596         local LANG_SAVE=$LANG
4597         local LC_LANG_SAVE=$LC_LANG
4598         export LANG=C LC_LANG=C # for date language
4599
4600         DATESTR="Dec 20  2000"
4601         test_mkdir $DIR/$tdir
4602         lctl set_param fail_loc=$fl
4603         date; date +%s
4604         cp /etc/hosts $DIR/$tdir/$tfile
4605         sync & # write RPC generated with "current" inode timestamp, but delayed
4606         sleep 1
4607         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4608         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4609         cancel_lru_locks $OSC
4610         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4611         date; date +%s
4612         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4613                 echo "BEFORE: $LS_BEFORE" && \
4614                 echo "AFTER : $LS_AFTER" && \
4615                 echo "WANT  : $DATESTR" && \
4616                 error "$DIR/$tdir/$tfile timestamps changed" || true
4617
4618         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4619 }
4620
4621 test_36f() {
4622         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4623
4624         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4625         subr_36fh "0x80000214"
4626 }
4627 run_test 36f "utime on file racing with OST BRW write =========="
4628
4629 test_36g() {
4630         remote_ost_nodsh && skip "remote OST with nodsh"
4631         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4632         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4633                 skip "Need MDS version at least 2.12.51"
4634
4635         local fmd_max_age
4636         local fmd
4637         local facet="ost1"
4638         local tgt="obdfilter"
4639
4640         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4641
4642         test_mkdir $DIR/$tdir
4643         fmd_max_age=$(do_facet $facet \
4644                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4645                 head -n 1")
4646
4647         echo "FMD max age: ${fmd_max_age}s"
4648         touch $DIR/$tdir/$tfile
4649         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4650                 gawk '{cnt=cnt+$1}  END{print cnt}')
4651         echo "FMD before: $fmd"
4652         [[ $fmd == 0 ]] &&
4653                 error "FMD wasn't create by touch"
4654         sleep $((fmd_max_age + 12))
4655         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4656                 gawk '{cnt=cnt+$1}  END{print cnt}')
4657         echo "FMD after: $fmd"
4658         [[ $fmd == 0 ]] ||
4659                 error "FMD wasn't expired by ping"
4660 }
4661 run_test 36g "FMD cache expiry ====================="
4662
4663 test_36h() {
4664         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4665
4666         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4667         subr_36fh "0x80000227"
4668 }
4669 run_test 36h "utime on file racing with OST BRW write =========="
4670
4671 test_36i() {
4672         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4673
4674         test_mkdir $DIR/$tdir
4675         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4676
4677         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4678         local new_mtime=$((mtime + 200))
4679
4680         #change Modify time of striped dir
4681         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4682                         error "change mtime failed"
4683
4684         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4685
4686         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4687 }
4688 run_test 36i "change mtime on striped directory"
4689
4690 # test_37 - duplicate with tests 32q 32r
4691
4692 test_38() {
4693         local file=$DIR/$tfile
4694         touch $file
4695         openfile -f O_DIRECTORY $file
4696         local RC=$?
4697         local ENOTDIR=20
4698         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4699         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4700 }
4701 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4702
4703 test_39a() { # was test_39
4704         touch $DIR/$tfile
4705         touch $DIR/${tfile}2
4706 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4707 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4708 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4709         sleep 2
4710         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4711         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4712                 echo "mtime"
4713                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4714                 echo "atime"
4715                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4716                 echo "ctime"
4717                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4718                 error "O_TRUNC didn't change timestamps"
4719         fi
4720 }
4721 run_test 39a "mtime changed on create"
4722
4723 test_39b() {
4724         test_mkdir -c1 $DIR/$tdir
4725         cp -p /etc/passwd $DIR/$tdir/fopen
4726         cp -p /etc/passwd $DIR/$tdir/flink
4727         cp -p /etc/passwd $DIR/$tdir/funlink
4728         cp -p /etc/passwd $DIR/$tdir/frename
4729         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4730
4731         sleep 1
4732         echo "aaaaaa" >> $DIR/$tdir/fopen
4733         echo "aaaaaa" >> $DIR/$tdir/flink
4734         echo "aaaaaa" >> $DIR/$tdir/funlink
4735         echo "aaaaaa" >> $DIR/$tdir/frename
4736
4737         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4738         local link_new=`stat -c %Y $DIR/$tdir/flink`
4739         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4740         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4741
4742         cat $DIR/$tdir/fopen > /dev/null
4743         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4744         rm -f $DIR/$tdir/funlink2
4745         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4746
4747         for (( i=0; i < 2; i++ )) ; do
4748                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4749                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4750                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4751                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4752
4753                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4754                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4755                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4756                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4757
4758                 cancel_lru_locks $OSC
4759                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4760         done
4761 }
4762 run_test 39b "mtime change on open, link, unlink, rename  ======"
4763
4764 # this should be set to past
4765 TEST_39_MTIME=`date -d "1 year ago" +%s`
4766
4767 # bug 11063
4768 test_39c() {
4769         touch $DIR1/$tfile
4770         sleep 2
4771         local mtime0=`stat -c %Y $DIR1/$tfile`
4772
4773         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4774         local mtime1=`stat -c %Y $DIR1/$tfile`
4775         [ "$mtime1" = $TEST_39_MTIME ] || \
4776                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4777
4778         local d1=`date +%s`
4779         echo hello >> $DIR1/$tfile
4780         local d2=`date +%s`
4781         local mtime2=`stat -c %Y $DIR1/$tfile`
4782         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4783                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4784
4785         mv $DIR1/$tfile $DIR1/$tfile-1
4786
4787         for (( i=0; i < 2; i++ )) ; do
4788                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4789                 [ "$mtime2" = "$mtime3" ] || \
4790                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4791
4792                 cancel_lru_locks $OSC
4793                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4794         done
4795 }
4796 run_test 39c "mtime change on rename ==========================="
4797
4798 # bug 21114
4799 test_39d() {
4800         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4801
4802         touch $DIR1/$tfile
4803         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4804
4805         for (( i=0; i < 2; i++ )) ; do
4806                 local mtime=`stat -c %Y $DIR1/$tfile`
4807                 [ $mtime = $TEST_39_MTIME ] || \
4808                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4809
4810                 cancel_lru_locks $OSC
4811                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4812         done
4813 }
4814 run_test 39d "create, utime, stat =============================="
4815
4816 # bug 21114
4817 test_39e() {
4818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4819
4820         touch $DIR1/$tfile
4821         local mtime1=`stat -c %Y $DIR1/$tfile`
4822
4823         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4824
4825         for (( i=0; i < 2; i++ )) ; do
4826                 local mtime2=`stat -c %Y $DIR1/$tfile`
4827                 [ $mtime2 = $TEST_39_MTIME ] || \
4828                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4829
4830                 cancel_lru_locks $OSC
4831                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4832         done
4833 }
4834 run_test 39e "create, stat, utime, stat ========================"
4835
4836 # bug 21114
4837 test_39f() {
4838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4839
4840         touch $DIR1/$tfile
4841         mtime1=`stat -c %Y $DIR1/$tfile`
4842
4843         sleep 2
4844         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4845
4846         for (( i=0; i < 2; i++ )) ; do
4847                 local mtime2=`stat -c %Y $DIR1/$tfile`
4848                 [ $mtime2 = $TEST_39_MTIME ] || \
4849                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4850
4851                 cancel_lru_locks $OSC
4852                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4853         done
4854 }
4855 run_test 39f "create, stat, sleep, utime, stat ================="
4856
4857 # bug 11063
4858 test_39g() {
4859         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4860
4861         echo hello >> $DIR1/$tfile
4862         local mtime1=`stat -c %Y $DIR1/$tfile`
4863
4864         sleep 2
4865         chmod o+r $DIR1/$tfile
4866
4867         for (( i=0; i < 2; i++ )) ; do
4868                 local mtime2=`stat -c %Y $DIR1/$tfile`
4869                 [ "$mtime1" = "$mtime2" ] || \
4870                         error "lost mtime: $mtime2, should be $mtime1"
4871
4872                 cancel_lru_locks $OSC
4873                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4874         done
4875 }
4876 run_test 39g "write, chmod, stat ==============================="
4877
4878 # bug 11063
4879 test_39h() {
4880         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4881
4882         touch $DIR1/$tfile
4883         sleep 1
4884
4885         local d1=`date`
4886         echo hello >> $DIR1/$tfile
4887         local mtime1=`stat -c %Y $DIR1/$tfile`
4888
4889         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4890         local d2=`date`
4891         if [ "$d1" != "$d2" ]; then
4892                 echo "write and touch not within one second"
4893         else
4894                 for (( i=0; i < 2; i++ )) ; do
4895                         local mtime2=`stat -c %Y $DIR1/$tfile`
4896                         [ "$mtime2" = $TEST_39_MTIME ] || \
4897                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4898
4899                         cancel_lru_locks $OSC
4900                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4901                 done
4902         fi
4903 }
4904 run_test 39h "write, utime within one second, stat ============="
4905
4906 test_39i() {
4907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4908
4909         touch $DIR1/$tfile
4910         sleep 1
4911
4912         echo hello >> $DIR1/$tfile
4913         local mtime1=`stat -c %Y $DIR1/$tfile`
4914
4915         mv $DIR1/$tfile $DIR1/$tfile-1
4916
4917         for (( i=0; i < 2; i++ )) ; do
4918                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4919
4920                 [ "$mtime1" = "$mtime2" ] || \
4921                         error "lost mtime: $mtime2, should be $mtime1"
4922
4923                 cancel_lru_locks $OSC
4924                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4925         done
4926 }
4927 run_test 39i "write, rename, stat =============================="
4928
4929 test_39j() {
4930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4931
4932         start_full_debug_logging
4933         touch $DIR1/$tfile
4934         sleep 1
4935
4936         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4937         lctl set_param fail_loc=0x80000412
4938         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4939                 error "multiop failed"
4940         local multipid=$!
4941         local mtime1=`stat -c %Y $DIR1/$tfile`
4942
4943         mv $DIR1/$tfile $DIR1/$tfile-1
4944
4945         kill -USR1 $multipid
4946         wait $multipid || error "multiop close failed"
4947
4948         for (( i=0; i < 2; i++ )) ; do
4949                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4950                 [ "$mtime1" = "$mtime2" ] ||
4951                         error "mtime is lost on close: $mtime2, " \
4952                               "should be $mtime1"
4953
4954                 cancel_lru_locks
4955                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4956         done
4957         lctl set_param fail_loc=0
4958         stop_full_debug_logging
4959 }
4960 run_test 39j "write, rename, close, stat ======================="
4961
4962 test_39k() {
4963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4964
4965         touch $DIR1/$tfile
4966         sleep 1
4967
4968         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4969         local multipid=$!
4970         local mtime1=`stat -c %Y $DIR1/$tfile`
4971
4972         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4973
4974         kill -USR1 $multipid
4975         wait $multipid || error "multiop close failed"
4976
4977         for (( i=0; i < 2; i++ )) ; do
4978                 local mtime2=`stat -c %Y $DIR1/$tfile`
4979
4980                 [ "$mtime2" = $TEST_39_MTIME ] || \
4981                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4982
4983                 cancel_lru_locks
4984                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4985         done
4986 }
4987 run_test 39k "write, utime, close, stat ========================"
4988
4989 # this should be set to future
4990 TEST_39_ATIME=`date -d "1 year" +%s`
4991
4992 test_39l() {
4993         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4994         remote_mds_nodsh && skip "remote MDS with nodsh"
4995
4996         local atime_diff=$(do_facet $SINGLEMDS \
4997                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4998         rm -rf $DIR/$tdir
4999         mkdir_on_mdt0 $DIR/$tdir
5000
5001         # test setting directory atime to future
5002         touch -a -d @$TEST_39_ATIME $DIR/$tdir
5003         local atime=$(stat -c %X $DIR/$tdir)
5004         [ "$atime" = $TEST_39_ATIME ] ||
5005                 error "atime is not set to future: $atime, $TEST_39_ATIME"
5006
5007         # test setting directory atime from future to now
5008         local now=$(date +%s)
5009         touch -a -d @$now $DIR/$tdir
5010
5011         atime=$(stat -c %X $DIR/$tdir)
5012         [ "$atime" -eq "$now"  ] ||
5013                 error "atime is not updated from future: $atime, $now"
5014
5015         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
5016         sleep 3
5017
5018         # test setting directory atime when now > dir atime + atime_diff
5019         local d1=$(date +%s)
5020         ls $DIR/$tdir
5021         local d2=$(date +%s)
5022         cancel_lru_locks mdc
5023         atime=$(stat -c %X $DIR/$tdir)
5024         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
5025                 error "atime is not updated  : $atime, should be $d2"
5026
5027         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
5028         sleep 3
5029
5030         # test not setting directory atime when now < dir atime + atime_diff
5031         ls $DIR/$tdir
5032         cancel_lru_locks mdc
5033         atime=$(stat -c %X $DIR/$tdir)
5034         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
5035                 error "atime is updated to $atime, should remain $d1<atime<$d2"
5036
5037         do_facet $SINGLEMDS \
5038                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
5039 }
5040 run_test 39l "directory atime update ==========================="
5041
5042 test_39m() {
5043         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5044
5045         touch $DIR1/$tfile
5046         sleep 2
5047         local far_past_mtime=$(date -d "May 29 1953" +%s)
5048         local far_past_atime=$(date -d "Dec 17 1903" +%s)
5049
5050         touch -m -d @$far_past_mtime $DIR1/$tfile
5051         touch -a -d @$far_past_atime $DIR1/$tfile
5052
5053         for (( i=0; i < 2; i++ )) ; do
5054                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
5055                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
5056                         error "atime or mtime set incorrectly"
5057
5058                 cancel_lru_locks $OSC
5059                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
5060         done
5061 }
5062 run_test 39m "test atime and mtime before 1970"
5063
5064 test_39n() { # LU-3832
5065         remote_mds_nodsh && skip "remote MDS with nodsh"
5066
5067         local atime_diff=$(do_facet $SINGLEMDS \
5068                 lctl get_param -n mdd.*MDT0000*.atime_diff)
5069         local atime0
5070         local atime1
5071         local atime2
5072
5073         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
5074
5075         rm -rf $DIR/$tfile
5076         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
5077         atime0=$(stat -c %X $DIR/$tfile)
5078
5079         sleep 5
5080         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
5081         atime1=$(stat -c %X $DIR/$tfile)
5082
5083         sleep 5
5084         cancel_lru_locks mdc
5085         cancel_lru_locks osc
5086         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
5087         atime2=$(stat -c %X $DIR/$tfile)
5088
5089         do_facet $SINGLEMDS \
5090                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
5091
5092         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
5093         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
5094 }
5095 run_test 39n "check that O_NOATIME is honored"
5096
5097 test_39o() {
5098         TESTDIR=$DIR/$tdir/$tfile
5099         [ -e $TESTDIR ] && rm -rf $TESTDIR
5100         mkdir -p $TESTDIR
5101         cd $TESTDIR
5102         links1=2
5103         ls
5104         mkdir a b
5105         ls
5106         links2=$(stat -c %h .)
5107         [ $(($links1 + 2)) != $links2 ] &&
5108                 error "wrong links count $(($links1 + 2)) != $links2"
5109         rmdir b
5110         links3=$(stat -c %h .)
5111         [ $(($links1 + 1)) != $links3 ] &&
5112                 error "wrong links count $links1 != $links3"
5113         return 0
5114 }
5115 run_test 39o "directory cached attributes updated after create"
5116
5117 test_39p() {
5118         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
5119
5120         local MDTIDX=1
5121         TESTDIR=$DIR/$tdir/$tdir
5122         [ -e $TESTDIR ] && rm -rf $TESTDIR
5123         test_mkdir -p $TESTDIR
5124         cd $TESTDIR
5125         links1=2
5126         ls
5127         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
5128         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
5129         ls
5130         links2=$(stat -c %h .)
5131         [ $(($links1 + 2)) != $links2 ] &&
5132                 error "wrong links count $(($links1 + 2)) != $links2"
5133         rmdir remote_dir2
5134         links3=$(stat -c %h .)
5135         [ $(($links1 + 1)) != $links3 ] &&
5136                 error "wrong links count $links1 != $links3"
5137         return 0
5138 }
5139 run_test 39p "remote directory cached attributes updated after create ========"
5140
5141 test_39r() {
5142         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
5143                 skip "no atime update on old OST"
5144         if [ "$ost1_FSTYPE" != ldiskfs ]; then
5145                 skip_env "ldiskfs only test"
5146         fi
5147
5148         local saved_adiff
5149         saved_adiff=$(do_facet ost1 \
5150                 lctl get_param -n obdfilter.*OST0000.atime_diff)
5151         stack_trap "do_facet ost1 \
5152                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
5153
5154         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
5155
5156         $LFS setstripe -i 0 $DIR/$tfile
5157         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
5158                 error "can't write initial file"
5159         cancel_lru_locks osc
5160
5161         # exceed atime_diff and access file
5162         sleep 10
5163         dd if=$DIR/$tfile of=/dev/null bs=4k count=1 ||
5164                 error "can't udpate atime"
5165
5166         local atime_cli=$(stat -c %X $DIR/$tfile)
5167         echo "client atime: $atime_cli"
5168         # allow atime update to be written to device
5169         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
5170         sleep 5
5171
5172         local ostdev=$(ostdevname 1)
5173         local fid=($(lfs getstripe -y $DIR/$tfile |
5174                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
5175         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
5176         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
5177
5178         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
5179         local atime_ost=$(do_facet ost1 "$cmd" |&
5180                           awk -F'[: ]' '/atime:/ { print $4 }')
5181         (( atime_cli == atime_ost )) ||
5182                 error "atime on client $atime_cli != ost $atime_ost"
5183 }
5184 run_test 39r "lazy atime update on OST"
5185
5186 test_39q() { # LU-8041
5187         local testdir=$DIR/$tdir
5188         mkdir -p $testdir
5189         multiop_bg_pause $testdir D_c || error "multiop failed"
5190         local multipid=$!
5191         cancel_lru_locks mdc
5192         kill -USR1 $multipid
5193         local atime=$(stat -c %X $testdir)
5194         [ "$atime" -ne 0 ] || error "atime is zero"
5195 }
5196 run_test 39q "close won't zero out atime"
5197
5198 test_40() {
5199         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
5200         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
5201                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
5202         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
5203                 error "$tfile is not 4096 bytes in size"
5204 }
5205 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
5206
5207 test_41() {
5208         # bug 1553
5209         small_write $DIR/f41 18
5210 }
5211 run_test 41 "test small file write + fstat ====================="
5212
5213 count_ost_writes() {
5214         lctl get_param -n ${OSC}.*.stats |
5215                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
5216                         END { printf("%0.0f", writes) }'
5217 }
5218
5219 # decent default
5220 WRITEBACK_SAVE=500
5221 DIRTY_RATIO_SAVE=40
5222 MAX_DIRTY_RATIO=50
5223 BG_DIRTY_RATIO_SAVE=10
5224 MAX_BG_DIRTY_RATIO=25
5225
5226 start_writeback() {
5227         trap 0
5228         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
5229         # dirty_ratio, dirty_background_ratio
5230         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5231                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
5232                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
5233                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
5234         else
5235                 # if file not here, we are a 2.4 kernel
5236                 kill -CONT `pidof kupdated`
5237         fi
5238 }
5239
5240 stop_writeback() {
5241         # setup the trap first, so someone cannot exit the test at the
5242         # exact wrong time and mess up a machine
5243         trap start_writeback EXIT
5244         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
5245         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
5246                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
5247                 sysctl -w vm.dirty_writeback_centisecs=0
5248                 sysctl -w vm.dirty_writeback_centisecs=0
5249                 # save and increase /proc/sys/vm/dirty_ratio
5250                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
5251                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
5252                 # save and increase /proc/sys/vm/dirty_background_ratio
5253                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
5254                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
5255         else
5256                 # if file not here, we are a 2.4 kernel
5257                 kill -STOP `pidof kupdated`
5258         fi
5259 }
5260
5261 # ensure that all stripes have some grant before we test client-side cache
5262 setup_test42() {
5263         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
5264                 dd if=/dev/zero of=$i bs=4k count=1
5265                 rm $i
5266         done
5267 }
5268
5269 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
5270 # file truncation, and file removal.
5271 test_42a() {
5272         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5273
5274         setup_test42
5275         cancel_lru_locks $OSC
5276         stop_writeback
5277         sync; sleep 1; sync # just to be safe
5278         BEFOREWRITES=`count_ost_writes`
5279         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
5280         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
5281         AFTERWRITES=`count_ost_writes`
5282         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
5283                 error "$BEFOREWRITES < $AFTERWRITES"
5284         start_writeback
5285 }
5286 run_test 42a "ensure that we don't flush on close"
5287
5288 test_42b() {
5289         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5290
5291         setup_test42
5292         cancel_lru_locks $OSC
5293         stop_writeback
5294         sync
5295         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
5296         BEFOREWRITES=$(count_ost_writes)
5297         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
5298         AFTERWRITES=$(count_ost_writes)
5299         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5300                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
5301         fi
5302         BEFOREWRITES=$(count_ost_writes)
5303         sync || error "sync: $?"
5304         AFTERWRITES=$(count_ost_writes)
5305         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
5306                 error "$BEFOREWRITES < $AFTERWRITES on sync"
5307         fi
5308         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
5309         start_writeback
5310         return 0
5311 }
5312 run_test 42b "test destroy of file with cached dirty data ======"
5313
5314 # if these tests just want to test the effect of truncation,
5315 # they have to be very careful.  consider:
5316 # - the first open gets a {0,EOF}PR lock
5317 # - the first write conflicts and gets a {0, count-1}PW
5318 # - the rest of the writes are under {count,EOF}PW
5319 # - the open for truncate tries to match a {0,EOF}PR
5320 #   for the filesize and cancels the PWs.
5321 # any number of fixes (don't get {0,EOF} on open, match
5322 # composite locks, do smarter file size management) fix
5323 # this, but for now we want these tests to verify that
5324 # the cancellation with truncate intent works, so we
5325 # start the file with a full-file pw lock to match against
5326 # until the truncate.
5327 trunc_test() {
5328         test=$1
5329         file=$DIR/$test
5330         offset=$2
5331         cancel_lru_locks $OSC
5332         stop_writeback
5333         # prime the file with 0,EOF PW to match
5334         touch $file
5335         $TRUNCATE $file 0
5336         sync; sync
5337         # now the real test..
5338         dd if=/dev/zero of=$file bs=1024 count=100
5339         BEFOREWRITES=`count_ost_writes`
5340         $TRUNCATE $file $offset
5341         cancel_lru_locks $OSC
5342         AFTERWRITES=`count_ost_writes`
5343         start_writeback
5344 }
5345
5346 test_42c() {
5347         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5348
5349         trunc_test 42c 1024
5350         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
5351                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
5352         rm $file
5353 }
5354 run_test 42c "test partial truncate of file with cached dirty data"
5355
5356 test_42d() {
5357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5358
5359         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
5360         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
5361         $LCTL set_param debug=+cache
5362
5363         trunc_test 42d 0
5364         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
5365                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
5366         rm $file
5367 }
5368 run_test 42d "test complete truncate of file with cached dirty data"
5369
5370 test_42e() { # bug22074
5371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5372
5373         local TDIR=$DIR/${tdir}e
5374         local pages=16 # hardcoded 16 pages, don't change it.
5375         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
5376         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
5377         local max_dirty_mb
5378         local warmup_files
5379
5380         test_mkdir $DIR/${tdir}e
5381         $LFS setstripe -c 1 $TDIR
5382         createmany -o $TDIR/f $files
5383
5384         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
5385
5386         # we assume that with $OSTCOUNT files, at least one of them will
5387         # be allocated on OST0.
5388         warmup_files=$((OSTCOUNT * max_dirty_mb))
5389         createmany -o $TDIR/w $warmup_files
5390
5391         # write a large amount of data into one file and sync, to get good
5392         # avail_grant number from OST.
5393         for ((i=0; i<$warmup_files; i++)); do
5394                 idx=$($LFS getstripe -i $TDIR/w$i)
5395                 [ $idx -ne 0 ] && continue
5396                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
5397                 break
5398         done
5399         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
5400         sync
5401         $LCTL get_param $proc_osc0/cur_dirty_bytes
5402         $LCTL get_param $proc_osc0/cur_grant_bytes
5403
5404         # create as much dirty pages as we can while not to trigger the actual
5405         # RPCs directly. but depends on the env, VFS may trigger flush during this
5406         # period, hopefully we are good.
5407         for ((i=0; i<$warmup_files; i++)); do
5408                 idx=$($LFS getstripe -i $TDIR/w$i)
5409                 [ $idx -ne 0 ] && continue
5410                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
5411         done
5412         $LCTL get_param $proc_osc0/cur_dirty_bytes
5413         $LCTL get_param $proc_osc0/cur_grant_bytes
5414
5415         # perform the real test
5416         $LCTL set_param $proc_osc0/rpc_stats 0
5417         for ((;i<$files; i++)); do
5418                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
5419                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
5420         done
5421         sync
5422         $LCTL get_param $proc_osc0/rpc_stats
5423
5424         local percent=0
5425         local have_ppr=false
5426         $LCTL get_param $proc_osc0/rpc_stats |
5427                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
5428                         # skip lines until we are at the RPC histogram data
5429                         [ "$PPR" == "pages" ] && have_ppr=true && continue
5430                         $have_ppr || continue
5431
5432                         # we only want the percent stat for < 16 pages
5433                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
5434
5435                         percent=$((percent + WPCT))
5436                         if [[ $percent -gt 15 ]]; then
5437                                 error "less than 16-pages write RPCs" \
5438                                       "$percent% > 15%"
5439                                 break
5440                         fi
5441                 done
5442         rm -rf $TDIR
5443 }
5444 run_test 42e "verify sub-RPC writes are not done synchronously"
5445
5446 test_43A() { # was test_43
5447         test_mkdir $DIR/$tdir
5448         cp -p /bin/ls $DIR/$tdir/$tfile
5449         $MULTIOP $DIR/$tdir/$tfile Ow_c &
5450         pid=$!
5451         # give multiop a chance to open
5452         sleep 1
5453
5454         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
5455         kill -USR1 $pid
5456         # Wait for multiop to exit
5457         wait $pid
5458 }
5459 run_test 43A "execution of file opened for write should return -ETXTBSY"
5460
5461 test_43a() {
5462         test_mkdir $DIR/$tdir
5463         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5464         $DIR/$tdir/sleep 60 &
5465         SLEEP_PID=$!
5466         # Make sure exec of $tdir/sleep wins race with truncate
5467         sleep 1
5468         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
5469         kill $SLEEP_PID
5470 }
5471 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
5472
5473 test_43b() {
5474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5475
5476         test_mkdir $DIR/$tdir
5477         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
5478         $DIR/$tdir/sleep 60 &
5479         SLEEP_PID=$!
5480         # Make sure exec of $tdir/sleep wins race with truncate
5481         sleep 1
5482         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
5483         kill $SLEEP_PID
5484 }
5485 run_test 43b "truncate of file being executed should return -ETXTBSY"
5486
5487 test_43c() {
5488         local testdir="$DIR/$tdir"
5489         test_mkdir $testdir
5490         cp $SHELL $testdir/
5491         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
5492                 ( cd $testdir && md5sum -c )
5493 }
5494 run_test 43c "md5sum of copy into lustre"
5495
5496 test_44A() { # was test_44
5497         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5498
5499         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
5500         dd if=$DIR/f1 bs=4k count=1 > /dev/null
5501 }
5502 run_test 44A "zero length read from a sparse stripe"
5503
5504 test_44a() {
5505         local nstripe=$($LFS getstripe -c -d $DIR)
5506         [ -z "$nstripe" ] && skip "can't get stripe info"
5507         [[ $nstripe -gt $OSTCOUNT ]] &&
5508                 skip "Wrong default stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
5509
5510         local stride=$($LFS getstripe -S -d $DIR)
5511         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
5512                 nstripe=$($LFS df $DIR | grep OST: | wc -l)
5513         fi
5514
5515         OFFSETS="0 $((stride/2)) $((stride-1))"
5516         for offset in $OFFSETS; do
5517                 for i in $(seq 0 $((nstripe-1))); do
5518                         local GLOBALOFFSETS=""
5519                         # size in Bytes
5520                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
5521                         local myfn=$DIR/d44a-$size
5522                         echo "--------writing $myfn at $size"
5523                         ll_sparseness_write $myfn $size ||
5524                                 error "ll_sparseness_write"
5525                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
5526                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5527                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5528
5529                         for j in $(seq 0 $((nstripe-1))); do
5530                                 # size in Bytes
5531                                 size=$((((j + $nstripe )*$stride + $offset)))
5532                                 ll_sparseness_write $myfn $size ||
5533                                         error "ll_sparseness_write"
5534                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
5535                         done
5536                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
5537                                 error "ll_sparseness_verify $GLOBALOFFSETS"
5538                         rm -f $myfn
5539                 done
5540         done
5541 }
5542 run_test 44a "test sparse pwrite ==============================="
5543
5544 dirty_osc_total() {
5545         tot=0
5546         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
5547                 tot=$(($tot + $d))
5548         done
5549         echo $tot
5550 }
5551 do_dirty_record() {
5552         before=`dirty_osc_total`
5553         echo executing "\"$*\""
5554         eval $*
5555         after=`dirty_osc_total`
5556         echo before $before, after $after
5557 }
5558 test_45() {
5559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5560
5561         f="$DIR/f45"
5562         # Obtain grants from OST if it supports it
5563         echo blah > ${f}_grant
5564         stop_writeback
5565         sync
5566         do_dirty_record "echo blah > $f"
5567         [[ $before -eq $after ]] && error "write wasn't cached"
5568         do_dirty_record "> $f"
5569         [[ $before -gt $after ]] || error "truncate 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 "sync"
5573         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
5574         do_dirty_record "echo blah > $f"
5575         [[ $before -eq $after ]] && error "write wasn't cached"
5576         do_dirty_record "cancel_lru_locks osc"
5577         [[ $before -gt $after ]] ||
5578                 error "lock cancellation didn't lower dirty count"
5579         start_writeback
5580 }
5581 run_test 45 "osc io page accounting ============================"
5582
5583 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
5584 # test tickles a bug where re-dirtying a page was failing to be mapped to the
5585 # objects offset and an assert hit when an rpc was built with 1023's mapped
5586 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
5587 test_46() {
5588         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5589
5590         f="$DIR/f46"
5591         stop_writeback
5592         sync
5593         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5594         sync
5595         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5596         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5597         sync
5598         start_writeback
5599 }
5600 run_test 46 "dirtying a previously written page ================"
5601
5602 # test_47 is removed "Device nodes check" is moved to test_28
5603
5604 test_48a() { # bug 2399
5605         [ "$mds1_FSTYPE" = "zfs" ] &&
5606         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5607                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5608
5609         test_mkdir $DIR/$tdir
5610         cd $DIR/$tdir
5611         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5612         test_mkdir $DIR/$tdir
5613         touch foo || error "'touch foo' failed after recreating cwd"
5614         test_mkdir bar
5615         touch .foo || error "'touch .foo' failed after recreating cwd"
5616         test_mkdir .bar
5617         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5618         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5619         cd . || error "'cd .' failed after recreating cwd"
5620         mkdir . && error "'mkdir .' worked after recreating cwd"
5621         rmdir . && error "'rmdir .' worked after recreating cwd"
5622         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5623         cd .. || error "'cd ..' failed after recreating cwd"
5624 }
5625 run_test 48a "Access renamed working dir (should return errors)="
5626
5627 test_48b() { # bug 2399
5628         rm -rf $DIR/$tdir
5629         test_mkdir $DIR/$tdir
5630         cd $DIR/$tdir
5631         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5632         touch foo && error "'touch foo' worked after removing cwd"
5633         mkdir foo && error "'mkdir foo' worked after removing cwd"
5634         touch .foo && error "'touch .foo' worked after removing cwd"
5635         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5636         ls . > /dev/null && error "'ls .' worked after removing cwd"
5637         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5638         mkdir . && error "'mkdir .' worked after removing cwd"
5639         rmdir . && error "'rmdir .' worked after removing cwd"
5640         ln -s . foo && error "'ln -s .' worked after removing cwd"
5641         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5642 }
5643 run_test 48b "Access removed working dir (should return errors)="
5644
5645 test_48c() { # bug 2350
5646         #lctl set_param debug=-1
5647         #set -vx
5648         rm -rf $DIR/$tdir
5649         test_mkdir -p $DIR/$tdir/dir
5650         cd $DIR/$tdir/dir
5651         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5652         $TRACE touch foo && error "touch foo worked after removing cwd"
5653         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5654         touch .foo && error "touch .foo worked after removing cwd"
5655         mkdir .foo && error "mkdir .foo worked after removing cwd"
5656         $TRACE ls . && error "'ls .' worked after removing cwd"
5657         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5658         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5659         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5660         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5661         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5662 }
5663 run_test 48c "Access removed working subdir (should return errors)"
5664
5665 test_48d() { # bug 2350
5666         #lctl set_param debug=-1
5667         #set -vx
5668         rm -rf $DIR/$tdir
5669         test_mkdir -p $DIR/$tdir/dir
5670         cd $DIR/$tdir/dir
5671         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5672         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5673         $TRACE touch foo && error "'touch foo' worked after removing parent"
5674         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5675         touch .foo && error "'touch .foo' worked after removing parent"
5676         mkdir .foo && error "mkdir .foo worked after removing parent"
5677         $TRACE ls . && error "'ls .' worked after removing parent"
5678         $TRACE ls .. && error "'ls ..' worked after removing parent"
5679         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5680         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5681         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5682         true
5683 }
5684 run_test 48d "Access removed parent subdir (should return errors)"
5685
5686 test_48e() { # bug 4134
5687         #lctl set_param debug=-1
5688         #set -vx
5689         rm -rf $DIR/$tdir
5690         test_mkdir -p $DIR/$tdir/dir
5691         cd $DIR/$tdir/dir
5692         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5693         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5694         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5695         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5696         # On a buggy kernel addition of "touch foo" after cd .. will
5697         # produce kernel oops in lookup_hash_it
5698         touch ../foo && error "'cd ..' worked after recreate parent"
5699         cd $DIR
5700         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5701 }
5702 run_test 48e "Access to recreated parent subdir (should return errors)"
5703
5704 test_48f() {
5705         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
5706                 skip "need MDS >= 2.13.55"
5707         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
5708         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] ||
5709                 skip "needs different host for mdt1 mdt2"
5710         [[ $(facet_fstype mds1) == ldiskfs ]] || skip "ldiskfs only"
5711
5712         $LFS mkdir -i0 $DIR/$tdir
5713         $LFS mkdir -i 1 $DIR/$tdir/sub1 $DIR/$tdir/sub2 $DIR/$tdir/sub3
5714
5715         for d in sub1 sub2 sub3; do
5716                 #define OBD_FAIL_OSD_REF_DEL    0x19c
5717                 do_facet mds1 $LCTL set_param fail_loc=0x8000019c
5718                 rm -rf $DIR/$tdir/$d && error "rm $d should fail"
5719         done
5720
5721         rm -d --interactive=never $DIR/$tdir || error "rm $tdir fail"
5722 }
5723 run_test 48f "non-zero nlink dir unlink won't LBUG()"
5724
5725 test_49() { # LU-1030
5726         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5727         remote_ost_nodsh && skip "remote OST with nodsh"
5728
5729         # get ost1 size - $FSNAME-OST0000
5730         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5731                 awk '{ print $4 }')
5732         # write 800M at maximum
5733         [[ $ost1_size -lt 2 ]] && ost1_size=2
5734         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5735
5736         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5737         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5738         local dd_pid=$!
5739
5740         # change max_pages_per_rpc while writing the file
5741         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5742         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5743         # loop until dd process exits
5744         while ps ax -opid | grep -wq $dd_pid; do
5745                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5746                 sleep $((RANDOM % 5 + 1))
5747         done
5748         # restore original max_pages_per_rpc
5749         $LCTL set_param $osc1_mppc=$orig_mppc
5750         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5751 }
5752 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5753
5754 test_50() {
5755         # bug 1485
5756         test_mkdir $DIR/$tdir
5757         cd $DIR/$tdir
5758         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5759 }
5760 run_test 50 "special situations: /proc symlinks  ==============="
5761
5762 test_51a() {    # was test_51
5763         # bug 1516 - create an empty entry right after ".." then split dir
5764         test_mkdir -c1 $DIR/$tdir
5765         touch $DIR/$tdir/foo
5766         $MCREATE $DIR/$tdir/bar
5767         rm $DIR/$tdir/foo
5768         createmany -m $DIR/$tdir/longfile 201
5769         FNUM=202
5770         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5771                 $MCREATE $DIR/$tdir/longfile$FNUM
5772                 FNUM=$(($FNUM + 1))
5773                 echo -n "+"
5774         done
5775         echo
5776         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5777 }
5778 run_test 51a "special situations: split htree with empty entry =="
5779
5780 cleanup_print_lfs_df () {
5781         trap 0
5782         $LFS df
5783         $LFS df -i
5784 }
5785
5786 test_51b() {
5787         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5788
5789         local dir=$DIR/$tdir
5790         local nrdirs=$((65536 + 100))
5791
5792         # cleanup the directory
5793         rm -fr $dir
5794
5795         mkdir_on_mdt -i $((RANDOM % MDSCOUNT)) $dir
5796
5797         $LFS df
5798         $LFS df -i
5799         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5800         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5801         [[ $numfree -lt $nrdirs ]] &&
5802                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5803
5804         # need to check free space for the directories as well
5805         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5806         numfree=$(( blkfree / $(fs_inode_ksize) ))
5807         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5808
5809         trap cleanup_print_lfs_df EXIT
5810
5811         # create files
5812         createmany -d $dir/d $nrdirs || {
5813                 unlinkmany $dir/d $nrdirs
5814                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5815         }
5816
5817         # really created :
5818         nrdirs=$(ls -U $dir | wc -l)
5819
5820         # unlink all but 100 subdirectories, then check it still works
5821         local left=100
5822         local delete=$((nrdirs - left))
5823
5824         $LFS df
5825         $LFS df -i
5826
5827         # for ldiskfs the nlink count should be 1, but this is OSD specific
5828         # and so this is listed for informational purposes only
5829         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5830         unlinkmany -d $dir/d $delete ||
5831                 error "unlink of first $delete subdirs failed"
5832
5833         echo "nlink between: $(stat -c %h $dir)"
5834         local found=$(ls -U $dir | wc -l)
5835         [ $found -ne $left ] &&
5836                 error "can't find subdirs: found only $found, expected $left"
5837
5838         unlinkmany -d $dir/d $delete $left ||
5839                 error "unlink of second $left subdirs failed"
5840         # regardless of whether the backing filesystem tracks nlink accurately
5841         # or not, the nlink count shouldn't be more than "." and ".." here
5842         local after=$(stat -c %h $dir)
5843         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5844                 echo "nlink after: $after"
5845
5846         cleanup_print_lfs_df
5847 }
5848 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5849
5850 test_51d_sub() {
5851         local stripecount=$1
5852         local nfiles=$2
5853
5854         log "create files with stripecount=$stripecount"
5855         $LFS setstripe -C $stripecount $DIR/$tdir
5856         createmany -o $DIR/$tdir/t- $nfiles
5857         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5858         for ((n = 0; n < $OSTCOUNT; n++)); do
5859                 objs[$n]=$(awk -vobjs=0 '($1 == '$n') { objs += 1 } \
5860                            END { printf("%0.0f", objs) }' $TMP/$tfile)
5861                 objs0[$n]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5862                             '($1 == '$n') { objs += 1 } \
5863                             END { printf("%0.0f", objs) }')
5864                 log "OST$n has ${objs[$n]} objects, ${objs0[$n]} are index 0"
5865         done
5866         unlinkmany $DIR/$tdir/t- $nfiles
5867         rm  -f $TMP/$tfile
5868
5869         local nlast
5870         local min=4
5871         local max=6 # allow variance of (1 - $min/$max) = 33% by default
5872
5873         # For some combinations of stripecount and OSTCOUNT current code
5874         # is not ideal, and allocates 50% fewer *first* objects to some OSTs
5875         # than others. Rather than skipping this test entirely, check that
5876         # and keep testing to ensure imbalance does not get worse. LU-15282
5877         (( (OSTCOUNT == 6 && stripecount == 4) ||
5878            (OSTCOUNT == 10 && (stripecount == 4 || stripecount == 8)) ||
5879            (OSTCOUNT == 12 && (stripecount == 8 || stripecount == 9)))) && max=9
5880         for ((nlast=0, n = 1; n < $OSTCOUNT; nlast=n,n++)); do
5881                 (( ${objs[$n]} > ${objs[$nlast]} * 4 / 5 )) ||
5882                         { $LFS df && $LFS df -i &&
5883                         error "stripecount=$stripecount: " \
5884                               "OST $n has fewer objects vs. OST $nlast " \
5885                               "(${objs[$n]} < ${objs[$nlast]} x 4/5)"; }
5886                 (( ${objs[$n]} < ${objs[$nlast]} * 5 / 4 )) ||
5887                         { $LFS df && $LFS df -i &&
5888                         error "stripecount=$stripecount: " \
5889                               "OST $n has more objects vs. OST $nlast " \
5890                               "(${objs[$n]} > ${objs[$nlast]} x 5/4)"; }
5891
5892                 (( ${objs0[$n]} > ${objs0[$nlast]} * $min / $max )) ||
5893                         { $LFS df && $LFS df -i &&
5894                         error "stripecount=$stripecount: " \
5895                               "OST $n has fewer #0 objects vs. OST $nlast " \
5896                               "(${objs0[$n]} < ${objs0[$nlast]} x $min/$max)"; }
5897                 (( ${objs0[$n]} < ${objs0[$nlast]} * $max / $min )) ||
5898                         { $LFS df && $LFS df -i &&
5899                         error "stripecount=$stripecount: " \
5900                               "OST $n has more #0 objects vs. OST $nlast " \
5901                               "(${objs0[$n]} > ${objs0[$nlast]} x $max/$min)"; }
5902         done
5903 }
5904
5905 test_51d() {
5906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5907         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5908
5909         local stripecount
5910         local per_ost=100
5911         local nfiles=$((per_ost * OSTCOUNT))
5912         local mdts=$(comma_list $(mdts_nodes))
5913         local param="osp.*.create_count"
5914         local qos_old=$(do_facet mds1 \
5915                 "$LCTL get_param -n lod.$FSNAME-*.qos_threshold_rr" | head -n 1)
5916
5917         do_nodes $mdts \
5918                 "$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=100"
5919         stack_trap "do_nodes $mdts \
5920                 '$LCTL set_param lod.$FSNAME-*.qos_threshold_rr=${qos_old%%%}'"
5921
5922         test_mkdir $DIR/$tdir
5923         local dirstripes=$(lfs getdirstripe -c $DIR/$tdir)
5924         (( dirstripes > 0 )) || dirstripes=1
5925
5926         # Ensure enough OST objects precreated for tests to pass without
5927         # running out of objects.  This is an LOV r-r OST algorithm test,
5928         # not an OST object precreation test.
5929         local old=$(do_facet mds1 "$LCTL get_param -n $param" | head -n 1)
5930         (( old >= nfiles )) ||
5931         {
5932                 local create_count=$((nfiles * OSTCOUNT / dirstripes))
5933
5934                 do_nodes $mdts "$LCTL set_param $param=$create_count"
5935                 stack_trap "do_nodes $mdts $LCTL set_param $param=$old"
5936
5937                 # trigger precreation from all MDTs for all OSTs
5938                 for ((i = 0; i < $MDSCOUNT * 2; i++ )); do
5939                         $LFS setstripe -c -1 $DIR/$tdir/wide.$i
5940                 done
5941         }
5942
5943         for ((stripecount = 3; stripecount <= $OSTCOUNT; stripecount++)); do
5944                 sleep 8  # allow object precreation to catch up
5945                 test_51d_sub $stripecount $nfiles
5946         done
5947 }
5948 run_test 51d "check LOV round-robin OST object distribution"
5949
5950 test_51e() {
5951         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5952                 skip_env "ldiskfs only test"
5953         fi
5954
5955         test_mkdir -c1 $DIR/$tdir
5956         test_mkdir -c1 $DIR/$tdir/d0
5957
5958         touch $DIR/$tdir/d0/foo
5959         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5960                 error "file exceed 65000 nlink limit!"
5961         unlinkmany $DIR/$tdir/d0/f- 65001
5962         return 0
5963 }
5964 run_test 51e "check file nlink limit"
5965
5966 test_51f() {
5967         test_mkdir $DIR/$tdir
5968
5969         local max=100000
5970         local ulimit_old=$(ulimit -n)
5971         local spare=20 # number of spare fd's for scripts/libraries, etc.
5972         local mdt=$($LFS getstripe -m $DIR/$tdir)
5973         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5974
5975         echo "MDT$mdt numfree=$numfree, max=$max"
5976         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5977         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5978                 while ! ulimit -n $((numfree + spare)); do
5979                         numfree=$((numfree * 3 / 4))
5980                 done
5981                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5982         else
5983                 echo "left ulimit at $ulimit_old"
5984         fi
5985
5986         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5987                 unlinkmany $DIR/$tdir/f $numfree
5988                 error "create+open $numfree files in $DIR/$tdir failed"
5989         }
5990         ulimit -n $ulimit_old
5991
5992         # if createmany exits at 120s there will be fewer than $numfree files
5993         unlinkmany $DIR/$tdir/f $numfree || true
5994 }
5995 run_test 51f "check many open files limit"
5996
5997 test_52a() {
5998         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5999         test_mkdir $DIR/$tdir
6000         touch $DIR/$tdir/foo
6001         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
6002         echo bar >> $DIR/$tdir/foo || error "append bar failed"
6003         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
6004         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
6005         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
6006                                         error "link worked"
6007         echo foo >> $DIR/$tdir/foo || error "append foo failed"
6008         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
6009         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
6010                                                      error "lsattr"
6011         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
6012         cp -r $DIR/$tdir $TMP/
6013         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
6014 }
6015 run_test 52a "append-only flag test (should return errors)"
6016
6017 test_52b() {
6018         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
6019         test_mkdir $DIR/$tdir
6020         touch $DIR/$tdir/foo
6021         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
6022         cat test > $DIR/$tdir/foo && error "cat test worked"
6023         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
6024         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
6025         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
6026                                         error "link worked"
6027         echo foo >> $DIR/$tdir/foo && error "echo worked"
6028         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
6029         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
6030         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
6031         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
6032                                                         error "lsattr"
6033         chattr -i $DIR/$tdir/foo || error "chattr failed"
6034
6035         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
6036 }
6037 run_test 52b "immutable flag test (should return errors) ======="
6038
6039 test_53() {
6040         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6041         remote_mds_nodsh && skip "remote MDS with nodsh"
6042         remote_ost_nodsh && skip "remote OST with nodsh"
6043
6044         local param
6045         local param_seq
6046         local ostname
6047         local mds_last
6048         local mds_last_seq
6049         local ost_last
6050         local ost_last_seq
6051         local ost_last_id
6052         local ostnum
6053         local node
6054         local found=false
6055         local support_last_seq=true
6056
6057         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
6058                 support_last_seq=false
6059
6060         # only test MDT0000
6061         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
6062         local value
6063         for value in $(do_facet $SINGLEMDS \
6064                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
6065                 param=$(echo ${value[0]} | cut -d "=" -f1)
6066                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
6067
6068                 if $support_last_seq; then
6069                         param_seq=$(echo $param |
6070                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
6071                         mds_last_seq=$(do_facet $SINGLEMDS \
6072                                        $LCTL get_param -n $param_seq)
6073                 fi
6074                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
6075
6076                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
6077                 node=$(facet_active_host ost$((ostnum+1)))
6078                 param="obdfilter.$ostname.last_id"
6079                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
6080                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
6081                         ost_last_id=$ost_last
6082
6083                         if $support_last_seq; then
6084                                 ost_last_id=$(echo $ost_last |
6085                                               awk -F':' '{print $2}' |
6086                                               sed -e "s/^0x//g")
6087                                 ost_last_seq=$(echo $ost_last |
6088                                                awk -F':' '{print $1}')
6089                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
6090                         fi
6091
6092                         if [[ $ost_last_id != $mds_last ]]; then
6093                                 error "$ost_last_id != $mds_last"
6094                         else
6095                                 found=true
6096                                 break
6097                         fi
6098                 done
6099         done
6100         $found || error "can not match last_seq/last_id for $mdtosc"
6101         return 0
6102 }
6103 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
6104
6105 test_54a() {
6106         perl -MSocket -e ';' || skip "no Socket perl module installed"
6107
6108         $SOCKETSERVER $DIR/socket ||
6109                 error "$SOCKETSERVER $DIR/socket failed: $?"
6110         $SOCKETCLIENT $DIR/socket ||
6111                 error "$SOCKETCLIENT $DIR/socket failed: $?"
6112         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
6113 }
6114 run_test 54a "unix domain socket test =========================="
6115
6116 test_54b() {
6117         f="$DIR/f54b"
6118         mknod $f c 1 3
6119         chmod 0666 $f
6120         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
6121 }
6122 run_test 54b "char device works in lustre ======================"
6123
6124 find_loop_dev() {
6125         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
6126         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
6127         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
6128
6129         for i in $(seq 3 7); do
6130                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
6131                 LOOPDEV=$LOOPBASE$i
6132                 LOOPNUM=$i
6133                 break
6134         done
6135 }
6136
6137 cleanup_54c() {
6138         local rc=0
6139         loopdev="$DIR/loop54c"
6140
6141         trap 0
6142         $UMOUNT $DIR/$tdir || rc=$?
6143         losetup -d $loopdev || true
6144         losetup -d $LOOPDEV || true
6145         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
6146         return $rc
6147 }
6148
6149 test_54c() {
6150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6151
6152         loopdev="$DIR/loop54c"
6153
6154         find_loop_dev
6155         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
6156         trap cleanup_54c EXIT
6157         mknod $loopdev b 7 $LOOPNUM
6158         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
6159         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
6160         losetup $loopdev $DIR/$tfile ||
6161                 error "can't set up $loopdev for $DIR/$tfile"
6162         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
6163         test_mkdir $DIR/$tdir
6164         mount -t ext2 $loopdev $DIR/$tdir ||
6165                 error "error mounting $loopdev on $DIR/$tdir"
6166         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
6167                 error "dd write"
6168         df $DIR/$tdir
6169         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
6170                 error "dd read"
6171         cleanup_54c
6172 }
6173 run_test 54c "block device works in lustre ====================="
6174
6175 test_54d() {
6176         local pipe="$DIR/$tfile.pipe"
6177         local string="aaaaaa"
6178
6179         mknod $pipe p
6180         echo -n "$string" > $pipe &
6181         local result=$(cat $pipe)
6182         [[ "$result" == "$string" ]] || error "$result != $string"
6183 }
6184 run_test 54d "fifo device works in lustre ======================"
6185
6186 test_54e() {
6187         f="$DIR/f54e"
6188         string="aaaaaa"
6189         cp -aL /dev/console $f
6190         echo $string > $f || error "echo $string to $f failed"
6191 }
6192 run_test 54e "console/tty device works in lustre ======================"
6193
6194 test_56a() {
6195         local numfiles=3
6196         local numdirs=2
6197         local dir=$DIR/$tdir
6198
6199         rm -rf $dir
6200         test_mkdir -p $dir/dir
6201         for i in $(seq $numfiles); do
6202                 touch $dir/file$i
6203                 touch $dir/dir/file$i
6204         done
6205
6206         local numcomp=$($LFS getstripe --component-count $dir)
6207
6208         [[ $numcomp == 0 ]] && numcomp=1
6209
6210         # test lfs getstripe with --recursive
6211         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
6212
6213         [[ $filenum -eq $((numfiles * 2)) ]] ||
6214                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
6215         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
6216         [[ $filenum -eq $numfiles ]] ||
6217                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
6218         echo "$LFS getstripe showed obdidx or l_ost_idx"
6219
6220         # test lfs getstripe with file instead of dir
6221         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
6222         [[ $filenum -eq 1 ]] ||
6223                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
6224         echo "$LFS getstripe file1 passed"
6225
6226         #test lfs getstripe with --verbose
6227         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
6228         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
6229                 error "$LFS getstripe --verbose $dir: "\
6230                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
6231         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
6232                 error "$LFS getstripe $dir: showed lmm_magic"
6233
6234         #test lfs getstripe with -v prints lmm_fid
6235         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
6236         local countfids=$((numdirs + numfiles * numcomp))
6237         [[ $filenum -eq $countfids ]] ||
6238                 error "$LFS getstripe -v $dir: "\
6239                       "got $filenum want $countfids lmm_fid"
6240         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
6241                 error "$LFS getstripe $dir: showed lmm_fid by default"
6242         echo "$LFS getstripe --verbose passed"
6243
6244         #check for FID information
6245         local fid1=$($LFS getstripe --fid $dir/file1)
6246         local fid2=$($LFS getstripe --verbose $dir/file1 |
6247                      awk '/lmm_fid: / { print $2; exit; }')
6248         local fid3=$($LFS path2fid $dir/file1)
6249
6250         [ "$fid1" != "$fid2" ] &&
6251                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
6252         [ "$fid1" != "$fid3" ] &&
6253                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
6254         echo "$LFS getstripe --fid passed"
6255
6256         #test lfs getstripe with --obd
6257         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
6258                 error "$LFS getstripe --obd wrong_uuid: should return error"
6259
6260         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6261
6262         local ostidx=1
6263         local obduuid=$(ostuuid_from_index $ostidx)
6264         local found=$($LFS getstripe -r --obd $obduuid $dir |
6265                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
6266
6267         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
6268         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
6269                 ((filenum--))
6270         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
6271                 ((filenum--))
6272
6273         [[ $found -eq $filenum ]] ||
6274                 error "$LFS getstripe --obd: found $found expect $filenum"
6275         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
6276                 sed '/^[         ]*'${ostidx}'[  ]/d' |
6277                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
6278                 error "$LFS getstripe --obd: should not show file on other obd"
6279         echo "$LFS getstripe --obd passed"
6280 }
6281 run_test 56a "check $LFS getstripe"
6282
6283 test_56b() {
6284         local dir=$DIR/$tdir
6285         local numdirs=3
6286
6287         test_mkdir $dir
6288         for i in $(seq $numdirs); do
6289                 test_mkdir $dir/dir$i
6290         done
6291
6292         # test lfs getdirstripe default mode is non-recursion, which is
6293         # different from lfs getstripe
6294         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
6295
6296         [[ $dircnt -eq 1 ]] ||
6297                 error "$LFS getdirstripe: found $dircnt, not 1"
6298         dircnt=$($LFS getdirstripe --recursive $dir |
6299                 grep -c lmv_stripe_count)
6300         [[ $dircnt -eq $((numdirs + 1)) ]] ||
6301                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
6302 }
6303 run_test 56b "check $LFS getdirstripe"
6304
6305 test_56c() {
6306         remote_ost_nodsh && skip "remote OST with nodsh"
6307
6308         local ost_idx=0
6309         local ost_name=$(ostname_from_index $ost_idx)
6310         local old_status=$(ost_dev_status $ost_idx)
6311         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
6312
6313         [[ -z "$old_status" ]] ||
6314                 skip_env "OST $ost_name is in $old_status status"
6315
6316         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
6317         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6318                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
6319         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6320                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
6321                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
6322         fi
6323
6324         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
6325                 error "$LFS df -v showing inactive devices"
6326         sleep_maxage
6327
6328         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
6329
6330         [[ "$new_status" =~ "D" ]] ||
6331                 error "$ost_name status is '$new_status', missing 'D'"
6332         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
6333                 [[ "$new_status" =~ "N" ]] ||
6334                         error "$ost_name status is '$new_status', missing 'N'"
6335         fi
6336         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
6337                 [[ "$new_status" =~ "f" ]] ||
6338                         error "$ost_name status is '$new_status', missing 'f'"
6339         fi
6340
6341         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
6342         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
6343                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
6344         [[ -z "$p" ]] && restore_lustre_params < $p || true
6345         sleep_maxage
6346
6347         new_status=$(ost_dev_status $ost_idx)
6348         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
6349                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
6350         # can't check 'f' as devices may actually be on flash
6351 }
6352 run_test 56c "check 'lfs df' showing device status"
6353
6354 test_56d() {
6355         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
6356         local osts=$($LFS df -v $MOUNT | grep -c OST)
6357
6358         $LFS df $MOUNT
6359
6360         (( mdts == MDSCOUNT )) ||
6361                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
6362         (( osts == OSTCOUNT )) ||
6363                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
6364 }
6365 run_test 56d "'lfs df -v' prints only configured devices"
6366
6367 test_56e() {
6368         err_enoent=2 # No such file or directory
6369         err_eopnotsupp=95 # Operation not supported
6370
6371         enoent_mnt=/pmt1 # Invalid dentry. Path not present
6372         notsup_mnt=/tmp  # Valid dentry, but Not a lustreFS
6373
6374         # Check for handling of path not exists
6375         output=$($LFS df $enoent_mnt 2>&1)
6376         ret=$?
6377
6378         fs=$(echo $output | awk -F: '{print $2}' | awk '{print $3}' | tr -d \')
6379         [[ $fs = $enoent_mnt && $ret -eq $err_enoent ]] ||
6380                 error "expect failure $err_enoent, not $ret"
6381
6382         # Check for handling of non-Lustre FS
6383         output=$($LFS df $notsup_mnt)
6384         ret=$?
6385
6386         fs=$(echo $output | awk '{print $1}' | awk -F: '{print $2}')
6387         [[ $fs = $notsup_mnt && $ret -eq $err_eopnotsupp ]] ||
6388                 error "expect success $err_eopnotsupp, not $ret"
6389
6390         # Check for multiple LustreFS argument
6391         output=$($LFS df $MOUNT $MOUNT $MOUNT | grep -c "filesystem_summary:")
6392         ret=$?
6393
6394         [[ $output -eq 3 && $ret -eq 0 ]] ||
6395                 error "expect success 3, not $output, rc = $ret"
6396
6397         # Check for correct non-Lustre FS handling among multiple
6398         # LustreFS argument
6399         output=$($LFS df $MOUNT $notsup_mnt $MOUNT |
6400                 grep -c "filesystem_summary:"; exit ${PIPESTATUS[0]})
6401         ret=$?
6402
6403         [[ $output -eq 2 && $ret -eq $err_eopnotsupp ]] ||
6404                 error "expect success 2, not $output, rc = $ret"
6405 }
6406 run_test 56e "'lfs df' Handle non LustreFS & multiple LustreFS"
6407
6408 NUMFILES=3
6409 NUMDIRS=3
6410 setup_56() {
6411         local local_tdir="$1"
6412         local local_numfiles="$2"
6413         local local_numdirs="$3"
6414         local dir_params="$4"
6415         local dir_stripe_params="$5"
6416
6417         if [ ! -d "$local_tdir" ] ; then
6418                 test_mkdir -p $dir_stripe_params $local_tdir
6419                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
6420                 for i in $(seq $local_numfiles) ; do
6421                         touch $local_tdir/file$i
6422                 done
6423                 for i in $(seq $local_numdirs) ; do
6424                         test_mkdir $dir_stripe_params $local_tdir/dir$i
6425                         for j in $(seq $local_numfiles) ; do
6426                                 touch $local_tdir/dir$i/file$j
6427                         done
6428                 done
6429         fi
6430 }
6431
6432 setup_56_special() {
6433         local local_tdir=$1
6434         local local_numfiles=$2
6435         local local_numdirs=$3
6436
6437         setup_56 $local_tdir $local_numfiles $local_numdirs
6438
6439         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
6440                 for i in $(seq $local_numfiles) ; do
6441                         mknod $local_tdir/loop${i}b b 7 $i
6442                         mknod $local_tdir/null${i}c c 1 3
6443                         ln -s $local_tdir/file1 $local_tdir/link${i}
6444                 done
6445                 for i in $(seq $local_numdirs) ; do
6446                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
6447                         mknod $local_tdir/dir$i/null${i}c c 1 3
6448                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
6449                 done
6450         fi
6451 }
6452
6453 test_56g() {
6454         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6455         local expected=$(($NUMDIRS + 2))
6456
6457         setup_56 $dir $NUMFILES $NUMDIRS
6458
6459         # test lfs find with -name
6460         for i in $(seq $NUMFILES) ; do
6461                 local nums=$($LFS find -name "*$i" $dir | wc -l)
6462
6463                 [ $nums -eq $expected ] ||
6464                         error "lfs find -name '*$i' $dir wrong: "\
6465                               "found $nums, expected $expected"
6466         done
6467 }
6468 run_test 56g "check lfs find -name"
6469
6470 test_56h() {
6471         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6472         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
6473
6474         setup_56 $dir $NUMFILES $NUMDIRS
6475
6476         # test lfs find with ! -name
6477         for i in $(seq $NUMFILES) ; do
6478                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
6479
6480                 [ $nums -eq $expected ] ||
6481                         error "lfs find ! -name '*$i' $dir wrong: "\
6482                               "found $nums, expected $expected"
6483         done
6484 }
6485 run_test 56h "check lfs find ! -name"
6486
6487 test_56i() {
6488         local dir=$DIR/$tdir
6489
6490         test_mkdir $dir
6491
6492         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
6493         local out=$($cmd)
6494
6495         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
6496 }
6497 run_test 56i "check 'lfs find -ost UUID' skips directories"
6498
6499 test_56j() {
6500         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6501
6502         setup_56_special $dir $NUMFILES $NUMDIRS
6503
6504         local expected=$((NUMDIRS + 1))
6505         local cmd="$LFS find -type d $dir"
6506         local nums=$($cmd | wc -l)
6507
6508         [ $nums -eq $expected ] ||
6509                 error "'$cmd' wrong: found $nums, expected $expected"
6510 }
6511 run_test 56j "check lfs find -type d"
6512
6513 test_56k() {
6514         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6515
6516         setup_56_special $dir $NUMFILES $NUMDIRS
6517
6518         local expected=$(((NUMDIRS + 1) * NUMFILES))
6519         local cmd="$LFS find -type f $dir"
6520         local nums=$($cmd | wc -l)
6521
6522         [ $nums -eq $expected ] ||
6523                 error "'$cmd' wrong: found $nums, expected $expected"
6524 }
6525 run_test 56k "check lfs find -type f"
6526
6527 test_56l() {
6528         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6529
6530         setup_56_special $dir $NUMFILES $NUMDIRS
6531
6532         local expected=$((NUMDIRS + NUMFILES))
6533         local cmd="$LFS find -type b $dir"
6534         local nums=$($cmd | wc -l)
6535
6536         [ $nums -eq $expected ] ||
6537                 error "'$cmd' wrong: found $nums, expected $expected"
6538 }
6539 run_test 56l "check lfs find -type b"
6540
6541 test_56m() {
6542         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6543
6544         setup_56_special $dir $NUMFILES $NUMDIRS
6545
6546         local expected=$((NUMDIRS + NUMFILES))
6547         local cmd="$LFS find -type c $dir"
6548         local nums=$($cmd | wc -l)
6549         [ $nums -eq $expected ] ||
6550                 error "'$cmd' wrong: found $nums, expected $expected"
6551 }
6552 run_test 56m "check lfs find -type c"
6553
6554 test_56n() {
6555         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
6556         setup_56_special $dir $NUMFILES $NUMDIRS
6557
6558         local expected=$((NUMDIRS + NUMFILES))
6559         local cmd="$LFS find -type l $dir"
6560         local nums=$($cmd | wc -l)
6561
6562         [ $nums -eq $expected ] ||
6563                 error "'$cmd' wrong: found $nums, expected $expected"
6564 }
6565 run_test 56n "check lfs find -type l"
6566
6567 test_56o() {
6568         local dir=$DIR/$tdir
6569
6570         setup_56 $dir $NUMFILES $NUMDIRS
6571         utime $dir/file1 > /dev/null || error "utime (1)"
6572         utime $dir/file2 > /dev/null || error "utime (2)"
6573         utime $dir/dir1 > /dev/null || error "utime (3)"
6574         utime $dir/dir2 > /dev/null || error "utime (4)"
6575         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
6576         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
6577
6578         local expected=4
6579         local nums=$($LFS find -mtime +0 $dir | wc -l)
6580
6581         [ $nums -eq $expected ] ||
6582                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
6583
6584         expected=12
6585         cmd="$LFS find -mtime 0 $dir"
6586         nums=$($cmd | wc -l)
6587         [ $nums -eq $expected ] ||
6588                 error "'$cmd' wrong: found $nums, expected $expected"
6589 }
6590 run_test 56o "check lfs find -mtime for old files"
6591
6592 test_56ob() {
6593         local dir=$DIR/$tdir
6594         local expected=1
6595         local count=0
6596
6597         # just to make sure there is something that won't be found
6598         test_mkdir $dir
6599         touch $dir/$tfile.now
6600
6601         for age in year week day hour min; do
6602                 count=$((count + 1))
6603
6604                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
6605                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
6606                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
6607
6608                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
6609                 local nums=$($cmd | wc -l)
6610                 [ $nums -eq $expected ] ||
6611                         error "'$cmd' wrong: found $nums, expected $expected"
6612
6613                 cmd="$LFS find $dir -atime $count${age:0:1}"
6614                 nums=$($cmd | wc -l)
6615                 [ $nums -eq $expected ] ||
6616                         error "'$cmd' wrong: found $nums, expected $expected"
6617         done
6618
6619         sleep 2
6620         cmd="$LFS find $dir -ctime +1s -type f"
6621         nums=$($cmd | wc -l)
6622         (( $nums == $count * 2 + 1)) ||
6623                 error "'$cmd' wrong: found $nums, expected $((count * 2 + 1))"
6624 }
6625 run_test 56ob "check lfs find -atime -mtime -ctime with units"
6626
6627 test_newerXY_base() {
6628         local x=$1
6629         local y=$2
6630         local dir=$DIR/$tdir
6631         local ref
6632         local negref
6633
6634         if [ $y == "t" ]; then
6635                 if [ $x == "b" ]; then
6636                         ref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6637                 else
6638                         ref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6639                 fi
6640         else
6641                 ref=$DIR/$tfile.newer.$x$y
6642                 touch $ref || error "touch $ref failed"
6643         fi
6644
6645         echo "before = $ref"
6646         sleep 2
6647         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6648         sleep 2
6649         if [ $y == "t" ]; then
6650                 if [ $x == "b" ]; then
6651                         negref=\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\"
6652                 else
6653                         negref=\"$(date +"%Y-%m-%d %H:%M:%S")\"
6654                 fi
6655         else
6656                 negref=$DIR/$tfile.negnewer.$x$y
6657                 touch $negref || error "touch $negref failed"
6658         fi
6659
6660         echo "after = $negref"
6661         local cmd="$LFS find $dir -newer$x$y $ref"
6662         local nums=$(eval $cmd | wc -l)
6663         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
6664
6665         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6666                 error "'$cmd' wrong: found $nums newer, expected $expected"  ; }
6667
6668         cmd="$LFS find $dir ! -newer$x$y $negref"
6669         nums=$(eval $cmd | wc -l)
6670         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6671                 error "'$cmd' wrong: found $nums older, expected $expected"  ; }
6672
6673         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
6674         nums=$(eval $cmd | wc -l)
6675         [ $nums -eq $expected ] || { ls -lauR --full-time $dir ;
6676                 error "'$cmd' wrong: found $nums between, expected $expected"; }
6677
6678         rm -rf $DIR/*
6679 }
6680
6681 test_56oc() {
6682         test_newerXY_base "a" "a"
6683         test_newerXY_base "a" "m"
6684         test_newerXY_base "a" "c"
6685         test_newerXY_base "m" "a"
6686         test_newerXY_base "m" "m"
6687         test_newerXY_base "m" "c"
6688         test_newerXY_base "c" "a"
6689         test_newerXY_base "c" "m"
6690         test_newerXY_base "c" "c"
6691
6692         [[ -n "$sles_version" ]] &&
6693                 echo "skip timestamp tests on SLES, LU-13665" && return 0
6694
6695         test_newerXY_base "a" "t"
6696         test_newerXY_base "m" "t"
6697         test_newerXY_base "c" "t"
6698
6699         [[ $MDS1_VERSION -lt $(version_code 2.13.54) ||
6700            $CLIENT_VERSION -lt $(version_code 2.13.54) ]] &&
6701                 ! btime_supported && echo "btime unsupported" && return 0
6702
6703         test_newerXY_base "b" "b"
6704         test_newerXY_base "b" "t"
6705 }
6706 run_test 56oc "check lfs find -newerXY work"
6707
6708 btime_supported() {
6709         local dir=$DIR/$tdir
6710         local rc
6711
6712         mkdir -p $dir
6713         touch $dir/$tfile
6714         $LFS find $dir -btime -1d -type f
6715         rc=$?
6716         rm -rf $dir
6717         return $rc
6718 }
6719
6720 test_56od() {
6721         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
6722                 ! btime_supported && skip "btime unsupported on MDS"
6723
6724         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6725                 ! btime_supported && skip "btime unsupported on clients"
6726
6727         local dir=$DIR/$tdir
6728         local ref=$DIR/$tfile.ref
6729         local negref=$DIR/$tfile.negref
6730
6731         mkdir $dir || error "mkdir $dir failed"
6732         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6733         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6734         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6735         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6736         touch $ref || error "touch $ref failed"
6737         # sleep 3 seconds at least
6738         sleep 3
6739
6740         local before=$(do_facet mds1 date +%s)
6741         local skew=$(($(date +%s) - before + 1))
6742
6743         if (( skew < 0 && skew > -5 )); then
6744                 sleep $((0 - skew + 1))
6745                 skew=0
6746         fi
6747
6748         # Set the dir stripe params to limit files all on MDT0,
6749         # otherwise we need to calc the max clock skew between
6750         # the client and MDTs.
6751         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6752         sleep 2
6753         touch $negref || error "touch $negref failed"
6754
6755         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6756         local nums=$($cmd | wc -l)
6757         local expected=$(((NUMFILES + 1) * NUMDIRS))
6758
6759         [ $nums -eq $expected ] ||
6760                 error "'$cmd' wrong: found $nums, expected $expected"
6761
6762         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6763         nums=$($cmd | wc -l)
6764         expected=$((NUMFILES + 1))
6765         [ $nums -eq $expected ] ||
6766                 error "'$cmd' wrong: found $nums, expected $expected"
6767
6768         [ $skew -lt 0 ] && return
6769
6770         local after=$(do_facet mds1 date +%s)
6771         local age=$((after - before + 1 + skew))
6772
6773         cmd="$LFS find $dir -btime -${age}s -type f"
6774         nums=$($cmd | wc -l)
6775         expected=$(((NUMFILES + 1) * NUMDIRS))
6776
6777         echo "Clock skew between client and server: $skew, age:$age"
6778         [ $nums -eq $expected ] ||
6779                 error "'$cmd' wrong: found $nums, expected $expected"
6780
6781         expected=$(($NUMDIRS + 1))
6782         cmd="$LFS find $dir -btime -${age}s -type d"
6783         nums=$($cmd | wc -l)
6784         [ $nums -eq $expected ] ||
6785                 error "'$cmd' wrong: found $nums, expected $expected"
6786         rm -f $ref $negref || error "Failed to remove $ref $negref"
6787 }
6788 run_test 56od "check lfs find -btime with units"
6789
6790 test_56p() {
6791         [ $RUNAS_ID -eq $UID ] &&
6792                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6793
6794         local dir=$DIR/$tdir
6795
6796         setup_56 $dir $NUMFILES $NUMDIRS
6797         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6798
6799         local expected=$NUMFILES
6800         local cmd="$LFS find -uid $RUNAS_ID $dir"
6801         local nums=$($cmd | wc -l)
6802
6803         [ $nums -eq $expected ] ||
6804                 error "'$cmd' wrong: found $nums, expected $expected"
6805
6806         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6807         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6808         nums=$($cmd | wc -l)
6809         [ $nums -eq $expected ] ||
6810                 error "'$cmd' wrong: found $nums, expected $expected"
6811 }
6812 run_test 56p "check lfs find -uid and ! -uid"
6813
6814 test_56q() {
6815         [ $RUNAS_ID -eq $UID ] &&
6816                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6817
6818         local dir=$DIR/$tdir
6819
6820         setup_56 $dir $NUMFILES $NUMDIRS
6821         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6822
6823         local expected=$NUMFILES
6824         local cmd="$LFS find -gid $RUNAS_GID $dir"
6825         local nums=$($cmd | wc -l)
6826
6827         [ $nums -eq $expected ] ||
6828                 error "'$cmd' wrong: found $nums, expected $expected"
6829
6830         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6831         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6832         nums=$($cmd | wc -l)
6833         [ $nums -eq $expected ] ||
6834                 error "'$cmd' wrong: found $nums, expected $expected"
6835 }
6836 run_test 56q "check lfs find -gid and ! -gid"
6837
6838 test_56r() {
6839         local dir=$DIR/$tdir
6840
6841         setup_56 $dir $NUMFILES $NUMDIRS
6842
6843         local expected=12
6844         local cmd="$LFS find -size 0 -type f -lazy $dir"
6845         local nums=$($cmd | wc -l)
6846
6847         [ $nums -eq $expected ] ||
6848                 error "'$cmd' wrong: found $nums, expected $expected"
6849         cmd="$LFS find -size 0 -type f $dir"
6850         nums=$($cmd | wc -l)
6851         [ $nums -eq $expected ] ||
6852                 error "'$cmd' wrong: found $nums, expected $expected"
6853
6854         expected=0
6855         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6856         nums=$($cmd | wc -l)
6857         [ $nums -eq $expected ] ||
6858                 error "'$cmd' wrong: found $nums, expected $expected"
6859         cmd="$LFS find ! -size 0 -type f $dir"
6860         nums=$($cmd | wc -l)
6861         [ $nums -eq $expected ] ||
6862                 error "'$cmd' wrong: found $nums, expected $expected"
6863
6864         echo "test" > $dir/$tfile
6865         echo "test2" > $dir/$tfile.2 && sync
6866         expected=1
6867         cmd="$LFS find -size 5 -type f -lazy $dir"
6868         nums=$($cmd | wc -l)
6869         [ $nums -eq $expected ] ||
6870                 error "'$cmd' wrong: found $nums, expected $expected"
6871         cmd="$LFS find -size 5 -type f $dir"
6872         nums=$($cmd | wc -l)
6873         [ $nums -eq $expected ] ||
6874                 error "'$cmd' wrong: found $nums, expected $expected"
6875
6876         expected=1
6877         cmd="$LFS find -size +5 -type f -lazy $dir"
6878         nums=$($cmd | wc -l)
6879         [ $nums -eq $expected ] ||
6880                 error "'$cmd' wrong: found $nums, expected $expected"
6881         cmd="$LFS find -size +5 -type f $dir"
6882         nums=$($cmd | wc -l)
6883         [ $nums -eq $expected ] ||
6884                 error "'$cmd' wrong: found $nums, expected $expected"
6885
6886         expected=2
6887         cmd="$LFS find -size +0 -type f -lazy $dir"
6888         nums=$($cmd | wc -l)
6889         [ $nums -eq $expected ] ||
6890                 error "'$cmd' wrong: found $nums, expected $expected"
6891         cmd="$LFS find -size +0 -type f $dir"
6892         nums=$($cmd | wc -l)
6893         [ $nums -eq $expected ] ||
6894                 error "'$cmd' wrong: found $nums, expected $expected"
6895
6896         expected=2
6897         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6898         nums=$($cmd | wc -l)
6899         [ $nums -eq $expected ] ||
6900                 error "'$cmd' wrong: found $nums, expected $expected"
6901         cmd="$LFS find ! -size -5 -type f $dir"
6902         nums=$($cmd | wc -l)
6903         [ $nums -eq $expected ] ||
6904                 error "'$cmd' wrong: found $nums, expected $expected"
6905
6906         expected=12
6907         cmd="$LFS find -size -5 -type f -lazy $dir"
6908         nums=$($cmd | wc -l)
6909         [ $nums -eq $expected ] ||
6910                 error "'$cmd' wrong: found $nums, expected $expected"
6911         cmd="$LFS find -size -5 -type f $dir"
6912         nums=$($cmd | wc -l)
6913         [ $nums -eq $expected ] ||
6914                 error "'$cmd' wrong: found $nums, expected $expected"
6915 }
6916 run_test 56r "check lfs find -size works"
6917
6918 test_56ra_sub() {
6919         local expected=$1
6920         local glimpses=$2
6921         local cmd="$3"
6922
6923         cancel_lru_locks $OSC
6924
6925         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6926         local nums=$($cmd | wc -l)
6927
6928         [ $nums -eq $expected ] ||
6929                 error "'$cmd' wrong: found $nums, expected $expected"
6930
6931         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6932
6933         if (( rpcs_before + glimpses != rpcs_after )); then
6934                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6935                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6936
6937                 if [[ $glimpses == 0 ]]; then
6938                         error "'$cmd' should not send glimpse RPCs to OST"
6939                 else
6940                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6941                 fi
6942         fi
6943 }
6944
6945 test_56ra() {
6946         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6947                 skip "MDS < 2.12.58 doesn't return LSOM data"
6948         local dir=$DIR/$tdir
6949         local old_agl=$($LCTL get_param -n llite.*.statahead_agl)
6950
6951         [[ $OSC == "mdc" ]] && skip "statahead not needed for DoM files"
6952
6953         # statahead_agl may cause extra glimpse which confuses results. LU-13017
6954         $LCTL set_param -n llite.*.statahead_agl=0
6955         stack_trap "$LCTL set_param -n llite.*.statahead_agl=$old_agl"
6956
6957         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6958         # open and close all files to ensure LSOM is updated
6959         cancel_lru_locks $OSC
6960         find $dir -type f | xargs cat > /dev/null
6961
6962         #   expect_found  glimpse_rpcs  command_to_run
6963         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6964         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6965         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6966         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6967
6968         echo "test" > $dir/$tfile
6969         echo "test2" > $dir/$tfile.2 && sync
6970         cancel_lru_locks $OSC
6971         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6972
6973         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6974         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6975         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6976         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6977
6978         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6979         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6980         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6981         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6982         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6983         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6984 }
6985 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6986
6987 test_56rb() {
6988         local dir=$DIR/$tdir
6989         local tmp=$TMP/$tfile.log
6990         local mdt_idx;
6991
6992         test_mkdir -p $dir || error "failed to mkdir $dir"
6993         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6994                 error "failed to setstripe $dir/$tfile"
6995         mdt_idx=$($LFS getdirstripe -i $dir)
6996         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6997
6998         stack_trap "rm -f $tmp" EXIT
6999         $LFS find --size +100K --ost 0 $dir |& tee $tmp
7000         ! grep -q obd_uuid $tmp ||
7001                 error "failed to find --size +100K --ost 0 $dir"
7002         $LFS find --size +100K --mdt $mdt_idx $dir |& tee $tmp
7003         ! grep -q obd_uuid $tmp ||
7004                 error "failed to find --size +100K --mdt $mdt_idx $dir"
7005 }
7006 run_test 56rb "check lfs find --size --ost/--mdt works"
7007
7008 test_56rc() {
7009         (( MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
7010         local dir=$DIR/$tdir
7011         local found
7012
7013         test_mkdir -c 2 -H all_char $dir || error "failed to mkdir $dir"
7014         $LFS mkdir -c 2 --mdt-hash all_char $dir/$tdir-all{1..10}
7015         (( $MDSCOUNT > 2 )) &&
7016                 $LFS mkdir -c 3 --mdt-hash fnv_1a_64 $dir/$tdir-fnv{1..10}
7017         mkdir $dir/$tdir-{1..10}
7018         touch $dir/$tfile-{1..10}
7019
7020         found=$($LFS find $dir --mdt-count 2 | wc -l)
7021         expect=11
7022         (( $found == $expect )) || error "found $found 2-stripe, expect $expect"
7023
7024         found=$($LFS find $dir -T +1 | wc -l)
7025         (( $MDSCOUNT > 2 )) && expect=$((expect + 10))
7026         (( $found == $expect )) || error "found $found 2+stripe, expect $expect"
7027
7028         found=$($LFS find $dir --mdt-hash all_char | wc -l)
7029         expect=11
7030         (( $found == $expect )) || error "found $found all_char, expect $expect"
7031
7032         found=$($LFS find $dir --mdt-hash fnv_1a_64 | wc -l)
7033         (( $MDSCOUNT > 2 )) && expect=10 || expect=0
7034         (( $found == $expect )) || error "found $found all_char, expect $expect"
7035 }
7036 run_test 56rc "check lfs find --mdt-count/--mdt-hash works"
7037
7038 test_56s() { # LU-611 #LU-9369
7039         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
7040
7041         local dir=$DIR/$tdir
7042         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
7043
7044         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
7045         for i in $(seq $NUMDIRS); do
7046                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
7047         done
7048
7049         local expected=$NUMDIRS
7050         local cmd="$LFS find -c $OSTCOUNT $dir"
7051         local nums=$($cmd | wc -l)
7052
7053         [ $nums -eq $expected ] || {
7054                 $LFS getstripe -R $dir
7055                 error "'$cmd' wrong: found $nums, expected $expected"
7056         }
7057
7058         expected=$((NUMDIRS + onestripe))
7059         cmd="$LFS find -stripe-count +0 -type f $dir"
7060         nums=$($cmd | wc -l)
7061         [ $nums -eq $expected ] || {
7062                 $LFS getstripe -R $dir
7063                 error "'$cmd' wrong: found $nums, expected $expected"
7064         }
7065
7066         expected=$onestripe
7067         cmd="$LFS find -stripe-count 1 -type f $dir"
7068         nums=$($cmd | wc -l)
7069         [ $nums -eq $expected ] || {
7070                 $LFS getstripe -R $dir
7071                 error "'$cmd' wrong: found $nums, expected $expected"
7072         }
7073
7074         cmd="$LFS find -stripe-count -2 -type f $dir"
7075         nums=$($cmd | wc -l)
7076         [ $nums -eq $expected ] || {
7077                 $LFS getstripe -R $dir
7078                 error "'$cmd' wrong: found $nums, expected $expected"
7079         }
7080
7081         expected=0
7082         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
7083         nums=$($cmd | wc -l)
7084         [ $nums -eq $expected ] || {
7085                 $LFS getstripe -R $dir
7086                 error "'$cmd' wrong: found $nums, expected $expected"
7087         }
7088 }
7089 run_test 56s "check lfs find -stripe-count works"
7090
7091 test_56t() { # LU-611 #LU-9369
7092         local dir=$DIR/$tdir
7093
7094         setup_56 $dir 0 $NUMDIRS
7095         for i in $(seq $NUMDIRS); do
7096                 $LFS setstripe -S 8M $dir/dir$i/$tfile
7097         done
7098
7099         local expected=$NUMDIRS
7100         local cmd="$LFS find -S 8M $dir"
7101         local nums=$($cmd | wc -l)
7102
7103         [ $nums -eq $expected ] || {
7104                 $LFS getstripe -R $dir
7105                 error "'$cmd' wrong: found $nums, expected $expected"
7106         }
7107         rm -rf $dir
7108
7109         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
7110
7111         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
7112
7113         expected=$(((NUMDIRS + 1) * NUMFILES))
7114         cmd="$LFS find -stripe-size 512k -type f $dir"
7115         nums=$($cmd | wc -l)
7116         [ $nums -eq $expected ] ||
7117                 error "'$cmd' wrong: found $nums, expected $expected"
7118
7119         cmd="$LFS find -stripe-size +320k -type f $dir"
7120         nums=$($cmd | wc -l)
7121         [ $nums -eq $expected ] ||
7122                 error "'$cmd' wrong: found $nums, expected $expected"
7123
7124         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
7125         cmd="$LFS find -stripe-size +200k -type f $dir"
7126         nums=$($cmd | wc -l)
7127         [ $nums -eq $expected ] ||
7128                 error "'$cmd' wrong: found $nums, expected $expected"
7129
7130         cmd="$LFS find -stripe-size -640k -type f $dir"
7131         nums=$($cmd | wc -l)
7132         [ $nums -eq $expected ] ||
7133                 error "'$cmd' wrong: found $nums, expected $expected"
7134
7135         expected=4
7136         cmd="$LFS find -stripe-size 256k -type f $dir"
7137         nums=$($cmd | wc -l)
7138         [ $nums -eq $expected ] ||
7139                 error "'$cmd' wrong: found $nums, expected $expected"
7140
7141         cmd="$LFS find -stripe-size -320k -type f $dir"
7142         nums=$($cmd | wc -l)
7143         [ $nums -eq $expected ] ||
7144                 error "'$cmd' wrong: found $nums, expected $expected"
7145
7146         expected=0
7147         cmd="$LFS find -stripe-size 1024k -type f $dir"
7148         nums=$($cmd | wc -l)
7149         [ $nums -eq $expected ] ||
7150                 error "'$cmd' wrong: found $nums, expected $expected"
7151 }
7152 run_test 56t "check lfs find -stripe-size works"
7153
7154 test_56u() { # LU-611
7155         local dir=$DIR/$tdir
7156
7157         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
7158
7159         if [[ $OSTCOUNT -gt 1 ]]; then
7160                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
7161                 onestripe=4
7162         else
7163                 onestripe=0
7164         fi
7165
7166         local expected=$(((NUMDIRS + 1) * NUMFILES))
7167         local cmd="$LFS find -stripe-index 0 -type f $dir"
7168         local nums=$($cmd | wc -l)
7169
7170         [ $nums -eq $expected ] ||
7171                 error "'$cmd' wrong: found $nums, expected $expected"
7172
7173         expected=$onestripe
7174         cmd="$LFS find -stripe-index 1 -type f $dir"
7175         nums=$($cmd | wc -l)
7176         [ $nums -eq $expected ] ||
7177                 error "'$cmd' wrong: found $nums, expected $expected"
7178
7179         cmd="$LFS find ! -stripe-index 0 -type f $dir"
7180         nums=$($cmd | wc -l)
7181         [ $nums -eq $expected ] ||
7182                 error "'$cmd' wrong: found $nums, expected $expected"
7183
7184         expected=0
7185         # This should produce an error and not return any files
7186         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
7187         nums=$($cmd 2>/dev/null | wc -l)
7188         [ $nums -eq $expected ] ||
7189                 error "'$cmd' wrong: found $nums, expected $expected"
7190
7191         if [[ $OSTCOUNT -gt 1 ]]; then
7192                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
7193                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
7194                 nums=$($cmd | wc -l)
7195                 [ $nums -eq $expected ] ||
7196                         error "'$cmd' wrong: found $nums, expected $expected"
7197         fi
7198 }
7199 run_test 56u "check lfs find -stripe-index works"
7200
7201 test_56v() {
7202         local mdt_idx=0
7203         local dir=$DIR/$tdir
7204
7205         setup_56 $dir $NUMFILES $NUMDIRS
7206
7207         UUID=$(mdtuuid_from_index $mdt_idx $dir)
7208         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
7209
7210         for file in $($LFS find -m $UUID $dir); do
7211                 file_midx=$($LFS getstripe -m $file)
7212                 [ $file_midx -eq $mdt_idx ] ||
7213                         error "lfs find -m $UUID != getstripe -m $file_midx"
7214         done
7215 }
7216 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
7217
7218 test_56w() {
7219         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7221
7222         local dir=$DIR/$tdir
7223
7224         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
7225
7226         local stripe_size=$($LFS getstripe -S -d $dir) ||
7227                 error "$LFS getstripe -S -d $dir failed"
7228         stripe_size=${stripe_size%% *}
7229
7230         local file_size=$((stripe_size * OSTCOUNT))
7231         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
7232         local required_space=$((file_num * file_size))
7233         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
7234                            head -n1)
7235         [[ $free_space -le $((required_space / 1024)) ]] &&
7236                 skip_env "need $required_space, have $free_space kbytes"
7237
7238         local dd_bs=65536
7239         local dd_count=$((file_size / dd_bs))
7240
7241         # write data into the files
7242         local i
7243         local j
7244         local file
7245
7246         for i in $(seq $NUMFILES); do
7247                 file=$dir/file$i
7248                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7249                         error "write data into $file failed"
7250         done
7251         for i in $(seq $NUMDIRS); do
7252                 for j in $(seq $NUMFILES); do
7253                         file=$dir/dir$i/file$j
7254                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
7255                                 error "write data into $file failed"
7256                 done
7257         done
7258
7259         # $LFS_MIGRATE will fail if hard link migration is unsupported
7260         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
7261                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
7262                         error "creating links to $dir/dir1/file1 failed"
7263         fi
7264
7265         local expected=-1
7266
7267         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
7268
7269         # lfs_migrate file
7270         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
7271
7272         echo "$cmd"
7273         eval $cmd || error "$cmd failed"
7274
7275         check_stripe_count $dir/file1 $expected
7276
7277         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
7278         then
7279                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
7280                 # OST 1 if it is on OST 0. This file is small enough to
7281                 # be on only one stripe.
7282                 file=$dir/migr_1_ost
7283                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
7284                         error "write data into $file failed"
7285                 local obdidx=$($LFS getstripe -i $file)
7286                 local oldmd5=$(md5sum $file)
7287                 local newobdidx=0
7288
7289                 [[ $obdidx -eq 0 ]] && newobdidx=1
7290                 cmd="$LFS migrate -i $newobdidx $file"
7291                 echo $cmd
7292                 eval $cmd || error "$cmd failed"
7293
7294                 local realobdix=$($LFS getstripe -i $file)
7295                 local newmd5=$(md5sum $file)
7296
7297                 [[ $newobdidx -ne $realobdix ]] &&
7298                         error "new OST is different (was=$obdidx, "\
7299                               "wanted=$newobdidx, got=$realobdix)"
7300                 [[ "$oldmd5" != "$newmd5" ]] &&
7301                         error "md5sum differ: $oldmd5, $newmd5"
7302         fi
7303
7304         # lfs_migrate dir
7305         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
7306         echo "$cmd"
7307         eval $cmd || error "$cmd failed"
7308
7309         for j in $(seq $NUMFILES); do
7310                 check_stripe_count $dir/dir1/file$j $expected
7311         done
7312
7313         # lfs_migrate works with lfs find
7314         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
7315              $LFS_MIGRATE -y -c $expected"
7316         echo "$cmd"
7317         eval $cmd || error "$cmd failed"
7318
7319         for i in $(seq 2 $NUMFILES); do
7320                 check_stripe_count $dir/file$i $expected
7321         done
7322         for i in $(seq 2 $NUMDIRS); do
7323                 for j in $(seq $NUMFILES); do
7324                 check_stripe_count $dir/dir$i/file$j $expected
7325                 done
7326         done
7327 }
7328 run_test 56w "check lfs_migrate -c stripe_count works"
7329
7330 test_56wb() {
7331         local file1=$DIR/$tdir/file1
7332         local create_pool=false
7333         local initial_pool=$($LFS getstripe -p $DIR)
7334         local pool_list=()
7335         local pool=""
7336
7337         echo -n "Creating test dir..."
7338         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7339         echo "done."
7340
7341         echo -n "Creating test file..."
7342         touch $file1 || error "cannot create file"
7343         echo "done."
7344
7345         echo -n "Detecting existing pools..."
7346         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
7347
7348         if [ ${#pool_list[@]} -gt 0 ]; then
7349                 echo "${pool_list[@]}"
7350                 for thispool in "${pool_list[@]}"; do
7351                         if [[ -z "$initial_pool" ||
7352                               "$initial_pool" != "$thispool" ]]; then
7353                                 pool="$thispool"
7354                                 echo "Using existing pool '$pool'"
7355                                 break
7356                         fi
7357                 done
7358         else
7359                 echo "none detected."
7360         fi
7361         if [ -z "$pool" ]; then
7362                 pool=${POOL:-testpool}
7363                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
7364                 echo -n "Creating pool '$pool'..."
7365                 create_pool=true
7366                 pool_add $pool &> /dev/null ||
7367                         error "pool_add failed"
7368                 echo "done."
7369
7370                 echo -n "Adding target to pool..."
7371                 pool_add_targets $pool 0 0 1 &> /dev/null ||
7372                         error "pool_add_targets failed"
7373                 echo "done."
7374         fi
7375
7376         echo -n "Setting pool using -p option..."
7377         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
7378                 error "migrate failed rc = $?"
7379         echo "done."
7380
7381         echo -n "Verifying test file is in pool after migrating..."
7382         [ "$($LFS getstripe -p $file1)" = $pool ] ||
7383                 error "file was not migrated to pool $pool"
7384         echo "done."
7385
7386         echo -n "Removing test file from pool '$pool'..."
7387         # "lfs migrate $file" won't remove the file from the pool
7388         # until some striping information is changed.
7389         $LFS migrate -c 1 $file1 &> /dev/null ||
7390                 error "cannot remove from pool"
7391         [ "$($LFS getstripe -p $file1)" ] &&
7392                 error "pool still set"
7393         echo "done."
7394
7395         echo -n "Setting pool using --pool option..."
7396         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
7397                 error "migrate failed rc = $?"
7398         echo "done."
7399
7400         # Clean up
7401         rm -f $file1
7402         if $create_pool; then
7403                 destroy_test_pools 2> /dev/null ||
7404                         error "destroy test pools failed"
7405         fi
7406 }
7407 run_test 56wb "check lfs_migrate pool support"
7408
7409 test_56wc() {
7410         local file1="$DIR/$tdir/file1"
7411         local parent_ssize
7412         local parent_scount
7413         local cur_ssize
7414         local cur_scount
7415         local orig_ssize
7416
7417         echo -n "Creating test dir..."
7418         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
7419         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
7420                 error "cannot set stripe by '-S 1M -c 1'"
7421         echo "done"
7422
7423         echo -n "Setting initial stripe for test file..."
7424         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
7425                 error "cannot set stripe"
7426         cur_ssize=$($LFS getstripe -S "$file1")
7427         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
7428         echo "done."
7429
7430         # File currently set to -S 512K -c 1
7431
7432         # Ensure -c and -S options are rejected when -R is set
7433         echo -n "Verifying incompatible options are detected..."
7434         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
7435                 error "incompatible -c and -R options not detected"
7436         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
7437                 error "incompatible -S and -R options not detected"
7438         echo "done."
7439
7440         # Ensure unrecognized options are passed through to 'lfs migrate'
7441         echo -n "Verifying -S option is passed through to lfs migrate..."
7442         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
7443                 error "migration failed"
7444         cur_ssize=$($LFS getstripe -S "$file1")
7445         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
7446         echo "done."
7447
7448         # File currently set to -S 1M -c 1
7449
7450         # Ensure long options are supported
7451         echo -n "Verifying long options supported..."
7452         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
7453                 error "long option without argument not supported"
7454         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
7455                 error "long option with argument not supported"
7456         cur_ssize=$($LFS getstripe -S "$file1")
7457         [ $cur_ssize -eq 524288 ] ||
7458                 error "migrate --stripe-size $cur_ssize != 524288"
7459         echo "done."
7460
7461         # File currently set to -S 512K -c 1
7462
7463         if [ "$OSTCOUNT" -gt 1 ]; then
7464                 echo -n "Verifying explicit stripe count can be set..."
7465                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
7466                         error "migrate failed"
7467                 cur_scount=$($LFS getstripe -c "$file1")
7468                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
7469                 echo "done."
7470         fi
7471
7472         # File currently set to -S 512K -c 1 or -S 512K -c 2
7473
7474         # Ensure parent striping is used if -R is set, and no stripe
7475         # count or size is specified
7476         echo -n "Setting stripe for parent directory..."
7477         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7478                 error "cannot set stripe '-S 2M -c 1'"
7479         echo "done."
7480
7481         echo -n "Verifying restripe option uses parent stripe settings..."
7482         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
7483         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
7484         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
7485                 error "migrate failed"
7486         cur_ssize=$($LFS getstripe -S "$file1")
7487         [ $cur_ssize -eq $parent_ssize ] ||
7488                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
7489         cur_scount=$($LFS getstripe -c "$file1")
7490         [ $cur_scount -eq $parent_scount ] ||
7491                 error "migrate -R stripe_count $cur_scount != $parent_scount"
7492         echo "done."
7493
7494         # File currently set to -S 1M -c 1
7495
7496         # Ensure striping is preserved if -R is not set, and no stripe
7497         # count or size is specified
7498         echo -n "Verifying striping size preserved when not specified..."
7499         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
7500         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
7501                 error "cannot set stripe on parent directory"
7502         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7503                 error "migrate failed"
7504         cur_ssize=$($LFS getstripe -S "$file1")
7505         [ $cur_ssize -eq $orig_ssize ] ||
7506                 error "migrate by default $cur_ssize != $orig_ssize"
7507         echo "done."
7508
7509         # Ensure file name properly detected when final option has no argument
7510         echo -n "Verifying file name properly detected..."
7511         $LFS_MIGRATE -y "$file1" &> /dev/null ||
7512                 error "file name interpreted as option argument"
7513         echo "done."
7514
7515         # Clean up
7516         rm -f "$file1"
7517 }
7518 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
7519
7520 test_56wd() {
7521         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7522
7523         local file1=$DIR/$tdir/file1
7524
7525         echo -n "Creating test dir..."
7526         test_mkdir $DIR/$tdir || error "cannot create dir"
7527         echo "done."
7528
7529         echo -n "Creating test file..."
7530         touch $file1
7531         echo "done."
7532
7533         # Ensure 'lfs migrate' will fail by using a non-existent option,
7534         # and make sure rsync is not called to recover
7535         echo -n "Make sure --no-rsync option works..."
7536         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
7537                 grep -q 'refusing to fall back to rsync' ||
7538                 error "rsync was called with --no-rsync set"
7539         echo "done."
7540
7541         # Ensure rsync is called without trying 'lfs migrate' first
7542         echo -n "Make sure --rsync option works..."
7543         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
7544                 grep -q 'falling back to rsync' &&
7545                 error "lfs migrate was called with --rsync set"
7546         echo "done."
7547
7548         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
7549         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
7550                 grep -q 'at the same time' ||
7551                 error "--rsync and --no-rsync accepted concurrently"
7552         echo "done."
7553
7554         # Clean up
7555         rm -f $file1
7556 }
7557 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
7558
7559 test_56we() {
7560         local td=$DIR/$tdir
7561         local tf=$td/$tfile
7562
7563         test_mkdir $td || error "cannot create $td"
7564         touch $tf || error "cannot touch $tf"
7565
7566         echo -n "Make sure --non-direct|-D works..."
7567         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
7568                 grep -q "lfs migrate --non-direct" ||
7569                 error "--non-direct option cannot work correctly"
7570         $LFS_MIGRATE -y -D -v $tf 2>&1 |
7571                 grep -q "lfs migrate -D" ||
7572                 error "-D option cannot work correctly"
7573         echo "done."
7574 }
7575 run_test 56we "check lfs_migrate --non-direct|-D support"
7576
7577 test_56x() {
7578         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7579         check_swap_layouts_support
7580
7581         local dir=$DIR/$tdir
7582         local ref1=/etc/passwd
7583         local file1=$dir/file1
7584
7585         test_mkdir $dir || error "creating dir $dir"
7586         $LFS setstripe -c 2 $file1
7587         cp $ref1 $file1
7588         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
7589         stripe=$($LFS getstripe -c $file1)
7590         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7591         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7592
7593         # clean up
7594         rm -f $file1
7595 }
7596 run_test 56x "lfs migration support"
7597
7598 test_56xa() {
7599         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7600         check_swap_layouts_support
7601
7602         local dir=$DIR/$tdir/$testnum
7603
7604         test_mkdir -p $dir
7605
7606         local ref1=/etc/passwd
7607         local file1=$dir/file1
7608
7609         $LFS setstripe -c 2 $file1
7610         cp $ref1 $file1
7611         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
7612
7613         local stripe=$($LFS getstripe -c $file1)
7614
7615         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
7616         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
7617
7618         # clean up
7619         rm -f $file1
7620 }
7621 run_test 56xa "lfs migration --block support"
7622
7623 check_migrate_links() {
7624         local dir="$1"
7625         local file1="$dir/file1"
7626         local begin="$2"
7627         local count="$3"
7628         local runas="$4"
7629         local total_count=$(($begin + $count - 1))
7630         local symlink_count=10
7631         local uniq_count=10
7632
7633         if [ ! -f "$file1" ]; then
7634                 echo -n "creating initial file..."
7635                 $LFS setstripe -c 1 -S "512k" "$file1" ||
7636                         error "cannot setstripe initial file"
7637                 echo "done"
7638
7639                 echo -n "creating symlinks..."
7640                 for s in $(seq 1 $symlink_count); do
7641                         ln -s "$file1" "$dir/slink$s" ||
7642                                 error "cannot create symlinks"
7643                 done
7644                 echo "done"
7645
7646                 echo -n "creating nonlinked files..."
7647                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
7648                         error "cannot create nonlinked files"
7649                 echo "done"
7650         fi
7651
7652         # create hard links
7653         if [ ! -f "$dir/file$total_count" ]; then
7654                 echo -n "creating hard links $begin:$total_count..."
7655                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
7656                         /dev/null || error "cannot create hard links"
7657                 echo "done"
7658         fi
7659
7660         echo -n "checking number of hard links listed in xattrs..."
7661         local fid=$($LFS getstripe -F "$file1")
7662         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
7663
7664         echo "${#paths[*]}"
7665         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
7666                         skip "hard link list has unexpected size, skipping test"
7667         fi
7668         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
7669                         error "link names should exceed xattrs size"
7670         fi
7671
7672         echo -n "migrating files..."
7673         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
7674         local rc=$?
7675         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
7676         echo "done"
7677
7678         # make sure all links have been properly migrated
7679         echo -n "verifying files..."
7680         fid=$($LFS getstripe -F "$file1") ||
7681                 error "cannot get fid for file $file1"
7682         for i in $(seq 2 $total_count); do
7683                 local fid2=$($LFS getstripe -F $dir/file$i)
7684
7685                 [ "$fid2" == "$fid" ] ||
7686                         error "migrated hard link has mismatched FID"
7687         done
7688
7689         # make sure hard links were properly detected, and migration was
7690         # performed only once for the entire link set; nonlinked files should
7691         # also be migrated
7692         local actual=$(grep -c 'done' <<< "$migrate_out")
7693         local expected=$(($uniq_count + 1))
7694
7695         [ "$actual" -eq  "$expected" ] ||
7696                 error "hard links individually migrated ($actual != $expected)"
7697
7698         # make sure the correct number of hard links are present
7699         local hardlinks=$(stat -c '%h' "$file1")
7700
7701         [ $hardlinks -eq $total_count ] ||
7702                 error "num hard links $hardlinks != $total_count"
7703         echo "done"
7704
7705         return 0
7706 }
7707
7708 test_56xb() {
7709         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
7710                 skip "Need MDS version at least 2.10.55"
7711
7712         local dir="$DIR/$tdir"
7713
7714         test_mkdir "$dir" || error "cannot create dir $dir"
7715
7716         echo "testing lfs migrate mode when all links fit within xattrs"
7717         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
7718
7719         echo "testing rsync mode when all links fit within xattrs"
7720         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
7721
7722         echo "testing lfs migrate mode when all links do not fit within xattrs"
7723         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
7724
7725         echo "testing rsync mode when all links do not fit within xattrs"
7726         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
7727
7728         chown -R $RUNAS_ID $dir
7729         echo "testing non-root lfs migrate mode when not all links are in xattr"
7730         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
7731
7732         # clean up
7733         rm -rf $dir
7734 }
7735 run_test 56xb "lfs migration hard link support"
7736
7737 test_56xc() {
7738         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7739
7740         local dir="$DIR/$tdir"
7741
7742         test_mkdir "$dir" || error "cannot create dir $dir"
7743
7744         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7745         echo -n "Setting initial stripe for 20MB test file..."
7746         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7747                 error "cannot setstripe 20MB file"
7748         echo "done"
7749         echo -n "Sizing 20MB test file..."
7750         $TRUNCATE "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7751         echo "done"
7752         echo -n "Verifying small file autostripe count is 1..."
7753         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7754                 error "cannot migrate 20MB file"
7755         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7756                 error "cannot get stripe for $dir/20mb"
7757         [ $stripe_count -eq 1 ] ||
7758                 error "unexpected stripe count $stripe_count for 20MB file"
7759         rm -f "$dir/20mb"
7760         echo "done"
7761
7762         # Test 2: File is small enough to fit within the available space on
7763         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7764         # have at least an additional 1KB for each desired stripe for test 3
7765         echo -n "Setting stripe for 1GB test file..."
7766         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7767         echo "done"
7768         echo -n "Sizing 1GB test file..."
7769         # File size is 1GB + 3KB
7770         $TRUNCATE "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7771         echo "done"
7772
7773         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7774         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7775         if (( avail > 524288 * OSTCOUNT )); then
7776                 echo -n "Migrating 1GB file..."
7777                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7778                         error "cannot migrate 1GB file"
7779                 echo "done"
7780                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7781                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7782                         error "cannot getstripe for 1GB file"
7783                 [ $stripe_count -eq 2 ] ||
7784                         error "unexpected stripe count $stripe_count != 2"
7785                 echo "done"
7786         fi
7787
7788         # Test 3: File is too large to fit within the available space on
7789         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7790         if [ $OSTCOUNT -ge 3 ]; then
7791                 # The required available space is calculated as
7792                 # file size (1GB + 3KB) / OST count (3).
7793                 local kb_per_ost=349526
7794
7795                 echo -n "Migrating 1GB file with limit..."
7796                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7797                         error "cannot migrate 1GB file with limit"
7798                 echo "done"
7799
7800                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7801                 echo -n "Verifying 1GB autostripe count with limited space..."
7802                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7803                         error "unexpected stripe count $stripe_count (min 3)"
7804                 echo "done"
7805         fi
7806
7807         # clean up
7808         rm -rf $dir
7809 }
7810 run_test 56xc "lfs migration autostripe"
7811
7812 test_56xd() {
7813         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7814
7815         local dir=$DIR/$tdir
7816         local f_mgrt=$dir/$tfile.mgrt
7817         local f_yaml=$dir/$tfile.yaml
7818         local f_copy=$dir/$tfile.copy
7819         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7820         local layout_copy="-c 2 -S 2M -i 1"
7821         local yamlfile=$dir/yamlfile
7822         local layout_before;
7823         local layout_after;
7824
7825         test_mkdir "$dir" || error "cannot create dir $dir"
7826         $LFS setstripe $layout_yaml $f_yaml ||
7827                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7828         $LFS getstripe --yaml $f_yaml > $yamlfile
7829         $LFS setstripe $layout_copy $f_copy ||
7830                 error "cannot setstripe $f_copy with layout $layout_copy"
7831         touch $f_mgrt
7832         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7833
7834         # 1. test option --yaml
7835         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7836                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7837         layout_before=$(get_layout_param $f_yaml)
7838         layout_after=$(get_layout_param $f_mgrt)
7839         [ "$layout_after" == "$layout_before" ] ||
7840                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7841
7842         # 2. test option --copy
7843         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7844                 error "cannot migrate $f_mgrt with --copy $f_copy"
7845         layout_before=$(get_layout_param $f_copy)
7846         layout_after=$(get_layout_param $f_mgrt)
7847         [ "$layout_after" == "$layout_before" ] ||
7848                 error "lfs_migrate --copy: $layout_after != $layout_before"
7849 }
7850 run_test 56xd "check lfs_migrate --yaml and --copy support"
7851
7852 test_56xe() {
7853         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7854
7855         local dir=$DIR/$tdir
7856         local f_comp=$dir/$tfile
7857         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7858         local layout_before=""
7859         local layout_after=""
7860
7861         test_mkdir "$dir" || error "cannot create dir $dir"
7862         $LFS setstripe $layout $f_comp ||
7863                 error "cannot setstripe $f_comp with layout $layout"
7864         layout_before=$(get_layout_param $f_comp)
7865         dd if=/dev/zero of=$f_comp bs=1M count=4
7866
7867         # 1. migrate a comp layout file by lfs_migrate
7868         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7869         layout_after=$(get_layout_param $f_comp)
7870         [ "$layout_before" == "$layout_after" ] ||
7871                 error "lfs_migrate: $layout_before != $layout_after"
7872
7873         # 2. migrate a comp layout file by lfs migrate
7874         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7875         layout_after=$(get_layout_param $f_comp)
7876         [ "$layout_before" == "$layout_after" ] ||
7877                 error "lfs migrate: $layout_before != $layout_after"
7878 }
7879 run_test 56xe "migrate a composite layout file"
7880
7881 test_56xf() {
7882         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7883
7884         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7885                 skip "Need server version at least 2.13.53"
7886
7887         local dir=$DIR/$tdir
7888         local f_comp=$dir/$tfile
7889         local layout="-E 1M -c1 -E -1 -c2"
7890         local fid_before=""
7891         local fid_after=""
7892
7893         test_mkdir "$dir" || error "cannot create dir $dir"
7894         $LFS setstripe $layout $f_comp ||
7895                 error "cannot setstripe $f_comp with layout $layout"
7896         fid_before=$($LFS getstripe --fid $f_comp)
7897         dd if=/dev/zero of=$f_comp bs=1M count=4
7898
7899         # 1. migrate a comp layout file to a comp layout
7900         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7901         fid_after=$($LFS getstripe --fid $f_comp)
7902         [ "$fid_before" == "$fid_after" ] ||
7903                 error "comp-to-comp migrate: $fid_before != $fid_after"
7904
7905         # 2. migrate a comp layout file to a plain layout
7906         $LFS migrate -c2 $f_comp ||
7907                 error "cannot migrate $f_comp by lfs migrate"
7908         fid_after=$($LFS getstripe --fid $f_comp)
7909         [ "$fid_before" == "$fid_after" ] ||
7910                 error "comp-to-plain migrate: $fid_before != $fid_after"
7911
7912         # 3. migrate a plain layout file to a comp layout
7913         $LFS migrate $layout $f_comp ||
7914                 error "cannot migrate $f_comp by lfs migrate"
7915         fid_after=$($LFS getstripe --fid $f_comp)
7916         [ "$fid_before" == "$fid_after" ] ||
7917                 error "plain-to-comp migrate: $fid_before != $fid_after"
7918 }
7919 run_test 56xf "FID is not lost during migration of a composite layout file"
7920
7921 check_file_ost_range() {
7922         local file="$1"
7923         shift
7924         local range="$*"
7925         local -a file_range
7926         local idx
7927
7928         file_range=($($LFS getstripe -y "$file" |
7929                 awk '/l_ost_idx:/ { print $NF }'))
7930
7931         if [[ "${#file_range[@]}" = 0 ]]; then
7932                 echo "No osts found for $file"
7933                 return 1
7934         fi
7935
7936         for idx in "${file_range[@]}"; do
7937                 [[ " $range " =~ " $idx " ]] ||
7938                         return 1
7939         done
7940
7941         return 0
7942 }
7943
7944 sub_test_56xg() {
7945         local stripe_opt="$1"
7946         local pool="$2"
7947         shift 2
7948         local pool_ostidx="$(seq $* | tr '\n' ' ')"
7949
7950         $LFS migrate $stripe_opt -p $pool $DIR/$tfile ||
7951                 error "Fail to migrate $tfile on $pool"
7952         [[ "$($LFS getstripe -p $DIR/$tfile)" = "$pool" ]] ||
7953                 error "$tfile is not in pool $pool"
7954         check_file_ost_range "$DIR/$tfile" $pool_ostidx ||
7955                 error "$tfile osts mismatch with pool $pool (osts $pool_ostidx)"
7956 }
7957
7958 test_56xg() {
7959         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
7960         [[ $OSTCOUNT -ge 2 ]] || skip "needs >= 2 OSTs"
7961         [[ $MDS1_VERSION -gt $(version_code 2.14.52) ]] ||
7962                 skip "Need MDS version newer than 2.14.52"
7963
7964         local -a pool_names=("${TESTNAME}_0" "${TESTNAME}_1" "${TESTNAME}_2")
7965         local -a pool_ranges=("0 0" "1 1" "0 1")
7966
7967         # init pools
7968         for i in "${!pool_names[@]}"; do
7969                 pool_add ${pool_names[$i]} ||
7970                         error "pool_add failed (pool: ${pool_names[$i]})"
7971                 pool_add_targets ${pool_names[$i]} ${pool_ranges[$i]} ||
7972                         error "pool_add_targets failed (pool: ${pool_names[$i]})"
7973         done
7974
7975         # init the file to migrate
7976         $LFS setstripe -c1 -i1 $DIR/$tfile ||
7977                 error "Unable to create $tfile on OST1"
7978         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=4 status=none ||
7979                 error "Unable to write on $tfile"
7980
7981         echo "1. migrate $tfile on pool ${pool_names[0]}"
7982         sub_test_56xg "-c-1" "${pool_names[0]}" ${pool_ranges[0]}
7983
7984         echo "2. migrate $tfile on pool ${pool_names[2]}"
7985         sub_test_56xg "-c-1 -S2M" "${pool_names[2]}" ${pool_ranges[2]}
7986
7987         echo "3. migrate $tfile on pool ${pool_names[1]}"
7988         sub_test_56xg "-n -c-1" "${pool_names[1]}" ${pool_ranges[1]}
7989
7990         echo "4. migrate $tfile on pool ${pool_names[2]} with default stripe parameters"
7991         sub_test_56xg "" "${pool_names[2]}" ${pool_ranges[2]}
7992         echo
7993
7994         # Clean pools
7995         destroy_test_pools ||
7996                 error "pool_destroy failed"
7997 }
7998 run_test 56xg "lfs migrate pool support"
7999
8000 test_56y() {
8001         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
8002                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
8003
8004         local res=""
8005         local dir=$DIR/$tdir
8006         local f1=$dir/file1
8007         local f2=$dir/file2
8008
8009         test_mkdir -p $dir || error "creating dir $dir"
8010         touch $f1 || error "creating std file $f1"
8011         $MULTIOP $f2 H2c || error "creating released file $f2"
8012
8013         # a directory can be raid0, so ask only for files
8014         res=$($LFS find $dir -L raid0 -type f | wc -l)
8015         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
8016
8017         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
8018         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
8019
8020         # only files can be released, so no need to force file search
8021         res=$($LFS find $dir -L released)
8022         [[ $res == $f2 ]] || error "search released: found $res != $f2"
8023
8024         res=$($LFS find $dir -type f \! -L released)
8025         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
8026 }
8027 run_test 56y "lfs find -L raid0|released"
8028
8029 test_56z() { # LU-4824
8030         # This checks to make sure 'lfs find' continues after errors
8031         # There are two classes of errors that should be caught:
8032         # - If multiple paths are provided, all should be searched even if one
8033         #   errors out
8034         # - If errors are encountered during the search, it should not terminate
8035         #   early
8036         local dir=$DIR/$tdir
8037         local i
8038
8039         test_mkdir $dir
8040         for i in d{0..9}; do
8041                 test_mkdir $dir/$i
8042                 touch $dir/$i/$tfile
8043         done
8044         $LFS find $DIR/non_existent_dir $dir &&
8045                 error "$LFS find did not return an error"
8046         # Make a directory unsearchable. This should NOT be the last entry in
8047         # directory order.  Arbitrarily pick the 6th entry
8048         chmod 700 $($LFS find $dir -type d | sed '6!d')
8049
8050         $RUNAS $LFS find $DIR/non_existent $dir
8051         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
8052
8053         # The user should be able to see 10 directories and 9 files
8054         (( count == 19 )) ||
8055                 error "$LFS find found $count != 19 entries after error"
8056 }
8057 run_test 56z "lfs find should continue after an error"
8058
8059 test_56aa() { # LU-5937
8060         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
8061
8062         local dir=$DIR/$tdir
8063
8064         mkdir $dir
8065         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
8066
8067         createmany -o $dir/striped_dir/${tfile}- 1024
8068         local dirs=$($LFS find --size +8k $dir/)
8069
8070         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
8071 }
8072 run_test 56aa "lfs find --size under striped dir"
8073
8074 test_56ab() { # LU-10705
8075         test_mkdir $DIR/$tdir
8076         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
8077         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
8078         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
8079         # Flush writes to ensure valid blocks.  Need to be more thorough for
8080         # ZFS, since blocks are not allocated/returned to client immediately.
8081         sync_all_data
8082         wait_zfs_commit ost1 2
8083         cancel_lru_locks osc
8084         ls -ls $DIR/$tdir
8085
8086         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
8087
8088         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
8089
8090         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
8091         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
8092
8093         rm -f $DIR/$tdir/$tfile.[123]
8094 }
8095 run_test 56ab "lfs find --blocks"
8096
8097 # LU-11188
8098 test_56aca() {
8099         local dir="$DIR/$tdir"
8100         local perms=(001 002 003 004 005 006 007
8101                      010 020 030 040 050 060 070
8102                      100 200 300 400 500 600 700
8103                      111 222 333 444 555 666 777)
8104         local perm_minus=(8 8 4 8 4 4 2
8105                           8 8 4 8 4 4 2
8106                           8 8 4 8 4 4 2
8107                           4 4 2 4 2 2 1)
8108         local perm_slash=(8  8 12  8 12 12 14
8109                           8  8 12  8 12 12 14
8110                           8  8 12  8 12 12 14
8111                          16 16 24 16 24 24 28)
8112
8113         test_mkdir "$dir"
8114         for perm in ${perms[*]}; do
8115                 touch "$dir/$tfile.$perm"
8116                 chmod $perm "$dir/$tfile.$perm"
8117         done
8118
8119         for ((i = 0; i < ${#perms[*]}; i++)); do
8120                 local num=$($LFS find $dir -perm ${perms[i]} | wc -l)
8121                 (( $num == 1 )) ||
8122                         error "lfs find -perm ${perms[i]}:"\
8123                               "$num != 1"
8124
8125                 num=$($LFS find $dir -perm -${perms[i]} -type f| wc -l)
8126                 (( $num == ${perm_minus[i]} )) ||
8127                         error "lfs find -perm -${perms[i]}:"\
8128                               "$num != ${perm_minus[i]}"
8129
8130                 num=$($LFS find $dir -perm /${perms[i]} -type f| wc -l)
8131                 (( $num == ${perm_slash[i]} )) ||
8132                         error "lfs find -perm /${perms[i]}:"\
8133                               "$num != ${perm_slash[i]}"
8134         done
8135 }
8136 run_test 56aca "check lfs find -perm with octal representation"
8137
8138 test_56acb() {
8139         local dir=$DIR/$tdir
8140         # p is the permission of write and execute for user, group and other
8141         # without the umask. It is used to test +wx.
8142         local p=$(printf "%o" "$((0333 & ~$(umask)))")
8143         local perms=(1000 000 2000 4000 $p 644 111 110 100 004)
8144         local symbolic=(+t  a+t u+t g+t o+t
8145                         g+s u+s o+s +s o+sr
8146                         o=r,ug+o,u+w
8147                         u+ g+ o+ a+ ugo+
8148                         u- g- o- a- ugo-
8149                         u= g= o= a= ugo=
8150                         o=r,ug+o,u+w u=r,a+u,u+w
8151                         g=r,ugo=g,u+w u+x,+X +X
8152                         u+x,u+X u+X u+x,g+X o+r,+X
8153                         u+x,go+X +wx +rwx)
8154
8155         test_mkdir $dir
8156         for perm in ${perms[*]}; do
8157                 touch "$dir/$tfile.$perm"
8158                 chmod $perm "$dir/$tfile.$perm"
8159         done
8160
8161         for (( i = 0; i < ${#symbolic[*]}; i++ )); do
8162                 local num=$($LFS find $dir -perm ${symbolic[i]} | wc -l)
8163
8164                 (( $num == 1 )) ||
8165                         error "lfs find $dir -perm ${symbolic[i]}: $num != 1"
8166         done
8167 }
8168 run_test 56acb "check lfs find -perm with symbolic representation"
8169
8170 test_56acc() {
8171         local dir=$DIR/$tdir
8172         local tests="17777 787 789 abcd
8173                 ug=uu ug=a ug=gu uo=ou urw
8174                 u+xg+x a=r,u+x,"
8175
8176         test_mkdir $dir
8177         for err in $tests; do
8178                 if $LFS find $dir -perm $err 2>/dev/null; then
8179                         error "lfs find -perm $err: parsing should have failed"
8180                 fi
8181         done
8182 }
8183 run_test 56acc "check parsing error for lfs find -perm"
8184
8185 test_56ba() {
8186         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
8187                 skip "Need MDS version at least 2.10.50"
8188
8189         # Create composite files with one component
8190         local dir=$DIR/$tdir
8191
8192         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
8193         # Create composite files with three components
8194         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
8195         # Create non-composite files
8196         createmany -o $dir/${tfile}- 10
8197
8198         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
8199
8200         [[ $nfiles == 10 ]] ||
8201                 error "lfs find -E 1M found $nfiles != 10 files"
8202
8203         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
8204         [[ $nfiles == 25 ]] ||
8205                 error "lfs find ! -E 1M found $nfiles != 25 files"
8206
8207         # All files have a component that starts at 0
8208         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
8209         [[ $nfiles == 35 ]] ||
8210                 error "lfs find --component-start 0 - $nfiles != 35 files"
8211
8212         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
8213         [[ $nfiles == 15 ]] ||
8214                 error "lfs find --component-start 2M - $nfiles != 15 files"
8215
8216         # All files created here have a componenet that does not starts at 2M
8217         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
8218         [[ $nfiles == 35 ]] ||
8219                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
8220
8221         # Find files with a specified number of components
8222         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
8223         [[ $nfiles == 15 ]] ||
8224                 error "lfs find --component-count 3 - $nfiles != 15 files"
8225
8226         # Remember non-composite files have a component count of zero
8227         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
8228         [[ $nfiles == 10 ]] ||
8229                 error "lfs find --component-count 0 - $nfiles != 10 files"
8230
8231         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
8232         [[ $nfiles == 20 ]] ||
8233                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
8234
8235         # All files have a flag called "init"
8236         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
8237         [[ $nfiles == 35 ]] ||
8238                 error "lfs find --component-flags init - $nfiles != 35 files"
8239
8240         # Multi-component files will have a component not initialized
8241         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
8242         [[ $nfiles == 15 ]] ||
8243                 error "lfs find !--component-flags init - $nfiles != 15 files"
8244
8245         rm -rf $dir
8246
8247 }
8248 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
8249
8250 test_56ca() {
8251         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
8252                 skip "Need MDS version at least 2.10.57"
8253
8254         local td=$DIR/$tdir
8255         local tf=$td/$tfile
8256         local dir
8257         local nfiles
8258         local cmd
8259         local i
8260         local j
8261
8262         # create mirrored directories and mirrored files
8263         mkdir $td || error "mkdir $td failed"
8264         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
8265         createmany -o $tf- 10 || error "create $tf- failed"
8266
8267         for i in $(seq 2); do
8268                 dir=$td/dir$i
8269                 mkdir $dir || error "mkdir $dir failed"
8270                 $LFS mirror create -N$((3 + i)) $dir ||
8271                         error "create mirrored dir $dir failed"
8272                 createmany -o $dir/$tfile- 10 ||
8273                         error "create $dir/$tfile- failed"
8274         done
8275
8276         # change the states of some mirrored files
8277         echo foo > $tf-6
8278         for i in $(seq 2); do
8279                 dir=$td/dir$i
8280                 for j in $(seq 4 9); do
8281                         echo foo > $dir/$tfile-$j
8282                 done
8283         done
8284
8285         # find mirrored files with specific mirror count
8286         cmd="$LFS find --mirror-count 3 --type f $td"
8287         nfiles=$($cmd | wc -l)
8288         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
8289
8290         cmd="$LFS find ! --mirror-count 3 --type f $td"
8291         nfiles=$($cmd | wc -l)
8292         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
8293
8294         cmd="$LFS find --mirror-count +2 --type f $td"
8295         nfiles=$($cmd | wc -l)
8296         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8297
8298         cmd="$LFS find --mirror-count -6 --type f $td"
8299         nfiles=$($cmd | wc -l)
8300         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8301
8302         # find mirrored files with specific file state
8303         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
8304         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
8305
8306         cmd="$LFS find --mirror-state=ro --type f $td"
8307         nfiles=$($cmd | wc -l)
8308         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
8309
8310         cmd="$LFS find ! --mirror-state=ro --type f $td"
8311         nfiles=$($cmd | wc -l)
8312         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8313
8314         cmd="$LFS find --mirror-state=wp --type f $td"
8315         nfiles=$($cmd | wc -l)
8316         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
8317
8318         cmd="$LFS find ! --mirror-state=sp --type f $td"
8319         nfiles=$($cmd | wc -l)
8320         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
8321 }
8322 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
8323
8324 test_56da() { # LU-14179
8325         local path=$DIR/$tdir
8326
8327         test_mkdir $path
8328         cd $path
8329
8330         local longdir=$(str_repeat 'a' 255)
8331
8332         for i in {1..15}; do
8333                 path=$path/$longdir
8334                 test_mkdir $longdir
8335                 cd $longdir
8336         done
8337
8338         local len=${#path}
8339         local lastdir=$(str_repeat 'a' $((4096 - 1 - $len - 1)))
8340
8341         test_mkdir $lastdir
8342         cd $lastdir
8343         # PATH_MAX-1
8344         (( ${#PWD} == 4095 )) || error "bad PWD length ${#PWD}, expect 4095"
8345
8346         # NAME_MAX
8347         touch $(str_repeat 'f' 255)
8348
8349         $LFS find $DIR/$tdir --type d |& grep "lfs find: error" &&
8350                 error "lfs find reported an error"
8351
8352         rm -rf $DIR/$tdir
8353 }
8354 run_test 56da "test lfs find with long paths"
8355
8356 test_56ea() { #LU-10378
8357         local path=$DIR/$tdir
8358         local pool=$TESTNAME
8359
8360         # Create ost pool
8361         pool_add $pool || error "pool_add $pool failed"
8362         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8363                 error "adding targets to $pool failed"
8364
8365         # Set default pool on directory before creating file
8366         mkdir $path || error "mkdir $path failed"
8367         $LFS setstripe -p $pool $path ||
8368                 error "set OST pool on $pool failed"
8369         touch $path/$tfile || error "touch $path/$tfile failed"
8370
8371         # Compare basic file attributes from -printf and stat
8372         local attr_printf=$($LFS find $path/$tfile -printf "%A@ %T@ %C@ %U %G")
8373         local attr_stat=$(stat -c "%X %Y %Z %u %g" $path/$tfile)
8374
8375         [[ "${attr_printf}" == "${attr_stat}" ]] ||
8376                 error "Attrs from lfs find and stat don't match"
8377
8378         # Compare Lustre attributes from lfs find and lfs getstripe
8379         local lattr_printf=$($LFS find $path/$tfile -printf "%Lc %LS %Li %Lp")
8380         local str_cnt=$($LFS getstripe --stripe-count $path/$tfile)
8381         local str_size=$($LFS getstripe --stripe-size $path/$tfile)
8382         local str_idx=$($LFS getstripe --stripe-index $path/$tfile)
8383         local fpool=$($LFS getstripe --pool $path/$tfile)
8384         local lattr_getstr="${str_cnt} ${str_size} ${str_idx} ${fpool}"
8385
8386         [[ "${lattr_printf}" == "${lattr_getstr}" ]] ||
8387                 error "Attrs from lfs find and lfs getstripe don't match"
8388
8389         # Verify behavior for unknown escape/format sequences
8390         local esc_printf=$($LFS find $path/$tfile -printf '\\ %% \Q %Q')
8391
8392         [[ "${esc_printf}" == '\ % \Q %Q' ]] ||
8393                 error "Escape/format codes don't match"
8394 }
8395 run_test 56ea "test lfs find -printf option"
8396
8397 test_57a() {
8398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8399         # note test will not do anything if MDS is not local
8400         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8401                 skip_env "ldiskfs only test"
8402         fi
8403         remote_mds_nodsh && skip "remote MDS with nodsh"
8404
8405         local MNTDEV="osd*.*MDT*.mntdev"
8406         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
8407         [ -z "$DEV" ] && error "can't access $MNTDEV"
8408         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
8409                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
8410                         error "can't access $DEV"
8411                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
8412                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
8413                 rm $TMP/t57a.dump
8414         done
8415 }
8416 run_test 57a "verify MDS filesystem created with large inodes =="
8417
8418 test_57b() {
8419         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8420         if [ "$mds1_FSTYPE" != ldiskfs ]; then
8421                 skip_env "ldiskfs only test"
8422         fi
8423         remote_mds_nodsh && skip "remote MDS with nodsh"
8424
8425         local dir=$DIR/$tdir
8426         local filecount=100
8427         local file1=$dir/f1
8428         local fileN=$dir/f$filecount
8429
8430         rm -rf $dir || error "removing $dir"
8431         test_mkdir -c1 $dir
8432         local mdtidx=$($LFS getstripe -m $dir)
8433         local mdtname=MDT$(printf %04x $mdtidx)
8434         local facet=mds$((mdtidx + 1))
8435
8436         echo "mcreating $filecount files"
8437         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
8438
8439         # verify that files do not have EAs yet
8440         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
8441                 error "$file1 has an EA"
8442         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
8443                 error "$fileN has an EA"
8444
8445         sync
8446         sleep 1
8447         df $dir  #make sure we get new statfs data
8448         local mdsfree=$(do_facet $facet \
8449                         lctl get_param -n osd*.*$mdtname.kbytesfree)
8450         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8451         local file
8452
8453         echo "opening files to create objects/EAs"
8454         for file in $(seq -f $dir/f%g 1 $filecount); do
8455                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
8456                         error "opening $file"
8457         done
8458
8459         # verify that files have EAs now
8460         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
8461         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
8462
8463         sleep 1  #make sure we get new statfs data
8464         df $dir
8465         local mdsfree2=$(do_facet $facet \
8466                          lctl get_param -n osd*.*$mdtname.kbytesfree)
8467         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
8468
8469         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
8470                 if [ "$mdsfree" != "$mdsfree2" ]; then
8471                         error "MDC before $mdcfree != after $mdcfree2"
8472                 else
8473                         echo "MDC before $mdcfree != after $mdcfree2"
8474                         echo "unable to confirm if MDS has large inodes"
8475                 fi
8476         fi
8477         rm -rf $dir
8478 }
8479 run_test 57b "default LOV EAs are stored inside large inodes ==="
8480
8481 test_58() {
8482         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8483         [ -z "$(which wiretest 2>/dev/null)" ] &&
8484                         skip_env "could not find wiretest"
8485
8486         wiretest
8487 }
8488 run_test 58 "verify cross-platform wire constants =============="
8489
8490 test_59() {
8491         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8492
8493         echo "touch 130 files"
8494         createmany -o $DIR/f59- 130
8495         echo "rm 130 files"
8496         unlinkmany $DIR/f59- 130
8497         sync
8498         # wait for commitment of removal
8499         wait_delete_completed
8500 }
8501 run_test 59 "verify cancellation of llog records async ========="
8502
8503 TEST60_HEAD="test_60 run $RANDOM"
8504 test_60a() {
8505         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8506         remote_mgs_nodsh && skip "remote MGS with nodsh"
8507         do_facet mgs "! which run-llog.sh &> /dev/null" &&
8508                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
8509                         skip_env "missing subtest run-llog.sh"
8510
8511         log "$TEST60_HEAD - from kernel mode"
8512         do_facet mgs "$LCTL dk > /dev/null"
8513         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
8514         do_facet mgs $LCTL dk > $TMP/$tfile
8515
8516         # LU-6388: test llog_reader
8517         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
8518         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
8519         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
8520                         skip_env "missing llog_reader"
8521         local fstype=$(facet_fstype mgs)
8522         [ $fstype != ldiskfs -a $fstype != zfs ] &&
8523                 skip_env "Only for ldiskfs or zfs type mgs"
8524
8525         local mntpt=$(facet_mntpt mgs)
8526         local mgsdev=$(mgsdevname 1)
8527         local fid_list
8528         local fid
8529         local rec_list
8530         local rec
8531         local rec_type
8532         local obj_file
8533         local path
8534         local seq
8535         local oid
8536         local pass=true
8537
8538         #get fid and record list
8539         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
8540                 tail -n 4))
8541         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
8542                 tail -n 4))
8543         #remount mgs as ldiskfs or zfs type
8544         stop mgs || error "stop mgs failed"
8545         mount_fstype mgs || error "remount mgs failed"
8546         for ((i = 0; i < ${#fid_list[@]}; i++)); do
8547                 fid=${fid_list[i]}
8548                 rec=${rec_list[i]}
8549                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
8550                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
8551                 oid=$((16#$oid))
8552
8553                 case $fstype in
8554                         ldiskfs )
8555                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
8556                         zfs )
8557                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
8558                 esac
8559                 echo "obj_file is $obj_file"
8560                 do_facet mgs $llog_reader $obj_file
8561
8562                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
8563                         awk '{ print $3 }' | sed -e "s/^type=//g")
8564                 if [ $rec_type != $rec ]; then
8565                         echo "FAILED test_60a wrong record type $rec_type," \
8566                               "should be $rec"
8567                         pass=false
8568                         break
8569                 fi
8570
8571                 #check obj path if record type is LLOG_LOGID_MAGIC
8572                 if [ "$rec" == "1064553b" ]; then
8573                         path=$(do_facet mgs $llog_reader $obj_file |
8574                                 grep "path=" | awk '{ print $NF }' |
8575                                 sed -e "s/^path=//g")
8576                         if [ $obj_file != $mntpt/$path ]; then
8577                                 echo "FAILED test_60a wrong obj path" \
8578                                       "$montpt/$path, should be $obj_file"
8579                                 pass=false
8580                                 break
8581                         fi
8582                 fi
8583         done
8584         rm -f $TMP/$tfile
8585         #restart mgs before "error", otherwise it will block the next test
8586         stop mgs || error "stop mgs failed"
8587         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
8588         $pass || error "test failed, see FAILED test_60a messages for specifics"
8589 }
8590 run_test 60a "llog_test run from kernel module and test llog_reader"
8591
8592 test_60b() { # bug 6411
8593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8594
8595         dmesg > $DIR/$tfile
8596         LLOG_COUNT=$(do_facet mgs dmesg |
8597                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
8598                           /llog_[a-z]*.c:[0-9]/ {
8599                                 if (marker)
8600                                         from_marker++
8601                                 from_begin++
8602                           }
8603                           END {
8604                                 if (marker)
8605                                         print from_marker
8606                                 else
8607                                         print from_begin
8608                           }")
8609
8610         [[ $LLOG_COUNT -gt 120 ]] &&
8611                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
8612 }
8613 run_test 60b "limit repeated messages from CERROR/CWARN"
8614
8615 test_60c() {
8616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8617
8618         echo "create 5000 files"
8619         createmany -o $DIR/f60c- 5000
8620 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
8621         lctl set_param fail_loc=0x80000137
8622         unlinkmany $DIR/f60c- 5000
8623         lctl set_param fail_loc=0
8624 }
8625 run_test 60c "unlink file when mds full"
8626
8627 test_60d() {
8628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8629
8630         SAVEPRINTK=$(lctl get_param -n printk)
8631         # verify "lctl mark" is even working"
8632         MESSAGE="test message ID $RANDOM $$"
8633         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8634         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
8635
8636         lctl set_param printk=0 || error "set lnet.printk failed"
8637         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
8638         MESSAGE="new test message ID $RANDOM $$"
8639         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
8640         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
8641         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
8642
8643         lctl set_param -n printk="$SAVEPRINTK"
8644 }
8645 run_test 60d "test printk console message masking"
8646
8647 test_60e() {
8648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8649         remote_mds_nodsh && skip "remote MDS with nodsh"
8650
8651         touch $DIR/$tfile
8652 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
8653         do_facet mds1 lctl set_param fail_loc=0x15b
8654         rm $DIR/$tfile
8655 }
8656 run_test 60e "no space while new llog is being created"
8657
8658 test_60f() {
8659         local old_path=$($LCTL get_param -n debug_path)
8660
8661         stack_trap "$LCTL set_param debug_path=$old_path"
8662         stack_trap "rm -f $TMP/$tfile*"
8663         rm -f $TMP/$tfile* 2> /dev/null
8664         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
8665         $LCTL set_param debug_path=$TMP/$tfile fail_loc=0x8000050e
8666         test_mkdir $DIR/$tdir
8667         # retry in case the open is cached and not released
8668         for (( i = 0; i < 100 && $(ls $TMP/$tfile* | wc -l) == 0; i++ )); do
8669                 echo $i > $DIR/$tdir/$tfile.$i && cat $DIR/$tdir/$tfile.$i
8670                 sleep 0.1
8671         done
8672         ls $TMP/$tfile*
8673         (( $(ls $TMP/$tfile* | wc -l) > 0 )) || error "$TMP/$tfile not dumped"
8674 }
8675 run_test 60f "change debug_path works"
8676
8677 test_60g() {
8678         local pid
8679         local i
8680
8681         test_mkdir -c $MDSCOUNT $DIR/$tdir
8682
8683         (
8684                 local index=0
8685                 while true; do
8686                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
8687                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
8688                                 2>/dev/null
8689                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
8690                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
8691                         index=$((index + 1))
8692                 done
8693         ) &
8694
8695         pid=$!
8696
8697         for i in {0..100}; do
8698                 # define OBD_FAIL_OSD_TXN_START    0x19a
8699                 local index=$((i % MDSCOUNT + 1))
8700
8701                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
8702                         > /dev/null
8703                 sleep 0.01
8704         done
8705
8706         kill -9 $pid
8707
8708         for i in $(seq $MDSCOUNT); do
8709                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
8710         done
8711
8712         mkdir $DIR/$tdir/new || error "mkdir failed"
8713         rmdir $DIR/$tdir/new || error "rmdir failed"
8714
8715         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
8716                 -t namespace
8717         for i in $(seq $MDSCOUNT); do
8718                 wait_update_facet mds$i "$LCTL get_param -n \
8719                         mdd.$(facet_svc mds$i).lfsck_namespace |
8720                         awk '/^status/ { print \\\$2 }'" "completed"
8721         done
8722
8723         ls -R $DIR/$tdir
8724         rm -rf $DIR/$tdir || error "rmdir failed"
8725 }
8726 run_test 60g "transaction abort won't cause MDT hung"
8727
8728 test_60h() {
8729         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
8730                 skip "Need MDS version at least 2.12.52"
8731         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
8732
8733         local f
8734
8735         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
8736         #define OBD_FAIL_MDS_STRIPE_FID          0x189
8737         for fail_loc in 0x80000188 0x80000189; do
8738                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
8739                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
8740                         error "mkdir $dir-$fail_loc failed"
8741                 for i in {0..10}; do
8742                         # create may fail on missing stripe
8743                         echo $i > $DIR/$tdir-$fail_loc/$i
8744                 done
8745                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8746                         error "getdirstripe $tdir-$fail_loc failed"
8747                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
8748                         error "migrate $tdir-$fail_loc failed"
8749                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
8750                         error "getdirstripe $tdir-$fail_loc failed"
8751                 pushd $DIR/$tdir-$fail_loc
8752                 for f in *; do
8753                         echo $f | cmp $f - || error "$f data mismatch"
8754                 done
8755                 popd
8756                 rm -rf $DIR/$tdir-$fail_loc
8757         done
8758 }
8759 run_test 60h "striped directory with missing stripes can be accessed"
8760
8761 function t60i_load() {
8762         mkdir $DIR/$tdir
8763         #define OBD_FAIL_LLOG_PAUSE_AFTER_PAD               0x131c
8764         $LCTL set_param fail_loc=0x131c fail_val=1
8765         for ((i=0; i<5000; i++)); do
8766                 touch $DIR/$tdir/f$i
8767         done
8768 }
8769
8770 test_60i() {
8771         changelog_register || error "changelog_register failed"
8772         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
8773         changelog_users $SINGLEMDS | grep -q $cl_user ||
8774                 error "User $cl_user not found in changelog_users"
8775         changelog_chmask "ALL"
8776         t60i_load &
8777         local PID=$!
8778         for((i=0; i<100; i++)); do
8779                 changelog_dump >/dev/null ||
8780                         error "can't read changelog"
8781         done
8782         kill $PID
8783         wait $PID
8784         changelog_deregister || error "changelog_deregister failed"
8785         $LCTL set_param fail_loc=0
8786 }
8787 run_test 60i "llog: new record vs reader race"
8788
8789 test_61a() {
8790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8791
8792         f="$DIR/f61"
8793         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
8794         cancel_lru_locks osc
8795         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
8796         sync
8797 }
8798 run_test 61a "mmap() writes don't make sync hang ================"
8799
8800 test_61b() {
8801         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
8802 }
8803 run_test 61b "mmap() of unstriped file is successful"
8804
8805 # bug 2330 - insufficient obd_match error checking causes LBUG
8806 test_62() {
8807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8808
8809         f="$DIR/f62"
8810         echo foo > $f
8811         cancel_lru_locks osc
8812         lctl set_param fail_loc=0x405
8813         cat $f && error "cat succeeded, expect -EIO"
8814         lctl set_param fail_loc=0
8815 }
8816 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
8817 # match every page all of the time.
8818 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
8819
8820 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
8821 # Though this test is irrelevant anymore, it helped to reveal some
8822 # other grant bugs (LU-4482), let's keep it.
8823 test_63a() {   # was test_63
8824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8825
8826         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
8827
8828         for i in `seq 10` ; do
8829                 dd if=/dev/zero of=$DIR/f63 bs=8k &
8830                 sleep 5
8831                 kill $!
8832                 sleep 1
8833         done
8834
8835         rm -f $DIR/f63 || true
8836 }
8837 run_test 63a "Verify oig_wait interruption does not crash ======="
8838
8839 # bug 2248 - async write errors didn't return to application on sync
8840 # bug 3677 - async write errors left page locked
8841 test_63b() {
8842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8843
8844         debugsave
8845         lctl set_param debug=-1
8846
8847         # ensure we have a grant to do async writes
8848         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
8849         rm $DIR/$tfile
8850
8851         sync    # sync lest earlier test intercept the fail_loc
8852
8853         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
8854         lctl set_param fail_loc=0x80000406
8855         $MULTIOP $DIR/$tfile Owy && \
8856                 error "sync didn't return ENOMEM"
8857         sync; sleep 2; sync     # do a real sync this time to flush page
8858         lctl get_param -n llite.*.dump_page_cache | grep locked && \
8859                 error "locked page left in cache after async error" || true
8860         debugrestore
8861 }
8862 run_test 63b "async write errors should be returned to fsync ==="
8863
8864 test_64a () {
8865         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8866
8867         lfs df $DIR
8868         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
8869 }
8870 run_test 64a "verify filter grant calculations (in kernel) ====="
8871
8872 test_64b () {
8873         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8874
8875         bash oos.sh $MOUNT || error "oos.sh failed: $?"
8876 }
8877 run_test 64b "check out-of-space detection on client"
8878
8879 test_64c() {
8880         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
8881 }
8882 run_test 64c "verify grant shrink"
8883
8884 import_param() {
8885         local tgt=$1
8886         local param=$2
8887
8888         $LCTL get_param osc.$tgt.import | awk "/$param/ { print \$2 }"
8889 }
8890
8891 # this does exactly what osc_request.c:osc_announce_cached() does in
8892 # order to calculate max amount of grants to ask from server
8893 want_grant() {
8894         local tgt=$1
8895
8896         local nrpages=$($LCTL get_param -n osc.$tgt.max_pages_per_rpc)
8897         local rpc_in_flight=$($LCTL get_param -n osc.$tgt.max_rpcs_in_flight)
8898
8899         ((rpc_in_flight++));
8900         nrpages=$((nrpages * rpc_in_flight))
8901
8902         local dirty_max_pages=$($LCTL get_param -n osc.$tgt.max_dirty_mb)
8903
8904         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
8905
8906         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
8907         local undirty=$((nrpages * PAGE_SIZE))
8908
8909         local max_extent_pages
8910         max_extent_pages=$(import_param $tgt grant_max_extent_size)
8911         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
8912         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
8913         local grant_extent_tax
8914         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8915
8916         undirty=$((undirty + nrextents * grant_extent_tax))
8917
8918         echo $undirty
8919 }
8920
8921 # this is size of unit for grant allocation. It should be equal to
8922 # what tgt_grant.c:tgt_grant_chunk() calculates
8923 grant_chunk() {
8924         local tgt=$1
8925         local max_brw_size
8926         local grant_extent_tax
8927
8928         max_brw_size=$(import_param $tgt max_brw_size)
8929
8930         grant_extent_tax=$(import_param $tgt grant_extent_tax)
8931
8932         echo $(((max_brw_size + grant_extent_tax) * 2))
8933 }
8934
8935 test_64d() {
8936         [ $OST1_VERSION -ge $(version_code 2.10.56) ] ||
8937                 skip "OST < 2.10.55 doesn't limit grants enough"
8938
8939         local tgt=$($LCTL dl | awk '/OST0000-osc-[^mM]/ { print $4 }')
8940
8941         [[ "$($LCTL get_param osc.${tgt}.import)" =~ "grant_param" ]] ||
8942                 skip "no grant_param connect flag"
8943
8944         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8945
8946         $LCTL set_param -n -n debug="$OLDDEBUG" || true
8947         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8948
8949
8950         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
8951         stack_trap "rm -f $DIR/$tfile && wait_delete_completed" EXIT
8952
8953         $LFS setstripe $DIR/$tfile -i 0 -c 1
8954         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1000 &
8955         ddpid=$!
8956
8957         while kill -0 $ddpid; do
8958                 local cur_grant=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8959
8960                 if [[ $cur_grant -gt $max_cur_granted ]]; then
8961                         kill $ddpid
8962                         error "cur_grant $cur_grant > $max_cur_granted"
8963                 fi
8964
8965                 sleep 1
8966         done
8967 }
8968 run_test 64d "check grant limit exceed"
8969
8970 check_grants() {
8971         local tgt=$1
8972         local expected=$2
8973         local msg=$3
8974         local cur_grants=$($LCTL get_param -n osc.$tgt.cur_grant_bytes)
8975
8976         ((cur_grants == expected)) ||
8977                 error "$msg: grants mismatch: $cur_grants, expected $expected"
8978 }
8979
8980 round_up_p2() {
8981         echo $((($1 + $2 - 1) & ~($2 - 1)))
8982 }
8983
8984 test_64e() {
8985         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8986         [ $OST1_VERSION -ge $(version_code 2.11.56) ] ||
8987                 skip "Need OSS version at least 2.11.56"
8988
8989         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
8990         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
8991         $LCTL set_param debug=+cache
8992
8993         # Remount client to reset grant
8994         remount_client $MOUNT || error "failed to remount client"
8995         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
8996
8997         local init_grants=$(import_param $osc_tgt initial_grant)
8998
8999         check_grants $osc_tgt $init_grants "init grants"
9000
9001         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
9002         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9003         local gbs=$(import_param $osc_tgt grant_block_size)
9004
9005         # write random number of bytes from max_brw_size / 4 to max_brw_size
9006         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
9007         # align for direct io
9008         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
9009         # round to grant consumption unit
9010         local wb_round_up=$(round_up_p2 $write_bytes gbs)
9011
9012         local grants=$((wb_round_up + extent_tax))
9013
9014         $LFS setstripe -c 1 -i 0 $DIR/$tfile  || error "lfs setstripe failed"
9015
9016         # define OBD_FAIL_TGT_NO_GRANT 0x725
9017         # make the server not grant more back
9018         do_facet ost1 $LCTL set_param fail_loc=0x725
9019         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct
9020
9021         do_facet ost1 $LCTL set_param fail_loc=0
9022
9023         check_grants $osc_tgt $((init_grants - grants)) "dio w/o grant alloc"
9024
9025         rm -f $DIR/$tfile || error "rm failed"
9026
9027         # Remount client to reset grant
9028         remount_client $MOUNT || error "failed to remount client"
9029         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9030
9031         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9032
9033         # define OBD_FAIL_TGT_NO_GRANT 0x725
9034         # make the server not grant more back
9035         do_facet ost1 $LCTL set_param fail_loc=0x725
9036         $MULTIOP $DIR/$tfile "oO_WRONLY:w${write_bytes}yc"
9037         do_facet ost1 $LCTL set_param fail_loc=0
9038
9039         check_grants $osc_tgt $((init_grants - grants)) "buf io w/o grant alloc"
9040 }
9041 run_test 64e "check grant consumption (no grant allocation)"
9042
9043 test_64f() {
9044         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9045
9046         local olddebug="$($LCTL get_param -n debug 2> /dev/null)"
9047         stack_trap "$LCTL set_param -n debug='$olddebug'" EXIT
9048         $LCTL set_param debug=+cache
9049
9050         # Remount client to reset grant
9051         remount_client $MOUNT || error "failed to remount client"
9052         local osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9053
9054         local init_grants=$(import_param $osc_tgt initial_grant)
9055         local extent_tax=$(import_param $osc_tgt grant_extent_tax)
9056         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9057         local gbs=$(import_param $osc_tgt grant_block_size)
9058         local chunk=$(grant_chunk $osc_tgt)
9059
9060         # write random number of bytes from max_brw_size / 4 to max_brw_size
9061         local write_bytes=$(shuf -i $((max_brw_size / 4))-$max_brw_size -n 1)
9062         # align for direct io
9063         write_bytes=$(round_up_p2 $write_bytes PAGE_SIZE)
9064         # round to grant consumption unit
9065         local wb_round_up=$(round_up_p2 $write_bytes gbs)
9066
9067         local grants=$((wb_round_up + extent_tax))
9068
9069         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9070         dd if=/dev/zero of=$DIR/$tfile bs=$write_bytes count=1 oflag=direct ||
9071                 error "error writing to $DIR/$tfile"
9072
9073         check_grants $osc_tgt $((init_grants - grants + chunk)) \
9074                 "direct io with grant allocation"
9075
9076         rm -f $DIR/$tfile || error "rm failed"
9077
9078         # Remount client to reset grant
9079         remount_client $MOUNT || error "failed to remount client"
9080         osc_tgt="$FSNAME-OST0000-osc-$($LFS getname -i $DIR)"
9081
9082         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "lfs setstripe failed"
9083
9084         local cmd="oO_WRONLY:w${write_bytes}_yc"
9085
9086         $MULTIOP $DIR/$tfile $cmd &
9087         MULTIPID=$!
9088         sleep 1
9089
9090         check_grants $osc_tgt $((init_grants - grants)) \
9091                 "buffered io, not write rpc"
9092
9093         kill -USR1 $MULTIPID
9094         wait
9095
9096         check_grants $osc_tgt $((init_grants - grants + chunk)) \
9097                 "buffered io, one RPC"
9098 }
9099 run_test 64f "check grant consumption (with grant allocation)"
9100
9101 test_64g() {
9102         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
9103                 skip "Need MDS version at least 2.14.56"
9104
9105         local mdts=$(comma_list $(mdts_nodes))
9106
9107         local old=$($LCTL get_param mdc.$FSNAME-*.grant_shrink_interval |
9108                         tr '\n' ' ')
9109         stack_trap "$LCTL set_param $old"
9110
9111         # generate dirty pages and increase dirty granted on MDT
9112         stack_trap "rm -f $DIR/$tfile-*"
9113         for (( i = 0; i < 10; i++)); do
9114                 $LFS setstripe -E 1M -L mdt $DIR/$tfile-$i ||
9115                         error "can't set stripe"
9116                 dd if=/dev/zero of=$DIR/$tfile-$i bs=128k count=1 ||
9117                         error "can't dd"
9118                 $LFS getstripe $DIR/$tfile-$i | grep -q pattern.*mdt || {
9119                         $LFS getstripe $DIR/$tfile-$i
9120                         error "not DoM file"
9121                 }
9122         done
9123
9124         # flush dirty pages
9125         sync
9126
9127         # wait until grant shrink reset grant dirty on MDTs
9128         for ((i = 0; i < 120; i++)); do
9129                 grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
9130                         awk '{sum=sum+$1} END {print sum}')
9131                 vm_dirty=$(awk '/Dirty:/{print $2}' /proc/meminfo)
9132                 echo "$grant_dirty grants, $vm_dirty pages"
9133                 (( grant_dirty + vm_dirty == 0 )) && break
9134                 (( i == 3 )) && sync &&
9135                         $LCTL set_param mdc.$FSNAME-*.grant_shrink_interval=5
9136                 sleep 1
9137         done
9138
9139         grant_dirty=$(do_nodes $mdts $LCTL get_param -n  mdt.*.tot_dirty |
9140                 awk '{sum=sum+$1} END {print sum}')
9141         (( grant_dirty == 0 )) || error "$grant_dirty on MDT"
9142 }
9143 run_test 64g "grant shrink on MDT"
9144
9145 test_64h() {
9146         (( $OST1_VERSION >= $(version_code 2.14.56) )) ||
9147                 skip "need OST at least 2.14.56 to avoid grant shrink on read"
9148
9149         local instance=$($LFS getname -i $DIR)
9150         local osc_tgt="$FSNAME-OST0000-osc-$instance"
9151         local num_exps=$(do_facet ost1 \
9152             $LCTL get_param -n obdfilter.*OST0000*.num_exports)
9153         local max_brw_size=$(import_param $osc_tgt max_brw_size)
9154         local avail=$($LCTL get_param -n osc.*OST0000-osc-$instance.kbytesavail)
9155         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9156
9157         # 10MiB is for file to be written, max_brw_size * 16 *
9158         # num_exps is space reserve so that tgt_grant_shrink() decided
9159         # to not shrink
9160         local expect=$((max_brw_size * 16 * num_exps + 10 * 1048576))
9161         (( avail * 1024 < expect )) &&
9162                 skip "need $expect bytes on ost1, have $(( avail * 1024 )) only"
9163
9164         save_lustre_params client "osc.*OST0000*.grant_shrink" > $p
9165         save_lustre_params client "osc.*OST0000*.grant_shrink_interval" >> $p
9166         stack_trap "restore_lustre_params < $p; rm -f $save" EXIT
9167         $LCTL set_param osc.*OST0000*.grant_shrink=1
9168         $LCTL set_param osc.*OST0000*.grant_shrink_interval=10
9169
9170         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9171         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 oflag=sync
9172
9173         # drop cache so that coming read would do rpc
9174         cancel_lru_locks osc
9175
9176         # shrink interval is set to 10, pause for 7 seconds so that
9177         # grant thread did not wake up yet but coming read entered
9178         # shrink mode for rpc (osc_should_shrink_grant())
9179         sleep 7
9180
9181         declare -a cur_grant_bytes
9182         declare -a tot_granted
9183         cur_grant_bytes[0]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
9184         tot_granted[0]=$(do_facet ost1 \
9185             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
9186
9187         dd if=$DIR/$tfile bs=4K count=1 of=/dev/null
9188
9189         cur_grant_bytes[1]=$($LCTL get_param -n osc.*OST0000*.cur_grant_bytes)
9190         tot_granted[1]=$(do_facet ost1 \
9191             $LCTL get_param -n obdfilter.*OST0000*.tot_granted)
9192
9193         # grant change should be equal on both sides
9194         (( cur_grant_bytes[0] - cur_grant_bytes[1] ==
9195                 tot_granted[0] - tot_granted[1])) ||
9196                 error "grant change mismatch, "                                \
9197                         "server: ${tot_granted[0]} to ${tot_granted[1]}, "     \
9198                         "client: ${cur_grant_bytes[0]} to ${cur_grant_bytes[1]}"
9199 }
9200 run_test 64h "grant shrink on read"
9201
9202 test_64i() {
9203         (( $OST1_VERSION >= $(version_code 2.14.56) )) ||
9204                 skip "need OST at least 2.14.56 to avoid grant shrink on replay"
9205
9206         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9207         remote_ost_nodsh && skip "remote OSTs with nodsh"
9208
9209         $LFS setstripe -c 1 -i 0 $DIR/$tfile
9210
9211         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
9212
9213         # lustre-ffff9fc75e850800 /mnt/lustre -> ffff9fc75e850800
9214         local instance=$($LFS getname -i $DIR)
9215
9216         local osc_tgt="$FSNAME-OST0000-osc-$instance"
9217         local cgb=$($LCTL get_param -n osc.$osc_tgt.cur_grant_bytes)
9218
9219         # shrink grants and simulate rpc loss
9220         #define OBD_FAIL_PTLRPC_DROP_REQ_OPC     0x513
9221         do_facet ost1 "$LCTL set_param fail_loc=0x80000513 fail_val=17"
9222         $LCTL set_param osc.$osc_tgt.cur_grant_bytes=$((cgb/2))B
9223
9224         fail ost1
9225
9226         dd if=/dev/zero of=$DIR/$tfile oflag=append bs=1M count=8 conv=notrunc
9227
9228         local testid=$(echo $TESTNAME | tr '_' ' ')
9229
9230         do_facet ost1 dmesg | tac | sed "/$testid/,$ d" |
9231                 grep "GRANT, real grant" &&
9232                 error "client has more grants then it owns" || true
9233 }
9234 run_test 64i "shrink on reconnect"
9235
9236 # bug 1414 - set/get directories' stripe info
9237 test_65a() {
9238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9239
9240         test_mkdir $DIR/$tdir
9241         touch $DIR/$tdir/f1
9242         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
9243 }
9244 run_test 65a "directory with no stripe info"
9245
9246 test_65b() {
9247         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9248
9249         test_mkdir $DIR/$tdir
9250         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9251
9252         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9253                                                 error "setstripe"
9254         touch $DIR/$tdir/f2
9255         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
9256 }
9257 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
9258
9259 test_65c() {
9260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9261         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
9262
9263         test_mkdir $DIR/$tdir
9264         local stripesize=$($LFS getstripe -S $DIR/$tdir)
9265
9266         $LFS setstripe -S $((stripesize * 4)) -i 1 \
9267                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
9268         touch $DIR/$tdir/f3
9269         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
9270 }
9271 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
9272
9273 test_65d() {
9274         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9275
9276         test_mkdir $DIR/$tdir
9277         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
9278         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9279
9280         if [[ $STRIPECOUNT -le 0 ]]; then
9281                 sc=1
9282         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
9283                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
9284                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
9285         else
9286                 sc=$(($STRIPECOUNT - 1))
9287         fi
9288         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
9289         touch $DIR/$tdir/f4 $DIR/$tdir/f5
9290         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
9291                 error "lverify failed"
9292 }
9293 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
9294
9295 test_65e() {
9296         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9297
9298         test_mkdir $DIR/$tdir
9299
9300         $LFS setstripe $DIR/$tdir || error "setstripe"
9301         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
9302                                         error "no stripe info failed"
9303         touch $DIR/$tdir/f6
9304         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
9305 }
9306 run_test 65e "directory setstripe defaults"
9307
9308 test_65f() {
9309         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9310
9311         test_mkdir $DIR/${tdir}f
9312         $RUNAS $LFS setstripe $DIR/${tdir}f &&
9313                 error "setstripe succeeded" || true
9314 }
9315 run_test 65f "dir setstripe permission (should return error) ==="
9316
9317 test_65g() {
9318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9319
9320         test_mkdir $DIR/$tdir
9321         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9322
9323         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9324                 error "setstripe -S failed"
9325         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
9326         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
9327                 error "delete default stripe failed"
9328 }
9329 run_test 65g "directory setstripe -d"
9330
9331 test_65h() {
9332         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9333
9334         test_mkdir $DIR/$tdir
9335         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
9336
9337         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
9338                 error "setstripe -S failed"
9339         test_mkdir $DIR/$tdir/dd1
9340         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
9341                 error "stripe info inherit failed"
9342 }
9343 run_test 65h "directory stripe info inherit ===================="
9344
9345 test_65i() {
9346         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9347
9348         save_layout_restore_at_exit $MOUNT
9349
9350         # bug6367: set non-default striping on root directory
9351         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
9352
9353         # bug12836: getstripe on -1 default directory striping
9354         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
9355
9356         # bug12836: getstripe -v on -1 default directory striping
9357         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
9358
9359         # bug12836: new find on -1 default directory striping
9360         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
9361 }
9362 run_test 65i "various tests to set root directory striping"
9363
9364 test_65j() { # bug6367
9365         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9366
9367         sync; sleep 1
9368
9369         # if we aren't already remounting for each test, do so for this test
9370         if [ "$I_MOUNTED" = "yes" ]; then
9371                 cleanup || error "failed to unmount"
9372                 setup
9373         fi
9374
9375         save_layout_restore_at_exit $MOUNT
9376
9377         $LFS setstripe -d $MOUNT || error "setstripe failed"
9378 }
9379 run_test 65j "set default striping on root directory (bug 6367)="
9380
9381 cleanup_65k() {
9382         rm -rf $DIR/$tdir
9383         wait_delete_completed
9384         do_facet $SINGLEMDS "lctl set_param -n \
9385                 osp.$ost*MDT0000.max_create_count=$max_count"
9386         do_facet $SINGLEMDS "lctl set_param -n \
9387                 osp.$ost*MDT0000.create_count=$count"
9388         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9389         echo $INACTIVE_OSC "is Activate"
9390
9391         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9392 }
9393
9394 test_65k() { # bug11679
9395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9396         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9397         remote_mds_nodsh && skip "remote MDS with nodsh"
9398
9399         local disable_precreate=true
9400         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
9401                 disable_precreate=false
9402
9403         echo "Check OST status: "
9404         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
9405                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
9406
9407         for OSC in $MDS_OSCS; do
9408                 echo $OSC "is active"
9409                 do_facet $SINGLEMDS lctl --device %$OSC activate
9410         done
9411
9412         for INACTIVE_OSC in $MDS_OSCS; do
9413                 local ost=$(osc_to_ost $INACTIVE_OSC)
9414                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
9415                                lov.*md*.target_obd |
9416                                awk -F: /$ost/'{ print $1 }' | head -n 1)
9417
9418                 mkdir -p $DIR/$tdir
9419                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
9420                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
9421
9422                 echo "Deactivate: " $INACTIVE_OSC
9423                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
9424
9425                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
9426                               osp.$ost*MDT0000.create_count")
9427                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
9428                                   osp.$ost*MDT0000.max_create_count")
9429                 $disable_precreate &&
9430                         do_facet $SINGLEMDS "lctl set_param -n \
9431                                 osp.$ost*MDT0000.max_create_count=0"
9432
9433                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
9434                         [ -f $DIR/$tdir/$idx ] && continue
9435                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
9436                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
9437                                 { cleanup_65k;
9438                                   error "setstripe $idx should succeed"; }
9439                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
9440                 done
9441                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
9442                 rmdir $DIR/$tdir
9443
9444                 do_facet $SINGLEMDS "lctl set_param -n \
9445                         osp.$ost*MDT0000.max_create_count=$max_count"
9446                 do_facet $SINGLEMDS "lctl set_param -n \
9447                         osp.$ost*MDT0000.create_count=$count"
9448                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
9449                 echo $INACTIVE_OSC "is Activate"
9450
9451                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
9452         done
9453 }
9454 run_test 65k "validate manual striping works properly with deactivated OSCs"
9455
9456 test_65l() { # bug 12836
9457         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9458
9459         test_mkdir -p $DIR/$tdir/test_dir
9460         $LFS setstripe -c -1 $DIR/$tdir/test_dir
9461         $LFS find -mtime -1 $DIR/$tdir >/dev/null
9462 }
9463 run_test 65l "lfs find on -1 stripe dir ========================"
9464
9465 test_65m() {
9466         local layout=$(save_layout $MOUNT)
9467         $RUNAS $LFS setstripe -c 2 $MOUNT && {
9468                 restore_layout $MOUNT $layout
9469                 error "setstripe should fail by non-root users"
9470         }
9471         true
9472 }
9473 run_test 65m "normal user can't set filesystem default stripe"
9474
9475 test_65n() {
9476         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
9477         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
9478                 skip "Need MDS version at least 2.12.50"
9479         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
9480
9481         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
9482         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
9483         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
9484
9485         save_layout_restore_at_exit $MOUNT
9486
9487         # new subdirectory under root directory should not inherit
9488         # the default layout from root
9489         local dir1=$MOUNT/$tdir-1
9490         mkdir $dir1 || error "mkdir $dir1 failed"
9491         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
9492                 error "$dir1 shouldn't have LOV EA"
9493
9494         # delete the default layout on root directory
9495         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
9496
9497         local dir2=$MOUNT/$tdir-2
9498         mkdir $dir2 || error "mkdir $dir2 failed"
9499         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
9500                 error "$dir2 shouldn't have LOV EA"
9501
9502         # set a new striping pattern on root directory
9503         local def_stripe_size=$($LFS getstripe -S $MOUNT)
9504         local new_def_stripe_size=$((def_stripe_size * 2))
9505         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
9506                 error "set stripe size on $MOUNT failed"
9507
9508         # new file created in $dir2 should inherit the new stripe size from
9509         # the filesystem default
9510         local file2=$dir2/$tfile-2
9511         touch $file2 || error "touch $file2 failed"
9512
9513         local file2_stripe_size=$($LFS getstripe -S $file2)
9514         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
9515         {
9516                 echo "file2_stripe_size: '$file2_stripe_size'"
9517                 echo "new_def_stripe_size: '$new_def_stripe_size'"
9518                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
9519         }
9520
9521         local dir3=$MOUNT/$tdir-3
9522         mkdir $dir3 || error "mkdir $dir3 failed"
9523         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
9524         # the root layout, which is the actual default layout that will be used
9525         # when new files are created in $dir3.
9526         local dir3_layout=$(get_layout_param $dir3)
9527         local root_dir_layout=$(get_layout_param $MOUNT)
9528         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
9529         {
9530                 echo "dir3_layout: '$dir3_layout'"
9531                 echo "root_dir_layout: '$root_dir_layout'"
9532                 error "$dir3 should show the default layout from $MOUNT"
9533         }
9534
9535         # set OST pool on root directory
9536         local pool=$TESTNAME
9537         pool_add $pool || error "add $pool failed"
9538         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
9539                 error "add targets to $pool failed"
9540
9541         $LFS setstripe -p $pool $MOUNT ||
9542                 error "set OST pool on $MOUNT failed"
9543
9544         # new file created in $dir3 should inherit the pool from
9545         # the filesystem default
9546         local file3=$dir3/$tfile-3
9547         touch $file3 || error "touch $file3 failed"
9548
9549         local file3_pool=$($LFS getstripe -p $file3)
9550         [[ "$file3_pool" = "$pool" ]] ||
9551                 error "$file3 ('$file3_pool') didn't inherit OST pool '$pool'"
9552
9553         local dir4=$MOUNT/$tdir-4
9554         mkdir $dir4 || error "mkdir $dir4 failed"
9555         local dir4_layout=$(get_layout_param $dir4)
9556         root_dir_layout=$(get_layout_param $MOUNT)
9557         echo "$LFS getstripe -d $dir4"
9558         $LFS getstripe -d $dir4
9559         echo "$LFS getstripe -d $MOUNT"
9560         $LFS getstripe -d $MOUNT
9561         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
9562         {
9563                 echo "dir4_layout: '$dir4_layout'"
9564                 echo "root_dir_layout: '$root_dir_layout'"
9565                 error "$dir4 should show the default layout from $MOUNT"
9566         }
9567
9568         # new file created in $dir4 should inherit the pool from
9569         # the filesystem default
9570         local file4=$dir4/$tfile-4
9571         touch $file4 || error "touch $file4 failed"
9572
9573         local file4_pool=$($LFS getstripe -p $file4)
9574         [[ "$file4_pool" = "$pool" ]] ||
9575                 error "$file4 ('$file4_pool') didn't inherit OST pool $pool"
9576
9577         # new subdirectory under non-root directory should inherit
9578         # the default layout from its parent directory
9579         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
9580                 error "set directory layout on $dir4 failed"
9581
9582         local dir5=$dir4/$tdir-5
9583         mkdir $dir5 || error "mkdir $dir5 failed"
9584
9585         dir4_layout=$(get_layout_param $dir4)
9586         local dir5_layout=$(get_layout_param $dir5)
9587         [[ "$dir4_layout" = "$dir5_layout" ]] ||
9588         {
9589                 echo "dir4_layout: '$dir4_layout'"
9590                 echo "dir5_layout: '$dir5_layout'"
9591                 error "$dir5 should inherit the default layout from $dir4"
9592         }
9593
9594         # though subdir under ROOT doesn't inherit default layout, but
9595         # its sub dir/file should be created with default layout.
9596         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
9597         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
9598                 skip "Need MDS version at least 2.12.59"
9599
9600         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
9601         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
9602         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
9603
9604         if [ $default_lmv_hash == "none" ]; then
9605                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
9606         else
9607                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
9608                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
9609         fi
9610
9611         $LFS setdirstripe -D -c 2 $MOUNT ||
9612                 error "setdirstripe -D -c 2 failed"
9613         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
9614         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
9615         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
9616
9617         # $dir4 layout includes pool
9618         $LFS setstripe -S $((new_def_stripe_size * 2)) $dir4
9619         [[ "$pool" = $($LFS getstripe -p -d $dir4) ]] ||
9620                 error "pool lost on setstripe"
9621         $LFS setstripe -E -1 -S $new_def_stripe_size $dir4
9622         [[ "$pool" = $($LFS getstripe -p -d $dir4) ]] ||
9623                 error "pool lost on compound layout setstripe"
9624 }
9625 run_test 65n "don't inherit default layout from root for new subdirectories"
9626
9627 test_65o() {
9628         (( $MDS1_VERSION >= $(version_code 2.14.57) )) ||
9629                 skip "need MDS version at least 2.14.57"
9630
9631         # set OST pool on root directory
9632         local pool=$TESTNAME
9633
9634         pool_add $pool || error "add $pool failed"
9635         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
9636                 error "add targets to $pool failed"
9637
9638         local dir1=$MOUNT/$tdir
9639
9640         mkdir $dir1 || error "mkdir $dir1 failed"
9641
9642         # set a new striping pattern on root directory
9643         local def_stripe_size=$($LFS getstripe -S $MOUNT)
9644
9645         $LFS setstripe -p $pool $dir1 ||
9646                 error "set directory layout on $dir1 failed"
9647
9648         # $dir1 layout includes pool
9649         $LFS setstripe -S $((def_stripe_size * 2)) $dir1
9650         [[ "$pool" = $($LFS getstripe -p -d $dir1) ]] ||
9651                 error "pool lost on setstripe"
9652         $LFS setstripe -E 1M -L mdt -E -1 -c 1 $dir1
9653         $LFS getstripe $dir1
9654         [[ "$pool" = $($LFS getstripe -p -d $dir1) ]] ||
9655                 error "pool lost on compound layout setstripe"
9656
9657         $LFS setdirstripe -i 0 -c 2 $dir1/dir2 ||
9658                 error "setdirstripe failed on sub-dir with inherited pool"
9659         $LFS getstripe $dir1/dir2
9660         [[ "$pool" = $($LFS getstripe -p -d $dir1/dir2) ]] ||
9661                 error "pool lost on compound layout setdirstripe"
9662
9663         $LFS setstripe -E -1 -c 1 $dir1
9664         $LFS getstripe -d $dir1
9665         [[ "$pool" = $($LFS getstripe -p -d $dir1) ]] ||
9666                 error "pool lost on setstripe"
9667 }
9668 run_test 65o "pool inheritance for mdt component"
9669
9670 test_65p () { # LU-16152
9671         local src_dir=$DIR/$tdir/src_dir
9672         local dst_dir=$DIR/$tdir/dst_dir
9673         local yaml_file=$DIR/$tdir/layout.yaml
9674         local border
9675
9676         (( $CLIENT_VERSION >= $(version_code 2.15.51) )) ||
9677                 skip "Need at least version 2.15.51"
9678
9679         test_mkdir -p $src_dir
9680         $LFS setstripe -E 2048M -c 4 -E EOF -c 8 $src_dir ||
9681                 error "failed to setstripe"
9682         $LFS getstripe --yaml -d $src_dir > $yaml_file ||
9683                 error "failed to getstripe"
9684
9685         test_mkdir -p $dst_dir
9686         $LFS setstripe --yaml $yaml_file $dst_dir ||
9687                 error "failed to setstripe with yaml file"
9688         border=$($LFS getstripe -d $dst_dir |
9689                 awk '/lcme_extent.e_end:/ { print $2; exit; }') ||
9690                 error "failed to getstripe"
9691
9692         # 2048M is 0x80000000, or 2147483648
9693         (( $border == 2147483648 )) ||
9694                 error "failed to handle huge number in yaml layout"
9695 }
9696 run_test 65p "setstripe with yaml file and huge number"
9697
9698 # bug 2543 - update blocks count on client
9699 test_66() {
9700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9701
9702         local COUNT=${COUNT:-8}
9703         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
9704         sync; sync_all_data; sync; sync_all_data
9705         cancel_lru_locks osc
9706         local BLOCKS=$(ls -s --block-size=1k $DIR/f66 | awk '{ print $1 }')
9707         (( BLOCKS >= COUNT )) || error "$DIR/f66 blocks $BLOCKS < $COUNT"
9708 }
9709 run_test 66 "update inode blocks count on client ==============="
9710
9711 meminfo() {
9712         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
9713 }
9714
9715 swap_used() {
9716         swapon -s | awk '($1 == "'$1'") { print $4 }'
9717 }
9718
9719 # bug5265, obdfilter oa2dentry return -ENOENT
9720 # #define OBD_FAIL_SRV_ENOENT 0x217
9721 test_69() {
9722         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9723         remote_ost_nodsh && skip "remote OST with nodsh"
9724
9725         f="$DIR/$tfile"
9726         $LFS setstripe -c 1 -i 0 $f
9727
9728         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
9729
9730         do_facet ost1 lctl set_param fail_loc=0x217
9731         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
9732         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
9733
9734         do_facet ost1 lctl set_param fail_loc=0
9735         $DIRECTIO write $f 0 2 || error "write error"
9736
9737         cancel_lru_locks osc
9738         $DIRECTIO read $f 0 1 || error "read error"
9739
9740         do_facet ost1 lctl set_param fail_loc=0x217
9741         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
9742
9743         do_facet ost1 lctl set_param fail_loc=0
9744         rm -f $f
9745 }
9746 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
9747
9748 test_71() {
9749         test_mkdir $DIR/$tdir
9750         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
9751         bash rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
9752 }
9753 run_test 71 "Running dbench on lustre (don't segment fault) ===="
9754
9755 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
9756         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9757         [ "$RUNAS_ID" = "$UID" ] &&
9758                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9759         # Check that testing environment is properly set up. Skip if not
9760         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
9761                 skip_env "User $RUNAS_ID does not exist - skipping"
9762
9763         touch $DIR/$tfile
9764         chmod 777 $DIR/$tfile
9765         chmod ug+s $DIR/$tfile
9766         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
9767                 error "$RUNAS dd $DIR/$tfile failed"
9768         # See if we are still setuid/sgid
9769         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9770                 error "S/gid is not dropped on write"
9771         # Now test that MDS is updated too
9772         cancel_lru_locks mdc
9773         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
9774                 error "S/gid is not dropped on MDS"
9775         rm -f $DIR/$tfile
9776 }
9777 run_test 72a "Test that remove suid works properly (bug5695) ===="
9778
9779 test_72b() { # bug 24226 -- keep mode setting when size is not changing
9780         local perm
9781
9782         [ "$RUNAS_ID" = "$UID" ] &&
9783                 skip_env "RUNAS_ID = UID = $UID -- skipping"
9784         [ "$RUNAS_ID" -eq 0 ] &&
9785                 skip_env "RUNAS_ID = 0 -- skipping"
9786         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9787         # Check that testing environment is properly set up. Skip if not
9788         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
9789                 skip_env "User $RUNAS_ID does not exist - skipping"
9790
9791         touch $DIR/${tfile}-f{g,u}
9792         test_mkdir $DIR/${tfile}-dg
9793         test_mkdir $DIR/${tfile}-du
9794         chmod 770 $DIR/${tfile}-{f,d}{g,u}
9795         chmod g+s $DIR/${tfile}-{f,d}g
9796         chmod u+s $DIR/${tfile}-{f,d}u
9797         for perm in 777 2777 4777; do
9798                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
9799                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
9800                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
9801                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
9802         done
9803         true
9804 }
9805 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
9806
9807 # bug 3462 - multiple simultaneous MDC requests
9808 test_73() {
9809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9810
9811         test_mkdir $DIR/d73-1
9812         test_mkdir $DIR/d73-2
9813         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
9814         pid1=$!
9815
9816         lctl set_param fail_loc=0x80000129
9817         $MULTIOP $DIR/d73-1/f73-2 Oc &
9818         sleep 1
9819         lctl set_param fail_loc=0
9820
9821         $MULTIOP $DIR/d73-2/f73-3 Oc &
9822         pid3=$!
9823
9824         kill -USR1 $pid1
9825         wait $pid1 || return 1
9826
9827         sleep 25
9828
9829         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
9830         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
9831         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
9832
9833         rm -rf $DIR/d73-*
9834 }
9835 run_test 73 "multiple MDC requests (should not deadlock)"
9836
9837 test_74a() { # bug 6149, 6184
9838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9839
9840         touch $DIR/f74a
9841         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9842         #
9843         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9844         # will spin in a tight reconnection loop
9845         $LCTL set_param fail_loc=0x8000030e
9846         # get any lock that won't be difficult - lookup works.
9847         ls $DIR/f74a
9848         $LCTL set_param fail_loc=0
9849         rm -f $DIR/f74a
9850         true
9851 }
9852 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
9853
9854 test_74b() { # bug 13310
9855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9856
9857         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
9858         #
9859         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
9860         # will spin in a tight reconnection loop
9861         $LCTL set_param fail_loc=0x8000030e
9862         # get a "difficult" lock
9863         touch $DIR/f74b
9864         $LCTL set_param fail_loc=0
9865         rm -f $DIR/f74b
9866         true
9867 }
9868 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
9869
9870 test_74c() {
9871         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9872
9873         #define OBD_FAIL_LDLM_NEW_LOCK
9874         $LCTL set_param fail_loc=0x319
9875         touch $DIR/$tfile && error "touch successful"
9876         $LCTL set_param fail_loc=0
9877         true
9878 }
9879 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
9880
9881 slab_lic=/sys/kernel/slab/lustre_inode_cache
9882 num_objects() {
9883         [ -f $slab_lic/shrink ] && echo 1 > $slab_lic/shrink
9884         [ -f $slab_lic/objects ] && awk '{ print $1 }' $slab_lic/objects ||
9885                 awk '/lustre_inode_cache/ { print $2; exit }' /proc/slabinfo
9886 }
9887
9888 test_76a() { # Now for b=20433, added originally in b=1443
9889         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9890
9891         cancel_lru_locks osc
9892         # there may be some slab objects cached per core
9893         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
9894         local before=$(num_objects)
9895         local count=$((512 * cpus))
9896         [ "$SLOW" = "no" ] && count=$((128 * cpus))
9897         local margin=$((count / 10))
9898         if [[ -f $slab_lic/aliases ]]; then
9899                 local aliases=$(cat $slab_lic/aliases)
9900                 (( aliases > 0 )) && margin=$((margin * aliases))
9901         fi
9902
9903         echo "before slab objects: $before"
9904         for i in $(seq $count); do
9905                 touch $DIR/$tfile
9906                 rm -f $DIR/$tfile
9907         done
9908         cancel_lru_locks osc
9909         local after=$(num_objects)
9910         echo "created: $count, after slab objects: $after"
9911         # shared slab counts are not very accurate, allow significant margin
9912         # the main goal is that the cache growth is not permanently > $count
9913         while (( after > before + margin )); do
9914                 sleep 1
9915                 after=$(num_objects)
9916                 wait=$((wait + 1))
9917                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9918                 if (( wait > 60 )); then
9919                         error "inode slab grew from $before+$margin to $after"
9920                 fi
9921         done
9922 }
9923 run_test 76a "confirm clients recycle inodes properly ===="
9924
9925 test_76b() {
9926         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9927         [ $CLIENT_VERSION -ge $(version_code 2.13.55) ] || skip "not supported"
9928
9929         local count=512
9930         local before=$(num_objects)
9931
9932         for i in $(seq $count); do
9933                 mkdir $DIR/$tdir
9934                 rmdir $DIR/$tdir
9935         done
9936
9937         local after=$(num_objects)
9938         local wait=0
9939
9940         while (( after > before )); do
9941                 sleep 1
9942                 after=$(num_objects)
9943                 wait=$((wait + 1))
9944                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
9945                 if (( wait > 60 )); then
9946                         error "inode slab grew from $before to $after"
9947                 fi
9948         done
9949
9950         echo "slab objects before: $before, after: $after"
9951 }
9952 run_test 76b "confirm clients recycle directory inodes properly ===="
9953
9954 export ORIG_CSUM=""
9955 set_checksums()
9956 {
9957         # Note: in sptlrpc modes which enable its own bulk checksum, the
9958         # original crc32_le bulk checksum will be automatically disabled,
9959         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
9960         # will be checked by sptlrpc code against sptlrpc bulk checksum.
9961         # In this case set_checksums() will not be no-op, because sptlrpc
9962         # bulk checksum will be enabled all through the test.
9963
9964         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
9965         lctl set_param -n osc.*.checksums $1
9966         return 0
9967 }
9968
9969 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9970                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
9971 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
9972                              tr -d [] | head -n1)}
9973 set_checksum_type()
9974 {
9975         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
9976         rc=$?
9977         log "set checksum type to $1, rc = $rc"
9978         return $rc
9979 }
9980
9981 get_osc_checksum_type()
9982 {
9983         # arugment 1: OST name, like OST0000
9984         ost=$1
9985         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
9986                         sed 's/.*\[\(.*\)\].*/\1/g')
9987         rc=$?
9988         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
9989         echo $checksum_type
9990 }
9991
9992 F77_TMP=$TMP/f77-temp
9993 F77SZ=8
9994 setup_f77() {
9995         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
9996                 error "error writing to $F77_TMP"
9997 }
9998
9999 test_77a() { # bug 10889
10000         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10001         $GSS && skip_env "could not run with gss"
10002
10003         [ ! -f $F77_TMP ] && setup_f77
10004         set_checksums 1
10005         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
10006         set_checksums 0
10007         rm -f $DIR/$tfile
10008 }
10009 run_test 77a "normal checksum read/write operation"
10010
10011 test_77b() { # bug 10889
10012         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10013         $GSS && skip_env "could not run with gss"
10014
10015         [ ! -f $F77_TMP ] && setup_f77
10016         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10017         $LCTL set_param fail_loc=0x80000409
10018         set_checksums 1
10019
10020         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
10021                 error "dd error: $?"
10022         $LCTL set_param fail_loc=0
10023
10024         for algo in $CKSUM_TYPES; do
10025                 cancel_lru_locks osc
10026                 set_checksum_type $algo
10027                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
10028                 $LCTL set_param fail_loc=0x80000408
10029                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
10030                 $LCTL set_param fail_loc=0
10031         done
10032         set_checksums 0
10033         set_checksum_type $ORIG_CSUM_TYPE
10034         rm -f $DIR/$tfile
10035 }
10036 run_test 77b "checksum error on client write, read"
10037
10038 cleanup_77c() {
10039         trap 0
10040         set_checksums 0
10041         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
10042         $check_ost &&
10043                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
10044         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
10045         $check_ost && [ -n "$ost_file_prefix" ] &&
10046                 do_facet ost1 rm -f ${ost_file_prefix}\*
10047 }
10048
10049 test_77c() {
10050         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10051         $GSS && skip_env "could not run with gss"
10052         remote_ost_nodsh && skip "remote OST with nodsh"
10053
10054         local bad1
10055         local osc_file_prefix
10056         local osc_file
10057         local check_ost=false
10058         local ost_file_prefix
10059         local ost_file
10060         local orig_cksum
10061         local dump_cksum
10062         local fid
10063
10064         # ensure corruption will occur on first OSS/OST
10065         $LFS setstripe -i 0 $DIR/$tfile
10066
10067         [ ! -f $F77_TMP ] && setup_f77
10068         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
10069                 error "dd write error: $?"
10070         fid=$($LFS path2fid $DIR/$tfile)
10071
10072         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
10073         then
10074                 check_ost=true
10075                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
10076                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
10077         else
10078                 echo "OSS do not support bulk pages dump upon error"
10079         fi
10080
10081         osc_file_prefix=$($LCTL get_param -n debug_path)
10082         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
10083
10084         trap cleanup_77c EXIT
10085
10086         set_checksums 1
10087         # enable bulk pages dump upon error on Client
10088         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
10089         # enable bulk pages dump upon error on OSS
10090         $check_ost &&
10091                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
10092
10093         # flush Client cache to allow next read to reach OSS
10094         cancel_lru_locks osc
10095
10096         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
10097         $LCTL set_param fail_loc=0x80000408
10098         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
10099         $LCTL set_param fail_loc=0
10100
10101         rm -f $DIR/$tfile
10102
10103         # check cksum dump on Client
10104         osc_file=$(ls ${osc_file_prefix}*)
10105         [ -n "$osc_file" ] || error "no checksum dump file on Client"
10106         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
10107         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
10108         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
10109         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
10110                      cksum)
10111         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
10112         [[ "$orig_cksum" == "$dump_cksum" ]] ||
10113                 error "dump content does not match on Client"
10114
10115         $check_ost || skip "No need to check cksum dump on OSS"
10116
10117         # check cksum dump on OSS
10118         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
10119         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
10120         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
10121         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
10122         [[ "$orig_cksum" == "$dump_cksum" ]] ||
10123                 error "dump content does not match on OSS"
10124
10125         cleanup_77c
10126 }
10127 run_test 77c "checksum error on client read with debug"
10128
10129 test_77d() { # bug 10889
10130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10131         $GSS && skip_env "could not run with gss"
10132
10133         stack_trap "rm -f $DIR/$tfile"
10134         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10135         $LCTL set_param fail_loc=0x80000409
10136         set_checksums 1
10137         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
10138                 error "direct write: rc=$?"
10139         $LCTL set_param fail_loc=0
10140         set_checksums 0
10141
10142         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
10143         $LCTL set_param fail_loc=0x80000408
10144         set_checksums 1
10145         cancel_lru_locks osc
10146         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
10147                 error "direct read: rc=$?"
10148         $LCTL set_param fail_loc=0
10149         set_checksums 0
10150 }
10151 run_test 77d "checksum error on OST direct write, read"
10152
10153 test_77f() { # bug 10889
10154         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10155         $GSS && skip_env "could not run with gss"
10156
10157         set_checksums 1
10158         stack_trap "rm -f $DIR/$tfile"
10159         for algo in $CKSUM_TYPES; do
10160                 cancel_lru_locks osc
10161                 set_checksum_type $algo
10162                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
10163                 $LCTL set_param fail_loc=0x409
10164                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
10165                         error "direct write succeeded"
10166                 $LCTL set_param fail_loc=0
10167         done
10168         set_checksum_type $ORIG_CSUM_TYPE
10169         set_checksums 0
10170 }
10171 run_test 77f "repeat checksum error on write (expect error)"
10172
10173 test_77g() { # bug 10889
10174         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10175         $GSS && skip_env "could not run with gss"
10176         remote_ost_nodsh && skip "remote OST with nodsh"
10177
10178         [ ! -f $F77_TMP ] && setup_f77
10179
10180         local file=$DIR/$tfile
10181         stack_trap "rm -f $file" EXIT
10182
10183         $LFS setstripe -c 1 -i 0 $file
10184         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
10185         do_facet ost1 lctl set_param fail_loc=0x8000021a
10186         set_checksums 1
10187         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
10188                 error "write error: rc=$?"
10189         do_facet ost1 lctl set_param fail_loc=0
10190         set_checksums 0
10191
10192         cancel_lru_locks osc
10193         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
10194         do_facet ost1 lctl set_param fail_loc=0x8000021b
10195         set_checksums 1
10196         cmp $F77_TMP $file || error "file compare failed"
10197         do_facet ost1 lctl set_param fail_loc=0
10198         set_checksums 0
10199 }
10200 run_test 77g "checksum error on OST write, read"
10201
10202 test_77k() { # LU-10906
10203         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10204         $GSS && skip_env "could not run with gss"
10205
10206         local cksum_param="osc.$FSNAME*.checksums"
10207         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
10208         local checksum
10209         local i
10210
10211         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
10212         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM || true"
10213         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM"
10214
10215         for i in 0 1; do
10216                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
10217                         error "failed to set checksum=$i on MGS"
10218                 wait_update $HOSTNAME "$get_checksum" $i
10219                 #remount
10220                 echo "remount client, checksum should be $i"
10221                 remount_client $MOUNT || error "failed to remount client"
10222                 checksum=$(eval $get_checksum)
10223                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
10224         done
10225         # remove persistent param to avoid races with checksum mountopt below
10226         do_facet mgs $LCTL set_param -P -d $cksum_param ||
10227                 error "failed to delete checksum on MGS"
10228
10229         for opt in "checksum" "nochecksum"; do
10230                 #remount with mount option
10231                 echo "remount client with option $opt, checksum should be $i"
10232                 umount_client $MOUNT || error "failed to umount client"
10233                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
10234                         error "failed to mount client with option '$opt'"
10235                 checksum=$(eval $get_checksum)
10236                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
10237                 i=$((i - 1))
10238         done
10239
10240         remount_client $MOUNT || error "failed to remount client"
10241 }
10242 run_test 77k "enable/disable checksum correctly"
10243
10244 test_77l() {
10245         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10246         $GSS && skip_env "could not run with gss"
10247
10248         set_checksums 1
10249         stack_trap "set_checksums $ORIG_CSUM" EXIT
10250         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
10251
10252         set_checksum_type invalid && error "unexpected success of invalid checksum type"
10253
10254         $LFS setstripe -c 1 -i 0 $DIR/$tfile
10255         for algo in $CKSUM_TYPES; do
10256                 set_checksum_type $algo || error "fail to set checksum type $algo"
10257                 osc_algo=$(get_osc_checksum_type OST0000)
10258                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
10259
10260                 # no locks, no reqs to let the connection idle
10261                 cancel_lru_locks osc
10262                 lru_resize_disable osc
10263                 wait_osc_import_state client ost1 IDLE
10264
10265                 # ensure ost1 is connected
10266                 stat $DIR/$tfile >/dev/null || error "can't stat"
10267                 wait_osc_import_state client ost1 FULL
10268
10269                 osc_algo=$(get_osc_checksum_type OST0000)
10270                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
10271         done
10272         return 0
10273 }
10274 run_test 77l "preferred checksum type is remembered after reconnected"
10275
10276 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
10277 rm -f $F77_TMP
10278 unset F77_TMP
10279
10280 test_77m() {
10281         (( $CLIENT_VERSION >= $(version_code 2.14.52) )) ||
10282                 skip "Need at least version 2.14.52"
10283         local param=checksum_speed
10284
10285         $LCTL get_param $param || error "reading $param failed"
10286
10287         csum_speeds=$($LCTL get_param -n $param)
10288
10289         [[ "$csum_speeds" =~ "adler32" && "$csum_speeds" =~ "crc32" ]] ||
10290                 error "known checksum types are missing"
10291 }
10292 run_test 77m "Verify checksum_speed is correctly read"
10293
10294 check_filefrag_77n() {
10295         local nr_ext=0
10296         local starts=()
10297         local ends=()
10298
10299         while read extidx a b start end rest; do
10300                 if [[ "${extidx}" =~ ^[0-9]+: ]]; then
10301                         nr_ext=$(( $nr_ext + 1 ))
10302                         starts+=( ${start%..} )
10303                         ends+=( ${end%:} )
10304                 fi
10305         done < <( filefrag -sv $1 )
10306
10307         [[ $nr_ext -eq 2 ]] && [[ "${starts[-1]}" == $(( ${ends[0]} + 1 )) ]] && return 0
10308         return 1
10309 }
10310
10311 test_77n() {
10312         [[ "$CKSUM_TYPES" =~ t10 ]] || skip "no T10 checksum support on osc"
10313
10314         touch $DIR/$tfile
10315         $TRUNCATE $DIR/$tfile 0
10316         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=0
10317         dd if=/dev/urandom of=$DIR/$tfile bs=4k conv=notrunc count=1 seek=2
10318         check_filefrag_77n $DIR/$tfile ||
10319                 skip "$tfile blocks not contiguous around hole"
10320
10321         set_checksums 1
10322         stack_trap "set_checksums $ORIG_CSUM" EXIT
10323         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
10324         stack_trap "rm -f $DIR/$tfile"
10325
10326         for algo in $CKSUM_TYPES; do
10327                 if [[ "$algo" =~ ^t10 ]]; then
10328                         set_checksum_type $algo ||
10329                                 error "fail to set checksum type $algo"
10330                         dd if=$DIR/$tfile of=/dev/null bs=12k count=1 iflag=direct ||
10331                                 error "fail to read $tfile with $algo"
10332                 fi
10333         done
10334         rm -f $DIR/$tfile
10335         return 0
10336 }
10337 run_test 77n "Verify read from a hole inside contiguous blocks with T10PI"
10338
10339 test_77o() {
10340         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
10341                 skip "Need MDS version at least 2.14.55"
10342         (( $OST1_VERSION >= $(version_code 2.14.55) )) ||
10343                 skip "Need OST version at least 2.14.55"
10344         local ofd=obdfilter
10345         local mdt=mdt
10346
10347         # print OST checksum_type
10348         echo "$ofd.$FSNAME-*.checksum_type:"
10349         do_nodes $(comma_list $(osts_nodes)) \
10350                 $LCTL get_param -n $ofd.$FSNAME-*.checksum_type
10351
10352         # print MDT checksum_type
10353         echo "$mdt.$FSNAME-*.checksum_type:"
10354         do_nodes $(comma_list $(mdts_nodes)) \
10355                 $LCTL get_param -n $mdt.$FSNAME-*.checksum_type
10356
10357         local o_count=$(do_nodes $(comma_list $(osts_nodes)) \
10358                    $LCTL get_param -n $ofd.$FSNAME-*.checksum_type | wc -l)
10359
10360         (( $o_count == $OSTCOUNT )) ||
10361                 error "found $o_count checksums, not \$MDSCOUNT=$OSTCOUNT"
10362
10363         local m_count=$(do_nodes $(comma_list $(mdts_nodes)) \
10364                    $LCTL get_param -n $mdt.$FSNAME-*.checksum_type | wc -l)
10365
10366         (( $m_count == $MDSCOUNT )) ||
10367                 error "found $m_count checksums, not \$MDSCOUNT=$MDSCOUNT"
10368 }
10369 run_test 77o "Verify checksum_type for server (mdt and ofd(obdfilter))"
10370
10371 cleanup_test_78() {
10372         trap 0
10373         rm -f $DIR/$tfile
10374 }
10375
10376 test_78() { # bug 10901
10377         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10378         remote_ost || skip_env "local OST"
10379
10380         NSEQ=5
10381         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
10382         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
10383         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
10384         echo "MemTotal: $MEMTOTAL"
10385
10386         # reserve 256MB of memory for the kernel and other running processes,
10387         # and then take 1/2 of the remaining memory for the read/write buffers.
10388         if [ $MEMTOTAL -gt 512 ] ;then
10389                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
10390         else
10391                 # for those poor memory-starved high-end clusters...
10392                 MEMTOTAL=$((MEMTOTAL / 2))
10393         fi
10394         echo "Mem to use for directio: $MEMTOTAL"
10395
10396         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
10397         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
10398         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
10399         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
10400                 head -n1)
10401         echo "Smallest OST: $SMALLESTOST"
10402         [[ $SMALLESTOST -lt 10240 ]] &&
10403                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
10404
10405         trap cleanup_test_78 EXIT
10406
10407         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
10408                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
10409
10410         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
10411         echo "File size: $F78SIZE"
10412         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
10413         for i in $(seq 1 $NSEQ); do
10414                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
10415                 echo directIO rdwr round $i of $NSEQ
10416                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
10417         done
10418
10419         cleanup_test_78
10420 }
10421 run_test 78 "handle large O_DIRECT writes correctly ============"
10422
10423 test_79() { # bug 12743
10424         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10425
10426         wait_delete_completed
10427
10428         BKTOTAL=$(calc_osc_kbytes kbytestotal)
10429         BKFREE=$(calc_osc_kbytes kbytesfree)
10430         BKAVAIL=$(calc_osc_kbytes kbytesavail)
10431
10432         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
10433         DFTOTAL=`echo $STRING | cut -d, -f1`
10434         DFUSED=`echo $STRING  | cut -d, -f2`
10435         DFAVAIL=`echo $STRING | cut -d, -f3`
10436         DFFREE=$(($DFTOTAL - $DFUSED))
10437
10438         ALLOWANCE=$((64 * $OSTCOUNT))
10439
10440         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
10441            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
10442                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
10443         fi
10444         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
10445            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
10446                 error "df free($DFFREE) mismatch OST free($BKFREE)"
10447         fi
10448         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
10449            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
10450                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
10451         fi
10452 }
10453 run_test 79 "df report consistency check ======================="
10454
10455 test_80() { # bug 10718
10456         remote_ost_nodsh && skip "remote OST with nodsh"
10457         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10458
10459         # relax strong synchronous semantics for slow backends like ZFS
10460         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
10461                 local soc="obdfilter.*.sync_lock_cancel"
10462                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10463
10464                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
10465                 if [ -z "$save" ]; then
10466                         soc="obdfilter.*.sync_on_lock_cancel"
10467                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
10468                 fi
10469
10470                 if [ "$save" != "never" ]; then
10471                         local hosts=$(comma_list $(osts_nodes))
10472
10473                         do_nodes $hosts $LCTL set_param $soc=never
10474                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
10475                 fi
10476         fi
10477
10478         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
10479         sync; sleep 1; sync
10480         local before=$(date +%s)
10481         cancel_lru_locks osc
10482         local after=$(date +%s)
10483         local diff=$((after - before))
10484         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
10485
10486         rm -f $DIR/$tfile
10487 }
10488 run_test 80 "Page eviction is equally fast at high offsets too"
10489
10490 test_81a() { # LU-456
10491         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10492         remote_ost_nodsh && skip "remote OST with nodsh"
10493
10494         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10495         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
10496         do_facet ost1 lctl set_param fail_loc=0x80000228
10497
10498         # write should trigger a retry and success
10499         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10500         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10501         RC=$?
10502         if [ $RC -ne 0 ] ; then
10503                 error "write should success, but failed for $RC"
10504         fi
10505 }
10506 run_test 81a "OST should retry write when get -ENOSPC ==============="
10507
10508 test_81b() { # LU-456
10509         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10510         remote_ost_nodsh && skip "remote OST with nodsh"
10511
10512         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
10513         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
10514         do_facet ost1 lctl set_param fail_loc=0x228
10515
10516         # write should retry several times and return -ENOSPC finally
10517         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10518         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10519         RC=$?
10520         ENOSPC=28
10521         if [ $RC -ne $ENOSPC ] ; then
10522                 error "dd should fail for -ENOSPC, but succeed."
10523         fi
10524 }
10525 run_test 81b "OST should return -ENOSPC when retry still fails ======="
10526
10527 test_99() {
10528         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
10529
10530         test_mkdir $DIR/$tdir.cvsroot
10531         chown $RUNAS_ID $DIR/$tdir.cvsroot
10532
10533         cd $TMP
10534         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
10535
10536         cd /etc/init.d
10537         # some versions of cvs import exit(1) when asked to import links or
10538         # files they can't read.  ignore those files.
10539         local toignore=$(find . -type l -printf '-I %f\n' -o \
10540                          ! -perm /4 -printf '-I %f\n')
10541         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
10542                 $tdir.reposname vtag rtag
10543
10544         cd $DIR
10545         test_mkdir $DIR/$tdir.reposname
10546         chown $RUNAS_ID $DIR/$tdir.reposname
10547         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
10548
10549         cd $DIR/$tdir.reposname
10550         $RUNAS touch foo99
10551         $RUNAS cvs add -m 'addmsg' foo99
10552         $RUNAS cvs update
10553         $RUNAS cvs commit -m 'nomsg' foo99
10554         rm -fr $DIR/$tdir.cvsroot
10555 }
10556 run_test 99 "cvs strange file/directory operations"
10557
10558 test_100() {
10559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10560         [[ "$NETTYPE" =~ tcp ]] ||
10561                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
10562         remote_ost_nodsh && skip "remote OST with nodsh"
10563         remote_mds_nodsh && skip "remote MDS with nodsh"
10564         remote_servers ||
10565                 skip "useless for local single node setup"
10566
10567         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
10568                 [ "$PROT" != "tcp" ] && continue
10569                 RPORT=$(echo $REMOTE | cut -d: -f2)
10570                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
10571
10572                 rc=0
10573                 LPORT=`echo $LOCAL | cut -d: -f2`
10574                 if [ $LPORT -ge 1024 ]; then
10575                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
10576                         netstat -tna
10577                         error_exit "local: $LPORT > 1024, remote: $RPORT"
10578                 fi
10579         done
10580         [ "$rc" = 0 ] || error_exit "privileged port not found" )
10581 }
10582 run_test 100 "check local port using privileged port ==========="
10583
10584 function get_named_value()
10585 {
10586     local tag=$1
10587
10588     grep -w "$tag" | sed "s/^$tag  *\([0-9]*\)  *.*/\1/"
10589 }
10590
10591 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
10592                    awk '/^max_cached_mb/ { print $2 }')
10593
10594 cleanup_101a() {
10595         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
10596         trap 0
10597 }
10598
10599 test_101a() {
10600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10601
10602         local s
10603         local discard
10604         local nreads=10000
10605         local cache_limit=32
10606
10607         $LCTL set_param -n osc.*-osc*.rpc_stats=0
10608         trap cleanup_101a EXIT
10609         $LCTL set_param -n llite.*.read_ahead_stats=0
10610         $LCTL set_param -n llite.*.max_cached_mb=$cache_limit
10611
10612         #
10613         # randomly read 10000 of 64K chunks from file 3x 32MB in size
10614         #
10615         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
10616         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
10617
10618         discard=0
10619         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
10620                    get_named_value 'read.but.discarded'); do
10621                         discard=$(($discard + $s))
10622         done
10623         cleanup_101a
10624
10625         $LCTL get_param osc.*-osc*.rpc_stats
10626         $LCTL get_param llite.*.read_ahead_stats
10627
10628         # Discard is generally zero, but sometimes a few random reads line up
10629         # and trigger larger readahead, which is wasted & leads to discards.
10630         if [[ $(($discard)) -gt $nreads ]]; then
10631                 error "too many ($discard) discarded pages"
10632         fi
10633         rm -f $DIR/$tfile || true
10634 }
10635 run_test 101a "check read-ahead for random reads"
10636
10637 setup_test101bc() {
10638         test_mkdir $DIR/$tdir
10639         local ssize=$1
10640         local FILE_LENGTH=$2
10641         STRIPE_OFFSET=0
10642
10643         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
10644
10645         local list=$(comma_list $(osts_nodes))
10646         set_osd_param $list '' read_cache_enable 0
10647         set_osd_param $list '' writethrough_cache_enable 0
10648
10649         trap cleanup_test101bc EXIT
10650         # prepare the read-ahead file
10651         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
10652
10653         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
10654                                 count=$FILE_SIZE_MB 2> /dev/null
10655
10656 }
10657
10658 cleanup_test101bc() {
10659         trap 0
10660         rm -rf $DIR/$tdir
10661         rm -f $DIR/$tfile
10662
10663         local list=$(comma_list $(osts_nodes))
10664         set_osd_param $list '' read_cache_enable 1
10665         set_osd_param $list '' writethrough_cache_enable 1
10666 }
10667
10668 calc_total() {
10669         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
10670 }
10671
10672 ra_check_101() {
10673         local read_size=$1
10674         local stripe_size=$2
10675         local stride_length=$((stripe_size / read_size))
10676         local stride_width=$((stride_length * OSTCOUNT))
10677         local discard_limit=$(( ((stride_length - 1) * 3 / stride_width) *
10678                                 (stride_width - stride_length) ))
10679         local discard=$($LCTL get_param -n llite.*.read_ahead_stats |
10680                   get_named_value 'read.but.discarded' | calc_total)
10681
10682         if [[ $discard -gt $discard_limit ]]; then
10683                 $LCTL get_param llite.*.read_ahead_stats
10684                 error "($discard limit ${discard_limit}) discarded pages with size (${read_size})"
10685         else
10686                 echo "Read-ahead success for size ${read_size}"
10687         fi
10688 }
10689
10690 test_101b() {
10691         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10692         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10693
10694         local STRIPE_SIZE=1048576
10695         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
10696
10697         if [ $SLOW == "yes" ]; then
10698                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
10699         else
10700                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
10701         fi
10702
10703         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
10704
10705         # prepare the read-ahead file
10706         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10707         cancel_lru_locks osc
10708         for BIDX in 2 4 8 16 32 64 128 256
10709         do
10710                 local BSIZE=$((BIDX*4096))
10711                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
10712                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
10713                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
10714                 $LCTL set_param -n llite.*.read_ahead_stats=0
10715                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
10716                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
10717                 cancel_lru_locks osc
10718                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
10719         done
10720         cleanup_test101bc
10721         true
10722 }
10723 run_test 101b "check stride-io mode read-ahead ================="
10724
10725 test_101c() {
10726         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10727
10728         local STRIPE_SIZE=1048576
10729         local FILE_LENGTH=$((STRIPE_SIZE*100))
10730         local nreads=10000
10731         local rsize=65536
10732         local osc_rpc_stats
10733
10734         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
10735
10736         cancel_lru_locks osc
10737         $LCTL set_param osc.*.rpc_stats=0
10738         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
10739         $LCTL get_param osc.*.rpc_stats
10740         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
10741                 local stats=$($LCTL get_param -n $osc_rpc_stats)
10742                 local lines=$(echo "$stats" | awk 'END {print NR;}')
10743                 local size
10744
10745                 if [ $lines -le 20 ]; then
10746                         echo "continue debug"
10747                         continue
10748                 fi
10749                 for size in 1 2 4 8; do
10750                         local rpc=$(echo "$stats" |
10751                                     awk '($1 == "'$size':") {print $2; exit; }')
10752                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
10753                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
10754                 done
10755                 echo "$osc_rpc_stats check passed!"
10756         done
10757         cleanup_test101bc
10758         true
10759 }
10760 run_test 101c "check stripe_size aligned read-ahead"
10761
10762 test_101d() {
10763         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10764
10765         local file=$DIR/$tfile
10766         local sz_MB=${FILESIZE_101d:-80}
10767         local ra_MB=${READAHEAD_MB:-40}
10768
10769         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
10770         [ $free_MB -lt $sz_MB ] &&
10771                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
10772
10773         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
10774         $LFS setstripe -c -1 $file || error "setstripe failed"
10775
10776         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
10777         echo Cancel LRU locks on lustre client to flush the client cache
10778         cancel_lru_locks osc
10779
10780         echo Disable read-ahead
10781         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
10782         $LCTL set_param -n llite.*.max_read_ahead_mb=0
10783         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb=$old_RA" EXIT
10784         $LCTL get_param -n llite.*.max_read_ahead_mb
10785
10786         echo "Reading the test file $file with read-ahead disabled"
10787         local sz_KB=$((sz_MB * 1024 / 4))
10788         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
10789         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
10790         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10791                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10792
10793         echo "Cancel LRU locks on lustre client to flush the client cache"
10794         cancel_lru_locks osc
10795         echo Enable read-ahead with ${ra_MB}MB
10796         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
10797
10798         echo "Reading the test file $file with read-ahead enabled"
10799         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
10800                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
10801
10802         echo "read-ahead disabled time read $raOFF"
10803         echo "read-ahead enabled time read $raON"
10804
10805         rm -f $file
10806         wait_delete_completed
10807
10808         # use awk for this check instead of bash because it handles decimals
10809         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
10810                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
10811 }
10812 run_test 101d "file read with and without read-ahead enabled"
10813
10814 test_101e() {
10815         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10816
10817         local file=$DIR/$tfile
10818         local size_KB=500  #KB
10819         local count=100
10820         local bsize=1024
10821
10822         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
10823         local need_KB=$((count * size_KB))
10824         [[ $free_KB -le $need_KB ]] &&
10825                 skip_env "Need free space $need_KB, have $free_KB"
10826
10827         echo "Creating $count ${size_KB}K test files"
10828         for ((i = 0; i < $count; i++)); do
10829                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
10830         done
10831
10832         echo "Cancel LRU locks on lustre client to flush the client cache"
10833         cancel_lru_locks $OSC
10834
10835         echo "Reset readahead stats"
10836         $LCTL set_param -n llite.*.read_ahead_stats=0
10837
10838         for ((i = 0; i < $count; i++)); do
10839                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
10840         done
10841
10842         $LCTL get_param llite.*.max_cached_mb
10843         $LCTL get_param llite.*.read_ahead_stats
10844         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10845                      get_named_value 'misses' | calc_total)
10846
10847         for ((i = 0; i < $count; i++)); do
10848                 rm -rf $file.$i 2>/dev/null
10849         done
10850
10851         #10000 means 20% reads are missing in readahead
10852         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
10853 }
10854 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
10855
10856 test_101f() {
10857         which iozone || skip_env "no iozone installed"
10858
10859         local old_debug=$($LCTL get_param debug)
10860         old_debug=${old_debug#*=}
10861         $LCTL set_param debug="reada mmap"
10862
10863         # create a test file
10864         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
10865
10866         echo Cancel LRU locks on lustre client to flush the client cache
10867         cancel_lru_locks osc
10868
10869         echo Reset readahead stats
10870         $LCTL set_param -n llite.*.read_ahead_stats=0
10871
10872         echo mmap read the file with small block size
10873         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
10874                 > /dev/null 2>&1
10875
10876         echo checking missing pages
10877         $LCTL get_param llite.*.read_ahead_stats
10878         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10879                         get_named_value 'misses' | calc_total)
10880
10881         $LCTL set_param debug="$old_debug"
10882         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
10883         rm -f $DIR/$tfile
10884 }
10885 run_test 101f "check mmap read performance"
10886
10887 test_101g_brw_size_test() {
10888         local mb=$1
10889         local pages=$((mb * 1048576 / PAGE_SIZE))
10890         local file=$DIR/$tfile
10891
10892         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
10893                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
10894         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
10895                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
10896                         return 2
10897         done
10898
10899         stack_trap "rm -f $file" EXIT
10900         $LCTL set_param -n osc.*.rpc_stats=0
10901
10902         # 10 RPCs should be enough for the test
10903         local count=10
10904         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
10905                 { error "dd write ${mb} MB blocks failed"; return 3; }
10906         cancel_lru_locks osc
10907         dd of=/dev/null if=$file bs=${mb}M count=$count ||
10908                 { error "dd write ${mb} MB blocks failed"; return 4; }
10909
10910         # calculate number of full-sized read and write RPCs
10911         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
10912                 sed -n '/pages per rpc/,/^$/p' |
10913                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
10914                 END { print reads,writes }'))
10915         # allow one extra full-sized read RPC for async readahead
10916         [[ ${rpcs[0]} == $count || ${rpcs[0]} == $((count + 1)) ]] ||
10917                 { error "${rpcs[0]} != $count read RPCs"; return 5; }
10918         [[ ${rpcs[1]} == $count ]] ||
10919                 { error "${rpcs[1]} != $count write RPCs"; return 6; }
10920 }
10921
10922 test_101g() {
10923         remote_ost_nodsh && skip "remote OST with nodsh"
10924
10925         local rpcs
10926         local osts=$(get_facets OST)
10927         local list=$(comma_list $(osts_nodes))
10928         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
10929         local brw_size="obdfilter.*.brw_size"
10930
10931         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10932
10933         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
10934
10935         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
10936                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
10937                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
10938            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
10939                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
10940                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
10941
10942                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
10943                         suffix="M"
10944
10945                 if [[ $orig_mb -lt 16 ]]; then
10946                         save_lustre_params $osts "$brw_size" > $p
10947                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
10948                                 error "set 16MB RPC size failed"
10949
10950                         echo "remount client to enable new RPC size"
10951                         remount_client $MOUNT || error "remount_client failed"
10952                 fi
10953
10954                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
10955                 # should be able to set brw_size=12, but no rpc_stats for that
10956                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
10957         fi
10958
10959         test_101g_brw_size_test 4 || error "4MB RPC test failed"
10960
10961         if [[ $orig_mb -lt 16 ]]; then
10962                 restore_lustre_params < $p
10963                 remount_client $MOUNT || error "remount_client restore failed"
10964         fi
10965
10966         rm -f $p $DIR/$tfile
10967 }
10968 run_test 101g "Big bulk(4/16 MiB) readahead"
10969
10970 test_101h() {
10971         $LFS setstripe -i 0 -c 1 $DIR/$tfile
10972
10973         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
10974                 error "dd 70M file failed"
10975         echo Cancel LRU locks on lustre client to flush the client cache
10976         cancel_lru_locks osc
10977
10978         echo "Reset readahead stats"
10979         $LCTL set_param -n llite.*.read_ahead_stats 0
10980
10981         echo "Read 10M of data but cross 64M bundary"
10982         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
10983         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
10984                      get_named_value 'misses' | calc_total)
10985         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
10986         rm -f $p $DIR/$tfile
10987 }
10988 run_test 101h "Readahead should cover current read window"
10989
10990 test_101i() {
10991         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
10992                 error "dd 10M file failed"
10993
10994         local max_per_file_mb=$($LCTL get_param -n \
10995                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
10996         cancel_lru_locks osc
10997         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
10998         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
10999                 error "set max_read_ahead_per_file_mb to 1 failed"
11000
11001         echo "Reset readahead stats"
11002         $LCTL set_param llite.*.read_ahead_stats=0
11003
11004         dd if=$DIR/$tfile of=/dev/null bs=2M
11005
11006         $LCTL get_param llite.*.read_ahead_stats
11007         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11008                      awk '/misses/ { print $2 }')
11009         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
11010         rm -f $DIR/$tfile
11011 }
11012 run_test 101i "allow current readahead to exceed reservation"
11013
11014 test_101j() {
11015         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
11016                 error "setstripe $DIR/$tfile failed"
11017         local file_size=$((1048576 * 16))
11018         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
11019         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
11020
11021         echo Disable read-ahead
11022         $LCTL set_param -n llite.*.max_read_ahead_mb=0
11023
11024         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
11025         for blk in $PAGE_SIZE 1048576 $file_size; do
11026                 cancel_lru_locks osc
11027                 echo "Reset readahead stats"
11028                 $LCTL set_param -n llite.*.read_ahead_stats=0
11029                 local count=$(($file_size / $blk))
11030                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
11031                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
11032                              get_named_value 'failed.to.fast.read' | calc_total)
11033                 $LCTL get_param -n llite.*.read_ahead_stats
11034                 [ $miss -eq $count ] || error "expected $count got $miss"
11035         done
11036
11037         rm -f $p $DIR/$tfile
11038 }
11039 run_test 101j "A complete read block should be submitted when no RA"
11040
11041 setup_test102() {
11042         test_mkdir $DIR/$tdir
11043         chown $RUNAS_ID $DIR/$tdir
11044         STRIPE_SIZE=65536
11045         STRIPE_OFFSET=1
11046         STRIPE_COUNT=$OSTCOUNT
11047         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
11048
11049         trap cleanup_test102 EXIT
11050         cd $DIR
11051         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
11052         cd $DIR/$tdir
11053         for num in 1 2 3 4; do
11054                 for count in $(seq 1 $STRIPE_COUNT); do
11055                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
11056                                 local size=`expr $STRIPE_SIZE \* $num`
11057                                 local file=file"$num-$idx-$count"
11058                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
11059                         done
11060                 done
11061         done
11062
11063         cd $DIR
11064         $1 tar cf $TMP/f102.tar $tdir --xattrs
11065 }
11066
11067 cleanup_test102() {
11068         trap 0
11069         rm -f $TMP/f102.tar
11070         rm -rf $DIR/d0.sanity/d102
11071 }
11072
11073 test_102a() {
11074         [ "$UID" != 0 ] && skip "must run as root"
11075         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
11076                 skip_env "must have user_xattr"
11077
11078         [ -z "$(which setfattr 2>/dev/null)" ] &&
11079                 skip_env "could not find setfattr"
11080
11081         local testfile=$DIR/$tfile
11082
11083         touch $testfile
11084         echo "set/get xattr..."
11085         setfattr -n trusted.name1 -v value1 $testfile ||
11086                 error "setfattr -n trusted.name1=value1 $testfile failed"
11087         getfattr -n trusted.name1 $testfile 2> /dev/null |
11088           grep "trusted.name1=.value1" ||
11089                 error "$testfile missing trusted.name1=value1"
11090
11091         setfattr -n user.author1 -v author1 $testfile ||
11092                 error "setfattr -n user.author1=author1 $testfile failed"
11093         getfattr -n user.author1 $testfile 2> /dev/null |
11094           grep "user.author1=.author1" ||
11095                 error "$testfile missing trusted.author1=author1"
11096
11097         echo "listxattr..."
11098         setfattr -n trusted.name2 -v value2 $testfile ||
11099                 error "$testfile unable to set trusted.name2"
11100         setfattr -n trusted.name3 -v value3 $testfile ||
11101                 error "$testfile unable to set trusted.name3"
11102         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
11103             grep "trusted.name" | wc -l) -eq 3 ] ||
11104                 error "$testfile missing 3 trusted.name xattrs"
11105
11106         setfattr -n user.author2 -v author2 $testfile ||
11107                 error "$testfile unable to set user.author2"
11108         setfattr -n user.author3 -v author3 $testfile ||
11109                 error "$testfile unable to set user.author3"
11110         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
11111             grep "user.author" | wc -l) -eq 3 ] ||
11112                 error "$testfile missing 3 user.author xattrs"
11113
11114         echo "remove xattr..."
11115         setfattr -x trusted.name1 $testfile ||
11116                 error "$testfile error deleting trusted.name1"
11117         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
11118                 error "$testfile did not delete trusted.name1 xattr"
11119
11120         setfattr -x user.author1 $testfile ||
11121                 error "$testfile error deleting user.author1"
11122         echo "set lustre special xattr ..."
11123         $LFS setstripe -c1 $testfile
11124         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
11125                 awk -F "=" '/trusted.lov/ { print $2 }' )
11126         setfattr -n "trusted.lov" -v $lovea $testfile ||
11127                 error "$testfile doesn't ignore setting trusted.lov again"
11128         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
11129                 error "$testfile allow setting invalid trusted.lov"
11130         rm -f $testfile
11131 }
11132 run_test 102a "user xattr test =================================="
11133
11134 check_102b_layout() {
11135         local layout="$*"
11136         local testfile=$DIR/$tfile
11137
11138         echo "test layout '$layout'"
11139         $LFS setstripe $layout $testfile || error "setstripe failed"
11140         $LFS getstripe -y $testfile
11141
11142         echo "get/set/list trusted.lov xattr ..." # b=10930
11143         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
11144         [[ "$value" =~ "trusted.lov" ]] ||
11145                 error "can't get trusted.lov from $testfile"
11146         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
11147                 error "getstripe failed"
11148
11149         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
11150
11151         value=$(cut -d= -f2 <<<$value)
11152         # LU-13168: truncated xattr should fail if short lov_user_md header
11153         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
11154                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
11155         for len in $lens; do
11156                 echo "setfattr $len $testfile.2"
11157                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
11158                         [ $len -lt 66 ] && error "short xattr len=$len worked"
11159         done
11160         local stripe_size=$($LFS getstripe -S $testfile.2)
11161         local stripe_count=$($LFS getstripe -c $testfile.2)
11162         [[ $stripe_size -eq 65536 ]] ||
11163                 error "stripe size $stripe_size != 65536"
11164         [[ $stripe_count -eq $stripe_count_orig ]] ||
11165                 error "stripe count $stripe_count != $stripe_count_orig"
11166         rm $testfile $testfile.2
11167 }
11168
11169 test_102b() {
11170         [ -z "$(which setfattr 2>/dev/null)" ] &&
11171                 skip_env "could not find setfattr"
11172         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11173
11174         # check plain layout
11175         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
11176
11177         # and also check composite layout
11178         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
11179
11180 }
11181 run_test 102b "getfattr/setfattr for trusted.lov EAs"
11182
11183 test_102c() {
11184         [ -z "$(which setfattr 2>/dev/null)" ] &&
11185                 skip_env "could not find setfattr"
11186         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11187
11188         # b10930: get/set/list lustre.lov xattr
11189         echo "get/set/list lustre.lov xattr ..."
11190         test_mkdir $DIR/$tdir
11191         chown $RUNAS_ID $DIR/$tdir
11192         local testfile=$DIR/$tdir/$tfile
11193         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
11194                 error "setstripe failed"
11195         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
11196                 error "getstripe failed"
11197         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
11198         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
11199
11200         local testfile2=${testfile}2
11201         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
11202                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
11203
11204         $RUNAS $MCREATE $testfile2
11205         $RUNAS setfattr -n lustre.lov -v $value $testfile2
11206         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
11207         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
11208         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
11209         [ $stripe_count -eq $STRIPECOUNT ] ||
11210                 error "stripe count $stripe_count != $STRIPECOUNT"
11211 }
11212 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
11213
11214 compare_stripe_info1() {
11215         local stripe_index_all_zero=true
11216
11217         for num in 1 2 3 4; do
11218                 for count in $(seq 1 $STRIPE_COUNT); do
11219                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
11220                                 local size=$((STRIPE_SIZE * num))
11221                                 local file=file"$num-$offset-$count"
11222                                 stripe_size=$($LFS getstripe -S $PWD/$file)
11223                                 [[ $stripe_size -ne $size ]] &&
11224                                     error "$file: size $stripe_size != $size"
11225                                 stripe_count=$($LFS getstripe -c $PWD/$file)
11226                                 # allow fewer stripes to be created, ORI-601
11227                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
11228                                     error "$file: count $stripe_count != $count"
11229                                 stripe_index=$($LFS getstripe -i $PWD/$file)
11230                                 [[ $stripe_index -ne 0 ]] &&
11231                                         stripe_index_all_zero=false
11232                         done
11233                 done
11234         done
11235         $stripe_index_all_zero &&
11236                 error "all files are being extracted starting from OST index 0"
11237         return 0
11238 }
11239
11240 have_xattrs_include() {
11241         tar --help | grep -q xattrs-include &&
11242                 echo --xattrs-include="lustre.*"
11243 }
11244
11245 test_102d() {
11246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11247         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11248
11249         XINC=$(have_xattrs_include)
11250         setup_test102
11251         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
11252         cd $DIR/$tdir/$tdir
11253         compare_stripe_info1
11254 }
11255 run_test 102d "tar restore stripe info from tarfile,not keep osts"
11256
11257 test_102f() {
11258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11259         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11260
11261         XINC=$(have_xattrs_include)
11262         setup_test102
11263         test_mkdir $DIR/$tdir.restore
11264         cd $DIR
11265         tar cf - --xattrs $tdir | tar xf - \
11266                 -C $DIR/$tdir.restore --xattrs $XINC
11267         cd $DIR/$tdir.restore/$tdir
11268         compare_stripe_info1
11269 }
11270 run_test 102f "tar copy files, not keep osts"
11271
11272 grow_xattr() {
11273         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
11274                 skip "must have user_xattr"
11275         [ -z "$(which setfattr 2>/dev/null)" ] &&
11276                 skip_env "could not find setfattr"
11277         [ -z "$(which getfattr 2>/dev/null)" ] &&
11278                 skip_env "could not find getfattr"
11279
11280         local xsize=${1:-1024}  # in bytes
11281         local file=$DIR/$tfile
11282         local value="$(generate_string $xsize)"
11283         local xbig=trusted.big
11284         local toobig=$2
11285
11286         touch $file
11287         log "save $xbig on $file"
11288         if [ -z "$toobig" ]
11289         then
11290                 setfattr -n $xbig -v $value $file ||
11291                         error "saving $xbig on $file failed"
11292         else
11293                 setfattr -n $xbig -v $value $file &&
11294                         error "saving $xbig on $file succeeded"
11295                 return 0
11296         fi
11297
11298         local orig=$(get_xattr_value $xbig $file)
11299         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
11300
11301         local xsml=trusted.sml
11302         log "save $xsml on $file"
11303         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
11304
11305         local new=$(get_xattr_value $xbig $file)
11306         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
11307
11308         log "grow $xsml on $file"
11309         setfattr -n $xsml -v "$value" $file ||
11310                 error "growing $xsml on $file failed"
11311
11312         new=$(get_xattr_value $xbig $file)
11313         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
11314         log "$xbig still valid after growing $xsml"
11315
11316         rm -f $file
11317 }
11318
11319 test_102h() { # bug 15777
11320         grow_xattr 1024
11321 }
11322 run_test 102h "grow xattr from inside inode to external block"
11323
11324 test_102ha() {
11325         large_xattr_enabled || skip_env "ea_inode feature disabled"
11326
11327         echo "setting xattr of max xattr size: $(max_xattr_size)"
11328         grow_xattr $(max_xattr_size)
11329
11330         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
11331         echo "This should fail:"
11332         grow_xattr $(($(max_xattr_size) + 10)) 1
11333 }
11334 run_test 102ha "grow xattr from inside inode to external inode"
11335
11336 test_102i() { # bug 17038
11337         [ -z "$(which getfattr 2>/dev/null)" ] &&
11338                 skip "could not find getfattr"
11339
11340         touch $DIR/$tfile
11341         ln -s $DIR/$tfile $DIR/${tfile}link
11342         getfattr -n trusted.lov $DIR/$tfile ||
11343                 error "lgetxattr on $DIR/$tfile failed"
11344         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
11345                 grep -i "no such attr" ||
11346                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
11347         rm -f $DIR/$tfile $DIR/${tfile}link
11348 }
11349 run_test 102i "lgetxattr test on symbolic link ============"
11350
11351 test_102j() {
11352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11353         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11354
11355         XINC=$(have_xattrs_include)
11356         setup_test102 "$RUNAS"
11357         chown $RUNAS_ID $DIR/$tdir
11358         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
11359         cd $DIR/$tdir/$tdir
11360         compare_stripe_info1 "$RUNAS"
11361 }
11362 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
11363
11364 test_102k() {
11365         [ -z "$(which setfattr 2>/dev/null)" ] &&
11366                 skip "could not find setfattr"
11367
11368         touch $DIR/$tfile
11369         # b22187 just check that does not crash for regular file.
11370         setfattr -n trusted.lov $DIR/$tfile
11371         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
11372         local test_kdir=$DIR/$tdir
11373         test_mkdir $test_kdir
11374         local default_size=$($LFS getstripe -S $test_kdir)
11375         local default_count=$($LFS getstripe -c $test_kdir)
11376         local default_offset=$($LFS getstripe -i $test_kdir)
11377         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
11378                 error 'dir setstripe failed'
11379         setfattr -n trusted.lov $test_kdir
11380         local stripe_size=$($LFS getstripe -S $test_kdir)
11381         local stripe_count=$($LFS getstripe -c $test_kdir)
11382         local stripe_offset=$($LFS getstripe -i $test_kdir)
11383         [ $stripe_size -eq $default_size ] ||
11384                 error "stripe size $stripe_size != $default_size"
11385         [ $stripe_count -eq $default_count ] ||
11386                 error "stripe count $stripe_count != $default_count"
11387         [ $stripe_offset -eq $default_offset ] ||
11388                 error "stripe offset $stripe_offset != $default_offset"
11389         rm -rf $DIR/$tfile $test_kdir
11390 }
11391 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
11392
11393 test_102l() {
11394         [ -z "$(which getfattr 2>/dev/null)" ] &&
11395                 skip "could not find getfattr"
11396
11397         # LU-532 trusted. xattr is invisible to non-root
11398         local testfile=$DIR/$tfile
11399
11400         touch $testfile
11401
11402         echo "listxattr as user..."
11403         chown $RUNAS_ID $testfile
11404         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
11405             grep -q "trusted" &&
11406                 error "$testfile trusted xattrs are user visible"
11407
11408         return 0;
11409 }
11410 run_test 102l "listxattr size test =================================="
11411
11412 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
11413         local path=$DIR/$tfile
11414         touch $path
11415
11416         listxattr_size_check $path || error "listattr_size_check $path failed"
11417 }
11418 run_test 102m "Ensure listxattr fails on small bufffer ========"
11419
11420 cleanup_test102
11421
11422 getxattr() { # getxattr path name
11423         # Return the base64 encoding of the value of xattr name on path.
11424         local path=$1
11425         local name=$2
11426
11427         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
11428         # file: $path
11429         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
11430         #
11431         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
11432
11433         getfattr --absolute-names --encoding=base64 --name=$name $path |
11434                 awk -F= -v name=$name '$1 == name {
11435                         print substr($0, index($0, "=") + 1);
11436         }'
11437 }
11438
11439 test_102n() { # LU-4101 mdt: protect internal xattrs
11440         [ -z "$(which setfattr 2>/dev/null)" ] &&
11441                 skip "could not find setfattr"
11442         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
11443         then
11444                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
11445         fi
11446
11447         local file0=$DIR/$tfile.0
11448         local file1=$DIR/$tfile.1
11449         local xattr0=$TMP/$tfile.0
11450         local xattr1=$TMP/$tfile.1
11451         local namelist="lov lma lmv link fid version som hsm"
11452         local name
11453         local value
11454
11455         rm -rf $file0 $file1 $xattr0 $xattr1
11456         touch $file0 $file1
11457
11458         # Get 'before' xattrs of $file1.
11459         getfattr --absolute-names --dump --match=- $file1 > $xattr0
11460
11461         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
11462                 namelist+=" lfsck_namespace"
11463         for name in $namelist; do
11464                 # Try to copy xattr from $file0 to $file1.
11465                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11466
11467                 setfattr --name=trusted.$name --value="$value" $file1 ||
11468                         error "setxattr 'trusted.$name' failed"
11469
11470                 # Try to set a garbage xattr.
11471                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11472
11473                 if [[ x$name == "xlov" ]]; then
11474                         setfattr --name=trusted.lov --value="$value" $file1 &&
11475                         error "setxattr invalid 'trusted.lov' success"
11476                 else
11477                         setfattr --name=trusted.$name --value="$value" $file1 ||
11478                                 error "setxattr invalid 'trusted.$name' failed"
11479                 fi
11480
11481                 # Try to remove the xattr from $file1. We don't care if this
11482                 # appears to succeed or fail, we just don't want there to be
11483                 # any changes or crashes.
11484                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11485         done
11486
11487         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
11488         then
11489                 name="lfsck_ns"
11490                 # Try to copy xattr from $file0 to $file1.
11491                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
11492
11493                 setfattr --name=trusted.$name --value="$value" $file1 ||
11494                         error "setxattr 'trusted.$name' failed"
11495
11496                 # Try to set a garbage xattr.
11497                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
11498
11499                 setfattr --name=trusted.$name --value="$value" $file1 ||
11500                         error "setxattr 'trusted.$name' failed"
11501
11502                 # Try to remove the xattr from $file1. We don't care if this
11503                 # appears to succeed or fail, we just don't want there to be
11504                 # any changes or crashes.
11505                 setfattr --remove=$trusted.$name $file1 2> /dev/null
11506         fi
11507
11508         # Get 'after' xattrs of file1.
11509         getfattr --absolute-names --dump --match=- $file1 > $xattr1
11510
11511         if ! diff $xattr0 $xattr1; then
11512                 error "before and after xattrs of '$file1' differ"
11513         fi
11514
11515         rm -rf $file0 $file1 $xattr0 $xattr1
11516
11517         return 0
11518 }
11519 run_test 102n "silently ignore setxattr on internal trusted xattrs"
11520
11521 test_102p() { # LU-4703 setxattr did not check ownership
11522         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
11523                 skip "MDS needs to be at least 2.5.56"
11524
11525         local testfile=$DIR/$tfile
11526
11527         touch $testfile
11528
11529         echo "setfacl as user..."
11530         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
11531         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
11532
11533         echo "setfattr as user..."
11534         setfacl -m "u:$RUNAS_ID:---" $testfile
11535         $RUNAS setfattr -x system.posix_acl_access $testfile
11536         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
11537 }
11538 run_test 102p "check setxattr(2) correctly fails without permission"
11539
11540 test_102q() {
11541         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
11542                 skip "MDS needs to be at least 2.6.92"
11543
11544         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
11545 }
11546 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
11547
11548 test_102r() {
11549         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
11550                 skip "MDS needs to be at least 2.6.93"
11551
11552         touch $DIR/$tfile || error "touch"
11553         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
11554         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
11555         rm $DIR/$tfile || error "rm"
11556
11557         #normal directory
11558         mkdir -p $DIR/$tdir || error "mkdir"
11559         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11560         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11561         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11562                 error "$testfile error deleting user.author1"
11563         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11564                 grep "user.$(basename $tdir)" &&
11565                 error "$tdir did not delete user.$(basename $tdir)"
11566         rmdir $DIR/$tdir || error "rmdir"
11567
11568         #striped directory
11569         test_mkdir $DIR/$tdir
11570         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
11571         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
11572         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
11573                 error "$testfile error deleting user.author1"
11574         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
11575                 grep "user.$(basename $tdir)" &&
11576                 error "$tdir did not delete user.$(basename $tdir)"
11577         rmdir $DIR/$tdir || error "rm striped dir"
11578 }
11579 run_test 102r "set EAs with empty values"
11580
11581 test_102s() {
11582         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11583                 skip "MDS needs to be at least 2.11.52"
11584
11585         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11586
11587         save_lustre_params client "llite.*.xattr_cache" > $save
11588
11589         for cache in 0 1; do
11590                 lctl set_param llite.*.xattr_cache=$cache
11591
11592                 rm -f $DIR/$tfile
11593                 touch $DIR/$tfile || error "touch"
11594                 for prefix in lustre security system trusted user; do
11595                         # Note getxattr() may fail with 'Operation not
11596                         # supported' or 'No such attribute' depending
11597                         # on prefix and cache.
11598                         getfattr -n $prefix.n102s $DIR/$tfile &&
11599                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
11600                 done
11601         done
11602
11603         restore_lustre_params < $save
11604 }
11605 run_test 102s "getting nonexistent xattrs should fail"
11606
11607 test_102t() {
11608         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
11609                 skip "MDS needs to be at least 2.11.52"
11610
11611         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
11612
11613         save_lustre_params client "llite.*.xattr_cache" > $save
11614
11615         for cache in 0 1; do
11616                 lctl set_param llite.*.xattr_cache=$cache
11617
11618                 for buf_size in 0 256; do
11619                         rm -f $DIR/$tfile
11620                         touch $DIR/$tfile || error "touch"
11621                         setfattr -n user.multiop $DIR/$tfile
11622                         $MULTIOP $DIR/$tfile oa$buf_size ||
11623                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
11624                 done
11625         done
11626
11627         restore_lustre_params < $save
11628 }
11629 run_test 102t "zero length xattr values handled correctly"
11630
11631 run_acl_subtest()
11632 {
11633         local test=$LUSTRE/tests/acl/$1.test
11634         local tmp=$(mktemp -t $1-XXXXXX).test
11635         local bin=$2
11636         local dmn=$3
11637         local grp=$4
11638         local nbd=$5
11639         export LANG=C
11640
11641
11642         local sedusers="-e s/bin/$bin/g -e s/daemon/$dmn/g"
11643         local sedgroups="-e s/:users/:$grp/g"
11644         [[ -z "$nbd" ]] || sedusers+=" -e s/nobody/$nbd/g"
11645
11646         sed $sedusers $sedgroups < $test > $tmp
11647         stack_trap "rm -f $tmp"
11648         [[ -s $tmp ]] || error "sed failed to create test script"
11649
11650         echo "performing $1 with bin='$bin' daemon='$dmn' users='$grp'..."
11651         $LUSTRE/tests/acl/run $tmp || error "run_acl_subtest '$1' failed"
11652 }
11653
11654 test_103a() {
11655         [ "$UID" != 0 ] && skip "must run as root"
11656         $GSS && skip_env "could not run under gss"
11657         [[ "$(lctl get_param -n mdc.*-mdc-*.connect_flags)" =~ "acl" ]] ||
11658                 skip_env "must have acl enabled"
11659         which setfacl || skip_env "could not find setfacl"
11660         remote_mds_nodsh && skip "remote MDS with nodsh"
11661
11662         ACLBIN=${ACLBIN:-"bin"}
11663         ACLDMN=${ACLDMN:-"daemon"}
11664         ACLGRP=${ACLGRP:-"users"}
11665         ACLNBD=${ACLNBD:-"nobody"}
11666
11667         if ! id $ACLBIN ||
11668            [[ "$(id -u $ACLBIN)" != "$(do_facet mds1 id -u $ACLBIN)" ]]; then
11669                 echo "bad 'bin' user '$ACLBIN', using '$USER0'"
11670                 ACLBIN=$USER0
11671                 if ! id $ACLBIN ; then
11672                         cat /etc/passwd
11673                         skip_env "can't find suitable ACL 'bin' $ACLBIN"
11674                 fi
11675         fi
11676         if ! id $ACLDMN || (( $(id -u $ACLDMN) < $(id -u $ACLBIN) )) ||
11677            [[ "$(id -u $ACLDMN)" != "$(do_facet mds1 id -u $ACLDMN)" ]]; then
11678                 echo "bad 'daemon' user '$ACLDMN', using '$USER1'"
11679                 ACLDMN=$USER1
11680                 if ! id $ACLDMN ; then
11681                         cat /etc/passwd
11682                         skip_env "can't find suitable ACL 'daemon' $ACLDMN"
11683                 fi
11684         fi
11685         if ! getent group $ACLGRP; then
11686                 echo "missing 'users' group '$ACLGRP', using '$TSTUSR'"
11687                 ACLGRP="$TSTUSR"
11688                 if ! getent group $ACLGRP; then
11689                         echo "cannot find group '$ACLGRP', adding it"
11690                         cat /etc/group
11691                         add_group 60000 $ACLGRP
11692                 fi
11693         fi
11694
11695         local bingid=$(getent group $ACLBIN | cut -d: -f 3)
11696         local dmngid=$(getent group $ACLDMN | cut -d: -f 3)
11697         local grpgid=$(getent group $ACLGRP | cut -d: -f 3)
11698
11699         if (( $bingid > $grpgid || $dmngid > $grpgid )); then
11700                 echo "group '$ACLGRP' has low gid=$grpgid, use '$TSTUSR'"
11701                 ACLGRP="$TSTUSR"
11702                 if ! getent group $ACLGRP; then
11703                         echo "cannot find group '$ACLGRP', adding it"
11704                         cat /etc/group
11705                         add_group 60000 $ACLGRP
11706                 fi
11707                 grpgid=$(getent group $ACLGRP | cut -d: -f 3)
11708                 if (( $bingid > $grpgid || $dmngid > $grpgid )); then
11709                         cat /etc/group
11710                         skip_env "$ACLGRP gid=$grpgid less than $bingid|$dmngid"
11711                 fi
11712         fi
11713
11714         gpasswd -a $ACLDMN $ACLBIN ||
11715                 error "setting client group failed"             # LU-5641
11716         do_facet mds1 gpasswd -a $ACLDMN $ACLBIN ||
11717                 error "setting MDS group failed"                # LU-5641
11718
11719         declare -a identity_old
11720
11721         for num in $(seq $MDSCOUNT); do
11722                 switch_identity $num true || identity_old[$num]=$?
11723         done
11724
11725         SAVE_UMASK=$(umask)
11726         umask 0022
11727         mkdir -p $DIR/$tdir
11728         cd $DIR/$tdir
11729
11730         run_acl_subtest cp $ACLBIN $ACLDMN $ACLGRP
11731         run_acl_subtest getfacl-noacl $ACLBIN $ACLDMN $ACLGRP
11732         run_acl_subtest misc $ACLBIN $ACLDMN $ACLGRP
11733         run_acl_subtest permissions $ACLBIN $ACLDMN $ACLGRP
11734         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
11735         # CentOS7- uses nobody=99, while newer distros use nobody=65534
11736         if ! id -u $ACLNBD ||
11737            (( $(id -u nobody) != $(do_facet mds1 id -u nobody) )); then
11738                 ACLNBD="nfsnobody"
11739                 if ! id -u $ACLNBD; then
11740                         ACLNBD=""
11741                 fi
11742         fi
11743         if [[ -n "$ACLNBD" ]] && ! getent group $ACLNBD; then
11744                 add_group $(id -u $ACLNBD) $ACLNBD
11745                 if ! getent group $ACLNBD; then
11746                         ACLNBD=""
11747                 fi
11748         fi
11749         if (( $MDS1_VERSION > $(version_code 2.8.55) )) &&
11750            [[ -n "$ACLNBD" ]] && which setfattr; then
11751                 run_acl_subtest permissions_xattr \
11752                         $ACLBIN $ACLDMN $ACLGRP $ACLNBD
11753         elif [[ -z "$ACLNBD" ]]; then
11754                 echo "skip 'permission_xattr' test - missing 'nobody' user/grp"
11755         else
11756                 echo "skip 'permission_xattr' test - missing setfattr command"
11757         fi
11758         run_acl_subtest setfacl $ACLBIN $ACLDMN $ACLGRP
11759
11760         # inheritance test got from HP
11761         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
11762         chmod +x make-tree || error "chmod +x failed"
11763         run_acl_subtest inheritance $ACLBIN $ACLDMN $ACLGRP
11764         rm -f make-tree
11765
11766         echo "LU-974 ignore umask when acl is enabled..."
11767         run_acl_subtest 974 $ACLBIN $ACLDMN $ACLGRP
11768         if [ $MDSCOUNT -ge 2 ]; then
11769                 run_acl_subtest 974_remote $ACLBIN $ACLDMN $ACLGRP
11770         fi
11771
11772         echo "LU-2561 newly created file is same size as directory..."
11773         if [ "$mds1_FSTYPE" != "zfs" ]; then
11774                 run_acl_subtest 2561 $ACLBIN $ACLDMN $ACLGRP
11775         else
11776                 run_acl_subtest 2561_zfs $ACLBIN $ACLDMN $ACLGRP
11777         fi
11778
11779         run_acl_subtest 4924 $ACLBIN $ACLDMN $ACLGRP
11780
11781         cd $SAVE_PWD
11782         umask $SAVE_UMASK
11783
11784         for num in $(seq $MDSCOUNT); do
11785                 if [ "${identity_old[$num]}" = 1 ]; then
11786                         switch_identity $num false || identity_old[$num]=$?
11787                 fi
11788         done
11789 }
11790 run_test 103a "acl test"
11791
11792 test_103b() {
11793         declare -a pids
11794         local U
11795
11796         for U in {0..511}; do
11797                 {
11798                 local O=$(printf "%04o" $U)
11799
11800                 umask $(printf "%04o" $((511 ^ $O)))
11801                 $LFS setstripe -c 1 $DIR/$tfile.s$O
11802                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
11803
11804                 (( $S == ($O & 0666) )) ||
11805                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
11806
11807                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
11808                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
11809                 (( $S == ($O & 0666) )) ||
11810                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
11811
11812                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
11813                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
11814                 (( $S == ($O & 0666) )) ||
11815                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
11816                 rm -f $DIR/$tfile.[smp]$0
11817                 } &
11818                 local pid=$!
11819
11820                 # limit the concurrently running threads to 64. LU-11878
11821                 local idx=$((U % 64))
11822                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
11823                 pids[idx]=$pid
11824         done
11825         wait
11826 }
11827 run_test 103b "umask lfs setstripe"
11828
11829 test_103c() {
11830         mkdir -p $DIR/$tdir
11831         cp -rp $DIR/$tdir $DIR/$tdir.bak
11832
11833         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
11834                 error "$DIR/$tdir shouldn't contain default ACL"
11835         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
11836                 error "$DIR/$tdir.bak shouldn't contain default ACL"
11837         true
11838 }
11839 run_test 103c "'cp -rp' won't set empty acl"
11840
11841 test_103e() {
11842         local numacl
11843         local fileacl
11844         local saved_debug=$($LCTL get_param -n debug)
11845
11846         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
11847                 skip "MDS needs to be at least 2.14.52"
11848
11849         large_xattr_enabled || skip_env "ea_inode feature disabled"
11850
11851         mkdir -p $DIR/$tdir
11852         # add big LOV EA to cause reply buffer overflow earlier
11853         $LFS setstripe -C 1000 $DIR/$tdir
11854         lctl set_param mdc.*-mdc*.stats=clear
11855
11856         $LCTL set_param debug=0
11857         stack_trap "$LCTL set_param debug=\"$saved_debug\"" EXIT
11858         stack_trap "$LCTL get_param mdc.*-mdc*.stats" EXIT
11859
11860         # add a large number of default ACLs (expect 8000+ for 2.13+)
11861         for U in {2..7000}; do
11862                 setfacl -d -m user:$U:rwx $DIR/$tdir ||
11863                         error "Able to add just $U default ACLs"
11864         done
11865         numacl=$(getfacl $DIR/$tdir |& grep -c "default:user")
11866         echo "$numacl default ACLs created"
11867
11868         stat $DIR/$tdir || error "Cannot stat directory"
11869         # check file creation
11870         touch $DIR/$tdir/$tfile ||
11871                 error "failed to create $tfile with $numacl default ACLs"
11872         stat $DIR/$tdir/$tfile  || error "Cannot stat file"
11873         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11874         echo "$fileacl ACLs were inherited"
11875         (( $fileacl == $numacl )) ||
11876                 error "Not all default ACLs were inherited: $numacl != $fileacl"
11877         # check that new ACLs creation adds new ACLs to inherited ACLs
11878         setfacl -m user:19000:rwx $DIR/$tdir/$tfile ||
11879                 error "Cannot set new ACL"
11880         numacl=$((numacl + 1))
11881         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11882         (( $fileacl == $numacl )) ||
11883                 error "failed to add new ACL: $fileacl != $numacl as expected"
11884         # adds more ACLs to a file to reach their maximum at 8000+
11885         numacl=0
11886         for U in {20000..25000}; do
11887                 setfacl -m user:$U:rwx $DIR/$tdir/$tfile || break
11888                 numacl=$((numacl + 1))
11889         done
11890         echo "Added $numacl more ACLs to the file"
11891         fileacl=$(getfacl $DIR/$tdir/$tfile |& grep -c "user:")
11892         echo "Total $fileacl ACLs in file"
11893         stat $DIR/$tdir/$tfile > /dev/null || error "Cannot stat file"
11894         rm -f $DIR/$tdir/$tfile || error "Cannot remove file"
11895         rmdir $DIR/$tdir || error "Cannot remove directory"
11896 }
11897 run_test 103e "inheritance of big amount of default ACLs"
11898
11899 test_103f() {
11900         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
11901                 skip "MDS needs to be at least 2.14.51"
11902
11903         large_xattr_enabled || skip_env "ea_inode feature disabled"
11904
11905         # enable changelog to consume more internal MDD buffers
11906         changelog_register
11907
11908         mkdir -p $DIR/$tdir
11909         # add big LOV EA
11910         $LFS setstripe -C 1000 $DIR/$tdir
11911         setfacl -d -m user:$U:rwx $DIR/$tdir || error "Cannot add default ACLs"
11912         mkdir $DIR/$tdir/inherited || error "failed to create subdirectory"
11913         rmdir $DIR/$tdir/inherited || error "Cannot remove subdirectory"
11914         rmdir $DIR/$tdir || error "Cannot remove directory"
11915 }
11916 run_test 103f "changelog doesn't interfere with default ACLs buffers"
11917
11918 test_104a() {
11919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11920
11921         touch $DIR/$tfile
11922         lfs df || error "lfs df failed"
11923         lfs df -ih || error "lfs df -ih failed"
11924         lfs df -h $DIR || error "lfs df -h $DIR failed"
11925         lfs df -i $DIR || error "lfs df -i $DIR failed"
11926         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
11927         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
11928
11929         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
11930         lctl --device %$OSC deactivate
11931         lfs df || error "lfs df with deactivated OSC failed"
11932         lctl --device %$OSC activate
11933         # wait the osc back to normal
11934         wait_osc_import_ready client ost
11935
11936         lfs df || error "lfs df with reactivated OSC failed"
11937         rm -f $DIR/$tfile
11938 }
11939 run_test 104a "lfs df [-ih] [path] test ========================="
11940
11941 test_104b() {
11942         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11943         [ $RUNAS_ID -eq $UID ] &&
11944                 skip_env "RUNAS_ID = UID = $UID -- skipping"
11945
11946         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
11947                         grep "Permission denied" | wc -l)))
11948         if [ $denied_cnt -ne 0 ]; then
11949                 error "lfs check servers test failed"
11950         fi
11951 }
11952 run_test 104b "$RUNAS lfs check servers test ===================="
11953
11954 #
11955 # Verify $1 is within range of $2.
11956 # Success when $1 is within range. That is, when $1 is >= 2% of $2 and
11957 # $1 is <= 2% of $2. Else Fail.
11958 #
11959 value_in_range() {
11960         # Strip all units (M, G, T)
11961         actual=$(echo $1 | tr -d A-Z)
11962         expect=$(echo $2 | tr -d A-Z)
11963
11964         expect_lo=$(($expect * 98 / 100)) # 2% below
11965         expect_hi=$(($expect * 102 / 100)) # 2% above
11966
11967         # permit 2% drift above and below
11968         (( $actual >= $expect_lo && $actual <= $expect_hi ))
11969 }
11970
11971 test_104c() {
11972         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11973         [ "$ost1_FSTYPE" == "zfs" ] || skip "zfs only test"
11974
11975         local ost_param="osd-zfs.$FSNAME-OST0000."
11976         local mdt_param="osd-zfs.$FSNAME-MDT0000."
11977         local ofacets=$(get_facets OST)
11978         local mfacets=$(get_facets MDS)
11979         local saved_ost_blocks=
11980         local saved_mdt_blocks=
11981
11982         echo "Before recordsize change"
11983         lfs_df=($($LFS df -h | grep "filesystem_summary:"))
11984         df=($(df -h | grep "$MOUNT"$))
11985
11986         # For checking.
11987         echo "lfs output : ${lfs_df[*]}"
11988         echo "df  output : ${df[*]}"
11989
11990         for facet in ${ofacets//,/ }; do
11991                 if [ -z $saved_ost_blocks ]; then
11992                         saved_ost_blocks=$(do_facet $facet \
11993                                 lctl get_param -n $ost_param.blocksize)
11994                         echo "OST Blocksize: $saved_ost_blocks"
11995                 fi
11996                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
11997                 do_facet $facet zfs set recordsize=32768 $ost
11998         done
11999
12000         # BS too small. Sufficient for functional testing.
12001         for facet in ${mfacets//,/ }; do
12002                 if [ -z $saved_mdt_blocks ]; then
12003                         saved_mdt_blocks=$(do_facet $facet \
12004                                 lctl get_param -n $mdt_param.blocksize)
12005                         echo "MDT Blocksize: $saved_mdt_blocks"
12006                 fi
12007                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
12008                 do_facet $facet zfs set recordsize=32768 $mdt
12009         done
12010
12011         # Give new values chance to reflect change
12012         sleep 2
12013
12014         echo "After recordsize change"
12015         lfs_df_after=($($LFS df -h | grep "filesystem_summary:"))
12016         df_after=($(df -h | grep "$MOUNT"$))
12017
12018         # For checking.
12019         echo "lfs output : ${lfs_df_after[*]}"
12020         echo "df  output : ${df_after[*]}"
12021
12022         # Verify lfs df
12023         value_in_range ${lfs_df_after[1]%.*} ${lfs_df[1]%.*} ||
12024                 error "lfs_df bytes: ${lfs_df_after[1]%.*} != ${lfs_df[1]%.*}"
12025         value_in_range ${lfs_df_after[2]%.*} ${lfs_df[2]%.*} ||
12026                 error "lfs_df used: ${lfs_df_after[2]%.*} != ${lfs_df[2]%.*}"
12027         value_in_range ${lfs_df_after[3]%.*} ${lfs_df[3]%.*} ||
12028                 error "lfs_df avail: ${lfs_df_after[3]%.*} != ${lfs_df[3]%.*}"
12029
12030         # Verify df
12031         value_in_range ${df_after[1]%.*} ${df[1]%.*} ||
12032                 error "df bytes: ${df_after[1]%.*} != ${df[1]%.*}"
12033         value_in_range ${df_after[2]%.*} ${df[2]%.*} ||
12034                 error "df used: ${df_after[2]%.*} != ${df[2]%.*}"
12035         value_in_range ${df_after[3]%.*} ${df[3]%.*} ||
12036                 error "df avail: ${df_after[3]%.*} != ${df[3]%.*}"
12037
12038         # Restore MDT recordize back to original
12039         for facet in ${mfacets//,/ }; do
12040                 mdt=$(do_facet $facet lctl get_param -n $mdt_param.mntdev)
12041                 do_facet $facet zfs set recordsize=$saved_mdt_blocks $mdt
12042         done
12043
12044         # Restore OST recordize back to original
12045         for facet in ${ofacets//,/ }; do
12046                 ost=$(do_facet $facet lctl get_param -n $ost_param.mntdev)
12047                 do_facet $facet zfs set recordsize=$saved_ost_blocks $ost
12048         done
12049
12050         return 0
12051 }
12052 run_test 104c "Verify df vs lfs_df stays same after recordsize change"
12053
12054 test_104d() {
12055         (( $RUNAS_ID != $UID )) ||
12056                 skip_env "RUNAS_ID = UID = $UID -- skipping"
12057
12058         (( $CLIENT_VERSION >= $(version_code 2.15.51) )) ||
12059                 skip "lustre version doesn't support lctl dl with non-root"
12060
12061         # debugfs only allows root users to access files, so the
12062         # previous move of the "devices" file to debugfs broke
12063         # "lctl dl" for non-root users. The LU-9680 Netlink
12064         # interface again allows non-root users to list devices.
12065         [ "$($RUNAS $LCTL dl | wc -l)" -ge 3 ] ||
12066                 error "lctl dl doesn't work for non root"
12067
12068         ost_count="$($RUNAS $LCTL dl | grep $FSNAME-OST* | wc -l)"
12069         [ "$ost_count" -eq $OSTCOUNT ]  ||
12070                 error "lctl dl reports wrong number of OST devices"
12071
12072         mdt_count="$($RUNAS $LCTL dl | grep $FSNAME-MDT* | wc -l)"
12073         [ "$mdt_count" -eq $MDSCOUNT ]  ||
12074                 error "lctl dl reports wrong number of MDT devices"
12075 }
12076 run_test 104d "$RUNAS lctl dl test"
12077
12078 test_105a() {
12079         # doesn't work on 2.4 kernels
12080         touch $DIR/$tfile
12081         if $(flock_is_enabled); then
12082                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
12083         else
12084                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
12085         fi
12086         rm -f $DIR/$tfile
12087 }
12088 run_test 105a "flock when mounted without -o flock test ========"
12089
12090 test_105b() {
12091         touch $DIR/$tfile
12092         if $(flock_is_enabled); then
12093                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
12094         else
12095                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
12096         fi
12097         rm -f $DIR/$tfile
12098 }
12099 run_test 105b "fcntl when mounted without -o flock test ========"
12100
12101 test_105c() {
12102         touch $DIR/$tfile
12103         if $(flock_is_enabled); then
12104                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
12105         else
12106                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
12107         fi
12108         rm -f $DIR/$tfile
12109 }
12110 run_test 105c "lockf when mounted without -o flock test"
12111
12112 test_105d() { # bug 15924
12113         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12114
12115         test_mkdir $DIR/$tdir
12116         flock_is_enabled || skip_env "mount w/o flock enabled"
12117         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
12118         $LCTL set_param fail_loc=0x80000315
12119         flocks_test 2 $DIR/$tdir
12120 }
12121 run_test 105d "flock race (should not freeze) ========"
12122
12123 test_105e() { # bug 22660 && 22040
12124         flock_is_enabled || skip_env "mount w/o flock enabled"
12125
12126         touch $DIR/$tfile
12127         flocks_test 3 $DIR/$tfile
12128 }
12129 run_test 105e "Two conflicting flocks from same process"
12130
12131 test_106() { #bug 10921
12132         test_mkdir $DIR/$tdir
12133         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
12134         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
12135 }
12136 run_test 106 "attempt exec of dir followed by chown of that dir"
12137
12138 test_107() {
12139         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12140
12141         CDIR=`pwd`
12142         local file=core
12143
12144         cd $DIR
12145         rm -f $file
12146
12147         local save_pattern=$(sysctl -n kernel.core_pattern)
12148         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
12149         sysctl -w kernel.core_pattern=$file
12150         sysctl -w kernel.core_uses_pid=0
12151
12152         ulimit -c unlimited
12153         sleep 60 &
12154         SLEEPPID=$!
12155
12156         sleep 1
12157
12158         kill -s 11 $SLEEPPID
12159         wait $SLEEPPID
12160         if [ -e $file ]; then
12161                 size=`stat -c%s $file`
12162                 [ $size -eq 0 ] && error "Fail to create core file $file"
12163         else
12164                 error "Fail to create core file $file"
12165         fi
12166         rm -f $file
12167         sysctl -w kernel.core_pattern=$save_pattern
12168         sysctl -w kernel.core_uses_pid=$save_uses_pid
12169         cd $CDIR
12170 }
12171 run_test 107 "Coredump on SIG"
12172
12173 test_110() {
12174         test_mkdir $DIR/$tdir
12175         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
12176         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
12177                 error "mkdir with 256 char should fail, but did not"
12178         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
12179                 error "create with 255 char failed"
12180         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
12181                 error "create with 256 char should fail, but did not"
12182
12183         ls -l $DIR/$tdir
12184         rm -rf $DIR/$tdir
12185 }
12186 run_test 110 "filename length checking"
12187
12188 #
12189 # Purpose: To verify dynamic thread (OSS) creation.
12190 #
12191 test_115() {
12192         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12193         remote_ost_nodsh && skip "remote OST with nodsh"
12194
12195         # Lustre does not stop service threads once they are started.
12196         # Reset number of running threads to default.
12197         stopall
12198         setupall
12199
12200         local OSTIO_pre
12201         local save_params="$TMP/sanity-$TESTNAME.parameters"
12202
12203         # Get ll_ost_io count before I/O
12204         OSTIO_pre=$(do_facet ost1 \
12205                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
12206         # Exit if lustre is not running (ll_ost_io not running).
12207         [ -z "$OSTIO_pre" ] && error "no OSS threads"
12208
12209         echo "Starting with $OSTIO_pre threads"
12210         local thread_max=$((OSTIO_pre * 2))
12211         local rpc_in_flight=$((thread_max * 2))
12212         # this is limited to OSC_MAX_RIF_MAX (256)
12213         [ $rpc_in_flight -gt 256 ] && rpc_in_flight=256
12214         thread_max=$((rpc_in_flight / 2))
12215         [ $thread_max -le $OSTIO_pre ] && skip "Too many ost_io threads" &&
12216                 return
12217
12218         # Number of I/O Process proposed to be started.
12219         local nfiles
12220         local facets=$(get_facets OST)
12221
12222         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
12223         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
12224
12225         # Set in_flight to $rpc_in_flight
12226         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
12227                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
12228         nfiles=${rpc_in_flight}
12229         # Set ost thread_max to $thread_max
12230         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
12231
12232         # 5 Minutes should be sufficient for max number of OSS
12233         # threads(thread_max) to be created.
12234         local timeout=300
12235
12236         # Start I/O.
12237         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
12238         test_mkdir $DIR/$tdir
12239         for i in $(seq $nfiles); do
12240                 local file=$DIR/$tdir/${tfile}-$i
12241                 $LFS setstripe -c -1 -i 0 $file
12242                 ($WTL $file $timeout)&
12243         done
12244
12245         # I/O Started - Wait for thread_started to reach thread_max or report
12246         # error if thread_started is more than thread_max.
12247         echo "Waiting for thread_started to reach thread_max"
12248         local thread_started=0
12249         local end_time=$((SECONDS + timeout))
12250
12251         while [ $SECONDS -le $end_time ] ; do
12252                 echo -n "."
12253                 # Get ost i/o thread_started count.
12254                 thread_started=$(do_facet ost1 \
12255                         "$LCTL get_param \
12256                         ost.OSS.ost_io.threads_started | cut -d= -f2")
12257                 # Break out if thread_started is equal/greater than thread_max
12258                 if [[ $thread_started -ge $thread_max ]]; then
12259                         echo ll_ost_io thread_started $thread_started, \
12260                                 equal/greater than thread_max $thread_max
12261                         break
12262                 fi
12263                 sleep 1
12264         done
12265
12266         # Cleanup - We have the numbers, Kill i/o jobs if running.
12267         jobcount=($(jobs -p))
12268         for i in $(seq 0 $((${#jobcount[@]}-1)))
12269         do
12270                 kill -9 ${jobcount[$i]}
12271                 if [ $? -ne 0 ] ; then
12272                         echo Warning: \
12273                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
12274                 fi
12275         done
12276
12277         # Cleanup files left by WTL binary.
12278         for i in $(seq $nfiles); do
12279                 local file=$DIR/$tdir/${tfile}-$i
12280                 rm -rf $file
12281                 if [ $? -ne 0 ] ; then
12282                         echo "Warning: Failed to delete file $file"
12283                 fi
12284         done
12285
12286         restore_lustre_params <$save_params
12287         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
12288
12289         # Error out if no new thread has started or Thread started is greater
12290         # than thread max.
12291         if [[ $thread_started -le $OSTIO_pre ||
12292                         $thread_started -gt $thread_max ]]; then
12293                 error "ll_ost_io: thread_started $thread_started" \
12294                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
12295                       "No new thread started or thread started greater " \
12296                       "than thread_max."
12297         fi
12298 }
12299 run_test 115 "verify dynamic thread creation===================="
12300
12301 test_116a() { # was previously test_116()
12302         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12303         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12304         remote_mds_nodsh && skip "remote MDS with nodsh"
12305
12306         echo -n "Free space priority "
12307         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
12308                 head -n1
12309         declare -a AVAIL
12310         free_min_max
12311
12312         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
12313         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
12314         stack_trap simple_cleanup_common
12315
12316         # Check if we need to generate uneven OSTs
12317         test_mkdir -p $DIR/$tdir/OST${MINI}
12318         local FILL=$((MINV / 4))
12319         local DIFF=$((MAXV - MINV))
12320         local DIFF2=$((DIFF * 100 / MINV))
12321
12322         local threshold=$(do_facet $SINGLEMDS \
12323                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
12324         threshold=${threshold%%%}
12325         echo -n "Check for uneven OSTs: "
12326         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
12327
12328         if [[ $DIFF2 -gt $threshold ]]; then
12329                 echo "ok"
12330                 echo "Don't need to fill OST$MINI"
12331         else
12332                 # generate uneven OSTs. Write 2% over the QOS threshold value
12333                 echo "no"
12334                 DIFF=$((threshold - DIFF2 + 2))
12335                 DIFF2=$((MINV * DIFF / 100))
12336                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
12337                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
12338                         error "setstripe failed"
12339                 DIFF=$((DIFF2 / 2048))
12340                 i=0
12341                 while [ $i -lt $DIFF ]; do
12342                         i=$((i + 1))
12343                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
12344                                 bs=2M count=1 2>/dev/null
12345                         echo -n .
12346                 done
12347                 echo .
12348                 sync
12349                 sleep_maxage
12350                 free_min_max
12351         fi
12352
12353         DIFF=$((MAXV - MINV))
12354         DIFF2=$((DIFF * 100 / MINV))
12355         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
12356         if [ $DIFF2 -gt $threshold ]; then
12357                 echo "ok"
12358         else
12359                 skip "QOS imbalance criteria not met"
12360         fi
12361
12362         MINI1=$MINI
12363         MINV1=$MINV
12364         MAXI1=$MAXI
12365         MAXV1=$MAXV
12366
12367         # now fill using QOS
12368         $LFS setstripe -c 1 $DIR/$tdir
12369         FILL=$((FILL / 200))
12370         if [ $FILL -gt 600 ]; then
12371                 FILL=600
12372         fi
12373         echo "writing $FILL files to QOS-assigned OSTs"
12374         i=0
12375         while [ $i -lt $FILL ]; do
12376                 i=$((i + 1))
12377                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
12378                         count=1 2>/dev/null
12379                 echo -n .
12380         done
12381         echo "wrote $i 200k files"
12382         sync
12383         sleep_maxage
12384
12385         echo "Note: free space may not be updated, so measurements might be off"
12386         free_min_max
12387         DIFF2=$((MAXV - MINV))
12388         echo "free space delta: orig $DIFF final $DIFF2"
12389         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
12390         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
12391         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
12392         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
12393         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
12394         if [[ $DIFF -gt 0 ]]; then
12395                 FILL=$((DIFF2 * 100 / DIFF - 100))
12396                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
12397         fi
12398
12399         # Figure out which files were written where
12400         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
12401                awk '/'$MINI1': / {print $2; exit}')
12402         echo $UUID
12403         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
12404         echo "$MINC files created on smaller OST $MINI1"
12405         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
12406                awk '/'$MAXI1': / {print $2; exit}')
12407         echo $UUID
12408         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
12409         echo "$MAXC files created on larger OST $MAXI1"
12410         if [[ $MINC -gt 0 ]]; then
12411                 FILL=$((MAXC * 100 / MINC - 100))
12412                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
12413         fi
12414         [[ $MAXC -gt $MINC ]] ||
12415                 error_ignore LU-9 "stripe QOS didn't balance free space"
12416 }
12417 run_test 116a "stripe QOS: free space balance ==================="
12418
12419 test_116b() { # LU-2093
12420         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12421         remote_mds_nodsh && skip "remote MDS with nodsh"
12422
12423 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
12424         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
12425                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
12426         [ -z "$old_rr" ] && skip "no QOS"
12427         do_facet $SINGLEMDS lctl set_param \
12428                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
12429         mkdir -p $DIR/$tdir
12430         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
12431         createmany -o $DIR/$tdir/f- 20 || error "can't create"
12432         do_facet $SINGLEMDS lctl set_param fail_loc=0
12433         rm -rf $DIR/$tdir
12434         do_facet $SINGLEMDS lctl set_param \
12435                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
12436 }
12437 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
12438
12439 test_117() # bug 10891
12440 {
12441         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12442
12443         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
12444         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
12445         lctl set_param fail_loc=0x21e
12446         > $DIR/$tfile || error "truncate failed"
12447         lctl set_param fail_loc=0
12448         echo "Truncate succeeded."
12449         rm -f $DIR/$tfile
12450 }
12451 run_test 117 "verify osd extend =========="
12452
12453 NO_SLOW_RESENDCOUNT=4
12454 export OLD_RESENDCOUNT=""
12455 set_resend_count () {
12456         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
12457         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
12458         lctl set_param -n $PROC_RESENDCOUNT $1
12459         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
12460 }
12461
12462 # for reduce test_118* time (b=14842)
12463 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12464
12465 # Reset async IO behavior after error case
12466 reset_async() {
12467         FILE=$DIR/reset_async
12468
12469         # Ensure all OSCs are cleared
12470         $LFS setstripe -c -1 $FILE
12471         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
12472         sync
12473         rm $FILE
12474 }
12475
12476 test_118a() #bug 11710
12477 {
12478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12479
12480         reset_async
12481
12482         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12483         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12484         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12485
12486         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12487                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12488                 return 1;
12489         fi
12490         rm -f $DIR/$tfile
12491 }
12492 run_test 118a "verify O_SYNC works =========="
12493
12494 test_118b()
12495 {
12496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12497         remote_ost_nodsh && skip "remote OST with nodsh"
12498
12499         reset_async
12500
12501         #define OBD_FAIL_SRV_ENOENT 0x217
12502         set_nodes_failloc "$(osts_nodes)" 0x217
12503         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12504         RC=$?
12505         set_nodes_failloc "$(osts_nodes)" 0
12506         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12507         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12508                     grep -c writeback)
12509
12510         if [[ $RC -eq 0 ]]; then
12511                 error "Must return error due to dropped pages, rc=$RC"
12512                 return 1;
12513         fi
12514
12515         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12516                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12517                 return 1;
12518         fi
12519
12520         echo "Dirty pages not leaked on ENOENT"
12521
12522         # Due to the above error the OSC will issue all RPCs syncronously
12523         # until a subsequent RPC completes successfully without error.
12524         $MULTIOP $DIR/$tfile Ow4096yc
12525         rm -f $DIR/$tfile
12526
12527         return 0
12528 }
12529 run_test 118b "Reclaim dirty pages on fatal error =========="
12530
12531 test_118c()
12532 {
12533         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12534
12535         # for 118c, restore the original resend count, LU-1940
12536         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
12537                                 set_resend_count $OLD_RESENDCOUNT
12538         remote_ost_nodsh && skip "remote OST with nodsh"
12539
12540         reset_async
12541
12542         #define OBD_FAIL_OST_EROFS               0x216
12543         set_nodes_failloc "$(osts_nodes)" 0x216
12544
12545         # multiop should block due to fsync until pages are written
12546         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12547         MULTIPID=$!
12548         sleep 1
12549
12550         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12551                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12552         fi
12553
12554         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12555                     grep -c writeback)
12556         if [[ $WRITEBACK -eq 0 ]]; then
12557                 error "No page in writeback, writeback=$WRITEBACK"
12558         fi
12559
12560         set_nodes_failloc "$(osts_nodes)" 0
12561         wait $MULTIPID
12562         RC=$?
12563         if [[ $RC -ne 0 ]]; then
12564                 error "Multiop fsync failed, rc=$RC"
12565         fi
12566
12567         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12568         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12569                     grep -c writeback)
12570         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12571                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12572         fi
12573
12574         rm -f $DIR/$tfile
12575         echo "Dirty pages flushed via fsync on EROFS"
12576         return 0
12577 }
12578 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
12579
12580 # continue to use small resend count to reduce test_118* time (b=14842)
12581 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
12582
12583 test_118d()
12584 {
12585         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12586         remote_ost_nodsh && skip "remote OST with nodsh"
12587
12588         reset_async
12589
12590         #define OBD_FAIL_OST_BRW_PAUSE_BULK
12591         set_nodes_failloc "$(osts_nodes)" 0x214
12592         # multiop should block due to fsync until pages are written
12593         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12594         MULTIPID=$!
12595         sleep 1
12596
12597         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
12598                 error "Multiop failed to block on fsync, pid=$MULTIPID"
12599         fi
12600
12601         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12602                     grep -c writeback)
12603         if [[ $WRITEBACK -eq 0 ]]; then
12604                 error "No page in writeback, writeback=$WRITEBACK"
12605         fi
12606
12607         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
12608         set_nodes_failloc "$(osts_nodes)" 0
12609
12610         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12611         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12612                     grep -c writeback)
12613         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12614                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12615         fi
12616
12617         rm -f $DIR/$tfile
12618         echo "Dirty pages gaurenteed flushed via fsync"
12619         return 0
12620 }
12621 run_test 118d "Fsync validation inject a delay of the bulk =========="
12622
12623 test_118f() {
12624         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12625
12626         reset_async
12627
12628         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
12629         lctl set_param fail_loc=0x8000040a
12630
12631         # Should simulate EINVAL error which is fatal
12632         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12633         RC=$?
12634         if [[ $RC -eq 0 ]]; then
12635                 error "Must return error due to dropped pages, rc=$RC"
12636         fi
12637
12638         lctl set_param fail_loc=0x0
12639
12640         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12641         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12642         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12643                     grep -c writeback)
12644         if [[ $LOCKED -ne 0 ]]; then
12645                 error "Locked pages remain in cache, locked=$LOCKED"
12646         fi
12647
12648         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12649                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12650         fi
12651
12652         rm -f $DIR/$tfile
12653         echo "No pages locked after fsync"
12654
12655         reset_async
12656         return 0
12657 }
12658 run_test 118f "Simulate unrecoverable OSC side error =========="
12659
12660 test_118g() {
12661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12662
12663         reset_async
12664
12665         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
12666         lctl set_param fail_loc=0x406
12667
12668         # simulate local -ENOMEM
12669         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12670         RC=$?
12671
12672         lctl set_param fail_loc=0
12673         if [[ $RC -eq 0 ]]; then
12674                 error "Must return error due to dropped pages, rc=$RC"
12675         fi
12676
12677         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12678         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12679         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12680                         grep -c writeback)
12681         if [[ $LOCKED -ne 0 ]]; then
12682                 error "Locked pages remain in cache, locked=$LOCKED"
12683         fi
12684
12685         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12686                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12687         fi
12688
12689         rm -f $DIR/$tfile
12690         echo "No pages locked after fsync"
12691
12692         reset_async
12693         return 0
12694 }
12695 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
12696
12697 test_118h() {
12698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12699         remote_ost_nodsh && skip "remote OST with nodsh"
12700
12701         reset_async
12702
12703         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12704         set_nodes_failloc "$(osts_nodes)" 0x20e
12705         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12706         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12707         RC=$?
12708
12709         set_nodes_failloc "$(osts_nodes)" 0
12710         if [[ $RC -eq 0 ]]; then
12711                 error "Must return error due to dropped pages, rc=$RC"
12712         fi
12713
12714         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12715         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12716         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
12717                     grep -c writeback)
12718         if [[ $LOCKED -ne 0 ]]; then
12719                 error "Locked pages remain in cache, locked=$LOCKED"
12720         fi
12721
12722         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12723                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12724         fi
12725
12726         rm -f $DIR/$tfile
12727         echo "No pages locked after fsync"
12728
12729         return 0
12730 }
12731 run_test 118h "Verify timeout in handling recoverables errors  =========="
12732
12733 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12734
12735 test_118i() {
12736         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12737         remote_ost_nodsh && skip "remote OST with nodsh"
12738
12739         reset_async
12740
12741         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12742         set_nodes_failloc "$(osts_nodes)" 0x20e
12743
12744         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
12745         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
12746         PID=$!
12747         sleep 5
12748         set_nodes_failloc "$(osts_nodes)" 0
12749
12750         wait $PID
12751         RC=$?
12752         if [[ $RC -ne 0 ]]; then
12753                 error "got error, but should be not, rc=$RC"
12754         fi
12755
12756         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12757         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12758         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12759         if [[ $LOCKED -ne 0 ]]; then
12760                 error "Locked pages remain in cache, locked=$LOCKED"
12761         fi
12762
12763         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12764                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12765         fi
12766
12767         rm -f $DIR/$tfile
12768         echo "No pages locked after fsync"
12769
12770         return 0
12771 }
12772 run_test 118i "Fix error before timeout in recoverable error  =========="
12773
12774 [ "$SLOW" = "no" ] && set_resend_count 4
12775
12776 test_118j() {
12777         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12778         remote_ost_nodsh && skip "remote OST with nodsh"
12779
12780         reset_async
12781
12782         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
12783         set_nodes_failloc "$(osts_nodes)" 0x220
12784
12785         # return -EIO from OST
12786         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
12787         RC=$?
12788         set_nodes_failloc "$(osts_nodes)" 0x0
12789         if [[ $RC -eq 0 ]]; then
12790                 error "Must return error due to dropped pages, rc=$RC"
12791         fi
12792
12793         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
12794         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
12795         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
12796         if [[ $LOCKED -ne 0 ]]; then
12797                 error "Locked pages remain in cache, locked=$LOCKED"
12798         fi
12799
12800         # in recoverable error on OST we want resend and stay until it finished
12801         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
12802                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
12803         fi
12804
12805         rm -f $DIR/$tfile
12806         echo "No pages locked after fsync"
12807
12808         return 0
12809 }
12810 run_test 118j "Simulate unrecoverable OST side error =========="
12811
12812 test_118k()
12813 {
12814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12815         remote_ost_nodsh && skip "remote OSTs with nodsh"
12816
12817         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
12818         set_nodes_failloc "$(osts_nodes)" 0x20e
12819         test_mkdir $DIR/$tdir
12820
12821         for ((i=0;i<10;i++)); do
12822                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
12823                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
12824                 SLEEPPID=$!
12825                 sleep 0.500s
12826                 kill $SLEEPPID
12827                 wait $SLEEPPID
12828         done
12829
12830         set_nodes_failloc "$(osts_nodes)" 0
12831         rm -rf $DIR/$tdir
12832 }
12833 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
12834
12835 test_118l() # LU-646
12836 {
12837         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12838
12839         test_mkdir $DIR/$tdir
12840         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
12841         rm -rf $DIR/$tdir
12842 }
12843 run_test 118l "fsync dir"
12844
12845 test_118m() # LU-3066
12846 {
12847         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12848
12849         test_mkdir $DIR/$tdir
12850         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
12851         rm -rf $DIR/$tdir
12852 }
12853 run_test 118m "fdatasync dir ========="
12854
12855 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
12856
12857 test_118n()
12858 {
12859         local begin
12860         local end
12861
12862         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12863         remote_ost_nodsh && skip "remote OSTs with nodsh"
12864
12865         # Sleep to avoid a cached response.
12866         #define OBD_STATFS_CACHE_SECONDS 1
12867         sleep 2
12868
12869         # Inject a 10 second delay in the OST_STATFS handler.
12870         #define OBD_FAIL_OST_STATFS_DELAY 0x242
12871         set_nodes_failloc "$(osts_nodes)" 0x242
12872
12873         begin=$SECONDS
12874         stat --file-system $MOUNT > /dev/null
12875         end=$SECONDS
12876
12877         set_nodes_failloc "$(osts_nodes)" 0
12878
12879         if ((end - begin > 20)); then
12880             error "statfs took $((end - begin)) seconds, expected 10"
12881         fi
12882 }
12883 run_test 118n "statfs() sends OST_STATFS requests in parallel"
12884
12885 test_119a() # bug 11737
12886 {
12887         BSIZE=$((512 * 1024))
12888         directio write $DIR/$tfile 0 1 $BSIZE
12889         # We ask to read two blocks, which is more than a file size.
12890         # directio will indicate an error when requested and actual
12891         # sizes aren't equeal (a normal situation in this case) and
12892         # print actual read amount.
12893         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
12894         if [ "$NOB" != "$BSIZE" ]; then
12895                 error "read $NOB bytes instead of $BSIZE"
12896         fi
12897         rm -f $DIR/$tfile
12898 }
12899 run_test 119a "Short directIO read must return actual read amount"
12900
12901 test_119b() # bug 11737
12902 {
12903         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12904
12905         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
12906         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
12907         sync
12908         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
12909                 error "direct read failed"
12910         rm -f $DIR/$tfile
12911 }
12912 run_test 119b "Sparse directIO read must return actual read amount"
12913
12914 test_119c() # bug 13099
12915 {
12916         BSIZE=1048576
12917         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
12918         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
12919         rm -f $DIR/$tfile
12920 }
12921 run_test 119c "Testing for direct read hitting hole"
12922
12923 test_119d() # bug 15950
12924 {
12925         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12926
12927         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
12928         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
12929         BSIZE=1048576
12930         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
12931         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
12932         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
12933         lctl set_param fail_loc=0x40d
12934         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
12935         pid_dio=$!
12936         sleep 1
12937         cat $DIR/$tfile > /dev/null &
12938         lctl set_param fail_loc=0
12939         pid_reads=$!
12940         wait $pid_dio
12941         log "the DIO writes have completed, now wait for the reads (should not block very long)"
12942         sleep 2
12943         [ -n "`ps h -p $pid_reads -o comm`" ] && \
12944         error "the read rpcs have not completed in 2s"
12945         rm -f $DIR/$tfile
12946         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
12947 }
12948 run_test 119d "The DIO path should try to send a new rpc once one is completed"
12949
12950 test_120a() {
12951         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12952         remote_mds_nodsh && skip "remote MDS with nodsh"
12953         test_mkdir -i0 -c1 $DIR/$tdir
12954         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12955                 skip_env "no early lock cancel on server"
12956
12957         lru_resize_disable mdc
12958         lru_resize_disable osc
12959         cancel_lru_locks mdc
12960         # asynchronous object destroy at MDT could cause bl ast to client
12961         cancel_lru_locks osc
12962
12963         stat $DIR/$tdir > /dev/null
12964         can1=$(do_facet mds1 \
12965                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12966                awk '/ldlm_cancel/ {print $2}')
12967         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12968                awk '/ldlm_bl_callback/ {print $2}')
12969         test_mkdir -i0 -c1 $DIR/$tdir/d1
12970         can2=$(do_facet mds1 \
12971                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12972                awk '/ldlm_cancel/ {print $2}')
12973         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12974                awk '/ldlm_bl_callback/ {print $2}')
12975         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
12976         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
12977         lru_resize_enable mdc
12978         lru_resize_enable osc
12979 }
12980 run_test 120a "Early Lock Cancel: mkdir test"
12981
12982 test_120b() {
12983         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12984         remote_mds_nodsh && skip "remote MDS with nodsh"
12985         test_mkdir $DIR/$tdir
12986         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
12987                 skip_env "no early lock cancel on server"
12988
12989         lru_resize_disable mdc
12990         lru_resize_disable osc
12991         cancel_lru_locks mdc
12992         stat $DIR/$tdir > /dev/null
12993         can1=$(do_facet $SINGLEMDS \
12994                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
12995                awk '/ldlm_cancel/ {print $2}')
12996         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
12997                awk '/ldlm_bl_callback/ {print $2}')
12998         touch $DIR/$tdir/f1
12999         can2=$(do_facet $SINGLEMDS \
13000                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13001                awk '/ldlm_cancel/ {print $2}')
13002         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13003                awk '/ldlm_bl_callback/ {print $2}')
13004         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13005         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13006         lru_resize_enable mdc
13007         lru_resize_enable osc
13008 }
13009 run_test 120b "Early Lock Cancel: create test"
13010
13011 test_120c() {
13012         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13013         remote_mds_nodsh && skip "remote MDS with nodsh"
13014         test_mkdir -i0 -c1 $DIR/$tdir
13015         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13016                 skip "no early lock cancel on server"
13017
13018         lru_resize_disable mdc
13019         lru_resize_disable osc
13020         test_mkdir -i0 -c1 $DIR/$tdir/d1
13021         test_mkdir -i0 -c1 $DIR/$tdir/d2
13022         touch $DIR/$tdir/d1/f1
13023         cancel_lru_locks mdc
13024         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
13025         can1=$(do_facet mds1 \
13026                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13027                awk '/ldlm_cancel/ {print $2}')
13028         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13029                awk '/ldlm_bl_callback/ {print $2}')
13030         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
13031         can2=$(do_facet mds1 \
13032                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13033                awk '/ldlm_cancel/ {print $2}')
13034         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13035                awk '/ldlm_bl_callback/ {print $2}')
13036         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13037         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13038         lru_resize_enable mdc
13039         lru_resize_enable osc
13040 }
13041 run_test 120c "Early Lock Cancel: link test"
13042
13043 test_120d() {
13044         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13045         remote_mds_nodsh && skip "remote MDS with nodsh"
13046         test_mkdir -i0 -c1 $DIR/$tdir
13047         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13048                 skip_env "no early lock cancel on server"
13049
13050         lru_resize_disable mdc
13051         lru_resize_disable osc
13052         touch $DIR/$tdir
13053         cancel_lru_locks mdc
13054         stat $DIR/$tdir > /dev/null
13055         can1=$(do_facet mds1 \
13056                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13057                awk '/ldlm_cancel/ {print $2}')
13058         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13059                awk '/ldlm_bl_callback/ {print $2}')
13060         chmod a+x $DIR/$tdir
13061         can2=$(do_facet mds1 \
13062                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13063                awk '/ldlm_cancel/ {print $2}')
13064         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13065                awk '/ldlm_bl_callback/ {print $2}')
13066         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13067         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13068         lru_resize_enable mdc
13069         lru_resize_enable osc
13070 }
13071 run_test 120d "Early Lock Cancel: setattr test"
13072
13073 test_120e() {
13074         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13075         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13076                 skip_env "no early lock cancel on server"
13077         remote_mds_nodsh && skip "remote MDS with nodsh"
13078
13079         local dlmtrace_set=false
13080
13081         test_mkdir -i0 -c1 $DIR/$tdir
13082         lru_resize_disable mdc
13083         lru_resize_disable osc
13084         ! $LCTL get_param debug | grep -q dlmtrace &&
13085                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
13086         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
13087         cancel_lru_locks mdc
13088         cancel_lru_locks osc
13089         dd if=$DIR/$tdir/f1 of=/dev/null
13090         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
13091         # XXX client can not do early lock cancel of OST lock
13092         # during unlink (LU-4206), so cancel osc lock now.
13093         sleep 2
13094         cancel_lru_locks osc
13095         can1=$(do_facet mds1 \
13096                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13097                awk '/ldlm_cancel/ {print $2}')
13098         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13099                awk '/ldlm_bl_callback/ {print $2}')
13100         unlink $DIR/$tdir/f1
13101         sleep 5
13102         can2=$(do_facet mds1 \
13103                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13104                awk '/ldlm_cancel/ {print $2}')
13105         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13106                awk '/ldlm_bl_callback/ {print $2}')
13107         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
13108                 $LCTL dk $TMP/cancel.debug.txt
13109         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
13110                 $LCTL dk $TMP/blocking.debug.txt
13111         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
13112         lru_resize_enable mdc
13113         lru_resize_enable osc
13114 }
13115 run_test 120e "Early Lock Cancel: unlink test"
13116
13117 test_120f() {
13118         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13119         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13120                 skip_env "no early lock cancel on server"
13121         remote_mds_nodsh && skip "remote MDS with nodsh"
13122
13123         test_mkdir -i0 -c1 $DIR/$tdir
13124         lru_resize_disable mdc
13125         lru_resize_disable osc
13126         test_mkdir -i0 -c1 $DIR/$tdir/d1
13127         test_mkdir -i0 -c1 $DIR/$tdir/d2
13128         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
13129         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
13130         cancel_lru_locks mdc
13131         cancel_lru_locks osc
13132         dd if=$DIR/$tdir/d1/f1 of=/dev/null
13133         dd if=$DIR/$tdir/d2/f2 of=/dev/null
13134         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
13135         # XXX client can not do early lock cancel of OST lock
13136         # during rename (LU-4206), so cancel osc lock now.
13137         sleep 2
13138         cancel_lru_locks osc
13139         can1=$(do_facet mds1 \
13140                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13141                awk '/ldlm_cancel/ {print $2}')
13142         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13143                awk '/ldlm_bl_callback/ {print $2}')
13144         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
13145         sleep 5
13146         can2=$(do_facet mds1 \
13147                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13148                awk '/ldlm_cancel/ {print $2}')
13149         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13150                awk '/ldlm_bl_callback/ {print $2}')
13151         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
13152         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
13153         lru_resize_enable mdc
13154         lru_resize_enable osc
13155 }
13156 run_test 120f "Early Lock Cancel: rename test"
13157
13158 test_120g() {
13159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13160         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
13161                 skip_env "no early lock cancel on server"
13162         remote_mds_nodsh && skip "remote MDS with nodsh"
13163
13164         lru_resize_disable mdc
13165         lru_resize_disable osc
13166         count=10000
13167         echo create $count files
13168         test_mkdir $DIR/$tdir
13169         cancel_lru_locks mdc
13170         cancel_lru_locks osc
13171         t0=$(date +%s)
13172
13173         can0=$(do_facet $SINGLEMDS \
13174                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13175                awk '/ldlm_cancel/ {print $2}')
13176         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13177                awk '/ldlm_bl_callback/ {print $2}')
13178         createmany -o $DIR/$tdir/f $count
13179         sync
13180         can1=$(do_facet $SINGLEMDS \
13181                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13182                awk '/ldlm_cancel/ {print $2}')
13183         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13184                awk '/ldlm_bl_callback/ {print $2}')
13185         t1=$(date +%s)
13186         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
13187         echo rm $count files
13188         rm -r $DIR/$tdir
13189         sync
13190         can2=$(do_facet $SINGLEMDS \
13191                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
13192                awk '/ldlm_cancel/ {print $2}')
13193         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
13194                awk '/ldlm_bl_callback/ {print $2}')
13195         t2=$(date +%s)
13196         echo total: $count removes in $((t2-t1))
13197         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
13198         sleep 2
13199         # wait for commitment of removal
13200         lru_resize_enable mdc
13201         lru_resize_enable osc
13202 }
13203 run_test 120g "Early Lock Cancel: performance test"
13204
13205 test_121() { #bug #10589
13206         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13207
13208         rm -rf $DIR/$tfile
13209         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
13210 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
13211         lctl set_param fail_loc=0x310
13212         cancel_lru_locks osc > /dev/null
13213         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
13214         lctl set_param fail_loc=0
13215         [[ $reads -eq $writes ]] ||
13216                 error "read $reads blocks, must be $writes blocks"
13217 }
13218 run_test 121 "read cancel race ========="
13219
13220 test_123a_base() { # was test 123, statahead(bug 11401)
13221         local lsx="$1"
13222
13223         SLOWOK=0
13224         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
13225                 log "testing UP system. Performance may be lower than expected."
13226                 SLOWOK=1
13227         fi
13228         running_in_vm && SLOWOK=1
13229
13230         rm -rf $DIR/$tdir
13231         test_mkdir $DIR/$tdir
13232         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
13233         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
13234         MULT=10
13235         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
13236                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
13237
13238                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
13239                 lctl set_param -n llite.*.statahead_max 0
13240                 lctl get_param llite.*.statahead_max
13241                 cancel_lru_locks mdc
13242                 cancel_lru_locks osc
13243                 stime=$(date +%s)
13244                 time $lsx $DIR/$tdir | wc -l
13245                 etime=$(date +%s)
13246                 delta=$((etime - stime))
13247                 log "$lsx $i files without statahead: $delta sec"
13248                 lctl set_param llite.*.statahead_max=$max
13249
13250                 swrong=$(lctl get_param -n llite.*.statahead_stats |
13251                          awk '/statahead.wrong:/ { print $NF }')
13252                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
13253                 cancel_lru_locks mdc
13254                 cancel_lru_locks osc
13255                 stime=$(date +%s)
13256                 time $lsx $DIR/$tdir | wc -l
13257                 etime=$(date +%s)
13258                 delta_sa=$((etime - stime))
13259                 log "$lsx $i files with statahead: $delta_sa sec"
13260                 lctl get_param -n llite.*.statahead_stats
13261                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
13262                          awk '/statahead.wrong:/ { print $NF }')
13263
13264                 [[ $swrong -lt $ewrong ]] &&
13265                         log "statahead was stopped, maybe too many locks held!"
13266                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
13267
13268                 if (( $delta_sa*100 > $delta*105 && $delta_sa > $delta+2)); then
13269                         max=$(lctl get_param -n llite.*.statahead_max |
13270                                 head -n 1)
13271                         lctl set_param -n llite.*.statahead_max 0
13272                         lctl get_param llite.*.statahead_max
13273                         cancel_lru_locks mdc
13274                         cancel_lru_locks osc
13275                         stime=$(date +%s)
13276                         time $lsx $DIR/$tdir | wc -l
13277                         etime=$(date +%s)
13278                         delta=$((etime - stime))
13279                         log "$lsx $i files again without statahead: $delta sec"
13280                         lctl set_param llite.*.statahead_max=$max
13281                         if (( $delta_sa*100 > delta*105 && delta_sa > delta+2 )); then
13282                                 if [ $SLOWOK -eq 0 ]; then
13283                                         error "$lsx $i files is slower with statahead!"
13284                                 else
13285                                         log "$lsx $i files is slower with statahead!"
13286                                 fi
13287                                 break
13288                         fi
13289                 fi
13290
13291                 [ $delta -gt 20 ] && break
13292                 [ $delta -gt 8 ] && MULT=$((50 / delta))
13293                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
13294         done
13295         log "$lsx done"
13296
13297         stime=$(date +%s)
13298         rm -r $DIR/$tdir
13299         sync
13300         etime=$(date +%s)
13301         delta=$((etime - stime))
13302         log "rm -r $DIR/$tdir/: $delta seconds"
13303         log "rm done"
13304         lctl get_param -n llite.*.statahead_stats
13305 }
13306
13307 test_123aa() {
13308         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13309
13310         test_123a_base "ls -l"
13311 }
13312 run_test 123aa "verify statahead work"
13313
13314 test_123ab() {
13315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13316
13317         statx_supported || skip_env "Test must be statx() syscall supported"
13318
13319         test_123a_base "$STATX -l"
13320 }
13321 run_test 123ab "verify statahead work by using statx"
13322
13323 test_123ac() {
13324         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13325
13326         statx_supported || skip_env "Test must be statx() syscall supported"
13327
13328         local rpcs_before
13329         local rpcs_after
13330         local agl_before
13331         local agl_after
13332
13333         cancel_lru_locks $OSC
13334         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
13335         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
13336                      awk '/agl.total:/ { print $NF }')
13337         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
13338         test_123a_base "$STATX --cached=always -D"
13339         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
13340                     awk '/agl.total:/ { print $NF }')
13341         [ $agl_before -eq $agl_after ] ||
13342                 error "Should not trigger AGL thread - $agl_before:$agl_after"
13343         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
13344         [ $rpcs_after -eq $rpcs_before ] ||
13345                 error "$STATX should not send glimpse RPCs to $OSC"
13346 }
13347 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
13348
13349 test_123b () { # statahead(bug 15027)
13350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13351
13352         test_mkdir $DIR/$tdir
13353         createmany -o $DIR/$tdir/$tfile-%d 1000
13354
13355         cancel_lru_locks mdc
13356         cancel_lru_locks osc
13357
13358 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
13359         lctl set_param fail_loc=0x80000803
13360         ls -lR $DIR/$tdir > /dev/null
13361         log "ls done"
13362         lctl set_param fail_loc=0x0
13363         lctl get_param -n llite.*.statahead_stats
13364         rm -r $DIR/$tdir
13365         sync
13366
13367 }
13368 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
13369
13370 test_123c() {
13371         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
13372
13373         test_mkdir -i 0 -c 1 $DIR/$tdir.0
13374         test_mkdir -i 1 -c 1 $DIR/$tdir.1
13375         touch $DIR/$tdir.1/{1..3}
13376         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
13377
13378         remount_client $MOUNT
13379
13380         $MULTIOP $DIR/$tdir.0 Q
13381
13382         # let statahead to complete
13383         ls -l $DIR/$tdir.0 > /dev/null
13384
13385         testid=$(echo $TESTNAME | tr '_' ' ')
13386         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
13387                 error "statahead warning" || true
13388 }
13389 run_test 123c "Can not initialize inode warning on DNE statahead"
13390
13391 test_123d() {
13392         local num=100
13393         local swrong
13394         local ewrong
13395
13396         test_mkdir -c -1 $DIR/$tdir || error "test_mkdir $DIR/$tdir failed"
13397         $LFS setdirstripe -D -c $MDSCOUNT $DIR/$tdir ||
13398                 error "setdirstripe $DIR/$tdir failed"
13399         createmany -d $DIR/$tdir/$tfile $num || error "createmany $num failed"
13400         remount_client $MOUNT
13401         $LCTL get_param llite.*.statahead_max
13402         $LCTL set_param llite.*.statahead_stats=0 ||
13403                 error "clear statahead_stats failed"
13404         swrong=$(lctl get_param -n llite.*.statahead_stats |
13405                  awk '/statahead.wrong:/ { print $NF }')
13406         ls -l $DIR/$tdir || error "ls -l $DIR/$tdir failed"
13407         # wait for statahead thread finished to update hit/miss stats.
13408         sleep 1
13409         $LCTL get_param -n llite.*.statahead_stats
13410         ewrong=$(lctl get_param -n llite.*.statahead_stats |
13411                  awk '/statahead.wrong:/ { print $NF }')
13412         (( $swrong == $ewrong )) ||
13413                 log "statahead was stopped, maybe too many locks held!"
13414 }
13415 run_test 123d "Statahead on striped directories works correctly"
13416
13417 test_124a() {
13418         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13419         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13420                 skip_env "no lru resize on server"
13421
13422         local NR=2000
13423
13424         test_mkdir $DIR/$tdir
13425
13426         log "create $NR files at $DIR/$tdir"
13427         createmany -o $DIR/$tdir/f $NR ||
13428                 error "failed to create $NR files in $DIR/$tdir"
13429
13430         cancel_lru_locks mdc
13431         ls -l $DIR/$tdir > /dev/null
13432
13433         local NSDIR=""
13434         local LRU_SIZE=0
13435         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
13436                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
13437                 LRU_SIZE=$($LCTL get_param -n $PARAM)
13438                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
13439                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
13440                         log "NSDIR=$NSDIR"
13441                         log "NS=$(basename $NSDIR)"
13442                         break
13443                 fi
13444         done
13445
13446         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
13447                 skip "Not enough cached locks created!"
13448         fi
13449         log "LRU=$LRU_SIZE"
13450
13451         local SLEEP=30
13452
13453         # We know that lru resize allows one client to hold $LIMIT locks
13454         # for 10h. After that locks begin to be killed by client.
13455         local MAX_HRS=10
13456         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
13457         log "LIMIT=$LIMIT"
13458         if [ $LIMIT -lt $LRU_SIZE ]; then
13459                 skip "Limit is too small $LIMIT"
13460         fi
13461
13462         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
13463         # killing locks. Some time was spent for creating locks. This means
13464         # that up to the moment of sleep finish we must have killed some of
13465         # them (10-100 locks). This depends on how fast ther were created.
13466         # Many of them were touched in almost the same moment and thus will
13467         # be killed in groups.
13468         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE * 100))
13469
13470         # Use $LRU_SIZE_B here to take into account real number of locks
13471         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
13472         local LRU_SIZE_B=$LRU_SIZE
13473         log "LVF=$LVF"
13474         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
13475         log "OLD_LVF=$OLD_LVF"
13476         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
13477
13478         # Let's make sure that we really have some margin. Client checks
13479         # cached locks every 10 sec.
13480         SLEEP=$((SLEEP+20))
13481         log "Sleep ${SLEEP} sec"
13482         local SEC=0
13483         while ((SEC<$SLEEP)); do
13484                 echo -n "..."
13485                 sleep 5
13486                 SEC=$((SEC+5))
13487                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
13488                 echo -n "$LRU_SIZE"
13489         done
13490         echo ""
13491         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
13492         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
13493
13494         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
13495                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
13496                 unlinkmany $DIR/$tdir/f $NR
13497                 return
13498         }
13499
13500         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
13501         log "unlink $NR files at $DIR/$tdir"
13502         unlinkmany $DIR/$tdir/f $NR
13503 }
13504 run_test 124a "lru resize ======================================="
13505
13506 get_max_pool_limit()
13507 {
13508         local limit=$($LCTL get_param \
13509                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
13510         local max=0
13511         for l in $limit; do
13512                 if [[ $l -gt $max ]]; then
13513                         max=$l
13514                 fi
13515         done
13516         echo $max
13517 }
13518
13519 test_124b() {
13520         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13521         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13522                 skip_env "no lru resize on server"
13523
13524         LIMIT=$(get_max_pool_limit)
13525
13526         NR=$(($(default_lru_size)*20))
13527         if [[ $NR -gt $LIMIT ]]; then
13528                 log "Limit lock number by $LIMIT locks"
13529                 NR=$LIMIT
13530         fi
13531
13532         IFree=$(mdsrate_inodes_available)
13533         if [ $IFree -lt $NR ]; then
13534                 log "Limit lock number by $IFree inodes"
13535                 NR=$IFree
13536         fi
13537
13538         lru_resize_disable mdc
13539         test_mkdir -p $DIR/$tdir/disable_lru_resize
13540
13541         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
13542         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
13543         cancel_lru_locks mdc
13544         stime=`date +%s`
13545         PID=""
13546         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13547         PID="$PID $!"
13548         sleep 2
13549         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13550         PID="$PID $!"
13551         sleep 2
13552         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
13553         PID="$PID $!"
13554         wait $PID
13555         etime=`date +%s`
13556         nolruresize_delta=$((etime-stime))
13557         log "ls -la time: $nolruresize_delta seconds"
13558         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
13559         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
13560
13561         lru_resize_enable mdc
13562         test_mkdir -p $DIR/$tdir/enable_lru_resize
13563
13564         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
13565         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
13566         cancel_lru_locks mdc
13567         stime=`date +%s`
13568         PID=""
13569         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13570         PID="$PID $!"
13571         sleep 2
13572         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13573         PID="$PID $!"
13574         sleep 2
13575         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
13576         PID="$PID $!"
13577         wait $PID
13578         etime=`date +%s`
13579         lruresize_delta=$((etime-stime))
13580         log "ls -la time: $lruresize_delta seconds"
13581         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
13582
13583         if [ $lruresize_delta -gt $nolruresize_delta ]; then
13584                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
13585         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
13586                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
13587         else
13588                 log "lru resize performs the same with no lru resize"
13589         fi
13590         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
13591 }
13592 run_test 124b "lru resize (performance test) ======================="
13593
13594 test_124c() {
13595         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13596         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13597                 skip_env "no lru resize on server"
13598
13599         # cache ununsed locks on client
13600         local nr=100
13601         cancel_lru_locks mdc
13602         test_mkdir $DIR/$tdir
13603         createmany -o $DIR/$tdir/f $nr ||
13604                 error "failed to create $nr files in $DIR/$tdir"
13605         ls -l $DIR/$tdir > /dev/null
13606
13607         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13608         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13609         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13610         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13611         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13612
13613         # set lru_max_age to 1 sec
13614         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13615         echo "sleep $((recalc_p * 2)) seconds..."
13616         sleep $((recalc_p * 2))
13617
13618         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13619         # restore lru_max_age
13620         $LCTL set_param -n $nsdir.lru_max_age $max_age
13621         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13622         unlinkmany $DIR/$tdir/f $nr
13623 }
13624 run_test 124c "LRUR cancel very aged locks"
13625
13626 test_124d() {
13627         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13628         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
13629                 skip_env "no lru resize on server"
13630
13631         # cache ununsed locks on client
13632         local nr=100
13633
13634         lru_resize_disable mdc
13635         stack_trap "lru_resize_enable mdc" EXIT
13636
13637         cancel_lru_locks mdc
13638
13639         # asynchronous object destroy at MDT could cause bl ast to client
13640         test_mkdir $DIR/$tdir
13641         createmany -o $DIR/$tdir/f $nr ||
13642                 error "failed to create $nr files in $DIR/$tdir"
13643         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
13644
13645         ls -l $DIR/$tdir > /dev/null
13646
13647         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
13648         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
13649         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
13650         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
13651
13652         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
13653
13654         # set lru_max_age to 1 sec
13655         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
13656         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
13657
13658         echo "sleep $((recalc_p * 2)) seconds..."
13659         sleep $((recalc_p * 2))
13660
13661         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
13662
13663         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
13664 }
13665 run_test 124d "cancel very aged locks if lru-resize diasbaled"
13666
13667 test_125() { # 13358
13668         $LCTL get_param -n llite.*.client_type | grep -q local ||
13669                 skip "must run as local client"
13670         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
13671                 skip_env "must have acl enabled"
13672         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13673
13674         test_mkdir $DIR/$tdir
13675         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
13676         setfacl -R -m u:$USER0:rwx $DIR/$tdir ||
13677                 error "setfacl $DIR/$tdir failed"
13678         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
13679 }
13680 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
13681
13682 test_126() { # bug 12829/13455
13683         $GSS && skip_env "must run as gss disabled"
13684         $LCTL get_param -n llite.*.client_type | grep -q local ||
13685                 skip "must run as local client"
13686         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
13687
13688         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
13689         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
13690         rm -f $DIR/$tfile
13691         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
13692 }
13693 run_test 126 "check that the fsgid provided by the client is taken into account"
13694
13695 test_127a() { # bug 15521
13696         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13697         local name count samp unit min max sum sumsq
13698
13699         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
13700         echo "stats before reset"
13701         $LCTL get_param osc.*.stats
13702         $LCTL set_param osc.*.stats=0
13703         local fsize=$((2048 * 1024))
13704
13705         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
13706         cancel_lru_locks osc
13707         dd if=$DIR/$tfile of=/dev/null bs=$fsize
13708
13709         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
13710         stack_trap "rm -f $TMP/$tfile.tmp"
13711         while read name count samp unit min max sum sumsq; do
13712                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13713                 [ ! $min ] && error "Missing min value for $name proc entry"
13714                 eval $name=$count || error "Wrong proc format"
13715
13716                 case $name in
13717                 read_bytes|write_bytes)
13718                         [[ "$unit" =~ "bytes" ]] ||
13719                                 error "unit is not 'bytes': $unit"
13720                         (( $min >= 4096 )) || error "min is too small: $min"
13721                         (( $min <= $fsize )) || error "min is too big: $min"
13722                         (( $max >= 4096 )) || error "max is too small: $max"
13723                         (( $max <= $fsize )) || error "max is too big: $max"
13724                         (( $sum == $fsize )) || error "sum is wrong: $sum"
13725                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
13726                                 error "sumsquare is too small: $sumsq"
13727                         (( $sumsq <= $fsize * $fsize )) ||
13728                                 error "sumsquare is too big: $sumsq"
13729                         ;;
13730                 ost_read|ost_write)
13731                         [[ "$unit" =~ "usec" ]] ||
13732                                 error "unit is not 'usec': $unit"
13733                         ;;
13734                 *)      ;;
13735                 esac
13736         done < $DIR/$tfile.tmp
13737
13738         #check that we actually got some stats
13739         [ "$read_bytes" ] || error "Missing read_bytes stats"
13740         [ "$write_bytes" ] || error "Missing write_bytes stats"
13741         [ "$read_bytes" != 0 ] || error "no read done"
13742         [ "$write_bytes" != 0 ] || error "no write done"
13743 }
13744 run_test 127a "verify the client stats are sane"
13745
13746 test_127b() { # bug LU-333
13747         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13748         local name count samp unit min max sum sumsq
13749
13750         echo "stats before reset"
13751         $LCTL get_param llite.*.stats
13752         $LCTL set_param llite.*.stats=0
13753
13754         # perform 2 reads and writes so MAX is different from SUM.
13755         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13756         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
13757         cancel_lru_locks osc
13758         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13759         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
13760
13761         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
13762         stack_trap "rm -f $TMP/$tfile.tmp"
13763         while read name count samp unit min max sum sumsq; do
13764                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
13765                 eval $name=$count || error "Wrong proc format"
13766
13767                 case $name in
13768                 read_bytes|write_bytes)
13769                         [[ "$unit" =~ "bytes" ]] ||
13770                                 error "unit is not 'bytes': $unit"
13771                         (( $count == 2 )) || error "count is not 2: $count"
13772                         (( $min == $PAGE_SIZE )) ||
13773                                 error "min is not $PAGE_SIZE: $min"
13774                         (( $max == $PAGE_SIZE )) ||
13775                                 error "max is not $PAGE_SIZE: $max"
13776                         (( $sum == $PAGE_SIZE * 2 )) ||
13777                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
13778                         ;;
13779                 read|write)
13780                         [[ "$unit" =~ "usec" ]] ||
13781                                 error "unit is not 'usec': $unit"
13782                         ;;
13783                 *)      ;;
13784                 esac
13785         done < $TMP/$tfile.tmp
13786
13787         #check that we actually got some stats
13788         [ "$read_bytes" ] || error "Missing read_bytes stats"
13789         [ "$write_bytes" ] || error "Missing write_bytes stats"
13790         [ "$read_bytes" != 0 ] || error "no read done"
13791         [ "$write_bytes" != 0 ] || error "no write done"
13792 }
13793 run_test 127b "verify the llite client stats are sane"
13794
13795 test_127c() { # LU-12394
13796         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
13797         local size
13798         local bsize
13799         local reads
13800         local writes
13801         local count
13802
13803         $LCTL set_param llite.*.extents_stats=1
13804         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
13805
13806         # Use two stripes so there is enough space in default config
13807         $LFS setstripe -c 2 $DIR/$tfile
13808
13809         # Extent stats start at 0-4K and go in power of two buckets
13810         # LL_HIST_START = 12 --> 2^12 = 4K
13811         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
13812         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
13813         # small configs
13814         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
13815                 do
13816                 # Write and read, 2x each, second time at a non-zero offset
13817                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
13818                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
13819                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
13820                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
13821                 rm -f $DIR/$tfile
13822         done
13823
13824         $LCTL get_param llite.*.extents_stats
13825
13826         count=2
13827         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
13828                 do
13829                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
13830                                 grep -m 1 $bsize)
13831                 reads=$(echo $bucket | awk '{print $5}')
13832                 writes=$(echo $bucket | awk '{print $9}')
13833                 [ "$reads" -eq $count ] ||
13834                         error "$reads reads in < $bsize bucket, expect $count"
13835                 [ "$writes" -eq $count ] ||
13836                         error "$writes writes in < $bsize bucket, expect $count"
13837         done
13838
13839         # Test mmap write and read
13840         $LCTL set_param llite.*.extents_stats=c
13841         size=512
13842         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
13843         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
13844         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
13845
13846         $LCTL get_param llite.*.extents_stats
13847
13848         count=$(((size*1024) / PAGE_SIZE))
13849
13850         bsize=$((2 * PAGE_SIZE / 1024))K
13851
13852         bucket=$($LCTL get_param -n llite.*.extents_stats |
13853                         grep -m 1 $bsize)
13854         reads=$(echo $bucket | awk '{print $5}')
13855         writes=$(echo $bucket | awk '{print $9}')
13856         # mmap writes fault in the page first, creating an additonal read
13857         [ "$reads" -eq $((2 * count)) ] ||
13858                 error "$reads reads in < $bsize bucket, expect $count"
13859         [ "$writes" -eq $count ] ||
13860                 error "$writes writes in < $bsize bucket, expect $count"
13861 }
13862 run_test 127c "test llite extent stats with regular & mmap i/o"
13863
13864 test_128() { # bug 15212
13865         touch $DIR/$tfile
13866         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
13867                 find $DIR/$tfile
13868                 find $DIR/$tfile
13869         EOF
13870
13871         result=$(grep error $TMP/$tfile.log)
13872         rm -f $DIR/$tfile $TMP/$tfile.log
13873         [ -z "$result" ] ||
13874                 error "consecutive find's under interactive lfs failed"
13875 }
13876 run_test 128 "interactive lfs for 2 consecutive find's"
13877
13878 set_dir_limits () {
13879         local mntdev
13880         local canondev
13881         local node
13882
13883         local ldproc=/proc/fs/ldiskfs
13884         local facets=$(get_facets MDS)
13885
13886         for facet in ${facets//,/ }; do
13887                 canondev=$(ldiskfs_canon \
13888                            *.$(convert_facet2label $facet).mntdev $facet)
13889                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
13890                         ldproc=/sys/fs/ldiskfs
13891                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
13892                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
13893         done
13894 }
13895
13896 check_mds_dmesg() {
13897         local facets=$(get_facets MDS)
13898         for facet in ${facets//,/ }; do
13899                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
13900         done
13901         return 1
13902 }
13903
13904 test_129() {
13905         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13906         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
13907                 skip "Need MDS version with at least 2.5.56"
13908         if [ "$mds1_FSTYPE" != ldiskfs ]; then
13909                 skip_env "ldiskfs only test"
13910         fi
13911         remote_mds_nodsh && skip "remote MDS with nodsh"
13912
13913         local ENOSPC=28
13914         local has_warning=false
13915
13916         rm -rf $DIR/$tdir
13917         mkdir -p $DIR/$tdir
13918
13919         # block size of mds1
13920         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
13921         set_dir_limits $maxsize $((maxsize * 6 / 8))
13922         stack_trap "set_dir_limits 0 0"
13923         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
13924         local dirsize=$(stat -c%s "$DIR/$tdir")
13925         local nfiles=0
13926         while (( $dirsize <= $maxsize )); do
13927                 $MCREATE $DIR/$tdir/file_base_$nfiles
13928                 rc=$?
13929                 # check two errors:
13930                 # ENOSPC for ext4 max_dir_size, which has been used since
13931                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
13932                 if (( rc == ENOSPC )); then
13933                         set_dir_limits 0 0
13934                         echo "rc=$rc returned as expected after $nfiles files"
13935
13936                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
13937                                 error "create failed w/o dir size limit"
13938
13939                         # messages may be rate limited if test is run repeatedly
13940                         check_mds_dmesg '"is approaching max"' ||
13941                                 echo "warning message should be output"
13942                         check_mds_dmesg '"has reached max"' ||
13943                                 echo "reached message should be output"
13944
13945                         dirsize=$(stat -c%s "$DIR/$tdir")
13946
13947                         [[ $dirsize -ge $maxsize ]] && return 0
13948                         error "dirsize $dirsize < $maxsize after $nfiles files"
13949                 elif (( rc != 0 )); then
13950                         break
13951                 fi
13952                 nfiles=$((nfiles + 1))
13953                 dirsize=$(stat -c%s "$DIR/$tdir")
13954         done
13955
13956         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
13957 }
13958 run_test 129 "test directory size limit ========================"
13959
13960 OLDIFS="$IFS"
13961 cleanup_130() {
13962         trap 0
13963         IFS="$OLDIFS"
13964 }
13965
13966 test_130a() {
13967         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
13968         [[ -z "$filefrag_op" ]] || skip_env "filefrag does not support FIEMAP"
13969
13970         trap cleanup_130 EXIT RETURN
13971
13972         local fm_file=$DIR/$tfile
13973         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
13974         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
13975                 error "dd failed for $fm_file"
13976
13977         # LU-1795: test filefrag/FIEMAP once, even if unsupported on ZFS
13978         filefrag -ves $fm_file
13979         local rc=$?
13980         [[ "$ost1_FSTYPE" != "zfs" ]] ||
13981                 skip "LU-1941: FIEMAP unimplemented on ZFS"
13982         (( $rc == 0 )) || error "filefrag $fm_file failed"
13983
13984         filefrag_op=$(filefrag -ve -k $fm_file |
13985                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
13986         local lun=$($LFS getstripe -i $fm_file)
13987
13988         local start_blk=$(echo $filefrag_op | cut -d: -f2 | cut -d. -f1)
13989         IFS=$'\n'
13990         local tot_len=0
13991         for line in $filefrag_op; do
13992                 local frag_lun=$(echo $line | cut -d: -f5)
13993                 local ext_len=$(echo $line | cut -d: -f4)
13994
13995                 if (( $frag_lun != $lun )); then
13996                         error "FIEMAP on 1-stripe file($fm_file) failed"
13997                         return
13998                 fi
13999                 (( tot_len += ext_len ))
14000         done
14001
14002         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
14003                 error "FIEMAP on 1-stripe file($fm_file) failed"
14004                 return
14005         fi
14006
14007         echo "FIEMAP on single striped file succeeded"
14008 }
14009 run_test 130a "FIEMAP (1-stripe file)"
14010
14011 test_130b() {
14012         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14013
14014         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14015         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14016         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14017                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14018
14019         trap cleanup_130 EXIT RETURN
14020
14021         local fm_file=$DIR/$tfile
14022         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
14023                 error "setstripe on $fm_file"
14024
14025         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
14026                 error "dd failed on $fm_file"
14027
14028         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14029         filefrag_op=$(filefrag -ve -k $fm_file |
14030                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14031
14032         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14033                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14034
14035         IFS=$'\n'
14036         local tot_len=0
14037         local num_luns=1
14038
14039         for line in $filefrag_op; do
14040                 local frag_lun=$(echo $line | cut -d: -f5 |
14041                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14042                 local ext_len=$(echo $line | cut -d: -f4)
14043                 if (( $frag_lun != $last_lun )); then
14044                         if (( tot_len != 1024 )); then
14045                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14046                                 return
14047                         else
14048                                 (( num_luns += 1 ))
14049                                 tot_len=0
14050                         fi
14051                 fi
14052                 (( tot_len += ext_len ))
14053                 last_lun=$frag_lun
14054         done
14055         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
14056                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14057                 return
14058         fi
14059
14060         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
14061 }
14062 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
14063
14064 test_130c() {
14065         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14066
14067         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14068         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14069         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14070                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14071
14072         trap cleanup_130 EXIT RETURN
14073
14074         local fm_file=$DIR/$tfile
14075         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
14076
14077         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
14078                 error "dd failed on $fm_file"
14079
14080         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14081         filefrag_op=$(filefrag -ve -k $fm_file |
14082                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14083
14084         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14085                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14086
14087         IFS=$'\n'
14088         local tot_len=0
14089         local num_luns=1
14090         for line in $filefrag_op; do
14091                 local frag_lun=$(echo $line | cut -d: -f5 |
14092                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14093                 local ext_len=$(echo $line | cut -d: -f4)
14094                 if (( $frag_lun != $last_lun )); then
14095                         local logical=$(echo $line | cut -d: -f2 | cut -d. -f1)
14096                         if (( logical != 512 )); then
14097                                 error "FIEMAP on $fm_file failed; returned logical start for lun $logical instead of 512"
14098                                 return
14099                         fi
14100                         if (( tot_len != 512 )); then
14101                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14102                                 return
14103                         else
14104                                 (( num_luns += 1 ))
14105                                 tot_len=0
14106                         fi
14107                 fi
14108                 (( tot_len += ext_len ))
14109                 last_lun=$frag_lun
14110         done
14111         if (( num_luns != 2 || tot_len != 512 )); then
14112                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14113                 return
14114         fi
14115
14116         echo "FIEMAP on 2-stripe file with hole succeeded"
14117 }
14118 run_test 130c "FIEMAP (2-stripe file with hole)"
14119
14120 test_130d() {
14121         (( $OSTCOUNT >= 3 )) || skip "needs >= 3 OSTs"
14122
14123         filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14124         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14125         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14126                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14127
14128         trap cleanup_130 EXIT RETURN
14129
14130         local fm_file=$DIR/$tfile
14131         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
14132                         error "setstripe on $fm_file"
14133
14134         local actual_stripe_count=$($LFS getstripe -c $fm_file)
14135         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
14136                 error "dd failed on $fm_file"
14137
14138         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14139         filefrag_op=$(filefrag -ve -k $fm_file |
14140                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14141
14142         local last_lun=$(echo $filefrag_op | cut -d: -f5 |
14143                          sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14144
14145         IFS=$'\n'
14146         local tot_len=0
14147         local num_luns=1
14148         for line in $filefrag_op; do
14149                 local frag_lun=$(echo $line | cut -d: -f5 |
14150                                  sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
14151                 local ext_len=$(echo $line | cut -d: -f4)
14152                 if (( $frag_lun != $last_lun )); then
14153                         if (( tot_len != 1024 )); then
14154                                 error "FIEMAP on $fm_file failed; returned len $tot_len for OST $last_lun instead of 1024"
14155                                 return
14156                         else
14157                                 (( num_luns += 1 ))
14158                                 local tot_len=0
14159                         fi
14160                 fi
14161                 (( tot_len += ext_len ))
14162                 last_lun=$frag_lun
14163         done
14164         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
14165                 error "FIEMAP on $fm_file failed; returned wrong number of luns or wrong len for OST $last_lun"
14166                 return
14167         fi
14168
14169         echo "FIEMAP on N-stripe file succeeded"
14170 }
14171 run_test 130d "FIEMAP (N-stripe file)"
14172
14173 test_130e() {
14174         (( $OSTCOUNT >= 2 )) || skip "needs >= 2 OSTs"
14175
14176         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14177         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14178         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14179                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14180
14181         trap cleanup_130 EXIT RETURN
14182
14183         local fm_file=$DIR/$tfile
14184         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
14185
14186         local num_blks=512
14187         local expected_len=$(( (num_blks / 2) * 64 ))
14188         for ((i = 0; i < $num_blks; i++)); do
14189                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) \
14190                         conv=notrunc > /dev/null 2>&1
14191         done
14192
14193         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14194         filefrag_op=$(filefrag -ve -k $fm_file |
14195                       sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
14196
14197         local last_lun=$(echo $filefrag_op | cut -d: -f5)
14198
14199         IFS=$'\n'
14200         local tot_len=0
14201         local num_luns=1
14202         for line in $filefrag_op; do
14203                 local frag_lun=$(echo $line | cut -d: -f5)
14204                 local ext_len=$(echo $line | cut -d: -f4)
14205                 if (( $frag_lun != $last_lun )); then
14206                         if (( tot_len != $expected_len )); then
14207                                 error "OST$last_lun $tot_len != $expected_len"
14208                         else
14209                                 (( num_luns += 1 ))
14210                                 tot_len=0
14211                         fi
14212                 fi
14213                 (( tot_len += ext_len ))
14214                 last_lun=$frag_lun
14215         done
14216         if (( num_luns != 2 || tot_len != $expected_len )); then
14217                 error "OST$last_lun $num_luns != 2, $tot_len != $expected_len"
14218         fi
14219
14220         echo "FIEMAP with continuation calls succeeded"
14221 }
14222 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
14223
14224 test_130f() {
14225         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14226         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14227         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14228                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14229
14230         local fm_file=$DIR/$tfile
14231         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
14232                 error "multiop create with lov_delay_create on $fm_file"
14233
14234         filefrag -ves $fm_file || error "filefrag $fm_file failed"
14235         filefrag_extents=$(filefrag -vek $fm_file |
14236                            awk '/extents? found/ { print $2 }')
14237         if (( $filefrag_extents != 0 )); then
14238                 error "$fm_file: filefrag_extents=$filefrag_extents != 0"
14239         fi
14240
14241         rm -f $fm_file
14242 }
14243 run_test 130f "FIEMAP (unstriped file)"
14244
14245 test_130g() {
14246         (( $MDS1_VERSION >= $(version_code 2.12.53) )) ||
14247                 skip "Need MDS version with at least 2.12.53 for overstriping"
14248         local filefrag_op=$(filefrag -l 2>&1 | grep "invalid option")
14249         [[ -z "$filefrag_op" ]] || skip_env "filefrag missing logical ordering"
14250         [[ "$ost1_FSTYPE" != "zfs" ]] ||
14251                 skip "LU-1941: FIEMAP unimplemented on ZFS"
14252
14253         local file=$DIR/$tfile
14254         local nr=$((OSTCOUNT * 100))
14255
14256         $LFS setstripe -C $nr $file || error "failed to setstripe -C $nr $file"
14257
14258         stack_trap "rm -f $file"
14259         dd if=/dev/zero of=$file count=$nr bs=1M
14260         sync
14261         nr=$($LFS getstripe -c $file)
14262
14263         local extents=$(filefrag -v $file |
14264                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}' | wc -l)
14265
14266         echo "filefrag list $extents extents in file with stripecount $nr"
14267         if (( extents < nr )); then
14268                 $LFS getstripe $file
14269                 filefrag -v $file
14270                 error "filefrag printed $extents < $nr extents"
14271         fi
14272 }
14273 run_test 130g "FIEMAP (overstripe file)"
14274
14275 # Test for writev/readv
14276 test_131a() {
14277         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
14278                 error "writev test failed"
14279         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
14280                 error "readv failed"
14281         rm -f $DIR/$tfile
14282 }
14283 run_test 131a "test iov's crossing stripe boundary for writev/readv"
14284
14285 test_131b() {
14286         local fsize=$((524288 + 1048576 + 1572864))
14287         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
14288                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
14289                         error "append writev test failed"
14290
14291         ((fsize += 1572864 + 1048576))
14292         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
14293                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
14294                         error "append writev test failed"
14295         rm -f $DIR/$tfile
14296 }
14297 run_test 131b "test append writev"
14298
14299 test_131c() {
14300         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
14301         error "NOT PASS"
14302 }
14303 run_test 131c "test read/write on file w/o objects"
14304
14305 test_131d() {
14306         rwv -f $DIR/$tfile -w -n 1 1572864
14307         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
14308         if [ "$NOB" != 1572864 ]; then
14309                 error "Short read filed: read $NOB bytes instead of 1572864"
14310         fi
14311         rm -f $DIR/$tfile
14312 }
14313 run_test 131d "test short read"
14314
14315 test_131e() {
14316         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
14317         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
14318         error "read hitting hole failed"
14319         rm -f $DIR/$tfile
14320 }
14321 run_test 131e "test read hitting hole"
14322
14323 check_stats() {
14324         local facet=$1
14325         local op=$2
14326         local want=${3:-0}
14327         local res
14328
14329         # open             11 samples [usecs] 468 4793 13658 35791898
14330         case $facet in
14331         mds*) res=($(do_facet $facet \
14332                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op"))
14333                  ;;
14334         ost*) res=($(do_facet $facet \
14335                   $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op"))
14336                  ;;
14337         *) error "Wrong facet '$facet'" ;;
14338         esac
14339         [[ -n "$res" ]] || error "counter for $op on $facet not incremented"
14340         # if $want is zero, it means any stat increment is ok.
14341         if (( $want > 0 )); then
14342                 local count=${res[1]}
14343
14344                 if (( $count != $want )); then
14345                         if [[ $facet =~ "mds" ]]; then
14346                                 do_nodes $(comma_list $(mdts_nodes)) \
14347                                         $LCTL get_param mdt.*.md_stats
14348                         else
14349                                 do_nodes $(comma_list $(osts-nodes)) \
14350                                         $LCTL get_param obdfilter.*.stats
14351                         fi
14352                         error "The $op counter on $facet is $count, not $want"
14353                 fi
14354         fi
14355 }
14356
14357 test_133a() {
14358         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14359         remote_ost_nodsh && skip "remote OST with nodsh"
14360         remote_mds_nodsh && skip "remote MDS with nodsh"
14361         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
14362                 skip_env "MDS doesn't support rename stats"
14363
14364         local testdir=$DIR/${tdir}/stats_testdir
14365
14366         mkdir -p $DIR/${tdir}
14367
14368         # clear stats.
14369         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14370         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14371
14372         # verify mdt stats first.
14373         mkdir_on_mdt0 ${testdir} || error "mkdir_on_mdt0 failed"
14374         check_stats $SINGLEMDS "mkdir" 1
14375
14376         # clear "open" from "lfs mkdir" above
14377         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14378         touch ${testdir}/${tfile} || error "touch failed"
14379         check_stats $SINGLEMDS "open" 1
14380         check_stats $SINGLEMDS "close" 1
14381         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
14382                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
14383                 check_stats $SINGLEMDS "mknod" 2
14384         }
14385         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
14386         check_stats $SINGLEMDS "unlink" 1
14387         rm -f ${testdir}/${tfile} || error "file remove failed"
14388         check_stats $SINGLEMDS "unlink" 2
14389
14390         # remove working dir and check mdt stats again.
14391         rmdir ${testdir} || error "rmdir failed"
14392         check_stats $SINGLEMDS "rmdir" 1
14393
14394         local testdir1=$DIR/${tdir}/stats_testdir1
14395         mkdir -p ${testdir}
14396         mkdir -p ${testdir1}
14397         touch ${testdir1}/test1
14398         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
14399         check_stats $SINGLEMDS "crossdir_rename" 1
14400
14401         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
14402         check_stats $SINGLEMDS "samedir_rename" 1
14403
14404         rm -rf $DIR/${tdir}
14405 }
14406 run_test 133a "Verifying MDT stats ========================================"
14407
14408 test_133b() {
14409         local res
14410
14411         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14412         remote_ost_nodsh && skip "remote OST with nodsh"
14413         remote_mds_nodsh && skip "remote MDS with nodsh"
14414
14415         local testdir=$DIR/${tdir}/stats_testdir
14416
14417         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
14418         mkdir_on_mdt0 ${testdir} || error "mkdir_on_mdt0 failed"
14419         touch ${testdir}/${tfile} || error "touch failed"
14420         cancel_lru_locks mdc
14421
14422         # clear stats.
14423         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14424         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14425
14426         # extra mdt stats verification.
14427         chmod 444 ${testdir}/${tfile} || error "chmod failed"
14428         check_stats $SINGLEMDS "setattr" 1
14429         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14430         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
14431         then            # LU-1740
14432                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
14433                 check_stats $SINGLEMDS "getattr" 1
14434         fi
14435         rm -rf $DIR/${tdir}
14436
14437         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
14438         # so the check below is not reliable
14439         [ $MDSCOUNT -eq 1 ] || return 0
14440
14441         # Sleep to avoid a cached response.
14442         #define OBD_STATFS_CACHE_SECONDS 1
14443         sleep 2
14444         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14445         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
14446         $LFS df || error "lfs failed"
14447         check_stats $SINGLEMDS "statfs" 1
14448
14449         # check aggregated statfs (LU-10018)
14450         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
14451                 return 0
14452         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
14453                 return 0
14454         sleep 2
14455         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14456         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
14457         df $DIR
14458         check_stats $SINGLEMDS "statfs" 1
14459
14460         # We want to check that the client didn't send OST_STATFS to
14461         # ost1 but the MDT also uses OST_STATFS for precreate. So some
14462         # extra care is needed here.
14463         if remote_mds; then
14464                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
14465                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
14466
14467                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
14468                 [ "$res" ] && error "OST got STATFS"
14469         fi
14470
14471         return 0
14472 }
14473 run_test 133b "Verifying extra MDT stats =================================="
14474
14475 test_133c() {
14476         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14477         remote_ost_nodsh && skip "remote OST with nodsh"
14478         remote_mds_nodsh && skip "remote MDS with nodsh"
14479
14480         local testdir=$DIR/$tdir/stats_testdir
14481
14482         test_mkdir -p $testdir
14483
14484         # verify obdfilter stats.
14485         $LFS setstripe -c 1 -i 0 $testdir/$tfile
14486         sync
14487         cancel_lru_locks osc
14488         wait_delete_completed
14489
14490         # clear stats.
14491         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
14492         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
14493
14494         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
14495                 error "dd failed"
14496         sync
14497         cancel_lru_locks osc
14498         check_stats ost1 "write" 1
14499
14500         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
14501         check_stats ost1 "read" 1
14502
14503         > $testdir/$tfile || error "truncate failed"
14504         check_stats ost1 "punch" 1
14505
14506         rm -f $testdir/$tfile || error "file remove failed"
14507         wait_delete_completed
14508         check_stats ost1 "destroy" 1
14509
14510         rm -rf $DIR/$tdir
14511 }
14512 run_test 133c "Verifying OST stats ========================================"
14513
14514 order_2() {
14515         local value=$1
14516         local orig=$value
14517         local order=1
14518
14519         while [ $value -ge 2 ]; do
14520                 order=$((order*2))
14521                 value=$((value/2))
14522         done
14523
14524         if [ $orig -gt $order ]; then
14525                 order=$((order*2))
14526         fi
14527         echo $order
14528 }
14529
14530 size_in_KMGT() {
14531     local value=$1
14532     local size=('K' 'M' 'G' 'T');
14533     local i=0
14534     local size_string=$value
14535
14536     while [ $value -ge 1024 ]; do
14537         if [ $i -gt 3 ]; then
14538             #T is the biggest unit we get here, if that is bigger,
14539             #just return XXXT
14540             size_string=${value}T
14541             break
14542         fi
14543         value=$((value >> 10))
14544         if [ $value -lt 1024 ]; then
14545             size_string=${value}${size[$i]}
14546             break
14547         fi
14548         i=$((i + 1))
14549     done
14550
14551     echo $size_string
14552 }
14553
14554 get_rename_size() {
14555         local size=$1
14556         local context=${2:-.}
14557         local sample=$(do_facet $SINGLEMDS $LCTL \
14558                 get_param mdt.$FSNAME-MDT0000.rename_stats |
14559                 grep -A1 $context |
14560                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
14561         echo $sample
14562 }
14563
14564 test_133d() {
14565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14566         remote_ost_nodsh && skip "remote OST with nodsh"
14567         remote_mds_nodsh && skip "remote MDS with nodsh"
14568         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
14569                 skip_env "MDS doesn't support rename stats"
14570
14571         local testdir1=$DIR/${tdir}/stats_testdir1
14572         local testdir2=$DIR/${tdir}/stats_testdir2
14573         mkdir -p $DIR/${tdir} || error "mkdir $tdir failed"
14574
14575         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14576
14577         mkdir_on_mdt0 ${testdir1} || error "mkdir $testdir1 failed"
14578         mkdir_on_mdt0 ${testdir2} || error "mkdir $testdir2 failed"
14579
14580         createmany -o $testdir1/test 512 || error "createmany failed"
14581
14582         # check samedir rename size
14583         mv ${testdir1}/test0 ${testdir1}/test_0
14584
14585         local testdir1_size=$(ls -l $DIR/${tdir} |
14586                 awk '/stats_testdir1/ {print $5}')
14587         local testdir2_size=$(ls -l $DIR/${tdir} |
14588                 awk '/stats_testdir2/ {print $5}')
14589
14590         testdir1_size=$(order_2 $testdir1_size)
14591         testdir2_size=$(order_2 $testdir2_size)
14592
14593         testdir1_size=$(size_in_KMGT $testdir1_size)
14594         testdir2_size=$(size_in_KMGT $testdir2_size)
14595
14596         echo "source rename dir size: ${testdir1_size}"
14597         echo "target rename dir size: ${testdir2_size}"
14598
14599         local cmd="do_facet $SINGLEMDS $LCTL "
14600         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
14601
14602         eval $cmd || error "$cmd failed"
14603         local samedir=$($cmd | grep 'same_dir')
14604         local same_sample=$(get_rename_size $testdir1_size)
14605         [ -z "$samedir" ] && error "samedir_rename_size count error"
14606         [[ $same_sample -eq 1 ]] ||
14607                 error "samedir_rename_size error $same_sample"
14608         echo "Check same dir rename stats success"
14609
14610         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
14611
14612         # check crossdir rename size
14613         mv ${testdir1}/test_0 ${testdir2}/test_0
14614
14615         testdir1_size=$(ls -l $DIR/${tdir} |
14616                 awk '/stats_testdir1/ {print $5}')
14617         testdir2_size=$(ls -l $DIR/${tdir} |
14618                 awk '/stats_testdir2/ {print $5}')
14619
14620         testdir1_size=$(order_2 $testdir1_size)
14621         testdir2_size=$(order_2 $testdir2_size)
14622
14623         testdir1_size=$(size_in_KMGT $testdir1_size)
14624         testdir2_size=$(size_in_KMGT $testdir2_size)
14625
14626         echo "source rename dir size: ${testdir1_size}"
14627         echo "target rename dir size: ${testdir2_size}"
14628
14629         eval $cmd || error "$cmd failed"
14630         local crossdir=$($cmd | grep 'crossdir')
14631         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
14632         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
14633         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
14634         [[ $src_sample -eq 1 ]] ||
14635                 error "crossdir_rename_size error $src_sample"
14636         [[ $tgt_sample -eq 1 ]] ||
14637                 error "crossdir_rename_size error $tgt_sample"
14638         echo "Check cross dir rename stats success"
14639         rm -rf $DIR/${tdir}
14640 }
14641 run_test 133d "Verifying rename_stats ========================================"
14642
14643 test_133e() {
14644         remote_mds_nodsh && skip "remote MDS with nodsh"
14645         remote_ost_nodsh && skip "remote OST with nodsh"
14646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14647
14648         local testdir=$DIR/${tdir}/stats_testdir
14649         local ctr f0 f1 bs=32768 count=42 sum
14650
14651         mkdir -p ${testdir} || error "mkdir failed"
14652
14653         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
14654
14655         for ctr in {write,read}_bytes; do
14656                 sync
14657                 cancel_lru_locks osc
14658
14659                 do_facet ost1 $LCTL set_param -n \
14660                         "obdfilter.*.exports.clear=clear"
14661
14662                 if [ $ctr = write_bytes ]; then
14663                         f0=/dev/zero
14664                         f1=${testdir}/${tfile}
14665                 else
14666                         f0=${testdir}/${tfile}
14667                         f1=/dev/null
14668                 fi
14669
14670                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
14671                         error "dd failed"
14672                 sync
14673                 cancel_lru_locks osc
14674
14675                 sum=$(do_facet ost1 $LCTL get_param \
14676                         "obdfilter.*.exports.*.stats" |
14677                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
14678                                 $1 == ctr { sum += $7 }
14679                                 END { printf("%0.0f", sum) }')
14680
14681                 if ((sum != bs * count)); then
14682                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
14683                 fi
14684         done
14685
14686         rm -rf $DIR/${tdir}
14687 }
14688 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
14689
14690 test_133f() {
14691         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
14692                 skip "too old lustre for get_param -R ($facet_ver)"
14693
14694         # verifying readability.
14695         $LCTL get_param -R '*' &> /dev/null
14696
14697         # Verifing writability with badarea_io.
14698         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14699         local skipped_params='force_lbug|changelog_mask|daemon_file'
14700         $LCTL list_param -FR '*' | grep '=' | tr -d = |
14701                 egrep -v "$skipped_params" |
14702                 xargs -n 1 find $proc_dirs -name |
14703                 xargs -n 1 badarea_io ||
14704                 error "client badarea_io failed"
14705
14706         # remount the FS in case writes/reads /proc break the FS
14707         cleanup || error "failed to unmount"
14708         setup || error "failed to setup"
14709 }
14710 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
14711
14712 test_133g() {
14713         remote_mds_nodsh && skip "remote MDS with nodsh"
14714         remote_ost_nodsh && skip "remote OST with nodsh"
14715
14716         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
14717         local skipped_params="'force_lbug|changelog_mask|daemon_file'"
14718         local facet
14719         for facet in mds1 ost1; do
14720                 local facet_ver=$(lustre_version_code $facet)
14721                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
14722                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
14723                 else
14724                         log "$facet: too old lustre for get_param -R"
14725                 fi
14726                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
14727                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
14728                                 tr -d = | egrep -v $skipped_params |
14729                                 xargs -n 1 find $proc_dirs -name |
14730                                 xargs -n 1 badarea_io" ||
14731                                         error "$facet badarea_io failed"
14732                 else
14733                         skip_noexit "$facet: too old lustre for get_param -R"
14734                 fi
14735         done
14736
14737         # remount the FS in case writes/reads /proc break the FS
14738         cleanup || error "failed to unmount"
14739         setup || error "failed to setup"
14740 }
14741 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
14742
14743 test_133h() {
14744         remote_mds_nodsh && skip "remote MDS with nodsh"
14745         remote_ost_nodsh && skip "remote OST with nodsh"
14746         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
14747                 skip "Need MDS version at least 2.9.54"
14748
14749         local facet
14750         for facet in client mds1 ost1; do
14751                 # Get the list of files that are missing the terminating newline
14752                 local plist=$(do_facet $facet
14753                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
14754                 local ent
14755                 for ent in $plist; do
14756                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
14757                                 awk -v FS='\v' -v RS='\v\v' \
14758                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
14759                                         print FILENAME}'" 2>/dev/null)
14760                         [ -z $missing ] || {
14761                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
14762                                 error "file does not end with newline: $facet-$ent"
14763                         }
14764                 done
14765         done
14766 }
14767 run_test 133h "Proc files should end with newlines"
14768
14769 test_134a() {
14770         remote_mds_nodsh && skip "remote MDS with nodsh"
14771         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14772                 skip "Need MDS version at least 2.7.54"
14773
14774         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14775         cancel_lru_locks mdc
14776
14777         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
14778         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14779         [ $unused -eq 0 ] || error "$unused locks are not cleared"
14780
14781         local nr=1000
14782         createmany -o $DIR/$tdir/f $nr ||
14783                 error "failed to create $nr files in $DIR/$tdir"
14784         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
14785
14786         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
14787         do_facet mds1 $LCTL set_param fail_loc=0x327
14788         do_facet mds1 $LCTL set_param fail_val=500
14789         touch $DIR/$tdir/m
14790
14791         echo "sleep 10 seconds ..."
14792         sleep 10
14793         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
14794
14795         do_facet mds1 $LCTL set_param fail_loc=0
14796         do_facet mds1 $LCTL set_param fail_val=0
14797         [ $lck_cnt -lt $unused ] ||
14798                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
14799
14800         rm $DIR/$tdir/m
14801         unlinkmany $DIR/$tdir/f $nr
14802 }
14803 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
14804
14805 test_134b() {
14806         remote_mds_nodsh && skip "remote MDS with nodsh"
14807         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
14808                 skip "Need MDS version at least 2.7.54"
14809
14810         mkdir_on_mdt0 $DIR/$tdir || error "failed to create $DIR/$tdir"
14811         cancel_lru_locks mdc
14812
14813         local low_wm=$(do_facet mds1 $LCTL get_param -n \
14814                         ldlm.lock_reclaim_threshold_mb)
14815         # disable reclaim temporarily
14816         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
14817
14818         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
14819         do_facet mds1 $LCTL set_param fail_loc=0x328
14820         do_facet mds1 $LCTL set_param fail_val=500
14821
14822         $LCTL set_param debug=+trace
14823
14824         local nr=600
14825         createmany -o $DIR/$tdir/f $nr &
14826         local create_pid=$!
14827
14828         echo "Sleep $TIMEOUT seconds ..."
14829         sleep $TIMEOUT
14830         if ! ps -p $create_pid  > /dev/null 2>&1; then
14831                 do_facet mds1 $LCTL set_param fail_loc=0
14832                 do_facet mds1 $LCTL set_param fail_val=0
14833                 do_facet mds1 $LCTL set_param \
14834                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
14835                 error "createmany finished incorrectly!"
14836         fi
14837         do_facet mds1 $LCTL set_param fail_loc=0
14838         do_facet mds1 $LCTL set_param fail_val=0
14839         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
14840         wait $create_pid || return 1
14841
14842         unlinkmany $DIR/$tdir/f $nr
14843 }
14844 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
14845
14846 test_135() {
14847         remote_mds_nodsh && skip "remote MDS with nodsh"
14848         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14849                 skip "Need MDS version at least 2.13.50"
14850         local fname
14851
14852         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14853
14854 #define OBD_FAIL_PLAIN_RECORDS 0x1319
14855         #set only one record at plain llog
14856         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
14857
14858         #fill already existed plain llog each 64767
14859         #wrapping whole catalog
14860         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14861
14862         createmany -o $DIR/$tdir/$tfile_ 64700
14863         for (( i = 0; i < 64700; i = i + 2 ))
14864         do
14865                 rm $DIR/$tdir/$tfile_$i &
14866                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14867                 local pid=$!
14868                 wait $pid
14869         done
14870
14871         #waiting osp synchronization
14872         wait_delete_completed
14873 }
14874 run_test 135 "Race catalog processing"
14875
14876 test_136() {
14877         remote_mds_nodsh && skip "remote MDS with nodsh"
14878         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
14879                 skip "Need MDS version at least 2.13.50"
14880         local fname
14881
14882         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
14883         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
14884         #set only one record at plain llog
14885 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
14886         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
14887
14888         #fill already existed 2 plain llogs each 64767
14889         #wrapping whole catalog
14890         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
14891         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
14892         wait_delete_completed
14893
14894         createmany -o $DIR/$tdir/$tfile_ 10
14895         sleep 25
14896
14897         do_facet $SINGLEMDS $LCTL set_param fail_val=3
14898         for (( i = 0; i < 10; i = i + 3 ))
14899         do
14900                 rm $DIR/$tdir/$tfile_$i &
14901                 rm $DIR/$tdir/$tfile_$((i + 1)) &
14902                 local pid=$!
14903                 wait $pid
14904                 sleep 7
14905                 rm $DIR/$tdir/$tfile_$((i + 2)) &
14906         done
14907
14908         #waiting osp synchronization
14909         wait_delete_completed
14910 }
14911 run_test 136 "Race catalog processing 2"
14912
14913 test_140() { #bug-17379
14914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14915
14916         test_mkdir $DIR/$tdir
14917         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
14918         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
14919
14920         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
14921         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
14922         local i=0
14923         while i=$((i + 1)); do
14924                 test_mkdir $i
14925                 cd $i || error "Changing to $i"
14926                 ln -s ../stat stat || error "Creating stat symlink"
14927                 # Read the symlink until ELOOP present,
14928                 # not LBUGing the system is considered success,
14929                 # we didn't overrun the stack.
14930                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
14931                 if [ $ret -ne 0 ]; then
14932                         if [ $ret -eq 40 ]; then
14933                                 break  # -ELOOP
14934                         else
14935                                 error "Open stat symlink"
14936                                         return
14937                         fi
14938                 fi
14939         done
14940         i=$((i - 1))
14941         echo "The symlink depth = $i"
14942         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
14943                 error "Invalid symlink depth"
14944
14945         # Test recursive symlink
14946         ln -s symlink_self symlink_self
14947         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
14948         echo "open symlink_self returns $ret"
14949         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
14950 }
14951 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
14952
14953 test_150a() {
14954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14955
14956         local TF="$TMP/$tfile"
14957
14958         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14959         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
14960         cp $TF $DIR/$tfile
14961         cancel_lru_locks $OSC
14962         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
14963         remount_client $MOUNT
14964         df -P $MOUNT
14965         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
14966
14967         $TRUNCATE $TF 6000
14968         $TRUNCATE $DIR/$tfile 6000
14969         cancel_lru_locks $OSC
14970         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
14971
14972         echo "12345" >>$TF
14973         echo "12345" >>$DIR/$tfile
14974         cancel_lru_locks $OSC
14975         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
14976
14977         echo "12345" >>$TF
14978         echo "12345" >>$DIR/$tfile
14979         cancel_lru_locks $OSC
14980         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
14981 }
14982 run_test 150a "truncate/append tests"
14983
14984 test_150b() {
14985         check_set_fallocate_or_skip
14986
14987         touch $DIR/$tfile
14988         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14989         check_fallocate $DIR/$tfile || skip_eopnotsupp "fallocate failed"
14990 }
14991 run_test 150b "Verify fallocate (prealloc) functionality"
14992
14993 test_150bb() {
14994         check_set_fallocate_or_skip
14995
14996         touch $DIR/$tfile
14997         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
14998         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=20 || error "dd failed"
14999         > $DIR/$tfile
15000         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
15001         # precomputed md5sum for 20MB of zeroes
15002         local expect="8f4e33f3dc3e414ff94e5fb6905cba8c"
15003         local sum=($(md5sum $DIR/$tfile))
15004
15005         [[ "${sum[0]}" == "$expect" ]] || error "fallocate unwritten is not zero"
15006
15007         check_set_fallocate 1
15008
15009         > $DIR/$tfile
15010         fallocate -l $((1048576 * 20)) $DIR/$tfile || error "fallocate failed"
15011         sum=($(md5sum $DIR/$tfile))
15012
15013         [[ "${sum[0]}" == "$expect" ]] || error "fallocate zero is not zero"
15014 }
15015 run_test 150bb "Verify fallocate modes both zero space"
15016
15017 test_150c() {
15018         check_set_fallocate_or_skip
15019         local striping="-c2"
15020
15021         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15022         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tfile || error "setstripe failed"
15023         fallocate -l ${OSTCOUNT}m $DIR/$tfile || error "fallocate failed"
15024         local bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
15025         local want=$((OSTCOUNT * 1048576))
15026
15027         # Must allocate all requested space, not more than 5% extra
15028         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
15029                 error "bytes $bytes is not $want"
15030
15031         rm -f $DIR/$tfile
15032
15033         echo "verify fallocate on PFL file"
15034
15035         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
15036
15037         $LFS setstripe -E1M $striping -E16M -c3 -Eeof -c 4 $DIR/$tfile ||
15038                 error "Create $DIR/$tfile failed"
15039         fallocate -l $((1048576 * 512)) $DIR/$tfile || error "fallocate failed"
15040         bytes=$(($(stat -c '%b * %B' $DIR/$tfile)))
15041         want=$((512 * 1048576))
15042
15043         # Must allocate all requested space, not more than 5% extra
15044         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
15045                 error "bytes $bytes is not $want"
15046 }
15047 run_test 150c "Verify fallocate Size and Blocks"
15048
15049 test_150d() {
15050         check_set_fallocate_or_skip
15051         local striping="-c2"
15052
15053         [[ "x$DOM" == "xyes" ]] && striping="-L mdt"
15054
15055         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15056         $LFS setstripe -E1M $striping -E eof -c $OSTCOUNT -S1M $DIR/$tdir ||
15057                 error "setstripe failed"
15058         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
15059         local bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
15060         local want=$((OSTCOUNT * 1048576))
15061
15062         # Must allocate all requested space, not more than 5% extra
15063         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
15064                 error "bytes $bytes is not $want"
15065 }
15066 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
15067
15068 test_150e() {
15069         check_set_fallocate_or_skip
15070
15071         echo "df before:"
15072         $LFS df
15073         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15074         $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
15075                 error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
15076
15077         # Find OST with Minimum Size
15078         min_size_ost=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
15079                        sort -un | head -1)
15080
15081         # Get 100MB per OST of the available space to reduce run time
15082         # else 60% of the available space if we are running SLOW tests
15083         if [ $SLOW == "no" ]; then
15084                 local space=$((1024 * 100 * OSTCOUNT))
15085         else
15086                 local space=$(((min_size_ost * 60)/100 * OSTCOUNT))
15087         fi
15088
15089         fallocate -l${space}k $DIR/$tfile ||
15090                 error "fallocate ${space}k $DIR/$tfile failed"
15091         echo "'fallocate -l ${space}k $DIR/$tfile' succeeded"
15092
15093         # get size immediately after fallocate. This should be correctly
15094         # updated
15095         local size=$(stat -c '%s' $DIR/$tfile)
15096         local used=$(( $(stat -c '%b * %B' $DIR/$tfile) / 1024))
15097
15098         # Sleep for a while for statfs to get updated. And not pull from cache.
15099         sleep 2
15100
15101         echo "df after fallocate:"
15102         $LFS df
15103
15104         (( size / 1024 == space )) || error "size $size != requested $space"
15105         [ "$ost1_FSTYPE" != ldiskfs ] || (( used >= space )) ||
15106                 error "used $used < space $space"
15107
15108         rm $DIR/$tfile || error "rm failed"
15109         sync
15110         wait_delete_completed
15111
15112         echo "df after unlink:"
15113         $LFS df
15114 }
15115 run_test 150e "Verify 60% of available OST space consumed by fallocate"
15116
15117 test_150f() {
15118         local size
15119         local blocks
15120         local want_size_before=20480 # in bytes
15121         local want_blocks_before=40 # 512 sized blocks
15122         local want_blocks_after=24  # 512 sized blocks
15123         local length=$(((want_blocks_before - want_blocks_after) * 512))
15124
15125         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
15126                 skip "need at least 2.14.0 for fallocate punch"
15127
15128         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
15129                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
15130         fi
15131
15132         check_set_fallocate_or_skip
15133         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15134
15135         [[ "x$DOM" == "xyes" ]] &&
15136                 $LFS setstripe -E1M -L mdt -E eof $DIR/$tfile
15137
15138         echo "Verify fallocate punch: Range within the file range"
15139         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
15140                 error "dd failed for bs 4096 and count 5"
15141
15142         # Call fallocate with punch range which is within the file range
15143         out=$(fallocate -p --offset 4096 -l $length $DIR/$tfile 2>&1) ||
15144                 skip_eopnotsupp "$out|fallocate: offset 4096 and length $length"
15145         # client must see changes immediately after fallocate
15146         size=$(stat -c '%s' $DIR/$tfile)
15147         blocks=$(stat -c '%b' $DIR/$tfile)
15148
15149         # Verify punch worked.
15150         (( blocks == want_blocks_after )) ||
15151                 error "punch failed: blocks $blocks != $want_blocks_after"
15152
15153         (( size == want_size_before )) ||
15154                 error "punch failed: size $size != $want_size_before"
15155
15156         # Verify there is hole in file
15157         local data_off=$(lseek_test -d 4096 $DIR/$tfile)
15158         # precomputed md5sum
15159         local expect="4a9a834a2db02452929c0a348273b4aa"
15160
15161         cksum=($(md5sum $DIR/$tfile))
15162         [[ "${cksum[0]}" == "$expect" ]] ||
15163                 error "unexpected MD5SUM after punch: ${cksum[0]}"
15164
15165         # Start second sub-case for fallocate punch.
15166         echo "Verify fallocate punch: Range overlapping and less than blocksize"
15167         yes 'A' | dd of=$DIR/$tfile bs=4096 count=5 ||
15168                 error "dd failed for bs 4096 and count 5"
15169
15170         # Punch range less than block size will have no change in block count
15171         want_blocks_after=40  # 512 sized blocks
15172
15173         # Punch overlaps two blocks and less than blocksize
15174         out=$(fallocate -p --offset 4000 -l 3000 $DIR/$tfile 2>&1) ||
15175                 skip_eopnotsupp "$out|fallocate: offset 4000 length 3000"
15176         size=$(stat -c '%s' $DIR/$tfile)
15177         blocks=$(stat -c '%b' $DIR/$tfile)
15178
15179         # Verify punch worked.
15180         (( blocks == want_blocks_after )) ||
15181                 error "punch failed: blocks $blocks != $want_blocks_after"
15182
15183         (( size == want_size_before )) ||
15184                 error "punch failed: size $size != $want_size_before"
15185
15186         # Verify if range is really zero'ed out. We expect Zeros.
15187         # precomputed md5sum
15188         expect="c57ec5d769c3dbe3426edc3f7d7e11d3"
15189         cksum=($(md5sum $DIR/$tfile))
15190         [[ "${cksum[0]}" == "$expect" ]] ||
15191                 error "unexpected MD5SUM after punch: ${cksum[0]}"
15192 }
15193 run_test 150f "Verify fallocate punch functionality"
15194
15195 test_150g() {
15196         local space
15197         local size
15198         local blocks
15199         local blocks_after
15200         local size_after
15201         local BS=4096 # Block size in bytes
15202
15203         [[ $OST1_VERSION -ge $(version_code 2.14.0) ]] ||
15204                 skip "need at least 2.14.0 for fallocate punch"
15205
15206         if [ "$ost1_FSTYPE" = "zfs" ] || [ "$mds1_FSTYPE" = "zfs" ]; then
15207                 skip "LU-14160: punch mode is not implemented on OSD ZFS"
15208         fi
15209
15210         check_set_fallocate_or_skip
15211         stack_trap "rm -f $DIR/$tfile; wait_delete_completed"
15212
15213         if [[ "x$DOM" == "xyes" ]]; then
15214                 $LFS setstripe -E2M -L mdt -E eof -c${OSTCOUNT} $DIR/$tfile ||
15215                         error "$LFS setstripe DoM + ${OSTCOUNT} OST failed"
15216         else
15217                 $LFS setstripe -c${OSTCOUNT} $DIR/$tfile ||
15218                         error "$LFS setstripe -c${OSTCOUNT} $DIR/$tfile failed"
15219         fi
15220
15221         # Get 100MB per OST of the available space to reduce run time
15222         # else 60% of the available space if we are running SLOW tests
15223         if [ $SLOW == "no" ]; then
15224                 space=$((1024 * 100 * OSTCOUNT))
15225         else
15226                 # Find OST with Minimum Size
15227                 space=$($LFS df | awk "/$FSNAME-OST/ { print \$4 }" |
15228                         sort -un | head -1)
15229                 echo "min size OST: $space"
15230                 space=$(((space * 60)/100 * OSTCOUNT))
15231         fi
15232         # space in 1k units, round to 4k blocks
15233         local blkcount=$((space * 1024 / $BS))
15234
15235         echo "Verify fallocate punch: Very large Range"
15236         fallocate -l${space}k $DIR/$tfile ||
15237                 error "fallocate ${space}k $DIR/$tfile failed"
15238         # write 1M at the end, start and in the middle
15239         yes 'A' | dd of=$DIR/$tfile bs=$BS count=256 ||
15240                 error "dd failed: bs $BS count 256"
15241         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount - 256)) count=256 ||
15242                 error "dd failed: bs $BS count 256 seek $((blkcount - 256))"
15243         yes 'A' | dd of=$DIR/$tfile bs=$BS seek=$((blkcount / 2)) count=1024 ||
15244                 error "dd failed: bs $BS count 256 seek $((blkcount / 2))"
15245
15246         # Gather stats.
15247         size=$(stat -c '%s' $DIR/$tfile)
15248
15249         # gather punch length.
15250         local punch_size=$((size - (BS * 2)))
15251
15252         echo "punch_size = $punch_size"
15253         echo "size - punch_size: $((size - punch_size))"
15254         echo "size - punch_size in blocks: $(((size - punch_size)/BS))"
15255
15256         # Call fallocate to punch all except 2 blocks. We leave the
15257         # first and the last block
15258         echo "fallocate -p --offset $BS -l $punch_size $DIR/$tfile"
15259         out=$(fallocate -p --offset $BS -l $punch_size $DIR/$tfile 2>&1) ||
15260                 skip_eopnotsupp "$out|fallocate: offset $BS length $punch_size"
15261
15262         size_after=$(stat -c '%s' $DIR/$tfile)
15263         blocks_after=$(stat -c '%b' $DIR/$tfile)
15264
15265         # Verify punch worked.
15266         # Size should be kept
15267         (( size == size_after )) ||
15268                 error "punch failed: size $size != $size_after"
15269
15270         # two 4k data blocks to remain plus possible 1 extra extent block
15271         (( blocks_after <= ((BS / 512) * 3) )) ||
15272                 error "too many blocks remains: $blocks_after"
15273
15274         # Verify that file has hole between the first and the last blocks
15275         local hole_start=$(lseek_test -l 0 $DIR/$tfile)
15276         local hole_end=$(lseek_test -d $BS $DIR/$tfile)
15277
15278         echo "Hole at [$hole_start, $hole_end)"
15279         (( hole_start == BS )) ||
15280                 error "no hole at offset $BS after punch"
15281
15282         (( hole_end == BS + punch_size )) ||
15283                 error "data at offset $hole_end < $((BS + punch_size))"
15284 }
15285 run_test 150g "Verify fallocate punch on large range"
15286
15287 #LU-2902 roc_hit was not able to read all values from lproc
15288 function roc_hit_init() {
15289         local list=$(comma_list $(osts_nodes))
15290         local dir=$DIR/$tdir-check
15291         local file=$dir/$tfile
15292         local BEFORE
15293         local AFTER
15294         local idx
15295
15296         test_mkdir $dir
15297         #use setstripe to do a write to every ost
15298         for i in $(seq 0 $((OSTCOUNT-1))); do
15299                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
15300                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
15301                 idx=$(printf %04x $i)
15302                 BEFORE=$(get_osd_param $list *OST*$idx stats |
15303                         awk '$1 == "cache_access" {sum += $7}
15304                                 END { printf("%0.0f", sum) }')
15305
15306                 cancel_lru_locks osc
15307                 cat $file >/dev/null
15308
15309                 AFTER=$(get_osd_param $list *OST*$idx stats |
15310                         awk '$1 == "cache_access" {sum += $7}
15311                                 END { printf("%0.0f", sum) }')
15312
15313                 echo BEFORE:$BEFORE AFTER:$AFTER
15314                 if ! let "AFTER - BEFORE == 4"; then
15315                         rm -rf $dir
15316                         error "roc_hit is not safe to use"
15317                 fi
15318                 rm $file
15319         done
15320
15321         rm -rf $dir
15322 }
15323
15324 function roc_hit() {
15325         local list=$(comma_list $(osts_nodes))
15326         echo $(get_osd_param $list '' stats |
15327                 awk '$1 == "cache_hit" {sum += $7}
15328                         END { printf("%0.0f", sum) }')
15329 }
15330
15331 function set_cache() {
15332         local on=1
15333
15334         if [ "$2" == "off" ]; then
15335                 on=0;
15336         fi
15337         local list=$(comma_list $(osts_nodes))
15338         set_osd_param $list '' $1_cache_enable $on
15339
15340         cancel_lru_locks osc
15341 }
15342
15343 test_151() {
15344         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15345         remote_ost_nodsh && skip "remote OST with nodsh"
15346
15347         local CPAGES=3
15348         local list=$(comma_list $(osts_nodes))
15349
15350         # check whether obdfilter is cache capable at all
15351         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
15352                 skip "not cache-capable obdfilter"
15353         fi
15354
15355         # check cache is enabled on all obdfilters
15356         if get_osd_param $list '' read_cache_enable | grep 0; then
15357                 skip "oss cache is disabled"
15358         fi
15359
15360         set_osd_param $list '' writethrough_cache_enable 1
15361
15362         # check write cache is enabled on all obdfilters
15363         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
15364                 skip "oss write cache is NOT enabled"
15365         fi
15366
15367         roc_hit_init
15368
15369         #define OBD_FAIL_OBD_NO_LRU  0x609
15370         do_nodes $list $LCTL set_param fail_loc=0x609
15371
15372         # pages should be in the case right after write
15373         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
15374                 error "dd failed"
15375
15376         local BEFORE=$(roc_hit)
15377         cancel_lru_locks osc
15378         cat $DIR/$tfile >/dev/null
15379         local AFTER=$(roc_hit)
15380
15381         do_nodes $list $LCTL set_param fail_loc=0
15382
15383         if ! let "AFTER - BEFORE == CPAGES"; then
15384                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
15385         fi
15386
15387         cancel_lru_locks osc
15388         # invalidates OST cache
15389         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
15390         set_osd_param $list '' read_cache_enable 0
15391         cat $DIR/$tfile >/dev/null
15392
15393         # now data shouldn't be found in the cache
15394         BEFORE=$(roc_hit)
15395         cancel_lru_locks osc
15396         cat $DIR/$tfile >/dev/null
15397         AFTER=$(roc_hit)
15398         if let "AFTER - BEFORE != 0"; then
15399                 error "IN CACHE: before: $BEFORE, after: $AFTER"
15400         fi
15401
15402         set_osd_param $list '' read_cache_enable 1
15403         rm -f $DIR/$tfile
15404 }
15405 run_test 151 "test cache on oss and controls ==============================="
15406
15407 test_152() {
15408         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15409
15410         local TF="$TMP/$tfile"
15411
15412         # simulate ENOMEM during write
15413 #define OBD_FAIL_OST_NOMEM      0x226
15414         lctl set_param fail_loc=0x80000226
15415         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
15416         cp $TF $DIR/$tfile
15417         sync || error "sync failed"
15418         lctl set_param fail_loc=0
15419
15420         # discard client's cache
15421         cancel_lru_locks osc
15422
15423         # simulate ENOMEM during read
15424         lctl set_param fail_loc=0x80000226
15425         cmp $TF $DIR/$tfile || error "cmp failed"
15426         lctl set_param fail_loc=0
15427
15428         rm -f $TF
15429 }
15430 run_test 152 "test read/write with enomem ============================"
15431
15432 test_153() {
15433         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
15434 }
15435 run_test 153 "test if fdatasync does not crash ======================="
15436
15437 dot_lustre_fid_permission_check() {
15438         local fid=$1
15439         local ffid=$MOUNT/.lustre/fid/$fid
15440         local test_dir=$2
15441
15442         echo "stat fid $fid"
15443         stat $ffid || error "stat $ffid failed."
15444         echo "touch fid $fid"
15445         touch $ffid || error "touch $ffid failed."
15446         echo "write to fid $fid"
15447         cat /etc/hosts > $ffid || error "write $ffid failed."
15448         echo "read fid $fid"
15449         diff /etc/hosts $ffid || error "read $ffid failed."
15450         echo "append write to fid $fid"
15451         cat /etc/hosts >> $ffid || error "append write $ffid failed."
15452         echo "rename fid $fid"
15453         mv $ffid $test_dir/$tfile.1 &&
15454                 error "rename $ffid to $tfile.1 should fail."
15455         touch $test_dir/$tfile.1
15456         mv $test_dir/$tfile.1 $ffid &&
15457                 error "rename $tfile.1 to $ffid should fail."
15458         rm -f $test_dir/$tfile.1
15459         echo "truncate fid $fid"
15460         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
15461         echo "link fid $fid"
15462         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
15463         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
15464                 echo "setfacl fid $fid"
15465                 setfacl -R -m u:$USER0:rwx $ffid ||
15466                         error "setfacl $ffid failed"
15467                 echo "getfacl fid $fid"
15468                 getfacl $ffid || error "getfacl $ffid failed."
15469         fi
15470         echo "unlink fid $fid"
15471         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
15472         echo "mknod fid $fid"
15473         mknod $ffid c 1 3 && error "mknod $ffid should fail."
15474
15475         fid=[0xf00000400:0x1:0x0]
15476         ffid=$MOUNT/.lustre/fid/$fid
15477
15478         echo "stat non-exist fid $fid"
15479         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
15480         echo "write to non-exist fid $fid"
15481         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
15482         echo "link new fid $fid"
15483         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
15484
15485         mkdir -p $test_dir/$tdir
15486         touch $test_dir/$tdir/$tfile
15487         fid=$($LFS path2fid $test_dir/$tdir)
15488         rc=$?
15489         [ $rc -ne 0 ] &&
15490                 error "error: could not get fid for $test_dir/$dir/$tfile."
15491
15492         ffid=$MOUNT/.lustre/fid/$fid
15493
15494         echo "ls $fid"
15495         ls $ffid || error "ls $ffid failed."
15496         echo "touch $fid/$tfile.1"
15497         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
15498
15499         echo "touch $MOUNT/.lustre/fid/$tfile"
15500         touch $MOUNT/.lustre/fid/$tfile && \
15501                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
15502
15503         echo "setxattr to $MOUNT/.lustre/fid"
15504         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
15505
15506         echo "listxattr for $MOUNT/.lustre/fid"
15507         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
15508
15509         echo "delxattr from $MOUNT/.lustre/fid"
15510         setfattr -x trusted.name1 $MOUNT/.lustre/fid
15511
15512         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
15513         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
15514                 error "touch invalid fid should fail."
15515
15516         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
15517         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
15518                 error "touch non-normal fid should fail."
15519
15520         echo "rename $tdir to $MOUNT/.lustre/fid"
15521         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
15522                 error "rename to $MOUNT/.lustre/fid should fail."
15523
15524         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
15525         then            # LU-3547
15526                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
15527                 local new_obf_mode=777
15528
15529                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
15530                 chmod $new_obf_mode $DIR/.lustre/fid ||
15531                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
15532
15533                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
15534                 [ $obf_mode -eq $new_obf_mode ] ||
15535                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
15536
15537                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
15538                 chmod $old_obf_mode $DIR/.lustre/fid ||
15539                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
15540         fi
15541
15542         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
15543         fid=$($LFS path2fid $test_dir/$tfile-2)
15544
15545         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
15546         then # LU-5424
15547                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
15548                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
15549                         error "create lov data thru .lustre failed"
15550         fi
15551         echo "cp /etc/passwd $test_dir/$tfile-2"
15552         cp /etc/passwd $test_dir/$tfile-2 ||
15553                 error "copy to $test_dir/$tfile-2 failed."
15554         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
15555         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
15556                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
15557
15558         rm -rf $test_dir/tfile.lnk
15559         rm -rf $test_dir/$tfile-2
15560 }
15561
15562 test_154A() {
15563         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15564                 skip "Need MDS version at least 2.4.1"
15565
15566         local tf=$DIR/$tfile
15567         touch $tf
15568
15569         local fid=$($LFS path2fid $tf)
15570         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
15571
15572         # check that we get the same pathname back
15573         local rootpath
15574         local found
15575         for rootpath in "$MOUNT" "$MOUNT///" "$MOUNT/$tfile"; do
15576                 echo "$rootpath $fid"
15577                 found=$($LFS fid2path $rootpath "$fid")
15578                 [ -z "$found" ] && error "fid2path unable to get '$fid' path"
15579                 [ "$found" == "$tf" ] || error "fid2path $found != $tf"
15580         done
15581
15582         # check wrong root path format
15583         rootpath=$MOUNT"_wrong"
15584         found=$($LFS fid2path $rootpath "$fid")
15585         [ -z "$found" ] || error "should fail ($rootpath != $MOUNT)"
15586 }
15587 run_test 154A "lfs path2fid and fid2path basic checks"
15588
15589 test_154B() {
15590         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15591                 skip "Need MDS version at least 2.4.1"
15592
15593         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
15594         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
15595         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
15596         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
15597
15598         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
15599         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
15600
15601         # check that we get the same pathname
15602         echo "PFID: $PFID, name: $name"
15603         local FOUND=$($LFS fid2path $MOUNT "$PFID")
15604         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
15605         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
15606                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
15607
15608         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
15609 }
15610 run_test 154B "verify the ll_decode_linkea tool"
15611
15612 test_154a() {
15613         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15614         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15615         (( $MDS1_VERSION >= $(version_code 2.2.51) )) ||
15616                 skip "Need MDS version at least 2.2.51"
15617         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
15618
15619         cp /etc/hosts $DIR/$tfile
15620
15621         fid=$($LFS path2fid $DIR/$tfile)
15622         rc=$?
15623         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
15624
15625         dot_lustre_fid_permission_check "$fid" $DIR ||
15626                 error "dot lustre permission check $fid failed"
15627
15628         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
15629
15630         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
15631
15632         touch $MOUNT/.lustre/file &&
15633                 error "creation is not allowed under .lustre"
15634
15635         mkdir $MOUNT/.lustre/dir &&
15636                 error "mkdir is not allowed under .lustre"
15637
15638         rm -rf $DIR/$tfile
15639 }
15640 run_test 154a "Open-by-FID"
15641
15642 test_154b() {
15643         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15644         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15645         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15646         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
15647                 skip "Need MDS version at least 2.2.51"
15648
15649         local remote_dir=$DIR/$tdir/remote_dir
15650         local MDTIDX=1
15651         local rc=0
15652
15653         mkdir -p $DIR/$tdir
15654         $LFS mkdir -i $MDTIDX $remote_dir ||
15655                 error "create remote directory failed"
15656
15657         cp /etc/hosts $remote_dir/$tfile
15658
15659         fid=$($LFS path2fid $remote_dir/$tfile)
15660         rc=$?
15661         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
15662
15663         dot_lustre_fid_permission_check "$fid" $remote_dir ||
15664                 error "dot lustre permission check $fid failed"
15665         rm -rf $DIR/$tdir
15666 }
15667 run_test 154b "Open-by-FID for remote directory"
15668
15669 test_154c() {
15670         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
15671                 skip "Need MDS version at least 2.4.1"
15672
15673         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
15674         local FID1=$($LFS path2fid $DIR/$tfile.1)
15675         local FID2=$($LFS path2fid $DIR/$tfile.2)
15676         local FID3=$($LFS path2fid $DIR/$tfile.3)
15677
15678         local N=1
15679         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
15680                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
15681                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
15682                 local want=FID$N
15683                 [ "$FID" = "${!want}" ] ||
15684                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
15685                 N=$((N + 1))
15686         done
15687
15688         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
15689         do
15690                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
15691                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
15692                 N=$((N + 1))
15693         done
15694 }
15695 run_test 154c "lfs path2fid and fid2path multiple arguments"
15696
15697 test_154d() {
15698         remote_mds_nodsh && skip "remote MDS with nodsh"
15699         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
15700                 skip "Need MDS version at least 2.5.53"
15701
15702         if remote_mds; then
15703                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
15704         else
15705                 nid="0@lo"
15706         fi
15707         local proc_ofile="mdt.*.exports.'$nid'.open_files"
15708         local fd
15709         local cmd
15710
15711         rm -f $DIR/$tfile
15712         touch $DIR/$tfile
15713
15714         local fid=$($LFS path2fid $DIR/$tfile)
15715         # Open the file
15716         fd=$(free_fd)
15717         cmd="exec $fd<$DIR/$tfile"
15718         eval $cmd
15719         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
15720         echo "$fid_list" | grep "$fid"
15721         rc=$?
15722
15723         cmd="exec $fd>/dev/null"
15724         eval $cmd
15725         if [ $rc -ne 0 ]; then
15726                 error "FID $fid not found in open files list $fid_list"
15727         fi
15728 }
15729 run_test 154d "Verify open file fid"
15730
15731 test_154e()
15732 {
15733         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
15734                 skip "Need MDS version at least 2.6.50"
15735
15736         if ls -a $MOUNT | grep -q '^\.lustre$'; then
15737                 error ".lustre returned by readdir"
15738         fi
15739 }
15740 run_test 154e ".lustre is not returned by readdir"
15741
15742 test_154f() {
15743         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15744
15745         # create parent directory on a single MDT to avoid cross-MDT hardlinks
15746         mkdir_on_mdt0 $DIR/$tdir
15747         # test dirs inherit from its stripe
15748         mkdir -p $DIR/$tdir/foo1 || error "mkdir error"
15749         mkdir -p $DIR/$tdir/foo2 || error "mkdir error"
15750         cp /etc/hosts $DIR/$tdir/foo1/$tfile
15751         ln $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/link
15752         touch $DIR/f
15753
15754         # get fid of parents
15755         local FID0=$($LFS path2fid $DIR/$tdir)
15756         local FID1=$($LFS path2fid $DIR/$tdir/foo1)
15757         local FID2=$($LFS path2fid $DIR/$tdir/foo2)
15758         local FID3=$($LFS path2fid $DIR)
15759
15760         # check that path2fid --parents returns expected <parent_fid>/name
15761         # 1) test for a directory (single parent)
15762         local parent=$($LFS path2fid --parents $DIR/$tdir/foo1)
15763         [ "$parent" == "$FID0/foo1" ] ||
15764                 error "expected parent: $FID0/foo1, got: $parent"
15765
15766         # 2) test for a file with nlink > 1 (multiple parents)
15767         parent=$($LFS path2fid --parents $DIR/$tdir/foo1/$tfile)
15768         echo "$parent" | grep -F "$FID1/$tfile" ||
15769                 error "$FID1/$tfile not returned in parent list"
15770         echo "$parent" | grep -F "$FID2/link" ||
15771                 error "$FID2/link not returned in parent list"
15772
15773         # 3) get parent by fid
15774         local file_fid=$($LFS path2fid $DIR/$tdir/foo1/$tfile)
15775         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15776         echo "$parent" | grep -F "$FID1/$tfile" ||
15777                 error "$FID1/$tfile not returned in parent list (by fid)"
15778         echo "$parent" | grep -F "$FID2/link" ||
15779                 error "$FID2/link not returned in parent list (by fid)"
15780
15781         # 4) test for entry in root directory
15782         parent=$($LFS path2fid --parents $DIR/f)
15783         echo "$parent" | grep -F "$FID3/f" ||
15784                 error "$FID3/f not returned in parent list"
15785
15786         # 5) test it on root directory
15787         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
15788                 error "$MOUNT should not have parents"
15789
15790         # enable xattr caching and check that linkea is correctly updated
15791         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
15792         save_lustre_params client "llite.*.xattr_cache" > $save
15793         lctl set_param llite.*.xattr_cache 1
15794
15795         # 6.1) linkea update on rename
15796         mv $DIR/$tdir/foo1/$tfile $DIR/$tdir/foo2/$tfile.moved
15797
15798         # get parents by fid
15799         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15800         # foo1 should no longer be returned in parent list
15801         echo "$parent" | grep -F "$FID1" &&
15802                 error "$FID1 should no longer be in parent list"
15803         # the new path should appear
15804         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
15805                 error "$FID2/$tfile.moved is not in parent list"
15806
15807         # 6.2) linkea update on unlink
15808         rm -f $DIR/$tdir/foo2/link
15809         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
15810         # foo2/link should no longer be returned in parent list
15811         echo "$parent" | grep -F "$FID2/link" &&
15812                 error "$FID2/link should no longer be in parent list"
15813         true
15814
15815         rm -f $DIR/f
15816         restore_lustre_params < $save
15817         rm -f $save
15818 }
15819 run_test 154f "get parent fids by reading link ea"
15820
15821 test_154g()
15822 {
15823         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15824         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
15825            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
15826                 skip "Need MDS version at least 2.6.92"
15827
15828         mkdir_on_mdt0 $DIR/$tdir
15829         llapi_fid_test -d $DIR/$tdir
15830 }
15831 run_test 154g "various llapi FID tests"
15832
15833 test_155_small_load() {
15834     local temp=$TMP/$tfile
15835     local file=$DIR/$tfile
15836
15837     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
15838         error "dd of=$temp bs=6096 count=1 failed"
15839     cp $temp $file
15840     cancel_lru_locks $OSC
15841     cmp $temp $file || error "$temp $file differ"
15842
15843     $TRUNCATE $temp 6000
15844     $TRUNCATE $file 6000
15845     cmp $temp $file || error "$temp $file differ (truncate1)"
15846
15847     echo "12345" >>$temp
15848     echo "12345" >>$file
15849     cmp $temp $file || error "$temp $file differ (append1)"
15850
15851     echo "12345" >>$temp
15852     echo "12345" >>$file
15853     cmp $temp $file || error "$temp $file differ (append2)"
15854
15855     rm -f $temp $file
15856     true
15857 }
15858
15859 test_155_big_load() {
15860         remote_ost_nodsh && skip "remote OST with nodsh"
15861
15862         local temp=$TMP/$tfile
15863         local file=$DIR/$tfile
15864
15865         free_min_max
15866         local cache_size=$(do_facet ost$((MAXI+1)) \
15867                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
15868
15869         # LU-16042: can not get the cache size on Arm64 VM here, fallback to a
15870         # pre-set value
15871         if [ -z "$cache_size" ]; then
15872                 cache_size=256
15873         fi
15874         local large_file_size=$((cache_size * 2))
15875
15876         echo "OSS cache size: $cache_size KB"
15877         echo "Large file size: $large_file_size KB"
15878
15879         [ $MAXV -le $large_file_size ] &&
15880                 skip_env "max available OST size needs > $large_file_size KB"
15881
15882         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
15883
15884         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
15885                 error "dd of=$temp bs=$large_file_size count=1k failed"
15886         cp $temp $file
15887         ls -lh $temp $file
15888         cancel_lru_locks osc
15889         cmp $temp $file || error "$temp $file differ"
15890
15891         rm -f $temp $file
15892         true
15893 }
15894
15895 save_writethrough() {
15896         local facets=$(get_facets OST)
15897
15898         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
15899 }
15900
15901 test_155a() {
15902         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15903
15904         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15905
15906         save_writethrough $p
15907
15908         set_cache read on
15909         set_cache writethrough on
15910         test_155_small_load
15911         restore_lustre_params < $p
15912         rm -f $p
15913 }
15914 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
15915
15916 test_155b() {
15917         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15918
15919         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15920
15921         save_writethrough $p
15922
15923         set_cache read on
15924         set_cache writethrough off
15925         test_155_small_load
15926         restore_lustre_params < $p
15927         rm -f $p
15928 }
15929 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
15930
15931 test_155c() {
15932         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15933
15934         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15935
15936         save_writethrough $p
15937
15938         set_cache read off
15939         set_cache writethrough on
15940         test_155_small_load
15941         restore_lustre_params < $p
15942         rm -f $p
15943 }
15944 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
15945
15946 test_155d() {
15947         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15948
15949         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15950
15951         save_writethrough $p
15952
15953         set_cache read off
15954         set_cache writethrough off
15955         test_155_small_load
15956         restore_lustre_params < $p
15957         rm -f $p
15958 }
15959 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
15960
15961 test_155e() {
15962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15963
15964         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15965
15966         save_writethrough $p
15967
15968         set_cache read on
15969         set_cache writethrough on
15970         test_155_big_load
15971         restore_lustre_params < $p
15972         rm -f $p
15973 }
15974 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
15975
15976 test_155f() {
15977         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15978
15979         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15980
15981         save_writethrough $p
15982
15983         set_cache read on
15984         set_cache writethrough off
15985         test_155_big_load
15986         restore_lustre_params < $p
15987         rm -f $p
15988 }
15989 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
15990
15991 test_155g() {
15992         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15993
15994         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
15995
15996         save_writethrough $p
15997
15998         set_cache read off
15999         set_cache writethrough on
16000         test_155_big_load
16001         restore_lustre_params < $p
16002         rm -f $p
16003 }
16004 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
16005
16006 test_155h() {
16007         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16008
16009         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16010
16011         save_writethrough $p
16012
16013         set_cache read off
16014         set_cache writethrough off
16015         test_155_big_load
16016         restore_lustre_params < $p
16017         rm -f $p
16018 }
16019 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
16020
16021 test_156() {
16022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16023         remote_ost_nodsh && skip "remote OST with nodsh"
16024         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
16025                 skip "stats not implemented on old servers"
16026         [ "$ost1_FSTYPE" = "zfs" ] &&
16027                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
16028
16029         local CPAGES=3
16030         local BEFORE
16031         local AFTER
16032         local file="$DIR/$tfile"
16033         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16034
16035         save_writethrough $p
16036         roc_hit_init
16037
16038         log "Turn on read and write cache"
16039         set_cache read on
16040         set_cache writethrough on
16041
16042         log "Write data and read it back."
16043         log "Read should be satisfied from the cache."
16044         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16045         BEFORE=$(roc_hit)
16046         cancel_lru_locks osc
16047         cat $file >/dev/null
16048         AFTER=$(roc_hit)
16049         if ! let "AFTER - BEFORE == CPAGES"; then
16050                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
16051         else
16052                 log "cache hits: before: $BEFORE, after: $AFTER"
16053         fi
16054
16055         log "Read again; it should be satisfied from the cache."
16056         BEFORE=$AFTER
16057         cancel_lru_locks osc
16058         cat $file >/dev/null
16059         AFTER=$(roc_hit)
16060         if ! let "AFTER - BEFORE == CPAGES"; then
16061                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
16062         else
16063                 log "cache hits:: before: $BEFORE, after: $AFTER"
16064         fi
16065
16066         log "Turn off the read cache and turn on the write cache"
16067         set_cache read off
16068         set_cache writethrough on
16069
16070         log "Read again; it should be satisfied from the cache."
16071         BEFORE=$(roc_hit)
16072         cancel_lru_locks osc
16073         cat $file >/dev/null
16074         AFTER=$(roc_hit)
16075         if ! let "AFTER - BEFORE == CPAGES"; then
16076                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
16077         else
16078                 log "cache hits:: before: $BEFORE, after: $AFTER"
16079         fi
16080
16081         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
16082                 # > 2.12.56 uses pagecache if cached
16083                 log "Read again; it should not be satisfied from the cache."
16084                 BEFORE=$AFTER
16085                 cancel_lru_locks osc
16086                 cat $file >/dev/null
16087                 AFTER=$(roc_hit)
16088                 if ! let "AFTER - BEFORE == 0"; then
16089                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
16090                 else
16091                         log "cache hits:: before: $BEFORE, after: $AFTER"
16092                 fi
16093         fi
16094
16095         log "Write data and read it back."
16096         log "Read should be satisfied from the cache."
16097         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16098         BEFORE=$(roc_hit)
16099         cancel_lru_locks osc
16100         cat $file >/dev/null
16101         AFTER=$(roc_hit)
16102         if ! let "AFTER - BEFORE == CPAGES"; then
16103                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
16104         else
16105                 log "cache hits:: before: $BEFORE, after: $AFTER"
16106         fi
16107
16108         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
16109                 # > 2.12.56 uses pagecache if cached
16110                 log "Read again; it should not be satisfied from the cache."
16111                 BEFORE=$AFTER
16112                 cancel_lru_locks osc
16113                 cat $file >/dev/null
16114                 AFTER=$(roc_hit)
16115                 if ! let "AFTER - BEFORE == 0"; then
16116                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
16117                 else
16118                         log "cache hits:: before: $BEFORE, after: $AFTER"
16119                 fi
16120         fi
16121
16122         log "Turn off read and write cache"
16123         set_cache read off
16124         set_cache writethrough off
16125
16126         log "Write data and read it back"
16127         log "It should not be satisfied from the cache."
16128         rm -f $file
16129         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16130         cancel_lru_locks osc
16131         BEFORE=$(roc_hit)
16132         cat $file >/dev/null
16133         AFTER=$(roc_hit)
16134         if ! let "AFTER - BEFORE == 0"; then
16135                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
16136         else
16137                 log "cache hits:: before: $BEFORE, after: $AFTER"
16138         fi
16139
16140         log "Turn on the read cache and turn off the write cache"
16141         set_cache read on
16142         set_cache writethrough off
16143
16144         log "Write data and read it back"
16145         log "It should not be satisfied from the cache."
16146         rm -f $file
16147         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
16148         BEFORE=$(roc_hit)
16149         cancel_lru_locks osc
16150         cat $file >/dev/null
16151         AFTER=$(roc_hit)
16152         if ! let "AFTER - BEFORE == 0"; then
16153                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
16154         else
16155                 log "cache hits:: before: $BEFORE, after: $AFTER"
16156         fi
16157
16158         log "Read again; it should be satisfied from the cache."
16159         BEFORE=$(roc_hit)
16160         cancel_lru_locks osc
16161         cat $file >/dev/null
16162         AFTER=$(roc_hit)
16163         if ! let "AFTER - BEFORE == CPAGES"; then
16164                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
16165         else
16166                 log "cache hits:: before: $BEFORE, after: $AFTER"
16167         fi
16168
16169         restore_lustre_params < $p
16170         rm -f $p $file
16171 }
16172 run_test 156 "Verification of tunables"
16173
16174 test_160a() {
16175         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16176         remote_mds_nodsh && skip "remote MDS with nodsh"
16177         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
16178                 skip "Need MDS version at least 2.2.0"
16179
16180         changelog_register || error "changelog_register failed"
16181         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16182         changelog_users $SINGLEMDS | grep -q $cl_user ||
16183                 error "User $cl_user not found in changelog_users"
16184
16185         mkdir_on_mdt0 $DIR/$tdir
16186
16187         # change something
16188         test_mkdir -p $DIR/$tdir/pics/2008/zachy
16189         changelog_clear 0 || error "changelog_clear failed"
16190         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
16191         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
16192         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
16193         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
16194         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
16195         rm $DIR/$tdir/pics/desktop.jpg
16196
16197         echo "verifying changelog mask"
16198         changelog_chmask "-MKDIR"
16199         changelog_chmask "-CLOSE"
16200
16201         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
16202         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
16203
16204         changelog_chmask "+MKDIR"
16205         changelog_chmask "+CLOSE"
16206
16207         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
16208         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
16209
16210         MKDIRS=$(changelog_dump | grep -c "MKDIR")
16211         CLOSES=$(changelog_dump | grep -c "CLOSE")
16212         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
16213         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
16214
16215         # verify contents
16216         echo "verifying target fid"
16217         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
16218         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
16219         [ "$fidc" == "$fidf" ] ||
16220                 error "changelog '$tfile' fid $fidc != file fid $fidf"
16221         echo "verifying parent fid"
16222         # The FID returned from the Changelog may be the directory shard on
16223         # a different MDT, and not the FID returned by path2fid on the parent.
16224         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
16225         # since this is what will matter when recreating this file in the tree.
16226         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
16227         local pathp=$($LFS fid2path $MOUNT "$fidp")
16228         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
16229                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
16230
16231         echo "getting records for $cl_user"
16232         changelog_users $SINGLEMDS
16233         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
16234         local nclr=3
16235         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
16236                 error "changelog_clear failed"
16237         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
16238         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
16239         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
16240                 error "user index expect $user_rec1 + $nclr != $user_rec2"
16241
16242         local min0_rec=$(changelog_users $SINGLEMDS |
16243                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
16244         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
16245                           awk '{ print $1; exit; }')
16246
16247         changelog_dump | tail -n 5
16248         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
16249         [ $first_rec == $((min0_rec + 1)) ] ||
16250                 error "first index should be $min0_rec + 1 not $first_rec"
16251
16252         # LU-3446 changelog index reset on MDT restart
16253         local cur_rec1=$(changelog_users $SINGLEMDS |
16254                          awk '/^current.index:/ { print $NF }')
16255         changelog_clear 0 ||
16256                 error "clear all changelog records for $cl_user failed"
16257         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
16258         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
16259                 error "Fail to start $SINGLEMDS"
16260         local cur_rec2=$(changelog_users $SINGLEMDS |
16261                          awk '/^current.index:/ { print $NF }')
16262         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
16263         [ $cur_rec1 == $cur_rec2 ] ||
16264                 error "current index should be $cur_rec1 not $cur_rec2"
16265
16266         echo "verifying users from this test are deregistered"
16267         changelog_deregister || error "changelog_deregister failed"
16268         changelog_users $SINGLEMDS | grep -q $cl_user &&
16269                 error "User '$cl_user' still in changelog_users"
16270
16271         # lctl get_param -n mdd.*.changelog_users
16272         # current_index: 144
16273         # ID    index (idle seconds)
16274         # cl3   144   (2) mask=<list>
16275         if [ -z "$(changelog_users $SINGLEMDS | grep -v current.index)" ]; then
16276                 # this is the normal case where all users were deregistered
16277                 # make sure no new records are added when no users are present
16278                 local last_rec1=$(changelog_users $SINGLEMDS |
16279                                   awk '/^current.index:/ { print $NF }')
16280                 touch $DIR/$tdir/chloe
16281                 local last_rec2=$(changelog_users $SINGLEMDS |
16282                                   awk '/^current.index:/ { print $NF }')
16283                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
16284                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
16285         else
16286                 # any changelog users must be leftovers from a previous test
16287                 changelog_users $SINGLEMDS
16288                 echo "other changelog users; can't verify off"
16289         fi
16290 }
16291 run_test 160a "changelog sanity"
16292
16293 test_160b() { # LU-3587
16294         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16295         remote_mds_nodsh && skip "remote MDS with nodsh"
16296         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
16297                 skip "Need MDS version at least 2.2.0"
16298
16299         changelog_register || error "changelog_register failed"
16300         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16301         changelog_users $SINGLEMDS | grep -q $cl_user ||
16302                 error "User '$cl_user' not found in changelog_users"
16303
16304         local longname1=$(str_repeat a 255)
16305         local longname2=$(str_repeat b 255)
16306
16307         cd $DIR
16308         echo "creating very long named file"
16309         touch $longname1 || error "create of '$longname1' failed"
16310         echo "renaming very long named file"
16311         mv $longname1 $longname2
16312
16313         changelog_dump | grep RENME | tail -n 5
16314         rm -f $longname2
16315 }
16316 run_test 160b "Verify that very long rename doesn't crash in changelog"
16317
16318 test_160c() {
16319         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16320         remote_mds_nodsh && skip "remote MDS with nodsh"
16321
16322         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
16323                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
16324                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
16325                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
16326
16327         local rc=0
16328
16329         # Registration step
16330         changelog_register || error "changelog_register failed"
16331
16332         rm -rf $DIR/$tdir
16333         mkdir -p $DIR/$tdir
16334         $MCREATE $DIR/$tdir/foo_160c
16335         changelog_chmask "-TRUNC"
16336         $TRUNCATE $DIR/$tdir/foo_160c 200
16337         changelog_chmask "+TRUNC"
16338         $TRUNCATE $DIR/$tdir/foo_160c 199
16339         changelog_dump | tail -n 5
16340         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
16341         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
16342 }
16343 run_test 160c "verify that changelog log catch the truncate event"
16344
16345 test_160d() {
16346         remote_mds_nodsh && skip "remote MDS with nodsh"
16347         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16349         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
16350                 skip "Need MDS version at least 2.7.60"
16351
16352         # Registration step
16353         changelog_register || error "changelog_register failed"
16354
16355         mkdir -p $DIR/$tdir/migrate_dir
16356         changelog_clear 0 || error "changelog_clear failed"
16357
16358         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
16359         changelog_dump | tail -n 5
16360         local migrates=$(changelog_dump | grep -c "MIGRT")
16361         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
16362 }
16363 run_test 160d "verify that changelog log catch the migrate event"
16364
16365 test_160e() {
16366         remote_mds_nodsh && skip "remote MDS with nodsh"
16367
16368         # Create a user
16369         changelog_register || error "changelog_register failed"
16370
16371         local MDT0=$(facet_svc $SINGLEMDS)
16372         local rc
16373
16374         # No user (expect fail)
16375         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister
16376         rc=$?
16377         if [ $rc -eq 0 ]; then
16378                 error "Should fail without user"
16379         elif [ $rc -ne 4 ]; then
16380                 error "changelog_deregister failed with $rc, expect 4(CMD_HELP)"
16381         fi
16382
16383         # Delete a future user (expect fail)
16384         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
16385         rc=$?
16386         if [ $rc -eq 0 ]; then
16387                 error "Deleted non-existant user cl77"
16388         elif [ $rc -ne 2 ]; then
16389                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
16390         fi
16391
16392         # Clear to a bad index (1 billion should be safe)
16393         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
16394         rc=$?
16395
16396         if [ $rc -eq 0 ]; then
16397                 error "Successfully cleared to invalid CL index"
16398         elif [ $rc -ne 22 ]; then
16399                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
16400         fi
16401 }
16402 run_test 160e "changelog negative testing (should return errors)"
16403
16404 test_160f() {
16405         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16406         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16407                 skip "Need MDS version at least 2.10.56"
16408
16409         local mdts=$(comma_list $(mdts_nodes))
16410
16411         # Create a user
16412         changelog_register || error "first changelog_register failed"
16413         changelog_register || error "second changelog_register failed"
16414         local cl_users
16415         declare -A cl_user1
16416         declare -A cl_user2
16417         local user_rec1
16418         local user_rec2
16419         local i
16420
16421         # generate some changelog records to accumulate on each MDT
16422         # use all_char because created files should be evenly distributed
16423         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16424                 error "test_mkdir $tdir failed"
16425         log "$(date +%s): creating first files"
16426         for ((i = 0; i < MDSCOUNT * 2; i++)); do
16427                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT)) ||
16428                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT)) failed"
16429         done
16430
16431         # check changelogs have been generated
16432         local start=$SECONDS
16433         local idle_time=$((MDSCOUNT * 5 + 5))
16434         local nbcl=$(changelog_dump | wc -l)
16435         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16436
16437         for param in "changelog_max_idle_time=$idle_time" \
16438                      "changelog_gc=1" \
16439                      "changelog_min_gc_interval=2" \
16440                      "changelog_min_free_cat_entries=3"; do
16441                 local MDT0=$(facet_svc $SINGLEMDS)
16442                 local var="${param%=*}"
16443                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16444
16445                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16446                 do_nodes $mdts $LCTL set_param mdd.*.$param
16447         done
16448
16449         # force cl_user2 to be idle (1st part), but also cancel the
16450         # cl_user1 records so that it is not evicted later in the test.
16451         local sleep1=$((idle_time / 2))
16452         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
16453         sleep $sleep1
16454
16455         # simulate changelog catalog almost full
16456         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
16457         do_nodes $mdts "$LCTL set_param fail_loc=0x1313 fail_val=3"
16458
16459         for i in $(seq $MDSCOUNT); do
16460                 cl_users=(${CL_USERS[mds$i]})
16461                 cl_user1[mds$i]="${cl_users[0]}"
16462                 cl_user2[mds$i]="${cl_users[1]}"
16463
16464                 [ -n "${cl_user1[mds$i]}" ] ||
16465                         error "mds$i: no user registered"
16466                 [ -n "${cl_user2[mds$i]}" ] ||
16467                         error "mds$i: only ${cl_user2[mds$i]} is registered"
16468
16469                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16470                 [ -n "$user_rec1" ] ||
16471                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16472                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16473                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16474                 [ -n "$user_rec2" ] ||
16475                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16476                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16477                      "$user_rec1 + 2 == $user_rec2"
16478                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16479                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16480                               "$user_rec1 + 2, but is $user_rec2"
16481                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16482                 [ -n "$user_rec2" ] ||
16483                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16484                 [ $user_rec1 == $user_rec2 ] ||
16485                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16486                               "$user_rec1, but is $user_rec2"
16487         done
16488
16489         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
16490         local sleep2=$((idle_time - (SECONDS - start) + 1))
16491         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
16492         sleep $sleep2
16493
16494         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
16495         # cl_user1 should be OK because it recently processed records.
16496         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
16497         for ((i = 0; i < MDSCOUNT * 2; i++)); do
16498                 $LFS mkdir -i $((i%MDSCOUNT)) $DIR/$tdir/d$i.$((i/MDSCOUNT+2))||
16499                         error "create $DIR/$tdir/d$i.$((i/MDSCOUNT+2)) failed"
16500         done
16501
16502         # ensure gc thread is done
16503         for i in $(mdts_nodes); do
16504                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
16505                         error "$i: GC-thread not done"
16506         done
16507
16508         local first_rec
16509         for (( i = 1; i <= MDSCOUNT; i++ )); do
16510                 # check cl_user1 still registered
16511                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16512                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16513                 # check cl_user2 unregistered
16514                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16515                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16516
16517                 # check changelogs are present and starting at $user_rec1 + 1
16518                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16519                 [ -n "$user_rec1" ] ||
16520                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16521                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16522                             awk '{ print $1; exit; }')
16523
16524                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
16525                 [ $((user_rec1 + 1)) == $first_rec ] ||
16526                         error "mds$i: rec $first_rec != $user_rec1 + 1"
16527         done
16528 }
16529 run_test 160f "changelog garbage collect (timestamped users)"
16530
16531 test_160g() {
16532         remote_mds_nodsh && skip "remote MDS with nodsh"
16533         [[ $MDS1_VERSION -ge $(version_code 2.14.55) ]] ||
16534                 skip "Need MDS version at least 2.14.55"
16535
16536         local mdts=$(comma_list $(mdts_nodes))
16537
16538         # Create a user
16539         changelog_register || error "first changelog_register failed"
16540         changelog_register || error "second changelog_register failed"
16541         local cl_users
16542         declare -A cl_user1
16543         declare -A cl_user2
16544         local user_rec1
16545         local user_rec2
16546         local i
16547
16548         # generate some changelog records to accumulate on each MDT
16549         # use all_char because created files should be evenly distributed
16550         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16551                 error "test_mkdir $tdir failed"
16552         for ((i = 0; i < MDSCOUNT; i++)); do
16553                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16554                         error "create $DIR/$tdir/d$i.1 failed"
16555         done
16556
16557         # check changelogs have been generated
16558         local nbcl=$(changelog_dump | wc -l)
16559         (( $nbcl > 0 )) || error "no changelogs found"
16560
16561         # reduce the max_idle_indexes value to make sure we exceed it
16562         for param in "changelog_max_idle_indexes=2" \
16563                      "changelog_gc=1" \
16564                      "changelog_min_gc_interval=2"; do
16565                 local MDT0=$(facet_svc $SINGLEMDS)
16566                 local var="${param%=*}"
16567                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16568
16569                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16570                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
16571                         error "unable to set mdd.*.$param"
16572         done
16573
16574         local start=$SECONDS
16575         for i in $(seq $MDSCOUNT); do
16576                 cl_users=(${CL_USERS[mds$i]})
16577                 cl_user1[mds$i]="${cl_users[0]}"
16578                 cl_user2[mds$i]="${cl_users[1]}"
16579
16580                 [ -n "${cl_user1[mds$i]}" ] ||
16581                         error "mds$i: user1 is not registered"
16582                 [ -n "${cl_user2[mds$i]}" ] ||
16583                         error "mds$i: only ${cl_user1[mds$i]} is registered"
16584
16585                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16586                 [ -n "$user_rec1" ] ||
16587                         error "mds$i: user1 ${cl_user1[mds$i]} not found"
16588                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16589                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16590                 [ -n "$user_rec2" ] ||
16591                         error "mds$i: user1 ${cl_user1[mds$i]} not found (2)"
16592                 echo "mds$i: verifying user1 ${cl_user1[mds$i]} clear: " \
16593                      "$user_rec1 + 2 == $user_rec2"
16594                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16595                         error "mds$i: user1 ${cl_user1[mds$i]} index " \
16596                               "expected $user_rec1 + 2, but is $user_rec2"
16597                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16598                 [ -n "$user_rec2" ] ||
16599                         error "mds$i: user2 ${cl_user2[mds$i]} not found"
16600                 [ $user_rec1 == $user_rec2 ] ||
16601                         error "mds$i: user2 ${cl_user2[mds$i]} index " \
16602                               "expected $user_rec1, but is $user_rec2"
16603         done
16604
16605         # ensure we are past the previous changelog_min_gc_interval set above
16606         local sleep2=$((start + 2 - SECONDS))
16607         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
16608         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
16609         # cl_user1 should be OK because it recently processed records.
16610         for ((i = 0; i < MDSCOUNT; i++)); do
16611                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 ||
16612                         error "create $DIR/$tdir/d$i.3 failed"
16613         done
16614
16615         # ensure gc thread is done
16616         for i in $(mdts_nodes); do
16617                 wait_update $i "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
16618                         error "$i: GC-thread not done"
16619         done
16620
16621         local first_rec
16622         for (( i = 1; i <= MDSCOUNT; i++ )); do
16623                 # check cl_user1 still registered
16624                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16625                         error "mds$i: user1 ${cl_user1[mds$i]} not found (3)"
16626                 # check cl_user2 unregistered
16627                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16628                         error "mds$i: user2 ${cl_user2[mds$i]} is registered"
16629
16630                 # check changelogs are present and starting at $user_rec1 + 1
16631                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16632                 [ -n "$user_rec1" ] ||
16633                         error "mds$i: user1 ${cl_user1[mds$i]} not found (4)"
16634                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16635                             awk '{ print $1; exit; }')
16636
16637                 echo "mds$i: $(date +%s) verify rec $user_rec1+1 == $first_rec"
16638                 [ $((user_rec1 + 1)) == $first_rec ] ||
16639                         error "mds$i: rec $first_rec != $user_rec1 + 1"
16640         done
16641 }
16642 run_test 160g "changelog garbage collect on idle records"
16643
16644 test_160h() {
16645         remote_mds_nodsh && skip "remote MDS with nodsh" && return
16646         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
16647                 skip "Need MDS version at least 2.10.56"
16648
16649         local mdts=$(comma_list $(mdts_nodes))
16650
16651         # Create a user
16652         changelog_register || error "first changelog_register failed"
16653         changelog_register || error "second changelog_register failed"
16654         local cl_users
16655         declare -A cl_user1
16656         declare -A cl_user2
16657         local user_rec1
16658         local user_rec2
16659         local i
16660
16661         # generate some changelog records to accumulate on each MDT
16662         # use all_char because created files should be evenly distributed
16663         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16664                 error "test_mkdir $tdir failed"
16665         for ((i = 0; i < MDSCOUNT; i++)); do
16666                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16667                         error "create $DIR/$tdir/d$i.1 failed"
16668         done
16669
16670         # check changelogs have been generated
16671         local nbcl=$(changelog_dump | wc -l)
16672         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16673
16674         for param in "changelog_max_idle_time=10" \
16675                      "changelog_gc=1" \
16676                      "changelog_min_gc_interval=2"; do
16677                 local MDT0=$(facet_svc $SINGLEMDS)
16678                 local var="${param%=*}"
16679                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
16680
16681                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
16682                 do_nodes $mdts $LCTL set_param mdd.*.$param
16683         done
16684
16685         # force cl_user2 to be idle (1st part)
16686         sleep 9
16687
16688         for i in $(seq $MDSCOUNT); do
16689                 cl_users=(${CL_USERS[mds$i]})
16690                 cl_user1[mds$i]="${cl_users[0]}"
16691                 cl_user2[mds$i]="${cl_users[1]}"
16692
16693                 [ -n "${cl_user1[mds$i]}" ] ||
16694                         error "mds$i: no user registered"
16695                 [ -n "${cl_user2[mds$i]}" ] ||
16696                         error "mds$i: only ${cl_user2[mds$i]} is registered"
16697
16698                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16699                 [ -n "$user_rec1" ] ||
16700                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16701                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
16702                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16703                 [ -n "$user_rec2" ] ||
16704                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16705                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
16706                      "$user_rec1 + 2 == $user_rec2"
16707                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
16708                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
16709                               "$user_rec1 + 2, but is $user_rec2"
16710                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
16711                 [ -n "$user_rec2" ] ||
16712                         error "mds$i: User ${cl_user2[mds$i]} not registered"
16713                 [ $user_rec1 == $user_rec2 ] ||
16714                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
16715                               "$user_rec1, but is $user_rec2"
16716         done
16717
16718         # force cl_user2 to be idle (2nd part) and to reach
16719         # changelog_max_idle_time
16720         sleep 2
16721
16722         # force each GC-thread start and block then
16723         # one per MDT/MDD, set fail_val accordingly
16724         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
16725         do_nodes $mdts $LCTL set_param fail_loc=0x1316
16726
16727         # generate more changelogs to trigger fail_loc
16728         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16729                 error "create $DIR/$tdir/${tfile}bis failed"
16730
16731         # stop MDT to stop GC-thread, should be done in back-ground as it will
16732         # block waiting for the thread to be released and exit
16733         declare -A stop_pids
16734         for i in $(seq $MDSCOUNT); do
16735                 stop mds$i &
16736                 stop_pids[mds$i]=$!
16737         done
16738
16739         for i in $(mdts_nodes); do
16740                 local facet
16741                 local nb=0
16742                 local facets=$(facets_up_on_host $i)
16743
16744                 for facet in ${facets//,/ }; do
16745                         if [[ $facet == mds* ]]; then
16746                                 nb=$((nb + 1))
16747                         fi
16748                 done
16749                 # ensure each MDS's gc threads are still present and all in "R"
16750                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
16751                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
16752                         error "$i: expected $nb GC-thread"
16753                 wait_update $i \
16754                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
16755                         "R" 20 ||
16756                         error "$i: GC-thread not found in R-state"
16757                 # check umounts of each MDT on MDS have reached kthread_stop()
16758                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
16759                         error "$i: expected $nb umount"
16760                 wait_update $i \
16761                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
16762                         error "$i: umount not found in D-state"
16763         done
16764
16765         # release all GC-threads
16766         do_nodes $mdts $LCTL set_param fail_loc=0
16767
16768         # wait for MDT stop to complete
16769         for i in $(seq $MDSCOUNT); do
16770                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
16771         done
16772
16773         # XXX
16774         # may try to check if any orphan changelog records are present
16775         # via ldiskfs/zfs and llog_reader...
16776
16777         # re-start/mount MDTs
16778         for i in $(seq $MDSCOUNT); do
16779                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
16780                         error "Fail to start mds$i"
16781         done
16782
16783         local first_rec
16784         for i in $(seq $MDSCOUNT); do
16785                 # check cl_user1 still registered
16786                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
16787                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16788                 # check cl_user2 unregistered
16789                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
16790                         error "mds$i: User ${cl_user2[mds$i]} still registered"
16791
16792                 # check changelogs are present and starting at $user_rec1 + 1
16793                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
16794                 [ -n "$user_rec1" ] ||
16795                         error "mds$i: User ${cl_user1[mds$i]} not registered"
16796                 first_rec=$($LFS changelog $(facet_svc mds$i) |
16797                             awk '{ print $1; exit; }')
16798
16799                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
16800                 [ $((user_rec1 + 1)) == $first_rec ] ||
16801                         error "mds$i: first index should be $user_rec1 + 1, " \
16802                               "but is $first_rec"
16803         done
16804 }
16805 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
16806               "during mount"
16807
16808 test_160i() {
16809
16810         local mdts=$(comma_list $(mdts_nodes))
16811
16812         changelog_register || error "first changelog_register failed"
16813
16814         # generate some changelog records to accumulate on each MDT
16815         # use all_char because created files should be evenly distributed
16816         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16817                 error "test_mkdir $tdir failed"
16818         for ((i = 0; i < MDSCOUNT; i++)); do
16819                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16820                         error "create $DIR/$tdir/d$i.1 failed"
16821         done
16822
16823         # check changelogs have been generated
16824         local nbcl=$(changelog_dump | wc -l)
16825         [[ $nbcl -eq 0 ]] && error "no changelogs found"
16826
16827         # simulate race between register and unregister
16828         # XXX as fail_loc is set per-MDS, with DNE configs the race
16829         # simulation will only occur for one MDT per MDS and for the
16830         # others the normal race scenario will take place
16831         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
16832         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
16833         do_nodes $mdts $LCTL set_param fail_val=1
16834
16835         # unregister 1st user
16836         changelog_deregister &
16837         local pid1=$!
16838         # wait some time for deregister work to reach race rdv
16839         sleep 2
16840         # register 2nd user
16841         changelog_register || error "2nd user register failed"
16842
16843         wait $pid1 || error "1st user deregister failed"
16844
16845         local i
16846         local last_rec
16847         declare -A LAST_REC
16848         for i in $(seq $MDSCOUNT); do
16849                 if changelog_users mds$i | grep "^cl"; then
16850                         # make sure new records are added with one user present
16851                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
16852                                           awk '/^current.index:/ { print $NF }')
16853                 else
16854                         error "mds$i has no user registered"
16855                 fi
16856         done
16857
16858         # generate more changelog records to accumulate on each MDT
16859         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
16860                 error "create $DIR/$tdir/${tfile}bis failed"
16861
16862         for i in $(seq $MDSCOUNT); do
16863                 last_rec=$(changelog_users $SINGLEMDS |
16864                            awk '/^current.index:/ { print $NF }')
16865                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
16866                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
16867                         error "changelogs are off on mds$i"
16868         done
16869 }
16870 run_test 160i "changelog user register/unregister race"
16871
16872 test_160j() {
16873         remote_mds_nodsh && skip "remote MDS with nodsh"
16874         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
16875                 skip "Need MDS version at least 2.12.56"
16876
16877         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
16878         stack_trap "umount $MOUNT2" EXIT
16879
16880         changelog_register || error "first changelog_register failed"
16881         stack_trap "changelog_deregister" EXIT
16882
16883         # generate some changelog
16884         # use all_char because created files should be evenly distributed
16885         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
16886                 error "mkdir $tdir failed"
16887         for ((i = 0; i < MDSCOUNT; i++)); do
16888                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
16889                         error "create $DIR/$tdir/d$i.1 failed"
16890         done
16891
16892         # open the changelog device
16893         exec 3>/dev/changelog-$FSNAME-MDT0000
16894         stack_trap "exec 3>&-" EXIT
16895         exec 4</dev/changelog-$FSNAME-MDT0000
16896         stack_trap "exec 4<&-" EXIT
16897
16898         # umount the first lustre mount
16899         umount $MOUNT
16900         stack_trap "mount_client $MOUNT" EXIT
16901
16902         # read changelog, which may or may not fail, but should not crash
16903         cat <&4 >/dev/null
16904
16905         # clear changelog
16906         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16907         changelog_users $SINGLEMDS | grep -q $cl_user ||
16908                 error "User $cl_user not found in changelog_users"
16909
16910         printf 'clear:'$cl_user':0' >&3
16911 }
16912 run_test 160j "client can be umounted while its chanangelog is being used"
16913
16914 test_160k() {
16915         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16916         remote_mds_nodsh && skip "remote MDS with nodsh"
16917
16918         mkdir -p $DIR/$tdir/1/1
16919
16920         changelog_register || error "changelog_register failed"
16921         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16922
16923         changelog_users $SINGLEMDS | grep -q $cl_user ||
16924                 error "User '$cl_user' not found in changelog_users"
16925 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
16926         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
16927         rmdir $DIR/$tdir/1/1 & sleep 1
16928         mkdir $DIR/$tdir/2
16929         touch $DIR/$tdir/2/2
16930         rm -rf $DIR/$tdir/2
16931
16932         wait
16933         sleep 4
16934
16935         changelog_dump | grep rmdir || error "rmdir not recorded"
16936 }
16937 run_test 160k "Verify that changelog records are not lost"
16938
16939 # Verifies that a file passed as a parameter has recently had an operation
16940 # performed on it that has generated an MTIME changelog which contains the
16941 # correct parent FID. As files might reside on a different MDT from the
16942 # parent directory in DNE configurations, the FIDs are translated to paths
16943 # before being compared, which should be identical
16944 compare_mtime_changelog() {
16945         local file="${1}"
16946         local mdtidx
16947         local mtime
16948         local cl_fid
16949         local pdir
16950         local dir
16951
16952         mdtidx=$($LFS getstripe --mdt-index $file)
16953         mdtidx=$(printf "%04x" $mdtidx)
16954
16955         # Obtain the parent FID from the MTIME changelog
16956         mtime=$($LFS changelog $FSNAME-MDT$mdtidx | tail -n 1 | grep MTIME)
16957         [ -z "$mtime" ] && error "MTIME changelog not recorded"
16958
16959         cl_fid=$(sed -e 's/.* p=//' -e 's/ .*//' <<<$mtime)
16960         [ -z "$cl_fid" ] && error "parent FID not present"
16961
16962         # Verify that the path for the parent FID is the same as the path for
16963         # the test directory
16964         pdir=$($LFS fid2path $MOUNT "$cl_fid")
16965
16966         dir=$(dirname $1)
16967
16968         [[ "${pdir%/}" == "$dir" ]] ||
16969                 error "MTIME changelog parent FID is wrong, expected $dir, got $pdir"
16970 }
16971
16972 test_160l() {
16973         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16974
16975         remote_mds_nodsh && skip "remote MDS with nodsh"
16976         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
16977                 skip "Need MDS version at least 2.13.55"
16978
16979         local cl_user
16980
16981         changelog_register || error "changelog_register failed"
16982         cl_user="${CL_USERS[$SINGLEMDS]%% *}"
16983
16984         changelog_users $SINGLEMDS | grep -q $cl_user ||
16985                 error "User '$cl_user' not found in changelog_users"
16986
16987         # Clear some types so that MTIME changelogs are generated
16988         changelog_chmask "-CREAT"
16989         changelog_chmask "-CLOSE"
16990
16991         test_mkdir $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
16992
16993         # Test CL_MTIME during setattr
16994         touch $DIR/$tdir/$tfile
16995         compare_mtime_changelog $DIR/$tdir/$tfile
16996
16997         # Test CL_MTIME during close
16998         $MULTIOP $DIR/$tdir/${tfile}_2 O_2w4096c || error "multiop failed"
16999         compare_mtime_changelog $DIR/$tdir/${tfile}_2
17000 }
17001 run_test 160l "Verify that MTIME changelog records contain the parent FID"
17002
17003 test_160m() {
17004         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17005         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17006                 skip "Need MDS version at least 2.14.51"
17007         local cl_users
17008         local cl_user1
17009         local cl_user2
17010         local pid1
17011
17012         # Create a user
17013         changelog_register || error "first changelog_register failed"
17014         changelog_register || error "second changelog_register failed"
17015
17016         cl_users=(${CL_USERS[mds1]})
17017         cl_user1="${cl_users[0]}"
17018         cl_user2="${cl_users[1]}"
17019         # generate some changelog records to accumulate on MDT0
17020         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17021         createmany -m $DIR/$tdir/$tfile 50 ||
17022                 error "create $DIR/$tdir/$tfile failed"
17023         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
17024         rm -f $DIR/$tdir
17025
17026         # check changelogs have been generated
17027         local nbcl=$(changelog_dump | wc -l)
17028         [[ $nbcl -eq 0 ]] && error "no changelogs found"
17029
17030 #define OBD_FAIL_MDS_CHANGELOG_RACE      0x15f
17031         do_facet mds1 $LCTL set_param fail_loc=0x8000015f fail_val=0
17032
17033         __changelog_clear mds1 $cl_user1 +10
17034         __changelog_clear mds1 $cl_user2 0 &
17035         pid1=$!
17036         sleep 2
17037         __changelog_clear mds1 $cl_user1 0 ||
17038                 error "fail to cancel record for $cl_user1"
17039         wait $pid1
17040         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
17041 }
17042 run_test 160m "Changelog clear race"
17043
17044 test_160n() {
17045         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17046         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17047                 skip "Need MDS version at least 2.14.51"
17048         local cl_users
17049         local cl_user1
17050         local cl_user2
17051         local pid1
17052         local first_rec
17053         local last_rec=0
17054
17055         # Create a user
17056         changelog_register || error "first changelog_register failed"
17057
17058         cl_users=(${CL_USERS[mds1]})
17059         cl_user1="${cl_users[0]}"
17060
17061         # generate some changelog records to accumulate on MDT0
17062         test_mkdir -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17063         first_rec=$(changelog_users $SINGLEMDS |
17064                         awk '/^current.index:/ { print $NF }')
17065         while (( last_rec < (( first_rec + 65000)) )); do
17066                 createmany -m $DIR/$tdir/$tfile 10000 ||
17067                         error "create $DIR/$tdir/$tfile failed"
17068
17069                 for i in $(seq 0 10000); do
17070                         mrename $DIR/$tdir/$tfile$i $DIR/$tdir/$tfile-new$i \
17071                                 > /dev/null
17072                 done
17073
17074                 unlinkmany $DIR/$tdir/$tfile-new 10000 ||
17075                         error "unlinkmany failed unlink"
17076                 last_rec=$(changelog_users $SINGLEMDS |
17077                         awk '/^current.index:/ { print $NF }')
17078                 echo last record $last_rec
17079                 (( last_rec == 0 )) && error "no changelog found"
17080         done
17081
17082 #define OBD_FAIL_MDS_CHANGELOG_DEL       0x16c
17083         do_facet mds1 $LCTL set_param fail_loc=0x8000016c fail_val=0
17084
17085         __changelog_clear mds1 $cl_user1 0 &
17086         pid1=$!
17087         sleep 2
17088         __changelog_clear mds1 $cl_user1 0 ||
17089                 error "fail to cancel record for $cl_user1"
17090         wait $pid1
17091         [[ $? -eq 0 ]] || error "fail to cancel record for $cl_user2"
17092 }
17093 run_test 160n "Changelog destroy race"
17094
17095 test_160o() {
17096         local mdt="$(facet_svc $SINGLEMDS)"
17097
17098         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
17099         remote_mds_nodsh && skip "remote MDS with nodsh"
17100         [ $MDS1_VERSION -ge $(version_code 2.14.52) ] ||
17101                 skip "Need MDS version at least 2.14.52"
17102
17103         changelog_register --user test_160o -m unlnk+close+open ||
17104                 error "changelog_register failed"
17105
17106         do_facet $SINGLEMDS $LCTL --device $mdt \
17107                                 changelog_register -u "Tt3_-#" &&
17108                 error "bad symbols in name should fail"
17109
17110         do_facet $SINGLEMDS $LCTL --device $mdt \
17111                                 changelog_register -u test_160o &&
17112                 error "the same name registration should fail"
17113
17114         do_facet $SINGLEMDS $LCTL --device $mdt \
17115                         changelog_register -u test_160toolongname &&
17116                 error "too long name registration should fail"
17117
17118         changelog_chmask "MARK+HSM"
17119         lctl get_param mdd.*.changelog*mask
17120         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
17121         changelog_users $SINGLEMDS | grep -q $cl_user ||
17122                 error "User $cl_user not found in changelog_users"
17123         #verify username
17124         echo $cl_user | grep -q test_160o ||
17125                 error "User $cl_user has no specific name 'test160o'"
17126
17127         # change something
17128         changelog_clear 0 || error "changelog_clear failed"
17129         # generate some changelog records to accumulate on MDT0
17130         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17131         touch $DIR/$tdir/$tfile                 # open 1
17132
17133         OPENS=$(changelog_dump | grep -c "OPEN")
17134         [[ $OPENS -eq 1 ]] || error "OPEN changelog mask count $OPENS != 1"
17135
17136         # must be no MKDIR it wasn't set as user mask
17137         MKDIR=$(changelog_dump | grep -c "MKDIR")
17138         [[ $MKDIR -eq 0 ]] || error "MKDIR changelog mask found $MKDIR > 0"
17139
17140         oldmask=$(do_facet $SINGLEMDS $LCTL get_param \
17141                                 mdd.$mdt.changelog_current_mask -n)
17142         # register maskless user
17143         changelog_register || error "changelog_register failed"
17144         # effective mask should be not changed because it is not minimal
17145         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17146                                 mdd.$mdt.changelog_current_mask -n)
17147         [[ $mask == $oldmask ]] || error "mask was changed: $mask vs $oldmask"
17148         # set server mask to minimal value
17149         changelog_chmask "MARK"
17150         # check effective mask again, should be treated as DEFMASK now
17151         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17152                                 mdd.$mdt.changelog_current_mask -n)
17153         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
17154
17155         do_facet $SINGLEMDS $LCTL --device $mdt \
17156                                 changelog_deregister -u test_160o ||
17157                 error "cannot deregister by name"
17158 }
17159 run_test 160o "changelog user name and mask"
17160
17161 test_160p() {
17162         remote_mds_nodsh && skip "remote MDS with nodsh" && return
17163         [[ $MDS1_VERSION -ge $(version_code 2.14.51) ]] ||
17164                 skip "Need MDS version at least 2.14.51"
17165         [[ "$mds1_FSTYPE" == "ldiskfs" ]] || skip "ldiskfs only test"
17166         local cl_users
17167         local cl_user1
17168         local entry_count
17169
17170         # Create a user
17171         changelog_register || error "first changelog_register failed"
17172
17173         cl_users=(${CL_USERS[mds1]})
17174         cl_user1="${cl_users[0]}"
17175
17176         test_mkdir -p -i0 -c1 $DIR/$tdir || error "test_mkdir $tdir failed"
17177         createmany -m $DIR/$tdir/$tfile 50 ||
17178                 error "create $DIR/$tdir/$tfile failed"
17179         unlinkmany $DIR/$tdir/$tfile 50 || error "unlinkmany failed"
17180         rm -rf $DIR/$tdir
17181
17182         # check changelogs have been generated
17183         entry_count=$(changelog_dump | wc -l)
17184         ((entry_count != 0)) || error "no changelog entries found"
17185
17186         # remove changelog_users and check that orphan entries are removed
17187         stop mds1
17188         local dev=$(mdsdevname 1)
17189         do_facet mds1 "$DEBUGFS -w -R 'rm changelog_users' $dev"
17190         start mds1 $dev $MDS_MOUNT_OPTS || error "cannot start mds1"
17191         entry_count=$(changelog_dump | wc -l)
17192         ((entry_count == 0)) ||
17193                 error "found $entry_count changelog entries, expected none"
17194 }
17195 run_test 160p "Changelog orphan cleanup with no users"
17196
17197 test_160q() {
17198         local mdt="$(facet_svc $SINGLEMDS)"
17199         local clu
17200
17201         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
17202         remote_mds_nodsh && skip "remote MDS with nodsh"
17203         [ $MDS1_VERSION -ge $(version_code 2.14.54) ] ||
17204                 skip "Need MDS version at least 2.14.54"
17205
17206         # set server mask to minimal value like server init does
17207         changelog_chmask "MARK"
17208         clu=$(do_facet $SINGLEMDS $LCTL --device $mdt changelog_register -n) ||
17209                 error "changelog_register failed"
17210         # check effective mask again, should be treated as DEFMASK now
17211         mask=$(do_facet $SINGLEMDS $LCTL get_param \
17212                                 mdd.$mdt.changelog_current_mask -n)
17213         do_facet $SINGLEMDS $LCTL --device $mdt changelog_deregister $clu ||
17214                 error "changelog_deregister failed"
17215         [[ $mask == *"HLINK"* ]] || error "mask is not DEFMASK as expected"
17216 }
17217 run_test 160q "changelog effective mask is DEFMASK if not set"
17218
17219 test_160s() {
17220         remote_mds_nodsh && skip "remote MDS with nodsh"
17221         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
17222                 skip "Need MDS version at least 2.14.55"
17223
17224         local mdts=$(comma_list $(mdts_nodes))
17225
17226         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
17227         do_nodes $mdts $LCTL set_param fail_loc=0x1314 \
17228                                        fail_val=$((24 * 3600 * 10))
17229
17230         # Create a user which is 10 days old
17231         changelog_register || error "first changelog_register failed"
17232         local cl_users
17233         declare -A cl_user1
17234         local i
17235
17236         # generate some changelog records to accumulate on each MDT
17237         # use all_char because created files should be evenly distributed
17238         test_mkdir -c $MDSCOUNT -H all_char $DIR/$tdir ||
17239                 error "test_mkdir $tdir failed"
17240         for ((i = 0; i < MDSCOUNT; i++)); do
17241                 $LFS mkdir -i $i $DIR/$tdir/d$i.1 $DIR/$tdir/d$i.2 ||
17242                         error "create $DIR/$tdir/d$i.1 failed"
17243         done
17244
17245         # check changelogs have been generated
17246         local nbcl=$(changelog_dump | wc -l)
17247         (( nbcl > 0 )) || error "no changelogs found"
17248
17249         # reduce the max_idle_indexes value to make sure we exceed it
17250         for param in "changelog_max_idle_indexes=2097446912" \
17251                      "changelog_max_idle_time=2592000" \
17252                      "changelog_gc=1" \
17253                      "changelog_min_gc_interval=2"; do
17254                 local MDT0=$(facet_svc $SINGLEMDS)
17255                 local var="${param%=*}"
17256                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17257
17258                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
17259                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
17260                         error "unable to set mdd.*.$param"
17261         done
17262
17263         local start=$SECONDS
17264         for i in $(seq $MDSCOUNT); do
17265                 cl_users=(${CL_USERS[mds$i]})
17266                 cl_user1[mds$i]="${cl_users[0]}"
17267
17268                 [[ -n "${cl_user1[mds$i]}" ]] ||
17269                         error "mds$i: no user registered"
17270         done
17271
17272         #define OBD_FAIL_MDS_CHANGELOG_IDX_PUMP   0x16d
17273         do_nodes $mdts $LCTL set_param fail_loc=0x16d fail_val=500000000
17274
17275         # ensure we are past the previous changelog_min_gc_interval set above
17276         local sleep2=$((start + 2 - SECONDS))
17277         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17278
17279         # Generate one more changelog to trigger GC
17280         for ((i = 0; i < MDSCOUNT; i++)); do
17281                 $LFS mkdir -i $i $DIR/$tdir/d$i.3 $DIR/$tdir/d$i.4 ||
17282                         error "create $DIR/$tdir/d$i.3 failed"
17283         done
17284
17285         # ensure gc thread is done
17286         for node in $(mdts_nodes); do
17287                 wait_update $node "pgrep chlg_gc_thread" "" 20 ||
17288                         error "$node: GC-thread not done"
17289         done
17290
17291         do_nodes $mdts $LCTL set_param fail_loc=0
17292
17293         for (( i = 1; i <= MDSCOUNT; i++ )); do
17294                 # check cl_user1 is purged
17295                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" &&
17296                         error "mds$i: User ${cl_user1[mds$i]} is registered"
17297         done
17298         return 0
17299 }
17300 run_test 160s "changelog garbage collect on idle records * time"
17301
17302 test_160t() {
17303         remote_mds_nodsh && skip "remote MDS with nodsh"
17304         (( $MDS1_VERSION >= $(version_code 2.15.50) )) ||
17305                 skip "Need MDS version at least 2.15.50"
17306
17307         local MDT0=$(facet_svc $SINGLEMDS)
17308         local cl_users
17309         local cl_user1
17310         local cl_user2
17311         local start
17312
17313         changelog_register --user user1 -m all ||
17314                 error "user1 failed to register"
17315
17316         mkdir_on_mdt0 $DIR/$tdir
17317         # create default overstripe to maximize changelog size
17318         $LFS setstripe  -C 8 $DIR/$tdir || error "setstripe failed"
17319         createmany -o $DIR/$tdir/u1_ 2000 || error "createmany for user1 failed"
17320         llog_size1=$(do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size)
17321
17322         # user2 consumes less records so less space
17323         changelog_register --user user2 || error "user2 failed to register"
17324         createmany -o $DIR/$tdir/u2_ 500 || error "createmany for user2 failed"
17325         llog_size2=$(do_facet mds1 $LCTL get_param -n mdd.$MDT0.changelog_size)
17326
17327         # check changelogs have been generated
17328         local nbcl=$(changelog_dump | wc -l)
17329         (( nbcl > 0 )) || error "no changelogs found"
17330
17331         # reduce the changelog_min_gc_interval to force check
17332         for param in "changelog_gc=1" "changelog_min_gc_interval=2"; do
17333                 local var="${param%=*}"
17334                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
17335
17336                 stack_trap "do_facet mds1 $LCTL set_param mdd.$MDT0.$var=$old"
17337                 do_facet mds1 $LCTL set_param mdd.$MDT0.$param ||
17338                         error "unable to set mdd.*.$param"
17339         done
17340
17341         start=$SECONDS
17342         cl_users=(${CL_USERS[mds1]})
17343         cl_user1="${cl_users[0]}"
17344         cl_user2="${cl_users[1]}"
17345
17346         [[ -n $cl_user1 ]] ||
17347                 error "mds1: user #1 isn't registered"
17348         [[ -n $cl_user2 ]] ||
17349                 error "mds1: user #2 isn't registered"
17350
17351         # ensure we are past the previous changelog_min_gc_interval set above
17352         local sleep2=$((start + 2 - SECONDS))
17353         (( sleep2 > 0 )) && echo "sleep $sleep2 for interval" && sleep $sleep2
17354
17355         #define OBD_FAIL_MDS_CHANGELOG_ENOSPC 0x018c
17356         do_facet mds1 $LCTL set_param fail_loc=0x018c \
17357                         fail_val=$(((llog_size1 + llog_size2) / 2))
17358
17359         # Generate more changelog to trigger GC
17360         createmany -o $DIR/$tdir/u3_ 4 ||
17361                 error "create failed for more files"
17362
17363         # ensure gc thread is done
17364         wait_update_facet mds1 "pgrep chlg_gc_thread" "" 20 ||
17365                 error "mds1: GC-thread not done"
17366
17367         do_facet mds1 $LCTL set_param fail_loc=0
17368
17369         # check cl_user1 is purged
17370         changelog_users mds1 | grep -q "$cl_user1" &&
17371                 error "User $cl_user1 is registered"
17372         # check cl_user2 is not purged
17373         changelog_users mds1 | grep -q "$cl_user2" ||
17374                 error "User $cl_user2 is not registered"
17375 }
17376 run_test 160t "changelog garbage collect on lack of space"
17377
17378 test_161a() {
17379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17380
17381         test_mkdir -c1 $DIR/$tdir
17382         cp /etc/hosts $DIR/$tdir/$tfile
17383         test_mkdir -c1 $DIR/$tdir/foo1
17384         test_mkdir -c1 $DIR/$tdir/foo2
17385         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
17386         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
17387         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
17388         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
17389         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
17390         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
17391                 $LFS fid2path $DIR $FID
17392                 error "bad link ea"
17393         fi
17394         # middle
17395         rm $DIR/$tdir/foo2/zachary
17396         # last
17397         rm $DIR/$tdir/foo2/thor
17398         # first
17399         rm $DIR/$tdir/$tfile
17400         # rename
17401         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
17402         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
17403                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
17404         rm $DIR/$tdir/foo2/maggie
17405
17406         # overflow the EA
17407         local longname=$tfile.avg_len_is_thirty_two_
17408         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
17409                 error_noexit 'failed to unlink many hardlinks'" EXIT
17410         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
17411                 error "failed to hardlink many files"
17412         links=$($LFS fid2path $DIR $FID | wc -l)
17413         echo -n "${links}/1000 links in link EA"
17414         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
17415 }
17416 run_test 161a "link ea sanity"
17417
17418 test_161b() {
17419         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17420         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
17421
17422         local MDTIDX=1
17423         local remote_dir=$DIR/$tdir/remote_dir
17424
17425         mkdir -p $DIR/$tdir
17426         $LFS mkdir -i $MDTIDX $remote_dir ||
17427                 error "create remote directory failed"
17428
17429         cp /etc/hosts $remote_dir/$tfile
17430         mkdir -p $remote_dir/foo1
17431         mkdir -p $remote_dir/foo2
17432         ln $remote_dir/$tfile $remote_dir/foo1/sofia
17433         ln $remote_dir/$tfile $remote_dir/foo2/zachary
17434         ln $remote_dir/$tfile $remote_dir/foo1/luna
17435         ln $remote_dir/$tfile $remote_dir/foo2/thor
17436
17437         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
17438                      tr -d ']')
17439         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
17440                 $LFS fid2path $DIR $FID
17441                 error "bad link ea"
17442         fi
17443         # middle
17444         rm $remote_dir/foo2/zachary
17445         # last
17446         rm $remote_dir/foo2/thor
17447         # first
17448         rm $remote_dir/$tfile
17449         # rename
17450         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
17451         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
17452         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
17453                 $LFS fid2path $DIR $FID
17454                 error "bad link rename"
17455         fi
17456         rm $remote_dir/foo2/maggie
17457
17458         # overflow the EA
17459         local longname=filename_avg_len_is_thirty_two_
17460         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
17461                 error "failed to hardlink many files"
17462         links=$($LFS fid2path $DIR $FID | wc -l)
17463         echo -n "${links}/1000 links in link EA"
17464         [[ ${links} -gt 60 ]] ||
17465                 error "expected at least 60 links in link EA"
17466         unlinkmany $remote_dir/foo2/$longname 1000 ||
17467         error "failed to unlink many hardlinks"
17468 }
17469 run_test 161b "link ea sanity under remote directory"
17470
17471 test_161c() {
17472         remote_mds_nodsh && skip "remote MDS with nodsh"
17473         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17474         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
17475                 skip "Need MDS version at least 2.1.5"
17476
17477         # define CLF_RENAME_LAST 0x0001
17478         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
17479         changelog_register || error "changelog_register failed"
17480
17481         rm -rf $DIR/$tdir
17482         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
17483         touch $DIR/$tdir/foo_161c
17484         touch $DIR/$tdir/bar_161c
17485         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
17486         changelog_dump | grep RENME | tail -n 5
17487         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
17488         changelog_clear 0 || error "changelog_clear failed"
17489         if [ x$flags != "x0x1" ]; then
17490                 error "flag $flags is not 0x1"
17491         fi
17492
17493         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
17494         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
17495         touch $DIR/$tdir/foo_161c
17496         touch $DIR/$tdir/bar_161c
17497         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
17498         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
17499         changelog_dump | grep RENME | tail -n 5
17500         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
17501         changelog_clear 0 || error "changelog_clear failed"
17502         if [ x$flags != "x0x0" ]; then
17503                 error "flag $flags is not 0x0"
17504         fi
17505         echo "rename overwrite a target having nlink > 1," \
17506                 "changelog record has flags of $flags"
17507
17508         # rename doesn't overwrite a target (changelog flag 0x0)
17509         touch $DIR/$tdir/foo_161c
17510         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
17511         changelog_dump | grep RENME | tail -n 5
17512         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
17513         changelog_clear 0 || error "changelog_clear failed"
17514         if [ x$flags != "x0x0" ]; then
17515                 error "flag $flags is not 0x0"
17516         fi
17517         echo "rename doesn't overwrite a target," \
17518                 "changelog record has flags of $flags"
17519
17520         # define CLF_UNLINK_LAST 0x0001
17521         # unlink a file having nlink = 1 (changelog flag 0x1)
17522         rm -f $DIR/$tdir/foo2_161c
17523         changelog_dump | grep UNLNK | tail -n 5
17524         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
17525         changelog_clear 0 || error "changelog_clear failed"
17526         if [ x$flags != "x0x1" ]; then
17527                 error "flag $flags is not 0x1"
17528         fi
17529         echo "unlink a file having nlink = 1," \
17530                 "changelog record has flags of $flags"
17531
17532         # unlink a file having nlink > 1 (changelog flag 0x0)
17533         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
17534         rm -f $DIR/$tdir/foobar_161c
17535         changelog_dump | grep UNLNK | tail -n 5
17536         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
17537         changelog_clear 0 || error "changelog_clear failed"
17538         if [ x$flags != "x0x0" ]; then
17539                 error "flag $flags is not 0x0"
17540         fi
17541         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
17542 }
17543 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
17544
17545 test_161d() {
17546         remote_mds_nodsh && skip "remote MDS with nodsh"
17547         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
17548
17549         local pid
17550         local fid
17551
17552         changelog_register || error "changelog_register failed"
17553
17554         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
17555         # interfer with $MOUNT/.lustre/fid/ access
17556         mkdir $DIR/$tdir
17557         [[ $? -eq 0 ]] || error "mkdir failed"
17558
17559         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
17560         $LCTL set_param fail_loc=0x8000140c
17561         # 5s pause
17562         $LCTL set_param fail_val=5
17563
17564         # create file
17565         echo foofoo > $DIR/$tdir/$tfile &
17566         pid=$!
17567
17568         # wait for create to be delayed
17569         sleep 2
17570
17571         ps -p $pid
17572         [[ $? -eq 0 ]] || error "create should be blocked"
17573
17574         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
17575         stack_trap "rm -f $tempfile"
17576         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
17577         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
17578         # some delay may occur during ChangeLog publishing and file read just
17579         # above, that could allow file write to happen finally
17580         [[ -s $tempfile ]] && echo "file should be empty"
17581
17582         $LCTL set_param fail_loc=0
17583
17584         wait $pid
17585         [[ $? -eq 0 ]] || error "create failed"
17586 }
17587 run_test 161d "create with concurrent .lustre/fid access"
17588
17589 check_path() {
17590         local expected="$1"
17591         shift
17592         local fid="$2"
17593
17594         local path
17595         path=$($LFS fid2path "$@")
17596         local rc=$?
17597
17598         if [ $rc -ne 0 ]; then
17599                 error "path looked up of '$expected' failed: rc=$rc"
17600         elif [ "$path" != "$expected" ]; then
17601                 error "path looked up '$path' instead of '$expected'"
17602         else
17603                 echo "FID '$fid' resolves to path '$path' as expected"
17604         fi
17605 }
17606
17607 test_162a() { # was test_162
17608         test_mkdir -p -c1 $DIR/$tdir/d2
17609         touch $DIR/$tdir/d2/$tfile
17610         touch $DIR/$tdir/d2/x1
17611         touch $DIR/$tdir/d2/x2
17612         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
17613         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
17614         # regular file
17615         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
17616         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
17617
17618         # softlink
17619         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
17620         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
17621         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
17622
17623         # softlink to wrong file
17624         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
17625         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
17626         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
17627
17628         # hardlink
17629         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
17630         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
17631         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
17632         # fid2path dir/fsname should both work
17633         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
17634         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
17635
17636         # hardlink count: check that there are 2 links
17637         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
17638         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
17639
17640         # hardlink indexing: remove the first link
17641         rm $DIR/$tdir/d2/p/q/r/hlink
17642         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
17643 }
17644 run_test 162a "path lookup sanity"
17645
17646 test_162b() {
17647         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17648         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17649
17650         mkdir $DIR/$tdir
17651         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
17652                                 error "create striped dir failed"
17653
17654         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
17655                                         tail -n 1 | awk '{print $2}')
17656         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
17657
17658         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
17659         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
17660
17661         # regular file
17662         for ((i=0;i<5;i++)); do
17663                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
17664                         error "get fid for f$i failed"
17665                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
17666
17667                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
17668                         error "get fid for d$i failed"
17669                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
17670         done
17671
17672         return 0
17673 }
17674 run_test 162b "striped directory path lookup sanity"
17675
17676 # LU-4239: Verify fid2path works with paths 100 or more directories deep
17677 test_162c() {
17678         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
17679                 skip "Need MDS version at least 2.7.51"
17680
17681         local lpath=$tdir.local
17682         local rpath=$tdir.remote
17683
17684         test_mkdir $DIR/$lpath
17685         test_mkdir $DIR/$rpath
17686
17687         for ((i = 0; i <= 101; i++)); do
17688                 lpath="$lpath/$i"
17689                 mkdir $DIR/$lpath
17690                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
17691                         error "get fid for local directory $DIR/$lpath failed"
17692                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
17693
17694                 rpath="$rpath/$i"
17695                 test_mkdir $DIR/$rpath
17696                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
17697                         error "get fid for remote directory $DIR/$rpath failed"
17698                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
17699         done
17700
17701         return 0
17702 }
17703 run_test 162c "fid2path works with paths 100 or more directories deep"
17704
17705 oalr_event_count() {
17706         local event="${1}"
17707         local trace="${2}"
17708
17709         awk -v name="${FSNAME}-OST0000" \
17710             -v event="${event}" \
17711             '$1 == "TRACE" && $2 == event && $3 == name' \
17712             "${trace}" |
17713         wc -l
17714 }
17715
17716 oalr_expect_event_count() {
17717         local event="${1}"
17718         local trace="${2}"
17719         local expect="${3}"
17720         local count
17721
17722         count=$(oalr_event_count "${event}" "${trace}")
17723         if ((count == expect)); then
17724                 return 0
17725         fi
17726
17727         error_noexit "${event} event count was '${count}', expected ${expect}"
17728         cat "${trace}" >&2
17729         exit 1
17730 }
17731
17732 cleanup_165() {
17733         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
17734         stop ost1
17735         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
17736 }
17737
17738 setup_165() {
17739         sync # Flush previous IOs so we can count log entries.
17740         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
17741         stack_trap cleanup_165 EXIT
17742 }
17743
17744 test_165a() {
17745         local trace="/tmp/${tfile}.trace"
17746         local rc
17747         local count
17748
17749         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17750                 skip "OFD access log unsupported"
17751
17752         setup_165
17753         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17754         sleep 5
17755
17756         do_facet ost1 ofd_access_log_reader --list
17757         stop ost1
17758
17759         do_facet ost1 killall -TERM ofd_access_log_reader
17760         wait
17761         rc=$?
17762
17763         if ((rc != 0)); then
17764                 error "ofd_access_log_reader exited with rc = '${rc}'"
17765         fi
17766
17767         # Parse trace file for discovery events:
17768         oalr_expect_event_count alr_log_add "${trace}" 1
17769         oalr_expect_event_count alr_log_eof "${trace}" 1
17770         oalr_expect_event_count alr_log_free "${trace}" 1
17771 }
17772 run_test 165a "ofd access log discovery"
17773
17774 test_165b() {
17775         local trace="/tmp/${tfile}.trace"
17776         local file="${DIR}/${tfile}"
17777         local pfid1
17778         local pfid2
17779         local -a entry
17780         local rc
17781         local count
17782         local size
17783         local flags
17784
17785         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17786                 skip "OFD access log unsupported"
17787
17788         setup_165
17789         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17790         sleep 5
17791
17792         do_facet ost1 ofd_access_log_reader --list
17793
17794         lfs setstripe -c 1 -i 0 "${file}"
17795         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17796                 error "cannot create '${file}'"
17797
17798         sleep 5
17799         do_facet ost1 killall -TERM ofd_access_log_reader
17800         wait
17801         rc=$?
17802
17803         if ((rc != 0)); then
17804                 error "ofd_access_log_reader exited with rc = '${rc}'"
17805         fi
17806
17807         oalr_expect_event_count alr_log_entry "${trace}" 1
17808
17809         pfid1=$($LFS path2fid "${file}")
17810
17811         # 1     2             3   4    5     6   7    8    9     10
17812         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
17813         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17814
17815         echo "entry = '${entry[*]}'" >&2
17816
17817         pfid2=${entry[4]}
17818         if [[ "${pfid1}" != "${pfid2}" ]]; then
17819                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17820         fi
17821
17822         size=${entry[8]}
17823         if ((size != 1048576)); then
17824                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
17825         fi
17826
17827         flags=${entry[10]}
17828         if [[ "${flags}" != "w" ]]; then
17829                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
17830         fi
17831
17832         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17833         sleep 5
17834
17835         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c ||
17836                 error "cannot read '${file}'"
17837         sleep 5
17838
17839         do_facet ost1 killall -TERM ofd_access_log_reader
17840         wait
17841         rc=$?
17842
17843         if ((rc != 0)); then
17844                 error "ofd_access_log_reader exited with rc = '${rc}'"
17845         fi
17846
17847         oalr_expect_event_count alr_log_entry "${trace}" 1
17848
17849         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
17850         echo "entry = '${entry[*]}'" >&2
17851
17852         pfid2=${entry[4]}
17853         if [[ "${pfid1}" != "${pfid2}" ]]; then
17854                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
17855         fi
17856
17857         size=${entry[8]}
17858         if ((size != 524288)); then
17859                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
17860         fi
17861
17862         flags=${entry[10]}
17863         if [[ "${flags}" != "r" ]]; then
17864                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
17865         fi
17866 }
17867 run_test 165b "ofd access log entries are produced and consumed"
17868
17869 test_165c() {
17870         local trace="/tmp/${tfile}.trace"
17871         local file="${DIR}/${tdir}/${tfile}"
17872
17873         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17874                 skip "OFD access log unsupported"
17875
17876         test_mkdir "${DIR}/${tdir}"
17877
17878         setup_165
17879         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
17880         sleep 5
17881
17882         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
17883
17884         # 4096 / 64 = 64. Create twice as many entries.
17885         for ((i = 0; i < 128; i++)); do
17886                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c ||
17887                         error "cannot create file"
17888         done
17889
17890         sync
17891
17892         do_facet ost1 killall -TERM ofd_access_log_reader
17893         wait
17894         rc=$?
17895         if ((rc != 0)); then
17896                 error "ofd_access_log_reader exited with rc = '${rc}'"
17897         fi
17898
17899         unlinkmany  "${file}-%d" 128
17900 }
17901 run_test 165c "full ofd access logs do not block IOs"
17902
17903 oal_get_read_count() {
17904         local stats="$1"
17905
17906         # STATS lustre-OST0001 alr_read_count 1
17907
17908         do_facet ost1 cat "${stats}" |
17909         awk '$1 == "STATS" && $3 == "alr_read_count" { count = $4; }
17910              END { print count; }'
17911 }
17912
17913 oal_expect_read_count() {
17914         local stats="$1"
17915         local count
17916         local expect="$2"
17917
17918         # Ask ofd_access_log_reader to write stats.
17919         do_facet ost1 killall -USR1 ofd_access_log_reader
17920
17921         # Allow some time for things to happen.
17922         sleep 1
17923
17924         count=$(oal_get_read_count "${stats}")
17925         if ((count == expect)); then
17926                 return 0
17927         fi
17928
17929         error_noexit "bad read count, got ${count}, expected ${expect}"
17930         do_facet ost1 cat "${stats}" >&2
17931         exit 1
17932 }
17933
17934 test_165d() {
17935         local stats="/tmp/${tfile}.stats"
17936         local file="${DIR}/${tdir}/${tfile}"
17937         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
17938
17939         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
17940                 skip "OFD access log unsupported"
17941
17942         test_mkdir "${DIR}/${tdir}"
17943
17944         setup_165
17945         do_facet ost1 ofd_access_log_reader --stats="${stats}" &
17946         sleep 5
17947
17948         lfs setstripe -c 1 -i 0 "${file}"
17949
17950         do_facet ost1 lctl set_param "${param}=rw"
17951         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17952                 error "cannot create '${file}'"
17953         oal_expect_read_count "${stats}" 1
17954
17955         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17956                 error "cannot read '${file}'"
17957         oal_expect_read_count "${stats}" 2
17958
17959         do_facet ost1 lctl set_param "${param}=r"
17960         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17961                 error "cannot create '${file}'"
17962         oal_expect_read_count "${stats}" 2
17963
17964         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17965                 error "cannot read '${file}'"
17966         oal_expect_read_count "${stats}" 3
17967
17968         do_facet ost1 lctl set_param "${param}=w"
17969         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17970                 error "cannot create '${file}'"
17971         oal_expect_read_count "${stats}" 4
17972
17973         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17974                 error "cannot read '${file}'"
17975         oal_expect_read_count "${stats}" 4
17976
17977         do_facet ost1 lctl set_param "${param}=0"
17978         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c ||
17979                 error "cannot create '${file}'"
17980         oal_expect_read_count "${stats}" 4
17981
17982         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c ||
17983                 error "cannot read '${file}'"
17984         oal_expect_read_count "${stats}" 4
17985
17986         do_facet ost1 killall -TERM ofd_access_log_reader
17987         wait
17988         rc=$?
17989         if ((rc != 0)); then
17990                 error "ofd_access_log_reader exited with rc = '${rc}'"
17991         fi
17992 }
17993 run_test 165d "ofd_access_log mask works"
17994
17995 test_165e() {
17996         local stats="/tmp/${tfile}.stats"
17997         local file0="${DIR}/${tdir}-0/${tfile}"
17998         local file1="${DIR}/${tdir}-1/${tfile}"
17999
18000         (( $OST1_VERSION >= $(version_code 2.13.54) )) ||
18001                 skip "OFD access log unsupported"
18002
18003         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
18004
18005         test_mkdir -c 1 -i 0 "${DIR}/${tdir}-0"
18006         test_mkdir -c 1 -i 1 "${DIR}/${tdir}-1"
18007
18008         lfs setstripe -c 1 -i 0 "${file0}"
18009         lfs setstripe -c 1 -i 0 "${file1}"
18010
18011         setup_165
18012         do_facet ost1 ofd_access_log_reader -I 1 --stats="${stats}" &
18013         sleep 5
18014
18015         $MULTIOP "${file0}" oO_CREAT:O_WRONLY:w512c ||
18016                 error "cannot create '${file0}'"
18017         sync
18018         oal_expect_read_count "${stats}" 0
18019
18020         $MULTIOP "${file1}" oO_CREAT:O_WRONLY:w512c ||
18021                 error "cannot create '${file1}'"
18022         sync
18023         oal_expect_read_count "${stats}" 1
18024
18025         do_facet ost1 killall -TERM ofd_access_log_reader
18026         wait
18027         rc=$?
18028         if ((rc != 0)); then
18029                 error "ofd_access_log_reader exited with rc = '${rc}'"
18030         fi
18031 }
18032 run_test 165e "ofd_access_log MDT index filter works"
18033
18034 test_165f() {
18035         local trace="/tmp/${tfile}.trace"
18036         local rc
18037         local count
18038
18039         setup_165
18040         do_facet ost1 timeout 60 ofd_access_log_reader \
18041                 --exit-on-close --debug=- --trace=- > "${trace}" &
18042         sleep 5
18043         stop ost1
18044
18045         wait
18046         rc=$?
18047
18048         if ((rc != 0)); then
18049                 error_noexit "ofd_access_log_reader exited with rc = '${rc}'"
18050                 cat "${trace}"
18051                 exit 1
18052         fi
18053 }
18054 run_test 165f "ofd_access_log_reader --exit-on-close works"
18055
18056 test_169() {
18057         # do directio so as not to populate the page cache
18058         log "creating a 10 Mb file"
18059         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c ||
18060                 error "multiop failed while creating a file"
18061         log "starting reads"
18062         dd if=$DIR/$tfile of=/dev/null bs=4096 &
18063         log "truncating the file"
18064         $MULTIOP $DIR/$tfile oO_TRUNC:c ||
18065                 error "multiop failed while truncating the file"
18066         log "killing dd"
18067         kill %+ || true # reads might have finished
18068         echo "wait until dd is finished"
18069         wait
18070         log "removing the temporary file"
18071         rm -rf $DIR/$tfile || error "tmp file removal failed"
18072 }
18073 run_test 169 "parallel read and truncate should not deadlock"
18074
18075 test_170() {
18076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18077
18078         $LCTL clear     # bug 18514
18079         $LCTL debug_daemon start $TMP/${tfile}_log_good
18080         touch $DIR/$tfile
18081         $LCTL debug_daemon stop
18082         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
18083                 error "sed failed to read log_good"
18084
18085         $LCTL debug_daemon start $TMP/${tfile}_log_good
18086         rm -rf $DIR/$tfile
18087         $LCTL debug_daemon stop
18088
18089         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
18090                error "lctl df log_bad failed"
18091
18092         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
18093         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
18094
18095         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
18096         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
18097
18098         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
18099                 error "bad_line good_line1 good_line2 are empty"
18100
18101         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
18102         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
18103         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
18104
18105         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
18106         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
18107         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
18108
18109         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
18110                 error "bad_line_new good_line_new are empty"
18111
18112         local expected_good=$((good_line1 + good_line2*2))
18113
18114         rm -f $TMP/${tfile}*
18115         # LU-231, short malformed line may not be counted into bad lines
18116         if [ $bad_line -ne $bad_line_new ] &&
18117                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
18118                 error "expected $bad_line bad lines, but got $bad_line_new"
18119                 return 1
18120         fi
18121
18122         if [ $expected_good -ne $good_line_new ]; then
18123                 error "expected $expected_good good lines, but got $good_line_new"
18124                 return 2
18125         fi
18126         true
18127 }
18128 run_test 170 "test lctl df to handle corrupted log ====================="
18129
18130 test_171() { # bug20592
18131         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18132
18133         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
18134         $LCTL set_param fail_loc=0x50e
18135         $LCTL set_param fail_val=3000
18136         multiop_bg_pause $DIR/$tfile O_s || true
18137         local MULTIPID=$!
18138         kill -USR1 $MULTIPID
18139         # cause log dump
18140         sleep 3
18141         wait $MULTIPID
18142         if dmesg | grep "recursive fault"; then
18143                 error "caught a recursive fault"
18144         fi
18145         $LCTL set_param fail_loc=0
18146         true
18147 }
18148 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
18149
18150 test_172() {
18151
18152         #define OBD_FAIL_OBD_CLEANUP  0x60e
18153         $LCTL set_param fail_loc=0x60e
18154         umount $MOUNT || error "umount $MOUNT failed"
18155         stack_trap "mount_client $MOUNT"
18156
18157         (( $($LCTL dl | egrep -c " osc | lov | lmv | mdc ") > 0 )) ||
18158                 error "no client OBDs are remained"
18159
18160         $LCTL dl | while read devno state type name foo; do
18161                 case $type in
18162                 lov|osc|lmv|mdc)
18163                         $LCTL --device $name cleanup
18164                         $LCTL --device $name detach
18165                         ;;
18166                 *)
18167                         # skip server devices
18168                         ;;
18169                 esac
18170         done
18171
18172         if (( $($LCTL dl | egrep -c " osc | lov | lmv | mdc ") > 0 )); then
18173                 $LCTL dl | egrep " osc | lov | lmv | mdc "
18174                 error "some client OBDs are still remained"
18175         fi
18176
18177 }
18178 run_test 172 "manual device removal with lctl cleanup/detach ======"
18179
18180 # it would be good to share it with obdfilter-survey/iokit-libecho code
18181 setup_obdecho_osc () {
18182         local rc=0
18183         local ost_nid=$1
18184         local obdfilter_name=$2
18185         echo "Creating new osc for $obdfilter_name on $ost_nid"
18186         # make sure we can find loopback nid
18187         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
18188
18189         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
18190                            ${obdfilter_name}_osc_UUID || rc=2; }
18191         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
18192                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
18193         return $rc
18194 }
18195
18196 cleanup_obdecho_osc () {
18197         local obdfilter_name=$1
18198         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
18199         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
18200         return 0
18201 }
18202
18203 obdecho_test() {
18204         local OBD=$1
18205         local node=$2
18206         local pages=${3:-64}
18207         local rc=0
18208         local id
18209
18210         local count=10
18211         local obd_size=$(get_obd_size $node $OBD)
18212         local page_size=$(get_page_size $node)
18213         if [[ -n "$obd_size" ]]; then
18214                 local new_count=$((obd_size / (pages * page_size / 1024)))
18215                 [[ $new_count -ge $count ]] || count=$new_count
18216         fi
18217
18218         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
18219         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
18220                            rc=2; }
18221         if [ $rc -eq 0 ]; then
18222             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
18223             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
18224         fi
18225         echo "New object id is $id"
18226         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
18227                            rc=4; }
18228         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
18229                            "test_brw $count w v $pages $id" || rc=4; }
18230         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
18231                            rc=4; }
18232         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
18233                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
18234         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
18235                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
18236         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
18237         return $rc
18238 }
18239
18240 test_180a() {
18241         skip "obdecho on osc is no longer supported"
18242 }
18243 run_test 180a "test obdecho on osc"
18244
18245 test_180b() {
18246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18247         remote_ost_nodsh && skip "remote OST with nodsh"
18248
18249         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
18250                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
18251                 error "failed to load module obdecho"
18252
18253         local target=$(do_facet ost1 $LCTL dl |
18254                        awk '/obdfilter/ { print $4; exit; }')
18255
18256         if [ -n "$target" ]; then
18257                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
18258         else
18259                 do_facet ost1 $LCTL dl
18260                 error "there is no obdfilter target on ost1"
18261         fi
18262 }
18263 run_test 180b "test obdecho directly on obdfilter"
18264
18265 test_180c() { # LU-2598
18266         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18267         remote_ost_nodsh && skip "remote OST with nodsh"
18268         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
18269                 skip "Need MDS version at least 2.4.0"
18270
18271         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
18272                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
18273                 error "failed to load module obdecho"
18274
18275         local target=$(do_facet ost1 $LCTL dl |
18276                        awk '/obdfilter/ { print $4; exit; }')
18277
18278         if [ -n "$target" ]; then
18279                 local pages=16384 # 64MB bulk I/O RPC size
18280
18281                 obdecho_test "$target" ost1 "$pages" ||
18282                         error "obdecho_test with pages=$pages failed with $?"
18283         else
18284                 do_facet ost1 $LCTL dl
18285                 error "there is no obdfilter target on ost1"
18286         fi
18287 }
18288 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
18289
18290 test_181() { # bug 22177
18291         test_mkdir $DIR/$tdir
18292         # create enough files to index the directory
18293         createmany -o $DIR/$tdir/foobar 4000
18294         # print attributes for debug purpose
18295         lsattr -d .
18296         # open dir
18297         multiop_bg_pause $DIR/$tdir D_Sc || return 1
18298         MULTIPID=$!
18299         # remove the files & current working dir
18300         unlinkmany $DIR/$tdir/foobar 4000
18301         rmdir $DIR/$tdir
18302         kill -USR1 $MULTIPID
18303         wait $MULTIPID
18304         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
18305         return 0
18306 }
18307 run_test 181 "Test open-unlinked dir ========================"
18308
18309 test_182a() {
18310         local fcount=1000
18311         local tcount=10
18312
18313         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
18314
18315         $LCTL set_param mdc.*.rpc_stats=clear
18316
18317         for (( i = 0; i < $tcount; i++ )) ; do
18318                 mkdir $DIR/$tdir/$i
18319         done
18320
18321         for (( i = 0; i < $tcount; i++ )) ; do
18322                 createmany -o $DIR/$tdir/$i/f- $fcount &
18323         done
18324         wait
18325
18326         for (( i = 0; i < $tcount; i++ )) ; do
18327                 unlinkmany $DIR/$tdir/$i/f- $fcount &
18328         done
18329         wait
18330
18331         $LCTL get_param mdc.*.rpc_stats
18332
18333         rm -rf $DIR/$tdir
18334 }
18335 run_test 182a "Test parallel modify metadata operations from mdc"
18336
18337 test_182b() {
18338         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18339         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
18340         local dcount=1000
18341         local tcount=10
18342         local stime
18343         local etime
18344         local delta
18345
18346         do_facet mds1 $LCTL list_param \
18347                 osp.$FSNAME-MDT*-osp-MDT*.rpc_stats ||
18348                 skip "MDS lacks parallel RPC handling"
18349
18350         $LFS mkdir -i 0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18351
18352         rpc_count=$(do_facet mds1 $LCTL get_param -n \
18353                     osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight)
18354
18355         stime=$(date +%s)
18356         createmany -i 0 -d $DIR/$tdir/t- $tcount
18357
18358         for (( i = 0; i < $tcount; i++ )) ; do
18359                 createmany -i 0 -d $DIR/$tdir/t-$i/d- 0 $dcount &
18360         done
18361         wait
18362         etime=$(date +%s)
18363         delta=$((etime - stime))
18364         echo "Time for file creation $delta sec for $rpc_count parallel RPCs"
18365
18366         stime=$(date +%s)
18367         for (( i = 0; i < $tcount; i++ )) ; do
18368                 unlinkmany -d $DIR/$tdir/$i/d- $dcount &
18369         done
18370         wait
18371         etime=$(date +%s)
18372         delta=$((etime - stime))
18373         echo "Time for file removal $delta sec for $rpc_count parallel RPCs"
18374
18375         rm -rf $DIR/$tdir
18376
18377         $LFS mkdir -i 0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18378
18379         do_facet mds1 $LCTL set_param osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight=1
18380
18381         stime=$(date +%s)
18382         createmany -i 0 -d $DIR/$tdir/t- $tcount
18383
18384         for (( i = 0; i < $tcount; i++ )) ; do
18385                 createmany -i 0 -d $DIR/$tdir/t-$i/d- 0 $dcount &
18386         done
18387         wait
18388         etime=$(date +%s)
18389         delta=$((etime - stime))
18390         echo "Time for file creation $delta sec for 1 RPC sent at a time"
18391
18392         stime=$(date +%s)
18393         for (( i = 0; i < $tcount; i++ )) ; do
18394                 unlinkmany -d $DIR/$tdir/t-$i/d- $dcount &
18395         done
18396         wait
18397         etime=$(date +%s)
18398         delta=$((etime - stime))
18399         echo "Time for file removal $delta sec for 1 RPC sent at a time"
18400
18401         do_facet mds1 $LCTL set_param osp.$FSNAME-MDT0001-osp-MDT0000.max_mod_rpcs_in_flight=$rpc_count
18402 }
18403 run_test 182b "Test parallel modify metadata operations from osp"
18404
18405 test_183() { # LU-2275
18406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18407         remote_mds_nodsh && skip "remote MDS with nodsh"
18408         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
18409                 skip "Need MDS version at least 2.3.56"
18410
18411         mkdir_on_mdt0 $DIR/$tdir || error "creating dir $DIR/$tdir"
18412         echo aaa > $DIR/$tdir/$tfile
18413
18414 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
18415         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
18416
18417         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
18418         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
18419
18420         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18421
18422         # Flush negative dentry cache
18423         touch $DIR/$tdir/$tfile
18424
18425         # We are not checking for any leaked references here, they'll
18426         # become evident next time we do cleanup with module unload.
18427         rm -rf $DIR/$tdir
18428 }
18429 run_test 183 "No crash or request leak in case of strange dispositions ========"
18430
18431 # test suite 184 is for LU-2016, LU-2017
18432 test_184a() {
18433         check_swap_layouts_support
18434
18435         dir0=$DIR/$tdir/$testnum
18436         test_mkdir -p -c1 $dir0
18437         ref1=/etc/passwd
18438         ref2=/etc/group
18439         file1=$dir0/f1
18440         file2=$dir0/f2
18441         $LFS setstripe -c1 $file1
18442         cp $ref1 $file1
18443         $LFS setstripe -c2 $file2
18444         cp $ref2 $file2
18445         gen1=$($LFS getstripe -g $file1)
18446         gen2=$($LFS getstripe -g $file2)
18447
18448         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
18449         gen=$($LFS getstripe -g $file1)
18450         [[ $gen1 != $gen ]] ||
18451                 error "Layout generation on $file1 does not change"
18452         gen=$($LFS getstripe -g $file2)
18453         [[ $gen2 != $gen ]] ||
18454                 error "Layout generation on $file2 does not change"
18455
18456         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
18457         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
18458
18459         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
18460 }
18461 run_test 184a "Basic layout swap"
18462
18463 test_184b() {
18464         check_swap_layouts_support
18465
18466         dir0=$DIR/$tdir/$testnum
18467         mkdir -p $dir0 || error "creating dir $dir0"
18468         file1=$dir0/f1
18469         file2=$dir0/f2
18470         file3=$dir0/f3
18471         dir1=$dir0/d1
18472         dir2=$dir0/d2
18473         mkdir $dir1 $dir2
18474         $LFS setstripe -c1 $file1
18475         $LFS setstripe -c2 $file2
18476         $LFS setstripe -c1 $file3
18477         chown $RUNAS_ID $file3
18478         gen1=$($LFS getstripe -g $file1)
18479         gen2=$($LFS getstripe -g $file2)
18480
18481         $LFS swap_layouts $dir1 $dir2 &&
18482                 error "swap of directories layouts should fail"
18483         $LFS swap_layouts $dir1 $file1 &&
18484                 error "swap of directory and file layouts should fail"
18485         $RUNAS $LFS swap_layouts $file1 $file2 &&
18486                 error "swap of file we cannot write should fail"
18487         $LFS swap_layouts $file1 $file3 &&
18488                 error "swap of file with different owner should fail"
18489         /bin/true # to clear error code
18490 }
18491 run_test 184b "Forbidden layout swap (will generate errors)"
18492
18493 test_184c() {
18494         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
18495         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
18496         check_swap_layouts_support
18497         check_swap_layout_no_dom $DIR
18498
18499         local dir0=$DIR/$tdir/$testnum
18500         mkdir -p $dir0 || error "creating dir $dir0"
18501
18502         local ref1=$dir0/ref1
18503         local ref2=$dir0/ref2
18504         local file1=$dir0/file1
18505         local file2=$dir0/file2
18506         # create a file large enough for the concurrent test
18507         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
18508         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
18509         echo "ref file size: ref1($(stat -c %s $ref1))," \
18510              "ref2($(stat -c %s $ref2))"
18511
18512         cp $ref2 $file2
18513         dd if=$ref1 of=$file1 bs=16k &
18514         local DD_PID=$!
18515
18516         # Make sure dd starts to copy file, but wait at most 5 seconds
18517         local loops=0
18518         while [ ! -s $file1 -a $((loops++)) -lt 50 ]; do sleep 0.1; done
18519
18520         $LFS swap_layouts $file1 $file2
18521         local rc=$?
18522         wait $DD_PID
18523         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
18524         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
18525
18526         # how many bytes copied before swapping layout
18527         local copied=$(stat -c %s $file2)
18528         local remaining=$(stat -c %s $ref1)
18529         remaining=$((remaining - copied))
18530         echo "Copied $copied bytes before swapping layout..."
18531
18532         cmp -n $copied $file1 $ref2 | grep differ &&
18533                 error "Content mismatch [0, $copied) of ref2 and file1"
18534         cmp -n $copied $file2 $ref1 ||
18535                 error "Content mismatch [0, $copied) of ref1 and file2"
18536         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
18537                 error "Content mismatch [$copied, EOF) of ref1 and file1"
18538
18539         # clean up
18540         rm -f $ref1 $ref2 $file1 $file2
18541 }
18542 run_test 184c "Concurrent write and layout swap"
18543
18544 test_184d() {
18545         check_swap_layouts_support
18546         check_swap_layout_no_dom $DIR
18547         [ -z "$(which getfattr 2>/dev/null)" ] &&
18548                 skip_env "no getfattr command"
18549
18550         local file1=$DIR/$tdir/$tfile-1
18551         local file2=$DIR/$tdir/$tfile-2
18552         local file3=$DIR/$tdir/$tfile-3
18553         local lovea1
18554         local lovea2
18555
18556         mkdir -p $DIR/$tdir
18557         touch $file1 || error "create $file1 failed"
18558         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
18559                 error "create $file2 failed"
18560         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
18561                 error "create $file3 failed"
18562         lovea1=$(get_layout_param $file1)
18563
18564         $LFS swap_layouts $file2 $file3 ||
18565                 error "swap $file2 $file3 layouts failed"
18566         $LFS swap_layouts $file1 $file2 ||
18567                 error "swap $file1 $file2 layouts failed"
18568
18569         lovea2=$(get_layout_param $file2)
18570         echo "$lovea1"
18571         echo "$lovea2"
18572         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
18573
18574         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
18575         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
18576 }
18577 run_test 184d "allow stripeless layouts swap"
18578
18579 test_184e() {
18580         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
18581                 skip "Need MDS version at least 2.6.94"
18582         check_swap_layouts_support
18583         check_swap_layout_no_dom $DIR
18584         [ -z "$(which getfattr 2>/dev/null)" ] &&
18585                 skip_env "no getfattr command"
18586
18587         local file1=$DIR/$tdir/$tfile-1
18588         local file2=$DIR/$tdir/$tfile-2
18589         local file3=$DIR/$tdir/$tfile-3
18590         local lovea
18591
18592         mkdir -p $DIR/$tdir
18593         touch $file1 || error "create $file1 failed"
18594         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
18595                 error "create $file2 failed"
18596         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
18597                 error "create $file3 failed"
18598
18599         $LFS swap_layouts $file1 $file2 ||
18600                 error "swap $file1 $file2 layouts failed"
18601
18602         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
18603         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
18604
18605         echo 123 > $file1 || error "Should be able to write into $file1"
18606
18607         $LFS swap_layouts $file1 $file3 ||
18608                 error "swap $file1 $file3 layouts failed"
18609
18610         echo 123 > $file1 || error "Should be able to write into $file1"
18611
18612         rm -rf $file1 $file2 $file3
18613 }
18614 run_test 184e "Recreate layout after stripeless layout swaps"
18615
18616 test_184f() {
18617         # Create a file with name longer than sizeof(struct stat) ==
18618         # 144 to see if we can get chars from the file name to appear
18619         # in the returned striping. Note that 'f' == 0x66.
18620         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
18621
18622         mkdir -p $DIR/$tdir
18623         mcreate $DIR/$tdir/$file
18624         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
18625                 error "IOC_MDC_GETFILEINFO returned garbage striping"
18626         fi
18627 }
18628 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
18629
18630 test_185() { # LU-2441
18631         # LU-3553 - no volatile file support in old servers
18632         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
18633                 skip "Need MDS version at least 2.3.60"
18634
18635         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
18636         touch $DIR/$tdir/spoo
18637         local mtime1=$(stat -c "%Y" $DIR/$tdir)
18638         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
18639                 error "cannot create/write a volatile file"
18640         [ "$FILESET" == "" ] &&
18641         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
18642                 error "FID is still valid after close"
18643
18644         multiop_bg_pause $DIR/$tdir vVw4096_c
18645         local multi_pid=$!
18646
18647         local OLD_IFS=$IFS
18648         IFS=":"
18649         local fidv=($fid)
18650         IFS=$OLD_IFS
18651         # assume that the next FID for this client is sequential, since stdout
18652         # is unfortunately eaten by multiop_bg_pause
18653         local n=$((${fidv[1]} + 1))
18654         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
18655         if [ "$FILESET" == "" ]; then
18656                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
18657                         error "FID is missing before close"
18658         fi
18659         kill -USR1 $multi_pid
18660         # 1 second delay, so if mtime change we will see it
18661         sleep 1
18662         local mtime2=$(stat -c "%Y" $DIR/$tdir)
18663         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
18664 }
18665 run_test 185 "Volatile file support"
18666
18667 function create_check_volatile() {
18668         local idx=$1
18669         local tgt
18670
18671         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
18672         local PID=$!
18673         sleep 1
18674         local FID=$(cat /tmp/${tfile}.fid)
18675         [ "$FID" == "" ] && error "can't get FID for volatile"
18676         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
18677         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
18678         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
18679         kill -USR1 $PID
18680         wait
18681         sleep 1
18682         cancel_lru_locks mdc # flush opencache
18683         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
18684         return 0
18685 }
18686
18687 test_185a(){
18688         # LU-12516 - volatile creation via .lustre
18689         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
18690                 skip "Need MDS version at least 2.3.55"
18691
18692         create_check_volatile 0
18693         [ $MDSCOUNT -lt 2 ] && return 0
18694
18695         # DNE case
18696         create_check_volatile 1
18697
18698         return 0
18699 }
18700 run_test 185a "Volatile file creation in .lustre/fid/"
18701
18702 test_187a() {
18703         remote_mds_nodsh && skip "remote MDS with nodsh"
18704         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
18705                 skip "Need MDS version at least 2.3.0"
18706
18707         local dir0=$DIR/$tdir/$testnum
18708         mkdir -p $dir0 || error "creating dir $dir0"
18709
18710         local file=$dir0/file1
18711         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
18712         local dv1=$($LFS data_version $file)
18713         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
18714         local dv2=$($LFS data_version $file)
18715         [[ $dv1 != $dv2 ]] ||
18716                 error "data version did not change on write $dv1 == $dv2"
18717
18718         # clean up
18719         rm -f $file1
18720 }
18721 run_test 187a "Test data version change"
18722
18723 test_187b() {
18724         remote_mds_nodsh && skip "remote MDS with nodsh"
18725         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
18726                 skip "Need MDS version at least 2.3.0"
18727
18728         local dir0=$DIR/$tdir/$testnum
18729         mkdir -p $dir0 || error "creating dir $dir0"
18730
18731         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
18732         [[ ${DV[0]} != ${DV[1]} ]] ||
18733                 error "data version did not change on write"\
18734                       " ${DV[0]} == ${DV[1]}"
18735
18736         # clean up
18737         rm -f $file1
18738 }
18739 run_test 187b "Test data version change on volatile file"
18740
18741 test_200() {
18742         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18743         remote_mgs_nodsh && skip "remote MGS with nodsh"
18744         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
18745
18746         local POOL=${POOL:-cea1}
18747         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
18748         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
18749         # Pool OST targets
18750         local first_ost=0
18751         local last_ost=$(($OSTCOUNT - 1))
18752         local ost_step=2
18753         local ost_list=$(seq $first_ost $ost_step $last_ost)
18754         local ost_range="$first_ost $last_ost $ost_step"
18755         local test_path=$POOL_ROOT/$POOL_DIR_NAME
18756         local file_dir=$POOL_ROOT/file_tst
18757         local subdir=$test_path/subdir
18758         local rc=0
18759
18760         while : ; do
18761                 # former test_200a test_200b
18762                 pool_add $POOL                          || { rc=$? ; break; }
18763                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
18764                 # former test_200c test_200d
18765                 mkdir -p $test_path
18766                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
18767                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
18768                 mkdir -p $subdir
18769                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
18770                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
18771                                                         || { rc=$? ; break; }
18772                 # former test_200e test_200f
18773                 local files=$((OSTCOUNT*3))
18774                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
18775                                                         || { rc=$? ; break; }
18776                 pool_create_files $POOL $file_dir $files "$ost_list" \
18777                                                         || { rc=$? ; break; }
18778                 # former test_200g test_200h
18779                 pool_lfs_df $POOL                       || { rc=$? ; break; }
18780                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
18781
18782                 # former test_201a test_201b test_201c
18783                 pool_remove_first_target $POOL          || { rc=$? ; break; }
18784
18785                 local f=$test_path/$tfile
18786                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
18787                 pool_remove $POOL $f                    || { rc=$? ; break; }
18788                 break
18789         done
18790
18791         destroy_test_pools
18792
18793         return $rc
18794 }
18795 run_test 200 "OST pools"
18796
18797 # usage: default_attr <count | size | offset>
18798 default_attr() {
18799         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
18800 }
18801
18802 # usage: check_default_stripe_attr
18803 check_default_stripe_attr() {
18804         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
18805         case $1 in
18806         --stripe-count|-c)
18807                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
18808         --stripe-size|-S)
18809                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
18810         --stripe-index|-i)
18811                 EXPECTED=-1;;
18812         *)
18813                 error "unknown getstripe attr '$1'"
18814         esac
18815
18816         [ $ACTUAL == $EXPECTED ] ||
18817                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
18818 }
18819
18820 test_204a() {
18821         test_mkdir $DIR/$tdir
18822         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
18823
18824         check_default_stripe_attr --stripe-count
18825         check_default_stripe_attr --stripe-size
18826         check_default_stripe_attr --stripe-index
18827 }
18828 run_test 204a "Print default stripe attributes"
18829
18830 test_204b() {
18831         test_mkdir $DIR/$tdir
18832         $LFS setstripe --stripe-count 1 $DIR/$tdir
18833
18834         check_default_stripe_attr --stripe-size
18835         check_default_stripe_attr --stripe-index
18836 }
18837 run_test 204b "Print default stripe size and offset"
18838
18839 test_204c() {
18840         test_mkdir $DIR/$tdir
18841         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18842
18843         check_default_stripe_attr --stripe-count
18844         check_default_stripe_attr --stripe-index
18845 }
18846 run_test 204c "Print default stripe count and offset"
18847
18848 test_204d() {
18849         test_mkdir $DIR/$tdir
18850         $LFS setstripe --stripe-index 0 $DIR/$tdir
18851
18852         check_default_stripe_attr --stripe-count
18853         check_default_stripe_attr --stripe-size
18854 }
18855 run_test 204d "Print default stripe count and size"
18856
18857 test_204e() {
18858         test_mkdir $DIR/$tdir
18859         $LFS setstripe -d $DIR/$tdir
18860
18861         check_default_stripe_attr --stripe-count --raw
18862         check_default_stripe_attr --stripe-size --raw
18863         check_default_stripe_attr --stripe-index --raw
18864 }
18865 run_test 204e "Print raw stripe attributes"
18866
18867 test_204f() {
18868         test_mkdir $DIR/$tdir
18869         $LFS setstripe --stripe-count 1 $DIR/$tdir
18870
18871         check_default_stripe_attr --stripe-size --raw
18872         check_default_stripe_attr --stripe-index --raw
18873 }
18874 run_test 204f "Print raw stripe size and offset"
18875
18876 test_204g() {
18877         test_mkdir $DIR/$tdir
18878         $LFS setstripe --stripe-size 65536 $DIR/$tdir
18879
18880         check_default_stripe_attr --stripe-count --raw
18881         check_default_stripe_attr --stripe-index --raw
18882 }
18883 run_test 204g "Print raw stripe count and offset"
18884
18885 test_204h() {
18886         test_mkdir $DIR/$tdir
18887         $LFS setstripe --stripe-index 0 $DIR/$tdir
18888
18889         check_default_stripe_attr --stripe-count --raw
18890         check_default_stripe_attr --stripe-size --raw
18891 }
18892 run_test 204h "Print raw stripe count and size"
18893
18894 # Figure out which job scheduler is being used, if any,
18895 # or use a fake one
18896 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
18897         JOBENV=SLURM_JOB_ID
18898 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
18899         JOBENV=LSB_JOBID
18900 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
18901         JOBENV=PBS_JOBID
18902 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
18903         JOBENV=LOADL_STEP_ID
18904 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
18905         JOBENV=JOB_ID
18906 else
18907         $LCTL list_param jobid_name > /dev/null 2>&1
18908         if [ $? -eq 0 ]; then
18909                 JOBENV=nodelocal
18910         else
18911                 JOBENV=FAKE_JOBID
18912         fi
18913 fi
18914 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
18915
18916 verify_jobstats() {
18917         local cmd=($1)
18918         shift
18919         local facets="$@"
18920
18921 # we don't really need to clear the stats for this test to work, since each
18922 # command has a unique jobid, but it makes debugging easier if needed.
18923 #       for facet in $facets; do
18924 #               local dev=$(convert_facet2label $facet)
18925 #               # clear old jobstats
18926 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
18927 #       done
18928
18929         # use a new JobID for each test, or we might see an old one
18930         [ "$JOBENV" = "FAKE_JOBID" ] &&
18931                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
18932
18933         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
18934
18935         [ "$JOBENV" = "nodelocal" ] && {
18936                 FAKE_JOBID=id.$testnum.%e.$RANDOM
18937                 $LCTL set_param jobid_name=$FAKE_JOBID
18938                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
18939         }
18940
18941         log "Test: ${cmd[*]}"
18942         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
18943
18944         if [ $JOBENV = "FAKE_JOBID" ]; then
18945                 FAKE_JOBID=$JOBVAL ${cmd[*]}
18946         else
18947                 ${cmd[*]}
18948         fi
18949
18950         # all files are created on OST0000
18951         for facet in $facets; do
18952                 local stats="*.$(convert_facet2label $facet).job_stats"
18953
18954                 # strip out libtool wrappers for in-tree executables
18955                 if (( $(do_facet $facet lctl get_param $stats |
18956                         sed -e 's/\.lt-/./' | grep -cw $JOBVAL) != 1 )); then
18957                         do_facet $facet lctl get_param $stats
18958                         error "No jobstats for $JOBVAL found on $facet::$stats"
18959                 fi
18960         done
18961 }
18962
18963 jobstats_set() {
18964         local new_jobenv=$1
18965
18966         set_persistent_param_and_check client "jobid_var" \
18967                 "$FSNAME.sys.jobid_var" $new_jobenv
18968 }
18969
18970 test_205a() { # Job stats
18971         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18972         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
18973                 skip "Need MDS version with at least 2.7.1"
18974         remote_mgs_nodsh && skip "remote MGS with nodsh"
18975         remote_mds_nodsh && skip "remote MDS with nodsh"
18976         remote_ost_nodsh && skip "remote OST with nodsh"
18977         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
18978                 skip "Server doesn't support jobstats"
18979         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
18980
18981         local old_jobenv=$($LCTL get_param -n jobid_var)
18982         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
18983
18984         if [[ $PERM_CMD == *"set_param -P"* ]]; then
18985                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
18986         else
18987                 stack_trap "do_facet mgs $PERM_CMD \
18988                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
18989         fi
18990         changelog_register
18991
18992         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
18993                                 mdt.*.job_cleanup_interval | head -n 1)
18994         local new_interval=5
18995         do_facet $SINGLEMDS \
18996                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
18997         stack_trap "do_facet $SINGLEMDS \
18998                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
18999         local start=$SECONDS
19000
19001         local cmd
19002         # mkdir
19003         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir"
19004         verify_jobstats "$cmd" "$SINGLEMDS"
19005         # rmdir
19006         cmd="rmdir $DIR/$tdir"
19007         verify_jobstats "$cmd" "$SINGLEMDS"
19008         # mkdir on secondary MDT
19009         if [ $MDSCOUNT -gt 1 ]; then
19010                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
19011                 verify_jobstats "$cmd" "mds2"
19012         fi
19013         # mknod
19014         cmd="mknod $DIR/$tfile c 1 3"
19015         verify_jobstats "$cmd" "$SINGLEMDS"
19016         # unlink
19017         cmd="rm -f $DIR/$tfile"
19018         verify_jobstats "$cmd" "$SINGLEMDS"
19019         # create all files on OST0000 so verify_jobstats can find OST stats
19020         # open & close
19021         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
19022         verify_jobstats "$cmd" "$SINGLEMDS"
19023         # setattr
19024         cmd="touch $DIR/$tfile"
19025         verify_jobstats "$cmd" "$SINGLEMDS ost1"
19026         # write
19027         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
19028         verify_jobstats "$cmd" "ost1"
19029         # read
19030         cancel_lru_locks osc
19031         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
19032         verify_jobstats "$cmd" "ost1"
19033         # truncate
19034         cmd="$TRUNCATE $DIR/$tfile 0"
19035         verify_jobstats "$cmd" "$SINGLEMDS ost1"
19036         # rename
19037         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
19038         verify_jobstats "$cmd" "$SINGLEMDS"
19039         # jobstats expiry - sleep until old stats should be expired
19040         local left=$((new_interval + 5 - (SECONDS - start)))
19041         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
19042                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
19043                         "0" $left
19044         cmd="$LFS mkdir -i 0 -c 1 $DIR/$tdir.expire"
19045         verify_jobstats "$cmd" "$SINGLEMDS"
19046         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
19047             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
19048
19049         # Ensure that jobid are present in changelog (if supported by MDS)
19050         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
19051                 changelog_dump | tail -10
19052                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
19053                 [ $jobids -eq 9 ] ||
19054                         error "Wrong changelog jobid count $jobids != 9"
19055
19056                 # LU-5862
19057                 JOBENV="disable"
19058                 jobstats_set $JOBENV
19059                 touch $DIR/$tfile
19060                 changelog_dump | grep $tfile
19061                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
19062                 [ $jobids -eq 0 ] ||
19063                         error "Unexpected jobids when jobid_var=$JOBENV"
19064         fi
19065
19066         # test '%j' access to environment variable - if supported
19067         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"; then
19068                 JOBENV="JOBCOMPLEX"
19069                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
19070
19071                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
19072         fi
19073
19074         if lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%H.E"; then
19075                 JOBENV="JOBCOMPLEX"
19076                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname -s).E"
19077
19078                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
19079         fi
19080
19081         # test '%j' access to per-session jobid - if supported
19082         if lctl list_param jobid_this_session > /dev/null 2>&1
19083         then
19084                 lctl set_param jobid_var=session jobid_name="S.%j.%e.%u.%h.E"
19085                 lctl set_param jobid_this_session=$USER
19086
19087                 JOBENV="JOBCOMPLEX"
19088                 JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
19089
19090                 verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
19091         fi
19092 }
19093 run_test 205a "Verify job stats"
19094
19095 # LU-13117, LU-13597
19096 test_205b() {
19097         (( $MDS1_VERSION >= $(version_code 2.13.54.91) )) ||
19098                 skip "Need MDS version at least 2.13.54.91"
19099
19100         local job_stats="mdt.*.job_stats"
19101         local old_jobid=$(do_facet mds1 $LCTL get_param jobid_var)
19102
19103         do_facet mds1 $LCTL set_param $job_stats=clear
19104
19105         # Setting jobid_var to USER might not be supported
19106         [[ -n "$old_jobid" ]] && stack_trap "$LCTL set_param $old_jobid"
19107         $LCTL set_param jobid_var=USER || true
19108         stack_trap "$LCTL set_param $($LCTL get_param jobid_name)"
19109         $LCTL set_param jobid_name="%j.%e.%u"
19110
19111         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
19112         do_facet mds1 $LCTL get_param $job_stats | grep "job_id:.*foolish" &&
19113                 { do_facet mds1 $LCTL get_param $job_stats;
19114                   error "Unexpected jobid found"; }
19115         do_facet mds1 $LCTL get_param $job_stats | grep "open:.*min.*max.*sum"||
19116                 { do_facet mds1 $LCTL get_param $job_stats;
19117                   error "wrong job_stats format found"; }
19118
19119         (( $MDS1_VERSION <= $(version_code 2.15.0) )) &&
19120                 echo "MDS does not yet escape jobid" && return 0
19121         $LCTL set_param jobid_var=TEST205b
19122         env -i TEST205b="has sp" touch $DIR/$tfile.2
19123         do_facet mds1 $LCTL get_param $job_stats | grep "has.*x20sp" ||
19124                 { do_facet mds1 $LCTL get_param $job_stats;
19125                   error "jobid not escaped"; }
19126 }
19127 run_test 205b "Verify job stats jobid and output format"
19128
19129 # LU-13733
19130 test_205c() {
19131         $LCTL set_param llite.*.stats=0
19132         dd if=/dev/zero of=$DIR/$tfile.1 bs=4k count=1
19133         $LCTL get_param llite.*.stats
19134         $LCTL get_param llite.*.stats | grep \
19135                 "write_bytes *1 samples \[bytes\] 4096 4096 4096 16777216" ||
19136                         error "wrong client stats format found"
19137 }
19138 run_test 205c "Verify client stats format"
19139
19140 # LU-1480, LU-1773 and LU-1657
19141 test_206() {
19142         mkdir -p $DIR/$tdir
19143         $LFS setstripe -c -1 $DIR/$tdir
19144 #define OBD_FAIL_LOV_INIT 0x1403
19145         $LCTL set_param fail_loc=0xa0001403
19146         $LCTL set_param fail_val=1
19147         touch $DIR/$tdir/$tfile || true
19148 }
19149 run_test 206 "fail lov_init_raid0() doesn't lbug"
19150
19151 test_207a() {
19152         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
19153         local fsz=`stat -c %s $DIR/$tfile`
19154         cancel_lru_locks mdc
19155
19156         # do not return layout in getattr intent
19157 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
19158         $LCTL set_param fail_loc=0x170
19159         local sz=`stat -c %s $DIR/$tfile`
19160
19161         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
19162
19163         rm -rf $DIR/$tfile
19164 }
19165 run_test 207a "can refresh layout at glimpse"
19166
19167 test_207b() {
19168         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
19169         local cksum=`md5sum $DIR/$tfile`
19170         local fsz=`stat -c %s $DIR/$tfile`
19171         cancel_lru_locks mdc
19172         cancel_lru_locks osc
19173
19174         # do not return layout in getattr intent
19175 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
19176         $LCTL set_param fail_loc=0x171
19177
19178         # it will refresh layout after the file is opened but before read issues
19179         echo checksum is "$cksum"
19180         echo "$cksum" |md5sum -c --quiet || error "file differs"
19181
19182         rm -rf $DIR/$tfile
19183 }
19184 run_test 207b "can refresh layout at open"
19185
19186 test_208() {
19187         # FIXME: in this test suite, only RD lease is used. This is okay
19188         # for now as only exclusive open is supported. After generic lease
19189         # is done, this test suite should be revised. - Jinshan
19190
19191         remote_mds_nodsh && skip "remote MDS with nodsh"
19192         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
19193                 skip "Need MDS version at least 2.4.52"
19194
19195         echo "==== test 1: verify get lease work"
19196         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
19197
19198         echo "==== test 2: verify lease can be broken by upcoming open"
19199         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
19200         local PID=$!
19201         sleep 2
19202
19203         $MULTIOP $DIR/$tfile oO_RDWR:c
19204         kill -USR1 $PID && wait $PID || error "break lease error"
19205
19206         echo "==== test 3: verify lease can't be granted if an open already exists"
19207         $MULTIOP $DIR/$tfile oO_RDWR:_c &
19208         local PID=$!
19209         sleep 2
19210
19211         $MULTIOP $DIR/$tfile oO_RDWR:eReUc && error "apply lease should fail"
19212         kill -USR1 $PID && wait $PID || error "open file error"
19213
19214         echo "==== test 4: lease can sustain over recovery"
19215         $MULTIOP $DIR/$tfile oO_RDWR:eR_E+eUc &
19216         PID=$!
19217         sleep 2
19218
19219         fail mds1
19220
19221         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
19222
19223         echo "==== test 5: lease broken can't be regained by replay"
19224         $MULTIOP $DIR/$tfile oO_RDWR:eR_E-eUc &
19225         PID=$!
19226         sleep 2
19227
19228         # open file to break lease and then recovery
19229         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
19230         fail mds1
19231
19232         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
19233
19234         rm -f $DIR/$tfile
19235 }
19236 run_test 208 "Exclusive open"
19237
19238 test_209() {
19239         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
19240                 skip_env "must have disp_stripe"
19241
19242         touch $DIR/$tfile
19243         sync; sleep 5; sync;
19244
19245         echo 3 > /proc/sys/vm/drop_caches
19246         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
19247                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
19248         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
19249
19250         # open/close 500 times
19251         for i in $(seq 500); do
19252                 cat $DIR/$tfile
19253         done
19254
19255         echo 3 > /proc/sys/vm/drop_caches
19256         [ -f /sys/kernel/slab/ptlrpc_cache/shrink ] &&
19257                 echo 1 > /sys/kernel/slab/ptlrpc_cache/shrink
19258         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
19259
19260         echo "before: $req_before, after: $req_after"
19261         [ $((req_after - req_before)) -ge 300 ] &&
19262                 error "open/close requests are not freed"
19263         return 0
19264 }
19265 run_test 209 "read-only open/close requests should be freed promptly"
19266
19267 test_210() {
19268         local pid
19269
19270         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
19271         pid=$!
19272         sleep 1
19273
19274         $LFS getstripe $DIR/$tfile
19275         kill -USR1 $pid
19276         wait $pid || error "multiop failed"
19277
19278         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
19279         pid=$!
19280         sleep 1
19281
19282         $LFS getstripe $DIR/$tfile
19283         kill -USR1 $pid
19284         wait $pid || error "multiop failed"
19285 }
19286 run_test 210 "lfs getstripe does not break leases"
19287
19288 test_212() {
19289         size=`date +%s`
19290         size=$((size % 8192 + 1))
19291         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
19292         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
19293         rm -f $DIR/f212 $DIR/f212.xyz
19294 }
19295 run_test 212 "Sendfile test ============================================"
19296
19297 test_213() {
19298         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
19299         cancel_lru_locks osc
19300         lctl set_param fail_loc=0x8000040f
19301         # generate a read lock
19302         cat $DIR/$tfile > /dev/null
19303         # write to the file, it will try to cancel the above read lock.
19304         cat /etc/hosts >> $DIR/$tfile
19305 }
19306 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
19307
19308 test_214() { # for bug 20133
19309         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
19310         for (( i=0; i < 340; i++ )) ; do
19311                 touch $DIR/$tdir/d214c/a$i
19312         done
19313
19314         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
19315         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
19316         ls $DIR/d214c || error "ls $DIR/d214c failed"
19317         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
19318         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
19319 }
19320 run_test 214 "hash-indexed directory test - bug 20133"
19321
19322 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
19323 create_lnet_proc_files() {
19324         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
19325 }
19326
19327 # counterpart of create_lnet_proc_files
19328 remove_lnet_proc_files() {
19329         rm -f $TMP/lnet_$1.sys
19330 }
19331
19332 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
19333 # 3rd arg as regexp for body
19334 check_lnet_proc_stats() {
19335         local l=$(cat "$TMP/lnet_$1" |wc -l)
19336         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
19337
19338         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
19339 }
19340
19341 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
19342 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
19343 # optional and can be regexp for 2nd line (lnet.routes case)
19344 check_lnet_proc_entry() {
19345         local blp=2          # blp stands for 'position of 1st line of body'
19346         [ -z "$5" ] || blp=3 # lnet.routes case
19347
19348         local l=$(cat "$TMP/lnet_$1" |wc -l)
19349         # subtracting one from $blp because the body can be empty
19350         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
19351
19352         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
19353                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
19354
19355         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
19356                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
19357
19358         # bail out if any unexpected line happened
19359         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
19360         [ "$?" != 0 ] || error "$2 misformatted"
19361 }
19362
19363 test_215() { # for bugs 18102, 21079, 21517
19364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19365
19366         local N='(0|[1-9][0-9]*)'       # non-negative numeric
19367         local P='[1-9][0-9]*'           # positive numeric
19368         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
19369         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
19370         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
19371         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
19372
19373         local L1 # regexp for 1st line
19374         local L2 # regexp for 2nd line (optional)
19375         local BR # regexp for the rest (body)
19376
19377         # lnet.stats should look as 11 space-separated non-negative numerics
19378         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
19379         create_lnet_proc_files "stats"
19380         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
19381         remove_lnet_proc_files "stats"
19382
19383         # lnet.routes should look like this:
19384         # Routing disabled/enabled
19385         # net hops priority state router
19386         # where net is a string like tcp0, hops > 0, priority >= 0,
19387         # state is up/down,
19388         # router is a string like 192.168.1.1@tcp2
19389         L1="^Routing (disabled|enabled)$"
19390         L2="^net +hops +priority +state +router$"
19391         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
19392         create_lnet_proc_files "routes"
19393         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
19394         remove_lnet_proc_files "routes"
19395
19396         # lnet.routers should look like this:
19397         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
19398         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
19399         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
19400         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
19401         L1="^ref +rtr_ref +alive +router$"
19402         BR="^$P +$P +(up|down) +$NID$"
19403         create_lnet_proc_files "routers"
19404         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
19405         remove_lnet_proc_files "routers"
19406
19407         # lnet.peers should look like this:
19408         # nid refs state last max rtr min tx min queue
19409         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
19410         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
19411         # numeric (0 or >0 or <0), queue >= 0.
19412         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
19413         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
19414         create_lnet_proc_files "peers"
19415         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
19416         remove_lnet_proc_files "peers"
19417
19418         # lnet.buffers  should look like this:
19419         # pages count credits min
19420         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
19421         L1="^pages +count +credits +min$"
19422         BR="^ +$N +$N +$I +$I$"
19423         create_lnet_proc_files "buffers"
19424         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
19425         remove_lnet_proc_files "buffers"
19426
19427         # lnet.nis should look like this:
19428         # nid status alive refs peer rtr max tx min
19429         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
19430         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
19431         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
19432         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
19433         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
19434         create_lnet_proc_files "nis"
19435         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
19436         remove_lnet_proc_files "nis"
19437
19438         # can we successfully write to lnet.stats?
19439         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
19440 }
19441 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
19442
19443 test_216() { # bug 20317
19444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19445         remote_ost_nodsh && skip "remote OST with nodsh"
19446
19447         local node
19448         local facets=$(get_facets OST)
19449         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
19450
19451         save_lustre_params client "osc.*.contention_seconds" > $p
19452         save_lustre_params $facets \
19453                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
19454         save_lustre_params $facets \
19455                 "ldlm.namespaces.filter-*.contended_locks" >> $p
19456         save_lustre_params $facets \
19457                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
19458         clear_stats osc.*.osc_stats
19459
19460         # agressive lockless i/o settings
19461         do_nodes $(comma_list $(osts_nodes)) \
19462                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
19463                         ldlm.namespaces.filter-*.contended_locks=0 \
19464                         ldlm.namespaces.filter-*.contention_seconds=60"
19465         lctl set_param -n osc.*.contention_seconds=60
19466
19467         $DIRECTIO write $DIR/$tfile 0 10 4096
19468         $CHECKSTAT -s 40960 $DIR/$tfile
19469
19470         # disable lockless i/o
19471         do_nodes $(comma_list $(osts_nodes)) \
19472                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
19473                         ldlm.namespaces.filter-*.contended_locks=32 \
19474                         ldlm.namespaces.filter-*.contention_seconds=0"
19475         lctl set_param -n osc.*.contention_seconds=0
19476         clear_stats osc.*.osc_stats
19477
19478         dd if=/dev/zero of=$DIR/$tfile count=0
19479         $CHECKSTAT -s 0 $DIR/$tfile
19480
19481         restore_lustre_params <$p
19482         rm -f $p
19483         rm $DIR/$tfile
19484 }
19485 run_test 216 "check lockless direct write updates file size and kms correctly"
19486
19487 test_217() { # bug 22430
19488         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19489
19490         local node
19491         local nid
19492
19493         for node in $(nodes_list); do
19494                 nid=$(host_nids_address $node $NETTYPE)
19495                 if [[ $nid = *-* ]] ; then
19496                         echo "lctl ping $(h2nettype $nid)"
19497                         lctl ping $(h2nettype $nid)
19498                 else
19499                         echo "skipping $node (no hyphen detected)"
19500                 fi
19501         done
19502 }
19503 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
19504
19505 test_218() {
19506        # do directio so as not to populate the page cache
19507        log "creating a 10 Mb file"
19508        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
19509        log "starting reads"
19510        dd if=$DIR/$tfile of=/dev/null bs=4096 &
19511        log "truncating the file"
19512        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
19513        log "killing dd"
19514        kill %+ || true # reads might have finished
19515        echo "wait until dd is finished"
19516        wait
19517        log "removing the temporary file"
19518        rm -rf $DIR/$tfile || error "tmp file removal failed"
19519 }
19520 run_test 218 "parallel read and truncate should not deadlock"
19521
19522 test_219() {
19523         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19524
19525         # write one partial page
19526         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
19527         # set no grant so vvp_io_commit_write will do sync write
19528         $LCTL set_param fail_loc=0x411
19529         # write a full page at the end of file
19530         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
19531
19532         $LCTL set_param fail_loc=0
19533         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
19534         $LCTL set_param fail_loc=0x411
19535         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
19536
19537         # LU-4201
19538         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
19539         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
19540 }
19541 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
19542
19543 test_220() { #LU-325
19544         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19545         remote_ost_nodsh && skip "remote OST with nodsh"
19546         remote_mds_nodsh && skip "remote MDS with nodsh"
19547         remote_mgs_nodsh && skip "remote MGS with nodsh"
19548
19549         local OSTIDX=0
19550
19551         # create on MDT0000 so the last_id and next_id are correct
19552         mkdir_on_mdt0 $DIR/$tdir
19553         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
19554         OST=${OST%_UUID}
19555
19556         # on the mdt's osc
19557         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
19558         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
19559                         osp.$mdtosc_proc1.prealloc_last_id)
19560         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
19561                         osp.$mdtosc_proc1.prealloc_next_id)
19562
19563         $LFS df -i
19564
19565         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
19566         #define OBD_FAIL_OST_ENOINO              0x229
19567         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
19568         create_pool $FSNAME.$TESTNAME || return 1
19569         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
19570
19571         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
19572
19573         MDSOBJS=$((last_id - next_id))
19574         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
19575
19576         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
19577         echo "OST still has $count kbytes free"
19578
19579         echo "create $MDSOBJS files @next_id..."
19580         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
19581
19582         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
19583                         osp.$mdtosc_proc1.prealloc_last_id)
19584         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
19585                         osp.$mdtosc_proc1.prealloc_next_id)
19586
19587         echo "after creation, last_id=$last_id2, next_id=$next_id2"
19588         $LFS df -i
19589
19590         echo "cleanup..."
19591
19592         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
19593         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
19594
19595         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
19596                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
19597         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
19598                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
19599         echo "unlink $MDSOBJS files @$next_id..."
19600         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
19601 }
19602 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
19603
19604 test_221() {
19605         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19606
19607         dd if=`which date` of=$MOUNT/date oflag=sync
19608         chmod +x $MOUNT/date
19609
19610         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
19611         $LCTL set_param fail_loc=0x80001401
19612
19613         $MOUNT/date > /dev/null
19614         rm -f $MOUNT/date
19615 }
19616 run_test 221 "make sure fault and truncate race to not cause OOM"
19617
19618 test_222a () {
19619         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19620
19621         rm -rf $DIR/$tdir
19622         test_mkdir $DIR/$tdir
19623         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19624         createmany -o $DIR/$tdir/$tfile 10
19625         cancel_lru_locks mdc
19626         cancel_lru_locks osc
19627         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
19628         $LCTL set_param fail_loc=0x31a
19629         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
19630         $LCTL set_param fail_loc=0
19631         rm -r $DIR/$tdir
19632 }
19633 run_test 222a "AGL for ls should not trigger CLIO lock failure"
19634
19635 test_222b () {
19636         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19637
19638         rm -rf $DIR/$tdir
19639         test_mkdir $DIR/$tdir
19640         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19641         createmany -o $DIR/$tdir/$tfile 10
19642         cancel_lru_locks mdc
19643         cancel_lru_locks osc
19644         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
19645         $LCTL set_param fail_loc=0x31a
19646         rm -r $DIR/$tdir || error "AGL for rmdir failed"
19647         $LCTL set_param fail_loc=0
19648 }
19649 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
19650
19651 test_223 () {
19652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19653
19654         rm -rf $DIR/$tdir
19655         test_mkdir $DIR/$tdir
19656         $LFS setstripe -c 1 -i 0 $DIR/$tdir
19657         createmany -o $DIR/$tdir/$tfile 10
19658         cancel_lru_locks mdc
19659         cancel_lru_locks osc
19660         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
19661         $LCTL set_param fail_loc=0x31b
19662         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
19663         $LCTL set_param fail_loc=0
19664         rm -r $DIR/$tdir
19665 }
19666 run_test 223 "osc reenqueue if without AGL lock granted ======================="
19667
19668 test_224a() { # LU-1039, MRP-303
19669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19670         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
19671         $LCTL set_param fail_loc=0x508
19672         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 conv=fsync
19673         $LCTL set_param fail_loc=0
19674         df $DIR
19675 }
19676 run_test 224a "Don't panic on bulk IO failure"
19677
19678 test_224bd_sub() { # LU-1039, MRP-303
19679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19680         local timeout=$1
19681
19682         shift
19683         dd if=/dev/urandom of=$TMP/$tfile bs=1M count=1
19684
19685         $LFS setstripe -c 1 -i 0 $DIR/$tfile
19686
19687         dd if=$TMP/$tfile of=$DIR/$tfile bs=1M count=1
19688         cancel_lru_locks osc
19689         set_checksums 0
19690         stack_trap "set_checksums $ORIG_CSUM" EXIT
19691         local at_max_saved=0
19692
19693         # adaptive timeouts may prevent seeing the issue
19694         if at_is_enabled; then
19695                 at_max_saved=$(at_max_get mds)
19696                 at_max_set 0 mds client
19697                 stack_trap "at_max_set $at_max_saved mds client" EXIT
19698         fi
19699
19700         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
19701         do_facet ost1 $LCTL set_param fail_val=$timeout fail_loc=0x80000515
19702         dd of=$TMP/$tfile.new if=$DIR/$tfile bs=1M count=1 || "$@"
19703
19704         do_facet ost1 $LCTL set_param fail_loc=0
19705         cmp $TMP/$tfile $TMP/$tfile.new || error "file contents wrong"
19706         df $DIR
19707 }
19708
19709 test_224b() {
19710         test_224bd_sub 3 error "dd failed"
19711 }
19712 run_test 224b "Don't panic on bulk IO failure"
19713
19714 test_224c() { # LU-6441
19715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19716         remote_mds_nodsh && skip "remote MDS with nodsh"
19717
19718         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
19719         save_writethrough $p
19720         set_cache writethrough on
19721
19722         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
19723         local at_max=$($LCTL get_param -n at_max)
19724         local timeout=$($LCTL get_param -n timeout)
19725         local test_at="at_max"
19726         local param_at="$FSNAME.sys.at_max"
19727         local test_timeout="timeout"
19728         local param_timeout="$FSNAME.sys.timeout"
19729
19730         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
19731
19732         set_persistent_param_and_check client "$test_at" "$param_at" 0
19733         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
19734
19735         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
19736         do_facet ost1 "$LCTL set_param fail_loc=0x520"
19737         $LFS setstripe -c 1 -i 0 $DIR/$tfile
19738         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
19739         sync
19740         do_facet ost1 "$LCTL set_param fail_loc=0"
19741
19742         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
19743         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
19744                 $timeout
19745
19746         $LCTL set_param -n $pages_per_rpc
19747         restore_lustre_params < $p
19748         rm -f $p
19749 }
19750 run_test 224c "Don't hang if one of md lost during large bulk RPC"
19751
19752 test_224d() { # LU-11169
19753         test_224bd_sub $((TIMEOUT + 2)) error "dd failed"
19754 }
19755 run_test 224d "Don't corrupt data on bulk IO timeout"
19756
19757 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
19758 test_225a () {
19759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19760         if [ -z ${MDSSURVEY} ]; then
19761                 skip_env "mds-survey not found"
19762         fi
19763         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
19764                 skip "Need MDS version at least 2.2.51"
19765
19766         local mds=$(facet_host $SINGLEMDS)
19767         local target=$(do_nodes $mds 'lctl dl' |
19768                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
19769
19770         local cmd1="file_count=1000 thrhi=4"
19771         local cmd2="dir_count=2 layer=mdd stripe_count=0"
19772         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
19773         local cmd="$cmd1 $cmd2 $cmd3"
19774
19775         rm -f ${TMP}/mds_survey*
19776         echo + $cmd
19777         eval $cmd || error "mds-survey with zero-stripe failed"
19778         cat ${TMP}/mds_survey*
19779         rm -f ${TMP}/mds_survey*
19780 }
19781 run_test 225a "Metadata survey sanity with zero-stripe"
19782
19783 test_225b () {
19784         if [ -z ${MDSSURVEY} ]; then
19785                 skip_env "mds-survey not found"
19786         fi
19787         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
19788                 skip "Need MDS version at least 2.2.51"
19789         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19790         remote_mds_nodsh && skip "remote MDS with nodsh"
19791         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
19792                 skip_env "Need to mount OST to test"
19793         fi
19794
19795         local mds=$(facet_host $SINGLEMDS)
19796         local target=$(do_nodes $mds 'lctl dl' |
19797                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
19798
19799         local cmd1="file_count=1000 thrhi=4"
19800         local cmd2="dir_count=2 layer=mdd stripe_count=1"
19801         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
19802         local cmd="$cmd1 $cmd2 $cmd3"
19803
19804         rm -f ${TMP}/mds_survey*
19805         echo + $cmd
19806         eval $cmd || error "mds-survey with stripe_count failed"
19807         cat ${TMP}/mds_survey*
19808         rm -f ${TMP}/mds_survey*
19809 }
19810 run_test 225b "Metadata survey sanity with stripe_count = 1"
19811
19812 mcreate_path2fid () {
19813         local mode=$1
19814         local major=$2
19815         local minor=$3
19816         local name=$4
19817         local desc=$5
19818         local path=$DIR/$tdir/$name
19819         local fid
19820         local rc
19821         local fid_path
19822
19823         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
19824                 error "cannot create $desc"
19825
19826         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
19827         rc=$?
19828         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
19829
19830         fid_path=$($LFS fid2path $MOUNT $fid)
19831         rc=$?
19832         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
19833
19834         [ "$path" == "$fid_path" ] ||
19835                 error "fid2path returned $fid_path, expected $path"
19836
19837         echo "pass with $path and $fid"
19838 }
19839
19840 test_226a () {
19841         rm -rf $DIR/$tdir
19842         mkdir -p $DIR/$tdir
19843
19844         mcreate_path2fid 0010666 0 0 fifo "FIFO"
19845         mcreate_path2fid 0020666 1 3 null "character special file (null)"
19846         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
19847         mcreate_path2fid 0040666 0 0 dir "directory"
19848         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
19849         mcreate_path2fid 0100666 0 0 file "regular file"
19850         mcreate_path2fid 0120666 0 0 link "symbolic link"
19851         mcreate_path2fid 0140666 0 0 sock "socket"
19852 }
19853 run_test 226a "call path2fid and fid2path on files of all type"
19854
19855 test_226b () {
19856         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19857
19858         local MDTIDX=1
19859
19860         rm -rf $DIR/$tdir
19861         mkdir -p $DIR/$tdir
19862         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
19863                 error "create remote directory failed"
19864         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
19865         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
19866                                 "character special file (null)"
19867         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
19868                                 "character special file (no device)"
19869         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
19870         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
19871                                 "block special file (loop)"
19872         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
19873         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
19874         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
19875 }
19876 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
19877
19878 test_226c () {
19879         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
19880         [[ $MDS1_VERSION -ge $(version_code 2.13.55) ]] ||
19881                 skip "Need MDS version at least 2.13.55"
19882
19883         local submnt=/mnt/submnt
19884         local srcfile=/etc/passwd
19885         local dstfile=$submnt/passwd
19886         local path
19887         local fid
19888
19889         rm -rf $DIR/$tdir
19890         rm -rf $submnt
19891         $LFS setdirstripe -c -1 -i 1 $DIR/$tdir ||
19892                 error "create remote directory failed"
19893         mkdir -p $submnt || error "create $submnt failed"
19894         $MOUNT_CMD $MGSNID:/$FSNAME/$tdir $submnt ||
19895                 error "mount $submnt failed"
19896         stack_trap "umount $submnt" EXIT
19897
19898         cp $srcfile $dstfile
19899         fid=$($LFS path2fid $dstfile)
19900         path=$($LFS fid2path $submnt "$fid")
19901         [ "$path" = "$dstfile" ] ||
19902                 error "fid2path $submnt $fid failed ($path != $dstfile)"
19903 }
19904 run_test 226c "call path2fid and fid2path under remote dir with subdir mount"
19905
19906 # LU-1299 Executing or running ldd on a truncated executable does not
19907 # cause an out-of-memory condition.
19908 test_227() {
19909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19910         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
19911
19912         dd if=$(which date) of=$MOUNT/date bs=1k count=1
19913         chmod +x $MOUNT/date
19914
19915         $MOUNT/date > /dev/null
19916         ldd $MOUNT/date > /dev/null
19917         rm -f $MOUNT/date
19918 }
19919 run_test 227 "running truncated executable does not cause OOM"
19920
19921 # LU-1512 try to reuse idle OI blocks
19922 test_228a() {
19923         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19924         remote_mds_nodsh && skip "remote MDS with nodsh"
19925         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19926
19927         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19928         local myDIR=$DIR/$tdir
19929
19930         mkdir -p $myDIR
19931         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19932         $LCTL set_param fail_loc=0x80001002
19933         createmany -o $myDIR/t- 10000
19934         $LCTL set_param fail_loc=0
19935         # The guard is current the largest FID holder
19936         touch $myDIR/guard
19937         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19938                     tr -d '[')
19939         local IDX=$(($SEQ % 64))
19940
19941         do_facet $SINGLEMDS sync
19942         # Make sure journal flushed.
19943         sleep 6
19944         local blk1=$(do_facet $SINGLEMDS \
19945                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19946                      grep Blockcount | awk '{print $4}')
19947
19948         # Remove old files, some OI blocks will become idle.
19949         unlinkmany $myDIR/t- 10000
19950         # Create new files, idle OI blocks should be reused.
19951         createmany -o $myDIR/t- 2000
19952         do_facet $SINGLEMDS sync
19953         # Make sure journal flushed.
19954         sleep 6
19955         local blk2=$(do_facet $SINGLEMDS \
19956                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19957                      grep Blockcount | awk '{print $4}')
19958
19959         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
19960 }
19961 run_test 228a "try to reuse idle OI blocks"
19962
19963 test_228b() {
19964         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19965         remote_mds_nodsh && skip "remote MDS with nodsh"
19966         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
19967
19968         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
19969         local myDIR=$DIR/$tdir
19970
19971         mkdir -p $myDIR
19972         #define OBD_FAIL_SEQ_EXHAUST             0x1002
19973         $LCTL set_param fail_loc=0x80001002
19974         createmany -o $myDIR/t- 10000
19975         $LCTL set_param fail_loc=0
19976         # The guard is current the largest FID holder
19977         touch $myDIR/guard
19978         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
19979                     tr -d '[')
19980         local IDX=$(($SEQ % 64))
19981
19982         do_facet $SINGLEMDS sync
19983         # Make sure journal flushed.
19984         sleep 6
19985         local blk1=$(do_facet $SINGLEMDS \
19986                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
19987                      grep Blockcount | awk '{print $4}')
19988
19989         # Remove old files, some OI blocks will become idle.
19990         unlinkmany $myDIR/t- 10000
19991
19992         # stop the MDT
19993         stop $SINGLEMDS || error "Fail to stop MDT."
19994         # remount the MDT
19995         start $SINGLEMDS $(facet_device $SINGLEMDS) $MDS_MOUNT_OPTS ||
19996                 error "Fail to start MDT."
19997
19998         df $MOUNT || error "Fail to df."
19999         # Create new files, idle OI blocks should be reused.
20000         createmany -o $myDIR/t- 2000
20001         do_facet $SINGLEMDS sync
20002         # Make sure journal flushed.
20003         sleep 6
20004         local blk2=$(do_facet $SINGLEMDS \
20005                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20006                      grep Blockcount | awk '{print $4}')
20007
20008         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
20009 }
20010 run_test 228b "idle OI blocks can be reused after MDT restart"
20011
20012 #LU-1881
20013 test_228c() {
20014         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20015         remote_mds_nodsh && skip "remote MDS with nodsh"
20016         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
20017
20018         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
20019         local myDIR=$DIR/$tdir
20020
20021         mkdir -p $myDIR
20022         #define OBD_FAIL_SEQ_EXHAUST             0x1002
20023         $LCTL set_param fail_loc=0x80001002
20024         # 20000 files can guarantee there are index nodes in the OI file
20025         createmany -o $myDIR/t- 20000
20026         $LCTL set_param fail_loc=0
20027         # The guard is current the largest FID holder
20028         touch $myDIR/guard
20029         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
20030                     tr -d '[')
20031         local IDX=$(($SEQ % 64))
20032
20033         do_facet $SINGLEMDS sync
20034         # Make sure journal flushed.
20035         sleep 6
20036         local blk1=$(do_facet $SINGLEMDS \
20037                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20038                      grep Blockcount | awk '{print $4}')
20039
20040         # Remove old files, some OI blocks will become idle.
20041         unlinkmany $myDIR/t- 20000
20042         rm -f $myDIR/guard
20043         # The OI file should become empty now
20044
20045         # Create new files, idle OI blocks should be reused.
20046         createmany -o $myDIR/t- 2000
20047         do_facet $SINGLEMDS sync
20048         # Make sure journal flushed.
20049         sleep 6
20050         local blk2=$(do_facet $SINGLEMDS \
20051                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
20052                      grep Blockcount | awk '{print $4}')
20053
20054         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
20055 }
20056 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
20057
20058 test_229() { # LU-2482, LU-3448
20059         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20060         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
20061         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
20062                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
20063
20064         rm -f $DIR/$tfile
20065
20066         # Create a file with a released layout and stripe count 2.
20067         $MULTIOP $DIR/$tfile H2c ||
20068                 error "failed to create file with released layout"
20069
20070         $LFS getstripe -v $DIR/$tfile
20071
20072         local pattern=$($LFS getstripe -L $DIR/$tfile)
20073         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
20074
20075         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
20076                 error "getstripe"
20077         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
20078         stat $DIR/$tfile || error "failed to stat released file"
20079
20080         chown $RUNAS_ID $DIR/$tfile ||
20081                 error "chown $RUNAS_ID $DIR/$tfile failed"
20082
20083         chgrp $RUNAS_ID $DIR/$tfile ||
20084                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
20085
20086         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
20087         rm $DIR/$tfile || error "failed to remove released file"
20088 }
20089 run_test 229 "getstripe/stat/rm/attr changes work on released files"
20090
20091 test_230a() {
20092         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20093         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20094         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20095                 skip "Need MDS version at least 2.11.52"
20096
20097         local MDTIDX=1
20098
20099         test_mkdir $DIR/$tdir
20100         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
20101         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
20102         [ $mdt_idx -ne 0 ] &&
20103                 error "create local directory on wrong MDT $mdt_idx"
20104
20105         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
20106                         error "create remote directory failed"
20107         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
20108         [ $mdt_idx -ne $MDTIDX ] &&
20109                 error "create remote directory on wrong MDT $mdt_idx"
20110
20111         createmany -o $DIR/$tdir/test_230/t- 10 ||
20112                 error "create files on remote directory failed"
20113         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
20114         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
20115         rm -r $DIR/$tdir || error "unlink remote directory failed"
20116 }
20117 run_test 230a "Create remote directory and files under the remote directory"
20118
20119 test_230b() {
20120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20121         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20122         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20123                 skip "Need MDS version at least 2.11.52"
20124
20125         local MDTIDX=1
20126         local mdt_index
20127         local i
20128         local file
20129         local pid
20130         local stripe_count
20131         local migrate_dir=$DIR/$tdir/migrate_dir
20132         local other_dir=$DIR/$tdir/other_dir
20133
20134         test_mkdir $DIR/$tdir
20135         test_mkdir -i0 -c1 $migrate_dir
20136         test_mkdir -i0 -c1 $other_dir
20137         for ((i=0; i<10; i++)); do
20138                 mkdir -p $migrate_dir/dir_${i}
20139                 createmany -o $migrate_dir/dir_${i}/f 10 ||
20140                         error "create files under remote dir failed $i"
20141         done
20142
20143         cp /etc/passwd $migrate_dir/$tfile
20144         cp /etc/passwd $other_dir/$tfile
20145         chattr +SAD $migrate_dir
20146         chattr +SAD $migrate_dir/$tfile
20147
20148         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20149         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20150         local old_dir_mode=$(stat -c%f $migrate_dir)
20151         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
20152
20153         mkdir -p $migrate_dir/dir_default_stripe2
20154         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
20155         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
20156
20157         mkdir -p $other_dir
20158         ln $migrate_dir/$tfile $other_dir/luna
20159         ln $migrate_dir/$tfile $migrate_dir/sofia
20160         ln $other_dir/$tfile $migrate_dir/david
20161         ln -s $migrate_dir/$tfile $other_dir/zachary
20162         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
20163         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
20164
20165         local len
20166         local lnktgt
20167
20168         # inline symlink
20169         for len in 58 59 60; do
20170                 lnktgt=$(str_repeat 'l' $len)
20171                 touch $migrate_dir/$lnktgt
20172                 ln -s $lnktgt $migrate_dir/${len}char_ln
20173         done
20174
20175         # PATH_MAX
20176         for len in 4094 4095; do
20177                 lnktgt=$(str_repeat 'l' $len)
20178                 ln -s $lnktgt $migrate_dir/${len}char_ln
20179         done
20180
20181         # NAME_MAX
20182         for len in 254 255; do
20183                 touch $migrate_dir/$(str_repeat 'l' $len)
20184         done
20185
20186         $LFS migrate -m $MDTIDX $migrate_dir ||
20187                 error "fails on migrating remote dir to MDT1"
20188
20189         echo "migratate to MDT1, then checking.."
20190         for ((i = 0; i < 10; i++)); do
20191                 for file in $(find $migrate_dir/dir_${i}); do
20192                         mdt_index=$($LFS getstripe -m $file)
20193                         # broken symlink getstripe will fail
20194                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
20195                                 error "$file is not on MDT${MDTIDX}"
20196                 done
20197         done
20198
20199         # the multiple link file should still in MDT0
20200         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
20201         [ $mdt_index == 0 ] ||
20202                 error "$file is not on MDT${MDTIDX}"
20203
20204         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20205         [ "$old_dir_flag" = "$new_dir_flag" ] ||
20206                 error " expect $old_dir_flag get $new_dir_flag"
20207
20208         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20209         [ "$old_file_flag" = "$new_file_flag" ] ||
20210                 error " expect $old_file_flag get $new_file_flag"
20211
20212         local new_dir_mode=$(stat -c%f $migrate_dir)
20213         [ "$old_dir_mode" = "$new_dir_mode" ] ||
20214                 error "expect mode $old_dir_mode get $new_dir_mode"
20215
20216         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
20217         [ "$old_file_mode" = "$new_file_mode" ] ||
20218                 error "expect mode $old_file_mode get $new_file_mode"
20219
20220         diff /etc/passwd $migrate_dir/$tfile ||
20221                 error "$tfile different after migration"
20222
20223         diff /etc/passwd $other_dir/luna ||
20224                 error "luna different after migration"
20225
20226         diff /etc/passwd $migrate_dir/sofia ||
20227                 error "sofia different after migration"
20228
20229         diff /etc/passwd $migrate_dir/david ||
20230                 error "david different after migration"
20231
20232         diff /etc/passwd $other_dir/zachary ||
20233                 error "zachary different after migration"
20234
20235         diff /etc/passwd $migrate_dir/${tfile}_ln ||
20236                 error "${tfile}_ln different after migration"
20237
20238         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
20239                 error "${tfile}_ln_other different after migration"
20240
20241         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
20242         [ $stripe_count = 2 ] ||
20243                 error "dir strpe_count $d != 2 after migration."
20244
20245         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
20246         [ $stripe_count = 2 ] ||
20247                 error "file strpe_count $d != 2 after migration."
20248
20249         #migrate back to MDT0
20250         MDTIDX=0
20251
20252         $LFS migrate -m $MDTIDX $migrate_dir ||
20253                 error "fails on migrating remote dir to MDT0"
20254
20255         echo "migrate back to MDT0, checking.."
20256         for file in $(find $migrate_dir); do
20257                 mdt_index=$($LFS getstripe -m $file)
20258                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
20259                         error "$file is not on MDT${MDTIDX}"
20260         done
20261
20262         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
20263         [ "$old_dir_flag" = "$new_dir_flag" ] ||
20264                 error " expect $old_dir_flag get $new_dir_flag"
20265
20266         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
20267         [ "$old_file_flag" = "$new_file_flag" ] ||
20268                 error " expect $old_file_flag get $new_file_flag"
20269
20270         local new_dir_mode=$(stat -c%f $migrate_dir)
20271         [ "$old_dir_mode" = "$new_dir_mode" ] ||
20272                 error "expect mode $old_dir_mode get $new_dir_mode"
20273
20274         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
20275         [ "$old_file_mode" = "$new_file_mode" ] ||
20276                 error "expect mode $old_file_mode get $new_file_mode"
20277
20278         diff /etc/passwd ${migrate_dir}/$tfile ||
20279                 error "$tfile different after migration"
20280
20281         diff /etc/passwd ${other_dir}/luna ||
20282                 error "luna different after migration"
20283
20284         diff /etc/passwd ${migrate_dir}/sofia ||
20285                 error "sofia different after migration"
20286
20287         diff /etc/passwd ${other_dir}/zachary ||
20288                 error "zachary different after migration"
20289
20290         diff /etc/passwd $migrate_dir/${tfile}_ln ||
20291                 error "${tfile}_ln different after migration"
20292
20293         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
20294                 error "${tfile}_ln_other different after migration"
20295
20296         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
20297         [ $stripe_count = 2 ] ||
20298                 error "dir strpe_count $d != 2 after migration."
20299
20300         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
20301         [ $stripe_count = 2 ] ||
20302                 error "file strpe_count $d != 2 after migration."
20303
20304         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20305 }
20306 run_test 230b "migrate directory"
20307
20308 test_230c() {
20309         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20310         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20311         remote_mds_nodsh && skip "remote MDS with nodsh"
20312         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20313                 skip "Need MDS version at least 2.11.52"
20314
20315         local MDTIDX=1
20316         local total=3
20317         local mdt_index
20318         local file
20319         local migrate_dir=$DIR/$tdir/migrate_dir
20320
20321         #If migrating directory fails in the middle, all entries of
20322         #the directory is still accessiable.
20323         test_mkdir $DIR/$tdir
20324         test_mkdir -i0 -c1 $migrate_dir
20325         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
20326         stat $migrate_dir
20327         createmany -o $migrate_dir/f $total ||
20328                 error "create files under ${migrate_dir} failed"
20329
20330         # fail after migrating top dir, and this will fail only once, so the
20331         # first sub file migration will fail (currently f3), others succeed.
20332         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
20333         do_facet mds1 lctl set_param fail_loc=0x1801
20334         local t=$(ls $migrate_dir | wc -l)
20335         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
20336                 error "migrate should fail"
20337         local u=$(ls $migrate_dir | wc -l)
20338         [ "$u" == "$t" ] || error "$u != $t during migration"
20339
20340         # add new dir/file should succeed
20341         mkdir $migrate_dir/dir ||
20342                 error "mkdir failed under migrating directory"
20343         touch $migrate_dir/file ||
20344                 error "create file failed under migrating directory"
20345
20346         # add file with existing name should fail
20347         for file in $migrate_dir/f*; do
20348                 stat $file > /dev/null || error "stat $file failed"
20349                 $OPENFILE -f O_CREAT:O_EXCL $file &&
20350                         error "open(O_CREAT|O_EXCL) $file should fail"
20351                 $MULTIOP $file m && error "create $file should fail"
20352                 touch $DIR/$tdir/remote_dir/$tfile ||
20353                         error "touch $tfile failed"
20354                 ln $DIR/$tdir/remote_dir/$tfile $file &&
20355                         error "link $file should fail"
20356                 mdt_index=$($LFS getstripe -m $file)
20357                 if [ $mdt_index == 0 ]; then
20358                         # file failed to migrate is not allowed to rename to
20359                         mv $DIR/$tdir/remote_dir/$tfile $file &&
20360                                 error "rename to $file should fail"
20361                 else
20362                         mv $DIR/$tdir/remote_dir/$tfile $file ||
20363                                 error "rename to $file failed"
20364                 fi
20365                 echo hello >> $file || error "write $file failed"
20366         done
20367
20368         # resume migration with different options should fail
20369         $LFS migrate -m 0 $migrate_dir &&
20370                 error "migrate -m 0 $migrate_dir should fail"
20371
20372         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
20373                 error "migrate -c 2 $migrate_dir should fail"
20374
20375         # resume migration should succeed
20376         $LFS migrate -m $MDTIDX $migrate_dir ||
20377                 error "migrate $migrate_dir failed"
20378
20379         echo "Finish migration, then checking.."
20380         for file in $(find $migrate_dir); do
20381                 mdt_index=$($LFS getstripe -m $file)
20382                 [ $mdt_index == $MDTIDX ] ||
20383                         error "$file is not on MDT${MDTIDX}"
20384         done
20385
20386         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20387 }
20388 run_test 230c "check directory accessiblity if migration failed"
20389
20390 test_230d() {
20391         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20392         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20393         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20394                 skip "Need MDS version at least 2.11.52"
20395         # LU-11235
20396         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
20397
20398         local migrate_dir=$DIR/$tdir/migrate_dir
20399         local old_index
20400         local new_index
20401         local old_count
20402         local new_count
20403         local new_hash
20404         local mdt_index
20405         local i
20406         local j
20407
20408         old_index=$((RANDOM % MDSCOUNT))
20409         old_count=$((MDSCOUNT - old_index))
20410         new_index=$((RANDOM % MDSCOUNT))
20411         new_count=$((MDSCOUNT - new_index))
20412         new_hash=1 # for all_char
20413
20414         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
20415         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
20416
20417         test_mkdir $DIR/$tdir
20418         test_mkdir -i $old_index -c $old_count $migrate_dir
20419
20420         for ((i=0; i<100; i++)); do
20421                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
20422                 createmany -o $migrate_dir/dir_${i}/f 100 ||
20423                         error "create files under remote dir failed $i"
20424         done
20425
20426         echo -n "Migrate from MDT$old_index "
20427         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
20428         echo -n "to MDT$new_index"
20429         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
20430         echo
20431
20432         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
20433         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
20434                 error "migrate remote dir error"
20435
20436         echo "Finish migration, then checking.."
20437         for file in $(find $migrate_dir -maxdepth 1); do
20438                 mdt_index=$($LFS getstripe -m $file)
20439                 if [ $mdt_index -lt $new_index ] ||
20440                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
20441                         error "$file is on MDT$mdt_index"
20442                 fi
20443         done
20444
20445         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20446 }
20447 run_test 230d "check migrate big directory"
20448
20449 test_230e() {
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         local i
20456         local j
20457         local a_fid
20458         local b_fid
20459
20460         mkdir_on_mdt0 $DIR/$tdir
20461         mkdir $DIR/$tdir/migrate_dir
20462         mkdir $DIR/$tdir/other_dir
20463         touch $DIR/$tdir/migrate_dir/a
20464         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
20465         ls $DIR/$tdir/other_dir
20466
20467         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
20468                 error "migrate dir fails"
20469
20470         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
20471         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
20472
20473         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20474         [ $mdt_index == 0 ] || error "a is not on MDT0"
20475
20476         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
20477                 error "migrate dir fails"
20478
20479         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
20480         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
20481
20482         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20483         [ $mdt_index == 1 ] || error "a is not on MDT1"
20484
20485         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
20486         [ $mdt_index == 1 ] || error "b is not on MDT1"
20487
20488         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
20489         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
20490
20491         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
20492
20493         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20494 }
20495 run_test 230e "migrate mulitple local link files"
20496
20497 test_230f() {
20498         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20499         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20500         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20501                 skip "Need MDS version at least 2.11.52"
20502
20503         local a_fid
20504         local ln_fid
20505
20506         mkdir -p $DIR/$tdir
20507         mkdir $DIR/$tdir/migrate_dir
20508         $LFS mkdir -i1 $DIR/$tdir/other_dir
20509         touch $DIR/$tdir/migrate_dir/a
20510         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
20511         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
20512         ls $DIR/$tdir/other_dir
20513
20514         # a should be migrated to MDT1, since no other links on MDT0
20515         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
20516                 error "#1 migrate dir fails"
20517         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
20518         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
20519         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20520         [ $mdt_index == 1 ] || error "a is not on MDT1"
20521
20522         # a should stay on MDT1, because it is a mulitple link file
20523         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
20524                 error "#2 migrate dir fails"
20525         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20526         [ $mdt_index == 1 ] || error "a is not on MDT1"
20527
20528         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
20529                 error "#3 migrate dir fails"
20530
20531         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
20532         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
20533         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
20534
20535         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
20536         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
20537
20538         # a should be migrated to MDT0, since no other links on MDT1
20539         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
20540                 error "#4 migrate dir fails"
20541         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
20542         [ $mdt_index == 0 ] || error "a is not on MDT0"
20543
20544         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20545 }
20546 run_test 230f "migrate mulitple remote link files"
20547
20548 test_230g() {
20549         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20550         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20551         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20552                 skip "Need MDS version at least 2.11.52"
20553
20554         mkdir -p $DIR/$tdir/migrate_dir
20555
20556         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
20557                 error "migrating dir to non-exist MDT succeeds"
20558         true
20559 }
20560 run_test 230g "migrate dir to non-exist MDT"
20561
20562 test_230h() {
20563         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20564         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20565         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20566                 skip "Need MDS version at least 2.11.52"
20567
20568         local mdt_index
20569
20570         mkdir -p $DIR/$tdir/migrate_dir
20571
20572         $LFS migrate -m1 $DIR &&
20573                 error "migrating mountpoint1 should fail"
20574
20575         $LFS migrate -m1 $DIR/$tdir/.. &&
20576                 error "migrating mountpoint2 should fail"
20577
20578         # same as mv
20579         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
20580                 error "migrating $tdir/migrate_dir/.. should fail"
20581
20582         true
20583 }
20584 run_test 230h "migrate .. and root"
20585
20586 test_230i() {
20587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20588         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20589         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
20590                 skip "Need MDS version at least 2.11.52"
20591
20592         mkdir -p $DIR/$tdir/migrate_dir
20593
20594         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
20595                 error "migration fails with a tailing slash"
20596
20597         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
20598                 error "migration fails with two tailing slashes"
20599 }
20600 run_test 230i "lfs migrate -m tolerates trailing slashes"
20601
20602 test_230j() {
20603         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20604         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
20605                 skip "Need MDS version at least 2.11.52"
20606
20607         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
20608         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
20609                 error "create $tfile failed"
20610         cat /etc/passwd > $DIR/$tdir/$tfile
20611
20612         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
20613
20614         cmp /etc/passwd $DIR/$tdir/$tfile ||
20615                 error "DoM file mismatch after migration"
20616 }
20617 run_test 230j "DoM file data not changed after dir migration"
20618
20619 test_230k() {
20620         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
20621         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20622                 skip "Need MDS version at least 2.11.56"
20623
20624         local total=20
20625         local files_on_starting_mdt=0
20626
20627         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
20628         $LFS getdirstripe $DIR/$tdir
20629         for i in $(seq $total); do
20630                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
20631                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
20632                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
20633         done
20634
20635         echo "$files_on_starting_mdt files on MDT0"
20636
20637         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
20638         $LFS getdirstripe $DIR/$tdir
20639
20640         files_on_starting_mdt=0
20641         for i in $(seq $total); do
20642                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
20643                         error "file $tfile.$i mismatch after migration"
20644                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
20645                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
20646         done
20647
20648         echo "$files_on_starting_mdt files on MDT1 after migration"
20649         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
20650
20651         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
20652         $LFS getdirstripe $DIR/$tdir
20653
20654         files_on_starting_mdt=0
20655         for i in $(seq $total); do
20656                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
20657                         error "file $tfile.$i mismatch after 2nd migration"
20658                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
20659                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
20660         done
20661
20662         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
20663         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
20664
20665         true
20666 }
20667 run_test 230k "file data not changed after dir migration"
20668
20669 test_230l() {
20670         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20671         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20672                 skip "Need MDS version at least 2.11.56"
20673
20674         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
20675         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
20676                 error "create files under remote dir failed $i"
20677         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
20678 }
20679 run_test 230l "readdir between MDTs won't crash"
20680
20681 test_230m() {
20682         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20683         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
20684                 skip "Need MDS version at least 2.11.56"
20685
20686         local MDTIDX=1
20687         local mig_dir=$DIR/$tdir/migrate_dir
20688         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
20689         local shortstr="b"
20690         local val
20691
20692         echo "Creating files and dirs with xattrs"
20693         test_mkdir $DIR/$tdir
20694         test_mkdir -i0 -c1 $mig_dir
20695         mkdir $mig_dir/dir
20696         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
20697                 error "cannot set xattr attr1 on dir"
20698         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
20699                 error "cannot set xattr attr2 on dir"
20700         touch $mig_dir/dir/f0
20701         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
20702                 error "cannot set xattr attr1 on file"
20703         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
20704                 error "cannot set xattr attr2 on file"
20705         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
20706         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
20707         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
20708         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
20709         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
20710         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
20711         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
20712         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
20713         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
20714
20715         echo "Migrating to MDT1"
20716         $LFS migrate -m $MDTIDX $mig_dir ||
20717                 error "fails on migrating dir to MDT1"
20718
20719         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
20720         echo "Checking xattrs"
20721         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
20722         [ "$val" = $longstr ] ||
20723                 error "expecting xattr1 $longstr on dir, found $val"
20724         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
20725         [ "$val" = $shortstr ] ||
20726                 error "expecting xattr2 $shortstr on dir, found $val"
20727         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
20728         [ "$val" = $longstr ] ||
20729                 error "expecting xattr1 $longstr on file, found $val"
20730         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
20731         [ "$val" = $shortstr ] ||
20732                 error "expecting xattr2 $shortstr on file, found $val"
20733 }
20734 run_test 230m "xattrs not changed after dir migration"
20735
20736 test_230n() {
20737         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
20738         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
20739                 skip "Need MDS version at least 2.13.53"
20740
20741         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
20742         cat /etc/hosts > $DIR/$tdir/$tfile
20743         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
20744         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
20745
20746         cmp /etc/hosts $DIR/$tdir/$tfile ||
20747                 error "File data mismatch after migration"
20748 }
20749 run_test 230n "Dir migration with mirrored file"
20750
20751 test_230o() {
20752         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
20753         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
20754                 skip "Need MDS version at least 2.13.52"
20755
20756         local mdts=$(comma_list $(mdts_nodes))
20757         local timeout=100
20758         local restripe_status
20759         local delta
20760         local i
20761
20762         [[ $mds1_FSTYPE == zfs ]] && timeout=300
20763
20764         # in case "crush" hash type is not set
20765         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20766
20767         restripe_status=$(do_facet mds1 $LCTL get_param -n \
20768                            mdt.*MDT0000.enable_dir_restripe)
20769         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
20770         stack_trap "do_nodes $mdts $LCTL set_param \
20771                     mdt.*.enable_dir_restripe=$restripe_status"
20772
20773         mkdir $DIR/$tdir
20774         createmany -m $DIR/$tdir/f 100 ||
20775                 error "create files under remote dir failed $i"
20776         createmany -d $DIR/$tdir/d 100 ||
20777                 error "create dirs under remote dir failed $i"
20778
20779         for i in $(seq 2 $MDSCOUNT); do
20780                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
20781                 $LFS setdirstripe -c $i $DIR/$tdir ||
20782                         error "split -c $i $tdir failed"
20783                 wait_update $HOSTNAME \
20784                         "$LFS getdirstripe -H $DIR/$tdir" "crush" $timeout ||
20785                         error "dir split not finished"
20786                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
20787                         awk '/migrate/ {sum += $2} END { print sum }')
20788                 echo "$delta migrated when dir split $((i - 1)) to $i stripes"
20789                 # delta is around total_files/stripe_count
20790                 (( $delta < 200 / (i - 1) + 4 )) ||
20791                         error "$delta files migrated >= $((200 / (i - 1) + 4))"
20792         done
20793 }
20794 run_test 230o "dir split"
20795
20796 test_230p() {
20797         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
20798         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
20799                 skip "Need MDS version at least 2.13.52"
20800
20801         local mdts=$(comma_list $(mdts_nodes))
20802         local timeout=100
20803         local restripe_status
20804         local delta
20805         local c
20806
20807         [[ $mds1_FSTYPE == zfs ]] && timeout=300
20808
20809         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20810
20811         restripe_status=$(do_facet mds1 $LCTL get_param -n \
20812                            mdt.*MDT0000.enable_dir_restripe)
20813         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
20814         stack_trap "do_nodes $mdts $LCTL set_param \
20815                     mdt.*.enable_dir_restripe=$restripe_status"
20816
20817         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
20818         createmany -m $DIR/$tdir/f 100 ||
20819                 error "create files under remote dir failed"
20820         createmany -d $DIR/$tdir/d 100 ||
20821                 error "create dirs under remote dir failed"
20822
20823         for c in $(seq $((MDSCOUNT - 1)) -1 1); do
20824                 local mdt_hash="crush"
20825
20826                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear >/dev/null"
20827                 $LFS setdirstripe -c $c $DIR/$tdir ||
20828                         error "split -c $c $tdir failed"
20829                 if (( MDS1_VERSION >= $(version_code 2.14.51) )); then
20830                         mdt_hash="$mdt_hash,fixed"
20831                 elif [ $c -eq 1 ]; then
20832                         mdt_hash="none"
20833                 fi
20834                 wait_update $HOSTNAME \
20835                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash $timeout ||
20836                         error "dir merge not finished"
20837                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
20838                         awk '/migrate/ {sum += $2} END { print sum }')
20839                 echo "$delta migrated when dir merge $((c + 1)) to $c stripes"
20840                 # delta is around total_files/stripe_count
20841                 (( delta < 200 / c + 4 )) ||
20842                         error "$delta files migrated >= $((200 / c + 4))"
20843         done
20844 }
20845 run_test 230p "dir merge"
20846
20847 test_230q() {
20848         (( MDSCOUNT > 1)) || skip "needs >= 2 MDTs"
20849         (( MDS1_VERSION >= $(version_code 2.13.52) )) ||
20850                 skip "Need MDS version at least 2.13.52"
20851
20852         local mdts=$(comma_list $(mdts_nodes))
20853         local saved_threshold=$(do_facet mds1 \
20854                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
20855         local saved_delta=$(do_facet mds1 \
20856                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
20857         local threshold=100
20858         local delta=2
20859         local total=0
20860         local stripe_count=0
20861         local stripe_index
20862         local nr_files
20863         local create
20864
20865         # test with fewer files on ZFS
20866         [ "$mds1_FSTYPE" == "zfs" ] && threshold=40
20867
20868         stack_trap "do_nodes $mdts $LCTL set_param \
20869                     mdt.*.dir_split_count=$saved_threshold"
20870         stack_trap "do_nodes $mdts $LCTL set_param \
20871                     mdt.*.dir_split_delta=$saved_delta"
20872         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
20873         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
20874         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
20875         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
20876         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
20877         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
20878
20879         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
20880         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
20881
20882         create=$((threshold * 3 / 2))
20883         while [ $stripe_count -lt $MDSCOUNT ]; do
20884                 createmany -m $DIR/$tdir/f $total $create ||
20885                         error "create sub files failed"
20886                 stat $DIR/$tdir > /dev/null
20887                 total=$((total + create))
20888                 stripe_count=$((stripe_count + delta))
20889                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
20890
20891                 wait_update $HOSTNAME \
20892                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
20893                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
20894
20895                 wait_update $HOSTNAME \
20896                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
20897                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
20898
20899                 nr_files=$($LFS find -m 1 $DIR/$tdir | grep -c -w $stripe_index)
20900                 echo "$nr_files/$total files on MDT$stripe_index after split"
20901                 # allow 10% margin of imbalance with crush hash
20902                 (( $nr_files <= $total / $stripe_count + $create / 10)) ||
20903                         error "$nr_files files on MDT$stripe_index after split"
20904
20905                 nr_files=$($LFS find -type f $DIR/$tdir | wc -l)
20906                 [ $nr_files -eq $total ] ||
20907                         error "total sub files $nr_files != $total"
20908         done
20909
20910         (( MDS1_VERSION >= $(version_code 2.14.51) )) || return 0
20911
20912         echo "fixed layout directory won't auto split"
20913         $LFS migrate -m 0 $DIR/$tdir || error "migrate $tdir failed"
20914         wait_update $HOSTNAME "$LFS getdirstripe -H $DIR/$tdir" "crush,fixed" \
20915                 10 || error "stripe hash $($LFS getdirstripe -H $DIR/$tdir)"
20916         wait_update $HOSTNAME "$LFS getdirstripe -c $DIR/$tdir" 1 10 ||
20917                 error "stripe count $($LFS getdirstripe -c $DIR/$tdir)"
20918 }
20919 run_test 230q "dir auto split"
20920
20921 test_230r() {
20922         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
20923         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20924         [[ $MDS1_VERSION -ge $(version_code 2.13.54) ]] ||
20925                 skip "Need MDS version at least 2.13.54"
20926
20927         # maximum amount of local locks:
20928         # parent striped dir - 2 locks
20929         # new stripe in parent to migrate to - 1 lock
20930         # source and target - 2 locks
20931         # Total 5 locks for regular file
20932         mkdir -p $DIR/$tdir
20933         $LFS mkdir -i1 -c2 $DIR/$tdir/dir1
20934         touch $DIR/$tdir/dir1/eee
20935
20936         # create 4 hardlink for 4 more locks
20937         # Total: 9 locks > RS_MAX_LOCKS (8)
20938         $LFS mkdir -i1 -c1 $DIR/$tdir/dir2
20939         $LFS mkdir -i1 -c1 $DIR/$tdir/dir3
20940         $LFS mkdir -i1 -c1 $DIR/$tdir/dir4
20941         $LFS mkdir -i1 -c1 $DIR/$tdir/dir5
20942         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir2/eee
20943         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir3/eee
20944         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir4/eee
20945         ln $DIR/$tdir/dir1/eee $DIR/$tdir/dir5/eee
20946
20947         cancel_lru_locks mdc
20948
20949         $LFS migrate -m1 -c1 $DIR/$tdir/dir1 ||
20950                 error "migrate dir fails"
20951
20952         rm -rf $DIR/$tdir || error "rm dir failed after migration"
20953 }
20954 run_test 230r "migrate with too many local locks"
20955
20956 test_230s() {
20957         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
20958                 skip "Need MDS version at least 2.14.52"
20959
20960         local mdts=$(comma_list $(mdts_nodes))
20961         local restripe_status=$(do_facet mds1 $LCTL get_param -n \
20962                                 mdt.*MDT0000.enable_dir_restripe)
20963
20964         stack_trap "do_nodes $mdts $LCTL set_param \
20965                     mdt.*.enable_dir_restripe=$restripe_status"
20966
20967         local st
20968         for st in 0 1; do
20969                 do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=$st"
20970                 test_mkdir $DIR/$tdir
20971                 $LFS mkdir $DIR/$tdir |& grep "File exists" ||
20972                         error "$LFS mkdir should return EEXIST if target exists"
20973                 rmdir $DIR/$tdir
20974         done
20975 }
20976 run_test 230s "lfs mkdir should return -EEXIST if target exists"
20977
20978 test_230t()
20979 {
20980         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
20981         [[ $MDS1_VERSION -ge $(version_code 2.14.50) ]] ||
20982                 skip "Need MDS version at least 2.14.50"
20983
20984         test_mkdir $DIR/$tdir || error "mkdir $tdir failed"
20985         test_mkdir $DIR/$tdir/subdir || error "mkdir subdir failed"
20986         $LFS project -p 1 -s $DIR/$tdir ||
20987                 error "set $tdir project id failed"
20988         $LFS project -p 2 -s $DIR/$tdir/subdir ||
20989                 error "set subdir project id failed"
20990         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir || error "migrate failed"
20991 }
20992 run_test 230t "migrate directory with project ID set"
20993
20994 test_230u()
20995 {
20996         (( MDSCOUNT > 3 )) || skip_env "needs >= 4 MDTs"
20997         (( MDS1_VERSION >= $(version_code 2.14.53) )) ||
20998                 skip "Need MDS version at least 2.14.53"
20999
21000         local count
21001
21002         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
21003         mkdir $DIR/$tdir/sub{0..99} || error "mkdir sub failed"
21004         $LFS migrate -m -1 $DIR/$tdir/sub{0..99} || error "migrate sub failed"
21005         for i in $(seq 0 $((MDSCOUNT - 1))); do
21006                 count=$($LFS getstripe -m $DIR/$tdir/sub* | grep -c ^$i)
21007                 echo "$count dirs migrated to MDT$i"
21008         done
21009         count=$($LFS getstripe -m $DIR/$tdir/sub* | sort -u | wc -l)
21010         (( count >= MDSCOUNT - 1 )) || error "dirs migrated to $count MDTs"
21011 }
21012 run_test 230u "migrate directory by QOS"
21013
21014 test_230v()
21015 {
21016         (( MDSCOUNT > 3 )) || skip_env "needs >= 4 MDTs"
21017         (( MDS1_VERSION >= $(version_code 2.14.53) )) ||
21018                 skip "Need MDS version at least 2.14.53"
21019
21020         local count
21021
21022         mkdir $DIR/$tdir || error "mkdir $tdir failed"
21023         mkdir $DIR/$tdir/sub{0..99} || error "mkdir sub failed"
21024         $LFS migrate -m 0,2,1 $DIR/$tdir || error "migrate $tdir failed"
21025         for i in $(seq 0 $((MDSCOUNT - 1))); do
21026                 count=$($LFS getstripe -m $DIR/$tdir/sub* | grep -c ^$i)
21027                 echo "$count subdirs migrated to MDT$i"
21028                 (( i == 3 )) && (( count > 0 )) &&
21029                         error "subdir shouldn't be migrated to MDT3"
21030         done
21031         count=$($LFS getstripe -m $DIR/$tdir/sub* | sort -u | wc -l)
21032         (( count == 3 )) || error "dirs migrated to $count MDTs"
21033 }
21034 run_test 230v "subdir migrated to the MDT where its parent is located"
21035
21036 test_230w() {
21037         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
21038         (( MDS1_VERSION >= $(version_code 2.15.0) )) ||
21039                 skip "Need MDS version at least 2.15.0"
21040
21041         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
21042         createmany -o $DIR/$tdir/f 10 || error "create files failed"
21043         createmany -d $DIR/$tdir/d 10 || error "create dirs failed"
21044
21045         $LFS migrate -m 1 -c $MDSCOUNT -d $DIR/$tdir ||
21046                 error "migrate failed"
21047
21048         (( $($LFS getdirstripe -c $DIR/$tdir) == MDSCOUNT )) ||
21049                 error "$tdir stripe count mismatch"
21050
21051         for i in $(seq 0 9); do
21052                 (( $($LFS getdirstripe -c $DIR/$tdir/d$i) == 0 )) ||
21053                         error "d$i is striped"
21054         done
21055 }
21056 run_test 230w "non-recursive mode dir migration"
21057
21058 test_230x() {
21059         (( MDSCOUNT > 1 )) || skip "needs >= 2 MDTs"
21060         (( MDS1_VERSION >= $(version_code 2.15.0) )) ||
21061                 skip "Need MDS version at least 2.15.0"
21062
21063         mkdir -p $DIR/$tdir || error "mkdir failed"
21064         createmany -d $DIR/$tdir/sub 100 || error "createmany failed"
21065
21066         local mdt_name=$(mdtname_from_index 0)
21067         local low=$(do_facet mds2 $LCTL get_param -n \
21068                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low)
21069         local high=$(do_facet mds2 $LCTL get_param -n \
21070                 osp.*$mdt_name-osp-MDT0001.reserved_ino_high)
21071         local ffree=$($LFS df -i $MOUNT | awk "/$mdt_name/ { print \$4 }")
21072         local maxage=$(do_facet mds2 $LCTL get_param -n \
21073                 osp.*$mdt_name-osp-MDT0001.maxage)
21074
21075         stack_trap "do_facet mds2 $LCTL set_param -n \
21076                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low=$low \
21077                 osp.*$mdt_name-osp-MDT0001.reserved_ino_high=$high" EXIT
21078         stack_trap "do_facet mds2 $LCTL set_param -n \
21079                 osp.*$mdt_name-osp-MDT0001.maxage=$maxage" EXIT
21080
21081         do_facet mds2 $LCTL set_param -n \
21082                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low=$((ffree + 1))
21083         do_facet mds2 $LCTL set_param -n osp.*$mdt_name-osp-MDT0001.maxage=1
21084         sleep 4
21085         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir &&
21086                 error "migrate $tdir should fail"
21087
21088         do_facet mds2 $LCTL set_param -n \
21089                 osp.*$mdt_name-osp-MDT0001.reserved_ino_low=$low
21090         do_facet mds2 $LCTL set_param -n \
21091                 osp.*$mdt_name-osp-MDT0001.reserved_ino_high=$high
21092         sleep 4
21093         $LFS migrate -m 1 -c $MDSCOUNT $DIR/$tdir ||
21094                 error "migrate failed"
21095         (( $($LFS getdirstripe -c $DIR/$tdir) == $MDSCOUNT )) ||
21096                 error "$tdir stripe count mismatch"
21097 }
21098 run_test 230x "dir migration check space"
21099
21100 test_231a()
21101 {
21102         # For simplicity this test assumes that max_pages_per_rpc
21103         # is the same across all OSCs
21104         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
21105         local bulk_size=$((max_pages * PAGE_SIZE))
21106         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
21107                                        head -n 1)
21108
21109         mkdir -p $DIR/$tdir
21110         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
21111                 error "failed to set stripe with -S ${brw_size}M option"
21112
21113         # clear the OSC stats
21114         $LCTL set_param osc.*.stats=0 &>/dev/null
21115         stop_writeback
21116
21117         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
21118         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
21119                 oflag=direct &>/dev/null || error "dd failed"
21120
21121         sync; sleep 1; sync # just to be safe
21122         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
21123         if [ x$nrpcs != "x1" ]; then
21124                 $LCTL get_param osc.*.stats
21125                 error "found $nrpcs ost_write RPCs, not 1 as expected"
21126         fi
21127
21128         start_writeback
21129         # Drop the OSC cache, otherwise we will read from it
21130         cancel_lru_locks osc
21131
21132         # clear the OSC stats
21133         $LCTL set_param osc.*.stats=0 &>/dev/null
21134
21135         # Client reads $bulk_size.
21136         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
21137                 iflag=direct &>/dev/null || error "dd failed"
21138
21139         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
21140         if [ x$nrpcs != "x1" ]; then
21141                 $LCTL get_param osc.*.stats
21142                 error "found $nrpcs ost_read RPCs, not 1 as expected"
21143         fi
21144 }
21145 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
21146
21147 test_231b() {
21148         mkdir -p $DIR/$tdir
21149         local i
21150         for i in {0..1023}; do
21151                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
21152                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
21153                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
21154         done
21155         sync
21156 }
21157 run_test 231b "must not assert on fully utilized OST request buffer"
21158
21159 test_232a() {
21160         mkdir -p $DIR/$tdir
21161         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
21162
21163         #define OBD_FAIL_LDLM_OST_LVB            0x31c
21164         do_facet ost1 $LCTL set_param fail_loc=0x31c
21165
21166         # ignore dd failure
21167         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
21168
21169         do_facet ost1 $LCTL set_param fail_loc=0
21170         umount_client $MOUNT || error "umount failed"
21171         mount_client $MOUNT || error "mount failed"
21172         stop ost1 || error "cannot stop ost1"
21173         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21174 }
21175 run_test 232a "failed lock should not block umount"
21176
21177 test_232b() {
21178         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
21179                 skip "Need MDS version at least 2.10.58"
21180
21181         mkdir -p $DIR/$tdir
21182         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
21183         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
21184         sync
21185         cancel_lru_locks osc
21186
21187         #define OBD_FAIL_LDLM_OST_LVB            0x31c
21188         do_facet ost1 $LCTL set_param fail_loc=0x31c
21189
21190         # ignore failure
21191         $LFS data_version $DIR/$tdir/$tfile || true
21192
21193         do_facet ost1 $LCTL set_param fail_loc=0
21194         umount_client $MOUNT || error "umount failed"
21195         mount_client $MOUNT || error "mount failed"
21196         stop ost1 || error "cannot stop ost1"
21197         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
21198 }
21199 run_test 232b "failed data version lock should not block umount"
21200
21201 test_233a() {
21202         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
21203                 skip "Need MDS version at least 2.3.64"
21204         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
21205
21206         local fid=$($LFS path2fid $MOUNT)
21207
21208         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21209                 error "cannot access $MOUNT using its FID '$fid'"
21210 }
21211 run_test 233a "checking that OBF of the FS root succeeds"
21212
21213 test_233b() {
21214         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
21215                 skip "Need MDS version at least 2.5.90"
21216         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
21217
21218         local fid=$($LFS path2fid $MOUNT/.lustre)
21219
21220         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21221                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
21222
21223         fid=$($LFS path2fid $MOUNT/.lustre/fid)
21224         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
21225                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
21226 }
21227 run_test 233b "checking that OBF of the FS .lustre succeeds"
21228
21229 test_234() {
21230         local p="$TMP/sanityN-$TESTNAME.parameters"
21231         save_lustre_params client "llite.*.xattr_cache" > $p
21232         lctl set_param llite.*.xattr_cache 1 ||
21233                 skip_env "xattr cache is not supported"
21234
21235         mkdir -p $DIR/$tdir || error "mkdir failed"
21236         touch $DIR/$tdir/$tfile || error "touch failed"
21237         # OBD_FAIL_LLITE_XATTR_ENOMEM
21238         $LCTL set_param fail_loc=0x1405
21239         getfattr -n user.attr $DIR/$tdir/$tfile &&
21240                 error "getfattr should have failed with ENOMEM"
21241         $LCTL set_param fail_loc=0x0
21242         rm -rf $DIR/$tdir
21243
21244         restore_lustre_params < $p
21245         rm -f $p
21246 }
21247 run_test 234 "xattr cache should not crash on ENOMEM"
21248
21249 test_235() {
21250         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
21251                 skip "Need MDS version at least 2.4.52"
21252
21253         flock_deadlock $DIR/$tfile
21254         local RC=$?
21255         case $RC in
21256                 0)
21257                 ;;
21258                 124) error "process hangs on a deadlock"
21259                 ;;
21260                 *) error "error executing flock_deadlock $DIR/$tfile"
21261                 ;;
21262         esac
21263 }
21264 run_test 235 "LU-1715: flock deadlock detection does not work properly"
21265
21266 #LU-2935
21267 test_236() {
21268         check_swap_layouts_support
21269
21270         local ref1=/etc/passwd
21271         local ref2=/etc/group
21272         local file1=$DIR/$tdir/f1
21273         local file2=$DIR/$tdir/f2
21274
21275         test_mkdir -c1 $DIR/$tdir
21276         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
21277         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
21278         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
21279         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
21280         local fd=$(free_fd)
21281         local cmd="exec $fd<>$file2"
21282         eval $cmd
21283         rm $file2
21284         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
21285                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
21286         cmd="exec $fd>&-"
21287         eval $cmd
21288         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
21289
21290         #cleanup
21291         rm -rf $DIR/$tdir
21292 }
21293 run_test 236 "Layout swap on open unlinked file"
21294
21295 # LU-4659 linkea consistency
21296 test_238() {
21297         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
21298                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
21299                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
21300                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
21301
21302         touch $DIR/$tfile
21303         ln $DIR/$tfile $DIR/$tfile.lnk
21304         touch $DIR/$tfile.new
21305         mv $DIR/$tfile.new $DIR/$tfile
21306         local fid1=$($LFS path2fid $DIR/$tfile)
21307         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
21308         local path1=$($LFS fid2path $FSNAME "$fid1")
21309         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
21310         local path2=$($LFS fid2path $FSNAME "$fid2")
21311         [ $tfile.lnk == $path2 ] ||
21312                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
21313         rm -f $DIR/$tfile*
21314 }
21315 run_test 238 "Verify linkea consistency"
21316
21317 test_239A() { # was test_239
21318         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
21319                 skip "Need MDS version at least 2.5.60"
21320
21321         local list=$(comma_list $(mdts_nodes))
21322
21323         mkdir -p $DIR/$tdir
21324         createmany -o $DIR/$tdir/f- 5000
21325         unlinkmany $DIR/$tdir/f- 5000
21326         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
21327                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
21328         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
21329                         osp.*MDT*.sync_in_flight" | calc_sum)
21330         [ "$changes" -eq 0 ] || error "$changes not synced"
21331 }
21332 run_test 239A "osp_sync test"
21333
21334 test_239a() { #LU-5297
21335         remote_mds_nodsh && skip "remote MDS with nodsh"
21336
21337         touch $DIR/$tfile
21338         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
21339         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
21340         chgrp $RUNAS_GID $DIR/$tfile
21341         wait_delete_completed
21342 }
21343 run_test 239a "process invalid osp sync record correctly"
21344
21345 test_239b() { #LU-5297
21346         remote_mds_nodsh && skip "remote MDS with nodsh"
21347
21348         touch $DIR/$tfile1
21349         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
21350         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
21351         chgrp $RUNAS_GID $DIR/$tfile1
21352         wait_delete_completed
21353         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
21354         touch $DIR/$tfile2
21355         chgrp $RUNAS_GID $DIR/$tfile2
21356         wait_delete_completed
21357 }
21358 run_test 239b "process osp sync record with ENOMEM error correctly"
21359
21360 test_240() {
21361         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21362         remote_mds_nodsh && skip "remote MDS with nodsh"
21363
21364         mkdir -p $DIR/$tdir
21365
21366         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
21367                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
21368         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
21369                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
21370
21371         umount_client $MOUNT || error "umount failed"
21372         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
21373         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
21374         mount_client $MOUNT || error "failed to mount client"
21375
21376         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
21377         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
21378 }
21379 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
21380
21381 test_241_bio() {
21382         local count=$1
21383         local bsize=$2
21384
21385         for LOOP in $(seq $count); do
21386                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
21387                 cancel_lru_locks $OSC || true
21388         done
21389 }
21390
21391 test_241_dio() {
21392         local count=$1
21393         local bsize=$2
21394
21395         for LOOP in $(seq $1); do
21396                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
21397                         2>/dev/null
21398         done
21399 }
21400
21401 test_241a() { # was test_241
21402         local bsize=$PAGE_SIZE
21403
21404         (( bsize < 40960 )) && bsize=40960
21405         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
21406         ls -la $DIR/$tfile
21407         cancel_lru_locks $OSC
21408         test_241_bio 1000 $bsize &
21409         PID=$!
21410         test_241_dio 1000 $bsize
21411         wait $PID
21412 }
21413 run_test 241a "bio vs dio"
21414
21415 test_241b() {
21416         local bsize=$PAGE_SIZE
21417
21418         (( bsize < 40960 )) && bsize=40960
21419         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
21420         ls -la $DIR/$tfile
21421         test_241_dio 1000 $bsize &
21422         PID=$!
21423         test_241_dio 1000 $bsize
21424         wait $PID
21425 }
21426 run_test 241b "dio vs dio"
21427
21428 test_242() {
21429         remote_mds_nodsh && skip "remote MDS with nodsh"
21430
21431         mkdir_on_mdt0 $DIR/$tdir
21432         touch $DIR/$tdir/$tfile
21433
21434         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
21435         do_facet mds1 lctl set_param fail_loc=0x105
21436         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
21437
21438         do_facet mds1 lctl set_param fail_loc=0
21439         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
21440 }
21441 run_test 242 "mdt_readpage failure should not cause directory unreadable"
21442
21443 test_243()
21444 {
21445         test_mkdir $DIR/$tdir
21446         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
21447 }
21448 run_test 243 "various group lock tests"
21449
21450 test_244a()
21451 {
21452         test_mkdir $DIR/$tdir
21453         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
21454         sendfile_grouplock $DIR/$tdir/$tfile || \
21455                 error "sendfile+grouplock failed"
21456         rm -rf $DIR/$tdir
21457 }
21458 run_test 244a "sendfile with group lock tests"
21459
21460 test_244b()
21461 {
21462         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
21463
21464         local threads=50
21465         local size=$((1024*1024))
21466
21467         test_mkdir $DIR/$tdir
21468         for i in $(seq 1 $threads); do
21469                 local file=$DIR/$tdir/file_$((i / 10))
21470                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
21471                 local pids[$i]=$!
21472         done
21473         for i in $(seq 1 $threads); do
21474                 wait ${pids[$i]}
21475         done
21476 }
21477 run_test 244b "multi-threaded write with group lock"
21478
21479 test_245a() {
21480         local flagname="multi_mod_rpcs"
21481         local connect_data_name="max_mod_rpcs"
21482         local out
21483
21484         # check if multiple modify RPCs flag is set
21485         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
21486                 grep "connect_flags:")
21487         echo "$out"
21488
21489         echo "$out" | grep -qw $flagname
21490         if [ $? -ne 0 ]; then
21491                 echo "connect flag $flagname is not set"
21492                 return
21493         fi
21494
21495         # check if multiple modify RPCs data is set
21496         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
21497         echo "$out"
21498
21499         echo "$out" | grep -qw $connect_data_name ||
21500                 error "import should have connect data $connect_data_name"
21501 }
21502 run_test 245a "check mdc connection flag/data: multiple modify RPCs"
21503
21504 test_245b() {
21505         local flagname="multi_mod_rpcs"
21506         local connect_data_name="max_mod_rpcs"
21507         local out
21508
21509         remote_mds_nodsh && skip "remote MDS with nodsh"
21510         [[ $MDSCOUNT -ge 2 ]] || skip "needs >= 2 MDTs"
21511
21512         # check if multiple modify RPCs flag is set
21513         out=$(do_facet mds1 \
21514               $LCTL get_param osp.$FSNAME-MDT0001-osp-MDT0000.import |
21515               grep "connect_flags:")
21516         echo "$out"
21517
21518         [[ "$out" =~ $flagname ]] || skip "connect flag $flagname is not set"
21519
21520         # check if multiple modify RPCs data is set
21521         out=$(do_facet mds1 \
21522               $LCTL get_param osp.$FSNAME-MDT0001-osp-MDT0000.import)
21523
21524         [[ "$out" =~ $connect_data_name ]] ||
21525                 {
21526                         echo "$out"
21527                         error "missing connect data $connect_data_name"
21528                 }
21529 }
21530 run_test 245b "check osp connection flag/data: multiple modify RPCs"
21531
21532 cleanup_247() {
21533         local submount=$1
21534
21535         trap 0
21536         umount_client $submount
21537         rmdir $submount
21538 }
21539
21540 test_247a() {
21541         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
21542                 grep -q subtree ||
21543                 skip_env "Fileset feature is not supported"
21544
21545         local submount=${MOUNT}_$tdir
21546
21547         mkdir $MOUNT/$tdir
21548         mkdir -p $submount || error "mkdir $submount failed"
21549         FILESET="$FILESET/$tdir" mount_client $submount ||
21550                 error "mount $submount failed"
21551         trap "cleanup_247 $submount" EXIT
21552         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
21553         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
21554                 error "read $MOUNT/$tdir/$tfile failed"
21555         cleanup_247 $submount
21556 }
21557 run_test 247a "mount subdir as fileset"
21558
21559 test_247b() {
21560         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
21561                 skip_env "Fileset feature is not supported"
21562
21563         local submount=${MOUNT}_$tdir
21564
21565         rm -rf $MOUNT/$tdir
21566         mkdir -p $submount || error "mkdir $submount failed"
21567         SKIP_FILESET=1
21568         FILESET="$FILESET/$tdir" mount_client $submount &&
21569                 error "mount $submount should fail"
21570         rmdir $submount
21571 }
21572 run_test 247b "mount subdir that dose not exist"
21573
21574 test_247c() {
21575         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
21576                 skip_env "Fileset feature is not supported"
21577
21578         local submount=${MOUNT}_$tdir
21579
21580         mkdir -p $MOUNT/$tdir/dir1
21581         mkdir -p $submount || error "mkdir $submount failed"
21582         trap "cleanup_247 $submount" EXIT
21583         FILESET="$FILESET/$tdir" mount_client $submount ||
21584                 error "mount $submount failed"
21585         local fid=$($LFS path2fid $MOUNT/)
21586         $LFS fid2path $submount $fid && error "fid2path should fail"
21587         cleanup_247 $submount
21588 }
21589 run_test 247c "running fid2path outside subdirectory root"
21590
21591 test_247d() {
21592         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
21593                 skip "Fileset feature is not supported"
21594
21595         local submount=${MOUNT}_$tdir
21596
21597         mkdir -p $MOUNT/$tdir/dir1
21598         mkdir -p $submount || error "mkdir $submount failed"
21599         FILESET="$FILESET/$tdir" mount_client $submount ||
21600                 error "mount $submount failed"
21601         trap "cleanup_247 $submount" EXIT
21602
21603         local td=$submount/dir1
21604         local fid=$($LFS path2fid $td)
21605         [ -z "$fid" ] && error "path2fid unable to get $td FID"
21606
21607         # check that we get the same pathname back
21608         local rootpath
21609         local found
21610         for rootpath in "$submount" "$submount///" "$submount/dir1"; do
21611                 echo "$rootpath $fid"
21612                 found=$($LFS fid2path $rootpath "$fid")
21613                 [ -n "$found" ] || error "fid2path should succeed"
21614                 [ "$found" == "$td" ] || error "fid2path $found != $td"
21615         done
21616         # check wrong root path format
21617         rootpath=$submount"_wrong"
21618         found=$($LFS fid2path $rootpath "$fid")
21619         [ -z "$found" ] || error "fid2path should fail ($rootpath != $submount)"
21620
21621         cleanup_247 $submount
21622 }
21623 run_test 247d "running fid2path inside subdirectory root"
21624
21625 # LU-8037
21626 test_247e() {
21627         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
21628                 grep -q subtree ||
21629                 skip "Fileset feature is not supported"
21630
21631         local submount=${MOUNT}_$tdir
21632
21633         mkdir $MOUNT/$tdir
21634         mkdir -p $submount || error "mkdir $submount failed"
21635         FILESET="$FILESET/.." mount_client $submount &&
21636                 error "mount $submount should fail"
21637         rmdir $submount
21638 }
21639 run_test 247e "mount .. as fileset"
21640
21641 test_247f() {
21642         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21643         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
21644                 skip "Need at least version 2.13.52"
21645         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
21646                 skip "Need at least version 2.14.50"
21647         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
21648                 grep -q subtree ||
21649                 skip "Fileset feature is not supported"
21650
21651         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
21652         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
21653                 error "mkdir remote failed"
21654         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote/subdir ||
21655                 error "mkdir remote/subdir failed"
21656         $LFS mkdir -i 0 -c $MDSCOUNT $DIR/$tdir/striped ||
21657                 error "mkdir striped failed"
21658         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
21659
21660         local submount=${MOUNT}_$tdir
21661
21662         mkdir -p $submount || error "mkdir $submount failed"
21663         stack_trap "rmdir $submount"
21664
21665         local dir
21666         local stat
21667         local fileset=$FILESET
21668         local mdts=$(comma_list $(mdts_nodes))
21669
21670         stat=$(do_facet mds1 $LCTL get_param -n \
21671                 mdt.*MDT0000.enable_remote_subdir_mount)
21672         stack_trap "do_nodes $mdts $LCTL set_param \
21673                 mdt.*.enable_remote_subdir_mount=$stat"
21674
21675         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=0"
21676         stack_trap "umount_client $submount"
21677         FILESET="$fileset/$tdir/remote" mount_client $submount &&
21678                 error "mount remote dir $dir should fail"
21679
21680         for dir in $tdir/remote/subdir $tdir/striped $tdir/striped/subdir \
21681                 $tdir/striped/. ; do
21682                 FILESET="$fileset/$dir" mount_client $submount ||
21683                         error "mount $dir failed"
21684                 umount_client $submount
21685         done
21686
21687         do_nodes $mdts "$LCTL set_param mdt.*.enable_remote_subdir_mount=1"
21688         FILESET="$fileset/$tdir/remote" mount_client $submount ||
21689                 error "mount $tdir/remote failed"
21690 }
21691 run_test 247f "mount striped or remote directory as fileset"
21692
21693 test_247g() {
21694         [ $MDSCOUNT -lt 4 ] && skip_env "needs >= 4 MDTs"
21695         [ $CLIENT_VERSION -lt $(version_code 2.14.50) ] &&
21696                 skip "Need at least version 2.14.50"
21697
21698         $LFS mkdir -i 0 -c 4 -H fnv_1a_64 $DIR/$tdir ||
21699                 error "mkdir $tdir failed"
21700         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
21701
21702         local submount=${MOUNT}_$tdir
21703
21704         mkdir -p $submount || error "mkdir $submount failed"
21705         stack_trap "rmdir $submount"
21706
21707         FILESET="$fileset/$tdir" mount_client $submount ||
21708                 error "mount $dir failed"
21709         stack_trap "umount $submount"
21710
21711         local mdts=$(comma_list $(mdts_nodes))
21712
21713         local nrpcs
21714
21715         stat $submount > /dev/null
21716         cancel_lru_locks $MDC
21717         stat $submount > /dev/null
21718         stat $submount/$tfile > /dev/null
21719         do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
21720         stat $submount/$tfile > /dev/null
21721         nrpcs=$(do_nodes $mdts "lctl get_param -n mdt.*.md_stats" |
21722                 awk '/getattr/ {sum += $2} END {print sum}')
21723
21724         [ -z "$nrpcs" ] || error "$nrpcs extra getattr sent"
21725 }
21726 run_test 247g "mount striped directory as fileset caches ROOT lookup lock"
21727
21728 test_248a() {
21729         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
21730         [ -z "$fast_read_sav" ] && skip "no fast read support"
21731
21732         # create a large file for fast read verification
21733         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
21734
21735         # make sure the file is created correctly
21736         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
21737                 { rm -f $DIR/$tfile; skip "file creation error"; }
21738
21739         echo "Test 1: verify that fast read is 4 times faster on cache read"
21740
21741         # small read with fast read enabled
21742         $LCTL set_param -n llite.*.fast_read=1
21743         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
21744                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21745                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21746         # small read with fast read disabled
21747         $LCTL set_param -n llite.*.fast_read=0
21748         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
21749                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21750                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21751
21752         # verify that fast read is 4 times faster for cache read
21753         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
21754                 error_not_in_vm "fast read was not 4 times faster: " \
21755                            "$t_fast vs $t_slow"
21756
21757         echo "Test 2: verify the performance between big and small read"
21758         $LCTL set_param -n llite.*.fast_read=1
21759
21760         # 1k non-cache read
21761         cancel_lru_locks osc
21762         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
21763                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21764                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21765
21766         # 1M non-cache read
21767         cancel_lru_locks osc
21768         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
21769                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
21770                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
21771
21772         # verify that big IO is not 4 times faster than small IO
21773         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
21774                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
21775
21776         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
21777         rm -f $DIR/$tfile
21778 }
21779 run_test 248a "fast read verification"
21780
21781 test_248b() {
21782         # Default short_io_bytes=16384, try both smaller and larger sizes.
21783         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
21784         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
21785         echo "bs=53248 count=113 normal buffered write"
21786         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
21787                 error "dd of initial data file failed"
21788         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
21789
21790         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
21791         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
21792                 error "dd with sync normal writes failed"
21793         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
21794
21795         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
21796         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
21797                 error "dd with sync small writes failed"
21798         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
21799
21800         cancel_lru_locks osc
21801
21802         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
21803         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
21804         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
21805         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
21806                 iflag=direct || error "dd with O_DIRECT small read failed"
21807         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
21808         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
21809                 error "compare $TMP/$tfile.1 failed"
21810
21811         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
21812         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
21813
21814         # just to see what the maximum tunable value is, and test parsing
21815         echo "test invalid parameter 2MB"
21816         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
21817                 error "too-large short_io_bytes allowed"
21818         echo "test maximum parameter 512KB"
21819         # if we can set a larger short_io_bytes, run test regardless of version
21820         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
21821                 # older clients may not allow setting it this large, that's OK
21822                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
21823                         skip "Need at least client version 2.13.50"
21824                 error "medium short_io_bytes failed"
21825         fi
21826         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
21827         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
21828
21829         echo "test large parameter 64KB"
21830         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
21831         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
21832
21833         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
21834         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
21835                 error "dd with sync large writes failed"
21836         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
21837
21838         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
21839         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
21840         num=$((113 * 4096 / PAGE_SIZE))
21841         echo "bs=$size count=$num oflag=direct large write $tfile.3"
21842         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
21843                 error "dd with O_DIRECT large writes failed"
21844         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
21845                 error "compare $DIR/$tfile.3 failed"
21846
21847         cancel_lru_locks osc
21848
21849         echo "bs=$size count=$num iflag=direct large read $tfile.2"
21850         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
21851                 error "dd with O_DIRECT large read failed"
21852         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
21853                 error "compare $TMP/$tfile.2 failed"
21854
21855         echo "bs=$size count=$num iflag=direct large read $tfile.3"
21856         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
21857                 error "dd with O_DIRECT large read failed"
21858         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
21859                 error "compare $TMP/$tfile.3 failed"
21860 }
21861 run_test 248b "test short_io read and write for both small and large sizes"
21862
21863 test_249() { # LU-7890
21864         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
21865                 skip "Need at least version 2.8.54"
21866
21867         rm -f $DIR/$tfile
21868         $LFS setstripe -c 1 $DIR/$tfile
21869         # Offset 2T == 4k * 512M
21870         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
21871                 error "dd to 2T offset failed"
21872 }
21873 run_test 249 "Write above 2T file size"
21874
21875 test_250() {
21876         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
21877          && skip "no 16TB file size limit on ZFS"
21878
21879         $LFS setstripe -c 1 $DIR/$tfile
21880         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
21881         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
21882         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
21883         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
21884                 conv=notrunc,fsync && error "append succeeded"
21885         return 0
21886 }
21887 run_test 250 "Write above 16T limit"
21888
21889 test_251() {
21890         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
21891
21892         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
21893         #Skip once - writing the first stripe will succeed
21894         $LCTL set_param fail_loc=0xa0001407 fail_val=1
21895         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
21896                 error "short write happened"
21897
21898         $LCTL set_param fail_loc=0xa0001407 fail_val=1
21899         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
21900                 error "short read happened"
21901
21902         rm -f $DIR/$tfile
21903 }
21904 run_test 251 "Handling short read and write correctly"
21905
21906 test_252() {
21907         remote_mds_nodsh && skip "remote MDS with nodsh"
21908         remote_ost_nodsh && skip "remote OST with nodsh"
21909         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
21910                 skip_env "ldiskfs only test"
21911         fi
21912
21913         local tgt
21914         local dev
21915         local out
21916         local uuid
21917         local num
21918         local gen
21919
21920         # check lr_reader on OST0000
21921         tgt=ost1
21922         dev=$(facet_device $tgt)
21923         out=$(do_facet $tgt $LR_READER $dev)
21924         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21925         echo "$out"
21926         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
21927         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
21928                 error "Invalid uuid returned by $LR_READER on target $tgt"
21929         echo -e "uuid returned by $LR_READER is '$uuid'\n"
21930
21931         # check lr_reader -c on MDT0000
21932         tgt=mds1
21933         dev=$(facet_device $tgt)
21934         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
21935                 skip "$LR_READER does not support additional options"
21936         fi
21937         out=$(do_facet $tgt $LR_READER -c $dev)
21938         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21939         echo "$out"
21940         num=$(echo "$out" | grep -c "mdtlov")
21941         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
21942                 error "Invalid number of mdtlov clients returned by $LR_READER"
21943         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
21944
21945         # check lr_reader -cr on MDT0000
21946         out=$(do_facet $tgt $LR_READER -cr $dev)
21947         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
21948         echo "$out"
21949         echo "$out" | grep -q "^reply_data:$" ||
21950                 error "$LR_READER should have returned 'reply_data' section"
21951         num=$(echo "$out" | grep -c "client_generation")
21952         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
21953 }
21954 run_test 252 "check lr_reader tool"
21955
21956 test_253() {
21957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21958         remote_mds_nodsh && skip "remote MDS with nodsh"
21959         remote_mgs_nodsh && skip "remote MGS with nodsh"
21960
21961         local ostidx=0
21962         local rc=0
21963         local ost_name=$(ostname_from_index $ostidx)
21964
21965         # on the mdt's osc
21966         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
21967         do_facet $SINGLEMDS $LCTL get_param -n \
21968                 osp.$mdtosc_proc1.reserved_mb_high ||
21969                 skip  "remote MDS does not support reserved_mb_high"
21970
21971         rm -rf $DIR/$tdir
21972         wait_mds_ost_sync
21973         wait_delete_completed
21974         mkdir $DIR/$tdir
21975
21976         pool_add $TESTNAME || error "Pool creation failed"
21977         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
21978
21979         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
21980                 error "Setstripe failed"
21981
21982         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
21983
21984         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
21985                     grep "watermarks")
21986         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
21987
21988         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
21989                         osp.$mdtosc_proc1.prealloc_status)
21990         echo "prealloc_status $oa_status"
21991
21992         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
21993                 error "File creation should fail"
21994
21995         #object allocation was stopped, but we still able to append files
21996         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
21997                 oflag=append || error "Append failed"
21998
21999         rm -f $DIR/$tdir/$tfile.0
22000
22001         # For this test, we want to delete the files we created to go out of
22002         # space but leave the watermark, so we remain nearly out of space
22003         ost_watermarks_enospc_delete_files $tfile $ostidx
22004
22005         wait_delete_completed
22006
22007         sleep_maxage
22008
22009         for i in $(seq 10 12); do
22010                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
22011                         2>/dev/null || error "File creation failed after rm"
22012         done
22013
22014         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
22015                         osp.$mdtosc_proc1.prealloc_status)
22016         echo "prealloc_status $oa_status"
22017
22018         if (( oa_status != 0 )); then
22019                 error "Object allocation still disable after rm"
22020         fi
22021 }
22022 run_test 253 "Check object allocation limit"
22023
22024 test_254() {
22025         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22026         remote_mds_nodsh && skip "remote MDS with nodsh"
22027
22028         local mdt=$(facet_svc $SINGLEMDS)
22029
22030         do_facet $SINGLEMDS $LCTL get_param -n mdd.$mdt.changelog_size ||
22031                 skip "MDS does not support changelog_size"
22032
22033         local cl_user
22034
22035         changelog_register || error "changelog_register failed"
22036
22037         changelog_clear 0 || error "changelog_clear failed"
22038
22039         local size1=$(do_facet $SINGLEMDS \
22040                       $LCTL get_param -n mdd.$mdt.changelog_size)
22041         echo "Changelog size $size1"
22042
22043         rm -rf $DIR/$tdir
22044         $LFS mkdir -i 0 $DIR/$tdir
22045         # change something
22046         mkdir -p $DIR/$tdir/pics/2008/zachy
22047         touch $DIR/$tdir/pics/2008/zachy/timestamp
22048         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
22049         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
22050         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
22051         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
22052         rm $DIR/$tdir/pics/desktop.jpg
22053
22054         local size2=$(do_facet $SINGLEMDS \
22055                       $LCTL get_param -n mdd.$mdt.changelog_size)
22056         echo "Changelog size after work $size2"
22057
22058         (( $size2 > $size1 )) ||
22059                 error "new Changelog size=$size2 less than old size=$size1"
22060 }
22061 run_test 254 "Check changelog size"
22062
22063 ladvise_no_type()
22064 {
22065         local type=$1
22066         local file=$2
22067
22068         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
22069                 awk -F: '{print $2}' | grep $type > /dev/null
22070         if [ $? -ne 0 ]; then
22071                 return 0
22072         fi
22073         return 1
22074 }
22075
22076 ladvise_no_ioctl()
22077 {
22078         local file=$1
22079
22080         lfs ladvise -a willread $file > /dev/null 2>&1
22081         if [ $? -eq 0 ]; then
22082                 return 1
22083         fi
22084
22085         lfs ladvise -a willread $file 2>&1 |
22086                 grep "Inappropriate ioctl for device" > /dev/null
22087         if [ $? -eq 0 ]; then
22088                 return 0
22089         fi
22090         return 1
22091 }
22092
22093 percent() {
22094         bc <<<"scale=2; ($1 - $2) * 100 / $2"
22095 }
22096
22097 # run a random read IO workload
22098 # usage: random_read_iops <filename> <filesize> <iosize>
22099 random_read_iops() {
22100         local file=$1
22101         local fsize=$2
22102         local iosize=${3:-4096}
22103
22104         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
22105                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
22106 }
22107
22108 drop_file_oss_cache() {
22109         local file="$1"
22110         local nodes="$2"
22111
22112         $LFS ladvise -a dontneed $file 2>/dev/null ||
22113                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
22114 }
22115
22116 ladvise_willread_performance()
22117 {
22118         local repeat=10
22119         local average_origin=0
22120         local average_cache=0
22121         local average_ladvise=0
22122
22123         for ((i = 1; i <= $repeat; i++)); do
22124                 echo "Iter $i/$repeat: reading without willread hint"
22125                 cancel_lru_locks osc
22126                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
22127                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
22128                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
22129                 average_origin=$(bc <<<"$average_origin + $speed_origin")
22130
22131                 cancel_lru_locks osc
22132                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
22133                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
22134                 average_cache=$(bc <<<"$average_cache + $speed_cache")
22135
22136                 cancel_lru_locks osc
22137                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
22138                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
22139                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
22140                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
22141                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
22142         done
22143         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
22144         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
22145         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
22146
22147         speedup_cache=$(percent $average_cache $average_origin)
22148         speedup_ladvise=$(percent $average_ladvise $average_origin)
22149
22150         echo "Average uncached read: $average_origin"
22151         echo "Average speedup with OSS cached read: " \
22152                 "$average_cache = +$speedup_cache%"
22153         echo "Average speedup with ladvise willread: " \
22154                 "$average_ladvise = +$speedup_ladvise%"
22155
22156         local lowest_speedup=20
22157         if (( ${average_cache%.*} < $lowest_speedup )); then
22158                 echo "Speedup with OSS cached read less than $lowest_speedup%,"\
22159                      " got $average_cache%. Skipping ladvise willread check."
22160                 return 0
22161         fi
22162
22163         # the test won't work on ZFS until it supports 'ladvise dontneed', but
22164         # it is still good to run until then to exercise 'ladvise willread'
22165         ! $LFS ladvise -a dontneed $DIR/$tfile &&
22166                 [ "$ost1_FSTYPE" = "zfs" ] &&
22167                 echo "osd-zfs does not support dontneed or drop_caches" &&
22168                 return 0
22169
22170         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
22171         (( ${average_ladvise%.*} > ${lowest_speedup%.*} )) ||
22172                 error_not_in_vm "Speedup with willread is less than " \
22173                         "$lowest_speedup%, got $average_ladvise%"
22174 }
22175
22176 test_255a() {
22177         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
22178                 skip "lustre < 2.8.54 does not support ladvise "
22179         remote_ost_nodsh && skip "remote OST with nodsh"
22180
22181         stack_trap "rm -f $DIR/$tfile"
22182         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
22183
22184         ladvise_no_type willread $DIR/$tfile &&
22185                 skip "willread ladvise is not supported"
22186
22187         ladvise_no_ioctl $DIR/$tfile &&
22188                 skip "ladvise ioctl is not supported"
22189
22190         local size_mb=100
22191         local size=$((size_mb * 1048576))
22192         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
22193                 error "dd to $DIR/$tfile failed"
22194
22195         lfs ladvise -a willread $DIR/$tfile ||
22196                 error "Ladvise failed with no range argument"
22197
22198         lfs ladvise -a willread -s 0 $DIR/$tfile ||
22199                 error "Ladvise failed with no -l or -e argument"
22200
22201         lfs ladvise -a willread -e 1 $DIR/$tfile ||
22202                 error "Ladvise failed with only -e argument"
22203
22204         lfs ladvise -a willread -l 1 $DIR/$tfile ||
22205                 error "Ladvise failed with only -l argument"
22206
22207         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
22208                 error "End offset should not be smaller than start offset"
22209
22210         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
22211                 error "End offset should not be equal to start offset"
22212
22213         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
22214                 error "Ladvise failed with overflowing -s argument"
22215
22216         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
22217                 error "Ladvise failed with overflowing -e argument"
22218
22219         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
22220                 error "Ladvise failed with overflowing -l argument"
22221
22222         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
22223                 error "Ladvise succeeded with conflicting -l and -e arguments"
22224
22225         echo "Synchronous ladvise should wait"
22226         local delay=4
22227 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
22228         do_nodes $(comma_list $(osts_nodes)) \
22229                 $LCTL set_param fail_val=$delay fail_loc=0x237
22230
22231         local start_ts=$SECONDS
22232         lfs ladvise -a willread $DIR/$tfile ||
22233                 error "Ladvise failed with no range argument"
22234         local end_ts=$SECONDS
22235         local inteval_ts=$((end_ts - start_ts))
22236
22237         if [ $inteval_ts -lt $(($delay - 1)) ]; then
22238                 error "Synchronous advice didn't wait reply"
22239         fi
22240
22241         echo "Asynchronous ladvise shouldn't wait"
22242         local start_ts=$SECONDS
22243         lfs ladvise -a willread -b $DIR/$tfile ||
22244                 error "Ladvise failed with no range argument"
22245         local end_ts=$SECONDS
22246         local inteval_ts=$((end_ts - start_ts))
22247
22248         if [ $inteval_ts -gt $(($delay / 2)) ]; then
22249                 error "Asynchronous advice blocked"
22250         fi
22251
22252         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
22253         ladvise_willread_performance
22254 }
22255 run_test 255a "check 'lfs ladvise -a willread'"
22256
22257 facet_meminfo() {
22258         local facet=$1
22259         local info=$2
22260
22261         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
22262 }
22263
22264 test_255b() {
22265         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
22266                 skip "lustre < 2.8.54 does not support ladvise "
22267         remote_ost_nodsh && skip "remote OST with nodsh"
22268
22269         stack_trap "rm -f $DIR/$tfile"
22270         lfs setstripe -c 1 -i 0 $DIR/$tfile
22271
22272         ladvise_no_type dontneed $DIR/$tfile &&
22273                 skip "dontneed ladvise is not supported"
22274
22275         ladvise_no_ioctl $DIR/$tfile &&
22276                 skip "ladvise ioctl is not supported"
22277
22278         ! $LFS ladvise -a dontneed $DIR/$tfile &&
22279                 [ "$ost1_FSTYPE" = "zfs" ] &&
22280                 skip "zfs-osd does not support 'ladvise dontneed'"
22281
22282         local size_mb=100
22283         local size=$((size_mb * 1048576))
22284         # In order to prevent disturbance of other processes, only check 3/4
22285         # of the memory usage
22286         local kibibytes=$((size_mb * 1024 * 3 / 4))
22287
22288         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
22289                 error "dd to $DIR/$tfile failed"
22290
22291         #force write to complete before dropping OST cache & checking memory
22292         sync
22293
22294         local total=$(facet_meminfo ost1 MemTotal)
22295         echo "Total memory: $total KiB"
22296
22297         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
22298         local before_read=$(facet_meminfo ost1 Cached)
22299         echo "Cache used before read: $before_read KiB"
22300
22301         lfs ladvise -a willread $DIR/$tfile ||
22302                 error "Ladvise willread failed"
22303         local after_read=$(facet_meminfo ost1 Cached)
22304         echo "Cache used after read: $after_read KiB"
22305
22306         lfs ladvise -a dontneed $DIR/$tfile ||
22307                 error "Ladvise dontneed again failed"
22308         local no_read=$(facet_meminfo ost1 Cached)
22309         echo "Cache used after dontneed ladvise: $no_read KiB"
22310
22311         if [ $total -lt $((before_read + kibibytes)) ]; then
22312                 echo "Memory is too small, abort checking"
22313                 return 0
22314         fi
22315
22316         if [ $((before_read + kibibytes)) -gt $after_read ]; then
22317                 error "Ladvise willread should use more memory" \
22318                         "than $kibibytes KiB"
22319         fi
22320
22321         if [ $((no_read + kibibytes)) -gt $after_read ]; then
22322                 error "Ladvise dontneed should release more memory" \
22323                         "than $kibibytes KiB"
22324         fi
22325 }
22326 run_test 255b "check 'lfs ladvise -a dontneed'"
22327
22328 test_255c() {
22329         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
22330                 skip "lustre < 2.10.50 does not support lockahead"
22331
22332         local ost1_imp=$(get_osc_import_name client ost1)
22333         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
22334                          cut -d'.' -f2)
22335         local count
22336         local new_count
22337         local difference
22338         local i
22339         local rc
22340
22341         test_mkdir -p $DIR/$tdir
22342         $LFS setstripe -i 0 -c 1 $DIR/$tdir
22343
22344         #test 10 returns only success/failure
22345         i=10
22346         lockahead_test -d $DIR/$tdir -t $i -f $tfile
22347         rc=$?
22348         if [ $rc -eq 255 ]; then
22349                 error "Ladvise test${i} failed, ${rc}"
22350         fi
22351
22352         #test 11 counts lock enqueue requests, all others count new locks
22353         i=11
22354         count=$(do_facet ost1 \
22355                 $LCTL get_param -n ost.OSS.ost.stats)
22356         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
22357
22358         lockahead_test -d $DIR/$tdir -t $i -f $tfile
22359         rc=$?
22360         if [ $rc -eq 255 ]; then
22361                 error "Ladvise test${i} failed, ${rc}"
22362         fi
22363
22364         new_count=$(do_facet ost1 \
22365                 $LCTL get_param -n ost.OSS.ost.stats)
22366         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
22367                    awk '{ print $2 }')
22368
22369         difference="$((new_count - count))"
22370         if [ $difference -ne $rc ]; then
22371                 error "Ladvise test${i}, bad enqueue count, returned " \
22372                       "${rc}, actual ${difference}"
22373         fi
22374
22375         for i in $(seq 12 21); do
22376                 # If we do not do this, we run the risk of having too many
22377                 # locks and starting lock cancellation while we are checking
22378                 # lock counts.
22379                 cancel_lru_locks osc
22380
22381                 count=$($LCTL get_param -n \
22382                        ldlm.namespaces.$imp_name.lock_unused_count)
22383
22384                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
22385                 rc=$?
22386                 if [ $rc -eq 255 ]; then
22387                         error "Ladvise test ${i} failed, ${rc}"
22388                 fi
22389
22390                 new_count=$($LCTL get_param -n \
22391                        ldlm.namespaces.$imp_name.lock_unused_count)
22392                 difference="$((new_count - count))"
22393
22394                 # Test 15 output is divided by 100 to map down to valid return
22395                 if [ $i -eq 15 ]; then
22396                         rc="$((rc * 100))"
22397                 fi
22398
22399                 if [ $difference -ne $rc ]; then
22400                         error "Ladvise test ${i}, bad lock count, returned " \
22401                               "${rc}, actual ${difference}"
22402                 fi
22403         done
22404
22405         #test 22 returns only success/failure
22406         i=22
22407         lockahead_test -d $DIR/$tdir -t $i -f $tfile
22408         rc=$?
22409         if [ $rc -eq 255 ]; then
22410                 error "Ladvise test${i} failed, ${rc}"
22411         fi
22412 }
22413 run_test 255c "suite of ladvise lockahead tests"
22414
22415 test_256() {
22416         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22417         remote_mds_nodsh && skip "remote MDS with nodsh"
22418         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
22419         changelog_users $SINGLEMDS | grep "^cl" &&
22420                 skip "active changelog user"
22421
22422         local cl_user
22423         local cat_sl
22424         local mdt_dev
22425
22426         mdt_dev=$(facet_device $SINGLEMDS)
22427         echo $mdt_dev
22428
22429         changelog_register || error "changelog_register failed"
22430
22431         rm -rf $DIR/$tdir
22432         mkdir_on_mdt -i$(($(facet_number $SINGLEMDS) - 1)) $DIR/$tdir
22433
22434         changelog_clear 0 || error "changelog_clear failed"
22435
22436         # change something
22437         touch $DIR/$tdir/{1..10}
22438
22439         # stop the MDT
22440         stop $SINGLEMDS || error "Fail to stop MDT"
22441
22442         # remount the MDT
22443         start $SINGLEMDS $(facet_device $SINGLEMDS) $MDS_MOUNT_OPTS ||
22444                 error "Fail to start MDT"
22445
22446         #after mount new plainllog is used
22447         touch $DIR/$tdir/{11..19}
22448         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
22449         stack_trap "rm -f $tmpfile"
22450         cat_sl=$(do_facet $SINGLEMDS "sync; \
22451                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
22452                  llog_reader $tmpfile | grep -c type=1064553b")
22453         do_facet $SINGLEMDS llog_reader $tmpfile
22454
22455         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
22456
22457         changelog_clear 0 || error "changelog_clear failed"
22458
22459         cat_sl=$(do_facet $SINGLEMDS "sync; \
22460                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
22461                  llog_reader $tmpfile | grep -c type=1064553b")
22462
22463         if (( cat_sl == 2 )); then
22464                 error "Empty plain llog was not deleted from changelog catalog"
22465         elif (( cat_sl != 1 )); then
22466                 error "Active plain llog shouldn't be deleted from catalog"
22467         fi
22468 }
22469 run_test 256 "Check llog delete for empty and not full state"
22470
22471 test_257() {
22472         remote_mds_nodsh && skip "remote MDS with nodsh"
22473         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
22474                 skip "Need MDS version at least 2.8.55"
22475
22476         test_mkdir $DIR/$tdir
22477
22478         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
22479                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
22480         stat $DIR/$tdir
22481
22482 #define OBD_FAIL_MDS_XATTR_REP                  0x161
22483         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
22484         local facet=mds$((mdtidx + 1))
22485         set_nodes_failloc $(facet_active_host $facet) 0x80000161
22486         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
22487
22488         stop $facet || error "stop MDS failed"
22489         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
22490                 error "start MDS fail"
22491         wait_recovery_complete $facet
22492 }
22493 run_test 257 "xattr locks are not lost"
22494
22495 # Verify we take the i_mutex when security requires it
22496 test_258a() {
22497 #define OBD_FAIL_IMUTEX_SEC 0x141c
22498         $LCTL set_param fail_loc=0x141c
22499         touch $DIR/$tfile
22500         chmod u+s $DIR/$tfile
22501         chmod a+rwx $DIR/$tfile
22502         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
22503         RC=$?
22504         if [ $RC -ne 0 ]; then
22505                 error "error, failed to take i_mutex, rc=$?"
22506         fi
22507         rm -f $DIR/$tfile
22508 }
22509 run_test 258a "verify i_mutex security behavior when suid attributes is set"
22510
22511 # Verify we do NOT take the i_mutex in the normal case
22512 test_258b() {
22513 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
22514         $LCTL set_param fail_loc=0x141d
22515         touch $DIR/$tfile
22516         chmod a+rwx $DIR
22517         chmod a+rw $DIR/$tfile
22518         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
22519         RC=$?
22520         if [ $RC -ne 0 ]; then
22521                 error "error, took i_mutex unnecessarily, rc=$?"
22522         fi
22523         rm -f $DIR/$tfile
22524
22525 }
22526 run_test 258b "verify i_mutex security behavior"
22527
22528 test_259() {
22529         local file=$DIR/$tfile
22530         local before
22531         local after
22532
22533         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
22534
22535         stack_trap "rm -f $file" EXIT
22536
22537         wait_delete_completed
22538         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22539         echo "before: $before"
22540
22541         $LFS setstripe -i 0 -c 1 $file
22542         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
22543         sync_all_data
22544         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22545         echo "after write: $after"
22546
22547 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
22548         do_facet ost1 $LCTL set_param fail_loc=0x2301
22549         $TRUNCATE $file 0
22550         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22551         echo "after truncate: $after"
22552
22553         stop ost1
22554         do_facet ost1 $LCTL set_param fail_loc=0
22555         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
22556         sleep 2
22557         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
22558         echo "after restart: $after"
22559         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
22560                 error "missing truncate?"
22561
22562         return 0
22563 }
22564 run_test 259 "crash at delayed truncate"
22565
22566 test_260() {
22567 #define OBD_FAIL_MDC_CLOSE               0x806
22568         $LCTL set_param fail_loc=0x80000806
22569         touch $DIR/$tfile
22570
22571 }
22572 run_test 260 "Check mdc_close fail"
22573
22574 ### Data-on-MDT sanity tests ###
22575 test_270a() {
22576         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22577                 skip "Need MDS version at least 2.10.55 for DoM"
22578
22579         # create DoM file
22580         local dom=$DIR/$tdir/dom_file
22581         local tmp=$DIR/$tdir/tmp_file
22582
22583         mkdir_on_mdt0 $DIR/$tdir
22584
22585         # basic checks for DoM component creation
22586         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
22587                 error "Can set MDT layout to non-first entry"
22588
22589         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
22590                 error "Can define multiple entries as MDT layout"
22591
22592         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
22593
22594         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
22595         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
22596         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
22597
22598         local mdtidx=$($LFS getstripe -m $dom)
22599         local mdtname=MDT$(printf %04x $mdtidx)
22600         local facet=mds$((mdtidx + 1))
22601         local space_check=1
22602
22603         # Skip free space checks with ZFS
22604         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
22605
22606         # write
22607         sync
22608         local size_tmp=$((65536 * 3))
22609         local mdtfree1=$(do_facet $facet \
22610                          lctl get_param -n osd*.*$mdtname.kbytesfree)
22611
22612         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
22613         # check also direct IO along write
22614         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
22615         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
22616         sync
22617         cmp $tmp $dom || error "file data is different"
22618         [ $(stat -c%s $dom) == $size_tmp ] ||
22619                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
22620         if [ $space_check == 1 ]; then
22621                 local mdtfree2=$(do_facet $facet \
22622                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
22623
22624                 # increase in usage from by $size_tmp
22625                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
22626                         error "MDT free space wrong after write: " \
22627                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
22628         fi
22629
22630         # truncate
22631         local size_dom=10000
22632
22633         $TRUNCATE $dom $size_dom
22634         [ $(stat -c%s $dom) == $size_dom ] ||
22635                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
22636         if [ $space_check == 1 ]; then
22637                 mdtfree1=$(do_facet $facet \
22638                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22639                 # decrease in usage from $size_tmp to new $size_dom
22640                 [ $(($mdtfree1 - $mdtfree2)) -ge \
22641                   $(((size_tmp - size_dom) / 1024)) ] ||
22642                         error "MDT free space is wrong after truncate: " \
22643                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
22644         fi
22645
22646         # append
22647         cat $tmp >> $dom
22648         sync
22649         size_dom=$((size_dom + size_tmp))
22650         [ $(stat -c%s $dom) == $size_dom ] ||
22651                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
22652         if [ $space_check == 1 ]; then
22653                 mdtfree2=$(do_facet $facet \
22654                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22655                 # increase in usage by $size_tmp from previous
22656                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
22657                         error "MDT free space is wrong after append: " \
22658                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
22659         fi
22660
22661         # delete
22662         rm $dom
22663         if [ $space_check == 1 ]; then
22664                 mdtfree1=$(do_facet $facet \
22665                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
22666                 # decrease in usage by $size_dom from previous
22667                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
22668                         error "MDT free space is wrong after removal: " \
22669                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
22670         fi
22671
22672         # combined striping
22673         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
22674                 error "Can't create DoM + OST striping"
22675
22676         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
22677         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
22678         # check also direct IO along write
22679         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
22680         sync
22681         cmp $tmp $dom || error "file data is different"
22682         [ $(stat -c%s $dom) == $size_tmp ] ||
22683                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
22684         rm $dom $tmp
22685
22686         return 0
22687 }
22688 run_test 270a "DoM: basic functionality tests"
22689
22690 test_270b() {
22691         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22692                 skip "Need MDS version at least 2.10.55"
22693
22694         local dom=$DIR/$tdir/dom_file
22695         local max_size=1048576
22696
22697         mkdir -p $DIR/$tdir
22698         $LFS setstripe -E $max_size -L mdt $dom
22699
22700         # truncate over the limit
22701         $TRUNCATE $dom $(($max_size + 1)) &&
22702                 error "successful truncate over the maximum size"
22703         # write over the limit
22704         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
22705                 error "successful write over the maximum size"
22706         # append over the limit
22707         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
22708         echo "12345" >> $dom && error "successful append over the maximum size"
22709         rm $dom
22710
22711         return 0
22712 }
22713 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
22714
22715 test_270c() {
22716         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22717                 skip "Need MDS version at least 2.10.55"
22718
22719         mkdir -p $DIR/$tdir
22720         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22721
22722         # check files inherit DoM EA
22723         touch $DIR/$tdir/first
22724         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
22725                 error "bad pattern"
22726         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
22727                 error "bad stripe count"
22728         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
22729                 error "bad stripe size"
22730
22731         # check directory inherits DoM EA and uses it as default
22732         mkdir $DIR/$tdir/subdir
22733         touch $DIR/$tdir/subdir/second
22734         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
22735                 error "bad pattern in sub-directory"
22736         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
22737                 error "bad stripe count in sub-directory"
22738         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
22739                 error "bad stripe size in sub-directory"
22740         return 0
22741 }
22742 run_test 270c "DoM: DoM EA inheritance tests"
22743
22744 test_270d() {
22745         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22746                 skip "Need MDS version at least 2.10.55"
22747
22748         mkdir -p $DIR/$tdir
22749         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
22750
22751         # inherit default DoM striping
22752         mkdir $DIR/$tdir/subdir
22753         touch $DIR/$tdir/subdir/f1
22754
22755         # change default directory striping
22756         $LFS setstripe -c 1 $DIR/$tdir/subdir
22757         touch $DIR/$tdir/subdir/f2
22758         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
22759                 error "wrong default striping in file 2"
22760         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
22761                 error "bad pattern in file 2"
22762         return 0
22763 }
22764 run_test 270d "DoM: change striping from DoM to RAID0"
22765
22766 test_270e() {
22767         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22768                 skip "Need MDS version at least 2.10.55"
22769
22770         mkdir -p $DIR/$tdir/dom
22771         mkdir -p $DIR/$tdir/norm
22772         DOMFILES=20
22773         NORMFILES=10
22774         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
22775         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
22776
22777         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
22778         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
22779
22780         # find DoM files by layout
22781         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
22782         [ $NUM -eq  $DOMFILES ] ||
22783                 error "lfs find -L: found $NUM, expected $DOMFILES"
22784         echo "Test 1: lfs find 20 DOM files by layout: OK"
22785
22786         # there should be 1 dir with default DOM striping
22787         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
22788         [ $NUM -eq  1 ] ||
22789                 error "lfs find -L: found $NUM, expected 1 dir"
22790         echo "Test 2: lfs find 1 DOM dir by layout: OK"
22791
22792         # find DoM files by stripe size
22793         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
22794         [ $NUM -eq  $DOMFILES ] ||
22795                 error "lfs find -S: found $NUM, expected $DOMFILES"
22796         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
22797
22798         # find files by stripe offset except DoM files
22799         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
22800         [ $NUM -eq  $NORMFILES ] ||
22801                 error "lfs find -i: found $NUM, expected $NORMFILES"
22802         echo "Test 5: lfs find no DOM files by stripe index: OK"
22803         return 0
22804 }
22805 run_test 270e "DoM: lfs find with DoM files test"
22806
22807 test_270f() {
22808         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
22809                 skip "Need MDS version at least 2.10.55"
22810
22811         local mdtname=${FSNAME}-MDT0000-mdtlov
22812         local dom=$DIR/$tdir/dom_file
22813         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
22814                                                 lod.$mdtname.dom_stripesize)
22815         local dom_limit=131072
22816
22817         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
22818         local dom_current=$(do_facet mds1 $LCTL get_param -n \
22819                                                 lod.$mdtname.dom_stripesize)
22820         [ ${dom_limit} -eq ${dom_current} ] ||
22821                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
22822
22823         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22824         $LFS setstripe -d $DIR/$tdir
22825         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
22826                 error "Can't set directory default striping"
22827
22828         # exceed maximum stripe size
22829         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
22830                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
22831         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
22832                 error "Able to create DoM component size more than LOD limit"
22833
22834         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
22835         dom_current=$(do_facet mds1 $LCTL get_param -n \
22836                                                 lod.$mdtname.dom_stripesize)
22837         [ 0 -eq ${dom_current} ] ||
22838                 error "Can't set zero DoM stripe limit"
22839         rm $dom
22840
22841         # attempt to create DoM file on server with disabled DoM should
22842         # remove DoM entry from layout and be succeed
22843         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
22844                 error "Can't create DoM file (DoM is disabled)"
22845         [ $($LFS getstripe -L $dom) == "mdt" ] &&
22846                 error "File has DoM component while DoM is disabled"
22847         rm $dom
22848
22849         # attempt to create DoM file with only DoM stripe should return error
22850         $LFS setstripe -E $dom_limit -L mdt $dom &&
22851                 error "Able to create DoM-only file while DoM is disabled"
22852
22853         # too low values to be aligned with smallest stripe size 64K
22854         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
22855         dom_current=$(do_facet mds1 $LCTL get_param -n \
22856                                                 lod.$mdtname.dom_stripesize)
22857         [ 30000 -eq ${dom_current} ] &&
22858                 error "Can set too small DoM stripe limit"
22859
22860         # 64K is a minimal stripe size in Lustre, expect limit of that size
22861         [ 65536 -eq ${dom_current} ] ||
22862                 error "Limit is not set to 64K but ${dom_current}"
22863
22864         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
22865         dom_current=$(do_facet mds1 $LCTL get_param -n \
22866                                                 lod.$mdtname.dom_stripesize)
22867         echo $dom_current
22868         [ 2147483648 -eq ${dom_current} ] &&
22869                 error "Can set too large DoM stripe limit"
22870
22871         do_facet mds1 $LCTL set_param -n \
22872                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
22873         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
22874                 error "Can't create DoM component size after limit change"
22875         do_facet mds1 $LCTL set_param -n \
22876                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
22877         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
22878                 error "Can't create DoM file after limit decrease"
22879         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
22880                 error "Can create big DoM component after limit decrease"
22881         touch ${dom}_def ||
22882                 error "Can't create file with old default layout"
22883
22884         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
22885         return 0
22886 }
22887 run_test 270f "DoM: maximum DoM stripe size checks"
22888
22889 test_270g() {
22890         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
22891                 skip "Need MDS version at least 2.13.52"
22892         local dom=$DIR/$tdir/$tfile
22893
22894         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22895         local lodname=${FSNAME}-MDT0000-mdtlov
22896
22897         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22898         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
22899         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
22900         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22901
22902         local dom_limit=1024
22903         local dom_threshold="50%"
22904
22905         $LFS setstripe -d $DIR/$tdir
22906         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
22907                 error "Can't set directory default striping"
22908
22909         do_facet mds1 $LCTL set_param -n \
22910                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
22911         # set 0 threshold and create DOM file to change tunable stripesize
22912         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
22913         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
22914                 error "Failed to create $dom file"
22915         # now tunable dom_cur_stripesize should reach maximum
22916         local dom_current=$(do_facet mds1 $LCTL get_param -n \
22917                                         lod.${lodname}.dom_stripesize_cur_kb)
22918         [[ $dom_current == $dom_limit ]] ||
22919                 error "Current DOM stripesize is not maximum"
22920         rm $dom
22921
22922         # set threshold for further tests
22923         do_facet mds1 $LCTL set_param -n \
22924                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
22925         echo "DOM threshold is $dom_threshold free space"
22926         local dom_def
22927         local dom_set
22928         # Spoof bfree to exceed threshold
22929         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
22930         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
22931         for spfree in 40 20 0 15 30 55; do
22932                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
22933                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
22934                         error "Failed to create $dom file"
22935                 dom_def=$(do_facet mds1 $LCTL get_param -n \
22936                                         lod.${lodname}.dom_stripesize_cur_kb)
22937                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
22938                 [[ $dom_def != $dom_current ]] ||
22939                         error "Default stripe size was not changed"
22940                 if (( spfree > 0 )) ; then
22941                         dom_set=$($LFS getstripe -S $dom)
22942                         (( dom_set == dom_def * 1024 )) ||
22943                                 error "DOM component size is still old"
22944                 else
22945                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
22946                                 error "DoM component is set with no free space"
22947                 fi
22948                 rm $dom
22949                 dom_current=$dom_def
22950         done
22951 }
22952 run_test 270g "DoM: default DoM stripe size depends on free space"
22953
22954 test_270h() {
22955         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
22956                 skip "Need MDS version at least 2.13.53"
22957
22958         local mdtname=${FSNAME}-MDT0000-mdtlov
22959         local dom=$DIR/$tdir/$tfile
22960         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
22961
22962         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
22963         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
22964
22965         $LFS mkdir -i 0 -c 1 $DIR/$tdir
22966         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
22967                 error "can't create OST file"
22968         # mirrored file with DOM entry in the second mirror
22969         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
22970                 error "can't create mirror with DoM component"
22971
22972         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
22973
22974         # DOM component in the middle and has other enries in the same mirror,
22975         # should succeed but lost DoM component
22976         $LFS setstripe --copy=${dom}_1 $dom ||
22977                 error "Can't create file from OST|DOM mirror layout"
22978         # check new file has no DoM layout after all
22979         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
22980                 error "File has DoM component while DoM is disabled"
22981 }
22982 run_test 270h "DoM: DoM stripe removal when disabled on server"
22983
22984 test_270i() {
22985         (( $MDS1_VERSION >= $(version_code 2.14.54) )) ||
22986                 skip "Need MDS version at least 2.14.54"
22987
22988         mkdir $DIR/$tdir
22989         # DoM with plain layout
22990         $LFS setstripe -L mdt -S 128k -c -1 $DIR/$tdir &&
22991                 error "default plain layout with DoM must fail"
22992         $LFS setstripe -L mdt -S 128k -c -1 $DIR/$tdir/$tfile &&
22993                 error "setstripe plain file layout with DoM must fail"
22994         $LFS setstripe -E 1M -L mdt -S 128k -c -1 -E eof $DIR/$tdir &&
22995                 error "default DoM layout with bad striping must fail"
22996         $LFS setstripe -E 1M -L mdt -S 128k -c -1 -E eof $DIR/$tdir/$tfile &&
22997                 error "setstripe to DoM layout with bad striping must fail"
22998         return 0
22999 }
23000 run_test 270i "DoM: setting invalid DoM striping should fail"
23001
23002 test_271a() {
23003         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23004                 skip "Need MDS version at least 2.10.55"
23005
23006         local dom=$DIR/$tdir/dom
23007
23008         mkdir -p $DIR/$tdir
23009
23010         $LFS setstripe -E 1024K -L mdt $dom
23011
23012         lctl set_param -n mdc.*.stats=clear
23013         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
23014         cat $dom > /dev/null
23015         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
23016         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
23017         ls $dom
23018         rm -f $dom
23019 }
23020 run_test 271a "DoM: data is cached for read after write"
23021
23022 test_271b() {
23023         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23024                 skip "Need MDS version at least 2.10.55"
23025
23026         local dom=$DIR/$tdir/dom
23027
23028         mkdir -p $DIR/$tdir
23029
23030         $LFS setstripe -E 1024K -L mdt -E EOF $dom
23031
23032         lctl set_param -n mdc.*.stats=clear
23033         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
23034         cancel_lru_locks mdc
23035         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
23036         # second stat to check size is cached on client
23037         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
23038         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
23039         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
23040         rm -f $dom
23041 }
23042 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
23043
23044 test_271ba() {
23045         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23046                 skip "Need MDS version at least 2.10.55"
23047
23048         local dom=$DIR/$tdir/dom
23049
23050         mkdir -p $DIR/$tdir
23051
23052         $LFS setstripe -E 1024K -L mdt -E EOF $dom
23053
23054         lctl set_param -n mdc.*.stats=clear
23055         lctl set_param -n osc.*.stats=clear
23056         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
23057         cancel_lru_locks mdc
23058         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
23059         # second stat to check size is cached on client
23060         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
23061         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
23062         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
23063         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
23064         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
23065         rm -f $dom
23066 }
23067 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
23068
23069
23070 get_mdc_stats() {
23071         local mdtidx=$1
23072         local param=$2
23073         local mdt=MDT$(printf %04x $mdtidx)
23074
23075         if [ -z $param ]; then
23076                 lctl get_param -n mdc.*$mdt*.stats
23077         else
23078                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
23079         fi
23080 }
23081
23082 test_271c() {
23083         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
23084                 skip "Need MDS version at least 2.10.55"
23085
23086         local dom=$DIR/$tdir/dom
23087
23088         mkdir -p $DIR/$tdir
23089
23090         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23091
23092         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
23093         local facet=mds$((mdtidx + 1))
23094
23095         cancel_lru_locks mdc
23096         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
23097         createmany -o $dom 1000
23098         lctl set_param -n mdc.*.stats=clear
23099         smalliomany -w $dom 1000 200
23100         get_mdc_stats $mdtidx
23101         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
23102         # Each file has 1 open, 1 IO enqueues, total 2000
23103         # but now we have also +1 getxattr for security.capability, total 3000
23104         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
23105         unlinkmany $dom 1000
23106
23107         cancel_lru_locks mdc
23108         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
23109         createmany -o $dom 1000
23110         lctl set_param -n mdc.*.stats=clear
23111         smalliomany -w $dom 1000 200
23112         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
23113         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
23114         # for OPEN and IO lock.
23115         [ $((enq - enq_2)) -ge 1000 ] ||
23116                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
23117         unlinkmany $dom 1000
23118         return 0
23119 }
23120 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
23121
23122 cleanup_271def_tests() {
23123         trap 0
23124         rm -f $1
23125 }
23126
23127 test_271d() {
23128         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
23129                 skip "Need MDS version at least 2.10.57"
23130
23131         local dom=$DIR/$tdir/dom
23132         local tmp=$TMP/$tfile
23133         trap "cleanup_271def_tests $tmp" EXIT
23134
23135         mkdir -p $DIR/$tdir
23136
23137         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23138
23139         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
23140
23141         cancel_lru_locks mdc
23142         dd if=/dev/urandom of=$tmp bs=1000 count=1
23143         dd if=$tmp of=$dom bs=1000 count=1
23144         cancel_lru_locks mdc
23145
23146         cat /etc/hosts >> $tmp
23147         lctl set_param -n mdc.*.stats=clear
23148
23149         # append data to the same file it should update local page
23150         echo "Append to the same page"
23151         cat /etc/hosts >> $dom
23152         local num=$(get_mdc_stats $mdtidx ost_read)
23153         local ra=$(get_mdc_stats $mdtidx req_active)
23154         local rw=$(get_mdc_stats $mdtidx req_waittime)
23155
23156         [ -z $num ] || error "$num READ RPC occured"
23157         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23158         echo "... DONE"
23159
23160         # compare content
23161         cmp $tmp $dom || error "file miscompare"
23162
23163         cancel_lru_locks mdc
23164         lctl set_param -n mdc.*.stats=clear
23165
23166         echo "Open and read file"
23167         cat $dom > /dev/null
23168         local num=$(get_mdc_stats $mdtidx ost_read)
23169         local ra=$(get_mdc_stats $mdtidx req_active)
23170         local rw=$(get_mdc_stats $mdtidx req_waittime)
23171
23172         [ -z $num ] || error "$num READ RPC occured"
23173         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23174         echo "... DONE"
23175
23176         # compare content
23177         cmp $tmp $dom || error "file miscompare"
23178
23179         return 0
23180 }
23181 run_test 271d "DoM: read on open (1K file in reply buffer)"
23182
23183 test_271f() {
23184         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
23185                 skip "Need MDS version at least 2.10.57"
23186
23187         local dom=$DIR/$tdir/dom
23188         local tmp=$TMP/$tfile
23189         trap "cleanup_271def_tests $tmp" EXIT
23190
23191         mkdir -p $DIR/$tdir
23192
23193         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
23194
23195         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
23196
23197         cancel_lru_locks mdc
23198         dd if=/dev/urandom of=$tmp bs=265000 count=1
23199         dd if=$tmp of=$dom bs=265000 count=1
23200         cancel_lru_locks mdc
23201         cat /etc/hosts >> $tmp
23202         lctl set_param -n mdc.*.stats=clear
23203
23204         echo "Append to the same page"
23205         cat /etc/hosts >> $dom
23206         local num=$(get_mdc_stats $mdtidx ost_read)
23207         local ra=$(get_mdc_stats $mdtidx req_active)
23208         local rw=$(get_mdc_stats $mdtidx req_waittime)
23209
23210         [ -z $num ] || error "$num READ RPC occured"
23211         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23212         echo "... DONE"
23213
23214         # compare content
23215         cmp $tmp $dom || error "file miscompare"
23216
23217         cancel_lru_locks mdc
23218         lctl set_param -n mdc.*.stats=clear
23219
23220         echo "Open and read file"
23221         cat $dom > /dev/null
23222         local num=$(get_mdc_stats $mdtidx ost_read)
23223         local ra=$(get_mdc_stats $mdtidx req_active)
23224         local rw=$(get_mdc_stats $mdtidx req_waittime)
23225
23226         [ -z $num ] && num=0
23227         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
23228         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
23229         echo "... DONE"
23230
23231         # compare content
23232         cmp $tmp $dom || error "file miscompare"
23233
23234         return 0
23235 }
23236 run_test 271f "DoM: read on open (200K file and read tail)"
23237
23238 test_271g() {
23239         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
23240                 skip "Skipping due to old client or server version"
23241
23242         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
23243         # to get layout
23244         $CHECKSTAT -t file $DIR1/$tfile
23245
23246         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
23247         MULTIOP_PID=$!
23248         sleep 1
23249         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
23250         $LCTL set_param fail_loc=0x80000314
23251         rm $DIR1/$tfile || error "Unlink fails"
23252         RC=$?
23253         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
23254         [ $RC -eq 0 ] || error "Failed write to stale object"
23255 }
23256 run_test 271g "Discard DoM data vs client flush race"
23257
23258 test_272a() {
23259         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23260                 skip "Need MDS version at least 2.11.50"
23261
23262         local dom=$DIR/$tdir/dom
23263         mkdir -p $DIR/$tdir
23264
23265         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
23266         dd if=/dev/urandom of=$dom bs=512K count=1 ||
23267                 error "failed to write data into $dom"
23268         local old_md5=$(md5sum $dom)
23269
23270         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
23271                 error "failed to migrate to the same DoM component"
23272
23273         local new_md5=$(md5sum $dom)
23274
23275         [ "$old_md5" == "$new_md5" ] ||
23276                 error "md5sum differ: $old_md5, $new_md5"
23277
23278         [ $($LFS getstripe -c $dom) -eq 2 ] ||
23279                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
23280 }
23281 run_test 272a "DoM migration: new layout with the same DOM component"
23282
23283 test_272b() {
23284         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23285                 skip "Need MDS version at least 2.11.50"
23286
23287         local dom=$DIR/$tdir/dom
23288         mkdir -p $DIR/$tdir
23289         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23290
23291         local mdtidx=$($LFS getstripe -m $dom)
23292         local mdtname=MDT$(printf %04x $mdtidx)
23293         local facet=mds$((mdtidx + 1))
23294
23295         local mdtfree1=$(do_facet $facet \
23296                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23297         dd if=/dev/urandom of=$dom bs=2M count=1 ||
23298                 error "failed to write data into $dom"
23299         local old_md5=$(md5sum $dom)
23300         cancel_lru_locks mdc
23301         local mdtfree1=$(do_facet $facet \
23302                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23303
23304         $LFS migrate -c2 $dom ||
23305                 error "failed to migrate to the new composite layout"
23306         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
23307                 error "MDT stripe was not removed"
23308
23309         cancel_lru_locks mdc
23310         local new_md5=$(md5sum $dom)
23311         [ "$old_md5" == "$new_md5" ] ||
23312                 error "$old_md5 != $new_md5"
23313
23314         # Skip free space checks with ZFS
23315         if [ "$(facet_fstype $facet)" != "zfs" ]; then
23316                 local mdtfree2=$(do_facet $facet \
23317                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23318                 [ $mdtfree2 -gt $mdtfree1 ] ||
23319                         error "MDT space is not freed after migration"
23320         fi
23321         return 0
23322 }
23323 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
23324
23325 test_272c() {
23326         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23327                 skip "Need MDS version at least 2.11.50"
23328
23329         local dom=$DIR/$tdir/$tfile
23330         mkdir -p $DIR/$tdir
23331         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23332
23333         local mdtidx=$($LFS getstripe -m $dom)
23334         local mdtname=MDT$(printf %04x $mdtidx)
23335         local facet=mds$((mdtidx + 1))
23336
23337         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23338                 error "failed to write data into $dom"
23339         local old_md5=$(md5sum $dom)
23340         cancel_lru_locks mdc
23341         local mdtfree1=$(do_facet $facet \
23342                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23343
23344         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
23345                 error "failed to migrate to the new composite layout"
23346         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
23347                 error "MDT stripe was not removed"
23348
23349         cancel_lru_locks mdc
23350         local new_md5=$(md5sum $dom)
23351         [ "$old_md5" == "$new_md5" ] ||
23352                 error "$old_md5 != $new_md5"
23353
23354         # Skip free space checks with ZFS
23355         if [ "$(facet_fstype $facet)" != "zfs" ]; then
23356                 local mdtfree2=$(do_facet $facet \
23357                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23358                 [ $mdtfree2 -gt $mdtfree1 ] ||
23359                         error "MDS space is not freed after migration"
23360         fi
23361         return 0
23362 }
23363 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
23364
23365 test_272d() {
23366         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
23367                 skip "Need MDS version at least 2.12.55"
23368
23369         local dom=$DIR/$tdir/$tfile
23370         mkdir -p $DIR/$tdir
23371         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
23372
23373         local mdtidx=$($LFS getstripe -m $dom)
23374         local mdtname=MDT$(printf %04x $mdtidx)
23375         local facet=mds$((mdtidx + 1))
23376
23377         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23378                 error "failed to write data into $dom"
23379         local old_md5=$(md5sum $dom)
23380         cancel_lru_locks mdc
23381         local mdtfree1=$(do_facet $facet \
23382                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23383
23384         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
23385                 error "failed mirroring to the new composite layout"
23386         $LFS mirror resync $dom ||
23387                 error "failed mirror resync"
23388         $LFS mirror split --mirror-id 1 -d $dom ||
23389                 error "failed mirror split"
23390
23391         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
23392                 error "MDT stripe was not removed"
23393
23394         cancel_lru_locks mdc
23395         local new_md5=$(md5sum $dom)
23396         [ "$old_md5" == "$new_md5" ] ||
23397                 error "$old_md5 != $new_md5"
23398
23399         # Skip free space checks with ZFS
23400         if [ "$(facet_fstype $facet)" != "zfs" ]; then
23401                 local mdtfree2=$(do_facet $facet \
23402                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
23403                 [ $mdtfree2 -gt $mdtfree1 ] ||
23404                         error "MDS space is not freed after DOM mirror deletion"
23405         fi
23406         return 0
23407 }
23408 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
23409
23410 test_272e() {
23411         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
23412                 skip "Need MDS version at least 2.12.55"
23413
23414         local dom=$DIR/$tdir/$tfile
23415         mkdir -p $DIR/$tdir
23416         $LFS setstripe -c 2 $dom
23417
23418         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23419                 error "failed to write data into $dom"
23420         local old_md5=$(md5sum $dom)
23421         cancel_lru_locks
23422
23423         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
23424                 error "failed mirroring to the DOM layout"
23425         $LFS mirror resync $dom ||
23426                 error "failed mirror resync"
23427         $LFS mirror split --mirror-id 1 -d $dom ||
23428                 error "failed mirror split"
23429
23430         [[ $($LFS getstripe -L --component-start=0 $dom) == 'mdt' ]] ||
23431                 error "MDT stripe wasn't set"
23432
23433         cancel_lru_locks
23434         local new_md5=$(md5sum $dom)
23435         [ "$old_md5" == "$new_md5" ] ||
23436                 error "$old_md5 != $new_md5"
23437
23438         return 0
23439 }
23440 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
23441
23442 test_272f() {
23443         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
23444                 skip "Need MDS version at least 2.12.55"
23445
23446         local dom=$DIR/$tdir/$tfile
23447         mkdir -p $DIR/$tdir
23448         $LFS setstripe -c 2 $dom
23449
23450         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
23451                 error "failed to write data into $dom"
23452         local old_md5=$(md5sum $dom)
23453         cancel_lru_locks
23454
23455         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
23456                 error "failed migrating to the DOM file"
23457
23458         [[ $($LFS getstripe -L --component-start=0 $dom) == 'mdt' ]] ||
23459                 error "MDT stripe wasn't set"
23460
23461         cancel_lru_locks
23462         local new_md5=$(md5sum $dom)
23463         [ "$old_md5" != "$new_md5" ] &&
23464                 error "$old_md5 != $new_md5"
23465
23466         return 0
23467 }
23468 run_test 272f "DoM migration: OST-striped file to DOM file"
23469
23470 test_273a() {
23471         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
23472                 skip "Need MDS version at least 2.11.50"
23473
23474         # Layout swap cannot be done if either file has DOM component,
23475         # this will never be supported, migration should be used instead
23476
23477         local dom=$DIR/$tdir/$tfile
23478         mkdir -p $DIR/$tdir
23479
23480         $LFS setstripe -c2 ${dom}_plain
23481         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
23482         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
23483                 error "can swap layout with DoM component"
23484         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
23485                 error "can swap layout with DoM component"
23486
23487         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
23488         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
23489                 error "can swap layout with DoM component"
23490         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
23491                 error "can swap layout with DoM component"
23492         return 0
23493 }
23494 run_test 273a "DoM: layout swapping should fail with DOM"
23495
23496 test_273b() {
23497         mkdir -p $DIR/$tdir
23498         $LFS setstripe -E 1M -L mdt -E -1 -c -1 $DIR/$tdir
23499
23500 #define OBD_FAIL_MDS_COMMITRW_DELAY      0x16b
23501         do_facet mds1 $LCTL set_param fail_loc=0x8000016b fail_val=2
23502
23503         $MULTIOP $DIR/$tdir/$tfile Ouw2097152c
23504 }
23505 run_test 273b "DoM: race writeback and object destroy"
23506
23507 test_275() {
23508         remote_ost_nodsh && skip "remote OST with nodsh"
23509         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
23510                 skip "Need OST version >= 2.10.57"
23511
23512         local file=$DIR/$tfile
23513         local oss
23514
23515         oss=$(comma_list $(osts_nodes))
23516
23517         dd if=/dev/urandom of=$file bs=1M count=2 ||
23518                 error "failed to create a file"
23519         cancel_lru_locks osc
23520
23521         #lock 1
23522         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
23523                 error "failed to read a file"
23524
23525 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
23526         $LCTL set_param fail_loc=0x8000031f
23527
23528         cancel_lru_locks osc &
23529         sleep 1
23530
23531 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
23532         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
23533         #IO takes another lock, but matches the PENDING one
23534         #and places it to the IO RPC
23535         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
23536                 error "failed to read a file with PENDING lock"
23537 }
23538 run_test 275 "Read on a canceled duplicate lock"
23539
23540 test_276() {
23541         remote_ost_nodsh && skip "remote OST with nodsh"
23542         local pid
23543
23544         do_facet ost1 "(while true; do \
23545                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
23546                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
23547         pid=$!
23548
23549         for LOOP in $(seq 20); do
23550                 stop ost1
23551                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
23552         done
23553         kill -9 $pid
23554         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
23555                 rm $TMP/sanity_276_pid"
23556 }
23557 run_test 276 "Race between mount and obd_statfs"
23558
23559 test_277() {
23560         $LCTL set_param ldlm.namespaces.*.lru_size=0
23561         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23562         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
23563                         grep ^used_mb | awk '{print $2}')
23564         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
23565         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
23566                 oflag=direct conv=notrunc
23567         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
23568                         grep ^used_mb | awk '{print $2}')
23569         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
23570 }
23571 run_test 277 "Direct IO shall drop page cache"
23572
23573 test_278() {
23574         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
23575         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
23576         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
23577                 skip "needs the same host for mdt1 mdt2" && return
23578
23579         local pid1
23580         local pid2
23581
23582 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
23583         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
23584         stop mds2 &
23585         pid2=$!
23586
23587         stop mds1
23588
23589         echo "Starting MDTs"
23590         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
23591         wait $pid2
23592 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
23593 #will return NULL
23594         do_facet mds2 $LCTL set_param fail_loc=0
23595
23596         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
23597         wait_recovery_complete mds2
23598 }
23599 run_test 278 "Race starting MDS between MDTs stop/start"
23600
23601 test_280() {
23602         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
23603                 skip "Need MGS version at least 2.13.52"
23604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23605         combined_mgs_mds || skip "needs combined MGS/MDT"
23606
23607         umount_client $MOUNT
23608 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
23609         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
23610
23611         mount_client $MOUNT &
23612         sleep 1
23613         stop mgs || error "stop mgs failed"
23614         #for a race mgs would crash
23615         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
23616         # make sure we unmount client before remounting
23617         wait
23618         umount_client $MOUNT
23619         mount_client $MOUNT || error "mount client failed"
23620 }
23621 run_test 280 "Race between MGS umount and client llog processing"
23622
23623 cleanup_test_300() {
23624         trap 0
23625         umask $SAVE_UMASK
23626 }
23627 test_striped_dir() {
23628         local mdt_index=$1
23629         local stripe_count
23630         local stripe_index
23631
23632         mkdir -p $DIR/$tdir
23633
23634         SAVE_UMASK=$(umask)
23635         trap cleanup_test_300 RETURN EXIT
23636
23637         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
23638                                                 $DIR/$tdir/striped_dir ||
23639                 error "set striped dir error"
23640
23641         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
23642         [ "$mode" = "755" ] || error "expect 755 got $mode"
23643
23644         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
23645                 error "getdirstripe failed"
23646         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
23647         if [ "$stripe_count" != "2" ]; then
23648                 error "1:stripe_count is $stripe_count, expect 2"
23649         fi
23650         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
23651         if [ "$stripe_count" != "2" ]; then
23652                 error "2:stripe_count is $stripe_count, expect 2"
23653         fi
23654
23655         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
23656         if [ "$stripe_index" != "$mdt_index" ]; then
23657                 error "stripe_index is $stripe_index, expect $mdt_index"
23658         fi
23659
23660         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
23661                 error "nlink error after create striped dir"
23662
23663         mkdir $DIR/$tdir/striped_dir/a
23664         mkdir $DIR/$tdir/striped_dir/b
23665
23666         stat $DIR/$tdir/striped_dir/a ||
23667                 error "create dir under striped dir failed"
23668         stat $DIR/$tdir/striped_dir/b ||
23669                 error "create dir under striped dir failed"
23670
23671         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
23672                 error "nlink error after mkdir"
23673
23674         rmdir $DIR/$tdir/striped_dir/a
23675         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
23676                 error "nlink error after rmdir"
23677
23678         rmdir $DIR/$tdir/striped_dir/b
23679         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
23680                 error "nlink error after rmdir"
23681
23682         chattr +i $DIR/$tdir/striped_dir
23683         createmany -o $DIR/$tdir/striped_dir/f 10 &&
23684                 error "immutable flags not working under striped dir!"
23685         chattr -i $DIR/$tdir/striped_dir
23686
23687         rmdir $DIR/$tdir/striped_dir ||
23688                 error "rmdir striped dir error"
23689
23690         cleanup_test_300
23691
23692         true
23693 }
23694
23695 test_300a() {
23696         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23697                 skip "skipped for lustre < 2.7.0"
23698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23699         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23700
23701         test_striped_dir 0 || error "failed on striped dir on MDT0"
23702         test_striped_dir 1 || error "failed on striped dir on MDT0"
23703 }
23704 run_test 300a "basic striped dir sanity test"
23705
23706 test_300b() {
23707         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23708                 skip "skipped for lustre < 2.7.0"
23709         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23710         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23711
23712         local i
23713         local mtime1
23714         local mtime2
23715         local mtime3
23716
23717         test_mkdir $DIR/$tdir || error "mkdir fail"
23718         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23719                 error "set striped dir error"
23720         for i in {0..9}; do
23721                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
23722                 sleep 1
23723                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
23724                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
23725                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
23726                 sleep 1
23727                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
23728                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
23729                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
23730         done
23731         true
23732 }
23733 run_test 300b "check ctime/mtime for striped dir"
23734
23735 test_300c() {
23736         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23737                 skip "skipped for lustre < 2.7.0"
23738         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23739         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23740
23741         local file_count
23742
23743         mkdir_on_mdt0 $DIR/$tdir
23744         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
23745                 error "set striped dir error"
23746
23747         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
23748                 error "chown striped dir failed"
23749
23750         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
23751                 error "create 5k files failed"
23752
23753         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
23754
23755         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
23756
23757         rm -rf $DIR/$tdir
23758 }
23759 run_test 300c "chown && check ls under striped directory"
23760
23761 test_300d() {
23762         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
23763                 skip "skipped for lustre < 2.7.0"
23764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23765         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23766
23767         local stripe_count
23768         local file
23769
23770         mkdir -p $DIR/$tdir
23771         $LFS setstripe -c 2 $DIR/$tdir
23772
23773         #local striped directory
23774         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23775                 error "set striped dir error"
23776         #look at the directories for debug purposes
23777         ls -l $DIR/$tdir
23778         $LFS getdirstripe $DIR/$tdir
23779         ls -l $DIR/$tdir/striped_dir
23780         $LFS getdirstripe $DIR/$tdir/striped_dir
23781         createmany -o $DIR/$tdir/striped_dir/f 10 ||
23782                 error "create 10 files failed"
23783
23784         #remote striped directory
23785         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
23786                 error "set striped dir error"
23787         #look at the directories for debug purposes
23788         ls -l $DIR/$tdir
23789         $LFS getdirstripe $DIR/$tdir
23790         ls -l $DIR/$tdir/remote_striped_dir
23791         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
23792         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
23793                 error "create 10 files failed"
23794
23795         for file in $(find $DIR/$tdir); do
23796                 stripe_count=$($LFS getstripe -c $file)
23797                 [ $stripe_count -eq 2 ] ||
23798                         error "wrong stripe $stripe_count for $file"
23799         done
23800
23801         rm -rf $DIR/$tdir
23802 }
23803 run_test 300d "check default stripe under striped directory"
23804
23805 test_300e() {
23806         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23807                 skip "Need MDS version at least 2.7.55"
23808         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23809         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23810
23811         local stripe_count
23812         local file
23813
23814         mkdir -p $DIR/$tdir
23815
23816         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23817                 error "set striped dir error"
23818
23819         touch $DIR/$tdir/striped_dir/a
23820         touch $DIR/$tdir/striped_dir/b
23821         touch $DIR/$tdir/striped_dir/c
23822
23823         mkdir $DIR/$tdir/striped_dir/dir_a
23824         mkdir $DIR/$tdir/striped_dir/dir_b
23825         mkdir $DIR/$tdir/striped_dir/dir_c
23826
23827         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
23828                 error "set striped adir under striped dir error"
23829
23830         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
23831                 error "set striped bdir under striped dir error"
23832
23833         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
23834                 error "set striped cdir under striped dir error"
23835
23836         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
23837                 error "rename dir under striped dir fails"
23838
23839         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
23840                 error "rename dir under different stripes fails"
23841
23842         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
23843                 error "rename file under striped dir should succeed"
23844
23845         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
23846                 error "rename dir under striped dir should succeed"
23847
23848         rm -rf $DIR/$tdir
23849 }
23850 run_test 300e "check rename under striped directory"
23851
23852 test_300f() {
23853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23854         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23855         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23856                 skip "Need MDS version at least 2.7.55"
23857
23858         local stripe_count
23859         local file
23860
23861         rm -rf $DIR/$tdir
23862         mkdir -p $DIR/$tdir
23863
23864         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
23865                 error "set striped dir error"
23866
23867         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
23868                 error "set striped dir error"
23869
23870         touch $DIR/$tdir/striped_dir/a
23871         mkdir $DIR/$tdir/striped_dir/dir_a
23872         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
23873                 error "create striped dir under striped dir fails"
23874
23875         touch $DIR/$tdir/striped_dir1/b
23876         mkdir $DIR/$tdir/striped_dir1/dir_b
23877         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
23878                 error "create striped dir under striped dir fails"
23879
23880         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
23881                 error "rename dir under different striped dir should fail"
23882
23883         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
23884                 error "rename striped dir under diff striped dir should fail"
23885
23886         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
23887                 error "rename file under diff striped dirs fails"
23888
23889         rm -rf $DIR/$tdir
23890 }
23891 run_test 300f "check rename cross striped directory"
23892
23893 test_300_check_default_striped_dir()
23894 {
23895         local dirname=$1
23896         local default_count=$2
23897         local default_index=$3
23898         local stripe_count
23899         local stripe_index
23900         local dir_stripe_index
23901         local dir
23902
23903         echo "checking $dirname $default_count $default_index"
23904         $LFS setdirstripe -D -c $default_count -i $default_index \
23905                                 -H all_char $DIR/$tdir/$dirname ||
23906                 error "set default stripe on striped dir error"
23907         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
23908         [ $stripe_count -eq $default_count ] ||
23909                 error "expect $default_count get $stripe_count for $dirname"
23910
23911         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
23912         [ $stripe_index -eq $default_index ] ||
23913                 error "expect $default_index get $stripe_index for $dirname"
23914
23915         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
23916                                                 error "create dirs failed"
23917
23918         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
23919         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
23920         for dir in $(find $DIR/$tdir/$dirname/*); do
23921                 stripe_count=$($LFS getdirstripe -c $dir)
23922                 (( $stripe_count == $default_count )) ||
23923                 (( $stripe_count == $MDSCOUNT && $default_count == -1 )) ||
23924                 (( $stripe_count == 0 )) || (( $default_count == 1 )) ||
23925                 error "stripe count $default_count != $stripe_count for $dir"
23926
23927                 stripe_index=$($LFS getdirstripe -i $dir)
23928                 [ $default_index -eq -1 ] ||
23929                         [ $stripe_index -eq $default_index ] ||
23930                         error "$stripe_index != $default_index for $dir"
23931
23932                 #check default stripe
23933                 stripe_count=$($LFS getdirstripe -D -c $dir)
23934                 [ $stripe_count -eq $default_count ] ||
23935                 error "default count $default_count != $stripe_count for $dir"
23936
23937                 stripe_index=$($LFS getdirstripe -D -i $dir)
23938                 [ $stripe_index -eq $default_index ] ||
23939                 error "default index $default_index != $stripe_index for $dir"
23940         done
23941         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
23942 }
23943
23944 test_300g() {
23945         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23946         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23947                 skip "Need MDS version at least 2.7.55"
23948
23949         local dir
23950         local stripe_count
23951         local stripe_index
23952
23953         mkdir_on_mdt0 $DIR/$tdir
23954         mkdir $DIR/$tdir/normal_dir
23955
23956         #Checking when client cache stripe index
23957         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
23958         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
23959                 error "create striped_dir failed"
23960
23961         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
23962                 error "create dir0 fails"
23963         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
23964         [ $stripe_index -eq 0 ] ||
23965                 error "dir0 expect index 0 got $stripe_index"
23966
23967         mkdir $DIR/$tdir/striped_dir/dir1 ||
23968                 error "create dir1 fails"
23969         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
23970         [ $stripe_index -eq 1 ] ||
23971                 error "dir1 expect index 1 got $stripe_index"
23972
23973         #check default stripe count/stripe index
23974         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
23975         test_300_check_default_striped_dir normal_dir 1 0
23976         test_300_check_default_striped_dir normal_dir -1 1
23977         test_300_check_default_striped_dir normal_dir 2 -1
23978
23979         #delete default stripe information
23980         echo "delete default stripeEA"
23981         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
23982                 error "set default stripe on striped dir error"
23983
23984         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
23985         for dir in $(find $DIR/$tdir/normal_dir/*); do
23986                 stripe_count=$($LFS getdirstripe -c $dir)
23987                 [ $stripe_count -eq 0 ] ||
23988                         error "expect 1 get $stripe_count for $dir"
23989         done
23990 }
23991 run_test 300g "check default striped directory for normal directory"
23992
23993 test_300h() {
23994         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
23995         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
23996                 skip "Need MDS version at least 2.7.55"
23997
23998         local dir
23999         local stripe_count
24000
24001         mkdir $DIR/$tdir
24002         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
24003                 error "set striped dir error"
24004
24005         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
24006         test_300_check_default_striped_dir striped_dir 1 0
24007         test_300_check_default_striped_dir striped_dir -1 1
24008         test_300_check_default_striped_dir striped_dir 2 -1
24009
24010         #delete default stripe information
24011         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
24012                 error "set default stripe on striped dir error"
24013
24014         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
24015         for dir in $(find $DIR/$tdir/striped_dir/*); do
24016                 stripe_count=$($LFS getdirstripe -c $dir)
24017                 [ $stripe_count -eq 0 ] ||
24018                         error "expect 1 get $stripe_count for $dir"
24019         done
24020 }
24021 run_test 300h "check default striped directory for striped directory"
24022
24023 test_300i() {
24024         [[ $PARALLEL == "yes" ]] && skip "skip parallel run"
24025         (( $MDSCOUNT >= 2 )) || skip_env "needs >= 2 MDTs"
24026         (( $MDS1_VERSION >= $(version_code 2.7.55) )) ||
24027                 skip "Need MDS version at least 2.7.55"
24028
24029         local stripe_count
24030         local file
24031
24032         mkdir $DIR/$tdir
24033
24034         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
24035                 error "set striped dir error"
24036
24037         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
24038                 error "create files under striped dir failed"
24039
24040         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
24041                 error "set striped hashdir error"
24042
24043         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
24044                 error "create dir0 under hash dir failed"
24045         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
24046                 error "create dir1 under hash dir failed"
24047         $LFS setdirstripe -i0 -c$MDSCOUNT -H crush $DIR/$tdir/hashdir/d2 ||
24048                 error "create dir2 under hash dir failed"
24049
24050         # unfortunately, we need to umount to clear dir layout cache for now
24051         # once we fully implement dir layout, we can drop this
24052         umount_client $MOUNT || error "umount failed"
24053         mount_client $MOUNT || error "mount failed"
24054
24055         $LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir
24056         local dircnt=$($LFS find -H fnv_1a_64,crush $DIR/$tdir/hashdir | wc -l)
24057         (( $dircnt == 2 )) || error "lfs find striped dir got $dircnt != 2"
24058
24059         if (( $MDS1_VERSION > $(version_code 2.15.0) )); then
24060                 $LFS mkdir -i0 -c$MDSCOUNT -H crush2 $DIR/$tdir/hashdir/d3 ||
24061                         error "create crush2 dir $tdir/hashdir/d3 failed"
24062                 $LFS find -H crush2 $DIR/$tdir/hashdir
24063                 dircnt=$($LFS find -H crush2 $DIR/$tdir/hashdir | wc -l)
24064                 (( $dircnt == 1 )) || error "find crush2 dir got $dircnt != 1"
24065
24066                 # mkdir with an invalid hash type (hash=fail_val) from client
24067                 # should be replaced on MDS with a valid (default) hash type
24068                 #define OBD_FAIL_LMV_UNKNOWN_STRIPE     0x1901
24069                 $LCTL set_param fail_loc=0x1901 fail_val=99
24070                 $LFS mkdir -c2 $DIR/$tdir/hashdir/d99
24071
24072                 local hash=$($LFS getdirstripe -H $DIR/$tdir/hashdir/d99)
24073                 local expect=$(do_facet mds1 \
24074                         $LCTL get_param -n lod.$FSNAME-MDT0000-mdtlov.mdt_hash)
24075                 [[ $hash == $expect ]] ||
24076                         error "d99 hash '$hash' != expected hash '$expect'"
24077         fi
24078
24079         #set the stripe to be unknown hash type on read
24080         #define OBD_FAIL_LMV_UNKNOWN_STRIPE     0x1901
24081         $LCTL set_param fail_loc=0x1901 fail_val=99
24082         for ((i = 0; i < 10; i++)); do
24083                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
24084                         error "stat f-$i failed"
24085                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
24086         done
24087
24088         touch $DIR/$tdir/striped_dir/f0 &&
24089                 error "create under striped dir with unknown hash should fail"
24090
24091         $LCTL set_param fail_loc=0
24092
24093         umount_client $MOUNT || error "umount failed"
24094         mount_client $MOUNT || error "mount failed"
24095
24096         return 0
24097 }
24098 run_test 300i "client handle unknown hash type striped directory"
24099
24100 test_300j() {
24101         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24102         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24103         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24104                 skip "Need MDS version at least 2.7.55"
24105
24106         local stripe_count
24107         local file
24108
24109         mkdir $DIR/$tdir
24110
24111         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
24112         $LCTL set_param fail_loc=0x1702
24113         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
24114                 error "set striped dir error"
24115
24116         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
24117                 error "create files under striped dir failed"
24118
24119         $LCTL set_param fail_loc=0
24120
24121         rm -rf $DIR/$tdir || error "unlink striped dir fails"
24122
24123         return 0
24124 }
24125 run_test 300j "test large update record"
24126
24127 test_300k() {
24128         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24129         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24130         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24131                 skip "Need MDS version at least 2.7.55"
24132
24133         # this test needs a huge transaction
24134         local kb
24135         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
24136              osd*.$FSNAME-MDT0000.kbytestotal")
24137         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
24138
24139         local stripe_count
24140         local file
24141
24142         mkdir $DIR/$tdir
24143
24144         #define OBD_FAIL_LARGE_STRIPE   0x1703
24145         $LCTL set_param fail_loc=0x1703
24146         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
24147                 error "set striped dir error"
24148         $LCTL set_param fail_loc=0
24149
24150         $LFS getdirstripe $DIR/$tdir/striped_dir ||
24151                 error "getstripeddir fails"
24152         rm -rf $DIR/$tdir/striped_dir ||
24153                 error "unlink striped dir fails"
24154
24155         return 0
24156 }
24157 run_test 300k "test large striped directory"
24158
24159 test_300l() {
24160         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24161         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24162         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24163                 skip "Need MDS version at least 2.7.55"
24164
24165         local stripe_index
24166
24167         test_mkdir -p $DIR/$tdir/striped_dir
24168         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
24169                         error "chown $RUNAS_ID failed"
24170         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
24171                 error "set default striped dir failed"
24172
24173         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
24174         $LCTL set_param fail_loc=0x80000158
24175         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
24176
24177         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
24178         [ $stripe_index -eq 1 ] ||
24179                 error "expect 1 get $stripe_index for $dir"
24180 }
24181 run_test 300l "non-root user to create dir under striped dir with stale layout"
24182
24183 test_300m() {
24184         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24185         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
24186         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24187                 skip "Need MDS version at least 2.7.55"
24188
24189         mkdir -p $DIR/$tdir/striped_dir
24190         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
24191                 error "set default stripes dir error"
24192
24193         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
24194
24195         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
24196         [ $stripe_count -eq 0 ] ||
24197                         error "expect 0 get $stripe_count for a"
24198
24199         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
24200                 error "set default stripes dir error"
24201
24202         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
24203
24204         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
24205         [ $stripe_count -eq 0 ] ||
24206                         error "expect 0 get $stripe_count for b"
24207
24208         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
24209                 error "set default stripes dir error"
24210
24211         mkdir $DIR/$tdir/striped_dir/c &&
24212                 error "default stripe_index is invalid, mkdir c should fails"
24213
24214         rm -rf $DIR/$tdir || error "rmdir fails"
24215 }
24216 run_test 300m "setstriped directory on single MDT FS"
24217
24218 cleanup_300n() {
24219         local list=$(comma_list $(mdts_nodes))
24220
24221         trap 0
24222         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24223 }
24224
24225 test_300n() {
24226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24227         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24228         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24229                 skip "Need MDS version at least 2.7.55"
24230         remote_mds_nodsh && skip "remote MDS with nodsh"
24231
24232         local stripe_index
24233         local list=$(comma_list $(mdts_nodes))
24234
24235         trap cleanup_300n RETURN EXIT
24236         mkdir -p $DIR/$tdir
24237         chmod 777 $DIR/$tdir
24238         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
24239                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
24240                 error "create striped dir succeeds with gid=0"
24241
24242         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
24243         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
24244                 error "create striped dir fails with gid=-1"
24245
24246         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24247         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
24248                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
24249                 error "set default striped dir succeeds with gid=0"
24250
24251
24252         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
24253         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
24254                 error "set default striped dir fails with gid=-1"
24255
24256
24257         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
24258         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
24259                                         error "create test_dir fails"
24260         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
24261                                         error "create test_dir1 fails"
24262         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
24263                                         error "create test_dir2 fails"
24264         cleanup_300n
24265 }
24266 run_test 300n "non-root user to create dir under striped dir with default EA"
24267
24268 test_300o() {
24269         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24270         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24271         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24272                 skip "Need MDS version at least 2.7.55"
24273
24274         local numfree1
24275         local numfree2
24276
24277         mkdir -p $DIR/$tdir
24278
24279         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
24280         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
24281         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
24282                 skip "not enough free inodes $numfree1 $numfree2"
24283         fi
24284
24285         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
24286         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
24287         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
24288                 skip "not enough free space $numfree1 $numfree2"
24289         fi
24290
24291         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
24292                 error "setdirstripe fails"
24293
24294         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
24295                 error "create dirs fails"
24296
24297         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
24298         ls $DIR/$tdir/striped_dir > /dev/null ||
24299                 error "ls striped dir fails"
24300         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
24301                 error "unlink big striped dir fails"
24302 }
24303 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
24304
24305 test_300p() {
24306         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24307         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24308         remote_mds_nodsh && skip "remote MDS with nodsh"
24309
24310         mkdir_on_mdt0 $DIR/$tdir
24311
24312         #define OBD_FAIL_OUT_ENOSPC     0x1704
24313         do_facet mds2 lctl set_param fail_loc=0x80001704
24314         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
24315                  && error "create striped directory should fail"
24316
24317         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
24318
24319         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
24320         true
24321 }
24322 run_test 300p "create striped directory without space"
24323
24324 test_300q() {
24325         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24326         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
24327
24328         local fd=$(free_fd)
24329         local cmd="exec $fd<$tdir"
24330         cd $DIR
24331         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
24332         eval $cmd
24333         cmd="exec $fd<&-"
24334         trap "eval $cmd" EXIT
24335         cd $tdir || error "cd $tdir fails"
24336         rmdir  ../$tdir || error "rmdir $tdir fails"
24337         mkdir local_dir && error "create dir succeeds"
24338         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
24339         eval $cmd
24340         return 0
24341 }
24342 run_test 300q "create remote directory under orphan directory"
24343
24344 test_300r() {
24345         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24346                 skip "Need MDS version at least 2.7.55" && return
24347         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
24348
24349         mkdir $DIR/$tdir
24350
24351         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
24352                 error "set striped dir error"
24353
24354         $LFS getdirstripe $DIR/$tdir/striped_dir ||
24355                 error "getstripeddir fails"
24356
24357         local stripe_count
24358         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
24359                       awk '/lmv_stripe_count:/ { print $2 }')
24360
24361         [ $MDSCOUNT -ne $stripe_count ] &&
24362                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
24363
24364         rm -rf $DIR/$tdir/striped_dir ||
24365                 error "unlink striped dir fails"
24366 }
24367 run_test 300r "test -1 striped directory"
24368
24369 test_300s_helper() {
24370         local count=$1
24371
24372         local stripe_dir=$DIR/$tdir/striped_dir.$count
24373
24374         $LFS mkdir -c $count $stripe_dir ||
24375                 error "lfs mkdir -c error"
24376
24377         $LFS getdirstripe $stripe_dir ||
24378                 error "lfs getdirstripe fails"
24379
24380         local stripe_count
24381         stripe_count=$($LFS getdirstripe $stripe_dir |
24382                       awk '/lmv_stripe_count:/ { print $2 }')
24383
24384         [ $count -ne $stripe_count ] &&
24385                 error_noexit "bad stripe count $stripe_count expected $count"
24386
24387         local dupe_stripes
24388         dupe_stripes=$($LFS getdirstripe $stripe_dir |
24389                 awk '/0x/ {count[$1] += 1}; END {
24390                         for (idx in count) {
24391                                 if (count[idx]>1) {
24392                                         print "index " idx " count " count[idx]
24393                                 }
24394                         }
24395                 }')
24396
24397         if [[ -n "$dupe_stripes" ]] ; then
24398                 lfs getdirstripe $stripe_dir
24399                 error_noexit "Dupe MDT above: $dupe_stripes "
24400         fi
24401
24402         rm -rf $stripe_dir ||
24403                 error_noexit "unlink $stripe_dir fails"
24404 }
24405
24406 test_300s() {
24407         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
24408                 skip "Need MDS version at least 2.7.55" && return
24409         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
24410
24411         mkdir $DIR/$tdir
24412         for count in $(seq 2 $MDSCOUNT); do
24413                 test_300s_helper $count
24414         done
24415 }
24416 run_test 300s "test lfs mkdir -c without -i"
24417
24418 test_300t() {
24419         (( $MDS1_VERSION >= $(version_code 2.14.55) )) ||
24420                 skip "need MDS 2.14.55 or later"
24421         (( $MDSCOUNT >= 2 )) || skip "needs at least 2 MDTs"
24422
24423         local testdir="$DIR/$tdir/striped_dir"
24424         local dir1=$testdir/dir1
24425         local dir2=$testdir/dir2
24426
24427         mkdir -p $testdir
24428
24429         $LFS setdirstripe -D -c -1 --max-inherit=3 $testdir ||
24430                 error "failed to set default stripe count for $testdir"
24431
24432         mkdir $dir1
24433         local stripe_count=$($LFS getdirstripe -c $dir1)
24434
24435         (( $stripe_count == $MDSCOUNT )) || error "wrong stripe count"
24436
24437         local max_count=$((MDSCOUNT - 1))
24438         local mdts=$(comma_list $(mdts_nodes))
24439
24440         do_nodes $mdts $LCTL set_param lod.*.max_mdt_stripecount=$max_count
24441         stack_trap "do_nodes $mdts $LCTL set_param lod.*.max_mdt_stripecount=0"
24442
24443         mkdir $dir2
24444         stripe_count=$($LFS getdirstripe -c $dir2)
24445
24446         (( $stripe_count == $max_count )) || error "wrong stripe count"
24447 }
24448 run_test 300t "test max_mdt_stripecount"
24449
24450 prepare_remote_file() {
24451         mkdir $DIR/$tdir/src_dir ||
24452                 error "create remote source failed"
24453
24454         cp /etc/hosts $DIR/$tdir/src_dir/a ||
24455                  error "cp to remote source failed"
24456         touch $DIR/$tdir/src_dir/a
24457
24458         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
24459                 error "create remote target dir failed"
24460
24461         touch $DIR/$tdir/tgt_dir/b
24462
24463         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
24464                 error "rename dir cross MDT failed!"
24465
24466         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
24467                 error "src_child still exists after rename"
24468
24469         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
24470                 error "missing file(a) after rename"
24471
24472         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
24473                 error "diff after rename"
24474 }
24475
24476 test_310a() {
24477         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
24478         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24479
24480         local remote_file=$DIR/$tdir/tgt_dir/b
24481
24482         mkdir -p $DIR/$tdir
24483
24484         prepare_remote_file || error "prepare remote file failed"
24485
24486         #open-unlink file
24487         $OPENUNLINK $remote_file $remote_file ||
24488                 error "openunlink $remote_file failed"
24489         $CHECKSTAT -a $remote_file || error "$remote_file exists"
24490 }
24491 run_test 310a "open unlink remote file"
24492
24493 test_310b() {
24494         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
24495         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24496
24497         local remote_file=$DIR/$tdir/tgt_dir/b
24498
24499         mkdir -p $DIR/$tdir
24500
24501         prepare_remote_file || error "prepare remote file failed"
24502
24503         ln $remote_file $DIR/$tfile || error "link failed for remote file"
24504         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
24505         $CHECKSTAT -t file $remote_file || error "check file failed"
24506 }
24507 run_test 310b "unlink remote file with multiple links while open"
24508
24509 test_310c() {
24510         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24511         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
24512
24513         local remote_file=$DIR/$tdir/tgt_dir/b
24514
24515         mkdir -p $DIR/$tdir
24516
24517         prepare_remote_file || error "prepare remote file failed"
24518
24519         ln $remote_file $DIR/$tfile || error "link failed for remote file"
24520         multiop_bg_pause $remote_file O_uc ||
24521                         error "mulitop failed for remote file"
24522         MULTIPID=$!
24523         $MULTIOP $DIR/$tfile Ouc
24524         kill -USR1 $MULTIPID
24525         wait $MULTIPID
24526 }
24527 run_test 310c "open-unlink remote file with multiple links"
24528
24529 #LU-4825
24530 test_311() {
24531         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24532         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
24533         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
24534                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
24535         remote_mds_nodsh && skip "remote MDS with nodsh"
24536
24537         local old_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
24538         local mdts=$(comma_list $(mdts_nodes))
24539
24540         mkdir -p $DIR/$tdir
24541         $LFS setstripe -i 0 -c 1 $DIR/$tdir
24542         createmany -o $DIR/$tdir/$tfile. 1000
24543
24544         # statfs data is not real time, let's just calculate it
24545         old_iused=$((old_iused + 1000))
24546
24547         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
24548                         osp.*OST0000*MDT0000.create_count")
24549         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
24550                                 osp.*OST0000*MDT0000.max_create_count")
24551         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
24552
24553         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
24554         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
24555         [ $index -ne 0 ] || error "$tfile stripe index is 0"
24556
24557         unlinkmany $DIR/$tdir/$tfile. 1000
24558
24559         do_nodes $mdts "$LCTL set_param -n \
24560                         osp.*OST0000*.max_create_count=$max_count"
24561         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
24562                 do_nodes $mdts "$LCTL set_param -n \
24563                                 osp.*OST0000*.create_count=$count"
24564         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
24565                         grep "=0" && error "create_count is zero"
24566
24567         local new_iused
24568         for i in $(seq 120); do
24569                 new_iused=$($LFS df -i | awk '/OST0000/ { print $3; exit; }')
24570                 # system may be too busy to destroy all objs in time, use
24571                 # a somewhat small value to not fail autotest
24572                 [ $((old_iused - new_iused)) -gt 400 ] && break
24573                 sleep 1
24574         done
24575
24576         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
24577         [ $((old_iused - new_iused)) -gt 400 ] ||
24578                 error "objs not destroyed after unlink"
24579 }
24580 run_test 311 "disable OSP precreate, and unlink should destroy objs"
24581
24582 zfs_oid_to_objid()
24583 {
24584         local ost=$1
24585         local objid=$2
24586
24587         local vdevdir=$(dirname $(facet_vdevice $ost))
24588         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
24589         local zfs_zapid=$(do_facet $ost $cmd |
24590                           grep -w "/O/0/d$((objid%32))" -C 5 |
24591                           awk '/Object/{getline; print $1}')
24592         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
24593                           awk "/$objid = /"'{printf $3}')
24594
24595         echo $zfs_objid
24596 }
24597
24598 zfs_object_blksz() {
24599         local ost=$1
24600         local objid=$2
24601
24602         local vdevdir=$(dirname $(facet_vdevice $ost))
24603         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
24604         local blksz=$(do_facet $ost $cmd $objid |
24605                       awk '/dblk/{getline; printf $4}')
24606
24607         case "${blksz: -1}" in
24608                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
24609                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
24610                 *) ;;
24611         esac
24612
24613         echo $blksz
24614 }
24615
24616 test_312() { # LU-4856
24617         remote_ost_nodsh && skip "remote OST with nodsh"
24618         [ "$ost1_FSTYPE" = "zfs" ] ||
24619                 skip_env "the test only applies to zfs"
24620
24621         local max_blksz=$(do_facet ost1 \
24622                           $ZFS get -p recordsize $(facet_device ost1) |
24623                           awk '!/VALUE/{print $3}')
24624
24625         # to make life a little bit easier
24626         $LFS mkdir -c 1 -i 0 $DIR/$tdir
24627         $LFS setstripe -c 1 -i 0 $DIR/$tdir
24628
24629         local tf=$DIR/$tdir/$tfile
24630         touch $tf
24631         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
24632
24633         # Get ZFS object id
24634         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
24635         # block size change by sequential overwrite
24636         local bs
24637
24638         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
24639                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
24640
24641                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
24642                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
24643         done
24644         rm -f $tf
24645
24646         # block size change by sequential append write
24647         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
24648         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
24649         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
24650         local count
24651
24652         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
24653                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
24654                         oflag=sync conv=notrunc
24655
24656                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
24657                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
24658                         error "blksz error, actual $blksz, " \
24659                                 "expected: 2 * $count * $PAGE_SIZE"
24660         done
24661         rm -f $tf
24662
24663         # random write
24664         touch $tf
24665         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
24666         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
24667
24668         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
24669         blksz=$(zfs_object_blksz ost1 $zfs_objid)
24670         [ $blksz -eq $PAGE_SIZE ] ||
24671                 error "blksz error: $blksz, expected: $PAGE_SIZE"
24672
24673         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
24674         blksz=$(zfs_object_blksz ost1 $zfs_objid)
24675         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
24676
24677         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
24678         blksz=$(zfs_object_blksz ost1 $zfs_objid)
24679         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
24680 }
24681 run_test 312 "make sure ZFS adjusts its block size by write pattern"
24682
24683 test_313() {
24684         remote_ost_nodsh && skip "remote OST with nodsh"
24685
24686         local file=$DIR/$tfile
24687
24688         rm -f $file
24689         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
24690
24691         # define OBD_FAIL_TGT_RCVD_EIO           0x720
24692         do_facet ost1 "$LCTL set_param fail_loc=0x720"
24693         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
24694                 error "write should failed"
24695         do_facet ost1 "$LCTL set_param fail_loc=0"
24696         rm -f $file
24697 }
24698 run_test 313 "io should fail after last_rcvd update fail"
24699
24700 test_314() {
24701         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
24702
24703         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
24704         do_facet ost1 "$LCTL set_param fail_loc=0x720"
24705         rm -f $DIR/$tfile
24706         wait_delete_completed
24707         do_facet ost1 "$LCTL set_param fail_loc=0"
24708 }
24709 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
24710
24711 test_315() { # LU-618
24712         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
24713
24714         local file=$DIR/$tfile
24715         rm -f $file
24716
24717         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
24718                 error "multiop file write failed"
24719         $MULTIOP $file oO_RDONLY:r4063232_c &
24720         PID=$!
24721
24722         sleep 2
24723
24724         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
24725         kill -USR1 $PID
24726
24727         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
24728         rm -f $file
24729 }
24730 run_test 315 "read should be accounted"
24731
24732 test_316() {
24733         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
24734         large_xattr_enabled || skip "ea_inode feature disabled"
24735
24736         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
24737         mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
24738         chown nobody $DIR/$tdir/d || error "chown $tdir/d failed"
24739         touch $DIR/$tdir/d/$tfile || error "touch $tdir/d/$tfile failed"
24740
24741         $LFS migrate -m1 $DIR/$tdir/d || error "lfs migrate -m1 failed"
24742 }
24743 run_test 316 "lfs migrate of file with large_xattr enabled"
24744
24745 test_317() {
24746         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
24747                 skip "Need MDS version at least 2.11.53"
24748         if [ "$ost1_FSTYPE" == "zfs" ]; then
24749                 skip "LU-10370: no implementation for ZFS"
24750         fi
24751
24752         local trunc_sz
24753         local grant_blk_size
24754
24755         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
24756                         awk '/grant_block_size:/ { print $2; exit; }')
24757         #
24758         # Create File of size 5M. Truncate it to below size's and verify
24759         # blocks count.
24760         #
24761         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
24762                 error "Create file $DIR/$tfile failed"
24763         stack_trap "rm -f $DIR/$tfile" EXIT
24764
24765         for trunc_sz in 2097152 4097 4000 509 0; do
24766                 $TRUNCATE $DIR/$tfile $trunc_sz ||
24767                         error "truncate $tfile to $trunc_sz failed"
24768                 local sz=$(stat --format=%s $DIR/$tfile)
24769                 local blk=$(stat --format=%b $DIR/$tfile)
24770                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
24771                                      grant_blk_size) * 8))
24772
24773                 if [[ $blk -ne $trunc_blk ]]; then
24774                         $(which stat) $DIR/$tfile
24775                         error "Expected Block $trunc_blk got $blk for $tfile"
24776                 fi
24777
24778                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
24779                         error "Expected Size $trunc_sz got $sz for $tfile"
24780         done
24781
24782         #
24783         # sparse file test
24784         # Create file with a hole and write actual 65536 bytes which aligned
24785         # with 4K and 64K PAGE_SIZE. Block count must be 128.
24786         #
24787         local bs=65536
24788         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 seek=5 conv=fsync ||
24789                 error "Create file : $DIR/$tfile"
24790
24791         #
24792         # Truncate to size $trunc_sz bytes. Strip tail blocks and leave only 8
24793         # blocks. The block count must drop to 8.
24794         #
24795         trunc_sz=$(($(stat --format=%s $DIR/$tfile) -
24796                 ((bs - grant_blk_size) + 1)))
24797         $TRUNCATE $DIR/$tfile $trunc_sz ||
24798                 error "truncate $tfile to $trunc_sz failed"
24799
24800         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
24801         sz=$(stat --format=%s $DIR/$tfile)
24802         blk=$(stat --format=%b $DIR/$tfile)
24803
24804         if [[ $blk -ne $trunc_bsz ]]; then
24805                 $(which stat) $DIR/$tfile
24806                 error "Expected Block $trunc_bsz got $blk for $tfile"
24807         fi
24808
24809         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
24810                 error "Expected Size $trunc_sz got $sz for $tfile"
24811 }
24812 run_test 317 "Verify blocks get correctly update after truncate"
24813
24814 test_318() {
24815         local llite_name="llite.$($LFS getname $MOUNT | awk '{print $1}')"
24816         local old_max_active=$($LCTL get_param -n \
24817                             ${llite_name}.max_read_ahead_async_active \
24818                             2>/dev/null)
24819
24820         $LCTL set_param llite.*.max_read_ahead_async_active=256
24821         local max_active=$($LCTL get_param -n \
24822                            ${llite_name}.max_read_ahead_async_active \
24823                            2>/dev/null)
24824         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
24825
24826         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
24827                 error "set max_read_ahead_async_active should succeed"
24828
24829         $LCTL set_param llite.*.max_read_ahead_async_active=512
24830         max_active=$($LCTL get_param -n \
24831                      ${llite_name}.max_read_ahead_async_active 2>/dev/null)
24832         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
24833
24834         # restore @max_active
24835         [ $old_max_active -ne 0 ] && $LCTL set_param \
24836                 llite.*.max_read_ahead_async_active=$old_max_active
24837
24838         local old_threshold=$($LCTL get_param -n \
24839                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
24840         local max_per_file_mb=$($LCTL get_param -n \
24841                 ${llite_name}.max_read_ahead_per_file_mb 2>/dev/null)
24842
24843         local invalid=$(($max_per_file_mb + 1))
24844         $LCTL set_param \
24845                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
24846                         && error "set $invalid should fail"
24847
24848         local valid=$(($invalid - 1))
24849         $LCTL set_param \
24850                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
24851                         error "set $valid should succeed"
24852         local threshold=$($LCTL get_param -n \
24853                 ${llite_name}.read_ahead_async_file_threshold_mb 2>/dev/null)
24854         [ $threshold -eq $valid ] || error \
24855                 "expect threshold $valid got $threshold"
24856         $LCTL set_param \
24857                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
24858 }
24859 run_test 318 "Verify async readahead tunables"
24860
24861 test_319() {
24862         (( $MDSCOUNT >= 2 )) || skip "needs >= 2 MDTs"
24863
24864         local before=$(date +%s)
24865         local evict
24866         local mdir=$DIR/$tdir
24867         local file=$mdir/xxx
24868
24869         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
24870         touch $file
24871
24872 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
24873         $LCTL set_param fail_val=5 fail_loc=0x8000032c
24874         $LFS migrate -m1 $mdir &
24875
24876         sleep 1
24877         dd if=$file of=/dev/null
24878         wait
24879         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
24880           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
24881
24882         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
24883 }
24884 run_test 319 "lost lease lock on migrate error"
24885
24886 test_398a() { # LU-4198
24887         local ost1_imp=$(get_osc_import_name client ost1)
24888         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
24889                          cut -d'.' -f2)
24890
24891         $LFS setstripe -c 1 -i 0 $DIR/$tfile
24892         $LCTL set_param ldlm.namespaces.*.lru_size=clear
24893
24894         # Disabled: DIO does not push out buffered I/O pages, see LU-12587
24895         # request a new lock on client
24896         #dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
24897
24898         #dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
24899         #local lock_count=$($LCTL get_param -n \
24900         #                  ldlm.namespaces.$imp_name.lru_size)
24901         #[[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
24902
24903         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
24904
24905         # no lock cached, should use lockless DIO and not enqueue new lock
24906         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct \
24907                 conv=notrunc ||
24908                 error "dio write failed"
24909         lock_count=$($LCTL get_param -n \
24910                      ldlm.namespaces.$imp_name.lru_size)
24911         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
24912
24913         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
24914
24915         # no lock cached, should use locked DIO append
24916         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct oflag=append \
24917                 conv=notrunc || error "DIO append failed"
24918         lock_count=$($LCTL get_param -n \
24919                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
24920         [[ $lock_count -ne 0 ]] || error "lock still must be held by DIO append"
24921 }
24922 run_test 398a "direct IO should cancel lock otherwise lockless"
24923
24924 test_398b() { # LU-4198
24925         which fio || skip_env "no fio installed"
24926         $LFS setstripe -c -1 -S 1M $DIR/$tfile
24927
24928         local size=48
24929         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
24930
24931         local njobs=4
24932         # Single page, multiple pages, stripe size, 4*stripe size
24933         for bsize in $(( $PAGE_SIZE )) $(( 4*$PAGE_SIZE )) 1048576 4194304; do
24934                 echo "mix direct rw ${bsize} by fio with $njobs jobs..."
24935                 fio --name=rand-rw --rw=randrw --bs=$bsize --direct=1 \
24936                         --numjobs=$njobs --fallocate=none \
24937                         --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24938                         --filename=$DIR/$tfile &
24939                 bg_pid=$!
24940
24941                 echo "mix buffer rw ${bsize} by fio with $njobs jobs..."
24942                 fio --name=rand-rw --rw=randrw --bs=$bsize \
24943                         --numjobs=$njobs --fallocate=none \
24944                         --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24945                         --filename=$DIR/$tfile || true
24946                 wait $bg_pid
24947         done
24948
24949         evict=$(do_facet client $LCTL get_param \
24950                 osc.$FSNAME-OST*-osc-*/state |
24951             awk -F"[ [,]" '/EVICTED ]$/ { if (t<$5) {t=$5;} } END { print t }')
24952
24953         [ -z "$evict" ] || [[ $evict -le $before ]] ||
24954                 (do_facet client $LCTL get_param \
24955                         osc.$FSNAME-OST*-osc-*/state;
24956                     error "eviction happened: $evict before:$before")
24957
24958         rm -f $DIR/$tfile
24959 }
24960 run_test 398b "DIO and buffer IO race"
24961
24962 test_398c() { # LU-4198
24963         local ost1_imp=$(get_osc_import_name client ost1)
24964         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
24965                          cut -d'.' -f2)
24966
24967         which fio || skip_env "no fio installed"
24968
24969         saved_debug=$($LCTL get_param -n debug)
24970         $LCTL set_param debug=0
24971
24972         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
24973         ((size /= 1024)) # by megabytes
24974         ((size /= 2)) # write half of the OST at most
24975         [ $size -gt 40 ] && size=40 #reduce test time anyway
24976
24977         $LFS setstripe -c 1 $DIR/$tfile
24978
24979         # it seems like ldiskfs reserves more space than necessary if the
24980         # writing blocks are not mapped, so it extends the file firstly
24981         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
24982         cancel_lru_locks osc
24983
24984         # clear and verify rpc_stats later
24985         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
24986
24987         local njobs=4
24988         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
24989         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
24990                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
24991                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
24992                 --filename=$DIR/$tfile
24993         [ $? -eq 0 ] || error "fio write error"
24994
24995         [ $($LCTL get_param -n ldlm.namespaces.$imp_name.lock_count) -eq 0 ] ||
24996                 error "Locks were requested while doing AIO"
24997
24998         # get the percentage of 1-page I/O
24999         pct=$($LCTL get_param osc.${imp_name}.rpc_stats |
25000                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
25001                 awk '{print $7}')
25002         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
25003
25004         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
25005         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
25006                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
25007                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
25008                 --filename=$DIR/$tfile
25009         [ $? -eq 0 ] || error "fio mixed read write error"
25010
25011         echo "AIO with large block size ${size}M"
25012         fio --name=rand-rw --rw=randrw --bs=${size}M --direct=1 \
25013                 --numjobs=1 --fallocate=none --ioengine=libaio \
25014                 --iodepth=16 --allow_file_create=0 --size=${size}M \
25015                 --filename=$DIR/$tfile
25016         [ $? -eq 0 ] || error "fio large block size failed"
25017
25018         rm -f $DIR/$tfile
25019         $LCTL set_param debug="$saved_debug"
25020 }
25021 run_test 398c "run fio to test AIO"
25022
25023 test_398d() { #  LU-13846
25024         which aiocp || skip_env "no aiocp installed"
25025         local aio_file=$DIR/$tfile.aio
25026
25027         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
25028
25029         dd if=/dev/urandom of=$DIR/$tfile bs=1M count=64
25030         aiocp -a $PAGE_SIZE -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file
25031         stack_trap "rm -f $DIR/$tfile $aio_file"
25032
25033         diff $DIR/$tfile $aio_file || error "file diff after aiocp"
25034
25035         # make sure we don't crash and fail properly
25036         aiocp -a 512 -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
25037                 error "aio not aligned with PAGE SIZE should fail"
25038
25039         rm -f $DIR/$tfile $aio_file
25040 }
25041 run_test 398d "run aiocp to verify block size > stripe size"
25042
25043 test_398e() {
25044         dd if=/dev/zero of=$DIR/$tfile bs=1234 count=1
25045         touch $DIR/$tfile.new
25046         dd if=$DIR/$tfile of=$DIR/$tfile.new bs=1M count=1 oflag=direct
25047 }
25048 run_test 398e "O_Direct open cleared by fcntl doesn't cause hang"
25049
25050 test_398f() { #  LU-14687
25051         which aiocp || skip_env "no aiocp installed"
25052         local aio_file=$DIR/$tfile.aio
25053
25054         $LFS setstripe -c -1 -S 1M $DIR/$tfile $aio_file
25055
25056         dd if=/dev/zero of=$DIR/$tfile bs=1M count=64
25057         stack_trap "rm -f $DIR/$tfile $aio_file"
25058
25059         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
25060         $LCTL set_param fail_loc=0x1418
25061         # make sure we don't crash and fail properly
25062         aiocp -b 64M -s 64M -f O_DIRECT $DIR/$tfile $aio_file &&
25063                 error "aio with page allocation failure succeeded"
25064         $LCTL set_param fail_loc=0
25065         diff $DIR/$tfile $aio_file
25066         [[ $? != 0 ]] || error "no diff after failed aiocp"
25067 }
25068 run_test 398f "verify aio handles ll_direct_rw_pages errors correctly"
25069
25070 # NB: To get the parallel DIO behavior in LU-13798, there must be > 1
25071 # stripe and i/o size must be > stripe size
25072 # Old style synchronous DIO waits after submitting each chunk, resulting in a
25073 # single RPC in flight.  This test shows async DIO submission is working by
25074 # showing multiple RPCs in flight.
25075 test_398g() { #  LU-13798
25076         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
25077
25078         # We need to do some i/o first to acquire enough grant to put our RPCs
25079         # in flight; otherwise a new connection may not have enough grant
25080         # available
25081         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
25082                 error "parallel dio failed"
25083         stack_trap "rm -f $DIR/$tfile"
25084
25085         # Reduce RPC size to 1M to avoid combination in to larger RPCs
25086         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
25087         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
25088         stack_trap "$LCTL set_param -n $pages_per_rpc"
25089
25090         # Recreate file so it's empty
25091         rm -f $DIR/$tfile
25092         $LFS setstripe -o 0,0 -S 1M $DIR/$tfile
25093         #Pause rpc completion to guarantee we see multiple rpcs in flight
25094         #define OBD_FAIL_OST_BRW_PAUSE_BULK
25095         do_facet ost1 $LCTL set_param fail_loc=0x214 fail_val=2
25096         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25097
25098         # Clear rpc stats
25099         $LCTL set_param osc.*.rpc_stats=c
25100
25101         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
25102                 error "parallel dio failed"
25103         stack_trap "rm -f $DIR/$tfile"
25104
25105         $LCTL get_param osc.*-OST0000-*.rpc_stats
25106         pct=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
25107                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
25108                 grep "8:" | awk '{print $8}')
25109         # We look at the "8 rpcs in flight" field, and verify A) it is present
25110         # and B) it includes all RPCs.  This proves we had 8 RPCs in flight,
25111         # as expected for an 8M DIO to a file with 1M stripes.
25112         [ $pct -eq 100 ] || error "we should see 8 RPCs in flight"
25113
25114         # Verify turning off parallel dio works as expected
25115         # Clear rpc stats
25116         $LCTL set_param osc.*.rpc_stats=c
25117         $LCTL set_param llite.*.parallel_dio=0
25118         stack_trap '$LCTL set_param llite.*.parallel_dio=1'
25119
25120         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=1 oflag=direct ||
25121                 error "dio with parallel dio disabled failed"
25122
25123         # Ideally, we would see only one RPC in flight here, but there is an
25124         # unavoidable race between i/o completion and RPC in flight counting,
25125         # so while only 1 i/o is in flight at a time, the RPC in flight counter
25126         # will sometimes exceed 1 (3 or 4 is not rare on VM testing).
25127         # So instead we just verify it's always < 8.
25128         $LCTL get_param osc.*-OST0000-*.rpc_stats
25129         ret=$($LCTL get_param osc.*-OST0000-*.rpc_stats |
25130                 grep -A 8 'rpcs in flight' | grep -v 'rpcs in flight' |
25131                 grep '^$' -B1 | grep . | awk '{print $1}')
25132         [ $ret != "8:" ] ||
25133                 error "we should see fewer than 8 RPCs in flight (saw $ret)"
25134 }
25135 run_test 398g "verify parallel dio async RPC submission"
25136
25137 test_398h() { #  LU-13798
25138         local dio_file=$DIR/$tfile.dio
25139
25140         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
25141
25142         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25143         stack_trap "rm -f $DIR/$tfile $dio_file"
25144
25145         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct ||
25146                 error "parallel dio failed"
25147         diff $DIR/$tfile $dio_file
25148         [[ $? == 0 ]] || error "file diff after aiocp"
25149 }
25150 run_test 398h "verify correctness of read & write with i/o size >> stripe size"
25151
25152 test_398i() { #  LU-13798
25153         local dio_file=$DIR/$tfile.dio
25154
25155         $LFS setstripe -C 2 -S 1M $DIR/$tfile $dio_file
25156
25157         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25158         stack_trap "rm -f $DIR/$tfile $dio_file"
25159
25160         #define OBD_FAIL_LLITE_PAGE_ALLOC 0x1418
25161         $LCTL set_param fail_loc=0x1418
25162         # make sure we don't crash and fail properly
25163         dd if=$DIR/$tfile of=$dio_file bs=8M count=8 iflag=direct oflag=direct &&
25164                 error "parallel dio page allocation failure succeeded"
25165         diff $DIR/$tfile $dio_file
25166         [[ $? != 0 ]] || error "no diff after failed aiocp"
25167 }
25168 run_test 398i "verify parallel dio handles ll_direct_rw_pages errors correctly"
25169
25170 test_398j() { #  LU-13798
25171         # Stripe size > RPC size but less than i/o size tests split across
25172         # stripes and RPCs for individual i/o op
25173         $LFS setstripe -o 0,0 -S 4M $DIR/$tfile $DIR/$tfile.2
25174
25175         # Reduce RPC size to 1M to guarantee split to multiple RPCs per stripe
25176         local pages_per_rpc=$($LCTL get_param osc.*-OST0000-*.max_pages_per_rpc)
25177         $LCTL set_param osc.*-OST0000-*.max_pages_per_rpc=1M
25178         stack_trap "$LCTL set_param -n $pages_per_rpc"
25179
25180         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25181                 error "parallel dio write failed"
25182         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.2"
25183
25184         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct ||
25185                 error "parallel dio read failed"
25186         diff $DIR/$tfile $DIR/$tfile.2
25187         [[ $? == 0 ]] || error "file diff after parallel dio read"
25188 }
25189 run_test 398j "test parallel dio where stripe size > rpc_size"
25190
25191 test_398k() { #  LU-13798
25192         wait_delete_completed
25193         wait_mds_ost_sync
25194
25195         # 4 stripe file; we will cause out of space on OST0
25196         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
25197
25198         # Fill OST0 (if it's not too large)
25199         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
25200                    head -n1)
25201         if [[ $ORIGFREE -gt $MAXFREE ]]; then
25202                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
25203         fi
25204         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
25205         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
25206                 error "dd should fill OST0"
25207         stack_trap "rm -f $DIR/$tfile.1"
25208
25209         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct
25210         err=$?
25211
25212         ls -la $DIR/$tfile
25213         $CHECKSTAT -t file -s 0 $DIR/$tfile ||
25214                 error "file is not 0 bytes in size"
25215
25216         # dd above should not succeed, but don't error until here so we can
25217         # get debug info above
25218         [[ $err != 0 ]] ||
25219                 error "parallel dio write with enospc succeeded"
25220         stack_trap "rm -f $DIR/$tfile"
25221 }
25222 run_test 398k "test enospc on first stripe"
25223
25224 test_398l() { #  LU-13798
25225         wait_delete_completed
25226         wait_mds_ost_sync
25227
25228         # 4 stripe file; we will cause out of space on OST0
25229         # Note the 1M stripe size and the > 1M i/o size mean this ENOSPC
25230         # happens on the second i/o chunk we issue
25231         $LFS setstripe -o 1,0,1,0 -S 1M $DIR/$tfile $DIR/$tfile.2
25232
25233         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=2 oflag=direct
25234         stack_trap "rm -f $DIR/$tfile"
25235
25236         # Fill OST0 (if it's not too large)
25237         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
25238                    head -n1)
25239         if [[ $ORIGFREE -gt $MAXFREE ]]; then
25240                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
25241         fi
25242         $LFS setstripe -i 0 -c 1 $DIR/$tfile.1
25243         dd if=/dev/zero of=$DIR/$tfile.1 bs=1024 count=$MAXFREE &&
25244                 error "dd should fill OST0"
25245         stack_trap "rm -f $DIR/$tfile.1"
25246
25247         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 oflag=direct
25248         err=$?
25249         stack_trap "rm -f $DIR/$tfile.2"
25250
25251         # Check that short write completed as expected
25252         ls -la $DIR/$tfile.2
25253         $CHECKSTAT -t file -s 1048576 $DIR/$tfile.2 ||
25254                 error "file is not 1M in size"
25255
25256         # dd above should not succeed, but don't error until here so we can
25257         # get debug info above
25258         [[ $err != 0 ]] ||
25259                 error "parallel dio write with enospc succeeded"
25260
25261         # Truncate source file to same length as output file and diff them
25262         $TRUNCATE $DIR/$tfile 1048576
25263         diff $DIR/$tfile $DIR/$tfile.2
25264         [[ $? == 0 ]] || error "data incorrect after short write"
25265 }
25266 run_test 398l "test enospc on intermediate stripe/RPC"
25267
25268 test_398m() { #  LU-13798
25269         $LFS setstripe -o 0,1,0,1 -S 1M $DIR/$tfile
25270
25271         # Set up failure on OST0, the first stripe:
25272         #define OBD_FAIL_OST_BRW_WRITE_BULK     0x20e
25273         #NB: Fail val is ost # + 1, because we cannot use cfs_fail_val = 0
25274         # So this fail_val specifies OST0
25275         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=1
25276         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25277
25278         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
25279                 error "parallel dio write with failure on first stripe succeeded"
25280         stack_trap "rm -f $DIR/$tfile"
25281         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25282
25283         # Place data in file for read
25284         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25285                 error "parallel dio write failed"
25286
25287         # Fail read on OST0, first stripe
25288         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
25289         do_facet ost1 $LCTL set_param fail_loc=0x20f fail_val=1
25290         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
25291                 error "parallel dio read with error on first stripe succeeded"
25292         rm -f $DIR/$tfile.2
25293         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25294
25295         # Switch to testing on OST1, second stripe
25296         # Clear file contents, maintain striping
25297         echo > $DIR/$tfile
25298         # Set up failure on OST1, second stripe:
25299         do_facet ost1 $LCTL set_param fail_loc=0x20e fail_val=2
25300         stack_trap "do_facet ost1 $LCTL set_param fail_loc=0"
25301
25302         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct &&
25303                 error "parallel dio write with failure on first stripe succeeded"
25304         stack_trap "rm -f $DIR/$tfile"
25305         do_facet ost1 $LCTL set_param fail_loc=0 fail_val=0
25306
25307         # Place data in file for read
25308         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 oflag=direct ||
25309                 error "parallel dio write failed"
25310
25311         # Fail read on OST1, second stripe
25312         #define OBD_FAIL_OST_BRW_READ_BULK       0x20f
25313         do_facet ost2 $LCTL set_param fail_loc=0x20f fail_val=2
25314         dd if=$DIR/$tfile of=$DIR/$tfile.2 bs=8M count=8 iflag=direct &&
25315                 error "parallel dio read with error on first stripe succeeded"
25316         rm -f $DIR/$tfile.2
25317         do_facet ost2 $LCTL set_param fail_loc=0 fail_val=0
25318 }
25319 run_test 398m "test RPC failures with parallel dio"
25320
25321 # Parallel submission of DIO should not cause problems for append, but it's
25322 # important to verify.
25323 test_398n() { #  LU-13798
25324         $LFS setstripe -C 2 -S 1M $DIR/$tfile
25325
25326         dd if=/dev/urandom of=$DIR/$tfile bs=8M count=8 ||
25327                 error "dd to create source file failed"
25328         stack_trap "rm -f $DIR/$tfile"
25329
25330         dd if=$DIR/$tfile of=$DIR/$tfile.1 bs=8M count=8 oflag=direct oflag=append ||
25331                 error "parallel dio write with failure on second stripe succeeded"
25332         stack_trap "rm -f $DIR/$tfile $DIR/$tfile.1"
25333         diff $DIR/$tfile $DIR/$tfile.1
25334         [[ $? == 0 ]] || error "data incorrect after append"
25335
25336 }
25337 run_test 398n "test append with parallel DIO"
25338
25339 test_398o() {
25340         directio rdwr $DIR/$tfile 0 1 1 || error "bad KMS"
25341 }
25342 run_test 398o "right kms with DIO"
25343
25344 test_fake_rw() {
25345         local read_write=$1
25346         if [ "$read_write" = "write" ]; then
25347                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
25348         elif [ "$read_write" = "read" ]; then
25349                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
25350         else
25351                 error "argument error"
25352         fi
25353
25354         # turn off debug for performance testing
25355         local saved_debug=$($LCTL get_param -n debug)
25356         $LCTL set_param debug=0
25357
25358         $LFS setstripe -c 1 -i 0 $DIR/$tfile
25359
25360         # get ost1 size - $FSNAME-OST0000
25361         local ost1_avail_size=$($LFS df $DIR | awk /${ost1_svc}/'{ print $4 }')
25362         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
25363         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
25364
25365         if [ "$read_write" = "read" ]; then
25366                 $TRUNCATE $DIR/$tfile $(expr 1048576 \* $blocks)
25367         fi
25368
25369         local start_time=$(date +%s.%N)
25370         $dd_cmd bs=1M count=$blocks oflag=sync ||
25371                 error "real dd $read_write error"
25372         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
25373
25374         if [ "$read_write" = "write" ]; then
25375                 rm -f $DIR/$tfile
25376         fi
25377
25378         # define OBD_FAIL_OST_FAKE_RW           0x238
25379         do_facet ost1 $LCTL set_param fail_loc=0x238
25380
25381         local start_time=$(date +%s.%N)
25382         $dd_cmd bs=1M count=$blocks oflag=sync ||
25383                 error "fake dd $read_write error"
25384         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
25385
25386         if [ "$read_write" = "write" ]; then
25387                 # verify file size
25388                 cancel_lru_locks osc
25389                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
25390                         error "$tfile size not $blocks MB"
25391         fi
25392         do_facet ost1 $LCTL set_param fail_loc=0
25393
25394         echo "fake $read_write $duration_fake vs. normal $read_write" \
25395                 "$duration in seconds"
25396         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
25397                 error_not_in_vm "fake write is slower"
25398
25399         $LCTL set_param -n debug="$saved_debug"
25400         rm -f $DIR/$tfile
25401 }
25402 test_399a() { # LU-7655 for OST fake write
25403         remote_ost_nodsh && skip "remote OST with nodsh"
25404
25405         test_fake_rw write
25406 }
25407 run_test 399a "fake write should not be slower than normal write"
25408
25409 test_399b() { # LU-8726 for OST fake read
25410         remote_ost_nodsh && skip "remote OST with nodsh"
25411         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
25412                 skip_env "ldiskfs only test"
25413         fi
25414
25415         test_fake_rw read
25416 }
25417 run_test 399b "fake read should not be slower than normal read"
25418
25419 test_400a() { # LU-1606, was conf-sanity test_74
25420         if ! which $CC > /dev/null 2>&1; then
25421                 skip_env "$CC is not installed"
25422         fi
25423
25424         local extra_flags=''
25425         local out=$TMP/$tfile
25426         local prefix=/usr/include/lustre
25427         local prog
25428
25429         # Oleg removes c files in his test rig so test if any c files exist
25430         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
25431                 skip_env "Needed c test files are missing"
25432
25433         if ! [[ -d $prefix ]]; then
25434                 # Assume we're running in tree and fixup the include path.
25435                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
25436                 extra_flags+=" -L$LUSTRE/utils/.lib"
25437         fi
25438
25439         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
25440                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
25441                         error "client api broken"
25442         done
25443         rm -f $out
25444 }
25445 run_test 400a "Lustre client api program can compile and link"
25446
25447 test_400b() { # LU-1606, LU-5011
25448         local header
25449         local out=$TMP/$tfile
25450         local prefix=/usr/include/linux/lustre
25451
25452         # We use a hard coded prefix so that this test will not fail
25453         # when run in tree. There are headers in lustre/include/lustre/
25454         # that are not packaged (like lustre_idl.h) and have more
25455         # complicated include dependencies (like config.h and lnet/types.h).
25456         # Since this test about correct packaging we just skip them when
25457         # they don't exist (see below) rather than try to fixup cppflags.
25458
25459         if ! which $CC > /dev/null 2>&1; then
25460                 skip_env "$CC is not installed"
25461         fi
25462
25463         for header in $prefix/*.h; do
25464                 if ! [[ -f "$header" ]]; then
25465                         continue
25466                 fi
25467
25468                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
25469                         continue # lustre_ioctl.h is internal header
25470                 fi
25471
25472                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
25473                         error "cannot compile '$header'"
25474         done
25475         rm -f $out
25476 }
25477 run_test 400b "packaged headers can be compiled"
25478
25479 test_401a() { #LU-7437
25480         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
25481         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
25482
25483         #count the number of parameters by "list_param -R"
25484         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
25485         #count the number of parameters by listing proc files
25486         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
25487         echo "proc_dirs='$proc_dirs'"
25488         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
25489         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
25490                       sort -u | wc -l)
25491
25492         [ $params -eq $procs ] ||
25493                 error "found $params parameters vs. $procs proc files"
25494
25495         # test the list_param -D option only returns directories
25496         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
25497         #count the number of parameters by listing proc directories
25498         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
25499                 sort -u | wc -l)
25500
25501         [ $params -eq $procs ] ||
25502                 error "found $params parameters vs. $procs proc files"
25503 }
25504 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
25505
25506 test_401b() {
25507         # jobid_var may not allow arbitrary values, so use jobid_name
25508         # if available
25509         if $LCTL list_param jobid_name > /dev/null 2>&1; then
25510                 local testname=jobid_name tmp='testing%p'
25511         else
25512                 local testname=jobid_var tmp=testing
25513         fi
25514
25515         local save=$($LCTL get_param -n $testname)
25516
25517         $LCTL set_param foo=bar $testname=$tmp bar=baz &&
25518                 error "no error returned when setting bad parameters"
25519
25520         local jobid_new=$($LCTL get_param -n foe $testname baz)
25521         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
25522
25523         $LCTL set_param -n fog=bam $testname=$save bat=fog
25524         local jobid_old=$($LCTL get_param -n foe $testname bag)
25525         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
25526 }
25527 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
25528
25529 test_401c() {
25530         # jobid_var may not allow arbitrary values, so use jobid_name
25531         # if available
25532         if $LCTL list_param jobid_name > /dev/null 2>&1; then
25533                 local testname=jobid_name
25534         else
25535                 local testname=jobid_var
25536         fi
25537
25538         local jobid_var_old=$($LCTL get_param -n $testname)
25539         local jobid_var_new
25540
25541         $LCTL set_param $testname= &&
25542                 error "no error returned for 'set_param a='"
25543
25544         jobid_var_new=$($LCTL get_param -n $testname)
25545         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
25546                 error "$testname was changed by setting without value"
25547
25548         $LCTL set_param $testname &&
25549                 error "no error returned for 'set_param a'"
25550
25551         jobid_var_new=$($LCTL get_param -n $testname)
25552         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
25553                 error "$testname was changed by setting without value"
25554 }
25555 run_test 401c "Verify 'lctl set_param' without value fails in either format."
25556
25557 test_401d() {
25558         # jobid_var may not allow arbitrary values, so use jobid_name
25559         # if available
25560         if $LCTL list_param jobid_name > /dev/null 2>&1; then
25561                 local testname=jobid_name new_value='foo=bar%p'
25562         else
25563                 local testname=jobid_var new_valuie=foo=bar
25564         fi
25565
25566         local jobid_var_old=$($LCTL get_param -n $testname)
25567         local jobid_var_new
25568
25569         $LCTL set_param $testname=$new_value ||
25570                 error "'set_param a=b' did not accept a value containing '='"
25571
25572         jobid_var_new=$($LCTL get_param -n $testname)
25573         [[ "$jobid_var_new" == "$new_value" ]] ||
25574                 error "'set_param a=b' failed on a value containing '='"
25575
25576         # Reset the $testname to test the other format
25577         $LCTL set_param $testname=$jobid_var_old
25578         jobid_var_new=$($LCTL get_param -n $testname)
25579         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
25580                 error "failed to reset $testname"
25581
25582         $LCTL set_param $testname $new_value ||
25583                 error "'set_param a b' did not accept a value containing '='"
25584
25585         jobid_var_new=$($LCTL get_param -n $testname)
25586         [[ "$jobid_var_new" == "$new_value" ]] ||
25587                 error "'set_param a b' failed on a value containing '='"
25588
25589         $LCTL set_param $testname $jobid_var_old
25590         jobid_var_new=$($LCTL get_param -n $testname)
25591         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
25592                 error "failed to reset $testname"
25593 }
25594 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
25595
25596 test_401e() { # LU-14779
25597         $LCTL list_param -R "ldlm.namespaces.MGC*" ||
25598                 error "lctl list_param MGC* failed"
25599         $LCTL get_param "ldlm.namespaces.MGC*" || error "lctl get_param failed"
25600         $LCTL get_param "ldlm.namespaces.MGC*.lru_size" ||
25601                 error "lctl get_param lru_size failed"
25602 }
25603 run_test 401e "verify 'lctl get_param' works with NID in parameter"
25604
25605 test_402() {
25606         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
25607         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
25608                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
25609         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
25610                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
25611                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
25612         remote_mds_nodsh && skip "remote MDS with nodsh"
25613
25614         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
25615 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
25616         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
25617         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
25618                 echo "Touch failed - OK"
25619 }
25620 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
25621
25622 test_403() {
25623         local file1=$DIR/$tfile.1
25624         local file2=$DIR/$tfile.2
25625         local tfile=$TMP/$tfile
25626
25627         rm -f $file1 $file2 $tfile
25628
25629         touch $file1
25630         ln $file1 $file2
25631
25632         # 30 sec OBD_TIMEOUT in ll_getattr()
25633         # right before populating st_nlink
25634         $LCTL set_param fail_loc=0x80001409
25635         stat -c %h $file1 > $tfile &
25636
25637         # create an alias, drop all locks and reclaim the dentry
25638         < $file2
25639         cancel_lru_locks mdc
25640         cancel_lru_locks osc
25641         sysctl -w vm.drop_caches=2
25642
25643         wait
25644
25645         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
25646
25647         rm -f $tfile $file1 $file2
25648 }
25649 run_test 403 "i_nlink should not drop to zero due to aliasing"
25650
25651 test_404() { # LU-6601
25652         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
25653                 skip "Need server version newer than 2.8.52"
25654         remote_mds_nodsh && skip "remote MDS with nodsh"
25655
25656         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
25657                 awk '/osp .*-osc-MDT/ { print $4}')
25658
25659         local osp
25660         for osp in $mosps; do
25661                 echo "Deactivate: " $osp
25662                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
25663                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
25664                         awk -vp=$osp '$4 == p { print $2 }')
25665                 [ $stat = IN ] || {
25666                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
25667                         error "deactivate error"
25668                 }
25669                 echo "Activate: " $osp
25670                 do_facet $SINGLEMDS $LCTL --device %$osp activate
25671                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
25672                         awk -vp=$osp '$4 == p { print $2 }')
25673                 [ $stat = UP ] || {
25674                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
25675                         error "activate error"
25676                 }
25677         done
25678 }
25679 run_test 404 "validate manual {de}activated works properly for OSPs"
25680
25681 test_405() {
25682         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
25683         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
25684                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
25685                         skip "Layout swap lock is not supported"
25686
25687         check_swap_layouts_support
25688         check_swap_layout_no_dom $DIR
25689
25690         test_mkdir $DIR/$tdir
25691         swap_lock_test -d $DIR/$tdir ||
25692                 error "One layout swap locked test failed"
25693 }
25694 run_test 405 "Various layout swap lock tests"
25695
25696 test_406() {
25697         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
25698         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
25699         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
25700         [ $PARALLEL == "yes" ] && skip "skip parallel run"
25701         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
25702                 skip "Need MDS version at least 2.8.50"
25703
25704         local def_stripe_size=$($LFS getstripe -S $MOUNT)
25705         local test_pool=$TESTNAME
25706
25707         pool_add $test_pool || error "pool_add failed"
25708         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
25709                 error "pool_add_targets failed"
25710
25711         save_layout_restore_at_exit $MOUNT
25712
25713         # parent set default stripe count only, child will stripe from both
25714         # parent and fs default
25715         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
25716                 error "setstripe $MOUNT failed"
25717         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
25718         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
25719         for i in $(seq 10); do
25720                 local f=$DIR/$tdir/$tfile.$i
25721                 touch $f || error "touch failed"
25722                 local count=$($LFS getstripe -c $f)
25723                 [ $count -eq $OSTCOUNT ] ||
25724                         error "$f stripe count $count != $OSTCOUNT"
25725                 local offset=$($LFS getstripe -i $f)
25726                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
25727                 local size=$($LFS getstripe -S $f)
25728                 [ $size -eq $((def_stripe_size * 2)) ] ||
25729                         error "$f stripe size $size != $((def_stripe_size * 2))"
25730                 local pool=$($LFS getstripe -p $f)
25731                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
25732         done
25733
25734         # change fs default striping, delete parent default striping, now child
25735         # will stripe from new fs default striping only
25736         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
25737                 error "change $MOUNT default stripe failed"
25738         $LFS setstripe -c 0 $DIR/$tdir ||
25739                 error "delete $tdir default stripe failed"
25740         for i in $(seq 11 20); do
25741                 local f=$DIR/$tdir/$tfile.$i
25742                 touch $f || error "touch $f failed"
25743                 local count=$($LFS getstripe -c $f)
25744                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
25745                 local offset=$($LFS getstripe -i $f)
25746                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
25747                 local size=$($LFS getstripe -S $f)
25748                 [ $size -eq $def_stripe_size ] ||
25749                         error "$f stripe size $size != $def_stripe_size"
25750                 local pool=$($LFS getstripe -p $f)
25751                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
25752         done
25753
25754         unlinkmany $DIR/$tdir/$tfile. 1 20
25755
25756         local f=$DIR/$tdir/$tfile
25757         pool_remove_all_targets $test_pool $f
25758         pool_remove $test_pool $f
25759 }
25760 run_test 406 "DNE support fs default striping"
25761
25762 test_407() {
25763         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
25764         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
25765                 skip "Need MDS version at least 2.8.55"
25766         remote_mds_nodsh && skip "remote MDS with nodsh"
25767
25768         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
25769                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
25770         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
25771                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
25772         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
25773
25774         #define OBD_FAIL_DT_TXN_STOP    0x2019
25775         for idx in $(seq $MDSCOUNT); do
25776                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
25777         done
25778         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
25779         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
25780                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
25781         true
25782 }
25783 run_test 407 "transaction fail should cause operation fail"
25784
25785 test_408() {
25786         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
25787
25788         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
25789         lctl set_param fail_loc=0x8000040a
25790         # let ll_prepare_partial_page() fail
25791         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
25792
25793         rm -f $DIR/$tfile
25794
25795         # create at least 100 unused inodes so that
25796         # shrink_icache_memory(0) should not return 0
25797         touch $DIR/$tfile-{0..100}
25798         rm -f $DIR/$tfile-{0..100}
25799         sync
25800
25801         echo 2 > /proc/sys/vm/drop_caches
25802 }
25803 run_test 408 "drop_caches should not hang due to page leaks"
25804
25805 test_409()
25806 {
25807         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
25808
25809         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
25810         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
25811         touch $DIR/$tdir/guard || error "(2) Fail to create"
25812
25813         local PREFIX=$(str_repeat 'A' 128)
25814         echo "Create 1K hard links start at $(date)"
25815         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
25816                 error "(3) Fail to hard link"
25817
25818         echo "Links count should be right although linkEA overflow"
25819         stat $DIR/$tdir/guard || error "(4) Fail to stat"
25820         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
25821         [ $linkcount -eq 1001 ] ||
25822                 error "(5) Unexpected hard links count: $linkcount"
25823
25824         echo "List all links start at $(date)"
25825         ls -l $DIR/$tdir/foo > /dev/null ||
25826                 error "(6) Fail to list $DIR/$tdir/foo"
25827
25828         echo "Unlink hard links start at $(date)"
25829         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
25830                 error "(7) Fail to unlink"
25831         echo "Unlink hard links finished at $(date)"
25832 }
25833 run_test 409 "Large amount of cross-MDTs hard links on the same file"
25834
25835 test_410()
25836 {
25837         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
25838                 skip "Need client version at least 2.9.59"
25839         [ -f $LUSTRE/tests/kernel/kinode.ko ] ||
25840                 skip "Need MODULES build"
25841
25842         # Create a file, and stat it from the kernel
25843         local testfile=$DIR/$tfile
25844         touch $testfile
25845
25846         local run_id=$RANDOM
25847         local my_ino=$(stat --format "%i" $testfile)
25848
25849         # Try to insert the module. This will always fail as the
25850         # module is designed to not be inserted.
25851         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
25852             &> /dev/null
25853
25854         # Anything but success is a test failure
25855         dmesg | grep -q \
25856             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
25857             error "no inode match"
25858 }
25859 run_test 410 "Test inode number returned from kernel thread"
25860
25861 cleanup_test411_cgroup() {
25862         trap 0
25863         rmdir "$1"
25864 }
25865
25866 test_411() {
25867         local cg_basedir=/sys/fs/cgroup/memory
25868         # LU-9966
25869         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
25870                 skip "no setup for cgroup"
25871
25872         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
25873                 error "test file creation failed"
25874         cancel_lru_locks osc
25875
25876         # Create a very small memory cgroup to force a slab allocation error
25877         local cgdir=$cg_basedir/osc_slab_alloc
25878         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
25879         trap "cleanup_test411_cgroup $cgdir" EXIT
25880         echo 2M > $cgdir/memory.kmem.limit_in_bytes
25881         echo 1M > $cgdir/memory.limit_in_bytes
25882
25883         # Should not LBUG, just be killed by oom-killer
25884         # dd will return 0 even allocation failure in some environment.
25885         # So don't check return value
25886         bash -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
25887         cleanup_test411_cgroup $cgdir
25888
25889         return 0
25890 }
25891 run_test 411 "Slab allocation error with cgroup does not LBUG"
25892
25893 test_412() {
25894         (( $MDSCOUNT > 1 )) || skip_env "needs >= 2 MDTs"
25895         (( $MDS1_VERSION >= $(version_code 2.10.55) )) ||
25896                 skip "Need server version at least 2.10.55"
25897
25898         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
25899                 error "mkdir failed"
25900         $LFS getdirstripe $DIR/$tdir
25901         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
25902         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
25903                 error "expect $((MDSCOUT - 1)) get $stripe_index"
25904         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
25905         [ $stripe_count -eq 2 ] ||
25906                 error "expect 2 get $stripe_count"
25907
25908         (( $MDS1_VERSION >= $(version_code 2.14.55) )) || return 0
25909
25910         local index
25911         local index2
25912
25913         # subdirs should be on the same MDT as parent
25914         for i in $(seq 0 $((MDSCOUNT - 1))); do
25915                 $LFS mkdir -i $i $DIR/$tdir/mdt$i || error "mkdir mdt$i failed"
25916                 mkdir $DIR/$tdir/mdt$i/sub || error "mkdir sub failed"
25917                 index=$($LFS getstripe -m $DIR/$tdir/mdt$i/sub)
25918                 (( index == i )) || error "mdt$i/sub on MDT$index"
25919         done
25920
25921         # stripe offset -1, ditto
25922         for i in {1..10}; do
25923                 $LFS mkdir -i -1 $DIR/$tdir/qos$i || error "mkdir qos$i failed"
25924                 index=$($LFS getstripe -m $DIR/$tdir/qos$i)
25925                 mkdir $DIR/$tdir/qos$i/sub || error "mkdir sub failed"
25926                 index2=$($LFS getstripe -m $DIR/$tdir/qos$i/sub)
25927                 (( index == index2 )) ||
25928                         error "qos$i on MDT$index, sub on MDT$index2"
25929         done
25930
25931         local testdir=$DIR/$tdir/inherit
25932
25933         $LFS mkdir -i 1 --max-inherit=3 $testdir || error "mkdir inherit failed"
25934         # inherit 2 levels
25935         for i in 1 2; do
25936                 testdir=$testdir/s$i
25937                 mkdir $testdir || error "mkdir $testdir failed"
25938                 index=$($LFS getstripe -m $testdir)
25939                 (( index == 1 )) ||
25940                         error "$testdir on MDT$index"
25941         done
25942
25943         # not inherit any more
25944         testdir=$testdir/s3
25945         mkdir $testdir || error "mkdir $testdir failed"
25946         getfattr -d -m dmv $testdir | grep dmv &&
25947                 error "default LMV set on $testdir" || true
25948 }
25949 run_test 412 "mkdir on specific MDTs"
25950
25951 TEST413_COUNT=${TEST413_COUNT:-200}
25952 generate_uneven_mdts() {
25953         local threshold=$1
25954         local lmv_qos_maxage
25955         local lod_qos_maxage
25956         local ffree
25957         local bavail
25958         local max
25959         local min
25960         local max_index
25961         local min_index
25962         local tmp
25963         local i
25964
25965         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
25966         $LCTL set_param lmv.*.qos_maxage=1
25967         stack_trap "$LCTL set_param \
25968                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" RETURN
25969         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
25970                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
25971         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
25972                 lod.*.mdt_qos_maxage=1
25973         stack_trap "do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param \
25974                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" RETURN
25975
25976         echo
25977         echo "Check for uneven MDTs: "
25978
25979         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
25980         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
25981         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
25982
25983         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25984         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
25985         max_index=0
25986         min_index=0
25987         for ((i = 1; i < ${#ffree[@]}; i++)); do
25988                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
25989                 if [ $tmp -gt $max ]; then
25990                         max=$tmp
25991                         max_index=$i
25992                 fi
25993                 if [ $tmp -lt $min ]; then
25994                         min=$tmp
25995                         min_index=$i
25996                 fi
25997         done
25998
25999         (( ${ffree[min_index]} > 0 )) ||
26000                 skip "no free files in MDT$min_index"
26001         (( ${ffree[min_index]} < 10000000 )) ||
26002                 skip "too many free files in MDT$min_index"
26003
26004         # Check if we need to generate uneven MDTs
26005         local diff=$(((max - min) * 100 / min))
26006         local testdir=$DIR/$tdir-fillmdt
26007         local start
26008
26009         i=0
26010         while (( diff < threshold )); do
26011                 mkdir -p $testdir
26012                 # generate uneven MDTs, create till $threshold% diff
26013                 echo -n "weight diff=$diff% must be > $threshold% ..."
26014                 echo "Fill MDT$min_index with $TEST413_COUNT files: loop $i"
26015                 testdir=$DIR/$tdir-fillmdt/$i
26016                 [ -d $testdir ] && continue
26017                 $LFS mkdir -i $min_index $testdir ||
26018                         error "mkdir $testdir failed"
26019                 $LFS setstripe -E 1M -L mdt $testdir ||
26020                         error "setstripe $testdir failed"
26021                 start=$SECONDS
26022                 for ((F=0; F < TEST413_COUNT; F++)); do
26023                         dd if=/dev/zero of=$testdir/f.$F bs=128K count=1 > \
26024                                 /dev/null 2>&1 || error "dd $F failed"
26025                 done
26026                 sync; sleep 1; sync
26027
26028                 # wait for QOS to update
26029                 (( SECONDS < start + 1 )) && sleep $((start + 1 - SECONDS))
26030
26031                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
26032                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
26033                 max=$(((${ffree[max_index]} >> 8) *
26034                         (${bavail[max_index]} * bsize >> 16)))
26035                 min=$(((${ffree[min_index]} >> 8) *
26036                         (${bavail[min_index]} * bsize >> 16)))
26037                 diff=$(((max - min) * 100 / min))
26038                 i=$((i + 1))
26039         done
26040
26041         echo "MDT filesfree available: ${ffree[*]}"
26042         echo "MDT blocks available: ${bavail[*]}"
26043         echo "weight diff=$diff%"
26044 }
26045
26046 test_qos_mkdir() {
26047         local mkdir_cmd=$1
26048         local stripe_count=$2
26049         local mdts=$(comma_list $(mdts_nodes))
26050
26051         local testdir
26052         local lmv_qos_prio_free
26053         local lmv_qos_threshold_rr
26054         local lmv_qos_maxage
26055         local lod_qos_prio_free
26056         local lod_qos_threshold_rr
26057         local lod_qos_maxage
26058         local count
26059         local i
26060
26061         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
26062         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
26063         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
26064                 head -n1)
26065         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
26066         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
26067         stack_trap "$LCTL set_param \
26068                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null"
26069         stack_trap "$LCTL set_param \
26070                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null"
26071         stack_trap "$LCTL set_param \
26072                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
26073
26074         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
26075                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
26076         lod_qos_prio_free=${lod_qos_prio_free%%%}
26077         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
26078                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
26079         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
26080         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
26081                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
26082         stack_trap "do_nodes $mdts $LCTL set_param \
26083                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null"
26084         stack_trap "do_nodes $mdts $LCTL set_param \
26085                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null"
26086         stack_trap "do_nodes $mdts $LCTL set_param \
26087                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null"
26088
26089         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
26090         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
26091
26092         testdir=$DIR/$tdir-s$stripe_count/rr
26093
26094         local stripe_index=$($LFS getstripe -m $testdir)
26095         local test_mkdir_rr=true
26096
26097         getfattr -d -m dmv -e hex $testdir | grep dmv
26098         if (( $? == 0 && $MDS1_VERSION >= $(version_code 2.14.51) )); then
26099                 echo "defstripe: '$($LFS getdirstripe -D $testdir)'"
26100                 (( $($LFS getdirstripe -D --max-inherit-rr $testdir) == 0 )) &&
26101                         test_mkdir_rr=false
26102         fi
26103
26104         echo
26105         $test_mkdir_rr &&
26106                 echo "Mkdir (stripe_count $stripe_count) roundrobin:" ||
26107                 echo "Mkdir (stripe_count $stripe_count) on stripe $stripe_index"
26108
26109         stack_trap "unlinkmany -d $testdir/subdir $((100 * MDSCOUNT))"
26110         for (( i = 0; i < 100 * MDSCOUNT; i++ )); do
26111                 eval $mkdir_cmd $testdir/subdir$i ||
26112                         error "$mkdir_cmd subdir$i failed"
26113         done
26114
26115         for (( i = 0; i < $MDSCOUNT; i++ )); do
26116                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
26117                 echo "$count directories created on MDT$i"
26118                 if $test_mkdir_rr; then
26119                         (( $count == 100 )) ||
26120                                 error "subdirs are not evenly distributed"
26121                 elif (( $i == $stripe_index )); then
26122                         (( $count == 100 * MDSCOUNT )) ||
26123                                 error "$count subdirs created on MDT$i"
26124                 else
26125                         (( $count == 0 )) ||
26126                                 error "$count subdirs created on MDT$i"
26127                 fi
26128
26129                 if $test_mkdir_rr && [ $stripe_count -gt 1 ]; then
26130                         count=$($LFS getdirstripe $testdir/* |
26131                                 grep -c -P "^\s+$i\t")
26132                         echo "$count stripes created on MDT$i"
26133                         # deviation should < 5% of average
26134                         (( $count >= 95 * stripe_count &&
26135                            $count <= 105 * stripe_count)) ||
26136                                 error "stripes are not evenly distributed"
26137                 fi
26138         done
26139
26140         echo
26141         echo "Check for uneven MDTs: "
26142
26143         local ffree
26144         local bavail
26145         local max
26146         local min
26147         local max_index
26148         local min_index
26149         local tmp
26150
26151         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26152         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26153         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26154
26155         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26156         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26157         max_index=0
26158         min_index=0
26159         for ((i = 1; i < ${#ffree[@]}; i++)); do
26160                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26161                 if [ $tmp -gt $max ]; then
26162                         max=$tmp
26163                         max_index=$i
26164                 fi
26165                 if [ $tmp -lt $min ]; then
26166                         min=$tmp
26167                         min_index=$i
26168                 fi
26169         done
26170
26171         (( ${ffree[min_index]} > 0 )) ||
26172                 skip "no free files in MDT$min_index"
26173         (( ${ffree[min_index]} < 10000000 )) ||
26174                 skip "too many free files in MDT$min_index"
26175
26176         echo "MDT filesfree available: ${ffree[*]}"
26177         echo "MDT blocks available: ${bavail[*]}"
26178         echo "weight diff=$(((max - min) * 100 / min))%"
26179         echo
26180         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
26181
26182         $LCTL set_param lmv.*.qos_threshold_rr=0 > /dev/null
26183         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
26184         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=0 > /dev/null
26185         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
26186         # decrease statfs age, so that it can be updated in time
26187         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
26188         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
26189
26190         sleep 1
26191
26192         testdir=$DIR/$tdir-s$stripe_count/qos
26193         local num=200
26194
26195         stack_trap "unlinkmany -d $testdir/subdir $((num * MDSCOUNT))"
26196         for (( i = 0; i < num * MDSCOUNT; i++ )); do
26197                 eval $mkdir_cmd $testdir/subdir$i ||
26198                         error "$mkdir_cmd subdir$i failed"
26199         done
26200
26201         max=0
26202         for (( i = 0; i < $MDSCOUNT; i++ )); do
26203                 count=$($LFS getdirstripe -i $testdir/* | grep -c "^$i$")
26204                 (( count > max )) && max=$count
26205                 echo "$count directories created on MDT$i"
26206         done
26207
26208         min=$($LFS getdirstripe -i $testdir/* | grep -c "^$min_index$")
26209
26210         # D-value should > 10% of averge
26211         (( max - min > num / 10 )) ||
26212                 error "subdirs shouldn't be evenly distributed: $max - $min < $((num / 10))"
26213
26214         # ditto for stripes
26215         if (( stripe_count > 1 )); then
26216                 max=0
26217                 for (( i = 0; i < $MDSCOUNT; i++ )); do
26218                         count=$($LFS getdirstripe $testdir/* |
26219                                 grep -c -P "^\s+$i\t")
26220                         (( count > max )) && max=$count
26221                         echo "$count stripes created on MDT$i"
26222                 done
26223
26224                 min=$($LFS getdirstripe $testdir/* |
26225                         grep -c -P "^\s+$min_index\t")
26226                 (( max - min > num * stripe_count / 10 )) ||
26227                         error "stripes shouldn't be evenly distributed: $max - $min < $((num / 10)) * $stripe_count"
26228         fi
26229 }
26230
26231 most_full_mdt() {
26232         local ffree
26233         local bavail
26234         local bsize
26235         local min
26236         local min_index
26237         local tmp
26238
26239         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
26240         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
26241         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
26242
26243         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
26244         min_index=0
26245         for ((i = 1; i < ${#ffree[@]}; i++)); do
26246                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
26247                 (( tmp < min )) && min=$tmp && min_index=$i
26248         done
26249
26250         echo -n $min_index
26251 }
26252
26253 test_413a() {
26254         [ $MDSCOUNT -lt 2 ] &&
26255                 skip "We need at least 2 MDTs for this test"
26256
26257         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
26258                 skip "Need server version at least 2.12.52"
26259
26260         local stripe_count
26261
26262         generate_uneven_mdts 100
26263         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
26264                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
26265                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
26266                 $LFS mkdir -i $(most_full_mdt) $DIR/$tdir-s$stripe_count/qos ||
26267                         error "mkdir failed"
26268                 test_qos_mkdir "$LFS mkdir -i -1 -c $stripe_count" $stripe_count
26269         done
26270 }
26271 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
26272
26273 test_413b() {
26274         [ $MDSCOUNT -lt 2 ] &&
26275                 skip "We need at least 2 MDTs for this test"
26276
26277         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
26278                 skip "Need server version at least 2.12.52"
26279
26280         local testdir
26281         local stripe_count
26282
26283         generate_uneven_mdts 100
26284         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
26285                 testdir=$DIR/$tdir-s$stripe_count
26286                 mkdir $testdir || error "mkdir $testdir failed"
26287                 mkdir $testdir/rr || error "mkdir rr failed"
26288                 $LFS mkdir -i $(most_full_mdt) $testdir/qos ||
26289                         error "mkdir qos failed"
26290                 $LFS setdirstripe -D -c $stripe_count --max-inherit-rr 2 \
26291                         $testdir/rr || error "setdirstripe rr failed"
26292                 $LFS setdirstripe -D -c $stripe_count $testdir/qos ||
26293                         error "setdirstripe failed"
26294                 test_qos_mkdir "mkdir" $stripe_count
26295         done
26296 }
26297 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
26298
26299 test_413c() {
26300         (( $MDSCOUNT >= 2 )) ||
26301                 skip "We need at least 2 MDTs for this test"
26302
26303         (( $MDS1_VERSION >= $(version_code 2.14.51) )) ||
26304                 skip "Need server version at least 2.14.51"
26305
26306         local testdir
26307         local inherit
26308         local inherit_rr
26309
26310         testdir=$DIR/${tdir}-s1
26311         mkdir $testdir || error "mkdir $testdir failed"
26312         mkdir $testdir/rr || error "mkdir rr failed"
26313         $LFS mkdir -i $(most_full_mdt) $testdir/qos || error "mkdir qos failed"
26314         # default max_inherit is -1, default max_inherit_rr is 0
26315         $LFS setdirstripe -D -c 1 $testdir/rr ||
26316                 error "setdirstripe rr failed"
26317         $LFS setdirstripe -D -c 1 -i -1 -X 2 --max-inherit-rr 1 $testdir/qos ||
26318                 error "setdirstripe qos failed"
26319         test_qos_mkdir "mkdir" 1
26320
26321         mkdir $testdir/rr/level1 || error "mkdir rr/level1 failed"
26322         inherit=$($LFS getdirstripe -D -X $testdir/rr/level1)
26323         (( $inherit == -1 )) || error "rr/level1 inherit $inherit != -1"
26324         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/rr/level1)
26325         (( $inherit_rr == 0 )) || error "rr/level1 inherit-rr $inherit_rr != 0"
26326
26327         mkdir $testdir/qos/level1 || error "mkdir qos/level1 failed"
26328         inherit=$($LFS getdirstripe -D -X $testdir/qos/level1)
26329         (( $inherit == 1 )) || error "qos/level1 inherit $inherit != 1"
26330         inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir/qos/level1)
26331         (( $inherit_rr == 0 )) || error "qos/level1 inherit-rr $inherit_rr != 0"
26332         mkdir $testdir/qos/level1/level2 || error "mkdir level2 failed"
26333         getfattr -d -m dmv -e hex $testdir/qos/level1/level2 | grep dmv &&
26334                 error "level2 shouldn't have default LMV" || true
26335 }
26336 run_test 413c "mkdir with default LMV max inherit rr"
26337
26338 test_413d() {
26339         (( MDSCOUNT >= 2 )) ||
26340                 skip "We need at least 2 MDTs for this test"
26341
26342         (( MDS1_VERSION >= $(version_code 2.14.51) )) ||
26343                 skip "Need server version at least 2.14.51"
26344
26345         local lmv_qos_threshold_rr
26346
26347         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
26348                 head -n1)
26349         stack_trap "$LCTL set_param \
26350                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
26351
26352         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
26353         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
26354         getfattr -d -m dmv -e hex $DIR/$tdir | grep dmv &&
26355                 error "$tdir shouldn't have default LMV"
26356         createmany -d $DIR/$tdir/sub $((100 * MDSCOUNT)) ||
26357                 error "mkdir sub failed"
26358
26359         local count=$($LFS getstripe -m $DIR/$tdir/* | grep -c ^0)
26360
26361         (( count == 100 )) || error "$count subdirs on MDT0"
26362 }
26363 run_test 413d "inherit ROOT default LMV"
26364
26365 test_413e() {
26366         (( MDSCOUNT >= 2 )) ||
26367                 skip "We need at least 2 MDTs for this test"
26368         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
26369                 skip "Need server version at least 2.14.55"
26370
26371         local testdir=$DIR/$tdir
26372         local tmpfile=$TMP/temp.setdirstripe.stderr.$$
26373         local max_inherit
26374         local sub_max_inherit
26375
26376         mkdir -p $testdir || error "failed to create $testdir"
26377
26378         # set default max-inherit to -1 if stripe count is 0 or 1
26379         $LFS setdirstripe -D -c 1 $testdir ||
26380                 error "failed to set default LMV"
26381         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
26382         (( max_inherit == -1 )) ||
26383                 error "wrong max_inherit value $max_inherit"
26384
26385         # set default max_inherit to a fixed value if stripe count is not 0 or 1
26386         $LFS setdirstripe -D -c -1 $testdir ||
26387                 error "failed to set default LMV"
26388         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
26389         (( max_inherit > 0 )) ||
26390                 error "wrong max_inherit value $max_inherit"
26391
26392         # and the subdir will decrease the max_inherit by 1
26393         mkdir -p $testdir/subdir-1 || error "failed to make subdir"
26394         sub_max_inherit=$($LFS getdirstripe -D --max-inherit $testdir/subdir-1)
26395         (( sub_max_inherit == max_inherit - 1)) ||
26396                 error "wrong max-inherit of subdir $sub_max_inherit"
26397
26398         # check specified --max-inherit and warning message
26399         stack_trap "rm -f $tmpfile"
26400         $LFS setdirstripe -D -c 2 --max-inherit=-1 $testdir 2> $tmpfile ||
26401                 error "failed to set default LMV"
26402         max_inherit=$($LFS getdirstripe -D --max-inherit $testdir)
26403         (( max_inherit == -1 )) ||
26404                 error "wrong max_inherit value $max_inherit"
26405
26406         # check the warning messages
26407         if ! [[ $(cat $tmpfile) =~ "max-inherit=" ]]; then
26408                 error "failed to detect warning string"
26409         fi
26410 }
26411 run_test 413e "check default max-inherit value"
26412
26413 test_fs_dmv_inherit()
26414 {
26415         local testdir=$DIR/$tdir
26416
26417         local count
26418         local inherit
26419         local inherit_rr
26420
26421         for i in 1 2 3; do
26422                 mkdir $testdir || error "mkdir $testdir failed"
26423                 count=$($LFS getdirstripe -D -c $testdir)
26424                 (( count == 1 )) ||
26425                         error "$testdir default LMV count mismatch $count != 1"
26426                 inherit=$($LFS getdirstripe -D -X $testdir)
26427                 (( inherit == 3 - i )) ||
26428                         error "$testdir default LMV max-inherit $inherit != $((3 - i))"
26429                 inherit_rr=$($LFS getdirstripe -D --max-inherit-rr $testdir)
26430                 (( inherit_rr == 3 - i )) ||
26431                         error "$testdir default LMV max-inherit-rr $inherit_rr != $((3 - i))"
26432                 testdir=$testdir/sub
26433         done
26434
26435         mkdir $testdir || error "mkdir $testdir failed"
26436         count=$($LFS getdirstripe -D -c $testdir)
26437         (( count == 0 )) ||
26438                 error "$testdir default LMV count not zero: $count"
26439 }
26440
26441 test_413f() {
26442         (( MDSCOUNT >= 2 )) || skip "We need at least 2 MDTs for this test"
26443
26444         (( MDS1_VERSION >= $(version_code 2.14.55) )) ||
26445                 skip "Need server version at least 2.14.55"
26446
26447         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
26448                 error "dump $DIR default LMV failed"
26449         stack_trap "setfattr --restore=$TMP/dmv.ea"
26450
26451         $LFS setdirstripe -D -i -1 -c 1 -X 3 --max-inherit-rr 3 $DIR ||
26452                 error "set $DIR default LMV failed"
26453
26454         test_fs_dmv_inherit
26455 }
26456 run_test 413f "lfs getdirstripe -D list ROOT default LMV if it's not set on dir"
26457
26458 test_413g() {
26459         (( MDSCOUNT >= 2 )) || skip "We need at least 2 MDTs for this test"
26460
26461         mkdir -p $DIR/$tdir/l2/l3/l4 || error "mkdir $tdir/l1/l2/l3 failed"
26462         getfattr -d -m trusted.dmv --absolute-names $DIR > $TMP/dmv.ea ||
26463                 error "dump $DIR default LMV failed"
26464         stack_trap "setfattr --restore=$TMP/dmv.ea"
26465
26466         $LFS setdirstripe -D -i -1 -c 1 -X 3 --max-inherit-rr 3 $DIR ||
26467                 error "set $DIR default LMV failed"
26468
26469         FILESET="$FILESET/$tdir/l2/l3/l4" mount_client $MOUNT2 ||
26470                 error "mount $MOUNT2 failed"
26471         stack_trap "umount_client $MOUNT2"
26472
26473         local saved_DIR=$DIR
26474
26475         export DIR=$MOUNT2
26476
26477         stack_trap "export DIR=$saved_DIR"
26478
26479         # first check filesystem-wide default LMV inheritance
26480         test_fs_dmv_inherit || error "incorrect fs default LMV inheritance"
26481
26482         # then check subdirs are spread to all MDTs
26483         createmany -d $DIR/s $((MDSCOUNT * 100)) || error "createmany failed"
26484
26485         local count=$($LFS getstripe -m $DIR/s* | sort -u | wc -l)
26486
26487         (( $count == $MDSCOUNT )) || error "dirs are spread to $count MDTs"
26488 }
26489 run_test 413g "enforce ROOT default LMV on subdir mount"
26490
26491 test_413h() {
26492         (( MDSCOUNT >= 2 )) ||
26493                 skip "We need at least 2 MDTs for this test"
26494
26495         (( MDS1_VERSION >= $(version_code 2.15.50.6) )) ||
26496                 skip "Need server version at least 2.15.50.6"
26497
26498         local lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
26499
26500         stack_trap "$LCTL set_param \
26501                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null"
26502         $LCTL set_param lmv.*.qos_maxage=1
26503
26504         local depth=5
26505         local rr_depth=4
26506         local dir=$DIR/$tdir/l1/l2/l3/l4/l5
26507         local count=$((MDSCOUNT * 20))
26508
26509         generate_uneven_mdts 50
26510
26511         mkdir -p $dir || error "mkdir $dir failed"
26512         stack_trap "rm -rf $dir"
26513         $LFS setdirstripe -D -c 1 -i -1 --max-inherit=$depth \
26514                 --max-inherit-rr=$rr_depth $dir
26515
26516         for ((d=0; d < depth + 2; d++)); do
26517                 log "dir=$dir:"
26518                 for ((sub=0; sub < count; sub++)); do
26519                         mkdir $dir/d$sub
26520                 done
26521                 $LFS getdirstripe -i $dir/d* | sort | uniq -c | sort -nr
26522                 local num=($($LFS getdirstripe -i $dir/d* | sort | uniq -c))
26523                 # subdirs within $rr_depth should be created round-robin
26524                 if (( d < rr_depth )); then
26525                         (( ${num[0]} != count )) ||
26526                                 error "all objects created on MDT ${num[1]}"
26527                 fi
26528
26529                 dir=$dir/d0
26530         done
26531 }
26532 run_test 413h "don't stick to parent for round-robin dirs"
26533
26534 test_413z() {
26535         local pids=""
26536         local subdir
26537         local pid
26538
26539         for subdir in $(\ls -1 -d $DIR/d413*-fillmdt/*); do
26540                 unlinkmany $subdir/f. $TEST413_COUNT &
26541                 pids="$pids $!"
26542         done
26543
26544         for pid in $pids; do
26545                 wait $pid
26546         done
26547 }
26548 run_test 413z "413 test cleanup"
26549
26550 test_414() {
26551 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
26552         $LCTL set_param fail_loc=0x80000521
26553         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
26554         rm -f $DIR/$tfile
26555 }
26556 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
26557
26558 test_415() {
26559         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26560         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
26561                 skip "Need server version at least 2.11.52"
26562
26563         # LU-11102
26564         local total
26565         local setattr_pid
26566         local start_time
26567         local end_time
26568         local duration
26569
26570         total=500
26571         # this test may be slow on ZFS
26572         [ "$mds1_FSTYPE" == "zfs" ] && total=50
26573
26574         # though this test is designed for striped directory, let's test normal
26575         # directory too since lock is always saved as CoS lock.
26576         test_mkdir $DIR/$tdir || error "mkdir $tdir"
26577         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
26578
26579         (
26580                 while true; do
26581                         touch $DIR/$tdir
26582                 done
26583         ) &
26584         setattr_pid=$!
26585
26586         start_time=$(date +%s)
26587         for i in $(seq $total); do
26588                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
26589                         > /dev/null
26590         done
26591         end_time=$(date +%s)
26592         duration=$((end_time - start_time))
26593
26594         kill -9 $setattr_pid
26595
26596         echo "rename $total files took $duration sec"
26597         [ $duration -lt 100 ] || error "rename took $duration sec"
26598 }
26599 run_test 415 "lock revoke is not missing"
26600
26601 test_416() {
26602         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
26603                 skip "Need server version at least 2.11.55"
26604
26605         # define OBD_FAIL_OSD_TXN_START    0x19a
26606         do_facet mds1 lctl set_param fail_loc=0x19a
26607
26608         lfs mkdir -c $MDSCOUNT $DIR/$tdir
26609
26610         true
26611 }
26612 run_test 416 "transaction start failure won't cause system hung"
26613
26614 cleanup_417() {
26615         trap 0
26616         do_nodes $(comma_list $(mdts_nodes)) \
26617                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
26618         do_nodes $(comma_list $(mdts_nodes)) \
26619                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
26620         do_nodes $(comma_list $(mdts_nodes)) \
26621                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
26622 }
26623
26624 test_417() {
26625         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26626         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
26627                 skip "Need MDS version at least 2.11.56"
26628
26629         trap cleanup_417 RETURN EXIT
26630
26631         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
26632         do_nodes $(comma_list $(mdts_nodes)) \
26633                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
26634         $LFS migrate -m 0 $DIR/$tdir.1 &&
26635                 error "migrate dir $tdir.1 should fail"
26636
26637         do_nodes $(comma_list $(mdts_nodes)) \
26638                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
26639         $LFS mkdir -i 1 $DIR/$tdir.2 &&
26640                 error "create remote dir $tdir.2 should fail"
26641
26642         do_nodes $(comma_list $(mdts_nodes)) \
26643                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
26644         $LFS mkdir -c 2 $DIR/$tdir.3 &&
26645                 error "create striped dir $tdir.3 should fail"
26646         true
26647 }
26648 run_test 417 "disable remote dir, striped dir and dir migration"
26649
26650 # Checks that the outputs of df [-i] and lfs df [-i] match
26651 #
26652 # usage: check_lfs_df <blocks | inodes> <mountpoint>
26653 check_lfs_df() {
26654         local dir=$2
26655         local inodes
26656         local df_out
26657         local lfs_df_out
26658         local count
26659         local passed=false
26660
26661         # blocks or inodes
26662         [ "$1" == "blocks" ] && inodes= || inodes="-i"
26663
26664         for count in {1..100}; do
26665                 do_nodes "$CLIENTS" \
26666                         $LCTL set_param ldlm.namespaces.*.lru_size=clear
26667                 sync; sleep 0.2
26668
26669                 # read the lines of interest
26670                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
26671                         error "df $inodes $dir | tail -n +2 failed"
26672                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
26673                         error "lfs df $inodes $dir | grep summary: failed"
26674
26675                 # skip first substrings of each output as they are different
26676                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
26677                 # compare the two outputs
26678                 passed=true
26679                 #  skip "available" on MDT until LU-13997 is fixed.
26680                 #for i in {1..5}; do
26681                 for i in 1 2 4 5; do
26682                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
26683                 done
26684                 $passed && break
26685         done
26686
26687         if ! $passed; then
26688                 df -P $inodes $dir
26689                 echo
26690                 lfs df $inodes $dir
26691                 error "df and lfs df $1 output mismatch: "      \
26692                       "df ${inodes}: ${df_out[*]}, "            \
26693                       "lfs df ${inodes}: ${lfs_df_out[*]}"
26694         fi
26695 }
26696
26697 test_418() {
26698         [ $PARALLEL == "yes" ] && skip "skip parallel run"
26699
26700         local dir=$DIR/$tdir
26701         local numfiles=$((RANDOM % 4096 + 2))
26702         local numblocks=$((RANDOM % 256 + 1))
26703
26704         wait_delete_completed
26705         test_mkdir $dir
26706
26707         # check block output
26708         check_lfs_df blocks $dir
26709         # check inode output
26710         check_lfs_df inodes $dir
26711
26712         # create a single file and retest
26713         echo "Creating a single file and testing"
26714         createmany -o $dir/$tfile- 1 &>/dev/null ||
26715                 error "creating 1 file in $dir failed"
26716         check_lfs_df blocks $dir
26717         check_lfs_df inodes $dir
26718
26719         # create a random number of files
26720         echo "Creating $((numfiles - 1)) files and testing"
26721         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
26722                 error "creating $((numfiles - 1)) files in $dir failed"
26723
26724         # write a random number of blocks to the first test file
26725         echo "Writing $numblocks 4K blocks and testing"
26726         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
26727                 count=$numblocks &>/dev/null ||
26728                 error "dd to $dir/${tfile}-0 failed"
26729
26730         # retest
26731         check_lfs_df blocks $dir
26732         check_lfs_df inodes $dir
26733
26734         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
26735                 error "unlinking $numfiles files in $dir failed"
26736 }
26737 run_test 418 "df and lfs df outputs match"
26738
26739 test_419()
26740 {
26741         local dir=$DIR/$tdir
26742
26743         mkdir -p $dir
26744         touch $dir/file
26745
26746         cancel_lru_locks mdc
26747
26748         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
26749         $LCTL set_param fail_loc=0x1410
26750         cat $dir/file
26751         $LCTL set_param fail_loc=0
26752         rm -rf $dir
26753 }
26754 run_test 419 "Verify open file by name doesn't crash kernel"
26755
26756 test_420()
26757 {
26758         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
26759                 skip "Need MDS version at least 2.12.53"
26760
26761         local SAVE_UMASK=$(umask)
26762         local dir=$DIR/$tdir
26763         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
26764
26765         mkdir -p $dir
26766         umask 0000
26767         mkdir -m03777 $dir/testdir
26768         ls -dn $dir/testdir
26769         # Need to remove trailing '.' when SELinux is enabled
26770         local dirperms=$(ls -dn $dir/testdir |
26771                          awk '{ sub(/\.$/, "", $1); print $1}')
26772         [ $dirperms == "drwxrwsrwt" ] ||
26773                 error "incorrect perms on $dir/testdir"
26774
26775         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
26776                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
26777         ls -n $dir/testdir/testfile
26778         local fileperms=$(ls -n $dir/testdir/testfile |
26779                           awk '{ sub(/\.$/, "", $1); print $1}')
26780         [ $fileperms == "-rwxr-xr-x" ] ||
26781                 error "incorrect perms on $dir/testdir/testfile"
26782
26783         umask $SAVE_UMASK
26784 }
26785 run_test 420 "clear SGID bit on non-directories for non-members"
26786
26787 test_421a() {
26788         local cnt
26789         local fid1
26790         local fid2
26791
26792         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26793                 skip "Need MDS version at least 2.12.54"
26794
26795         test_mkdir $DIR/$tdir
26796         createmany -o $DIR/$tdir/f 3
26797         cnt=$(ls -1 $DIR/$tdir | wc -l)
26798         [ $cnt != 3 ] && error "unexpected #files: $cnt"
26799
26800         fid1=$(lfs path2fid $DIR/$tdir/f1)
26801         fid2=$(lfs path2fid $DIR/$tdir/f2)
26802         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
26803
26804         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
26805         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
26806
26807         cnt=$(ls -1 $DIR/$tdir | wc -l)
26808         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
26809
26810         rm -f $DIR/$tdir/f3 || error "can't remove f3"
26811         createmany -o $DIR/$tdir/f 3
26812         cnt=$(ls -1 $DIR/$tdir | wc -l)
26813         [ $cnt != 3 ] && error "unexpected #files: $cnt"
26814
26815         fid1=$(lfs path2fid $DIR/$tdir/f1)
26816         fid2=$(lfs path2fid $DIR/$tdir/f2)
26817         echo "remove using fsname $FSNAME"
26818         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
26819
26820         cnt=$(ls -1 $DIR/$tdir | wc -l)
26821         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
26822 }
26823 run_test 421a "simple rm by fid"
26824
26825 test_421b() {
26826         local cnt
26827         local FID1
26828         local FID2
26829
26830         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26831                 skip "Need MDS version at least 2.12.54"
26832
26833         test_mkdir $DIR/$tdir
26834         createmany -o $DIR/$tdir/f 3
26835         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
26836         MULTIPID=$!
26837
26838         FID1=$(lfs path2fid $DIR/$tdir/f1)
26839         FID2=$(lfs path2fid $DIR/$tdir/f2)
26840         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
26841
26842         kill -USR1 $MULTIPID
26843         wait
26844
26845         cnt=$(ls $DIR/$tdir | wc -l)
26846         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
26847 }
26848 run_test 421b "rm by fid on open file"
26849
26850 test_421c() {
26851         local cnt
26852         local FIDS
26853
26854         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26855                 skip "Need MDS version at least 2.12.54"
26856
26857         test_mkdir $DIR/$tdir
26858         createmany -o $DIR/$tdir/f 3
26859         touch $DIR/$tdir/$tfile
26860         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
26861         cnt=$(ls -1 $DIR/$tdir | wc -l)
26862         [ $cnt != 184 ] && error "unexpected #files: $cnt"
26863
26864         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
26865         $LFS rmfid $DIR $FID1 || error "rmfid failed"
26866
26867         cnt=$(ls $DIR/$tdir | wc -l)
26868         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
26869 }
26870 run_test 421c "rm by fid against hardlinked files"
26871
26872 test_421d() {
26873         local cnt
26874         local FIDS
26875
26876         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26877                 skip "Need MDS version at least 2.12.54"
26878
26879         test_mkdir $DIR/$tdir
26880         createmany -o $DIR/$tdir/f 4097
26881         cnt=$(ls -1 $DIR/$tdir | wc -l)
26882         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
26883
26884         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
26885         $LFS rmfid $DIR $FIDS || error "rmfid failed"
26886
26887         cnt=$(ls $DIR/$tdir | wc -l)
26888         rm -rf $DIR/$tdir
26889         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
26890 }
26891 run_test 421d "rmfid en masse"
26892
26893 test_421e() {
26894         local cnt
26895         local FID
26896
26897         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26898         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26899                 skip "Need MDS version at least 2.12.54"
26900
26901         mkdir -p $DIR/$tdir
26902         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
26903         createmany -o $DIR/$tdir/striped_dir/f 512
26904         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
26905         [ $cnt != 512 ] && error "unexpected #files: $cnt"
26906
26907         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
26908                 sed "s/[/][^:]*://g")
26909         $LFS rmfid $DIR $FIDS || error "rmfid failed"
26910
26911         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
26912         rm -rf $DIR/$tdir
26913         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
26914 }
26915 run_test 421e "rmfid in DNE"
26916
26917 test_421f() {
26918         local cnt
26919         local FID
26920
26921         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26922                 skip "Need MDS version at least 2.12.54"
26923
26924         test_mkdir $DIR/$tdir
26925         touch $DIR/$tdir/f
26926         cnt=$(ls -1 $DIR/$tdir | wc -l)
26927         [ $cnt != 1 ] && error "unexpected #files: $cnt"
26928
26929         FID=$(lfs path2fid $DIR/$tdir/f)
26930         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
26931         # rmfid should fail
26932         cnt=$(ls -1 $DIR/$tdir | wc -l)
26933         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
26934
26935         chmod a+rw $DIR/$tdir
26936         ls -la $DIR/$tdir
26937         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
26938         # rmfid should fail
26939         cnt=$(ls -1 $DIR/$tdir | wc -l)
26940         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
26941
26942         rm -f $DIR/$tdir/f
26943         $RUNAS touch $DIR/$tdir/f
26944         FID=$(lfs path2fid $DIR/$tdir/f)
26945         echo "rmfid as root"
26946         $LFS rmfid $DIR $FID || error "rmfid as root failed"
26947         cnt=$(ls -1 $DIR/$tdir | wc -l)
26948         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
26949
26950         rm -f $DIR/$tdir/f
26951         $RUNAS touch $DIR/$tdir/f
26952         cnt=$(ls -1 $DIR/$tdir | wc -l)
26953         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
26954         FID=$(lfs path2fid $DIR/$tdir/f)
26955         # rmfid w/o user_fid2path mount option should fail
26956         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
26957         cnt=$(ls -1 $DIR/$tdir | wc -l)
26958         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
26959
26960         tmpdir=$(mktemp -d /tmp/lustre-XXXXXX)
26961         stack_trap "rmdir $tmpdir"
26962         mount_client $tmpdir "$MOUNT_OPTS,user_fid2path" ||
26963                 error "failed to mount client'"
26964         stack_trap "umount_client $tmpdir"
26965
26966         $RUNAS $LFS rmfid $tmpdir $FID || error "rmfid failed"
26967         # rmfid should succeed
26968         cnt=$(ls -1 $tmpdir/$tdir | wc -l)
26969         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
26970
26971         # rmfid shouldn't allow to remove files due to dir's permission
26972         chmod a+rwx $tmpdir/$tdir
26973         touch $tmpdir/$tdir/f
26974         ls -la $tmpdir/$tdir
26975         FID=$(lfs path2fid $tmpdir/$tdir/f)
26976         $RUNAS $LFS rmfid $tmpdir $FID && error "rmfid didn't fail"
26977         return 0
26978 }
26979 run_test 421f "rmfid checks permissions"
26980
26981 test_421g() {
26982         local cnt
26983         local FIDS
26984
26985         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
26986         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
26987                 skip "Need MDS version at least 2.12.54"
26988
26989         mkdir -p $DIR/$tdir
26990         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
26991         createmany -o $DIR/$tdir/striped_dir/f 512
26992         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
26993         [ $cnt != 512 ] && error "unexpected #files: $cnt"
26994
26995         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
26996                 sed "s/[/][^:]*://g")
26997
26998         rm -f $DIR/$tdir/striped_dir/f1*
26999         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
27000         removed=$((512 - cnt))
27001
27002         # few files have been just removed, so we expect
27003         # rmfid to fail on their fids
27004         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
27005         [ $removed != $errors ] && error "$errors != $removed"
27006
27007         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
27008         rm -rf $DIR/$tdir
27009         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
27010 }
27011 run_test 421g "rmfid to return errors properly"
27012
27013 test_422() {
27014         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
27015         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
27016         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
27017         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
27018         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
27019
27020         local amc=$(at_max_get client)
27021         local amo=$(at_max_get mds1)
27022         local timeout=`lctl get_param -n timeout`
27023
27024         at_max_set 0 client
27025         at_max_set 0 mds1
27026
27027 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
27028         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
27029                         fail_val=$(((2*timeout + 10)*1000))
27030         touch $DIR/$tdir/d3/file &
27031         sleep 2
27032 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
27033         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
27034                         fail_val=$((2*timeout + 5))
27035         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
27036         local pid=$!
27037         sleep 1
27038         kill -9 $pid
27039         sleep $((2 * timeout))
27040         echo kill $pid
27041         kill -9 $pid
27042         lctl mark touch
27043         touch $DIR/$tdir/d2/file3
27044         touch $DIR/$tdir/d2/file4
27045         touch $DIR/$tdir/d2/file5
27046
27047         wait
27048         at_max_set $amc client
27049         at_max_set $amo mds1
27050
27051         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
27052         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
27053                 error "Watchdog is always throttled"
27054 }
27055 run_test 422 "kill a process with RPC in progress"
27056
27057 stat_test() {
27058     df -h $MOUNT &
27059     df -h $MOUNT &
27060     df -h $MOUNT &
27061     df -h $MOUNT &
27062     df -h $MOUNT &
27063     df -h $MOUNT &
27064 }
27065
27066 test_423() {
27067     local _stats
27068     # ensure statfs cache is expired
27069     sleep 2;
27070
27071     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
27072     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
27073
27074     return 0
27075 }
27076 run_test 423 "statfs should return a right data"
27077
27078 test_424() {
27079 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
27080         $LCTL set_param fail_loc=0x80000522
27081         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
27082         rm -f $DIR/$tfile
27083 }
27084 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
27085
27086 test_425() {
27087         test_mkdir -c -1 $DIR/$tdir
27088         $LFS setstripe -c -1 $DIR/$tdir
27089
27090         lru_resize_disable "" 100
27091         stack_trap "lru_resize_enable" EXIT
27092
27093         sleep 5
27094
27095         for i in $(seq $((MDSCOUNT * 125))); do
27096                 local t=$DIR/$tdir/$tfile_$i
27097
27098                 dd if=/dev/zero of=$t bs=4K count=1 > /dev/null 2>&1 ||
27099                         error_noexit "Create file $t"
27100         done
27101         stack_trap "rm -rf $DIR/$tdir" EXIT
27102
27103         for oscparam in $($LCTL list_param ldlm.namespaces.*osc-[-0-9a-f]*); do
27104                 local lru_size=$($LCTL get_param -n $oscparam.lru_size)
27105                 local lock_count=$($LCTL get_param -n $oscparam.lock_count)
27106
27107                 [ $lock_count -le $lru_size ] ||
27108                         error "osc lock count $lock_count > lru size $lru_size"
27109         done
27110
27111         for mdcparam in $($LCTL list_param ldlm.namespaces.*mdc-*); do
27112                 local lru_size=$($LCTL get_param -n $mdcparam.lru_size)
27113                 local lock_count=$($LCTL get_param -n $mdcparam.lock_count)
27114
27115                 [ $lock_count -le $lru_size ] ||
27116                         error "mdc lock count $lock_count > lru size $lru_size"
27117         done
27118 }
27119 run_test 425 "lock count should not exceed lru size"
27120
27121 test_426() {
27122         splice-test -r $DIR/$tfile
27123         splice-test -rd $DIR/$tfile
27124         splice-test $DIR/$tfile
27125         splice-test -d $DIR/$tfile
27126 }
27127 run_test 426 "splice test on Lustre"
27128
27129 test_427() {
27130         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
27131         (( $MDS1_VERSION >= $(version_code 2.12.4) )) ||
27132                 skip "Need MDS version at least 2.12.4"
27133         local log
27134
27135         mkdir $DIR/$tdir
27136         mkdir $DIR/$tdir/1
27137         mkdir $DIR/$tdir/2
27138         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/1/dir
27139         test_mkdir -c $MDSCOUNT -i 1 $DIR/$tdir/2/dir2
27140
27141         $LFS getdirstripe $DIR/$tdir/1/dir
27142
27143         #first setfattr for creating updatelog
27144         setfattr -n user.attr0 -v "some text" $DIR/$tdir/1/dir
27145
27146 #define OBD_FAIL_OUT_OBJECT_MISS        0x1708
27147         do_nodes $(comma_list $(mdts_nodes)) $LCTL set_param fail_loc=0x80001708
27148         setfattr -n user.attr1 -v "some text" $DIR/$tdir/1/dir &
27149         setfattr -n user.attr2 -v "another attr"  $DIR/$tdir/2/dir2 &
27150
27151         sleep 2
27152         fail mds2
27153         wait_recovery_complete mds2 $((2*TIMEOUT))
27154
27155         log=$(do_facet mds1 dmesg | tac | sed "/${TESTNAME//_/ }/,$ d")
27156         echo $log | grep "get update log failed" &&
27157                 error "update log corruption is detected" || true
27158 }
27159 run_test 427 "Failed DNE2 update request shouldn't corrupt updatelog"
27160
27161 test_428() {
27162         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27163         local cache_limit=$CACHE_MAX
27164
27165         stack_trap "$LCTL set_param -n llite.*.max_cached_mb=$cache_limit"
27166         $LCTL set_param -n llite.*.max_cached_mb=64
27167
27168         mkdir $DIR/$tdir
27169         $LFS setstripe -c 1 $DIR/$tdir
27170         eval touch $DIR/$tdir/$tfile.{1..$OSTCOUNT}
27171         stack_trap "rm -f $DIR/$tdir/$tfile.*"
27172         #test write
27173         for f in $(seq 4); do
27174                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$f bs=128M count=1 &
27175         done
27176         wait
27177
27178         cancel_lru_locks osc
27179         # Test read
27180         for f in $(seq 4); do
27181                 dd if=$DIR/$tdir/$tfile.$f of=/dev/null bs=128M count=1 &
27182         done
27183         wait
27184 }
27185 run_test 428 "large block size IO should not hang"
27186
27187 test_429() { # LU-7915 / LU-10948
27188         local ll_opencache_threshold_count="llite.*.opencache_threshold_count"
27189         local testfile=$DIR/$tfile
27190         local mdc_rpcstats="mdc.$FSNAME-MDT0000-*.stats"
27191         local new_flag=1
27192         local first_rpc
27193         local second_rpc
27194         local third_rpc
27195
27196         $LCTL get_param $ll_opencache_threshold_count ||
27197                 skip "client does not have opencache parameter"
27198
27199         set_opencache $new_flag
27200         stack_trap "restore_opencache"
27201         [ $($LCTL get_param -n $ll_opencache_threshold_count) == $new_flag ] ||
27202                 error "enable opencache failed"
27203         touch $testfile
27204         # drop MDC DLM locks
27205         cancel_lru_locks mdc
27206         # clear MDC RPC stats counters
27207         $LCTL set_param $mdc_rpcstats=clear
27208
27209         # According to the current implementation, we need to run 3 times
27210         # open & close file to verify if opencache is enabled correctly.
27211         # 1st, RPCs are sent for lookup/open and open handle is released on
27212         #      close finally.
27213         # 2nd, RPC is sent for open, MDS_OPEN_LOCK is fetched automatically,
27214         #      so open handle won't be released thereafter.
27215         # 3rd, No RPC is sent out.
27216         $MULTIOP $testfile oc || error "multiop failed"
27217         first_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
27218         echo "1st: $first_rpc RPCs in flight"
27219
27220         $MULTIOP $testfile oc || error "multiop failed"
27221         second_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
27222         echo "2nd: $second_rpc RPCs in flight"
27223
27224         $MULTIOP $testfile oc || error "multiop failed"
27225         third_rpc=$(calc_stats $mdc_rpcstats ldlm_ibits_enqueue)
27226         echo "3rd: $third_rpc RPCs in flight"
27227
27228         #verify no MDC RPC is sent
27229         [[ $second_rpc == $third_rpc ]] || error "MDC RPC is still sent"
27230 }
27231 run_test 429 "verify if opencache flag on client side does work"
27232
27233 lseek_test_430() {
27234         local offset
27235         local file=$1
27236
27237         # data at [200K, 400K)
27238         dd if=/dev/urandom of=$file bs=256K count=1 seek=1 ||
27239                 error "256K->512K dd fails"
27240         # data at [2M, 3M)
27241         dd if=/dev/urandom of=$file bs=1M count=1 seek=2 ||
27242                 error "2M->3M dd fails"
27243         # data at [4M, 5M)
27244         dd if=/dev/urandom of=$file bs=1M count=1 seek=4 ||
27245                 error "4M->5M dd fails"
27246         echo "Data at 256K...512K, 2M...3M and 4M...5M"
27247         # start at first component hole #1
27248         printf "Seeking hole from 1000 ... "
27249         offset=$(lseek_test -l 1000 $file)
27250         echo $offset
27251         [[ $offset == 1000 ]] || error "offset $offset != 1000"
27252         printf "Seeking data from 1000 ... "
27253         offset=$(lseek_test -d 1000 $file)
27254         echo $offset
27255         [[ $offset == 262144 ]] || error "offset $offset != 262144"
27256
27257         # start at first component data block
27258         printf "Seeking hole from 300000 ... "
27259         offset=$(lseek_test -l 300000 $file)
27260         echo $offset
27261         [[ $offset == 524288 ]] || error "offset $offset != 524288"
27262         printf "Seeking data from 300000 ... "
27263         offset=$(lseek_test -d 300000 $file)
27264         echo $offset
27265         [[ $offset == 300000 ]] || error "offset $offset != 300000"
27266
27267         # start at the first component but beyond end of object size
27268         printf "Seeking hole from 1000000 ... "
27269         offset=$(lseek_test -l 1000000 $file)
27270         echo $offset
27271         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
27272         printf "Seeking data from 1000000 ... "
27273         offset=$(lseek_test -d 1000000 $file)
27274         echo $offset
27275         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
27276
27277         # start at second component stripe 2 (empty file)
27278         printf "Seeking hole from 1500000 ... "
27279         offset=$(lseek_test -l 1500000 $file)
27280         echo $offset
27281         [[ $offset == 1500000 ]] || error "offset $offset != 1500000"
27282         printf "Seeking data from 1500000 ... "
27283         offset=$(lseek_test -d 1500000 $file)
27284         echo $offset
27285         [[ $offset == 2097152 ]] || error "offset $offset != 2097152"
27286
27287         # start at second component stripe 1 (all data)
27288         printf "Seeking hole from 3000000 ... "
27289         offset=$(lseek_test -l 3000000 $file)
27290         echo $offset
27291         [[ $offset == 3145728 ]] || error "offset $offset != 3145728"
27292         printf "Seeking data from 3000000 ... "
27293         offset=$(lseek_test -d 3000000 $file)
27294         echo $offset
27295         [[ $offset == 3000000 ]] || error "offset $offset != 3000000"
27296
27297         dd if=/dev/urandom of=$file bs=640K count=1 seek=1 ||
27298                 error "2nd dd fails"
27299         echo "Add data block at 640K...1280K"
27300
27301         # start at before new data block, in hole
27302         printf "Seeking hole from 600000 ... "
27303         offset=$(lseek_test -l 600000 $file)
27304         echo $offset
27305         [[ $offset == 600000 ]] || error "offset $offset != 600000"
27306         printf "Seeking data from 600000 ... "
27307         offset=$(lseek_test -d 600000 $file)
27308         echo $offset
27309         [[ $offset == 655360 ]] || error "offset $offset != 655360"
27310
27311         # start at the first component new data block
27312         printf "Seeking hole from 1000000 ... "
27313         offset=$(lseek_test -l 1000000 $file)
27314         echo $offset
27315         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
27316         printf "Seeking data from 1000000 ... "
27317         offset=$(lseek_test -d 1000000 $file)
27318         echo $offset
27319         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
27320
27321         # start at second component stripe 2, new data
27322         printf "Seeking hole from 1200000 ... "
27323         offset=$(lseek_test -l 1200000 $file)
27324         echo $offset
27325         [[ $offset == 1310720 ]] || error "offset $offset != 1310720"
27326         printf "Seeking data from 1200000 ... "
27327         offset=$(lseek_test -d 1200000 $file)
27328         echo $offset
27329         [[ $offset == 1200000 ]] || error "offset $offset != 1200000"
27330
27331         # start beyond file end
27332         printf "Using offset > filesize ... "
27333         lseek_test -l 4000000 $file && error "lseek should fail"
27334         printf "Using offset > filesize ... "
27335         lseek_test -d 4000000 $file && error "lseek should fail"
27336
27337         printf "Done\n\n"
27338 }
27339
27340 test_430a() {
27341         $LCTL get_param mdc.*.import | grep -q 'connect_flags:.*seek' ||
27342                 skip "MDT does not support SEEK_HOLE"
27343
27344         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
27345                 skip "OST does not support SEEK_HOLE"
27346
27347         local file=$DIR/$tdir/$tfile
27348
27349         mkdir -p $DIR/$tdir
27350
27351         $LFS setstripe -E 1M -L mdt -E eof -c2 $file
27352         # OST stripe #1 will have continuous data at [1M, 3M)
27353         # OST stripe #2 is empty
27354         echo "Component #1: 1M DoM, component #2: EOF, 2 stripes 1M"
27355         lseek_test_430 $file
27356         rm $file
27357         $LFS setstripe -E 1M -c2 -S 64K -E 10M -c2 -S 1M $file
27358         echo "Component #1: 1M, 2 stripes 64K, component #2: EOF, 2 stripes 1M"
27359         lseek_test_430 $file
27360         rm $file
27361         $LFS setstripe -c2 -S 512K $file
27362         echo "Two stripes, stripe size 512K"
27363         lseek_test_430 $file
27364         rm $file
27365         # FLR with stale mirror
27366         $LFS setstripe -N -E 512K -c1 -S 64K -E eof -c2 -S 512K \
27367                        -N -c2 -S 1M $file
27368         echo "Mirrored file:"
27369         echo "Component #1: 512K, stripe 64K, component #2: EOF, 2 stripes 512K"
27370         echo "Plain 2 stripes 1M"
27371         lseek_test_430 $file
27372         rm $file
27373 }
27374 run_test 430a "lseek: SEEK_DATA/SEEK_HOLE basic functionality"
27375
27376 test_430b() {
27377         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
27378                 skip "OST does not support SEEK_HOLE"
27379
27380         local offset
27381         local file=$DIR/$tdir/$tfile
27382
27383         mkdir -p $DIR/$tdir
27384         # Empty layout lseek should fail
27385         $MCREATE $file
27386         # seek from 0
27387         printf "Seeking hole from 0 ... "
27388         lseek_test -l 0 $file && error "lseek should fail"
27389         printf "Seeking data from 0 ... "
27390         lseek_test -d 0 $file && error "lseek should fail"
27391         rm $file
27392
27393         # 1M-hole file
27394         $LFS setstripe -E 1M -c2 -E eof $file
27395         $TRUNCATE $file 1048576
27396         printf "Seeking hole from 1000000 ... "
27397         offset=$(lseek_test -l 1000000 $file)
27398         echo $offset
27399         [[ $offset == 1000000 ]] || error "offset $offset != 1000000"
27400         printf "Seeking data from 1000000 ... "
27401         lseek_test -d 1000000 $file && error "lseek should fail"
27402         rm $file
27403
27404         # full component followed by non-inited one
27405         $LFS setstripe -E 1M -c2 -E eof $file
27406         dd if=/dev/urandom of=$file bs=1M count=1
27407         printf "Seeking hole from 1000000 ... "
27408         offset=$(lseek_test -l 1000000 $file)
27409         echo $offset
27410         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
27411         printf "Seeking hole from 1048576 ... "
27412         lseek_test -l 1048576 $file && error "lseek should fail"
27413         # init second component and truncate back
27414         echo "123" >> $file
27415         $TRUNCATE $file 1048576
27416         printf "Seeking hole from 1000000 ... "
27417         offset=$(lseek_test -l 1000000 $file)
27418         echo $offset
27419         [[ $offset == 1048576 ]] || error "offset $offset != 1048576"
27420         printf "Seeking hole from 1048576 ... "
27421         lseek_test -l 1048576 $file && error "lseek should fail"
27422         # boundary checks for big values
27423         dd if=/dev/urandom of=$file.10g bs=1 count=1 seek=10G
27424         offset=$(lseek_test -d 0 $file.10g)
27425         [[ $offset == 10737418240 ]] || error "offset $offset != 10737418240"
27426         dd if=/dev/urandom of=$file.100g bs=1 count=1 seek=100G
27427         offset=$(lseek_test -d 0 $file.100g)
27428         [[ $offset == 107374182400 ]] || error "offset $offset != 107374182400"
27429         return 0
27430 }
27431 run_test 430b "lseek: SEEK_DATA/SEEK_HOLE special cases"
27432
27433 test_430c() {
27434         $LCTL get_param osc.*.import | grep -q 'connect_flags:.*seek' ||
27435                 skip "OST does not support SEEK_HOLE"
27436
27437         local file=$DIR/$tdir/$tfile
27438         local start
27439
27440         mkdir -p $DIR/$tdir
27441         dd if=/dev/urandom of=$file bs=1k count=1 seek=5M
27442
27443         # cp version 8.33+ prefers lseek over fiemap
27444         if [[ $(cp --version | head -n1 | sed "s/[^0-9]//g") -ge 833 ]]; then
27445                 start=$SECONDS
27446                 time cp $file /dev/null
27447                 (( SECONDS - start < 5 )) ||
27448                         error "cp: too long runtime $((SECONDS - start))"
27449
27450         fi
27451         # tar version 1.29+ supports SEEK_HOLE/DATA
27452         if [[ $(tar --version | head -n1 | sed "s/[^0-9]//g") -ge 129 ]]; then
27453                 start=$SECONDS
27454                 time tar cS $file - | cat > /dev/null
27455                 (( SECONDS - start < 5 )) ||
27456                         error "tar: too long runtime $((SECONDS - start))"
27457         fi
27458 }
27459 run_test 430c "lseek: external tools check"
27460
27461 test_431() { # LU-14187
27462         local file=$DIR/$tdir/$tfile
27463
27464         mkdir -p $DIR/$tdir
27465         $LFS setstripe -c 1 -i 0 $file || error "lfs setstripe failed"
27466         dd if=/dev/urandom of=$file bs=4k count=1
27467         dd if=/dev/urandom of=$file bs=4k count=1 seek=10 conv=notrunc
27468         dd if=/dev/urandom of=$file bs=4k count=1 seek=12 conv=notrunc
27469         #define OBD_FAIL_OST_RESTART_IO 0x251
27470         do_facet ost1 "$LCTL set_param fail_loc=0x251"
27471         $LFS setstripe -c 1 -i 0 $file.0 || error "lfs setstripe failed"
27472         cp $file $file.0
27473         cancel_lru_locks
27474         sync_all_data
27475         echo 3 > /proc/sys/vm/drop_caches
27476         diff  $file $file.0 || error "data diff"
27477 }
27478 run_test 431 "Restart transaction for IO"
27479
27480 cleanup_test_432() {
27481         do_facet mgs $LCTL nodemap_activate 0
27482         wait_nm_sync active
27483 }
27484
27485 test_432() {
27486         local tmpdir=$TMP/dir432
27487
27488         (( $MDS1_VERSION >= $(version_code 2.14.52) )) ||
27489                 skip "Need MDS version at least 2.14.52"
27490
27491         stack_trap cleanup_test_432 EXIT
27492         mkdir $DIR/$tdir
27493         mkdir $tmpdir
27494
27495         do_facet mgs $LCTL nodemap_activate 1
27496         wait_nm_sync active
27497         do_facet mgs $LCTL nodemap_modify --name default \
27498                 --property admin --value 1
27499         do_facet mgs $LCTL nodemap_modify --name default \
27500                 --property trusted --value 1
27501         cancel_lru_locks mdc
27502         wait_nm_sync default admin_nodemap
27503         wait_nm_sync default trusted_nodemap
27504
27505         if [ $(mv $tmpdir $DIR/$tdir/ 2>&1 |
27506                grep -ci "Operation not permitted") -ne 0 ]; then
27507                 error "mv $tmpdir $DIR/$tdir/ hits 'Operation not permitted'"
27508         fi
27509 }
27510 run_test 432 "mv dir from outside Lustre"
27511
27512 test_433() {
27513         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27514
27515         [[ -n "$($LCTL list_param llite.*.inode_cache 2>/dev/null)" ]] ||
27516                 skip "inode cache not supported"
27517
27518         $LCTL set_param llite.*.inode_cache=0
27519         stack_trap "$LCTL set_param llite.*.inode_cache=1"
27520
27521         local count=256
27522         local before
27523         local after
27524
27525         cancel_lru_locks mdc
27526         test_mkdir $DIR/$tdir || error "mkdir $tdir"
27527         createmany -m $DIR/$tdir/f $count
27528         createmany -d $DIR/$tdir/d $count
27529         ls -l $DIR/$tdir > /dev/null
27530         stack_trap "rm -rf $DIR/$tdir"
27531
27532         before=$(num_objects)
27533         cancel_lru_locks mdc
27534         after=$(num_objects)
27535
27536         # sometimes even @before is less than 2 * count
27537         while (( before - after < count )); do
27538                 sleep 1
27539                 after=$(num_objects)
27540                 wait=$((wait + 1))
27541                 (( wait % 5 == 0 )) && echo "wait $wait seconds objects: $after"
27542                 if (( wait > 60 )); then
27543                         error "inode slab grew from $before to $after"
27544                 fi
27545         done
27546
27547         echo "lustre_inode_cache $before objs before lock cancel, $after after"
27548 }
27549 run_test 433 "ldlm lock cancel releases dentries and inodes"
27550
27551 prep_801() {
27552         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
27553         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
27554                 skip "Need server version at least 2.9.55"
27555
27556         start_full_debug_logging
27557 }
27558
27559 post_801() {
27560         stop_full_debug_logging
27561 }
27562
27563 barrier_stat() {
27564         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
27565                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
27566                            awk '/The barrier for/ { print $7 }')
27567                 echo $st
27568         else
27569                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
27570                 echo \'$st\'
27571         fi
27572 }
27573
27574 barrier_expired() {
27575         local expired
27576
27577         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
27578                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
27579                           awk '/will be expired/ { print $7 }')
27580         else
27581                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
27582         fi
27583
27584         echo $expired
27585 }
27586
27587 test_801a() {
27588         prep_801
27589
27590         echo "Start barrier_freeze at: $(date)"
27591         #define OBD_FAIL_BARRIER_DELAY          0x2202
27592         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
27593         # Do not reduce barrier time - See LU-11873
27594         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
27595
27596         sleep 2
27597         local b_status=$(barrier_stat)
27598         echo "Got barrier status at: $(date)"
27599         [ "$b_status" = "'freezing_p1'" ] ||
27600                 error "(1) unexpected barrier status $b_status"
27601
27602         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
27603         wait
27604         b_status=$(barrier_stat)
27605         [ "$b_status" = "'frozen'" ] ||
27606                 error "(2) unexpected barrier status $b_status"
27607
27608         local expired=$(barrier_expired)
27609         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
27610         sleep $((expired + 3))
27611
27612         b_status=$(barrier_stat)
27613         [ "$b_status" = "'expired'" ] ||
27614                 error "(3) unexpected barrier status $b_status"
27615
27616         # Do not reduce barrier time - See LU-11873
27617         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
27618                 error "(4) fail to freeze barrier"
27619
27620         b_status=$(barrier_stat)
27621         [ "$b_status" = "'frozen'" ] ||
27622                 error "(5) unexpected barrier status $b_status"
27623
27624         echo "Start barrier_thaw at: $(date)"
27625         #define OBD_FAIL_BARRIER_DELAY          0x2202
27626         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
27627         do_facet mgs $LCTL barrier_thaw $FSNAME &
27628
27629         sleep 2
27630         b_status=$(barrier_stat)
27631         echo "Got barrier status at: $(date)"
27632         [ "$b_status" = "'thawing'" ] ||
27633                 error "(6) unexpected barrier status $b_status"
27634
27635         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
27636         wait
27637         b_status=$(barrier_stat)
27638         [ "$b_status" = "'thawed'" ] ||
27639                 error "(7) unexpected barrier status $b_status"
27640
27641         #define OBD_FAIL_BARRIER_FAILURE        0x2203
27642         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
27643         do_facet mgs $LCTL barrier_freeze $FSNAME
27644
27645         b_status=$(barrier_stat)
27646         [ "$b_status" = "'failed'" ] ||
27647                 error "(8) unexpected barrier status $b_status"
27648
27649         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
27650         do_facet mgs $LCTL barrier_thaw $FSNAME
27651
27652         post_801
27653 }
27654 run_test 801a "write barrier user interfaces and stat machine"
27655
27656 test_801b() {
27657         prep_801
27658
27659         mkdir $DIR/$tdir || error "(1) fail to mkdir"
27660         createmany -d $DIR/$tdir/d 6 || error "(2) fail to mkdir"
27661         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
27662         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
27663         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
27664
27665         cancel_lru_locks mdc
27666
27667         # 180 seconds should be long enough
27668         do_facet mgs $LCTL barrier_freeze $FSNAME 180
27669
27670         local b_status=$(barrier_stat)
27671         [ "$b_status" = "'frozen'" ] ||
27672                 error "(6) unexpected barrier status $b_status"
27673
27674         mkdir $DIR/$tdir/d0/d10 &
27675         mkdir_pid=$!
27676
27677         touch $DIR/$tdir/d1/f13 &
27678         touch_pid=$!
27679
27680         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
27681         ln_pid=$!
27682
27683         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
27684         mv_pid=$!
27685
27686         rm -f $DIR/$tdir/d4/f12 &
27687         rm_pid=$!
27688
27689         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
27690
27691         # To guarantee taht the 'stat' is not blocked
27692         b_status=$(barrier_stat)
27693         [ "$b_status" = "'frozen'" ] ||
27694                 error "(8) unexpected barrier status $b_status"
27695
27696         # let above commands to run at background
27697         sleep 5
27698
27699         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
27700         ps -p $touch_pid || error "(10) touch should be blocked"
27701         ps -p $ln_pid || error "(11) link should be blocked"
27702         ps -p $mv_pid || error "(12) rename should be blocked"
27703         ps -p $rm_pid || error "(13) unlink should be blocked"
27704
27705         b_status=$(barrier_stat)
27706         [ "$b_status" = "'frozen'" ] ||
27707                 error "(14) unexpected barrier status $b_status"
27708
27709         do_facet mgs $LCTL barrier_thaw $FSNAME
27710         b_status=$(barrier_stat)
27711         [ "$b_status" = "'thawed'" ] ||
27712                 error "(15) unexpected barrier status $b_status"
27713
27714         wait $mkdir_pid || error "(16) mkdir should succeed"
27715         wait $touch_pid || error "(17) touch should succeed"
27716         wait $ln_pid || error "(18) link should succeed"
27717         wait $mv_pid || error "(19) rename should succeed"
27718         wait $rm_pid || error "(20) unlink should succeed"
27719
27720         post_801
27721 }
27722 run_test 801b "modification will be blocked by write barrier"
27723
27724 test_801c() {
27725         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27726
27727         prep_801
27728
27729         stop mds2 || error "(1) Fail to stop mds2"
27730
27731         do_facet mgs $LCTL barrier_freeze $FSNAME 30
27732
27733         local b_status=$(barrier_stat)
27734         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
27735                 do_facet mgs $LCTL barrier_thaw $FSNAME
27736                 error "(2) unexpected barrier status $b_status"
27737         }
27738
27739         do_facet mgs $LCTL barrier_rescan $FSNAME ||
27740                 error "(3) Fail to rescan barrier bitmap"
27741
27742         # Do not reduce barrier time - See LU-11873
27743         do_facet mgs $LCTL barrier_freeze $FSNAME 20
27744
27745         b_status=$(barrier_stat)
27746         [ "$b_status" = "'frozen'" ] ||
27747                 error "(4) unexpected barrier status $b_status"
27748
27749         do_facet mgs $LCTL barrier_thaw $FSNAME
27750         b_status=$(barrier_stat)
27751         [ "$b_status" = "'thawed'" ] ||
27752                 error "(5) unexpected barrier status $b_status"
27753
27754         local devname=$(mdsdevname 2)
27755
27756         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
27757
27758         do_facet mgs $LCTL barrier_rescan $FSNAME ||
27759                 error "(7) Fail to rescan barrier bitmap"
27760
27761         post_801
27762 }
27763 run_test 801c "rescan barrier bitmap"
27764
27765 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
27766 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
27767 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
27768 saved_MOUNT_OPTS=$MOUNT_OPTS
27769
27770 cleanup_802a() {
27771         trap 0
27772
27773         stopall
27774         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
27775         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
27776         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
27777         MOUNT_OPTS=$saved_MOUNT_OPTS
27778         setupall
27779 }
27780
27781 test_802a() {
27782         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
27783         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
27784         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
27785                 skip "Need server version at least 2.9.55"
27786
27787         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
27788
27789         mkdir $DIR/$tdir || error "(1) fail to mkdir"
27790
27791         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
27792                 error "(2) Fail to copy"
27793
27794         trap cleanup_802a EXIT
27795
27796         # sync by force before remount as readonly
27797         sync; sync_all_data; sleep 3; sync_all_data
27798
27799         stopall
27800
27801         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
27802         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
27803         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
27804
27805         echo "Mount the server as read only"
27806         setupall server_only || error "(3) Fail to start servers"
27807
27808         echo "Mount client without ro should fail"
27809         mount_client $MOUNT &&
27810                 error "(4) Mount client without 'ro' should fail"
27811
27812         echo "Mount client with ro should succeed"
27813         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
27814         mount_client $MOUNT ||
27815                 error "(5) Mount client with 'ro' should succeed"
27816
27817         echo "Modify should be refused"
27818         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
27819
27820         echo "Read should be allowed"
27821         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
27822                 error "(7) Read should succeed under ro mode"
27823
27824         cleanup_802a
27825 }
27826 run_test 802a "simulate readonly device"
27827
27828 test_802b() {
27829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27830         remote_mds_nodsh && skip "remote MDS with nodsh"
27831
27832         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
27833                 skip "readonly option not available"
27834
27835         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
27836
27837         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
27838                 error "(2) Fail to copy"
27839
27840         # write back all cached data before setting MDT to readonly
27841         cancel_lru_locks
27842         sync_all_data
27843
27844         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
27845         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
27846
27847         echo "Modify should be refused"
27848         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
27849
27850         echo "Read should be allowed"
27851         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
27852                 error "(7) Read should succeed under ro mode"
27853
27854         # disable readonly
27855         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
27856 }
27857 run_test 802b "be able to set MDTs to readonly"
27858
27859 test_803a() {
27860         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27861         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
27862                 skip "MDS needs to be newer than 2.10.54"
27863
27864         mkdir_on_mdt0 $DIR/$tdir
27865         # Create some objects on all MDTs to trigger related logs objects
27866         for idx in $(seq $MDSCOUNT); do
27867                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
27868                         $DIR/$tdir/dir${idx} ||
27869                         error "Fail to create $DIR/$tdir/dir${idx}"
27870         done
27871
27872         wait_delete_completed # ensure old test cleanups are finished
27873         sleep 3
27874         echo "before create:"
27875         $LFS df -i $MOUNT
27876         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
27877
27878         for i in {1..10}; do
27879                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
27880                         error "Fail to create $DIR/$tdir/foo$i"
27881         done
27882
27883         # sync ZFS-on-MDS to refresh statfs data
27884         wait_zfs_commit mds1
27885         sleep 3
27886         echo "after create:"
27887         $LFS df -i $MOUNT
27888         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
27889
27890         # allow for an llog to be cleaned up during the test
27891         [ $after_used -ge $((before_used + 10 - 1)) ] ||
27892                 error "before ($before_used) + 10 > after ($after_used)"
27893
27894         for i in {1..10}; do
27895                 rm -rf $DIR/$tdir/foo$i ||
27896                         error "Fail to remove $DIR/$tdir/foo$i"
27897         done
27898
27899         # sync ZFS-on-MDS to refresh statfs data
27900         wait_zfs_commit mds1
27901         wait_delete_completed
27902         sleep 3 # avoid MDT return cached statfs
27903         echo "after unlink:"
27904         $LFS df -i $MOUNT
27905         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
27906
27907         # allow for an llog to be created during the test
27908         [ $after_used -le $((before_used + 1)) ] ||
27909                 error "after ($after_used) > before ($before_used) + 1"
27910 }
27911 run_test 803a "verify agent object for remote object"
27912
27913 test_803b() {
27914         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27915         [ $MDS1_VERSION -lt $(version_code 2.13.56) ] &&
27916                 skip "MDS needs to be newer than 2.13.56"
27917         [ $PARALLEL == "yes" ] && skip "skip parallel run"
27918
27919         for i in $(seq 0 $((MDSCOUNT - 1))); do
27920                 $LFS mkdir -i $i $DIR/$tdir.$i || error "mkdir $tdir.$i"
27921         done
27922
27923         local before=0
27924         local after=0
27925
27926         local tmp
27927
27928         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
27929         for i in $(seq 0 $((MDSCOUNT - 1))); do
27930                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
27931                         awk '/getattr/ { print $2 }')
27932                 before=$((before + tmp))
27933         done
27934         stat $DIR/$tdir.* >/dev/null || error "stat $tdir.*"
27935         for i in $(seq 0 $((MDSCOUNT - 1))); do
27936                 tmp=$(do_facet mds$i $LCTL get_param mdt.*-MDT000$i.md_stats |
27937                         awk '/getattr/ { print $2 }')
27938                 after=$((after + tmp))
27939         done
27940
27941         [ $before -eq $after ] || error "getattr count $before != $after"
27942 }
27943 run_test 803b "remote object can getattr from cache"
27944
27945 test_804() {
27946         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
27947         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
27948                 skip "MDS needs to be newer than 2.10.54"
27949         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
27950
27951         mkdir -p $DIR/$tdir
27952         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
27953                 error "Fail to create $DIR/$tdir/dir0"
27954
27955         local fid=$($LFS path2fid $DIR/$tdir/dir0)
27956         local dev=$(mdsdevname 2)
27957
27958         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27959                 grep ${fid} || error "NOT found agent entry for dir0"
27960
27961         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
27962                 error "Fail to create $DIR/$tdir/dir1"
27963
27964         touch $DIR/$tdir/dir1/foo0 ||
27965                 error "Fail to create $DIR/$tdir/dir1/foo0"
27966         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
27967         local rc=0
27968
27969         for idx in $(seq $MDSCOUNT); do
27970                 dev=$(mdsdevname $idx)
27971                 do_facet mds${idx} \
27972                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27973                         grep ${fid} && rc=$idx
27974         done
27975
27976         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
27977                 error "Fail to rename foo0 to foo1"
27978         if [ $rc -eq 0 ]; then
27979                 for idx in $(seq $MDSCOUNT); do
27980                         dev=$(mdsdevname $idx)
27981                         do_facet mds${idx} \
27982                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27983                         grep ${fid} && rc=$idx
27984                 done
27985         fi
27986
27987         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
27988                 error "Fail to rename foo1 to foo2"
27989         if [ $rc -eq 0 ]; then
27990                 for idx in $(seq $MDSCOUNT); do
27991                         dev=$(mdsdevname $idx)
27992                         do_facet mds${idx} \
27993                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
27994                         grep ${fid} && rc=$idx
27995                 done
27996         fi
27997
27998         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
27999
28000         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
28001                 error "Fail to link to $DIR/$tdir/dir1/foo2"
28002         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
28003                 error "Fail to rename foo2 to foo0"
28004         unlink $DIR/$tdir/dir1/foo0 ||
28005                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
28006         rm -rf $DIR/$tdir/dir0 ||
28007                 error "Fail to rm $DIR/$tdir/dir0"
28008
28009         for idx in $(seq $MDSCOUNT); do
28010                 rc=0
28011
28012                 stop mds${idx}
28013                 dev=$(mdsdevname $idx)
28014                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
28015                         rc=$?
28016                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
28017                         error "mount mds$idx failed"
28018                 df $MOUNT > /dev/null 2>&1
28019
28020                 # e2fsck should not return error
28021                 [ $rc -eq 0 ] ||
28022                         error "e2fsck detected error on MDT${idx}: rc=$rc"
28023         done
28024 }
28025 run_test 804 "verify agent entry for remote entry"
28026
28027 cleanup_805() {
28028         do_facet $SINGLEMDS zfs set quota=$old $fsset
28029         unlinkmany $DIR/$tdir/f- 1000000
28030         trap 0
28031 }
28032
28033 test_805() {
28034         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
28035         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
28036         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
28037                 skip "netfree not implemented before 0.7"
28038         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
28039                 skip "Need MDS version at least 2.10.57"
28040
28041         local fsset
28042         local freekb
28043         local usedkb
28044         local old
28045         local quota
28046         local pref="osd-zfs.$FSNAME-MDT0000."
28047
28048         # limit available space on MDS dataset to meet nospace issue
28049         # quickly. then ZFS 0.7.2 can use reserved space if asked
28050         # properly (using netfree flag in osd_declare_destroy()
28051         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
28052         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
28053                 gawk '{print $3}')
28054         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
28055         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
28056         let "usedkb=usedkb-freekb"
28057         let "freekb=freekb/2"
28058         if let "freekb > 5000"; then
28059                 let "freekb=5000"
28060         fi
28061         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
28062         trap cleanup_805 EXIT
28063         mkdir_on_mdt0 $DIR/$tdir
28064         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
28065                 error "Can't set PFL layout"
28066         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
28067         rm -rf $DIR/$tdir || error "not able to remove"
28068         do_facet $SINGLEMDS zfs set quota=$old $fsset
28069         trap 0
28070 }
28071 run_test 805 "ZFS can remove from full fs"
28072
28073 # Size-on-MDS test
28074 check_lsom_data()
28075 {
28076         local file=$1
28077         local expect=$(stat -c %s $file)
28078
28079         check_lsom_size $1 $expect
28080
28081         local blocks=$($LFS getsom -b $file)
28082         expect=$(stat -c %b $file)
28083         [[ $blocks == $expect ]] ||
28084                 error "$file expected blocks: $expect, got: $blocks"
28085 }
28086
28087 check_lsom_size()
28088 {
28089         local size
28090         local expect=$2
28091
28092         cancel_lru_locks mdc
28093
28094         size=$($LFS getsom -s $1)
28095         [[ $size == $expect ]] ||
28096                 error "$file expected size: $expect, got: $size"
28097 }
28098
28099 test_806() {
28100         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
28101                 skip "Need MDS version at least 2.11.52"
28102
28103         local bs=1048576
28104
28105         touch $DIR/$tfile || error "touch $tfile failed"
28106
28107         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
28108         save_lustre_params client "llite.*.xattr_cache" > $save
28109         lctl set_param llite.*.xattr_cache=0
28110         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
28111
28112         # single-threaded write
28113         echo "Test SOM for single-threaded write"
28114         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
28115                 error "write $tfile failed"
28116         check_lsom_size $DIR/$tfile $bs
28117
28118         local num=32
28119         local size=$(($num * $bs))
28120         local offset=0
28121         local i
28122
28123         echo "Test SOM for single client multi-threaded($num) write"
28124         $TRUNCATE $DIR/$tfile 0
28125         for ((i = 0; i < $num; i++)); do
28126                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28127                 local pids[$i]=$!
28128                 offset=$((offset + $bs))
28129         done
28130         for (( i=0; i < $num; i++ )); do
28131                 wait ${pids[$i]}
28132         done
28133         check_lsom_size $DIR/$tfile $size
28134
28135         $TRUNCATE $DIR/$tfile 0
28136         for ((i = 0; i < $num; i++)); do
28137                 offset=$((offset - $bs))
28138                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28139                 local pids[$i]=$!
28140         done
28141         for (( i=0; i < $num; i++ )); do
28142                 wait ${pids[$i]}
28143         done
28144         check_lsom_size $DIR/$tfile $size
28145
28146         # multi-client writes
28147         num=$(get_node_count ${CLIENTS//,/ })
28148         size=$(($num * $bs))
28149         offset=0
28150         i=0
28151
28152         echo "Test SOM for multi-client ($num) writes"
28153         $TRUNCATE $DIR/$tfile 0
28154         for client in ${CLIENTS//,/ }; do
28155                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28156                 local pids[$i]=$!
28157                 i=$((i + 1))
28158                 offset=$((offset + $bs))
28159         done
28160         for (( i=0; i < $num; i++ )); do
28161                 wait ${pids[$i]}
28162         done
28163         check_lsom_size $DIR/$tfile $offset
28164
28165         i=0
28166         $TRUNCATE $DIR/$tfile 0
28167         for client in ${CLIENTS//,/ }; do
28168                 offset=$((offset - $bs))
28169                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28170                 local pids[$i]=$!
28171                 i=$((i + 1))
28172         done
28173         for (( i=0; i < $num; i++ )); do
28174                 wait ${pids[$i]}
28175         done
28176         check_lsom_size $DIR/$tfile $size
28177
28178         # verify truncate
28179         echo "Test SOM for truncate"
28180         $TRUNCATE $DIR/$tfile 1048576
28181         check_lsom_size $DIR/$tfile 1048576
28182         $TRUNCATE $DIR/$tfile 1234
28183         check_lsom_size $DIR/$tfile 1234
28184
28185         # verify SOM blocks count
28186         echo "Verify SOM block count"
28187         $TRUNCATE $DIR/$tfile 0
28188         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
28189                 error "failed to write file $tfile"
28190         check_lsom_data $DIR/$tfile
28191 }
28192 run_test 806 "Verify Lazy Size on MDS"
28193
28194 test_807() {
28195         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
28196         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
28197                 skip "Need MDS version at least 2.11.52"
28198
28199         # Registration step
28200         changelog_register || error "changelog_register failed"
28201         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
28202         changelog_users $SINGLEMDS | grep -q $cl_user ||
28203                 error "User $cl_user not found in changelog_users"
28204
28205         rm -rf $DIR/$tdir || error "rm $tdir failed"
28206         mkdir_on_mdt0 $DIR/$tdir || error "mkdir $tdir failed"
28207         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
28208         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
28209         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
28210                 error "truncate $tdir/trunc failed"
28211
28212         local bs=1048576
28213         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
28214                 error "write $tfile failed"
28215
28216         # multi-client wirtes
28217         local num=$(get_node_count ${CLIENTS//,/ })
28218         local offset=0
28219         local i=0
28220
28221         echo "Test SOM for multi-client ($num) writes"
28222         touch $DIR/$tfile || error "touch $tfile failed"
28223         $TRUNCATE $DIR/$tfile 0
28224         for client in ${CLIENTS//,/ }; do
28225                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
28226                 local pids[$i]=$!
28227                 i=$((i + 1))
28228                 offset=$((offset + $bs))
28229         done
28230         for (( i=0; i < $num; i++ )); do
28231                 wait ${pids[$i]}
28232         done
28233
28234         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
28235         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
28236         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
28237         check_lsom_data $DIR/$tdir/trunc
28238         check_lsom_data $DIR/$tdir/single_dd
28239         check_lsom_data $DIR/$tfile
28240
28241         rm -rf $DIR/$tdir
28242         # Deregistration step
28243         changelog_deregister || error "changelog_deregister failed"
28244 }
28245 run_test 807 "verify LSOM syncing tool"
28246
28247 check_som_nologged()
28248 {
28249         local lines=$($LFS changelog $FSNAME-MDT0000 |
28250                 grep 'x=trusted.som' | wc -l)
28251         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
28252 }
28253
28254 test_808() {
28255         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
28256                 skip "Need MDS version at least 2.11.55"
28257
28258         # Registration step
28259         changelog_register || error "changelog_register failed"
28260
28261         touch $DIR/$tfile || error "touch $tfile failed"
28262         check_som_nologged
28263
28264         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
28265                 error "write $tfile failed"
28266         check_som_nologged
28267
28268         $TRUNCATE $DIR/$tfile 1234
28269         check_som_nologged
28270
28271         $TRUNCATE $DIR/$tfile 1048576
28272         check_som_nologged
28273
28274         # Deregistration step
28275         changelog_deregister || error "changelog_deregister failed"
28276 }
28277 run_test 808 "Check trusted.som xattr not logged in Changelogs"
28278
28279 check_som_nodata()
28280 {
28281         $LFS getsom $1
28282         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
28283 }
28284
28285 test_809() {
28286         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
28287                 skip "Need MDS version at least 2.11.56"
28288
28289         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
28290                 error "failed to create DoM-only file $DIR/$tfile"
28291         touch $DIR/$tfile || error "touch $tfile failed"
28292         check_som_nodata $DIR/$tfile
28293
28294         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
28295                 error "write $tfile failed"
28296         check_som_nodata $DIR/$tfile
28297
28298         $TRUNCATE $DIR/$tfile 1234
28299         check_som_nodata $DIR/$tfile
28300
28301         $TRUNCATE $DIR/$tfile 4097
28302         check_som_nodata $DIR/$file
28303 }
28304 run_test 809 "Verify no SOM xattr store for DoM-only files"
28305
28306 test_810() {
28307         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28308         $GSS && skip_env "could not run with gss"
28309         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
28310                 skip "OST < 2.12.58 doesn't align checksum"
28311
28312         set_checksums 1
28313         stack_trap "set_checksums $ORIG_CSUM" EXIT
28314         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
28315
28316         local csum
28317         local before
28318         local after
28319         for csum in $CKSUM_TYPES; do
28320                 #define OBD_FAIL_OSC_NO_GRANT   0x411
28321                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
28322                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
28323                         eval set -- $i
28324                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
28325                         before=$(md5sum $DIR/$tfile)
28326                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
28327                         after=$(md5sum $DIR/$tfile)
28328                         [ "$before" == "$after" ] ||
28329                                 error "$csum: $before != $after bs=$1 seek=$2"
28330                 done
28331         done
28332 }
28333 run_test 810 "partial page writes on ZFS (LU-11663)"
28334
28335 test_812a() {
28336         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
28337                 skip "OST < 2.12.51 doesn't support this fail_loc"
28338
28339         $LFS setstripe -c 1 -i 0 $DIR/$tfile
28340         # ensure ost1 is connected
28341         stat $DIR/$tfile >/dev/null || error "can't stat"
28342         wait_osc_import_state client ost1 FULL
28343         # no locks, no reqs to let the connection idle
28344         cancel_lru_locks osc
28345
28346         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
28347 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
28348         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
28349         wait_osc_import_state client ost1 CONNECTING
28350         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
28351
28352         stat $DIR/$tfile >/dev/null || error "can't stat file"
28353 }
28354 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
28355
28356 test_812b() { # LU-12378
28357         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
28358                 skip "OST < 2.12.51 doesn't support this fail_loc"
28359
28360         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
28361         # ensure ost1 is connected
28362         stat $DIR/$tfile >/dev/null || error "can't stat"
28363         wait_osc_import_state client ost1 FULL
28364         # no locks, no reqs to let the connection idle
28365         cancel_lru_locks osc
28366
28367         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
28368 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
28369         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
28370         wait_osc_import_state client ost1 CONNECTING
28371         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
28372
28373         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
28374         wait_osc_import_state client ost1 IDLE
28375 }
28376 run_test 812b "do not drop no resend request for idle connect"
28377
28378 test_812c() {
28379         local old
28380
28381         old=$($LCTL get_param -n osc.*.idle_timeout | head -n 1)
28382
28383         $LFS setstripe -c 1 -o 0 $DIR/$tfile
28384         $LFS getstripe $DIR/$tfile
28385         $LCTL set_param osc.*.idle_timeout=10
28386         stack_trap "$LCTL set_param osc.*.idle_timeout=$old" EXIT
28387         # ensure ost1 is connected
28388         stat $DIR/$tfile >/dev/null || error "can't stat"
28389         wait_osc_import_state client ost1 FULL
28390         # no locks, no reqs to let the connection idle
28391         cancel_lru_locks osc
28392
28393 #define OBD_FAIL_PTLRPC_IDLE_RACE        0x533
28394         $LCTL set_param fail_loc=0x80000533
28395         sleep 15
28396         dd if=/dev/zero of=$DIR/$tfile count=1 conv=sync || error "dd failed"
28397 }
28398 run_test 812c "idle import vs lock enqueue race"
28399
28400 test_813() {
28401         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
28402         [ -z "$file_heat_sav" ] && skip "no file heat support"
28403
28404         local readsample
28405         local writesample
28406         local readbyte
28407         local writebyte
28408         local readsample1
28409         local writesample1
28410         local readbyte1
28411         local writebyte1
28412
28413         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
28414         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
28415
28416         $LCTL set_param -n llite.*.file_heat=1
28417         echo "Turn on file heat"
28418         echo "Period second: $period_second, Decay percentage: $decay_pct"
28419
28420         echo "QQQQ" > $DIR/$tfile
28421         echo "QQQQ" > $DIR/$tfile
28422         echo "QQQQ" > $DIR/$tfile
28423         cat $DIR/$tfile > /dev/null
28424         cat $DIR/$tfile > /dev/null
28425         cat $DIR/$tfile > /dev/null
28426         cat $DIR/$tfile > /dev/null
28427
28428         local out=$($LFS heat_get $DIR/$tfile)
28429
28430         $LFS heat_get $DIR/$tfile
28431         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28432         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28433         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28434         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28435
28436         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
28437         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
28438         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
28439         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
28440
28441         sleep $((period_second + 3))
28442         echo "Sleep $((period_second + 3)) seconds..."
28443         # The recursion formula to calculate the heat of the file f is as
28444         # follow:
28445         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
28446         # Where Hi is the heat value in the period between time points i*I and
28447         # (i+1)*I; Ci is the access count in the period; the symbol P refers
28448         # to the weight of Ci.
28449         out=$($LFS heat_get $DIR/$tfile)
28450         $LFS heat_get $DIR/$tfile
28451         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28452         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28453         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28454         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28455
28456         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
28457                 error "read sample ($readsample) is wrong"
28458         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
28459                 error "write sample ($writesample) is wrong"
28460         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
28461                 error "read bytes ($readbyte) is wrong"
28462         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
28463                 error "write bytes ($writebyte) is wrong"
28464
28465         echo "QQQQ" > $DIR/$tfile
28466         echo "QQQQ" > $DIR/$tfile
28467         echo "QQQQ" > $DIR/$tfile
28468         cat $DIR/$tfile > /dev/null
28469         cat $DIR/$tfile > /dev/null
28470         cat $DIR/$tfile > /dev/null
28471         cat $DIR/$tfile > /dev/null
28472
28473         sleep $((period_second + 3))
28474         echo "Sleep $((period_second + 3)) seconds..."
28475
28476         out=$($LFS heat_get $DIR/$tfile)
28477         $LFS heat_get $DIR/$tfile
28478         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28479         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28480         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28481         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28482
28483         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
28484                 4 * $decay_pct) / 100") -eq 1 ] ||
28485                 error "read sample ($readsample1) is wrong"
28486         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
28487                 3 * $decay_pct) / 100") -eq 1 ] ||
28488                 error "write sample ($writesample1) is wrong"
28489         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
28490                 20 * $decay_pct) / 100") -eq 1 ] ||
28491                 error "read bytes ($readbyte1) is wrong"
28492         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
28493                 15 * $decay_pct) / 100") -eq 1 ] ||
28494                 error "write bytes ($writebyte1) is wrong"
28495
28496         echo "Turn off file heat for the file $DIR/$tfile"
28497         $LFS heat_set -o $DIR/$tfile
28498
28499         echo "QQQQ" > $DIR/$tfile
28500         echo "QQQQ" > $DIR/$tfile
28501         echo "QQQQ" > $DIR/$tfile
28502         cat $DIR/$tfile > /dev/null
28503         cat $DIR/$tfile > /dev/null
28504         cat $DIR/$tfile > /dev/null
28505         cat $DIR/$tfile > /dev/null
28506
28507         out=$($LFS heat_get $DIR/$tfile)
28508         $LFS heat_get $DIR/$tfile
28509         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28510         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28511         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28512         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28513
28514         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
28515         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
28516         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
28517         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
28518
28519         echo "Trun on file heat for the file $DIR/$tfile"
28520         $LFS heat_set -O $DIR/$tfile
28521
28522         echo "QQQQ" > $DIR/$tfile
28523         echo "QQQQ" > $DIR/$tfile
28524         echo "QQQQ" > $DIR/$tfile
28525         cat $DIR/$tfile > /dev/null
28526         cat $DIR/$tfile > /dev/null
28527         cat $DIR/$tfile > /dev/null
28528         cat $DIR/$tfile > /dev/null
28529
28530         out=$($LFS heat_get $DIR/$tfile)
28531         $LFS heat_get $DIR/$tfile
28532         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28533         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28534         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28535         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28536
28537         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
28538         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
28539         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
28540         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
28541
28542         $LFS heat_set -c $DIR/$tfile
28543         $LCTL set_param -n llite.*.file_heat=0
28544         echo "Turn off file heat support for the Lustre filesystem"
28545
28546         echo "QQQQ" > $DIR/$tfile
28547         echo "QQQQ" > $DIR/$tfile
28548         echo "QQQQ" > $DIR/$tfile
28549         cat $DIR/$tfile > /dev/null
28550         cat $DIR/$tfile > /dev/null
28551         cat $DIR/$tfile > /dev/null
28552         cat $DIR/$tfile > /dev/null
28553
28554         out=$($LFS heat_get $DIR/$tfile)
28555         $LFS heat_get $DIR/$tfile
28556         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
28557         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
28558         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
28559         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
28560
28561         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
28562         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
28563         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
28564         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
28565
28566         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
28567         rm -f $DIR/$tfile
28568 }
28569 run_test 813 "File heat verfication"
28570
28571 test_814()
28572 {
28573         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
28574         echo -n y >> $DIR/$tfile
28575         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
28576         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
28577 }
28578 run_test 814 "sparse cp works as expected (LU-12361)"
28579
28580 test_815()
28581 {
28582         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
28583         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
28584 }
28585 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
28586
28587 test_816() {
28588         local ost1_imp=$(get_osc_import_name client ost1)
28589         local imp_name=$($LCTL list_param osc.$ost1_imp | head -n1 |
28590                          cut -d'.' -f2)
28591
28592         $LFS setstripe -c 1 -i 0 $DIR/$tfile
28593         # ensure ost1 is connected
28594
28595         stat $DIR/$tfile >/dev/null || error "can't stat"
28596         wait_osc_import_state client ost1 FULL
28597         # no locks, no reqs to let the connection idle
28598         cancel_lru_locks osc
28599         lru_resize_disable osc
28600         local before
28601         local now
28602         before=$($LCTL get_param -n \
28603                  ldlm.namespaces.$imp_name.lru_size)
28604
28605         wait_osc_import_state client ost1 IDLE
28606         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
28607         now=$($LCTL get_param -n \
28608               ldlm.namespaces.$imp_name.lru_size)
28609         [ $before == $now ] || error "lru_size changed $before != $now"
28610 }
28611 run_test 816 "do not reset lru_resize on idle reconnect"
28612
28613 cleanup_817() {
28614         umount $tmpdir
28615         exportfs -u localhost:$DIR/nfsexp
28616         rm -rf $DIR/nfsexp
28617 }
28618
28619 test_817() {
28620         systemctl restart nfs-server.service || skip "failed to restart nfsd"
28621
28622         mkdir -p $DIR/nfsexp
28623         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
28624                 error "failed to export nfs"
28625
28626         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
28627         stack_trap cleanup_817 EXIT
28628
28629         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
28630                 error "failed to mount nfs to $tmpdir"
28631
28632         cp /bin/true $tmpdir
28633         $DIR/nfsexp/true || error "failed to execute 'true' command"
28634 }
28635 run_test 817 "nfsd won't cache write lock for exec file"
28636
28637 test_818() {
28638         test_mkdir -i0 -c1 $DIR/$tdir
28639         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
28640         $LFS setstripe -c1 -i1 $DIR/$tdir/$tfile
28641         stop $SINGLEMDS
28642
28643         # restore osp-syn threads
28644         stack_trap "fail $SINGLEMDS"
28645
28646         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
28647         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
28648         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
28649                 error "start $SINGLEMDS failed"
28650         rm -rf $DIR/$tdir
28651
28652         local testid=$(echo $TESTNAME | tr '_' ' ')
28653
28654         do_facet mds1 dmesg | tac | sed "/$testid/,$ d" |
28655                 grep "run LFSCK" || error "run LFSCK is not suggested"
28656 }
28657 run_test 818 "unlink with failed llog"
28658
28659 test_819a() {
28660         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
28661         cancel_lru_locks osc
28662         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
28663         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
28664         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
28665         rm -f $TDIR/$tfile
28666 }
28667 run_test 819a "too big niobuf in read"
28668
28669 test_819b() {
28670         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
28671         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
28672         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
28673         cancel_lru_locks osc
28674         sleep 1
28675         rm -f $TDIR/$tfile
28676 }
28677 run_test 819b "too big niobuf in write"
28678
28679
28680 function test_820_start_ost() {
28681         sleep 5
28682
28683         for num in $(seq $OSTCOUNT); do
28684                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
28685         done
28686 }
28687
28688 test_820() {
28689         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
28690
28691         mkdir $DIR/$tdir
28692         umount_client $MOUNT || error "umount failed"
28693         for num in $(seq $OSTCOUNT); do
28694                 stop ost$num
28695         done
28696
28697         # mount client with no active OSTs
28698         # so that the client can't initialize max LOV EA size
28699         # from OSC notifications
28700         mount_client $MOUNT || error "mount failed"
28701         # delay OST starting to keep this 0 max EA size for a while
28702         test_820_start_ost &
28703
28704         # create a directory on MDS2
28705         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
28706                 error "Failed to create directory"
28707         # open intent should update default EA size
28708         # see mdc_update_max_ea_from_body()
28709         # notice this is the very first RPC to MDS2
28710         out=$(cp /etc/services $DIR/$tdir/mds2 2>&1)
28711         ret=$?
28712         echo $out
28713         # With SSK, this situation can lead to -EPERM being returned.
28714         # In that case, simply retry.
28715         if [ $ret -ne 0 ] && $SHARED_KEY; then
28716                 if echo "$out" | grep -q "not permitted"; then
28717                         cp /etc/services $DIR/$tdir/mds2
28718                         ret=$?
28719                 fi
28720         fi
28721         [ $ret -eq 0 ] || error "Failed to copy files to mds$n"
28722 }
28723 run_test 820 "update max EA from open intent"
28724
28725 test_823() {
28726         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
28727         local OST_MAX_PRECREATE=20000
28728
28729         (( $MDS1_VERSION >= $(version_code 2.14.56) )) ||
28730                 skip "Need MDS version at least 2.14.56"
28731
28732         save_lustre_params mds1 \
28733                 "osp.$FSNAME-OST*-osc-MDT0000.max_create_count" > $p
28734         do_facet $SINGLEMDS "$LCTL set_param -n \
28735                 osp.$FSNAME-OST*MDT0000.max_create_count=0"
28736         do_facet $SINGLEMDS "$LCTL set_param -n \
28737                 osp.$FSNAME-OST0000*MDT0000.max_create_count=$OST_MAX_PRECREATE"
28738
28739         stack_trap "restore_lustre_params < $p; rm $p"
28740
28741         do_facet $SINGLEMDS "$LCTL set_param -n \
28742                 osp.$FSNAME-OST*-osc-MDT*.create_count=100200"
28743
28744         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
28745                       osp.$FSNAME-OST0000*MDT0000.create_count")
28746         local max=$(do_facet $SINGLEMDS "$LCTL get_param -n \
28747                     osp.$FSNAME-OST0000*MDT0000.max_create_count")
28748         local expect_count=$(((($max/2)/256) * 256))
28749
28750         log "setting create_count to 100200:"
28751         log " -result- count: $count with max: $max, expecting: $expect_count"
28752
28753         [[ $count -eq expect_count ]] ||
28754                 error "Create count not set to max precreate."
28755 }
28756 run_test 823 "Setting create_count > OST_MAX_PRECREATE is lowered to maximum"
28757
28758 test_831() {
28759         [[ $MDS1_VERSION -lt $(version_code 2.14.56) ]] &&
28760                 skip "Need MDS version 2.14.56"
28761
28762         local sync_changes=$(do_facet $SINGLEMDS \
28763                 $LCTL get_param -n osp.$FSNAME-OST0000-osc-MDT0000.sync_changes)
28764
28765         [ "$sync_changes" -gt 100 ] &&
28766                 skip "Sync changes $sync_changes > 100 already"
28767
28768         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
28769
28770         $LFS mkdir -i 0 $DIR/$tdir
28771         $LFS setstripe -c 1 -i 0 $DIR/$tdir
28772
28773         save_lustre_params mds1 \
28774                 "osp.$FSNAME-OST*-osc-MDT0000.max_sync_changes" > $p
28775         save_lustre_params mds1 \
28776                 "osp.$FSNAME-OST*-osc-MDT0000.max_rpcs_in_progress" >> $p
28777
28778         do_facet mds1 "$LCTL set_param -n \
28779                 osp.$FSNAME-OST*-osc-MDT0000.max_sync_changes=100 \
28780                 osp.$FSNAME-OST*-osc-MDT0000.max_rpcs_in_progress=128"
28781         stack_trap "restore_lustre_params < $p" EXIT
28782
28783         createmany -o $DIR/$tdir/f- 1000
28784         unlinkmany $DIR/$tdir/f- 1000 &
28785         local UNLINK_PID=$!
28786
28787         while sleep 1; do
28788                 sync_changes=$(do_facet mds1 \
28789                 $LCTL get_param -n osp.$FSNAME-OST0000-osc-MDT0000.sync_changes)
28790                 # the check in the code is racy, fail the test
28791                 # if the value above the limit by 10.
28792                 [ $sync_changes -gt 110 ] && {
28793                         kill -2 $UNLINK_PID
28794                         wait
28795                         error "osp changes throttling failed, $sync_changes>110"
28796                 }
28797                 kill -0 $UNLINK_PID 2> /dev/null || break
28798         done
28799         wait
28800 }
28801 run_test 831 "throttling unlink/setattr queuing on OSP"
28802
28803 #
28804 # tests that do cleanup/setup should be run at the end
28805 #
28806
28807 test_900() {
28808         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28809         local ls
28810
28811         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
28812         $LCTL set_param fail_loc=0x903
28813
28814         cancel_lru_locks MGC
28815
28816         FAIL_ON_ERROR=true cleanup
28817         FAIL_ON_ERROR=true setup
28818 }
28819 run_test 900 "umount should not race with any mgc requeue thread"
28820
28821 # LUS-6253/LU-11185
28822 test_901() {
28823         local old
28824         local count
28825         local oldc
28826         local newc
28827         local olds
28828         local news
28829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
28830
28831         # some get_param have a bug to handle dot in param name
28832         cancel_lru_locks MGC
28833         old=$(mount -t lustre | wc -l)
28834         # 1 config+sptlrpc
28835         # 2 params
28836         # 3 nodemap
28837         # 4 IR
28838         old=$((old * 4))
28839         oldc=0
28840         count=0
28841         while [ $old -ne $oldc ]; do
28842                 oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
28843                 sleep 1
28844                 ((count++))
28845                 if [ $count -ge $TIMEOUT ]; then
28846                         error "too large timeout"
28847                 fi
28848         done
28849         umount_client $MOUNT || error "umount failed"
28850         mount_client $MOUNT || error "mount failed"
28851         cancel_lru_locks MGC
28852         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
28853
28854         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
28855
28856         return 0
28857 }
28858 run_test 901 "don't leak a mgc lock on client umount"
28859
28860 # LU-13377
28861 test_902() {
28862         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
28863                 skip "client does not have LU-13377 fix"
28864         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
28865         $LCTL set_param fail_loc=0x1415
28866         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
28867         cancel_lru_locks osc
28868         rm -f $DIR/$tfile
28869 }
28870 run_test 902 "test short write doesn't hang lustre"
28871
28872 # LU-14711
28873 test_903() {
28874         $LFS setstripe -i 0 -c 1 $DIR/$tfile $DIR/${tfile}-2
28875         echo "blah" > $DIR/${tfile}-2
28876         dd if=/dev/zero of=$DIR/$tfile bs=1M count=6 conv=fsync
28877         #define OBD_FAIL_OSC_SLOW_PAGE_EVICT 0x417
28878         $LCTL set_param fail_loc=0x417 fail_val=20
28879
28880         mv $DIR/${tfile}-2 $DIR/$tfile # Destroys the big object
28881         sleep 1 # To start the destroy
28882         wait_destroy_complete 150 || error "Destroy taking too long"
28883         cat $DIR/$tfile > /dev/null || error "Evicted"
28884 }
28885 run_test 903 "Test long page discard does not cause evictions"
28886
28887 test_904() {
28888         [ "$mds1_FSTYPE" == "ldiskfs" ] || skip "ldiskfs only test"
28889         do_facet mds1 $DEBUGFS -R features $(mdsdevname 1) |
28890                 grep -q project || skip "skip project quota not supported"
28891
28892         local testfile="$DIR/$tdir/$tfile"
28893         local xattr="trusted.projid"
28894         local projid
28895         local mdts=$(comma_list $(mdts_nodes))
28896         local saved=$(do_facet mds1 $LCTL get_param -n \
28897                 osd-ldiskfs.*MDT0000.enable_projid_xattr)
28898
28899         do_nodes $mdts $LCTL set_param osd-ldiskfs.*MDT*.enable_projid_xattr=0
28900         stack_trap "do_nodes $mdts $LCTL set_param \
28901                 osd-ldiskfs.*MDT*.enable_projid_xattr=$saved"
28902
28903         mkdir -p $DIR/$tdir
28904         touch $testfile
28905         #hide projid xattr on server
28906         $LFS project -p 1 $testfile ||
28907                 error "set $testfile project id failed"
28908         getfattr -m - $testfile | grep $xattr &&
28909                 error "do not show trusted.projid when disabled on server"
28910         do_nodes $mdts $LCTL set_param osd-ldiskfs.*MDT*.enable_projid_xattr=1
28911         #should be hidden when projid is 0
28912         $LFS project -p 0 $testfile ||
28913                 error "set $testfile project id failed"
28914         getfattr -m - $testfile | grep $xattr &&
28915                 error "do not show trusted.projid with project ID 0"
28916
28917         #still can getxattr explicitly
28918         projid=$(getfattr -n $xattr $testfile |
28919                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
28920         [ $projid == "0" ] ||
28921                 error "projid expected 0 not $projid"
28922
28923         #set the projid via setxattr
28924         setfattr -n $xattr -v "1000" $testfile ||
28925                 error "setattr failed with $?"
28926         projid=($($LFS project $testfile))
28927         [ ${projid[0]} == "1000" ] ||
28928                 error "projid expected 1000 not $projid"
28929
28930         #check the new projid via getxattr
28931         $LFS project -p 1001 $testfile ||
28932                 error "set $testfile project id failed"
28933         getfattr -m - $testfile | grep $xattr ||
28934                 error "should show trusted.projid when project ID != 0"
28935         projid=$(getfattr -n $xattr $testfile |
28936                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
28937         [ $projid == "1001" ] ||
28938                 error "projid expected 1001 not $projid"
28939
28940         #try to set invalid projid
28941         setfattr -n $xattr -v "4294967295" $testfile &&
28942                 error "set invalid projid should fail"
28943
28944         #remove the xattr means setting projid to 0
28945         setfattr -x $xattr $testfile ||
28946                 error "setfattr failed with $?"
28947         projid=($($LFS project $testfile))
28948         [ ${projid[0]} == "0" ] ||
28949                 error "projid expected 0 not $projid"
28950
28951         #should be hidden when parent has inherit flag and same projid
28952         $LFS project -srp 1002 $DIR/$tdir ||
28953                 error "set $tdir project id failed"
28954         getfattr -m - $testfile | grep $xattr &&
28955                 error "do not show trusted.projid with inherit flag"
28956
28957         #still can getxattr explicitly
28958         projid=$(getfattr -n $xattr $testfile |
28959                 sed -n 's/^trusted\.projid="\(.*\)"/\1/p')
28960         [ $projid == "1002" ] ||
28961                 error "projid expected 1002 not $projid"
28962 }
28963 run_test 904 "virtual project ID xattr"
28964
28965 # LU-8582
28966 test_905() {
28967         (( $OST1_VERSION >= $(version_code 2.8.54) )) ||
28968                 skip "lustre < 2.8.54 does not support ladvise"
28969
28970         remote_ost_nodsh && skip "remote OST with nodsh"
28971         $LFS setstripe -c -1 -i 0 $DIR/$tfile || error "setstripe failed"
28972
28973         $LFS ladvise -a willread $DIR/$tfile || error "ladvise does not work"
28974
28975         #define OBD_FAIL_OST_OPCODE 0x253
28976         # OST_LADVISE = 21
28977         do_facet ost1 "$LCTL set_param fail_val=21 fail_loc=0x0253"
28978         $LFS ladvise -a willread $DIR/$tfile &&
28979                 error "unexpected success of ladvise with fault injection"
28980         $LFS ladvise -a willread $DIR/$tfile |&
28981                 grep -q "Operation not supported"
28982         (( $? == 0 )) || error "unexpected stderr of ladvise with fault injection"
28983 }
28984 run_test 905 "bad or new opcode should not stuck client"
28985
28986 test_906() {
28987         grep -q io_uring_setup /proc/kallsyms ||
28988                 skip "Client OS does not support io_uring I/O engine"
28989         io_uring_probe || skip "kernel does not support io_uring fully"
28990         which fio || skip_env "no fio installed"
28991         fio --enghelp | grep -q io_uring ||
28992                 skip_env "fio does not support io_uring I/O engine"
28993
28994         local file=$DIR/$tfile
28995         local ioengine="io_uring"
28996         local numjobs=2
28997         local size=50M
28998
28999         fio --name=seqwrite --ioengine=$ioengine        \
29000                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
29001                 --iodepth=64 --size=$size --filename=$file --rw=write ||
29002                 error "fio seqwrite $file failed"
29003
29004         fio --name=seqread --ioengine=$ioengine \
29005                 --bs=$PAGE_SIZE --direct=1 --numjobs=$numjobs   \
29006                 --iodepth=64 --size=$size --filename=$file --rw=read ||
29007                 error "fio seqread $file failed"
29008
29009         rm -f $file || error "rm -f $file failed"
29010 }
29011 run_test 906 "Simple test for io_uring I/O engine via fio"
29012
29013 complete $SECONDS
29014 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
29015 check_and_cleanup_lustre
29016 if [ "$I_MOUNTED" != "yes" ]; then
29017         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
29018 fi
29019 exit_status