Whamcloud - gitweb
414d95e8238e3efc4d020c88c2cc76ae1498f895
[fs/lustre-release.git] / lustre / tests / sanity.sh
1 #!/bin/bash
2 #
3 # Run select tests by setting ONLY, or as arguments to the script.
4 # Skip specific tests by setting EXCEPT.
5 #
6 # e.g. ONLY="22 23" or ONLY="`seq 32 39`" or EXCEPT="31"
7 set -e
8
9 ONLY=${ONLY:-"$*"}
10
11 # Check Grants after these tests
12 GRANT_CHECK_LIST="$GRANT_CHECK_LIST 42a 42b 42c 42d 42e 63a 63b 64a 64b 64c 64d"
13
14 OSC=${OSC:-"osc"}
15
16 CC=${CC:-cc}
17 CREATETEST=${CREATETEST:-createtest}
18 LVERIFY=${LVERIFY:-ll_dirstripe_verify}
19 OPENFILE=${OPENFILE:-openfile}
20 OPENUNLINK=${OPENUNLINK:-openunlink}
21 READS=${READS:-"reads"}
22 MUNLINK=${MUNLINK:-munlink}
23 SOCKETSERVER=${SOCKETSERVER:-socketserver}
24 SOCKETCLIENT=${SOCKETCLIENT:-socketclient}
25 MEMHOG=${MEMHOG:-memhog}
26 DIRECTIO=${DIRECTIO:-directio}
27 ACCEPTOR_PORT=${ACCEPTOR_PORT:-988}
28 DEF_STRIPE_COUNT=-1
29 CHECK_GRANT=${CHECK_GRANT:-"yes"}
30 GRANT_CHECK_LIST=${GRANT_CHECK_LIST:-""}
31 export PARALLEL=${PARALLEL:-"no"}
32
33 TRACE=${TRACE:-""}
34 LUSTRE=${LUSTRE:-$(dirname $0)/..}
35 LUSTRE_TESTS_API_DIR=${LUSTRE_TESTS_API_DIR:-${LUSTRE}/tests/clientapi}
36 . $LUSTRE/tests/test-framework.sh
37 init_test_env $@
38
39 init_logging
40
41 ALWAYS_EXCEPT="$SANITY_EXCEPT "
42 # bug number for skipped test: LU-9693 LU-6493 LU-9693
43 ALWAYS_EXCEPT+="               42a     42b     42c "
44 # bug number:    LU-8411 LU-9054 LU-13314
45 ALWAYS_EXCEPT+=" 407     312    56ob"
46
47 if $SHARED_KEY; then
48         # bug number:    LU-9795 LU-9795 LU-9795 LU-9795
49         ALWAYS_EXCEPT+=" 17n     60a     133g    300f"
50 fi
51
52 selinux_status=$(getenforce)
53 if [ "$selinux_status" != "Disabled" ]; then
54         # bug number:
55         ALWAYS_EXCEPT+=""
56 fi
57
58 # skip the grant tests for ARM until they are fixed
59 if [[ $(uname -m) = aarch64 ]]; then
60         # bug number:    LU-11596
61         ALWAYS_EXCEPT+=" $GRANT_CHECK_LIST"
62         # bug number:    LU-11671 LU-11667
63         ALWAYS_EXCEPT+=" 45       317"
64 fi
65
66 # skip nfs tests on kernels >= 4.14.0 until they are fixed
67 if [ $LINUX_VERSION_CODE -ge $(version_code 4.14.0) ]; then
68         # bug number:   LU-12661
69         ALWAYS_EXCEPT+=" 817"
70 fi
71 # skip cgroup tests on RHEL8.1 kernels until they are fixed
72 if (( $LINUX_VERSION_CODE >= $(version_code 4.18.0) &&
73       $LINUX_VERSION_CODE <  $(version_code 5.4.0) )); then
74         # bug number:   LU-13063
75         ALWAYS_EXCEPT+=" 411"
76 fi
77
78 #                                  5          12     8   12  (min)"
79 [ "$SLOW" = "no" ] && EXCEPT_SLOW="27m 64b 68 71 115 135 136 300o"
80
81 if [ "$mds1_FSTYPE" = "zfs" ]; then
82         # bug number for skipped test:
83         ALWAYS_EXCEPT="$ALWAYS_EXCEPT  "
84         #                                               13    (min)"
85         [ "$SLOW" = "no" ] && EXCEPT_SLOW="$EXCEPT_SLOW 51b"
86 fi
87
88 # Get the SLES distro version
89 #
90 # Returns a version string that should only be used in comparing
91 # strings returned by version_code()
92 sles_version_code()
93 {
94         local version=$(grep VERSION_ID /etc/os-release | cut -d'"' -f2)
95
96         # All SuSE Linux versions have one decimal. version_code expects two
97         local sles_version=$version.0
98         version_code $sles_version
99 }
100
101 # Check if we are running on Ubuntu or SLES so we can make decisions on
102 # what tests to run
103 if [ -r /etc/SuSE-release ]; then
104         sles_version=$(sles_version_code)
105         [ $sles_version -lt $(version_code 11.4.0) ] &&
106                 # bug number for skipped test: LU-4341
107                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  170"
108         [ $sles_version -lt $(version_code 12.0.0) ] &&
109                 # bug number for skipped test: LU-3703
110                 ALWAYS_EXCEPT="$ALWAYS_EXCEPT  234"
111 elif [ -r /etc/os-release ]; then
112         if grep -qi ubuntu /etc/os-release; then
113                 ubuntu_version=$(version_code $(sed -n -e 's/"//g' \
114                                                 -e 's/^VERSION=//p' \
115                                                 /etc/os-release |
116                                                 awk '{ print $1 }'))
117
118                 if [[ $ubuntu_version -gt $(version_code 16.0.0) ]]; then
119                         # bug number for skipped test:
120                         #                LU-10334 LU-10366
121                         ALWAYS_EXCEPT+=" 103a     410"
122                 fi
123         fi
124 fi
125
126 build_test_filter
127 FAIL_ON_ERROR=false
128
129 cleanup() {
130         echo -n "cln.."
131         pgrep ll_sa > /dev/null && { echo "There are ll_sa thread not exit!"; exit 20; }
132         cleanupall ${FORCE} $* || { echo "FAILed to clean up"; exit 20; }
133 }
134 setup() {
135         echo -n "mnt.."
136         load_modules
137         setupall || exit 10
138         echo "done"
139 }
140
141 check_swap_layouts_support()
142 {
143         $LCTL get_param -n llite.*.sbi_flags | grep -q layout ||
144                 skip "Does not support layout lock."
145 }
146
147 check_and_setup_lustre
148 DIR=${DIR:-$MOUNT}
149 assert_DIR
150
151 MAXFREE=${MAXFREE:-$((200000 * $OSTCOUNT))}
152
153 [ -f $DIR/d52a/foo ] && chattr -a $DIR/d52a/foo
154 [ -f $DIR/d52b/foo ] && chattr -i $DIR/d52b/foo
155 rm -rf $DIR/[Rdfs][0-9]*
156
157 # $RUNAS_ID may get set incorrectly somewhere else
158 [ $UID -eq 0 -a $RUNAS_ID -eq 0 ] &&
159         error "\$RUNAS_ID set to 0, but \$UID is also 0!"
160
161 check_runas_id $RUNAS_ID $RUNAS_GID $RUNAS
162
163 if [ "${ONLY}" = "MOUNT" ] ; then
164         echo "Lustre is up, please go on"
165         exit
166 fi
167
168 echo "preparing for tests involving mounts"
169 EXT2_DEV=${EXT2_DEV:-$TMP/SANITY.LOOP}
170 touch $EXT2_DEV
171 mke2fs -j -F $EXT2_DEV 8000 > /dev/null
172 echo # add a newline after mke2fs.
173
174 umask 077
175
176 OLDDEBUG=$(lctl get_param -n debug 2> /dev/null)
177 lctl set_param debug=-1 2> /dev/null || true
178 test_0a() {
179         touch $DIR/$tfile
180         $CHECKSTAT -t file $DIR/$tfile || error "$tfile is not a file"
181         rm $DIR/$tfile
182         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
183 }
184 run_test 0a "touch; rm ====================="
185
186 test_0b() {
187         chmod 0755 $DIR || error "chmod 0755 $DIR failed"
188         $CHECKSTAT -p 0755 $DIR || error "$DIR permission is not 0755"
189 }
190 run_test 0b "chmod 0755 $DIR ============================="
191
192 test_0c() {
193         $LCTL get_param mdc.*.import | grep "state: FULL" ||
194                 error "import not FULL"
195         $LCTL get_param mdc.*.import | grep "target: $FSNAME-MDT" ||
196                 error "bad target"
197 }
198 run_test 0c "check import proc"
199
200 test_0d() { # LU-3397
201         [ $MGS_VERSION -lt $(version_code 2.10.57) ] &&
202                 skip "proc exports not supported before 2.10.57"
203
204         local mgs_exp="mgs.MGS.exports"
205         local client_uuid=$($LCTL get_param -n mgc.*.uuid)
206         local exp_client_nid
207         local exp_client_version
208         local exp_val
209         local imp_val
210         local temp_imp=$DIR/$tfile.import
211         local temp_exp=$DIR/$tfile.export
212
213         # save mgc import file to $temp_imp
214         $LCTL get_param mgc.*.import | tee $temp_imp
215         # Check if client uuid is found in MGS export
216         for exp_client_nid in $(do_facet mgs $LCTL get_param -N $mgs_exp.*); do
217                 [ $(do_facet mgs $LCTL get_param -n $exp_client_nid.uuid) == \
218                         $client_uuid ] &&
219                         break;
220         done
221         # save mgs export file to $temp_exp
222         do_facet mgs $LCTL get_param $exp_client_nid.export | tee $temp_exp
223
224         # Compare the value of field "connect_flags"
225         imp_val=$(grep "connect_flags" $temp_imp)
226         exp_val=$(grep "connect_flags" $temp_exp)
227         [ "$exp_val" == "$imp_val" ] ||
228                 error "export flags '$exp_val' != import flags '$imp_val'"
229
230         # Compare the value of client version
231         exp_client_version=$(awk '/target_version:/ { print $2 }' $temp_exp)
232         exp_val=$(version_code $exp_client_version)
233         imp_val=$CLIENT_VERSION
234         [ "$exp_val" == "$imp_val" ] ||
235                 error "export client version '$exp_val' != '$imp_val'"
236 }
237 run_test 0d "check export proc ============================="
238
239 test_1() {
240         test_mkdir $DIR/$tdir
241         test_mkdir $DIR/$tdir/d2
242         mkdir $DIR/$tdir/d2 && error "we expect EEXIST, but not returned"
243         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a dir"
244         rmdir $DIR/$tdir/d2
245         rmdir $DIR/$tdir
246         $CHECKSTAT -a $DIR/$tdir || error "$tdir was not removed"
247 }
248 run_test 1 "mkdir; remkdir; rmdir"
249
250 test_2() {
251         test_mkdir $DIR/$tdir
252         touch $DIR/$tdir/$tfile || error "touch $tdir/$tfile failed"
253         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
254         rm -r $DIR/$tdir
255         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$file is not removed"
256 }
257 run_test 2 "mkdir; touch; rmdir; check file"
258
259 test_3() {
260         test_mkdir $DIR/$tdir
261         $CHECKSTAT -t dir $DIR/$tdir || error "$tdir is not a directory"
262         touch $DIR/$tdir/$tfile
263         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "$tdir/$tfile not a file"
264         rm -r $DIR/$tdir
265         $CHECKSTAT -a $DIR/$tdir || error "$tdir is not removed"
266 }
267 run_test 3 "mkdir; touch; rmdir; check dir"
268
269 # LU-4471 - failed rmdir on remote directories still removes directory on MDT0
270 test_4() {
271         test_mkdir -i 1 $DIR/$tdir
272
273         touch $DIR/$tdir/$tfile ||
274                 error "Create file under remote directory failed"
275
276         rmdir $DIR/$tdir &&
277                 error "Expect error removing in-use dir $DIR/$tdir"
278
279         test -d $DIR/$tdir || error "Remote directory disappeared"
280
281         rm -rf $DIR/$tdir || error "remove remote dir error"
282 }
283 run_test 4 "mkdir; touch dir/file; rmdir; checkdir (expect error)"
284
285 test_5() {
286         test_mkdir $DIR/$tdir
287         test_mkdir $DIR/$tdir/d2
288         chmod 0707 $DIR/$tdir/d2 || error "chmod 0707 $tdir/d2 failed"
289         $CHECKSTAT -t dir -p 0707 $DIR/$tdir/d2 || error "$tdir/d2 not mode 707"
290         $CHECKSTAT -t dir $DIR/$tdir/d2 || error "$tdir/d2 is not a directory"
291 }
292 run_test 5 "mkdir .../d5 .../d5/d2; chmod .../d5/d2"
293
294 test_6a() {
295         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
296         chmod 0666 $DIR/$tfile || error "chmod 0666 $tfile failed"
297         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
298                 error "$tfile does not have perm 0666 or UID $UID"
299         $RUNAS chmod 0444 $DIR/$tfile && error "chmod $tfile worked on UID $UID"
300         $CHECKSTAT -t file -p 0666 -u \#$UID $DIR/$tfile ||
301                 error "$tfile should be 0666 and owned by UID $UID"
302 }
303 run_test 6a "touch f6a; chmod f6a; $RUNAS chmod f6a (should return error) =="
304
305 test_6c() {
306         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
307
308         touch $DIR/$tfile
309         chown $RUNAS_ID $DIR/$tfile || error "chown $RUNAS_ID $file failed"
310         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
311                 error "$tfile should be owned by UID $RUNAS_ID"
312         $RUNAS chown $UID $DIR/$tfile && error "chown $UID $file succeeded"
313         $CHECKSTAT -t file -u \#$RUNAS_ID $DIR/$tfile ||
314                 error "$tfile should be owned by UID $RUNAS_ID"
315 }
316 run_test 6c "touch f6c; chown f6c; $RUNAS chown f6c (should return error) =="
317
318 test_6e() {
319         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
320
321         touch $DIR/$tfile
322         chgrp $RUNAS_ID $DIR/$tfile || error "chgrp $RUNAS_ID $file failed"
323         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
324                 error "$tfile should be owned by GID $UID"
325         $RUNAS chgrp $UID $DIR/$tfile && error "chgrp $UID $file succeeded"
326         $CHECKSTAT -t file -u \#$UID -g \#$RUNAS_ID $DIR/$tfile ||
327                 error "$tfile should be owned by UID $UID and GID $RUNAS_ID"
328 }
329 run_test 6e "touch+chgrp $tfile; $RUNAS chgrp $tfile (should return error)"
330
331 test_6g() {
332         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
333
334         test_mkdir $DIR/$tdir
335         chmod 777 $DIR/$tdir || error "chmod 0777 $tdir failed"
336         $RUNAS mkdir $DIR/$tdir/d || error "mkdir $tdir/d failed"
337         chmod g+s $DIR/$tdir/d || error "chmod g+s $tdir/d failed"
338         test_mkdir $DIR/$tdir/d/subdir
339         $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir/d/subdir ||
340                 error "$tdir/d/subdir should be GID $RUNAS_GID"
341         if [[ $MDSCOUNT -gt 1 ]]; then
342                 # check remote dir sgid inherite
343                 $LFS mkdir -i 0 $DIR/$tdir.local ||
344                         error "mkdir $tdir.local failed"
345                 chmod g+s $DIR/$tdir.local ||
346                         error "chmod $tdir.local failed"
347                 chgrp $RUNAS_GID $DIR/$tdir.local ||
348                         error "chgrp $tdir.local failed"
349                 $LFS mkdir -i 1 $DIR/$tdir.local/$tdir.remote ||
350                         error "mkdir $tdir.remote failed"
351                 $CHECKSTAT -g \#$RUNAS_GID $DIR/$tdir.local/$tdir.remote ||
352                         error "$tdir.remote should be owned by $UID.$RUNAS_ID"
353                 $CHECKSTAT -p 02755 $DIR/$tdir.local/$tdir.remote ||
354                         error "$tdir.remote should be mode 02755"
355         fi
356 }
357 run_test 6g "verify new dir in sgid dir inherits group"
358
359 test_6h() { # bug 7331
360         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID"
361
362         touch $DIR/$tfile || error "touch failed"
363         chown $RUNAS_ID:$RUNAS_GID $DIR/$tfile || error "initial chown failed"
364         $RUNAS -G$RUNAS_GID chown $RUNAS_ID:0 $DIR/$tfile &&
365                 error "chown $RUNAS_ID:0 $tfile worked as GID $RUNAS_GID"
366         $CHECKSTAT -t file -u \#$RUNAS_ID -g \#$RUNAS_GID $DIR/$tfile ||
367                 error "$tdir/$tfile should be UID $RUNAS_UID GID $RUNAS_GID"
368 }
369 run_test 6h "$RUNAS chown RUNAS_ID.0 .../$tfile (should return error)"
370
371 test_7a() {
372         test_mkdir $DIR/$tdir
373         $MCREATE $DIR/$tdir/$tfile
374         chmod 0666 $DIR/$tdir/$tfile
375         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
376                 error "$tdir/$tfile should be mode 0666"
377 }
378 run_test 7a "mkdir .../d7; mcreate .../d7/f; chmod .../d7/f ===="
379
380 test_7b() {
381         if [ ! -d $DIR/$tdir ]; then
382                 test_mkdir $DIR/$tdir
383         fi
384         $MCREATE $DIR/$tdir/$tfile
385         echo -n foo > $DIR/$tdir/$tfile
386         [ "$(cat $DIR/$tdir/$tfile)" = "foo" ] || error "$tdir/$tfile not 'foo'"
387         $CHECKSTAT -t file -s 3 $DIR/$tdir/$tfile || error "$tfile size not 3"
388 }
389 run_test 7b "mkdir .../d7; mcreate d7/f2; echo foo > d7/f2 ====="
390
391 test_8() {
392         test_mkdir $DIR/$tdir
393         touch $DIR/$tdir/$tfile
394         chmod 0666 $DIR/$tdir/$tfile
395         $CHECKSTAT -t file -p 0666 $DIR/$tdir/$tfile ||
396                 error "$tfile mode not 0666"
397 }
398 run_test 8 "mkdir .../d8; touch .../d8/f; chmod .../d8/f ======="
399
400 test_9() {
401         test_mkdir $DIR/$tdir
402         test_mkdir $DIR/$tdir/d2
403         test_mkdir $DIR/$tdir/d2/d3
404         $CHECKSTAT -t dir $DIR/$tdir/d2/d3 || error "$tdir/d2/d3 not a dir"
405 }
406 run_test 9 "mkdir .../d9 .../d9/d2 .../d9/d2/d3 ================"
407
408 test_10() {
409         test_mkdir $DIR/$tdir
410         test_mkdir $DIR/$tdir/d2
411         touch $DIR/$tdir/d2/$tfile
412         $CHECKSTAT -t file $DIR/$tdir/d2/$tfile ||
413                 error "$tdir/d2/$tfile not a file"
414 }
415 run_test 10 "mkdir .../d10 .../d10/d2; touch .../d10/d2/f ======"
416
417 test_11() {
418         test_mkdir $DIR/$tdir
419         test_mkdir $DIR/$tdir/d2
420         chmod 0666 $DIR/$tdir/d2
421         chmod 0705 $DIR/$tdir/d2
422         $CHECKSTAT -t dir -p 0705 $DIR/$tdir/d2 ||
423                 error "$tdir/d2 mode not 0705"
424 }
425 run_test 11 "mkdir .../d11 d11/d2; chmod .../d11/d2 ============"
426
427 test_12() {
428         test_mkdir $DIR/$tdir
429         touch $DIR/$tdir/$tfile
430         chmod 0666 $DIR/$tdir/$tfile
431         chmod 0654 $DIR/$tdir/$tfile
432         $CHECKSTAT -t file -p 0654 $DIR/$tdir/$tfile ||
433                 error "$tdir/d2 mode not 0654"
434 }
435 run_test 12 "touch .../d12/f; chmod .../d12/f .../d12/f ========"
436
437 test_13() {
438         test_mkdir $DIR/$tdir
439         dd if=/dev/zero of=$DIR/$tdir/$tfile count=10
440         >  $DIR/$tdir/$tfile
441         $CHECKSTAT -t file -s 0 $DIR/$tdir/$tfile ||
442                 error "$tdir/$tfile size not 0 after truncate"
443 }
444 run_test 13 "creat .../d13/f; dd .../d13/f; > .../d13/f ========"
445
446 test_14() {
447         test_mkdir $DIR/$tdir
448         touch $DIR/$tdir/$tfile
449         rm $DIR/$tdir/$tfile
450         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
451 }
452 run_test 14 "touch .../d14/f; rm .../d14/f; rm .../d14/f ======="
453
454 test_15() {
455         test_mkdir $DIR/$tdir
456         touch $DIR/$tdir/$tfile
457         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}_2
458         $CHECKSTAT -t file $DIR/$tdir/${tfile}_2 ||
459                 error "$tdir/${tfile_2} not a file after rename"
460         rm $DIR/$tdir/${tfile}_2 || error "unlink failed after rename"
461 }
462 run_test 15 "touch .../d15/f; mv .../d15/f .../d15/f2 =========="
463
464 test_16() {
465         test_mkdir $DIR/$tdir
466         touch $DIR/$tdir/$tfile
467         rm -rf $DIR/$tdir/$tfile
468         $CHECKSTAT -a $DIR/$tdir/$tfile || error "$tdir/$tfile not removed"
469 }
470 run_test 16 "touch .../d16/f; rm -rf .../d16/f"
471
472 test_17a() {
473         test_mkdir $DIR/$tdir
474         touch $DIR/$tdir/$tfile
475         ln -s $DIR/$tdir/$tfile $DIR/$tdir/l-exist
476         ls -l $DIR/$tdir
477         $CHECKSTAT -l $DIR/$tdir/$tfile $DIR/$tdir/l-exist ||
478                 error "$tdir/l-exist not a symlink"
479         $CHECKSTAT -f -t f $DIR/$tdir/l-exist ||
480                 error "$tdir/l-exist not referencing a file"
481         rm -f $DIR/$tdir/l-exist
482         $CHECKSTAT -a $DIR/$tdir/l-exist || error "$tdir/l-exist not removed"
483 }
484 run_test 17a "symlinks: create, remove (real)"
485
486 test_17b() {
487         test_mkdir $DIR/$tdir
488         ln -s no-such-file $DIR/$tdir/l-dangle
489         ls -l $DIR/$tdir
490         $CHECKSTAT -l no-such-file $DIR/$tdir/l-dangle ||
491                 error "$tdir/l-dangle not referencing no-such-file"
492         $CHECKSTAT -fa $DIR/$tdir/l-dangle ||
493                 error "$tdir/l-dangle not referencing non-existent file"
494         rm -f $DIR/$tdir/l-dangle
495         $CHECKSTAT -a $DIR/$tdir/l-dangle || error "$tdir/l-dangle not removed"
496 }
497 run_test 17b "symlinks: create, remove (dangling)"
498
499 test_17c() { # bug 3440 - don't save failed open RPC for replay
500         test_mkdir $DIR/$tdir
501         ln -s foo $DIR/$tdir/$tfile
502         cat $DIR/$tdir/$tfile && error "opened non-existent symlink" || true
503 }
504 run_test 17c "symlinks: open dangling (should return error)"
505
506 test_17d() {
507         test_mkdir $DIR/$tdir
508         ln -s foo $DIR/$tdir/$tfile
509         touch $DIR/$tdir/$tfile || error "creating to new symlink"
510 }
511 run_test 17d "symlinks: create dangling"
512
513 test_17e() {
514         test_mkdir $DIR/$tdir
515         local foo=$DIR/$tdir/$tfile
516         ln -s $foo $foo || error "create symlink failed"
517         ls -l $foo || error "ls -l failed"
518         ls $foo && error "ls not failed" || true
519 }
520 run_test 17e "symlinks: create recursive symlink (should return error)"
521
522 test_17f() {
523         test_mkdir $DIR/$tdir
524         ln -s 1234567890/2234567890/3234567890/4234567890 $DIR/$tdir/111
525         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890 $DIR/$tdir/222
526         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890 $DIR/$tdir/333
527         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890 $DIR/$tdir/444
528         ln -s 1234567890/2234567890/3234567890/4234567890/5234567890/6234567890/7234567890/8234567890/9234567890/a234567890/b234567890/c234567890/d234567890/f234567890 $DIR/$tdir/555
529         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
530         ls -l  $DIR/$tdir
531 }
532 run_test 17f "symlinks: long and very long symlink name"
533
534 # str_repeat(S, N) generate a string that is string S repeated N times
535 str_repeat() {
536         local s=$1
537         local n=$2
538         local ret=''
539         while [ $((n -= 1)) -ge 0 ]; do
540                 ret=$ret$s
541         done
542         echo $ret
543 }
544
545 # Long symlinks and LU-2241
546 test_17g() {
547         test_mkdir $DIR/$tdir
548         local TESTS="59 60 61 4094 4095"
549
550         # Fix for inode size boundary in 2.1.4
551         [ $MDS1_VERSION -lt $(version_code 2.1.4) ] &&
552                 TESTS="4094 4095"
553
554         # Patch not applied to 2.2 or 2.3 branches
555         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
556         [ $MDS1_VERSION -le $(version_code 2.3.55) ] &&
557                 TESTS="4094 4095"
558
559         for i in $TESTS; do
560                 local SYMNAME=$(str_repeat 'x' $i)
561                 ln -s $SYMNAME $DIR/$tdir/f$i || error "failed $i-char symlink"
562                 readlink $DIR/$tdir/f$i || error "failed $i-char readlink"
563         done
564 }
565 run_test 17g "symlinks: really long symlink name and inode boundaries"
566
567 test_17h() { #bug 17378
568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
569         remote_mds_nodsh && skip "remote MDS with nodsh"
570
571         local mdt_idx
572
573         test_mkdir $DIR/$tdir
574         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
575         $LFS setstripe -c -1 $DIR/$tdir
576         #define OBD_FAIL_MDS_LOV_PREP_CREATE 0x141
577         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000141
578         touch $DIR/$tdir/$tfile || true
579 }
580 run_test 17h "create objects: lov_free_memmd() doesn't lbug"
581
582 test_17i() { #bug 20018
583         [ $PARALLEL == "yes" ] && skip "skip parallel run"
584         remote_mds_nodsh && skip "remote MDS with nodsh"
585
586         local foo=$DIR/$tdir/$tfile
587         local mdt_idx
588
589         test_mkdir -c1 $DIR/$tdir
590         mdt_idx=$($LFS getdirstripe -i $DIR/$tdir)
591         ln -s $foo $foo || error "create symlink failed"
592 #define OBD_FAIL_MDS_READLINK_EPROTO     0x143
593         do_facet mds$((mdt_idx + 1)) lctl set_param fail_loc=0x80000143
594         ls -l $foo && error "error not detected"
595         return 0
596 }
597 run_test 17i "don't panic on short symlink (should return error)"
598
599 test_17k() { #bug 22301
600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
601         [[ -z "$(which rsync 2>/dev/null)" ]] &&
602                 skip "no rsync command"
603         rsync --help | grep -q xattr ||
604                 skip_env "$(rsync --version | head -n1) does not support xattrs"
605         test_mkdir $DIR/$tdir
606         test_mkdir $DIR/$tdir.new
607         touch $DIR/$tdir/$tfile
608         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
609         rsync -av -X $DIR/$tdir/ $DIR/$tdir.new ||
610                 error "rsync failed with xattrs enabled"
611 }
612 run_test 17k "symlinks: rsync with xattrs enabled"
613
614 test_17l() { # LU-279
615         [[ -z "$(which getfattr 2>/dev/null)" ]] &&
616                 skip "no getfattr command"
617
618         test_mkdir $DIR/$tdir
619         touch $DIR/$tdir/$tfile
620         ln -s $DIR/$tdir/$tfile $DIR/$tdir/$tfile.lnk
621         for path in "$DIR/$tdir" "$DIR/$tdir/$tfile" "$DIR/$tdir/$tfile.lnk"; do
622                 # -h to not follow symlinks. -m '' to list all the xattrs.
623                 # grep to remove first line: '# file: $path'.
624                 for xattr in `getfattr -hm '' $path 2>/dev/null | grep -v '^#'`;
625                 do
626                         lgetxattr_size_check $path $xattr ||
627                                 error "lgetxattr_size_check $path $xattr failed"
628                 done
629         done
630 }
631 run_test 17l "Ensure lgetxattr's returned xattr size is consistent"
632
633 # LU-1540
634 test_17m() {
635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
636         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
637         remote_mds_nodsh && skip "remote MDS with nodsh"
638         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
639         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
640                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
641
642         local short_sym="0123456789"
643         local wdir=$DIR/$tdir
644         local i
645
646         test_mkdir $wdir
647         long_sym=$short_sym
648         # create a long symlink file
649         for ((i = 0; i < 4; ++i)); do
650                 long_sym=${long_sym}${long_sym}
651         done
652
653         echo "create 512 short and long symlink files under $wdir"
654         for ((i = 0; i < 256; ++i)); do
655                 ln -sf ${long_sym}"a5a5" $wdir/long-$i
656                 ln -sf ${short_sym}"a5a5" $wdir/short-$i
657         done
658
659         echo "erase them"
660         rm -f $wdir/*
661         sync
662         wait_delete_completed
663
664         echo "recreate the 512 symlink files with a shorter string"
665         for ((i = 0; i < 512; ++i)); do
666                 # rewrite the symlink file with a shorter string
667                 ln -sf ${long_sym} $wdir/long-$i || error "long_sym failed"
668                 ln -sf ${short_sym} $wdir/short-$i || error "short_sym failed"
669         done
670
671         local mds_index=$(($($LFS getstripe -m $wdir) + 1))
672         local devname=$(mdsdevname $mds_index)
673
674         echo "stop and checking mds${mds_index}:"
675         # e2fsck should not return error
676         stop mds${mds_index}
677         run_e2fsck $(facet_active_host mds${mds_index}) $devname -n
678         rc=$?
679
680         start mds${mds_index} $devname $MDS_MOUNT_OPTS ||
681                 error "start mds${mds_index} failed"
682         df $MOUNT > /dev/null 2>&1
683         [ $rc -eq 0 ] ||
684                 error "e2fsck detected error for short/long symlink: rc=$rc"
685         rm -f $wdir/*
686 }
687 run_test 17m "run e2fsck against MDT which contains short/long symlink"
688
689 check_fs_consistency_17n() {
690         local mdt_index
691         local rc=0
692
693         # create/unlink in 17n only change 2 MDTs(MDT1/MDT2),
694         # so it only check MDT1/MDT2 instead of all of MDTs.
695         for mdt_index in 1 2; do
696                 local devname=$(mdsdevname $mdt_index)
697                 # e2fsck should not return error
698                 stop mds${mdt_index}
699                 run_e2fsck $(facet_active_host mds$mdt_index) $devname -n ||
700                         rc=$((rc + $?))
701
702                 start mds${mdt_index} $devname $MDS_MOUNT_OPTS ||
703                         error "mount mds$mdt_index failed"
704                 df $MOUNT > /dev/null 2>&1
705         done
706         return $rc
707 }
708
709 test_17n() {
710         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
711         [ $PARALLEL == "yes" ] && skip "skip parallel run"
712         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
713         remote_mds_nodsh && skip "remote MDS with nodsh"
714         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] &&
715         [ $MDS1_VERSION -le $(version_code 2.2.93) ] &&
716                 skip "MDS 2.2.0-2.2.93 do not NUL-terminate symlinks"
717
718         local i
719
720         test_mkdir $DIR/$tdir
721         for ((i=0; i<10; i++)); do
722                 $LFS mkdir -i1 -c2 $DIR/$tdir/remote_dir_${i} ||
723                         error "create remote dir error $i"
724                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
725                         error "create files under remote dir failed $i"
726         done
727
728         check_fs_consistency_17n ||
729                 error "e2fsck report error after create files under remote dir"
730
731         for ((i = 0; i < 10; i++)); do
732                 rm -rf $DIR/$tdir/remote_dir_${i} ||
733                         error "destroy remote dir error $i"
734         done
735
736         check_fs_consistency_17n ||
737                 error "e2fsck report error after unlink files under remote dir"
738
739         [ $MDS1_VERSION -lt $(version_code 2.4.50) ] &&
740                 skip "lustre < 2.4.50 does not support migrate mv"
741
742         for ((i = 0; i < 10; i++)); do
743                 mkdir -p $DIR/$tdir/remote_dir_${i}
744                 createmany -o $DIR/$tdir/remote_dir_${i}/f 10 ||
745                         error "create files under remote dir failed $i"
746                 $LFS migrate --mdt-index 1 $DIR/$tdir/remote_dir_${i} ||
747                         error "migrate remote dir error $i"
748         done
749         check_fs_consistency_17n || error "e2fsck report error after migration"
750
751         for ((i = 0; i < 10; i++)); do
752                 rm -rf $DIR/$tdir/remote_dir_${i} ||
753                         error "destroy remote dir error $i"
754         done
755
756         check_fs_consistency_17n || error "e2fsck report error after unlink"
757 }
758 run_test 17n "run e2fsck against master/slave MDT which contains remote dir"
759
760 test_17o() {
761         remote_mds_nodsh && skip "remote MDS with nodsh"
762         [ $MDS1_VERSION -lt $(version_code 2.3.64) ] &&
763                 skip "Need MDS version at least 2.3.64"
764
765         local wdir=$DIR/${tdir}o
766         local mdt_index
767         local rc=0
768
769         test_mkdir $wdir
770         touch $wdir/$tfile
771         mdt_index=$($LFS getstripe -m $wdir/$tfile)
772         mdt_index=$((mdt_index + 1))
773
774         cancel_lru_locks mdc
775         #fail mds will wait the failover finish then set
776         #following fail_loc to avoid interfer the recovery process.
777         fail mds${mdt_index}
778
779         #define OBD_FAIL_OSD_LMA_INCOMPAT 0x194
780         do_facet mds${mdt_index} lctl set_param fail_loc=0x194
781         ls -l $wdir/$tfile && rc=1
782         do_facet mds${mdt_index} lctl set_param fail_loc=0
783         [[ $rc -eq 0 ]] || error "stat file should fail"
784 }
785 run_test 17o "stat file with incompat LMA feature"
786
787 test_18() {
788         touch $DIR/$tfile || error "Failed to touch $DIR/$tfile: $?"
789         ls $DIR || error "Failed to ls $DIR: $?"
790 }
791 run_test 18 "touch .../f ; ls ... =============================="
792
793 test_19a() {
794         touch $DIR/$tfile
795         ls -l $DIR
796         rm $DIR/$tfile
797         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
798 }
799 run_test 19a "touch .../f19 ; ls -l ... ; rm .../f19 ==========="
800
801 test_19b() {
802         ls -l $DIR/$tfile && error "ls -l $tfile failed"|| true
803 }
804 run_test 19b "ls -l .../f19 (should return error) =============="
805
806 test_19c() {
807         [ $RUNAS_ID -eq $UID ] &&
808                 skip_env "RUNAS_ID = UID = $UID -- skipping"
809
810         $RUNAS touch $DIR/$tfile && error "create non-root file failed" || true
811 }
812 run_test 19c "$RUNAS touch .../f19 (should return error) =="
813
814 test_19d() {
815         cat $DIR/f19 && error || true
816 }
817 run_test 19d "cat .../f19 (should return error) =============="
818
819 test_20() {
820         touch $DIR/$tfile
821         rm $DIR/$tfile
822         touch $DIR/$tfile
823         rm $DIR/$tfile
824         touch $DIR/$tfile
825         rm $DIR/$tfile
826         $CHECKSTAT -a $DIR/$tfile || error "$tfile was not removed"
827 }
828 run_test 20 "touch .../f ; ls -l ..."
829
830 test_21() {
831         test_mkdir $DIR/$tdir
832         [ -f $DIR/$tdir/dangle ] && rm -f $DIR/$tdir/dangle
833         ln -s dangle $DIR/$tdir/link
834         echo foo >> $DIR/$tdir/link
835         cat $DIR/$tdir/dangle
836         $CHECKSTAT -t link $DIR/$tdir/link || error "$tdir/link not a link"
837         $CHECKSTAT -f -t file $DIR/$tdir/link ||
838                 error "$tdir/link not linked to a file"
839 }
840 run_test 21 "write to dangling link"
841
842 test_22() {
843         local wdir=$DIR/$tdir
844         test_mkdir $wdir
845         chown $RUNAS_ID:$RUNAS_GID $wdir
846         (cd $wdir || error "cd $wdir failed";
847                 $RUNAS tar cf - /etc/hosts /etc/sysconfig/network |
848                 $RUNAS tar xf -)
849         ls -lR $wdir/etc || error "ls -lR $wdir/etc failed"
850         $CHECKSTAT -t dir $wdir/etc || error "checkstat -t dir failed"
851         $CHECKSTAT -u \#$RUNAS_ID -g \#$RUNAS_GID $wdir/etc ||
852                 error "checkstat -u failed"
853 }
854 run_test 22 "unpack tar archive as non-root user"
855
856 # was test_23
857 test_23a() {
858         test_mkdir $DIR/$tdir
859         local file=$DIR/$tdir/$tfile
860
861         openfile -f O_CREAT:O_EXCL $file || error "$file create failed"
862         openfile -f O_CREAT:O_EXCL $file &&
863                 error "$file recreate succeeded" || true
864 }
865 run_test 23a "O_CREAT|O_EXCL in subdir"
866
867 test_23b() { # bug 18988
868         test_mkdir $DIR/$tdir
869         local file=$DIR/$tdir/$tfile
870
871         rm -f $file
872         echo foo > $file || error "write filed"
873         echo bar >> $file || error "append filed"
874         $CHECKSTAT -s 8 $file || error "wrong size"
875         rm $file
876 }
877 run_test 23b "O_APPEND check"
878
879 # LU-9409, size with O_APPEND and tiny writes
880 test_23c() {
881         local file=$DIR/$tfile
882
883         # single dd
884         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800
885         $CHECKSTAT -s 6400 $file || error "wrong size, expected 6400"
886         rm -f $file
887
888         # racing tiny writes
889         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
890         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=800 &
891         wait
892         $CHECKSTAT -s 12800 $file || error "wrong size, expected 12800"
893         rm -f $file
894
895         #racing tiny & normal writes
896         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4096 count=4 &
897         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=8 count=100 &
898         wait
899         $CHECKSTAT -s 17184 $file || error "wrong size, expected 17184"
900         rm -f $file
901
902         #racing tiny & normal writes 2, ugly numbers
903         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=4099 count=11 &
904         dd conv=notrunc oflag=append if=/dev/zero of=$file bs=17 count=173 &
905         wait
906         $CHECKSTAT -s 48030 $file || error "wrong size, expected 48030"
907         rm -f $file
908 }
909 run_test 23c "O_APPEND size checks for tiny writes"
910
911 # LU-11069 file offset is correct after appending writes
912 test_23d() {
913         local file=$DIR/$tfile
914         local offset
915
916         echo CentaurHauls > $file
917         offset=$($MULTIOP $file oO_WRONLY:O_APPEND:w13Zp)
918         if ((offset != 26)); then
919                 error "wrong offset, expected 26, got '$offset'"
920         fi
921 }
922 run_test 23d "file offset is correct after appending writes"
923
924 # rename sanity
925 test_24a() {
926         echo '-- same directory rename'
927         test_mkdir $DIR/$tdir
928         touch $DIR/$tdir/$tfile.1
929         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
930         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
931 }
932 run_test 24a "rename file to non-existent target"
933
934 test_24b() {
935         test_mkdir $DIR/$tdir
936         touch $DIR/$tdir/$tfile.{1,2}
937         mv $DIR/$tdir/$tfile.1 $DIR/$tdir/$tfile.2
938         $CHECKSTAT -a $DIR/$tdir/$tfile.1 || error "$tfile.1 exists"
939         $CHECKSTAT -t file $DIR/$tdir/$tfile.2 || error "$tfile.2 not a file"
940 }
941 run_test 24b "rename file to existing target"
942
943 test_24c() {
944         test_mkdir $DIR/$tdir
945         test_mkdir $DIR/$tdir/d$testnum.1
946         mv $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
947         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
948         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
949 }
950 run_test 24c "rename directory to non-existent target"
951
952 test_24d() {
953         test_mkdir -c1 $DIR/$tdir
954         test_mkdir -c1 $DIR/$tdir/d$testnum.1
955         test_mkdir -c1 $DIR/$tdir/d$testnum.2
956         mrename $DIR/$tdir/d$testnum.1 $DIR/$tdir/d$testnum.2
957         $CHECKSTAT -a $DIR/$tdir/d$testnum.1 || error "d$testnum.1 exists"
958         $CHECKSTAT -t dir $DIR/$tdir/d$testnum.2 || error "d$testnum.2 not dir"
959 }
960 run_test 24d "rename directory to existing target"
961
962 test_24e() {
963         echo '-- cross directory renames --'
964         test_mkdir $DIR/R5a
965         test_mkdir $DIR/R5b
966         touch $DIR/R5a/f
967         mv $DIR/R5a/f $DIR/R5b/g
968         $CHECKSTAT -a $DIR/R5a/f || error "$DIR/R5a/f exists"
969         $CHECKSTAT -t file $DIR/R5b/g || error "$DIR/R5b/g not file type"
970 }
971 run_test 24e "touch .../R5a/f; rename .../R5a/f .../R5b/g ======"
972
973 test_24f() {
974         test_mkdir $DIR/R6a
975         test_mkdir $DIR/R6b
976         touch $DIR/R6a/f $DIR/R6b/g
977         mv $DIR/R6a/f $DIR/R6b/g
978         $CHECKSTAT -a $DIR/R6a/f || error "$DIR/R6a/f exists"
979         $CHECKSTAT -t file $DIR/R6b/g || error "$DIR/R6b/g not file type"
980 }
981 run_test 24f "touch .../R6a/f R6b/g; mv .../R6a/f .../R6b/g ===="
982
983 test_24g() {
984         test_mkdir $DIR/R7a
985         test_mkdir $DIR/R7b
986         test_mkdir $DIR/R7a/d
987         mv $DIR/R7a/d $DIR/R7b/e
988         $CHECKSTAT -a $DIR/R7a/d || error "$DIR/R7a/d exists"
989         $CHECKSTAT -t dir $DIR/R7b/e || error "$DIR/R7b/e not dir type"
990 }
991 run_test 24g "mkdir .../R7{a,b}/d; mv .../R7a/d .../R7b/e ======"
992
993 test_24h() {
994         test_mkdir -c1 $DIR/R8a
995         test_mkdir -c1 $DIR/R8b
996         test_mkdir -c1 $DIR/R8a/d
997         test_mkdir -c1 $DIR/R8b/e
998         mrename $DIR/R8a/d $DIR/R8b/e
999         $CHECKSTAT -a $DIR/R8a/d || error "$DIR/R8a/d exists"
1000         $CHECKSTAT -t dir $DIR/R8b/e || error "$DIR/R8b/e not dir type"
1001 }
1002 run_test 24h "mkdir .../R8{a,b}/{d,e}; rename .../R8a/d .../R8b/e"
1003
1004 test_24i() {
1005         echo "-- rename error cases"
1006         test_mkdir $DIR/R9
1007         test_mkdir $DIR/R9/a
1008         touch $DIR/R9/f
1009         mrename $DIR/R9/f $DIR/R9/a
1010         $CHECKSTAT -t file $DIR/R9/f || error "$DIR/R9/f not file type"
1011         $CHECKSTAT -t dir  $DIR/R9/a || error "$DIR/R9/a not dir type"
1012         $CHECKSTAT -a $DIR/R9/a/f || error "$DIR/R9/a/f exists"
1013 }
1014 run_test 24i "rename file to dir error: touch f ; mkdir a ; rename f a"
1015
1016 test_24j() {
1017         test_mkdir $DIR/R10
1018         mrename $DIR/R10/f $DIR/R10/g
1019         $CHECKSTAT -t dir $DIR/R10 || error "$DIR/R10 not dir type"
1020         $CHECKSTAT -a $DIR/R10/f || error "$DIR/R10/f exists"
1021         $CHECKSTAT -a $DIR/R10/g || error "$DIR/R10/g exists"
1022 }
1023 run_test 24j "source does not exist ============================"
1024
1025 test_24k() {
1026         test_mkdir $DIR/R11a
1027         test_mkdir $DIR/R11a/d
1028         touch $DIR/R11a/f
1029         mv $DIR/R11a/f $DIR/R11a/d
1030         $CHECKSTAT -a $DIR/R11a/f || error "$DIR/R11a/f exists"
1031         $CHECKSTAT -t file $DIR/R11a/d/f || error "$DIR/R11a/d/f not file type"
1032 }
1033 run_test 24k "touch .../R11a/f; mv .../R11a/f .../R11a/d ======="
1034
1035 # bug 2429 - rename foo foo foo creates invalid file
1036 test_24l() {
1037         f="$DIR/f24l"
1038         $MULTIOP $f OcNs || error "rename of ${f} to itself failed"
1039 }
1040 run_test 24l "Renaming a file to itself ========================"
1041
1042 test_24m() {
1043         f="$DIR/f24m"
1044         $MULTIOP $f OcLN ${f}2 ${f}2 || error "link ${f}2 ${f}2 failed"
1045         # on ext3 this does not remove either the source or target files
1046         # though the "expected" operation would be to remove the source
1047         $CHECKSTAT -t file ${f} || error "${f} missing"
1048         $CHECKSTAT -t file ${f}2 || error "${f}2 missing"
1049 }
1050 run_test 24m "Renaming a file to a hard link to itself ========="
1051
1052 test_24n() {
1053     f="$DIR/f24n"
1054     # this stats the old file after it was renamed, so it should fail
1055     touch ${f}
1056     $CHECKSTAT ${f} || error "${f} missing"
1057     mv ${f} ${f}.rename
1058     $CHECKSTAT ${f}.rename || error "${f}.rename missing"
1059     $CHECKSTAT -a ${f} || error "${f} exists"
1060 }
1061 run_test 24n "Statting the old file after renaming (Posix rename 2)"
1062
1063 test_24o() {
1064         test_mkdir $DIR/$tdir
1065         rename_many -s random -v -n 10 $DIR/$tdir
1066 }
1067 run_test 24o "rename of files during htree split"
1068
1069 test_24p() {
1070         test_mkdir $DIR/R12a
1071         test_mkdir $DIR/R12b
1072         DIRINO=`ls -lid $DIR/R12a | awk '{ print $1 }'`
1073         mrename $DIR/R12a $DIR/R12b
1074         $CHECKSTAT -a $DIR/R12a || error "$DIR/R12a exists"
1075         $CHECKSTAT -t dir $DIR/R12b || error "$DIR/R12b not dir type"
1076         DIRINO2=`ls -lid $DIR/R12b | awk '{ print $1 }'`
1077         [ "$DIRINO" = "$DIRINO2" ] || error "R12a $DIRINO != R12b $DIRINO2"
1078 }
1079 run_test 24p "mkdir .../R12{a,b}; rename .../R12a .../R12b"
1080
1081 cleanup_multiop_pause() {
1082         trap 0
1083         kill -USR1 $MULTIPID
1084 }
1085
1086 test_24q() {
1087         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1088
1089         test_mkdir $DIR/R13a
1090         test_mkdir $DIR/R13b
1091         local DIRINO=$(ls -lid $DIR/R13a | awk '{ print $1 }')
1092         multiop_bg_pause $DIR/R13b D_c || error "multiop failed to start"
1093         MULTIPID=$!
1094
1095         trap cleanup_multiop_pause EXIT
1096         mrename $DIR/R13a $DIR/R13b
1097         $CHECKSTAT -a $DIR/R13a || error "R13a still exists"
1098         $CHECKSTAT -t dir $DIR/R13b || error "R13b does not exist"
1099         local DIRINO2=$(ls -lid $DIR/R13b | awk '{ print $1 }')
1100         [ "$DIRINO" = "$DIRINO2" ] || error "R13a $DIRINO != R13b $DIRINO2"
1101         cleanup_multiop_pause
1102         wait $MULTIPID || error "multiop close failed"
1103 }
1104 run_test 24q "mkdir .../R13{a,b}; open R13b rename R13a R13b ==="
1105
1106 test_24r() { #bug 3789
1107         test_mkdir $DIR/R14a
1108         test_mkdir $DIR/R14a/b
1109         mrename $DIR/R14a $DIR/R14a/b && error "rename to subdir worked!"
1110         $CHECKSTAT -t dir $DIR/R14a || error "$DIR/R14a missing"
1111         $CHECKSTAT -t dir $DIR/R14a/b || error "$DIR/R14a/b missing"
1112 }
1113 run_test 24r "mkdir .../R14a/b; rename .../R14a .../R14a/b ====="
1114
1115 test_24s() {
1116         test_mkdir $DIR/R15a
1117         test_mkdir $DIR/R15a/b
1118         test_mkdir $DIR/R15a/b/c
1119         mrename $DIR/R15a $DIR/R15a/b/c && error "rename to sub-subdir worked!"
1120         $CHECKSTAT -t dir $DIR/R15a || error "$DIR/R15a missing"
1121         $CHECKSTAT -t dir $DIR/R15a/b/c || error "$DIR/R15a/b/c missing"
1122 }
1123 run_test 24s "mkdir .../R15a/b/c; rename .../R15a .../R15a/b/c ="
1124 test_24t() {
1125         test_mkdir $DIR/R16a
1126         test_mkdir $DIR/R16a/b
1127         test_mkdir $DIR/R16a/b/c
1128         mrename $DIR/R16a/b/c $DIR/R16a && error "rename to sub-subdir worked!"
1129         $CHECKSTAT -t dir $DIR/R16a || error "$DIR/R16a missing"
1130         $CHECKSTAT -t dir $DIR/R16a/b/c || error "$DIR/R16a/b/c missing"
1131 }
1132 run_test 24t "mkdir .../R16a/b/c; rename .../R16a/b/c .../R16a ="
1133
1134 test_24u() { # bug12192
1135         $MULTIOP $DIR/$tfile C2w$((2048 * 1024))c || error "multiop failed"
1136         $CHECKSTAT -s $((2048 * 1024)) $DIR/$tfile || error "wrong file size"
1137 }
1138 run_test 24u "create stripe file"
1139
1140 simple_cleanup_common() {
1141         local rc=0
1142         trap 0
1143         [ -z "$DIR" ] || [ -z "$tdir" ] && return 0
1144
1145         local start=$SECONDS
1146         rm -rf $DIR/$tdir
1147         rc=$?
1148         wait_delete_completed
1149         echo "cleanup time $((SECONDS - start))"
1150         return $rc
1151 }
1152
1153 max_pages_per_rpc() {
1154         local mdtname="$(printf "MDT%04x" ${1:-0})"
1155         $LCTL get_param -n mdc.*$mdtname*.max_pages_per_rpc
1156 }
1157
1158 test_24v() {
1159         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1160
1161         local nrfiles=${COUNT:-100000}
1162         local fname="$DIR/$tdir/$tfile"
1163
1164         # Performance issue on ZFS see LU-4072 (c.f. LU-2887)
1165         [ "$mds1_FSTYPE" = "zfs" ] && nrfiles=${COUNT:-10000}
1166
1167         test_mkdir "$(dirname $fname)"
1168         # assume MDT0000 has the fewest inodes
1169         local stripes=$($LFS getdirstripe -c $(dirname $fname))
1170         local free_inodes=$(($(mdt_free_inodes 0) * stripes))
1171         [[ $free_inodes -lt $nrfiles ]] && nrfiles=$free_inodes
1172
1173         trap simple_cleanup_common EXIT
1174
1175         createmany -m "$fname" $nrfiles
1176
1177         cancel_lru_locks mdc
1178         lctl set_param mdc.*.stats clear
1179
1180         # was previously test_24D: LU-6101
1181         # readdir() returns correct number of entries after cursor reload
1182         local num_ls=$(ls $DIR/$tdir | wc -l)
1183         local num_uniq=$(ls $DIR/$tdir | sort -u | wc -l)
1184         local num_all=$(ls -a $DIR/$tdir | wc -l)
1185         if [ $num_ls -ne $nrfiles ] || [ $num_uniq -ne $nrfiles ] ||
1186                 [ $num_all -ne $((nrfiles + 2)) ]; then
1187                         error "Expected $nrfiles files, got $num_ls " \
1188                                 "($num_uniq unique $num_all .&..)"
1189         fi
1190         # LU-5 large readdir
1191         # dirent_size = 32 bytes for sizeof(struct lu_dirent) +
1192         #               N bytes for name (len($nrfiles) rounded to 8 bytes) +
1193         #               8 bytes for luda_type (4 bytes rounded to 8 bytes)
1194         # take into account of overhead in lu_dirpage header and end mark in
1195         # each page, plus one in rpc_num calculation.
1196         local dirent_size=$((32 + (${#tfile} | 7) + 1 + 8))
1197         local page_entries=$(((PAGE_SIZE - 24) / dirent_size))
1198         local mdt_idx=$($LFS getdirstripe -i $(dirname $fname))
1199         local rpc_pages=$(max_pages_per_rpc $mdt_idx)
1200         local rpc_max=$((nrfiles / (page_entries * rpc_pages) + stripes))
1201         local mds_readpage=$(calc_stats mdc.*.stats mds_readpage)
1202         echo "readpages: $mds_readpage rpc_max: $rpc_max"
1203         (( $mds_readpage < $rpc_max - 2 || $mds_readpage > $rpc_max + 1)) &&
1204                 error "large readdir doesn't take effect: " \
1205                       "$mds_readpage should be about $rpc_max"
1206
1207         simple_cleanup_common
1208 }
1209 run_test 24v "list large directory (test hash collision, b=17560)"
1210
1211 test_24w() { # bug21506
1212         SZ1=234852
1213         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=4096 || return 1
1214         dd if=/dev/zero bs=$SZ1 count=1 >> $DIR/$tfile || return 2
1215         dd if=$DIR/$tfile of=$DIR/${tfile}_left bs=1M skip=4097 || return 3
1216         SZ2=`ls -l $DIR/${tfile}_left | awk '{print $5}'`
1217         [[ "$SZ1" -eq "$SZ2" ]] ||
1218                 error "Error reading at the end of the file $tfile"
1219 }
1220 run_test 24w "Reading a file larger than 4Gb"
1221
1222 test_24x() {
1223         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1225         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1226                 skip "Need MDS version at least 2.7.56"
1227
1228         local MDTIDX=1
1229         local remote_dir=$DIR/$tdir/remote_dir
1230
1231         test_mkdir $DIR/$tdir
1232         $LFS mkdir -i $MDTIDX $remote_dir ||
1233                 error "create remote directory failed"
1234
1235         test_mkdir $DIR/$tdir/src_dir
1236         touch $DIR/$tdir/src_file
1237         test_mkdir $remote_dir/tgt_dir
1238         touch $remote_dir/tgt_file
1239
1240         mrename $DIR/$tdir/src_dir $remote_dir/tgt_dir ||
1241                 error "rename dir cross MDT failed!"
1242
1243         mrename $DIR/$tdir/src_file $remote_dir/tgt_file ||
1244                 error "rename file cross MDT failed!"
1245
1246         touch $DIR/$tdir/ln_file
1247         ln $DIR/$tdir/ln_file $remote_dir/ln_name ||
1248                 error "ln file cross MDT failed"
1249
1250         rm -rf $DIR/$tdir || error "Can not delete directories"
1251 }
1252 run_test 24x "cross MDT rename/link"
1253
1254 test_24y() {
1255         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1257
1258         local remote_dir=$DIR/$tdir/remote_dir
1259         local mdtidx=1
1260
1261         test_mkdir $DIR/$tdir
1262         $LFS mkdir -i $mdtidx $remote_dir ||
1263                 error "create remote directory failed"
1264
1265         test_mkdir $remote_dir/src_dir
1266         touch $remote_dir/src_file
1267         test_mkdir $remote_dir/tgt_dir
1268         touch $remote_dir/tgt_file
1269
1270         mrename $remote_dir/src_dir $remote_dir/tgt_dir ||
1271                 error "rename subdir in the same remote dir failed!"
1272
1273         mrename $remote_dir/src_file $remote_dir/tgt_file ||
1274                 error "rename files in the same remote dir failed!"
1275
1276         ln $remote_dir/tgt_file $remote_dir/tgt_file1 ||
1277                 error "link files in the same remote dir failed!"
1278
1279         rm -rf $DIR/$tdir || error "Can not delete directories"
1280 }
1281 run_test 24y "rename/link on the same dir should succeed"
1282
1283 test_24z() {
1284         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
1285         [[ $MDS1_VERSION -lt $(version_code 2.12.51) ]] &&
1286                 skip "Need MDS version at least 2.12.51"
1287
1288         local index
1289
1290         for index in 0 1; do
1291                 $LFS mkdir -i $index $DIR/$tdir.$index || error "mkdir failed"
1292                 touch $DIR/$tdir.0/$tfile.$index || error "touch failed"
1293         done
1294
1295         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1 || error "mv $tfile.0 failed"
1296
1297         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.0)
1298         [ $index -eq 0 ] || error "$tfile.0 is on MDT$index"
1299
1300         local mdts=$(comma_list $(mdts_nodes))
1301
1302         do_nodes $mdts $LCTL set_param mdt.*.enable_remote_rename=0
1303         stack_trap "do_nodes $mdts $LCTL \
1304                 set_param mdt.*.enable_remote_rename=1" EXIT
1305
1306         mv $DIR/$tdir.0/$tfile.1 $DIR/$tdir.1 || error "mv $tfile.1 failed"
1307
1308         index=$($LFS getstripe -m $DIR/$tdir.1/$tfile.1)
1309         [ $index -eq 1 ] || error "$tfile.1 is on MDT$index"
1310 }
1311 run_test 24z "cross-MDT rename is done as cp"
1312
1313 test_24A() { # LU-3182
1314         local NFILES=5000
1315
1316         rm -rf $DIR/$tdir
1317         test_mkdir $DIR/$tdir
1318         trap simple_cleanup_common EXIT
1319         createmany -m $DIR/$tdir/$tfile $NFILES
1320         local t=$(ls $DIR/$tdir | wc -l)
1321         local u=$(ls $DIR/$tdir | sort -u | wc -l)
1322         local v=$(ls -ai $DIR/$tdir | sort -u | wc -l)
1323         if [ $t -ne $NFILES ] || [ $u -ne $NFILES ] ||
1324            [ $v -ne $((NFILES + 2)) ] ; then
1325                 error "Expected $NFILES files, got $t ($u unique $v .&..)"
1326         fi
1327
1328         simple_cleanup_common || error "Can not delete directories"
1329 }
1330 run_test 24A "readdir() returns correct number of entries."
1331
1332 test_24B() { # LU-4805
1333         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1334
1335         local count
1336
1337         test_mkdir $DIR/$tdir
1338         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
1339                 error "create striped dir failed"
1340
1341         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1342         [ $count -eq 2 ] || error "Expected 2, got $count"
1343
1344         touch $DIR/$tdir/striped_dir/a
1345
1346         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1347         [ $count -eq 3 ] || error "Expected 3, got $count"
1348
1349         touch $DIR/$tdir/striped_dir/.f
1350
1351         count=$(ls -ai $DIR/$tdir/striped_dir | wc -l)
1352         [ $count -eq 4 ] || error "Expected 4, got $count"
1353
1354         rm -rf $DIR/$tdir || error "Can not delete directories"
1355 }
1356 run_test 24B "readdir for striped dir return correct number of entries"
1357
1358 test_24C() {
1359         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
1360
1361         mkdir $DIR/$tdir
1362         mkdir $DIR/$tdir/d0
1363         mkdir $DIR/$tdir/d1
1364
1365         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/d0/striped_dir ||
1366                 error "create striped dir failed"
1367
1368         cd $DIR/$tdir/d0/striped_dir
1369
1370         local d0_ino=$(ls -i -l -a $DIR/$tdir | grep "d0" | awk '{print $1}')
1371         local d1_ino=$(ls -i -l -a $DIR/$tdir | grep "d1" | awk '{print $1}')
1372         local parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1373
1374         [ "$d0_ino" = "$parent_ino" ] ||
1375                 error ".. wrong, expect $d0_ino, get $parent_ino"
1376
1377         mv $DIR/$tdir/d0/striped_dir $DIR/$tdir/d1/ ||
1378                 error "mv striped dir failed"
1379
1380         parent_ino=$(ls -i -l -a | grep "\.\." | awk '{print $1}')
1381
1382         [ "$d1_ino" = "$parent_ino" ] ||
1383                 error ".. wrong after mv, expect $d1_ino, get $parent_ino"
1384 }
1385 run_test 24C "check .. in striped dir"
1386
1387 test_24E() {
1388         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
1389         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1390
1391         mkdir -p $DIR/$tdir
1392         mkdir $DIR/$tdir/src_dir
1393         $LFS mkdir -i 1 $DIR/$tdir/src_dir/src_child ||
1394                 error "create remote source failed"
1395
1396         touch $DIR/$tdir/src_dir/src_child/a
1397
1398         $LFS mkdir -i 2 $DIR/$tdir/tgt_dir ||
1399                 error "create remote target dir failed"
1400
1401         $LFS mkdir -i 3 $DIR/$tdir/tgt_dir/tgt_child ||
1402                 error "create remote target child failed"
1403
1404         mrename $DIR/$tdir/src_dir/src_child $DIR/$tdir/tgt_dir/tgt_child ||
1405                 error "rename dir cross MDT failed!"
1406
1407         find $DIR/$tdir
1408
1409         $CHECKSTAT -t dir $DIR/$tdir/src_dir/src_child &&
1410                 error "src_child still exists after rename"
1411
1412         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/tgt_child/a ||
1413                 error "missing file(a) after rename"
1414
1415         rm -rf $DIR/$tdir || error "Can not delete directories"
1416 }
1417 run_test 24E "cross MDT rename/link"
1418
1419 test_24F () {
1420         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
1421
1422         local repeats=1000
1423         [ "$SLOW" = "no" ] && repeats=100
1424
1425         mkdir -p $DIR/$tdir
1426
1427         echo "$repeats repeats"
1428         for ((i = 0; i < repeats; i++)); do
1429                 $LFS mkdir -i0 -c2 $DIR/$tdir/test || error "mkdir fails"
1430                 touch $DIR/$tdir/test/a || error "touch fails"
1431                 mkdir $DIR/$tdir/test/b || error "mkdir fails"
1432                 rm -rf $DIR/$tdir/test || error "rmdir fails"
1433         done
1434
1435         true
1436 }
1437 run_test 24F "hash order vs readdir (LU-11330)"
1438
1439 test_25a() {
1440         echo '== symlink sanity ============================================='
1441
1442         test_mkdir $DIR/d25
1443         ln -s d25 $DIR/s25
1444         touch $DIR/s25/foo ||
1445                 error "File creation in symlinked directory failed"
1446 }
1447 run_test 25a "create file in symlinked directory ==============="
1448
1449 test_25b() {
1450         [ ! -d $DIR/d25 ] && test_25a
1451         $CHECKSTAT -t file $DIR/s25/foo || error "$DIR/s25/foo not file type"
1452 }
1453 run_test 25b "lookup file in symlinked directory ==============="
1454
1455 test_26a() {
1456         test_mkdir $DIR/d26
1457         test_mkdir $DIR/d26/d26-2
1458         ln -s d26/d26-2 $DIR/s26
1459         touch $DIR/s26/foo || error "File creation failed"
1460 }
1461 run_test 26a "multiple component symlink ======================="
1462
1463 test_26b() {
1464         test_mkdir -p $DIR/$tdir/d26-2
1465         ln -s $tdir/d26-2/foo $DIR/s26-2
1466         touch $DIR/s26-2 || error "File creation failed"
1467 }
1468 run_test 26b "multiple component symlink at end of lookup ======"
1469
1470 test_26c() {
1471         test_mkdir $DIR/d26.2
1472         touch $DIR/d26.2/foo
1473         ln -s d26.2 $DIR/s26.2-1
1474         ln -s s26.2-1 $DIR/s26.2-2
1475         ln -s s26.2-2 $DIR/s26.2-3
1476         chmod 0666 $DIR/s26.2-3/foo
1477 }
1478 run_test 26c "chain of symlinks"
1479
1480 # recursive symlinks (bug 439)
1481 test_26d() {
1482         ln -s d26-3/foo $DIR/d26-3
1483 }
1484 run_test 26d "create multiple component recursive symlink"
1485
1486 test_26e() {
1487         [ ! -h $DIR/d26-3 ] && test_26d
1488         rm $DIR/d26-3
1489 }
1490 run_test 26e "unlink multiple component recursive symlink"
1491
1492 # recursive symlinks (bug 7022)
1493 test_26f() {
1494         test_mkdir $DIR/$tdir
1495         test_mkdir $DIR/$tdir/$tfile
1496         cd $DIR/$tdir/$tfile           || error "cd $DIR/$tdir/$tfile failed"
1497         test_mkdir -p lndir/bar1
1498         test_mkdir $DIR/$tdir/$tfile/$tfile
1499         cd $tfile                || error "cd $tfile failed"
1500         ln -s .. dotdot          || error "ln dotdot failed"
1501         ln -s dotdot/lndir lndir || error "ln lndir failed"
1502         cd $DIR/$tdir                 || error "cd $DIR/$tdir failed"
1503         output=`ls $tfile/$tfile/lndir/bar1`
1504         [ "$output" = bar1 ] && error "unexpected output"
1505         rm -r $tfile             || error "rm $tfile failed"
1506         $CHECKSTAT -a $DIR/$tfile || error "$tfile not gone"
1507 }
1508 run_test 26f "rm -r of a directory which has recursive symlink"
1509
1510 test_27a() {
1511         test_mkdir $DIR/$tdir
1512         $LFS getstripe $DIR/$tdir
1513         $LFS setstripe -c 1 $DIR/$tdir/$tfile || error "setstripe failed"
1514         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1515         cp /etc/hosts $DIR/$tdir/$tfile || error "Can't copy to one stripe file"
1516 }
1517 run_test 27a "one stripe file"
1518
1519 test_27b() {
1520         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1521
1522         test_mkdir $DIR/$tdir
1523         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1524         $LFS getstripe -c $DIR/$tdir/$tfile
1525         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
1526                 error "two-stripe file doesn't have two stripes"
1527
1528         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1529 }
1530 run_test 27b "create and write to two stripe file"
1531
1532 # 27c family tests specific striping, setstripe -o
1533 test_27ca() {
1534         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1535         test_mkdir -p $DIR/$tdir
1536         local osts="1"
1537
1538         $LFS setstripe -o $osts $DIR/$tdir/$tfile  || error "setstripe failed"
1539         $LFS getstripe -i $DIR/$tdir/$tfile
1540         [ $($LFS getstripe -i $DIR/$tdir/$tfile ) -eq $osts ] ||
1541                 error "stripe not on specified OST"
1542
1543         dd if=/dev/zero of=$DIR/$tdir/$tfile  bs=1M count=4 || error "dd failed"
1544 }
1545 run_test 27ca "one stripe on specified OST"
1546
1547 test_27cb() {
1548         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1549         test_mkdir -p $DIR/$tdir
1550         local osts="1,0"
1551         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1552         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1553         echo "$getstripe"
1554
1555         # Strip getstripe output to a space separated list of OSTs
1556         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1557                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1558         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1559                 error "stripes not on specified OSTs"
1560
1561         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1562 }
1563 run_test 27cb "two stripes on specified OSTs"
1564
1565 test_27cc() {
1566         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1567         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1568                 skip "server does not support overstriping"
1569
1570         test_mkdir -p $DIR/$tdir
1571         local osts="0,0"
1572         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1573         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1574         echo "$getstripe"
1575
1576         # Strip getstripe output to a space separated list of OSTs
1577         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1578                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1579         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1580                 error "stripes not on specified OSTs"
1581
1582         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1583 }
1584 run_test 27cc "two stripes on the same OST"
1585
1586 test_27cd() {
1587         [[ $OSTCOUNT -lt 2 ]] && skip_env "skipping 2-stripe test"
1588         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1589                 skip "server does not support overstriping"
1590         test_mkdir -p $DIR/$tdir
1591         local osts="0,1,1,0"
1592         $LFS setstripe -o $osts $DIR/$tdir/$tfile || error "setstripe failed"
1593         local getstripe=$($LFS getstripe $DIR/$tdir/$tfile)
1594         echo "$getstripe"
1595
1596         # Strip getstripe output to a space separated list of OSTs
1597         local getstripe_osts=$(echo "$getstripe" | sed -e '1,/obdidx/d' |\
1598                 awk '{print $1}' | tr '\n' '\ ' | sed -e 's/[[:space:]]*$//')
1599         [ "$getstripe_osts" = "${osts//,/ }" ] ||
1600                 error "stripes not on specified OSTs"
1601
1602         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 || error "dd failed"
1603 }
1604 run_test 27cd "four stripes on two OSTs"
1605
1606 test_27ce() {
1607         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
1608                 skip_env "too many osts, skipping"
1609         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
1610                 skip "server does not support overstriping"
1611         # We do one more stripe than we have OSTs
1612         [ $OSTCOUNT -ge 159 ] || large_xattr_enabled ||
1613                 skip_env "ea_inode feature disabled"
1614
1615         test_mkdir -p $DIR/$tdir
1616         local osts=""
1617         for i in $(seq 0 $OSTCOUNT);
1618         do
1619                 osts=$osts"0"
1620                 if [ $i -ne $OSTCOUNT ]; then
1621                         osts=$osts","
1622                 fi
1623         done
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 27ce "more stripes than OSTs with -o"
1637
1638 test_27cf() {
1639         local osp_proc="osp.$FSNAME-OST0000-osc-MDT000*.active"
1640         local pid=0
1641
1642         test_mkdir -p $DIR/$tdir || error "failed to mkdir $DIR/$tdir"
1643         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=0"
1644         stack_trap "do_facet $SINGLEMDS $LCTL set_param -n $osp_proc=1" EXIT
1645         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 1" ||
1646                 error "failed to set $osp_proc=0"
1647
1648         $LFS setstripe -o 0 $DIR/$tdir/$tfile &
1649         pid=$!
1650         sleep 1
1651         do_facet $SINGLEMDS "$LCTL set_param -n $osp_proc=1"
1652         wait_update_facet $SINGLEMDS "$LCTL get_param -n $osp_proc | grep 0" ||
1653                 error "failed to set $osp_proc=1"
1654         wait $pid
1655         [[ $pid -ne 0 ]] ||
1656                 error "should return error due to $osp_proc=0"
1657 }
1658 run_test 27cf "'setstripe -o' on inactive OSTs should return error"
1659
1660 test_27d() {
1661         test_mkdir $DIR/$tdir
1662         $LFS setstripe -c 0 -i -1 -S 0 $DIR/$tdir/$tfile ||
1663                 error "setstripe failed"
1664         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1665         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1666 }
1667 run_test 27d "create file with default settings"
1668
1669 test_27e() {
1670         # LU-5839 adds check for existed layout before setting it
1671         [[ $MDS1_VERSION -lt $(version_code 2.7.56) ]] &&
1672                 skip "Need MDS version at least 2.7.56"
1673
1674         test_mkdir $DIR/$tdir
1675         $LFS setstripe -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1676         $LFS setstripe -c 2 $DIR/$tdir/$tfile && error "setstripe worked twice"
1677         $CHECKSTAT -t file $DIR/$tdir/$tfile || error "checkstat failed"
1678 }
1679 run_test 27e "setstripe existing file (should return error)"
1680
1681 test_27f() {
1682         test_mkdir $DIR/$tdir
1683         $LFS setstripe -S 100 -i 0 -c 1 $DIR/$tdir/$tfile &&
1684                 error "$LFS setstripe $DIR/$tdir/$tfile failed"
1685         $CHECKSTAT -t file $DIR/$tdir/$tfile &&
1686                 error "$CHECKSTAT -t file $DIR/$tdir/$tfile should fail"
1687         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
1688         $LFS getstripe $DIR/$tdir/$tfile || error "$LFS getstripe failed"
1689 }
1690 run_test 27f "setstripe with bad stripe size (should return error)"
1691
1692 test_27g() {
1693         test_mkdir $DIR/$tdir
1694         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1695         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "no stripe info" ||
1696                 error "$DIR/$tdir/$tfile has object"
1697 }
1698 run_test 27g "$LFS getstripe with no objects"
1699
1700 test_27ga() {
1701         test_mkdir $DIR/$tdir
1702         touch $DIR/$tdir/$tfile || error "touch failed"
1703         ln -s bogus $DIR/$tdir/$tfile.2 || error "ln failed"
1704         $LFS getstripe -m $DIR/$tdir/$tfile $DIR/$tdir/$tfile.2
1705         local rc=$?
1706         (( rc == 2 )) || error "getstripe did not return ENOENT"
1707 }
1708 run_test 27ga "$LFS getstripe with missing file (should return error)"
1709
1710 test_27i() {
1711         test_mkdir $DIR/$tdir
1712         touch $DIR/$tdir/$tfile || error "touch failed"
1713         [[ $($LFS getstripe -c $DIR/$tdir/$tfile) -gt 0 ]] ||
1714                 error "missing objects"
1715 }
1716 run_test 27i "$LFS getstripe with some objects"
1717
1718 test_27j() {
1719         test_mkdir $DIR/$tdir
1720         $LFS setstripe -i $OSTCOUNT $DIR/$tdir/$tfile &&
1721                 error "setstripe failed" || true
1722 }
1723 run_test 27j "setstripe with bad stripe offset (should return error)"
1724
1725 test_27k() { # bug 2844
1726         test_mkdir $DIR/$tdir
1727         local file=$DIR/$tdir/$tfile
1728         local ll_max_blksize=$((4 * 1024 * 1024))
1729         $LFS setstripe -S 67108864 $file || error "setstripe failed"
1730         local blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1731         [ $blksize -le $ll_max_blksize ] || error "1:$blksize > $ll_max_blksize"
1732         dd if=/dev/zero of=$file bs=4k count=1
1733         blksize=$(stat $file | awk '/IO Block:/ { print $7 }')
1734         [ $blksize -le $ll_max_blksize ] || error "2:$blksize > $ll_max_blksize"
1735 }
1736 run_test 27k "limit i_blksize for broken user apps"
1737
1738 test_27l() {
1739         mcreate $DIR/$tfile || error "creating file"
1740         $RUNAS $LFS setstripe -c 1 $DIR/$tfile &&
1741                 error "setstripe should have failed" || true
1742 }
1743 run_test 27l "check setstripe permissions (should return error)"
1744
1745 test_27m() {
1746         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1747
1748         [ -n "$RCLIENTS" -o -n "$MOUNT_2" ] &&
1749                 skip_env "multiple clients -- skipping"
1750
1751         ORIGFREE=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
1752                    head -n1)
1753         if [[ $ORIGFREE -gt $MAXFREE ]]; then
1754                 skip "$ORIGFREE > $MAXFREE skipping out-of-space test on OST0"
1755         fi
1756         trap simple_cleanup_common EXIT
1757         test_mkdir $DIR/$tdir
1758         $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.1
1759         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1024 count=$MAXFREE &&
1760                 error "dd should fill OST0"
1761         i=2
1762         while $LFS setstripe -i 0 -c 1 $DIR/$tdir/$tfile.$i; do
1763                 i=$((i + 1))
1764                 [ $i -gt 256 ] && break
1765         done
1766         i=$((i + 1))
1767         touch $DIR/$tdir/$tfile.$i
1768         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1769             awk '{print $1}'| grep -w "0") ] &&
1770                 error "OST0 was full but new created file still use it"
1771         i=$((i + 1))
1772         touch $DIR/$tdir/$tfile.$i
1773         [ $($LFS getstripe $DIR/$tdir/$tfile.$i | grep -A 10 obdidx |
1774             awk '{print $1}'| grep -w "0") ] &&
1775                 error "OST0 was full but new created file still use it"
1776         simple_cleanup_common
1777 }
1778 run_test 27m "create file while OST0 was full"
1779
1780 # OSCs keep a NOSPC flag that will be reset after ~5s (qos_maxage)
1781 # if the OST isn't full anymore.
1782 reset_enospc() {
1783         local ostidx=${1:-""}
1784         local delay
1785         local ready
1786         local get_prealloc
1787
1788         local list=$(comma_list $(osts_nodes))
1789         [ "$ostidx" ] && list=$(facet_host ost$((ostidx + 1)))
1790
1791         do_nodes $list lctl set_param fail_loc=0
1792         wait_delete_completed   # initiate all OST_DESTROYs from MDS to OST
1793         delay=$(do_facet $SINGLEMDS lctl get_param -n lov.*.qos_maxage |
1794                 awk '{print $1 * 2;exit;}')
1795         get_prealloc="$LCTL get_param -n osc.*MDT*.prealloc_status |
1796                         grep -v \"^0$\""
1797         wait_update_facet $SINGLEMDS "$get_prealloc" "" $delay
1798 }
1799
1800 __exhaust_precreations() {
1801         local OSTIDX=$1
1802         local FAILLOC=$2
1803         local FAILIDX=${3:-$OSTIDX}
1804         local ofacet=ost$((OSTIDX + 1))
1805
1806         test_mkdir -p -c1 $DIR/$tdir
1807         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1808         local mfacet=mds$((mdtidx + 1))
1809         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1810
1811         local OST=$(ostname_from_index $OSTIDX)
1812
1813         # on the mdt's osc
1814         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1815         local last_id=$(do_facet $mfacet lctl get_param -n \
1816                         osp.$mdtosc_proc1.prealloc_last_id)
1817         local next_id=$(do_facet $mfacet lctl get_param -n \
1818                         osp.$mdtosc_proc1.prealloc_next_id)
1819
1820         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1821         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1822
1823         test_mkdir -p $DIR/$tdir/${OST}
1824         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1825 #define OBD_FAIL_OST_ENOSPC              0x215
1826         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1827         echo "Creating to objid $last_id on ost $OST..."
1828         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1829         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1830         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1831 }
1832
1833 exhaust_precreations() {
1834         __exhaust_precreations $1 $2 $3
1835         sleep_maxage
1836 }
1837
1838 exhaust_all_precreations() {
1839         local i
1840         for (( i=0; i < OSTCOUNT; i++ )) ; do
1841                 __exhaust_precreations $i $1 -1
1842         done
1843         sleep_maxage
1844 }
1845
1846 test_27n() {
1847         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1848         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1849         remote_mds_nodsh && skip "remote MDS with nodsh"
1850         remote_ost_nodsh && skip "remote OST with nodsh"
1851
1852         reset_enospc
1853         rm -f $DIR/$tdir/$tfile
1854         exhaust_precreations 0 0x80000215
1855         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1856         touch $DIR/$tdir/$tfile || error "touch failed"
1857         $LFS getstripe $DIR/$tdir/$tfile
1858         reset_enospc
1859 }
1860 run_test 27n "create file with some full OSTs"
1861
1862 test_27o() {
1863         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1865         remote_mds_nodsh && skip "remote MDS with nodsh"
1866         remote_ost_nodsh && skip "remote OST with nodsh"
1867
1868         reset_enospc
1869         rm -f $DIR/$tdir/$tfile
1870         exhaust_all_precreations 0x215
1871
1872         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1873
1874         reset_enospc
1875         rm -rf $DIR/$tdir/*
1876 }
1877 run_test 27o "create file with all full OSTs (should error)"
1878
1879 test_27p() {
1880         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1881         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1882         remote_mds_nodsh && skip "remote MDS with nodsh"
1883         remote_ost_nodsh && skip "remote OST with nodsh"
1884
1885         reset_enospc
1886         rm -f $DIR/$tdir/$tfile
1887         test_mkdir $DIR/$tdir
1888
1889         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1890         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1891         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1892
1893         exhaust_precreations 0 0x80000215
1894         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1895         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1896         $LFS getstripe $DIR/$tdir/$tfile
1897
1898         reset_enospc
1899 }
1900 run_test 27p "append to a truncated file with some full OSTs"
1901
1902 test_27q() {
1903         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1904         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1905         remote_mds_nodsh && skip "remote MDS with nodsh"
1906         remote_ost_nodsh && skip "remote OST with nodsh"
1907
1908         reset_enospc
1909         rm -f $DIR/$tdir/$tfile
1910
1911         test_mkdir $DIR/$tdir
1912         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1913         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1914                 error "truncate $DIR/$tdir/$tfile failed"
1915         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1916
1917         exhaust_all_precreations 0x215
1918
1919         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1920         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1921
1922         reset_enospc
1923 }
1924 run_test 27q "append to truncated file with all OSTs full (should error)"
1925
1926 test_27r() {
1927         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1929         remote_mds_nodsh && skip "remote MDS with nodsh"
1930         remote_ost_nodsh && skip "remote OST with nodsh"
1931
1932         reset_enospc
1933         rm -f $DIR/$tdir/$tfile
1934         exhaust_precreations 0 0x80000215
1935
1936         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1937
1938         reset_enospc
1939 }
1940 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1941
1942 test_27s() { # bug 10725
1943         test_mkdir $DIR/$tdir
1944         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1945         local stripe_count=0
1946         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1947         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1948                 error "stripe width >= 2^32 succeeded" || true
1949
1950 }
1951 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1952
1953 test_27t() { # bug 10864
1954         WDIR=$(pwd)
1955         WLFS=$(which lfs)
1956         cd $DIR
1957         touch $tfile
1958         $WLFS getstripe $tfile
1959         cd $WDIR
1960 }
1961 run_test 27t "check that utils parse path correctly"
1962
1963 test_27u() { # bug 4900
1964         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1965         remote_mds_nodsh && skip "remote MDS with nodsh"
1966
1967         local index
1968         local list=$(comma_list $(mdts_nodes))
1969
1970 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1971         do_nodes $list $LCTL set_param fail_loc=0x139
1972         test_mkdir -p $DIR/$tdir
1973         trap simple_cleanup_common EXIT
1974         createmany -o $DIR/$tdir/t- 1000
1975         do_nodes $list $LCTL set_param fail_loc=0
1976
1977         TLOG=$TMP/$tfile.getstripe
1978         $LFS getstripe $DIR/$tdir > $TLOG
1979         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1980         unlinkmany $DIR/$tdir/t- 1000
1981         trap 0
1982         [[ $OBJS -gt 0 ]] &&
1983                 error "$OBJS objects created on OST-0. See $TLOG" ||
1984                 rm -f $TLOG
1985 }
1986 run_test 27u "skip object creation on OSC w/o objects"
1987
1988 test_27v() { # bug 4900
1989         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1990         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1991         remote_mds_nodsh && skip "remote MDS with nodsh"
1992         remote_ost_nodsh && skip "remote OST with nodsh"
1993
1994         exhaust_all_precreations 0x215
1995         reset_enospc
1996
1997         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1998
1999         touch $DIR/$tdir/$tfile
2000         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
2001         # all except ost1
2002         for (( i=1; i < OSTCOUNT; i++ )); do
2003                 do_facet ost$i lctl set_param fail_loc=0x705
2004         done
2005         local START=`date +%s`
2006         createmany -o $DIR/$tdir/$tfile 32
2007
2008         local FINISH=`date +%s`
2009         local TIMEOUT=`lctl get_param -n timeout`
2010         local PROCESS=$((FINISH - START))
2011         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
2012                error "$FINISH - $START >= $TIMEOUT / 2"
2013         sleep $((TIMEOUT / 2 - PROCESS))
2014         reset_enospc
2015 }
2016 run_test 27v "skip object creation on slow OST"
2017
2018 test_27w() { # bug 10997
2019         test_mkdir $DIR/$tdir
2020         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
2021         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
2022                 error "stripe size $size != 65536" || true
2023         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
2024                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
2025 }
2026 run_test 27w "check $LFS setstripe -S and getstrip -d options"
2027
2028 test_27wa() {
2029         [[ $OSTCOUNT -lt 2 ]] &&
2030                 skip_env "skipping multiple stripe count/offset test"
2031
2032         test_mkdir $DIR/$tdir
2033         for i in $(seq 1 $OSTCOUNT); do
2034                 offset=$((i - 1))
2035                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2036                         error "setstripe -c $i -i $offset failed"
2037                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2038                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2039                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2040                 [ $index -ne $offset ] &&
2041                         error "stripe offset $index != $offset" || true
2042         done
2043 }
2044 run_test 27wa "check $LFS setstripe -c -i options"
2045
2046 test_27x() {
2047         remote_ost_nodsh && skip "remote OST with nodsh"
2048         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2049         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2050
2051         OFFSET=$(($OSTCOUNT - 1))
2052         OSTIDX=0
2053         local OST=$(ostname_from_index $OSTIDX)
2054
2055         test_mkdir $DIR/$tdir
2056         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2057         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2058         sleep_maxage
2059         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2060         for i in $(seq 0 $OFFSET); do
2061                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2062                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2063                 error "OST0 was degraded but new created file still use it"
2064         done
2065         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2066 }
2067 run_test 27x "create files while OST0 is degraded"
2068
2069 test_27y() {
2070         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2071         remote_mds_nodsh && skip "remote MDS with nodsh"
2072         remote_ost_nodsh && skip "remote OST with nodsh"
2073         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2074
2075         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2076         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2077                 osp.$mdtosc.prealloc_last_id)
2078         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2079                 osp.$mdtosc.prealloc_next_id)
2080         local fcount=$((last_id - next_id))
2081         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2082         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2083
2084         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2085                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2086         local OST_DEACTIVE_IDX=-1
2087         local OSC
2088         local OSTIDX
2089         local OST
2090
2091         for OSC in $MDS_OSCS; do
2092                 OST=$(osc_to_ost $OSC)
2093                 OSTIDX=$(index_from_ostuuid $OST)
2094                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2095                         OST_DEACTIVE_IDX=$OSTIDX
2096                 fi
2097                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2098                         echo $OSC "is Deactivated:"
2099                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2100                 fi
2101         done
2102
2103         OSTIDX=$(index_from_ostuuid $OST)
2104         test_mkdir $DIR/$tdir
2105         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2106
2107         for OSC in $MDS_OSCS; do
2108                 OST=$(osc_to_ost $OSC)
2109                 OSTIDX=$(index_from_ostuuid $OST)
2110                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2111                         echo $OST "is degraded:"
2112                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2113                                                 obdfilter.$OST.degraded=1
2114                 fi
2115         done
2116
2117         sleep_maxage
2118         createmany -o $DIR/$tdir/$tfile $fcount
2119
2120         for OSC in $MDS_OSCS; do
2121                 OST=$(osc_to_ost $OSC)
2122                 OSTIDX=$(index_from_ostuuid $OST)
2123                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2124                         echo $OST "is recovered from degraded:"
2125                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2126                                                 obdfilter.$OST.degraded=0
2127                 else
2128                         do_facet $SINGLEMDS lctl --device %$OSC activate
2129                 fi
2130         done
2131
2132         # all osp devices get activated, hence -1 stripe count restored
2133         local stripe_count=0
2134
2135         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2136         # devices get activated.
2137         sleep_maxage
2138         $LFS setstripe -c -1 $DIR/$tfile
2139         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2140         rm -f $DIR/$tfile
2141         [ $stripe_count -ne $OSTCOUNT ] &&
2142                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2143         return 0
2144 }
2145 run_test 27y "create files while OST0 is degraded and the rest inactive"
2146
2147 check_seq_oid()
2148 {
2149         log "check file $1"
2150
2151         lmm_count=$($LFS getstripe -c $1)
2152         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2153         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2154
2155         local old_ifs="$IFS"
2156         IFS=$'[:]'
2157         fid=($($LFS path2fid $1))
2158         IFS="$old_ifs"
2159
2160         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2161         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2162
2163         # compare lmm_seq and lu_fid->f_seq
2164         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2165         # compare lmm_object_id and lu_fid->oid
2166         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2167
2168         # check the trusted.fid attribute of the OST objects of the file
2169         local have_obdidx=false
2170         local stripe_nr=0
2171         $LFS getstripe $1 | while read obdidx oid hex seq; do
2172                 # skip lines up to and including "obdidx"
2173                 [ -z "$obdidx" ] && break
2174                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2175                 $have_obdidx || continue
2176
2177                 local ost=$((obdidx + 1))
2178                 local dev=$(ostdevname $ost)
2179                 local oid_hex
2180
2181                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2182
2183                 seq=$(echo $seq | sed -e "s/^0x//g")
2184                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2185                         oid_hex=$(echo $oid)
2186                 else
2187                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2188                 fi
2189                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2190
2191                 local ff=""
2192                 #
2193                 # Don't unmount/remount the OSTs if we don't need to do that.
2194                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2195                 # update too, until that use mount/ll_decode_filter_fid/mount.
2196                 # Re-enable when debugfs will understand new filter_fid.
2197                 #
2198                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2199                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2200                                 $dev 2>/dev/null" | grep "parent=")
2201                 fi
2202                 if [ -z "$ff" ]; then
2203                         stop ost$ost
2204                         mount_fstype ost$ost
2205                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2206                                 $(facet_mntpt ost$ost)/$obj_file)
2207                         unmount_fstype ost$ost
2208                         start ost$ost $dev $OST_MOUNT_OPTS
2209                         clients_up
2210                 fi
2211
2212                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2213
2214                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2215
2216                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2217                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2218                 #
2219                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2220                 #       stripe_size=1048576 component_id=1 component_start=0 \
2221                 #       component_end=33554432
2222                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2223                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2224                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2225                 local ff_pstripe
2226                 if grep -q 'stripe=' <<<$ff; then
2227                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2228                 else
2229                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2230                         # into f_ver in this case.  See comment on ff_parent.
2231                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2232                 fi
2233
2234                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2235                 [ $ff_pseq = $lmm_seq ] ||
2236                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2237                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2238                 [ $ff_poid = $lmm_oid ] ||
2239                         error "FF parent OID $ff_poid != $lmm_oid"
2240                 (($ff_pstripe == $stripe_nr)) ||
2241                         error "FF stripe $ff_pstripe != $stripe_nr"
2242
2243                 stripe_nr=$((stripe_nr + 1))
2244                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2245                         continue
2246                 if grep -q 'stripe_count=' <<<$ff; then
2247                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2248                                             -e 's/ .*//' <<<$ff)
2249                         [ $lmm_count = $ff_scnt ] ||
2250                                 error "FF stripe count $lmm_count != $ff_scnt"
2251                 fi
2252         done
2253 }
2254
2255 test_27z() {
2256         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2257         remote_ost_nodsh && skip "remote OST with nodsh"
2258
2259         test_mkdir $DIR/$tdir
2260         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2261                 { error "setstripe -c -1 failed"; return 1; }
2262         # We need to send a write to every object to get parent FID info set.
2263         # This _should_ also work for setattr, but does not currently.
2264         # touch $DIR/$tdir/$tfile-1 ||
2265         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2266                 { error "dd $tfile-1 failed"; return 2; }
2267         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2268                 { error "setstripe -c -1 failed"; return 3; }
2269         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2270                 { error "dd $tfile-2 failed"; return 4; }
2271
2272         # make sure write RPCs have been sent to OSTs
2273         sync; sleep 5; sync
2274
2275         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2276         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2277 }
2278 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2279
2280 test_27A() { # b=19102
2281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2282
2283         save_layout_restore_at_exit $MOUNT
2284         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2285         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2286                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2287         local default_size=$($LFS getstripe -S $MOUNT)
2288         local default_offset=$($LFS getstripe -i $MOUNT)
2289         local dsize=$(do_facet $SINGLEMDS \
2290                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2291         [ $default_size -eq $dsize ] ||
2292                 error "stripe size $default_size != $dsize"
2293         [ $default_offset -eq -1 ] ||
2294                 error "stripe offset $default_offset != -1"
2295 }
2296 run_test 27A "check filesystem-wide default LOV EA values"
2297
2298 test_27B() { # LU-2523
2299         test_mkdir $DIR/$tdir
2300         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2301         touch $DIR/$tdir/f0
2302         # open f1 with O_LOV_DELAY_CREATE
2303         # rename f0 onto f1
2304         # call setstripe ioctl on open file descriptor for f1
2305         # close
2306         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2307                 $DIR/$tdir/f0
2308
2309         rm -f $DIR/$tdir/f1
2310         # open f1 with O_LOV_DELAY_CREATE
2311         # unlink f1
2312         # call setstripe ioctl on open file descriptor for f1
2313         # close
2314         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2315
2316         # Allow multiop to fail in imitation of NFS's busted semantics.
2317         true
2318 }
2319 run_test 27B "call setstripe on open unlinked file/rename victim"
2320
2321 # 27C family tests full striping and overstriping
2322 test_27Ca() { #LU-2871
2323         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2324
2325         declare -a ost_idx
2326         local index
2327         local found
2328         local i
2329         local j
2330
2331         test_mkdir $DIR/$tdir
2332         cd $DIR/$tdir
2333         for i in $(seq 0 $((OSTCOUNT - 1))); do
2334                 # set stripe across all OSTs starting from OST$i
2335                 $LFS setstripe -i $i -c -1 $tfile$i
2336                 # get striping information
2337                 ost_idx=($($LFS getstripe $tfile$i |
2338                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2339                 echo ${ost_idx[@]}
2340
2341                 # check the layout
2342                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2343                         error "${#ost_idx[@]} != $OSTCOUNT"
2344
2345                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2346                         found=0
2347                         for j in $(echo ${ost_idx[@]}); do
2348                                 if [ $index -eq $j ]; then
2349                                         found=1
2350                                         break
2351                                 fi
2352                         done
2353                         [ $found = 1 ] ||
2354                                 error "Can not find $index in ${ost_idx[@]}"
2355                 done
2356         done
2357 }
2358 run_test 27Ca "check full striping across all OSTs"
2359
2360 test_27Cb() {
2361         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2362                 skip "server does not support overstriping"
2363         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2364                 skip_env "too many osts, skipping"
2365
2366         test_mkdir -p $DIR/$tdir
2367         local setcount=$(($OSTCOUNT * 2))
2368         [ $setcount -ge 160 ] || large_xattr_enabled ||
2369                 skip_env "ea_inode feature disabled"
2370
2371         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2372                 error "setstripe failed"
2373
2374         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2375         [ $count -eq $setcount ] ||
2376                 error "stripe count $count, should be $setcount"
2377
2378         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2379                 error "overstriped should be set in pattern"
2380
2381         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2382                 error "dd failed"
2383 }
2384 run_test 27Cb "more stripes than OSTs with -C"
2385
2386 test_27Cc() {
2387         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2388                 skip "server does not support overstriping"
2389         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2390
2391         test_mkdir -p $DIR/$tdir
2392         local setcount=$(($OSTCOUNT - 1))
2393
2394         [ $setcount -ge 160 ] || large_xattr_enabled ||
2395                 skip_env "ea_inode feature disabled"
2396
2397         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2398                 error "setstripe failed"
2399
2400         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2401         [ $count -eq $setcount ] ||
2402                 error "stripe count $count, should be $setcount"
2403
2404         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2405                 error "overstriped should not be set in pattern"
2406
2407         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2408                 error "dd failed"
2409 }
2410 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2411
2412 test_27Cd() {
2413         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2414                 skip "server does not support overstriping"
2415         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2416         large_xattr_enabled || skip_env "ea_inode feature disabled"
2417
2418         test_mkdir -p $DIR/$tdir
2419         local setcount=$LOV_MAX_STRIPE_COUNT
2420
2421         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2422                 error "setstripe failed"
2423
2424         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2425         [ $count -eq $setcount ] ||
2426                 error "stripe count $count, should be $setcount"
2427
2428         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2429                 error "overstriped should be set in pattern"
2430
2431         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2432                 error "dd failed"
2433
2434         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2435 }
2436 run_test 27Cd "test maximum stripe count"
2437
2438 test_27Ce() {
2439         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2440                 skip "server does not support overstriping"
2441         test_mkdir -p $DIR/$tdir
2442
2443         pool_add $TESTNAME || error "Pool creation failed"
2444         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2445
2446         local setcount=8
2447
2448         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2449                 error "setstripe failed"
2450
2451         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2452         [ $count -eq $setcount ] ||
2453                 error "stripe count $count, should be $setcount"
2454
2455         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2456                 error "overstriped should be set in pattern"
2457
2458         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2459                 error "dd failed"
2460
2461         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2462 }
2463 run_test 27Ce "test pool with overstriping"
2464
2465 test_27Cf() {
2466         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2467                 skip "server does not support overstriping"
2468         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2469                 skip_env "too many osts, skipping"
2470
2471         test_mkdir -p $DIR/$tdir
2472
2473         local setcount=$(($OSTCOUNT * 2))
2474         [ $setcount -ge 160 ] || large_xattr_enabled ||
2475                 skip_env "ea_inode feature disabled"
2476
2477         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2478                 error "setstripe failed"
2479
2480         echo 1 > $DIR/$tdir/$tfile
2481
2482         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2483         [ $count -eq $setcount ] ||
2484                 error "stripe count $count, should be $setcount"
2485
2486         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2487                 error "overstriped should be set in pattern"
2488
2489         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2490                 error "dd failed"
2491
2492         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2493 }
2494 run_test 27Cf "test default inheritance with overstriping"
2495
2496 test_27D() {
2497         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2498         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2499         remote_mds_nodsh && skip "remote MDS with nodsh"
2500
2501         local POOL=${POOL:-testpool}
2502         local first_ost=0
2503         local last_ost=$(($OSTCOUNT - 1))
2504         local ost_step=1
2505         local ost_list=$(seq $first_ost $ost_step $last_ost)
2506         local ost_range="$first_ost $last_ost $ost_step"
2507
2508         test_mkdir $DIR/$tdir
2509         pool_add $POOL || error "pool_add failed"
2510         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2511
2512         local skip27D
2513         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2514                 skip27D+="-s 29"
2515         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2516                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2517                         skip27D+=" -s 30,31"
2518         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2519           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2520                 skip27D+=" -s 32,33"
2521         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2522                 skip27D+=" -s 34"
2523         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2524                 error "llapi_layout_test failed"
2525
2526         destroy_test_pools || error "destroy test pools failed"
2527 }
2528 run_test 27D "validate llapi_layout API"
2529
2530 # Verify that default_easize is increased from its initial value after
2531 # accessing a widely striped file.
2532 test_27E() {
2533         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2534         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2535                 skip "client does not have LU-3338 fix"
2536
2537         # 72 bytes is the minimum space required to store striping
2538         # information for a file striped across one OST:
2539         # (sizeof(struct lov_user_md_v3) +
2540         #  sizeof(struct lov_user_ost_data_v1))
2541         local min_easize=72
2542         $LCTL set_param -n llite.*.default_easize $min_easize ||
2543                 error "lctl set_param failed"
2544         local easize=$($LCTL get_param -n llite.*.default_easize)
2545
2546         [ $easize -eq $min_easize ] ||
2547                 error "failed to set default_easize"
2548
2549         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2550                 error "setstripe failed"
2551         # In order to ensure stat() call actually talks to MDS we need to
2552         # do something drastic to this file to shake off all lock, e.g.
2553         # rename it (kills lookup lock forcing cache cleaning)
2554         mv $DIR/$tfile $DIR/${tfile}-1
2555         ls -l $DIR/${tfile}-1
2556         rm $DIR/${tfile}-1
2557
2558         easize=$($LCTL get_param -n llite.*.default_easize)
2559
2560         [ $easize -gt $min_easize ] ||
2561                 error "default_easize not updated"
2562 }
2563 run_test 27E "check that default extended attribute size properly increases"
2564
2565 test_27F() { # LU-5346/LU-7975
2566         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2567         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2568         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2569                 skip "Need MDS version at least 2.8.51"
2570         remote_ost_nodsh && skip "remote OST with nodsh"
2571
2572         test_mkdir $DIR/$tdir
2573         rm -f $DIR/$tdir/f0
2574         $LFS setstripe -c 2 $DIR/$tdir
2575
2576         # stop all OSTs to reproduce situation for LU-7975 ticket
2577         for num in $(seq $OSTCOUNT); do
2578                 stop ost$num
2579         done
2580
2581         # open/create f0 with O_LOV_DELAY_CREATE
2582         # truncate f0 to a non-0 size
2583         # close
2584         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2585
2586         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2587         # open/write it again to force delayed layout creation
2588         cat /etc/hosts > $DIR/$tdir/f0 &
2589         catpid=$!
2590
2591         # restart OSTs
2592         for num in $(seq $OSTCOUNT); do
2593                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2594                         error "ost$num failed to start"
2595         done
2596
2597         wait $catpid || error "cat failed"
2598
2599         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2600         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2601                 error "wrong stripecount"
2602
2603 }
2604 run_test 27F "Client resend delayed layout creation with non-zero size"
2605
2606 test_27G() { #LU-10629
2607         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2608                 skip "Need MDS version at least 2.11.51"
2609         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2610         remote_mds_nodsh && skip "remote MDS with nodsh"
2611         local POOL=${POOL:-testpool}
2612         local ostrange="0 0 1"
2613
2614         test_mkdir $DIR/$tdir
2615         pool_add $POOL || error "pool_add failed"
2616         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2617         $LFS setstripe -p $POOL $DIR/$tdir
2618
2619         local pool=$($LFS getstripe -p $DIR/$tdir)
2620
2621         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2622
2623         $LFS setstripe -d $DIR/$tdir
2624
2625         pool=$($LFS getstripe -p $DIR/$tdir)
2626
2627         rmdir $DIR/$tdir
2628
2629         [ -z "$pool" ] || error "'$pool' is not empty"
2630 }
2631 run_test 27G "Clear OST pool from stripe"
2632
2633 test_27H() {
2634         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2635                 skip "Need MDS version newer than 2.11.54"
2636         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2637         test_mkdir $DIR/$tdir
2638         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2639         touch $DIR/$tdir/$tfile
2640         $LFS getstripe -c $DIR/$tdir/$tfile
2641         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2642                 error "two-stripe file doesn't have two stripes"
2643
2644         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2645         $LFS getstripe -y $DIR/$tdir/$tfile
2646         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2647              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2648                 error "expected l_ost_idx: [02]$ not matched"
2649
2650         # make sure ost list has been cleared
2651         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2652         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2653                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2654         touch $DIR/$tdir/f3
2655         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2656 }
2657 run_test 27H "Set specific OSTs stripe"
2658
2659 test_27I() {
2660         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2661         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2662         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2663                 skip "Need MDS version newer than 2.12.52"
2664         local pool=$TESTNAME
2665         local ostrange="1 1 1"
2666
2667         save_layout_restore_at_exit $MOUNT
2668         $LFS setstripe -c 2 -i 0 $MOUNT
2669         pool_add $pool || error "pool_add failed"
2670         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2671         test_mkdir $DIR/$tdir
2672         $LFS setstripe -p $pool $DIR/$tdir
2673         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2674         $LFS getstripe $DIR/$tdir/$tfile
2675 }
2676 run_test 27I "check that root dir striping does not break parent dir one"
2677
2678 test_27J() {
2679         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2680                 skip "Need MDS version newer than 2.12.51"
2681
2682         test_mkdir $DIR/$tdir
2683         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2684         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2685
2686         # create foreign file (raw way)
2687         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2688                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2689
2690         # verify foreign file (raw way)
2691         parse_foreign_file -f $DIR/$tdir/$tfile |
2692                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2693                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2694         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2695                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2696         parse_foreign_file -f $DIR/$tdir/$tfile |
2697                 grep "lov_foreign_size: 73" ||
2698                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2699         parse_foreign_file -f $DIR/$tdir/$tfile |
2700                 grep "lov_foreign_type: 1" ||
2701                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2702         parse_foreign_file -f $DIR/$tdir/$tfile |
2703                 grep "lov_foreign_flags: 0x0000DA08" ||
2704                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2705         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2706                 grep "lov_foreign_value: 0x" |
2707                 sed -e 's/lov_foreign_value: 0x//')
2708         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2709         [[ $lov = ${lov2// /} ]] ||
2710                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2711
2712         # create foreign file (lfs + API)
2713         $LFS setstripe --foreign=daos --flags 0xda08 \
2714                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2715                 error "$DIR/$tdir/${tfile}2: create failed"
2716
2717         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2718                 grep "lfm_magic:.*0x0BD70BD0" ||
2719                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2720         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2721         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2722                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2723         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2724                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2725         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2726                 grep "lfm_flags:.*0x0000DA08" ||
2727                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2728         $LFS getstripe $DIR/$tdir/${tfile}2 |
2729                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2730                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2731
2732         # modify striping should fail
2733         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2734                 error "$DIR/$tdir/$tfile: setstripe should fail"
2735         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2736                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2737
2738         # R/W should fail
2739         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2740         cat $DIR/$tdir/${tfile}2 &&
2741                 error "$DIR/$tdir/${tfile}2: read should fail"
2742         cat /etc/passwd > $DIR/$tdir/$tfile &&
2743                 error "$DIR/$tdir/$tfile: write should fail"
2744         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2745                 error "$DIR/$tdir/${tfile}2: write should fail"
2746
2747         # chmod should work
2748         chmod 222 $DIR/$tdir/$tfile ||
2749                 error "$DIR/$tdir/$tfile: chmod failed"
2750         chmod 222 $DIR/$tdir/${tfile}2 ||
2751                 error "$DIR/$tdir/${tfile}2: chmod failed"
2752
2753         # chown should work
2754         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2755                 error "$DIR/$tdir/$tfile: chown failed"
2756         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2757                 error "$DIR/$tdir/${tfile}2: chown failed"
2758
2759         # rename should work
2760         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2761                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2762         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2763                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2764
2765         #remove foreign file
2766         rm $DIR/$tdir/${tfile}.new ||
2767                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2768         rm $DIR/$tdir/${tfile}2.new ||
2769                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2770 }
2771 run_test 27J "basic ops on file with foreign LOV"
2772
2773 test_27K() {
2774         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2775                 skip "Need MDS version newer than 2.12.49"
2776
2777         test_mkdir $DIR/$tdir
2778         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2779         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2780
2781         # create foreign dir (raw way)
2782         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2783                 error "create_foreign_dir FAILED"
2784
2785         # verify foreign dir (raw way)
2786         parse_foreign_dir -d $DIR/$tdir/$tdir |
2787                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2788                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2789         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2790                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2791         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2792                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2793         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2794                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2795         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2796                 grep "lmv_foreign_value: 0x" |
2797                 sed 's/lmv_foreign_value: 0x//')
2798         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2799                 sed 's/ //g')
2800         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2801
2802         # create foreign dir (lfs + API)
2803         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2804                 $DIR/$tdir/${tdir}2 ||
2805                 error "$DIR/$tdir/${tdir}2: create failed"
2806
2807         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2808                 grep "lfm_magic:.*0x0CD50CD0" ||
2809                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2810         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2811         # - sizeof(lfm_type) - sizeof(lfm_flags)
2812         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2813                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2814         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2815                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2816         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2817                 grep "lfm_flags:.*0x0000DA05" ||
2818                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2819         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2820                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2821                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2822
2823         # file create in dir should fail
2824         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2825         touch $DIR/$tdir/${tdir}2/$tfile &&
2826                 "$DIR/${tdir}2: file create should fail"
2827
2828         # chmod should work
2829         chmod 777 $DIR/$tdir/$tdir ||
2830                 error "$DIR/$tdir: chmod failed"
2831         chmod 777 $DIR/$tdir/${tdir}2 ||
2832                 error "$DIR/${tdir}2: chmod failed"
2833
2834         # chown should work
2835         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2836                 error "$DIR/$tdir: chown failed"
2837         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2838                 error "$DIR/${tdir}2: chown failed"
2839
2840         # rename should work
2841         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2842                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2843         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2844                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2845
2846         #remove foreign dir
2847         rmdir $DIR/$tdir/${tdir}.new ||
2848                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2849         rmdir $DIR/$tdir/${tdir}2.new ||
2850                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2851 }
2852 run_test 27K "basic ops on dir with foreign LMV"
2853
2854 test_27L() {
2855         remote_mds_nodsh && skip "remote MDS with nodsh"
2856
2857         local POOL=${POOL:-$TESTNAME}
2858
2859         pool_add $POOL || error "pool_add failed"
2860
2861         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2862                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2863                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2864 }
2865 run_test 27L "lfs pool_list gives correct pool name"
2866
2867 test_27M() {
2868         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2869                 skip "Need MDS version >= than 2.12.57"
2870         remote_mds_nodsh && skip "remote MDS with nodsh"
2871         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2872
2873         test_mkdir $DIR/$tdir
2874
2875         # Set default striping on directory
2876         $LFS setstripe -C 4 $DIR/$tdir
2877
2878         echo 1 > $DIR/$tdir/${tfile}.1
2879         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2880         local setcount=4
2881         [ $count -eq $setcount ] ||
2882                 error "(1) stripe count $count, should be $setcount"
2883
2884         # Capture existing append_stripe_count setting for restore
2885         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2886         local mdts=$(comma_list $(mdts_nodes))
2887         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2888
2889         local appendcount=$orig_count
2890         echo 1 >> $DIR/$tdir/${tfile}.2_append
2891         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2892         [ $count -eq $appendcount ] ||
2893                 error "(2)stripe count $count, should be $appendcount for append"
2894
2895         # Disable O_APPEND striping, verify it works
2896         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2897
2898         # Should now get the default striping, which is 4
2899         setcount=4
2900         echo 1 >> $DIR/$tdir/${tfile}.3_append
2901         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2902         [ $count -eq $setcount ] ||
2903                 error "(3) stripe count $count, should be $setcount"
2904
2905         # Try changing the stripe count for append files
2906         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2907
2908         # Append striping is now 2 (directory default is still 4)
2909         appendcount=2
2910         echo 1 >> $DIR/$tdir/${tfile}.4_append
2911         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2912         [ $count -eq $appendcount ] ||
2913                 error "(4) stripe count $count, should be $appendcount for append"
2914
2915         # Test append stripe count of -1
2916         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2917         appendcount=$OSTCOUNT
2918         echo 1 >> $DIR/$tdir/${tfile}.5
2919         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2920         [ $count -eq $appendcount ] ||
2921                 error "(5) stripe count $count, should be $appendcount for append"
2922
2923         # Set append striping back to default of 1
2924         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2925
2926         # Try a new default striping, PFL + DOM
2927         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2928
2929         # Create normal DOM file, DOM returns stripe count == 0
2930         setcount=0
2931         touch $DIR/$tdir/${tfile}.6
2932         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2933         [ $count -eq $setcount ] ||
2934                 error "(6) stripe count $count, should be $setcount"
2935
2936         # Show
2937         appendcount=1
2938         echo 1 >> $DIR/$tdir/${tfile}.7_append
2939         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2940         [ $count -eq $appendcount ] ||
2941                 error "(7) stripe count $count, should be $appendcount for append"
2942
2943         # Clean up DOM layout
2944         $LFS setstripe -d $DIR/$tdir
2945
2946         # Now test that append striping works when layout is from root
2947         $LFS setstripe -c 2 $MOUNT
2948         # Make a special directory for this
2949         mkdir $DIR/${tdir}/${tdir}.2
2950         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2951
2952         # Verify for normal file
2953         setcount=2
2954         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2955         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2956         [ $count -eq $setcount ] ||
2957                 error "(8) stripe count $count, should be $setcount"
2958
2959         appendcount=1
2960         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2961         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2962         [ $count -eq $appendcount ] ||
2963                 error "(9) stripe count $count, should be $appendcount for append"
2964
2965         # Now test O_APPEND striping with pools
2966         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2967         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2968
2969         # Create the pool
2970         pool_add $TESTNAME || error "pool creation failed"
2971         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2972
2973         echo 1 >> $DIR/$tdir/${tfile}.10_append
2974
2975         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2976         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2977
2978         # Check that count is still correct
2979         appendcount=1
2980         echo 1 >> $DIR/$tdir/${tfile}.11_append
2981         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2982         [ $count -eq $appendcount ] ||
2983                 error "(11) stripe count $count, should be $appendcount for append"
2984
2985         # Disable O_APPEND stripe count, verify pool works separately
2986         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2987
2988         echo 1 >> $DIR/$tdir/${tfile}.12_append
2989
2990         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2991         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2992
2993         # Remove pool setting, verify it's not applied
2994         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2995
2996         echo 1 >> $DIR/$tdir/${tfile}.13_append
2997
2998         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2999         [ "$pool" = "" ] || error "(13) pool found: $pool"
3000 }
3001 run_test 27M "test O_APPEND striping"
3002
3003 test_27N() {
3004         combined_mgs_mds && skip "needs separate MGS/MDT"
3005
3006         pool_add $TESTNAME || error "pool_add failed"
3007         do_facet mgs "$LCTL pool_list $FSNAME" |
3008                 grep -Fx "${FSNAME}.${TESTNAME}" ||
3009                 error "lctl pool_list on MGS failed"
3010 }
3011 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
3012
3013 # createtest also checks that device nodes are created and
3014 # then visible correctly (#2091)
3015 test_28() { # bug 2091
3016         test_mkdir $DIR/d28
3017         $CREATETEST $DIR/d28/ct || error "createtest failed"
3018 }
3019 run_test 28 "create/mknod/mkdir with bad file types ============"
3020
3021 test_29() {
3022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3023
3024         sync; sleep 1; sync # flush out any dirty pages from previous tests
3025         cancel_lru_locks
3026         test_mkdir $DIR/d29
3027         touch $DIR/d29/foo
3028         log 'first d29'
3029         ls -l $DIR/d29
3030
3031         declare -i LOCKCOUNTORIG=0
3032         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3033                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3034         done
3035         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3036
3037         declare -i LOCKUNUSEDCOUNTORIG=0
3038         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3039                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3040         done
3041
3042         log 'second d29'
3043         ls -l $DIR/d29
3044         log 'done'
3045
3046         declare -i LOCKCOUNTCURRENT=0
3047         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3048                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3049         done
3050
3051         declare -i LOCKUNUSEDCOUNTCURRENT=0
3052         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3053                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3054         done
3055
3056         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3057                 $LCTL set_param -n ldlm.dump_namespaces ""
3058                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3059                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3060                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3061                 return 2
3062         fi
3063         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3064                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3065                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3066                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3067                 return 3
3068         fi
3069 }
3070 run_test 29 "IT_GETATTR regression  ============================"
3071
3072 test_30a() { # was test_30
3073         cp $(which ls) $DIR || cp /bin/ls $DIR
3074         $DIR/ls / || error "Can't execute binary from lustre"
3075         rm $DIR/ls
3076 }
3077 run_test 30a "execute binary from Lustre (execve) =============="
3078
3079 test_30b() {
3080         cp `which ls` $DIR || cp /bin/ls $DIR
3081         chmod go+rx $DIR/ls
3082         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3083         rm $DIR/ls
3084 }
3085 run_test 30b "execute binary from Lustre as non-root ==========="
3086
3087 test_30c() { # b=22376
3088         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3089
3090         cp $(which ls) $DIR || cp /bin/ls $DIR
3091         chmod a-rw $DIR/ls
3092         cancel_lru_locks mdc
3093         cancel_lru_locks osc
3094         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3095         rm -f $DIR/ls
3096 }
3097 run_test 30c "execute binary from Lustre without read perms ===="
3098
3099 test_30d() {
3100         cp $(which dd) $DIR || error "failed to copy dd to $DIR/dd"
3101
3102         for i in {1..10}; do
3103                 $DIR/dd bs=1M count=128 if=/dev/zero of=$DIR/$tfile &
3104                 local PID=$!
3105                 sleep 1
3106                 $LCTL set_param ldlm.namespaces.*MDT*.lru_size=clear
3107                 wait $PID || error "executing dd from Lustre failed"
3108                 rm -f $DIR/$tfile
3109         done
3110
3111         rm -f $DIR/dd
3112 }
3113 run_test 30d "execute binary from Lustre while clear locks"
3114
3115 test_31a() {
3116         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3117         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3118 }
3119 run_test 31a "open-unlink file =================================="
3120
3121 test_31b() {
3122         touch $DIR/f31 || error "touch $DIR/f31 failed"
3123         ln $DIR/f31 $DIR/f31b || error "ln failed"
3124         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3125         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3126 }
3127 run_test 31b "unlink file with multiple links while open ======="
3128
3129 test_31c() {
3130         touch $DIR/f31 || error "touch $DIR/f31 failed"
3131         ln $DIR/f31 $DIR/f31c || error "ln failed"
3132         multiop_bg_pause $DIR/f31 O_uc ||
3133                 error "multiop_bg_pause for $DIR/f31 failed"
3134         MULTIPID=$!
3135         $MULTIOP $DIR/f31c Ouc
3136         kill -USR1 $MULTIPID
3137         wait $MULTIPID
3138 }
3139 run_test 31c "open-unlink file with multiple links ============="
3140
3141 test_31d() {
3142         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3143         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3144 }
3145 run_test 31d "remove of open directory ========================="
3146
3147 test_31e() { # bug 2904
3148         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3149 }
3150 run_test 31e "remove of open non-empty directory ==============="
3151
3152 test_31f() { # bug 4554
3153         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3154
3155         set -vx
3156         test_mkdir $DIR/d31f
3157         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3158         cp /etc/hosts $DIR/d31f
3159         ls -l $DIR/d31f
3160         $LFS getstripe $DIR/d31f/hosts
3161         multiop_bg_pause $DIR/d31f D_c || return 1
3162         MULTIPID=$!
3163
3164         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3165         test_mkdir $DIR/d31f
3166         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3167         cp /etc/hosts $DIR/d31f
3168         ls -l $DIR/d31f
3169         $LFS getstripe $DIR/d31f/hosts
3170         multiop_bg_pause $DIR/d31f D_c || return 1
3171         MULTIPID2=$!
3172
3173         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3174         wait $MULTIPID || error "first opendir $MULTIPID failed"
3175
3176         sleep 6
3177
3178         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3179         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3180         set +vx
3181 }
3182 run_test 31f "remove of open directory with open-unlink file ==="
3183
3184 test_31g() {
3185         echo "-- cross directory link --"
3186         test_mkdir -c1 $DIR/${tdir}ga
3187         test_mkdir -c1 $DIR/${tdir}gb
3188         touch $DIR/${tdir}ga/f
3189         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3190         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3191         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3192         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3193         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3194 }
3195 run_test 31g "cross directory link==============="
3196
3197 test_31h() {
3198         echo "-- cross directory link --"
3199         test_mkdir -c1 $DIR/${tdir}
3200         test_mkdir -c1 $DIR/${tdir}/dir
3201         touch $DIR/${tdir}/f
3202         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3203         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3204         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3205         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3206         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3207 }
3208 run_test 31h "cross directory link under child==============="
3209
3210 test_31i() {
3211         echo "-- cross directory link --"
3212         test_mkdir -c1 $DIR/$tdir
3213         test_mkdir -c1 $DIR/$tdir/dir
3214         touch $DIR/$tdir/dir/f
3215         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3216         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3217         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3218         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3219         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3220 }
3221 run_test 31i "cross directory link under parent==============="
3222
3223 test_31j() {
3224         test_mkdir -c1 -p $DIR/$tdir
3225         test_mkdir -c1 -p $DIR/$tdir/dir1
3226         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3227         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3228         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3229         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3230         return 0
3231 }
3232 run_test 31j "link for directory==============="
3233
3234 test_31k() {
3235         test_mkdir -c1 -p $DIR/$tdir
3236         touch $DIR/$tdir/s
3237         touch $DIR/$tdir/exist
3238         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3239         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3240         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3241         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3242         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3243         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3244         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3245         return 0
3246 }
3247 run_test 31k "link to file: the same, non-existing, dir==============="
3248
3249 test_31m() {
3250         mkdir $DIR/d31m
3251         touch $DIR/d31m/s
3252         mkdir $DIR/d31m2
3253         touch $DIR/d31m2/exist
3254         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3255         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3256         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3257         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3258         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3259         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3260         return 0
3261 }
3262 run_test 31m "link to file: the same, non-existing, dir==============="
3263
3264 test_31n() {
3265         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3266         nlink=$(stat --format=%h $DIR/$tfile)
3267         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3268         local fd=$(free_fd)
3269         local cmd="exec $fd<$DIR/$tfile"
3270         eval $cmd
3271         cmd="exec $fd<&-"
3272         trap "eval $cmd" EXIT
3273         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3274         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3275         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3276         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3277         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3278         eval $cmd
3279 }
3280 run_test 31n "check link count of unlinked file"
3281
3282 link_one() {
3283         local tempfile=$(mktemp $1_XXXXXX)
3284         mlink $tempfile $1 2> /dev/null &&
3285                 echo "$BASHPID: link $tempfile to $1 succeeded"
3286         munlink $tempfile
3287 }
3288
3289 test_31o() { # LU-2901
3290         test_mkdir $DIR/$tdir
3291         for LOOP in $(seq 100); do
3292                 rm -f $DIR/$tdir/$tfile*
3293                 for THREAD in $(seq 8); do
3294                         link_one $DIR/$tdir/$tfile.$LOOP &
3295                 done
3296                 wait
3297                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3298                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3299                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3300                         break || true
3301         done
3302 }
3303 run_test 31o "duplicate hard links with same filename"
3304
3305 test_31p() {
3306         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3307
3308         test_mkdir $DIR/$tdir
3309         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3310         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3311
3312         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3313                 error "open unlink test1 failed"
3314         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3315                 error "open unlink test2 failed"
3316
3317         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3318                 error "test1 still exists"
3319         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3320                 error "test2 still exists"
3321 }
3322 run_test 31p "remove of open striped directory"
3323
3324 cleanup_test32_mount() {
3325         local rc=0
3326         trap 0
3327         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3328         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3329         losetup -d $loopdev || true
3330         rm -rf $DIR/$tdir
3331         return $rc
3332 }
3333
3334 test_32a() {
3335         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3336
3337         echo "== more mountpoints and symlinks ================="
3338         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3339         trap cleanup_test32_mount EXIT
3340         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3341         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3342                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3343         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3344                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3345         cleanup_test32_mount
3346 }
3347 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3348
3349 test_32b() {
3350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3351
3352         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3353         trap cleanup_test32_mount EXIT
3354         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3355         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3356                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3357         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3358                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3359         cleanup_test32_mount
3360 }
3361 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3362
3363 test_32c() {
3364         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3365
3366         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3367         trap cleanup_test32_mount EXIT
3368         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3369         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3370                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3371         test_mkdir -p $DIR/$tdir/d2/test_dir
3372         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3373                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3374         cleanup_test32_mount
3375 }
3376 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3377
3378 test_32d() {
3379         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3380
3381         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3382         trap cleanup_test32_mount EXIT
3383         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3384         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3385                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3386         test_mkdir -p $DIR/$tdir/d2/test_dir
3387         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3388                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3389         cleanup_test32_mount
3390 }
3391 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3392
3393 test_32e() {
3394         rm -fr $DIR/$tdir
3395         test_mkdir -p $DIR/$tdir/tmp
3396         local tmp_dir=$DIR/$tdir/tmp
3397         ln -s $DIR/$tdir $tmp_dir/symlink11
3398         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3399         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3400         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3401 }
3402 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3403
3404 test_32f() {
3405         rm -fr $DIR/$tdir
3406         test_mkdir -p $DIR/$tdir/tmp
3407         local tmp_dir=$DIR/$tdir/tmp
3408         ln -s $DIR/$tdir $tmp_dir/symlink11
3409         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3410         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3411         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3412 }
3413 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3414
3415 test_32g() {
3416         local tmp_dir=$DIR/$tdir/tmp
3417         test_mkdir -p $tmp_dir
3418         test_mkdir $DIR/${tdir}2
3419         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3420         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3421         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3422         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3423         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3424         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3425 }
3426 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3427
3428 test_32h() {
3429         rm -fr $DIR/$tdir $DIR/${tdir}2
3430         tmp_dir=$DIR/$tdir/tmp
3431         test_mkdir -p $tmp_dir
3432         test_mkdir $DIR/${tdir}2
3433         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3434         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3435         ls $tmp_dir/symlink12 || error "listing symlink12"
3436         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3437 }
3438 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3439
3440 test_32i() {
3441         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3442
3443         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3444         trap cleanup_test32_mount EXIT
3445         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3446         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3447                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3448         touch $DIR/$tdir/test_file
3449         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3450                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3451         cleanup_test32_mount
3452 }
3453 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3454
3455 test_32j() {
3456         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3457
3458         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3459         trap cleanup_test32_mount EXIT
3460         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3461         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3462                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3463         touch $DIR/$tdir/test_file
3464         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3465                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3466         cleanup_test32_mount
3467 }
3468 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3469
3470 test_32k() {
3471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3472
3473         rm -fr $DIR/$tdir
3474         trap cleanup_test32_mount EXIT
3475         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3476         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3477                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3478         test_mkdir -p $DIR/$tdir/d2
3479         touch $DIR/$tdir/d2/test_file || error "touch failed"
3480         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3481                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3482         cleanup_test32_mount
3483 }
3484 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3485
3486 test_32l() {
3487         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3488
3489         rm -fr $DIR/$tdir
3490         trap cleanup_test32_mount EXIT
3491         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3492         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3493                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3494         test_mkdir -p $DIR/$tdir/d2
3495         touch $DIR/$tdir/d2/test_file || error "touch failed"
3496         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3497                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3498         cleanup_test32_mount
3499 }
3500 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3501
3502 test_32m() {
3503         rm -fr $DIR/d32m
3504         test_mkdir -p $DIR/d32m/tmp
3505         TMP_DIR=$DIR/d32m/tmp
3506         ln -s $DIR $TMP_DIR/symlink11
3507         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3508         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3509                 error "symlink11 not a link"
3510         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3511                 error "symlink01 not a link"
3512 }
3513 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3514
3515 test_32n() {
3516         rm -fr $DIR/d32n
3517         test_mkdir -p $DIR/d32n/tmp
3518         TMP_DIR=$DIR/d32n/tmp
3519         ln -s $DIR $TMP_DIR/symlink11
3520         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3521         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3522         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3523 }
3524 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3525
3526 test_32o() {
3527         touch $DIR/$tfile
3528         test_mkdir -p $DIR/d32o/tmp
3529         TMP_DIR=$DIR/d32o/tmp
3530         ln -s $DIR/$tfile $TMP_DIR/symlink12
3531         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3532         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3533                 error "symlink12 not a link"
3534         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3535         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3536                 error "$DIR/d32o/tmp/symlink12 not file type"
3537         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3538                 error "$DIR/d32o/symlink02 not file type"
3539 }
3540 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3541
3542 test_32p() {
3543         log 32p_1
3544         rm -fr $DIR/d32p
3545         log 32p_2
3546         rm -f $DIR/$tfile
3547         log 32p_3
3548         touch $DIR/$tfile
3549         log 32p_4
3550         test_mkdir -p $DIR/d32p/tmp
3551         log 32p_5
3552         TMP_DIR=$DIR/d32p/tmp
3553         log 32p_6
3554         ln -s $DIR/$tfile $TMP_DIR/symlink12
3555         log 32p_7
3556         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3557         log 32p_8
3558         cat $DIR/d32p/tmp/symlink12 ||
3559                 error "Can't open $DIR/d32p/tmp/symlink12"
3560         log 32p_9
3561         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3562         log 32p_10
3563 }
3564 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3565
3566 test_32q() {
3567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3568
3569         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3570         trap cleanup_test32_mount EXIT
3571         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3572         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3573         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3574                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3575         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3576         cleanup_test32_mount
3577 }
3578 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3579
3580 test_32r() {
3581         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3582
3583         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3584         trap cleanup_test32_mount EXIT
3585         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3586         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3587         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3588                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3589         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3590         cleanup_test32_mount
3591 }
3592 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3593
3594 test_33aa() {
3595         rm -f $DIR/$tfile
3596         touch $DIR/$tfile
3597         chmod 444 $DIR/$tfile
3598         chown $RUNAS_ID $DIR/$tfile
3599         log 33_1
3600         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3601         log 33_2
3602 }
3603 run_test 33aa "write file with mode 444 (should return error)"
3604
3605 test_33a() {
3606         rm -fr $DIR/$tdir
3607         test_mkdir $DIR/$tdir
3608         chown $RUNAS_ID $DIR/$tdir
3609         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3610                 error "$RUNAS create $tdir/$tfile failed"
3611         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3612                 error "open RDWR" || true
3613 }
3614 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3615
3616 test_33b() {
3617         rm -fr $DIR/$tdir
3618         test_mkdir $DIR/$tdir
3619         chown $RUNAS_ID $DIR/$tdir
3620         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3621 }
3622 run_test 33b "test open file with malformed flags (No panic)"
3623
3624 test_33c() {
3625         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3626         remote_ost_nodsh && skip "remote OST with nodsh"
3627
3628         local ostnum
3629         local ostname
3630         local write_bytes
3631         local all_zeros
3632
3633         all_zeros=:
3634         rm -fr $DIR/$tdir
3635         test_mkdir $DIR/$tdir
3636         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3637
3638         sync
3639         for ostnum in $(seq $OSTCOUNT); do
3640                 # test-framework's OST numbering is one-based, while Lustre's
3641                 # is zero-based
3642                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3643                 # Parsing llobdstat's output sucks; we could grep the /proc
3644                 # path, but that's likely to not be as portable as using the
3645                 # llobdstat utility.  So we parse lctl output instead.
3646                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3647                         obdfilter/$ostname/stats |
3648                         awk '/^write_bytes/ {print $7}' )
3649                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3650                 if (( ${write_bytes:-0} > 0 ))
3651                 then
3652                         all_zeros=false
3653                         break;
3654                 fi
3655         done
3656
3657         $all_zeros || return 0
3658
3659         # Write four bytes
3660         echo foo > $DIR/$tdir/bar
3661         # Really write them
3662         sync
3663
3664         # Total up write_bytes after writing.  We'd better find non-zeros.
3665         for ostnum in $(seq $OSTCOUNT); do
3666                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3667                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3668                         obdfilter/$ostname/stats |
3669                         awk '/^write_bytes/ {print $7}' )
3670                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3671                 if (( ${write_bytes:-0} > 0 ))
3672                 then
3673                         all_zeros=false
3674                         break;
3675                 fi
3676         done
3677
3678         if $all_zeros
3679         then
3680                 for ostnum in $(seq $OSTCOUNT); do
3681                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3682                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3683                         do_facet ost$ostnum lctl get_param -n \
3684                                 obdfilter/$ostname/stats
3685                 done
3686                 error "OST not keeping write_bytes stats (b22312)"
3687         fi
3688 }
3689 run_test 33c "test llobdstat and write_bytes"
3690
3691 test_33d() {
3692         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3693         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3694
3695         local MDTIDX=1
3696         local remote_dir=$DIR/$tdir/remote_dir
3697
3698         test_mkdir $DIR/$tdir
3699         $LFS mkdir -i $MDTIDX $remote_dir ||
3700                 error "create remote directory failed"
3701
3702         touch $remote_dir/$tfile
3703         chmod 444 $remote_dir/$tfile
3704         chown $RUNAS_ID $remote_dir/$tfile
3705
3706         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3707
3708         chown $RUNAS_ID $remote_dir
3709         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3710                                         error "create" || true
3711         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3712                                     error "open RDWR" || true
3713         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3714 }
3715 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3716
3717 test_33e() {
3718         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3719
3720         mkdir $DIR/$tdir
3721
3722         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3723         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3724         mkdir $DIR/$tdir/local_dir
3725
3726         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3727         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3728         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3729
3730         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3731                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3732
3733         rmdir $DIR/$tdir/* || error "rmdir failed"
3734
3735         umask 777
3736         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3737         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3738         mkdir $DIR/$tdir/local_dir
3739
3740         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3741         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3742         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3743
3744         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3745                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3746
3747         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3748
3749         umask 000
3750         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3751         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3752         mkdir $DIR/$tdir/local_dir
3753
3754         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3755         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3756         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3757
3758         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3759                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3760 }
3761 run_test 33e "mkdir and striped directory should have same mode"
3762
3763 cleanup_33f() {
3764         trap 0
3765         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3766 }
3767
3768 test_33f() {
3769         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3770         remote_mds_nodsh && skip "remote MDS with nodsh"
3771
3772         mkdir $DIR/$tdir
3773         chmod go+rwx $DIR/$tdir
3774         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3775         trap cleanup_33f EXIT
3776
3777         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3778                 error "cannot create striped directory"
3779
3780         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3781                 error "cannot create files in striped directory"
3782
3783         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3784                 error "cannot remove files in striped directory"
3785
3786         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3787                 error "cannot remove striped directory"
3788
3789         cleanup_33f
3790 }
3791 run_test 33f "nonroot user can create, access, and remove a striped directory"
3792
3793 test_33g() {
3794         mkdir -p $DIR/$tdir/dir2
3795
3796         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3797         echo $err
3798         [[ $err =~ "exists" ]] || error "Not exists error"
3799 }
3800 run_test 33g "nonroot user create already existing root created file"
3801
3802 test_33h() {
3803         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3804         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
3805                 skip "Need MDS version at least 2.13.50"
3806
3807         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
3808                 error "mkdir $tdir failed"
3809         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
3810
3811         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
3812         local index2
3813
3814         for fname in $DIR/$tdir/$tfile.bak \
3815                      $DIR/$tdir/$tfile.SAV \
3816                      $DIR/$tdir/$tfile.orig \
3817                      $DIR/$tdir/$tfile~; do
3818                 touch $fname  || error "touch $fname failed"
3819                 index2=$($LFS getstripe -m $fname)
3820                 [ $index -eq $index2 ] ||
3821                         error "$fname MDT index mismatch $index != $index2"
3822         done
3823
3824         local failed=0
3825         for i in {1..50}; do
3826                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
3827                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
3828                         touch $fname  || error "touch $fname failed"
3829                         index2=$($LFS getstripe -m $fname)
3830                         if [[ $index != $index2 ]]; then
3831                                 failed=$((failed + 1))
3832                                 echo "$fname MDT index mismatch $index != $index2"
3833                         fi
3834                 done
3835         done
3836         echo "$failed MDT index mismatches"
3837         (( failed < 4 )) || error "MDT index mismatch $failed times"
3838
3839 }
3840 run_test 33h "temp file is located on the same MDT as target"
3841
3842 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3843 test_34a() {
3844         rm -f $DIR/f34
3845         $MCREATE $DIR/f34 || error "mcreate failed"
3846         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3847                 error "getstripe failed"
3848         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3849         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3850                 error "getstripe failed"
3851         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3852                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3853 }
3854 run_test 34a "truncate file that has not been opened ==========="
3855
3856 test_34b() {
3857         [ ! -f $DIR/f34 ] && test_34a
3858         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3859                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3860         $OPENFILE -f O_RDONLY $DIR/f34
3861         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3862                 error "getstripe failed"
3863         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3864                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3865 }
3866 run_test 34b "O_RDONLY opening file doesn't create objects ====="
3867
3868 test_34c() {
3869         [ ! -f $DIR/f34 ] && test_34a
3870         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3871                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3872         $OPENFILE -f O_RDWR $DIR/f34
3873         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3874                 error "$LFS getstripe failed"
3875         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3876                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3877 }
3878 run_test 34c "O_RDWR opening file-with-size works =============="
3879
3880 test_34d() {
3881         [ ! -f $DIR/f34 ] && test_34a
3882         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3883                 error "dd failed"
3884         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3885                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3886         rm $DIR/f34
3887 }
3888 run_test 34d "write to sparse file ============================="
3889
3890 test_34e() {
3891         rm -f $DIR/f34e
3892         $MCREATE $DIR/f34e || error "mcreate failed"
3893         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3894         $CHECKSTAT -s 1000 $DIR/f34e ||
3895                 error "Size of $DIR/f34e not equal to 1000 bytes"
3896         $OPENFILE -f O_RDWR $DIR/f34e
3897         $CHECKSTAT -s 1000 $DIR/f34e ||
3898                 error "Size of $DIR/f34e not equal to 1000 bytes"
3899 }
3900 run_test 34e "create objects, some with size and some without =="
3901
3902 test_34f() { # bug 6242, 6243
3903         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3904
3905         SIZE34F=48000
3906         rm -f $DIR/f34f
3907         $MCREATE $DIR/f34f || error "mcreate failed"
3908         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3909         dd if=$DIR/f34f of=$TMP/f34f
3910         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3911         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3912         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3913         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3914         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3915 }
3916 run_test 34f "read from a file with no objects until EOF ======="
3917
3918 test_34g() {
3919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3920
3921         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3922                 error "dd failed"
3923         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3924         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3925                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3926         cancel_lru_locks osc
3927         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3928                 error "wrong size after lock cancel"
3929
3930         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3931         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3932                 error "expanding truncate failed"
3933         cancel_lru_locks osc
3934         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3935                 error "wrong expanded size after lock cancel"
3936 }
3937 run_test 34g "truncate long file ==============================="
3938
3939 test_34h() {
3940         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3941
3942         local gid=10
3943         local sz=1000
3944
3945         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3946         sync # Flush the cache so that multiop below does not block on cache
3947              # flush when getting the group lock
3948         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3949         MULTIPID=$!
3950
3951         # Since just timed wait is not good enough, let's do a sync write
3952         # that way we are sure enough time for a roundtrip + processing
3953         # passed + 2 seconds of extra margin.
3954         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3955         rm $DIR/${tfile}-1
3956         sleep 2
3957
3958         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3959                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3960                 kill -9 $MULTIPID
3961         fi
3962         wait $MULTIPID
3963         local nsz=`stat -c %s $DIR/$tfile`
3964         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3965 }
3966 run_test 34h "ftruncate file under grouplock should not block"
3967
3968 test_35a() {
3969         cp /bin/sh $DIR/f35a
3970         chmod 444 $DIR/f35a
3971         chown $RUNAS_ID $DIR/f35a
3972         $RUNAS $DIR/f35a && error || true
3973         rm $DIR/f35a
3974 }
3975 run_test 35a "exec file with mode 444 (should return and not leak)"
3976
3977 test_36a() {
3978         rm -f $DIR/f36
3979         utime $DIR/f36 || error "utime failed for MDS"
3980 }
3981 run_test 36a "MDS utime check (mknod, utime)"
3982
3983 test_36b() {
3984         echo "" > $DIR/f36
3985         utime $DIR/f36 || error "utime failed for OST"
3986 }
3987 run_test 36b "OST utime check (open, utime)"
3988
3989 test_36c() {
3990         rm -f $DIR/d36/f36
3991         test_mkdir $DIR/d36
3992         chown $RUNAS_ID $DIR/d36
3993         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3994 }
3995 run_test 36c "non-root MDS utime check (mknod, utime)"
3996
3997 test_36d() {
3998         [ ! -d $DIR/d36 ] && test_36c
3999         echo "" > $DIR/d36/f36
4000         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
4001 }
4002 run_test 36d "non-root OST utime check (open, utime)"
4003
4004 test_36e() {
4005         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
4006
4007         test_mkdir $DIR/$tdir
4008         touch $DIR/$tdir/$tfile
4009         $RUNAS utime $DIR/$tdir/$tfile &&
4010                 error "utime worked, expected failure" || true
4011 }
4012 run_test 36e "utime on non-owned file (should return error)"
4013
4014 subr_36fh() {
4015         local fl="$1"
4016         local LANG_SAVE=$LANG
4017         local LC_LANG_SAVE=$LC_LANG
4018         export LANG=C LC_LANG=C # for date language
4019
4020         DATESTR="Dec 20  2000"
4021         test_mkdir $DIR/$tdir
4022         lctl set_param fail_loc=$fl
4023         date; date +%s
4024         cp /etc/hosts $DIR/$tdir/$tfile
4025         sync & # write RPC generated with "current" inode timestamp, but delayed
4026         sleep 1
4027         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4028         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4029         cancel_lru_locks $OSC
4030         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4031         date; date +%s
4032         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4033                 echo "BEFORE: $LS_BEFORE" && \
4034                 echo "AFTER : $LS_AFTER" && \
4035                 echo "WANT  : $DATESTR" && \
4036                 error "$DIR/$tdir/$tfile timestamps changed" || true
4037
4038         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4039 }
4040
4041 test_36f() {
4042         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4043
4044         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4045         subr_36fh "0x80000214"
4046 }
4047 run_test 36f "utime on file racing with OST BRW write =========="
4048
4049 test_36g() {
4050         remote_ost_nodsh && skip "remote OST with nodsh"
4051         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4052         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4053                 skip "Need MDS version at least 2.12.51"
4054
4055         local fmd_max_age
4056         local fmd
4057         local facet="ost1"
4058         local tgt="obdfilter"
4059
4060         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4061
4062         test_mkdir $DIR/$tdir
4063         fmd_max_age=$(do_facet $facet \
4064                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4065                 head -n 1")
4066
4067         echo "FMD max age: ${fmd_max_age}s"
4068         touch $DIR/$tdir/$tfile
4069         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4070                 gawk '{cnt=cnt+$1}  END{print cnt}')
4071         echo "FMD before: $fmd"
4072         [[ $fmd == 0 ]] &&
4073                 error "FMD wasn't create by touch"
4074         sleep $((fmd_max_age + 12))
4075         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4076                 gawk '{cnt=cnt+$1}  END{print cnt}')
4077         echo "FMD after: $fmd"
4078         [[ $fmd == 0 ]] ||
4079                 error "FMD wasn't expired by ping"
4080 }
4081 run_test 36g "FMD cache expiry ====================="
4082
4083 test_36h() {
4084         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4085
4086         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4087         subr_36fh "0x80000227"
4088 }
4089 run_test 36h "utime on file racing with OST BRW write =========="
4090
4091 test_36i() {
4092         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4093
4094         test_mkdir $DIR/$tdir
4095         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4096
4097         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4098         local new_mtime=$((mtime + 200))
4099
4100         #change Modify time of striped dir
4101         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4102                         error "change mtime failed"
4103
4104         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4105
4106         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4107 }
4108 run_test 36i "change mtime on striped directory"
4109
4110 # test_37 - duplicate with tests 32q 32r
4111
4112 test_38() {
4113         local file=$DIR/$tfile
4114         touch $file
4115         openfile -f O_DIRECTORY $file
4116         local RC=$?
4117         local ENOTDIR=20
4118         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4119         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4120 }
4121 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4122
4123 test_39a() { # was test_39
4124         touch $DIR/$tfile
4125         touch $DIR/${tfile}2
4126 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4127 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4128 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4129         sleep 2
4130         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4131         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4132                 echo "mtime"
4133                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4134                 echo "atime"
4135                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4136                 echo "ctime"
4137                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4138                 error "O_TRUNC didn't change timestamps"
4139         fi
4140 }
4141 run_test 39a "mtime changed on create"
4142
4143 test_39b() {
4144         test_mkdir -c1 $DIR/$tdir
4145         cp -p /etc/passwd $DIR/$tdir/fopen
4146         cp -p /etc/passwd $DIR/$tdir/flink
4147         cp -p /etc/passwd $DIR/$tdir/funlink
4148         cp -p /etc/passwd $DIR/$tdir/frename
4149         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4150
4151         sleep 1
4152         echo "aaaaaa" >> $DIR/$tdir/fopen
4153         echo "aaaaaa" >> $DIR/$tdir/flink
4154         echo "aaaaaa" >> $DIR/$tdir/funlink
4155         echo "aaaaaa" >> $DIR/$tdir/frename
4156
4157         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4158         local link_new=`stat -c %Y $DIR/$tdir/flink`
4159         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4160         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4161
4162         cat $DIR/$tdir/fopen > /dev/null
4163         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4164         rm -f $DIR/$tdir/funlink2
4165         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4166
4167         for (( i=0; i < 2; i++ )) ; do
4168                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4169                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4170                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4171                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4172
4173                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4174                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4175                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4176                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4177
4178                 cancel_lru_locks $OSC
4179                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4180         done
4181 }
4182 run_test 39b "mtime change on open, link, unlink, rename  ======"
4183
4184 # this should be set to past
4185 TEST_39_MTIME=`date -d "1 year ago" +%s`
4186
4187 # bug 11063
4188 test_39c() {
4189         touch $DIR1/$tfile
4190         sleep 2
4191         local mtime0=`stat -c %Y $DIR1/$tfile`
4192
4193         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4194         local mtime1=`stat -c %Y $DIR1/$tfile`
4195         [ "$mtime1" = $TEST_39_MTIME ] || \
4196                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4197
4198         local d1=`date +%s`
4199         echo hello >> $DIR1/$tfile
4200         local d2=`date +%s`
4201         local mtime2=`stat -c %Y $DIR1/$tfile`
4202         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4203                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4204
4205         mv $DIR1/$tfile $DIR1/$tfile-1
4206
4207         for (( i=0; i < 2; i++ )) ; do
4208                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4209                 [ "$mtime2" = "$mtime3" ] || \
4210                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4211
4212                 cancel_lru_locks $OSC
4213                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4214         done
4215 }
4216 run_test 39c "mtime change on rename ==========================="
4217
4218 # bug 21114
4219 test_39d() {
4220         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4221
4222         touch $DIR1/$tfile
4223         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4224
4225         for (( i=0; i < 2; i++ )) ; do
4226                 local mtime=`stat -c %Y $DIR1/$tfile`
4227                 [ $mtime = $TEST_39_MTIME ] || \
4228                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4229
4230                 cancel_lru_locks $OSC
4231                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4232         done
4233 }
4234 run_test 39d "create, utime, stat =============================="
4235
4236 # bug 21114
4237 test_39e() {
4238         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4239
4240         touch $DIR1/$tfile
4241         local mtime1=`stat -c %Y $DIR1/$tfile`
4242
4243         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4244
4245         for (( i=0; i < 2; i++ )) ; do
4246                 local mtime2=`stat -c %Y $DIR1/$tfile`
4247                 [ $mtime2 = $TEST_39_MTIME ] || \
4248                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4249
4250                 cancel_lru_locks $OSC
4251                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4252         done
4253 }
4254 run_test 39e "create, stat, utime, stat ========================"
4255
4256 # bug 21114
4257 test_39f() {
4258         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4259
4260         touch $DIR1/$tfile
4261         mtime1=`stat -c %Y $DIR1/$tfile`
4262
4263         sleep 2
4264         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4265
4266         for (( i=0; i < 2; i++ )) ; do
4267                 local mtime2=`stat -c %Y $DIR1/$tfile`
4268                 [ $mtime2 = $TEST_39_MTIME ] || \
4269                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4270
4271                 cancel_lru_locks $OSC
4272                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4273         done
4274 }
4275 run_test 39f "create, stat, sleep, utime, stat ================="
4276
4277 # bug 11063
4278 test_39g() {
4279         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4280
4281         echo hello >> $DIR1/$tfile
4282         local mtime1=`stat -c %Y $DIR1/$tfile`
4283
4284         sleep 2
4285         chmod o+r $DIR1/$tfile
4286
4287         for (( i=0; i < 2; i++ )) ; do
4288                 local mtime2=`stat -c %Y $DIR1/$tfile`
4289                 [ "$mtime1" = "$mtime2" ] || \
4290                         error "lost mtime: $mtime2, should be $mtime1"
4291
4292                 cancel_lru_locks $OSC
4293                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4294         done
4295 }
4296 run_test 39g "write, chmod, stat ==============================="
4297
4298 # bug 11063
4299 test_39h() {
4300         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4301
4302         touch $DIR1/$tfile
4303         sleep 1
4304
4305         local d1=`date`
4306         echo hello >> $DIR1/$tfile
4307         local mtime1=`stat -c %Y $DIR1/$tfile`
4308
4309         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4310         local d2=`date`
4311         if [ "$d1" != "$d2" ]; then
4312                 echo "write and touch not within one second"
4313         else
4314                 for (( i=0; i < 2; i++ )) ; do
4315                         local mtime2=`stat -c %Y $DIR1/$tfile`
4316                         [ "$mtime2" = $TEST_39_MTIME ] || \
4317                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4318
4319                         cancel_lru_locks $OSC
4320                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4321                 done
4322         fi
4323 }
4324 run_test 39h "write, utime within one second, stat ============="
4325
4326 test_39i() {
4327         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4328
4329         touch $DIR1/$tfile
4330         sleep 1
4331
4332         echo hello >> $DIR1/$tfile
4333         local mtime1=`stat -c %Y $DIR1/$tfile`
4334
4335         mv $DIR1/$tfile $DIR1/$tfile-1
4336
4337         for (( i=0; i < 2; i++ )) ; do
4338                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4339
4340                 [ "$mtime1" = "$mtime2" ] || \
4341                         error "lost mtime: $mtime2, should be $mtime1"
4342
4343                 cancel_lru_locks $OSC
4344                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4345         done
4346 }
4347 run_test 39i "write, rename, stat =============================="
4348
4349 test_39j() {
4350         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4351
4352         start_full_debug_logging
4353         touch $DIR1/$tfile
4354         sleep 1
4355
4356         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4357         lctl set_param fail_loc=0x80000412
4358         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4359                 error "multiop failed"
4360         local multipid=$!
4361         local mtime1=`stat -c %Y $DIR1/$tfile`
4362
4363         mv $DIR1/$tfile $DIR1/$tfile-1
4364
4365         kill -USR1 $multipid
4366         wait $multipid || error "multiop close failed"
4367
4368         for (( i=0; i < 2; i++ )) ; do
4369                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4370                 [ "$mtime1" = "$mtime2" ] ||
4371                         error "mtime is lost on close: $mtime2, " \
4372                               "should be $mtime1"
4373
4374                 cancel_lru_locks
4375                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4376         done
4377         lctl set_param fail_loc=0
4378         stop_full_debug_logging
4379 }
4380 run_test 39j "write, rename, close, stat ======================="
4381
4382 test_39k() {
4383         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4384
4385         touch $DIR1/$tfile
4386         sleep 1
4387
4388         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4389         local multipid=$!
4390         local mtime1=`stat -c %Y $DIR1/$tfile`
4391
4392         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4393
4394         kill -USR1 $multipid
4395         wait $multipid || error "multiop close failed"
4396
4397         for (( i=0; i < 2; i++ )) ; do
4398                 local mtime2=`stat -c %Y $DIR1/$tfile`
4399
4400                 [ "$mtime2" = $TEST_39_MTIME ] || \
4401                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4402
4403                 cancel_lru_locks
4404                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4405         done
4406 }
4407 run_test 39k "write, utime, close, stat ========================"
4408
4409 # this should be set to future
4410 TEST_39_ATIME=`date -d "1 year" +%s`
4411
4412 test_39l() {
4413         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4414         remote_mds_nodsh && skip "remote MDS with nodsh"
4415
4416         local atime_diff=$(do_facet $SINGLEMDS \
4417                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4418         rm -rf $DIR/$tdir
4419         mkdir -p $DIR/$tdir
4420
4421         # test setting directory atime to future
4422         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4423         local atime=$(stat -c %X $DIR/$tdir)
4424         [ "$atime" = $TEST_39_ATIME ] ||
4425                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4426
4427         # test setting directory atime from future to now
4428         local now=$(date +%s)
4429         touch -a -d @$now $DIR/$tdir
4430
4431         atime=$(stat -c %X $DIR/$tdir)
4432         [ "$atime" -eq "$now"  ] ||
4433                 error "atime is not updated from future: $atime, $now"
4434
4435         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4436         sleep 3
4437
4438         # test setting directory atime when now > dir atime + atime_diff
4439         local d1=$(date +%s)
4440         ls $DIR/$tdir
4441         local d2=$(date +%s)
4442         cancel_lru_locks mdc
4443         atime=$(stat -c %X $DIR/$tdir)
4444         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4445                 error "atime is not updated  : $atime, should be $d2"
4446
4447         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4448         sleep 3
4449
4450         # test not setting directory atime when now < dir atime + atime_diff
4451         ls $DIR/$tdir
4452         cancel_lru_locks mdc
4453         atime=$(stat -c %X $DIR/$tdir)
4454         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4455                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4456
4457         do_facet $SINGLEMDS \
4458                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4459 }
4460 run_test 39l "directory atime update ==========================="
4461
4462 test_39m() {
4463         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4464
4465         touch $DIR1/$tfile
4466         sleep 2
4467         local far_past_mtime=$(date -d "May 29 1953" +%s)
4468         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4469
4470         touch -m -d @$far_past_mtime $DIR1/$tfile
4471         touch -a -d @$far_past_atime $DIR1/$tfile
4472
4473         for (( i=0; i < 2; i++ )) ; do
4474                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4475                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4476                         error "atime or mtime set incorrectly"
4477
4478                 cancel_lru_locks $OSC
4479                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4480         done
4481 }
4482 run_test 39m "test atime and mtime before 1970"
4483
4484 test_39n() { # LU-3832
4485         remote_mds_nodsh && skip "remote MDS with nodsh"
4486
4487         local atime_diff=$(do_facet $SINGLEMDS \
4488                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4489         local atime0
4490         local atime1
4491         local atime2
4492
4493         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4494
4495         rm -rf $DIR/$tfile
4496         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4497         atime0=$(stat -c %X $DIR/$tfile)
4498
4499         sleep 5
4500         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4501         atime1=$(stat -c %X $DIR/$tfile)
4502
4503         sleep 5
4504         cancel_lru_locks mdc
4505         cancel_lru_locks osc
4506         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4507         atime2=$(stat -c %X $DIR/$tfile)
4508
4509         do_facet $SINGLEMDS \
4510                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4511
4512         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4513         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4514 }
4515 run_test 39n "check that O_NOATIME is honored"
4516
4517 test_39o() {
4518         TESTDIR=$DIR/$tdir/$tfile
4519         [ -e $TESTDIR ] && rm -rf $TESTDIR
4520         mkdir -p $TESTDIR
4521         cd $TESTDIR
4522         links1=2
4523         ls
4524         mkdir a b
4525         ls
4526         links2=$(stat -c %h .)
4527         [ $(($links1 + 2)) != $links2 ] &&
4528                 error "wrong links count $(($links1 + 2)) != $links2"
4529         rmdir b
4530         links3=$(stat -c %h .)
4531         [ $(($links1 + 1)) != $links3 ] &&
4532                 error "wrong links count $links1 != $links3"
4533         return 0
4534 }
4535 run_test 39o "directory cached attributes updated after create"
4536
4537 test_39p() {
4538         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4539
4540         local MDTIDX=1
4541         TESTDIR=$DIR/$tdir/$tdir
4542         [ -e $TESTDIR ] && rm -rf $TESTDIR
4543         test_mkdir -p $TESTDIR
4544         cd $TESTDIR
4545         links1=2
4546         ls
4547         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4548         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4549         ls
4550         links2=$(stat -c %h .)
4551         [ $(($links1 + 2)) != $links2 ] &&
4552                 error "wrong links count $(($links1 + 2)) != $links2"
4553         rmdir remote_dir2
4554         links3=$(stat -c %h .)
4555         [ $(($links1 + 1)) != $links3 ] &&
4556                 error "wrong links count $links1 != $links3"
4557         return 0
4558 }
4559 run_test 39p "remote directory cached attributes updated after create ========"
4560
4561 test_39r() {
4562         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
4563                 skip "no atime update on old OST"
4564         if [ "$ost1_FSTYPE" != ldiskfs ]; then
4565                 skip_env "ldiskfs only test"
4566         fi
4567
4568         local saved_adiff
4569         saved_adiff=$(do_facet ost1 \
4570                 lctl get_param -n obdfilter.*OST0000.atime_diff)
4571         stack_trap "do_facet ost1 \
4572                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
4573
4574         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
4575
4576         $LFS setstripe -i 0 $DIR/$tfile
4577         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
4578                 error "can't write initial file"
4579         cancel_lru_locks osc
4580
4581         # exceed atime_diff and access file
4582         sleep 6
4583         dd if=$DIR/$tfile of=/dev/null || error "can't udpate atime"
4584
4585         local atime_cli=$(stat -c %X $DIR/$tfile)
4586         echo "client atime: $atime_cli"
4587         # allow atime update to be written to device
4588         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
4589         sleep 5
4590
4591         local ostdev=$(ostdevname 1)
4592         local fid=($(lfs getstripe -y $DIR/$tfile |
4593                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
4594         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
4595         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
4596
4597         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
4598         local atime_ost=$(do_facet ost1 "$cmd" |&
4599                           awk -F'[: ]' '/atime:/ { print $4 }')
4600         (( atime_cli == atime_ost )) ||
4601                 error "atime on client $atime_cli != ost $atime_ost"
4602 }
4603 run_test 39r "lazy atime update on OST"
4604
4605 test_39q() { # LU-8041
4606         local testdir=$DIR/$tdir
4607         mkdir -p $testdir
4608         multiop_bg_pause $testdir D_c || error "multiop failed"
4609         local multipid=$!
4610         cancel_lru_locks mdc
4611         kill -USR1 $multipid
4612         local atime=$(stat -c %X $testdir)
4613         [ "$atime" -ne 0 ] || error "atime is zero"
4614 }
4615 run_test 39q "close won't zero out atime"
4616
4617 test_40() {
4618         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4619         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4620                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4621         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4622                 error "$tfile is not 4096 bytes in size"
4623 }
4624 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4625
4626 test_41() {
4627         # bug 1553
4628         small_write $DIR/f41 18
4629 }
4630 run_test 41 "test small file write + fstat ====================="
4631
4632 count_ost_writes() {
4633         lctl get_param -n ${OSC}.*.stats |
4634                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4635                         END { printf("%0.0f", writes) }'
4636 }
4637
4638 # decent default
4639 WRITEBACK_SAVE=500
4640 DIRTY_RATIO_SAVE=40
4641 MAX_DIRTY_RATIO=50
4642 BG_DIRTY_RATIO_SAVE=10
4643 MAX_BG_DIRTY_RATIO=25
4644
4645 start_writeback() {
4646         trap 0
4647         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4648         # dirty_ratio, dirty_background_ratio
4649         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4650                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4651                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4652                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4653         else
4654                 # if file not here, we are a 2.4 kernel
4655                 kill -CONT `pidof kupdated`
4656         fi
4657 }
4658
4659 stop_writeback() {
4660         # setup the trap first, so someone cannot exit the test at the
4661         # exact wrong time and mess up a machine
4662         trap start_writeback EXIT
4663         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4664         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4665                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4666                 sysctl -w vm.dirty_writeback_centisecs=0
4667                 sysctl -w vm.dirty_writeback_centisecs=0
4668                 # save and increase /proc/sys/vm/dirty_ratio
4669                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4670                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4671                 # save and increase /proc/sys/vm/dirty_background_ratio
4672                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4673                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4674         else
4675                 # if file not here, we are a 2.4 kernel
4676                 kill -STOP `pidof kupdated`
4677         fi
4678 }
4679
4680 # ensure that all stripes have some grant before we test client-side cache
4681 setup_test42() {
4682         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4683                 dd if=/dev/zero of=$i bs=4k count=1
4684                 rm $i
4685         done
4686 }
4687
4688 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4689 # file truncation, and file removal.
4690 test_42a() {
4691         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4692
4693         setup_test42
4694         cancel_lru_locks $OSC
4695         stop_writeback
4696         sync; sleep 1; sync # just to be safe
4697         BEFOREWRITES=`count_ost_writes`
4698         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4699         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4700         AFTERWRITES=`count_ost_writes`
4701         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4702                 error "$BEFOREWRITES < $AFTERWRITES"
4703         start_writeback
4704 }
4705 run_test 42a "ensure that we don't flush on close"
4706
4707 test_42b() {
4708         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4709
4710         setup_test42
4711         cancel_lru_locks $OSC
4712         stop_writeback
4713         sync
4714         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4715         BEFOREWRITES=$(count_ost_writes)
4716         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4717         AFTERWRITES=$(count_ost_writes)
4718         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4719                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4720         fi
4721         BEFOREWRITES=$(count_ost_writes)
4722         sync || error "sync: $?"
4723         AFTERWRITES=$(count_ost_writes)
4724         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4725                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4726         fi
4727         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4728         start_writeback
4729         return 0
4730 }
4731 run_test 42b "test destroy of file with cached dirty data ======"
4732
4733 # if these tests just want to test the effect of truncation,
4734 # they have to be very careful.  consider:
4735 # - the first open gets a {0,EOF}PR lock
4736 # - the first write conflicts and gets a {0, count-1}PW
4737 # - the rest of the writes are under {count,EOF}PW
4738 # - the open for truncate tries to match a {0,EOF}PR
4739 #   for the filesize and cancels the PWs.
4740 # any number of fixes (don't get {0,EOF} on open, match
4741 # composite locks, do smarter file size management) fix
4742 # this, but for now we want these tests to verify that
4743 # the cancellation with truncate intent works, so we
4744 # start the file with a full-file pw lock to match against
4745 # until the truncate.
4746 trunc_test() {
4747         test=$1
4748         file=$DIR/$test
4749         offset=$2
4750         cancel_lru_locks $OSC
4751         stop_writeback
4752         # prime the file with 0,EOF PW to match
4753         touch $file
4754         $TRUNCATE $file 0
4755         sync; sync
4756         # now the real test..
4757         dd if=/dev/zero of=$file bs=1024 count=100
4758         BEFOREWRITES=`count_ost_writes`
4759         $TRUNCATE $file $offset
4760         cancel_lru_locks $OSC
4761         AFTERWRITES=`count_ost_writes`
4762         start_writeback
4763 }
4764
4765 test_42c() {
4766         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4767
4768         trunc_test 42c 1024
4769         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4770                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4771         rm $file
4772 }
4773 run_test 42c "test partial truncate of file with cached dirty data"
4774
4775 test_42d() {
4776         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4777
4778         trunc_test 42d 0
4779         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4780                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4781         rm $file
4782 }
4783 run_test 42d "test complete truncate of file with cached dirty data"
4784
4785 test_42e() { # bug22074
4786         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4787
4788         local TDIR=$DIR/${tdir}e
4789         local pages=16 # hardcoded 16 pages, don't change it.
4790         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4791         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4792         local max_dirty_mb
4793         local warmup_files
4794
4795         test_mkdir $DIR/${tdir}e
4796         $LFS setstripe -c 1 $TDIR
4797         createmany -o $TDIR/f $files
4798
4799         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4800
4801         # we assume that with $OSTCOUNT files, at least one of them will
4802         # be allocated on OST0.
4803         warmup_files=$((OSTCOUNT * max_dirty_mb))
4804         createmany -o $TDIR/w $warmup_files
4805
4806         # write a large amount of data into one file and sync, to get good
4807         # avail_grant number from OST.
4808         for ((i=0; i<$warmup_files; i++)); do
4809                 idx=$($LFS getstripe -i $TDIR/w$i)
4810                 [ $idx -ne 0 ] && continue
4811                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4812                 break
4813         done
4814         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4815         sync
4816         $LCTL get_param $proc_osc0/cur_dirty_bytes
4817         $LCTL get_param $proc_osc0/cur_grant_bytes
4818
4819         # create as much dirty pages as we can while not to trigger the actual
4820         # RPCs directly. but depends on the env, VFS may trigger flush during this
4821         # period, hopefully we are good.
4822         for ((i=0; i<$warmup_files; i++)); do
4823                 idx=$($LFS getstripe -i $TDIR/w$i)
4824                 [ $idx -ne 0 ] && continue
4825                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4826         done
4827         $LCTL get_param $proc_osc0/cur_dirty_bytes
4828         $LCTL get_param $proc_osc0/cur_grant_bytes
4829
4830         # perform the real test
4831         $LCTL set_param $proc_osc0/rpc_stats 0
4832         for ((;i<$files; i++)); do
4833                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4834                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4835         done
4836         sync
4837         $LCTL get_param $proc_osc0/rpc_stats
4838
4839         local percent=0
4840         local have_ppr=false
4841         $LCTL get_param $proc_osc0/rpc_stats |
4842                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4843                         # skip lines until we are at the RPC histogram data
4844                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4845                         $have_ppr || continue
4846
4847                         # we only want the percent stat for < 16 pages
4848                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4849
4850                         percent=$((percent + WPCT))
4851                         if [[ $percent -gt 15 ]]; then
4852                                 error "less than 16-pages write RPCs" \
4853                                       "$percent% > 15%"
4854                                 break
4855                         fi
4856                 done
4857         rm -rf $TDIR
4858 }
4859 run_test 42e "verify sub-RPC writes are not done synchronously"
4860
4861 test_43A() { # was test_43
4862         test_mkdir $DIR/$tdir
4863         cp -p /bin/ls $DIR/$tdir/$tfile
4864         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4865         pid=$!
4866         # give multiop a chance to open
4867         sleep 1
4868
4869         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4870         kill -USR1 $pid
4871 }
4872 run_test 43A "execution of file opened for write should return -ETXTBSY"
4873
4874 test_43a() {
4875         test_mkdir $DIR/$tdir
4876         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4877         $DIR/$tdir/sleep 60 &
4878         SLEEP_PID=$!
4879         # Make sure exec of $tdir/sleep wins race with truncate
4880         sleep 1
4881         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4882         kill $SLEEP_PID
4883 }
4884 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4885
4886 test_43b() {
4887         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4888
4889         test_mkdir $DIR/$tdir
4890         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4891         $DIR/$tdir/sleep 60 &
4892         SLEEP_PID=$!
4893         # Make sure exec of $tdir/sleep wins race with truncate
4894         sleep 1
4895         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4896         kill $SLEEP_PID
4897 }
4898 run_test 43b "truncate of file being executed should return -ETXTBSY"
4899
4900 test_43c() {
4901         local testdir="$DIR/$tdir"
4902         test_mkdir $testdir
4903         cp $SHELL $testdir/
4904         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4905                 ( cd $testdir && md5sum -c )
4906 }
4907 run_test 43c "md5sum of copy into lustre"
4908
4909 test_44A() { # was test_44
4910         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4911
4912         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4913         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4914 }
4915 run_test 44A "zero length read from a sparse stripe"
4916
4917 test_44a() {
4918         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4919                 awk '{ print $2 }')
4920         [ -z "$nstripe" ] && skip "can't get stripe info"
4921         [[ $nstripe -gt $OSTCOUNT ]] &&
4922                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4923
4924         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4925                 awk '{ print $2 }')
4926         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4927                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4928                         awk '{ print $2 }')
4929         fi
4930
4931         OFFSETS="0 $((stride/2)) $((stride-1))"
4932         for offset in $OFFSETS; do
4933                 for i in $(seq 0 $((nstripe-1))); do
4934                         local GLOBALOFFSETS=""
4935                         # size in Bytes
4936                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4937                         local myfn=$DIR/d44a-$size
4938                         echo "--------writing $myfn at $size"
4939                         ll_sparseness_write $myfn $size ||
4940                                 error "ll_sparseness_write"
4941                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4942                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4943                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4944
4945                         for j in $(seq 0 $((nstripe-1))); do
4946                                 # size in Bytes
4947                                 size=$((((j + $nstripe )*$stride + $offset)))
4948                                 ll_sparseness_write $myfn $size ||
4949                                         error "ll_sparseness_write"
4950                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4951                         done
4952                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4953                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4954                         rm -f $myfn
4955                 done
4956         done
4957 }
4958 run_test 44a "test sparse pwrite ==============================="
4959
4960 dirty_osc_total() {
4961         tot=0
4962         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4963                 tot=$(($tot + $d))
4964         done
4965         echo $tot
4966 }
4967 do_dirty_record() {
4968         before=`dirty_osc_total`
4969         echo executing "\"$*\""
4970         eval $*
4971         after=`dirty_osc_total`
4972         echo before $before, after $after
4973 }
4974 test_45() {
4975         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4976
4977         f="$DIR/f45"
4978         # Obtain grants from OST if it supports it
4979         echo blah > ${f}_grant
4980         stop_writeback
4981         sync
4982         do_dirty_record "echo blah > $f"
4983         [[ $before -eq $after ]] && error "write wasn't cached"
4984         do_dirty_record "> $f"
4985         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4986         do_dirty_record "echo blah > $f"
4987         [[ $before -eq $after ]] && error "write wasn't cached"
4988         do_dirty_record "sync"
4989         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4990         do_dirty_record "echo blah > $f"
4991         [[ $before -eq $after ]] && error "write wasn't cached"
4992         do_dirty_record "cancel_lru_locks osc"
4993         [[ $before -gt $after ]] ||
4994                 error "lock cancellation didn't lower dirty count"
4995         start_writeback
4996 }
4997 run_test 45 "osc io page accounting ============================"
4998
4999 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
5000 # test tickles a bug where re-dirtying a page was failing to be mapped to the
5001 # objects offset and an assert hit when an rpc was built with 1023's mapped
5002 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
5003 test_46() {
5004         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5005
5006         f="$DIR/f46"
5007         stop_writeback
5008         sync
5009         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5010         sync
5011         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5012         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5013         sync
5014         start_writeback
5015 }
5016 run_test 46 "dirtying a previously written page ================"
5017
5018 # test_47 is removed "Device nodes check" is moved to test_28
5019
5020 test_48a() { # bug 2399
5021         [ "$mds1_FSTYPE" = "zfs" ] &&
5022         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5023                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5024
5025         test_mkdir $DIR/$tdir
5026         cd $DIR/$tdir
5027         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5028         test_mkdir $DIR/$tdir
5029         touch foo || error "'touch foo' failed after recreating cwd"
5030         test_mkdir bar
5031         touch .foo || error "'touch .foo' failed after recreating cwd"
5032         test_mkdir .bar
5033         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5034         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5035         cd . || error "'cd .' failed after recreating cwd"
5036         mkdir . && error "'mkdir .' worked after recreating cwd"
5037         rmdir . && error "'rmdir .' worked after recreating cwd"
5038         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5039         cd .. || error "'cd ..' failed after recreating cwd"
5040 }
5041 run_test 48a "Access renamed working dir (should return errors)="
5042
5043 test_48b() { # bug 2399
5044         rm -rf $DIR/$tdir
5045         test_mkdir $DIR/$tdir
5046         cd $DIR/$tdir
5047         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5048         touch foo && error "'touch foo' worked after removing cwd"
5049         mkdir foo && error "'mkdir foo' worked after removing cwd"
5050         touch .foo && error "'touch .foo' worked after removing cwd"
5051         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5052         ls . > /dev/null && error "'ls .' worked after removing cwd"
5053         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5054         mkdir . && error "'mkdir .' worked after removing cwd"
5055         rmdir . && error "'rmdir .' worked after removing cwd"
5056         ln -s . foo && error "'ln -s .' worked after removing cwd"
5057         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5058 }
5059 run_test 48b "Access removed working dir (should return errors)="
5060
5061 test_48c() { # bug 2350
5062         #lctl set_param debug=-1
5063         #set -vx
5064         rm -rf $DIR/$tdir
5065         test_mkdir -p $DIR/$tdir/dir
5066         cd $DIR/$tdir/dir
5067         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5068         $TRACE touch foo && error "touch foo worked after removing cwd"
5069         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5070         touch .foo && error "touch .foo worked after removing cwd"
5071         mkdir .foo && error "mkdir .foo worked after removing cwd"
5072         $TRACE ls . && error "'ls .' worked after removing cwd"
5073         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5074         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5075         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5076         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5077         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5078 }
5079 run_test 48c "Access removed working subdir (should return errors)"
5080
5081 test_48d() { # bug 2350
5082         #lctl set_param debug=-1
5083         #set -vx
5084         rm -rf $DIR/$tdir
5085         test_mkdir -p $DIR/$tdir/dir
5086         cd $DIR/$tdir/dir
5087         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5088         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5089         $TRACE touch foo && error "'touch foo' worked after removing parent"
5090         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5091         touch .foo && error "'touch .foo' worked after removing parent"
5092         mkdir .foo && error "mkdir .foo worked after removing parent"
5093         $TRACE ls . && error "'ls .' worked after removing parent"
5094         $TRACE ls .. && error "'ls ..' worked after removing parent"
5095         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5096         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5097         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5098         true
5099 }
5100 run_test 48d "Access removed parent subdir (should return errors)"
5101
5102 test_48e() { # bug 4134
5103         #lctl set_param debug=-1
5104         #set -vx
5105         rm -rf $DIR/$tdir
5106         test_mkdir -p $DIR/$tdir/dir
5107         cd $DIR/$tdir/dir
5108         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5109         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5110         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5111         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5112         # On a buggy kernel addition of "touch foo" after cd .. will
5113         # produce kernel oops in lookup_hash_it
5114         touch ../foo && error "'cd ..' worked after recreate parent"
5115         cd $DIR
5116         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5117 }
5118 run_test 48e "Access to recreated parent subdir (should return errors)"
5119
5120 test_49() { # LU-1030
5121         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5122         remote_ost_nodsh && skip "remote OST with nodsh"
5123
5124         # get ost1 size - $FSNAME-OST0000
5125         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5126                 awk '{ print $4 }')
5127         # write 800M at maximum
5128         [[ $ost1_size -lt 2 ]] && ost1_size=2
5129         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5130
5131         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5132         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5133         local dd_pid=$!
5134
5135         # change max_pages_per_rpc while writing the file
5136         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5137         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5138         # loop until dd process exits
5139         while ps ax -opid | grep -wq $dd_pid; do
5140                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5141                 sleep $((RANDOM % 5 + 1))
5142         done
5143         # restore original max_pages_per_rpc
5144         $LCTL set_param $osc1_mppc=$orig_mppc
5145         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5146 }
5147 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5148
5149 test_50() {
5150         # bug 1485
5151         test_mkdir $DIR/$tdir
5152         cd $DIR/$tdir
5153         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5154 }
5155 run_test 50 "special situations: /proc symlinks  ==============="
5156
5157 test_51a() {    # was test_51
5158         # bug 1516 - create an empty entry right after ".." then split dir
5159         test_mkdir -c1 $DIR/$tdir
5160         touch $DIR/$tdir/foo
5161         $MCREATE $DIR/$tdir/bar
5162         rm $DIR/$tdir/foo
5163         createmany -m $DIR/$tdir/longfile 201
5164         FNUM=202
5165         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5166                 $MCREATE $DIR/$tdir/longfile$FNUM
5167                 FNUM=$(($FNUM + 1))
5168                 echo -n "+"
5169         done
5170         echo
5171         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5172 }
5173 run_test 51a "special situations: split htree with empty entry =="
5174
5175 cleanup_print_lfs_df () {
5176         trap 0
5177         $LFS df
5178         $LFS df -i
5179 }
5180
5181 test_51b() {
5182         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5183
5184         local dir=$DIR/$tdir
5185         local nrdirs=$((65536 + 100))
5186
5187         # cleanup the directory
5188         rm -fr $dir
5189
5190         test_mkdir -c1 $dir
5191
5192         $LFS df
5193         $LFS df -i
5194         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5195         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5196         [[ $numfree -lt $nrdirs ]] &&
5197                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5198
5199         # need to check free space for the directories as well
5200         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5201         numfree=$(( blkfree / $(fs_inode_ksize) ))
5202         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5203
5204         trap cleanup_print_lfs_df EXIT
5205
5206         # create files
5207         createmany -d $dir/d $nrdirs || {
5208                 unlinkmany $dir/d $nrdirs
5209                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5210         }
5211
5212         # really created :
5213         nrdirs=$(ls -U $dir | wc -l)
5214
5215         # unlink all but 100 subdirectories, then check it still works
5216         local left=100
5217         local delete=$((nrdirs - left))
5218
5219         $LFS df
5220         $LFS df -i
5221
5222         # for ldiskfs the nlink count should be 1, but this is OSD specific
5223         # and so this is listed for informational purposes only
5224         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5225         unlinkmany -d $dir/d $delete ||
5226                 error "unlink of first $delete subdirs failed"
5227
5228         echo "nlink between: $(stat -c %h $dir)"
5229         local found=$(ls -U $dir | wc -l)
5230         [ $found -ne $left ] &&
5231                 error "can't find subdirs: found only $found, expected $left"
5232
5233         unlinkmany -d $dir/d $delete $left ||
5234                 error "unlink of second $left subdirs failed"
5235         # regardless of whether the backing filesystem tracks nlink accurately
5236         # or not, the nlink count shouldn't be more than "." and ".." here
5237         local after=$(stat -c %h $dir)
5238         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5239                 echo "nlink after: $after"
5240
5241         cleanup_print_lfs_df
5242 }
5243 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5244
5245 test_51d() {
5246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5247         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5248
5249         test_mkdir $DIR/$tdir
5250         createmany -o $DIR/$tdir/t- 1000
5251         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5252         for N in $(seq 0 $((OSTCOUNT - 1))); do
5253                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5254                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5255                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5256                         '($1 == '$N') { objs += 1 } \
5257                         END { printf("%0.0f", objs) }')
5258                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5259         done
5260         unlinkmany $DIR/$tdir/t- 1000
5261
5262         NLAST=0
5263         for N in $(seq 1 $((OSTCOUNT - 1))); do
5264                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5265                         error "OST $N has less objects vs OST $NLAST" \
5266                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5267                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5268                         error "OST $N has less objects vs OST $NLAST" \
5269                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5270
5271                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5272                         error "OST $N has less #0 objects vs OST $NLAST" \
5273                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5274                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5275                         error "OST $N has less #0 objects vs OST $NLAST" \
5276                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5277                 NLAST=$N
5278         done
5279         rm -f $TMP/$tfile
5280 }
5281 run_test 51d "check object distribution"
5282
5283 test_51e() {
5284         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5285                 skip_env "ldiskfs only test"
5286         fi
5287
5288         test_mkdir -c1 $DIR/$tdir
5289         test_mkdir -c1 $DIR/$tdir/d0
5290
5291         touch $DIR/$tdir/d0/foo
5292         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5293                 error "file exceed 65000 nlink limit!"
5294         unlinkmany $DIR/$tdir/d0/f- 65001
5295         return 0
5296 }
5297 run_test 51e "check file nlink limit"
5298
5299 test_51f() {
5300         test_mkdir $DIR/$tdir
5301
5302         local max=100000
5303         local ulimit_old=$(ulimit -n)
5304         local spare=20 # number of spare fd's for scripts/libraries, etc.
5305         local mdt=$($LFS getstripe -m $DIR/$tdir)
5306         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5307
5308         echo "MDT$mdt numfree=$numfree, max=$max"
5309         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5310         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5311                 while ! ulimit -n $((numfree + spare)); do
5312                         numfree=$((numfree * 3 / 4))
5313                 done
5314                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5315         else
5316                 echo "left ulimit at $ulimit_old"
5317         fi
5318
5319         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5320                 unlinkmany $DIR/$tdir/f $numfree
5321                 error "create+open $numfree files in $DIR/$tdir failed"
5322         }
5323         ulimit -n $ulimit_old
5324
5325         # if createmany exits at 120s there will be fewer than $numfree files
5326         unlinkmany $DIR/$tdir/f $numfree || true
5327 }
5328 run_test 51f "check many open files limit"
5329
5330 test_52a() {
5331         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5332         test_mkdir $DIR/$tdir
5333         touch $DIR/$tdir/foo
5334         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5335         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5336         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5337         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5338         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5339                                         error "link worked"
5340         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5341         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5342         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5343                                                      error "lsattr"
5344         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5345         cp -r $DIR/$tdir $TMP/
5346         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5347 }
5348 run_test 52a "append-only flag test (should return errors)"
5349
5350 test_52b() {
5351         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5352         test_mkdir $DIR/$tdir
5353         touch $DIR/$tdir/foo
5354         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5355         cat test > $DIR/$tdir/foo && error "cat test worked"
5356         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5357         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5358         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5359                                         error "link worked"
5360         echo foo >> $DIR/$tdir/foo && error "echo worked"
5361         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5362         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5363         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5364         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5365                                                         error "lsattr"
5366         chattr -i $DIR/$tdir/foo || error "chattr failed"
5367
5368         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5369 }
5370 run_test 52b "immutable flag test (should return errors) ======="
5371
5372 test_53() {
5373         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5374         remote_mds_nodsh && skip "remote MDS with nodsh"
5375         remote_ost_nodsh && skip "remote OST with nodsh"
5376
5377         local param
5378         local param_seq
5379         local ostname
5380         local mds_last
5381         local mds_last_seq
5382         local ost_last
5383         local ost_last_seq
5384         local ost_last_id
5385         local ostnum
5386         local node
5387         local found=false
5388         local support_last_seq=true
5389
5390         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5391                 support_last_seq=false
5392
5393         # only test MDT0000
5394         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5395         local value
5396         for value in $(do_facet $SINGLEMDS \
5397                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5398                 param=$(echo ${value[0]} | cut -d "=" -f1)
5399                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5400
5401                 if $support_last_seq; then
5402                         param_seq=$(echo $param |
5403                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5404                         mds_last_seq=$(do_facet $SINGLEMDS \
5405                                        $LCTL get_param -n $param_seq)
5406                 fi
5407                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5408
5409                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5410                 node=$(facet_active_host ost$((ostnum+1)))
5411                 param="obdfilter.$ostname.last_id"
5412                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5413                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5414                         ost_last_id=$ost_last
5415
5416                         if $support_last_seq; then
5417                                 ost_last_id=$(echo $ost_last |
5418                                               awk -F':' '{print $2}' |
5419                                               sed -e "s/^0x//g")
5420                                 ost_last_seq=$(echo $ost_last |
5421                                                awk -F':' '{print $1}')
5422                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5423                         fi
5424
5425                         if [[ $ost_last_id != $mds_last ]]; then
5426                                 error "$ost_last_id != $mds_last"
5427                         else
5428                                 found=true
5429                                 break
5430                         fi
5431                 done
5432         done
5433         $found || error "can not match last_seq/last_id for $mdtosc"
5434         return 0
5435 }
5436 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5437
5438 test_54a() {
5439         perl -MSocket -e ';' || skip "no Socket perl module installed"
5440
5441         $SOCKETSERVER $DIR/socket ||
5442                 error "$SOCKETSERVER $DIR/socket failed: $?"
5443         $SOCKETCLIENT $DIR/socket ||
5444                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5445         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5446 }
5447 run_test 54a "unix domain socket test =========================="
5448
5449 test_54b() {
5450         f="$DIR/f54b"
5451         mknod $f c 1 3
5452         chmod 0666 $f
5453         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5454 }
5455 run_test 54b "char device works in lustre ======================"
5456
5457 find_loop_dev() {
5458         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5459         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5460         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5461
5462         for i in $(seq 3 7); do
5463                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5464                 LOOPDEV=$LOOPBASE$i
5465                 LOOPNUM=$i
5466                 break
5467         done
5468 }
5469
5470 cleanup_54c() {
5471         local rc=0
5472         loopdev="$DIR/loop54c"
5473
5474         trap 0
5475         $UMOUNT $DIR/$tdir || rc=$?
5476         losetup -d $loopdev || true
5477         losetup -d $LOOPDEV || true
5478         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5479         return $rc
5480 }
5481
5482 test_54c() {
5483         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5484
5485         loopdev="$DIR/loop54c"
5486
5487         find_loop_dev
5488         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5489         trap cleanup_54c EXIT
5490         mknod $loopdev b 7 $LOOPNUM
5491         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5492         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5493         losetup $loopdev $DIR/$tfile ||
5494                 error "can't set up $loopdev for $DIR/$tfile"
5495         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5496         test_mkdir $DIR/$tdir
5497         mount -t ext2 $loopdev $DIR/$tdir ||
5498                 error "error mounting $loopdev on $DIR/$tdir"
5499         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5500                 error "dd write"
5501         df $DIR/$tdir
5502         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5503                 error "dd read"
5504         cleanup_54c
5505 }
5506 run_test 54c "block device works in lustre ====================="
5507
5508 test_54d() {
5509         f="$DIR/f54d"
5510         string="aaaaaa"
5511         mknod $f p
5512         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5513 }
5514 run_test 54d "fifo device works in lustre ======================"
5515
5516 test_54e() {
5517         f="$DIR/f54e"
5518         string="aaaaaa"
5519         cp -aL /dev/console $f
5520         echo $string > $f || error "echo $string to $f failed"
5521 }
5522 run_test 54e "console/tty device works in lustre ======================"
5523
5524 test_56a() {
5525         local numfiles=3
5526         local dir=$DIR/$tdir
5527
5528         rm -rf $dir
5529         test_mkdir -p $dir/dir
5530         for i in $(seq $numfiles); do
5531                 touch $dir/file$i
5532                 touch $dir/dir/file$i
5533         done
5534
5535         local numcomp=$($LFS getstripe --component-count $dir)
5536
5537         [[ $numcomp == 0 ]] && numcomp=1
5538
5539         # test lfs getstripe with --recursive
5540         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5541
5542         [[ $filenum -eq $((numfiles * 2)) ]] ||
5543                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5544         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5545         [[ $filenum -eq $numfiles ]] ||
5546                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5547         echo "$LFS getstripe showed obdidx or l_ost_idx"
5548
5549         # test lfs getstripe with file instead of dir
5550         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5551         [[ $filenum -eq 1 ]] ||
5552                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5553         echo "$LFS getstripe file1 passed"
5554
5555         #test lfs getstripe with --verbose
5556         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5557         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5558                 error "$LFS getstripe --verbose $dir: "\
5559                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5560         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5561                 error "$LFS getstripe $dir: showed lmm_magic"
5562
5563         #test lfs getstripe with -v prints lmm_fid
5564         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5565         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5566                 error "$LFS getstripe -v $dir: "\
5567                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5568         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5569                 error "$LFS getstripe $dir: showed lmm_fid by default"
5570         echo "$LFS getstripe --verbose passed"
5571
5572         #check for FID information
5573         local fid1=$($LFS getstripe --fid $dir/file1)
5574         local fid2=$($LFS getstripe --verbose $dir/file1 |
5575                      awk '/lmm_fid: / { print $2; exit; }')
5576         local fid3=$($LFS path2fid $dir/file1)
5577
5578         [ "$fid1" != "$fid2" ] &&
5579                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5580         [ "$fid1" != "$fid3" ] &&
5581                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5582         echo "$LFS getstripe --fid passed"
5583
5584         #test lfs getstripe with --obd
5585         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5586                 error "$LFS getstripe --obd wrong_uuid: should return error"
5587
5588         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5589
5590         local ostidx=1
5591         local obduuid=$(ostuuid_from_index $ostidx)
5592         local found=$($LFS getstripe -r --obd $obduuid $dir |
5593                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5594
5595         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5596         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5597                 ((filenum--))
5598         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5599                 ((filenum--))
5600
5601         [[ $found -eq $filenum ]] ||
5602                 error "$LFS getstripe --obd: found $found expect $filenum"
5603         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5604                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5605                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5606                 error "$LFS getstripe --obd: should not show file on other obd"
5607         echo "$LFS getstripe --obd passed"
5608 }
5609 run_test 56a "check $LFS getstripe"
5610
5611 test_56b() {
5612         local dir=$DIR/$tdir
5613         local numdirs=3
5614
5615         test_mkdir $dir
5616         for i in $(seq $numdirs); do
5617                 test_mkdir $dir/dir$i
5618         done
5619
5620         # test lfs getdirstripe default mode is non-recursion, which is
5621         # different from lfs getstripe
5622         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5623
5624         [[ $dircnt -eq 1 ]] ||
5625                 error "$LFS getdirstripe: found $dircnt, not 1"
5626         dircnt=$($LFS getdirstripe --recursive $dir |
5627                 grep -c lmv_stripe_count)
5628         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5629                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5630 }
5631 run_test 56b "check $LFS getdirstripe"
5632
5633 test_56c() {
5634         remote_ost_nodsh && skip "remote OST with nodsh"
5635
5636         local ost_idx=0
5637         local ost_name=$(ostname_from_index $ost_idx)
5638         local old_status=$(ost_dev_status $ost_idx)
5639         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
5640
5641         [[ -z "$old_status" ]] ||
5642                 skip_env "OST $ost_name is in $old_status status"
5643
5644         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5645         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
5646                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5647         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
5648                 save_lustre_params ost1 osd-*.$ost_name.nonrotational > $p
5649                 do_facet ost1 $LCTL set_param -n osd-*.$ost_name.nonrotational=1
5650         fi
5651
5652         [[ $($LFS df -v $MOUNT |& grep -c "inactive device") -eq 0 ]] ||
5653                 error "$LFS df -v showing inactive devices"
5654         sleep_maxage
5655
5656         local new_status=$(ost_dev_status $ost_idx $MOUNT -v)
5657
5658         [[ "$new_status" =~ "D" ]] ||
5659                 error "$ost_name status is '$new_status', missing 'D'"
5660         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5661                 [[ "$new_status" =~ "N" ]] ||
5662                         error "$ost_name status is '$new_status', missing 'N'"
5663         fi
5664         if [[ $OST1_VERSION -ge $(version_code 2.12.57) ]]; then
5665                 [[ "$new_status" =~ "f" ]] ||
5666                         error "$ost_name status is '$new_status', missing 'f'"
5667         fi
5668
5669         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5670         [[ $OST1_VERSION -lt $(version_code 2.12.55) ]] || do_facet ost1 \
5671                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5672         [[ -z "$p" ]] && restore_lustre_params < $p || true
5673         sleep_maxage
5674
5675         new_status=$(ost_dev_status $ost_idx)
5676         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5677                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5678         # can't check 'f' as devices may actually be on flash
5679 }
5680 run_test 56c "check 'lfs df' showing device status"
5681
5682 test_56d() {
5683         local mdts=$($LFS df -v $MOUNT | grep -c MDT)
5684         local osts=$($LFS df -v $MOUNT | grep -c OST)
5685
5686         $LFS df $MOUNT
5687
5688         (( mdts == MDSCOUNT )) ||
5689                 error "lfs df -v showed $mdts MDTs, not $MDSCOUNT"
5690         (( osts == OSTCOUNT )) ||
5691                 error "lfs df -v showed $osts OSTs, not $OSTCOUNT"
5692 }
5693 run_test 56d "'lfs df -v' prints only configured devices"
5694
5695 NUMFILES=3
5696 NUMDIRS=3
5697 setup_56() {
5698         local local_tdir="$1"
5699         local local_numfiles="$2"
5700         local local_numdirs="$3"
5701         local dir_params="$4"
5702         local dir_stripe_params="$5"
5703
5704         if [ ! -d "$local_tdir" ] ; then
5705                 test_mkdir -p $dir_stripe_params $local_tdir
5706                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5707                 for i in $(seq $local_numfiles) ; do
5708                         touch $local_tdir/file$i
5709                 done
5710                 for i in $(seq $local_numdirs) ; do
5711                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5712                         for j in $(seq $local_numfiles) ; do
5713                                 touch $local_tdir/dir$i/file$j
5714                         done
5715                 done
5716         fi
5717 }
5718
5719 setup_56_special() {
5720         local local_tdir=$1
5721         local local_numfiles=$2
5722         local local_numdirs=$3
5723
5724         setup_56 $local_tdir $local_numfiles $local_numdirs
5725
5726         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5727                 for i in $(seq $local_numfiles) ; do
5728                         mknod $local_tdir/loop${i}b b 7 $i
5729                         mknod $local_tdir/null${i}c c 1 3
5730                         ln -s $local_tdir/file1 $local_tdir/link${i}
5731                 done
5732                 for i in $(seq $local_numdirs) ; do
5733                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5734                         mknod $local_tdir/dir$i/null${i}c c 1 3
5735                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5736                 done
5737         fi
5738 }
5739
5740 test_56g() {
5741         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5742         local expected=$(($NUMDIRS + 2))
5743
5744         setup_56 $dir $NUMFILES $NUMDIRS
5745
5746         # test lfs find with -name
5747         for i in $(seq $NUMFILES) ; do
5748                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5749
5750                 [ $nums -eq $expected ] ||
5751                         error "lfs find -name '*$i' $dir wrong: "\
5752                               "found $nums, expected $expected"
5753         done
5754 }
5755 run_test 56g "check lfs find -name"
5756
5757 test_56h() {
5758         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5759         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5760
5761         setup_56 $dir $NUMFILES $NUMDIRS
5762
5763         # test lfs find with ! -name
5764         for i in $(seq $NUMFILES) ; do
5765                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5766
5767                 [ $nums -eq $expected ] ||
5768                         error "lfs find ! -name '*$i' $dir wrong: "\
5769                               "found $nums, expected $expected"
5770         done
5771 }
5772 run_test 56h "check lfs find ! -name"
5773
5774 test_56i() {
5775         local dir=$DIR/$tdir
5776
5777         test_mkdir $dir
5778
5779         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5780         local out=$($cmd)
5781
5782         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5783 }
5784 run_test 56i "check 'lfs find -ost UUID' skips directories"
5785
5786 test_56j() {
5787         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5788
5789         setup_56_special $dir $NUMFILES $NUMDIRS
5790
5791         local expected=$((NUMDIRS + 1))
5792         local cmd="$LFS find -type d $dir"
5793         local nums=$($cmd | wc -l)
5794
5795         [ $nums -eq $expected ] ||
5796                 error "'$cmd' wrong: found $nums, expected $expected"
5797 }
5798 run_test 56j "check lfs find -type d"
5799
5800 test_56k() {
5801         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5802
5803         setup_56_special $dir $NUMFILES $NUMDIRS
5804
5805         local expected=$(((NUMDIRS + 1) * NUMFILES))
5806         local cmd="$LFS find -type f $dir"
5807         local nums=$($cmd | wc -l)
5808
5809         [ $nums -eq $expected ] ||
5810                 error "'$cmd' wrong: found $nums, expected $expected"
5811 }
5812 run_test 56k "check lfs find -type f"
5813
5814 test_56l() {
5815         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5816
5817         setup_56_special $dir $NUMFILES $NUMDIRS
5818
5819         local expected=$((NUMDIRS + NUMFILES))
5820         local cmd="$LFS find -type b $dir"
5821         local nums=$($cmd | wc -l)
5822
5823         [ $nums -eq $expected ] ||
5824                 error "'$cmd' wrong: found $nums, expected $expected"
5825 }
5826 run_test 56l "check lfs find -type b"
5827
5828 test_56m() {
5829         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5830
5831         setup_56_special $dir $NUMFILES $NUMDIRS
5832
5833         local expected=$((NUMDIRS + NUMFILES))
5834         local cmd="$LFS find -type c $dir"
5835         local nums=$($cmd | wc -l)
5836         [ $nums -eq $expected ] ||
5837                 error "'$cmd' wrong: found $nums, expected $expected"
5838 }
5839 run_test 56m "check lfs find -type c"
5840
5841 test_56n() {
5842         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5843         setup_56_special $dir $NUMFILES $NUMDIRS
5844
5845         local expected=$((NUMDIRS + NUMFILES))
5846         local cmd="$LFS find -type l $dir"
5847         local nums=$($cmd | wc -l)
5848
5849         [ $nums -eq $expected ] ||
5850                 error "'$cmd' wrong: found $nums, expected $expected"
5851 }
5852 run_test 56n "check lfs find -type l"
5853
5854 test_56o() {
5855         local dir=$DIR/$tdir
5856
5857         setup_56 $dir $NUMFILES $NUMDIRS
5858         utime $dir/file1 > /dev/null || error "utime (1)"
5859         utime $dir/file2 > /dev/null || error "utime (2)"
5860         utime $dir/dir1 > /dev/null || error "utime (3)"
5861         utime $dir/dir2 > /dev/null || error "utime (4)"
5862         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5863         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5864
5865         local expected=4
5866         local nums=$($LFS find -mtime +0 $dir | wc -l)
5867
5868         [ $nums -eq $expected ] ||
5869                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5870
5871         expected=12
5872         cmd="$LFS find -mtime 0 $dir"
5873         nums=$($cmd | wc -l)
5874         [ $nums -eq $expected ] ||
5875                 error "'$cmd' wrong: found $nums, expected $expected"
5876 }
5877 run_test 56o "check lfs find -mtime for old files"
5878
5879 test_56ob() {
5880         local dir=$DIR/$tdir
5881         local expected=1
5882         local count=0
5883
5884         # just to make sure there is something that won't be found
5885         test_mkdir $dir
5886         touch $dir/$tfile.now
5887
5888         for age in year week day hour min; do
5889                 count=$((count + 1))
5890
5891                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5892                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5893                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5894
5895                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5896                 local nums=$($cmd | wc -l)
5897                 [ $nums -eq $expected ] ||
5898                         error "'$cmd' wrong: found $nums, expected $expected"
5899
5900                 cmd="$LFS find $dir -atime $count${age:0:1}"
5901                 nums=$($cmd | wc -l)
5902                 [ $nums -eq $expected ] ||
5903                         error "'$cmd' wrong: found $nums, expected $expected"
5904         done
5905
5906         sleep 2
5907         cmd="$LFS find $dir -ctime +1s -type f"
5908         nums=$($cmd | wc -l)
5909         (( $nums == $count * 2 + 1)) ||
5910                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5911 }
5912 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5913
5914 test_newerXY_base() {
5915         local x=$1
5916         local y=$2
5917         local dir=$DIR/$tdir
5918         local ref
5919         local negref
5920
5921         if [ $y == "t" ]; then
5922                 if [ $x == "b" ]; then
5923                         ref="\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\""
5924                 else
5925                         ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5926                 fi
5927         else
5928                 ref=$DIR/$tfile.newer.$x$y
5929                 touch $ref || error "touch $ref failed"
5930         fi
5931         sleep 2
5932         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5933         sleep 2
5934         if [ $y == "t" ]; then
5935                 if [ $x == "b" ]; then
5936                         negref="\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\""
5937                 else
5938                         negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5939                 fi
5940         else
5941                 negref=$DIR/$tfile.negnewer.$x$y
5942                 touch $negref || error "touch $negref failed"
5943         fi
5944
5945         local cmd="$LFS find $dir -newer$x$y $ref"
5946         local nums=$(eval $cmd | wc -l)
5947         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5948
5949         [ $nums -eq $expected ] ||
5950                 error "'$cmd' wrong: found $nums, expected $expected"
5951
5952         cmd="$LFS find $dir ! -newer$x$y $negref"
5953         nums=$(eval $cmd | wc -l)
5954         [ $nums -eq $expected ] ||
5955                 error "'$cmd' wrong: found $nums, expected $expected"
5956
5957         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5958         nums=$(eval $cmd | wc -l)
5959         [ $nums -eq $expected ] ||
5960                 error "'$cmd' wrong: found $nums, expected $expected"
5961
5962         rm -rf $DIR/*
5963 }
5964
5965 test_56oc() {
5966         test_newerXY_base "b" "t"
5967         test_newerXY_base "a" "a"
5968         test_newerXY_base "a" "m"
5969         test_newerXY_base "a" "c"
5970         test_newerXY_base "m" "a"
5971         test_newerXY_base "m" "m"
5972         test_newerXY_base "m" "c"
5973         test_newerXY_base "c" "a"
5974         test_newerXY_base "c" "m"
5975         test_newerXY_base "c" "c"
5976         test_newerXY_base "b" "b"
5977         test_newerXY_base "a" "t"
5978         test_newerXY_base "m" "t"
5979         test_newerXY_base "c" "t"
5980         test_newerXY_base "b" "t"
5981 }
5982 run_test 56oc "check lfs find -newerXY work"
5983
5984 btime_supported() {
5985         local dir=$DIR/$tdir
5986         local rc
5987
5988         mkdir -p $dir
5989         touch $dir/$tfile
5990         $LFS find $dir -btime -1d -type f
5991         rc=$?
5992         rm -rf $dir
5993         return $rc
5994 }
5995
5996 test_56od() {
5997         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
5998                 ! btime_supported && skip "btime unsupported on MDS"
5999
6000         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
6001                 ! btime_supported && skip "btime unsupported on clients"
6002
6003         local dir=$DIR/$tdir
6004         local ref=$DIR/$tfile.ref
6005         local negref=$DIR/$tfile.negref
6006
6007         mkdir $dir || error "mkdir $dir failed"
6008         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
6009         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
6010         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
6011         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
6012         touch $ref || error "touch $ref failed"
6013         # sleep 3 seconds at least
6014         sleep 3
6015
6016         local before=$(do_facet mds1 date +%s)
6017         local skew=$(($(date +%s) - before + 1))
6018
6019         if (( skew < 0 && skew > -5 )); then
6020                 sleep $((0 - skew + 1))
6021                 skew=0
6022         fi
6023
6024         # Set the dir stripe params to limit files all on MDT0,
6025         # otherwise we need to calc the max clock skew between
6026         # the client and MDTs.
6027         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
6028         sleep 2
6029         touch $negref || error "touch $negref failed"
6030
6031         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
6032         local nums=$($cmd | wc -l)
6033         local expected=$(((NUMFILES + 1) * NUMDIRS))
6034
6035         [ $nums -eq $expected ] ||
6036                 error "'$cmd' wrong: found $nums, expected $expected"
6037
6038         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6039         nums=$($cmd | wc -l)
6040         expected=$((NUMFILES + 1))
6041         [ $nums -eq $expected ] ||
6042                 error "'$cmd' wrong: found $nums, expected $expected"
6043
6044         [ $skew -lt 0 ] && return
6045
6046         local after=$(do_facet mds1 date +%s)
6047         local age=$((after - before + 1 + skew))
6048
6049         cmd="$LFS find $dir -btime -${age}s -type f"
6050         nums=$($cmd | wc -l)
6051         expected=$(((NUMFILES + 1) * NUMDIRS))
6052
6053         echo "Clock skew between client and server: $skew, age:$age"
6054         [ $nums -eq $expected ] ||
6055                 error "'$cmd' wrong: found $nums, expected $expected"
6056
6057         expected=$(($NUMDIRS + 1))
6058         cmd="$LFS find $dir -btime -${age}s -type d"
6059         nums=$($cmd | wc -l)
6060         [ $nums -eq $expected ] ||
6061                 error "'$cmd' wrong: found $nums, expected $expected"
6062         rm -f $ref $negref || error "Failed to remove $ref $negref"
6063 }
6064 run_test 56od "check lfs find -btime with units"
6065
6066 test_56p() {
6067         [ $RUNAS_ID -eq $UID ] &&
6068                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6069
6070         local dir=$DIR/$tdir
6071
6072         setup_56 $dir $NUMFILES $NUMDIRS
6073         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6074
6075         local expected=$NUMFILES
6076         local cmd="$LFS find -uid $RUNAS_ID $dir"
6077         local nums=$($cmd | wc -l)
6078
6079         [ $nums -eq $expected ] ||
6080                 error "'$cmd' wrong: found $nums, expected $expected"
6081
6082         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6083         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6084         nums=$($cmd | wc -l)
6085         [ $nums -eq $expected ] ||
6086                 error "'$cmd' wrong: found $nums, expected $expected"
6087 }
6088 run_test 56p "check lfs find -uid and ! -uid"
6089
6090 test_56q() {
6091         [ $RUNAS_ID -eq $UID ] &&
6092                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6093
6094         local dir=$DIR/$tdir
6095
6096         setup_56 $dir $NUMFILES $NUMDIRS
6097         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6098
6099         local expected=$NUMFILES
6100         local cmd="$LFS find -gid $RUNAS_GID $dir"
6101         local nums=$($cmd | wc -l)
6102
6103         [ $nums -eq $expected ] ||
6104                 error "'$cmd' wrong: found $nums, expected $expected"
6105
6106         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6107         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6108         nums=$($cmd | wc -l)
6109         [ $nums -eq $expected ] ||
6110                 error "'$cmd' wrong: found $nums, expected $expected"
6111 }
6112 run_test 56q "check lfs find -gid and ! -gid"
6113
6114 test_56r() {
6115         local dir=$DIR/$tdir
6116
6117         setup_56 $dir $NUMFILES $NUMDIRS
6118
6119         local expected=12
6120         local cmd="$LFS find -size 0 -type f -lazy $dir"
6121         local nums=$($cmd | wc -l)
6122
6123         [ $nums -eq $expected ] ||
6124                 error "'$cmd' wrong: found $nums, expected $expected"
6125         cmd="$LFS find -size 0 -type f $dir"
6126         nums=$($cmd | wc -l)
6127         [ $nums -eq $expected ] ||
6128                 error "'$cmd' wrong: found $nums, expected $expected"
6129
6130         expected=0
6131         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6132         nums=$($cmd | wc -l)
6133         [ $nums -eq $expected ] ||
6134                 error "'$cmd' wrong: found $nums, expected $expected"
6135         cmd="$LFS find ! -size 0 -type f $dir"
6136         nums=$($cmd | wc -l)
6137         [ $nums -eq $expected ] ||
6138                 error "'$cmd' wrong: found $nums, expected $expected"
6139
6140         echo "test" > $dir/$tfile
6141         echo "test2" > $dir/$tfile.2 && sync
6142         expected=1
6143         cmd="$LFS find -size 5 -type f -lazy $dir"
6144         nums=$($cmd | wc -l)
6145         [ $nums -eq $expected ] ||
6146                 error "'$cmd' wrong: found $nums, expected $expected"
6147         cmd="$LFS find -size 5 -type f $dir"
6148         nums=$($cmd | wc -l)
6149         [ $nums -eq $expected ] ||
6150                 error "'$cmd' wrong: found $nums, expected $expected"
6151
6152         expected=1
6153         cmd="$LFS find -size +5 -type f -lazy $dir"
6154         nums=$($cmd | wc -l)
6155         [ $nums -eq $expected ] ||
6156                 error "'$cmd' wrong: found $nums, expected $expected"
6157         cmd="$LFS find -size +5 -type f $dir"
6158         nums=$($cmd | wc -l)
6159         [ $nums -eq $expected ] ||
6160                 error "'$cmd' wrong: found $nums, expected $expected"
6161
6162         expected=2
6163         cmd="$LFS find -size +0 -type f -lazy $dir"
6164         nums=$($cmd | wc -l)
6165         [ $nums -eq $expected ] ||
6166                 error "'$cmd' wrong: found $nums, expected $expected"
6167         cmd="$LFS find -size +0 -type f $dir"
6168         nums=$($cmd | wc -l)
6169         [ $nums -eq $expected ] ||
6170                 error "'$cmd' wrong: found $nums, expected $expected"
6171
6172         expected=2
6173         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6174         nums=$($cmd | wc -l)
6175         [ $nums -eq $expected ] ||
6176                 error "'$cmd' wrong: found $nums, expected $expected"
6177         cmd="$LFS find ! -size -5 -type f $dir"
6178         nums=$($cmd | wc -l)
6179         [ $nums -eq $expected ] ||
6180                 error "'$cmd' wrong: found $nums, expected $expected"
6181
6182         expected=12
6183         cmd="$LFS find -size -5 -type f -lazy $dir"
6184         nums=$($cmd | wc -l)
6185         [ $nums -eq $expected ] ||
6186                 error "'$cmd' wrong: found $nums, expected $expected"
6187         cmd="$LFS find -size -5 -type f $dir"
6188         nums=$($cmd | wc -l)
6189         [ $nums -eq $expected ] ||
6190                 error "'$cmd' wrong: found $nums, expected $expected"
6191 }
6192 run_test 56r "check lfs find -size works"
6193
6194 test_56ra_sub() {
6195         local expected=$1
6196         local glimpses=$2
6197         local cmd="$3"
6198
6199         cancel_lru_locks $OSC
6200
6201         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6202         local nums=$($cmd | wc -l)
6203
6204         [ $nums -eq $expected ] ||
6205                 error "'$cmd' wrong: found $nums, expected $expected"
6206
6207         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6208
6209         if (( rpcs_before + glimpses != rpcs_after )); then
6210                 echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6211                 $LCTL get_param osc.*.stats | grep ldlm_glimpse_enqueue
6212
6213                 if [[ $glimpses == 0 ]]; then
6214                         error "'$cmd' should not send glimpse RPCs to OST"
6215                 else
6216                         error "'$cmd' should send $glimpses glimpse RPCs to OST"
6217                 fi
6218         fi
6219 }
6220
6221 test_56ra() {
6222         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6223                 skip "MDS < 2.12.58 doesn't return LSOM data"
6224         local dir=$DIR/$tdir
6225
6226         [[ $OSC == "mdc" ]] && skip "DoM files" && return
6227
6228         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6229         # open and close all files to ensure LSOM is updated
6230         cancel_lru_locks $OSC
6231         find $dir -type f | xargs cat > /dev/null
6232
6233         #   expect_found  glimpse_rpcs  command_to_run
6234         test_56ra_sub 12  0 "$LFS find -size 0 -type f -lazy $dir"
6235         test_56ra_sub 12 12 "$LFS find -size 0 -type f $dir"
6236         test_56ra_sub  0  0 "$LFS find ! -size 0 -type f -lazy $dir"
6237         test_56ra_sub  0 12 "$LFS find ! -size 0 -type f $dir"
6238
6239         echo "test" > $dir/$tfile
6240         echo "test2" > $dir/$tfile.2 && sync
6241         cancel_lru_locks $OSC
6242         cat $dir/$tfile $dir/$tfile.2 > /dev/null
6243
6244         test_56ra_sub  1  0 "$LFS find -size 5 -type f -lazy $dir"
6245         test_56ra_sub  1 14 "$LFS find -size 5 -type f $dir"
6246         test_56ra_sub  1  0 "$LFS find -size +5 -type f -lazy $dir"
6247         test_56ra_sub  1 14 "$LFS find -size +5 -type f $dir"
6248
6249         test_56ra_sub  2  0 "$LFS find -size +0 -type f -lazy $dir"
6250         test_56ra_sub  2 14 "$LFS find -size +0 -type f $dir"
6251         test_56ra_sub  2  0 "$LFS find ! -size -5 -type f -lazy $dir"
6252         test_56ra_sub  2 14 "$LFS find ! -size -5 -type f $dir"
6253         test_56ra_sub 12  0 "$LFS find -size -5 -type f -lazy $dir"
6254         test_56ra_sub 12 14 "$LFS find -size -5 -type f $dir"
6255 }
6256 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6257
6258 test_56rb() {
6259         local dir=$DIR/$tdir
6260         local tmp=$TMP/$tfile.log
6261         local mdt_idx;
6262
6263         test_mkdir -p $dir || error "failed to mkdir $dir"
6264         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6265                 error "failed to setstripe $dir/$tfile"
6266         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6267
6268         stack_trap "rm -f $tmp" EXIT
6269         $LFS find --size +100K --ost 0 $dir 2>&1 | tee $tmp
6270         [ -z "$(cat $tmp | grep "obd_uuid: ")" ] ||
6271                 error "failed to find --size +100K --ost 0 $dir"
6272         $LFS find --size +100K --mdt $mdt_idx $dir 2>&1 | tee $tmp
6273         [ -z "$(cat $tmp | grep "obd_uuid: ")" ] ||
6274                 error "failed to find --size +100K --mdt $mdt_idx $dir"
6275 }
6276 run_test 56rb "check lfs find --size --ost/--mdt works"
6277
6278 test_56s() { # LU-611 #LU-9369
6279         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6280
6281         local dir=$DIR/$tdir
6282         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6283
6284         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6285         for i in $(seq $NUMDIRS); do
6286                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6287         done
6288
6289         local expected=$NUMDIRS
6290         local cmd="$LFS find -c $OSTCOUNT $dir"
6291         local nums=$($cmd | wc -l)
6292
6293         [ $nums -eq $expected ] || {
6294                 $LFS getstripe -R $dir
6295                 error "'$cmd' wrong: found $nums, expected $expected"
6296         }
6297
6298         expected=$((NUMDIRS + onestripe))
6299         cmd="$LFS find -stripe-count +0 -type f $dir"
6300         nums=$($cmd | wc -l)
6301         [ $nums -eq $expected ] || {
6302                 $LFS getstripe -R $dir
6303                 error "'$cmd' wrong: found $nums, expected $expected"
6304         }
6305
6306         expected=$onestripe
6307         cmd="$LFS find -stripe-count 1 -type f $dir"
6308         nums=$($cmd | wc -l)
6309         [ $nums -eq $expected ] || {
6310                 $LFS getstripe -R $dir
6311                 error "'$cmd' wrong: found $nums, expected $expected"
6312         }
6313
6314         cmd="$LFS find -stripe-count -2 -type f $dir"
6315         nums=$($cmd | wc -l)
6316         [ $nums -eq $expected ] || {
6317                 $LFS getstripe -R $dir
6318                 error "'$cmd' wrong: found $nums, expected $expected"
6319         }
6320
6321         expected=0
6322         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6323         nums=$($cmd | wc -l)
6324         [ $nums -eq $expected ] || {
6325                 $LFS getstripe -R $dir
6326                 error "'$cmd' wrong: found $nums, expected $expected"
6327         }
6328 }
6329 run_test 56s "check lfs find -stripe-count works"
6330
6331 test_56t() { # LU-611 #LU-9369
6332         local dir=$DIR/$tdir
6333
6334         setup_56 $dir 0 $NUMDIRS
6335         for i in $(seq $NUMDIRS); do
6336                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6337         done
6338
6339         local expected=$NUMDIRS
6340         local cmd="$LFS find -S 8M $dir"
6341         local nums=$($cmd | wc -l)
6342
6343         [ $nums -eq $expected ] || {
6344                 $LFS getstripe -R $dir
6345                 error "'$cmd' wrong: found $nums, expected $expected"
6346         }
6347         rm -rf $dir
6348
6349         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6350
6351         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6352
6353         expected=$(((NUMDIRS + 1) * NUMFILES))
6354         cmd="$LFS find -stripe-size 512k -type f $dir"
6355         nums=$($cmd | wc -l)
6356         [ $nums -eq $expected ] ||
6357                 error "'$cmd' wrong: found $nums, expected $expected"
6358
6359         cmd="$LFS find -stripe-size +320k -type f $dir"
6360         nums=$($cmd | wc -l)
6361         [ $nums -eq $expected ] ||
6362                 error "'$cmd' wrong: found $nums, expected $expected"
6363
6364         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6365         cmd="$LFS find -stripe-size +200k -type f $dir"
6366         nums=$($cmd | wc -l)
6367         [ $nums -eq $expected ] ||
6368                 error "'$cmd' wrong: found $nums, expected $expected"
6369
6370         cmd="$LFS find -stripe-size -640k -type f $dir"
6371         nums=$($cmd | wc -l)
6372         [ $nums -eq $expected ] ||
6373                 error "'$cmd' wrong: found $nums, expected $expected"
6374
6375         expected=4
6376         cmd="$LFS find -stripe-size 256k -type f $dir"
6377         nums=$($cmd | wc -l)
6378         [ $nums -eq $expected ] ||
6379                 error "'$cmd' wrong: found $nums, expected $expected"
6380
6381         cmd="$LFS find -stripe-size -320k -type f $dir"
6382         nums=$($cmd | wc -l)
6383         [ $nums -eq $expected ] ||
6384                 error "'$cmd' wrong: found $nums, expected $expected"
6385
6386         expected=0
6387         cmd="$LFS find -stripe-size 1024k -type f $dir"
6388         nums=$($cmd | wc -l)
6389         [ $nums -eq $expected ] ||
6390                 error "'$cmd' wrong: found $nums, expected $expected"
6391 }
6392 run_test 56t "check lfs find -stripe-size works"
6393
6394 test_56u() { # LU-611
6395         local dir=$DIR/$tdir
6396
6397         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6398
6399         if [[ $OSTCOUNT -gt 1 ]]; then
6400                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6401                 onestripe=4
6402         else
6403                 onestripe=0
6404         fi
6405
6406         local expected=$(((NUMDIRS + 1) * NUMFILES))
6407         local cmd="$LFS find -stripe-index 0 -type f $dir"
6408         local nums=$($cmd | wc -l)
6409
6410         [ $nums -eq $expected ] ||
6411                 error "'$cmd' wrong: found $nums, expected $expected"
6412
6413         expected=$onestripe
6414         cmd="$LFS find -stripe-index 1 -type f $dir"
6415         nums=$($cmd | wc -l)
6416         [ $nums -eq $expected ] ||
6417                 error "'$cmd' wrong: found $nums, expected $expected"
6418
6419         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6420         nums=$($cmd | wc -l)
6421         [ $nums -eq $expected ] ||
6422                 error "'$cmd' wrong: found $nums, expected $expected"
6423
6424         expected=0
6425         # This should produce an error and not return any files
6426         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6427         nums=$($cmd 2>/dev/null | wc -l)
6428         [ $nums -eq $expected ] ||
6429                 error "'$cmd' wrong: found $nums, expected $expected"
6430
6431         if [[ $OSTCOUNT -gt 1 ]]; then
6432                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6433                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6434                 nums=$($cmd | wc -l)
6435                 [ $nums -eq $expected ] ||
6436                         error "'$cmd' wrong: found $nums, expected $expected"
6437         fi
6438 }
6439 run_test 56u "check lfs find -stripe-index works"
6440
6441 test_56v() {
6442         local mdt_idx=0
6443         local dir=$DIR/$tdir
6444
6445         setup_56 $dir $NUMFILES $NUMDIRS
6446
6447         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6448         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6449
6450         for file in $($LFS find -m $UUID $dir); do
6451                 file_midx=$($LFS getstripe -m $file)
6452                 [ $file_midx -eq $mdt_idx ] ||
6453                         error "lfs find -m $UUID != getstripe -m $file_midx"
6454         done
6455 }
6456 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6457
6458 test_56w() {
6459         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6460         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6461
6462         local dir=$DIR/$tdir
6463
6464         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6465
6466         local stripe_size=$($LFS getstripe -S -d $dir) ||
6467                 error "$LFS getstripe -S -d $dir failed"
6468         stripe_size=${stripe_size%% *}
6469
6470         local file_size=$((stripe_size * OSTCOUNT))
6471         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6472         local required_space=$((file_num * file_size))
6473         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6474                            head -n1)
6475         [[ $free_space -le $((required_space / 1024)) ]] &&
6476                 skip_env "need $required_space, have $free_space kbytes"
6477
6478         local dd_bs=65536
6479         local dd_count=$((file_size / dd_bs))
6480
6481         # write data into the files
6482         local i
6483         local j
6484         local file
6485
6486         for i in $(seq $NUMFILES); do
6487                 file=$dir/file$i
6488                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6489                         error "write data into $file failed"
6490         done
6491         for i in $(seq $NUMDIRS); do
6492                 for j in $(seq $NUMFILES); do
6493                         file=$dir/dir$i/file$j
6494                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6495                                 error "write data into $file failed"
6496                 done
6497         done
6498
6499         # $LFS_MIGRATE will fail if hard link migration is unsupported
6500         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6501                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6502                         error "creating links to $dir/dir1/file1 failed"
6503         fi
6504
6505         local expected=-1
6506
6507         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6508
6509         # lfs_migrate file
6510         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6511
6512         echo "$cmd"
6513         eval $cmd || error "$cmd failed"
6514
6515         check_stripe_count $dir/file1 $expected
6516
6517         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6518         then
6519                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6520                 # OST 1 if it is on OST 0. This file is small enough to
6521                 # be on only one stripe.
6522                 file=$dir/migr_1_ost
6523                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6524                         error "write data into $file failed"
6525                 local obdidx=$($LFS getstripe -i $file)
6526                 local oldmd5=$(md5sum $file)
6527                 local newobdidx=0
6528
6529                 [[ $obdidx -eq 0 ]] && newobdidx=1
6530                 cmd="$LFS migrate -i $newobdidx $file"
6531                 echo $cmd
6532                 eval $cmd || error "$cmd failed"
6533
6534                 local realobdix=$($LFS getstripe -i $file)
6535                 local newmd5=$(md5sum $file)
6536
6537                 [[ $newobdidx -ne $realobdix ]] &&
6538                         error "new OST is different (was=$obdidx, "\
6539                               "wanted=$newobdidx, got=$realobdix)"
6540                 [[ "$oldmd5" != "$newmd5" ]] &&
6541                         error "md5sum differ: $oldmd5, $newmd5"
6542         fi
6543
6544         # lfs_migrate dir
6545         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6546         echo "$cmd"
6547         eval $cmd || error "$cmd failed"
6548
6549         for j in $(seq $NUMFILES); do
6550                 check_stripe_count $dir/dir1/file$j $expected
6551         done
6552
6553         # lfs_migrate works with lfs find
6554         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6555              $LFS_MIGRATE -y -c $expected"
6556         echo "$cmd"
6557         eval $cmd || error "$cmd failed"
6558
6559         for i in $(seq 2 $NUMFILES); do
6560                 check_stripe_count $dir/file$i $expected
6561         done
6562         for i in $(seq 2 $NUMDIRS); do
6563                 for j in $(seq $NUMFILES); do
6564                 check_stripe_count $dir/dir$i/file$j $expected
6565                 done
6566         done
6567 }
6568 run_test 56w "check lfs_migrate -c stripe_count works"
6569
6570 test_56wb() {
6571         local file1=$DIR/$tdir/file1
6572         local create_pool=false
6573         local initial_pool=$($LFS getstripe -p $DIR)
6574         local pool_list=()
6575         local pool=""
6576
6577         echo -n "Creating test dir..."
6578         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6579         echo "done."
6580
6581         echo -n "Creating test file..."
6582         touch $file1 || error "cannot create file"
6583         echo "done."
6584
6585         echo -n "Detecting existing pools..."
6586         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6587
6588         if [ ${#pool_list[@]} -gt 0 ]; then
6589                 echo "${pool_list[@]}"
6590                 for thispool in "${pool_list[@]}"; do
6591                         if [[ -z "$initial_pool" ||
6592                               "$initial_pool" != "$thispool" ]]; then
6593                                 pool="$thispool"
6594                                 echo "Using existing pool '$pool'"
6595                                 break
6596                         fi
6597                 done
6598         else
6599                 echo "none detected."
6600         fi
6601         if [ -z "$pool" ]; then
6602                 pool=${POOL:-testpool}
6603                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6604                 echo -n "Creating pool '$pool'..."
6605                 create_pool=true
6606                 pool_add $pool &> /dev/null ||
6607                         error "pool_add failed"
6608                 echo "done."
6609
6610                 echo -n "Adding target to pool..."
6611                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6612                         error "pool_add_targets failed"
6613                 echo "done."
6614         fi
6615
6616         echo -n "Setting pool using -p option..."
6617         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6618                 error "migrate failed rc = $?"
6619         echo "done."
6620
6621         echo -n "Verifying test file is in pool after migrating..."
6622         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6623                 error "file was not migrated to pool $pool"
6624         echo "done."
6625
6626         echo -n "Removing test file from pool '$pool'..."
6627         # "lfs migrate $file" won't remove the file from the pool
6628         # until some striping information is changed.
6629         $LFS migrate -c 1 $file1 &> /dev/null ||
6630                 error "cannot remove from pool"
6631         [ "$($LFS getstripe -p $file1)" ] &&
6632                 error "pool still set"
6633         echo "done."
6634
6635         echo -n "Setting pool using --pool option..."
6636         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6637                 error "migrate failed rc = $?"
6638         echo "done."
6639
6640         # Clean up
6641         rm -f $file1
6642         if $create_pool; then
6643                 destroy_test_pools 2> /dev/null ||
6644                         error "destroy test pools failed"
6645         fi
6646 }
6647 run_test 56wb "check lfs_migrate pool support"
6648
6649 test_56wc() {
6650         local file1="$DIR/$tdir/file1"
6651         local parent_ssize
6652         local parent_scount
6653         local cur_ssize
6654         local cur_scount
6655         local orig_ssize
6656
6657         echo -n "Creating test dir..."
6658         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6659         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6660                 error "cannot set stripe by '-S 1M -c 1'"
6661         echo "done"
6662
6663         echo -n "Setting initial stripe for test file..."
6664         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6665                 error "cannot set stripe"
6666         cur_ssize=$($LFS getstripe -S "$file1")
6667         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
6668         echo "done."
6669
6670         # File currently set to -S 512K -c 1
6671
6672         # Ensure -c and -S options are rejected when -R is set
6673         echo -n "Verifying incompatible options are detected..."
6674         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6675                 error "incompatible -c and -R options not detected"
6676         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6677                 error "incompatible -S and -R options not detected"
6678         echo "done."
6679
6680         # Ensure unrecognized options are passed through to 'lfs migrate'
6681         echo -n "Verifying -S option is passed through to lfs migrate..."
6682         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6683                 error "migration failed"
6684         cur_ssize=$($LFS getstripe -S "$file1")
6685         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
6686         echo "done."
6687
6688         # File currently set to -S 1M -c 1
6689
6690         # Ensure long options are supported
6691         echo -n "Verifying long options supported..."
6692         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6693                 error "long option without argument not supported"
6694         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6695                 error "long option with argument not supported"
6696         cur_ssize=$($LFS getstripe -S "$file1")
6697         [ $cur_ssize -eq 524288 ] ||
6698                 error "migrate --stripe-size $cur_ssize != 524288"
6699         echo "done."
6700
6701         # File currently set to -S 512K -c 1
6702
6703         if [ "$OSTCOUNT" -gt 1 ]; then
6704                 echo -n "Verifying explicit stripe count can be set..."
6705                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6706                         error "migrate failed"
6707                 cur_scount=$($LFS getstripe -c "$file1")
6708                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
6709                 echo "done."
6710         fi
6711
6712         # File currently set to -S 512K -c 1 or -S 512K -c 2
6713
6714         # Ensure parent striping is used if -R is set, and no stripe
6715         # count or size is specified
6716         echo -n "Setting stripe for parent directory..."
6717         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6718                 error "cannot set stripe '-S 2M -c 1'"
6719         echo "done."
6720
6721         echo -n "Verifying restripe option uses parent stripe settings..."
6722         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6723         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
6724         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6725                 error "migrate failed"
6726         cur_ssize=$($LFS getstripe -S "$file1")
6727         [ $cur_ssize -eq $parent_ssize ] ||
6728                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
6729         cur_scount=$($LFS getstripe -c "$file1")
6730         [ $cur_scount -eq $parent_scount ] ||
6731                 error "migrate -R stripe_count $cur_scount != $parent_scount"
6732         echo "done."
6733
6734         # File currently set to -S 1M -c 1
6735
6736         # Ensure striping is preserved if -R is not set, and no stripe
6737         # count or size is specified
6738         echo -n "Verifying striping size preserved when not specified..."
6739         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
6740         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6741                 error "cannot set stripe on parent directory"
6742         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6743                 error "migrate failed"
6744         cur_ssize=$($LFS getstripe -S "$file1")
6745         [ $cur_ssize -eq $orig_ssize ] ||
6746                 error "migrate by default $cur_ssize != $orig_ssize"
6747         echo "done."
6748
6749         # Ensure file name properly detected when final option has no argument
6750         echo -n "Verifying file name properly detected..."
6751         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6752                 error "file name interpreted as option argument"
6753         echo "done."
6754
6755         # Clean up
6756         rm -f "$file1"
6757 }
6758 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6759
6760 test_56wd() {
6761         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6762
6763         local file1=$DIR/$tdir/file1
6764
6765         echo -n "Creating test dir..."
6766         test_mkdir $DIR/$tdir || error "cannot create dir"
6767         echo "done."
6768
6769         echo -n "Creating test file..."
6770         touch $file1
6771         echo "done."
6772
6773         # Ensure 'lfs migrate' will fail by using a non-existent option,
6774         # and make sure rsync is not called to recover
6775         echo -n "Make sure --no-rsync option works..."
6776         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6777                 grep -q 'refusing to fall back to rsync' ||
6778                 error "rsync was called with --no-rsync set"
6779         echo "done."
6780
6781         # Ensure rsync is called without trying 'lfs migrate' first
6782         echo -n "Make sure --rsync option works..."
6783         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6784                 grep -q 'falling back to rsync' &&
6785                 error "lfs migrate was called with --rsync set"
6786         echo "done."
6787
6788         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6789         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6790                 grep -q 'at the same time' ||
6791                 error "--rsync and --no-rsync accepted concurrently"
6792         echo "done."
6793
6794         # Clean up
6795         rm -f $file1
6796 }
6797 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6798
6799 test_56we() {
6800         local td=$DIR/$tdir
6801         local tf=$td/$tfile
6802
6803         test_mkdir $td || error "cannot create $td"
6804         touch $tf || error "cannot touch $tf"
6805
6806         echo -n "Make sure --non-direct|-D works..."
6807         $LFS_MIGRATE -y --non-direct -v $tf 2>&1 |
6808                 grep -q "lfs migrate --non-direct" ||
6809                 error "--non-direct option cannot work correctly"
6810         $LFS_MIGRATE -y -D -v $tf 2>&1 |
6811                 grep -q "lfs migrate -D" ||
6812                 error "-D option cannot work correctly"
6813         echo "done."
6814 }
6815 run_test 56we "check lfs_migrate --non-direct|-D support"
6816
6817 test_56x() {
6818         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6819         check_swap_layouts_support
6820
6821         local dir=$DIR/$tdir
6822         local ref1=/etc/passwd
6823         local file1=$dir/file1
6824
6825         test_mkdir $dir || error "creating dir $dir"
6826         $LFS setstripe -c 2 $file1
6827         cp $ref1 $file1
6828         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6829         stripe=$($LFS getstripe -c $file1)
6830         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6831         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6832
6833         # clean up
6834         rm -f $file1
6835 }
6836 run_test 56x "lfs migration support"
6837
6838 test_56xa() {
6839         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6840         check_swap_layouts_support
6841
6842         local dir=$DIR/$tdir/$testnum
6843
6844         test_mkdir -p $dir
6845
6846         local ref1=/etc/passwd
6847         local file1=$dir/file1
6848
6849         $LFS setstripe -c 2 $file1
6850         cp $ref1 $file1
6851         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6852
6853         local stripe=$($LFS getstripe -c $file1)
6854
6855         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6856         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6857
6858         # clean up
6859         rm -f $file1
6860 }
6861 run_test 56xa "lfs migration --block support"
6862
6863 check_migrate_links() {
6864         local dir="$1"
6865         local file1="$dir/file1"
6866         local begin="$2"
6867         local count="$3"
6868         local runas="$4"
6869         local total_count=$(($begin + $count - 1))
6870         local symlink_count=10
6871         local uniq_count=10
6872
6873         if [ ! -f "$file1" ]; then
6874                 echo -n "creating initial file..."
6875                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6876                         error "cannot setstripe initial file"
6877                 echo "done"
6878
6879                 echo -n "creating symlinks..."
6880                 for s in $(seq 1 $symlink_count); do
6881                         ln -s "$file1" "$dir/slink$s" ||
6882                                 error "cannot create symlinks"
6883                 done
6884                 echo "done"
6885
6886                 echo -n "creating nonlinked files..."
6887                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6888                         error "cannot create nonlinked files"
6889                 echo "done"
6890         fi
6891
6892         # create hard links
6893         if [ ! -f "$dir/file$total_count" ]; then
6894                 echo -n "creating hard links $begin:$total_count..."
6895                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6896                         /dev/null || error "cannot create hard links"
6897                 echo "done"
6898         fi
6899
6900         echo -n "checking number of hard links listed in xattrs..."
6901         local fid=$($LFS getstripe -F "$file1")
6902         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6903
6904         echo "${#paths[*]}"
6905         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6906                         skip "hard link list has unexpected size, skipping test"
6907         fi
6908         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6909                         error "link names should exceed xattrs size"
6910         fi
6911
6912         echo -n "migrating files..."
6913         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6914         local rc=$?
6915         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6916         echo "done"
6917
6918         # make sure all links have been properly migrated
6919         echo -n "verifying files..."
6920         fid=$($LFS getstripe -F "$file1") ||
6921                 error "cannot get fid for file $file1"
6922         for i in $(seq 2 $total_count); do
6923                 local fid2=$($LFS getstripe -F $dir/file$i)
6924
6925                 [ "$fid2" == "$fid" ] ||
6926                         error "migrated hard link has mismatched FID"
6927         done
6928
6929         # make sure hard links were properly detected, and migration was
6930         # performed only once for the entire link set; nonlinked files should
6931         # also be migrated
6932         local actual=$(grep -c 'done' <<< "$migrate_out")
6933         local expected=$(($uniq_count + 1))
6934
6935         [ "$actual" -eq  "$expected" ] ||
6936                 error "hard links individually migrated ($actual != $expected)"
6937
6938         # make sure the correct number of hard links are present
6939         local hardlinks=$(stat -c '%h' "$file1")
6940
6941         [ $hardlinks -eq $total_count ] ||
6942                 error "num hard links $hardlinks != $total_count"
6943         echo "done"
6944
6945         return 0
6946 }
6947
6948 test_56xb() {
6949         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6950                 skip "Need MDS version at least 2.10.55"
6951
6952         local dir="$DIR/$tdir"
6953
6954         test_mkdir "$dir" || error "cannot create dir $dir"
6955
6956         echo "testing lfs migrate mode when all links fit within xattrs"
6957         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6958
6959         echo "testing rsync mode when all links fit within xattrs"
6960         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6961
6962         echo "testing lfs migrate mode when all links do not fit within xattrs"
6963         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6964
6965         echo "testing rsync mode when all links do not fit within xattrs"
6966         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6967
6968         chown -R $RUNAS_ID $dir
6969         echo "testing non-root lfs migrate mode when not all links are in xattr"
6970         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
6971
6972         # clean up
6973         rm -rf $dir
6974 }
6975 run_test 56xb "lfs migration hard link support"
6976
6977 test_56xc() {
6978         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6979
6980         local dir="$DIR/$tdir"
6981
6982         test_mkdir "$dir" || error "cannot create dir $dir"
6983
6984         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
6985         echo -n "Setting initial stripe for 20MB test file..."
6986         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
6987                 error "cannot setstripe 20MB file"
6988         echo "done"
6989         echo -n "Sizing 20MB test file..."
6990         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
6991         echo "done"
6992         echo -n "Verifying small file autostripe count is 1..."
6993         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
6994                 error "cannot migrate 20MB file"
6995         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
6996                 error "cannot get stripe for $dir/20mb"
6997         [ $stripe_count -eq 1 ] ||
6998                 error "unexpected stripe count $stripe_count for 20MB file"
6999         rm -f "$dir/20mb"
7000         echo "done"
7001
7002         # Test 2: File is small enough to fit within the available space on
7003         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7004         # have at least an additional 1KB for each desired stripe for test 3
7005         echo -n "Setting stripe for 1GB test file..."
7006         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7007         echo "done"
7008         echo -n "Sizing 1GB test file..."
7009         # File size is 1GB + 3KB
7010         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7011         echo "done"
7012
7013         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7014         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7015         if (( avail > 524288 * OSTCOUNT )); then
7016                 echo -n "Migrating 1GB file..."
7017                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7018                         error "cannot migrate 1GB file"
7019                 echo "done"
7020                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7021                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7022                         error "cannot getstripe for 1GB file"
7023                 [ $stripe_count -eq 2 ] ||
7024                         error "unexpected stripe count $stripe_count != 2"
7025                 echo "done"
7026         fi
7027
7028         # Test 3: File is too large to fit within the available space on
7029         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7030         if [ $OSTCOUNT -ge 3 ]; then
7031                 # The required available space is calculated as
7032                 # file size (1GB + 3KB) / OST count (3).
7033                 local kb_per_ost=349526
7034
7035                 echo -n "Migrating 1GB file with limit..."
7036                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7037                         error "cannot migrate 1GB file with limit"
7038                 echo "done"
7039
7040                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7041                 echo -n "Verifying 1GB autostripe count with limited space..."
7042                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7043                         error "unexpected stripe count $stripe_count (min 3)"
7044                 echo "done"
7045         fi
7046
7047         # clean up
7048         rm -rf $dir
7049 }
7050 run_test 56xc "lfs migration autostripe"
7051
7052 test_56xd() {
7053         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7054
7055         local dir=$DIR/$tdir
7056         local f_mgrt=$dir/$tfile.mgrt
7057         local f_yaml=$dir/$tfile.yaml
7058         local f_copy=$dir/$tfile.copy
7059         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7060         local layout_copy="-c 2 -S 2M -i 1"
7061         local yamlfile=$dir/yamlfile
7062         local layout_before;
7063         local layout_after;
7064
7065         test_mkdir "$dir" || error "cannot create dir $dir"
7066         $LFS setstripe $layout_yaml $f_yaml ||
7067                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7068         $LFS getstripe --yaml $f_yaml > $yamlfile
7069         $LFS setstripe $layout_copy $f_copy ||
7070                 error "cannot setstripe $f_copy with layout $layout_copy"
7071         touch $f_mgrt
7072         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7073
7074         # 1. test option --yaml
7075         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7076                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7077         layout_before=$(get_layout_param $f_yaml)
7078         layout_after=$(get_layout_param $f_mgrt)
7079         [ "$layout_after" == "$layout_before" ] ||
7080                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7081
7082         # 2. test option --copy
7083         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7084                 error "cannot migrate $f_mgrt with --copy $f_copy"
7085         layout_before=$(get_layout_param $f_copy)
7086         layout_after=$(get_layout_param $f_mgrt)
7087         [ "$layout_after" == "$layout_before" ] ||
7088                 error "lfs_migrate --copy: $layout_after != $layout_before"
7089 }
7090 run_test 56xd "check lfs_migrate --yaml and --copy support"
7091
7092 test_56xe() {
7093         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7094
7095         local dir=$DIR/$tdir
7096         local f_comp=$dir/$tfile
7097         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7098         local layout_before=""
7099         local layout_after=""
7100
7101         test_mkdir "$dir" || error "cannot create dir $dir"
7102         $LFS setstripe $layout $f_comp ||
7103                 error "cannot setstripe $f_comp with layout $layout"
7104         layout_before=$(get_layout_param $f_comp)
7105         dd if=/dev/zero of=$f_comp bs=1M count=4
7106
7107         # 1. migrate a comp layout file by lfs_migrate
7108         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7109         layout_after=$(get_layout_param $f_comp)
7110         [ "$layout_before" == "$layout_after" ] ||
7111                 error "lfs_migrate: $layout_before != $layout_after"
7112
7113         # 2. migrate a comp layout file by lfs migrate
7114         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7115         layout_after=$(get_layout_param $f_comp)
7116         [ "$layout_before" == "$layout_after" ] ||
7117                 error "lfs migrate: $layout_before != $layout_after"
7118 }
7119 run_test 56xe "migrate a composite layout file"
7120
7121 test_56xf() {
7122         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7123
7124         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7125                 skip "Need server version at least 2.13.53"
7126
7127         local dir=$DIR/$tdir
7128         local f_comp=$dir/$tfile
7129         local layout="-E 1M -c1 -E -1 -c2"
7130         local fid_before=""
7131         local fid_after=""
7132
7133         test_mkdir "$dir" || error "cannot create dir $dir"
7134         $LFS setstripe $layout $f_comp ||
7135                 error "cannot setstripe $f_comp with layout $layout"
7136         fid_before=$($LFS getstripe --fid $f_comp)
7137         dd if=/dev/zero of=$f_comp bs=1M count=4
7138
7139         # 1. migrate a comp layout file to a comp layout
7140         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7141         fid_after=$($LFS getstripe --fid $f_comp)
7142         [ "$fid_before" == "$fid_after" ] ||
7143                 error "comp-to-comp migrate: $fid_before != $fid_after"
7144
7145         # 2. migrate a comp layout file to a plain layout
7146         $LFS migrate -c2 $f_comp ||
7147                 error "cannot migrate $f_comp by lfs migrate"
7148         fid_after=$($LFS getstripe --fid $f_comp)
7149         [ "$fid_before" == "$fid_after" ] ||
7150                 error "comp-to-plain migrate: $fid_before != $fid_after"
7151
7152         # 3. migrate a plain layout file to a comp layout
7153         $LFS migrate $layout $f_comp ||
7154                 error "cannot migrate $f_comp by lfs migrate"
7155         fid_after=$($LFS getstripe --fid $f_comp)
7156         [ "$fid_before" == "$fid_after" ] ||
7157                 error "plain-to-comp migrate: $fid_before != $fid_after"
7158 }
7159 run_test 56xf "FID is not lost during migration of a composite layout file"
7160
7161 test_56y() {
7162         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7163                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7164
7165         local res=""
7166         local dir=$DIR/$tdir
7167         local f1=$dir/file1
7168         local f2=$dir/file2
7169
7170         test_mkdir -p $dir || error "creating dir $dir"
7171         touch $f1 || error "creating std file $f1"
7172         $MULTIOP $f2 H2c || error "creating released file $f2"
7173
7174         # a directory can be raid0, so ask only for files
7175         res=$($LFS find $dir -L raid0 -type f | wc -l)
7176         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7177
7178         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7179         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7180
7181         # only files can be released, so no need to force file search
7182         res=$($LFS find $dir -L released)
7183         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7184
7185         res=$($LFS find $dir -type f \! -L released)
7186         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7187 }
7188 run_test 56y "lfs find -L raid0|released"
7189
7190 test_56z() { # LU-4824
7191         # This checks to make sure 'lfs find' continues after errors
7192         # There are two classes of errors that should be caught:
7193         # - If multiple paths are provided, all should be searched even if one
7194         #   errors out
7195         # - If errors are encountered during the search, it should not terminate
7196         #   early
7197         local dir=$DIR/$tdir
7198         local i
7199
7200         test_mkdir $dir
7201         for i in d{0..9}; do
7202                 test_mkdir $dir/$i
7203                 touch $dir/$i/$tfile
7204         done
7205         $LFS find $DIR/non_existent_dir $dir &&
7206                 error "$LFS find did not return an error"
7207         # Make a directory unsearchable. This should NOT be the last entry in
7208         # directory order.  Arbitrarily pick the 6th entry
7209         chmod 700 $($LFS find $dir -type d | sed '6!d')
7210
7211         $RUNAS $LFS find $DIR/non_existent $dir
7212         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7213
7214         # The user should be able to see 10 directories and 9 files
7215         (( count == 19 )) ||
7216                 error "$LFS find found $count != 19 entries after error"
7217 }
7218 run_test 56z "lfs find should continue after an error"
7219
7220 test_56aa() { # LU-5937
7221         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7222
7223         local dir=$DIR/$tdir
7224
7225         mkdir $dir
7226         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7227
7228         createmany -o $dir/striped_dir/${tfile}- 1024
7229         local dirs=$($LFS find --size +8k $dir/)
7230
7231         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7232 }
7233 run_test 56aa "lfs find --size under striped dir"
7234
7235 test_56ab() { # LU-10705
7236         test_mkdir $DIR/$tdir
7237         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7238         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7239         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7240         # Flush writes to ensure valid blocks.  Need to be more thorough for
7241         # ZFS, since blocks are not allocated/returned to client immediately.
7242         sync_all_data
7243         wait_zfs_commit ost1 2
7244         cancel_lru_locks osc
7245         ls -ls $DIR/$tdir
7246
7247         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7248
7249         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7250
7251         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7252         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7253
7254         rm -f $DIR/$tdir/$tfile.[123]
7255 }
7256 run_test 56ab "lfs find --blocks"
7257
7258 test_56ba() {
7259         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7260                 skip "Need MDS version at least 2.10.50"
7261
7262         # Create composite files with one component
7263         local dir=$DIR/$tdir
7264
7265         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7266         # Create composite files with three components
7267         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7268         # Create non-composite files
7269         createmany -o $dir/${tfile}- 10
7270
7271         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7272
7273         [[ $nfiles == 10 ]] ||
7274                 error "lfs find -E 1M found $nfiles != 10 files"
7275
7276         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7277         [[ $nfiles == 25 ]] ||
7278                 error "lfs find ! -E 1M found $nfiles != 25 files"
7279
7280         # All files have a component that starts at 0
7281         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7282         [[ $nfiles == 35 ]] ||
7283                 error "lfs find --component-start 0 - $nfiles != 35 files"
7284
7285         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7286         [[ $nfiles == 15 ]] ||
7287                 error "lfs find --component-start 2M - $nfiles != 15 files"
7288
7289         # All files created here have a componenet that does not starts at 2M
7290         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7291         [[ $nfiles == 35 ]] ||
7292                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7293
7294         # Find files with a specified number of components
7295         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7296         [[ $nfiles == 15 ]] ||
7297                 error "lfs find --component-count 3 - $nfiles != 15 files"
7298
7299         # Remember non-composite files have a component count of zero
7300         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7301         [[ $nfiles == 10 ]] ||
7302                 error "lfs find --component-count 0 - $nfiles != 10 files"
7303
7304         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7305         [[ $nfiles == 20 ]] ||
7306                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
7307
7308         # All files have a flag called "init"
7309         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
7310         [[ $nfiles == 35 ]] ||
7311                 error "lfs find --component-flags init - $nfiles != 35 files"
7312
7313         # Multi-component files will have a component not initialized
7314         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
7315         [[ $nfiles == 15 ]] ||
7316                 error "lfs find !--component-flags init - $nfiles != 15 files"
7317
7318         rm -rf $dir
7319
7320 }
7321 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
7322
7323 test_56ca() {
7324         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7325                 skip "Need MDS version at least 2.10.57"
7326
7327         local td=$DIR/$tdir
7328         local tf=$td/$tfile
7329         local dir
7330         local nfiles
7331         local cmd
7332         local i
7333         local j
7334
7335         # create mirrored directories and mirrored files
7336         mkdir $td || error "mkdir $td failed"
7337         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7338         createmany -o $tf- 10 || error "create $tf- failed"
7339
7340         for i in $(seq 2); do
7341                 dir=$td/dir$i
7342                 mkdir $dir || error "mkdir $dir failed"
7343                 $LFS mirror create -N$((3 + i)) $dir ||
7344                         error "create mirrored dir $dir failed"
7345                 createmany -o $dir/$tfile- 10 ||
7346                         error "create $dir/$tfile- failed"
7347         done
7348
7349         # change the states of some mirrored files
7350         echo foo > $tf-6
7351         for i in $(seq 2); do
7352                 dir=$td/dir$i
7353                 for j in $(seq 4 9); do
7354                         echo foo > $dir/$tfile-$j
7355                 done
7356         done
7357
7358         # find mirrored files with specific mirror count
7359         cmd="$LFS find --mirror-count 3 --type f $td"
7360         nfiles=$($cmd | wc -l)
7361         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7362
7363         cmd="$LFS find ! --mirror-count 3 --type f $td"
7364         nfiles=$($cmd | wc -l)
7365         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7366
7367         cmd="$LFS find --mirror-count +2 --type f $td"
7368         nfiles=$($cmd | wc -l)
7369         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7370
7371         cmd="$LFS find --mirror-count -6 --type f $td"
7372         nfiles=$($cmd | wc -l)
7373         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7374
7375         # find mirrored files with specific file state
7376         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7377         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7378
7379         cmd="$LFS find --mirror-state=ro --type f $td"
7380         nfiles=$($cmd | wc -l)
7381         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7382
7383         cmd="$LFS find ! --mirror-state=ro --type f $td"
7384         nfiles=$($cmd | wc -l)
7385         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7386
7387         cmd="$LFS find --mirror-state=wp --type f $td"
7388         nfiles=$($cmd | wc -l)
7389         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7390
7391         cmd="$LFS find ! --mirror-state=sp --type f $td"
7392         nfiles=$($cmd | wc -l)
7393         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7394 }
7395 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7396
7397 test_57a() {
7398         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7399         # note test will not do anything if MDS is not local
7400         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7401                 skip_env "ldiskfs only test"
7402         fi
7403         remote_mds_nodsh && skip "remote MDS with nodsh"
7404
7405         local MNTDEV="osd*.*MDT*.mntdev"
7406         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7407         [ -z "$DEV" ] && error "can't access $MNTDEV"
7408         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7409                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7410                         error "can't access $DEV"
7411                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7412                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7413                 rm $TMP/t57a.dump
7414         done
7415 }
7416 run_test 57a "verify MDS filesystem created with large inodes =="
7417
7418 test_57b() {
7419         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7420         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7421                 skip_env "ldiskfs only test"
7422         fi
7423         remote_mds_nodsh && skip "remote MDS with nodsh"
7424
7425         local dir=$DIR/$tdir
7426         local filecount=100
7427         local file1=$dir/f1
7428         local fileN=$dir/f$filecount
7429
7430         rm -rf $dir || error "removing $dir"
7431         test_mkdir -c1 $dir
7432         local mdtidx=$($LFS getstripe -m $dir)
7433         local mdtname=MDT$(printf %04x $mdtidx)
7434         local facet=mds$((mdtidx + 1))
7435
7436         echo "mcreating $filecount files"
7437         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7438
7439         # verify that files do not have EAs yet
7440         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7441                 error "$file1 has an EA"
7442         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7443                 error "$fileN has an EA"
7444
7445         sync
7446         sleep 1
7447         df $dir  #make sure we get new statfs data
7448         local mdsfree=$(do_facet $facet \
7449                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7450         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7451         local file
7452
7453         echo "opening files to create objects/EAs"
7454         for file in $(seq -f $dir/f%g 1 $filecount); do
7455                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7456                         error "opening $file"
7457         done
7458
7459         # verify that files have EAs now
7460         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7461         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7462
7463         sleep 1  #make sure we get new statfs data
7464         df $dir
7465         local mdsfree2=$(do_facet $facet \
7466                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7467         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7468
7469         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7470                 if [ "$mdsfree" != "$mdsfree2" ]; then
7471                         error "MDC before $mdcfree != after $mdcfree2"
7472                 else
7473                         echo "MDC before $mdcfree != after $mdcfree2"
7474                         echo "unable to confirm if MDS has large inodes"
7475                 fi
7476         fi
7477         rm -rf $dir
7478 }
7479 run_test 57b "default LOV EAs are stored inside large inodes ==="
7480
7481 test_58() {
7482         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7483         [ -z "$(which wiretest 2>/dev/null)" ] &&
7484                         skip_env "could not find wiretest"
7485
7486         wiretest
7487 }
7488 run_test 58 "verify cross-platform wire constants =============="
7489
7490 test_59() {
7491         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7492
7493         echo "touch 130 files"
7494         createmany -o $DIR/f59- 130
7495         echo "rm 130 files"
7496         unlinkmany $DIR/f59- 130
7497         sync
7498         # wait for commitment of removal
7499         wait_delete_completed
7500 }
7501 run_test 59 "verify cancellation of llog records async ========="
7502
7503 TEST60_HEAD="test_60 run $RANDOM"
7504 test_60a() {
7505         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7506         remote_mgs_nodsh && skip "remote MGS with nodsh"
7507         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7508                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7509                         skip_env "missing subtest run-llog.sh"
7510
7511         log "$TEST60_HEAD - from kernel mode"
7512         do_facet mgs "$LCTL dk > /dev/null"
7513         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7514         do_facet mgs $LCTL dk > $TMP/$tfile
7515
7516         # LU-6388: test llog_reader
7517         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7518         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7519         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7520                         skip_env "missing llog_reader"
7521         local fstype=$(facet_fstype mgs)
7522         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7523                 skip_env "Only for ldiskfs or zfs type mgs"
7524
7525         local mntpt=$(facet_mntpt mgs)
7526         local mgsdev=$(mgsdevname 1)
7527         local fid_list
7528         local fid
7529         local rec_list
7530         local rec
7531         local rec_type
7532         local obj_file
7533         local path
7534         local seq
7535         local oid
7536         local pass=true
7537
7538         #get fid and record list
7539         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7540                 tail -n 4))
7541         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7542                 tail -n 4))
7543         #remount mgs as ldiskfs or zfs type
7544         stop mgs || error "stop mgs failed"
7545         mount_fstype mgs || error "remount mgs failed"
7546         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7547                 fid=${fid_list[i]}
7548                 rec=${rec_list[i]}
7549                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7550                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7551                 oid=$((16#$oid))
7552
7553                 case $fstype in
7554                         ldiskfs )
7555                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7556                         zfs )
7557                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7558                 esac
7559                 echo "obj_file is $obj_file"
7560                 do_facet mgs $llog_reader $obj_file
7561
7562                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7563                         awk '{ print $3 }' | sed -e "s/^type=//g")
7564                 if [ $rec_type != $rec ]; then
7565                         echo "FAILED test_60a wrong record type $rec_type," \
7566                               "should be $rec"
7567                         pass=false
7568                         break
7569                 fi
7570
7571                 #check obj path if record type is LLOG_LOGID_MAGIC
7572                 if [ "$rec" == "1064553b" ]; then
7573                         path=$(do_facet mgs $llog_reader $obj_file |
7574                                 grep "path=" | awk '{ print $NF }' |
7575                                 sed -e "s/^path=//g")
7576                         if [ $obj_file != $mntpt/$path ]; then
7577                                 echo "FAILED test_60a wrong obj path" \
7578                                       "$montpt/$path, should be $obj_file"
7579                                 pass=false
7580                                 break
7581                         fi
7582                 fi
7583         done
7584         rm -f $TMP/$tfile
7585         #restart mgs before "error", otherwise it will block the next test
7586         stop mgs || error "stop mgs failed"
7587         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7588         $pass || error "test failed, see FAILED test_60a messages for specifics"
7589 }
7590 run_test 60a "llog_test run from kernel module and test llog_reader"
7591
7592 test_60b() { # bug 6411
7593         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7594
7595         dmesg > $DIR/$tfile
7596         LLOG_COUNT=$(do_facet mgs dmesg |
7597                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7598                           /llog_[a-z]*.c:[0-9]/ {
7599                                 if (marker)
7600                                         from_marker++
7601                                 from_begin++
7602                           }
7603                           END {
7604                                 if (marker)
7605                                         print from_marker
7606                                 else
7607                                         print from_begin
7608                           }")
7609
7610         [[ $LLOG_COUNT -gt 120 ]] &&
7611                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7612 }
7613 run_test 60b "limit repeated messages from CERROR/CWARN"
7614
7615 test_60c() {
7616         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7617
7618         echo "create 5000 files"
7619         createmany -o $DIR/f60c- 5000
7620 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7621         lctl set_param fail_loc=0x80000137
7622         unlinkmany $DIR/f60c- 5000
7623         lctl set_param fail_loc=0
7624 }
7625 run_test 60c "unlink file when mds full"
7626
7627 test_60d() {
7628         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7629
7630         SAVEPRINTK=$(lctl get_param -n printk)
7631         # verify "lctl mark" is even working"
7632         MESSAGE="test message ID $RANDOM $$"
7633         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7634         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7635
7636         lctl set_param printk=0 || error "set lnet.printk failed"
7637         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7638         MESSAGE="new test message ID $RANDOM $$"
7639         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7640         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7641         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7642
7643         lctl set_param -n printk="$SAVEPRINTK"
7644 }
7645 run_test 60d "test printk console message masking"
7646
7647 test_60e() {
7648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7649         remote_mds_nodsh && skip "remote MDS with nodsh"
7650
7651         touch $DIR/$tfile
7652 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7653         do_facet mds1 lctl set_param fail_loc=0x15b
7654         rm $DIR/$tfile
7655 }
7656 run_test 60e "no space while new llog is being created"
7657
7658 test_60g() {
7659         local pid
7660         local i
7661
7662         test_mkdir -c $MDSCOUNT $DIR/$tdir
7663
7664         (
7665                 local index=0
7666                 while true; do
7667                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7668                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7669                                 2>/dev/null
7670                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7671                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7672                         index=$((index + 1))
7673                 done
7674         ) &
7675
7676         pid=$!
7677
7678         for i in {0..100}; do
7679                 # define OBD_FAIL_OSD_TXN_START    0x19a
7680                 local index=$((i % MDSCOUNT + 1))
7681
7682                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7683                         > /dev/null
7684                 usleep 100
7685         done
7686
7687         kill -9 $pid
7688
7689         for i in $(seq $MDSCOUNT); do
7690                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7691         done
7692
7693         mkdir $DIR/$tdir/new || error "mkdir failed"
7694         rmdir $DIR/$tdir/new || error "rmdir failed"
7695
7696         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7697                 -t namespace
7698         for i in $(seq $MDSCOUNT); do
7699                 wait_update_facet mds$i "$LCTL get_param -n \
7700                         mdd.$(facet_svc mds$i).lfsck_namespace |
7701                         awk '/^status/ { print \\\$2 }'" "completed"
7702         done
7703
7704         ls -R $DIR/$tdir || error "ls failed"
7705         rm -rf $DIR/$tdir || error "rmdir failed"
7706 }
7707 run_test 60g "transaction abort won't cause MDT hung"
7708
7709 test_60h() {
7710         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7711                 skip "Need MDS version at least 2.12.52"
7712         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7713
7714         local f
7715
7716         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7717         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7718         for fail_loc in 0x80000188 0x80000189; do
7719                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7720                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7721                         error "mkdir $dir-$fail_loc failed"
7722                 for i in {0..10}; do
7723                         # create may fail on missing stripe
7724                         echo $i > $DIR/$tdir-$fail_loc/$i
7725                 done
7726                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7727                         error "getdirstripe $tdir-$fail_loc failed"
7728                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7729                         error "migrate $tdir-$fail_loc failed"
7730                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7731                         error "getdirstripe $tdir-$fail_loc failed"
7732                 pushd $DIR/$tdir-$fail_loc
7733                 for f in *; do
7734                         echo $f | cmp $f - || error "$f data mismatch"
7735                 done
7736                 popd
7737                 rm -rf $DIR/$tdir-$fail_loc
7738         done
7739 }
7740 run_test 60h "striped directory with missing stripes can be accessed"
7741
7742 test_61a() {
7743         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7744
7745         f="$DIR/f61"
7746         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7747         cancel_lru_locks osc
7748         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7749         sync
7750 }
7751 run_test 61a "mmap() writes don't make sync hang ================"
7752
7753 test_61b() {
7754         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7755 }
7756 run_test 61b "mmap() of unstriped file is successful"
7757
7758 # bug 2330 - insufficient obd_match error checking causes LBUG
7759 test_62() {
7760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7761
7762         f="$DIR/f62"
7763         echo foo > $f
7764         cancel_lru_locks osc
7765         lctl set_param fail_loc=0x405
7766         cat $f && error "cat succeeded, expect -EIO"
7767         lctl set_param fail_loc=0
7768 }
7769 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7770 # match every page all of the time.
7771 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7772
7773 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7774 # Though this test is irrelevant anymore, it helped to reveal some
7775 # other grant bugs (LU-4482), let's keep it.
7776 test_63a() {   # was test_63
7777         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7778
7779         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7780
7781         for i in `seq 10` ; do
7782                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7783                 sleep 5
7784                 kill $!
7785                 sleep 1
7786         done
7787
7788         rm -f $DIR/f63 || true
7789 }
7790 run_test 63a "Verify oig_wait interruption does not crash ======="
7791
7792 # bug 2248 - async write errors didn't return to application on sync
7793 # bug 3677 - async write errors left page locked
7794 test_63b() {
7795         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7796
7797         debugsave
7798         lctl set_param debug=-1
7799
7800         # ensure we have a grant to do async writes
7801         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7802         rm $DIR/$tfile
7803
7804         sync    # sync lest earlier test intercept the fail_loc
7805
7806         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7807         lctl set_param fail_loc=0x80000406
7808         $MULTIOP $DIR/$tfile Owy && \
7809                 error "sync didn't return ENOMEM"
7810         sync; sleep 2; sync     # do a real sync this time to flush page
7811         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7812                 error "locked page left in cache after async error" || true
7813         debugrestore
7814 }
7815 run_test 63b "async write errors should be returned to fsync ==="
7816
7817 test_64a () {
7818         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7819
7820         lfs df $DIR
7821         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
7822 }
7823 run_test 64a "verify filter grant calculations (in kernel) ====="
7824
7825 test_64b () {
7826         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7827
7828         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7829 }
7830 run_test 64b "check out-of-space detection on client"
7831
7832 test_64c() {
7833         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7834 }
7835 run_test 64c "verify grant shrink"
7836
7837 # this does exactly what osc_request.c:osc_announce_cached() does in
7838 # order to calculate max amount of grants to ask from server
7839 want_grant() {
7840         local tgt=$1
7841
7842         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7843         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7844
7845         ((rpc_in_flight ++));
7846         nrpages=$((nrpages * rpc_in_flight))
7847
7848         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7849
7850         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7851
7852         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7853         local undirty=$((nrpages * PAGE_SIZE))
7854
7855         local max_extent_pages
7856         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7857             grep grant_max_extent_size | awk '{print $2}')
7858         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7859         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7860         local grant_extent_tax
7861         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7862             grep grant_extent_tax | awk '{print $2}')
7863
7864         undirty=$((undirty + nrextents * grant_extent_tax))
7865
7866         echo $undirty
7867 }
7868
7869 # this is size of unit for grant allocation. It should be equal to
7870 # what tgt_grant.c:tgt_grant_chunk() calculates
7871 grant_chunk() {
7872         local tgt=$1
7873         local max_brw_size
7874         local grant_extent_tax
7875
7876         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7877             grep max_brw_size | awk '{print $2}')
7878
7879         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7880             grep grant_extent_tax | awk '{print $2}')
7881
7882         echo $(((max_brw_size + grant_extent_tax) * 2))
7883 }
7884
7885 test_64d() {
7886         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7887                 skip "OST < 2.10.55 doesn't limit grants enough"
7888
7889         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7890         local file=$DIR/$tfile
7891
7892         [[ $($LCTL get_param osc.${tgt}.import |
7893              grep "connect_flags:.*grant_param") ]] ||
7894                 skip "no grant_param connect flag"
7895
7896         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7897
7898         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7899
7900         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7901         stack_trap "rm -f $file" EXIT
7902
7903         $LFS setstripe $file -i 0 -c 1
7904         dd if=/dev/zero of=$file bs=1M count=1000 &
7905         ddpid=$!
7906
7907         while true
7908         do
7909                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7910                 if [[ $cur_grant -gt $max_cur_granted ]]
7911                 then
7912                         kill $ddpid
7913                         error "cur_grant $cur_grant > $max_cur_granted"
7914                 fi
7915                 kill -0 $ddpid
7916                 [[ $? -ne 0 ]] && break;
7917                 sleep 2
7918         done
7919
7920         rm -f $DIR/$tfile
7921         wait_delete_completed
7922         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7923 }
7924 run_test 64d "check grant limit exceed"
7925
7926 # bug 1414 - set/get directories' stripe info
7927 test_65a() {
7928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7929
7930         test_mkdir $DIR/$tdir
7931         touch $DIR/$tdir/f1
7932         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7933 }
7934 run_test 65a "directory with no stripe info"
7935
7936 test_65b() {
7937         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7938
7939         test_mkdir $DIR/$tdir
7940         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7941
7942         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7943                                                 error "setstripe"
7944         touch $DIR/$tdir/f2
7945         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7946 }
7947 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7948
7949 test_65c() {
7950         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7951         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7952
7953         test_mkdir $DIR/$tdir
7954         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7955
7956         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7957                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7958         touch $DIR/$tdir/f3
7959         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7960 }
7961 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7962
7963 test_65d() {
7964         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7965
7966         test_mkdir $DIR/$tdir
7967         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7968         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7969
7970         if [[ $STRIPECOUNT -le 0 ]]; then
7971                 sc=1
7972         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
7973                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
7974                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
7975         else
7976                 sc=$(($STRIPECOUNT - 1))
7977         fi
7978         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
7979         touch $DIR/$tdir/f4 $DIR/$tdir/f5
7980         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
7981                 error "lverify failed"
7982 }
7983 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
7984
7985 test_65e() {
7986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7987
7988         test_mkdir $DIR/$tdir
7989
7990         $LFS setstripe $DIR/$tdir || error "setstripe"
7991         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
7992                                         error "no stripe info failed"
7993         touch $DIR/$tdir/f6
7994         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
7995 }
7996 run_test 65e "directory setstripe defaults"
7997
7998 test_65f() {
7999         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8000
8001         test_mkdir $DIR/${tdir}f
8002         $RUNAS $LFS setstripe $DIR/${tdir}f &&
8003                 error "setstripe succeeded" || true
8004 }
8005 run_test 65f "dir setstripe permission (should return error) ==="
8006
8007 test_65g() {
8008         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8009
8010         test_mkdir $DIR/$tdir
8011         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8012
8013         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8014                 error "setstripe -S failed"
8015         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
8016         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8017                 error "delete default stripe failed"
8018 }
8019 run_test 65g "directory setstripe -d"
8020
8021 test_65h() {
8022         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8023
8024         test_mkdir $DIR/$tdir
8025         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8026
8027         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8028                 error "setstripe -S failed"
8029         test_mkdir $DIR/$tdir/dd1
8030         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
8031                 error "stripe info inherit failed"
8032 }
8033 run_test 65h "directory stripe info inherit ===================="
8034
8035 test_65i() {
8036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8037
8038         save_layout_restore_at_exit $MOUNT
8039
8040         # bug6367: set non-default striping on root directory
8041         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
8042
8043         # bug12836: getstripe on -1 default directory striping
8044         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
8045
8046         # bug12836: getstripe -v on -1 default directory striping
8047         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
8048
8049         # bug12836: new find on -1 default directory striping
8050         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
8051 }
8052 run_test 65i "various tests to set root directory striping"
8053
8054 test_65j() { # bug6367
8055         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8056
8057         sync; sleep 1
8058
8059         # if we aren't already remounting for each test, do so for this test
8060         if [ "$I_MOUNTED" = "yes" ]; then
8061                 cleanup || error "failed to unmount"
8062                 setup
8063         fi
8064
8065         save_layout_restore_at_exit $MOUNT
8066
8067         $LFS setstripe -d $MOUNT || error "setstripe failed"
8068 }
8069 run_test 65j "set default striping on root directory (bug 6367)="
8070
8071 cleanup_65k() {
8072         rm -rf $DIR/$tdir
8073         wait_delete_completed
8074         do_facet $SINGLEMDS "lctl set_param -n \
8075                 osp.$ost*MDT0000.max_create_count=$max_count"
8076         do_facet $SINGLEMDS "lctl set_param -n \
8077                 osp.$ost*MDT0000.create_count=$count"
8078         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8079         echo $INACTIVE_OSC "is Activate"
8080
8081         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8082 }
8083
8084 test_65k() { # bug11679
8085         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8086         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8087         remote_mds_nodsh && skip "remote MDS with nodsh"
8088
8089         local disable_precreate=true
8090         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
8091                 disable_precreate=false
8092
8093         echo "Check OST status: "
8094         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
8095                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
8096
8097         for OSC in $MDS_OSCS; do
8098                 echo $OSC "is active"
8099                 do_facet $SINGLEMDS lctl --device %$OSC activate
8100         done
8101
8102         for INACTIVE_OSC in $MDS_OSCS; do
8103                 local ost=$(osc_to_ost $INACTIVE_OSC)
8104                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
8105                                lov.*md*.target_obd |
8106                                awk -F: /$ost/'{ print $1 }' | head -n 1)
8107
8108                 mkdir -p $DIR/$tdir
8109                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
8110                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
8111
8112                 echo "Deactivate: " $INACTIVE_OSC
8113                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
8114
8115                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
8116                               osp.$ost*MDT0000.create_count")
8117                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
8118                                   osp.$ost*MDT0000.max_create_count")
8119                 $disable_precreate &&
8120                         do_facet $SINGLEMDS "lctl set_param -n \
8121                                 osp.$ost*MDT0000.max_create_count=0"
8122
8123                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
8124                         [ -f $DIR/$tdir/$idx ] && continue
8125                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
8126                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
8127                                 { cleanup_65k;
8128                                   error "setstripe $idx should succeed"; }
8129                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
8130                 done
8131                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
8132                 rmdir $DIR/$tdir
8133
8134                 do_facet $SINGLEMDS "lctl set_param -n \
8135                         osp.$ost*MDT0000.max_create_count=$max_count"
8136                 do_facet $SINGLEMDS "lctl set_param -n \
8137                         osp.$ost*MDT0000.create_count=$count"
8138                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8139                 echo $INACTIVE_OSC "is Activate"
8140
8141                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8142         done
8143 }
8144 run_test 65k "validate manual striping works properly with deactivated OSCs"
8145
8146 test_65l() { # bug 12836
8147         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8148
8149         test_mkdir -p $DIR/$tdir/test_dir
8150         $LFS setstripe -c -1 $DIR/$tdir/test_dir
8151         $LFS find -mtime -1 $DIR/$tdir >/dev/null
8152 }
8153 run_test 65l "lfs find on -1 stripe dir ========================"
8154
8155 test_65m() {
8156         local layout=$(save_layout $MOUNT)
8157         $RUNAS $LFS setstripe -c 2 $MOUNT && {
8158                 restore_layout $MOUNT $layout
8159                 error "setstripe should fail by non-root users"
8160         }
8161         true
8162 }
8163 run_test 65m "normal user can't set filesystem default stripe"
8164
8165 test_65n() {
8166         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
8167         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
8168                 skip "Need MDS version at least 2.12.50"
8169         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
8170
8171         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
8172         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
8173         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
8174
8175         local root_layout=$(save_layout $MOUNT)
8176         stack_trap "restore_layout $MOUNT $root_layout" EXIT
8177
8178         # new subdirectory under root directory should not inherit
8179         # the default layout from root
8180         local dir1=$MOUNT/$tdir-1
8181         mkdir $dir1 || error "mkdir $dir1 failed"
8182         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
8183                 error "$dir1 shouldn't have LOV EA"
8184
8185         # delete the default layout on root directory
8186         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
8187
8188         local dir2=$MOUNT/$tdir-2
8189         mkdir $dir2 || error "mkdir $dir2 failed"
8190         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
8191                 error "$dir2 shouldn't have LOV EA"
8192
8193         # set a new striping pattern on root directory
8194         local def_stripe_size=$($LFS getstripe -S $MOUNT)
8195         local new_def_stripe_size=$((def_stripe_size * 2))
8196         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
8197                 error "set stripe size on $MOUNT failed"
8198
8199         # new file created in $dir2 should inherit the new stripe size from
8200         # the filesystem default
8201         local file2=$dir2/$tfile-2
8202         touch $file2 || error "touch $file2 failed"
8203
8204         local file2_stripe_size=$($LFS getstripe -S $file2)
8205         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
8206                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
8207
8208         local dir3=$MOUNT/$tdir-3
8209         mkdir $dir3 || error "mkdir $dir3 failed"
8210         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
8211         # the root layout, which is the actual default layout that will be used
8212         # when new files are created in $dir3.
8213         local dir3_layout=$(get_layout_param $dir3)
8214         local root_dir_layout=$(get_layout_param $MOUNT)
8215         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
8216                 error "$dir3 should show the default layout from $MOUNT"
8217
8218         # set OST pool on root directory
8219         local pool=$TESTNAME
8220         pool_add $pool || error "add $pool failed"
8221         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8222                 error "add targets to $pool failed"
8223
8224         $LFS setstripe -p $pool $MOUNT ||
8225                 error "set OST pool on $MOUNT failed"
8226
8227         # new file created in $dir3 should inherit the pool from
8228         # the filesystem default
8229         local file3=$dir3/$tfile-3
8230         touch $file3 || error "touch $file3 failed"
8231
8232         local file3_pool=$($LFS getstripe -p $file3)
8233         [[ "$file3_pool" = "$pool" ]] ||
8234                 error "$file3 didn't inherit OST pool $pool"
8235
8236         local dir4=$MOUNT/$tdir-4
8237         mkdir $dir4 || error "mkdir $dir4 failed"
8238         local dir4_layout=$(get_layout_param $dir4)
8239         root_dir_layout=$(get_layout_param $MOUNT)
8240         echo "$LFS getstripe -d $dir4"
8241         $LFS getstripe -d $dir4
8242         echo "$LFS getstripe -d $MOUNT"
8243         $LFS getstripe -d $MOUNT
8244         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
8245                 error "$dir4 should show the default layout from $MOUNT"
8246
8247         # new file created in $dir4 should inherit the pool from
8248         # the filesystem default
8249         local file4=$dir4/$tfile-4
8250         touch $file4 || error "touch $file4 failed"
8251
8252         local file4_pool=$($LFS getstripe -p $file4)
8253         [[ "$file4_pool" = "$pool" ]] ||
8254                 error "$file4 didn't inherit OST pool $pool"
8255
8256         # new subdirectory under non-root directory should inherit
8257         # the default layout from its parent directory
8258         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
8259                 error "set directory layout on $dir4 failed"
8260
8261         local dir5=$dir4/$tdir-5
8262         mkdir $dir5 || error "mkdir $dir5 failed"
8263
8264         dir4_layout=$(get_layout_param $dir4)
8265         local dir5_layout=$(get_layout_param $dir5)
8266         [[ "$dir4_layout" = "$dir5_layout" ]] ||
8267                 error "$dir5 should inherit the default layout from $dir4"
8268
8269         # though subdir under ROOT doesn't inherit default layout, but
8270         # its sub dir/file should be created with default layout.
8271         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
8272         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
8273                 skip "Need MDS version at least 2.12.59"
8274
8275         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
8276         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
8277         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
8278
8279         if [ $default_lmv_hash == "none" ]; then
8280                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
8281         else
8282                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
8283                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
8284         fi
8285
8286         $LFS setdirstripe -D -c 2 $MOUNT ||
8287                 error "setdirstripe -D -c 2 failed"
8288         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
8289         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
8290         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
8291 }
8292 run_test 65n "don't inherit default layout from root for new subdirectories"
8293
8294 # bug 2543 - update blocks count on client
8295 test_66() {
8296         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8297
8298         COUNT=${COUNT:-8}
8299         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
8300         sync; sync_all_data; sync; sync_all_data
8301         cancel_lru_locks osc
8302         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
8303         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
8304 }
8305 run_test 66 "update inode blocks count on client ==============="
8306
8307 meminfo() {
8308         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
8309 }
8310
8311 swap_used() {
8312         swapon -s | awk '($1 == "'$1'") { print $4 }'
8313 }
8314
8315 # bug5265, obdfilter oa2dentry return -ENOENT
8316 # #define OBD_FAIL_SRV_ENOENT 0x217
8317 test_69() {
8318         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8319         remote_ost_nodsh && skip "remote OST with nodsh"
8320
8321         f="$DIR/$tfile"
8322         $LFS setstripe -c 1 -i 0 $f
8323
8324         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
8325
8326         do_facet ost1 lctl set_param fail_loc=0x217
8327         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8328         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8329
8330         do_facet ost1 lctl set_param fail_loc=0
8331         $DIRECTIO write $f 0 2 || error "write error"
8332
8333         cancel_lru_locks osc
8334         $DIRECTIO read $f 0 1 || error "read error"
8335
8336         do_facet ost1 lctl set_param fail_loc=0x217
8337         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8338
8339         do_facet ost1 lctl set_param fail_loc=0
8340         rm -f $f
8341 }
8342 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8343
8344 test_71() {
8345         test_mkdir $DIR/$tdir
8346         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8347         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8348 }
8349 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8350
8351 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8353         [ "$RUNAS_ID" = "$UID" ] &&
8354                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8355         # Check that testing environment is properly set up. Skip if not
8356         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8357                 skip_env "User $RUNAS_ID does not exist - skipping"
8358
8359         touch $DIR/$tfile
8360         chmod 777 $DIR/$tfile
8361         chmod ug+s $DIR/$tfile
8362         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8363                 error "$RUNAS dd $DIR/$tfile failed"
8364         # See if we are still setuid/sgid
8365         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8366                 error "S/gid is not dropped on write"
8367         # Now test that MDS is updated too
8368         cancel_lru_locks mdc
8369         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8370                 error "S/gid is not dropped on MDS"
8371         rm -f $DIR/$tfile
8372 }
8373 run_test 72a "Test that remove suid works properly (bug5695) ===="
8374
8375 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8376         local perm
8377
8378         [ "$RUNAS_ID" = "$UID" ] &&
8379                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8380         [ "$RUNAS_ID" -eq 0 ] &&
8381                 skip_env "RUNAS_ID = 0 -- skipping"
8382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8383         # Check that testing environment is properly set up. Skip if not
8384         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8385                 skip_env "User $RUNAS_ID does not exist - skipping"
8386
8387         touch $DIR/${tfile}-f{g,u}
8388         test_mkdir $DIR/${tfile}-dg
8389         test_mkdir $DIR/${tfile}-du
8390         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8391         chmod g+s $DIR/${tfile}-{f,d}g
8392         chmod u+s $DIR/${tfile}-{f,d}u
8393         for perm in 777 2777 4777; do
8394                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8395                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8396                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8397                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8398         done
8399         true
8400 }
8401 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8402
8403 # bug 3462 - multiple simultaneous MDC requests
8404 test_73() {
8405         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8406
8407         test_mkdir $DIR/d73-1
8408         test_mkdir $DIR/d73-2
8409         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8410         pid1=$!
8411
8412         lctl set_param fail_loc=0x80000129
8413         $MULTIOP $DIR/d73-1/f73-2 Oc &
8414         sleep 1
8415         lctl set_param fail_loc=0
8416
8417         $MULTIOP $DIR/d73-2/f73-3 Oc &
8418         pid3=$!
8419
8420         kill -USR1 $pid1
8421         wait $pid1 || return 1
8422
8423         sleep 25
8424
8425         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8426         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8427         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8428
8429         rm -rf $DIR/d73-*
8430 }
8431 run_test 73 "multiple MDC requests (should not deadlock)"
8432
8433 test_74a() { # bug 6149, 6184
8434         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8435
8436         touch $DIR/f74a
8437         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8438         #
8439         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8440         # will spin in a tight reconnection loop
8441         $LCTL set_param fail_loc=0x8000030e
8442         # get any lock that won't be difficult - lookup works.
8443         ls $DIR/f74a
8444         $LCTL set_param fail_loc=0
8445         rm -f $DIR/f74a
8446         true
8447 }
8448 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8449
8450 test_74b() { # bug 13310
8451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8452
8453         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8454         #
8455         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8456         # will spin in a tight reconnection loop
8457         $LCTL set_param fail_loc=0x8000030e
8458         # get a "difficult" lock
8459         touch $DIR/f74b
8460         $LCTL set_param fail_loc=0
8461         rm -f $DIR/f74b
8462         true
8463 }
8464 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8465
8466 test_74c() {
8467         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8468
8469         #define OBD_FAIL_LDLM_NEW_LOCK
8470         $LCTL set_param fail_loc=0x319
8471         touch $DIR/$tfile && error "touch successful"
8472         $LCTL set_param fail_loc=0
8473         true
8474 }
8475 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8476
8477 num_inodes() {
8478         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8479 }
8480
8481 test_76() { # Now for bug 20433, added originally in bug 1443
8482         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8483
8484         cancel_lru_locks osc
8485         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8486         local before=$(num_inodes)
8487         local count=$((512 * cpus))
8488         [ "$SLOW" = "no" ] && count=$((64 * cpus))
8489
8490         echo "before inodes: $before"
8491         for i in $(seq $count); do
8492                 touch $DIR/$tfile
8493                 rm -f $DIR/$tfile
8494         done
8495         cancel_lru_locks osc
8496         local after=$(num_inodes)
8497         echo "after inodes: $after"
8498         while (( after > before + 8 * ${cpus:-1} )); do
8499                 sleep 1
8500                 after=$(num_inodes)
8501                 wait=$((wait + 1))
8502                 (( wait % 5 == 0 )) && echo "wait $wait seconds inodes: $after"
8503                 if (( wait > 30 )); then
8504                         error "inode slab grew from $before to $after"
8505                 fi
8506         done
8507 }
8508 run_test 76 "confirm clients recycle inodes properly ===="
8509
8510
8511 export ORIG_CSUM=""
8512 set_checksums()
8513 {
8514         # Note: in sptlrpc modes which enable its own bulk checksum, the
8515         # original crc32_le bulk checksum will be automatically disabled,
8516         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8517         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8518         # In this case set_checksums() will not be no-op, because sptlrpc
8519         # bulk checksum will be enabled all through the test.
8520
8521         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8522         lctl set_param -n osc.*.checksums $1
8523         return 0
8524 }
8525
8526 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8527                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8528 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8529                              tr -d [] | head -n1)}
8530 set_checksum_type()
8531 {
8532         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8533         rc=$?
8534         log "set checksum type to $1, rc = $rc"
8535         return $rc
8536 }
8537
8538 get_osc_checksum_type()
8539 {
8540         # arugment 1: OST name, like OST0000
8541         ost=$1
8542         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8543                         sed 's/.*\[\(.*\)\].*/\1/g')
8544         rc=$?
8545         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8546         echo $checksum_type
8547 }
8548
8549 F77_TMP=$TMP/f77-temp
8550 F77SZ=8
8551 setup_f77() {
8552         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8553                 error "error writing to $F77_TMP"
8554 }
8555
8556 test_77a() { # bug 10889
8557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8558         $GSS && skip_env "could not run with gss"
8559
8560         [ ! -f $F77_TMP ] && setup_f77
8561         set_checksums 1
8562         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8563         set_checksums 0
8564         rm -f $DIR/$tfile
8565 }
8566 run_test 77a "normal checksum read/write operation"
8567
8568 test_77b() { # bug 10889
8569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8570         $GSS && skip_env "could not run with gss"
8571
8572         [ ! -f $F77_TMP ] && setup_f77
8573         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8574         $LCTL set_param fail_loc=0x80000409
8575         set_checksums 1
8576
8577         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8578                 error "dd error: $?"
8579         $LCTL set_param fail_loc=0
8580
8581         for algo in $CKSUM_TYPES; do
8582                 cancel_lru_locks osc
8583                 set_checksum_type $algo
8584                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8585                 $LCTL set_param fail_loc=0x80000408
8586                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8587                 $LCTL set_param fail_loc=0
8588         done
8589         set_checksums 0
8590         set_checksum_type $ORIG_CSUM_TYPE
8591         rm -f $DIR/$tfile
8592 }
8593 run_test 77b "checksum error on client write, read"
8594
8595 cleanup_77c() {
8596         trap 0
8597         set_checksums 0
8598         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8599         $check_ost &&
8600                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8601         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8602         $check_ost && [ -n "$ost_file_prefix" ] &&
8603                 do_facet ost1 rm -f ${ost_file_prefix}\*
8604 }
8605
8606 test_77c() {
8607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8608         $GSS && skip_env "could not run with gss"
8609         remote_ost_nodsh && skip "remote OST with nodsh"
8610
8611         local bad1
8612         local osc_file_prefix
8613         local osc_file
8614         local check_ost=false
8615         local ost_file_prefix
8616         local ost_file
8617         local orig_cksum
8618         local dump_cksum
8619         local fid
8620
8621         # ensure corruption will occur on first OSS/OST
8622         $LFS setstripe -i 0 $DIR/$tfile
8623
8624         [ ! -f $F77_TMP ] && setup_f77
8625         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8626                 error "dd write error: $?"
8627         fid=$($LFS path2fid $DIR/$tfile)
8628
8629         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8630         then
8631                 check_ost=true
8632                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8633                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8634         else
8635                 echo "OSS do not support bulk pages dump upon error"
8636         fi
8637
8638         osc_file_prefix=$($LCTL get_param -n debug_path)
8639         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8640
8641         trap cleanup_77c EXIT
8642
8643         set_checksums 1
8644         # enable bulk pages dump upon error on Client
8645         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8646         # enable bulk pages dump upon error on OSS
8647         $check_ost &&
8648                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8649
8650         # flush Client cache to allow next read to reach OSS
8651         cancel_lru_locks osc
8652
8653         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8654         $LCTL set_param fail_loc=0x80000408
8655         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8656         $LCTL set_param fail_loc=0
8657
8658         rm -f $DIR/$tfile
8659
8660         # check cksum dump on Client
8661         osc_file=$(ls ${osc_file_prefix}*)
8662         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8663         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8664         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8665         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8666         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8667                      cksum)
8668         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8669         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8670                 error "dump content does not match on Client"
8671
8672         $check_ost || skip "No need to check cksum dump on OSS"
8673
8674         # check cksum dump on OSS
8675         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8676         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8677         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8678         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8679         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8680                 error "dump content does not match on OSS"
8681
8682         cleanup_77c
8683 }
8684 run_test 77c "checksum error on client read with debug"
8685
8686 test_77d() { # bug 10889
8687         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8688         $GSS && skip_env "could not run with gss"
8689
8690         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8691         $LCTL set_param fail_loc=0x80000409
8692         set_checksums 1
8693         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8694                 error "direct write: rc=$?"
8695         $LCTL set_param fail_loc=0
8696         set_checksums 0
8697
8698         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8699         $LCTL set_param fail_loc=0x80000408
8700         set_checksums 1
8701         cancel_lru_locks osc
8702         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8703                 error "direct read: rc=$?"
8704         $LCTL set_param fail_loc=0
8705         set_checksums 0
8706 }
8707 run_test 77d "checksum error on OST direct write, read"
8708
8709 test_77f() { # bug 10889
8710         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8711         $GSS && skip_env "could not run with gss"
8712
8713         set_checksums 1
8714         for algo in $CKSUM_TYPES; do
8715                 cancel_lru_locks osc
8716                 set_checksum_type $algo
8717                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8718                 $LCTL set_param fail_loc=0x409
8719                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8720                         error "direct write succeeded"
8721                 $LCTL set_param fail_loc=0
8722         done
8723         set_checksum_type $ORIG_CSUM_TYPE
8724         set_checksums 0
8725 }
8726 run_test 77f "repeat checksum error on write (expect error)"
8727
8728 test_77g() { # bug 10889
8729         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8730         $GSS && skip_env "could not run with gss"
8731         remote_ost_nodsh && skip "remote OST with nodsh"
8732
8733         [ ! -f $F77_TMP ] && setup_f77
8734
8735         local file=$DIR/$tfile
8736         stack_trap "rm -f $file" EXIT
8737
8738         $LFS setstripe -c 1 -i 0 $file
8739         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8740         do_facet ost1 lctl set_param fail_loc=0x8000021a
8741         set_checksums 1
8742         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8743                 error "write error: rc=$?"
8744         do_facet ost1 lctl set_param fail_loc=0
8745         set_checksums 0
8746
8747         cancel_lru_locks osc
8748         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8749         do_facet ost1 lctl set_param fail_loc=0x8000021b
8750         set_checksums 1
8751         cmp $F77_TMP $file || error "file compare failed"
8752         do_facet ost1 lctl set_param fail_loc=0
8753         set_checksums 0
8754 }
8755 run_test 77g "checksum error on OST write, read"
8756
8757 test_77k() { # LU-10906
8758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8759         $GSS && skip_env "could not run with gss"
8760
8761         local cksum_param="osc.$FSNAME*.checksums"
8762         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8763         local checksum
8764         local i
8765
8766         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8767         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8768         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8769                 EXIT
8770
8771         for i in 0 1; do
8772                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8773                         error "failed to set checksum=$i on MGS"
8774                 wait_update $HOSTNAME "$get_checksum" $i
8775                 #remount
8776                 echo "remount client, checksum should be $i"
8777                 remount_client $MOUNT || error "failed to remount client"
8778                 checksum=$(eval $get_checksum)
8779                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8780         done
8781         # remove persistent param to avoid races with checksum mountopt below
8782         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8783                 error "failed to delete checksum on MGS"
8784
8785         for opt in "checksum" "nochecksum"; do
8786                 #remount with mount option
8787                 echo "remount client with option $opt, checksum should be $i"
8788                 umount_client $MOUNT || error "failed to umount client"
8789                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8790                         error "failed to mount client with option '$opt'"
8791                 checksum=$(eval $get_checksum)
8792                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8793                 i=$((i - 1))
8794         done
8795
8796         remount_client $MOUNT || error "failed to remount client"
8797 }
8798 run_test 77k "enable/disable checksum correctly"
8799
8800 test_77l() {
8801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8802         $GSS && skip_env "could not run with gss"
8803
8804         set_checksums 1
8805         stack_trap "set_checksums $ORIG_CSUM" EXIT
8806         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8807
8808         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8809
8810         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8811         for algo in $CKSUM_TYPES; do
8812                 set_checksum_type $algo || error "fail to set checksum type $algo"
8813                 osc_algo=$(get_osc_checksum_type OST0000)
8814                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8815
8816                 # no locks, no reqs to let the connection idle
8817                 cancel_lru_locks osc
8818                 lru_resize_disable osc
8819                 wait_osc_import_state client ost1 IDLE
8820
8821                 # ensure ost1 is connected
8822                 stat $DIR/$tfile >/dev/null || error "can't stat"
8823                 wait_osc_import_state client ost1 FULL
8824
8825                 osc_algo=$(get_osc_checksum_type OST0000)
8826                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8827         done
8828         return 0
8829 }
8830 run_test 77l "preferred checksum type is remembered after reconnected"
8831
8832 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8833 rm -f $F77_TMP
8834 unset F77_TMP
8835
8836 cleanup_test_78() {
8837         trap 0
8838         rm -f $DIR/$tfile
8839 }
8840
8841 test_78() { # bug 10901
8842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8843         remote_ost || skip_env "local OST"
8844
8845         NSEQ=5
8846         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8847         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8848         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8849         echo "MemTotal: $MEMTOTAL"
8850
8851         # reserve 256MB of memory for the kernel and other running processes,
8852         # and then take 1/2 of the remaining memory for the read/write buffers.
8853         if [ $MEMTOTAL -gt 512 ] ;then
8854                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8855         else
8856                 # for those poor memory-starved high-end clusters...
8857                 MEMTOTAL=$((MEMTOTAL / 2))
8858         fi
8859         echo "Mem to use for directio: $MEMTOTAL"
8860
8861         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8862         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8863         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8864         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8865                 head -n1)
8866         echo "Smallest OST: $SMALLESTOST"
8867         [[ $SMALLESTOST -lt 10240 ]] &&
8868                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8869
8870         trap cleanup_test_78 EXIT
8871
8872         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8873                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8874
8875         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8876         echo "File size: $F78SIZE"
8877         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8878         for i in $(seq 1 $NSEQ); do
8879                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8880                 echo directIO rdwr round $i of $NSEQ
8881                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8882         done
8883
8884         cleanup_test_78
8885 }
8886 run_test 78 "handle large O_DIRECT writes correctly ============"
8887
8888 test_79() { # bug 12743
8889         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8890
8891         wait_delete_completed
8892
8893         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8894         BKFREE=$(calc_osc_kbytes kbytesfree)
8895         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8896
8897         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8898         DFTOTAL=`echo $STRING | cut -d, -f1`
8899         DFUSED=`echo $STRING  | cut -d, -f2`
8900         DFAVAIL=`echo $STRING | cut -d, -f3`
8901         DFFREE=$(($DFTOTAL - $DFUSED))
8902
8903         ALLOWANCE=$((64 * $OSTCOUNT))
8904
8905         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8906            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8907                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8908         fi
8909         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8910            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8911                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8912         fi
8913         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8914            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8915                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8916         fi
8917 }
8918 run_test 79 "df report consistency check ======================="
8919
8920 test_80() { # bug 10718
8921         remote_ost_nodsh && skip "remote OST with nodsh"
8922         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8923
8924         # relax strong synchronous semantics for slow backends like ZFS
8925         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8926                 local soc="obdfilter.*.sync_lock_cancel"
8927                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8928
8929                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8930                 if [ -z "$save" ]; then
8931                         soc="obdfilter.*.sync_on_lock_cancel"
8932                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8933                 fi
8934
8935                 if [ "$save" != "never" ]; then
8936                         local hosts=$(comma_list $(osts_nodes))
8937
8938                         do_nodes $hosts $LCTL set_param $soc=never
8939                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8940                 fi
8941         fi
8942
8943         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8944         sync; sleep 1; sync
8945         local before=$(date +%s)
8946         cancel_lru_locks osc
8947         local after=$(date +%s)
8948         local diff=$((after - before))
8949         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8950
8951         rm -f $DIR/$tfile
8952 }
8953 run_test 80 "Page eviction is equally fast at high offsets too"
8954
8955 test_81a() { # LU-456
8956         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8957         remote_ost_nodsh && skip "remote OST with nodsh"
8958
8959         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8960         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8961         do_facet ost1 lctl set_param fail_loc=0x80000228
8962
8963         # write should trigger a retry and success
8964         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8965         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8966         RC=$?
8967         if [ $RC -ne 0 ] ; then
8968                 error "write should success, but failed for $RC"
8969         fi
8970 }
8971 run_test 81a "OST should retry write when get -ENOSPC ==============="
8972
8973 test_81b() { # LU-456
8974         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8975         remote_ost_nodsh && skip "remote OST with nodsh"
8976
8977         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8978         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
8979         do_facet ost1 lctl set_param fail_loc=0x228
8980
8981         # write should retry several times and return -ENOSPC finally
8982         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8983         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8984         RC=$?
8985         ENOSPC=28
8986         if [ $RC -ne $ENOSPC ] ; then
8987                 error "dd should fail for -ENOSPC, but succeed."
8988         fi
8989 }
8990 run_test 81b "OST should return -ENOSPC when retry still fails ======="
8991
8992 test_82() { # LU-1031
8993         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
8994         local gid1=14091995
8995         local gid2=16022000
8996
8997         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
8998         local MULTIPID1=$!
8999         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
9000         local MULTIPID2=$!
9001         kill -USR1 $MULTIPID2
9002         sleep 2
9003         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
9004                 error "First grouplock does not block second one"
9005         else
9006                 echo "Second grouplock blocks first one"
9007         fi
9008         kill -USR1 $MULTIPID1
9009         wait $MULTIPID1
9010         wait $MULTIPID2
9011 }
9012 run_test 82 "Basic grouplock test"
9013
9014 test_99() {
9015         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
9016
9017         test_mkdir $DIR/$tdir.cvsroot
9018         chown $RUNAS_ID $DIR/$tdir.cvsroot
9019
9020         cd $TMP
9021         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
9022
9023         cd /etc/init.d
9024         # some versions of cvs import exit(1) when asked to import links or
9025         # files they can't read.  ignore those files.
9026         local toignore=$(find . -type l -printf '-I %f\n' -o \
9027                          ! -perm /4 -printf '-I %f\n')
9028         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
9029                 $tdir.reposname vtag rtag
9030
9031         cd $DIR
9032         test_mkdir $DIR/$tdir.reposname
9033         chown $RUNAS_ID $DIR/$tdir.reposname
9034         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
9035
9036         cd $DIR/$tdir.reposname
9037         $RUNAS touch foo99
9038         $RUNAS cvs add -m 'addmsg' foo99
9039         $RUNAS cvs update
9040         $RUNAS cvs commit -m 'nomsg' foo99
9041         rm -fr $DIR/$tdir.cvsroot
9042 }
9043 run_test 99 "cvs strange file/directory operations"
9044
9045 test_100() {
9046         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9047         [[ "$NETTYPE" =~ tcp ]] ||
9048                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
9049         remote_ost_nodsh && skip "remote OST with nodsh"
9050         remote_mds_nodsh && skip "remote MDS with nodsh"
9051         remote_servers ||
9052                 skip "useless for local single node setup"
9053
9054         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
9055                 [ "$PROT" != "tcp" ] && continue
9056                 RPORT=$(echo $REMOTE | cut -d: -f2)
9057                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
9058
9059                 rc=0
9060                 LPORT=`echo $LOCAL | cut -d: -f2`
9061                 if [ $LPORT -ge 1024 ]; then
9062                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
9063                         netstat -tna
9064                         error_exit "local: $LPORT > 1024, remote: $RPORT"
9065                 fi
9066         done
9067         [ "$rc" = 0 ] || error_exit "privileged port not found" )
9068 }
9069 run_test 100 "check local port using privileged port ==========="
9070
9071 function get_named_value()
9072 {
9073     local tag
9074
9075     tag=$1
9076     while read ;do
9077         line=$REPLY
9078         case $line in
9079         $tag*)
9080             echo $line | sed "s/^$tag[ ]*//"
9081             break
9082             ;;
9083         esac
9084     done
9085 }
9086
9087 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
9088                    awk '/^max_cached_mb/ { print $2 }')
9089
9090 cleanup_101a() {
9091         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
9092         trap 0
9093 }
9094
9095 test_101a() {
9096         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9097
9098         local s
9099         local discard
9100         local nreads=10000
9101         local cache_limit=32
9102
9103         $LCTL set_param -n osc.*-osc*.rpc_stats 0
9104         trap cleanup_101a EXIT
9105         $LCTL set_param -n llite.*.read_ahead_stats 0
9106         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
9107
9108         #
9109         # randomly read 10000 of 64K chunks from file 3x 32MB in size
9110         #
9111         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
9112         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
9113
9114         discard=0
9115         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
9116                 get_named_value 'read but discarded' | cut -d" " -f1); do
9117                         discard=$(($discard + $s))
9118         done
9119         cleanup_101a
9120
9121         $LCTL get_param osc.*-osc*.rpc_stats
9122         $LCTL get_param llite.*.read_ahead_stats
9123
9124         # Discard is generally zero, but sometimes a few random reads line up
9125         # and trigger larger readahead, which is wasted & leads to discards.
9126         if [[ $(($discard)) -gt $nreads ]]; then
9127                 error "too many ($discard) discarded pages"
9128         fi
9129         rm -f $DIR/$tfile || true
9130 }
9131 run_test 101a "check read-ahead for random reads"
9132
9133 setup_test101bc() {
9134         test_mkdir $DIR/$tdir
9135         local ssize=$1
9136         local FILE_LENGTH=$2
9137         STRIPE_OFFSET=0
9138
9139         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
9140
9141         local list=$(comma_list $(osts_nodes))
9142         set_osd_param $list '' read_cache_enable 0
9143         set_osd_param $list '' writethrough_cache_enable 0
9144
9145         trap cleanup_test101bc EXIT
9146         # prepare the read-ahead file
9147         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
9148
9149         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
9150                                 count=$FILE_SIZE_MB 2> /dev/null
9151
9152 }
9153
9154 cleanup_test101bc() {
9155         trap 0
9156         rm -rf $DIR/$tdir
9157         rm -f $DIR/$tfile
9158
9159         local list=$(comma_list $(osts_nodes))
9160         set_osd_param $list '' read_cache_enable 1
9161         set_osd_param $list '' writethrough_cache_enable 1
9162 }
9163
9164 calc_total() {
9165         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
9166 }
9167
9168 ra_check_101() {
9169         local READ_SIZE=$1
9170         local STRIPE_SIZE=$2
9171         local FILE_LENGTH=$3
9172         local RA_INC=1048576
9173         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
9174         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
9175                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
9176         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
9177                         get_named_value 'read but discarded' |
9178                         cut -d" " -f1 | calc_total)
9179         if [[ $DISCARD -gt $discard_limit ]]; then
9180                 $LCTL get_param llite.*.read_ahead_stats
9181                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
9182         else
9183                 echo "Read-ahead success for size ${READ_SIZE}"
9184         fi
9185 }
9186
9187 test_101b() {
9188         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9189         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9190
9191         local STRIPE_SIZE=1048576
9192         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
9193
9194         if [ $SLOW == "yes" ]; then
9195                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
9196         else
9197                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
9198         fi
9199
9200         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
9201
9202         # prepare the read-ahead file
9203         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9204         cancel_lru_locks osc
9205         for BIDX in 2 4 8 16 32 64 128 256
9206         do
9207                 local BSIZE=$((BIDX*4096))
9208                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
9209                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
9210                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
9211                 $LCTL set_param -n llite.*.read_ahead_stats 0
9212                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
9213                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
9214                 cancel_lru_locks osc
9215                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
9216         done
9217         cleanup_test101bc
9218         true
9219 }
9220 run_test 101b "check stride-io mode read-ahead ================="
9221
9222 test_101c() {
9223         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9224
9225         local STRIPE_SIZE=1048576
9226         local FILE_LENGTH=$((STRIPE_SIZE*100))
9227         local nreads=10000
9228         local rsize=65536
9229         local osc_rpc_stats
9230
9231         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9232
9233         cancel_lru_locks osc
9234         $LCTL set_param osc.*.rpc_stats 0
9235         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
9236         $LCTL get_param osc.*.rpc_stats
9237         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
9238                 local stats=$($LCTL get_param -n $osc_rpc_stats)
9239                 local lines=$(echo "$stats" | awk 'END {print NR;}')
9240                 local size
9241
9242                 if [ $lines -le 20 ]; then
9243                         echo "continue debug"
9244                         continue
9245                 fi
9246                 for size in 1 2 4 8; do
9247                         local rpc=$(echo "$stats" |
9248                                     awk '($1 == "'$size':") {print $2; exit; }')
9249                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
9250                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
9251                 done
9252                 echo "$osc_rpc_stats check passed!"
9253         done
9254         cleanup_test101bc
9255         true
9256 }
9257 run_test 101c "check stripe_size aligned read-ahead ================="
9258
9259 test_101d() {
9260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9261
9262         local file=$DIR/$tfile
9263         local sz_MB=${FILESIZE_101d:-80}
9264         local ra_MB=${READAHEAD_MB:-40}
9265
9266         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
9267         [ $free_MB -lt $sz_MB ] &&
9268                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
9269
9270         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
9271         $LFS setstripe -c -1 $file || error "setstripe failed"
9272
9273         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
9274         echo Cancel LRU locks on lustre client to flush the client cache
9275         cancel_lru_locks osc
9276
9277         echo Disable read-ahead
9278         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9279         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9280         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_RA" EXIT
9281         $LCTL get_param -n llite.*.max_read_ahead_mb
9282
9283         echo "Reading the test file $file with read-ahead disabled"
9284         local sz_KB=$((sz_MB * 1024 / 4))
9285         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
9286         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
9287         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
9288                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
9289
9290         echo "Cancel LRU locks on lustre client to flush the client cache"
9291         cancel_lru_locks osc
9292         echo Enable read-ahead with ${ra_MB}MB
9293         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
9294
9295         echo "Reading the test file $file with read-ahead enabled"
9296         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
9297                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
9298
9299         echo "read-ahead disabled time read $raOFF"
9300         echo "read-ahead enabled time read $raON"
9301
9302         rm -f $file
9303         wait_delete_completed
9304
9305         # use awk for this check instead of bash because it handles decimals
9306         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
9307                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
9308 }
9309 run_test 101d "file read with and without read-ahead enabled"
9310
9311 test_101e() {
9312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9313
9314         local file=$DIR/$tfile
9315         local size_KB=500  #KB
9316         local count=100
9317         local bsize=1024
9318
9319         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
9320         local need_KB=$((count * size_KB))
9321         [[ $free_KB -le $need_KB ]] &&
9322                 skip_env "Need free space $need_KB, have $free_KB"
9323
9324         echo "Creating $count ${size_KB}K test files"
9325         for ((i = 0; i < $count; i++)); do
9326                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
9327         done
9328
9329         echo "Cancel LRU locks on lustre client to flush the client cache"
9330         cancel_lru_locks $OSC
9331
9332         echo "Reset readahead stats"
9333         $LCTL set_param -n llite.*.read_ahead_stats 0
9334
9335         for ((i = 0; i < $count; i++)); do
9336                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9337         done
9338
9339         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9340                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9341
9342         for ((i = 0; i < $count; i++)); do
9343                 rm -rf $file.$i 2>/dev/null
9344         done
9345
9346         #10000 means 20% reads are missing in readahead
9347         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9348 }
9349 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9350
9351 test_101f() {
9352         which iozone || skip_env "no iozone installed"
9353
9354         local old_debug=$($LCTL get_param debug)
9355         old_debug=${old_debug#*=}
9356         $LCTL set_param debug="reada mmap"
9357
9358         # create a test file
9359         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9360
9361         echo Cancel LRU locks on lustre client to flush the client cache
9362         cancel_lru_locks osc
9363
9364         echo Reset readahead stats
9365         $LCTL set_param -n llite.*.read_ahead_stats 0
9366
9367         echo mmap read the file with small block size
9368         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9369                 > /dev/null 2>&1
9370
9371         echo checking missing pages
9372         $LCTL get_param llite.*.read_ahead_stats
9373         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9374                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9375
9376         $LCTL set_param debug="$old_debug"
9377         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9378         rm -f $DIR/$tfile
9379 }
9380 run_test 101f "check mmap read performance"
9381
9382 test_101g_brw_size_test() {
9383         local mb=$1
9384         local pages=$((mb * 1048576 / PAGE_SIZE))
9385         local file=$DIR/$tfile
9386
9387         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9388                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9389         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9390                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9391                         return 2
9392         done
9393
9394         stack_trap "rm -f $file" EXIT
9395         $LCTL set_param -n osc.*.rpc_stats=0
9396
9397         # 10 RPCs should be enough for the test
9398         local count=10
9399         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9400                 { error "dd write ${mb} MB blocks failed"; return 3; }
9401         cancel_lru_locks osc
9402         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9403                 { error "dd write ${mb} MB blocks failed"; return 4; }
9404
9405         # calculate number of full-sized read and write RPCs
9406         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9407                 sed -n '/pages per rpc/,/^$/p' |
9408                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9409                 END { print reads,writes }'))
9410         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9411                 return 5
9412         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9413                 return 6
9414
9415         return 0
9416 }
9417
9418 test_101g() {
9419         remote_ost_nodsh && skip "remote OST with nodsh"
9420
9421         local rpcs
9422         local osts=$(get_facets OST)
9423         local list=$(comma_list $(osts_nodes))
9424         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9425         local brw_size="obdfilter.*.brw_size"
9426
9427         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9428
9429         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9430
9431         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9432                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9433                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9434            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9435                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9436                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9437
9438                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9439                         suffix="M"
9440
9441                 if [[ $orig_mb -lt 16 ]]; then
9442                         save_lustre_params $osts "$brw_size" > $p
9443                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9444                                 error "set 16MB RPC size failed"
9445
9446                         echo "remount client to enable new RPC size"
9447                         remount_client $MOUNT || error "remount_client failed"
9448                 fi
9449
9450                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9451                 # should be able to set brw_size=12, but no rpc_stats for that
9452                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9453         fi
9454
9455         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9456
9457         if [[ $orig_mb -lt 16 ]]; then
9458                 restore_lustre_params < $p
9459                 remount_client $MOUNT || error "remount_client restore failed"
9460         fi
9461
9462         rm -f $p $DIR/$tfile
9463 }
9464 run_test 101g "Big bulk(4/16 MiB) readahead"
9465
9466 test_101h() {
9467         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9468
9469         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9470                 error "dd 70M file failed"
9471         echo Cancel LRU locks on lustre client to flush the client cache
9472         cancel_lru_locks osc
9473
9474         echo "Reset readahead stats"
9475         $LCTL set_param -n llite.*.read_ahead_stats 0
9476
9477         echo "Read 10M of data but cross 64M bundary"
9478         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9479         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9480                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9481         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9482         rm -f $p $DIR/$tfile
9483 }
9484 run_test 101h "Readahead should cover current read window"
9485
9486 test_101i() {
9487         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
9488                 error "dd 10M file failed"
9489
9490         local max_per_file_mb=$($LCTL get_param -n \
9491                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
9492         cancel_lru_locks osc
9493         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
9494         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
9495                 error "set max_read_ahead_per_file_mb to 1 failed"
9496
9497         echo "Reset readahead stats"
9498         $LCTL set_param llite.*.read_ahead_stats=0
9499
9500         dd if=$DIR/$tfile of=/dev/null bs=2M
9501
9502         $LCTL get_param llite.*.read_ahead_stats
9503         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9504                      awk '/misses/ { print $2 }')
9505         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
9506         rm -f $DIR/$tfile
9507 }
9508 run_test 101i "allow current readahead to exceed reservation"
9509
9510 test_101j() {
9511         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
9512                 error "setstripe $DIR/$tfile failed"
9513         local file_size=$((1048576 * 16))
9514         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9515         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
9516
9517         echo Disable read-ahead
9518         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9519
9520         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
9521         for blk in $PAGE_SIZE 1048576 $file_size; do
9522                 cancel_lru_locks osc
9523                 echo "Reset readahead stats"
9524                 $LCTL set_param -n llite.*.read_ahead_stats=0
9525                 local count=$(($file_size / $blk))
9526                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
9527                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9528                              get_named_value 'failed to fast read' |
9529                              cut -d" " -f1 | calc_total)
9530                 $LCTL get_param -n llite.*.read_ahead_stats
9531                 [ $miss -eq $count ] || error "expected $count got $miss"
9532         done
9533
9534         rm -f $p $DIR/$tfile
9535 }
9536 run_test 101j "A complete read block should be submitted when no RA"
9537
9538 setup_test102() {
9539         test_mkdir $DIR/$tdir
9540         chown $RUNAS_ID $DIR/$tdir
9541         STRIPE_SIZE=65536
9542         STRIPE_OFFSET=1
9543         STRIPE_COUNT=$OSTCOUNT
9544         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9545
9546         trap cleanup_test102 EXIT
9547         cd $DIR
9548         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9549         cd $DIR/$tdir
9550         for num in 1 2 3 4; do
9551                 for count in $(seq 1 $STRIPE_COUNT); do
9552                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9553                                 local size=`expr $STRIPE_SIZE \* $num`
9554                                 local file=file"$num-$idx-$count"
9555                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9556                         done
9557                 done
9558         done
9559
9560         cd $DIR
9561         $1 tar cf $TMP/f102.tar $tdir --xattrs
9562 }
9563
9564 cleanup_test102() {
9565         trap 0
9566         rm -f $TMP/f102.tar
9567         rm -rf $DIR/d0.sanity/d102
9568 }
9569
9570 test_102a() {
9571         [ "$UID" != 0 ] && skip "must run as root"
9572         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9573                 skip_env "must have user_xattr"
9574
9575         [ -z "$(which setfattr 2>/dev/null)" ] &&
9576                 skip_env "could not find setfattr"
9577
9578         local testfile=$DIR/$tfile
9579
9580         touch $testfile
9581         echo "set/get xattr..."
9582         setfattr -n trusted.name1 -v value1 $testfile ||
9583                 error "setfattr -n trusted.name1=value1 $testfile failed"
9584         getfattr -n trusted.name1 $testfile 2> /dev/null |
9585           grep "trusted.name1=.value1" ||
9586                 error "$testfile missing trusted.name1=value1"
9587
9588         setfattr -n user.author1 -v author1 $testfile ||
9589                 error "setfattr -n user.author1=author1 $testfile failed"
9590         getfattr -n user.author1 $testfile 2> /dev/null |
9591           grep "user.author1=.author1" ||
9592                 error "$testfile missing trusted.author1=author1"
9593
9594         echo "listxattr..."
9595         setfattr -n trusted.name2 -v value2 $testfile ||
9596                 error "$testfile unable to set trusted.name2"
9597         setfattr -n trusted.name3 -v value3 $testfile ||
9598                 error "$testfile unable to set trusted.name3"
9599         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9600             grep "trusted.name" | wc -l) -eq 3 ] ||
9601                 error "$testfile missing 3 trusted.name xattrs"
9602
9603         setfattr -n user.author2 -v author2 $testfile ||
9604                 error "$testfile unable to set user.author2"
9605         setfattr -n user.author3 -v author3 $testfile ||
9606                 error "$testfile unable to set user.author3"
9607         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9608             grep "user.author" | wc -l) -eq 3 ] ||
9609                 error "$testfile missing 3 user.author xattrs"
9610
9611         echo "remove xattr..."
9612         setfattr -x trusted.name1 $testfile ||
9613                 error "$testfile error deleting trusted.name1"
9614         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9615                 error "$testfile did not delete trusted.name1 xattr"
9616
9617         setfattr -x user.author1 $testfile ||
9618                 error "$testfile error deleting user.author1"
9619         echo "set lustre special xattr ..."
9620         $LFS setstripe -c1 $testfile
9621         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9622                 awk -F "=" '/trusted.lov/ { print $2 }' )
9623         setfattr -n "trusted.lov" -v $lovea $testfile ||
9624                 error "$testfile doesn't ignore setting trusted.lov again"
9625         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9626                 error "$testfile allow setting invalid trusted.lov"
9627         rm -f $testfile
9628 }
9629 run_test 102a "user xattr test =================================="
9630
9631 check_102b_layout() {
9632         local layout="$*"
9633         local testfile=$DIR/$tfile
9634
9635         echo "test layout '$layout'"
9636         $LFS setstripe $layout $testfile || error "setstripe failed"
9637         $LFS getstripe -y $testfile
9638
9639         echo "get/set/list trusted.lov xattr ..." # b=10930
9640         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
9641         [[ "$value" =~ "trusted.lov" ]] ||
9642                 error "can't get trusted.lov from $testfile"
9643         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
9644                 error "getstripe failed"
9645
9646         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
9647
9648         value=$(cut -d= -f2 <<<$value)
9649         # LU-13168: truncated xattr should fail if short lov_user_md header
9650         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
9651                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
9652         for len in $lens; do
9653                 echo "setfattr $len $testfile.2"
9654                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
9655                         [ $len -lt 66 ] && error "short xattr len=$len worked"
9656         done
9657         local stripe_size=$($LFS getstripe -S $testfile.2)
9658         local stripe_count=$($LFS getstripe -c $testfile.2)
9659         [[ $stripe_size -eq 65536 ]] ||
9660                 error "stripe size $stripe_size != 65536"
9661         [[ $stripe_count -eq $stripe_count_orig ]] ||
9662                 error "stripe count $stripe_count != $stripe_count_orig"
9663         rm $testfile $testfile.2
9664 }
9665
9666 test_102b() {
9667         [ -z "$(which setfattr 2>/dev/null)" ] &&
9668                 skip_env "could not find setfattr"
9669         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9670
9671         # check plain layout
9672         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
9673
9674         # and also check composite layout
9675         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
9676
9677 }
9678 run_test 102b "getfattr/setfattr for trusted.lov EAs"
9679
9680 test_102c() {
9681         [ -z "$(which setfattr 2>/dev/null)" ] &&
9682                 skip_env "could not find setfattr"
9683         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9684
9685         # b10930: get/set/list lustre.lov xattr
9686         echo "get/set/list lustre.lov xattr ..."
9687         test_mkdir $DIR/$tdir
9688         chown $RUNAS_ID $DIR/$tdir
9689         local testfile=$DIR/$tdir/$tfile
9690         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9691                 error "setstripe failed"
9692         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9693                 error "getstripe failed"
9694         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9695         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9696
9697         local testfile2=${testfile}2
9698         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9699                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9700
9701         $RUNAS $MCREATE $testfile2
9702         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9703         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9704         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9705         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9706         [ $stripe_count -eq $STRIPECOUNT ] ||
9707                 error "stripe count $stripe_count != $STRIPECOUNT"
9708 }
9709 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9710
9711 compare_stripe_info1() {
9712         local stripe_index_all_zero=true
9713
9714         for num in 1 2 3 4; do
9715                 for count in $(seq 1 $STRIPE_COUNT); do
9716                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9717                                 local size=$((STRIPE_SIZE * num))
9718                                 local file=file"$num-$offset-$count"
9719                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9720                                 [[ $stripe_size -ne $size ]] &&
9721                                     error "$file: size $stripe_size != $size"
9722                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9723                                 # allow fewer stripes to be created, ORI-601
9724                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9725                                     error "$file: count $stripe_count != $count"
9726                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9727                                 [[ $stripe_index -ne 0 ]] &&
9728                                         stripe_index_all_zero=false
9729                         done
9730                 done
9731         done
9732         $stripe_index_all_zero &&
9733                 error "all files are being extracted starting from OST index 0"
9734         return 0
9735 }
9736
9737 have_xattrs_include() {
9738         tar --help | grep -q xattrs-include &&
9739                 echo --xattrs-include="lustre.*"
9740 }
9741
9742 test_102d() {
9743         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9744         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9745
9746         XINC=$(have_xattrs_include)
9747         setup_test102
9748         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9749         cd $DIR/$tdir/$tdir
9750         compare_stripe_info1
9751 }
9752 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9753
9754 test_102f() {
9755         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9756         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9757
9758         XINC=$(have_xattrs_include)
9759         setup_test102
9760         test_mkdir $DIR/$tdir.restore
9761         cd $DIR
9762         tar cf - --xattrs $tdir | tar xf - \
9763                 -C $DIR/$tdir.restore --xattrs $XINC
9764         cd $DIR/$tdir.restore/$tdir
9765         compare_stripe_info1
9766 }
9767 run_test 102f "tar copy files, not keep osts"
9768
9769 grow_xattr() {
9770         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9771                 skip "must have user_xattr"
9772         [ -z "$(which setfattr 2>/dev/null)" ] &&
9773                 skip_env "could not find setfattr"
9774         [ -z "$(which getfattr 2>/dev/null)" ] &&
9775                 skip_env "could not find getfattr"
9776
9777         local xsize=${1:-1024}  # in bytes
9778         local file=$DIR/$tfile
9779         local value="$(generate_string $xsize)"
9780         local xbig=trusted.big
9781         local toobig=$2
9782
9783         touch $file
9784         log "save $xbig on $file"
9785         if [ -z "$toobig" ]
9786         then
9787                 setfattr -n $xbig -v $value $file ||
9788                         error "saving $xbig on $file failed"
9789         else
9790                 setfattr -n $xbig -v $value $file &&
9791                         error "saving $xbig on $file succeeded"
9792                 return 0
9793         fi
9794
9795         local orig=$(get_xattr_value $xbig $file)
9796         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9797
9798         local xsml=trusted.sml
9799         log "save $xsml on $file"
9800         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9801
9802         local new=$(get_xattr_value $xbig $file)
9803         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9804
9805         log "grow $xsml on $file"
9806         setfattr -n $xsml -v "$value" $file ||
9807                 error "growing $xsml on $file failed"
9808
9809         new=$(get_xattr_value $xbig $file)
9810         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9811         log "$xbig still valid after growing $xsml"
9812
9813         rm -f $file
9814 }
9815
9816 test_102h() { # bug 15777
9817         grow_xattr 1024
9818 }
9819 run_test 102h "grow xattr from inside inode to external block"
9820
9821 test_102ha() {
9822         large_xattr_enabled || skip_env "ea_inode feature disabled"
9823
9824         echo "setting xattr of max xattr size: $(max_xattr_size)"
9825         grow_xattr $(max_xattr_size)
9826
9827         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9828         echo "This should fail:"
9829         grow_xattr $(($(max_xattr_size) + 10)) 1
9830 }
9831 run_test 102ha "grow xattr from inside inode to external inode"
9832
9833 test_102i() { # bug 17038
9834         [ -z "$(which getfattr 2>/dev/null)" ] &&
9835                 skip "could not find getfattr"
9836
9837         touch $DIR/$tfile
9838         ln -s $DIR/$tfile $DIR/${tfile}link
9839         getfattr -n trusted.lov $DIR/$tfile ||
9840                 error "lgetxattr on $DIR/$tfile failed"
9841         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9842                 grep -i "no such attr" ||
9843                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9844         rm -f $DIR/$tfile $DIR/${tfile}link
9845 }
9846 run_test 102i "lgetxattr test on symbolic link ============"
9847
9848 test_102j() {
9849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9850         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9851
9852         XINC=$(have_xattrs_include)
9853         setup_test102 "$RUNAS"
9854         chown $RUNAS_ID $DIR/$tdir
9855         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9856         cd $DIR/$tdir/$tdir
9857         compare_stripe_info1 "$RUNAS"
9858 }
9859 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9860
9861 test_102k() {
9862         [ -z "$(which setfattr 2>/dev/null)" ] &&
9863                 skip "could not find setfattr"
9864
9865         touch $DIR/$tfile
9866         # b22187 just check that does not crash for regular file.
9867         setfattr -n trusted.lov $DIR/$tfile
9868         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9869         local test_kdir=$DIR/$tdir
9870         test_mkdir $test_kdir
9871         local default_size=$($LFS getstripe -S $test_kdir)
9872         local default_count=$($LFS getstripe -c $test_kdir)
9873         local default_offset=$($LFS getstripe -i $test_kdir)
9874         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9875                 error 'dir setstripe failed'
9876         setfattr -n trusted.lov $test_kdir
9877         local stripe_size=$($LFS getstripe -S $test_kdir)
9878         local stripe_count=$($LFS getstripe -c $test_kdir)
9879         local stripe_offset=$($LFS getstripe -i $test_kdir)
9880         [ $stripe_size -eq $default_size ] ||
9881                 error "stripe size $stripe_size != $default_size"
9882         [ $stripe_count -eq $default_count ] ||
9883                 error "stripe count $stripe_count != $default_count"
9884         [ $stripe_offset -eq $default_offset ] ||
9885                 error "stripe offset $stripe_offset != $default_offset"
9886         rm -rf $DIR/$tfile $test_kdir
9887 }
9888 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9889
9890 test_102l() {
9891         [ -z "$(which getfattr 2>/dev/null)" ] &&
9892                 skip "could not find getfattr"
9893
9894         # LU-532 trusted. xattr is invisible to non-root
9895         local testfile=$DIR/$tfile
9896
9897         touch $testfile
9898
9899         echo "listxattr as user..."
9900         chown $RUNAS_ID $testfile
9901         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9902             grep -q "trusted" &&
9903                 error "$testfile trusted xattrs are user visible"
9904
9905         return 0;
9906 }
9907 run_test 102l "listxattr size test =================================="
9908
9909 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9910         local path=$DIR/$tfile
9911         touch $path
9912
9913         listxattr_size_check $path || error "listattr_size_check $path failed"
9914 }
9915 run_test 102m "Ensure listxattr fails on small bufffer ========"
9916
9917 cleanup_test102
9918
9919 getxattr() { # getxattr path name
9920         # Return the base64 encoding of the value of xattr name on path.
9921         local path=$1
9922         local name=$2
9923
9924         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9925         # file: $path
9926         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9927         #
9928         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9929
9930         getfattr --absolute-names --encoding=base64 --name=$name $path |
9931                 awk -F= -v name=$name '$1 == name {
9932                         print substr($0, index($0, "=") + 1);
9933         }'
9934 }
9935
9936 test_102n() { # LU-4101 mdt: protect internal xattrs
9937         [ -z "$(which setfattr 2>/dev/null)" ] &&
9938                 skip "could not find setfattr"
9939         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9940         then
9941                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9942         fi
9943
9944         local file0=$DIR/$tfile.0
9945         local file1=$DIR/$tfile.1
9946         local xattr0=$TMP/$tfile.0
9947         local xattr1=$TMP/$tfile.1
9948         local namelist="lov lma lmv link fid version som hsm"
9949         local name
9950         local value
9951
9952         rm -rf $file0 $file1 $xattr0 $xattr1
9953         touch $file0 $file1
9954
9955         # Get 'before' xattrs of $file1.
9956         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9957
9958         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9959                 namelist+=" lfsck_namespace"
9960         for name in $namelist; do
9961                 # Try to copy xattr from $file0 to $file1.
9962                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9963
9964                 setfattr --name=trusted.$name --value="$value" $file1 ||
9965                         error "setxattr 'trusted.$name' failed"
9966
9967                 # Try to set a garbage xattr.
9968                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9969
9970                 if [[ x$name == "xlov" ]]; then
9971                         setfattr --name=trusted.lov --value="$value" $file1 &&
9972                         error "setxattr invalid 'trusted.lov' success"
9973                 else
9974                         setfattr --name=trusted.$name --value="$value" $file1 ||
9975                                 error "setxattr invalid 'trusted.$name' failed"
9976                 fi
9977
9978                 # Try to remove the xattr from $file1. We don't care if this
9979                 # appears to succeed or fail, we just don't want there to be
9980                 # any changes or crashes.
9981                 setfattr --remove=$trusted.$name $file1 2> /dev/null
9982         done
9983
9984         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
9985         then
9986                 name="lfsck_ns"
9987                 # Try to copy xattr from $file0 to $file1.
9988                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9989
9990                 setfattr --name=trusted.$name --value="$value" $file1 ||
9991                         error "setxattr 'trusted.$name' failed"
9992
9993                 # Try to set a garbage xattr.
9994                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9995
9996                 setfattr --name=trusted.$name --value="$value" $file1 ||
9997                         error "setxattr 'trusted.$name' failed"
9998
9999                 # Try to remove the xattr from $file1. We don't care if this
10000                 # appears to succeed or fail, we just don't want there to be
10001                 # any changes or crashes.
10002                 setfattr --remove=$trusted.$name $file1 2> /dev/null
10003         fi
10004
10005         # Get 'after' xattrs of file1.
10006         getfattr --absolute-names --dump --match=- $file1 > $xattr1
10007
10008         if ! diff $xattr0 $xattr1; then
10009                 error "before and after xattrs of '$file1' differ"
10010         fi
10011
10012         rm -rf $file0 $file1 $xattr0 $xattr1
10013
10014         return 0
10015 }
10016 run_test 102n "silently ignore setxattr on internal trusted xattrs"
10017
10018 test_102p() { # LU-4703 setxattr did not check ownership
10019         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
10020                 skip "MDS needs to be at least 2.5.56"
10021
10022         local testfile=$DIR/$tfile
10023
10024         touch $testfile
10025
10026         echo "setfacl as user..."
10027         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
10028         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
10029
10030         echo "setfattr as user..."
10031         setfacl -m "u:$RUNAS_ID:---" $testfile
10032         $RUNAS setfattr -x system.posix_acl_access $testfile
10033         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
10034 }
10035 run_test 102p "check setxattr(2) correctly fails without permission"
10036
10037 test_102q() {
10038         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
10039                 skip "MDS needs to be at least 2.6.92"
10040
10041         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
10042 }
10043 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
10044
10045 test_102r() {
10046         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
10047                 skip "MDS needs to be at least 2.6.93"
10048
10049         touch $DIR/$tfile || error "touch"
10050         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
10051         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
10052         rm $DIR/$tfile || error "rm"
10053
10054         #normal directory
10055         mkdir -p $DIR/$tdir || error "mkdir"
10056         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10057         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10058         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10059                 error "$testfile error deleting user.author1"
10060         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10061                 grep "user.$(basename $tdir)" &&
10062                 error "$tdir did not delete user.$(basename $tdir)"
10063         rmdir $DIR/$tdir || error "rmdir"
10064
10065         #striped directory
10066         test_mkdir $DIR/$tdir
10067         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10068         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10069         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10070                 error "$testfile error deleting user.author1"
10071         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10072                 grep "user.$(basename $tdir)" &&
10073                 error "$tdir did not delete user.$(basename $tdir)"
10074         rmdir $DIR/$tdir || error "rm striped dir"
10075 }
10076 run_test 102r "set EAs with empty values"
10077
10078 test_102s() {
10079         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10080                 skip "MDS needs to be at least 2.11.52"
10081
10082         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10083
10084         save_lustre_params client "llite.*.xattr_cache" > $save
10085
10086         for cache in 0 1; do
10087                 lctl set_param llite.*.xattr_cache=$cache
10088
10089                 rm -f $DIR/$tfile
10090                 touch $DIR/$tfile || error "touch"
10091                 for prefix in lustre security system trusted user; do
10092                         # Note getxattr() may fail with 'Operation not
10093                         # supported' or 'No such attribute' depending
10094                         # on prefix and cache.
10095                         getfattr -n $prefix.n102s $DIR/$tfile &&
10096                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
10097                 done
10098         done
10099
10100         restore_lustre_params < $save
10101 }
10102 run_test 102s "getting nonexistent xattrs should fail"
10103
10104 test_102t() {
10105         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10106                 skip "MDS needs to be at least 2.11.52"
10107
10108         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10109
10110         save_lustre_params client "llite.*.xattr_cache" > $save
10111
10112         for cache in 0 1; do
10113                 lctl set_param llite.*.xattr_cache=$cache
10114
10115                 for buf_size in 0 256; do
10116                         rm -f $DIR/$tfile
10117                         touch $DIR/$tfile || error "touch"
10118                         setfattr -n user.multiop $DIR/$tfile
10119                         $MULTIOP $DIR/$tfile oa$buf_size ||
10120                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
10121                 done
10122         done
10123
10124         restore_lustre_params < $save
10125 }
10126 run_test 102t "zero length xattr values handled correctly"
10127
10128 run_acl_subtest()
10129 {
10130     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
10131     return $?
10132 }
10133
10134 test_103a() {
10135         [ "$UID" != 0 ] && skip "must run as root"
10136         $GSS && skip_env "could not run under gss"
10137         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
10138                 skip_env "must have acl enabled"
10139         [ -z "$(which setfacl 2>/dev/null)" ] &&
10140                 skip_env "could not find setfacl"
10141         remote_mds_nodsh && skip "remote MDS with nodsh"
10142
10143         gpasswd -a daemon bin                           # LU-5641
10144         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
10145
10146         declare -a identity_old
10147
10148         for num in $(seq $MDSCOUNT); do
10149                 switch_identity $num true || identity_old[$num]=$?
10150         done
10151
10152         SAVE_UMASK=$(umask)
10153         umask 0022
10154         mkdir -p $DIR/$tdir
10155         cd $DIR/$tdir
10156
10157         echo "performing cp ..."
10158         run_acl_subtest cp || error "run_acl_subtest cp failed"
10159         echo "performing getfacl-noacl..."
10160         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
10161         echo "performing misc..."
10162         run_acl_subtest misc || error  "misc test failed"
10163         echo "performing permissions..."
10164         run_acl_subtest permissions || error "permissions failed"
10165         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
10166         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
10167                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
10168                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
10169         then
10170                 echo "performing permissions xattr..."
10171                 run_acl_subtest permissions_xattr ||
10172                         error "permissions_xattr failed"
10173         fi
10174         echo "performing setfacl..."
10175         run_acl_subtest setfacl || error  "setfacl test failed"
10176
10177         # inheritance test got from HP
10178         echo "performing inheritance..."
10179         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
10180         chmod +x make-tree || error "chmod +x failed"
10181         run_acl_subtest inheritance || error "inheritance test failed"
10182         rm -f make-tree
10183
10184         echo "LU-974 ignore umask when acl is enabled..."
10185         run_acl_subtest 974 || error "LU-974 umask test failed"
10186         if [ $MDSCOUNT -ge 2 ]; then
10187                 run_acl_subtest 974_remote ||
10188                         error "LU-974 umask test failed under remote dir"
10189         fi
10190
10191         echo "LU-2561 newly created file is same size as directory..."
10192         if [ "$mds1_FSTYPE" != "zfs" ]; then
10193                 run_acl_subtest 2561 || error "LU-2561 test failed"
10194         else
10195                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
10196         fi
10197
10198         run_acl_subtest 4924 || error "LU-4924 test failed"
10199
10200         cd $SAVE_PWD
10201         umask $SAVE_UMASK
10202
10203         for num in $(seq $MDSCOUNT); do
10204                 if [ "${identity_old[$num]}" = 1 ]; then
10205                         switch_identity $num false || identity_old[$num]=$?
10206                 fi
10207         done
10208 }
10209 run_test 103a "acl test"
10210
10211 test_103b() {
10212         declare -a pids
10213         local U
10214
10215         for U in {0..511}; do
10216                 {
10217                 local O=$(printf "%04o" $U)
10218
10219                 umask $(printf "%04o" $((511 ^ $O)))
10220                 $LFS setstripe -c 1 $DIR/$tfile.s$O
10221                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
10222
10223                 (( $S == ($O & 0666) )) ||
10224                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
10225
10226                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
10227                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
10228                 (( $S == ($O & 0666) )) ||
10229                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
10230
10231                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
10232                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
10233                 (( $S == ($O & 0666) )) ||
10234                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
10235                 rm -f $DIR/$tfile.[smp]$0
10236                 } &
10237                 local pid=$!
10238
10239                 # limit the concurrently running threads to 64. LU-11878
10240                 local idx=$((U % 64))
10241                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
10242                 pids[idx]=$pid
10243         done
10244         wait
10245 }
10246 run_test 103b "umask lfs setstripe"
10247
10248 test_103c() {
10249         mkdir -p $DIR/$tdir
10250         cp -rp $DIR/$tdir $DIR/$tdir.bak
10251
10252         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
10253                 error "$DIR/$tdir shouldn't contain default ACL"
10254         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
10255                 error "$DIR/$tdir.bak shouldn't contain default ACL"
10256         true
10257 }
10258 run_test 103c "'cp -rp' won't set empty acl"
10259
10260 test_104a() {
10261         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10262
10263         touch $DIR/$tfile
10264         lfs df || error "lfs df failed"
10265         lfs df -ih || error "lfs df -ih failed"
10266         lfs df -h $DIR || error "lfs df -h $DIR failed"
10267         lfs df -i $DIR || error "lfs df -i $DIR failed"
10268         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
10269         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
10270
10271         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
10272         lctl --device %$OSC deactivate
10273         lfs df || error "lfs df with deactivated OSC failed"
10274         lctl --device %$OSC activate
10275         # wait the osc back to normal
10276         wait_osc_import_ready client ost
10277
10278         lfs df || error "lfs df with reactivated OSC failed"
10279         rm -f $DIR/$tfile
10280 }
10281 run_test 104a "lfs df [-ih] [path] test ========================="
10282
10283 test_104b() {
10284         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10285         [ $RUNAS_ID -eq $UID ] &&
10286                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10287
10288         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
10289                         grep "Permission denied" | wc -l)))
10290         if [ $denied_cnt -ne 0 ]; then
10291                 error "lfs check servers test failed"
10292         fi
10293 }
10294 run_test 104b "$RUNAS lfs check servers test ===================="
10295
10296 test_105a() {
10297         # doesn't work on 2.4 kernels
10298         touch $DIR/$tfile
10299         if $(flock_is_enabled); then
10300                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
10301         else
10302                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
10303         fi
10304         rm -f $DIR/$tfile
10305 }
10306 run_test 105a "flock when mounted without -o flock test ========"
10307
10308 test_105b() {
10309         touch $DIR/$tfile
10310         if $(flock_is_enabled); then
10311                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
10312         else
10313                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
10314         fi
10315         rm -f $DIR/$tfile
10316 }
10317 run_test 105b "fcntl when mounted without -o flock test ========"
10318
10319 test_105c() {
10320         touch $DIR/$tfile
10321         if $(flock_is_enabled); then
10322                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
10323         else
10324                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
10325         fi
10326         rm -f $DIR/$tfile
10327 }
10328 run_test 105c "lockf when mounted without -o flock test"
10329
10330 test_105d() { # bug 15924
10331         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10332
10333         test_mkdir $DIR/$tdir
10334         flock_is_enabled || skip_env "mount w/o flock enabled"
10335         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
10336         $LCTL set_param fail_loc=0x80000315
10337         flocks_test 2 $DIR/$tdir
10338 }
10339 run_test 105d "flock race (should not freeze) ========"
10340
10341 test_105e() { # bug 22660 && 22040
10342         flock_is_enabled || skip_env "mount w/o flock enabled"
10343
10344         touch $DIR/$tfile
10345         flocks_test 3 $DIR/$tfile
10346 }
10347 run_test 105e "Two conflicting flocks from same process"
10348
10349 test_106() { #bug 10921
10350         test_mkdir $DIR/$tdir
10351         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
10352         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
10353 }
10354 run_test 106 "attempt exec of dir followed by chown of that dir"
10355
10356 test_107() {
10357         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10358
10359         CDIR=`pwd`
10360         local file=core
10361
10362         cd $DIR
10363         rm -f $file
10364
10365         local save_pattern=$(sysctl -n kernel.core_pattern)
10366         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
10367         sysctl -w kernel.core_pattern=$file
10368         sysctl -w kernel.core_uses_pid=0
10369
10370         ulimit -c unlimited
10371         sleep 60 &
10372         SLEEPPID=$!
10373
10374         sleep 1
10375
10376         kill -s 11 $SLEEPPID
10377         wait $SLEEPPID
10378         if [ -e $file ]; then
10379                 size=`stat -c%s $file`
10380                 [ $size -eq 0 ] && error "Fail to create core file $file"
10381         else
10382                 error "Fail to create core file $file"
10383         fi
10384         rm -f $file
10385         sysctl -w kernel.core_pattern=$save_pattern
10386         sysctl -w kernel.core_uses_pid=$save_uses_pid
10387         cd $CDIR
10388 }
10389 run_test 107 "Coredump on SIG"
10390
10391 test_110() {
10392         test_mkdir $DIR/$tdir
10393         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
10394         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
10395                 error "mkdir with 256 char should fail, but did not"
10396         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
10397                 error "create with 255 char failed"
10398         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
10399                 error "create with 256 char should fail, but did not"
10400
10401         ls -l $DIR/$tdir
10402         rm -rf $DIR/$tdir
10403 }
10404 run_test 110 "filename length checking"
10405
10406 #
10407 # Purpose: To verify dynamic thread (OSS) creation.
10408 #
10409 test_115() {
10410         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10411         remote_ost_nodsh && skip "remote OST with nodsh"
10412
10413         # Lustre does not stop service threads once they are started.
10414         # Reset number of running threads to default.
10415         stopall
10416         setupall
10417
10418         local OSTIO_pre
10419         local save_params="$TMP/sanity-$TESTNAME.parameters"
10420
10421         # Get ll_ost_io count before I/O
10422         OSTIO_pre=$(do_facet ost1 \
10423                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10424         # Exit if lustre is not running (ll_ost_io not running).
10425         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10426
10427         echo "Starting with $OSTIO_pre threads"
10428         local thread_max=$((OSTIO_pre * 2))
10429         local rpc_in_flight=$((thread_max * 2))
10430         # Number of I/O Process proposed to be started.
10431         local nfiles
10432         local facets=$(get_facets OST)
10433
10434         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10435         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10436
10437         # Set in_flight to $rpc_in_flight
10438         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10439                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10440         nfiles=${rpc_in_flight}
10441         # Set ost thread_max to $thread_max
10442         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10443
10444         # 5 Minutes should be sufficient for max number of OSS
10445         # threads(thread_max) to be created.
10446         local timeout=300
10447
10448         # Start I/O.
10449         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10450         test_mkdir $DIR/$tdir
10451         for i in $(seq $nfiles); do
10452                 local file=$DIR/$tdir/${tfile}-$i
10453                 $LFS setstripe -c -1 -i 0 $file
10454                 ($WTL $file $timeout)&
10455         done
10456
10457         # I/O Started - Wait for thread_started to reach thread_max or report
10458         # error if thread_started is more than thread_max.
10459         echo "Waiting for thread_started to reach thread_max"
10460         local thread_started=0
10461         local end_time=$((SECONDS + timeout))
10462
10463         while [ $SECONDS -le $end_time ] ; do
10464                 echo -n "."
10465                 # Get ost i/o thread_started count.
10466                 thread_started=$(do_facet ost1 \
10467                         "$LCTL get_param \
10468                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10469                 # Break out if thread_started is equal/greater than thread_max
10470                 if [[ $thread_started -ge $thread_max ]]; then
10471                         echo ll_ost_io thread_started $thread_started, \
10472                                 equal/greater than thread_max $thread_max
10473                         break
10474                 fi
10475                 sleep 1
10476         done
10477
10478         # Cleanup - We have the numbers, Kill i/o jobs if running.
10479         jobcount=($(jobs -p))
10480         for i in $(seq 0 $((${#jobcount[@]}-1)))
10481         do
10482                 kill -9 ${jobcount[$i]}
10483                 if [ $? -ne 0 ] ; then
10484                         echo Warning: \
10485                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10486                 fi
10487         done
10488
10489         # Cleanup files left by WTL binary.
10490         for i in $(seq $nfiles); do
10491                 local file=$DIR/$tdir/${tfile}-$i
10492                 rm -rf $file
10493                 if [ $? -ne 0 ] ; then
10494                         echo "Warning: Failed to delete file $file"
10495                 fi
10496         done
10497
10498         restore_lustre_params <$save_params
10499         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10500
10501         # Error out if no new thread has started or Thread started is greater
10502         # than thread max.
10503         if [[ $thread_started -le $OSTIO_pre ||
10504                         $thread_started -gt $thread_max ]]; then
10505                 error "ll_ost_io: thread_started $thread_started" \
10506                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10507                       "No new thread started or thread started greater " \
10508                       "than thread_max."
10509         fi
10510 }
10511 run_test 115 "verify dynamic thread creation===================="
10512
10513 free_min_max () {
10514         wait_delete_completed
10515         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10516         echo "OST kbytes available: ${AVAIL[@]}"
10517         MAXV=${AVAIL[0]}
10518         MAXI=0
10519         MINV=${AVAIL[0]}
10520         MINI=0
10521         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10522                 #echo OST $i: ${AVAIL[i]}kb
10523                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10524                         MAXV=${AVAIL[i]}
10525                         MAXI=$i
10526                 fi
10527                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10528                         MINV=${AVAIL[i]}
10529                         MINI=$i
10530                 fi
10531         done
10532         echo "Min free space: OST $MINI: $MINV"
10533         echo "Max free space: OST $MAXI: $MAXV"
10534 }
10535
10536 test_116a() { # was previously test_116()
10537         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10538         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10539         remote_mds_nodsh && skip "remote MDS with nodsh"
10540
10541         echo -n "Free space priority "
10542         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10543                 head -n1
10544         declare -a AVAIL
10545         free_min_max
10546
10547         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10548         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10549         trap simple_cleanup_common EXIT
10550
10551         # Check if we need to generate uneven OSTs
10552         test_mkdir -p $DIR/$tdir/OST${MINI}
10553         local FILL=$((MINV / 4))
10554         local DIFF=$((MAXV - MINV))
10555         local DIFF2=$((DIFF * 100 / MINV))
10556
10557         local threshold=$(do_facet $SINGLEMDS \
10558                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10559         threshold=${threshold%%%}
10560         echo -n "Check for uneven OSTs: "
10561         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10562
10563         if [[ $DIFF2 -gt $threshold ]]; then
10564                 echo "ok"
10565                 echo "Don't need to fill OST$MINI"
10566         else
10567                 # generate uneven OSTs. Write 2% over the QOS threshold value
10568                 echo "no"
10569                 DIFF=$((threshold - DIFF2 + 2))
10570                 DIFF2=$((MINV * DIFF / 100))
10571                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10572                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10573                         error "setstripe failed"
10574                 DIFF=$((DIFF2 / 2048))
10575                 i=0
10576                 while [ $i -lt $DIFF ]; do
10577                         i=$((i + 1))
10578                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10579                                 bs=2M count=1 2>/dev/null
10580                         echo -n .
10581                 done
10582                 echo .
10583                 sync
10584                 sleep_maxage
10585                 free_min_max
10586         fi
10587
10588         DIFF=$((MAXV - MINV))
10589         DIFF2=$((DIFF * 100 / MINV))
10590         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10591         if [ $DIFF2 -gt $threshold ]; then
10592                 echo "ok"
10593         else
10594                 echo "failed - QOS mode won't be used"
10595                 simple_cleanup_common
10596                 skip "QOS imbalance criteria not met"
10597         fi
10598
10599         MINI1=$MINI
10600         MINV1=$MINV
10601         MAXI1=$MAXI
10602         MAXV1=$MAXV
10603
10604         # now fill using QOS
10605         $LFS setstripe -c 1 $DIR/$tdir
10606         FILL=$((FILL / 200))
10607         if [ $FILL -gt 600 ]; then
10608                 FILL=600
10609         fi
10610         echo "writing $FILL files to QOS-assigned OSTs"
10611         i=0
10612         while [ $i -lt $FILL ]; do
10613                 i=$((i + 1))
10614                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10615                         count=1 2>/dev/null
10616                 echo -n .
10617         done
10618         echo "wrote $i 200k files"
10619         sync
10620         sleep_maxage
10621
10622         echo "Note: free space may not be updated, so measurements might be off"
10623         free_min_max
10624         DIFF2=$((MAXV - MINV))
10625         echo "free space delta: orig $DIFF final $DIFF2"
10626         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10627         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10628         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10629         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10630         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10631         if [[ $DIFF -gt 0 ]]; then
10632                 FILL=$((DIFF2 * 100 / DIFF - 100))
10633                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10634         fi
10635
10636         # Figure out which files were written where
10637         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10638                awk '/'$MINI1': / {print $2; exit}')
10639         echo $UUID
10640         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10641         echo "$MINC files created on smaller OST $MINI1"
10642         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10643                awk '/'$MAXI1': / {print $2; exit}')
10644         echo $UUID
10645         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10646         echo "$MAXC files created on larger OST $MAXI1"
10647         if [[ $MINC -gt 0 ]]; then
10648                 FILL=$((MAXC * 100 / MINC - 100))
10649                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10650         fi
10651         [[ $MAXC -gt $MINC ]] ||
10652                 error_ignore LU-9 "stripe QOS didn't balance free space"
10653         simple_cleanup_common
10654 }
10655 run_test 116a "stripe QOS: free space balance ==================="
10656
10657 test_116b() { # LU-2093
10658         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10659         remote_mds_nodsh && skip "remote MDS with nodsh"
10660
10661 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10662         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10663                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10664         [ -z "$old_rr" ] && skip "no QOS"
10665         do_facet $SINGLEMDS lctl set_param \
10666                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10667         mkdir -p $DIR/$tdir
10668         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10669         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10670         do_facet $SINGLEMDS lctl set_param fail_loc=0
10671         rm -rf $DIR/$tdir
10672         do_facet $SINGLEMDS lctl set_param \
10673                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10674 }
10675 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10676
10677 test_117() # bug 10891
10678 {
10679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10680
10681         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10682         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10683         lctl set_param fail_loc=0x21e
10684         > $DIR/$tfile || error "truncate failed"
10685         lctl set_param fail_loc=0
10686         echo "Truncate succeeded."
10687         rm -f $DIR/$tfile
10688 }
10689 run_test 117 "verify osd extend =========="
10690
10691 NO_SLOW_RESENDCOUNT=4
10692 export OLD_RESENDCOUNT=""
10693 set_resend_count () {
10694         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10695         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10696         lctl set_param -n $PROC_RESENDCOUNT $1
10697         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10698 }
10699
10700 # for reduce test_118* time (b=14842)
10701 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10702
10703 # Reset async IO behavior after error case
10704 reset_async() {
10705         FILE=$DIR/reset_async
10706
10707         # Ensure all OSCs are cleared
10708         $LFS setstripe -c -1 $FILE
10709         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10710         sync
10711         rm $FILE
10712 }
10713
10714 test_118a() #bug 11710
10715 {
10716         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10717
10718         reset_async
10719
10720         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10721         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10722         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10723
10724         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10725                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10726                 return 1;
10727         fi
10728         rm -f $DIR/$tfile
10729 }
10730 run_test 118a "verify O_SYNC works =========="
10731
10732 test_118b()
10733 {
10734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10735         remote_ost_nodsh && skip "remote OST with nodsh"
10736
10737         reset_async
10738
10739         #define OBD_FAIL_SRV_ENOENT 0x217
10740         set_nodes_failloc "$(osts_nodes)" 0x217
10741         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10742         RC=$?
10743         set_nodes_failloc "$(osts_nodes)" 0
10744         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10745         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10746                     grep -c writeback)
10747
10748         if [[ $RC -eq 0 ]]; then
10749                 error "Must return error due to dropped pages, rc=$RC"
10750                 return 1;
10751         fi
10752
10753         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10754                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10755                 return 1;
10756         fi
10757
10758         echo "Dirty pages not leaked on ENOENT"
10759
10760         # Due to the above error the OSC will issue all RPCs syncronously
10761         # until a subsequent RPC completes successfully without error.
10762         $MULTIOP $DIR/$tfile Ow4096yc
10763         rm -f $DIR/$tfile
10764
10765         return 0
10766 }
10767 run_test 118b "Reclaim dirty pages on fatal error =========="
10768
10769 test_118c()
10770 {
10771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10772
10773         # for 118c, restore the original resend count, LU-1940
10774         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10775                                 set_resend_count $OLD_RESENDCOUNT
10776         remote_ost_nodsh && skip "remote OST with nodsh"
10777
10778         reset_async
10779
10780         #define OBD_FAIL_OST_EROFS               0x216
10781         set_nodes_failloc "$(osts_nodes)" 0x216
10782
10783         # multiop should block due to fsync until pages are written
10784         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10785         MULTIPID=$!
10786         sleep 1
10787
10788         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10789                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10790         fi
10791
10792         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10793                     grep -c writeback)
10794         if [[ $WRITEBACK -eq 0 ]]; then
10795                 error "No page in writeback, writeback=$WRITEBACK"
10796         fi
10797
10798         set_nodes_failloc "$(osts_nodes)" 0
10799         wait $MULTIPID
10800         RC=$?
10801         if [[ $RC -ne 0 ]]; then
10802                 error "Multiop fsync failed, rc=$RC"
10803         fi
10804
10805         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10806         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10807                     grep -c writeback)
10808         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10809                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10810         fi
10811
10812         rm -f $DIR/$tfile
10813         echo "Dirty pages flushed via fsync on EROFS"
10814         return 0
10815 }
10816 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10817
10818 # continue to use small resend count to reduce test_118* time (b=14842)
10819 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10820
10821 test_118d()
10822 {
10823         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10824         remote_ost_nodsh && skip "remote OST with nodsh"
10825
10826         reset_async
10827
10828         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10829         set_nodes_failloc "$(osts_nodes)" 0x214
10830         # multiop should block due to fsync until pages are written
10831         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10832         MULTIPID=$!
10833         sleep 1
10834
10835         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10836                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10837         fi
10838
10839         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10840                     grep -c writeback)
10841         if [[ $WRITEBACK -eq 0 ]]; then
10842                 error "No page in writeback, writeback=$WRITEBACK"
10843         fi
10844
10845         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10846         set_nodes_failloc "$(osts_nodes)" 0
10847
10848         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10849         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10850                     grep -c writeback)
10851         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10852                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10853         fi
10854
10855         rm -f $DIR/$tfile
10856         echo "Dirty pages gaurenteed flushed via fsync"
10857         return 0
10858 }
10859 run_test 118d "Fsync validation inject a delay of the bulk =========="
10860
10861 test_118f() {
10862         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10863
10864         reset_async
10865
10866         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10867         lctl set_param fail_loc=0x8000040a
10868
10869         # Should simulate EINVAL error which is fatal
10870         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10871         RC=$?
10872         if [[ $RC -eq 0 ]]; then
10873                 error "Must return error due to dropped pages, rc=$RC"
10874         fi
10875
10876         lctl set_param fail_loc=0x0
10877
10878         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10879         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10880         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10881                     grep -c writeback)
10882         if [[ $LOCKED -ne 0 ]]; then
10883                 error "Locked pages remain in cache, locked=$LOCKED"
10884         fi
10885
10886         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10887                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10888         fi
10889
10890         rm -f $DIR/$tfile
10891         echo "No pages locked after fsync"
10892
10893         reset_async
10894         return 0
10895 }
10896 run_test 118f "Simulate unrecoverable OSC side error =========="
10897
10898 test_118g() {
10899         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10900
10901         reset_async
10902
10903         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10904         lctl set_param fail_loc=0x406
10905
10906         # simulate local -ENOMEM
10907         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10908         RC=$?
10909
10910         lctl set_param fail_loc=0
10911         if [[ $RC -eq 0 ]]; then
10912                 error "Must return error due to dropped pages, rc=$RC"
10913         fi
10914
10915         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10916         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10917         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10918                         grep -c writeback)
10919         if [[ $LOCKED -ne 0 ]]; then
10920                 error "Locked pages remain in cache, locked=$LOCKED"
10921         fi
10922
10923         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10924                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10925         fi
10926
10927         rm -f $DIR/$tfile
10928         echo "No pages locked after fsync"
10929
10930         reset_async
10931         return 0
10932 }
10933 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10934
10935 test_118h() {
10936         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10937         remote_ost_nodsh && skip "remote OST with nodsh"
10938
10939         reset_async
10940
10941         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10942         set_nodes_failloc "$(osts_nodes)" 0x20e
10943         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10944         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10945         RC=$?
10946
10947         set_nodes_failloc "$(osts_nodes)" 0
10948         if [[ $RC -eq 0 ]]; then
10949                 error "Must return error due to dropped pages, rc=$RC"
10950         fi
10951
10952         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10953         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10954         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10955                     grep -c writeback)
10956         if [[ $LOCKED -ne 0 ]]; then
10957                 error "Locked pages remain in cache, locked=$LOCKED"
10958         fi
10959
10960         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10961                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10962         fi
10963
10964         rm -f $DIR/$tfile
10965         echo "No pages locked after fsync"
10966
10967         return 0
10968 }
10969 run_test 118h "Verify timeout in handling recoverables errors  =========="
10970
10971 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
10972
10973 test_118i() {
10974         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10975         remote_ost_nodsh && skip "remote OST with nodsh"
10976
10977         reset_async
10978
10979         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10980         set_nodes_failloc "$(osts_nodes)" 0x20e
10981
10982         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10983         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10984         PID=$!
10985         sleep 5
10986         set_nodes_failloc "$(osts_nodes)" 0
10987
10988         wait $PID
10989         RC=$?
10990         if [[ $RC -ne 0 ]]; then
10991                 error "got error, but should be not, rc=$RC"
10992         fi
10993
10994         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10995         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10996         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10997         if [[ $LOCKED -ne 0 ]]; then
10998                 error "Locked pages remain in cache, locked=$LOCKED"
10999         fi
11000
11001         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11002                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11003         fi
11004
11005         rm -f $DIR/$tfile
11006         echo "No pages locked after fsync"
11007
11008         return 0
11009 }
11010 run_test 118i "Fix error before timeout in recoverable error  =========="
11011
11012 [ "$SLOW" = "no" ] && set_resend_count 4
11013
11014 test_118j() {
11015         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11016         remote_ost_nodsh && skip "remote OST with nodsh"
11017
11018         reset_async
11019
11020         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
11021         set_nodes_failloc "$(osts_nodes)" 0x220
11022
11023         # return -EIO from OST
11024         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11025         RC=$?
11026         set_nodes_failloc "$(osts_nodes)" 0x0
11027         if [[ $RC -eq 0 ]]; then
11028                 error "Must return error due to dropped pages, rc=$RC"
11029         fi
11030
11031         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11032         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11033         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11034         if [[ $LOCKED -ne 0 ]]; then
11035                 error "Locked pages remain in cache, locked=$LOCKED"
11036         fi
11037
11038         # in recoverable error on OST we want resend and stay until it finished
11039         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11040                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11041         fi
11042
11043         rm -f $DIR/$tfile
11044         echo "No pages locked after fsync"
11045
11046         return 0
11047 }
11048 run_test 118j "Simulate unrecoverable OST side error =========="
11049
11050 test_118k()
11051 {
11052         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11053         remote_ost_nodsh && skip "remote OSTs with nodsh"
11054
11055         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
11056         set_nodes_failloc "$(osts_nodes)" 0x20e
11057         test_mkdir $DIR/$tdir
11058
11059         for ((i=0;i<10;i++)); do
11060                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
11061                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
11062                 SLEEPPID=$!
11063                 sleep 0.500s
11064                 kill $SLEEPPID
11065                 wait $SLEEPPID
11066         done
11067
11068         set_nodes_failloc "$(osts_nodes)" 0
11069         rm -rf $DIR/$tdir
11070 }
11071 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
11072
11073 test_118l() # LU-646
11074 {
11075         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11076
11077         test_mkdir $DIR/$tdir
11078         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
11079         rm -rf $DIR/$tdir
11080 }
11081 run_test 118l "fsync dir"
11082
11083 test_118m() # LU-3066
11084 {
11085         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11086
11087         test_mkdir $DIR/$tdir
11088         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
11089         rm -rf $DIR/$tdir
11090 }
11091 run_test 118m "fdatasync dir ========="
11092
11093 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
11094
11095 test_118n()
11096 {
11097         local begin
11098         local end
11099
11100         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11101         remote_ost_nodsh && skip "remote OSTs with nodsh"
11102
11103         # Sleep to avoid a cached response.
11104         #define OBD_STATFS_CACHE_SECONDS 1
11105         sleep 2
11106
11107         # Inject a 10 second delay in the OST_STATFS handler.
11108         #define OBD_FAIL_OST_STATFS_DELAY 0x242
11109         set_nodes_failloc "$(osts_nodes)" 0x242
11110
11111         begin=$SECONDS
11112         stat --file-system $MOUNT > /dev/null
11113         end=$SECONDS
11114
11115         set_nodes_failloc "$(osts_nodes)" 0
11116
11117         if ((end - begin > 20)); then
11118             error "statfs took $((end - begin)) seconds, expected 10"
11119         fi
11120 }
11121 run_test 118n "statfs() sends OST_STATFS requests in parallel"
11122
11123 test_119a() # bug 11737
11124 {
11125         BSIZE=$((512 * 1024))
11126         directio write $DIR/$tfile 0 1 $BSIZE
11127         # We ask to read two blocks, which is more than a file size.
11128         # directio will indicate an error when requested and actual
11129         # sizes aren't equeal (a normal situation in this case) and
11130         # print actual read amount.
11131         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
11132         if [ "$NOB" != "$BSIZE" ]; then
11133                 error "read $NOB bytes instead of $BSIZE"
11134         fi
11135         rm -f $DIR/$tfile
11136 }
11137 run_test 119a "Short directIO read must return actual read amount"
11138
11139 test_119b() # bug 11737
11140 {
11141         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11142
11143         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
11144         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
11145         sync
11146         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
11147                 error "direct read failed"
11148         rm -f $DIR/$tfile
11149 }
11150 run_test 119b "Sparse directIO read must return actual read amount"
11151
11152 test_119c() # bug 13099
11153 {
11154         BSIZE=1048576
11155         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
11156         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
11157         rm -f $DIR/$tfile
11158 }
11159 run_test 119c "Testing for direct read hitting hole"
11160
11161 test_119d() # bug 15950
11162 {
11163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11164
11165         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
11166         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
11167         BSIZE=1048576
11168         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
11169         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
11170         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
11171         lctl set_param fail_loc=0x40d
11172         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
11173         pid_dio=$!
11174         sleep 1
11175         cat $DIR/$tfile > /dev/null &
11176         lctl set_param fail_loc=0
11177         pid_reads=$!
11178         wait $pid_dio
11179         log "the DIO writes have completed, now wait for the reads (should not block very long)"
11180         sleep 2
11181         [ -n "`ps h -p $pid_reads -o comm`" ] && \
11182         error "the read rpcs have not completed in 2s"
11183         rm -f $DIR/$tfile
11184         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
11185 }
11186 run_test 119d "The DIO path should try to send a new rpc once one is completed"
11187
11188 test_120a() {
11189         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11190         remote_mds_nodsh && skip "remote MDS with nodsh"
11191         test_mkdir -i0 -c1 $DIR/$tdir
11192         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11193                 skip_env "no early lock cancel on server"
11194
11195         lru_resize_disable mdc
11196         lru_resize_disable osc
11197         cancel_lru_locks mdc
11198         # asynchronous object destroy at MDT could cause bl ast to client
11199         cancel_lru_locks osc
11200
11201         stat $DIR/$tdir > /dev/null
11202         can1=$(do_facet mds1 \
11203                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11204                awk '/ldlm_cancel/ {print $2}')
11205         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11206                awk '/ldlm_bl_callback/ {print $2}')
11207         test_mkdir -i0 -c1 $DIR/$tdir/d1
11208         can2=$(do_facet mds1 \
11209                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11210                awk '/ldlm_cancel/ {print $2}')
11211         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11212                awk '/ldlm_bl_callback/ {print $2}')
11213         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11214         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11215         lru_resize_enable mdc
11216         lru_resize_enable osc
11217 }
11218 run_test 120a "Early Lock Cancel: mkdir test"
11219
11220 test_120b() {
11221         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11222         remote_mds_nodsh && skip "remote MDS with nodsh"
11223         test_mkdir $DIR/$tdir
11224         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11225                 skip_env "no early lock cancel on server"
11226
11227         lru_resize_disable mdc
11228         lru_resize_disable osc
11229         cancel_lru_locks mdc
11230         stat $DIR/$tdir > /dev/null
11231         can1=$(do_facet $SINGLEMDS \
11232                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11233                awk '/ldlm_cancel/ {print $2}')
11234         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11235                awk '/ldlm_bl_callback/ {print $2}')
11236         touch $DIR/$tdir/f1
11237         can2=$(do_facet $SINGLEMDS \
11238                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11239                awk '/ldlm_cancel/ {print $2}')
11240         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11241                awk '/ldlm_bl_callback/ {print $2}')
11242         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11243         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11244         lru_resize_enable mdc
11245         lru_resize_enable osc
11246 }
11247 run_test 120b "Early Lock Cancel: create test"
11248
11249 test_120c() {
11250         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11251         remote_mds_nodsh && skip "remote MDS with nodsh"
11252         test_mkdir -i0 -c1 $DIR/$tdir
11253         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11254                 skip "no early lock cancel on server"
11255
11256         lru_resize_disable mdc
11257         lru_resize_disable osc
11258         test_mkdir -i0 -c1 $DIR/$tdir/d1
11259         test_mkdir -i0 -c1 $DIR/$tdir/d2
11260         touch $DIR/$tdir/d1/f1
11261         cancel_lru_locks mdc
11262         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /dev/null
11263         can1=$(do_facet mds1 \
11264                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11265                awk '/ldlm_cancel/ {print $2}')
11266         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11267                awk '/ldlm_bl_callback/ {print $2}')
11268         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11269         can2=$(do_facet mds1 \
11270                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11271                awk '/ldlm_cancel/ {print $2}')
11272         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11273                awk '/ldlm_bl_callback/ {print $2}')
11274         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11275         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11276         lru_resize_enable mdc
11277         lru_resize_enable osc
11278 }
11279 run_test 120c "Early Lock Cancel: link test"
11280
11281 test_120d() {
11282         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11283         remote_mds_nodsh && skip "remote MDS with nodsh"
11284         test_mkdir -i0 -c1 $DIR/$tdir
11285         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11286                 skip_env "no early lock cancel on server"
11287
11288         lru_resize_disable mdc
11289         lru_resize_disable osc
11290         touch $DIR/$tdir
11291         cancel_lru_locks mdc
11292         stat $DIR/$tdir > /dev/null
11293         can1=$(do_facet mds1 \
11294                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11295                awk '/ldlm_cancel/ {print $2}')
11296         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11297                awk '/ldlm_bl_callback/ {print $2}')
11298         chmod a+x $DIR/$tdir
11299         can2=$(do_facet mds1 \
11300                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11301                awk '/ldlm_cancel/ {print $2}')
11302         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11303                awk '/ldlm_bl_callback/ {print $2}')
11304         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11305         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11306         lru_resize_enable mdc
11307         lru_resize_enable osc
11308 }
11309 run_test 120d "Early Lock Cancel: setattr test"
11310
11311 test_120e() {
11312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11313         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11314                 skip_env "no early lock cancel on server"
11315         remote_mds_nodsh && skip "remote MDS with nodsh"
11316
11317         local dlmtrace_set=false
11318
11319         test_mkdir -i0 -c1 $DIR/$tdir
11320         lru_resize_disable mdc
11321         lru_resize_disable osc
11322         ! $LCTL get_param debug | grep -q dlmtrace &&
11323                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
11324         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
11325         cancel_lru_locks mdc
11326         cancel_lru_locks osc
11327         dd if=$DIR/$tdir/f1 of=/dev/null
11328         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
11329         # XXX client can not do early lock cancel of OST lock
11330         # during unlink (LU-4206), so cancel osc lock now.
11331         sleep 2
11332         cancel_lru_locks osc
11333         can1=$(do_facet mds1 \
11334                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11335                awk '/ldlm_cancel/ {print $2}')
11336         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11337                awk '/ldlm_bl_callback/ {print $2}')
11338         unlink $DIR/$tdir/f1
11339         sleep 5
11340         can2=$(do_facet mds1 \
11341                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11342                awk '/ldlm_cancel/ {print $2}')
11343         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11344                awk '/ldlm_bl_callback/ {print $2}')
11345         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
11346                 $LCTL dk $TMP/cancel.debug.txt
11347         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
11348                 $LCTL dk $TMP/blocking.debug.txt
11349         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
11350         lru_resize_enable mdc
11351         lru_resize_enable osc
11352 }
11353 run_test 120e "Early Lock Cancel: unlink test"
11354
11355 test_120f() {
11356         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11357         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11358                 skip_env "no early lock cancel on server"
11359         remote_mds_nodsh && skip "remote MDS with nodsh"
11360
11361         test_mkdir -i0 -c1 $DIR/$tdir
11362         lru_resize_disable mdc
11363         lru_resize_disable osc
11364         test_mkdir -i0 -c1 $DIR/$tdir/d1
11365         test_mkdir -i0 -c1 $DIR/$tdir/d2
11366         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
11367         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
11368         cancel_lru_locks mdc
11369         cancel_lru_locks osc
11370         dd if=$DIR/$tdir/d1/f1 of=/dev/null
11371         dd if=$DIR/$tdir/d2/f2 of=/dev/null
11372         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
11373         # XXX client can not do early lock cancel of OST lock
11374         # during rename (LU-4206), so cancel osc lock now.
11375         sleep 2
11376         cancel_lru_locks osc
11377         can1=$(do_facet mds1 \
11378                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11379                awk '/ldlm_cancel/ {print $2}')
11380         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11381                awk '/ldlm_bl_callback/ {print $2}')
11382         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11383         sleep 5
11384         can2=$(do_facet mds1 \
11385                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11386                awk '/ldlm_cancel/ {print $2}')
11387         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11388                awk '/ldlm_bl_callback/ {print $2}')
11389         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11390         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11391         lru_resize_enable mdc
11392         lru_resize_enable osc
11393 }
11394 run_test 120f "Early Lock Cancel: rename test"
11395
11396 test_120g() {
11397         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11398         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11399                 skip_env "no early lock cancel on server"
11400         remote_mds_nodsh && skip "remote MDS with nodsh"
11401
11402         lru_resize_disable mdc
11403         lru_resize_disable osc
11404         count=10000
11405         echo create $count files
11406         test_mkdir $DIR/$tdir
11407         cancel_lru_locks mdc
11408         cancel_lru_locks osc
11409         t0=$(date +%s)
11410
11411         can0=$(do_facet $SINGLEMDS \
11412                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11413                awk '/ldlm_cancel/ {print $2}')
11414         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11415                awk '/ldlm_bl_callback/ {print $2}')
11416         createmany -o $DIR/$tdir/f $count
11417         sync
11418         can1=$(do_facet $SINGLEMDS \
11419                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11420                awk '/ldlm_cancel/ {print $2}')
11421         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11422                awk '/ldlm_bl_callback/ {print $2}')
11423         t1=$(date +%s)
11424         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11425         echo rm $count files
11426         rm -r $DIR/$tdir
11427         sync
11428         can2=$(do_facet $SINGLEMDS \
11429                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11430                awk '/ldlm_cancel/ {print $2}')
11431         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11432                awk '/ldlm_bl_callback/ {print $2}')
11433         t2=$(date +%s)
11434         echo total: $count removes in $((t2-t1))
11435         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11436         sleep 2
11437         # wait for commitment of removal
11438         lru_resize_enable mdc
11439         lru_resize_enable osc
11440 }
11441 run_test 120g "Early Lock Cancel: performance test"
11442
11443 test_121() { #bug #10589
11444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11445
11446         rm -rf $DIR/$tfile
11447         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11448 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11449         lctl set_param fail_loc=0x310
11450         cancel_lru_locks osc > /dev/null
11451         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11452         lctl set_param fail_loc=0
11453         [[ $reads -eq $writes ]] ||
11454                 error "read $reads blocks, must be $writes blocks"
11455 }
11456 run_test 121 "read cancel race ========="
11457
11458 test_123a_base() { # was test 123, statahead(bug 11401)
11459         local lsx="$1"
11460
11461         SLOWOK=0
11462         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11463                 log "testing UP system. Performance may be lower than expected."
11464                 SLOWOK=1
11465         fi
11466
11467         rm -rf $DIR/$tdir
11468         test_mkdir $DIR/$tdir
11469         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11470         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11471         MULT=10
11472         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11473                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11474
11475                 max=$(lctl get_param -n llite.*.statahead_max | head -n 1)
11476                 lctl set_param -n llite.*.statahead_max 0
11477                 lctl get_param llite.*.statahead_max
11478                 cancel_lru_locks mdc
11479                 cancel_lru_locks osc
11480                 stime=$(date +%s)
11481                 time $lsx $DIR/$tdir | wc -l
11482                 etime=$(date +%s)
11483                 delta=$((etime - stime))
11484                 log "$lsx $i files without statahead: $delta sec"
11485                 lctl set_param llite.*.statahead_max=$max
11486
11487                 swrong=$(lctl get_param -n llite.*.statahead_stats |
11488                         grep "statahead wrong:" | awk '{print $3}')
11489                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11490                 cancel_lru_locks mdc
11491                 cancel_lru_locks osc
11492                 stime=$(date +%s)
11493                 time $lsx $DIR/$tdir | wc -l
11494                 etime=$(date +%s)
11495                 delta_sa=$((etime - stime))
11496                 log "$lsx $i files with statahead: $delta_sa sec"
11497                 lctl get_param -n llite.*.statahead_stats
11498                 ewrong=$(lctl get_param -n llite.*.statahead_stats |
11499                         grep "statahead wrong:" | awk '{print $3}')
11500
11501                 [[ $swrong -lt $ewrong ]] &&
11502                         log "statahead was stopped, maybe too many locks held!"
11503                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11504
11505                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11506                         max=$(lctl get_param -n llite.*.statahead_max |
11507                                 head -n 1)
11508                         lctl set_param -n llite.*.statahead_max 0
11509                         lctl get_param llite.*.statahead_max
11510                         cancel_lru_locks mdc
11511                         cancel_lru_locks osc
11512                         stime=$(date +%s)
11513                         time $lsx $DIR/$tdir | wc -l
11514                         etime=$(date +%s)
11515                         delta=$((etime - stime))
11516                         log "$lsx $i files again without statahead: $delta sec"
11517                         lctl set_param llite.*.statahead_max=$max
11518                         if [ $((delta_sa * 100 > delta * 105 && delta_sa > delta + 2)) ]; then
11519                                 if [  $SLOWOK -eq 0 ]; then
11520                                         error "$lsx $i files is slower with statahead!"
11521                                 else
11522                                         log "$lsx $i files is slower with statahead!"
11523                                 fi
11524                                 break
11525                         fi
11526                 fi
11527
11528                 [ $delta -gt 20 ] && break
11529                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11530                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11531         done
11532         log "$lsx done"
11533
11534         stime=$(date +%s)
11535         rm -r $DIR/$tdir
11536         sync
11537         etime=$(date +%s)
11538         delta=$((etime - stime))
11539         log "rm -r $DIR/$tdir/: $delta seconds"
11540         log "rm done"
11541         lctl get_param -n llite.*.statahead_stats
11542 }
11543
11544 test_123aa() {
11545         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11546
11547         test_123a_base "ls -l"
11548 }
11549 run_test 123aa "verify statahead work"
11550
11551 test_123ab() {
11552         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11553
11554         statx_supported || skip_env "Test must be statx() syscall supported"
11555
11556         test_123a_base "$STATX -l"
11557 }
11558 run_test 123ab "verify statahead work by using statx"
11559
11560 test_123ac() {
11561         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11562
11563         statx_supported || skip_env "Test must be statx() syscall supported"
11564
11565         local rpcs_before
11566         local rpcs_after
11567         local agl_before
11568         local agl_after
11569
11570         cancel_lru_locks $OSC
11571         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
11572         agl_before=$($LCTL get_param -n llite.*.statahead_stats |
11573                 awk '/agl.total:/ {print $3}')
11574         test_123a_base "$STATX -c \"%n %i %A %h %u %g %W %X %Z\" -D"
11575         test_123a_base "$STATX --cached=always -D"
11576         agl_after=$($LCTL get_param -n llite.*.statahead_stats |
11577                 awk '/agl.total:/ {print $3}')
11578         [ $agl_before -eq $agl_after ] ||
11579                 error "Should not trigger AGL thread - $agl_before:$agl_after"
11580         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
11581         [ $rpcs_after -eq $rpcs_before ] ||
11582                 error "$STATX should not send glimpse RPCs to $OSC"
11583 }
11584 run_test 123ac "verify statahead work by using statx without glimpse RPCs"
11585
11586 test_123b () { # statahead(bug 15027)
11587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11588
11589         test_mkdir $DIR/$tdir
11590         createmany -o $DIR/$tdir/$tfile-%d 1000
11591
11592         cancel_lru_locks mdc
11593         cancel_lru_locks osc
11594
11595 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11596         lctl set_param fail_loc=0x80000803
11597         ls -lR $DIR/$tdir > /dev/null
11598         log "ls done"
11599         lctl set_param fail_loc=0x0
11600         lctl get_param -n llite.*.statahead_stats
11601         rm -r $DIR/$tdir
11602         sync
11603
11604 }
11605 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11606
11607 test_123c() {
11608         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11609
11610         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11611         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11612         touch $DIR/$tdir.1/{1..3}
11613         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11614
11615         remount_client $MOUNT
11616
11617         $MULTIOP $DIR/$tdir.0 Q
11618
11619         # let statahead to complete
11620         ls -l $DIR/$tdir.0 > /dev/null
11621
11622         testid=$(echo $TESTNAME | tr '_' ' ')
11623         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11624                 error "statahead warning" || true
11625 }
11626 run_test 123c "Can not initialize inode warning on DNE statahead"
11627
11628 test_124a() {
11629         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11630         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11631                 skip_env "no lru resize on server"
11632
11633         local NR=2000
11634
11635         test_mkdir $DIR/$tdir
11636
11637         log "create $NR files at $DIR/$tdir"
11638         createmany -o $DIR/$tdir/f $NR ||
11639                 error "failed to create $NR files in $DIR/$tdir"
11640
11641         cancel_lru_locks mdc
11642         ls -l $DIR/$tdir > /dev/null
11643
11644         local NSDIR=""
11645         local LRU_SIZE=0
11646         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11647                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11648                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11649                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11650                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11651                         log "NSDIR=$NSDIR"
11652                         log "NS=$(basename $NSDIR)"
11653                         break
11654                 fi
11655         done
11656
11657         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11658                 skip "Not enough cached locks created!"
11659         fi
11660         log "LRU=$LRU_SIZE"
11661
11662         local SLEEP=30
11663
11664         # We know that lru resize allows one client to hold $LIMIT locks
11665         # for 10h. After that locks begin to be killed by client.
11666         local MAX_HRS=10
11667         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11668         log "LIMIT=$LIMIT"
11669         if [ $LIMIT -lt $LRU_SIZE ]; then
11670                 skip "Limit is too small $LIMIT"
11671         fi
11672
11673         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11674         # killing locks. Some time was spent for creating locks. This means
11675         # that up to the moment of sleep finish we must have killed some of
11676         # them (10-100 locks). This depends on how fast ther were created.
11677         # Many of them were touched in almost the same moment and thus will
11678         # be killed in groups.
11679         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11680
11681         # Use $LRU_SIZE_B here to take into account real number of locks
11682         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11683         local LRU_SIZE_B=$LRU_SIZE
11684         log "LVF=$LVF"
11685         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11686         log "OLD_LVF=$OLD_LVF"
11687         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11688
11689         # Let's make sure that we really have some margin. Client checks
11690         # cached locks every 10 sec.
11691         SLEEP=$((SLEEP+20))
11692         log "Sleep ${SLEEP} sec"
11693         local SEC=0
11694         while ((SEC<$SLEEP)); do
11695                 echo -n "..."
11696                 sleep 5
11697                 SEC=$((SEC+5))
11698                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11699                 echo -n "$LRU_SIZE"
11700         done
11701         echo ""
11702         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11703         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11704
11705         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11706                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11707                 unlinkmany $DIR/$tdir/f $NR
11708                 return
11709         }
11710
11711         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11712         log "unlink $NR files at $DIR/$tdir"
11713         unlinkmany $DIR/$tdir/f $NR
11714 }
11715 run_test 124a "lru resize ======================================="
11716
11717 get_max_pool_limit()
11718 {
11719         local limit=$($LCTL get_param \
11720                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11721         local max=0
11722         for l in $limit; do
11723                 if [[ $l -gt $max ]]; then
11724                         max=$l
11725                 fi
11726         done
11727         echo $max
11728 }
11729
11730 test_124b() {
11731         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11732         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11733                 skip_env "no lru resize on server"
11734
11735         LIMIT=$(get_max_pool_limit)
11736
11737         NR=$(($(default_lru_size)*20))
11738         if [[ $NR -gt $LIMIT ]]; then
11739                 log "Limit lock number by $LIMIT locks"
11740                 NR=$LIMIT
11741         fi
11742
11743         IFree=$(mdsrate_inodes_available)
11744         if [ $IFree -lt $NR ]; then
11745                 log "Limit lock number by $IFree inodes"
11746                 NR=$IFree
11747         fi
11748
11749         lru_resize_disable mdc
11750         test_mkdir -p $DIR/$tdir/disable_lru_resize
11751
11752         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11753         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11754         cancel_lru_locks mdc
11755         stime=`date +%s`
11756         PID=""
11757         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11758         PID="$PID $!"
11759         sleep 2
11760         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11761         PID="$PID $!"
11762         sleep 2
11763         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11764         PID="$PID $!"
11765         wait $PID
11766         etime=`date +%s`
11767         nolruresize_delta=$((etime-stime))
11768         log "ls -la time: $nolruresize_delta seconds"
11769         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11770         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11771
11772         lru_resize_enable mdc
11773         test_mkdir -p $DIR/$tdir/enable_lru_resize
11774
11775         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11776         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11777         cancel_lru_locks mdc
11778         stime=`date +%s`
11779         PID=""
11780         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11781         PID="$PID $!"
11782         sleep 2
11783         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11784         PID="$PID $!"
11785         sleep 2
11786         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11787         PID="$PID $!"
11788         wait $PID
11789         etime=`date +%s`
11790         lruresize_delta=$((etime-stime))
11791         log "ls -la time: $lruresize_delta seconds"
11792         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11793
11794         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11795                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11796         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11797                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11798         else
11799                 log "lru resize performs the same with no lru resize"
11800         fi
11801         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11802 }
11803 run_test 124b "lru resize (performance test) ======================="
11804
11805 test_124c() {
11806         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11807         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11808                 skip_env "no lru resize on server"
11809
11810         # cache ununsed locks on client
11811         local nr=100
11812         cancel_lru_locks mdc
11813         test_mkdir $DIR/$tdir
11814         createmany -o $DIR/$tdir/f $nr ||
11815                 error "failed to create $nr files in $DIR/$tdir"
11816         ls -l $DIR/$tdir > /dev/null
11817
11818         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11819         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11820         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11821         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11822         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11823
11824         # set lru_max_age to 1 sec
11825         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11826         echo "sleep $((recalc_p * 2)) seconds..."
11827         sleep $((recalc_p * 2))
11828
11829         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11830         # restore lru_max_age
11831         $LCTL set_param -n $nsdir.lru_max_age $max_age
11832         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11833         unlinkmany $DIR/$tdir/f $nr
11834 }
11835 run_test 124c "LRUR cancel very aged locks"
11836
11837 test_124d() {
11838         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11839         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11840                 skip_env "no lru resize on server"
11841
11842         # cache ununsed locks on client
11843         local nr=100
11844
11845         lru_resize_disable mdc
11846         stack_trap "lru_resize_enable mdc" EXIT
11847
11848         cancel_lru_locks mdc
11849
11850         # asynchronous object destroy at MDT could cause bl ast to client
11851         test_mkdir $DIR/$tdir
11852         createmany -o $DIR/$tdir/f $nr ||
11853                 error "failed to create $nr files in $DIR/$tdir"
11854         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11855
11856         ls -l $DIR/$tdir > /dev/null
11857
11858         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11859         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11860         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11861         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11862
11863         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11864
11865         # set lru_max_age to 1 sec
11866         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11867         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11868
11869         echo "sleep $((recalc_p * 2)) seconds..."
11870         sleep $((recalc_p * 2))
11871
11872         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11873
11874         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11875 }
11876 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11877
11878 test_125() { # 13358
11879         $LCTL get_param -n llite.*.client_type | grep -q local ||
11880                 skip "must run as local client"
11881         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11882                 skip_env "must have acl enabled"
11883         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11884
11885         test_mkdir $DIR/$tdir
11886         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11887         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11888         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11889 }
11890 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11891
11892 test_126() { # bug 12829/13455
11893         $GSS && skip_env "must run as gss disabled"
11894         $LCTL get_param -n llite.*.client_type | grep -q local ||
11895                 skip "must run as local client"
11896         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11897
11898         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11899         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11900         rm -f $DIR/$tfile
11901         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11902 }
11903 run_test 126 "check that the fsgid provided by the client is taken into account"
11904
11905 test_127a() { # bug 15521
11906         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11907         local name count samp unit min max sum sumsq
11908
11909         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11910         echo "stats before reset"
11911         $LCTL get_param osc.*.stats
11912         $LCTL set_param osc.*.stats=0
11913         local fsize=$((2048 * 1024))
11914
11915         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11916         cancel_lru_locks osc
11917         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11918
11919         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11920         stack_trap "rm -f $TMP/$tfile.tmp"
11921         while read name count samp unit min max sum sumsq; do
11922                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11923                 [ ! $min ] && error "Missing min value for $name proc entry"
11924                 eval $name=$count || error "Wrong proc format"
11925
11926                 case $name in
11927                 read_bytes|write_bytes)
11928                         [[ "$unit" =~ "bytes" ]] ||
11929                                 error "unit is not 'bytes': $unit"
11930                         (( $min >= 4096 )) || error "min is too small: $min"
11931                         (( $min <= $fsize )) || error "min is too big: $min"
11932                         (( $max >= 4096 )) || error "max is too small: $max"
11933                         (( $max <= $fsize )) || error "max is too big: $max"
11934                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11935                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11936                                 error "sumsquare is too small: $sumsq"
11937                         (( $sumsq <= $fsize * $fsize )) ||
11938                                 error "sumsquare is too big: $sumsq"
11939                         ;;
11940                 ost_read|ost_write)
11941                         [[ "$unit" =~ "usec" ]] ||
11942                                 error "unit is not 'usec': $unit"
11943                         ;;
11944                 *)      ;;
11945                 esac
11946         done < $DIR/$tfile.tmp
11947
11948         #check that we actually got some stats
11949         [ "$read_bytes" ] || error "Missing read_bytes stats"
11950         [ "$write_bytes" ] || error "Missing write_bytes stats"
11951         [ "$read_bytes" != 0 ] || error "no read done"
11952         [ "$write_bytes" != 0 ] || error "no write done"
11953 }
11954 run_test 127a "verify the client stats are sane"
11955
11956 test_127b() { # bug LU-333
11957         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11958         local name count samp unit min max sum sumsq
11959
11960         echo "stats before reset"
11961         $LCTL get_param llite.*.stats
11962         $LCTL set_param llite.*.stats=0
11963
11964         # perform 2 reads and writes so MAX is different from SUM.
11965         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11966         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11967         cancel_lru_locks osc
11968         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11969         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11970
11971         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11972         stack_trap "rm -f $TMP/$tfile.tmp"
11973         while read name count samp unit min max sum sumsq; do
11974                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11975                 eval $name=$count || error "Wrong proc format"
11976
11977                 case $name in
11978                 read_bytes|write_bytes)
11979                         [[ "$unit" =~ "bytes" ]] ||
11980                                 error "unit is not 'bytes': $unit"
11981                         (( $count == 2 )) || error "count is not 2: $count"
11982                         (( $min == $PAGE_SIZE )) ||
11983                                 error "min is not $PAGE_SIZE: $min"
11984                         (( $max == $PAGE_SIZE )) ||
11985                                 error "max is not $PAGE_SIZE: $max"
11986                         (( $sum == $PAGE_SIZE * 2 )) ||
11987                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11988                         ;;
11989                 read|write)
11990                         [[ "$unit" =~ "usec" ]] ||
11991                                 error "unit is not 'usec': $unit"
11992                         ;;
11993                 *)      ;;
11994                 esac
11995         done < $TMP/$tfile.tmp
11996
11997         #check that we actually got some stats
11998         [ "$read_bytes" ] || error "Missing read_bytes stats"
11999         [ "$write_bytes" ] || error "Missing write_bytes stats"
12000         [ "$read_bytes" != 0 ] || error "no read done"
12001         [ "$write_bytes" != 0 ] || error "no write done"
12002 }
12003 run_test 127b "verify the llite client stats are sane"
12004
12005 test_127c() { # LU-12394
12006         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
12007         local size
12008         local bsize
12009         local reads
12010         local writes
12011         local count
12012
12013         $LCTL set_param llite.*.extents_stats=1
12014         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
12015
12016         # Use two stripes so there is enough space in default config
12017         $LFS setstripe -c 2 $DIR/$tfile
12018
12019         # Extent stats start at 0-4K and go in power of two buckets
12020         # LL_HIST_START = 12 --> 2^12 = 4K
12021         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
12022         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
12023         # small configs
12024         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
12025                 do
12026                 # Write and read, 2x each, second time at a non-zero offset
12027                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
12028                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
12029                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
12030                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
12031                 rm -f $DIR/$tfile
12032         done
12033
12034         $LCTL get_param llite.*.extents_stats
12035
12036         count=2
12037         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
12038                 do
12039                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
12040                                 grep -m 1 $bsize)
12041                 reads=$(echo $bucket | awk '{print $5}')
12042                 writes=$(echo $bucket | awk '{print $9}')
12043                 [ "$reads" -eq $count ] ||
12044                         error "$reads reads in < $bsize bucket, expect $count"
12045                 [ "$writes" -eq $count ] ||
12046                         error "$writes writes in < $bsize bucket, expect $count"
12047         done
12048
12049         # Test mmap write and read
12050         $LCTL set_param llite.*.extents_stats=c
12051         size=512
12052         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
12053         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
12054         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
12055
12056         $LCTL get_param llite.*.extents_stats
12057
12058         count=$(((size*1024) / PAGE_SIZE))
12059
12060         bsize=$((2 * PAGE_SIZE / 1024))K
12061
12062         bucket=$($LCTL get_param -n llite.*.extents_stats |
12063                         grep -m 1 $bsize)
12064         reads=$(echo $bucket | awk '{print $5}')
12065         writes=$(echo $bucket | awk '{print $9}')
12066         # mmap writes fault in the page first, creating an additonal read
12067         [ "$reads" -eq $((2 * count)) ] ||
12068                 error "$reads reads in < $bsize bucket, expect $count"
12069         [ "$writes" -eq $count ] ||
12070                 error "$writes writes in < $bsize bucket, expect $count"
12071 }
12072 run_test 127c "test llite extent stats with regular & mmap i/o"
12073
12074 test_128() { # bug 15212
12075         touch $DIR/$tfile
12076         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
12077                 find $DIR/$tfile
12078                 find $DIR/$tfile
12079         EOF
12080
12081         result=$(grep error $TMP/$tfile.log)
12082         rm -f $DIR/$tfile $TMP/$tfile.log
12083         [ -z "$result" ] ||
12084                 error "consecutive find's under interactive lfs failed"
12085 }
12086 run_test 128 "interactive lfs for 2 consecutive find's"
12087
12088 set_dir_limits () {
12089         local mntdev
12090         local canondev
12091         local node
12092
12093         local ldproc=/proc/fs/ldiskfs
12094         local facets=$(get_facets MDS)
12095
12096         for facet in ${facets//,/ }; do
12097                 canondev=$(ldiskfs_canon \
12098                            *.$(convert_facet2label $facet).mntdev $facet)
12099                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
12100                         ldproc=/sys/fs/ldiskfs
12101                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
12102                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
12103         done
12104 }
12105
12106 check_mds_dmesg() {
12107         local facets=$(get_facets MDS)
12108         for facet in ${facets//,/ }; do
12109                 do_facet $facet "dmesg | tail -3 | grep $1" && return 0
12110         done
12111         return 1
12112 }
12113
12114 test_129() {
12115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12116         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
12117                 skip "Need MDS version with at least 2.5.56"
12118         if [ "$mds1_FSTYPE" != ldiskfs ]; then
12119                 skip_env "ldiskfs only test"
12120         fi
12121         remote_mds_nodsh && skip "remote MDS with nodsh"
12122
12123         local ENOSPC=28
12124         local has_warning=false
12125
12126         rm -rf $DIR/$tdir
12127         mkdir -p $DIR/$tdir
12128
12129         # block size of mds1
12130         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 8))
12131         set_dir_limits $maxsize $((maxsize * 6 / 8))
12132         stack_trap "set_dir_limits 0 0"
12133         stack_trap "unlinkmany $DIR/$tdir/file_base_ 2000 || true"
12134         local dirsize=$(stat -c%s "$DIR/$tdir")
12135         local nfiles=0
12136         while (( $dirsize <= $maxsize )); do
12137                 $MCREATE $DIR/$tdir/file_base_$nfiles
12138                 rc=$?
12139                 # check two errors:
12140                 # ENOSPC for ext4 max_dir_size, which has been used since
12141                 # kernel v3.6-rc1-8-gdf981d03ee, lustre v2_4_50_0-79-gaed82035c0
12142                 if (( rc == ENOSPC )); then
12143                         set_dir_limits 0 0
12144                         echo "rc=$rc returned as expected after $nfiles files"
12145
12146                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
12147                                 error "create failed w/o dir size limit"
12148
12149                         # messages may be rate limited if test is run repeatedly
12150                         check_mds_dmesg '"is approaching max"' ||
12151                                 echo "warning message should be output"
12152                         check_mds_dmesg '"has reached max"' ||
12153                                 echo "reached message should be output"
12154
12155                         dirsize=$(stat -c%s "$DIR/$tdir")
12156
12157                         [[ $dirsize -ge $maxsize ]] && return 0
12158                         error "dirsize $dirsize < $maxsize after $nfiles files"
12159                 elif (( rc != 0 )); then
12160                         break
12161                 fi
12162                 nfiles=$((nfiles + 1))
12163                 dirsize=$(stat -c%s "$DIR/$tdir")
12164         done
12165
12166         error "rc=$rc, size=$dirsize/$maxsize, mdt=$MDSCOUNT, nfiles=$nfiles"
12167 }
12168 run_test 129 "test directory size limit ========================"
12169
12170 OLDIFS="$IFS"
12171 cleanup_130() {
12172         trap 0
12173         IFS="$OLDIFS"
12174 }
12175
12176 test_130a() {
12177         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12178         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12179
12180         trap cleanup_130 EXIT RETURN
12181
12182         local fm_file=$DIR/$tfile
12183         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
12184         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
12185                 error "dd failed for $fm_file"
12186
12187         # LU-1795: test filefrag/FIEMAP once, even if unsupported
12188         filefrag -ves $fm_file
12189         RC=$?
12190         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12191                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12192         [ $RC != 0 ] && error "filefrag $fm_file failed"
12193
12194         filefrag_op=$(filefrag -ve -k $fm_file |
12195                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12196         lun=$($LFS getstripe -i $fm_file)
12197
12198         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
12199         IFS=$'\n'
12200         tot_len=0
12201         for line in $filefrag_op
12202         do
12203                 frag_lun=`echo $line | cut -d: -f5`
12204                 ext_len=`echo $line | cut -d: -f4`
12205                 if (( $frag_lun != $lun )); then
12206                         cleanup_130
12207                         error "FIEMAP on 1-stripe file($fm_file) failed"
12208                         return
12209                 fi
12210                 (( tot_len += ext_len ))
12211         done
12212
12213         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
12214                 cleanup_130
12215                 error "FIEMAP on 1-stripe file($fm_file) failed;"
12216                 return
12217         fi
12218
12219         cleanup_130
12220
12221         echo "FIEMAP on single striped file succeeded"
12222 }
12223 run_test 130a "FIEMAP (1-stripe file)"
12224
12225 test_130b() {
12226         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
12227
12228         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12229         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12230
12231         trap cleanup_130 EXIT RETURN
12232
12233         local fm_file=$DIR/$tfile
12234         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12235                         error "setstripe on $fm_file"
12236         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12237                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12238
12239         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
12240                 error "dd failed on $fm_file"
12241
12242         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12243         filefrag_op=$(filefrag -ve -k $fm_file |
12244                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12245
12246         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12247                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12248
12249         IFS=$'\n'
12250         tot_len=0
12251         num_luns=1
12252         for line in $filefrag_op
12253         do
12254                 frag_lun=$(echo $line | cut -d: -f5 |
12255                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12256                 ext_len=$(echo $line | cut -d: -f4)
12257                 if (( $frag_lun != $last_lun )); then
12258                         if (( tot_len != 1024 )); then
12259                                 cleanup_130
12260                                 error "FIEMAP on $fm_file failed; returned " \
12261                                 "len $tot_len for OST $last_lun instead of 1024"
12262                                 return
12263                         else
12264                                 (( num_luns += 1 ))
12265                                 tot_len=0
12266                         fi
12267                 fi
12268                 (( tot_len += ext_len ))
12269                 last_lun=$frag_lun
12270         done
12271         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
12272                 cleanup_130
12273                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12274                         "luns or wrong len for OST $last_lun"
12275                 return
12276         fi
12277
12278         cleanup_130
12279
12280         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
12281 }
12282 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
12283
12284 test_130c() {
12285         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12286
12287         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12288         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12289
12290         trap cleanup_130 EXIT RETURN
12291
12292         local fm_file=$DIR/$tfile
12293         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
12294         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12295                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12296
12297         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
12298                         error "dd failed on $fm_file"
12299
12300         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12301         filefrag_op=$(filefrag -ve -k $fm_file |
12302                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12303
12304         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12305                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12306
12307         IFS=$'\n'
12308         tot_len=0
12309         num_luns=1
12310         for line in $filefrag_op
12311         do
12312                 frag_lun=$(echo $line | cut -d: -f5 |
12313                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12314                 ext_len=$(echo $line | cut -d: -f4)
12315                 if (( $frag_lun != $last_lun )); then
12316                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
12317                         if (( logical != 512 )); then
12318                                 cleanup_130
12319                                 error "FIEMAP on $fm_file failed; returned " \
12320                                 "logical start for lun $logical instead of 512"
12321                                 return
12322                         fi
12323                         if (( tot_len != 512 )); then
12324                                 cleanup_130
12325                                 error "FIEMAP on $fm_file failed; returned " \
12326                                 "len $tot_len for OST $last_lun instead of 1024"
12327                                 return
12328                         else
12329                                 (( num_luns += 1 ))
12330                                 tot_len=0
12331                         fi
12332                 fi
12333                 (( tot_len += ext_len ))
12334                 last_lun=$frag_lun
12335         done
12336         if (( num_luns != 2 || tot_len != 512 )); then
12337                 cleanup_130
12338                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12339                         "luns or wrong len for OST $last_lun"
12340                 return
12341         fi
12342
12343         cleanup_130
12344
12345         echo "FIEMAP on 2-stripe file with hole succeeded"
12346 }
12347 run_test 130c "FIEMAP (2-stripe file with hole)"
12348
12349 test_130d() {
12350         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
12351
12352         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12353         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12354
12355         trap cleanup_130 EXIT RETURN
12356
12357         local fm_file=$DIR/$tfile
12358         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12359                         error "setstripe on $fm_file"
12360         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12361                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12362
12363         local actual_stripe_count=$($LFS getstripe -c $fm_file)
12364         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
12365                 error "dd failed on $fm_file"
12366
12367         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12368         filefrag_op=$(filefrag -ve -k $fm_file |
12369                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12370
12371         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12372                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12373
12374         IFS=$'\n'
12375         tot_len=0
12376         num_luns=1
12377         for line in $filefrag_op
12378         do
12379                 frag_lun=$(echo $line | cut -d: -f5 |
12380                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12381                 ext_len=$(echo $line | cut -d: -f4)
12382                 if (( $frag_lun != $last_lun )); then
12383                         if (( tot_len != 1024 )); then
12384                                 cleanup_130
12385                                 error "FIEMAP on $fm_file failed; returned " \
12386                                 "len $tot_len for OST $last_lun instead of 1024"
12387                                 return
12388                         else
12389                                 (( num_luns += 1 ))
12390                                 tot_len=0
12391                         fi
12392                 fi
12393                 (( tot_len += ext_len ))
12394                 last_lun=$frag_lun
12395         done
12396         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
12397                 cleanup_130
12398                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12399                         "luns or wrong len for OST $last_lun"
12400                 return
12401         fi
12402
12403         cleanup_130
12404
12405         echo "FIEMAP on N-stripe file succeeded"
12406 }
12407 run_test 130d "FIEMAP (N-stripe file)"
12408
12409 test_130e() {
12410         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12411
12412         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12413         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12414
12415         trap cleanup_130 EXIT RETURN
12416
12417         local fm_file=$DIR/$tfile
12418         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
12419         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12420                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12421
12422         NUM_BLKS=512
12423         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
12424         for ((i = 0; i < $NUM_BLKS; i++))
12425         do
12426                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
12427         done
12428
12429         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12430         filefrag_op=$(filefrag -ve -k $fm_file |
12431                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12432
12433         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12434                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12435
12436         IFS=$'\n'
12437         tot_len=0
12438         num_luns=1
12439         for line in $filefrag_op
12440         do
12441                 frag_lun=$(echo $line | cut -d: -f5 |
12442                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12443                 ext_len=$(echo $line | cut -d: -f4)
12444                 if (( $frag_lun != $last_lun )); then
12445                         if (( tot_len != $EXPECTED_LEN )); then
12446                                 cleanup_130
12447                                 error "FIEMAP on $fm_file failed; returned " \
12448                                 "len $tot_len for OST $last_lun instead " \
12449                                 "of $EXPECTED_LEN"
12450                                 return
12451                         else
12452                                 (( num_luns += 1 ))
12453                                 tot_len=0
12454                         fi
12455                 fi
12456                 (( tot_len += ext_len ))
12457                 last_lun=$frag_lun
12458         done
12459         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12460                 cleanup_130
12461                 error "FIEMAP on $fm_file failed; returned wrong number " \
12462                         "of luns or wrong len for OST $last_lun"
12463                 return
12464         fi
12465
12466         cleanup_130
12467
12468         echo "FIEMAP with continuation calls succeeded"
12469 }
12470 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12471
12472 test_130f() {
12473         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12474         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12475
12476         local fm_file=$DIR/$tfile
12477         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12478                 error "multiop create with lov_delay_create on $fm_file"
12479
12480         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12481         filefrag_extents=$(filefrag -vek $fm_file |
12482                            awk '/extents? found/ { print $2 }')
12483         if [[ "$filefrag_extents" != "0" ]]; then
12484                 error "FIEMAP on $fm_file failed; " \
12485                       "returned $filefrag_extents expected 0"
12486         fi
12487
12488         rm -f $fm_file
12489 }
12490 run_test 130f "FIEMAP (unstriped file)"
12491
12492 # Test for writev/readv
12493 test_131a() {
12494         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12495                 error "writev test failed"
12496         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12497                 error "readv failed"
12498         rm -f $DIR/$tfile
12499 }
12500 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12501
12502 test_131b() {
12503         local fsize=$((524288 + 1048576 + 1572864))
12504         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12505                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12506                         error "append writev test failed"
12507
12508         ((fsize += 1572864 + 1048576))
12509         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12510                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12511                         error "append writev test failed"
12512         rm -f $DIR/$tfile
12513 }
12514 run_test 131b "test append writev"
12515
12516 test_131c() {
12517         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12518         error "NOT PASS"
12519 }
12520 run_test 131c "test read/write on file w/o objects"
12521
12522 test_131d() {
12523         rwv -f $DIR/$tfile -w -n 1 1572864
12524         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12525         if [ "$NOB" != 1572864 ]; then
12526                 error "Short read filed: read $NOB bytes instead of 1572864"
12527         fi
12528         rm -f $DIR/$tfile
12529 }
12530 run_test 131d "test short read"
12531
12532 test_131e() {
12533         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12534         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12535         error "read hitting hole failed"
12536         rm -f $DIR/$tfile
12537 }
12538 run_test 131e "test read hitting hole"
12539
12540 check_stats() {
12541         local facet=$1
12542         local op=$2
12543         local want=${3:-0}
12544         local res
12545
12546         case $facet in
12547         mds*) res=$(do_facet $facet \
12548                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12549                  ;;
12550         ost*) res=$(do_facet $facet \
12551                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12552                  ;;
12553         *) error "Wrong facet '$facet'" ;;
12554         esac
12555         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12556         # if the argument $3 is zero, it means any stat increment is ok.
12557         if [[ $want -gt 0 ]]; then
12558                 local count=$(echo $res | awk '{ print $2 }')
12559                 [[ $count -ne $want ]] &&
12560                         error "The $op counter on $facet is $count, not $want"
12561         fi
12562 }
12563
12564 test_133a() {
12565         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12566         remote_ost_nodsh && skip "remote OST with nodsh"
12567         remote_mds_nodsh && skip "remote MDS with nodsh"
12568         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12569                 skip_env "MDS doesn't support rename stats"
12570
12571         local testdir=$DIR/${tdir}/stats_testdir
12572
12573         mkdir -p $DIR/${tdir}
12574
12575         # clear stats.
12576         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12577         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12578
12579         # verify mdt stats first.
12580         mkdir ${testdir} || error "mkdir failed"
12581         check_stats $SINGLEMDS "mkdir" 1
12582         touch ${testdir}/${tfile} || error "touch failed"
12583         check_stats $SINGLEMDS "open" 1
12584         check_stats $SINGLEMDS "close" 1
12585         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12586                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12587                 check_stats $SINGLEMDS "mknod" 2
12588         }
12589         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12590         check_stats $SINGLEMDS "unlink" 1
12591         rm -f ${testdir}/${tfile} || error "file remove failed"
12592         check_stats $SINGLEMDS "unlink" 2
12593
12594         # remove working dir and check mdt stats again.
12595         rmdir ${testdir} || error "rmdir failed"
12596         check_stats $SINGLEMDS "rmdir" 1
12597
12598         local testdir1=$DIR/${tdir}/stats_testdir1
12599         mkdir -p ${testdir}
12600         mkdir -p ${testdir1}
12601         touch ${testdir1}/test1
12602         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12603         check_stats $SINGLEMDS "crossdir_rename" 1
12604
12605         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12606         check_stats $SINGLEMDS "samedir_rename" 1
12607
12608         rm -rf $DIR/${tdir}
12609 }
12610 run_test 133a "Verifying MDT stats ========================================"
12611
12612 test_133b() {
12613         local res
12614
12615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12616         remote_ost_nodsh && skip "remote OST with nodsh"
12617         remote_mds_nodsh && skip "remote MDS with nodsh"
12618
12619         local testdir=$DIR/${tdir}/stats_testdir
12620
12621         mkdir -p ${testdir} || error "mkdir failed"
12622         touch ${testdir}/${tfile} || error "touch failed"
12623         cancel_lru_locks mdc
12624
12625         # clear stats.
12626         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12627         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12628
12629         # extra mdt stats verification.
12630         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12631         check_stats $SINGLEMDS "setattr" 1
12632         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12633         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12634         then            # LU-1740
12635                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12636                 check_stats $SINGLEMDS "getattr" 1
12637         fi
12638         rm -rf $DIR/${tdir}
12639
12640         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12641         # so the check below is not reliable
12642         [ $MDSCOUNT -eq 1 ] || return 0
12643
12644         # Sleep to avoid a cached response.
12645         #define OBD_STATFS_CACHE_SECONDS 1
12646         sleep 2
12647         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12648         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12649         $LFS df || error "lfs failed"
12650         check_stats $SINGLEMDS "statfs" 1
12651
12652         # check aggregated statfs (LU-10018)
12653         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12654                 return 0
12655         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12656                 return 0
12657         sleep 2
12658         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12659         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12660         df $DIR
12661         check_stats $SINGLEMDS "statfs" 1
12662
12663         # We want to check that the client didn't send OST_STATFS to
12664         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12665         # extra care is needed here.
12666         if remote_mds; then
12667                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12668                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12669
12670                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12671                 [ "$res" ] && error "OST got STATFS"
12672         fi
12673
12674         return 0
12675 }
12676 run_test 133b "Verifying extra MDT stats =================================="
12677
12678 test_133c() {
12679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12680         remote_ost_nodsh && skip "remote OST with nodsh"
12681         remote_mds_nodsh && skip "remote MDS with nodsh"
12682
12683         local testdir=$DIR/$tdir/stats_testdir
12684
12685         test_mkdir -p $testdir
12686
12687         # verify obdfilter stats.
12688         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12689         sync
12690         cancel_lru_locks osc
12691         wait_delete_completed
12692
12693         # clear stats.
12694         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12695         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12696
12697         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12698                 error "dd failed"
12699         sync
12700         cancel_lru_locks osc
12701         check_stats ost1 "write" 1
12702
12703         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12704         check_stats ost1 "read" 1
12705
12706         > $testdir/$tfile || error "truncate failed"
12707         check_stats ost1 "punch" 1
12708
12709         rm -f $testdir/$tfile || error "file remove failed"
12710         wait_delete_completed
12711         check_stats ost1 "destroy" 1
12712
12713         rm -rf $DIR/$tdir
12714 }
12715 run_test 133c "Verifying OST stats ========================================"
12716
12717 order_2() {
12718         local value=$1
12719         local orig=$value
12720         local order=1
12721
12722         while [ $value -ge 2 ]; do
12723                 order=$((order*2))
12724                 value=$((value/2))
12725         done
12726
12727         if [ $orig -gt $order ]; then
12728                 order=$((order*2))
12729         fi
12730         echo $order
12731 }
12732
12733 size_in_KMGT() {
12734     local value=$1
12735     local size=('K' 'M' 'G' 'T');
12736     local i=0
12737     local size_string=$value
12738
12739     while [ $value -ge 1024 ]; do
12740         if [ $i -gt 3 ]; then
12741             #T is the biggest unit we get here, if that is bigger,
12742             #just return XXXT
12743             size_string=${value}T
12744             break
12745         fi
12746         value=$((value >> 10))
12747         if [ $value -lt 1024 ]; then
12748             size_string=${value}${size[$i]}
12749             break
12750         fi
12751         i=$((i + 1))
12752     done
12753
12754     echo $size_string
12755 }
12756
12757 get_rename_size() {
12758         local size=$1
12759         local context=${2:-.}
12760         local sample=$(do_facet $SINGLEMDS $LCTL \
12761                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12762                 grep -A1 $context |
12763                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12764         echo $sample
12765 }
12766
12767 test_133d() {
12768         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12769         remote_ost_nodsh && skip "remote OST with nodsh"
12770         remote_mds_nodsh && skip "remote MDS with nodsh"
12771         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12772                 skip_env "MDS doesn't support rename stats"
12773
12774         local testdir1=$DIR/${tdir}/stats_testdir1
12775         local testdir2=$DIR/${tdir}/stats_testdir2
12776         mkdir -p $DIR/${tdir}
12777
12778         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12779
12780         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12781         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12782
12783         createmany -o $testdir1/test 512 || error "createmany failed"
12784
12785         # check samedir rename size
12786         mv ${testdir1}/test0 ${testdir1}/test_0
12787
12788         local testdir1_size=$(ls -l $DIR/${tdir} |
12789                 awk '/stats_testdir1/ {print $5}')
12790         local testdir2_size=$(ls -l $DIR/${tdir} |
12791                 awk '/stats_testdir2/ {print $5}')
12792
12793         testdir1_size=$(order_2 $testdir1_size)
12794         testdir2_size=$(order_2 $testdir2_size)
12795
12796         testdir1_size=$(size_in_KMGT $testdir1_size)
12797         testdir2_size=$(size_in_KMGT $testdir2_size)
12798
12799         echo "source rename dir size: ${testdir1_size}"
12800         echo "target rename dir size: ${testdir2_size}"
12801
12802         local cmd="do_facet $SINGLEMDS $LCTL "
12803         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12804
12805         eval $cmd || error "$cmd failed"
12806         local samedir=$($cmd | grep 'same_dir')
12807         local same_sample=$(get_rename_size $testdir1_size)
12808         [ -z "$samedir" ] && error "samedir_rename_size count error"
12809         [[ $same_sample -eq 1 ]] ||
12810                 error "samedir_rename_size error $same_sample"
12811         echo "Check same dir rename stats success"
12812
12813         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12814
12815         # check crossdir rename size
12816         mv ${testdir1}/test_0 ${testdir2}/test_0
12817
12818         testdir1_size=$(ls -l $DIR/${tdir} |
12819                 awk '/stats_testdir1/ {print $5}')
12820         testdir2_size=$(ls -l $DIR/${tdir} |
12821                 awk '/stats_testdir2/ {print $5}')
12822
12823         testdir1_size=$(order_2 $testdir1_size)
12824         testdir2_size=$(order_2 $testdir2_size)
12825
12826         testdir1_size=$(size_in_KMGT $testdir1_size)
12827         testdir2_size=$(size_in_KMGT $testdir2_size)
12828
12829         echo "source rename dir size: ${testdir1_size}"
12830         echo "target rename dir size: ${testdir2_size}"
12831
12832         eval $cmd || error "$cmd failed"
12833         local crossdir=$($cmd | grep 'crossdir')
12834         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12835         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12836         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12837         [[ $src_sample -eq 1 ]] ||
12838                 error "crossdir_rename_size error $src_sample"
12839         [[ $tgt_sample -eq 1 ]] ||
12840                 error "crossdir_rename_size error $tgt_sample"
12841         echo "Check cross dir rename stats success"
12842         rm -rf $DIR/${tdir}
12843 }
12844 run_test 133d "Verifying rename_stats ========================================"
12845
12846 test_133e() {
12847         remote_mds_nodsh && skip "remote MDS with nodsh"
12848         remote_ost_nodsh && skip "remote OST with nodsh"
12849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12850
12851         local testdir=$DIR/${tdir}/stats_testdir
12852         local ctr f0 f1 bs=32768 count=42 sum
12853
12854         mkdir -p ${testdir} || error "mkdir failed"
12855
12856         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12857
12858         for ctr in {write,read}_bytes; do
12859                 sync
12860                 cancel_lru_locks osc
12861
12862                 do_facet ost1 $LCTL set_param -n \
12863                         "obdfilter.*.exports.clear=clear"
12864
12865                 if [ $ctr = write_bytes ]; then
12866                         f0=/dev/zero
12867                         f1=${testdir}/${tfile}
12868                 else
12869                         f0=${testdir}/${tfile}
12870                         f1=/dev/null
12871                 fi
12872
12873                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12874                         error "dd failed"
12875                 sync
12876                 cancel_lru_locks osc
12877
12878                 sum=$(do_facet ost1 $LCTL get_param \
12879                         "obdfilter.*.exports.*.stats" |
12880                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12881                                 $1 == ctr { sum += $7 }
12882                                 END { printf("%0.0f", sum) }')
12883
12884                 if ((sum != bs * count)); then
12885                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12886                 fi
12887         done
12888
12889         rm -rf $DIR/${tdir}
12890 }
12891 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12892
12893 test_133f() {
12894         [[ $(lustre_version_code $facet) -ge $(version_code 2.7.65) ]] ||
12895                 skip "too old lustre for get_param -R ($facet_ver)"
12896
12897         # verifying readability.
12898         $LCTL get_param -R '*' &> /dev/null
12899
12900         # Verifing writability with badarea_io.
12901         $LCTL list_param -FR '*' | grep '=' | tr -d = |
12902                 egrep -v 'force_lbug|changelog_mask' | xargs badarea_io ||
12903                 error "client badarea_io failed"
12904
12905         # remount the FS in case writes/reads /proc break the FS
12906         cleanup || error "failed to unmount"
12907         setup || error "failed to setup"
12908 }
12909 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12910
12911 test_133g() {
12912         remote_mds_nodsh && skip "remote MDS with nodsh"
12913         remote_ost_nodsh && skip "remote OST with nodsh"
12914
12915         local facet
12916         for facet in mds1 ost1; do
12917                 local facet_ver=$(lustre_version_code $facet)
12918                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
12919                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
12920                 else
12921                         log "$facet: too old lustre for get_param -R"
12922                 fi
12923                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
12924                         do_facet $facet "$LCTL list_param -FR '*' | grep '=' |
12925                                 tr -d = | egrep -v 'force_lbug|changelog_mask' |
12926                                 xargs badarea_io" ||
12927                                         error "$facet badarea_io failed"
12928                 else
12929                         skip_noexit "$facet: too old lustre for get_param -R"
12930                 fi
12931         done
12932
12933         # remount the FS in case writes/reads /proc break the FS
12934         cleanup || error "failed to unmount"
12935         setup || error "failed to setup"
12936 }
12937 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12938
12939 test_133h() {
12940         remote_mds_nodsh && skip "remote MDS with nodsh"
12941         remote_ost_nodsh && skip "remote OST with nodsh"
12942         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12943                 skip "Need MDS version at least 2.9.54"
12944
12945         local facet
12946         for facet in client mds1 ost1; do
12947                 # Get the list of files that are missing the terminating newline
12948                 local plist=$(do_facet $facet
12949                         $LCTL list_param -FR '*' | grep '=' | tr -d =)
12950                 local ent
12951                 for ent in $plist; do
12952                         local missing=$(do_facet $facet $LCTL get_param $ent \|\
12953                                 awk -v FS='\v' -v RS='\v\v' \
12954                                 "'END { if(NR>0 && \\\$NF !~ /.*\\\n\$/) \
12955                                         print FILENAME}'" 2>/dev/null)
12956                         [ -z $missing ] || {
12957                                 do_facet $facet $LCTL get_param $ent | od -An -tx1
12958                                 error "file does not end with newline: $facet-$ent"
12959                         }
12960                 done
12961         done
12962 }
12963 run_test 133h "Proc files should end with newlines"
12964
12965 test_134a() {
12966         remote_mds_nodsh && skip "remote MDS with nodsh"
12967         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12968                 skip "Need MDS version at least 2.7.54"
12969
12970         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12971         cancel_lru_locks mdc
12972
12973         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12974         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12975         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12976
12977         local nr=1000
12978         createmany -o $DIR/$tdir/f $nr ||
12979                 error "failed to create $nr files in $DIR/$tdir"
12980         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12981
12982         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12983         do_facet mds1 $LCTL set_param fail_loc=0x327
12984         do_facet mds1 $LCTL set_param fail_val=500
12985         touch $DIR/$tdir/m
12986
12987         echo "sleep 10 seconds ..."
12988         sleep 10
12989         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
12990
12991         do_facet mds1 $LCTL set_param fail_loc=0
12992         do_facet mds1 $LCTL set_param fail_val=0
12993         [ $lck_cnt -lt $unused ] ||
12994                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
12995
12996         rm $DIR/$tdir/m
12997         unlinkmany $DIR/$tdir/f $nr
12998 }
12999 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
13000
13001 test_134b() {
13002         remote_mds_nodsh && skip "remote MDS with nodsh"
13003         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
13004                 skip "Need MDS version at least 2.7.54"
13005
13006         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13007         cancel_lru_locks mdc
13008
13009         local low_wm=$(do_facet mds1 $LCTL get_param -n \
13010                         ldlm.lock_reclaim_threshold_mb)
13011         # disable reclaim temporarily
13012         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
13013
13014         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
13015         do_facet mds1 $LCTL set_param fail_loc=0x328
13016         do_facet mds1 $LCTL set_param fail_val=500
13017
13018         $LCTL set_param debug=+trace
13019
13020         local nr=600
13021         createmany -o $DIR/$tdir/f $nr &
13022         local create_pid=$!
13023
13024         echo "Sleep $TIMEOUT seconds ..."
13025         sleep $TIMEOUT
13026         if ! ps -p $create_pid  > /dev/null 2>&1; then
13027                 do_facet mds1 $LCTL set_param fail_loc=0
13028                 do_facet mds1 $LCTL set_param fail_val=0
13029                 do_facet mds1 $LCTL set_param \
13030                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
13031                 error "createmany finished incorrectly!"
13032         fi
13033         do_facet mds1 $LCTL set_param fail_loc=0
13034         do_facet mds1 $LCTL set_param fail_val=0
13035         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
13036         wait $create_pid || return 1
13037
13038         unlinkmany $DIR/$tdir/f $nr
13039 }
13040 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
13041
13042 test_135() {
13043         remote_mds_nodsh && skip "remote MDS with nodsh"
13044         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
13045                 skip "Need MDS version at least 2.13.50"
13046         local fname
13047
13048         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13049
13050 #define OBD_FAIL_PLAIN_RECORDS 0x1319
13051         #set only one record at plain llog
13052         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
13053
13054         #fill already existed plain llog each 64767
13055         #wrapping whole catalog
13056         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
13057
13058         createmany -o $DIR/$tdir/$tfile_ 64700
13059         for (( i = 0; i < 64700; i = i + 2 ))
13060         do
13061                 rm $DIR/$tdir/$tfile_$i &
13062                 rm $DIR/$tdir/$tfile_$((i + 1)) &
13063                 local pid=$!
13064                 wait $pid
13065         done
13066
13067         #waiting osp synchronization
13068         wait_delete_completed
13069 }
13070 run_test 135 "Race catalog processing"
13071
13072 test_136() {
13073         remote_mds_nodsh && skip "remote MDS with nodsh"
13074         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
13075                 skip "Need MDS version at least 2.13.50"
13076         local fname
13077
13078         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13079         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
13080         #set only one record at plain llog
13081 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
13082         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
13083
13084         #fill already existed 2 plain llogs each 64767
13085         #wrapping whole catalog
13086         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
13087         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
13088         wait_delete_completed
13089
13090         createmany -o $DIR/$tdir/$tfile_ 10
13091         sleep 25
13092
13093         do_facet $SINGLEMDS $LCTL set_param fail_val=3
13094         for (( i = 0; i < 10; i = i + 3 ))
13095         do
13096                 rm $DIR/$tdir/$tfile_$i &
13097                 rm $DIR/$tdir/$tfile_$((i + 1)) &
13098                 local pid=$!
13099                 wait $pid
13100                 sleep 7
13101                 rm $DIR/$tdir/$tfile_$((i + 2)) &
13102         done
13103
13104         #waiting osp synchronization
13105         wait_delete_completed
13106 }
13107 run_test 136 "Race catalog processing 2"
13108
13109 test_140() { #bug-17379
13110         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13111
13112         test_mkdir $DIR/$tdir
13113         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
13114         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
13115
13116         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
13117         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
13118         local i=0
13119         while i=$((i + 1)); do
13120                 test_mkdir $i
13121                 cd $i || error "Changing to $i"
13122                 ln -s ../stat stat || error "Creating stat symlink"
13123                 # Read the symlink until ELOOP present,
13124                 # not LBUGing the system is considered success,
13125                 # we didn't overrun the stack.
13126                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
13127                 if [ $ret -ne 0 ]; then
13128                         if [ $ret -eq 40 ]; then
13129                                 break  # -ELOOP
13130                         else
13131                                 error "Open stat symlink"
13132                                         return
13133                         fi
13134                 fi
13135         done
13136         i=$((i - 1))
13137         echo "The symlink depth = $i"
13138         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
13139                 error "Invalid symlink depth"
13140
13141         # Test recursive symlink
13142         ln -s symlink_self symlink_self
13143         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
13144         echo "open symlink_self returns $ret"
13145         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
13146 }
13147 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
13148
13149 test_150a() {
13150         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13151
13152         local TF="$TMP/$tfile"
13153
13154         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13155         cp $TF $DIR/$tfile
13156         cancel_lru_locks $OSC
13157         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
13158         remount_client $MOUNT
13159         df -P $MOUNT
13160         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
13161
13162         $TRUNCATE $TF 6000
13163         $TRUNCATE $DIR/$tfile 6000
13164         cancel_lru_locks $OSC
13165         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
13166
13167         echo "12345" >>$TF
13168         echo "12345" >>$DIR/$tfile
13169         cancel_lru_locks $OSC
13170         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
13171
13172         echo "12345" >>$TF
13173         echo "12345" >>$DIR/$tfile
13174         cancel_lru_locks $OSC
13175         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
13176
13177         rm -f $TF
13178         true
13179 }
13180 run_test 150a "truncate/append tests"
13181
13182 test_150b() {
13183         [ "$ost1_FSTYPE" != ldiskfs ] && skip "non-ldiskfs backend"
13184         [ $OST1_VERSION -lt $(version_code 2.13.50) ] &&
13185                 skip "Need OST version at least 2.13.53"
13186         touch $DIR/$tfile
13187         check_fallocate $DIR/$tfile || error "fallocate failed"
13188 }
13189 run_test 150b "Verify fallocate (prealloc) functionality"
13190
13191 test_150c() {
13192         local bytes
13193         local want
13194
13195         [ "$ost1_FSTYPE" != ldiskfs ] && skip "non-ldiskfs backend"
13196         [ $OST1_VERSION -lt $(version_code 2.13.50) ] &&
13197                 skip "Need OST version at least 2.13.53"
13198
13199         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tdir || error "setstripe failed"
13200         fallocate -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
13201         sync; sync_all_data
13202         cancel_lru_locks $OSC
13203         sleep 5
13204         bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
13205         want=$((OSTCOUNT * 1048576))
13206
13207         # Must allocate all requested space, not more than 5% extra
13208         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
13209                 error "bytes $bytes is not $want"
13210 }
13211 run_test 150c "Verify fallocate Size and Blocks"
13212
13213 test_150d() {
13214         local bytes
13215         local want
13216
13217         [ "$ost1_FSTYPE" != ldiskfs ] && skip "non-ldiskfs backend"
13218         [ $OST1_VERSION -lt $(version_code 2.13.50) ] &&
13219                 skip "Need OST version at least 2.13.53"
13220
13221         $LFS setstripe -c $OSTCOUNT -S1M $DIR/$tdir || error "setstripe failed"
13222         fallocate -o 1G -l ${OSTCOUNT}m $DIR/$tdir || error "fallocate failed"
13223         sync; sync_all_data
13224         cancel_lru_locks $OSC
13225         sleep 5
13226         bytes=$(($(stat -c '%b * %B' $DIR/$tdir)))
13227         want=$((OSTCOUNT * 1048576))
13228
13229         # Must allocate all requested space, not more than 5% extra
13230         (( $bytes >= $want && $bytes < $want * 105 / 100 )) ||
13231                 error "bytes $bytes is not $want"
13232 }
13233 run_test 150d "Verify fallocate Size and Blocks - Non zero start"
13234
13235 #LU-2902 roc_hit was not able to read all values from lproc
13236 function roc_hit_init() {
13237         local list=$(comma_list $(osts_nodes))
13238         local dir=$DIR/$tdir-check
13239         local file=$dir/$tfile
13240         local BEFORE
13241         local AFTER
13242         local idx
13243
13244         test_mkdir $dir
13245         #use setstripe to do a write to every ost
13246         for i in $(seq 0 $((OSTCOUNT-1))); do
13247                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
13248                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
13249                 idx=$(printf %04x $i)
13250                 BEFORE=$(get_osd_param $list *OST*$idx stats |
13251                         awk '$1 == "cache_access" {sum += $7}
13252                                 END { printf("%0.0f", sum) }')
13253
13254                 cancel_lru_locks osc
13255                 cat $file >/dev/null
13256
13257                 AFTER=$(get_osd_param $list *OST*$idx stats |
13258                         awk '$1 == "cache_access" {sum += $7}
13259                                 END { printf("%0.0f", sum) }')
13260
13261                 echo BEFORE:$BEFORE AFTER:$AFTER
13262                 if ! let "AFTER - BEFORE == 4"; then
13263                         rm -rf $dir
13264                         error "roc_hit is not safe to use"
13265                 fi
13266                 rm $file
13267         done
13268
13269         rm -rf $dir
13270 }
13271
13272 function roc_hit() {
13273         local list=$(comma_list $(osts_nodes))
13274         echo $(get_osd_param $list '' stats |
13275                 awk '$1 == "cache_hit" {sum += $7}
13276                         END { printf("%0.0f", sum) }')
13277 }
13278
13279 function set_cache() {
13280         local on=1
13281
13282         if [ "$2" == "off" ]; then
13283                 on=0;
13284         fi
13285         local list=$(comma_list $(osts_nodes))
13286         set_osd_param $list '' $1_cache_enable $on
13287
13288         cancel_lru_locks osc
13289 }
13290
13291 test_151() {
13292         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13293         remote_ost_nodsh && skip "remote OST with nodsh"
13294
13295         local CPAGES=3
13296         local list=$(comma_list $(osts_nodes))
13297
13298         # check whether obdfilter is cache capable at all
13299         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
13300                 skip "not cache-capable obdfilter"
13301         fi
13302
13303         # check cache is enabled on all obdfilters
13304         if get_osd_param $list '' read_cache_enable | grep 0; then
13305                 skip "oss cache is disabled"
13306         fi
13307
13308         set_osd_param $list '' writethrough_cache_enable 1
13309
13310         # check write cache is enabled on all obdfilters
13311         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
13312                 skip "oss write cache is NOT enabled"
13313         fi
13314
13315         roc_hit_init
13316
13317         #define OBD_FAIL_OBD_NO_LRU  0x609
13318         do_nodes $list $LCTL set_param fail_loc=0x609
13319
13320         # pages should be in the case right after write
13321         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
13322                 error "dd failed"
13323
13324         local BEFORE=$(roc_hit)
13325         cancel_lru_locks osc
13326         cat $DIR/$tfile >/dev/null
13327         local AFTER=$(roc_hit)
13328
13329         do_nodes $list $LCTL set_param fail_loc=0
13330
13331         if ! let "AFTER - BEFORE == CPAGES"; then
13332                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13333         fi
13334
13335         cancel_lru_locks osc
13336         # invalidates OST cache
13337         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
13338         set_osd_param $list '' read_cache_enable 0
13339         cat $DIR/$tfile >/dev/null
13340
13341         # now data shouldn't be found in the cache
13342         BEFORE=$(roc_hit)
13343         cancel_lru_locks osc
13344         cat $DIR/$tfile >/dev/null
13345         AFTER=$(roc_hit)
13346         if let "AFTER - BEFORE != 0"; then
13347                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13348         fi
13349
13350         set_osd_param $list '' read_cache_enable 1
13351         rm -f $DIR/$tfile
13352 }
13353 run_test 151 "test cache on oss and controls ==============================="
13354
13355 test_152() {
13356         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13357
13358         local TF="$TMP/$tfile"
13359
13360         # simulate ENOMEM during write
13361 #define OBD_FAIL_OST_NOMEM      0x226
13362         lctl set_param fail_loc=0x80000226
13363         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13364         cp $TF $DIR/$tfile
13365         sync || error "sync failed"
13366         lctl set_param fail_loc=0
13367
13368         # discard client's cache
13369         cancel_lru_locks osc
13370
13371         # simulate ENOMEM during read
13372         lctl set_param fail_loc=0x80000226
13373         cmp $TF $DIR/$tfile || error "cmp failed"
13374         lctl set_param fail_loc=0
13375
13376         rm -f $TF
13377 }
13378 run_test 152 "test read/write with enomem ============================"
13379
13380 test_153() {
13381         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
13382 }
13383 run_test 153 "test if fdatasync does not crash ======================="
13384
13385 dot_lustre_fid_permission_check() {
13386         local fid=$1
13387         local ffid=$MOUNT/.lustre/fid/$fid
13388         local test_dir=$2
13389
13390         echo "stat fid $fid"
13391         stat $ffid > /dev/null || error "stat $ffid failed."
13392         echo "touch fid $fid"
13393         touch $ffid || error "touch $ffid failed."
13394         echo "write to fid $fid"
13395         cat /etc/hosts > $ffid || error "write $ffid failed."
13396         echo "read fid $fid"
13397         diff /etc/hosts $ffid || error "read $ffid failed."
13398         echo "append write to fid $fid"
13399         cat /etc/hosts >> $ffid || error "append write $ffid failed."
13400         echo "rename fid $fid"
13401         mv $ffid $test_dir/$tfile.1 &&
13402                 error "rename $ffid to $tfile.1 should fail."
13403         touch $test_dir/$tfile.1
13404         mv $test_dir/$tfile.1 $ffid &&
13405                 error "rename $tfile.1 to $ffid should fail."
13406         rm -f $test_dir/$tfile.1
13407         echo "truncate fid $fid"
13408         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
13409         echo "link fid $fid"
13410         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
13411         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
13412                 echo "setfacl fid $fid"
13413                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
13414                 echo "getfacl fid $fid"
13415                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
13416         fi
13417         echo "unlink fid $fid"
13418         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
13419         echo "mknod fid $fid"
13420         mknod $ffid c 1 3 && error "mknod $ffid should fail."
13421
13422         fid=[0xf00000400:0x1:0x0]
13423         ffid=$MOUNT/.lustre/fid/$fid
13424
13425         echo "stat non-exist fid $fid"
13426         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
13427         echo "write to non-exist fid $fid"
13428         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
13429         echo "link new fid $fid"
13430         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
13431
13432         mkdir -p $test_dir/$tdir
13433         touch $test_dir/$tdir/$tfile
13434         fid=$($LFS path2fid $test_dir/$tdir)
13435         rc=$?
13436         [ $rc -ne 0 ] &&
13437                 error "error: could not get fid for $test_dir/$dir/$tfile."
13438
13439         ffid=$MOUNT/.lustre/fid/$fid
13440
13441         echo "ls $fid"
13442         ls $ffid > /dev/null || error "ls $ffid failed."
13443         echo "touch $fid/$tfile.1"
13444         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
13445
13446         echo "touch $MOUNT/.lustre/fid/$tfile"
13447         touch $MOUNT/.lustre/fid/$tfile && \
13448                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
13449
13450         echo "setxattr to $MOUNT/.lustre/fid"
13451         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
13452
13453         echo "listxattr for $MOUNT/.lustre/fid"
13454         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
13455
13456         echo "delxattr from $MOUNT/.lustre/fid"
13457         setfattr -x trusted.name1 $MOUNT/.lustre/fid
13458
13459         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
13460         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
13461                 error "touch invalid fid should fail."
13462
13463         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
13464         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
13465                 error "touch non-normal fid should fail."
13466
13467         echo "rename $tdir to $MOUNT/.lustre/fid"
13468         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
13469                 error "rename to $MOUNT/.lustre/fid should fail."
13470
13471         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13472         then            # LU-3547
13473                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13474                 local new_obf_mode=777
13475
13476                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13477                 chmod $new_obf_mode $DIR/.lustre/fid ||
13478                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13479
13480                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13481                 [ $obf_mode -eq $new_obf_mode ] ||
13482                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13483
13484                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13485                 chmod $old_obf_mode $DIR/.lustre/fid ||
13486                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13487         fi
13488
13489         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13490         fid=$($LFS path2fid $test_dir/$tfile-2)
13491
13492         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13493         then # LU-5424
13494                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13495                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13496                         error "create lov data thru .lustre failed"
13497         fi
13498         echo "cp /etc/passwd $test_dir/$tfile-2"
13499         cp /etc/passwd $test_dir/$tfile-2 ||
13500                 error "copy to $test_dir/$tfile-2 failed."
13501         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13502         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13503                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13504
13505         rm -rf $test_dir/tfile.lnk
13506         rm -rf $test_dir/$tfile-2
13507 }
13508
13509 test_154A() {
13510         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13511                 skip "Need MDS version at least 2.4.1"
13512
13513         local tf=$DIR/$tfile
13514         touch $tf
13515
13516         local fid=$($LFS path2fid $tf)
13517         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13518
13519         # check that we get the same pathname back
13520         local found=$($LFS fid2path $MOUNT "$fid")
13521         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13522         [ "$found" == "$tf" ] ||
13523                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13524 }
13525 run_test 154A "lfs path2fid and fid2path basic checks"
13526
13527 test_154B() {
13528         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13529                 skip "Need MDS version at least 2.4.1"
13530
13531         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13532         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13533         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13534         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13535
13536         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13537         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13538
13539         # check that we get the same pathname
13540         echo "PFID: $PFID, name: $name"
13541         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13542         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13543         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13544                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13545
13546         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13547 }
13548 run_test 154B "verify the ll_decode_linkea tool"
13549
13550 test_154a() {
13551         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13552         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13553         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13554                 skip "Need MDS version at least 2.2.51"
13555         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13556
13557         cp /etc/hosts $DIR/$tfile
13558
13559         fid=$($LFS path2fid $DIR/$tfile)
13560         rc=$?
13561         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13562
13563         dot_lustre_fid_permission_check "$fid" $DIR ||
13564                 error "dot lustre permission check $fid failed"
13565
13566         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13567
13568         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13569
13570         touch $MOUNT/.lustre/file &&
13571                 error "creation is not allowed under .lustre"
13572
13573         mkdir $MOUNT/.lustre/dir &&
13574                 error "mkdir is not allowed under .lustre"
13575
13576         rm -rf $DIR/$tfile
13577 }
13578 run_test 154a "Open-by-FID"
13579
13580 test_154b() {
13581         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13582         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13583         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13584         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13585                 skip "Need MDS version at least 2.2.51"
13586
13587         local remote_dir=$DIR/$tdir/remote_dir
13588         local MDTIDX=1
13589         local rc=0
13590
13591         mkdir -p $DIR/$tdir
13592         $LFS mkdir -i $MDTIDX $remote_dir ||
13593                 error "create remote directory failed"
13594
13595         cp /etc/hosts $remote_dir/$tfile
13596
13597         fid=$($LFS path2fid $remote_dir/$tfile)
13598         rc=$?
13599         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13600
13601         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13602                 error "dot lustre permission check $fid failed"
13603         rm -rf $DIR/$tdir
13604 }
13605 run_test 154b "Open-by-FID for remote directory"
13606
13607 test_154c() {
13608         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13609                 skip "Need MDS version at least 2.4.1"
13610
13611         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13612         local FID1=$($LFS path2fid $DIR/$tfile.1)
13613         local FID2=$($LFS path2fid $DIR/$tfile.2)
13614         local FID3=$($LFS path2fid $DIR/$tfile.3)
13615
13616         local N=1
13617         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13618                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13619                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13620                 local want=FID$N
13621                 [ "$FID" = "${!want}" ] ||
13622                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13623                 N=$((N + 1))
13624         done
13625
13626         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13627         do
13628                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13629                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13630                 N=$((N + 1))
13631         done
13632 }
13633 run_test 154c "lfs path2fid and fid2path multiple arguments"
13634
13635 test_154d() {
13636         remote_mds_nodsh && skip "remote MDS with nodsh"
13637         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13638                 skip "Need MDS version at least 2.5.53"
13639
13640         if remote_mds; then
13641                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13642         else
13643                 nid="0@lo"
13644         fi
13645         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13646         local fd
13647         local cmd
13648
13649         rm -f $DIR/$tfile
13650         touch $DIR/$tfile
13651
13652         local fid=$($LFS path2fid $DIR/$tfile)
13653         # Open the file
13654         fd=$(free_fd)
13655         cmd="exec $fd<$DIR/$tfile"
13656         eval $cmd
13657         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13658         echo "$fid_list" | grep "$fid"
13659         rc=$?
13660
13661         cmd="exec $fd>/dev/null"
13662         eval $cmd
13663         if [ $rc -ne 0 ]; then
13664                 error "FID $fid not found in open files list $fid_list"
13665         fi
13666 }
13667 run_test 154d "Verify open file fid"
13668
13669 test_154e()
13670 {
13671         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13672                 skip "Need MDS version at least 2.6.50"
13673
13674         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13675                 error ".lustre returned by readdir"
13676         fi
13677 }
13678 run_test 154e ".lustre is not returned by readdir"
13679
13680 test_154f() {
13681         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13682
13683         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13684         test_mkdir -p -c1 $DIR/$tdir/d
13685         # test dirs inherit from its stripe
13686         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13687         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13688         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13689         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13690         touch $DIR/f
13691
13692         # get fid of parents
13693         local FID0=$($LFS path2fid $DIR/$tdir/d)
13694         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13695         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13696         local FID3=$($LFS path2fid $DIR)
13697
13698         # check that path2fid --parents returns expected <parent_fid>/name
13699         # 1) test for a directory (single parent)
13700         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13701         [ "$parent" == "$FID0/foo1" ] ||
13702                 error "expected parent: $FID0/foo1, got: $parent"
13703
13704         # 2) test for a file with nlink > 1 (multiple parents)
13705         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13706         echo "$parent" | grep -F "$FID1/$tfile" ||
13707                 error "$FID1/$tfile not returned in parent list"
13708         echo "$parent" | grep -F "$FID2/link" ||
13709                 error "$FID2/link not returned in parent list"
13710
13711         # 3) get parent by fid
13712         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13713         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13714         echo "$parent" | grep -F "$FID1/$tfile" ||
13715                 error "$FID1/$tfile not returned in parent list (by fid)"
13716         echo "$parent" | grep -F "$FID2/link" ||
13717                 error "$FID2/link not returned in parent list (by fid)"
13718
13719         # 4) test for entry in root directory
13720         parent=$($LFS path2fid --parents $DIR/f)
13721         echo "$parent" | grep -F "$FID3/f" ||
13722                 error "$FID3/f not returned in parent list"
13723
13724         # 5) test it on root directory
13725         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13726                 error "$MOUNT should not have parents"
13727
13728         # enable xattr caching and check that linkea is correctly updated
13729         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13730         save_lustre_params client "llite.*.xattr_cache" > $save
13731         lctl set_param llite.*.xattr_cache 1
13732
13733         # 6.1) linkea update on rename
13734         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13735
13736         # get parents by fid
13737         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13738         # foo1 should no longer be returned in parent list
13739         echo "$parent" | grep -F "$FID1" &&
13740                 error "$FID1 should no longer be in parent list"
13741         # the new path should appear
13742         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13743                 error "$FID2/$tfile.moved is not in parent list"
13744
13745         # 6.2) linkea update on unlink
13746         rm -f $DIR/$tdir/d/foo2/link
13747         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13748         # foo2/link should no longer be returned in parent list
13749         echo "$parent" | grep -F "$FID2/link" &&
13750                 error "$FID2/link should no longer be in parent list"
13751         true
13752
13753         rm -f $DIR/f
13754         restore_lustre_params < $save
13755         rm -f $save
13756 }
13757 run_test 154f "get parent fids by reading link ea"
13758
13759 test_154g()
13760 {
13761         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13762         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13763            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13764                 skip "Need MDS version at least 2.6.92"
13765
13766         mkdir -p $DIR/$tdir
13767         llapi_fid_test -d $DIR/$tdir
13768 }
13769 run_test 154g "various llapi FID tests"
13770
13771 test_155_small_load() {
13772     local temp=$TMP/$tfile
13773     local file=$DIR/$tfile
13774
13775     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13776         error "dd of=$temp bs=6096 count=1 failed"
13777     cp $temp $file
13778     cancel_lru_locks $OSC
13779     cmp $temp $file || error "$temp $file differ"
13780
13781     $TRUNCATE $temp 6000
13782     $TRUNCATE $file 6000
13783     cmp $temp $file || error "$temp $file differ (truncate1)"
13784
13785     echo "12345" >>$temp
13786     echo "12345" >>$file
13787     cmp $temp $file || error "$temp $file differ (append1)"
13788
13789     echo "12345" >>$temp
13790     echo "12345" >>$file
13791     cmp $temp $file || error "$temp $file differ (append2)"
13792
13793     rm -f $temp $file
13794     true
13795 }
13796
13797 test_155_big_load() {
13798         remote_ost_nodsh && skip "remote OST with nodsh"
13799
13800         local temp=$TMP/$tfile
13801         local file=$DIR/$tfile
13802
13803         free_min_max
13804         local cache_size=$(do_facet ost$((MAXI+1)) \
13805                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13806         local large_file_size=$((cache_size * 2))
13807
13808         echo "OSS cache size: $cache_size KB"
13809         echo "Large file size: $large_file_size KB"
13810
13811         [ $MAXV -le $large_file_size ] &&
13812                 skip_env "max available OST size needs > $large_file_size KB"
13813
13814         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13815
13816         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13817                 error "dd of=$temp bs=$large_file_size count=1k failed"
13818         cp $temp $file
13819         ls -lh $temp $file
13820         cancel_lru_locks osc
13821         cmp $temp $file || error "$temp $file differ"
13822
13823         rm -f $temp $file
13824         true
13825 }
13826
13827 save_writethrough() {
13828         local facets=$(get_facets OST)
13829
13830         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13831 }
13832
13833 test_155a() {
13834         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13835
13836         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13837
13838         save_writethrough $p
13839
13840         set_cache read on
13841         set_cache writethrough on
13842         test_155_small_load
13843         restore_lustre_params < $p
13844         rm -f $p
13845 }
13846 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13847
13848 test_155b() {
13849         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13850
13851         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13852
13853         save_writethrough $p
13854
13855         set_cache read on
13856         set_cache writethrough off
13857         test_155_small_load
13858         restore_lustre_params < $p
13859         rm -f $p
13860 }
13861 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13862
13863 test_155c() {
13864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13865
13866         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13867
13868         save_writethrough $p
13869
13870         set_cache read off
13871         set_cache writethrough on
13872         test_155_small_load
13873         restore_lustre_params < $p
13874         rm -f $p
13875 }
13876 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13877
13878 test_155d() {
13879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13880
13881         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13882
13883         save_writethrough $p
13884
13885         set_cache read off
13886         set_cache writethrough off
13887         test_155_small_load
13888         restore_lustre_params < $p
13889         rm -f $p
13890 }
13891 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13892
13893 test_155e() {
13894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13895
13896         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13897
13898         save_writethrough $p
13899
13900         set_cache read on
13901         set_cache writethrough on
13902         test_155_big_load
13903         restore_lustre_params < $p
13904         rm -f $p
13905 }
13906 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13907
13908 test_155f() {
13909         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13910
13911         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13912
13913         save_writethrough $p
13914
13915         set_cache read on
13916         set_cache writethrough off
13917         test_155_big_load
13918         restore_lustre_params < $p
13919         rm -f $p
13920 }
13921 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13922
13923 test_155g() {
13924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13925
13926         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13927
13928         save_writethrough $p
13929
13930         set_cache read off
13931         set_cache writethrough on
13932         test_155_big_load
13933         restore_lustre_params < $p
13934         rm -f $p
13935 }
13936 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13937
13938 test_155h() {
13939         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13940
13941         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13942
13943         save_writethrough $p
13944
13945         set_cache read off
13946         set_cache writethrough off
13947         test_155_big_load
13948         restore_lustre_params < $p
13949         rm -f $p
13950 }
13951 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13952
13953 test_156() {
13954         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13955         remote_ost_nodsh && skip "remote OST with nodsh"
13956         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13957                 skip "stats not implemented on old servers"
13958         [ "$ost1_FSTYPE" = "zfs" ] &&
13959                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13960
13961         local CPAGES=3
13962         local BEFORE
13963         local AFTER
13964         local file="$DIR/$tfile"
13965         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13966
13967         save_writethrough $p
13968         roc_hit_init
13969
13970         log "Turn on read and write cache"
13971         set_cache read on
13972         set_cache writethrough on
13973
13974         log "Write data and read it back."
13975         log "Read should be satisfied from the cache."
13976         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13977         BEFORE=$(roc_hit)
13978         cancel_lru_locks osc
13979         cat $file >/dev/null
13980         AFTER=$(roc_hit)
13981         if ! let "AFTER - BEFORE == CPAGES"; then
13982                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13983         else
13984                 log "cache hits: before: $BEFORE, after: $AFTER"
13985         fi
13986
13987         log "Read again; it should be satisfied from the cache."
13988         BEFORE=$AFTER
13989         cancel_lru_locks osc
13990         cat $file >/dev/null
13991         AFTER=$(roc_hit)
13992         if ! let "AFTER - BEFORE == CPAGES"; then
13993                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13994         else
13995                 log "cache hits:: before: $BEFORE, after: $AFTER"
13996         fi
13997
13998         log "Turn off the read cache and turn on the write cache"
13999         set_cache read off
14000         set_cache writethrough on
14001
14002         log "Read again; it should be satisfied from the cache."
14003         BEFORE=$(roc_hit)
14004         cancel_lru_locks osc
14005         cat $file >/dev/null
14006         AFTER=$(roc_hit)
14007         if ! let "AFTER - BEFORE == CPAGES"; then
14008                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
14009         else
14010                 log "cache hits:: before: $BEFORE, after: $AFTER"
14011         fi
14012
14013         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
14014                 # > 2.12.56 uses pagecache if cached
14015                 log "Read again; it should not be satisfied from the cache."
14016                 BEFORE=$AFTER
14017                 cancel_lru_locks osc
14018                 cat $file >/dev/null
14019                 AFTER=$(roc_hit)
14020                 if ! let "AFTER - BEFORE == 0"; then
14021                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
14022                 else
14023                         log "cache hits:: before: $BEFORE, after: $AFTER"
14024                 fi
14025         fi
14026
14027         log "Write data and read it back."
14028         log "Read should be satisfied from the cache."
14029         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14030         BEFORE=$(roc_hit)
14031         cancel_lru_locks osc
14032         cat $file >/dev/null
14033         AFTER=$(roc_hit)
14034         if ! let "AFTER - BEFORE == CPAGES"; then
14035                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
14036         else
14037                 log "cache hits:: before: $BEFORE, after: $AFTER"
14038         fi
14039
14040         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
14041                 # > 2.12.56 uses pagecache if cached
14042                 log "Read again; it should not be satisfied from the cache."
14043                 BEFORE=$AFTER
14044                 cancel_lru_locks osc
14045                 cat $file >/dev/null
14046                 AFTER=$(roc_hit)
14047                 if ! let "AFTER - BEFORE == 0"; then
14048                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
14049                 else
14050                         log "cache hits:: before: $BEFORE, after: $AFTER"
14051                 fi
14052         fi
14053
14054         log "Turn off read and write cache"
14055         set_cache read off
14056         set_cache writethrough off
14057
14058         log "Write data and read it back"
14059         log "It should not be satisfied from the cache."
14060         rm -f $file
14061         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14062         cancel_lru_locks osc
14063         BEFORE=$(roc_hit)
14064         cat $file >/dev/null
14065         AFTER=$(roc_hit)
14066         if ! let "AFTER - BEFORE == 0"; then
14067                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
14068         else
14069                 log "cache hits:: before: $BEFORE, after: $AFTER"
14070         fi
14071
14072         log "Turn on the read cache and turn off the write cache"
14073         set_cache read on
14074         set_cache writethrough off
14075
14076         log "Write data and read it back"
14077         log "It should not be satisfied from the cache."
14078         rm -f $file
14079         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14080         BEFORE=$(roc_hit)
14081         cancel_lru_locks osc
14082         cat $file >/dev/null
14083         AFTER=$(roc_hit)
14084         if ! let "AFTER - BEFORE == 0"; then
14085                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
14086         else
14087                 log "cache hits:: before: $BEFORE, after: $AFTER"
14088         fi
14089
14090         log "Read again; it should be satisfied from the cache."
14091         BEFORE=$(roc_hit)
14092         cancel_lru_locks osc
14093         cat $file >/dev/null
14094         AFTER=$(roc_hit)
14095         if ! let "AFTER - BEFORE == CPAGES"; then
14096                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
14097         else
14098                 log "cache hits:: before: $BEFORE, after: $AFTER"
14099         fi
14100
14101         restore_lustre_params < $p
14102         rm -f $p $file
14103 }
14104 run_test 156 "Verification of tunables"
14105
14106 test_160a() {
14107         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14108         remote_mds_nodsh && skip "remote MDS with nodsh"
14109         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
14110                 skip "Need MDS version at least 2.2.0"
14111
14112         changelog_register || error "changelog_register failed"
14113         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14114         changelog_users $SINGLEMDS | grep -q $cl_user ||
14115                 error "User $cl_user not found in changelog_users"
14116
14117         # change something
14118         test_mkdir -p $DIR/$tdir/pics/2008/zachy
14119         changelog_clear 0 || error "changelog_clear failed"
14120         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
14121         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
14122         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14123         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14124         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14125         rm $DIR/$tdir/pics/desktop.jpg
14126
14127         changelog_dump | tail -10
14128
14129         echo "verifying changelog mask"
14130         changelog_chmask "-MKDIR"
14131         changelog_chmask "-CLOSE"
14132
14133         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
14134         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
14135
14136         changelog_chmask "+MKDIR"
14137         changelog_chmask "+CLOSE"
14138
14139         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
14140         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
14141
14142         changelog_dump | tail -10
14143         MKDIRS=$(changelog_dump | grep -c "MKDIR")
14144         CLOSES=$(changelog_dump | grep -c "CLOSE")
14145         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
14146         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
14147
14148         # verify contents
14149         echo "verifying target fid"
14150         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
14151         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
14152         [ "$fidc" == "$fidf" ] ||
14153                 error "changelog '$tfile' fid $fidc != file fid $fidf"
14154         echo "verifying parent fid"
14155         # The FID returned from the Changelog may be the directory shard on
14156         # a different MDT, and not the FID returned by path2fid on the parent.
14157         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
14158         # since this is what will matter when recreating this file in the tree.
14159         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
14160         local pathp=$($LFS fid2path $MOUNT "$fidp")
14161         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
14162                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
14163
14164         echo "getting records for $cl_user"
14165         changelog_users $SINGLEMDS
14166         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
14167         local nclr=3
14168         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
14169                 error "changelog_clear failed"
14170         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
14171         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
14172         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
14173                 error "user index expect $user_rec1 + $nclr != $user_rec2"
14174
14175         local min0_rec=$(changelog_users $SINGLEMDS |
14176                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
14177         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
14178                           awk '{ print $1; exit; }')
14179
14180         changelog_dump | tail -n 5
14181         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
14182         [ $first_rec == $((min0_rec + 1)) ] ||
14183                 error "first index should be $min0_rec + 1 not $first_rec"
14184
14185         # LU-3446 changelog index reset on MDT restart
14186         local cur_rec1=$(changelog_users $SINGLEMDS |
14187                          awk '/^current.index:/ { print $NF }')
14188         changelog_clear 0 ||
14189                 error "clear all changelog records for $cl_user failed"
14190         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
14191         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
14192                 error "Fail to start $SINGLEMDS"
14193         local cur_rec2=$(changelog_users $SINGLEMDS |
14194                          awk '/^current.index:/ { print $NF }')
14195         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
14196         [ $cur_rec1 == $cur_rec2 ] ||
14197                 error "current index should be $cur_rec1 not $cur_rec2"
14198
14199         echo "verifying users from this test are deregistered"
14200         changelog_deregister || error "changelog_deregister failed"
14201         changelog_users $SINGLEMDS | grep -q $cl_user &&
14202                 error "User '$cl_user' still in changelog_users"
14203
14204         # lctl get_param -n mdd.*.changelog_users
14205         # current index: 144
14206         # ID    index (idle seconds)
14207         # cl3   144 (2)
14208         if ! changelog_users $SINGLEMDS | grep "^cl"; then
14209                 # this is the normal case where all users were deregistered
14210                 # make sure no new records are added when no users are present
14211                 local last_rec1=$(changelog_users $SINGLEMDS |
14212                                   awk '/^current.index:/ { print $NF }')
14213                 touch $DIR/$tdir/chloe
14214                 local last_rec2=$(changelog_users $SINGLEMDS |
14215                                   awk '/^current.index:/ { print $NF }')
14216                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
14217                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
14218         else
14219                 # any changelog users must be leftovers from a previous test
14220                 changelog_users $SINGLEMDS
14221                 echo "other changelog users; can't verify off"
14222         fi
14223 }
14224 run_test 160a "changelog sanity"
14225
14226 test_160b() { # LU-3587
14227         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14228         remote_mds_nodsh && skip "remote MDS with nodsh"
14229         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
14230                 skip "Need MDS version at least 2.2.0"
14231
14232         changelog_register || error "changelog_register failed"
14233         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14234         changelog_users $SINGLEMDS | grep -q $cl_user ||
14235                 error "User '$cl_user' not found in changelog_users"
14236
14237         local longname1=$(str_repeat a 255)
14238         local longname2=$(str_repeat b 255)
14239
14240         cd $DIR
14241         echo "creating very long named file"
14242         touch $longname1 || error "create of '$longname1' failed"
14243         echo "renaming very long named file"
14244         mv $longname1 $longname2
14245
14246         changelog_dump | grep RENME | tail -n 5
14247         rm -f $longname2
14248 }
14249 run_test 160b "Verify that very long rename doesn't crash in changelog"
14250
14251 test_160c() {
14252         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14253         remote_mds_nodsh && skip "remote MDS with nodsh"
14254
14255         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
14256                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
14257                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
14258                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
14259
14260         local rc=0
14261
14262         # Registration step
14263         changelog_register || error "changelog_register failed"
14264
14265         rm -rf $DIR/$tdir
14266         mkdir -p $DIR/$tdir
14267         $MCREATE $DIR/$tdir/foo_160c
14268         changelog_chmask "-TRUNC"
14269         $TRUNCATE $DIR/$tdir/foo_160c 200
14270         changelog_chmask "+TRUNC"
14271         $TRUNCATE $DIR/$tdir/foo_160c 199
14272         changelog_dump | tail -n 5
14273         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
14274         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
14275 }
14276 run_test 160c "verify that changelog log catch the truncate event"
14277
14278 test_160d() {
14279         remote_mds_nodsh && skip "remote MDS with nodsh"
14280         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14282         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
14283                 skip "Need MDS version at least 2.7.60"
14284
14285         # Registration step
14286         changelog_register || error "changelog_register failed"
14287
14288         mkdir -p $DIR/$tdir/migrate_dir
14289         changelog_clear 0 || error "changelog_clear failed"
14290
14291         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
14292         changelog_dump | tail -n 5
14293         local migrates=$(changelog_dump | grep -c "MIGRT")
14294         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
14295 }
14296 run_test 160d "verify that changelog log catch the migrate event"
14297
14298 test_160e() {
14299         remote_mds_nodsh && skip "remote MDS with nodsh"
14300
14301         # Create a user
14302         changelog_register || error "changelog_register failed"
14303
14304         # Delete a future user (expect fail)
14305         local MDT0=$(facet_svc $SINGLEMDS)
14306         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
14307         local rc=$?
14308
14309         if [ $rc -eq 0 ]; then
14310                 error "Deleted non-existant user cl77"
14311         elif [ $rc -ne 2 ]; then
14312                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
14313         fi
14314
14315         # Clear to a bad index (1 billion should be safe)
14316         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
14317         rc=$?
14318
14319         if [ $rc -eq 0 ]; then
14320                 error "Successfully cleared to invalid CL index"
14321         elif [ $rc -ne 22 ]; then
14322                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
14323         fi
14324 }
14325 run_test 160e "changelog negative testing (should return errors)"
14326
14327 test_160f() {
14328         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14329         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14330                 skip "Need MDS version at least 2.10.56"
14331
14332         local mdts=$(comma_list $(mdts_nodes))
14333
14334         # Create a user
14335         changelog_register || error "first changelog_register failed"
14336         changelog_register || error "second changelog_register failed"
14337         local cl_users
14338         declare -A cl_user1
14339         declare -A cl_user2
14340         local user_rec1
14341         local user_rec2
14342         local i
14343
14344         # generate some changelog records to accumulate on each MDT
14345         # use fnv1a because created files should be evenly distributed
14346         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14347                 error "test_mkdir $tdir failed"
14348         log "$(date +%s): creating first files"
14349         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14350                 error "create $DIR/$tdir/$tfile failed"
14351
14352         # check changelogs have been generated
14353         local start=$SECONDS
14354         local idle_time=$((MDSCOUNT * 5 + 5))
14355         local nbcl=$(changelog_dump | wc -l)
14356         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14357
14358         for param in "changelog_max_idle_time=$idle_time" \
14359                      "changelog_gc=1" \
14360                      "changelog_min_gc_interval=2" \
14361                      "changelog_min_free_cat_entries=3"; do
14362                 local MDT0=$(facet_svc $SINGLEMDS)
14363                 local var="${param%=*}"
14364                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14365
14366                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14367                 do_nodes $mdts $LCTL set_param mdd.*.$param
14368         done
14369
14370         # force cl_user2 to be idle (1st part), but also cancel the
14371         # cl_user1 records so that it is not evicted later in the test.
14372         local sleep1=$((idle_time / 2))
14373         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
14374         sleep $sleep1
14375
14376         # simulate changelog catalog almost full
14377         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14378         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14379
14380         for i in $(seq $MDSCOUNT); do
14381                 cl_users=(${CL_USERS[mds$i]})
14382                 cl_user1[mds$i]="${cl_users[0]}"
14383                 cl_user2[mds$i]="${cl_users[1]}"
14384
14385                 [ -n "${cl_user1[mds$i]}" ] ||
14386                         error "mds$i: no user registered"
14387                 [ -n "${cl_user2[mds$i]}" ] ||
14388                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14389
14390                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14391                 [ -n "$user_rec1" ] ||
14392                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14393                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14394                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14395                 [ -n "$user_rec2" ] ||
14396                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14397                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14398                      "$user_rec1 + 2 == $user_rec2"
14399                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14400                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14401                               "$user_rec1 + 2, but is $user_rec2"
14402                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14403                 [ -n "$user_rec2" ] ||
14404                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14405                 [ $user_rec1 == $user_rec2 ] ||
14406                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14407                               "$user_rec1, but is $user_rec2"
14408         done
14409
14410         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
14411         local sleep2=$((idle_time - (SECONDS - start) + 1))
14412         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
14413         sleep $sleep2
14414
14415         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
14416         # cl_user1 should be OK because it recently processed records.
14417         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
14418         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
14419                 error "create $DIR/$tdir/${tfile}b failed"
14420
14421         # ensure gc thread is done
14422         for i in $(mdts_nodes); do
14423                 wait_update $i \
14424                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14425                         error "$i: GC-thread not done"
14426         done
14427
14428         local first_rec
14429         for i in $(seq $MDSCOUNT); do
14430                 # check cl_user1 still registered
14431                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14432                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14433                 # check cl_user2 unregistered
14434                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14435                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14436
14437                 # check changelogs are present and starting at $user_rec1 + 1
14438                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14439                 [ -n "$user_rec1" ] ||
14440                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14441                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14442                             awk '{ print $1; exit; }')
14443
14444                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14445                 [ $((user_rec1 + 1)) == $first_rec ] ||
14446                         error "mds$i: first index should be $user_rec1 + 1, " \
14447                               "but is $first_rec"
14448         done
14449 }
14450 run_test 160f "changelog garbage collect (timestamped users)"
14451
14452 test_160g() {
14453         remote_mds_nodsh && skip "remote MDS with nodsh"
14454         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14455                 skip "Need MDS version at least 2.10.56"
14456
14457         local mdts=$(comma_list $(mdts_nodes))
14458
14459         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
14460         do_nodes $mdts $LCTL set_param fail_loc=0x1314
14461
14462         # Create a user
14463         changelog_register || error "first changelog_register failed"
14464         changelog_register || error "second changelog_register failed"
14465         local cl_users
14466         declare -A cl_user1
14467         declare -A cl_user2
14468         local user_rec1
14469         local user_rec2
14470         local i
14471
14472         # generate some changelog records to accumulate on each MDT
14473         # use fnv1a because created files should be evenly distributed
14474         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14475                 error "mkdir $tdir failed"
14476         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14477                 error "create $DIR/$tdir/$tfile failed"
14478
14479         # check changelogs have been generated
14480         local nbcl=$(changelog_dump | wc -l)
14481         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14482
14483         # reduce the max_idle_indexes value to make sure we exceed it
14484         max_ndx=$((nbcl / 2 - 1))
14485
14486         for param in "changelog_max_idle_indexes=$max_ndx" \
14487                      "changelog_gc=1" \
14488                      "changelog_min_gc_interval=2" \
14489                      "changelog_min_free_cat_entries=3"; do
14490                 local MDT0=$(facet_svc $SINGLEMDS)
14491                 local var="${param%=*}"
14492                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14493
14494                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14495                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14496                         error "unable to set mdd.*.$param"
14497         done
14498
14499         # simulate changelog catalog almost full
14500         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14501         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14502
14503         for i in $(seq $MDSCOUNT); do
14504                 cl_users=(${CL_USERS[mds$i]})
14505                 cl_user1[mds$i]="${cl_users[0]}"
14506                 cl_user2[mds$i]="${cl_users[1]}"
14507
14508                 [ -n "${cl_user1[mds$i]}" ] ||
14509                         error "mds$i: no user registered"
14510                 [ -n "${cl_user2[mds$i]}" ] ||
14511                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14512
14513                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14514                 [ -n "$user_rec1" ] ||
14515                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14516                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14517                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14518                 [ -n "$user_rec2" ] ||
14519                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14520                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14521                      "$user_rec1 + 2 == $user_rec2"
14522                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14523                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14524                               "$user_rec1 + 2, but is $user_rec2"
14525                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14526                 [ -n "$user_rec2" ] ||
14527                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14528                 [ $user_rec1 == $user_rec2 ] ||
14529                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14530                               "$user_rec1, but is $user_rec2"
14531         done
14532
14533         # ensure we are past the previous changelog_min_gc_interval set above
14534         sleep 2
14535
14536         # generate one more changelog to trigger fail_loc
14537         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14538                 error "create $DIR/$tdir/${tfile}bis failed"
14539
14540         # ensure gc thread is done
14541         for i in $(mdts_nodes); do
14542                 wait_update $i \
14543                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14544                         error "$i: GC-thread not done"
14545         done
14546
14547         local first_rec
14548         for i in $(seq $MDSCOUNT); do
14549                 # check cl_user1 still registered
14550                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14551                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14552                 # check cl_user2 unregistered
14553                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14554                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14555
14556                 # check changelogs are present and starting at $user_rec1 + 1
14557                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14558                 [ -n "$user_rec1" ] ||
14559                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14560                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14561                             awk '{ print $1; exit; }')
14562
14563                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14564                 [ $((user_rec1 + 1)) == $first_rec ] ||
14565                         error "mds$i: first index should be $user_rec1 + 1, " \
14566                               "but is $first_rec"
14567         done
14568 }
14569 run_test 160g "changelog garbage collect (old users)"
14570
14571 test_160h() {
14572         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14573         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14574                 skip "Need MDS version at least 2.10.56"
14575
14576         local mdts=$(comma_list $(mdts_nodes))
14577
14578         # Create a user
14579         changelog_register || error "first changelog_register failed"
14580         changelog_register || error "second changelog_register failed"
14581         local cl_users
14582         declare -A cl_user1
14583         declare -A cl_user2
14584         local user_rec1
14585         local user_rec2
14586         local i
14587
14588         # generate some changelog records to accumulate on each MDT
14589         # use fnv1a because created files should be evenly distributed
14590         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14591                 error "test_mkdir $tdir failed"
14592         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14593                 error "create $DIR/$tdir/$tfile failed"
14594
14595         # check changelogs have been generated
14596         local nbcl=$(changelog_dump | wc -l)
14597         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14598
14599         for param in "changelog_max_idle_time=10" \
14600                      "changelog_gc=1" \
14601                      "changelog_min_gc_interval=2"; do
14602                 local MDT0=$(facet_svc $SINGLEMDS)
14603                 local var="${param%=*}"
14604                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14605
14606                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14607                 do_nodes $mdts $LCTL set_param mdd.*.$param
14608         done
14609
14610         # force cl_user2 to be idle (1st part)
14611         sleep 9
14612
14613         for i in $(seq $MDSCOUNT); do
14614                 cl_users=(${CL_USERS[mds$i]})
14615                 cl_user1[mds$i]="${cl_users[0]}"
14616                 cl_user2[mds$i]="${cl_users[1]}"
14617
14618                 [ -n "${cl_user1[mds$i]}" ] ||
14619                         error "mds$i: no user registered"
14620                 [ -n "${cl_user2[mds$i]}" ] ||
14621                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14622
14623                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14624                 [ -n "$user_rec1" ] ||
14625                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14626                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14627                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14628                 [ -n "$user_rec2" ] ||
14629                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14630                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14631                      "$user_rec1 + 2 == $user_rec2"
14632                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14633                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14634                               "$user_rec1 + 2, but is $user_rec2"
14635                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14636                 [ -n "$user_rec2" ] ||
14637                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14638                 [ $user_rec1 == $user_rec2 ] ||
14639                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14640                               "$user_rec1, but is $user_rec2"
14641         done
14642
14643         # force cl_user2 to be idle (2nd part) and to reach
14644         # changelog_max_idle_time
14645         sleep 2
14646
14647         # force each GC-thread start and block then
14648         # one per MDT/MDD, set fail_val accordingly
14649         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14650         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14651
14652         # generate more changelogs to trigger fail_loc
14653         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14654                 error "create $DIR/$tdir/${tfile}bis failed"
14655
14656         # stop MDT to stop GC-thread, should be done in back-ground as it will
14657         # block waiting for the thread to be released and exit
14658         declare -A stop_pids
14659         for i in $(seq $MDSCOUNT); do
14660                 stop mds$i &
14661                 stop_pids[mds$i]=$!
14662         done
14663
14664         for i in $(mdts_nodes); do
14665                 local facet
14666                 local nb=0
14667                 local facets=$(facets_up_on_host $i)
14668
14669                 for facet in ${facets//,/ }; do
14670                         if [[ $facet == mds* ]]; then
14671                                 nb=$((nb + 1))
14672                         fi
14673                 done
14674                 # ensure each MDS's gc threads are still present and all in "R"
14675                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14676                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14677                         error "$i: expected $nb GC-thread"
14678                 wait_update $i \
14679                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14680                         "R" 20 ||
14681                         error "$i: GC-thread not found in R-state"
14682                 # check umounts of each MDT on MDS have reached kthread_stop()
14683                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14684                         error "$i: expected $nb umount"
14685                 wait_update $i \
14686                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14687                         error "$i: umount not found in D-state"
14688         done
14689
14690         # release all GC-threads
14691         do_nodes $mdts $LCTL set_param fail_loc=0
14692
14693         # wait for MDT stop to complete
14694         for i in $(seq $MDSCOUNT); do
14695                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14696         done
14697
14698         # XXX
14699         # may try to check if any orphan changelog records are present
14700         # via ldiskfs/zfs and llog_reader...
14701
14702         # re-start/mount MDTs
14703         for i in $(seq $MDSCOUNT); do
14704                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14705                         error "Fail to start mds$i"
14706         done
14707
14708         local first_rec
14709         for i in $(seq $MDSCOUNT); do
14710                 # check cl_user1 still registered
14711                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14712                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14713                 # check cl_user2 unregistered
14714                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14715                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14716
14717                 # check changelogs are present and starting at $user_rec1 + 1
14718                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14719                 [ -n "$user_rec1" ] ||
14720                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14721                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14722                             awk '{ print $1; exit; }')
14723
14724                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14725                 [ $((user_rec1 + 1)) == $first_rec ] ||
14726                         error "mds$i: first index should be $user_rec1 + 1, " \
14727                               "but is $first_rec"
14728         done
14729 }
14730 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14731               "during mount"
14732
14733 test_160i() {
14734
14735         local mdts=$(comma_list $(mdts_nodes))
14736
14737         changelog_register || error "first changelog_register failed"
14738
14739         # generate some changelog records to accumulate on each MDT
14740         # use fnv1a because created files should be evenly distributed
14741         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14742                 error "mkdir $tdir failed"
14743         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14744                 error "create $DIR/$tdir/$tfile failed"
14745
14746         # check changelogs have been generated
14747         local nbcl=$(changelog_dump | wc -l)
14748         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14749
14750         # simulate race between register and unregister
14751         # XXX as fail_loc is set per-MDS, with DNE configs the race
14752         # simulation will only occur for one MDT per MDS and for the
14753         # others the normal race scenario will take place
14754         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14755         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14756         do_nodes $mdts $LCTL set_param fail_val=1
14757
14758         # unregister 1st user
14759         changelog_deregister &
14760         local pid1=$!
14761         # wait some time for deregister work to reach race rdv
14762         sleep 2
14763         # register 2nd user
14764         changelog_register || error "2nd user register failed"
14765
14766         wait $pid1 || error "1st user deregister failed"
14767
14768         local i
14769         local last_rec
14770         declare -A LAST_REC
14771         for i in $(seq $MDSCOUNT); do
14772                 if changelog_users mds$i | grep "^cl"; then
14773                         # make sure new records are added with one user present
14774                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14775                                           awk '/^current.index:/ { print $NF }')
14776                 else
14777                         error "mds$i has no user registered"
14778                 fi
14779         done
14780
14781         # generate more changelog records to accumulate on each MDT
14782         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14783                 error "create $DIR/$tdir/${tfile}bis failed"
14784
14785         for i in $(seq $MDSCOUNT); do
14786                 last_rec=$(changelog_users $SINGLEMDS |
14787                            awk '/^current.index:/ { print $NF }')
14788                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14789                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14790                         error "changelogs are off on mds$i"
14791         done
14792 }
14793 run_test 160i "changelog user register/unregister race"
14794
14795 test_160j() {
14796         remote_mds_nodsh && skip "remote MDS with nodsh"
14797         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14798                 skip "Need MDS version at least 2.12.56"
14799
14800         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14801         stack_trap "umount $MOUNT2" EXIT
14802
14803         changelog_register || error "first changelog_register failed"
14804         stack_trap "changelog_deregister" EXIT
14805
14806         # generate some changelog
14807         # use fnv1a because created files should be evenly distributed
14808         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14809                 error "mkdir $tdir failed"
14810         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14811                 error "create $DIR/$tdir/${tfile}bis failed"
14812
14813         # open the changelog device
14814         exec 3>/dev/changelog-$FSNAME-MDT0000
14815         stack_trap "exec 3>&-" EXIT
14816         exec 4</dev/changelog-$FSNAME-MDT0000
14817         stack_trap "exec 4<&-" EXIT
14818
14819         # umount the first lustre mount
14820         umount $MOUNT
14821         stack_trap "mount_client $MOUNT" EXIT
14822
14823         # read changelog
14824         cat <&4 >/dev/null || error "read changelog failed"
14825
14826         # clear changelog
14827         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14828         changelog_users $SINGLEMDS | grep -q $cl_user ||
14829                 error "User $cl_user not found in changelog_users"
14830
14831         printf 'clear:'$cl_user':0' >&3
14832 }
14833 run_test 160j "client can be umounted  while its chanangelog is being used"
14834
14835 test_160k() {
14836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14837         remote_mds_nodsh && skip "remote MDS with nodsh"
14838
14839         mkdir -p $DIR/$tdir/1/1
14840
14841         changelog_register || error "changelog_register failed"
14842         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14843
14844         changelog_users $SINGLEMDS | grep -q $cl_user ||
14845                 error "User '$cl_user' not found in changelog_users"
14846 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14847         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14848         rmdir $DIR/$tdir/1/1 & sleep 1
14849         mkdir $DIR/$tdir/2
14850         touch $DIR/$tdir/2/2
14851         rm -rf $DIR/$tdir/2
14852
14853         wait
14854         sleep 4
14855
14856         changelog_dump | grep rmdir || error "rmdir not recorded"
14857
14858         rm -rf $DIR/$tdir
14859         changelog_deregister
14860 }
14861 run_test 160k "Verify that changelog records are not lost"
14862
14863 test_161a() {
14864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14865
14866         test_mkdir -c1 $DIR/$tdir
14867         cp /etc/hosts $DIR/$tdir/$tfile
14868         test_mkdir -c1 $DIR/$tdir/foo1
14869         test_mkdir -c1 $DIR/$tdir/foo2
14870         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14871         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14872         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14873         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14874         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14875         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14876                 $LFS fid2path $DIR $FID
14877                 error "bad link ea"
14878         fi
14879         # middle
14880         rm $DIR/$tdir/foo2/zachary
14881         # last
14882         rm $DIR/$tdir/foo2/thor
14883         # first
14884         rm $DIR/$tdir/$tfile
14885         # rename
14886         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14887         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14888                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14889         rm $DIR/$tdir/foo2/maggie
14890
14891         # overflow the EA
14892         local longname=$tfile.avg_len_is_thirty_two_
14893         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14894                 error_noexit 'failed to unlink many hardlinks'" EXIT
14895         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14896                 error "failed to hardlink many files"
14897         links=$($LFS fid2path $DIR $FID | wc -l)
14898         echo -n "${links}/1000 links in link EA"
14899         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14900 }
14901 run_test 161a "link ea sanity"
14902
14903 test_161b() {
14904         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14905         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14906
14907         local MDTIDX=1
14908         local remote_dir=$DIR/$tdir/remote_dir
14909
14910         mkdir -p $DIR/$tdir
14911         $LFS mkdir -i $MDTIDX $remote_dir ||
14912                 error "create remote directory failed"
14913
14914         cp /etc/hosts $remote_dir/$tfile
14915         mkdir -p $remote_dir/foo1
14916         mkdir -p $remote_dir/foo2
14917         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14918         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14919         ln $remote_dir/$tfile $remote_dir/foo1/luna
14920         ln $remote_dir/$tfile $remote_dir/foo2/thor
14921
14922         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14923                      tr -d ']')
14924         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14925                 $LFS fid2path $DIR $FID
14926                 error "bad link ea"
14927         fi
14928         # middle
14929         rm $remote_dir/foo2/zachary
14930         # last
14931         rm $remote_dir/foo2/thor
14932         # first
14933         rm $remote_dir/$tfile
14934         # rename
14935         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14936         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14937         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14938                 $LFS fid2path $DIR $FID
14939                 error "bad link rename"
14940         fi
14941         rm $remote_dir/foo2/maggie
14942
14943         # overflow the EA
14944         local longname=filename_avg_len_is_thirty_two_
14945         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14946                 error "failed to hardlink many files"
14947         links=$($LFS fid2path $DIR $FID | wc -l)
14948         echo -n "${links}/1000 links in link EA"
14949         [[ ${links} -gt 60 ]] ||
14950                 error "expected at least 60 links in link EA"
14951         unlinkmany $remote_dir/foo2/$longname 1000 ||
14952         error "failed to unlink many hardlinks"
14953 }
14954 run_test 161b "link ea sanity under remote directory"
14955
14956 test_161c() {
14957         remote_mds_nodsh && skip "remote MDS with nodsh"
14958         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14959         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14960                 skip "Need MDS version at least 2.1.5"
14961
14962         # define CLF_RENAME_LAST 0x0001
14963         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14964         changelog_register || error "changelog_register failed"
14965
14966         rm -rf $DIR/$tdir
14967         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14968         touch $DIR/$tdir/foo_161c
14969         touch $DIR/$tdir/bar_161c
14970         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14971         changelog_dump | grep RENME | tail -n 5
14972         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14973         changelog_clear 0 || error "changelog_clear failed"
14974         if [ x$flags != "x0x1" ]; then
14975                 error "flag $flags is not 0x1"
14976         fi
14977
14978         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14979         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14980         touch $DIR/$tdir/foo_161c
14981         touch $DIR/$tdir/bar_161c
14982         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14983         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14984         changelog_dump | grep RENME | tail -n 5
14985         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14986         changelog_clear 0 || error "changelog_clear failed"
14987         if [ x$flags != "x0x0" ]; then
14988                 error "flag $flags is not 0x0"
14989         fi
14990         echo "rename overwrite a target having nlink > 1," \
14991                 "changelog record has flags of $flags"
14992
14993         # rename doesn't overwrite a target (changelog flag 0x0)
14994         touch $DIR/$tdir/foo_161c
14995         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14996         changelog_dump | grep RENME | tail -n 5
14997         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14998         changelog_clear 0 || error "changelog_clear failed"
14999         if [ x$flags != "x0x0" ]; then
15000                 error "flag $flags is not 0x0"
15001         fi
15002         echo "rename doesn't overwrite a target," \
15003                 "changelog record has flags of $flags"
15004
15005         # define CLF_UNLINK_LAST 0x0001
15006         # unlink a file having nlink = 1 (changelog flag 0x1)
15007         rm -f $DIR/$tdir/foo2_161c
15008         changelog_dump | grep UNLNK | tail -n 5
15009         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
15010         changelog_clear 0 || error "changelog_clear failed"
15011         if [ x$flags != "x0x1" ]; then
15012                 error "flag $flags is not 0x1"
15013         fi
15014         echo "unlink a file having nlink = 1," \
15015                 "changelog record has flags of $flags"
15016
15017         # unlink a file having nlink > 1 (changelog flag 0x0)
15018         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
15019         rm -f $DIR/$tdir/foobar_161c
15020         changelog_dump | grep UNLNK | tail -n 5
15021         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
15022         changelog_clear 0 || error "changelog_clear failed"
15023         if [ x$flags != "x0x0" ]; then
15024                 error "flag $flags is not 0x0"
15025         fi
15026         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
15027 }
15028 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
15029
15030 test_161d() {
15031         remote_mds_nodsh && skip "remote MDS with nodsh"
15032         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
15033
15034         local pid
15035         local fid
15036
15037         changelog_register || error "changelog_register failed"
15038
15039         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
15040         # interfer with $MOUNT/.lustre/fid/ access
15041         mkdir $DIR/$tdir
15042         [[ $? -eq 0 ]] || error "mkdir failed"
15043
15044         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
15045         $LCTL set_param fail_loc=0x8000140c
15046         # 5s pause
15047         $LCTL set_param fail_val=5
15048
15049         # create file
15050         echo foofoo > $DIR/$tdir/$tfile &
15051         pid=$!
15052
15053         # wait for create to be delayed
15054         sleep 2
15055
15056         ps -p $pid
15057         [[ $? -eq 0 ]] || error "create should be blocked"
15058
15059         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
15060         stack_trap "rm -f $tempfile"
15061         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
15062         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
15063         # some delay may occur during ChangeLog publishing and file read just
15064         # above, that could allow file write to happen finally
15065         [[ -s $tempfile ]] && echo "file should be empty"
15066
15067         $LCTL set_param fail_loc=0
15068
15069         wait $pid
15070         [[ $? -eq 0 ]] || error "create failed"
15071 }
15072 run_test 161d "create with concurrent .lustre/fid access"
15073
15074 check_path() {
15075         local expected="$1"
15076         shift
15077         local fid="$2"
15078
15079         local path
15080         path=$($LFS fid2path "$@")
15081         local rc=$?
15082
15083         if [ $rc -ne 0 ]; then
15084                 error "path looked up of '$expected' failed: rc=$rc"
15085         elif [ "$path" != "$expected" ]; then
15086                 error "path looked up '$path' instead of '$expected'"
15087         else
15088                 echo "FID '$fid' resolves to path '$path' as expected"
15089         fi
15090 }
15091
15092 test_162a() { # was test_162
15093         test_mkdir -p -c1 $DIR/$tdir/d2
15094         touch $DIR/$tdir/d2/$tfile
15095         touch $DIR/$tdir/d2/x1
15096         touch $DIR/$tdir/d2/x2
15097         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
15098         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
15099         # regular file
15100         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
15101         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
15102
15103         # softlink
15104         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
15105         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
15106         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
15107
15108         # softlink to wrong file
15109         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
15110         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
15111         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
15112
15113         # hardlink
15114         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
15115         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
15116         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
15117         # fid2path dir/fsname should both work
15118         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
15119         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
15120
15121         # hardlink count: check that there are 2 links
15122         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
15123         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
15124
15125         # hardlink indexing: remove the first link
15126         rm $DIR/$tdir/d2/p/q/r/hlink
15127         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
15128 }
15129 run_test 162a "path lookup sanity"
15130
15131 test_162b() {
15132         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15133         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15134
15135         mkdir $DIR/$tdir
15136         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
15137                                 error "create striped dir failed"
15138
15139         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
15140                                         tail -n 1 | awk '{print $2}')
15141         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
15142
15143         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
15144         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
15145
15146         # regular file
15147         for ((i=0;i<5;i++)); do
15148                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
15149                         error "get fid for f$i failed"
15150                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
15151
15152                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
15153                         error "get fid for d$i failed"
15154                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
15155         done
15156
15157         return 0
15158 }
15159 run_test 162b "striped directory path lookup sanity"
15160
15161 # LU-4239: Verify fid2path works with paths 100 or more directories deep
15162 test_162c() {
15163         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
15164                 skip "Need MDS version at least 2.7.51"
15165
15166         local lpath=$tdir.local
15167         local rpath=$tdir.remote
15168
15169         test_mkdir $DIR/$lpath
15170         test_mkdir $DIR/$rpath
15171
15172         for ((i = 0; i <= 101; i++)); do
15173                 lpath="$lpath/$i"
15174                 mkdir $DIR/$lpath
15175                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
15176                         error "get fid for local directory $DIR/$lpath failed"
15177                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
15178
15179                 rpath="$rpath/$i"
15180                 test_mkdir $DIR/$rpath
15181                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
15182                         error "get fid for remote directory $DIR/$rpath failed"
15183                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
15184         done
15185
15186         return 0
15187 }
15188 run_test 162c "fid2path works with paths 100 or more directories deep"
15189
15190 oalr_event_count() {
15191         local event="${1}"
15192         local trace="${2}"
15193
15194         awk -v name="${FSNAME}-OST0000" \
15195             -v event="${event}" \
15196             '$1 == "TRACE" && $2 == event && $3 == name' \
15197             "${trace}" |
15198         wc -l
15199 }
15200
15201 oalr_expect_event_count() {
15202         local event="${1}"
15203         local trace="${2}"
15204         local expect="${3}"
15205         local count
15206
15207         count=$(oalr_event_count "${event}" "${trace}")
15208         if ((count == expect)); then
15209                 return 0
15210         fi
15211
15212         error_noexit "${event} event count was '${count}', expected ${expect}"
15213         cat "${trace}" >&2
15214         exit 1
15215 }
15216
15217 cleanup_165() {
15218         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
15219         stop ost1
15220         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
15221 }
15222
15223 setup_165() {
15224         sync # Flush previous IOs so we can count log entries.
15225         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
15226         stack_trap cleanup_165 EXIT
15227 }
15228
15229 test_165a() {
15230         local trace="/tmp/${tfile}.trace"
15231         local rc
15232         local count
15233
15234         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15235         setup_165
15236         sleep 5
15237
15238         do_facet ost1 ofd_access_log_reader --list
15239         stop ost1
15240
15241         do_facet ost1 killall -TERM ofd_access_log_reader
15242         wait
15243         rc=$?
15244
15245         if ((rc != 0)); then
15246                 error "ofd_access_log_reader exited with rc = '${rc}'"
15247         fi
15248
15249         # Parse trace file for discovery events:
15250         oalr_expect_event_count alr_log_add "${trace}" 1
15251         oalr_expect_event_count alr_log_eof "${trace}" 1
15252         oalr_expect_event_count alr_log_free "${trace}" 1
15253 }
15254 run_test 165a "ofd access log discovery"
15255
15256 test_165b() {
15257         local trace="/tmp/${tfile}.trace"
15258         local file="${DIR}/${tfile}"
15259         local pfid1
15260         local pfid2
15261         local -a entry
15262         local rc
15263         local count
15264         local size
15265         local flags
15266
15267         setup_165
15268
15269         lfs setstripe -c 1 -i 0 "${file}"
15270         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15271         do_facet ost1 ofd_access_log_reader --list
15272
15273         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15274         sleep 5
15275         do_facet ost1 killall -TERM ofd_access_log_reader
15276         wait
15277         rc=$?
15278
15279         if ((rc != 0)); then
15280                 error "ofd_access_log_reader exited with rc = '${rc}'"
15281         fi
15282
15283         oalr_expect_event_count alr_log_entry "${trace}" 1
15284
15285         pfid1=$($LFS path2fid "${file}")
15286
15287         # 1     2             3   4    5     6   7    8    9     10
15288         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
15289         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
15290
15291         echo "entry = '${entry[*]}'" >&2
15292
15293         pfid2=${entry[4]}
15294         if [[ "${pfid1}" != "${pfid2}" ]]; then
15295                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
15296         fi
15297
15298         size=${entry[8]}
15299         if ((size != 1048576)); then
15300                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
15301         fi
15302
15303         flags=${entry[10]}
15304         if [[ "${flags}" != "w" ]]; then
15305                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
15306         fi
15307
15308         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15309         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c || error "cannot read '${file}'"
15310         sleep 5
15311         do_facet ost1 killall -TERM ofd_access_log_reader
15312         wait
15313         rc=$?
15314
15315         if ((rc != 0)); then
15316                 error "ofd_access_log_reader exited with rc = '${rc}'"
15317         fi
15318
15319         oalr_expect_event_count alr_log_entry "${trace}" 1
15320
15321         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
15322         echo "entry = '${entry[*]}'" >&2
15323
15324         pfid2=${entry[4]}
15325         if [[ "${pfid1}" != "${pfid2}" ]]; then
15326                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
15327         fi
15328
15329         size=${entry[8]}
15330         if ((size != 524288)); then
15331                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
15332         fi
15333
15334         flags=${entry[10]}
15335         if [[ "${flags}" != "r" ]]; then
15336                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
15337         fi
15338 }
15339 run_test 165b "ofd access log entries are produced and consumed"
15340
15341 test_165c() {
15342         local file="${DIR}/${tdir}/${tfile}"
15343         test_mkdir "${DIR}/${tdir}"
15344
15345         setup_165
15346
15347         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
15348
15349         # 4096 / 64 = 64. Create twice as many entries.
15350         for ((i = 0; i < 128; i++)); do
15351                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c || error "cannot create file"
15352         done
15353
15354         sync
15355         do_facet ost1 ofd_access_log_reader --list
15356         unlinkmany  "${file}-%d" 128
15357 }
15358 run_test 165c "full ofd access logs do not block IOs"
15359
15360 oal_peek_entry_count() {
15361         do_facet ost1 ofd_access_log_reader --list | awk '$1 == "_entry_count:" { print $2; }'
15362 }
15363
15364 oal_expect_entry_count() {
15365         local entry_count=$(oal_peek_entry_count)
15366         local expect="$1"
15367
15368         if ((entry_count == expect)); then
15369                 return 0
15370         fi
15371
15372         error_noexit "bad entry count, got ${entry_count}, expected ${expect}"
15373         do_facet ost1 ofd_access_log_reader --list >&2
15374         exit 1
15375 }
15376
15377 test_165d() {
15378         local trace="/tmp/${tfile}.trace"
15379         local file="${DIR}/${tdir}/${tfile}"
15380         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
15381         local entry_count
15382         test_mkdir "${DIR}/${tdir}"
15383
15384         setup_165
15385         lfs setstripe -c 1 -i 0 "${file}"
15386
15387         do_facet ost1 lctl set_param "${param}=rw"
15388         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15389         oal_expect_entry_count 1
15390
15391         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15392         oal_expect_entry_count 2
15393
15394         do_facet ost1 lctl set_param "${param}=r"
15395         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15396         oal_expect_entry_count 2
15397
15398         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15399         oal_expect_entry_count 3
15400
15401         do_facet ost1 lctl set_param "${param}=w"
15402         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15403         oal_expect_entry_count 4
15404
15405         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15406         oal_expect_entry_count 4
15407
15408         do_facet ost1 lctl set_param "${param}=0"
15409         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15410         oal_expect_entry_count 4
15411
15412         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15413         oal_expect_entry_count 4
15414 }
15415 run_test 165d "ofd_access_log mask works"
15416
15417 test_169() {
15418         # do directio so as not to populate the page cache
15419         log "creating a 10 Mb file"
15420         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15421         log "starting reads"
15422         dd if=$DIR/$tfile of=/dev/null bs=4096 &
15423         log "truncating the file"
15424         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15425         log "killing dd"
15426         kill %+ || true # reads might have finished
15427         echo "wait until dd is finished"
15428         wait
15429         log "removing the temporary file"
15430         rm -rf $DIR/$tfile || error "tmp file removal failed"
15431 }
15432 run_test 169 "parallel read and truncate should not deadlock"
15433
15434 test_170() {
15435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15436
15437         $LCTL clear     # bug 18514
15438         $LCTL debug_daemon start $TMP/${tfile}_log_good
15439         touch $DIR/$tfile
15440         $LCTL debug_daemon stop
15441         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
15442                 error "sed failed to read log_good"
15443
15444         $LCTL debug_daemon start $TMP/${tfile}_log_good
15445         rm -rf $DIR/$tfile
15446         $LCTL debug_daemon stop
15447
15448         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
15449                error "lctl df log_bad failed"
15450
15451         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15452         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15453
15454         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
15455         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
15456
15457         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
15458                 error "bad_line good_line1 good_line2 are empty"
15459
15460         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15461         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
15462         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15463
15464         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
15465         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15466         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15467
15468         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
15469                 error "bad_line_new good_line_new are empty"
15470
15471         local expected_good=$((good_line1 + good_line2*2))
15472
15473         rm -f $TMP/${tfile}*
15474         # LU-231, short malformed line may not be counted into bad lines
15475         if [ $bad_line -ne $bad_line_new ] &&
15476                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
15477                 error "expected $bad_line bad lines, but got $bad_line_new"
15478                 return 1
15479         fi
15480
15481         if [ $expected_good -ne $good_line_new ]; then
15482                 error "expected $expected_good good lines, but got $good_line_new"
15483                 return 2
15484         fi
15485         true
15486 }
15487 run_test 170 "test lctl df to handle corrupted log ====================="
15488
15489 test_171() { # bug20592
15490         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15491
15492         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
15493         $LCTL set_param fail_loc=0x50e
15494         $LCTL set_param fail_val=3000
15495         multiop_bg_pause $DIR/$tfile O_s || true
15496         local MULTIPID=$!
15497         kill -USR1 $MULTIPID
15498         # cause log dump
15499         sleep 3
15500         wait $MULTIPID
15501         if dmesg | grep "recursive fault"; then
15502                 error "caught a recursive fault"
15503         fi
15504         $LCTL set_param fail_loc=0
15505         true
15506 }
15507 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
15508
15509 # it would be good to share it with obdfilter-survey/iokit-libecho code
15510 setup_obdecho_osc () {
15511         local rc=0
15512         local ost_nid=$1
15513         local obdfilter_name=$2
15514         echo "Creating new osc for $obdfilter_name on $ost_nid"
15515         # make sure we can find loopback nid
15516         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
15517
15518         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
15519                            ${obdfilter_name}_osc_UUID || rc=2; }
15520         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
15521                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
15522         return $rc
15523 }
15524
15525 cleanup_obdecho_osc () {
15526         local obdfilter_name=$1
15527         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
15528         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
15529         return 0
15530 }
15531
15532 obdecho_test() {
15533         local OBD=$1
15534         local node=$2
15535         local pages=${3:-64}
15536         local rc=0
15537         local id
15538
15539         local count=10
15540         local obd_size=$(get_obd_size $node $OBD)
15541         local page_size=$(get_page_size $node)
15542         if [[ -n "$obd_size" ]]; then
15543                 local new_count=$((obd_size / (pages * page_size / 1024)))
15544                 [[ $new_count -ge $count ]] || count=$new_count
15545         fi
15546
15547         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
15548         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
15549                            rc=2; }
15550         if [ $rc -eq 0 ]; then
15551             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
15552             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
15553         fi
15554         echo "New object id is $id"
15555         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
15556                            rc=4; }
15557         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
15558                            "test_brw $count w v $pages $id" || rc=4; }
15559         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
15560                            rc=4; }
15561         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
15562                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
15563         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
15564                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
15565         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
15566         return $rc
15567 }
15568
15569 test_180a() {
15570         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15571
15572         if ! module_loaded obdecho; then
15573                 load_module obdecho/obdecho &&
15574                         stack_trap "rmmod obdecho" EXIT ||
15575                         error "unable to load obdecho on client"
15576         fi
15577
15578         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
15579         local host=$($LCTL get_param -n osc.$osc.import |
15580                      awk '/current_connection:/ { print $2 }' )
15581         local target=$($LCTL get_param -n osc.$osc.import |
15582                        awk '/target:/ { print $2 }' )
15583         target=${target%_UUID}
15584
15585         if [ -n "$target" ]; then
15586                 setup_obdecho_osc $host $target &&
15587                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
15588                         { error "obdecho setup failed with $?"; return; }
15589
15590                 obdecho_test ${target}_osc client ||
15591                         error "obdecho_test failed on ${target}_osc"
15592         else
15593                 $LCTL get_param osc.$osc.import
15594                 error "there is no osc.$osc.import target"
15595         fi
15596 }
15597 run_test 180a "test obdecho on osc"
15598
15599 test_180b() {
15600         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15601         remote_ost_nodsh && skip "remote OST with nodsh"
15602
15603         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15604                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15605                 error "failed to load module obdecho"
15606
15607         local target=$(do_facet ost1 $LCTL dl |
15608                        awk '/obdfilter/ { print $4; exit; }')
15609
15610         if [ -n "$target" ]; then
15611                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
15612         else
15613                 do_facet ost1 $LCTL dl
15614                 error "there is no obdfilter target on ost1"
15615         fi
15616 }
15617 run_test 180b "test obdecho directly on obdfilter"
15618
15619 test_180c() { # LU-2598
15620         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15621         remote_ost_nodsh && skip "remote OST with nodsh"
15622         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
15623                 skip "Need MDS version at least 2.4.0"
15624
15625         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15626                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15627                 error "failed to load module obdecho"
15628
15629         local target=$(do_facet ost1 $LCTL dl |
15630                        awk '/obdfilter/ { print $4; exit; }')
15631
15632         if [ -n "$target" ]; then
15633                 local pages=16384 # 64MB bulk I/O RPC size
15634
15635                 obdecho_test "$target" ost1 "$pages" ||
15636                         error "obdecho_test with pages=$pages failed with $?"
15637         else
15638                 do_facet ost1 $LCTL dl
15639                 error "there is no obdfilter target on ost1"
15640         fi
15641 }
15642 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
15643
15644 test_181() { # bug 22177
15645         test_mkdir $DIR/$tdir
15646         # create enough files to index the directory
15647         createmany -o $DIR/$tdir/foobar 4000
15648         # print attributes for debug purpose
15649         lsattr -d .
15650         # open dir
15651         multiop_bg_pause $DIR/$tdir D_Sc || return 1
15652         MULTIPID=$!
15653         # remove the files & current working dir
15654         unlinkmany $DIR/$tdir/foobar 4000
15655         rmdir $DIR/$tdir
15656         kill -USR1 $MULTIPID
15657         wait $MULTIPID
15658         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
15659         return 0
15660 }
15661 run_test 181 "Test open-unlinked dir ========================"
15662
15663 test_182() {
15664         local fcount=1000
15665         local tcount=10
15666
15667         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15668
15669         $LCTL set_param mdc.*.rpc_stats=clear
15670
15671         for (( i = 0; i < $tcount; i++ )) ; do
15672                 mkdir $DIR/$tdir/$i
15673         done
15674
15675         for (( i = 0; i < $tcount; i++ )) ; do
15676                 createmany -o $DIR/$tdir/$i/f- $fcount &
15677         done
15678         wait
15679
15680         for (( i = 0; i < $tcount; i++ )) ; do
15681                 unlinkmany $DIR/$tdir/$i/f- $fcount &
15682         done
15683         wait
15684
15685         $LCTL get_param mdc.*.rpc_stats
15686
15687         rm -rf $DIR/$tdir
15688 }
15689 run_test 182 "Test parallel modify metadata operations ================"
15690
15691 test_183() { # LU-2275
15692         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15693         remote_mds_nodsh && skip "remote MDS with nodsh"
15694         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
15695                 skip "Need MDS version at least 2.3.56"
15696
15697         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15698         echo aaa > $DIR/$tdir/$tfile
15699
15700 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
15701         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
15702
15703         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
15704         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
15705
15706         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15707
15708         # Flush negative dentry cache
15709         touch $DIR/$tdir/$tfile
15710
15711         # We are not checking for any leaked references here, they'll
15712         # become evident next time we do cleanup with module unload.
15713         rm -rf $DIR/$tdir
15714 }
15715 run_test 183 "No crash or request leak in case of strange dispositions ========"
15716
15717 # test suite 184 is for LU-2016, LU-2017
15718 test_184a() {
15719         check_swap_layouts_support
15720
15721         dir0=$DIR/$tdir/$testnum
15722         test_mkdir -p -c1 $dir0
15723         ref1=/etc/passwd
15724         ref2=/etc/group
15725         file1=$dir0/f1
15726         file2=$dir0/f2
15727         $LFS setstripe -c1 $file1
15728         cp $ref1 $file1
15729         $LFS setstripe -c2 $file2
15730         cp $ref2 $file2
15731         gen1=$($LFS getstripe -g $file1)
15732         gen2=$($LFS getstripe -g $file2)
15733
15734         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15735         gen=$($LFS getstripe -g $file1)
15736         [[ $gen1 != $gen ]] ||
15737                 "Layout generation on $file1 does not change"
15738         gen=$($LFS getstripe -g $file2)
15739         [[ $gen2 != $gen ]] ||
15740                 "Layout generation on $file2 does not change"
15741
15742         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15743         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15744
15745         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15746 }
15747 run_test 184a "Basic layout swap"
15748
15749 test_184b() {
15750         check_swap_layouts_support
15751
15752         dir0=$DIR/$tdir/$testnum
15753         mkdir -p $dir0 || error "creating dir $dir0"
15754         file1=$dir0/f1
15755         file2=$dir0/f2
15756         file3=$dir0/f3
15757         dir1=$dir0/d1
15758         dir2=$dir0/d2
15759         mkdir $dir1 $dir2
15760         $LFS setstripe -c1 $file1
15761         $LFS setstripe -c2 $file2
15762         $LFS setstripe -c1 $file3
15763         chown $RUNAS_ID $file3
15764         gen1=$($LFS getstripe -g $file1)
15765         gen2=$($LFS getstripe -g $file2)
15766
15767         $LFS swap_layouts $dir1 $dir2 &&
15768                 error "swap of directories layouts should fail"
15769         $LFS swap_layouts $dir1 $file1 &&
15770                 error "swap of directory and file layouts should fail"
15771         $RUNAS $LFS swap_layouts $file1 $file2 &&
15772                 error "swap of file we cannot write should fail"
15773         $LFS swap_layouts $file1 $file3 &&
15774                 error "swap of file with different owner should fail"
15775         /bin/true # to clear error code
15776 }
15777 run_test 184b "Forbidden layout swap (will generate errors)"
15778
15779 test_184c() {
15780         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15781         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15782         check_swap_layouts_support
15783
15784         local dir0=$DIR/$tdir/$testnum
15785         mkdir -p $dir0 || error "creating dir $dir0"
15786
15787         local ref1=$dir0/ref1
15788         local ref2=$dir0/ref2
15789         local file1=$dir0/file1
15790         local file2=$dir0/file2
15791         # create a file large enough for the concurrent test
15792         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15793         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15794         echo "ref file size: ref1($(stat -c %s $ref1))," \
15795              "ref2($(stat -c %s $ref2))"
15796
15797         cp $ref2 $file2
15798         dd if=$ref1 of=$file1 bs=16k &
15799         local DD_PID=$!
15800
15801         # Make sure dd starts to copy file
15802         while [ ! -f $file1 ]; do sleep 0.1; done
15803
15804         $LFS swap_layouts $file1 $file2
15805         local rc=$?
15806         wait $DD_PID
15807         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15808         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15809
15810         # how many bytes copied before swapping layout
15811         local copied=$(stat -c %s $file2)
15812         local remaining=$(stat -c %s $ref1)
15813         remaining=$((remaining - copied))
15814         echo "Copied $copied bytes before swapping layout..."
15815
15816         cmp -n $copied $file1 $ref2 | grep differ &&
15817                 error "Content mismatch [0, $copied) of ref2 and file1"
15818         cmp -n $copied $file2 $ref1 ||
15819                 error "Content mismatch [0, $copied) of ref1 and file2"
15820         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15821                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15822
15823         # clean up
15824         rm -f $ref1 $ref2 $file1 $file2
15825 }
15826 run_test 184c "Concurrent write and layout swap"
15827
15828 test_184d() {
15829         check_swap_layouts_support
15830         [ -z "$(which getfattr 2>/dev/null)" ] &&
15831                 skip_env "no getfattr command"
15832
15833         local file1=$DIR/$tdir/$tfile-1
15834         local file2=$DIR/$tdir/$tfile-2
15835         local file3=$DIR/$tdir/$tfile-3
15836         local lovea1
15837         local lovea2
15838
15839         mkdir -p $DIR/$tdir
15840         touch $file1 || error "create $file1 failed"
15841         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15842                 error "create $file2 failed"
15843         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15844                 error "create $file3 failed"
15845         lovea1=$(get_layout_param $file1)
15846
15847         $LFS swap_layouts $file2 $file3 ||
15848                 error "swap $file2 $file3 layouts failed"
15849         $LFS swap_layouts $file1 $file2 ||
15850                 error "swap $file1 $file2 layouts failed"
15851
15852         lovea2=$(get_layout_param $file2)
15853         echo "$lovea1"
15854         echo "$lovea2"
15855         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15856
15857         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15858         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15859 }
15860 run_test 184d "allow stripeless layouts swap"
15861
15862 test_184e() {
15863         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15864                 skip "Need MDS version at least 2.6.94"
15865         check_swap_layouts_support
15866         [ -z "$(which getfattr 2>/dev/null)" ] &&
15867                 skip_env "no getfattr command"
15868
15869         local file1=$DIR/$tdir/$tfile-1
15870         local file2=$DIR/$tdir/$tfile-2
15871         local file3=$DIR/$tdir/$tfile-3
15872         local lovea
15873
15874         mkdir -p $DIR/$tdir
15875         touch $file1 || error "create $file1 failed"
15876         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15877                 error "create $file2 failed"
15878         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15879                 error "create $file3 failed"
15880
15881         $LFS swap_layouts $file1 $file2 ||
15882                 error "swap $file1 $file2 layouts failed"
15883
15884         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15885         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15886
15887         echo 123 > $file1 || error "Should be able to write into $file1"
15888
15889         $LFS swap_layouts $file1 $file3 ||
15890                 error "swap $file1 $file3 layouts failed"
15891
15892         echo 123 > $file1 || error "Should be able to write into $file1"
15893
15894         rm -rf $file1 $file2 $file3
15895 }
15896 run_test 184e "Recreate layout after stripeless layout swaps"
15897
15898 test_184f() {
15899         # Create a file with name longer than sizeof(struct stat) ==
15900         # 144 to see if we can get chars from the file name to appear
15901         # in the returned striping. Note that 'f' == 0x66.
15902         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15903
15904         mkdir -p $DIR/$tdir
15905         mcreate $DIR/$tdir/$file
15906         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15907                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15908         fi
15909 }
15910 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15911
15912 test_185() { # LU-2441
15913         # LU-3553 - no volatile file support in old servers
15914         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15915                 skip "Need MDS version at least 2.3.60"
15916
15917         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15918         touch $DIR/$tdir/spoo
15919         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15920         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15921                 error "cannot create/write a volatile file"
15922         [ "$FILESET" == "" ] &&
15923         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15924                 error "FID is still valid after close"
15925
15926         multiop_bg_pause $DIR/$tdir vVw4096_c
15927         local multi_pid=$!
15928
15929         local OLD_IFS=$IFS
15930         IFS=":"
15931         local fidv=($fid)
15932         IFS=$OLD_IFS
15933         # assume that the next FID for this client is sequential, since stdout
15934         # is unfortunately eaten by multiop_bg_pause
15935         local n=$((${fidv[1]} + 1))
15936         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15937         if [ "$FILESET" == "" ]; then
15938                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15939                         error "FID is missing before close"
15940         fi
15941         kill -USR1 $multi_pid
15942         # 1 second delay, so if mtime change we will see it
15943         sleep 1
15944         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15945         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15946 }
15947 run_test 185 "Volatile file support"
15948
15949 function create_check_volatile() {
15950         local idx=$1
15951         local tgt
15952
15953         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15954         local PID=$!
15955         sleep 1
15956         local FID=$(cat /tmp/${tfile}.fid)
15957         [ "$FID" == "" ] && error "can't get FID for volatile"
15958         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15959         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15960         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15961         kill -USR1 $PID
15962         wait
15963         sleep 1
15964         cancel_lru_locks mdc # flush opencache
15965         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15966         return 0
15967 }
15968
15969 test_185a(){
15970         # LU-12516 - volatile creation via .lustre
15971         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15972                 skip "Need MDS version at least 2.3.55"
15973
15974         create_check_volatile 0
15975         [ $MDSCOUNT -lt 2 ] && return 0
15976
15977         # DNE case
15978         create_check_volatile 1
15979
15980         return 0
15981 }
15982 run_test 185a "Volatile file creation in .lustre/fid/"
15983
15984 test_187a() {
15985         remote_mds_nodsh && skip "remote MDS with nodsh"
15986         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15987                 skip "Need MDS version at least 2.3.0"
15988
15989         local dir0=$DIR/$tdir/$testnum
15990         mkdir -p $dir0 || error "creating dir $dir0"
15991
15992         local file=$dir0/file1
15993         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15994         local dv1=$($LFS data_version $file)
15995         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15996         local dv2=$($LFS data_version $file)
15997         [[ $dv1 != $dv2 ]] ||
15998                 error "data version did not change on write $dv1 == $dv2"
15999
16000         # clean up
16001         rm -f $file1
16002 }
16003 run_test 187a "Test data version change"
16004
16005 test_187b() {
16006         remote_mds_nodsh && skip "remote MDS with nodsh"
16007         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
16008                 skip "Need MDS version at least 2.3.0"
16009
16010         local dir0=$DIR/$tdir/$testnum
16011         mkdir -p $dir0 || error "creating dir $dir0"
16012
16013         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
16014         [[ ${DV[0]} != ${DV[1]} ]] ||
16015                 error "data version did not change on write"\
16016                       " ${DV[0]} == ${DV[1]}"
16017
16018         # clean up
16019         rm -f $file1
16020 }
16021 run_test 187b "Test data version change on volatile file"
16022
16023 test_200() {
16024         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16025         remote_mgs_nodsh && skip "remote MGS with nodsh"
16026         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
16027
16028         local POOL=${POOL:-cea1}
16029         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
16030         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
16031         # Pool OST targets
16032         local first_ost=0
16033         local last_ost=$(($OSTCOUNT - 1))
16034         local ost_step=2
16035         local ost_list=$(seq $first_ost $ost_step $last_ost)
16036         local ost_range="$first_ost $last_ost $ost_step"
16037         local test_path=$POOL_ROOT/$POOL_DIR_NAME
16038         local file_dir=$POOL_ROOT/file_tst
16039         local subdir=$test_path/subdir
16040         local rc=0
16041
16042         while : ; do
16043                 # former test_200a test_200b
16044                 pool_add $POOL                          || { rc=$? ; break; }
16045                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
16046                 # former test_200c test_200d
16047                 mkdir -p $test_path
16048                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
16049                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
16050                 mkdir -p $subdir
16051                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
16052                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
16053                                                         || { rc=$? ; break; }
16054                 # former test_200e test_200f
16055                 local files=$((OSTCOUNT*3))
16056                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
16057                                                         || { rc=$? ; break; }
16058                 pool_create_files $POOL $file_dir $files "$ost_list" \
16059                                                         || { rc=$? ; break; }
16060                 # former test_200g test_200h
16061                 pool_lfs_df $POOL                       || { rc=$? ; break; }
16062                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
16063
16064                 # former test_201a test_201b test_201c
16065                 pool_remove_first_target $POOL          || { rc=$? ; break; }
16066
16067                 local f=$test_path/$tfile
16068                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
16069                 pool_remove $POOL $f                    || { rc=$? ; break; }
16070                 break
16071         done
16072
16073         destroy_test_pools
16074
16075         return $rc
16076 }
16077 run_test 200 "OST pools"
16078
16079 # usage: default_attr <count | size | offset>
16080 default_attr() {
16081         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
16082 }
16083
16084 # usage: check_default_stripe_attr
16085 check_default_stripe_attr() {
16086         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
16087         case $1 in
16088         --stripe-count|-c)
16089                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
16090         --stripe-size|-S)
16091                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
16092         --stripe-index|-i)
16093                 EXPECTED=-1;;
16094         *)
16095                 error "unknown getstripe attr '$1'"
16096         esac
16097
16098         [ $ACTUAL == $EXPECTED ] ||
16099                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
16100 }
16101
16102 test_204a() {
16103         test_mkdir $DIR/$tdir
16104         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
16105
16106         check_default_stripe_attr --stripe-count
16107         check_default_stripe_attr --stripe-size
16108         check_default_stripe_attr --stripe-index
16109 }
16110 run_test 204a "Print default stripe attributes"
16111
16112 test_204b() {
16113         test_mkdir $DIR/$tdir
16114         $LFS setstripe --stripe-count 1 $DIR/$tdir
16115
16116         check_default_stripe_attr --stripe-size
16117         check_default_stripe_attr --stripe-index
16118 }
16119 run_test 204b "Print default stripe size and offset"
16120
16121 test_204c() {
16122         test_mkdir $DIR/$tdir
16123         $LFS setstripe --stripe-size 65536 $DIR/$tdir
16124
16125         check_default_stripe_attr --stripe-count
16126         check_default_stripe_attr --stripe-index
16127 }
16128 run_test 204c "Print default stripe count and offset"
16129
16130 test_204d() {
16131         test_mkdir $DIR/$tdir
16132         $LFS setstripe --stripe-index 0 $DIR/$tdir
16133
16134         check_default_stripe_attr --stripe-count
16135         check_default_stripe_attr --stripe-size
16136 }
16137 run_test 204d "Print default stripe count and size"
16138
16139 test_204e() {
16140         test_mkdir $DIR/$tdir
16141         $LFS setstripe -d $DIR/$tdir
16142
16143         check_default_stripe_attr --stripe-count --raw
16144         check_default_stripe_attr --stripe-size --raw
16145         check_default_stripe_attr --stripe-index --raw
16146 }
16147 run_test 204e "Print raw stripe attributes"
16148
16149 test_204f() {
16150         test_mkdir $DIR/$tdir
16151         $LFS setstripe --stripe-count 1 $DIR/$tdir
16152
16153         check_default_stripe_attr --stripe-size --raw
16154         check_default_stripe_attr --stripe-index --raw
16155 }
16156 run_test 204f "Print raw stripe size and offset"
16157
16158 test_204g() {
16159         test_mkdir $DIR/$tdir
16160         $LFS setstripe --stripe-size 65536 $DIR/$tdir
16161
16162         check_default_stripe_attr --stripe-count --raw
16163         check_default_stripe_attr --stripe-index --raw
16164 }
16165 run_test 204g "Print raw stripe count and offset"
16166
16167 test_204h() {
16168         test_mkdir $DIR/$tdir
16169         $LFS setstripe --stripe-index 0 $DIR/$tdir
16170
16171         check_default_stripe_attr --stripe-count --raw
16172         check_default_stripe_attr --stripe-size --raw
16173 }
16174 run_test 204h "Print raw stripe count and size"
16175
16176 # Figure out which job scheduler is being used, if any,
16177 # or use a fake one
16178 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
16179         JOBENV=SLURM_JOB_ID
16180 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
16181         JOBENV=LSB_JOBID
16182 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
16183         JOBENV=PBS_JOBID
16184 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
16185         JOBENV=LOADL_STEP_ID
16186 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
16187         JOBENV=JOB_ID
16188 else
16189         $LCTL list_param jobid_name > /dev/null 2>&1
16190         if [ $? -eq 0 ]; then
16191                 JOBENV=nodelocal
16192         else
16193                 JOBENV=FAKE_JOBID
16194         fi
16195 fi
16196 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
16197
16198 verify_jobstats() {
16199         local cmd=($1)
16200         shift
16201         local facets="$@"
16202
16203 # we don't really need to clear the stats for this test to work, since each
16204 # command has a unique jobid, but it makes debugging easier if needed.
16205 #       for facet in $facets; do
16206 #               local dev=$(convert_facet2label $facet)
16207 #               # clear old jobstats
16208 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
16209 #       done
16210
16211         # use a new JobID for each test, or we might see an old one
16212         [ "$JOBENV" = "FAKE_JOBID" ] &&
16213                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
16214
16215         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
16216
16217         [ "$JOBENV" = "nodelocal" ] && {
16218                 FAKE_JOBID=id.$testnum.%e.$RANDOM
16219                 $LCTL set_param jobid_name=$FAKE_JOBID
16220                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
16221         }
16222
16223         log "Test: ${cmd[*]}"
16224         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
16225
16226         if [ $JOBENV = "FAKE_JOBID" ]; then
16227                 FAKE_JOBID=$JOBVAL ${cmd[*]}
16228         else
16229                 ${cmd[*]}
16230         fi
16231
16232         # all files are created on OST0000
16233         for facet in $facets; do
16234                 local stats="*.$(convert_facet2label $facet).job_stats"
16235
16236                 # strip out libtool wrappers for in-tree executables
16237                 if [ $(do_facet $facet lctl get_param $stats |
16238                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
16239                         do_facet $facet lctl get_param $stats
16240                         error "No jobstats for $JOBVAL found on $facet::$stats"
16241                 fi
16242         done
16243 }
16244
16245 jobstats_set() {
16246         local new_jobenv=$1
16247
16248         set_persistent_param_and_check client "jobid_var" \
16249                 "$FSNAME.sys.jobid_var" $new_jobenv
16250 }
16251
16252 test_205a() { # Job stats
16253         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16254         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
16255                 skip "Need MDS version with at least 2.7.1"
16256         remote_mgs_nodsh && skip "remote MGS with nodsh"
16257         remote_mds_nodsh && skip "remote MDS with nodsh"
16258         remote_ost_nodsh && skip "remote OST with nodsh"
16259         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
16260                 skip "Server doesn't support jobstats"
16261         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
16262
16263         local old_jobenv=$($LCTL get_param -n jobid_var)
16264         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
16265
16266         if [[ $PERM_CMD == *"set_param -P"* ]]; then
16267                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
16268         else
16269                 stack_trap "do_facet mgs $PERM_CMD \
16270                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
16271         fi
16272         changelog_register
16273
16274         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
16275                                 mdt.*.job_cleanup_interval | head -n 1)
16276         local new_interval=5
16277         do_facet $SINGLEMDS \
16278                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
16279         stack_trap "do_facet $SINGLEMDS \
16280                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
16281         local start=$SECONDS
16282
16283         local cmd
16284         # mkdir
16285         cmd="mkdir $DIR/$tdir"
16286         verify_jobstats "$cmd" "$SINGLEMDS"
16287         # rmdir
16288         cmd="rmdir $DIR/$tdir"
16289         verify_jobstats "$cmd" "$SINGLEMDS"
16290         # mkdir on secondary MDT
16291         if [ $MDSCOUNT -gt 1 ]; then
16292                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
16293                 verify_jobstats "$cmd" "mds2"
16294         fi
16295         # mknod
16296         cmd="mknod $DIR/$tfile c 1 3"
16297         verify_jobstats "$cmd" "$SINGLEMDS"
16298         # unlink
16299         cmd="rm -f $DIR/$tfile"
16300         verify_jobstats "$cmd" "$SINGLEMDS"
16301         # create all files on OST0000 so verify_jobstats can find OST stats
16302         # open & close
16303         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
16304         verify_jobstats "$cmd" "$SINGLEMDS"
16305         # setattr
16306         cmd="touch $DIR/$tfile"
16307         verify_jobstats "$cmd" "$SINGLEMDS ost1"
16308         # write
16309         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
16310         verify_jobstats "$cmd" "ost1"
16311         # read
16312         cancel_lru_locks osc
16313         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
16314         verify_jobstats "$cmd" "ost1"
16315         # truncate
16316         cmd="$TRUNCATE $DIR/$tfile 0"
16317         verify_jobstats "$cmd" "$SINGLEMDS ost1"
16318         # rename
16319         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
16320         verify_jobstats "$cmd" "$SINGLEMDS"
16321         # jobstats expiry - sleep until old stats should be expired
16322         local left=$((new_interval + 5 - (SECONDS - start)))
16323         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
16324                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
16325                         "0" $left
16326         cmd="mkdir $DIR/$tdir.expire"
16327         verify_jobstats "$cmd" "$SINGLEMDS"
16328         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
16329             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
16330
16331         # Ensure that jobid are present in changelog (if supported by MDS)
16332         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
16333                 changelog_dump | tail -10
16334                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
16335                 [ $jobids -eq 9 ] ||
16336                         error "Wrong changelog jobid count $jobids != 9"
16337
16338                 # LU-5862
16339                 JOBENV="disable"
16340                 jobstats_set $JOBENV
16341                 touch $DIR/$tfile
16342                 changelog_dump | grep $tfile
16343                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
16344                 [ $jobids -eq 0 ] ||
16345                         error "Unexpected jobids when jobid_var=$JOBENV"
16346         fi
16347
16348         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
16349         JOBENV="JOBCOMPLEX"
16350         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
16351
16352         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
16353 }
16354 run_test 205a "Verify job stats"
16355
16356 # LU-13117, LU-13597
16357 test_205b() {
16358         job_stats="mdt.*.job_stats"
16359         $LCTL set_param $job_stats=clear
16360         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
16361         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
16362         do_facet $SINGLEMDS $LCTL get_param $job_stats |
16363                 grep "job_id:.*foolish" &&
16364                         error "Unexpected jobid found"
16365         do_facet $SINGLEMDS $LCTL get_param $job_stats |
16366                 grep "open:.*min.*max.*sum" ||
16367                         error "wrong job_stats format found"
16368 }
16369 run_test 205b "Verify job stats jobid and output format"
16370
16371 # LU-1480, LU-1773 and LU-1657
16372 test_206() {
16373         mkdir -p $DIR/$tdir
16374         $LFS setstripe -c -1 $DIR/$tdir
16375 #define OBD_FAIL_LOV_INIT 0x1403
16376         $LCTL set_param fail_loc=0xa0001403
16377         $LCTL set_param fail_val=1
16378         touch $DIR/$tdir/$tfile || true
16379 }
16380 run_test 206 "fail lov_init_raid0() doesn't lbug"
16381
16382 test_207a() {
16383         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
16384         local fsz=`stat -c %s $DIR/$tfile`
16385         cancel_lru_locks mdc
16386
16387         # do not return layout in getattr intent
16388 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
16389         $LCTL set_param fail_loc=0x170
16390         local sz=`stat -c %s $DIR/$tfile`
16391
16392         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
16393
16394         rm -rf $DIR/$tfile
16395 }
16396 run_test 207a "can refresh layout at glimpse"
16397
16398 test_207b() {
16399         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
16400         local cksum=`md5sum $DIR/$tfile`
16401         local fsz=`stat -c %s $DIR/$tfile`
16402         cancel_lru_locks mdc
16403         cancel_lru_locks osc
16404
16405         # do not return layout in getattr intent
16406 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
16407         $LCTL set_param fail_loc=0x171
16408
16409         # it will refresh layout after the file is opened but before read issues
16410         echo checksum is "$cksum"
16411         echo "$cksum" |md5sum -c --quiet || error "file differs"
16412
16413         rm -rf $DIR/$tfile
16414 }
16415 run_test 207b "can refresh layout at open"
16416
16417 test_208() {
16418         # FIXME: in this test suite, only RD lease is used. This is okay
16419         # for now as only exclusive open is supported. After generic lease
16420         # is done, this test suite should be revised. - Jinshan
16421
16422         remote_mds_nodsh && skip "remote MDS with nodsh"
16423         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
16424                 skip "Need MDS version at least 2.4.52"
16425
16426         echo "==== test 1: verify get lease work"
16427         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
16428
16429         echo "==== test 2: verify lease can be broken by upcoming open"
16430         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
16431         local PID=$!
16432         sleep 1
16433
16434         $MULTIOP $DIR/$tfile oO_RDONLY:c
16435         kill -USR1 $PID && wait $PID || error "break lease error"
16436
16437         echo "==== test 3: verify lease can't be granted if an open already exists"
16438         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
16439         local PID=$!
16440         sleep 1
16441
16442         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
16443         kill -USR1 $PID && wait $PID || error "open file error"
16444
16445         echo "==== test 4: lease can sustain over recovery"
16446         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
16447         PID=$!
16448         sleep 1
16449
16450         fail mds1
16451
16452         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
16453
16454         echo "==== test 5: lease broken can't be regained by replay"
16455         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
16456         PID=$!
16457         sleep 1
16458
16459         # open file to break lease and then recovery
16460         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
16461         fail mds1
16462
16463         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
16464
16465         rm -f $DIR/$tfile
16466 }
16467 run_test 208 "Exclusive open"
16468
16469 test_209() {
16470         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
16471                 skip_env "must have disp_stripe"
16472
16473         touch $DIR/$tfile
16474         sync; sleep 5; sync;
16475
16476         echo 3 > /proc/sys/vm/drop_caches
16477         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16478
16479         # open/close 500 times
16480         for i in $(seq 500); do
16481                 cat $DIR/$tfile
16482         done
16483
16484         echo 3 > /proc/sys/vm/drop_caches
16485         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16486
16487         echo "before: $req_before, after: $req_after"
16488         [ $((req_after - req_before)) -ge 300 ] &&
16489                 error "open/close requests are not freed"
16490         return 0
16491 }
16492 run_test 209 "read-only open/close requests should be freed promptly"
16493
16494 test_210() {
16495         local pid
16496
16497         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eW_E+eUc &
16498         pid=$!
16499         sleep 1
16500
16501         $LFS getstripe $DIR/$tfile
16502         kill -USR1 $pid
16503         wait $pid || error "multiop failed"
16504
16505         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
16506         pid=$!
16507         sleep 1
16508
16509         $LFS getstripe $DIR/$tfile
16510         kill -USR1 $pid
16511         wait $pid || error "multiop failed"
16512 }
16513 run_test 210 "lfs getstripe does not break leases"
16514
16515 test_212() {
16516         size=`date +%s`
16517         size=$((size % 8192 + 1))
16518         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
16519         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
16520         rm -f $DIR/f212 $DIR/f212.xyz
16521 }
16522 run_test 212 "Sendfile test ============================================"
16523
16524 test_213() {
16525         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
16526         cancel_lru_locks osc
16527         lctl set_param fail_loc=0x8000040f
16528         # generate a read lock
16529         cat $DIR/$tfile > /dev/null
16530         # write to the file, it will try to cancel the above read lock.
16531         cat /etc/hosts >> $DIR/$tfile
16532 }
16533 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
16534
16535 test_214() { # for bug 20133
16536         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
16537         for (( i=0; i < 340; i++ )) ; do
16538                 touch $DIR/$tdir/d214c/a$i
16539         done
16540
16541         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
16542         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
16543         ls $DIR/d214c || error "ls $DIR/d214c failed"
16544         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
16545         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
16546 }
16547 run_test 214 "hash-indexed directory test - bug 20133"
16548
16549 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
16550 create_lnet_proc_files() {
16551         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
16552 }
16553
16554 # counterpart of create_lnet_proc_files
16555 remove_lnet_proc_files() {
16556         rm -f $TMP/lnet_$1.sys
16557 }
16558
16559 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16560 # 3rd arg as regexp for body
16561 check_lnet_proc_stats() {
16562         local l=$(cat "$TMP/lnet_$1" |wc -l)
16563         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
16564
16565         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
16566 }
16567
16568 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16569 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
16570 # optional and can be regexp for 2nd line (lnet.routes case)
16571 check_lnet_proc_entry() {
16572         local blp=2          # blp stands for 'position of 1st line of body'
16573         [ -z "$5" ] || blp=3 # lnet.routes case
16574
16575         local l=$(cat "$TMP/lnet_$1" |wc -l)
16576         # subtracting one from $blp because the body can be empty
16577         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
16578
16579         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
16580                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
16581
16582         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
16583                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
16584
16585         # bail out if any unexpected line happened
16586         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
16587         [ "$?" != 0 ] || error "$2 misformatted"
16588 }
16589
16590 test_215() { # for bugs 18102, 21079, 21517
16591         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16592
16593         local N='(0|[1-9][0-9]*)'       # non-negative numeric
16594         local P='[1-9][0-9]*'           # positive numeric
16595         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
16596         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
16597         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
16598         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
16599
16600         local L1 # regexp for 1st line
16601         local L2 # regexp for 2nd line (optional)
16602         local BR # regexp for the rest (body)
16603
16604         # lnet.stats should look as 11 space-separated non-negative numerics
16605         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
16606         create_lnet_proc_files "stats"
16607         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
16608         remove_lnet_proc_files "stats"
16609
16610         # lnet.routes should look like this:
16611         # Routing disabled/enabled
16612         # net hops priority state router
16613         # where net is a string like tcp0, hops > 0, priority >= 0,
16614         # state is up/down,
16615         # router is a string like 192.168.1.1@tcp2
16616         L1="^Routing (disabled|enabled)$"
16617         L2="^net +hops +priority +state +router$"
16618         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
16619         create_lnet_proc_files "routes"
16620         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
16621         remove_lnet_proc_files "routes"
16622
16623         # lnet.routers should look like this:
16624         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
16625         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
16626         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
16627         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
16628         L1="^ref +rtr_ref +alive +router$"
16629         BR="^$P +$P +(up|down) +$NID$"
16630         create_lnet_proc_files "routers"
16631         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
16632         remove_lnet_proc_files "routers"
16633
16634         # lnet.peers should look like this:
16635         # nid refs state last max rtr min tx min queue
16636         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
16637         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
16638         # numeric (0 or >0 or <0), queue >= 0.
16639         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
16640         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
16641         create_lnet_proc_files "peers"
16642         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
16643         remove_lnet_proc_files "peers"
16644
16645         # lnet.buffers  should look like this:
16646         # pages count credits min
16647         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
16648         L1="^pages +count +credits +min$"
16649         BR="^ +$N +$N +$I +$I$"
16650         create_lnet_proc_files "buffers"
16651         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
16652         remove_lnet_proc_files "buffers"
16653
16654         # lnet.nis should look like this:
16655         # nid status alive refs peer rtr max tx min
16656         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
16657         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
16658         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
16659         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
16660         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
16661         create_lnet_proc_files "nis"
16662         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
16663         remove_lnet_proc_files "nis"
16664
16665         # can we successfully write to lnet.stats?
16666         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
16667 }
16668 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
16669
16670 test_216() { # bug 20317
16671         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16672         remote_ost_nodsh && skip "remote OST with nodsh"
16673
16674         local node
16675         local facets=$(get_facets OST)
16676         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16677
16678         save_lustre_params client "osc.*.contention_seconds" > $p
16679         save_lustre_params $facets \
16680                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
16681         save_lustre_params $facets \
16682                 "ldlm.namespaces.filter-*.contended_locks" >> $p
16683         save_lustre_params $facets \
16684                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
16685         clear_stats osc.*.osc_stats
16686
16687         # agressive lockless i/o settings
16688         do_nodes $(comma_list $(osts_nodes)) \
16689                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
16690                         ldlm.namespaces.filter-*.contended_locks=0 \
16691                         ldlm.namespaces.filter-*.contention_seconds=60"
16692         lctl set_param -n osc.*.contention_seconds=60
16693
16694         $DIRECTIO write $DIR/$tfile 0 10 4096
16695         $CHECKSTAT -s 40960 $DIR/$tfile
16696
16697         # disable lockless i/o
16698         do_nodes $(comma_list $(osts_nodes)) \
16699                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
16700                         ldlm.namespaces.filter-*.contended_locks=32 \
16701                         ldlm.namespaces.filter-*.contention_seconds=0"
16702         lctl set_param -n osc.*.contention_seconds=0
16703         clear_stats osc.*.osc_stats
16704
16705         dd if=/dev/zero of=$DIR/$tfile count=0
16706         $CHECKSTAT -s 0 $DIR/$tfile
16707
16708         restore_lustre_params <$p
16709         rm -f $p
16710         rm $DIR/$tfile
16711 }
16712 run_test 216 "check lockless direct write updates file size and kms correctly"
16713
16714 test_217() { # bug 22430
16715         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16716
16717         local node
16718         local nid
16719
16720         for node in $(nodes_list); do
16721                 nid=$(host_nids_address $node $NETTYPE)
16722                 if [[ $nid = *-* ]] ; then
16723                         echo "lctl ping $(h2nettype $nid)"
16724                         lctl ping $(h2nettype $nid)
16725                 else
16726                         echo "skipping $node (no hyphen detected)"
16727                 fi
16728         done
16729 }
16730 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16731
16732 test_218() {
16733        # do directio so as not to populate the page cache
16734        log "creating a 10 Mb file"
16735        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16736        log "starting reads"
16737        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16738        log "truncating the file"
16739        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16740        log "killing dd"
16741        kill %+ || true # reads might have finished
16742        echo "wait until dd is finished"
16743        wait
16744        log "removing the temporary file"
16745        rm -rf $DIR/$tfile || error "tmp file removal failed"
16746 }
16747 run_test 218 "parallel read and truncate should not deadlock"
16748
16749 test_219() {
16750         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16751
16752         # write one partial page
16753         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16754         # set no grant so vvp_io_commit_write will do sync write
16755         $LCTL set_param fail_loc=0x411
16756         # write a full page at the end of file
16757         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16758
16759         $LCTL set_param fail_loc=0
16760         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16761         $LCTL set_param fail_loc=0x411
16762         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16763
16764         # LU-4201
16765         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16766         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16767 }
16768 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16769
16770 test_220() { #LU-325
16771         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16772         remote_ost_nodsh && skip "remote OST with nodsh"
16773         remote_mds_nodsh && skip "remote MDS with nodsh"
16774         remote_mgs_nodsh && skip "remote MGS with nodsh"
16775
16776         local OSTIDX=0
16777
16778         # create on MDT0000 so the last_id and next_id are correct
16779         mkdir $DIR/$tdir
16780         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16781         OST=${OST%_UUID}
16782
16783         # on the mdt's osc
16784         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16785         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16786                         osp.$mdtosc_proc1.prealloc_last_id)
16787         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16788                         osp.$mdtosc_proc1.prealloc_next_id)
16789
16790         $LFS df -i
16791
16792         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16793         #define OBD_FAIL_OST_ENOINO              0x229
16794         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16795         create_pool $FSNAME.$TESTNAME || return 1
16796         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16797
16798         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16799
16800         MDSOBJS=$((last_id - next_id))
16801         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16802
16803         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16804         echo "OST still has $count kbytes free"
16805
16806         echo "create $MDSOBJS files @next_id..."
16807         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16808
16809         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16810                         osp.$mdtosc_proc1.prealloc_last_id)
16811         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16812                         osp.$mdtosc_proc1.prealloc_next_id)
16813
16814         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16815         $LFS df -i
16816
16817         echo "cleanup..."
16818
16819         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16820         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16821
16822         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16823                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16824         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16825                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16826         echo "unlink $MDSOBJS files @$next_id..."
16827         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16828 }
16829 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16830
16831 test_221() {
16832         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16833
16834         dd if=`which date` of=$MOUNT/date oflag=sync
16835         chmod +x $MOUNT/date
16836
16837         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16838         $LCTL set_param fail_loc=0x80001401
16839
16840         $MOUNT/date > /dev/null
16841         rm -f $MOUNT/date
16842 }
16843 run_test 221 "make sure fault and truncate race to not cause OOM"
16844
16845 test_222a () {
16846         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16847
16848         rm -rf $DIR/$tdir
16849         test_mkdir $DIR/$tdir
16850         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16851         createmany -o $DIR/$tdir/$tfile 10
16852         cancel_lru_locks mdc
16853         cancel_lru_locks osc
16854         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16855         $LCTL set_param fail_loc=0x31a
16856         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16857         $LCTL set_param fail_loc=0
16858         rm -r $DIR/$tdir
16859 }
16860 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16861
16862 test_222b () {
16863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16864
16865         rm -rf $DIR/$tdir
16866         test_mkdir $DIR/$tdir
16867         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16868         createmany -o $DIR/$tdir/$tfile 10
16869         cancel_lru_locks mdc
16870         cancel_lru_locks osc
16871         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16872         $LCTL set_param fail_loc=0x31a
16873         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16874         $LCTL set_param fail_loc=0
16875 }
16876 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16877
16878 test_223 () {
16879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16880
16881         rm -rf $DIR/$tdir
16882         test_mkdir $DIR/$tdir
16883         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16884         createmany -o $DIR/$tdir/$tfile 10
16885         cancel_lru_locks mdc
16886         cancel_lru_locks osc
16887         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16888         $LCTL set_param fail_loc=0x31b
16889         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16890         $LCTL set_param fail_loc=0
16891         rm -r $DIR/$tdir
16892 }
16893 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16894
16895 test_224a() { # LU-1039, MRP-303
16896         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16897
16898         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16899         $LCTL set_param fail_loc=0x508
16900         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16901         $LCTL set_param fail_loc=0
16902         df $DIR
16903 }
16904 run_test 224a "Don't panic on bulk IO failure"
16905
16906 test_224b() { # LU-1039, MRP-303
16907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16908
16909         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16910         cancel_lru_locks osc
16911         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16912         $LCTL set_param fail_loc=0x515
16913         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16914         $LCTL set_param fail_loc=0
16915         df $DIR
16916 }
16917 run_test 224b "Don't panic on bulk IO failure"
16918
16919 test_224c() { # LU-6441
16920         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16921         remote_mds_nodsh && skip "remote MDS with nodsh"
16922
16923         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16924         save_writethrough $p
16925         set_cache writethrough on
16926
16927         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
16928         local at_max=$($LCTL get_param -n at_max)
16929         local timeout=$($LCTL get_param -n timeout)
16930         local test_at="at_max"
16931         local param_at="$FSNAME.sys.at_max"
16932         local test_timeout="timeout"
16933         local param_timeout="$FSNAME.sys.timeout"
16934
16935         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16936
16937         set_persistent_param_and_check client "$test_at" "$param_at" 0
16938         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16939
16940         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16941         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16942         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16943         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16944         sync
16945         do_facet ost1 "$LCTL set_param fail_loc=0"
16946
16947         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16948         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16949                 $timeout
16950
16951         $LCTL set_param -n $pages_per_rpc
16952         restore_lustre_params < $p
16953         rm -f $p
16954 }
16955 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16956
16957 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16958 test_225a () {
16959         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16960         if [ -z ${MDSSURVEY} ]; then
16961                 skip_env "mds-survey not found"
16962         fi
16963         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16964                 skip "Need MDS version at least 2.2.51"
16965
16966         local mds=$(facet_host $SINGLEMDS)
16967         local target=$(do_nodes $mds 'lctl dl' |
16968                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16969
16970         local cmd1="file_count=1000 thrhi=4"
16971         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16972         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16973         local cmd="$cmd1 $cmd2 $cmd3"
16974
16975         rm -f ${TMP}/mds_survey*
16976         echo + $cmd
16977         eval $cmd || error "mds-survey with zero-stripe failed"
16978         cat ${TMP}/mds_survey*
16979         rm -f ${TMP}/mds_survey*
16980 }
16981 run_test 225a "Metadata survey sanity with zero-stripe"
16982
16983 test_225b () {
16984         if [ -z ${MDSSURVEY} ]; then
16985                 skip_env "mds-survey not found"
16986         fi
16987         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16988                 skip "Need MDS version at least 2.2.51"
16989         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16990         remote_mds_nodsh && skip "remote MDS with nodsh"
16991         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16992                 skip_env "Need to mount OST to test"
16993         fi
16994
16995         local mds=$(facet_host $SINGLEMDS)
16996         local target=$(do_nodes $mds 'lctl dl' |
16997                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16998
16999         local cmd1="file_count=1000 thrhi=4"
17000         local cmd2="dir_count=2 layer=mdd stripe_count=1"
17001         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
17002         local cmd="$cmd1 $cmd2 $cmd3"
17003
17004         rm -f ${TMP}/mds_survey*
17005         echo + $cmd
17006         eval $cmd || error "mds-survey with stripe_count failed"
17007         cat ${TMP}/mds_survey*
17008         rm -f ${TMP}/mds_survey*
17009 }
17010 run_test 225b "Metadata survey sanity with stripe_count = 1"
17011
17012 mcreate_path2fid () {
17013         local mode=$1
17014         local major=$2
17015         local minor=$3
17016         local name=$4
17017         local desc=$5
17018         local path=$DIR/$tdir/$name
17019         local fid
17020         local rc
17021         local fid_path
17022
17023         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
17024                 error "cannot create $desc"
17025
17026         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
17027         rc=$?
17028         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
17029
17030         fid_path=$($LFS fid2path $MOUNT $fid)
17031         rc=$?
17032         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
17033
17034         [ "$path" == "$fid_path" ] ||
17035                 error "fid2path returned $fid_path, expected $path"
17036
17037         echo "pass with $path and $fid"
17038 }
17039
17040 test_226a () {
17041         rm -rf $DIR/$tdir
17042         mkdir -p $DIR/$tdir
17043
17044         mcreate_path2fid 0010666 0 0 fifo "FIFO"
17045         mcreate_path2fid 0020666 1 3 null "character special file (null)"
17046         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
17047         mcreate_path2fid 0040666 0 0 dir "directory"
17048         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
17049         mcreate_path2fid 0100666 0 0 file "regular file"
17050         mcreate_path2fid 0120666 0 0 link "symbolic link"
17051         mcreate_path2fid 0140666 0 0 sock "socket"
17052 }
17053 run_test 226a "call path2fid and fid2path on files of all type"
17054
17055 test_226b () {
17056         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17057
17058         local MDTIDX=1
17059
17060         rm -rf $DIR/$tdir
17061         mkdir -p $DIR/$tdir
17062         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
17063                 error "create remote directory failed"
17064         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
17065         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
17066                                 "character special file (null)"
17067         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
17068                                 "character special file (no device)"
17069         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
17070         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
17071                                 "block special file (loop)"
17072         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
17073         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
17074         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
17075 }
17076 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
17077
17078 # LU-1299 Executing or running ldd on a truncated executable does not
17079 # cause an out-of-memory condition.
17080 test_227() {
17081         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17082         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
17083
17084         dd if=$(which date) of=$MOUNT/date bs=1k count=1
17085         chmod +x $MOUNT/date
17086
17087         $MOUNT/date > /dev/null
17088         ldd $MOUNT/date > /dev/null
17089         rm -f $MOUNT/date
17090 }
17091 run_test 227 "running truncated executable does not cause OOM"
17092
17093 # LU-1512 try to reuse idle OI blocks
17094 test_228a() {
17095         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17096         remote_mds_nodsh && skip "remote MDS with nodsh"
17097         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17098
17099         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17100         local myDIR=$DIR/$tdir
17101
17102         mkdir -p $myDIR
17103         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17104         $LCTL set_param fail_loc=0x80001002
17105         createmany -o $myDIR/t- 10000
17106         $LCTL set_param fail_loc=0
17107         # The guard is current the largest FID holder
17108         touch $myDIR/guard
17109         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17110                     tr -d '[')
17111         local IDX=$(($SEQ % 64))
17112
17113         do_facet $SINGLEMDS sync
17114         # Make sure journal flushed.
17115         sleep 6
17116         local blk1=$(do_facet $SINGLEMDS \
17117                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17118                      grep Blockcount | awk '{print $4}')
17119
17120         # Remove old files, some OI blocks will become idle.
17121         unlinkmany $myDIR/t- 10000
17122         # Create new files, idle OI blocks should be reused.
17123         createmany -o $myDIR/t- 2000
17124         do_facet $SINGLEMDS sync
17125         # Make sure journal flushed.
17126         sleep 6
17127         local blk2=$(do_facet $SINGLEMDS \
17128                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17129                      grep Blockcount | awk '{print $4}')
17130
17131         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17132 }
17133 run_test 228a "try to reuse idle OI blocks"
17134
17135 test_228b() {
17136         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17137         remote_mds_nodsh && skip "remote MDS with nodsh"
17138         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17139
17140         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17141         local myDIR=$DIR/$tdir
17142
17143         mkdir -p $myDIR
17144         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17145         $LCTL set_param fail_loc=0x80001002
17146         createmany -o $myDIR/t- 10000
17147         $LCTL set_param fail_loc=0
17148         # The guard is current the largest FID holder
17149         touch $myDIR/guard
17150         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17151                     tr -d '[')
17152         local IDX=$(($SEQ % 64))
17153
17154         do_facet $SINGLEMDS sync
17155         # Make sure journal flushed.
17156         sleep 6
17157         local blk1=$(do_facet $SINGLEMDS \
17158                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17159                      grep Blockcount | awk '{print $4}')
17160
17161         # Remove old files, some OI blocks will become idle.
17162         unlinkmany $myDIR/t- 10000
17163
17164         # stop the MDT
17165         stop $SINGLEMDS || error "Fail to stop MDT."
17166         # remount the MDT
17167         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
17168
17169         df $MOUNT || error "Fail to df."
17170         # Create new files, idle OI blocks should be reused.
17171         createmany -o $myDIR/t- 2000
17172         do_facet $SINGLEMDS sync
17173         # Make sure journal flushed.
17174         sleep 6
17175         local blk2=$(do_facet $SINGLEMDS \
17176                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17177                      grep Blockcount | awk '{print $4}')
17178
17179         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17180 }
17181 run_test 228b "idle OI blocks can be reused after MDT restart"
17182
17183 #LU-1881
17184 test_228c() {
17185         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17186         remote_mds_nodsh && skip "remote MDS with nodsh"
17187         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17188
17189         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17190         local myDIR=$DIR/$tdir
17191
17192         mkdir -p $myDIR
17193         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17194         $LCTL set_param fail_loc=0x80001002
17195         # 20000 files can guarantee there are index nodes in the OI file
17196         createmany -o $myDIR/t- 20000
17197         $LCTL set_param fail_loc=0
17198         # The guard is current the largest FID holder
17199         touch $myDIR/guard
17200         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17201                     tr -d '[')
17202         local IDX=$(($SEQ % 64))
17203
17204         do_facet $SINGLEMDS sync
17205         # Make sure journal flushed.
17206         sleep 6
17207         local blk1=$(do_facet $SINGLEMDS \
17208                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17209                      grep Blockcount | awk '{print $4}')
17210
17211         # Remove old files, some OI blocks will become idle.
17212         unlinkmany $myDIR/t- 20000
17213         rm -f $myDIR/guard
17214         # The OI file should become empty now
17215
17216         # Create new files, idle OI blocks should be reused.
17217         createmany -o $myDIR/t- 2000
17218         do_facet $SINGLEMDS sync
17219         # Make sure journal flushed.
17220         sleep 6
17221         local blk2=$(do_facet $SINGLEMDS \
17222                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17223                      grep Blockcount | awk '{print $4}')
17224
17225         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17226 }
17227 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
17228
17229 test_229() { # LU-2482, LU-3448
17230         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17231         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
17232         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
17233                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
17234
17235         rm -f $DIR/$tfile
17236
17237         # Create a file with a released layout and stripe count 2.
17238         $MULTIOP $DIR/$tfile H2c ||
17239                 error "failed to create file with released layout"
17240
17241         $LFS getstripe -v $DIR/$tfile
17242
17243         local pattern=$($LFS getstripe -L $DIR/$tfile)
17244         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
17245
17246         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
17247                 error "getstripe"
17248         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
17249         stat $DIR/$tfile || error "failed to stat released file"
17250
17251         chown $RUNAS_ID $DIR/$tfile ||
17252                 error "chown $RUNAS_ID $DIR/$tfile failed"
17253
17254         chgrp $RUNAS_ID $DIR/$tfile ||
17255                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
17256
17257         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
17258         rm $DIR/$tfile || error "failed to remove released file"
17259 }
17260 run_test 229 "getstripe/stat/rm/attr changes work on released files"
17261
17262 test_230a() {
17263         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17264         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17265         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17266                 skip "Need MDS version at least 2.11.52"
17267
17268         local MDTIDX=1
17269
17270         test_mkdir $DIR/$tdir
17271         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
17272         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
17273         [ $mdt_idx -ne 0 ] &&
17274                 error "create local directory on wrong MDT $mdt_idx"
17275
17276         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
17277                         error "create remote directory failed"
17278         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
17279         [ $mdt_idx -ne $MDTIDX ] &&
17280                 error "create remote directory on wrong MDT $mdt_idx"
17281
17282         createmany -o $DIR/$tdir/test_230/t- 10 ||
17283                 error "create files on remote directory failed"
17284         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
17285         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
17286         rm -r $DIR/$tdir || error "unlink remote directory failed"
17287 }
17288 run_test 230a "Create remote directory and files under the remote directory"
17289
17290 test_230b() {
17291         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17292         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17293         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17294                 skip "Need MDS version at least 2.11.52"
17295
17296         local MDTIDX=1
17297         local mdt_index
17298         local i
17299         local file
17300         local pid
17301         local stripe_count
17302         local migrate_dir=$DIR/$tdir/migrate_dir
17303         local other_dir=$DIR/$tdir/other_dir
17304
17305         test_mkdir $DIR/$tdir
17306         test_mkdir -i0 -c1 $migrate_dir
17307         test_mkdir -i0 -c1 $other_dir
17308         for ((i=0; i<10; i++)); do
17309                 mkdir -p $migrate_dir/dir_${i}
17310                 createmany -o $migrate_dir/dir_${i}/f 10 ||
17311                         error "create files under remote dir failed $i"
17312         done
17313
17314         cp /etc/passwd $migrate_dir/$tfile
17315         cp /etc/passwd $other_dir/$tfile
17316         chattr +SAD $migrate_dir
17317         chattr +SAD $migrate_dir/$tfile
17318
17319         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17320         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17321         local old_dir_mode=$(stat -c%f $migrate_dir)
17322         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
17323
17324         mkdir -p $migrate_dir/dir_default_stripe2
17325         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
17326         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
17327
17328         mkdir -p $other_dir
17329         ln $migrate_dir/$tfile $other_dir/luna
17330         ln $migrate_dir/$tfile $migrate_dir/sofia
17331         ln $other_dir/$tfile $migrate_dir/david
17332         ln -s $migrate_dir/$tfile $other_dir/zachary
17333         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
17334         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
17335
17336         local len
17337         local lnktgt
17338
17339         # inline symlink
17340         for len in 58 59 60; do
17341                 lnktgt=$(str_repeat 'l' $len)
17342                 touch $migrate_dir/$lnktgt
17343                 ln -s $lnktgt $migrate_dir/${len}char_ln
17344         done
17345
17346         # PATH_MAX
17347         for len in 4094 4095; do
17348                 lnktgt=$(str_repeat 'l' $len)
17349                 ln -s $lnktgt $migrate_dir/${len}char_ln
17350         done
17351
17352         # NAME_MAX
17353         for len in 254 255; do
17354                 touch $migrate_dir/$(str_repeat 'l' $len)
17355         done
17356
17357         $LFS migrate -m $MDTIDX $migrate_dir ||
17358                 error "fails on migrating remote dir to MDT1"
17359
17360         echo "migratate to MDT1, then checking.."
17361         for ((i = 0; i < 10; i++)); do
17362                 for file in $(find $migrate_dir/dir_${i}); do
17363                         mdt_index=$($LFS getstripe -m $file)
17364                         # broken symlink getstripe will fail
17365                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
17366                                 error "$file is not on MDT${MDTIDX}"
17367                 done
17368         done
17369
17370         # the multiple link file should still in MDT0
17371         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
17372         [ $mdt_index == 0 ] ||
17373                 error "$file is not on MDT${MDTIDX}"
17374
17375         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17376         [ "$old_dir_flag" = "$new_dir_flag" ] ||
17377                 error " expect $old_dir_flag get $new_dir_flag"
17378
17379         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17380         [ "$old_file_flag" = "$new_file_flag" ] ||
17381                 error " expect $old_file_flag get $new_file_flag"
17382
17383         local new_dir_mode=$(stat -c%f $migrate_dir)
17384         [ "$old_dir_mode" = "$new_dir_mode" ] ||
17385                 error "expect mode $old_dir_mode get $new_dir_mode"
17386
17387         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
17388         [ "$old_file_mode" = "$new_file_mode" ] ||
17389                 error "expect mode $old_file_mode get $new_file_mode"
17390
17391         diff /etc/passwd $migrate_dir/$tfile ||
17392                 error "$tfile different after migration"
17393
17394         diff /etc/passwd $other_dir/luna ||
17395                 error "luna different after migration"
17396
17397         diff /etc/passwd $migrate_dir/sofia ||
17398                 error "sofia different after migration"
17399
17400         diff /etc/passwd $migrate_dir/david ||
17401                 error "david different after migration"
17402
17403         diff /etc/passwd $other_dir/zachary ||
17404                 error "zachary different after migration"
17405
17406         diff /etc/passwd $migrate_dir/${tfile}_ln ||
17407                 error "${tfile}_ln different after migration"
17408
17409         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
17410                 error "${tfile}_ln_other different after migration"
17411
17412         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
17413         [ $stripe_count = 2 ] ||
17414                 error "dir strpe_count $d != 2 after migration."
17415
17416         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
17417         [ $stripe_count = 2 ] ||
17418                 error "file strpe_count $d != 2 after migration."
17419
17420         #migrate back to MDT0
17421         MDTIDX=0
17422
17423         $LFS migrate -m $MDTIDX $migrate_dir ||
17424                 error "fails on migrating remote dir to MDT0"
17425
17426         echo "migrate back to MDT0, checking.."
17427         for file in $(find $migrate_dir); do
17428                 mdt_index=$($LFS getstripe -m $file)
17429                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
17430                         error "$file is not on MDT${MDTIDX}"
17431         done
17432
17433         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17434         [ "$old_dir_flag" = "$new_dir_flag" ] ||
17435                 error " expect $old_dir_flag get $new_dir_flag"
17436
17437         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17438         [ "$old_file_flag" = "$new_file_flag" ] ||
17439                 error " expect $old_file_flag get $new_file_flag"
17440
17441         local new_dir_mode=$(stat -c%f $migrate_dir)
17442         [ "$old_dir_mode" = "$new_dir_mode" ] ||
17443                 error "expect mode $old_dir_mode get $new_dir_mode"
17444
17445         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
17446         [ "$old_file_mode" = "$new_file_mode" ] ||
17447                 error "expect mode $old_file_mode get $new_file_mode"
17448
17449         diff /etc/passwd ${migrate_dir}/$tfile ||
17450                 error "$tfile different after migration"
17451
17452         diff /etc/passwd ${other_dir}/luna ||
17453                 error "luna different after migration"
17454
17455         diff /etc/passwd ${migrate_dir}/sofia ||
17456                 error "sofia different after migration"
17457
17458         diff /etc/passwd ${other_dir}/zachary ||
17459                 error "zachary different after migration"
17460
17461         diff /etc/passwd $migrate_dir/${tfile}_ln ||
17462                 error "${tfile}_ln different after migration"
17463
17464         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
17465                 error "${tfile}_ln_other different after migration"
17466
17467         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
17468         [ $stripe_count = 2 ] ||
17469                 error "dir strpe_count $d != 2 after migration."
17470
17471         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
17472         [ $stripe_count = 2 ] ||
17473                 error "file strpe_count $d != 2 after migration."
17474
17475         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17476 }
17477 run_test 230b "migrate directory"
17478
17479 test_230c() {
17480         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17481         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17482         remote_mds_nodsh && skip "remote MDS with nodsh"
17483         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17484                 skip "Need MDS version at least 2.11.52"
17485
17486         local MDTIDX=1
17487         local total=3
17488         local mdt_index
17489         local file
17490         local migrate_dir=$DIR/$tdir/migrate_dir
17491
17492         #If migrating directory fails in the middle, all entries of
17493         #the directory is still accessiable.
17494         test_mkdir $DIR/$tdir
17495         test_mkdir -i0 -c1 $migrate_dir
17496         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
17497         stat $migrate_dir
17498         createmany -o $migrate_dir/f $total ||
17499                 error "create files under ${migrate_dir} failed"
17500
17501         # fail after migrating top dir, and this will fail only once, so the
17502         # first sub file migration will fail (currently f3), others succeed.
17503         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
17504         do_facet mds1 lctl set_param fail_loc=0x1801
17505         local t=$(ls $migrate_dir | wc -l)
17506         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
17507                 error "migrate should fail"
17508         local u=$(ls $migrate_dir | wc -l)
17509         [ "$u" == "$t" ] || error "$u != $t during migration"
17510
17511         # add new dir/file should succeed
17512         mkdir $migrate_dir/dir ||
17513                 error "mkdir failed under migrating directory"
17514         touch $migrate_dir/file ||
17515                 error "create file failed under migrating directory"
17516
17517         # add file with existing name should fail
17518         for file in $migrate_dir/f*; do
17519                 stat $file > /dev/null || error "stat $file failed"
17520                 $OPENFILE -f O_CREAT:O_EXCL $file &&
17521                         error "open(O_CREAT|O_EXCL) $file should fail"
17522                 $MULTIOP $file m && error "create $file should fail"
17523                 touch $DIR/$tdir/remote_dir/$tfile ||
17524                         error "touch $tfile failed"
17525                 ln $DIR/$tdir/remote_dir/$tfile $file &&
17526                         error "link $file should fail"
17527                 mdt_index=$($LFS getstripe -m $file)
17528                 if [ $mdt_index == 0 ]; then
17529                         # file failed to migrate is not allowed to rename to
17530                         mv $DIR/$tdir/remote_dir/$tfile $file &&
17531                                 error "rename to $file should fail"
17532                 else
17533                         mv $DIR/$tdir/remote_dir/$tfile $file ||
17534                                 error "rename to $file failed"
17535                 fi
17536                 echo hello >> $file || error "write $file failed"
17537         done
17538
17539         # resume migration with different options should fail
17540         $LFS migrate -m 0 $migrate_dir &&
17541                 error "migrate -m 0 $migrate_dir should fail"
17542
17543         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
17544                 error "migrate -c 2 $migrate_dir should fail"
17545
17546         # resume migration should succeed
17547         $LFS migrate -m $MDTIDX $migrate_dir ||
17548                 error "migrate $migrate_dir failed"
17549
17550         echo "Finish migration, then checking.."
17551         for file in $(find $migrate_dir); do
17552                 mdt_index=$($LFS getstripe -m $file)
17553                 [ $mdt_index == $MDTIDX ] ||
17554                         error "$file is not on MDT${MDTIDX}"
17555         done
17556
17557         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17558 }
17559 run_test 230c "check directory accessiblity if migration failed"
17560
17561 test_230d() {
17562         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17563         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17564         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17565                 skip "Need MDS version at least 2.11.52"
17566         # LU-11235
17567         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
17568
17569         local migrate_dir=$DIR/$tdir/migrate_dir
17570         local old_index
17571         local new_index
17572         local old_count
17573         local new_count
17574         local new_hash
17575         local mdt_index
17576         local i
17577         local j
17578
17579         old_index=$((RANDOM % MDSCOUNT))
17580         old_count=$((MDSCOUNT - old_index))
17581         new_index=$((RANDOM % MDSCOUNT))
17582         new_count=$((MDSCOUNT - new_index))
17583         new_hash=1 # for all_char
17584
17585         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
17586         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
17587
17588         test_mkdir $DIR/$tdir
17589         test_mkdir -i $old_index -c $old_count $migrate_dir
17590
17591         for ((i=0; i<100; i++)); do
17592                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
17593                 createmany -o $migrate_dir/dir_${i}/f 100 ||
17594                         error "create files under remote dir failed $i"
17595         done
17596
17597         echo -n "Migrate from MDT$old_index "
17598         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
17599         echo -n "to MDT$new_index"
17600         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
17601         echo
17602
17603         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
17604         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
17605                 error "migrate remote dir error"
17606
17607         echo "Finish migration, then checking.."
17608         for file in $(find $migrate_dir); do
17609                 mdt_index=$($LFS getstripe -m $file)
17610                 if [ $mdt_index -lt $new_index ] ||
17611                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
17612                         error "$file is on MDT$mdt_index"
17613                 fi
17614         done
17615
17616         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17617 }
17618 run_test 230d "check migrate big directory"
17619
17620 test_230e() {
17621         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17622         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17623         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17624                 skip "Need MDS version at least 2.11.52"
17625
17626         local i
17627         local j
17628         local a_fid
17629         local b_fid
17630
17631         mkdir -p $DIR/$tdir
17632         mkdir $DIR/$tdir/migrate_dir
17633         mkdir $DIR/$tdir/other_dir
17634         touch $DIR/$tdir/migrate_dir/a
17635         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
17636         ls $DIR/$tdir/other_dir
17637
17638         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17639                 error "migrate dir fails"
17640
17641         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17642         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17643
17644         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17645         [ $mdt_index == 0 ] || error "a is not on MDT0"
17646
17647         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
17648                 error "migrate dir fails"
17649
17650         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
17651         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
17652
17653         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17654         [ $mdt_index == 1 ] || error "a is not on MDT1"
17655
17656         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
17657         [ $mdt_index == 1 ] || error "b is not on MDT1"
17658
17659         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17660         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
17661
17662         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
17663
17664         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17665 }
17666 run_test 230e "migrate mulitple local link files"
17667
17668 test_230f() {
17669         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17670         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17671         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17672                 skip "Need MDS version at least 2.11.52"
17673
17674         local a_fid
17675         local ln_fid
17676
17677         mkdir -p $DIR/$tdir
17678         mkdir $DIR/$tdir/migrate_dir
17679         $LFS mkdir -i1 $DIR/$tdir/other_dir
17680         touch $DIR/$tdir/migrate_dir/a
17681         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
17682         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
17683         ls $DIR/$tdir/other_dir
17684
17685         # a should be migrated to MDT1, since no other links on MDT0
17686         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17687                 error "#1 migrate dir fails"
17688         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17689         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17690         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17691         [ $mdt_index == 1 ] || error "a is not on MDT1"
17692
17693         # a should stay on MDT1, because it is a mulitple link file
17694         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17695                 error "#2 migrate dir fails"
17696         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17697         [ $mdt_index == 1 ] || error "a is not on MDT1"
17698
17699         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17700                 error "#3 migrate dir fails"
17701
17702         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17703         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
17704         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
17705
17706         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
17707         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
17708
17709         # a should be migrated to MDT0, since no other links on MDT1
17710         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17711                 error "#4 migrate dir fails"
17712         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17713         [ $mdt_index == 0 ] || error "a is not on MDT0"
17714
17715         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17716 }
17717 run_test 230f "migrate mulitple remote link files"
17718
17719 test_230g() {
17720         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17721         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17722         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17723                 skip "Need MDS version at least 2.11.52"
17724
17725         mkdir -p $DIR/$tdir/migrate_dir
17726
17727         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
17728                 error "migrating dir to non-exist MDT succeeds"
17729         true
17730 }
17731 run_test 230g "migrate dir to non-exist MDT"
17732
17733 test_230h() {
17734         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17735         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17736         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17737                 skip "Need MDS version at least 2.11.52"
17738
17739         local mdt_index
17740
17741         mkdir -p $DIR/$tdir/migrate_dir
17742
17743         $LFS migrate -m1 $DIR &&
17744                 error "migrating mountpoint1 should fail"
17745
17746         $LFS migrate -m1 $DIR/$tdir/.. &&
17747                 error "migrating mountpoint2 should fail"
17748
17749         # same as mv
17750         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
17751                 error "migrating $tdir/migrate_dir/.. should fail"
17752
17753         true
17754 }
17755 run_test 230h "migrate .. and root"
17756
17757 test_230i() {
17758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17759         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17760         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17761                 skip "Need MDS version at least 2.11.52"
17762
17763         mkdir -p $DIR/$tdir/migrate_dir
17764
17765         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17766                 error "migration fails with a tailing slash"
17767
17768         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17769                 error "migration fails with two tailing slashes"
17770 }
17771 run_test 230i "lfs migrate -m tolerates trailing slashes"
17772
17773 test_230j() {
17774         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17775         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
17776                 skip "Need MDS version at least 2.11.52"
17777
17778         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17779         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17780                 error "create $tfile failed"
17781         cat /etc/passwd > $DIR/$tdir/$tfile
17782
17783         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17784
17785         cmp /etc/passwd $DIR/$tdir/$tfile ||
17786                 error "DoM file mismatch after migration"
17787 }
17788 run_test 230j "DoM file data not changed after dir migration"
17789
17790 test_230k() {
17791         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17792         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17793                 skip "Need MDS version at least 2.11.56"
17794
17795         local total=20
17796         local files_on_starting_mdt=0
17797
17798         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17799         $LFS getdirstripe $DIR/$tdir
17800         for i in $(seq $total); do
17801                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17802                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17803                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17804         done
17805
17806         echo "$files_on_starting_mdt files on MDT0"
17807
17808         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17809         $LFS getdirstripe $DIR/$tdir
17810
17811         files_on_starting_mdt=0
17812         for i in $(seq $total); do
17813                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17814                         error "file $tfile.$i mismatch after migration"
17815                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17816                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17817         done
17818
17819         echo "$files_on_starting_mdt files on MDT1 after migration"
17820         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17821
17822         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17823         $LFS getdirstripe $DIR/$tdir
17824
17825         files_on_starting_mdt=0
17826         for i in $(seq $total); do
17827                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17828                         error "file $tfile.$i mismatch after 2nd migration"
17829                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17830                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17831         done
17832
17833         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17834         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17835
17836         true
17837 }
17838 run_test 230k "file data not changed after dir migration"
17839
17840 test_230l() {
17841         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17842         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17843                 skip "Need MDS version at least 2.11.56"
17844
17845         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17846         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17847                 error "create files under remote dir failed $i"
17848         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17849 }
17850 run_test 230l "readdir between MDTs won't crash"
17851
17852 test_230m() {
17853         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17854         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17855                 skip "Need MDS version at least 2.11.56"
17856
17857         local MDTIDX=1
17858         local mig_dir=$DIR/$tdir/migrate_dir
17859         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17860         local shortstr="b"
17861         local val
17862
17863         echo "Creating files and dirs with xattrs"
17864         test_mkdir $DIR/$tdir
17865         test_mkdir -i0 -c1 $mig_dir
17866         mkdir $mig_dir/dir
17867         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17868                 error "cannot set xattr attr1 on dir"
17869         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17870                 error "cannot set xattr attr2 on dir"
17871         touch $mig_dir/dir/f0
17872         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17873                 error "cannot set xattr attr1 on file"
17874         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17875                 error "cannot set xattr attr2 on file"
17876         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17877         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17878         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17879         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17880         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17881         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17882         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17883         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17884         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17885
17886         echo "Migrating to MDT1"
17887         $LFS migrate -m $MDTIDX $mig_dir ||
17888                 error "fails on migrating dir to MDT1"
17889
17890         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17891         echo "Checking xattrs"
17892         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17893         [ "$val" = $longstr ] ||
17894                 error "expecting xattr1 $longstr on dir, found $val"
17895         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17896         [ "$val" = $shortstr ] ||
17897                 error "expecting xattr2 $shortstr on dir, found $val"
17898         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17899         [ "$val" = $longstr ] ||
17900                 error "expecting xattr1 $longstr on file, found $val"
17901         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17902         [ "$val" = $shortstr ] ||
17903                 error "expecting xattr2 $shortstr on file, found $val"
17904 }
17905 run_test 230m "xattrs not changed after dir migration"
17906
17907 test_230n() {
17908         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17909         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
17910                 skip "Need MDS version at least 2.13.53"
17911
17912         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
17913         cat /etc/hosts > $DIR/$tdir/$tfile
17914         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
17915         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
17916
17917         cmp /etc/hosts $DIR/$tdir/$tfile ||
17918                 error "File data mismatch after migration"
17919 }
17920 run_test 230n "Dir migration with mirrored file"
17921
17922 test_230o() {
17923         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
17924         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
17925                 skip "Need MDS version at least 2.13.52"
17926
17927         local mdts=$(comma_list $(mdts_nodes))
17928
17929         local restripe_status
17930         local delta
17931         local i
17932         local j
17933
17934         # in case "crush" hash type is not set
17935         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
17936
17937         restripe_status=$(do_facet mds1 $LCTL get_param -n \
17938                            mdt.*MDT0000.enable_dir_restripe)
17939         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
17940         stack_trap "do_nodes $mdts $LCTL set_param \
17941                     mdt.*.enable_dir_restripe=$restripe_status"
17942
17943         mkdir $DIR/$tdir
17944         createmany -m $DIR/$tdir/f 100 ||
17945                 error "create files under remote dir failed $i"
17946         createmany -d $DIR/$tdir/d 100 ||
17947                 error "create dirs under remote dir failed $i"
17948
17949         for i in $(seq 2 $MDSCOUNT); do
17950                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
17951                 $LFS setdirstripe -c $i $DIR/$tdir ||
17952                         error "split -c $i $tdir failed"
17953                 wait_update $HOSTNAME \
17954                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 100 ||
17955                         error "dir split not finished"
17956                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
17957                         awk '/migrate/ {sum += $2} END { print sum }')
17958                 echo "$delta files migrated when dir split from $((i - 1)) to $i stripes"
17959                 # delta is around total_files/stripe_count
17960                 [ $delta -lt $((200 /(i - 1))) ] ||
17961                         error "$delta files migrated"
17962         done
17963 }
17964 run_test 230o "dir split"
17965
17966 test_230p() {
17967         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
17968         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
17969                 skip "Need MDS version at least 2.13.52"
17970
17971         local mdts=$(comma_list $(mdts_nodes))
17972
17973         local restripe_status
17974         local delta
17975         local i
17976         local j
17977
17978         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
17979
17980         restripe_status=$(do_facet mds1 $LCTL get_param -n \
17981                            mdt.*MDT0000.enable_dir_restripe)
17982         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_restripe=1"
17983         stack_trap "do_nodes $mdts $LCTL set_param \
17984                     mdt.*.enable_dir_restripe=$restripe_status"
17985
17986         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir
17987         createmany -m $DIR/$tdir/f 100 ||
17988                 error "create files under remote dir failed $i"
17989         createmany -d $DIR/$tdir/d 100 ||
17990                 error "create dirs under remote dir failed $i"
17991
17992         for i in $(seq $((MDSCOUNT - 1)) -1 1); do
17993                 local mdt_hash="crush"
17994
17995                 do_nodes $mdts "$LCTL set_param mdt.*.md_stats=clear > /dev/null"
17996                 $LFS setdirstripe -c $i $DIR/$tdir ||
17997                         error "split -c $i $tdir failed"
17998                 [ $i -eq 1 ] && mdt_hash="none"
17999                 wait_update $HOSTNAME \
18000                         "$LFS getdirstripe -H $DIR/$tdir" $mdt_hash 100 ||
18001                         error "dir merge not finished"
18002                 delta=$(do_nodes $mdts "lctl get_param -n mdt.*MDT*.md_stats" |
18003                         awk '/migrate/ {sum += $2} END { print sum }')
18004                 echo "$delta files migrated when dir merge from $((i + 1)) to $i stripes"
18005                 # delta is around total_files/stripe_count
18006                 [ $delta -lt $((200 / i)) ] ||
18007                         error "$delta files migrated"
18008         done
18009 }
18010 run_test 230p "dir merge"
18011
18012 test_230q() {
18013         [ $MDSCOUNT -ge 2 ] || skip "needs >= 2 MDTs"
18014         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
18015                 skip "Need MDS version at least 2.13.52"
18016
18017         local mdts=$(comma_list $(mdts_nodes))
18018         local saved_threshold=$(do_facet mds1 \
18019                         $LCTL get_param -n mdt.*-MDT0000.dir_split_count)
18020         local saved_delta=$(do_facet mds1 \
18021                         $LCTL get_param -n mdt.*-MDT0000.dir_split_delta)
18022         local threshold=100
18023         local delta=2
18024         local total=0
18025         local stripe_count=0
18026         local stripe_index
18027         local nr_files
18028
18029         stack_trap "do_nodes $mdts $LCTL set_param \
18030                     mdt.*.dir_split_count=$saved_threshold"
18031         stack_trap "do_nodes $mdts $LCTL set_param \
18032                     mdt.*.dir_split_delta=$saved_delta"
18033         stack_trap "do_nodes $mdts $LCTL set_param mdt.*.dir_restripe_nsonly=1"
18034         do_nodes $mdts "$LCTL set_param mdt.*.enable_dir_auto_split=1"
18035         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_count=$threshold"
18036         do_nodes $mdts "$LCTL set_param mdt.*.dir_split_delta=$delta"
18037         do_nodes $mdts "$LCTL set_param mdt.*.dir_restripe_nsonly=0"
18038         do_nodes $mdts "$LCTL set_param lod.*.mdt_hash=crush"
18039
18040         $LFS mkdir -i -1 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
18041         stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
18042
18043         while [ $stripe_count -lt $MDSCOUNT ]; do
18044                 createmany -m $DIR/$tdir/f $total $((threshold * 3 / 2)) ||
18045                         error "create sub files failed"
18046                 stat $DIR/$tdir > /dev/null
18047                 total=$((total + threshold * 3 / 2))
18048                 stripe_count=$((stripe_count + delta))
18049                 [ $stripe_count -gt $MDSCOUNT ] && stripe_count=$MDSCOUNT
18050
18051                 wait_update $HOSTNAME \
18052                         "$LFS getdirstripe -c $DIR/$tdir" "$stripe_count" 40 ||
18053                         error "stripe count $($LFS getdirstripe -c $DIR/$tdir) != $stripe_count"
18054
18055                 wait_update $HOSTNAME \
18056                         "$LFS getdirstripe -H $DIR/$tdir" "crush" 200 ||
18057                         error "stripe hash $($LFS getdirstripe -H $DIR/$tdir) != crush"
18058
18059                 nr_files=$($LFS getstripe -m $DIR/$tdir/* |
18060                            grep -w $stripe_index | wc -l)
18061                 echo "$nr_files files on MDT$stripe_index after split"
18062                 [ $nr_files -lt $((total / (stripe_count - 1))) ] ||
18063                         error "$nr_files files on MDT$stripe_index after split"
18064
18065                 nr_files=$(ls $DIR/$tdir | wc -w)
18066                 [ $nr_files -eq $total ] ||
18067                         error "total sub files $nr_files != $total"
18068         done
18069 }
18070 run_test 230q "dir auto split"
18071
18072 test_231a()
18073 {
18074         # For simplicity this test assumes that max_pages_per_rpc
18075         # is the same across all OSCs
18076         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
18077         local bulk_size=$((max_pages * PAGE_SIZE))
18078         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
18079                                        head -n 1)
18080
18081         mkdir -p $DIR/$tdir
18082         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
18083                 error "failed to set stripe with -S ${brw_size}M option"
18084
18085         # clear the OSC stats
18086         $LCTL set_param osc.*.stats=0 &>/dev/null
18087         stop_writeback
18088
18089         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
18090         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
18091                 oflag=direct &>/dev/null || error "dd failed"
18092
18093         sync; sleep 1; sync # just to be safe
18094         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
18095         if [ x$nrpcs != "x1" ]; then
18096                 $LCTL get_param osc.*.stats
18097                 error "found $nrpcs ost_write RPCs, not 1 as expected"
18098         fi
18099
18100         start_writeback
18101         # Drop the OSC cache, otherwise we will read from it
18102         cancel_lru_locks osc
18103
18104         # clear the OSC stats
18105         $LCTL set_param osc.*.stats=0 &>/dev/null
18106
18107         # Client reads $bulk_size.
18108         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
18109                 iflag=direct &>/dev/null || error "dd failed"
18110
18111         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
18112         if [ x$nrpcs != "x1" ]; then
18113                 $LCTL get_param osc.*.stats
18114                 error "found $nrpcs ost_read RPCs, not 1 as expected"
18115         fi
18116 }
18117 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
18118
18119 test_231b() {
18120         mkdir -p $DIR/$tdir
18121         local i
18122         for i in {0..1023}; do
18123                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
18124                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
18125                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
18126         done
18127         sync
18128 }
18129 run_test 231b "must not assert on fully utilized OST request buffer"
18130
18131 test_232a() {
18132         mkdir -p $DIR/$tdir
18133         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
18134
18135         #define OBD_FAIL_LDLM_OST_LVB            0x31c
18136         do_facet ost1 $LCTL set_param fail_loc=0x31c
18137
18138         # ignore dd failure
18139         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
18140
18141         do_facet ost1 $LCTL set_param fail_loc=0
18142         umount_client $MOUNT || error "umount failed"
18143         mount_client $MOUNT || error "mount failed"
18144         stop ost1 || error "cannot stop ost1"
18145         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18146 }
18147 run_test 232a "failed lock should not block umount"
18148
18149 test_232b() {
18150         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
18151                 skip "Need MDS version at least 2.10.58"
18152
18153         mkdir -p $DIR/$tdir
18154         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
18155         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
18156         sync
18157         cancel_lru_locks osc
18158
18159         #define OBD_FAIL_LDLM_OST_LVB            0x31c
18160         do_facet ost1 $LCTL set_param fail_loc=0x31c
18161
18162         # ignore failure
18163         $LFS data_version $DIR/$tdir/$tfile || true
18164
18165         do_facet ost1 $LCTL set_param fail_loc=0
18166         umount_client $MOUNT || error "umount failed"
18167         mount_client $MOUNT || error "mount failed"
18168         stop ost1 || error "cannot stop ost1"
18169         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
18170 }
18171 run_test 232b "failed data version lock should not block umount"
18172
18173 test_233a() {
18174         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
18175                 skip "Need MDS version at least 2.3.64"
18176         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
18177
18178         local fid=$($LFS path2fid $MOUNT)
18179
18180         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
18181                 error "cannot access $MOUNT using its FID '$fid'"
18182 }
18183 run_test 233a "checking that OBF of the FS root succeeds"
18184
18185 test_233b() {
18186         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
18187                 skip "Need MDS version at least 2.5.90"
18188         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
18189
18190         local fid=$($LFS path2fid $MOUNT/.lustre)
18191
18192         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
18193                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
18194
18195         fid=$($LFS path2fid $MOUNT/.lustre/fid)
18196         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
18197                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
18198 }
18199 run_test 233b "checking that OBF of the FS .lustre succeeds"
18200
18201 test_234() {
18202         local p="$TMP/sanityN-$TESTNAME.parameters"
18203         save_lustre_params client "llite.*.xattr_cache" > $p
18204         lctl set_param llite.*.xattr_cache 1 ||
18205                 skip_env "xattr cache is not supported"
18206
18207         mkdir -p $DIR/$tdir || error "mkdir failed"
18208         touch $DIR/$tdir/$tfile || error "touch failed"
18209         # OBD_FAIL_LLITE_XATTR_ENOMEM
18210         $LCTL set_param fail_loc=0x1405
18211         getfattr -n user.attr $DIR/$tdir/$tfile &&
18212                 error "getfattr should have failed with ENOMEM"
18213         $LCTL set_param fail_loc=0x0
18214         rm -rf $DIR/$tdir
18215
18216         restore_lustre_params < $p
18217         rm -f $p
18218 }
18219 run_test 234 "xattr cache should not crash on ENOMEM"
18220
18221 test_235() {
18222         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
18223                 skip "Need MDS version at least 2.4.52"
18224
18225         flock_deadlock $DIR/$tfile
18226         local RC=$?
18227         case $RC in
18228                 0)
18229                 ;;
18230                 124) error "process hangs on a deadlock"
18231                 ;;
18232                 *) error "error executing flock_deadlock $DIR/$tfile"
18233                 ;;
18234         esac
18235 }
18236 run_test 235 "LU-1715: flock deadlock detection does not work properly"
18237
18238 #LU-2935
18239 test_236() {
18240         check_swap_layouts_support
18241
18242         local ref1=/etc/passwd
18243         local ref2=/etc/group
18244         local file1=$DIR/$tdir/f1
18245         local file2=$DIR/$tdir/f2
18246
18247         test_mkdir -c1 $DIR/$tdir
18248         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
18249         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
18250         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
18251         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
18252         local fd=$(free_fd)
18253         local cmd="exec $fd<>$file2"
18254         eval $cmd
18255         rm $file2
18256         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
18257                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
18258         cmd="exec $fd>&-"
18259         eval $cmd
18260         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
18261
18262         #cleanup
18263         rm -rf $DIR/$tdir
18264 }
18265 run_test 236 "Layout swap on open unlinked file"
18266
18267 # LU-4659 linkea consistency
18268 test_238() {
18269         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
18270                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
18271                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
18272                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
18273
18274         touch $DIR/$tfile
18275         ln $DIR/$tfile $DIR/$tfile.lnk
18276         touch $DIR/$tfile.new
18277         mv $DIR/$tfile.new $DIR/$tfile
18278         local fid1=$($LFS path2fid $DIR/$tfile)
18279         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
18280         local path1=$($LFS fid2path $FSNAME "$fid1")
18281         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
18282         local path2=$($LFS fid2path $FSNAME "$fid2")
18283         [ $tfile.lnk == $path2 ] ||
18284                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
18285         rm -f $DIR/$tfile*
18286 }
18287 run_test 238 "Verify linkea consistency"
18288
18289 test_239A() { # was test_239
18290         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
18291                 skip "Need MDS version at least 2.5.60"
18292
18293         local list=$(comma_list $(mdts_nodes))
18294
18295         mkdir -p $DIR/$tdir
18296         createmany -o $DIR/$tdir/f- 5000
18297         unlinkmany $DIR/$tdir/f- 5000
18298         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
18299                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
18300         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
18301                         osp.*MDT*.sync_in_flight" | calc_sum)
18302         [ "$changes" -eq 0 ] || error "$changes not synced"
18303 }
18304 run_test 239A "osp_sync test"
18305
18306 test_239a() { #LU-5297
18307         remote_mds_nodsh && skip "remote MDS with nodsh"
18308
18309         touch $DIR/$tfile
18310         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
18311         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
18312         chgrp $RUNAS_GID $DIR/$tfile
18313         wait_delete_completed
18314 }
18315 run_test 239a "process invalid osp sync record correctly"
18316
18317 test_239b() { #LU-5297
18318         remote_mds_nodsh && skip "remote MDS with nodsh"
18319
18320         touch $DIR/$tfile1
18321         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
18322         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
18323         chgrp $RUNAS_GID $DIR/$tfile1
18324         wait_delete_completed
18325         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18326         touch $DIR/$tfile2
18327         chgrp $RUNAS_GID $DIR/$tfile2
18328         wait_delete_completed
18329 }
18330 run_test 239b "process osp sync record with ENOMEM error correctly"
18331
18332 test_240() {
18333         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18334         remote_mds_nodsh && skip "remote MDS with nodsh"
18335
18336         mkdir -p $DIR/$tdir
18337
18338         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
18339                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
18340         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
18341                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
18342
18343         umount_client $MOUNT || error "umount failed"
18344         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
18345         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
18346         mount_client $MOUNT || error "failed to mount client"
18347
18348         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
18349         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
18350 }
18351 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
18352
18353 test_241_bio() {
18354         local count=$1
18355         local bsize=$2
18356
18357         for LOOP in $(seq $count); do
18358                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
18359                 cancel_lru_locks $OSC || true
18360         done
18361 }
18362
18363 test_241_dio() {
18364         local count=$1
18365         local bsize=$2
18366
18367         for LOOP in $(seq $1); do
18368                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
18369                         2>/dev/null
18370         done
18371 }
18372
18373 test_241a() { # was test_241
18374         local bsize=$PAGE_SIZE
18375
18376         (( bsize < 40960 )) && bsize=40960
18377         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
18378         ls -la $DIR/$tfile
18379         cancel_lru_locks $OSC
18380         test_241_bio 1000 $bsize &
18381         PID=$!
18382         test_241_dio 1000 $bsize
18383         wait $PID
18384 }
18385 run_test 241a "bio vs dio"
18386
18387 test_241b() {
18388         local bsize=$PAGE_SIZE
18389
18390         (( bsize < 40960 )) && bsize=40960
18391         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
18392         ls -la $DIR/$tfile
18393         test_241_dio 1000 $bsize &
18394         PID=$!
18395         test_241_dio 1000 $bsize
18396         wait $PID
18397 }
18398 run_test 241b "dio vs dio"
18399
18400 test_242() {
18401         remote_mds_nodsh && skip "remote MDS with nodsh"
18402
18403         mkdir -p $DIR/$tdir
18404         touch $DIR/$tdir/$tfile
18405
18406         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
18407         do_facet mds1 lctl set_param fail_loc=0x105
18408         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
18409
18410         do_facet mds1 lctl set_param fail_loc=0
18411         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
18412 }
18413 run_test 242 "mdt_readpage failure should not cause directory unreadable"
18414
18415 test_243()
18416 {
18417         test_mkdir $DIR/$tdir
18418         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
18419 }
18420 run_test 243 "various group lock tests"
18421
18422 test_244a()
18423 {
18424         test_mkdir $DIR/$tdir
18425         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
18426         sendfile_grouplock $DIR/$tdir/$tfile || \
18427                 error "sendfile+grouplock failed"
18428         rm -rf $DIR/$tdir
18429 }
18430 run_test 244a "sendfile with group lock tests"
18431
18432 test_244b()
18433 {
18434         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18435
18436         local threads=50
18437         local size=$((1024*1024))
18438
18439         test_mkdir $DIR/$tdir
18440         for i in $(seq 1 $threads); do
18441                 local file=$DIR/$tdir/file_$((i / 10))
18442                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
18443                 local pids[$i]=$!
18444         done
18445         for i in $(seq 1 $threads); do
18446                 wait ${pids[$i]}
18447         done
18448 }
18449 run_test 244b "multi-threaded write with group lock"
18450
18451 test_245() {
18452         local flagname="multi_mod_rpcs"
18453         local connect_data_name="max_mod_rpcs"
18454         local out
18455
18456         # check if multiple modify RPCs flag is set
18457         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
18458                 grep "connect_flags:")
18459         echo "$out"
18460
18461         echo "$out" | grep -qw $flagname
18462         if [ $? -ne 0 ]; then
18463                 echo "connect flag $flagname is not set"
18464                 return
18465         fi
18466
18467         # check if multiple modify RPCs data is set
18468         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
18469         echo "$out"
18470
18471         echo "$out" | grep -qw $connect_data_name ||
18472                 error "import should have connect data $connect_data_name"
18473 }
18474 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
18475
18476 cleanup_247() {
18477         local submount=$1
18478
18479         trap 0
18480         umount_client $submount
18481         rmdir $submount
18482 }
18483
18484 test_247a() {
18485         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18486                 grep -q subtree ||
18487                 skip_env "Fileset feature is not supported"
18488
18489         local submount=${MOUNT}_$tdir
18490
18491         mkdir $MOUNT/$tdir
18492         mkdir -p $submount || error "mkdir $submount failed"
18493         FILESET="$FILESET/$tdir" mount_client $submount ||
18494                 error "mount $submount failed"
18495         trap "cleanup_247 $submount" EXIT
18496         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
18497         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
18498                 error "read $MOUNT/$tdir/$tfile failed"
18499         cleanup_247 $submount
18500 }
18501 run_test 247a "mount subdir as fileset"
18502
18503 test_247b() {
18504         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18505                 skip_env "Fileset feature is not supported"
18506
18507         local submount=${MOUNT}_$tdir
18508
18509         rm -rf $MOUNT/$tdir
18510         mkdir -p $submount || error "mkdir $submount failed"
18511         SKIP_FILESET=1
18512         FILESET="$FILESET/$tdir" mount_client $submount &&
18513                 error "mount $submount should fail"
18514         rmdir $submount
18515 }
18516 run_test 247b "mount subdir that dose not exist"
18517
18518 test_247c() {
18519         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18520                 skip_env "Fileset feature is not supported"
18521
18522         local submount=${MOUNT}_$tdir
18523
18524         mkdir -p $MOUNT/$tdir/dir1
18525         mkdir -p $submount || error "mkdir $submount failed"
18526         trap "cleanup_247 $submount" EXIT
18527         FILESET="$FILESET/$tdir" mount_client $submount ||
18528                 error "mount $submount failed"
18529         local fid=$($LFS path2fid $MOUNT/)
18530         $LFS fid2path $submount $fid && error "fid2path should fail"
18531         cleanup_247 $submount
18532 }
18533 run_test 247c "running fid2path outside root"
18534
18535 test_247d() {
18536         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18537                 skip "Fileset feature is not supported"
18538
18539         local submount=${MOUNT}_$tdir
18540
18541         mkdir -p $MOUNT/$tdir/dir1
18542         mkdir -p $submount || error "mkdir $submount failed"
18543         FILESET="$FILESET/$tdir" mount_client $submount ||
18544                 error "mount $submount failed"
18545         trap "cleanup_247 $submount" EXIT
18546         local fid=$($LFS path2fid $submount/dir1)
18547         $LFS fid2path $submount $fid || error "fid2path should succeed"
18548         cleanup_247 $submount
18549 }
18550 run_test 247d "running fid2path inside root"
18551
18552 # LU-8037
18553 test_247e() {
18554         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18555                 grep -q subtree ||
18556                 skip "Fileset feature is not supported"
18557
18558         local submount=${MOUNT}_$tdir
18559
18560         mkdir $MOUNT/$tdir
18561         mkdir -p $submount || error "mkdir $submount failed"
18562         FILESET="$FILESET/.." mount_client $submount &&
18563                 error "mount $submount should fail"
18564         rmdir $submount
18565 }
18566 run_test 247e "mount .. as fileset"
18567
18568 test_247f() {
18569         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18570         [ $MDS1_VERSION -lt $(version_code 2.13.52) ] &&
18571                 skip "Need at least version 2.13.52"
18572         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18573                 grep -q subtree ||
18574                 skip "Fileset feature is not supported"
18575
18576         mkdir $DIR/$tdir || error "mkdir $tdir failed"
18577         $LFS mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir/remote ||
18578                 error "mkdir remote failed"
18579         mkdir $DIR/$tdir/remote/subdir || error "mkdir remote/subdir failed"
18580         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/striped ||
18581                 error "mkdir striped failed"
18582         mkdir $DIR/$tdir/striped/subdir || error "mkdir striped/subdir failed"
18583
18584         local submount=${MOUNT}_$tdir
18585
18586         mkdir -p $submount || error "mkdir $submount failed"
18587
18588         local dir
18589         local fileset=$FILESET
18590
18591         for dir in $tdir/remote $tdir/remote/subdir \
18592                    $tdir/striped $tdir/striped/subdir $tdir/striped/. ; do
18593                 FILESET="$fileset/$dir" mount_client $submount ||
18594                         error "mount $dir failed"
18595                 umount_client $submount
18596         done
18597 }
18598 run_test 247f "mount striped or remote directory as fileset"
18599
18600 test_248a() {
18601         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
18602         [ -z "$fast_read_sav" ] && skip "no fast read support"
18603
18604         # create a large file for fast read verification
18605         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
18606
18607         # make sure the file is created correctly
18608         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
18609                 { rm -f $DIR/$tfile; skip "file creation error"; }
18610
18611         echo "Test 1: verify that fast read is 4 times faster on cache read"
18612
18613         # small read with fast read enabled
18614         $LCTL set_param -n llite.*.fast_read=1
18615         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
18616                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18617                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18618         # small read with fast read disabled
18619         $LCTL set_param -n llite.*.fast_read=0
18620         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
18621                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18622                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18623
18624         # verify that fast read is 4 times faster for cache read
18625         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
18626                 error_not_in_vm "fast read was not 4 times faster: " \
18627                            "$t_fast vs $t_slow"
18628
18629         echo "Test 2: verify the performance between big and small read"
18630         $LCTL set_param -n llite.*.fast_read=1
18631
18632         # 1k non-cache read
18633         cancel_lru_locks osc
18634         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
18635                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18636                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18637
18638         # 1M non-cache read
18639         cancel_lru_locks osc
18640         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
18641                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18642                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18643
18644         # verify that big IO is not 4 times faster than small IO
18645         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
18646                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
18647
18648         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
18649         rm -f $DIR/$tfile
18650 }
18651 run_test 248a "fast read verification"
18652
18653 test_248b() {
18654         # Default short_io_bytes=16384, try both smaller and larger sizes.
18655         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
18656         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
18657         echo "bs=53248 count=113 normal buffered write"
18658         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
18659                 error "dd of initial data file failed"
18660         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
18661
18662         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
18663         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
18664                 error "dd with sync normal writes failed"
18665         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
18666
18667         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
18668         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
18669                 error "dd with sync small writes failed"
18670         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
18671
18672         cancel_lru_locks osc
18673
18674         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
18675         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
18676         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
18677         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
18678                 iflag=direct || error "dd with O_DIRECT small read failed"
18679         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
18680         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
18681                 error "compare $TMP/$tfile.1 failed"
18682
18683         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
18684         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
18685
18686         # just to see what the maximum tunable value is, and test parsing
18687         echo "test invalid parameter 2MB"
18688         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
18689                 error "too-large short_io_bytes allowed"
18690         echo "test maximum parameter 512KB"
18691         # if we can set a larger short_io_bytes, run test regardless of version
18692         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
18693                 # older clients may not allow setting it this large, that's OK
18694                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
18695                         skip "Need at least client version 2.13.50"
18696                 error "medium short_io_bytes failed"
18697         fi
18698         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18699         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
18700
18701         echo "test large parameter 64KB"
18702         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
18703         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18704
18705         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
18706         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
18707                 error "dd with sync large writes failed"
18708         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
18709
18710         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
18711         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
18712         num=$((113 * 4096 / PAGE_SIZE))
18713         echo "bs=$size count=$num oflag=direct large write $tfile.3"
18714         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
18715                 error "dd with O_DIRECT large writes failed"
18716         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
18717                 error "compare $DIR/$tfile.3 failed"
18718
18719         cancel_lru_locks osc
18720
18721         echo "bs=$size count=$num iflag=direct large read $tfile.2"
18722         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
18723                 error "dd with O_DIRECT large read failed"
18724         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
18725                 error "compare $TMP/$tfile.2 failed"
18726
18727         echo "bs=$size count=$num iflag=direct large read $tfile.3"
18728         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
18729                 error "dd with O_DIRECT large read failed"
18730         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
18731                 error "compare $TMP/$tfile.3 failed"
18732 }
18733 run_test 248b "test short_io read and write for both small and large sizes"
18734
18735 test_249() { # LU-7890
18736         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
18737                 skip "Need at least version 2.8.54"
18738
18739         rm -f $DIR/$tfile
18740         $LFS setstripe -c 1 $DIR/$tfile
18741         # Offset 2T == 4k * 512M
18742         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
18743                 error "dd to 2T offset failed"
18744 }
18745 run_test 249 "Write above 2T file size"
18746
18747 test_250() {
18748         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
18749          && skip "no 16TB file size limit on ZFS"
18750
18751         $LFS setstripe -c 1 $DIR/$tfile
18752         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
18753         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
18754         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
18755         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
18756                 conv=notrunc,fsync && error "append succeeded"
18757         return 0
18758 }
18759 run_test 250 "Write above 16T limit"
18760
18761 test_251() {
18762         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
18763
18764         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
18765         #Skip once - writing the first stripe will succeed
18766         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18767         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
18768                 error "short write happened"
18769
18770         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18771         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
18772                 error "short read happened"
18773
18774         rm -f $DIR/$tfile
18775 }
18776 run_test 251 "Handling short read and write correctly"
18777
18778 test_252() {
18779         remote_mds_nodsh && skip "remote MDS with nodsh"
18780         remote_ost_nodsh && skip "remote OST with nodsh"
18781         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
18782                 skip_env "ldiskfs only test"
18783         fi
18784
18785         local tgt
18786         local dev
18787         local out
18788         local uuid
18789         local num
18790         local gen
18791
18792         # check lr_reader on OST0000
18793         tgt=ost1
18794         dev=$(facet_device $tgt)
18795         out=$(do_facet $tgt $LR_READER $dev)
18796         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18797         echo "$out"
18798         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
18799         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
18800                 error "Invalid uuid returned by $LR_READER on target $tgt"
18801         echo -e "uuid returned by $LR_READER is '$uuid'\n"
18802
18803         # check lr_reader -c on MDT0000
18804         tgt=mds1
18805         dev=$(facet_device $tgt)
18806         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
18807                 skip "$LR_READER does not support additional options"
18808         fi
18809         out=$(do_facet $tgt $LR_READER -c $dev)
18810         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18811         echo "$out"
18812         num=$(echo "$out" | grep -c "mdtlov")
18813         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
18814                 error "Invalid number of mdtlov clients returned by $LR_READER"
18815         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
18816
18817         # check lr_reader -cr on MDT0000
18818         out=$(do_facet $tgt $LR_READER -cr $dev)
18819         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18820         echo "$out"
18821         echo "$out" | grep -q "^reply_data:$" ||
18822                 error "$LR_READER should have returned 'reply_data' section"
18823         num=$(echo "$out" | grep -c "client_generation")
18824         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
18825 }
18826 run_test 252 "check lr_reader tool"
18827
18828 test_253() {
18829         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18830         remote_mds_nodsh && skip "remote MDS with nodsh"
18831         remote_mgs_nodsh && skip "remote MGS with nodsh"
18832
18833         local ostidx=0
18834         local rc=0
18835         local ost_name=$(ostname_from_index $ostidx)
18836
18837         # on the mdt's osc
18838         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
18839         do_facet $SINGLEMDS $LCTL get_param -n \
18840                 osp.$mdtosc_proc1.reserved_mb_high ||
18841                 skip  "remote MDS does not support reserved_mb_high"
18842
18843         rm -rf $DIR/$tdir
18844         wait_mds_ost_sync
18845         wait_delete_completed
18846         mkdir $DIR/$tdir
18847
18848         pool_add $TESTNAME || error "Pool creation failed"
18849         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
18850
18851         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
18852                 error "Setstripe failed"
18853
18854         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
18855
18856         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
18857                     grep "watermarks")
18858         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
18859
18860         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18861                         osp.$mdtosc_proc1.prealloc_status)
18862         echo "prealloc_status $oa_status"
18863
18864         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
18865                 error "File creation should fail"
18866
18867         #object allocation was stopped, but we still able to append files
18868         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
18869                 oflag=append || error "Append failed"
18870
18871         rm -f $DIR/$tdir/$tfile.0
18872
18873         # For this test, we want to delete the files we created to go out of
18874         # space but leave the watermark, so we remain nearly out of space
18875         ost_watermarks_enospc_delete_files $tfile $ostidx
18876
18877         wait_delete_completed
18878
18879         sleep_maxage
18880
18881         for i in $(seq 10 12); do
18882                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
18883                         2>/dev/null || error "File creation failed after rm"
18884         done
18885
18886         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18887                         osp.$mdtosc_proc1.prealloc_status)
18888         echo "prealloc_status $oa_status"
18889
18890         if (( oa_status != 0 )); then
18891                 error "Object allocation still disable after rm"
18892         fi
18893 }
18894 run_test 253 "Check object allocation limit"
18895
18896 test_254() {
18897         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18898         remote_mds_nodsh && skip "remote MDS with nodsh"
18899         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
18900                 skip "MDS does not support changelog_size"
18901
18902         local cl_user
18903         local MDT0=$(facet_svc $SINGLEMDS)
18904
18905         changelog_register || error "changelog_register failed"
18906
18907         changelog_clear 0 || error "changelog_clear failed"
18908
18909         local size1=$(do_facet $SINGLEMDS \
18910                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18911         echo "Changelog size $size1"
18912
18913         rm -rf $DIR/$tdir
18914         $LFS mkdir -i 0 $DIR/$tdir
18915         # change something
18916         mkdir -p $DIR/$tdir/pics/2008/zachy
18917         touch $DIR/$tdir/pics/2008/zachy/timestamp
18918         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
18919         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
18920         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
18921         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
18922         rm $DIR/$tdir/pics/desktop.jpg
18923
18924         local size2=$(do_facet $SINGLEMDS \
18925                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18926         echo "Changelog size after work $size2"
18927
18928         (( $size2 > $size1 )) ||
18929                 error "new Changelog size=$size2 less than old size=$size1"
18930 }
18931 run_test 254 "Check changelog size"
18932
18933 ladvise_no_type()
18934 {
18935         local type=$1
18936         local file=$2
18937
18938         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
18939                 awk -F: '{print $2}' | grep $type > /dev/null
18940         if [ $? -ne 0 ]; then
18941                 return 0
18942         fi
18943         return 1
18944 }
18945
18946 ladvise_no_ioctl()
18947 {
18948         local file=$1
18949
18950         lfs ladvise -a willread $file > /dev/null 2>&1
18951         if [ $? -eq 0 ]; then
18952                 return 1
18953         fi
18954
18955         lfs ladvise -a willread $file 2>&1 |
18956                 grep "Inappropriate ioctl for device" > /dev/null
18957         if [ $? -eq 0 ]; then
18958                 return 0
18959         fi
18960         return 1
18961 }
18962
18963 percent() {
18964         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18965 }
18966
18967 # run a random read IO workload
18968 # usage: random_read_iops <filename> <filesize> <iosize>
18969 random_read_iops() {
18970         local file=$1
18971         local fsize=$2
18972         local iosize=${3:-4096}
18973
18974         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18975                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18976 }
18977
18978 drop_file_oss_cache() {
18979         local file="$1"
18980         local nodes="$2"
18981
18982         $LFS ladvise -a dontneed $file 2>/dev/null ||
18983                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18984 }
18985
18986 ladvise_willread_performance()
18987 {
18988         local repeat=10
18989         local average_origin=0
18990         local average_cache=0
18991         local average_ladvise=0
18992
18993         for ((i = 1; i <= $repeat; i++)); do
18994                 echo "Iter $i/$repeat: reading without willread hint"
18995                 cancel_lru_locks osc
18996                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18997                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18998                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18999                 average_origin=$(bc <<<"$average_origin + $speed_origin")
19000
19001                 cancel_lru_locks osc
19002                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
19003                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
19004                 average_cache=$(bc <<<"$average_cache + $speed_cache")
19005
19006                 cancel_lru_locks osc
19007                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
19008                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
19009                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
19010                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
19011                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
19012         done
19013         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
19014         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
19015         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
19016
19017         speedup_cache=$(percent $average_cache $average_origin)
19018         speedup_ladvise=$(percent $average_ladvise $average_origin)
19019
19020         echo "Average uncached read: $average_origin"
19021         echo "Average speedup with OSS cached read: " \
19022                 "$average_cache = +$speedup_cache%"
19023         echo "Average speedup with ladvise willread: " \
19024                 "$average_ladvise = +$speedup_ladvise%"
19025
19026         local lowest_speedup=20
19027         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
19028                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
19029                         "got $average_cache%. Skipping ladvise willread check."
19030                 return 0
19031         fi
19032
19033         # the test won't work on ZFS until it supports 'ladvise dontneed', but
19034         # it is still good to run until then to exercise 'ladvise willread'
19035         ! $LFS ladvise -a dontneed $DIR/$tfile &&
19036                 [ "$ost1_FSTYPE" = "zfs" ] &&
19037                 echo "osd-zfs does not support dontneed or drop_caches" &&
19038                 return 0
19039
19040         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
19041         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
19042                 error_not_in_vm "Speedup with willread is less than " \
19043                         "$lowest_speedup%, got $average_ladvise%"
19044 }
19045
19046 test_255a() {
19047         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
19048                 skip "lustre < 2.8.54 does not support ladvise "
19049         remote_ost_nodsh && skip "remote OST with nodsh"
19050
19051         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
19052
19053         ladvise_no_type willread $DIR/$tfile &&
19054                 skip "willread ladvise is not supported"
19055
19056         ladvise_no_ioctl $DIR/$tfile &&
19057                 skip "ladvise ioctl is not supported"
19058
19059         local size_mb=100
19060         local size=$((size_mb * 1048576))
19061         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
19062                 error "dd to $DIR/$tfile failed"
19063
19064         lfs ladvise -a willread $DIR/$tfile ||
19065                 error "Ladvise failed with no range argument"
19066
19067         lfs ladvise -a willread -s 0 $DIR/$tfile ||
19068                 error "Ladvise failed with no -l or -e argument"
19069
19070         lfs ladvise -a willread -e 1 $DIR/$tfile ||
19071                 error "Ladvise failed with only -e argument"
19072
19073         lfs ladvise -a willread -l 1 $DIR/$tfile ||
19074                 error "Ladvise failed with only -l argument"
19075
19076         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
19077                 error "End offset should not be smaller than start offset"
19078
19079         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
19080                 error "End offset should not be equal to start offset"
19081
19082         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
19083                 error "Ladvise failed with overflowing -s argument"
19084
19085         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
19086                 error "Ladvise failed with overflowing -e argument"
19087
19088         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
19089                 error "Ladvise failed with overflowing -l argument"
19090
19091         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
19092                 error "Ladvise succeeded with conflicting -l and -e arguments"
19093
19094         echo "Synchronous ladvise should wait"
19095         local delay=4
19096 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
19097         do_nodes $(comma_list $(osts_nodes)) \
19098                 $LCTL set_param fail_val=$delay fail_loc=0x237
19099
19100         local start_ts=$SECONDS
19101         lfs ladvise -a willread $DIR/$tfile ||
19102                 error "Ladvise failed with no range argument"
19103         local end_ts=$SECONDS
19104         local inteval_ts=$((end_ts - start_ts))
19105
19106         if [ $inteval_ts -lt $(($delay - 1)) ]; then
19107                 error "Synchronous advice didn't wait reply"
19108         fi
19109
19110         echo "Asynchronous ladvise shouldn't wait"
19111         local start_ts=$SECONDS
19112         lfs ladvise -a willread -b $DIR/$tfile ||
19113                 error "Ladvise failed with no range argument"
19114         local end_ts=$SECONDS
19115         local inteval_ts=$((end_ts - start_ts))
19116
19117         if [ $inteval_ts -gt $(($delay / 2)) ]; then
19118                 error "Asynchronous advice blocked"
19119         fi
19120
19121         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
19122         ladvise_willread_performance
19123 }
19124 run_test 255a "check 'lfs ladvise -a willread'"
19125
19126 facet_meminfo() {
19127         local facet=$1
19128         local info=$2
19129
19130         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
19131 }
19132
19133 test_255b() {
19134         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
19135                 skip "lustre < 2.8.54 does not support ladvise "
19136         remote_ost_nodsh && skip "remote OST with nodsh"
19137
19138         lfs setstripe -c 1 -i 0 $DIR/$tfile
19139
19140         ladvise_no_type dontneed $DIR/$tfile &&
19141                 skip "dontneed ladvise is not supported"
19142
19143         ladvise_no_ioctl $DIR/$tfile &&
19144                 skip "ladvise ioctl is not supported"
19145
19146         ! $LFS ladvise -a dontneed $DIR/$tfile &&
19147                 [ "$ost1_FSTYPE" = "zfs" ] &&
19148                 skip "zfs-osd does not support 'ladvise dontneed'"
19149
19150         local size_mb=100
19151         local size=$((size_mb * 1048576))
19152         # In order to prevent disturbance of other processes, only check 3/4
19153         # of the memory usage
19154         local kibibytes=$((size_mb * 1024 * 3 / 4))
19155
19156         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
19157                 error "dd to $DIR/$tfile failed"
19158
19159         #force write to complete before dropping OST cache & checking memory
19160         sync
19161
19162         local total=$(facet_meminfo ost1 MemTotal)
19163         echo "Total memory: $total KiB"
19164
19165         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
19166         local before_read=$(facet_meminfo ost1 Cached)
19167         echo "Cache used before read: $before_read KiB"
19168
19169         lfs ladvise -a willread $DIR/$tfile ||
19170                 error "Ladvise willread failed"
19171         local after_read=$(facet_meminfo ost1 Cached)
19172         echo "Cache used after read: $after_read KiB"
19173
19174         lfs ladvise -a dontneed $DIR/$tfile ||
19175                 error "Ladvise dontneed again failed"
19176         local no_read=$(facet_meminfo ost1 Cached)
19177         echo "Cache used after dontneed ladvise: $no_read KiB"
19178
19179         if [ $total -lt $((before_read + kibibytes)) ]; then
19180                 echo "Memory is too small, abort checking"
19181                 return 0
19182         fi
19183
19184         if [ $((before_read + kibibytes)) -gt $after_read ]; then
19185                 error "Ladvise willread should use more memory" \
19186                         "than $kibibytes KiB"
19187         fi
19188
19189         if [ $((no_read + kibibytes)) -gt $after_read ]; then
19190                 error "Ladvise dontneed should release more memory" \
19191                         "than $kibibytes KiB"
19192         fi
19193 }
19194 run_test 255b "check 'lfs ladvise -a dontneed'"
19195
19196 test_255c() {
19197         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
19198                 skip "lustre < 2.10.50 does not support lockahead"
19199
19200         local count
19201         local new_count
19202         local difference
19203         local i
19204         local rc
19205
19206         test_mkdir -p $DIR/$tdir
19207         $LFS setstripe -i 0 -c 1 $DIR/$tdir
19208
19209         #test 10 returns only success/failure
19210         i=10
19211         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19212         rc=$?
19213         if [ $rc -eq 255 ]; then
19214                 error "Ladvise test${i} failed, ${rc}"
19215         fi
19216
19217         #test 11 counts lock enqueue requests, all others count new locks
19218         i=11
19219         count=$(do_facet ost1 \
19220                 $LCTL get_param -n ost.OSS.ost.stats)
19221         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
19222
19223         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19224         rc=$?
19225         if [ $rc -eq 255 ]; then
19226                 error "Ladvise test${i} failed, ${rc}"
19227         fi
19228
19229         new_count=$(do_facet ost1 \
19230                 $LCTL get_param -n ost.OSS.ost.stats)
19231         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
19232                    awk '{ print $2 }')
19233
19234         difference="$((new_count - count))"
19235         if [ $difference -ne $rc ]; then
19236                 error "Ladvise test${i}, bad enqueue count, returned " \
19237                       "${rc}, actual ${difference}"
19238         fi
19239
19240         for i in $(seq 12 21); do
19241                 # If we do not do this, we run the risk of having too many
19242                 # locks and starting lock cancellation while we are checking
19243                 # lock counts.
19244                 cancel_lru_locks osc
19245
19246                 count=$($LCTL get_param -n \
19247                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
19248
19249                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
19250                 rc=$?
19251                 if [ $rc -eq 255 ]; then
19252                         error "Ladvise test ${i} failed, ${rc}"
19253                 fi
19254
19255                 new_count=$($LCTL get_param -n \
19256                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
19257                 difference="$((new_count - count))"
19258
19259                 # Test 15 output is divided by 100 to map down to valid return
19260                 if [ $i -eq 15 ]; then
19261                         rc="$((rc * 100))"
19262                 fi
19263
19264                 if [ $difference -ne $rc ]; then
19265                         error "Ladvise test ${i}, bad lock count, returned " \
19266                               "${rc}, actual ${difference}"
19267                 fi
19268         done
19269
19270         #test 22 returns only success/failure
19271         i=22
19272         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19273         rc=$?
19274         if [ $rc -eq 255 ]; then
19275                 error "Ladvise test${i} failed, ${rc}"
19276         fi
19277 }
19278 run_test 255c "suite of ladvise lockahead tests"
19279
19280 test_256() {
19281         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19282         remote_mds_nodsh && skip "remote MDS with nodsh"
19283         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
19284         changelog_users $SINGLEMDS | grep "^cl" &&
19285                 skip "active changelog user"
19286
19287         local cl_user
19288         local cat_sl
19289         local mdt_dev
19290
19291         mdt_dev=$(mdsdevname 1)
19292         echo $mdt_dev
19293
19294         changelog_register || error "changelog_register failed"
19295
19296         rm -rf $DIR/$tdir
19297         mkdir -p $DIR/$tdir
19298
19299         changelog_clear 0 || error "changelog_clear failed"
19300
19301         # change something
19302         touch $DIR/$tdir/{1..10}
19303
19304         # stop the MDT
19305         stop $SINGLEMDS || error "Fail to stop MDT"
19306
19307         # remount the MDT
19308
19309         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
19310
19311         #after mount new plainllog is used
19312         touch $DIR/$tdir/{11..19}
19313         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
19314         stack_trap "rm -f $tmpfile"
19315         cat_sl=$(do_facet $SINGLEMDS "sync; \
19316                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
19317                  llog_reader $tmpfile | grep -c type=1064553b")
19318         do_facet $SINGLEMDS llog_reader $tmpfile
19319
19320         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
19321
19322         changelog_clear 0 || error "changelog_clear failed"
19323
19324         cat_sl=$(do_facet $SINGLEMDS "sync; \
19325                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
19326                  llog_reader $tmpfile | grep -c type=1064553b")
19327
19328         if (( cat_sl == 2 )); then
19329                 error "Empty plain llog was not deleted from changelog catalog"
19330         elif (( cat_sl != 1 )); then
19331                 error "Active plain llog shouldn't be deleted from catalog"
19332         fi
19333 }
19334 run_test 256 "Check llog delete for empty and not full state"
19335
19336 test_257() {
19337         remote_mds_nodsh && skip "remote MDS with nodsh"
19338         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19339                 skip "Need MDS version at least 2.8.55"
19340
19341         test_mkdir $DIR/$tdir
19342
19343         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
19344                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
19345         stat $DIR/$tdir
19346
19347 #define OBD_FAIL_MDS_XATTR_REP                  0x161
19348         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19349         local facet=mds$((mdtidx + 1))
19350         set_nodes_failloc $(facet_active_host $facet) 0x80000161
19351         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
19352
19353         stop $facet || error "stop MDS failed"
19354         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
19355                 error "start MDS fail"
19356         wait_recovery_complete $facet
19357 }
19358 run_test 257 "xattr locks are not lost"
19359
19360 # Verify we take the i_mutex when security requires it
19361 test_258a() {
19362 #define OBD_FAIL_IMUTEX_SEC 0x141c
19363         $LCTL set_param fail_loc=0x141c
19364         touch $DIR/$tfile
19365         chmod u+s $DIR/$tfile
19366         chmod a+rwx $DIR/$tfile
19367         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
19368         RC=$?
19369         if [ $RC -ne 0 ]; then
19370                 error "error, failed to take i_mutex, rc=$?"
19371         fi
19372         rm -f $DIR/$tfile
19373 }
19374 run_test 258a "verify i_mutex security behavior when suid attributes is set"
19375
19376 # Verify we do NOT take the i_mutex in the normal case
19377 test_258b() {
19378 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
19379         $LCTL set_param fail_loc=0x141d
19380         touch $DIR/$tfile
19381         chmod a+rwx $DIR
19382         chmod a+rw $DIR/$tfile
19383         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
19384         RC=$?
19385         if [ $RC -ne 0 ]; then
19386                 error "error, took i_mutex unnecessarily, rc=$?"
19387         fi
19388         rm -f $DIR/$tfile
19389
19390 }
19391 run_test 258b "verify i_mutex security behavior"
19392
19393 test_259() {
19394         local file=$DIR/$tfile
19395         local before
19396         local after
19397
19398         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
19399
19400         stack_trap "rm -f $file" EXIT
19401
19402         wait_delete_completed
19403         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19404         echo "before: $before"
19405
19406         $LFS setstripe -i 0 -c 1 $file
19407         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
19408         sync_all_data
19409         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19410         echo "after write: $after"
19411
19412 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
19413         do_facet ost1 $LCTL set_param fail_loc=0x2301
19414         $TRUNCATE $file 0
19415         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19416         echo "after truncate: $after"
19417
19418         stop ost1
19419         do_facet ost1 $LCTL set_param fail_loc=0
19420         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
19421         sleep 2
19422         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19423         echo "after restart: $after"
19424         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
19425                 error "missing truncate?"
19426
19427         return 0
19428 }
19429 run_test 259 "crash at delayed truncate"
19430
19431 test_260() {
19432 #define OBD_FAIL_MDC_CLOSE               0x806
19433         $LCTL set_param fail_loc=0x80000806
19434         touch $DIR/$tfile
19435
19436 }
19437 run_test 260 "Check mdc_close fail"
19438
19439 ### Data-on-MDT sanity tests ###
19440 test_270a() {
19441         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19442                 skip "Need MDS version at least 2.10.55 for DoM"
19443
19444         # create DoM file
19445         local dom=$DIR/$tdir/dom_file
19446         local tmp=$DIR/$tdir/tmp_file
19447
19448         mkdir -p $DIR/$tdir
19449
19450         # basic checks for DoM component creation
19451         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
19452                 error "Can set MDT layout to non-first entry"
19453
19454         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
19455                 error "Can define multiple entries as MDT layout"
19456
19457         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
19458
19459         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
19460         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
19461         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
19462
19463         local mdtidx=$($LFS getstripe -m $dom)
19464         local mdtname=MDT$(printf %04x $mdtidx)
19465         local facet=mds$((mdtidx + 1))
19466         local space_check=1
19467
19468         # Skip free space checks with ZFS
19469         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
19470
19471         # write
19472         sync
19473         local size_tmp=$((65536 * 3))
19474         local mdtfree1=$(do_facet $facet \
19475                          lctl get_param -n osd*.*$mdtname.kbytesfree)
19476
19477         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
19478         # check also direct IO along write
19479         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
19480         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
19481         sync
19482         cmp $tmp $dom || error "file data is different"
19483         [ $(stat -c%s $dom) == $size_tmp ] ||
19484                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
19485         if [ $space_check == 1 ]; then
19486                 local mdtfree2=$(do_facet $facet \
19487                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
19488
19489                 # increase in usage from by $size_tmp
19490                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
19491                         error "MDT free space wrong after write: " \
19492                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
19493         fi
19494
19495         # truncate
19496         local size_dom=10000
19497
19498         $TRUNCATE $dom $size_dom
19499         [ $(stat -c%s $dom) == $size_dom ] ||
19500                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
19501         if [ $space_check == 1 ]; then
19502                 mdtfree1=$(do_facet $facet \
19503                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19504                 # decrease in usage from $size_tmp to new $size_dom
19505                 [ $(($mdtfree1 - $mdtfree2)) -ge \
19506                   $(((size_tmp - size_dom) / 1024)) ] ||
19507                         error "MDT free space is wrong after truncate: " \
19508                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
19509         fi
19510
19511         # append
19512         cat $tmp >> $dom
19513         sync
19514         size_dom=$((size_dom + size_tmp))
19515         [ $(stat -c%s $dom) == $size_dom ] ||
19516                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
19517         if [ $space_check == 1 ]; then
19518                 mdtfree2=$(do_facet $facet \
19519                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19520                 # increase in usage by $size_tmp from previous
19521                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
19522                         error "MDT free space is wrong after append: " \
19523                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
19524         fi
19525
19526         # delete
19527         rm $dom
19528         if [ $space_check == 1 ]; then
19529                 mdtfree1=$(do_facet $facet \
19530                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19531                 # decrease in usage by $size_dom from previous
19532                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
19533                         error "MDT free space is wrong after removal: " \
19534                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
19535         fi
19536
19537         # combined striping
19538         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
19539                 error "Can't create DoM + OST striping"
19540
19541         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
19542         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
19543         # check also direct IO along write
19544         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
19545         sync
19546         cmp $tmp $dom || error "file data is different"
19547         [ $(stat -c%s $dom) == $size_tmp ] ||
19548                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
19549         rm $dom $tmp
19550
19551         return 0
19552 }
19553 run_test 270a "DoM: basic functionality tests"
19554
19555 test_270b() {
19556         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19557                 skip "Need MDS version at least 2.10.55"
19558
19559         local dom=$DIR/$tdir/dom_file
19560         local max_size=1048576
19561
19562         mkdir -p $DIR/$tdir
19563         $LFS setstripe -E $max_size -L mdt $dom
19564
19565         # truncate over the limit
19566         $TRUNCATE $dom $(($max_size + 1)) &&
19567                 error "successful truncate over the maximum size"
19568         # write over the limit
19569         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
19570                 error "successful write over the maximum size"
19571         # append over the limit
19572         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
19573         echo "12345" >> $dom && error "successful append over the maximum size"
19574         rm $dom
19575
19576         return 0
19577 }
19578 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
19579
19580 test_270c() {
19581         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19582                 skip "Need MDS version at least 2.10.55"
19583
19584         mkdir -p $DIR/$tdir
19585         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19586
19587         # check files inherit DoM EA
19588         touch $DIR/$tdir/first
19589         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
19590                 error "bad pattern"
19591         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
19592                 error "bad stripe count"
19593         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
19594                 error "bad stripe size"
19595
19596         # check directory inherits DoM EA and uses it as default
19597         mkdir $DIR/$tdir/subdir
19598         touch $DIR/$tdir/subdir/second
19599         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
19600                 error "bad pattern in sub-directory"
19601         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
19602                 error "bad stripe count in sub-directory"
19603         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
19604                 error "bad stripe size in sub-directory"
19605         return 0
19606 }
19607 run_test 270c "DoM: DoM EA inheritance tests"
19608
19609 test_270d() {
19610         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19611                 skip "Need MDS version at least 2.10.55"
19612
19613         mkdir -p $DIR/$tdir
19614         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19615
19616         # inherit default DoM striping
19617         mkdir $DIR/$tdir/subdir
19618         touch $DIR/$tdir/subdir/f1
19619
19620         # change default directory striping
19621         $LFS setstripe -c 1 $DIR/$tdir/subdir
19622         touch $DIR/$tdir/subdir/f2
19623         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
19624                 error "wrong default striping in file 2"
19625         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
19626                 error "bad pattern in file 2"
19627         return 0
19628 }
19629 run_test 270d "DoM: change striping from DoM to RAID0"
19630
19631 test_270e() {
19632         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19633                 skip "Need MDS version at least 2.10.55"
19634
19635         mkdir -p $DIR/$tdir/dom
19636         mkdir -p $DIR/$tdir/norm
19637         DOMFILES=20
19638         NORMFILES=10
19639         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
19640         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
19641
19642         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
19643         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
19644
19645         # find DoM files by layout
19646         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
19647         [ $NUM -eq  $DOMFILES ] ||
19648                 error "lfs find -L: found $NUM, expected $DOMFILES"
19649         echo "Test 1: lfs find 20 DOM files by layout: OK"
19650
19651         # there should be 1 dir with default DOM striping
19652         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
19653         [ $NUM -eq  1 ] ||
19654                 error "lfs find -L: found $NUM, expected 1 dir"
19655         echo "Test 2: lfs find 1 DOM dir by layout: OK"
19656
19657         # find DoM files by stripe size
19658         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
19659         [ $NUM -eq  $DOMFILES ] ||
19660                 error "lfs find -S: found $NUM, expected $DOMFILES"
19661         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
19662
19663         # find files by stripe offset except DoM files
19664         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
19665         [ $NUM -eq  $NORMFILES ] ||
19666                 error "lfs find -i: found $NUM, expected $NORMFILES"
19667         echo "Test 5: lfs find no DOM files by stripe index: OK"
19668         return 0
19669 }
19670 run_test 270e "DoM: lfs find with DoM files test"
19671
19672 test_270f() {
19673         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19674                 skip "Need MDS version at least 2.10.55"
19675
19676         local mdtname=${FSNAME}-MDT0000-mdtlov
19677         local dom=$DIR/$tdir/dom_file
19678         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
19679                                                 lod.$mdtname.dom_stripesize)
19680         local dom_limit=131072
19681
19682         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
19683         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19684                                                 lod.$mdtname.dom_stripesize)
19685         [ ${dom_limit} -eq ${dom_current} ] ||
19686                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
19687
19688         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19689         $LFS setstripe -d $DIR/$tdir
19690         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
19691                 error "Can't set directory default striping"
19692
19693         # exceed maximum stripe size
19694         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19695                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
19696         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
19697                 error "Able to create DoM component size more than LOD limit"
19698
19699         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
19700         dom_current=$(do_facet mds1 $LCTL get_param -n \
19701                                                 lod.$mdtname.dom_stripesize)
19702         [ 0 -eq ${dom_current} ] ||
19703                 error "Can't set zero DoM stripe limit"
19704         rm $dom
19705
19706         # attempt to create DoM file on server with disabled DoM should
19707         # remove DoM entry from layout and be succeed
19708         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
19709                 error "Can't create DoM file (DoM is disabled)"
19710         [ $($LFS getstripe -L $dom) == "mdt" ] &&
19711                 error "File has DoM component while DoM is disabled"
19712         rm $dom
19713
19714         # attempt to create DoM file with only DoM stripe should return error
19715         $LFS setstripe -E $dom_limit -L mdt $dom &&
19716                 error "Able to create DoM-only file while DoM is disabled"
19717
19718         # too low values to be aligned with smallest stripe size 64K
19719         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
19720         dom_current=$(do_facet mds1 $LCTL get_param -n \
19721                                                 lod.$mdtname.dom_stripesize)
19722         [ 30000 -eq ${dom_current} ] &&
19723                 error "Can set too small DoM stripe limit"
19724
19725         # 64K is a minimal stripe size in Lustre, expect limit of that size
19726         [ 65536 -eq ${dom_current} ] ||
19727                 error "Limit is not set to 64K but ${dom_current}"
19728
19729         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
19730         dom_current=$(do_facet mds1 $LCTL get_param -n \
19731                                                 lod.$mdtname.dom_stripesize)
19732         echo $dom_current
19733         [ 2147483648 -eq ${dom_current} ] &&
19734                 error "Can set too large DoM stripe limit"
19735
19736         do_facet mds1 $LCTL set_param -n \
19737                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
19738         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19739                 error "Can't create DoM component size after limit change"
19740         do_facet mds1 $LCTL set_param -n \
19741                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
19742         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
19743                 error "Can't create DoM file after limit decrease"
19744         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
19745                 error "Can create big DoM component after limit decrease"
19746         touch ${dom}_def ||
19747                 error "Can't create file with old default layout"
19748
19749         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
19750         return 0
19751 }
19752 run_test 270f "DoM: maximum DoM stripe size checks"
19753
19754 test_270g() {
19755         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
19756                 skip "Need MDS version at least 2.13.52"
19757         local dom=$DIR/$tdir/$tfile
19758
19759         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19760         local lodname=${FSNAME}-MDT0000-mdtlov
19761
19762         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
19763         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
19764         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
19765         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
19766
19767         local dom_limit=1024
19768         local dom_threshold="50%"
19769
19770         $LFS setstripe -d $DIR/$tdir
19771         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
19772                 error "Can't set directory default striping"
19773
19774         do_facet mds1 $LCTL set_param -n \
19775                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
19776         # set 0 threshold and create DOM file to change tunable stripesize
19777         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
19778         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
19779                 error "Failed to create $dom file"
19780         # now tunable dom_cur_stripesize should reach maximum
19781         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19782                                         lod.${lodname}.dom_stripesize_cur_kb)
19783         [[ $dom_current == $dom_limit ]] ||
19784                 error "Current DOM stripesize is not maximum"
19785         rm $dom
19786
19787         # set threshold for further tests
19788         do_facet mds1 $LCTL set_param -n \
19789                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
19790         echo "DOM threshold is $dom_threshold free space"
19791         local dom_def
19792         local dom_set
19793         # Spoof bfree to exceed threshold
19794         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
19795         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
19796         for spfree in 40 20 0 15 30 55; do
19797                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
19798                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
19799                         error "Failed to create $dom file"
19800                 dom_def=$(do_facet mds1 $LCTL get_param -n \
19801                                         lod.${lodname}.dom_stripesize_cur_kb)
19802                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
19803                 [[ $dom_def != $dom_current ]] ||
19804                         error "Default stripe size was not changed"
19805                 if [[ $spfree > 0 ]] ; then
19806                         dom_set=$($LFS getstripe -S $dom)
19807                         [[ $dom_set == $((dom_def * 1024)) ]] ||
19808                                 error "DOM component size is still old"
19809                 else
19810                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
19811                                 error "DoM component is set with no free space"
19812                 fi
19813                 rm $dom
19814                 dom_current=$dom_def
19815         done
19816 }
19817 run_test 270g "DoM: default DoM stripe size depends on free space"
19818
19819 test_270h() {
19820         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
19821                 skip "Need MDS version at least 2.13.53"
19822
19823         local mdtname=${FSNAME}-MDT0000-mdtlov
19824         local dom=$DIR/$tdir/$tfile
19825         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
19826
19827         save_lustre_params mds1 "lod.*.dom_stripesize" > $save
19828         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
19829
19830         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19831         $LFS setstripe -E 1M -c1  -E -1 -c2 ${dom}_1 ||
19832                 error "can't create OST file"
19833         # mirrored file with DOM entry in the second mirror
19834         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 ${dom}_1 ||
19835                 error "can't create mirror with DoM component"
19836
19837         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
19838
19839         # DOM component in the middle and has other enries in the same mirror,
19840         # should succeed but lost DoM component
19841         $LFS setstripe --copy=${dom}_1 $dom ||
19842                 error "Can't create file from OST|DOM mirror layout"
19843         # check new file has no DoM layout after all
19844         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
19845                 error "File has DoM component while DoM is disabled"
19846 }
19847 run_test 270h "DoM: DoM stripe removal when disabled on server"
19848
19849 test_271a() {
19850         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19851                 skip "Need MDS version at least 2.10.55"
19852
19853         local dom=$DIR/$tdir/dom
19854
19855         mkdir -p $DIR/$tdir
19856
19857         $LFS setstripe -E 1024K -L mdt $dom
19858
19859         lctl set_param -n mdc.*.stats=clear
19860         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19861         cat $dom > /dev/null
19862         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
19863         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
19864         ls $dom
19865         rm -f $dom
19866 }
19867 run_test 271a "DoM: data is cached for read after write"
19868
19869 test_271b() {
19870         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19871                 skip "Need MDS version at least 2.10.55"
19872
19873         local dom=$DIR/$tdir/dom
19874
19875         mkdir -p $DIR/$tdir
19876
19877         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19878
19879         lctl set_param -n mdc.*.stats=clear
19880         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19881         cancel_lru_locks mdc
19882         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
19883         # second stat to check size is cached on client
19884         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
19885         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19886         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
19887         rm -f $dom
19888 }
19889 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
19890
19891 test_271ba() {
19892         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19893                 skip "Need MDS version at least 2.10.55"
19894
19895         local dom=$DIR/$tdir/dom
19896
19897         mkdir -p $DIR/$tdir
19898
19899         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19900
19901         lctl set_param -n mdc.*.stats=clear
19902         lctl set_param -n osc.*.stats=clear
19903         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
19904         cancel_lru_locks mdc
19905         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19906         # second stat to check size is cached on client
19907         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19908         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19909         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
19910         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
19911         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
19912         rm -f $dom
19913 }
19914 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
19915
19916
19917 get_mdc_stats() {
19918         local mdtidx=$1
19919         local param=$2
19920         local mdt=MDT$(printf %04x $mdtidx)
19921
19922         if [ -z $param ]; then
19923                 lctl get_param -n mdc.*$mdt*.stats
19924         else
19925                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
19926         fi
19927 }
19928
19929 test_271c() {
19930         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19931                 skip "Need MDS version at least 2.10.55"
19932
19933         local dom=$DIR/$tdir/dom
19934
19935         mkdir -p $DIR/$tdir
19936
19937         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19938
19939         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19940         local facet=mds$((mdtidx + 1))
19941
19942         cancel_lru_locks mdc
19943         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
19944         createmany -o $dom 1000
19945         lctl set_param -n mdc.*.stats=clear
19946         smalliomany -w $dom 1000 200
19947         get_mdc_stats $mdtidx
19948         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19949         # Each file has 1 open, 1 IO enqueues, total 2000
19950         # but now we have also +1 getxattr for security.capability, total 3000
19951         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
19952         unlinkmany $dom 1000
19953
19954         cancel_lru_locks mdc
19955         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
19956         createmany -o $dom 1000
19957         lctl set_param -n mdc.*.stats=clear
19958         smalliomany -w $dom 1000 200
19959         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19960         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
19961         # for OPEN and IO lock.
19962         [ $((enq - enq_2)) -ge 1000 ] ||
19963                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
19964         unlinkmany $dom 1000
19965         return 0
19966 }
19967 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
19968
19969 cleanup_271def_tests() {
19970         trap 0
19971         rm -f $1
19972 }
19973
19974 test_271d() {
19975         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19976                 skip "Need MDS version at least 2.10.57"
19977
19978         local dom=$DIR/$tdir/dom
19979         local tmp=$TMP/$tfile
19980         trap "cleanup_271def_tests $tmp" EXIT
19981
19982         mkdir -p $DIR/$tdir
19983
19984         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19985
19986         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19987
19988         cancel_lru_locks mdc
19989         dd if=/dev/urandom of=$tmp bs=1000 count=1
19990         dd if=$tmp of=$dom bs=1000 count=1
19991         cancel_lru_locks mdc
19992
19993         cat /etc/hosts >> $tmp
19994         lctl set_param -n mdc.*.stats=clear
19995
19996         # append data to the same file it should update local page
19997         echo "Append to the same page"
19998         cat /etc/hosts >> $dom
19999         local num=$(get_mdc_stats $mdtidx ost_read)
20000         local ra=$(get_mdc_stats $mdtidx req_active)
20001         local rw=$(get_mdc_stats $mdtidx req_waittime)
20002
20003         [ -z $num ] || error "$num READ RPC occured"
20004         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
20005         echo "... DONE"
20006
20007         # compare content
20008         cmp $tmp $dom || error "file miscompare"
20009
20010         cancel_lru_locks mdc
20011         lctl set_param -n mdc.*.stats=clear
20012
20013         echo "Open and read file"
20014         cat $dom > /dev/null
20015         local num=$(get_mdc_stats $mdtidx ost_read)
20016         local ra=$(get_mdc_stats $mdtidx req_active)
20017         local rw=$(get_mdc_stats $mdtidx req_waittime)
20018
20019         [ -z $num ] || error "$num READ RPC occured"
20020         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
20021         echo "... DONE"
20022
20023         # compare content
20024         cmp $tmp $dom || error "file miscompare"
20025
20026         return 0
20027 }
20028 run_test 271d "DoM: read on open (1K file in reply buffer)"
20029
20030 test_271f() {
20031         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
20032                 skip "Need MDS version at least 2.10.57"
20033
20034         local dom=$DIR/$tdir/dom
20035         local tmp=$TMP/$tfile
20036         trap "cleanup_271def_tests $tmp" EXIT
20037
20038         mkdir -p $DIR/$tdir
20039
20040         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
20041
20042         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
20043
20044         cancel_lru_locks mdc
20045         dd if=/dev/urandom of=$tmp bs=265000 count=1
20046         dd if=$tmp of=$dom bs=265000 count=1
20047         cancel_lru_locks mdc
20048         cat /etc/hosts >> $tmp
20049         lctl set_param -n mdc.*.stats=clear
20050
20051         echo "Append to the same page"
20052         cat /etc/hosts >> $dom
20053         local num=$(get_mdc_stats $mdtidx ost_read)
20054         local ra=$(get_mdc_stats $mdtidx req_active)
20055         local rw=$(get_mdc_stats $mdtidx req_waittime)
20056
20057         [ -z $num ] || error "$num READ RPC occured"
20058         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
20059         echo "... DONE"
20060
20061         # compare content
20062         cmp $tmp $dom || error "file miscompare"
20063
20064         cancel_lru_locks mdc
20065         lctl set_param -n mdc.*.stats=clear
20066
20067         echo "Open and read file"
20068         cat $dom > /dev/null
20069         local num=$(get_mdc_stats $mdtidx ost_read)
20070         local ra=$(get_mdc_stats $mdtidx req_active)
20071         local rw=$(get_mdc_stats $mdtidx req_waittime)
20072
20073         [ -z $num ] && num=0
20074         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
20075         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
20076         echo "... DONE"
20077
20078         # compare content
20079         cmp $tmp $dom || error "file miscompare"
20080
20081         return 0
20082 }
20083 run_test 271f "DoM: read on open (200K file and read tail)"
20084
20085 test_271g() {
20086         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
20087                 skip "Skipping due to old client or server version"
20088
20089         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
20090         # to get layout
20091         $CHECKSTAT -t file $DIR1/$tfile
20092
20093         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
20094         MULTIOP_PID=$!
20095         sleep 1
20096         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
20097         $LCTL set_param fail_loc=0x80000314
20098         rm $DIR1/$tfile || error "Unlink fails"
20099         RC=$?
20100         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
20101         [ $RC -eq 0 ] || error "Failed write to stale object"
20102 }
20103 run_test 271g "Discard DoM data vs client flush race"
20104
20105 test_272a() {
20106         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
20107                 skip "Need MDS version at least 2.11.50"
20108
20109         local dom=$DIR/$tdir/dom
20110         mkdir -p $DIR/$tdir
20111
20112         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
20113         dd if=/dev/urandom of=$dom bs=512K count=1 ||
20114                 error "failed to write data into $dom"
20115         local old_md5=$(md5sum $dom)
20116
20117         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
20118                 error "failed to migrate to the same DoM component"
20119
20120         local new_md5=$(md5sum $dom)
20121
20122         [ "$old_md5" == "$new_md5" ] ||
20123                 error "md5sum differ: $old_md5, $new_md5"
20124
20125         [ $($LFS getstripe -c $dom) -eq 2 ] ||
20126                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
20127 }
20128 run_test 272a "DoM migration: new layout with the same DOM component"
20129
20130 test_272b() {
20131         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
20132                 skip "Need MDS version at least 2.11.50"
20133
20134         local dom=$DIR/$tdir/dom
20135         mkdir -p $DIR/$tdir
20136         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
20137
20138         local mdtidx=$($LFS getstripe -m $dom)
20139         local mdtname=MDT$(printf %04x $mdtidx)
20140         local facet=mds$((mdtidx + 1))
20141
20142         local mdtfree1=$(do_facet $facet \
20143                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20144         dd if=/dev/urandom of=$dom bs=2M count=1 ||
20145                 error "failed to write data into $dom"
20146         local old_md5=$(md5sum $dom)
20147         cancel_lru_locks mdc
20148         local mdtfree1=$(do_facet $facet \
20149                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20150
20151         $LFS migrate -c2 $dom ||
20152                 error "failed to migrate to the new composite layout"
20153         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
20154                 error "MDT stripe was not removed"
20155
20156         cancel_lru_locks mdc
20157         local new_md5=$(md5sum $dom)
20158         [ "$old_md5" == "$new_md5" ] ||
20159                 error "$old_md5 != $new_md5"
20160
20161         # Skip free space checks with ZFS
20162         if [ "$(facet_fstype $facet)" != "zfs" ]; then
20163                 local mdtfree2=$(do_facet $facet \
20164                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20165                 [ $mdtfree2 -gt $mdtfree1 ] ||
20166                         error "MDT space is not freed after migration"
20167         fi
20168         return 0
20169 }
20170 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
20171
20172 test_272c() {
20173         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
20174                 skip "Need MDS version at least 2.11.50"
20175
20176         local dom=$DIR/$tdir/$tfile
20177         mkdir -p $DIR/$tdir
20178         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
20179
20180         local mdtidx=$($LFS getstripe -m $dom)
20181         local mdtname=MDT$(printf %04x $mdtidx)
20182         local facet=mds$((mdtidx + 1))
20183
20184         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
20185                 error "failed to write data into $dom"
20186         local old_md5=$(md5sum $dom)
20187         cancel_lru_locks mdc
20188         local mdtfree1=$(do_facet $facet \
20189                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20190
20191         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
20192                 error "failed to migrate to the new composite layout"
20193         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
20194                 error "MDT stripe was not removed"
20195
20196         cancel_lru_locks mdc
20197         local new_md5=$(md5sum $dom)
20198         [ "$old_md5" == "$new_md5" ] ||
20199                 error "$old_md5 != $new_md5"
20200
20201         # Skip free space checks with ZFS
20202         if [ "$(facet_fstype $facet)" != "zfs" ]; then
20203                 local mdtfree2=$(do_facet $facet \
20204                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20205                 [ $mdtfree2 -gt $mdtfree1 ] ||
20206                         error "MDS space is not freed after migration"
20207         fi
20208         return 0
20209 }
20210 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
20211
20212 test_272d() {
20213         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
20214                 skip "Need MDS version at least 2.12.55"
20215
20216         local dom=$DIR/$tdir/$tfile
20217         mkdir -p $DIR/$tdir
20218         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
20219
20220         local mdtidx=$($LFS getstripe -m $dom)
20221         local mdtname=MDT$(printf %04x $mdtidx)
20222         local facet=mds$((mdtidx + 1))
20223
20224         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
20225                 error "failed to write data into $dom"
20226         local old_md5=$(md5sum $dom)
20227         cancel_lru_locks mdc
20228         local mdtfree1=$(do_facet $facet \
20229                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20230
20231         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
20232                 error "failed mirroring to the new composite layout"
20233         $LFS mirror resync $dom ||
20234                 error "failed mirror resync"
20235         $LFS mirror split --mirror-id 1 -d $dom ||
20236                 error "failed mirror split"
20237
20238         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
20239                 error "MDT stripe was not removed"
20240
20241         cancel_lru_locks mdc
20242         local new_md5=$(md5sum $dom)
20243         [ "$old_md5" == "$new_md5" ] ||
20244                 error "$old_md5 != $new_md5"
20245
20246         # Skip free space checks with ZFS
20247         if [ "$(facet_fstype $facet)" != "zfs" ]; then
20248                 local mdtfree2=$(do_facet $facet \
20249                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
20250                 [ $mdtfree2 -gt $mdtfree1 ] ||
20251                         error "MDS space is not freed after DOM mirror deletion"
20252         fi
20253         return 0
20254 }
20255 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
20256
20257 test_272e() {
20258         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
20259                 skip "Need MDS version at least 2.12.55"
20260
20261         local dom=$DIR/$tdir/$tfile
20262         mkdir -p $DIR/$tdir
20263         $LFS setstripe -c 2 $dom
20264
20265         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
20266                 error "failed to write data into $dom"
20267         local old_md5=$(md5sum $dom)
20268         cancel_lru_locks mdc
20269
20270         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
20271                 error "failed mirroring to the DOM layout"
20272         $LFS mirror resync $dom ||
20273                 error "failed mirror resync"
20274         $LFS mirror split --mirror-id 1 -d $dom ||
20275                 error "failed mirror split"
20276
20277         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
20278                 error "MDT stripe was not removed"
20279
20280         cancel_lru_locks mdc
20281         local new_md5=$(md5sum $dom)
20282         [ "$old_md5" == "$new_md5" ] ||
20283                 error "$old_md5 != $new_md5"
20284
20285         return 0
20286 }
20287 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
20288
20289 test_272f() {
20290         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
20291                 skip "Need MDS version at least 2.12.55"
20292
20293         local dom=$DIR/$tdir/$tfile
20294         mkdir -p $DIR/$tdir
20295         $LFS setstripe -c 2 $dom
20296
20297         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
20298                 error "failed to write data into $dom"
20299         local old_md5=$(md5sum $dom)
20300         cancel_lru_locks mdc
20301
20302         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
20303                 error "failed migrating to the DOM file"
20304
20305         cancel_lru_locks mdc
20306         local new_md5=$(md5sum $dom)
20307         [ "$old_md5" != "$new_md5" ] &&
20308                 error "$old_md5 != $new_md5"
20309
20310         return 0
20311 }
20312 run_test 272f "DoM migration: OST-striped file to DOM file"
20313
20314 test_273a() {
20315         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
20316                 skip "Need MDS version at least 2.11.50"
20317
20318         # Layout swap cannot be done if either file has DOM component,
20319         # this will never be supported, migration should be used instead
20320
20321         local dom=$DIR/$tdir/$tfile
20322         mkdir -p $DIR/$tdir
20323
20324         $LFS setstripe -c2 ${dom}_plain
20325         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
20326         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
20327                 error "can swap layout with DoM component"
20328         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
20329                 error "can swap layout with DoM component"
20330
20331         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
20332         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
20333                 error "can swap layout with DoM component"
20334         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
20335                 error "can swap layout with DoM component"
20336         return 0
20337 }
20338 run_test 273a "DoM: layout swapping should fail with DOM"
20339
20340 test_275() {
20341         remote_ost_nodsh && skip "remote OST with nodsh"
20342         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
20343                 skip "Need OST version >= 2.10.57"
20344
20345         local file=$DIR/$tfile
20346         local oss
20347
20348         oss=$(comma_list $(osts_nodes))
20349
20350         dd if=/dev/urandom of=$file bs=1M count=2 ||
20351                 error "failed to create a file"
20352         cancel_lru_locks osc
20353
20354         #lock 1
20355         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
20356                 error "failed to read a file"
20357
20358 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
20359         $LCTL set_param fail_loc=0x8000031f
20360
20361         cancel_lru_locks osc &
20362         sleep 1
20363
20364 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
20365         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
20366         #IO takes another lock, but matches the PENDING one
20367         #and places it to the IO RPC
20368         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
20369                 error "failed to read a file with PENDING lock"
20370 }
20371 run_test 275 "Read on a canceled duplicate lock"
20372
20373 test_276() {
20374         remote_ost_nodsh && skip "remote OST with nodsh"
20375         local pid
20376
20377         do_facet ost1 "(while true; do \
20378                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
20379                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
20380         pid=$!
20381
20382         for LOOP in $(seq 20); do
20383                 stop ost1
20384                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
20385         done
20386         kill -9 $pid
20387         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
20388                 rm $TMP/sanity_276_pid"
20389 }
20390 run_test 276 "Race between mount and obd_statfs"
20391
20392 test_277() {
20393         $LCTL set_param ldlm.namespaces.*.lru_size=0
20394         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
20395         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
20396                         grep ^used_mb | awk '{print $2}')
20397         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
20398         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
20399                 oflag=direct conv=notrunc
20400         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
20401                         grep ^used_mb | awk '{print $2}')
20402         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
20403 }
20404 run_test 277 "Direct IO shall drop page cache"
20405
20406 test_278() {
20407         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
20408         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20409         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
20410                 skip "needs the same host for mdt1 mdt2" && return
20411
20412         local pid1
20413         local pid2
20414
20415 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
20416         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
20417         stop mds2 &
20418         pid2=$!
20419
20420         stop mds1
20421
20422         echo "Starting MDTs"
20423         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
20424         wait $pid2
20425 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
20426 #will return NULL
20427         do_facet mds2 $LCTL set_param fail_loc=0
20428
20429         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
20430         wait_recovery_complete mds2
20431 }
20432 run_test 278 "Race starting MDS between MDTs stop/start"
20433
20434 test_280() {
20435         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
20436                 skip "Need MGS version at least 2.13.52"
20437         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20438         combined_mgs_mds || skip "needs combined MGS/MDT"
20439
20440         umount_client $MOUNT
20441 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
20442         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
20443
20444         mount_client $MOUNT &
20445         sleep 1
20446         stop mgs || error "stop mgs failed"
20447         #for a race mgs would crash
20448         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
20449         mount_client $MOUNT || error "mount client failed"
20450 }
20451 run_test 280 "Race between MGS umount and client llog processing"
20452
20453 cleanup_test_300() {
20454         trap 0
20455         umask $SAVE_UMASK
20456 }
20457 test_striped_dir() {
20458         local mdt_index=$1
20459         local stripe_count
20460         local stripe_index
20461
20462         mkdir -p $DIR/$tdir
20463
20464         SAVE_UMASK=$(umask)
20465         trap cleanup_test_300 RETURN EXIT
20466
20467         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
20468                                                 $DIR/$tdir/striped_dir ||
20469                 error "set striped dir error"
20470
20471         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
20472         [ "$mode" = "755" ] || error "expect 755 got $mode"
20473
20474         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
20475                 error "getdirstripe failed"
20476         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
20477         if [ "$stripe_count" != "2" ]; then
20478                 error "1:stripe_count is $stripe_count, expect 2"
20479         fi
20480         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
20481         if [ "$stripe_count" != "2" ]; then
20482                 error "2:stripe_count is $stripe_count, expect 2"
20483         fi
20484
20485         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
20486         if [ "$stripe_index" != "$mdt_index" ]; then
20487                 error "stripe_index is $stripe_index, expect $mdt_index"
20488         fi
20489
20490         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
20491                 error "nlink error after create striped dir"
20492
20493         mkdir $DIR/$tdir/striped_dir/a
20494         mkdir $DIR/$tdir/striped_dir/b
20495
20496         stat $DIR/$tdir/striped_dir/a ||
20497                 error "create dir under striped dir failed"
20498         stat $DIR/$tdir/striped_dir/b ||
20499                 error "create dir under striped dir failed"
20500
20501         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
20502                 error "nlink error after mkdir"
20503
20504         rmdir $DIR/$tdir/striped_dir/a
20505         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
20506                 error "nlink error after rmdir"
20507
20508         rmdir $DIR/$tdir/striped_dir/b
20509         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
20510                 error "nlink error after rmdir"
20511
20512         chattr +i $DIR/$tdir/striped_dir
20513         createmany -o $DIR/$tdir/striped_dir/f 10 &&
20514                 error "immutable flags not working under striped dir!"
20515         chattr -i $DIR/$tdir/striped_dir
20516
20517         rmdir $DIR/$tdir/striped_dir ||
20518                 error "rmdir striped dir error"
20519
20520         cleanup_test_300
20521
20522         true
20523 }
20524
20525 test_300a() {
20526         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20527                 skip "skipped for lustre < 2.7.0"
20528         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20529         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20530
20531         test_striped_dir 0 || error "failed on striped dir on MDT0"
20532         test_striped_dir 1 || error "failed on striped dir on MDT0"
20533 }
20534 run_test 300a "basic striped dir sanity test"
20535
20536 test_300b() {
20537         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20538                 skip "skipped for lustre < 2.7.0"
20539         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20540         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20541
20542         local i
20543         local mtime1
20544         local mtime2
20545         local mtime3
20546
20547         test_mkdir $DIR/$tdir || error "mkdir fail"
20548         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20549                 error "set striped dir error"
20550         for i in {0..9}; do
20551                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
20552                 sleep 1
20553                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
20554                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
20555                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
20556                 sleep 1
20557                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
20558                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
20559                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
20560         done
20561         true
20562 }
20563 run_test 300b "check ctime/mtime for striped dir"
20564
20565 test_300c() {
20566         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20567                 skip "skipped for lustre < 2.7.0"
20568         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20569         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20570
20571         local file_count
20572
20573         mkdir -p $DIR/$tdir
20574         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
20575                 error "set striped dir error"
20576
20577         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
20578                 error "chown striped dir failed"
20579
20580         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
20581                 error "create 5k files failed"
20582
20583         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
20584
20585         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
20586
20587         rm -rf $DIR/$tdir
20588 }
20589 run_test 300c "chown && check ls under striped directory"
20590
20591 test_300d() {
20592         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20593                 skip "skipped for lustre < 2.7.0"
20594         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20595         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20596
20597         local stripe_count
20598         local file
20599
20600         mkdir -p $DIR/$tdir
20601         $LFS setstripe -c 2 $DIR/$tdir
20602
20603         #local striped directory
20604         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20605                 error "set striped dir error"
20606         #look at the directories for debug purposes
20607         ls -l $DIR/$tdir
20608         $LFS getdirstripe $DIR/$tdir
20609         ls -l $DIR/$tdir/striped_dir
20610         $LFS getdirstripe $DIR/$tdir/striped_dir
20611         createmany -o $DIR/$tdir/striped_dir/f 10 ||
20612                 error "create 10 files failed"
20613
20614         #remote striped directory
20615         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
20616                 error "set striped dir error"
20617         #look at the directories for debug purposes
20618         ls -l $DIR/$tdir
20619         $LFS getdirstripe $DIR/$tdir
20620         ls -l $DIR/$tdir/remote_striped_dir
20621         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
20622         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
20623                 error "create 10 files failed"
20624
20625         for file in $(find $DIR/$tdir); do
20626                 stripe_count=$($LFS getstripe -c $file)
20627                 [ $stripe_count -eq 2 ] ||
20628                         error "wrong stripe $stripe_count for $file"
20629         done
20630
20631         rm -rf $DIR/$tdir
20632 }
20633 run_test 300d "check default stripe under striped directory"
20634
20635 test_300e() {
20636         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20637                 skip "Need MDS version at least 2.7.55"
20638         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20639         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20640
20641         local stripe_count
20642         local file
20643
20644         mkdir -p $DIR/$tdir
20645
20646         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20647                 error "set striped dir error"
20648
20649         touch $DIR/$tdir/striped_dir/a
20650         touch $DIR/$tdir/striped_dir/b
20651         touch $DIR/$tdir/striped_dir/c
20652
20653         mkdir $DIR/$tdir/striped_dir/dir_a
20654         mkdir $DIR/$tdir/striped_dir/dir_b
20655         mkdir $DIR/$tdir/striped_dir/dir_c
20656
20657         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
20658                 error "set striped adir under striped dir error"
20659
20660         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
20661                 error "set striped bdir under striped dir error"
20662
20663         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
20664                 error "set striped cdir under striped dir error"
20665
20666         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
20667                 error "rename dir under striped dir fails"
20668
20669         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
20670                 error "rename dir under different stripes fails"
20671
20672         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
20673                 error "rename file under striped dir should succeed"
20674
20675         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
20676                 error "rename dir under striped dir should succeed"
20677
20678         rm -rf $DIR/$tdir
20679 }
20680 run_test 300e "check rename under striped directory"
20681
20682 test_300f() {
20683         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20684         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20685         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20686                 skip "Need MDS version at least 2.7.55"
20687
20688         local stripe_count
20689         local file
20690
20691         rm -rf $DIR/$tdir
20692         mkdir -p $DIR/$tdir
20693
20694         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20695                 error "set striped dir error"
20696
20697         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
20698                 error "set striped dir error"
20699
20700         touch $DIR/$tdir/striped_dir/a
20701         mkdir $DIR/$tdir/striped_dir/dir_a
20702         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
20703                 error "create striped dir under striped dir fails"
20704
20705         touch $DIR/$tdir/striped_dir1/b
20706         mkdir $DIR/$tdir/striped_dir1/dir_b
20707         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
20708                 error "create striped dir under striped dir fails"
20709
20710         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
20711                 error "rename dir under different striped dir should fail"
20712
20713         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
20714                 error "rename striped dir under diff striped dir should fail"
20715
20716         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
20717                 error "rename file under diff striped dirs fails"
20718
20719         rm -rf $DIR/$tdir
20720 }
20721 run_test 300f "check rename cross striped directory"
20722
20723 test_300_check_default_striped_dir()
20724 {
20725         local dirname=$1
20726         local default_count=$2
20727         local default_index=$3
20728         local stripe_count
20729         local stripe_index
20730         local dir_stripe_index
20731         local dir
20732
20733         echo "checking $dirname $default_count $default_index"
20734         $LFS setdirstripe -D -c $default_count -i $default_index \
20735                                 -t all_char $DIR/$tdir/$dirname ||
20736                 error "set default stripe on striped dir error"
20737         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
20738         [ $stripe_count -eq $default_count ] ||
20739                 error "expect $default_count get $stripe_count for $dirname"
20740
20741         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
20742         [ $stripe_index -eq $default_index ] ||
20743                 error "expect $default_index get $stripe_index for $dirname"
20744
20745         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
20746                                                 error "create dirs failed"
20747
20748         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
20749         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
20750         for dir in $(find $DIR/$tdir/$dirname/*); do
20751                 stripe_count=$($LFS getdirstripe -c $dir)
20752                 [ $stripe_count -eq $default_count ] ||
20753                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
20754                 error "stripe count $default_count != $stripe_count for $dir"
20755
20756                 stripe_index=$($LFS getdirstripe -i $dir)
20757                 [ $default_index -eq -1 ] ||
20758                         [ $stripe_index -eq $default_index ] ||
20759                         error "$stripe_index != $default_index for $dir"
20760
20761                 #check default stripe
20762                 stripe_count=$($LFS getdirstripe -D -c $dir)
20763                 [ $stripe_count -eq $default_count ] ||
20764                 error "default count $default_count != $stripe_count for $dir"
20765
20766                 stripe_index=$($LFS getdirstripe -D -i $dir)
20767                 [ $stripe_index -eq $default_index ] ||
20768                 error "default index $default_index != $stripe_index for $dir"
20769         done
20770         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
20771 }
20772
20773 test_300g() {
20774         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20775         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20776                 skip "Need MDS version at least 2.7.55"
20777
20778         local dir
20779         local stripe_count
20780         local stripe_index
20781
20782         mkdir $DIR/$tdir
20783         mkdir $DIR/$tdir/normal_dir
20784
20785         #Checking when client cache stripe index
20786         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20787         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
20788                 error "create striped_dir failed"
20789
20790         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
20791                 error "create dir0 fails"
20792         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
20793         [ $stripe_index -eq 0 ] ||
20794                 error "dir0 expect index 0 got $stripe_index"
20795
20796         mkdir $DIR/$tdir/striped_dir/dir1 ||
20797                 error "create dir1 fails"
20798         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
20799         [ $stripe_index -eq 1 ] ||
20800                 error "dir1 expect index 1 got $stripe_index"
20801
20802         #check default stripe count/stripe index
20803         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
20804         test_300_check_default_striped_dir normal_dir 1 0
20805         test_300_check_default_striped_dir normal_dir 2 1
20806         test_300_check_default_striped_dir normal_dir 2 -1
20807
20808         #delete default stripe information
20809         echo "delete default stripeEA"
20810         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
20811                 error "set default stripe on striped dir error"
20812
20813         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
20814         for dir in $(find $DIR/$tdir/normal_dir/*); do
20815                 stripe_count=$($LFS getdirstripe -c $dir)
20816                 [ $stripe_count -eq 0 ] ||
20817                         error "expect 1 get $stripe_count for $dir"
20818                 stripe_index=$($LFS getdirstripe -i $dir)
20819                 [ $stripe_index -eq 0 ] ||
20820                         error "expect 0 get $stripe_index for $dir"
20821         done
20822 }
20823 run_test 300g "check default striped directory for normal directory"
20824
20825 test_300h() {
20826         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20827         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20828                 skip "Need MDS version at least 2.7.55"
20829
20830         local dir
20831         local stripe_count
20832
20833         mkdir $DIR/$tdir
20834         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20835                 error "set striped dir error"
20836
20837         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
20838         test_300_check_default_striped_dir striped_dir 1 0
20839         test_300_check_default_striped_dir striped_dir 2 1
20840         test_300_check_default_striped_dir striped_dir 2 -1
20841
20842         #delete default stripe information
20843         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
20844                 error "set default stripe on striped dir error"
20845
20846         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
20847         for dir in $(find $DIR/$tdir/striped_dir/*); do
20848                 stripe_count=$($LFS getdirstripe -c $dir)
20849                 [ $stripe_count -eq 0 ] ||
20850                         error "expect 1 get $stripe_count for $dir"
20851         done
20852 }
20853 run_test 300h "check default striped directory for striped directory"
20854
20855 test_300i() {
20856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20857         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20858         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20859                 skip "Need MDS version at least 2.7.55"
20860
20861         local stripe_count
20862         local file
20863
20864         mkdir $DIR/$tdir
20865
20866         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20867                 error "set striped dir error"
20868
20869         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20870                 error "create files under striped dir failed"
20871
20872         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
20873                 error "set striped hashdir error"
20874
20875         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
20876                 error "create dir0 under hash dir failed"
20877         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
20878                 error "create dir1 under hash dir failed"
20879
20880         # unfortunately, we need to umount to clear dir layout cache for now
20881         # once we fully implement dir layout, we can drop this
20882         umount_client $MOUNT || error "umount failed"
20883         mount_client $MOUNT || error "mount failed"
20884
20885         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
20886         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
20887         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
20888
20889         #set the stripe to be unknown hash type
20890         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
20891         $LCTL set_param fail_loc=0x1901
20892         for ((i = 0; i < 10; i++)); do
20893                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
20894                         error "stat f-$i failed"
20895                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
20896         done
20897
20898         touch $DIR/$tdir/striped_dir/f0 &&
20899                 error "create under striped dir with unknown hash should fail"
20900
20901         $LCTL set_param fail_loc=0
20902
20903         umount_client $MOUNT || error "umount failed"
20904         mount_client $MOUNT || error "mount failed"
20905
20906         return 0
20907 }
20908 run_test 300i "client handle unknown hash type striped directory"
20909
20910 test_300j() {
20911         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20912         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20913         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20914                 skip "Need MDS version at least 2.7.55"
20915
20916         local stripe_count
20917         local file
20918
20919         mkdir $DIR/$tdir
20920
20921         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
20922         $LCTL set_param fail_loc=0x1702
20923         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20924                 error "set striped dir error"
20925
20926         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20927                 error "create files under striped dir failed"
20928
20929         $LCTL set_param fail_loc=0
20930
20931         rm -rf $DIR/$tdir || error "unlink striped dir fails"
20932
20933         return 0
20934 }
20935 run_test 300j "test large update record"
20936
20937 test_300k() {
20938         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20939         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20940         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20941                 skip "Need MDS version at least 2.7.55"
20942
20943         # this test needs a huge transaction
20944         local kb
20945         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20946              osd*.$FSNAME-MDT0000.kbytestotal")
20947         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
20948
20949         local stripe_count
20950         local file
20951
20952         mkdir $DIR/$tdir
20953
20954         #define OBD_FAIL_LARGE_STRIPE   0x1703
20955         $LCTL set_param fail_loc=0x1703
20956         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
20957                 error "set striped dir error"
20958         $LCTL set_param fail_loc=0
20959
20960         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20961                 error "getstripeddir fails"
20962         rm -rf $DIR/$tdir/striped_dir ||
20963                 error "unlink striped dir fails"
20964
20965         return 0
20966 }
20967 run_test 300k "test large striped directory"
20968
20969 test_300l() {
20970         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20971         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20972         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20973                 skip "Need MDS version at least 2.7.55"
20974
20975         local stripe_index
20976
20977         test_mkdir -p $DIR/$tdir/striped_dir
20978         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
20979                         error "chown $RUNAS_ID failed"
20980         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
20981                 error "set default striped dir failed"
20982
20983         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
20984         $LCTL set_param fail_loc=0x80000158
20985         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
20986
20987         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
20988         [ $stripe_index -eq 1 ] ||
20989                 error "expect 1 get $stripe_index for $dir"
20990 }
20991 run_test 300l "non-root user to create dir under striped dir with stale layout"
20992
20993 test_300m() {
20994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20995         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
20996         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20997                 skip "Need MDS version at least 2.7.55"
20998
20999         mkdir -p $DIR/$tdir/striped_dir
21000         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
21001                 error "set default stripes dir error"
21002
21003         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
21004
21005         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
21006         [ $stripe_count -eq 0 ] ||
21007                         error "expect 0 get $stripe_count for a"
21008
21009         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
21010                 error "set default stripes dir error"
21011
21012         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
21013
21014         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
21015         [ $stripe_count -eq 0 ] ||
21016                         error "expect 0 get $stripe_count for b"
21017
21018         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
21019                 error "set default stripes dir error"
21020
21021         mkdir $DIR/$tdir/striped_dir/c &&
21022                 error "default stripe_index is invalid, mkdir c should fails"
21023
21024         rm -rf $DIR/$tdir || error "rmdir fails"
21025 }
21026 run_test 300m "setstriped directory on single MDT FS"
21027
21028 cleanup_300n() {
21029         local list=$(comma_list $(mdts_nodes))
21030
21031         trap 0
21032         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
21033 }
21034
21035 test_300n() {
21036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21037         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21038         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
21039                 skip "Need MDS version at least 2.7.55"
21040         remote_mds_nodsh && skip "remote MDS with nodsh"
21041
21042         local stripe_index
21043         local list=$(comma_list $(mdts_nodes))
21044
21045         trap cleanup_300n RETURN EXIT
21046         mkdir -p $DIR/$tdir
21047         chmod 777 $DIR/$tdir
21048         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
21049                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
21050                 error "create striped dir succeeds with gid=0"
21051
21052         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
21053         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
21054                 error "create striped dir fails with gid=-1"
21055
21056         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
21057         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
21058                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
21059                 error "set default striped dir succeeds with gid=0"
21060
21061
21062         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
21063         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
21064                 error "set default striped dir fails with gid=-1"
21065
21066
21067         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
21068         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
21069                                         error "create test_dir fails"
21070         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
21071                                         error "create test_dir1 fails"
21072         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
21073                                         error "create test_dir2 fails"
21074         cleanup_300n
21075 }
21076 run_test 300n "non-root user to create dir under striped dir with default EA"
21077
21078 test_300o() {
21079         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21080         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21081         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
21082                 skip "Need MDS version at least 2.7.55"
21083
21084         local numfree1
21085         local numfree2
21086
21087         mkdir -p $DIR/$tdir
21088
21089         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
21090         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
21091         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
21092                 skip "not enough free inodes $numfree1 $numfree2"
21093         fi
21094
21095         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
21096         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
21097         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
21098                 skip "not enough free space $numfree1 $numfree2"
21099         fi
21100
21101         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
21102                 error "setdirstripe fails"
21103
21104         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
21105                 error "create dirs fails"
21106
21107         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
21108         ls $DIR/$tdir/striped_dir > /dev/null ||
21109                 error "ls striped dir fails"
21110         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
21111                 error "unlink big striped dir fails"
21112 }
21113 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
21114
21115 test_300p() {
21116         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21117         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21118         remote_mds_nodsh && skip "remote MDS with nodsh"
21119
21120         mkdir -p $DIR/$tdir
21121
21122         #define OBD_FAIL_OUT_ENOSPC     0x1704
21123         do_facet mds2 lctl set_param fail_loc=0x80001704
21124         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
21125                  && error "create striped directory should fail"
21126
21127         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
21128
21129         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
21130         true
21131 }
21132 run_test 300p "create striped directory without space"
21133
21134 test_300q() {
21135         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21136         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21137
21138         local fd=$(free_fd)
21139         local cmd="exec $fd<$tdir"
21140         cd $DIR
21141         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
21142         eval $cmd
21143         cmd="exec $fd<&-"
21144         trap "eval $cmd" EXIT
21145         cd $tdir || error "cd $tdir fails"
21146         rmdir  ../$tdir || error "rmdir $tdir fails"
21147         mkdir local_dir && error "create dir succeeds"
21148         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
21149         eval $cmd
21150         return 0
21151 }
21152 run_test 300q "create remote directory under orphan directory"
21153
21154 test_300r() {
21155         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
21156                 skip "Need MDS version at least 2.7.55" && return
21157         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
21158
21159         mkdir $DIR/$tdir
21160
21161         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
21162                 error "set striped dir error"
21163
21164         $LFS getdirstripe $DIR/$tdir/striped_dir ||
21165                 error "getstripeddir fails"
21166
21167         local stripe_count
21168         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
21169                       awk '/lmv_stripe_count:/ { print $2 }')
21170
21171         [ $MDSCOUNT -ne $stripe_count ] &&
21172                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
21173
21174         rm -rf $DIR/$tdir/striped_dir ||
21175                 error "unlink striped dir fails"
21176 }
21177 run_test 300r "test -1 striped directory"
21178
21179 prepare_remote_file() {
21180         mkdir $DIR/$tdir/src_dir ||
21181                 error "create remote source failed"
21182
21183         cp /etc/hosts $DIR/$tdir/src_dir/a ||
21184                  error "cp to remote source failed"
21185         touch $DIR/$tdir/src_dir/a
21186
21187         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
21188                 error "create remote target dir failed"
21189
21190         touch $DIR/$tdir/tgt_dir/b
21191
21192         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
21193                 error "rename dir cross MDT failed!"
21194
21195         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
21196                 error "src_child still exists after rename"
21197
21198         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
21199                 error "missing file(a) after rename"
21200
21201         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
21202                 error "diff after rename"
21203 }
21204
21205 test_310a() {
21206         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
21207         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21208
21209         local remote_file=$DIR/$tdir/tgt_dir/b
21210
21211         mkdir -p $DIR/$tdir
21212
21213         prepare_remote_file || error "prepare remote file failed"
21214
21215         #open-unlink file
21216         $OPENUNLINK $remote_file $remote_file ||
21217                 error "openunlink $remote_file failed"
21218         $CHECKSTAT -a $remote_file || error "$remote_file exists"
21219 }
21220 run_test 310a "open unlink remote file"
21221
21222 test_310b() {
21223         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
21224         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21225
21226         local remote_file=$DIR/$tdir/tgt_dir/b
21227
21228         mkdir -p $DIR/$tdir
21229
21230         prepare_remote_file || error "prepare remote file failed"
21231
21232         ln $remote_file $DIR/$tfile || error "link failed for remote file"
21233         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
21234         $CHECKSTAT -t file $remote_file || error "check file failed"
21235 }
21236 run_test 310b "unlink remote file with multiple links while open"
21237
21238 test_310c() {
21239         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21240         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
21241
21242         local remote_file=$DIR/$tdir/tgt_dir/b
21243
21244         mkdir -p $DIR/$tdir
21245
21246         prepare_remote_file || error "prepare remote file failed"
21247
21248         ln $remote_file $DIR/$tfile || error "link failed for remote file"
21249         multiop_bg_pause $remote_file O_uc ||
21250                         error "mulitop failed for remote file"
21251         MULTIPID=$!
21252         $MULTIOP $DIR/$tfile Ouc
21253         kill -USR1 $MULTIPID
21254         wait $MULTIPID
21255 }
21256 run_test 310c "open-unlink remote file with multiple links"
21257
21258 #LU-4825
21259 test_311() {
21260         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21261         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
21262         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
21263                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
21264         remote_mds_nodsh && skip "remote MDS with nodsh"
21265
21266         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
21267         local mdts=$(comma_list $(mdts_nodes))
21268
21269         mkdir -p $DIR/$tdir
21270         $LFS setstripe -i 0 -c 1 $DIR/$tdir
21271         createmany -o $DIR/$tdir/$tfile. 1000
21272
21273         # statfs data is not real time, let's just calculate it
21274         old_iused=$((old_iused + 1000))
21275
21276         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
21277                         osp.*OST0000*MDT0000.create_count")
21278         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
21279                                 osp.*OST0000*MDT0000.max_create_count")
21280         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
21281
21282         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
21283         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
21284         [ $index -ne 0 ] || error "$tfile stripe index is 0"
21285
21286         unlinkmany $DIR/$tdir/$tfile. 1000
21287
21288         do_nodes $mdts "$LCTL set_param -n \
21289                         osp.*OST0000*.max_create_count=$max_count"
21290         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
21291                 do_nodes $mdts "$LCTL set_param -n \
21292                                 osp.*OST0000*.create_count=$count"
21293         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
21294                         grep "=0" && error "create_count is zero"
21295
21296         local new_iused
21297         for i in $(seq 120); do
21298                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
21299                 # system may be too busy to destroy all objs in time, use
21300                 # a somewhat small value to not fail autotest
21301                 [ $((old_iused - new_iused)) -gt 400 ] && break
21302                 sleep 1
21303         done
21304
21305         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
21306         [ $((old_iused - new_iused)) -gt 400 ] ||
21307                 error "objs not destroyed after unlink"
21308 }
21309 run_test 311 "disable OSP precreate, and unlink should destroy objs"
21310
21311 zfs_oid_to_objid()
21312 {
21313         local ost=$1
21314         local objid=$2
21315
21316         local vdevdir=$(dirname $(facet_vdevice $ost))
21317         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
21318         local zfs_zapid=$(do_facet $ost $cmd |
21319                           grep -w "/O/0/d$((objid%32))" -C 5 |
21320                           awk '/Object/{getline; print $1}')
21321         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
21322                           awk "/$objid = /"'{printf $3}')
21323
21324         echo $zfs_objid
21325 }
21326
21327 zfs_object_blksz() {
21328         local ost=$1
21329         local objid=$2
21330
21331         local vdevdir=$(dirname $(facet_vdevice $ost))
21332         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
21333         local blksz=$(do_facet $ost $cmd $objid |
21334                       awk '/dblk/{getline; printf $4}')
21335
21336         case "${blksz: -1}" in
21337                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
21338                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
21339                 *) ;;
21340         esac
21341
21342         echo $blksz
21343 }
21344
21345 test_312() { # LU-4856
21346         remote_ost_nodsh && skip "remote OST with nodsh"
21347         [ "$ost1_FSTYPE" = "zfs" ] ||
21348                 skip_env "the test only applies to zfs"
21349
21350         local max_blksz=$(do_facet ost1 \
21351                           $ZFS get -p recordsize $(facet_device ost1) |
21352                           awk '!/VALUE/{print $3}')
21353
21354         # to make life a little bit easier
21355         $LFS mkdir -c 1 -i 0 $DIR/$tdir
21356         $LFS setstripe -c 1 -i 0 $DIR/$tdir
21357
21358         local tf=$DIR/$tdir/$tfile
21359         touch $tf
21360         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21361
21362         # Get ZFS object id
21363         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21364         # block size change by sequential overwrite
21365         local bs
21366
21367         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
21368                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
21369
21370                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
21371                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
21372         done
21373         rm -f $tf
21374
21375         # block size change by sequential append write
21376         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
21377         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21378         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21379         local count
21380
21381         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
21382                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
21383                         oflag=sync conv=notrunc
21384
21385                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
21386                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
21387                         error "blksz error, actual $blksz, " \
21388                                 "expected: 2 * $count * $PAGE_SIZE"
21389         done
21390         rm -f $tf
21391
21392         # random write
21393         touch $tf
21394         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21395         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21396
21397         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
21398         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21399         [ $blksz -eq $PAGE_SIZE ] ||
21400                 error "blksz error: $blksz, expected: $PAGE_SIZE"
21401
21402         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
21403         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21404         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
21405
21406         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
21407         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21408         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
21409 }
21410 run_test 312 "make sure ZFS adjusts its block size by write pattern"
21411
21412 test_313() {
21413         remote_ost_nodsh && skip "remote OST with nodsh"
21414
21415         local file=$DIR/$tfile
21416
21417         rm -f $file
21418         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
21419
21420         # define OBD_FAIL_TGT_RCVD_EIO           0x720
21421         do_facet ost1 "$LCTL set_param fail_loc=0x720"
21422         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
21423                 error "write should failed"
21424         do_facet ost1 "$LCTL set_param fail_loc=0"
21425         rm -f $file
21426 }
21427 run_test 313 "io should fail after last_rcvd update fail"
21428
21429 test_314() {
21430         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
21431
21432         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
21433         do_facet ost1 "$LCTL set_param fail_loc=0x720"
21434         rm -f $DIR/$tfile
21435         wait_delete_completed
21436         do_facet ost1 "$LCTL set_param fail_loc=0"
21437 }
21438 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
21439
21440 test_315() { # LU-618
21441         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
21442
21443         local file=$DIR/$tfile
21444         rm -f $file
21445
21446         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
21447                 error "multiop file write failed"
21448         $MULTIOP $file oO_RDONLY:r4063232_c &
21449         PID=$!
21450
21451         sleep 2
21452
21453         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
21454         kill -USR1 $PID
21455
21456         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
21457         rm -f $file
21458 }
21459 run_test 315 "read should be accounted"
21460
21461 test_316() {
21462         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21463         large_xattr_enabled || skip_env "ea_inode feature disabled"
21464
21465         rm -rf $DIR/$tdir/d
21466         mkdir -p $DIR/$tdir/d
21467         chown nobody $DIR/$tdir/d
21468         touch $DIR/$tdir/d/file
21469
21470         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
21471 }
21472 run_test 316 "lfs mv"
21473
21474 test_317() {
21475         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
21476                 skip "Need MDS version at least 2.11.53"
21477         if [ "$ost1_FSTYPE" == "zfs" ]; then
21478                 skip "LU-10370: no implementation for ZFS"
21479         fi
21480
21481         local trunc_sz
21482         local grant_blk_size
21483
21484         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
21485                         awk '/grant_block_size:/ { print $2; exit; }')
21486         #
21487         # Create File of size 5M. Truncate it to below size's and verify
21488         # blocks count.
21489         #
21490         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
21491                 error "Create file $DIR/$tfile failed"
21492         stack_trap "rm -f $DIR/$tfile" EXIT
21493
21494         for trunc_sz in 2097152 4097 4000 509 0; do
21495                 $TRUNCATE $DIR/$tfile $trunc_sz ||
21496                         error "truncate $tfile to $trunc_sz failed"
21497                 local sz=$(stat --format=%s $DIR/$tfile)
21498                 local blk=$(stat --format=%b $DIR/$tfile)
21499                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
21500                                      grant_blk_size) * 8))
21501
21502                 if [[ $blk -ne $trunc_blk ]]; then
21503                         $(which stat) $DIR/$tfile
21504                         error "Expected Block $trunc_blk got $blk for $tfile"
21505                 fi
21506
21507                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
21508                         error "Expected Size $trunc_sz got $sz for $tfile"
21509         done
21510
21511         #
21512         # sparse file test
21513         # Create file with a hole and write actual two blocks. Block count
21514         # must be 16.
21515         #
21516         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
21517                 conv=fsync || error "Create file : $DIR/$tfile"
21518
21519         # Calculate the final truncate size.
21520         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
21521
21522         #
21523         # truncate to size $trunc_sz bytes. Strip the last block
21524         # The block count must drop to 8
21525         #
21526         $TRUNCATE $DIR/$tfile $trunc_sz ||
21527                 error "truncate $tfile to $trunc_sz failed"
21528
21529         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
21530         sz=$(stat --format=%s $DIR/$tfile)
21531         blk=$(stat --format=%b $DIR/$tfile)
21532
21533         if [[ $blk -ne $trunc_bsz ]]; then
21534                 $(which stat) $DIR/$tfile
21535                 error "Expected Block $trunc_bsz got $blk for $tfile"
21536         fi
21537
21538         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
21539                 error "Expected Size $trunc_sz got $sz for $tfile"
21540 }
21541 run_test 317 "Verify blocks get correctly update after truncate"
21542
21543 test_318() {
21544         local old_max_active=$($LCTL get_param -n \
21545                             llite.*.max_read_ahead_async_active 2>/dev/null)
21546
21547         $LCTL set_param llite.*.max_read_ahead_async_active=256
21548         local max_active=$($LCTL get_param -n \
21549                            llite.*.max_read_ahead_async_active 2>/dev/null)
21550         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
21551
21552         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
21553                 error "set max_read_ahead_async_active should succeed"
21554
21555         $LCTL set_param llite.*.max_read_ahead_async_active=512
21556         max_active=$($LCTL get_param -n \
21557                      llite.*.max_read_ahead_async_active 2>/dev/null)
21558         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
21559
21560         # restore @max_active
21561         [ $old_max_active -ne 0 ] && $LCTL set_param \
21562                 llite.*.max_read_ahead_async_active=$old_max_active
21563
21564         local old_threshold=$($LCTL get_param -n \
21565                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
21566         local max_per_file_mb=$($LCTL get_param -n \
21567                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
21568
21569         local invalid=$(($max_per_file_mb + 1))
21570         $LCTL set_param \
21571                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
21572                         && error "set $invalid should fail"
21573
21574         local valid=$(($invalid - 1))
21575         $LCTL set_param \
21576                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
21577                         error "set $valid should succeed"
21578         local threshold=$($LCTL get_param -n \
21579                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
21580         [ $threshold -eq $valid ] || error \
21581                 "expect threshold $valid got $threshold"
21582         $LCTL set_param \
21583                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
21584 }
21585 run_test 318 "Verify async readahead tunables"
21586
21587 test_319() {
21588         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
21589
21590         local before=$(date +%s)
21591         local evict
21592         local mdir=$DIR/$tdir
21593         local file=$mdir/xxx
21594
21595         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
21596         touch $file
21597
21598 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
21599         $LCTL set_param fail_val=5 fail_loc=0x8000032c
21600         $LFS mv -m1 $file &
21601
21602         sleep 1
21603         dd if=$file of=/dev/null
21604         wait
21605         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
21606           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
21607
21608         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
21609 }
21610 run_test 319 "lost lease lock on migrate error"
21611
21612 test_398a() { # LU-4198
21613         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21614         $LCTL set_param ldlm.namespaces.*.lru_size=clear
21615
21616         # request a new lock on client
21617         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21618
21619         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
21620         local lock_count=$($LCTL get_param -n \
21621                            ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
21622         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
21623
21624         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
21625
21626         # no lock cached, should use lockless IO and not enqueue new lock
21627         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
21628         lock_count=$($LCTL get_param -n \
21629                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
21630         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
21631 }
21632 run_test 398a "direct IO should cancel lock otherwise lockless"
21633
21634 test_398b() { # LU-4198
21635         which fio || skip_env "no fio installed"
21636         $LFS setstripe -c -1 $DIR/$tfile
21637
21638         local size=12
21639         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
21640
21641         local njobs=4
21642         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
21643         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
21644                 --numjobs=$njobs --fallocate=none \
21645                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21646                 --filename=$DIR/$tfile &
21647         bg_pid=$!
21648
21649         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
21650         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
21651                 --numjobs=$njobs --fallocate=none \
21652                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21653                 --filename=$DIR/$tfile || true
21654         wait $bg_pid
21655
21656         rm -rf $DIR/$tfile
21657 }
21658 run_test 398b "DIO and buffer IO race"
21659
21660 test_398c() { # LU-4198
21661         which fio || skip_env "no fio installed"
21662
21663         saved_debug=$($LCTL get_param -n debug)
21664         $LCTL set_param debug=0
21665
21666         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
21667         ((size /= 1024)) # by megabytes
21668         ((size /= 2)) # write half of the OST at most
21669         [ $size -gt 40 ] && size=40 #reduce test time anyway
21670
21671         $LFS setstripe -c 1 $DIR/$tfile
21672
21673         # it seems like ldiskfs reserves more space than necessary if the
21674         # writing blocks are not mapped, so it extends the file firstly
21675         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
21676         cancel_lru_locks osc
21677
21678         # clear and verify rpc_stats later
21679         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
21680
21681         local njobs=4
21682         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
21683         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
21684                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
21685                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21686                 --filename=$DIR/$tfile
21687         [ $? -eq 0 ] || error "fio write error"
21688
21689         [ $($LCTL get_param -n \
21690          ldlm.namespaces.${FSNAME}-OST0000-osc-ffff*.lock_count) -eq 0 ] ||
21691                 error "Locks were requested while doing AIO"
21692
21693         # get the percentage of 1-page I/O
21694         pct=$($LCTL get_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats |
21695                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
21696                 awk '{print $7}')
21697         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
21698
21699         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
21700         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
21701                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
21702                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21703                 --filename=$DIR/$tfile
21704         [ $? -eq 0 ] || error "fio mixed read write error"
21705
21706         rm -rf $DIR/$tfile
21707         $LCTL set_param debug="$saved_debug"
21708 }
21709 run_test 398c "run fio to test AIO"
21710
21711 test_fake_rw() {
21712         local read_write=$1
21713         if [ "$read_write" = "write" ]; then
21714                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
21715         elif [ "$read_write" = "read" ]; then
21716                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
21717         else
21718                 error "argument error"
21719         fi
21720
21721         # turn off debug for performance testing
21722         local saved_debug=$($LCTL get_param -n debug)
21723         $LCTL set_param debug=0
21724
21725         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21726
21727         # get ost1 size - $FSNAME-OST0000
21728         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
21729         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
21730         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
21731
21732         if [ "$read_write" = "read" ]; then
21733                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
21734         fi
21735
21736         local start_time=$(date +%s.%N)
21737         $dd_cmd bs=1M count=$blocks oflag=sync ||
21738                 error "real dd $read_write error"
21739         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
21740
21741         if [ "$read_write" = "write" ]; then
21742                 rm -f $DIR/$tfile
21743         fi
21744
21745         # define OBD_FAIL_OST_FAKE_RW           0x238
21746         do_facet ost1 $LCTL set_param fail_loc=0x238
21747
21748         local start_time=$(date +%s.%N)
21749         $dd_cmd bs=1M count=$blocks oflag=sync ||
21750                 error "fake dd $read_write error"
21751         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
21752
21753         if [ "$read_write" = "write" ]; then
21754                 # verify file size
21755                 cancel_lru_locks osc
21756                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
21757                         error "$tfile size not $blocks MB"
21758         fi
21759         do_facet ost1 $LCTL set_param fail_loc=0
21760
21761         echo "fake $read_write $duration_fake vs. normal $read_write" \
21762                 "$duration in seconds"
21763         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
21764                 error_not_in_vm "fake write is slower"
21765
21766         $LCTL set_param -n debug="$saved_debug"
21767         rm -f $DIR/$tfile
21768 }
21769 test_399a() { # LU-7655 for OST fake write
21770         remote_ost_nodsh && skip "remote OST with nodsh"
21771
21772         test_fake_rw write
21773 }
21774 run_test 399a "fake write should not be slower than normal write"
21775
21776 test_399b() { # LU-8726 for OST fake read
21777         remote_ost_nodsh && skip "remote OST with nodsh"
21778         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
21779                 skip_env "ldiskfs only test"
21780         fi
21781
21782         test_fake_rw read
21783 }
21784 run_test 399b "fake read should not be slower than normal read"
21785
21786 test_400a() { # LU-1606, was conf-sanity test_74
21787         if ! which $CC > /dev/null 2>&1; then
21788                 skip_env "$CC is not installed"
21789         fi
21790
21791         local extra_flags=''
21792         local out=$TMP/$tfile
21793         local prefix=/usr/include/lustre
21794         local prog
21795
21796         # Oleg removes c files in his test rig so test if any c files exist
21797         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
21798                 skip_env "Needed c test files are missing"
21799
21800         if ! [[ -d $prefix ]]; then
21801                 # Assume we're running in tree and fixup the include path.
21802                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
21803                 extra_flags+=" -L$LUSTRE/utils/.lib"
21804         fi
21805
21806         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
21807                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
21808                         error "client api broken"
21809         done
21810         rm -f $out
21811 }
21812 run_test 400a "Lustre client api program can compile and link"
21813
21814 test_400b() { # LU-1606, LU-5011
21815         local header
21816         local out=$TMP/$tfile
21817         local prefix=/usr/include/linux/lustre
21818
21819         # We use a hard coded prefix so that this test will not fail
21820         # when run in tree. There are headers in lustre/include/lustre/
21821         # that are not packaged (like lustre_idl.h) and have more
21822         # complicated include dependencies (like config.h and lnet/types.h).
21823         # Since this test about correct packaging we just skip them when
21824         # they don't exist (see below) rather than try to fixup cppflags.
21825
21826         if ! which $CC > /dev/null 2>&1; then
21827                 skip_env "$CC is not installed"
21828         fi
21829
21830         for header in $prefix/*.h; do
21831                 if ! [[ -f "$header" ]]; then
21832                         continue
21833                 fi
21834
21835                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
21836                         continue # lustre_ioctl.h is internal header
21837                 fi
21838
21839                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
21840                         error "cannot compile '$header'"
21841         done
21842         rm -f $out
21843 }
21844 run_test 400b "packaged headers can be compiled"
21845
21846 test_401a() { #LU-7437
21847         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
21848         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
21849
21850         #count the number of parameters by "list_param -R"
21851         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
21852         #count the number of parameters by listing proc files
21853         local proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
21854         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
21855         echo "proc_dirs='$proc_dirs'"
21856         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
21857         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
21858                       sort -u | wc -l)
21859
21860         [ $params -eq $procs ] ||
21861                 error "found $params parameters vs. $procs proc files"
21862
21863         # test the list_param -D option only returns directories
21864         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
21865         #count the number of parameters by listing proc directories
21866         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
21867                 sort -u | wc -l)
21868
21869         [ $params -eq $procs ] ||
21870                 error "found $params parameters vs. $procs proc files"
21871 }
21872 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
21873
21874 test_401b() {
21875         local save=$($LCTL get_param -n jobid_var)
21876         local tmp=testing
21877
21878         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
21879                 error "no error returned when setting bad parameters"
21880
21881         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
21882         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
21883
21884         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
21885         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
21886         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
21887 }
21888 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
21889
21890 test_401c() {
21891         local jobid_var_old=$($LCTL get_param -n jobid_var)
21892         local jobid_var_new
21893
21894         $LCTL set_param jobid_var= &&
21895                 error "no error returned for 'set_param a='"
21896
21897         jobid_var_new=$($LCTL get_param -n jobid_var)
21898         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21899                 error "jobid_var was changed by setting without value"
21900
21901         $LCTL set_param jobid_var &&
21902                 error "no error returned for 'set_param a'"
21903
21904         jobid_var_new=$($LCTL get_param -n jobid_var)
21905         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21906                 error "jobid_var was changed by setting without value"
21907 }
21908 run_test 401c "Verify 'lctl set_param' without value fails in either format."
21909
21910 test_401d() {
21911         local jobid_var_old=$($LCTL get_param -n jobid_var)
21912         local jobid_var_new
21913         local new_value="foo=bar"
21914
21915         $LCTL set_param jobid_var=$new_value ||
21916                 error "'set_param a=b' did not accept a value containing '='"
21917
21918         jobid_var_new=$($LCTL get_param -n jobid_var)
21919         [[ "$jobid_var_new" == "$new_value" ]] ||
21920                 error "'set_param a=b' failed on a value containing '='"
21921
21922         # Reset the jobid_var to test the other format
21923         $LCTL set_param jobid_var=$jobid_var_old
21924         jobid_var_new=$($LCTL get_param -n jobid_var)
21925         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21926                 error "failed to reset jobid_var"
21927
21928         $LCTL set_param jobid_var $new_value ||
21929                 error "'set_param a b' did not accept a value containing '='"
21930
21931         jobid_var_new=$($LCTL get_param -n jobid_var)
21932         [[ "$jobid_var_new" == "$new_value" ]] ||
21933                 error "'set_param a b' failed on a value containing '='"
21934
21935         $LCTL set_param jobid_var $jobid_var_old
21936         jobid_var_new=$($LCTL get_param -n jobid_var)
21937         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21938                 error "failed to reset jobid_var"
21939 }
21940 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
21941
21942 test_402() {
21943         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
21944         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
21945                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
21946         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
21947                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
21948                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
21949         remote_mds_nodsh && skip "remote MDS with nodsh"
21950
21951         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
21952 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
21953         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
21954         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
21955                 echo "Touch failed - OK"
21956 }
21957 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
21958
21959 test_403() {
21960         local file1=$DIR/$tfile.1
21961         local file2=$DIR/$tfile.2
21962         local tfile=$TMP/$tfile
21963
21964         rm -f $file1 $file2 $tfile
21965
21966         touch $file1
21967         ln $file1 $file2
21968
21969         # 30 sec OBD_TIMEOUT in ll_getattr()
21970         # right before populating st_nlink
21971         $LCTL set_param fail_loc=0x80001409
21972         stat -c %h $file1 > $tfile &
21973
21974         # create an alias, drop all locks and reclaim the dentry
21975         < $file2
21976         cancel_lru_locks mdc
21977         cancel_lru_locks osc
21978         sysctl -w vm.drop_caches=2
21979
21980         wait
21981
21982         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
21983
21984         rm -f $tfile $file1 $file2
21985 }
21986 run_test 403 "i_nlink should not drop to zero due to aliasing"
21987
21988 test_404() { # LU-6601
21989         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
21990                 skip "Need server version newer than 2.8.52"
21991         remote_mds_nodsh && skip "remote MDS with nodsh"
21992
21993         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
21994                 awk '/osp .*-osc-MDT/ { print $4}')
21995
21996         local osp
21997         for osp in $mosps; do
21998                 echo "Deactivate: " $osp
21999                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
22000                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
22001                         awk -vp=$osp '$4 == p { print $2 }')
22002                 [ $stat = IN ] || {
22003                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
22004                         error "deactivate error"
22005                 }
22006                 echo "Activate: " $osp
22007                 do_facet $SINGLEMDS $LCTL --device %$osp activate
22008                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
22009                         awk -vp=$osp '$4 == p { print $2 }')
22010                 [ $stat = UP ] || {
22011                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
22012                         error "activate error"
22013                 }
22014         done
22015 }
22016 run_test 404 "validate manual {de}activated works properly for OSPs"
22017
22018 test_405() {
22019         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
22020         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
22021                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
22022                         skip "Layout swap lock is not supported"
22023
22024         check_swap_layouts_support
22025
22026         test_mkdir $DIR/$tdir
22027         swap_lock_test -d $DIR/$tdir ||
22028                 error "One layout swap locked test failed"
22029 }
22030 run_test 405 "Various layout swap lock tests"
22031
22032 test_406() {
22033         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22034         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
22035         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
22036         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22037         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
22038                 skip "Need MDS version at least 2.8.50"
22039
22040         local def_stripe_size=$($LFS getstripe -S $MOUNT)
22041         local test_pool=$TESTNAME
22042
22043         pool_add $test_pool || error "pool_add failed"
22044         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
22045                 error "pool_add_targets failed"
22046
22047         save_layout_restore_at_exit $MOUNT
22048
22049         # parent set default stripe count only, child will stripe from both
22050         # parent and fs default
22051         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
22052                 error "setstripe $MOUNT failed"
22053         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
22054         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
22055         for i in $(seq 10); do
22056                 local f=$DIR/$tdir/$tfile.$i
22057                 touch $f || error "touch failed"
22058                 local count=$($LFS getstripe -c $f)
22059                 [ $count -eq $OSTCOUNT ] ||
22060                         error "$f stripe count $count != $OSTCOUNT"
22061                 local offset=$($LFS getstripe -i $f)
22062                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
22063                 local size=$($LFS getstripe -S $f)
22064                 [ $size -eq $((def_stripe_size * 2)) ] ||
22065                         error "$f stripe size $size != $((def_stripe_size * 2))"
22066                 local pool=$($LFS getstripe -p $f)
22067                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
22068         done
22069
22070         # change fs default striping, delete parent default striping, now child
22071         # will stripe from new fs default striping only
22072         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
22073                 error "change $MOUNT default stripe failed"
22074         $LFS setstripe -c 0 $DIR/$tdir ||
22075                 error "delete $tdir default stripe failed"
22076         for i in $(seq 11 20); do
22077                 local f=$DIR/$tdir/$tfile.$i
22078                 touch $f || error "touch $f failed"
22079                 local count=$($LFS getstripe -c $f)
22080                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
22081                 local offset=$($LFS getstripe -i $f)
22082                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
22083                 local size=$($LFS getstripe -S $f)
22084                 [ $size -eq $def_stripe_size ] ||
22085                         error "$f stripe size $size != $def_stripe_size"
22086                 local pool=$($LFS getstripe -p $f)
22087                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
22088         done
22089
22090         unlinkmany $DIR/$tdir/$tfile. 1 20
22091
22092         local f=$DIR/$tdir/$tfile
22093         pool_remove_all_targets $test_pool $f
22094         pool_remove $test_pool $f
22095 }
22096 run_test 406 "DNE support fs default striping"
22097
22098 test_407() {
22099         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22100         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
22101                 skip "Need MDS version at least 2.8.55"
22102         remote_mds_nodsh && skip "remote MDS with nodsh"
22103
22104         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
22105                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
22106         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
22107                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
22108         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
22109
22110         #define OBD_FAIL_DT_TXN_STOP    0x2019
22111         for idx in $(seq $MDSCOUNT); do
22112                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
22113         done
22114         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
22115         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
22116                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
22117         true
22118 }
22119 run_test 407 "transaction fail should cause operation fail"
22120
22121 test_408() {
22122         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
22123
22124         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
22125         lctl set_param fail_loc=0x8000040a
22126         # let ll_prepare_partial_page() fail
22127         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
22128
22129         rm -f $DIR/$tfile
22130
22131         # create at least 100 unused inodes so that
22132         # shrink_icache_memory(0) should not return 0
22133         touch $DIR/$tfile-{0..100}
22134         rm -f $DIR/$tfile-{0..100}
22135         sync
22136
22137         echo 2 > /proc/sys/vm/drop_caches
22138 }
22139 run_test 408 "drop_caches should not hang due to page leaks"
22140
22141 test_409()
22142 {
22143         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22144
22145         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
22146         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
22147         touch $DIR/$tdir/guard || error "(2) Fail to create"
22148
22149         local PREFIX=$(str_repeat 'A' 128)
22150         echo "Create 1K hard links start at $(date)"
22151         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
22152                 error "(3) Fail to hard link"
22153
22154         echo "Links count should be right although linkEA overflow"
22155         stat $DIR/$tdir/guard || error "(4) Fail to stat"
22156         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
22157         [ $linkcount -eq 1001 ] ||
22158                 error "(5) Unexpected hard links count: $linkcount"
22159
22160         echo "List all links start at $(date)"
22161         ls -l $DIR/$tdir/foo > /dev/null ||
22162                 error "(6) Fail to list $DIR/$tdir/foo"
22163
22164         echo "Unlink hard links start at $(date)"
22165         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
22166                 error "(7) Fail to unlink"
22167         echo "Unlink hard links finished at $(date)"
22168 }
22169 run_test 409 "Large amount of cross-MDTs hard links on the same file"
22170
22171 test_410()
22172 {
22173         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
22174                 skip "Need client version at least 2.9.59"
22175
22176         # Create a file, and stat it from the kernel
22177         local testfile=$DIR/$tfile
22178         touch $testfile
22179
22180         local run_id=$RANDOM
22181         local my_ino=$(stat --format "%i" $testfile)
22182
22183         # Try to insert the module. This will always fail as the
22184         # module is designed to not be inserted.
22185         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
22186             &> /dev/null
22187
22188         # Anything but success is a test failure
22189         dmesg | grep -q \
22190             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
22191             error "no inode match"
22192 }
22193 run_test 410 "Test inode number returned from kernel thread"
22194
22195 cleanup_test411_cgroup() {
22196         trap 0
22197         rmdir "$1"
22198 }
22199
22200 test_411() {
22201         local cg_basedir=/sys/fs/cgroup/memory
22202         # LU-9966
22203         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
22204                 skip "no setup for cgroup"
22205
22206         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
22207                 error "test file creation failed"
22208         cancel_lru_locks osc
22209
22210         # Create a very small memory cgroup to force a slab allocation error
22211         local cgdir=$cg_basedir/osc_slab_alloc
22212         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
22213         trap "cleanup_test411_cgroup $cgdir" EXIT
22214         echo 2M > $cgdir/memory.kmem.limit_in_bytes
22215         echo 1M > $cgdir/memory.limit_in_bytes
22216
22217         # Should not LBUG, just be killed by oom-killer
22218         # dd will return 0 even allocation failure in some environment.
22219         # So don't check return value
22220         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
22221         cleanup_test411_cgroup $cgdir
22222
22223         return 0
22224 }
22225 run_test 411 "Slab allocation error with cgroup does not LBUG"
22226
22227 test_412() {
22228         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
22229         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
22230                 skip "Need server version at least 2.10.55"
22231         fi
22232
22233         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
22234                 error "mkdir failed"
22235         $LFS getdirstripe $DIR/$tdir
22236         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
22237         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
22238                 error "expect $((MDSCOUT - 1)) get $stripe_index"
22239         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
22240         [ $stripe_count -eq 2 ] ||
22241                 error "expect 2 get $stripe_count"
22242 }
22243 run_test 412 "mkdir on specific MDTs"
22244
22245 test_qos_mkdir() {
22246         local mkdir_cmd=$1
22247         local stripe_count=$2
22248         local mdts=$(comma_list $(mdts_nodes))
22249
22250         local testdir
22251         local lmv_qos_prio_free
22252         local lmv_qos_threshold_rr
22253         local lmv_qos_maxage
22254         local lod_qos_prio_free
22255         local lod_qos_threshold_rr
22256         local lod_qos_maxage
22257         local count
22258         local i
22259
22260         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
22261         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
22262         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
22263                 head -n1)
22264         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
22265         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
22266         stack_trap "$LCTL set_param \
22267                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
22268         stack_trap "$LCTL set_param \
22269                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
22270         stack_trap "$LCTL set_param \
22271                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
22272
22273         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
22274                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
22275         lod_qos_prio_free=${lod_qos_prio_free%%%}
22276         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
22277                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
22278         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
22279         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
22280                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
22281         stack_trap "do_nodes $mdts $LCTL set_param \
22282                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
22283         stack_trap "do_nodes $mdts $LCTL set_param \
22284                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
22285                 EXIT
22286         stack_trap "do_nodes $mdts $LCTL set_param \
22287                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
22288
22289         echo
22290         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
22291
22292         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
22293         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
22294
22295         testdir=$DIR/$tdir-s$stripe_count/rr
22296
22297         for i in $(seq $((100 * MDSCOUNT))); do
22298                 eval $mkdir_cmd $testdir/subdir$i ||
22299                         error "$mkdir_cmd subdir$i failed"
22300         done
22301
22302         for i in $(seq $MDSCOUNT); do
22303                 count=$($LFS getdirstripe -i $testdir/* |
22304                                 grep ^$((i - 1))$ | wc -l)
22305                 echo "$count directories created on MDT$((i - 1))"
22306                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
22307
22308                 if [ $stripe_count -gt 1 ]; then
22309                         count=$($LFS getdirstripe $testdir/* |
22310                                 grep -P "^\s+$((i - 1))\t" | wc -l)
22311                         echo "$count stripes created on MDT$((i - 1))"
22312                         # deviation should < 5% of average
22313                         [ $count -lt $((95 * stripe_count)) ] ||
22314                         [ $count -gt $((105 * stripe_count)) ] &&
22315                                 error "stripes are not evenly distributed"
22316                 fi
22317         done
22318
22319         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
22320         do_nodes $mdts $LCTL set_param \
22321                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
22322
22323         echo
22324         echo "Check for uneven MDTs: "
22325
22326         local ffree
22327         local bavail
22328         local max
22329         local min
22330         local max_index
22331         local min_index
22332         local tmp
22333
22334         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
22335         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
22336         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
22337
22338         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
22339         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
22340         max_index=0
22341         min_index=0
22342         for ((i = 1; i < ${#ffree[@]}; i++)); do
22343                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
22344                 if [ $tmp -gt $max ]; then
22345                         max=$tmp
22346                         max_index=$i
22347                 fi
22348                 if [ $tmp -lt $min ]; then
22349                         min=$tmp
22350                         min_index=$i
22351                 fi
22352         done
22353
22354         [ ${ffree[min_index]} -eq 0 ] &&
22355                 skip "no free files in MDT$min_index"
22356         [ ${ffree[min_index]} -gt 100000000 ] &&
22357                 skip "too much free files in MDT$min_index"
22358
22359         # Check if we need to generate uneven MDTs
22360         local threshold=50
22361         local diff=$(((max - min) * 100 / min))
22362         local value="$(generate_string 1024)"
22363
22364         while [ $diff -lt $threshold ]; do
22365                 # generate uneven MDTs, create till $threshold% diff
22366                 echo -n "weight diff=$diff% must be > $threshold% ..."
22367                 count=$((${ffree[min_index]} / 10))
22368                 # 50 sec per 10000 files in vm
22369                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
22370                         skip "$count files to create"
22371                 echo "Fill MDT$min_index with $count files"
22372                 [ -d $DIR/$tdir-MDT$min_index ] ||
22373                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
22374                         error "mkdir $tdir-MDT$min_index failed"
22375                 for i in $(seq $count); do
22376                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
22377                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
22378                                 error "create f$j_$i failed"
22379                         setfattr -n user.413b -v $value \
22380                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
22381                                 error "setfattr f$j_$i failed"
22382                 done
22383
22384                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
22385                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
22386                 max=$(((${ffree[max_index]} >> 8) * \
22387                         (${bavail[max_index]} * bsize >> 16)))
22388                 min=$(((${ffree[min_index]} >> 8) * \
22389                         (${bavail[min_index]} * bsize >> 16)))
22390                 diff=$(((max - min) * 100 / min))
22391         done
22392
22393         echo "MDT filesfree available: ${ffree[@]}"
22394         echo "MDT blocks available: ${bavail[@]}"
22395         echo "weight diff=$diff%"
22396
22397         echo
22398         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
22399
22400         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
22401         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
22402         # decrease statfs age, so that it can be updated in time
22403         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
22404         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
22405
22406         sleep 1
22407
22408         testdir=$DIR/$tdir-s$stripe_count/qos
22409
22410         for i in $(seq $((100 * MDSCOUNT))); do
22411                 eval $mkdir_cmd $testdir/subdir$i ||
22412                         error "$mkdir_cmd subdir$i failed"
22413         done
22414
22415         for i in $(seq $MDSCOUNT); do
22416                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
22417                         wc -l)
22418                 echo "$count directories created on MDT$((i - 1))"
22419
22420                 if [ $stripe_count -gt 1 ]; then
22421                         count=$($LFS getdirstripe $testdir/* |
22422                                 grep -P "^\s+$((i - 1))\t" | wc -l)
22423                         echo "$count stripes created on MDT$((i - 1))"
22424                 fi
22425         done
22426
22427         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
22428         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
22429
22430         # D-value should > 10% of averge
22431         [ $((max - min)) -lt 10 ] &&
22432                 error "subdirs shouldn't be evenly distributed"
22433
22434         # ditto
22435         if [ $stripe_count -gt 1 ]; then
22436                 max=$($LFS getdirstripe $testdir/* |
22437                         grep -P "^\s+$max_index\t" | wc -l)
22438                 min=$($LFS getdirstripe $testdir/* |
22439                         grep -P "^\s+$min_index\t" | wc -l)
22440                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
22441                         error "stripes shouldn't be evenly distributed"|| true
22442         fi
22443 }
22444
22445 test_413a() {
22446         [ $MDSCOUNT -lt 2 ] &&
22447                 skip "We need at least 2 MDTs for this test"
22448
22449         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
22450                 skip "Need server version at least 2.12.52"
22451
22452         local stripe_count
22453
22454         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
22455                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
22456                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
22457                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
22458                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
22459         done
22460 }
22461 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
22462
22463 test_413b() {
22464         [ $MDSCOUNT -lt 2 ] &&
22465                 skip "We need at least 2 MDTs for this test"
22466
22467         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
22468                 skip "Need server version at least 2.12.52"
22469
22470         local stripe_count
22471
22472         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
22473                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
22474                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
22475                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
22476                 $LFS setdirstripe -D -c $stripe_count \
22477                         $DIR/$tdir-s$stripe_count/rr ||
22478                         error "setdirstripe failed"
22479                 $LFS setdirstripe -D -c $stripe_count \
22480                         $DIR/$tdir-s$stripe_count/qos ||
22481                         error "setdirstripe failed"
22482                 test_qos_mkdir "mkdir" $stripe_count
22483         done
22484 }
22485 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
22486
22487 test_414() {
22488 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
22489         $LCTL set_param fail_loc=0x80000521
22490         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
22491         rm -f $DIR/$tfile
22492 }
22493 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
22494
22495 test_415() {
22496         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22497         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22498                 skip "Need server version at least 2.11.52"
22499
22500         # LU-11102
22501         local total
22502         local setattr_pid
22503         local start_time
22504         local end_time
22505         local duration
22506
22507         total=500
22508         # this test may be slow on ZFS
22509         [ "$mds1_FSTYPE" == "zfs" ] && total=100
22510
22511         # though this test is designed for striped directory, let's test normal
22512         # directory too since lock is always saved as CoS lock.
22513         test_mkdir $DIR/$tdir || error "mkdir $tdir"
22514         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
22515
22516         (
22517                 while true; do
22518                         touch $DIR/$tdir
22519                 done
22520         ) &
22521         setattr_pid=$!
22522
22523         start_time=$(date +%s)
22524         for i in $(seq $total); do
22525                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
22526                         > /dev/null
22527         done
22528         end_time=$(date +%s)
22529         duration=$((end_time - start_time))
22530
22531         kill -9 $setattr_pid
22532
22533         echo "rename $total files took $duration sec"
22534         [ $duration -lt 100 ] || error "rename took $duration sec"
22535 }
22536 run_test 415 "lock revoke is not missing"
22537
22538 test_416() {
22539         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22540                 skip "Need server version at least 2.11.55"
22541
22542         # define OBD_FAIL_OSD_TXN_START    0x19a
22543         do_facet mds1 lctl set_param fail_loc=0x19a
22544
22545         lfs mkdir -c $MDSCOUNT $DIR/$tdir
22546
22547         true
22548 }
22549 run_test 416 "transaction start failure won't cause system hung"
22550
22551 cleanup_417() {
22552         trap 0
22553         do_nodes $(comma_list $(mdts_nodes)) \
22554                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
22555         do_nodes $(comma_list $(mdts_nodes)) \
22556                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
22557         do_nodes $(comma_list $(mdts_nodes)) \
22558                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
22559 }
22560
22561 test_417() {
22562         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22563         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
22564                 skip "Need MDS version at least 2.11.56"
22565
22566         trap cleanup_417 RETURN EXIT
22567
22568         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
22569         do_nodes $(comma_list $(mdts_nodes)) \
22570                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
22571         $LFS migrate -m 0 $DIR/$tdir.1 &&
22572                 error "migrate dir $tdir.1 should fail"
22573
22574         do_nodes $(comma_list $(mdts_nodes)) \
22575                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
22576         $LFS mkdir -i 1 $DIR/$tdir.2 &&
22577                 error "create remote dir $tdir.2 should fail"
22578
22579         do_nodes $(comma_list $(mdts_nodes)) \
22580                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
22581         $LFS mkdir -c 2 $DIR/$tdir.3 &&
22582                 error "create striped dir $tdir.3 should fail"
22583         true
22584 }
22585 run_test 417 "disable remote dir, striped dir and dir migration"
22586
22587 # Checks that the outputs of df [-i] and lfs df [-i] match
22588 #
22589 # usage: check_lfs_df <blocks | inodes> <mountpoint>
22590 check_lfs_df() {
22591         local dir=$2
22592         local inodes
22593         local df_out
22594         local lfs_df_out
22595         local count
22596         local passed=false
22597
22598         # blocks or inodes
22599         [ "$1" == "blocks" ] && inodes= || inodes="-i"
22600
22601         for count in {1..100}; do
22602                 cancel_lru_locks
22603                 sync; sleep 0.2
22604
22605                 # read the lines of interest
22606                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
22607                         error "df $inodes $dir | tail -n +2 failed"
22608                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
22609                         error "lfs df $inodes $dir | grep summary: failed"
22610
22611                 # skip first substrings of each output as they are different
22612                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
22613                 # compare the two outputs
22614                 passed=true
22615                 for i in {1..5}; do
22616                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
22617                 done
22618                 $passed && break
22619         done
22620
22621         if ! $passed; then
22622                 df -P $inodes $dir
22623                 echo
22624                 lfs df $inodes $dir
22625                 error "df and lfs df $1 output mismatch: "      \
22626                       "df ${inodes}: ${df_out[*]}, "            \
22627                       "lfs df ${inodes}: ${lfs_df_out[*]}"
22628         fi
22629 }
22630
22631 test_418() {
22632         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22633
22634         local dir=$DIR/$tdir
22635         local numfiles=$((RANDOM % 4096 + 2))
22636         local numblocks=$((RANDOM % 256 + 1))
22637
22638         wait_delete_completed
22639         test_mkdir $dir
22640
22641         # check block output
22642         check_lfs_df blocks $dir
22643         # check inode output
22644         check_lfs_df inodes $dir
22645
22646         # create a single file and retest
22647         echo "Creating a single file and testing"
22648         createmany -o $dir/$tfile- 1 &>/dev/null ||
22649                 error "creating 1 file in $dir failed"
22650         check_lfs_df blocks $dir
22651         check_lfs_df inodes $dir
22652
22653         # create a random number of files
22654         echo "Creating $((numfiles - 1)) files and testing"
22655         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
22656                 error "creating $((numfiles - 1)) files in $dir failed"
22657
22658         # write a random number of blocks to the first test file
22659         echo "Writing $numblocks 4K blocks and testing"
22660         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
22661                 count=$numblocks &>/dev/null ||
22662                 error "dd to $dir/${tfile}-0 failed"
22663
22664         # retest
22665         check_lfs_df blocks $dir
22666         check_lfs_df inodes $dir
22667
22668         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
22669                 error "unlinking $numfiles files in $dir failed"
22670 }
22671 run_test 418 "df and lfs df outputs match"
22672
22673 test_419()
22674 {
22675         local dir=$DIR/$tdir
22676
22677         mkdir -p $dir
22678         touch $dir/file
22679
22680         cancel_lru_locks mdc
22681
22682         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
22683         $LCTL set_param fail_loc=0x1410
22684         cat $dir/file
22685         $LCTL set_param fail_loc=0
22686         rm -rf $dir
22687 }
22688 run_test 419 "Verify open file by name doesn't crash kernel"
22689
22690 test_420()
22691 {
22692         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
22693                 skip "Need MDS version at least 2.12.53"
22694
22695         local SAVE_UMASK=$(umask)
22696         local dir=$DIR/$tdir
22697         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
22698
22699         mkdir -p $dir
22700         umask 0000
22701         mkdir -m03777 $dir/testdir
22702         ls -dn $dir/testdir
22703         # Need to remove trailing '.' when SELinux is enabled
22704         local dirperms=$(ls -dn $dir/testdir |
22705                          awk '{ sub(/\.$/, "", $1); print $1}')
22706         [ $dirperms == "drwxrwsrwt" ] ||
22707                 error "incorrect perms on $dir/testdir"
22708
22709         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
22710                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
22711         ls -n $dir/testdir/testfile
22712         local fileperms=$(ls -n $dir/testdir/testfile |
22713                           awk '{ sub(/\.$/, "", $1); print $1}')
22714         [ $fileperms == "-rwxr-xr-x" ] ||
22715                 error "incorrect perms on $dir/testdir/testfile"
22716
22717         umask $SAVE_UMASK
22718 }
22719 run_test 420 "clear SGID bit on non-directories for non-members"
22720
22721 test_421a() {
22722         local cnt
22723         local fid1
22724         local fid2
22725
22726         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22727                 skip "Need MDS version at least 2.12.54"
22728
22729         test_mkdir $DIR/$tdir
22730         createmany -o $DIR/$tdir/f 3
22731         cnt=$(ls -1 $DIR/$tdir | wc -l)
22732         [ $cnt != 3 ] && error "unexpected #files: $cnt"
22733
22734         fid1=$(lfs path2fid $DIR/$tdir/f1)
22735         fid2=$(lfs path2fid $DIR/$tdir/f2)
22736         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
22737
22738         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
22739         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
22740
22741         cnt=$(ls -1 $DIR/$tdir | wc -l)
22742         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
22743
22744         rm -f $DIR/$tdir/f3 || error "can't remove f3"
22745         createmany -o $DIR/$tdir/f 3
22746         cnt=$(ls -1 $DIR/$tdir | wc -l)
22747         [ $cnt != 3 ] && error "unexpected #files: $cnt"
22748
22749         fid1=$(lfs path2fid $DIR/$tdir/f1)
22750         fid2=$(lfs path2fid $DIR/$tdir/f2)
22751         echo "remove using fsname $FSNAME"
22752         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
22753
22754         cnt=$(ls -1 $DIR/$tdir | wc -l)
22755         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
22756 }
22757 run_test 421a "simple rm by fid"
22758
22759 test_421b() {
22760         local cnt
22761         local FID1
22762         local FID2
22763
22764         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22765                 skip "Need MDS version at least 2.12.54"
22766
22767         test_mkdir $DIR/$tdir
22768         createmany -o $DIR/$tdir/f 3
22769         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
22770         MULTIPID=$!
22771
22772         FID1=$(lfs path2fid $DIR/$tdir/f1)
22773         FID2=$(lfs path2fid $DIR/$tdir/f2)
22774         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
22775
22776         kill -USR1 $MULTIPID
22777         wait
22778
22779         cnt=$(ls $DIR/$tdir | wc -l)
22780         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
22781 }
22782 run_test 421b "rm by fid on open file"
22783
22784 test_421c() {
22785         local cnt
22786         local FIDS
22787
22788         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22789                 skip "Need MDS version at least 2.12.54"
22790
22791         test_mkdir $DIR/$tdir
22792         createmany -o $DIR/$tdir/f 3
22793         touch $DIR/$tdir/$tfile
22794         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
22795         cnt=$(ls -1 $DIR/$tdir | wc -l)
22796         [ $cnt != 184 ] && error "unexpected #files: $cnt"
22797
22798         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
22799         $LFS rmfid $DIR $FID1 || error "rmfid failed"
22800
22801         cnt=$(ls $DIR/$tdir | wc -l)
22802         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
22803 }
22804 run_test 421c "rm by fid against hardlinked files"
22805
22806 test_421d() {
22807         local cnt
22808         local FIDS
22809
22810         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22811                 skip "Need MDS version at least 2.12.54"
22812
22813         test_mkdir $DIR/$tdir
22814         createmany -o $DIR/$tdir/f 4097
22815         cnt=$(ls -1 $DIR/$tdir | wc -l)
22816         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
22817
22818         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
22819         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22820
22821         cnt=$(ls $DIR/$tdir | wc -l)
22822         rm -rf $DIR/$tdir
22823         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22824 }
22825 run_test 421d "rmfid en masse"
22826
22827 test_421e() {
22828         local cnt
22829         local FID
22830
22831         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22832         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22833                 skip "Need MDS version at least 2.12.54"
22834
22835         mkdir -p $DIR/$tdir
22836         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22837         createmany -o $DIR/$tdir/striped_dir/f 512
22838         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22839         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22840
22841         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22842                 sed "s/[/][^:]*://g")
22843         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22844
22845         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22846         rm -rf $DIR/$tdir
22847         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22848 }
22849 run_test 421e "rmfid in DNE"
22850
22851 test_421f() {
22852         local cnt
22853         local FID
22854
22855         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22856                 skip "Need MDS version at least 2.12.54"
22857
22858         test_mkdir $DIR/$tdir
22859         touch $DIR/$tdir/f
22860         cnt=$(ls -1 $DIR/$tdir | wc -l)
22861         [ $cnt != 1 ] && error "unexpected #files: $cnt"
22862
22863         FID=$(lfs path2fid $DIR/$tdir/f)
22864         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
22865         # rmfid should fail
22866         cnt=$(ls -1 $DIR/$tdir | wc -l)
22867         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
22868
22869         chmod a+rw $DIR/$tdir
22870         ls -la $DIR/$tdir
22871         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
22872         # rmfid should fail
22873         cnt=$(ls -1 $DIR/$tdir | wc -l)
22874         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
22875
22876         rm -f $DIR/$tdir/f
22877         $RUNAS touch $DIR/$tdir/f
22878         FID=$(lfs path2fid $DIR/$tdir/f)
22879         echo "rmfid as root"
22880         $LFS rmfid $DIR $FID || error "rmfid as root failed"
22881         cnt=$(ls -1 $DIR/$tdir | wc -l)
22882         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
22883
22884         rm -f $DIR/$tdir/f
22885         $RUNAS touch $DIR/$tdir/f
22886         cnt=$(ls -1 $DIR/$tdir | wc -l)
22887         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
22888         FID=$(lfs path2fid $DIR/$tdir/f)
22889         # rmfid w/o user_fid2path mount option should fail
22890         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
22891         cnt=$(ls -1 $DIR/$tdir | wc -l)
22892         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
22893
22894         umount_client $MOUNT || error "failed to umount client"
22895         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
22896                 error "failed to mount client'"
22897
22898         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
22899         # rmfid should succeed
22900         cnt=$(ls -1 $DIR/$tdir | wc -l)
22901         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
22902
22903         # rmfid shouldn't allow to remove files due to dir's permission
22904         chmod a+rwx $DIR/$tdir
22905         touch $DIR/$tdir/f
22906         ls -la $DIR/$tdir
22907         FID=$(lfs path2fid $DIR/$tdir/f)
22908         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
22909
22910         umount_client $MOUNT || error "failed to umount client"
22911         mount_client $MOUNT "$MOUNT_OPTS" ||
22912                 error "failed to mount client'"
22913
22914 }
22915 run_test 421f "rmfid checks permissions"
22916
22917 test_421g() {
22918         local cnt
22919         local FIDS
22920
22921         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22922         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22923                 skip "Need MDS version at least 2.12.54"
22924
22925         mkdir -p $DIR/$tdir
22926         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22927         createmany -o $DIR/$tdir/striped_dir/f 512
22928         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22929         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22930
22931         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22932                 sed "s/[/][^:]*://g")
22933
22934         rm -f $DIR/$tdir/striped_dir/f1*
22935         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22936         removed=$((512 - cnt))
22937
22938         # few files have been just removed, so we expect
22939         # rmfid to fail on their fids
22940         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
22941         [ $removed != $errors ] && error "$errors != $removed"
22942
22943         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22944         rm -rf $DIR/$tdir
22945         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22946 }
22947 run_test 421g "rmfid to return errors properly"
22948
22949 test_422() {
22950         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
22951         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
22952         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
22953         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
22954         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
22955
22956         local amc=$(at_max_get client)
22957         local amo=$(at_max_get mds1)
22958         local timeout=`lctl get_param -n timeout`
22959
22960         at_max_set 0 client
22961         at_max_set 0 mds1
22962
22963 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
22964         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
22965                         fail_val=$(((2*timeout + 10)*1000))
22966         touch $DIR/$tdir/d3/file &
22967         sleep 2
22968 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
22969         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
22970                         fail_val=$((2*timeout + 5))
22971         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
22972         local pid=$!
22973         sleep 1
22974         kill -9 $pid
22975         sleep $((2 * timeout))
22976         echo kill $pid
22977         kill -9 $pid
22978         lctl mark touch
22979         touch $DIR/$tdir/d2/file3
22980         touch $DIR/$tdir/d2/file4
22981         touch $DIR/$tdir/d2/file5
22982
22983         wait
22984         at_max_set $amc client
22985         at_max_set $amo mds1
22986
22987         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
22988         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
22989                 error "Watchdog is always throttled"
22990 }
22991 run_test 422 "kill a process with RPC in progress"
22992
22993 stat_test() {
22994     df -h $MOUNT &
22995     df -h $MOUNT &
22996     df -h $MOUNT &
22997     df -h $MOUNT &
22998     df -h $MOUNT &
22999     df -h $MOUNT &
23000 }
23001
23002 test_423() {
23003     local _stats
23004     # ensure statfs cache is expired
23005     sleep 2;
23006
23007     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
23008     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
23009
23010     return 0
23011 }
23012 run_test 423 "statfs should return a right data"
23013
23014 test_424() {
23015 #define OBD_FAIL_PTLRPC_BULK_REPLY_ATTACH      0x522 | OBD_FAIL_ONCE
23016         $LCTL set_param fail_loc=0x80000522
23017         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
23018         rm -f $DIR/$tfile
23019 }
23020 run_test 424 "simulate ENOMEM in ptl_send_rpc bulk reply ME attach"
23021
23022 prep_801() {
23023         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
23024         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
23025                 skip "Need server version at least 2.9.55"
23026
23027         start_full_debug_logging
23028 }
23029
23030 post_801() {
23031         stop_full_debug_logging
23032 }
23033
23034 barrier_stat() {
23035         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
23036                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
23037                            awk '/The barrier for/ { print $7 }')
23038                 echo $st
23039         else
23040                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
23041                 echo \'$st\'
23042         fi
23043 }
23044
23045 barrier_expired() {
23046         local expired
23047
23048         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
23049                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
23050                           awk '/will be expired/ { print $7 }')
23051         else
23052                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
23053         fi
23054
23055         echo $expired
23056 }
23057
23058 test_801a() {
23059         prep_801
23060
23061         echo "Start barrier_freeze at: $(date)"
23062         #define OBD_FAIL_BARRIER_DELAY          0x2202
23063         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
23064         # Do not reduce barrier time - See LU-11873
23065         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
23066
23067         sleep 2
23068         local b_status=$(barrier_stat)
23069         echo "Got barrier status at: $(date)"
23070         [ "$b_status" = "'freezing_p1'" ] ||
23071                 error "(1) unexpected barrier status $b_status"
23072
23073         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
23074         wait
23075         b_status=$(barrier_stat)
23076         [ "$b_status" = "'frozen'" ] ||
23077                 error "(2) unexpected barrier status $b_status"
23078
23079         local expired=$(barrier_expired)
23080         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
23081         sleep $((expired + 3))
23082
23083         b_status=$(barrier_stat)
23084         [ "$b_status" = "'expired'" ] ||
23085                 error "(3) unexpected barrier status $b_status"
23086
23087         # Do not reduce barrier time - See LU-11873
23088         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
23089                 error "(4) fail to freeze barrier"
23090
23091         b_status=$(barrier_stat)
23092         [ "$b_status" = "'frozen'" ] ||
23093                 error "(5) unexpected barrier status $b_status"
23094
23095         echo "Start barrier_thaw at: $(date)"
23096         #define OBD_FAIL_BARRIER_DELAY          0x2202
23097         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
23098         do_facet mgs $LCTL barrier_thaw $FSNAME &
23099
23100         sleep 2
23101         b_status=$(barrier_stat)
23102         echo "Got barrier status at: $(date)"
23103         [ "$b_status" = "'thawing'" ] ||
23104                 error "(6) unexpected barrier status $b_status"
23105
23106         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
23107         wait
23108         b_status=$(barrier_stat)
23109         [ "$b_status" = "'thawed'" ] ||
23110                 error "(7) unexpected barrier status $b_status"
23111
23112         #define OBD_FAIL_BARRIER_FAILURE        0x2203
23113         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
23114         do_facet mgs $LCTL barrier_freeze $FSNAME
23115
23116         b_status=$(barrier_stat)
23117         [ "$b_status" = "'failed'" ] ||
23118                 error "(8) unexpected barrier status $b_status"
23119
23120         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
23121         do_facet mgs $LCTL barrier_thaw $FSNAME
23122
23123         post_801
23124 }
23125 run_test 801a "write barrier user interfaces and stat machine"
23126
23127 test_801b() {
23128         prep_801
23129
23130         mkdir $DIR/$tdir || error "(1) fail to mkdir"
23131         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
23132         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
23133         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
23134         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
23135
23136         cancel_lru_locks mdc
23137
23138         # 180 seconds should be long enough
23139         do_facet mgs $LCTL barrier_freeze $FSNAME 180
23140
23141         local b_status=$(barrier_stat)
23142         [ "$b_status" = "'frozen'" ] ||
23143                 error "(6) unexpected barrier status $b_status"
23144
23145         mkdir $DIR/$tdir/d0/d10 &
23146         mkdir_pid=$!
23147
23148         touch $DIR/$tdir/d1/f13 &
23149         touch_pid=$!
23150
23151         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
23152         ln_pid=$!
23153
23154         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
23155         mv_pid=$!
23156
23157         rm -f $DIR/$tdir/d4/f12 &
23158         rm_pid=$!
23159
23160         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
23161
23162         # To guarantee taht the 'stat' is not blocked
23163         b_status=$(barrier_stat)
23164         [ "$b_status" = "'frozen'" ] ||
23165                 error "(8) unexpected barrier status $b_status"
23166
23167         # let above commands to run at background
23168         sleep 5
23169
23170         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
23171         ps -p $touch_pid || error "(10) touch should be blocked"
23172         ps -p $ln_pid || error "(11) link should be blocked"
23173         ps -p $mv_pid || error "(12) rename should be blocked"
23174         ps -p $rm_pid || error "(13) unlink should be blocked"
23175
23176         b_status=$(barrier_stat)
23177         [ "$b_status" = "'frozen'" ] ||
23178                 error "(14) unexpected barrier status $b_status"
23179
23180         do_facet mgs $LCTL barrier_thaw $FSNAME
23181         b_status=$(barrier_stat)
23182         [ "$b_status" = "'thawed'" ] ||
23183                 error "(15) unexpected barrier status $b_status"
23184
23185         wait $mkdir_pid || error "(16) mkdir should succeed"
23186         wait $touch_pid || error "(17) touch should succeed"
23187         wait $ln_pid || error "(18) link should succeed"
23188         wait $mv_pid || error "(19) rename should succeed"
23189         wait $rm_pid || error "(20) unlink should succeed"
23190
23191         post_801
23192 }
23193 run_test 801b "modification will be blocked by write barrier"
23194
23195 test_801c() {
23196         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23197
23198         prep_801
23199
23200         stop mds2 || error "(1) Fail to stop mds2"
23201
23202         do_facet mgs $LCTL barrier_freeze $FSNAME 30
23203
23204         local b_status=$(barrier_stat)
23205         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
23206                 do_facet mgs $LCTL barrier_thaw $FSNAME
23207                 error "(2) unexpected barrier status $b_status"
23208         }
23209
23210         do_facet mgs $LCTL barrier_rescan $FSNAME ||
23211                 error "(3) Fail to rescan barrier bitmap"
23212
23213         # Do not reduce barrier time - See LU-11873
23214         do_facet mgs $LCTL barrier_freeze $FSNAME 20
23215
23216         b_status=$(barrier_stat)
23217         [ "$b_status" = "'frozen'" ] ||
23218                 error "(4) unexpected barrier status $b_status"
23219
23220         do_facet mgs $LCTL barrier_thaw $FSNAME
23221         b_status=$(barrier_stat)
23222         [ "$b_status" = "'thawed'" ] ||
23223                 error "(5) unexpected barrier status $b_status"
23224
23225         local devname=$(mdsdevname 2)
23226
23227         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
23228
23229         do_facet mgs $LCTL barrier_rescan $FSNAME ||
23230                 error "(7) Fail to rescan barrier bitmap"
23231
23232         post_801
23233 }
23234 run_test 801c "rescan barrier bitmap"
23235
23236 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
23237 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
23238 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
23239 saved_MOUNT_OPTS=$MOUNT_OPTS
23240
23241 cleanup_802a() {
23242         trap 0
23243
23244         stopall
23245         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
23246         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
23247         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
23248         MOUNT_OPTS=$saved_MOUNT_OPTS
23249         setupall
23250 }
23251
23252 test_802a() {
23253         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
23254         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
23255         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
23256                 skip "Need server version at least 2.9.55"
23257
23258         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
23259
23260         mkdir $DIR/$tdir || error "(1) fail to mkdir"
23261
23262         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
23263                 error "(2) Fail to copy"
23264
23265         trap cleanup_802a EXIT
23266
23267         # sync by force before remount as readonly
23268         sync; sync_all_data; sleep 3; sync_all_data
23269
23270         stopall
23271
23272         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
23273         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
23274         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
23275
23276         echo "Mount the server as read only"
23277         setupall server_only || error "(3) Fail to start servers"
23278
23279         echo "Mount client without ro should fail"
23280         mount_client $MOUNT &&
23281                 error "(4) Mount client without 'ro' should fail"
23282
23283         echo "Mount client with ro should succeed"
23284         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
23285         mount_client $MOUNT ||
23286                 error "(5) Mount client with 'ro' should succeed"
23287
23288         echo "Modify should be refused"
23289         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
23290
23291         echo "Read should be allowed"
23292         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
23293                 error "(7) Read should succeed under ro mode"
23294
23295         cleanup_802a
23296 }
23297 run_test 802a "simulate readonly device"
23298
23299 test_802b() {
23300         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23301         remote_mds_nodsh && skip "remote MDS with nodsh"
23302
23303         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
23304                 skip "readonly option not available"
23305
23306         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
23307
23308         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
23309                 error "(2) Fail to copy"
23310
23311         # write back all cached data before setting MDT to readonly
23312         cancel_lru_locks
23313         sync_all_data
23314
23315         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
23316         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
23317
23318         echo "Modify should be refused"
23319         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
23320
23321         echo "Read should be allowed"
23322         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
23323                 error "(7) Read should succeed under ro mode"
23324
23325         # disable readonly
23326         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
23327 }
23328 run_test 802b "be able to set MDTs to readonly"
23329
23330 test_803() {
23331         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23332         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
23333                 skip "MDS needs to be newer than 2.10.54"
23334
23335         mkdir -p $DIR/$tdir
23336         # Create some objects on all MDTs to trigger related logs objects
23337         for idx in $(seq $MDSCOUNT); do
23338                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
23339                         $DIR/$tdir/dir${idx} ||
23340                         error "Fail to create $DIR/$tdir/dir${idx}"
23341         done
23342
23343         sync; sleep 3
23344         wait_delete_completed # ensure old test cleanups are finished
23345         echo "before create:"
23346         $LFS df -i $MOUNT
23347         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23348
23349         for i in {1..10}; do
23350                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
23351                         error "Fail to create $DIR/$tdir/foo$i"
23352         done
23353
23354         sync; sleep 3
23355         echo "after create:"
23356         $LFS df -i $MOUNT
23357         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23358
23359         # allow for an llog to be cleaned up during the test
23360         [ $after_used -ge $((before_used + 10 - 1)) ] ||
23361                 error "before ($before_used) + 10 > after ($after_used)"
23362
23363         for i in {1..10}; do
23364                 rm -rf $DIR/$tdir/foo$i ||
23365                         error "Fail to remove $DIR/$tdir/foo$i"
23366         done
23367
23368         sleep 3 # avoid MDT return cached statfs
23369         wait_delete_completed
23370         echo "after unlink:"
23371         $LFS df -i $MOUNT
23372         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23373
23374         # allow for an llog to be created during the test
23375         [ $after_used -le $((before_used + 1)) ] ||
23376                 error "after ($after_used) > before ($before_used) + 1"
23377 }
23378 run_test 803 "verify agent object for remote object"
23379
23380 test_804() {
23381         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23382         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
23383                 skip "MDS needs to be newer than 2.10.54"
23384         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
23385
23386         mkdir -p $DIR/$tdir
23387         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
23388                 error "Fail to create $DIR/$tdir/dir0"
23389
23390         local fid=$($LFS path2fid $DIR/$tdir/dir0)
23391         local dev=$(mdsdevname 2)
23392
23393         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23394                 grep ${fid} || error "NOT found agent entry for dir0"
23395
23396         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
23397                 error "Fail to create $DIR/$tdir/dir1"
23398
23399         touch $DIR/$tdir/dir1/foo0 ||
23400                 error "Fail to create $DIR/$tdir/dir1/foo0"
23401         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
23402         local rc=0
23403
23404         for idx in $(seq $MDSCOUNT); do
23405                 dev=$(mdsdevname $idx)
23406                 do_facet mds${idx} \
23407                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23408                         grep ${fid} && rc=$idx
23409         done
23410
23411         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
23412                 error "Fail to rename foo0 to foo1"
23413         if [ $rc -eq 0 ]; then
23414                 for idx in $(seq $MDSCOUNT); do
23415                         dev=$(mdsdevname $idx)
23416                         do_facet mds${idx} \
23417                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23418                         grep ${fid} && rc=$idx
23419                 done
23420         fi
23421
23422         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
23423                 error "Fail to rename foo1 to foo2"
23424         if [ $rc -eq 0 ]; then
23425                 for idx in $(seq $MDSCOUNT); do
23426                         dev=$(mdsdevname $idx)
23427                         do_facet mds${idx} \
23428                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23429                         grep ${fid} && rc=$idx
23430                 done
23431         fi
23432
23433         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
23434
23435         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
23436                 error "Fail to link to $DIR/$tdir/dir1/foo2"
23437         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
23438                 error "Fail to rename foo2 to foo0"
23439         unlink $DIR/$tdir/dir1/foo0 ||
23440                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
23441         rm -rf $DIR/$tdir/dir0 ||
23442                 error "Fail to rm $DIR/$tdir/dir0"
23443
23444         for idx in $(seq $MDSCOUNT); do
23445                 dev=$(mdsdevname $idx)
23446                 rc=0
23447
23448                 stop mds${idx}
23449                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
23450                         rc=$?
23451                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
23452                         error "mount mds$idx failed"
23453                 df $MOUNT > /dev/null 2>&1
23454
23455                 # e2fsck should not return error
23456                 [ $rc -eq 0 ] ||
23457                         error "e2fsck detected error on MDT${idx}: rc=$rc"
23458         done
23459 }
23460 run_test 804 "verify agent entry for remote entry"
23461
23462 cleanup_805() {
23463         do_facet $SINGLEMDS zfs set quota=$old $fsset
23464         unlinkmany $DIR/$tdir/f- 1000000
23465         trap 0
23466 }
23467
23468 test_805() {
23469         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
23470         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
23471         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
23472                 skip "netfree not implemented before 0.7"
23473         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
23474                 skip "Need MDS version at least 2.10.57"
23475
23476         local fsset
23477         local freekb
23478         local usedkb
23479         local old
23480         local quota
23481         local pref="osd-zfs.$FSNAME-MDT0000."
23482
23483         # limit available space on MDS dataset to meet nospace issue
23484         # quickly. then ZFS 0.7.2 can use reserved space if asked
23485         # properly (using netfree flag in osd_declare_destroy()
23486         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
23487         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
23488                 gawk '{print $3}')
23489         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
23490         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
23491         let "usedkb=usedkb-freekb"
23492         let "freekb=freekb/2"
23493         if let "freekb > 5000"; then
23494                 let "freekb=5000"
23495         fi
23496         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
23497         trap cleanup_805 EXIT
23498         mkdir $DIR/$tdir
23499         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
23500                 error "Can't set PFL layout"
23501         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
23502         rm -rf $DIR/$tdir || error "not able to remove"
23503         do_facet $SINGLEMDS zfs set quota=$old $fsset
23504         trap 0
23505 }
23506 run_test 805 "ZFS can remove from full fs"
23507
23508 # Size-on-MDS test
23509 check_lsom_data()
23510 {
23511         local file=$1
23512         local size=$($LFS getsom -s $file)
23513         local expect=$(stat -c %s $file)
23514
23515         [[ $size == $expect ]] ||
23516                 error "$file expected size: $expect, got: $size"
23517
23518         local blocks=$($LFS getsom -b $file)
23519         expect=$(stat -c %b $file)
23520         [[ $blocks == $expect ]] ||
23521                 error "$file expected blocks: $expect, got: $blocks"
23522 }
23523
23524 check_lsom_size()
23525 {
23526         local size=$($LFS getsom -s $1)
23527         local expect=$2
23528
23529         [[ $size == $expect ]] ||
23530                 error "$file expected size: $expect, got: $size"
23531 }
23532
23533 test_806() {
23534         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
23535                 skip "Need MDS version at least 2.11.52"
23536
23537         local bs=1048576
23538
23539         touch $DIR/$tfile || error "touch $tfile failed"
23540
23541         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23542         save_lustre_params client "llite.*.xattr_cache" > $save
23543         lctl set_param llite.*.xattr_cache=0
23544         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23545
23546         # single-threaded write
23547         echo "Test SOM for single-threaded write"
23548         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
23549                 error "write $tfile failed"
23550         check_lsom_size $DIR/$tfile $bs
23551
23552         local num=32
23553         local size=$(($num * $bs))
23554         local offset=0
23555         local i
23556
23557         echo "Test SOM for single client multi-threaded($num) write"
23558         $TRUNCATE $DIR/$tfile 0
23559         for ((i = 0; i < $num; i++)); do
23560                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23561                 local pids[$i]=$!
23562                 offset=$((offset + $bs))
23563         done
23564         for (( i=0; i < $num; i++ )); do
23565                 wait ${pids[$i]}
23566         done
23567         check_lsom_size $DIR/$tfile $size
23568
23569         $TRUNCATE $DIR/$tfile 0
23570         for ((i = 0; i < $num; i++)); do
23571                 offset=$((offset - $bs))
23572                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23573                 local pids[$i]=$!
23574         done
23575         for (( i=0; i < $num; i++ )); do
23576                 wait ${pids[$i]}
23577         done
23578         check_lsom_size $DIR/$tfile $size
23579
23580         # multi-client writes
23581         num=$(get_node_count ${CLIENTS//,/ })
23582         size=$(($num * $bs))
23583         offset=0
23584         i=0
23585
23586         echo "Test SOM for multi-client ($num) writes"
23587         $TRUNCATE $DIR/$tfile 0
23588         for client in ${CLIENTS//,/ }; do
23589                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23590                 local pids[$i]=$!
23591                 i=$((i + 1))
23592                 offset=$((offset + $bs))
23593         done
23594         for (( i=0; i < $num; i++ )); do
23595                 wait ${pids[$i]}
23596         done
23597         check_lsom_size $DIR/$tfile $offset
23598
23599         i=0
23600         $TRUNCATE $DIR/$tfile 0
23601         for client in ${CLIENTS//,/ }; do
23602                 offset=$((offset - $bs))
23603                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23604                 local pids[$i]=$!
23605                 i=$((i + 1))
23606         done
23607         for (( i=0; i < $num; i++ )); do
23608                 wait ${pids[$i]}
23609         done
23610         check_lsom_size $DIR/$tfile $size
23611
23612         # verify truncate
23613         echo "Test SOM for truncate"
23614         $TRUNCATE $DIR/$tfile 1048576
23615         check_lsom_size $DIR/$tfile 1048576
23616         $TRUNCATE $DIR/$tfile 1234
23617         check_lsom_size $DIR/$tfile 1234
23618
23619         # verify SOM blocks count
23620         echo "Verify SOM block count"
23621         $TRUNCATE $DIR/$tfile 0
23622         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
23623                 error "failed to write file $tfile"
23624         check_lsom_data $DIR/$tfile
23625 }
23626 run_test 806 "Verify Lazy Size on MDS"
23627
23628 test_807() {
23629         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
23630         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
23631                 skip "Need MDS version at least 2.11.52"
23632
23633         # Registration step
23634         changelog_register || error "changelog_register failed"
23635         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
23636         changelog_users $SINGLEMDS | grep -q $cl_user ||
23637                 error "User $cl_user not found in changelog_users"
23638
23639         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23640         save_lustre_params client "llite.*.xattr_cache" > $save
23641         lctl set_param llite.*.xattr_cache=0
23642         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23643
23644         rm -rf $DIR/$tdir || error "rm $tdir failed"
23645         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
23646         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
23647         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
23648         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
23649                 error "truncate $tdir/trunc failed"
23650
23651         local bs=1048576
23652         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
23653                 error "write $tfile failed"
23654
23655         # multi-client wirtes
23656         local num=$(get_node_count ${CLIENTS//,/ })
23657         local offset=0
23658         local i=0
23659
23660         echo "Test SOM for multi-client ($num) writes"
23661         touch $DIR/$tfile || error "touch $tfile failed"
23662         $TRUNCATE $DIR/$tfile 0
23663         for client in ${CLIENTS//,/ }; do
23664                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23665                 local pids[$i]=$!
23666                 i=$((i + 1))
23667                 offset=$((offset + $bs))
23668         done
23669         for (( i=0; i < $num; i++ )); do
23670                 wait ${pids[$i]}
23671         done
23672
23673         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
23674         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
23675         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
23676         check_lsom_data $DIR/$tdir/trunc
23677         check_lsom_data $DIR/$tdir/single_dd
23678         check_lsom_data $DIR/$tfile
23679
23680         rm -rf $DIR/$tdir
23681         # Deregistration step
23682         changelog_deregister || error "changelog_deregister failed"
23683 }
23684 run_test 807 "verify LSOM syncing tool"
23685
23686 check_som_nologged()
23687 {
23688         local lines=$($LFS changelog $FSNAME-MDT0000 |
23689                 grep 'x=trusted.som' | wc -l)
23690         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
23691 }
23692
23693 test_808() {
23694         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
23695                 skip "Need MDS version at least 2.11.55"
23696
23697         # Registration step
23698         changelog_register || error "changelog_register failed"
23699
23700         touch $DIR/$tfile || error "touch $tfile failed"
23701         check_som_nologged
23702
23703         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
23704                 error "write $tfile failed"
23705         check_som_nologged
23706
23707         $TRUNCATE $DIR/$tfile 1234
23708         check_som_nologged
23709
23710         $TRUNCATE $DIR/$tfile 1048576
23711         check_som_nologged
23712
23713         # Deregistration step
23714         changelog_deregister || error "changelog_deregister failed"
23715 }
23716 run_test 808 "Check trusted.som xattr not logged in Changelogs"
23717
23718 check_som_nodata()
23719 {
23720         $LFS getsom $1
23721         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
23722 }
23723
23724 test_809() {
23725         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
23726                 skip "Need MDS version at least 2.11.56"
23727
23728         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
23729                 error "failed to create DoM-only file $DIR/$tfile"
23730         touch $DIR/$tfile || error "touch $tfile failed"
23731         check_som_nodata $DIR/$tfile
23732
23733         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
23734                 error "write $tfile failed"
23735         check_som_nodata $DIR/$tfile
23736
23737         $TRUNCATE $DIR/$tfile 1234
23738         check_som_nodata $DIR/$tfile
23739
23740         $TRUNCATE $DIR/$tfile 4097
23741         check_som_nodata $DIR/$file
23742 }
23743 run_test 809 "Verify no SOM xattr store for DoM-only files"
23744
23745 test_810() {
23746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23747         $GSS && skip_env "could not run with gss"
23748         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
23749                 skip "OST < 2.12.58 doesn't align checksum"
23750
23751         set_checksums 1
23752         stack_trap "set_checksums $ORIG_CSUM" EXIT
23753         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
23754
23755         local csum
23756         local before
23757         local after
23758         for csum in $CKSUM_TYPES; do
23759                 #define OBD_FAIL_OSC_NO_GRANT   0x411
23760                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
23761                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
23762                         eval set -- $i
23763                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
23764                         before=$(md5sum $DIR/$tfile)
23765                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
23766                         after=$(md5sum $DIR/$tfile)
23767                         [ "$before" == "$after" ] ||
23768                                 error "$csum: $before != $after bs=$1 seek=$2"
23769                 done
23770         done
23771 }
23772 run_test 810 "partial page writes on ZFS (LU-11663)"
23773
23774 test_812a() {
23775         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23776                 skip "OST < 2.12.51 doesn't support this fail_loc"
23777         [ "$SHARED_KEY" = true ] &&
23778                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23779
23780         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23781         # ensure ost1 is connected
23782         stat $DIR/$tfile >/dev/null || error "can't stat"
23783         wait_osc_import_state client ost1 FULL
23784         # no locks, no reqs to let the connection idle
23785         cancel_lru_locks osc
23786
23787         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23788 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23789         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23790         wait_osc_import_state client ost1 CONNECTING
23791         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23792
23793         stat $DIR/$tfile >/dev/null || error "can't stat file"
23794 }
23795 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
23796
23797 test_812b() { # LU-12378
23798         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23799                 skip "OST < 2.12.51 doesn't support this fail_loc"
23800         [ "$SHARED_KEY" = true ] &&
23801                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23802
23803         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
23804         # ensure ost1 is connected
23805         stat $DIR/$tfile >/dev/null || error "can't stat"
23806         wait_osc_import_state client ost1 FULL
23807         # no locks, no reqs to let the connection idle
23808         cancel_lru_locks osc
23809
23810         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23811 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23812         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23813         wait_osc_import_state client ost1 CONNECTING
23814         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23815
23816         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
23817         wait_osc_import_state client ost1 IDLE
23818 }
23819 run_test 812b "do not drop no resend request for idle connect"
23820
23821 test_813() {
23822         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
23823         [ -z "$file_heat_sav" ] && skip "no file heat support"
23824
23825         local readsample
23826         local writesample
23827         local readbyte
23828         local writebyte
23829         local readsample1
23830         local writesample1
23831         local readbyte1
23832         local writebyte1
23833
23834         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
23835         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
23836
23837         $LCTL set_param -n llite.*.file_heat=1
23838         echo "Turn on file heat"
23839         echo "Period second: $period_second, Decay percentage: $decay_pct"
23840
23841         echo "QQQQ" > $DIR/$tfile
23842         echo "QQQQ" > $DIR/$tfile
23843         echo "QQQQ" > $DIR/$tfile
23844         cat $DIR/$tfile > /dev/null
23845         cat $DIR/$tfile > /dev/null
23846         cat $DIR/$tfile > /dev/null
23847         cat $DIR/$tfile > /dev/null
23848
23849         local out=$($LFS heat_get $DIR/$tfile)
23850
23851         $LFS heat_get $DIR/$tfile
23852         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23853         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23854         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23855         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23856
23857         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
23858         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
23859         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
23860         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
23861
23862         sleep $((period_second + 3))
23863         echo "Sleep $((period_second + 3)) seconds..."
23864         # The recursion formula to calculate the heat of the file f is as
23865         # follow:
23866         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
23867         # Where Hi is the heat value in the period between time points i*I and
23868         # (i+1)*I; Ci is the access count in the period; the symbol P refers
23869         # to the weight of Ci.
23870         out=$($LFS heat_get $DIR/$tfile)
23871         $LFS heat_get $DIR/$tfile
23872         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23873         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23874         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23875         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23876
23877         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
23878                 error "read sample ($readsample) is wrong"
23879         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
23880                 error "write sample ($writesample) is wrong"
23881         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
23882                 error "read bytes ($readbyte) is wrong"
23883         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
23884                 error "write bytes ($writebyte) is wrong"
23885
23886         echo "QQQQ" > $DIR/$tfile
23887         echo "QQQQ" > $DIR/$tfile
23888         echo "QQQQ" > $DIR/$tfile
23889         cat $DIR/$tfile > /dev/null
23890         cat $DIR/$tfile > /dev/null
23891         cat $DIR/$tfile > /dev/null
23892         cat $DIR/$tfile > /dev/null
23893
23894         sleep $((period_second + 3))
23895         echo "Sleep $((period_second + 3)) seconds..."
23896
23897         out=$($LFS heat_get $DIR/$tfile)
23898         $LFS heat_get $DIR/$tfile
23899         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23900         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23901         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23902         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23903
23904         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
23905                 4 * $decay_pct) / 100") -eq 1 ] ||
23906                 error "read sample ($readsample1) is wrong"
23907         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
23908                 3 * $decay_pct) / 100") -eq 1 ] ||
23909                 error "write sample ($writesample1) is wrong"
23910         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
23911                 20 * $decay_pct) / 100") -eq 1 ] ||
23912                 error "read bytes ($readbyte1) is wrong"
23913         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
23914                 15 * $decay_pct) / 100") -eq 1 ] ||
23915                 error "write bytes ($writebyte1) is wrong"
23916
23917         echo "Turn off file heat for the file $DIR/$tfile"
23918         $LFS heat_set -o $DIR/$tfile
23919
23920         echo "QQQQ" > $DIR/$tfile
23921         echo "QQQQ" > $DIR/$tfile
23922         echo "QQQQ" > $DIR/$tfile
23923         cat $DIR/$tfile > /dev/null
23924         cat $DIR/$tfile > /dev/null
23925         cat $DIR/$tfile > /dev/null
23926         cat $DIR/$tfile > /dev/null
23927
23928         out=$($LFS heat_get $DIR/$tfile)
23929         $LFS heat_get $DIR/$tfile
23930         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23931         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23932         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23933         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23934
23935         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23936         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23937         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23938         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23939
23940         echo "Trun on file heat for the file $DIR/$tfile"
23941         $LFS heat_set -O $DIR/$tfile
23942
23943         echo "QQQQ" > $DIR/$tfile
23944         echo "QQQQ" > $DIR/$tfile
23945         echo "QQQQ" > $DIR/$tfile
23946         cat $DIR/$tfile > /dev/null
23947         cat $DIR/$tfile > /dev/null
23948         cat $DIR/$tfile > /dev/null
23949         cat $DIR/$tfile > /dev/null
23950
23951         out=$($LFS heat_get $DIR/$tfile)
23952         $LFS heat_get $DIR/$tfile
23953         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23954         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23955         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23956         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23957
23958         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
23959         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
23960         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
23961         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
23962
23963         $LFS heat_set -c $DIR/$tfile
23964         $LCTL set_param -n llite.*.file_heat=0
23965         echo "Turn off file heat support for the Lustre filesystem"
23966
23967         echo "QQQQ" > $DIR/$tfile
23968         echo "QQQQ" > $DIR/$tfile
23969         echo "QQQQ" > $DIR/$tfile
23970         cat $DIR/$tfile > /dev/null
23971         cat $DIR/$tfile > /dev/null
23972         cat $DIR/$tfile > /dev/null
23973         cat $DIR/$tfile > /dev/null
23974
23975         out=$($LFS heat_get $DIR/$tfile)
23976         $LFS heat_get $DIR/$tfile
23977         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23978         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23979         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23980         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23981
23982         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23983         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23984         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23985         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23986
23987         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
23988         rm -f $DIR/$tfile
23989 }
23990 run_test 813 "File heat verfication"
23991
23992 test_814()
23993 {
23994         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
23995         echo -n y >> $DIR/$tfile
23996         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
23997         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
23998 }
23999 run_test 814 "sparse cp works as expected (LU-12361)"
24000
24001 test_815()
24002 {
24003         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
24004         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
24005 }
24006 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
24007
24008 test_816() {
24009         [ "$SHARED_KEY" = true ] &&
24010                 skip "OSC connections never go IDLE with Shared-Keys enabled"
24011
24012         $LFS setstripe -c 1 -i 0 $DIR/$tfile
24013         # ensure ost1 is connected
24014         stat $DIR/$tfile >/dev/null || error "can't stat"
24015         wait_osc_import_state client ost1 FULL
24016         # no locks, no reqs to let the connection idle
24017         cancel_lru_locks osc
24018         lru_resize_disable osc
24019         local before
24020         local now
24021         before=$($LCTL get_param -n \
24022                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
24023
24024         wait_osc_import_state client ost1 IDLE
24025         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
24026         now=$($LCTL get_param -n \
24027               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
24028         [ $before == $now ] || error "lru_size changed $before != $now"
24029 }
24030 run_test 816 "do not reset lru_resize on idle reconnect"
24031
24032 cleanup_817() {
24033         umount $tmpdir
24034         exportfs -u localhost:$DIR/nfsexp
24035         rm -rf $DIR/nfsexp
24036 }
24037
24038 test_817() {
24039         systemctl restart nfs-server.service || skip "failed to restart nfsd"
24040
24041         mkdir -p $DIR/nfsexp
24042         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
24043                 error "failed to export nfs"
24044
24045         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
24046         stack_trap cleanup_817 EXIT
24047
24048         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
24049                 error "failed to mount nfs to $tmpdir"
24050
24051         cp /bin/true $tmpdir
24052         $DIR/nfsexp/true || error "failed to execute 'true' command"
24053 }
24054 run_test 817 "nfsd won't cache write lock for exec file"
24055
24056 test_818() {
24057         mkdir $DIR/$tdir
24058         $LFS setstripe -c1 -i0 $DIR/$tfile
24059         $LFS setstripe -c1 -i1 $DIR/$tfile
24060         stop $SINGLEMDS
24061         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
24062         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
24063         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
24064                 error "start $SINGLEMDS failed"
24065         rm -rf $DIR/$tdir
24066 }
24067 run_test 818 "unlink with failed llog"
24068
24069 test_819a() {
24070         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
24071         cancel_lru_locks osc
24072         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
24073         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
24074         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
24075         rm -f $TDIR/$tfile
24076 }
24077 run_test 819a "too big niobuf in read"
24078
24079 test_819b() {
24080         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
24081         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
24082         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
24083         cancel_lru_locks osc
24084         sleep 1
24085         rm -f $TDIR/$tfile
24086 }
24087 run_test 819b "too big niobuf in write"
24088
24089
24090 function test_820_start_ost() {
24091         sleep 5
24092
24093         for num in $(seq $OSTCOUNT); do
24094                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
24095         done
24096 }
24097
24098 test_820() {
24099         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
24100
24101         mkdir $DIR/$tdir
24102         umount_client $MOUNT || error "umount failed"
24103         for num in $(seq $OSTCOUNT); do
24104                 stop ost$num
24105         done
24106
24107         # mount client with no active OSTs
24108         # so that the client can't initialize max LOV EA size
24109         # from OSC notifications
24110         mount_client $MOUNT || error "mount failed"
24111         # delay OST starting to keep this 0 max EA size for a while
24112         test_820_start_ost &
24113
24114         # create a directory on MDS2
24115         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
24116                 error "Failed to create directory"
24117         # open intent should update default EA size
24118         # see mdc_update_max_ea_from_body()
24119         # notice this is the very first RPC to MDS2
24120         cp /etc/services $DIR/$tdir/mds2 ||
24121                 error "Failed to copy files to mds$n"
24122 }
24123 run_test 820 "update max EA from open intent"
24124
24125 #
24126 # tests that do cleanup/setup should be run at the end
24127 #
24128
24129 test_900() {
24130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24131         local ls
24132
24133         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
24134         $LCTL set_param fail_loc=0x903
24135
24136         cancel_lru_locks MGC
24137
24138         FAIL_ON_ERROR=true cleanup
24139         FAIL_ON_ERROR=true setup
24140 }
24141 run_test 900 "umount should not race with any mgc requeue thread"
24142
24143 # LUS-6253/LU-11185
24144 test_901() {
24145         local oldc
24146         local newc
24147         local olds
24148         local news
24149         [ $PARALLEL == "yes" ] && skip "skip parallel run"
24150
24151         # some get_param have a bug to handle dot in param name
24152         cancel_lru_locks MGC
24153         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
24154         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
24155         umount_client $MOUNT || error "umount failed"
24156         mount_client $MOUNT || error "mount failed"
24157         cancel_lru_locks MGC
24158         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
24159         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
24160
24161         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
24162         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
24163
24164         return 0
24165 }
24166 run_test 901 "don't leak a mgc lock on client umount"
24167
24168 # LU-13377
24169 test_902() {
24170         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
24171                 skip "client does not have LU-13377 fix"
24172         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
24173         $LCTL set_param fail_loc=0x1415
24174         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
24175         cancel_lru_locks osc
24176         rm -f $DIR/$tfile
24177 }
24178 run_test 902 "test short write doesn't hang lustre"
24179
24180 complete $SECONDS
24181 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
24182 check_and_cleanup_lustre
24183 if [ "$I_MOUNTED" != "yes" ]; then
24184         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
24185 fi
24186 exit_status