Whamcloud - gitweb
6f91998c8f0dd7390d9d778657335b49deeaf9d5
[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
1785         local list=$(comma_list $(osts_nodes))
1786         [ "$OSTIDX" ] && list=$(facet_host ost$((OSTIDX + 1)))
1787
1788         do_nodes $list lctl set_param fail_loc=0
1789         sync    # initiate all OST_DESTROYs from MDS to OST
1790         sleep_maxage
1791 }
1792
1793 exhaust_precreations() {
1794         local OSTIDX=$1
1795         local FAILLOC=$2
1796         local FAILIDX=${3:-$OSTIDX}
1797         local ofacet=ost$((OSTIDX + 1))
1798
1799         test_mkdir -p -c1 $DIR/$tdir
1800         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
1801         local mfacet=mds$((mdtidx + 1))
1802         echo OSTIDX=$OSTIDX MDTIDX=$mdtidx
1803
1804         local OST=$(ostname_from_index $OSTIDX)
1805
1806         # on the mdt's osc
1807         local mdtosc_proc1=$(get_mdtosc_proc_path $mfacet $OST)
1808         local last_id=$(do_facet $mfacet lctl get_param -n \
1809                         osp.$mdtosc_proc1.prealloc_last_id)
1810         local next_id=$(do_facet $mfacet lctl get_param -n \
1811                         osp.$mdtosc_proc1.prealloc_next_id)
1812
1813         local mdtosc_proc2=$(get_mdtosc_proc_path $mfacet)
1814         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1815
1816         test_mkdir -p $DIR/$tdir/${OST}
1817         $LFS setstripe -i $OSTIDX -c 1 $DIR/$tdir/${OST}
1818 #define OBD_FAIL_OST_ENOSPC              0x215
1819         do_facet $ofacet lctl set_param fail_val=$FAILIDX fail_loc=0x215
1820         echo "Creating to objid $last_id on ost $OST..."
1821         createmany -o $DIR/$tdir/${OST}/f $next_id $((last_id - next_id + 2))
1822         do_facet $mfacet lctl get_param osp.$mdtosc_proc2.prealloc*
1823         do_facet $ofacet lctl set_param fail_loc=$FAILLOC
1824         sleep_maxage
1825 }
1826
1827 exhaust_all_precreations() {
1828         local i
1829         for (( i=0; i < OSTCOUNT; i++ )) ; do
1830                 exhaust_precreations $i $1 -1
1831         done
1832 }
1833
1834 test_27n() {
1835         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1836         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1837         remote_mds_nodsh && skip "remote MDS with nodsh"
1838         remote_ost_nodsh && skip "remote OST with nodsh"
1839
1840         reset_enospc
1841         rm -f $DIR/$tdir/$tfile
1842         exhaust_precreations 0 0x80000215
1843         $LFS setstripe -c -1 $DIR/$tdir || error "setstripe failed"
1844         touch $DIR/$tdir/$tfile || error "touch failed"
1845         $LFS getstripe $DIR/$tdir/$tfile
1846         reset_enospc
1847 }
1848 run_test 27n "create file with some full OSTs"
1849
1850 test_27o() {
1851         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1852         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1853         remote_mds_nodsh && skip "remote MDS with nodsh"
1854         remote_ost_nodsh && skip "remote OST with nodsh"
1855
1856         reset_enospc
1857         rm -f $DIR/$tdir/$tfile
1858         exhaust_all_precreations 0x215
1859
1860         touch $DIR/$tdir/$tfile && error "able to create $DIR/$tdir/$tfile"
1861
1862         reset_enospc
1863         rm -rf $DIR/$tdir/*
1864 }
1865 run_test 27o "create file with all full OSTs (should error)"
1866
1867 test_27p() {
1868         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1870         remote_mds_nodsh && skip "remote MDS with nodsh"
1871         remote_ost_nodsh && skip "remote OST with nodsh"
1872
1873         reset_enospc
1874         rm -f $DIR/$tdir/$tfile
1875         test_mkdir $DIR/$tdir
1876
1877         $MCREATE $DIR/$tdir/$tfile || error "mcreate failed"
1878         $TRUNCATE $DIR/$tdir/$tfile 80000000 || error "truncate failed"
1879         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1880
1881         exhaust_precreations 0 0x80000215
1882         echo foo >> $DIR/$tdir/$tfile || error "append failed"
1883         $CHECKSTAT -s 80000004 $DIR/$tdir/$tfile || error "checkstat failed"
1884         $LFS getstripe $DIR/$tdir/$tfile
1885
1886         reset_enospc
1887 }
1888 run_test 27p "append to a truncated file with some full OSTs"
1889
1890 test_27q() {
1891         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1893         remote_mds_nodsh && skip "remote MDS with nodsh"
1894         remote_ost_nodsh && skip "remote OST with nodsh"
1895
1896         reset_enospc
1897         rm -f $DIR/$tdir/$tfile
1898
1899         test_mkdir $DIR/$tdir
1900         $MCREATE $DIR/$tdir/$tfile || error "mcreate $DIR/$tdir/$tfile failed"
1901         $TRUNCATE $DIR/$tdir/$tfile 80000000 ||
1902                 error "truncate $DIR/$tdir/$tfile failed"
1903         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat failed"
1904
1905         exhaust_all_precreations 0x215
1906
1907         echo foo >> $DIR/$tdir/$tfile && error "append succeeded"
1908         $CHECKSTAT -s 80000000 $DIR/$tdir/$tfile || error "checkstat 2 failed"
1909
1910         reset_enospc
1911 }
1912 run_test 27q "append to truncated file with all OSTs full (should error)"
1913
1914 test_27r() {
1915         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1916         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1917         remote_mds_nodsh && skip "remote MDS with nodsh"
1918         remote_ost_nodsh && skip "remote OST with nodsh"
1919
1920         reset_enospc
1921         rm -f $DIR/$tdir/$tfile
1922         exhaust_precreations 0 0x80000215
1923
1924         $LFS setstripe -i 0 -c 2 $DIR/$tdir/$tfile || error "setstripe failed"
1925
1926         reset_enospc
1927 }
1928 run_test 27r "stripe file with some full OSTs (shouldn't LBUG) ="
1929
1930 test_27s() { # bug 10725
1931         test_mkdir $DIR/$tdir
1932         local stripe_size=$((4096 * 1024 * 1024))       # 2^32
1933         local stripe_count=0
1934         [ $OSTCOUNT -eq 1 ] || stripe_count=2
1935         $LFS setstripe -S $stripe_size -c $stripe_count $DIR/$tdir &&
1936                 error "stripe width >= 2^32 succeeded" || true
1937
1938 }
1939 run_test 27s "lsm_xfersize overflow (should error) (bug 10725)"
1940
1941 test_27t() { # bug 10864
1942         WDIR=$(pwd)
1943         WLFS=$(which lfs)
1944         cd $DIR
1945         touch $tfile
1946         $WLFS getstripe $tfile
1947         cd $WDIR
1948 }
1949 run_test 27t "check that utils parse path correctly"
1950
1951 test_27u() { # bug 4900
1952         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1953         remote_mds_nodsh && skip "remote MDS with nodsh"
1954
1955         local index
1956         local list=$(comma_list $(mdts_nodes))
1957
1958 #define OBD_FAIL_MDS_OSC_PRECREATE      0x139
1959         do_nodes $list $LCTL set_param fail_loc=0x139
1960         test_mkdir -p $DIR/$tdir
1961         trap simple_cleanup_common EXIT
1962         createmany -o $DIR/$tdir/t- 1000
1963         do_nodes $list $LCTL set_param fail_loc=0
1964
1965         TLOG=$TMP/$tfile.getstripe
1966         $LFS getstripe $DIR/$tdir > $TLOG
1967         OBJS=$(awk -vobj=0 '($1 == 0) { obj += 1 } END { print obj; }' $TLOG)
1968         unlinkmany $DIR/$tdir/t- 1000
1969         trap 0
1970         [[ $OBJS -gt 0 ]] &&
1971                 error "$OBJS objects created on OST-0. See $TLOG" ||
1972                 rm -f $TLOG
1973 }
1974 run_test 27u "skip object creation on OSC w/o objects"
1975
1976 test_27v() { # bug 4900
1977         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
1978         [ $PARALLEL == "yes" ] && skip "skip parallel run"
1979         remote_mds_nodsh && skip "remote MDS with nodsh"
1980         remote_ost_nodsh && skip "remote OST with nodsh"
1981
1982         exhaust_all_precreations 0x215
1983         reset_enospc
1984
1985         $LFS setstripe -c 1 $DIR/$tdir         # 1 stripe / file
1986
1987         touch $DIR/$tdir/$tfile
1988         #define OBD_FAIL_TGT_DELAY_PRECREATE     0x705
1989         # all except ost1
1990         for (( i=1; i < OSTCOUNT; i++ )); do
1991                 do_facet ost$i lctl set_param fail_loc=0x705
1992         done
1993         local START=`date +%s`
1994         createmany -o $DIR/$tdir/$tfile 32
1995
1996         local FINISH=`date +%s`
1997         local TIMEOUT=`lctl get_param -n timeout`
1998         local PROCESS=$((FINISH - START))
1999         [ $PROCESS -ge $((TIMEOUT / 2)) ] && \
2000                error "$FINISH - $START >= $TIMEOUT / 2"
2001         sleep $((TIMEOUT / 2 - PROCESS))
2002         reset_enospc
2003 }
2004 run_test 27v "skip object creation on slow OST"
2005
2006 test_27w() { # bug 10997
2007         test_mkdir $DIR/$tdir
2008         $LFS setstripe -S 65536 $DIR/$tdir/f0 || error "setstripe failed"
2009         [ $($LFS getstripe -S $DIR/$tdir/f0) -ne 65536 ] &&
2010                 error "stripe size $size != 65536" || true
2011         [ $($LFS getstripe -d $DIR/$tdir | grep -c "stripe_count") -eq 0 ] &&
2012                 error "$LFS getstripe -d $DIR/$tdir no 'stripe_count'" || true
2013 }
2014 run_test 27w "check $LFS setstripe -S and getstrip -d options"
2015
2016 test_27wa() {
2017         [[ $OSTCOUNT -lt 2 ]] &&
2018                 skip_env "skipping multiple stripe count/offset test"
2019
2020         test_mkdir $DIR/$tdir
2021         for i in $(seq 1 $OSTCOUNT); do
2022                 offset=$((i - 1))
2023                 $LFS setstripe -c $i -i $offset $DIR/$tdir/f$i ||
2024                         error "setstripe -c $i -i $offset failed"
2025                 count=$($LFS getstripe -c $DIR/$tdir/f$i)
2026                 index=$($LFS getstripe -i $DIR/$tdir/f$i)
2027                 [ $count -ne $i ] && error "stripe count $count != $i" || true
2028                 [ $index -ne $offset ] &&
2029                         error "stripe offset $index != $offset" || true
2030         done
2031 }
2032 run_test 27wa "check $LFS setstripe -c -i options"
2033
2034 test_27x() {
2035         remote_ost_nodsh && skip "remote OST with nodsh"
2036         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2037         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2038
2039         OFFSET=$(($OSTCOUNT - 1))
2040         OSTIDX=0
2041         local OST=$(ostname_from_index $OSTIDX)
2042
2043         test_mkdir $DIR/$tdir
2044         $LFS setstripe -c 1 $DIR/$tdir  # 1 stripe per file
2045         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 1
2046         sleep_maxage
2047         createmany -o $DIR/$tdir/$tfile $OSTCOUNT
2048         for i in $(seq 0 $OFFSET); do
2049                 [ $($LFS getstripe $DIR/$tdir/$tfile$i | grep -A 10 obdidx |
2050                         awk '{print $1}' | grep -w "$OSTIDX") ] &&
2051                 error "OST0 was degraded but new created file still use it"
2052         done
2053         do_facet ost$((OSTIDX + 1)) lctl set_param -n obdfilter.$OST.degraded 0
2054 }
2055 run_test 27x "create files while OST0 is degraded"
2056
2057 test_27y() {
2058         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2059         remote_mds_nodsh && skip "remote MDS with nodsh"
2060         remote_ost_nodsh && skip "remote OST with nodsh"
2061         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2062
2063         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS $FSNAME-OST0000)
2064         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
2065                 osp.$mdtosc.prealloc_last_id)
2066         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
2067                 osp.$mdtosc.prealloc_next_id)
2068         local fcount=$((last_id - next_id))
2069         [[ $fcount -eq 0 ]] && skip "not enough space on OST0"
2070         [[ $fcount -gt $OSTCOUNT ]] && fcount=$OSTCOUNT
2071
2072         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
2073                          awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
2074         local OST_DEACTIVE_IDX=-1
2075         local OSC
2076         local OSTIDX
2077         local OST
2078
2079         for OSC in $MDS_OSCS; do
2080                 OST=$(osc_to_ost $OSC)
2081                 OSTIDX=$(index_from_ostuuid $OST)
2082                 if [ $OST_DEACTIVE_IDX == -1 ]; then
2083                         OST_DEACTIVE_IDX=$OSTIDX
2084                 fi
2085                 if [ $OSTIDX != $OST_DEACTIVE_IDX ]; then
2086                         echo $OSC "is Deactivated:"
2087                         do_facet $SINGLEMDS lctl --device  %$OSC deactivate
2088                 fi
2089         done
2090
2091         OSTIDX=$(index_from_ostuuid $OST)
2092         test_mkdir $DIR/$tdir
2093         $LFS setstripe -c 1 $DIR/$tdir      # 1 stripe / file
2094
2095         for OSC in $MDS_OSCS; do
2096                 OST=$(osc_to_ost $OSC)
2097                 OSTIDX=$(index_from_ostuuid $OST)
2098                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2099                         echo $OST "is degraded:"
2100                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2101                                                 obdfilter.$OST.degraded=1
2102                 fi
2103         done
2104
2105         sleep_maxage
2106         createmany -o $DIR/$tdir/$tfile $fcount
2107
2108         for OSC in $MDS_OSCS; do
2109                 OST=$(osc_to_ost $OSC)
2110                 OSTIDX=$(index_from_ostuuid $OST)
2111                 if [ $OSTIDX == $OST_DEACTIVE_IDX ]; then
2112                         echo $OST "is recovered from degraded:"
2113                         do_facet ost$((OSTIDX+1)) lctl set_param -n \
2114                                                 obdfilter.$OST.degraded=0
2115                 else
2116                         do_facet $SINGLEMDS lctl --device %$OSC activate
2117                 fi
2118         done
2119
2120         # all osp devices get activated, hence -1 stripe count restored
2121         local stripe_count=0
2122
2123         # sleep 2*lod_qos_maxage seconds waiting for lod qos to notice osp
2124         # devices get activated.
2125         sleep_maxage
2126         $LFS setstripe -c -1 $DIR/$tfile
2127         stripe_count=$($LFS getstripe -c $DIR/$tfile)
2128         rm -f $DIR/$tfile
2129         [ $stripe_count -ne $OSTCOUNT ] &&
2130                 error "Of $OSTCOUNT OSTs, only $stripe_count is available"
2131         return 0
2132 }
2133 run_test 27y "create files while OST0 is degraded and the rest inactive"
2134
2135 check_seq_oid()
2136 {
2137         log "check file $1"
2138
2139         lmm_count=$($LFS getstripe -c $1)
2140         lmm_seq=$($LFS getstripe -v $1 | awk '/lmm_seq/ { print $2 }')
2141         lmm_oid=$($LFS getstripe -v $1 | awk '/lmm_object_id/ { print $2 }')
2142
2143         local old_ifs="$IFS"
2144         IFS=$'[:]'
2145         fid=($($LFS path2fid $1))
2146         IFS="$old_ifs"
2147
2148         log "FID seq ${fid[1]}, oid ${fid[2]} ver ${fid[3]}"
2149         log "LOV seq $lmm_seq, oid $lmm_oid, count: $lmm_count"
2150
2151         # compare lmm_seq and lu_fid->f_seq
2152         [ $lmm_seq = ${fid[1]} ] || { error "SEQ mismatch"; return 1; }
2153         # compare lmm_object_id and lu_fid->oid
2154         [ $lmm_oid = ${fid[2]} ] || { error "OID mismatch"; return 2; }
2155
2156         # check the trusted.fid attribute of the OST objects of the file
2157         local have_obdidx=false
2158         local stripe_nr=0
2159         $LFS getstripe $1 | while read obdidx oid hex seq; do
2160                 # skip lines up to and including "obdidx"
2161                 [ -z "$obdidx" ] && break
2162                 [ "$obdidx" = "obdidx" ] && have_obdidx=true && continue
2163                 $have_obdidx || continue
2164
2165                 local ost=$((obdidx + 1))
2166                 local dev=$(ostdevname $ost)
2167                 local oid_hex
2168
2169                 log "want: stripe:$stripe_nr ost:$obdidx oid:$oid/$hex seq:$seq"
2170
2171                 seq=$(echo $seq | sed -e "s/^0x//g")
2172                 if [ $seq == 0 ] || [ $(facet_fstype ost$ost) == zfs ]; then
2173                         oid_hex=$(echo $oid)
2174                 else
2175                         oid_hex=$(echo $hex | sed -e "s/^0x//g")
2176                 fi
2177                 local obj_file="O/$seq/d$((oid %32))/$oid_hex"
2178
2179                 local ff=""
2180                 #
2181                 # Don't unmount/remount the OSTs if we don't need to do that.
2182                 # LU-2577 changes filter_fid to be smaller, so debugfs needs
2183                 # update too, until that use mount/ll_decode_filter_fid/mount.
2184                 # Re-enable when debugfs will understand new filter_fid.
2185                 #
2186                 if [ $(facet_fstype ost$ost) == ldiskfs ]; then
2187                         ff=$(do_facet ost$ost "$DEBUGFS -c -R 'stat $obj_file' \
2188                                 $dev 2>/dev/null" | grep "parent=")
2189                 fi
2190                 if [ -z "$ff" ]; then
2191                         stop ost$ost
2192                         mount_fstype ost$ost
2193                         ff=$(do_facet ost$ost $LL_DECODE_FILTER_FID \
2194                                 $(facet_mntpt ost$ost)/$obj_file)
2195                         unmount_fstype ost$ost
2196                         start ost$ost $dev $OST_MOUNT_OPTS
2197                         clients_up
2198                 fi
2199
2200                 [ -z "$ff" ] && error "$obj_file: no filter_fid info"
2201
2202                 echo "$ff" | sed -e 's#.*objid=#got: objid=#'
2203
2204                 # /mnt/O/0/d23/23: objid=23 seq=0 parent=[0x200000400:0x1e:0x1]
2205                 # fid: objid=23 seq=0 parent=[0x200000400:0x1e:0x0] stripe=1
2206                 #
2207                 # fid: parent=[0x200000400:0x1e:0x0] stripe=1 stripe_count=2 \
2208                 #       stripe_size=1048576 component_id=1 component_start=0 \
2209                 #       component_end=33554432
2210                 local ff_parent=$(sed -e 's/.*parent=.//' <<<$ff)
2211                 local ff_pseq=$(cut -d: -f1 <<<$ff_parent)
2212                 local ff_poid=$(cut -d: -f2 <<<$ff_parent)
2213                 local ff_pstripe
2214                 if grep -q 'stripe=' <<<$ff; then
2215                         ff_pstripe=$(sed -e 's/.*stripe=//' -e 's/ .*//' <<<$ff)
2216                 else
2217                         # $LL_DECODE_FILTER_FID does not print "stripe="; look
2218                         # into f_ver in this case.  See comment on ff_parent.
2219                         ff_pstripe=$(cut -d: -f3 <<<$ff_parent | sed -e 's/]//')
2220                 fi
2221
2222                 # compare lmm_seq and filter_fid->ff_parent.f_seq
2223                 [ $ff_pseq = $lmm_seq ] ||
2224                         error "FF parent SEQ $ff_pseq != $lmm_seq"
2225                 # compare lmm_object_id and filter_fid->ff_parent.f_oid
2226                 [ $ff_poid = $lmm_oid ] ||
2227                         error "FF parent OID $ff_poid != $lmm_oid"
2228                 (($ff_pstripe == $stripe_nr)) ||
2229                         error "FF stripe $ff_pstripe != $stripe_nr"
2230
2231                 stripe_nr=$((stripe_nr + 1))
2232                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2233                         continue
2234                 if grep -q 'stripe_count=' <<<$ff; then
2235                         local ff_scnt=$(sed -e 's/.*stripe_count=//' \
2236                                             -e 's/ .*//' <<<$ff)
2237                         [ $lmm_count = $ff_scnt ] ||
2238                                 error "FF stripe count $lmm_count != $ff_scnt"
2239                 fi
2240         done
2241 }
2242
2243 test_27z() {
2244         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2245         remote_ost_nodsh && skip "remote OST with nodsh"
2246
2247         test_mkdir $DIR/$tdir
2248         $LFS setstripe -c 1 -i 0 -S 64k $DIR/$tdir/$tfile-1 ||
2249                 { error "setstripe -c -1 failed"; return 1; }
2250         # We need to send a write to every object to get parent FID info set.
2251         # This _should_ also work for setattr, but does not currently.
2252         # touch $DIR/$tdir/$tfile-1 ||
2253         dd if=/dev/zero of=$DIR/$tdir/$tfile-1 bs=1M count=1 ||
2254                 { error "dd $tfile-1 failed"; return 2; }
2255         $LFS setstripe -c -1 -i $((OSTCOUNT - 1)) -S 1M $DIR/$tdir/$tfile-2 ||
2256                 { error "setstripe -c -1 failed"; return 3; }
2257         dd if=/dev/zero of=$DIR/$tdir/$tfile-2 bs=1M count=$OSTCOUNT ||
2258                 { error "dd $tfile-2 failed"; return 4; }
2259
2260         # make sure write RPCs have been sent to OSTs
2261         sync; sleep 5; sync
2262
2263         check_seq_oid $DIR/$tdir/$tfile-1 || return 5
2264         check_seq_oid $DIR/$tdir/$tfile-2 || return 6
2265 }
2266 run_test 27z "check SEQ/OID on the MDT and OST filesystems"
2267
2268 test_27A() { # b=19102
2269         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2270
2271         save_layout_restore_at_exit $MOUNT
2272         $LFS setstripe -c 0 -i -1 -S 0 $MOUNT
2273         wait_update $HOSTNAME "$LFS getstripe -c $MOUNT | sed 's/  *//g'" "1" 20 ||
2274                 error "stripe count $($LFS getstripe -c $MOUNT) != 1"
2275         local default_size=$($LFS getstripe -S $MOUNT)
2276         local default_offset=$($LFS getstripe -i $MOUNT)
2277         local dsize=$(do_facet $SINGLEMDS \
2278                 "$LCTL get_param -n lod.$(facet_svc $SINGLEMDS)*.stripesize")
2279         [ $default_size -eq $dsize ] ||
2280                 error "stripe size $default_size != $dsize"
2281         [ $default_offset -eq -1 ] ||
2282                 error "stripe offset $default_offset != -1"
2283 }
2284 run_test 27A "check filesystem-wide default LOV EA values"
2285
2286 test_27B() { # LU-2523
2287         test_mkdir $DIR/$tdir
2288         rm -f $DIR/$tdir/f0 $DIR/$tdir/f1
2289         touch $DIR/$tdir/f0
2290         # open f1 with O_LOV_DELAY_CREATE
2291         # rename f0 onto f1
2292         # call setstripe ioctl on open file descriptor for f1
2293         # close
2294         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:nB1c \
2295                 $DIR/$tdir/f0
2296
2297         rm -f $DIR/$tdir/f1
2298         # open f1 with O_LOV_DELAY_CREATE
2299         # unlink f1
2300         # call setstripe ioctl on open file descriptor for f1
2301         # close
2302         multiop $DIR/$tdir/f1 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:uB1c
2303
2304         # Allow multiop to fail in imitation of NFS's busted semantics.
2305         true
2306 }
2307 run_test 27B "call setstripe on open unlinked file/rename victim"
2308
2309 # 27C family tests full striping and overstriping
2310 test_27Ca() { #LU-2871
2311         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2312
2313         declare -a ost_idx
2314         local index
2315         local found
2316         local i
2317         local j
2318
2319         test_mkdir $DIR/$tdir
2320         cd $DIR/$tdir
2321         for i in $(seq 0 $((OSTCOUNT - 1))); do
2322                 # set stripe across all OSTs starting from OST$i
2323                 $LFS setstripe -i $i -c -1 $tfile$i
2324                 # get striping information
2325                 ost_idx=($($LFS getstripe $tfile$i |
2326                          tail -n $((OSTCOUNT + 1)) | awk '{print $1}'))
2327                 echo ${ost_idx[@]}
2328
2329                 # check the layout
2330                 [ ${#ost_idx[@]} -eq $OSTCOUNT ] ||
2331                         error "${#ost_idx[@]} != $OSTCOUNT"
2332
2333                 for index in $(seq 0 $((OSTCOUNT - 1))); do
2334                         found=0
2335                         for j in $(echo ${ost_idx[@]}); do
2336                                 if [ $index -eq $j ]; then
2337                                         found=1
2338                                         break
2339                                 fi
2340                         done
2341                         [ $found = 1 ] ||
2342                                 error "Can not find $index in ${ost_idx[@]}"
2343                 done
2344         done
2345 }
2346 run_test 27Ca "check full striping across all OSTs"
2347
2348 test_27Cb() {
2349         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2350                 skip "server does not support overstriping"
2351         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2352                 skip_env "too many osts, skipping"
2353
2354         test_mkdir -p $DIR/$tdir
2355         local setcount=$(($OSTCOUNT * 2))
2356         [ $setcount -ge 160 ] || large_xattr_enabled ||
2357                 skip_env "ea_inode feature disabled"
2358
2359         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2360                 error "setstripe failed"
2361
2362         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2363         [ $count -eq $setcount ] ||
2364                 error "stripe count $count, should be $setcount"
2365
2366         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2367                 error "overstriped should be set in pattern"
2368
2369         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2370                 error "dd failed"
2371 }
2372 run_test 27Cb "more stripes than OSTs with -C"
2373
2374 test_27Cc() {
2375         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2376                 skip "server does not support overstriping"
2377         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2378
2379         test_mkdir -p $DIR/$tdir
2380         local setcount=$(($OSTCOUNT - 1))
2381
2382         [ $setcount -ge 160 ] || large_xattr_enabled ||
2383                 skip_env "ea_inode feature disabled"
2384
2385         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2386                 error "setstripe failed"
2387
2388         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2389         [ $count -eq $setcount ] ||
2390                 error "stripe count $count, should be $setcount"
2391
2392         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" &&
2393                 error "overstriped should not be set in pattern"
2394
2395         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2396                 error "dd failed"
2397 }
2398 run_test 27Cc "fewer stripes than OSTs does not set overstriping"
2399
2400 test_27Cd() {
2401         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2402                 skip "server does not support overstriping"
2403         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2404         large_xattr_enabled || skip_env "ea_inode feature disabled"
2405
2406         test_mkdir -p $DIR/$tdir
2407         local setcount=$LOV_MAX_STRIPE_COUNT
2408
2409         $LFS setstripe -C $setcount $DIR/$tdir/$tfile ||
2410                 error "setstripe failed"
2411
2412         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2413         [ $count -eq $setcount ] ||
2414                 error "stripe count $count, should be $setcount"
2415
2416         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2417                 error "overstriped should be set in pattern"
2418
2419         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2420                 error "dd failed"
2421
2422         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2423 }
2424 run_test 27Cd "test maximum stripe count"
2425
2426 test_27Ce() {
2427         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2428                 skip "server does not support overstriping"
2429         test_mkdir -p $DIR/$tdir
2430
2431         pool_add $TESTNAME || error "Pool creation failed"
2432         pool_add_targets $TESTNAME 0 || error "pool_add_targets failed"
2433
2434         local setcount=8
2435
2436         $LFS setstripe  -C $setcount -p "$TESTNAME" $DIR/$tdir/$tfile ||
2437                 error "setstripe failed"
2438
2439         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2440         [ $count -eq $setcount ] ||
2441                 error "stripe count $count, should be $setcount"
2442
2443         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2444                 error "overstriped should be set in pattern"
2445
2446         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2447                 error "dd failed"
2448
2449         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2450 }
2451 run_test 27Ce "test pool with overstriping"
2452
2453 test_27Cf() {
2454         [[ $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ]] ||
2455                 skip "server does not support overstriping"
2456         [[ $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2457                 skip_env "too many osts, skipping"
2458
2459         test_mkdir -p $DIR/$tdir
2460
2461         local setcount=$(($OSTCOUNT * 2))
2462         [ $setcount -ge 160 ] || large_xattr_enabled ||
2463                 skip_env "ea_inode feature disabled"
2464
2465         $LFS setstripe  -C $setcount $DIR/$tdir/ ||
2466                 error "setstripe failed"
2467
2468         echo 1 > $DIR/$tdir/$tfile
2469
2470         local count=$($LFS getstripe -c $DIR/$tdir/$tfile)
2471         [ $count -eq $setcount ] ||
2472                 error "stripe count $count, should be $setcount"
2473
2474         $LFS getstripe $DIR/$tdir/$tfile 2>&1 | grep "overstriped" ||
2475                 error "overstriped should be set in pattern"
2476
2477         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=4 conv=notrunc ||
2478                 error "dd failed"
2479
2480         rm -f $DIR/$tdir/$tfile || error "Delete $tfile failed"
2481 }
2482 run_test 27Cf "test default inheritance with overstriping"
2483
2484 test_27D() {
2485         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2486         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2487         remote_mds_nodsh && skip "remote MDS with nodsh"
2488
2489         local POOL=${POOL:-testpool}
2490         local first_ost=0
2491         local last_ost=$(($OSTCOUNT - 1))
2492         local ost_step=1
2493         local ost_list=$(seq $first_ost $ost_step $last_ost)
2494         local ost_range="$first_ost $last_ost $ost_step"
2495
2496         test_mkdir $DIR/$tdir
2497         pool_add $POOL || error "pool_add failed"
2498         pool_add_targets $POOL $ost_range || error "pool_add_targets failed"
2499
2500         local skip27D
2501         [ $MDS1_VERSION -lt $(version_code 2.8.55) ] &&
2502                 skip27D+="-s 29"
2503         [ $MDS1_VERSION -lt $(version_code 2.9.55) ] ||
2504                 [ $CLIENT_VERSION -lt $(version_code 2.9.55) ] &&
2505                         skip27D+=" -s 30,31"
2506         [[ ! $($LCTL get_param mdc.*.import) =~ connect_flags.*overstriping ||
2507           $OSTCOUNT -ge $(($LOV_MAX_STRIPE_COUNT / 2)) ]] &&
2508                 skip27D+=" -s 32,33"
2509         [[ $MDS_VERSION -lt $(version_code $SEL_VER) ]] &&
2510                 skip27D+=" -s 34"
2511         llapi_layout_test -d$DIR/$tdir -p$POOL -o$OSTCOUNT $skip27D ||
2512                 error "llapi_layout_test failed"
2513
2514         destroy_test_pools || error "destroy test pools failed"
2515 }
2516 run_test 27D "validate llapi_layout API"
2517
2518 # Verify that default_easize is increased from its initial value after
2519 # accessing a widely striped file.
2520 test_27E() {
2521         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
2522         [ $CLIENT_VERSION -lt $(version_code 2.5.57) ] &&
2523                 skip "client does not have LU-3338 fix"
2524
2525         # 72 bytes is the minimum space required to store striping
2526         # information for a file striped across one OST:
2527         # (sizeof(struct lov_user_md_v3) +
2528         #  sizeof(struct lov_user_ost_data_v1))
2529         local min_easize=72
2530         $LCTL set_param -n llite.*.default_easize $min_easize ||
2531                 error "lctl set_param failed"
2532         local easize=$($LCTL get_param -n llite.*.default_easize)
2533
2534         [ $easize -eq $min_easize ] ||
2535                 error "failed to set default_easize"
2536
2537         $LFS setstripe -c $OSTCOUNT $DIR/$tfile ||
2538                 error "setstripe failed"
2539         # In order to ensure stat() call actually talks to MDS we need to
2540         # do something drastic to this file to shake off all lock, e.g.
2541         # rename it (kills lookup lock forcing cache cleaning)
2542         mv $DIR/$tfile $DIR/${tfile}-1
2543         ls -l $DIR/${tfile}-1
2544         rm $DIR/${tfile}-1
2545
2546         easize=$($LCTL get_param -n llite.*.default_easize)
2547
2548         [ $easize -gt $min_easize ] ||
2549                 error "default_easize not updated"
2550 }
2551 run_test 27E "check that default extended attribute size properly increases"
2552
2553 test_27F() { # LU-5346/LU-7975
2554         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2555         [[ $OSTCOUNT -lt 2 ]] && skip "needs >= 2 OSTs"
2556         [[ $MDS1_VERSION -lt $(version_code 2.8.51) ]] &&
2557                 skip "Need MDS version at least 2.8.51"
2558         remote_ost_nodsh && skip "remote OST with nodsh"
2559
2560         test_mkdir $DIR/$tdir
2561         rm -f $DIR/$tdir/f0
2562         $LFS setstripe -c 2 $DIR/$tdir
2563
2564         # stop all OSTs to reproduce situation for LU-7975 ticket
2565         for num in $(seq $OSTCOUNT); do
2566                 stop ost$num
2567         done
2568
2569         # open/create f0 with O_LOV_DELAY_CREATE
2570         # truncate f0 to a non-0 size
2571         # close
2572         multiop $DIR/$tdir/f0 oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T1050000c
2573
2574         $CHECKSTAT -s 1050000 $DIR/$tdir/f0 || error "checkstat failed"
2575         # open/write it again to force delayed layout creation
2576         cat /etc/hosts > $DIR/$tdir/f0 &
2577         catpid=$!
2578
2579         # restart OSTs
2580         for num in $(seq $OSTCOUNT); do
2581                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS ||
2582                         error "ost$num failed to start"
2583         done
2584
2585         wait $catpid || error "cat failed"
2586
2587         cmp /etc/hosts $DIR/$tdir/f0 || error "cmp failed"
2588         [[ $($LFS getstripe -c $DIR/$tdir/f0) == 2 ]] ||
2589                 error "wrong stripecount"
2590
2591 }
2592 run_test 27F "Client resend delayed layout creation with non-zero size"
2593
2594 test_27G() { #LU-10629
2595         [ $MDS1_VERSION -lt $(version_code 2.11.51) ] &&
2596                 skip "Need MDS version at least 2.11.51"
2597         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
2598         remote_mds_nodsh && skip "remote MDS with nodsh"
2599         local POOL=${POOL:-testpool}
2600         local ostrange="0 0 1"
2601
2602         test_mkdir $DIR/$tdir
2603         pool_add $POOL || error "pool_add failed"
2604         pool_add_targets $POOL $ostrange || error "pool_add_targets failed"
2605         $LFS setstripe -p $POOL $DIR/$tdir
2606
2607         local pool=$($LFS getstripe -p $DIR/$tdir)
2608
2609         [ "$pool" = "$POOL" ] || error "Striping failed got '$pool' not '$POOL'"
2610
2611         $LFS setstripe -d $DIR/$tdir
2612
2613         pool=$($LFS getstripe -p $DIR/$tdir)
2614
2615         rmdir $DIR/$tdir
2616
2617         [ -z "$pool" ] || error "'$pool' is not empty"
2618 }
2619 run_test 27G "Clear OST pool from stripe"
2620
2621 test_27H() {
2622         [[ $MDS1_VERSION -le $(version_code 2.11.54) ]] &&
2623                 skip "Need MDS version newer than 2.11.54"
2624         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
2625         test_mkdir $DIR/$tdir
2626         $LFS setstripe -o 0 -o 2 $DIR/$tdir || error "setstripe failed"
2627         touch $DIR/$tdir/$tfile
2628         $LFS getstripe -c $DIR/$tdir/$tfile
2629         [ $($LFS getstripe -c $DIR/$tdir/$tfile) -eq 2 ] ||
2630                 error "two-stripe file doesn't have two stripes"
2631
2632         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=4k count=4 || error "dd failed"
2633         $LFS getstripe -y $DIR/$tdir/$tfile
2634         (( $($LFS getstripe -y $DIR/$tdir/$tfile |
2635              egrep -c "l_ost_idx: [02]$") == "2" )) ||
2636                 error "expected l_ost_idx: [02]$ not matched"
2637
2638         # make sure ost list has been cleared
2639         local stripesize=$($LFS getstripe -S $DIR/$tdir)
2640         $LFS setstripe -S $((stripesize * 4)) -i 1 \
2641                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
2642         touch $DIR/$tdir/f3
2643         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
2644 }
2645 run_test 27H "Set specific OSTs stripe"
2646
2647 test_27I() {
2648         [ $PARALLEL == "yes" ] && skip "skip parallel run"
2649         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
2650         [[ $MDS1_VERSION -gt $(version_code 2.12.52) ]] ||
2651                 skip "Need MDS version newer than 2.12.52"
2652         local pool=$TESTNAME
2653         local ostrange="1 1 1"
2654
2655         save_layout_restore_at_exit $MOUNT
2656         $LFS setstripe -c 2 -i 0 $MOUNT
2657         pool_add $pool || error "pool_add failed"
2658         pool_add_targets $pool $ostrange || "pool_add_targets failed"
2659         test_mkdir $DIR/$tdir
2660         $LFS setstripe -p $pool $DIR/$tdir
2661         $MULTIOP $DIR/$tdir/$tfile Oc || error "multiop failed"
2662         $LFS getstripe $DIR/$tdir/$tfile
2663 }
2664 run_test 27I "check that root dir striping does not break parent dir one"
2665
2666 test_27J() {
2667         [[ $MDS1_VERSION -le $(version_code 2.12.51) ]] &&
2668                 skip "Need MDS version newer than 2.12.51"
2669
2670         test_mkdir $DIR/$tdir
2671         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2672         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2673
2674         # create foreign file (raw way)
2675         create_foreign_file -f $DIR/$tdir/$tfile -x "${uuid1}@${uuid2}" \
2676                 -t 1 -F 0xda08 || error "create_foreign_file failed"
2677
2678         # verify foreign file (raw way)
2679         parse_foreign_file -f $DIR/$tdir/$tfile |
2680                 grep "lov_foreign_magic: 0x0BD70BD0" ||
2681                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign magic"
2682         parse_foreign_file -f $DIR/$tdir/$tfile | grep "lov_xattr_size: 89" ||
2683                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2684         parse_foreign_file -f $DIR/$tdir/$tfile |
2685                 grep "lov_foreign_size: 73" ||
2686                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign size"
2687         parse_foreign_file -f $DIR/$tdir/$tfile |
2688                 grep "lov_foreign_type: 1" ||
2689                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign type"
2690         parse_foreign_file -f $DIR/$tdir/$tfile |
2691                 grep "lov_foreign_flags: 0x0000DA08" ||
2692                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign flags"
2693         local lov=$(parse_foreign_file -f $DIR/$tdir/$tfile |
2694                 grep "lov_foreign_value: 0x" |
2695                 sed -e 's/lov_foreign_value: 0x//')
2696         local lov2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160)
2697         [[ $lov = ${lov2// /} ]] ||
2698                 error "$DIR/$tdir/$tfile: invalid LOV EA foreign value"
2699
2700         # create foreign file (lfs + API)
2701         $LFS setstripe --foreign=daos --flags 0xda08 \
2702                 -x "${uuid1}@${uuid2}" $DIR/$tdir/${tfile}2 ||
2703                 error "$DIR/$tdir/${tfile}2: create failed"
2704
2705         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2706                 grep "lfm_magic:.*0x0BD70BD0" ||
2707                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign magic"
2708         # lfm_length is LOV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2709         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_length:.*73" ||
2710                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign size"
2711         $LFS getstripe -v $DIR/$tdir/${tfile}2 | grep "lfm_type:.*daos" ||
2712                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign type"
2713         $LFS getstripe -v $DIR/$tdir/${tfile}2 |
2714                 grep "lfm_flags:.*0x0000DA08" ||
2715                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign flags"
2716         $LFS getstripe $DIR/$tdir/${tfile}2 |
2717                 grep "lfm_value:.*${uuid1}@${uuid2}" ||
2718                 error "$DIR/$tdir/${tfile}2: invalid LOV EA foreign value"
2719
2720         # modify striping should fail
2721         $LFS setstripe -c 2 $DIR/$tdir/$tfile &&
2722                 error "$DIR/$tdir/$tfile: setstripe should fail"
2723         $LFS setstripe -c 2 $DIR/$tdir/${tfile}2 &&
2724                 error "$DIR/$tdir/${tfile}2: setstripe should fail"
2725
2726         # R/W should fail
2727         cat $DIR/$tdir/$tfile && error "$DIR/$tdir/$tfile: read should fail"
2728         cat $DIR/$tdir/${tfile}2 &&
2729                 error "$DIR/$tdir/${tfile}2: read should fail"
2730         cat /etc/passwd > $DIR/$tdir/$tfile &&
2731                 error "$DIR/$tdir/$tfile: write should fail"
2732         cat /etc/passwd > $DIR/$tdir/${tfile}2 &&
2733                 error "$DIR/$tdir/${tfile}2: write should fail"
2734
2735         # chmod should work
2736         chmod 222 $DIR/$tdir/$tfile ||
2737                 error "$DIR/$tdir/$tfile: chmod failed"
2738         chmod 222 $DIR/$tdir/${tfile}2 ||
2739                 error "$DIR/$tdir/${tfile}2: chmod failed"
2740
2741         # chown should work
2742         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tfile ||
2743                 error "$DIR/$tdir/$tfile: chown failed"
2744         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tfile}2 ||
2745                 error "$DIR/$tdir/${tfile}2: chown failed"
2746
2747         # rename should work
2748         mv $DIR/$tdir/$tfile $DIR/$tdir/${tfile}.new ||
2749                 error "$DIR/$tdir/$tfile: rename of foreign file has failed"
2750         mv $DIR/$tdir/${tfile}2 $DIR/$tdir/${tfile}2.new ||
2751                 error "$DIR/$tdir/${tfile}2: rename of foreign file has failed"
2752
2753         #remove foreign file
2754         rm $DIR/$tdir/${tfile}.new ||
2755                 error "$DIR/$tdir/${tfile}.new: remove of foreign file has failed"
2756         rm $DIR/$tdir/${tfile}2.new ||
2757                 error "$DIR/$tdir/${tfile}2.new: remove of foreign file has failed"
2758 }
2759 run_test 27J "basic ops on file with foreign LOV"
2760
2761 test_27K() {
2762         [[ $MDS1_VERSION -le $(version_code 2.12.49) ]] &&
2763                 skip "Need MDS version newer than 2.12.49"
2764
2765         test_mkdir $DIR/$tdir
2766         local uuid1=$(cat /proc/sys/kernel/random/uuid)
2767         local uuid2=$(cat /proc/sys/kernel/random/uuid)
2768
2769         # create foreign dir (raw way)
2770         create_foreign_dir -d $DIR/$tdir/$tdir -x "${uuid1}@${uuid2}" -t 1 ||
2771                 error "create_foreign_dir FAILED"
2772
2773         # verify foreign dir (raw way)
2774         parse_foreign_dir -d $DIR/$tdir/$tdir |
2775                 grep "lmv_foreign_magic:.*0xcd50cd0" ||
2776                 error "$DIR/$tdir/$tfile: invalid LMV EA magic"
2777         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_xattr_size:.*89$" ||
2778                 error "$DIR/$tdir/$tdir: invalid LMV EA size"
2779         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_type: 1$" ||
2780                 error "$DIR/$tdir/$tdir: invalid LMV EA type"
2781         parse_foreign_dir -d $DIR/$tdir/$tdir | grep "lmv_foreign_flags: 0$" ||
2782                 error "$DIR/$tdir/$tdir: invalid LMV EA flags"
2783         local lmv=$(parse_foreign_dir -d $DIR/$tdir/$tdir |
2784                 grep "lmv_foreign_value: 0x" |
2785                 sed 's/lmv_foreign_value: 0x//')
2786         local lmv2=$(echo -n "${uuid1}@${uuid2}" | od -A n -t x1 -w160 |
2787                 sed 's/ //g')
2788         [[ $lmv == $lmv2 ]] || error "$DIR/$tdir/$tdir: invalid LMV EA value"
2789
2790         # create foreign dir (lfs + API)
2791         $LFS mkdir --foreign=daos --xattr="${uuid1}@${uuid2}" --flags=0xda05 \
2792                 $DIR/$tdir/${tdir}2 ||
2793                 error "$DIR/$tdir/${tdir}2: create failed"
2794
2795         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2796                 grep "lfm_magic:.*0x0CD50CD0" ||
2797                 error "$DIR/$tdir/${tdir}2: invalid LMV EA magic"
2798         # lfm_length is LMV EA size - sizeof(lfm_magic) - sizeof(lfm_length)
2799         # - sizeof(lfm_type) - sizeof(lfm_flags)
2800         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_length:.*73" ||
2801                 error "$DIR/$tdir/${tdir}2: invalid LMV EA size"
2802         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 | grep "lfm_type:.*daos" ||
2803                 error "$DIR/$tdir/${tdir}2: invalid LMV EA type"
2804         $LFS getdirstripe -v $DIR/$tdir/${tdir}2 |
2805                 grep "lfm_flags:.*0x0000DA05" ||
2806                 error "$DIR/$tdir/${tdir}2: invalid LMV EA flags"
2807         $LFS getdirstripe $DIR/$tdir/${tdir}2 |
2808                 grep "lfm_value.*${uuid1}@${uuid2}" ||
2809                 error "$DIR/$tdir/${tdir}2: invalid LMV EA value"
2810
2811         # file create in dir should fail
2812         touch $DIR/$tdir/$tdir/$tfile && "$DIR/$tdir: file create should fail"
2813         touch $DIR/$tdir/${tdir}2/$tfile &&
2814                 "$DIR/${tdir}2: file create should fail"
2815
2816         # chmod should work
2817         chmod 777 $DIR/$tdir/$tdir ||
2818                 error "$DIR/$tdir: chmod failed"
2819         chmod 777 $DIR/$tdir/${tdir}2 ||
2820                 error "$DIR/${tdir}2: chmod failed"
2821
2822         # chown should work
2823         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/$tdir ||
2824                 error "$DIR/$tdir: chown failed"
2825         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/${tdir}2 ||
2826                 error "$DIR/${tdir}2: chown failed"
2827
2828         # rename should work
2829         mv $DIR/$tdir/$tdir $DIR/$tdir/${tdir}.new ||
2830                 error "$DIR/$tdir/$tdir: rename of foreign dir has failed"
2831         mv $DIR/$tdir/${tdir}2 $DIR/$tdir/${tdir}2.new ||
2832                 error "$DIR/$tdir/${tdir}2: rename of foreign dir has failed"
2833
2834         #remove foreign dir
2835         rmdir $DIR/$tdir/${tdir}.new ||
2836                 error "$DIR/$tdir/${tdir}.new: remove of foreign dir has failed"
2837         rmdir $DIR/$tdir/${tdir}2.new ||
2838                 error "$DIR/$tdir/${tdir}2.new: remove of foreign dir has failed"
2839 }
2840 run_test 27K "basic ops on dir with foreign LMV"
2841
2842 test_27L() {
2843         remote_mds_nodsh && skip "remote MDS with nodsh"
2844
2845         local POOL=${POOL:-$TESTNAME}
2846
2847         pool_add $POOL || error "pool_add failed"
2848
2849         lfs pool_list $MOUNT | grep -Fx "${FSNAME}.${POOL}" ||
2850                  error "pool_list does not contain ${FSNAME}.${POOL}:" \
2851                        "$(lfs pool_list $MOUNT | grep -F "${POOL}")"
2852 }
2853 run_test 27L "lfs pool_list gives correct pool name"
2854
2855 test_27M() {
2856         [[ $(lustre_version_code $SINGLEMDS) -lt $(version_code 2.12.57) ]] &&
2857                 skip "Need MDS version >= than 2.12.57"
2858         remote_mds_nodsh && skip "remote MDS with nodsh"
2859         [[ $OSTCOUNT -lt 2 ]] && skip_env "need > 1 OST"
2860
2861         test_mkdir $DIR/$tdir
2862
2863         # Set default striping on directory
2864         $LFS setstripe -C 4 $DIR/$tdir
2865
2866         echo 1 > $DIR/$tdir/${tfile}.1
2867         local count=$($LFS getstripe -c $DIR/$tdir/${tfile}.1)
2868         local setcount=4
2869         [ $count -eq $setcount ] ||
2870                 error "(1) stripe count $count, should be $setcount"
2871
2872         # Capture existing append_stripe_count setting for restore
2873         local orig_count=$(do_facet mds1 $LCTL get_param -n mdd.$FSNAME-MDT0000.append_stripe_count)
2874         local mdts=$(comma_list $(mdts_nodes))
2875         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=$orig_count" EXIT
2876
2877         local appendcount=$orig_count
2878         echo 1 >> $DIR/$tdir/${tfile}.2_append
2879         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.2_append)
2880         [ $count -eq $appendcount ] ||
2881                 error "(2)stripe count $count, should be $appendcount for append"
2882
2883         # Disable O_APPEND striping, verify it works
2884         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2885
2886         # Should now get the default striping, which is 4
2887         setcount=4
2888         echo 1 >> $DIR/$tdir/${tfile}.3_append
2889         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.3_append)
2890         [ $count -eq $setcount ] ||
2891                 error "(3) stripe count $count, should be $setcount"
2892
2893         # Try changing the stripe count for append files
2894         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=2
2895
2896         # Append striping is now 2 (directory default is still 4)
2897         appendcount=2
2898         echo 1 >> $DIR/$tdir/${tfile}.4_append
2899         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.4_append)
2900         [ $count -eq $appendcount ] ||
2901                 error "(4) stripe count $count, should be $appendcount for append"
2902
2903         # Test append stripe count of -1
2904         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=-1
2905         appendcount=$OSTCOUNT
2906         echo 1 >> $DIR/$tdir/${tfile}.5
2907         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.5)
2908         [ $count -eq $appendcount ] ||
2909                 error "(5) stripe count $count, should be $appendcount for append"
2910
2911         # Set append striping back to default of 1
2912         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=1
2913
2914         # Try a new default striping, PFL + DOM
2915         $LFS setstripe -L mdt -E 1M -E -1 -c 2 $DIR/$tdir
2916
2917         # Create normal DOM file, DOM returns stripe count == 0
2918         setcount=0
2919         touch $DIR/$tdir/${tfile}.6
2920         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.6)
2921         [ $count -eq $setcount ] ||
2922                 error "(6) stripe count $count, should be $setcount"
2923
2924         # Show
2925         appendcount=1
2926         echo 1 >> $DIR/$tdir/${tfile}.7_append
2927         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.7_append)
2928         [ $count -eq $appendcount ] ||
2929                 error "(7) stripe count $count, should be $appendcount for append"
2930
2931         # Clean up DOM layout
2932         $LFS setstripe -d $DIR/$tdir
2933
2934         # Now test that append striping works when layout is from root
2935         $LFS setstripe -c 2 $MOUNT
2936         # Make a special directory for this
2937         mkdir $DIR/${tdir}/${tdir}.2
2938         stack_trap "$LFS setstripe -d $MOUNT" EXIT
2939
2940         # Verify for normal file
2941         setcount=2
2942         echo 1 > $DIR/${tdir}/${tdir}.2/${tfile}.8
2943         count=$($LFS getstripe -c $DIR/$tdir/${tdir}.2/${tfile}.8)
2944         [ $count -eq $setcount ] ||
2945                 error "(8) stripe count $count, should be $setcount"
2946
2947         appendcount=1
2948         echo 1 >> $DIR/${tdir}/${tdir}.2/${tfile}.9_append
2949         count=$($LFS getstripe -c $DIR/${tdir}/${tdir}.2/${tfile}.9_append)
2950         [ $count -eq $appendcount ] ||
2951                 error "(9) stripe count $count, should be $appendcount for append"
2952
2953         # Now test O_APPEND striping with pools
2954         do_nodes $mdts $LCTL set_param mdd.*.append_pool="$TESTNAME"
2955         stack_trap "do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'" EXIT
2956
2957         # Create the pool
2958         pool_add $TESTNAME || error "pool creation failed"
2959         pool_add_targets $TESTNAME 0 1 || error "Pool add targets failed"
2960
2961         echo 1 >> $DIR/$tdir/${tfile}.10_append
2962
2963         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.10_append)
2964         [ "$pool" = "$TESTNAME" ] || error "(10) incorrect pool: $pool"
2965
2966         # Check that count is still correct
2967         appendcount=1
2968         echo 1 >> $DIR/$tdir/${tfile}.11_append
2969         count=$($LFS getstripe -c $DIR/$tdir/${tfile}.11_append)
2970         [ $count -eq $appendcount ] ||
2971                 error "(11) stripe count $count, should be $appendcount for append"
2972
2973         # Disable O_APPEND stripe count, verify pool works separately
2974         do_nodes $mdts $LCTL set_param mdd.*.append_stripe_count=0
2975
2976         echo 1 >> $DIR/$tdir/${tfile}.12_append
2977
2978         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.12_append)
2979         [ "$pool" = "$TESTNAME" ] || error "(12) incorrect pool: $pool"
2980
2981         # Remove pool setting, verify it's not applied
2982         do_nodes $mdts $LCTL set_param mdd.*.append_pool='none'
2983
2984         echo 1 >> $DIR/$tdir/${tfile}.13_append
2985
2986         pool=$($LFS getstripe -p $DIR/$tdir/${tfile}.13_append)
2987         [ "$pool" = "" ] || error "(13) pool found: $pool"
2988 }
2989 run_test 27M "test O_APPEND striping"
2990
2991 test_27N() {
2992         combined_mgs_mds && skip "needs separate MGS/MDT"
2993
2994         pool_add $TESTNAME || error "pool_add failed"
2995         do_facet mgs "$LCTL pool_list $FSNAME" |
2996                 grep -Fx "${FSNAME}.${TESTNAME}" ||
2997                 error "lctl pool_list on MGS failed"
2998 }
2999 run_test 27N "lctl pool_list on separate MGS gives correct pool name"
3000
3001 # createtest also checks that device nodes are created and
3002 # then visible correctly (#2091)
3003 test_28() { # bug 2091
3004         test_mkdir $DIR/d28
3005         $CREATETEST $DIR/d28/ct || error "createtest failed"
3006 }
3007 run_test 28 "create/mknod/mkdir with bad file types ============"
3008
3009 test_29() {
3010         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3011
3012         sync; sleep 1; sync # flush out any dirty pages from previous tests
3013         cancel_lru_locks
3014         test_mkdir $DIR/d29
3015         touch $DIR/d29/foo
3016         log 'first d29'
3017         ls -l $DIR/d29
3018
3019         declare -i LOCKCOUNTORIG=0
3020         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3021                 let LOCKCOUNTORIG=$LOCKCOUNTORIG+$lock_count
3022         done
3023         [ $LOCKCOUNTORIG -eq 0 ] && error "No mdc lock count" && return 1
3024
3025         declare -i LOCKUNUSEDCOUNTORIG=0
3026         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3027                 let LOCKUNUSEDCOUNTORIG=$LOCKUNUSEDCOUNTORIG+$unused_count
3028         done
3029
3030         log 'second d29'
3031         ls -l $DIR/d29
3032         log 'done'
3033
3034         declare -i LOCKCOUNTCURRENT=0
3035         for lock_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_count); do
3036                 let LOCKCOUNTCURRENT=$LOCKCOUNTCURRENT+$lock_count
3037         done
3038
3039         declare -i LOCKUNUSEDCOUNTCURRENT=0
3040         for unused_count in $(lctl get_param -n ldlm.namespaces.*mdc*.lock_unused_count); do
3041                 let LOCKUNUSEDCOUNTCURRENT=$LOCKUNUSEDCOUNTCURRENT+$unused_count
3042         done
3043
3044         if [[ $LOCKCOUNTCURRENT -gt $LOCKCOUNTORIG ]]; then
3045                 $LCTL set_param -n ldlm.dump_namespaces ""
3046                 error "CURRENT: $LOCKCOUNTCURRENT > $LOCKCOUNTORIG"
3047                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3048                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3049                 return 2
3050         fi
3051         if [[ $LOCKUNUSEDCOUNTCURRENT -gt $LOCKUNUSEDCOUNTORIG ]]; then
3052                 error "UNUSED: $LOCKUNUSEDCOUNTCURRENT > $LOCKUNUSEDCOUNTORIG"
3053                 $LCTL dk | sort -k4 -t: > $TMP/test_29.dk
3054                 log "dumped log to $TMP/test_29.dk (bug 5793)"
3055                 return 3
3056         fi
3057 }
3058 run_test 29 "IT_GETATTR regression  ============================"
3059
3060 test_30a() { # was test_30
3061         cp $(which ls) $DIR || cp /bin/ls $DIR
3062         $DIR/ls / || error "Can't execute binary from lustre"
3063         rm $DIR/ls
3064 }
3065 run_test 30a "execute binary from Lustre (execve) =============="
3066
3067 test_30b() {
3068         cp `which ls` $DIR || cp /bin/ls $DIR
3069         chmod go+rx $DIR/ls
3070         $RUNAS $DIR/ls / || error "Can't execute binary from lustre as non-root"
3071         rm $DIR/ls
3072 }
3073 run_test 30b "execute binary from Lustre as non-root ==========="
3074
3075 test_30c() { # b=22376
3076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3077
3078         cp $(which ls) $DIR || cp /bin/ls $DIR
3079         chmod a-rw $DIR/ls
3080         cancel_lru_locks mdc
3081         cancel_lru_locks osc
3082         $RUNAS $DIR/ls / || error "Can't execute binary from lustre"
3083         rm -f $DIR/ls
3084 }
3085 run_test 30c "execute binary from Lustre without read perms ===="
3086
3087 test_30d() {
3088         cp $(which dd) $DIR || error "failed to copy dd to $DIR/dd"
3089
3090         for i in {1..10}; do
3091                 $DIR/dd bs=1M count=128 if=/dev/zero of=$DIR/$tfile &
3092                 local PID=$!
3093                 sleep 1
3094                 $LCTL set_param ldlm.namespaces.*MDT*.lru_size=clear
3095                 wait $PID || error "executing dd from Lustre failed"
3096                 rm -f $DIR/$tfile
3097         done
3098
3099         rm -f $DIR/dd
3100 }
3101 run_test 30d "execute binary from Lustre while clear locks"
3102
3103 test_31a() {
3104         $OPENUNLINK $DIR/f31 $DIR/f31 || error "openunlink failed"
3105         $CHECKSTAT -a $DIR/f31 || error "$DIR/f31 exists"
3106 }
3107 run_test 31a "open-unlink file =================================="
3108
3109 test_31b() {
3110         touch $DIR/f31 || error "touch $DIR/f31 failed"
3111         ln $DIR/f31 $DIR/f31b || error "ln failed"
3112         $MULTIOP $DIR/f31b Ouc || error "multiop failed"
3113         $CHECKSTAT -t file $DIR/f31 || error "$DIR/f31 not file type"
3114 }
3115 run_test 31b "unlink file with multiple links while open ======="
3116
3117 test_31c() {
3118         touch $DIR/f31 || error "touch $DIR/f31 failed"
3119         ln $DIR/f31 $DIR/f31c || error "ln failed"
3120         multiop_bg_pause $DIR/f31 O_uc ||
3121                 error "multiop_bg_pause for $DIR/f31 failed"
3122         MULTIPID=$!
3123         $MULTIOP $DIR/f31c Ouc
3124         kill -USR1 $MULTIPID
3125         wait $MULTIPID
3126 }
3127 run_test 31c "open-unlink file with multiple links ============="
3128
3129 test_31d() {
3130         opendirunlink $DIR/d31d $DIR/d31d || error "opendirunlink failed"
3131         $CHECKSTAT -a $DIR/d31d || error "$DIR/d31d exists"
3132 }
3133 run_test 31d "remove of open directory ========================="
3134
3135 test_31e() { # bug 2904
3136         openfilleddirunlink $DIR/d31e || error "openfilleddirunlink failed"
3137 }
3138 run_test 31e "remove of open non-empty directory ==============="
3139
3140 test_31f() { # bug 4554
3141         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3142
3143         set -vx
3144         test_mkdir $DIR/d31f
3145         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3146         cp /etc/hosts $DIR/d31f
3147         ls -l $DIR/d31f
3148         $LFS getstripe $DIR/d31f/hosts
3149         multiop_bg_pause $DIR/d31f D_c || return 1
3150         MULTIPID=$!
3151
3152         rm -rv $DIR/d31f || error "first of $DIR/d31f"
3153         test_mkdir $DIR/d31f
3154         $LFS setstripe -S 1048576 -c 1 $DIR/d31f
3155         cp /etc/hosts $DIR/d31f
3156         ls -l $DIR/d31f
3157         $LFS getstripe $DIR/d31f/hosts
3158         multiop_bg_pause $DIR/d31f D_c || return 1
3159         MULTIPID2=$!
3160
3161         kill -USR1 $MULTIPID || error "first opendir $MULTIPID not running"
3162         wait $MULTIPID || error "first opendir $MULTIPID failed"
3163
3164         sleep 6
3165
3166         kill -USR1 $MULTIPID2 || error "second opendir $MULTIPID not running"
3167         wait $MULTIPID2 || error "second opendir $MULTIPID2 failed"
3168         set +vx
3169 }
3170 run_test 31f "remove of open directory with open-unlink file ==="
3171
3172 test_31g() {
3173         echo "-- cross directory link --"
3174         test_mkdir -c1 $DIR/${tdir}ga
3175         test_mkdir -c1 $DIR/${tdir}gb
3176         touch $DIR/${tdir}ga/f
3177         ln $DIR/${tdir}ga/f $DIR/${tdir}gb/g
3178         $CHECKSTAT -t file $DIR/${tdir}ga/f || error "source"
3179         [ `stat -c%h $DIR/${tdir}ga/f` == '2' ] || error "source nlink"
3180         $CHECKSTAT -t file $DIR/${tdir}gb/g || error "target"
3181         [ `stat -c%h $DIR/${tdir}gb/g` == '2' ] || error "target nlink"
3182 }
3183 run_test 31g "cross directory link==============="
3184
3185 test_31h() {
3186         echo "-- cross directory link --"
3187         test_mkdir -c1 $DIR/${tdir}
3188         test_mkdir -c1 $DIR/${tdir}/dir
3189         touch $DIR/${tdir}/f
3190         ln $DIR/${tdir}/f $DIR/${tdir}/dir/g
3191         $CHECKSTAT -t file $DIR/${tdir}/f || error "source"
3192         [ `stat -c%h $DIR/${tdir}/f` == '2' ] || error "source nlink"
3193         $CHECKSTAT -t file $DIR/${tdir}/dir/g || error "target"
3194         [ `stat -c%h $DIR/${tdir}/dir/g` == '2' ] || error "target nlink"
3195 }
3196 run_test 31h "cross directory link under child==============="
3197
3198 test_31i() {
3199         echo "-- cross directory link --"
3200         test_mkdir -c1 $DIR/$tdir
3201         test_mkdir -c1 $DIR/$tdir/dir
3202         touch $DIR/$tdir/dir/f
3203         ln $DIR/$tdir/dir/f $DIR/$tdir/g
3204         $CHECKSTAT -t file $DIR/$tdir/dir/f || error "source"
3205         [ `stat -c%h $DIR/$tdir/dir/f` == '2' ] || error "source nlink"
3206         $CHECKSTAT -t file $DIR/$tdir/g || error "target"
3207         [ `stat -c%h $DIR/$tdir/g` == '2' ] || error "target nlink"
3208 }
3209 run_test 31i "cross directory link under parent==============="
3210
3211 test_31j() {
3212         test_mkdir -c1 -p $DIR/$tdir
3213         test_mkdir -c1 -p $DIR/$tdir/dir1
3214         ln $DIR/$tdir/dir1 $DIR/$tdir/dir2 && error "ln for dir"
3215         link $DIR/$tdir/dir1 $DIR/$tdir/dir3 && error "link for dir"
3216         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir4 && error "mlink for dir"
3217         mlink $DIR/$tdir/dir1 $DIR/$tdir/dir1 && error "mlink to the same dir"
3218         return 0
3219 }
3220 run_test 31j "link for directory==============="
3221
3222 test_31k() {
3223         test_mkdir -c1 -p $DIR/$tdir
3224         touch $DIR/$tdir/s
3225         touch $DIR/$tdir/exist
3226         mlink $DIR/$tdir/s $DIR/$tdir/t || error "mlink"
3227         mlink $DIR/$tdir/s $DIR/$tdir/exist && error "mlink to exist file"
3228         mlink $DIR/$tdir/s $DIR/$tdir/s && error "mlink to the same file"
3229         mlink $DIR/$tdir/s $DIR/$tdir && error "mlink to parent dir"
3230         mlink $DIR/$tdir $DIR/$tdir/s && error "mlink parent dir to target"
3231         mlink $DIR/$tdir/not-exist $DIR/$tdir/foo && error "mlink non-existing to new"
3232         mlink $DIR/$tdir/not-exist $DIR/$tdir/s && error "mlink non-existing to exist"
3233         return 0
3234 }
3235 run_test 31k "link to file: the same, non-existing, dir==============="
3236
3237 test_31m() {
3238         mkdir $DIR/d31m
3239         touch $DIR/d31m/s
3240         mkdir $DIR/d31m2
3241         touch $DIR/d31m2/exist
3242         mlink $DIR/d31m/s $DIR/d31m2/t || error "mlink"
3243         mlink $DIR/d31m/s $DIR/d31m2/exist && error "mlink to exist file"
3244         mlink $DIR/d31m/s $DIR/d31m2 && error "mlink to parent dir"
3245         mlink $DIR/d31m2 $DIR/d31m/s && error "mlink parent dir to target"
3246         mlink $DIR/d31m/not-exist $DIR/d31m2/foo && error "mlink non-existing to new"
3247         mlink $DIR/d31m/not-exist $DIR/d31m2/s && error "mlink non-existing to exist"
3248         return 0
3249 }
3250 run_test 31m "link to file: the same, non-existing, dir==============="
3251
3252 test_31n() {
3253         touch $DIR/$tfile || error "cannot create '$DIR/$tfile'"
3254         nlink=$(stat --format=%h $DIR/$tfile)
3255         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3256         local fd=$(free_fd)
3257         local cmd="exec $fd<$DIR/$tfile"
3258         eval $cmd
3259         cmd="exec $fd<&-"
3260         trap "eval $cmd" EXIT
3261         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3262         [ ${nlink:--1} -eq 1 ] || error "nlink is $nlink, expected 1"
3263         rm $DIR/$tfile || error "cannot remove '$DIR/$tfile'"
3264         nlink=$(stat --dereference --format=%h /proc/self/fd/$fd)
3265         [ ${nlink:--1} -eq 0 ] || error "nlink is $nlink, expected 0"
3266         eval $cmd
3267 }
3268 run_test 31n "check link count of unlinked file"
3269
3270 link_one() {
3271         local tempfile=$(mktemp $1_XXXXXX)
3272         mlink $tempfile $1 2> /dev/null &&
3273                 echo "$BASHPID: link $tempfile to $1 succeeded"
3274         munlink $tempfile
3275 }
3276
3277 test_31o() { # LU-2901
3278         test_mkdir $DIR/$tdir
3279         for LOOP in $(seq 100); do
3280                 rm -f $DIR/$tdir/$tfile*
3281                 for THREAD in $(seq 8); do
3282                         link_one $DIR/$tdir/$tfile.$LOOP &
3283                 done
3284                 wait
3285                 local LINKS=$(ls -1 $DIR/$tdir | grep -c $tfile.$LOOP)
3286                 [[ $LINKS -gt 1 ]] && ls $DIR/$tdir &&
3287                         error "$LINKS duplicate links to $tfile.$LOOP" &&
3288                         break || true
3289         done
3290 }
3291 run_test 31o "duplicate hard links with same filename"
3292
3293 test_31p() {
3294         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3295
3296         test_mkdir $DIR/$tdir
3297         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3298         $LFS setdirstripe -D -c2 -H all_char $DIR/$tdir/striped_dir
3299
3300         opendirunlink $DIR/$tdir/striped_dir/test1 ||
3301                 error "open unlink test1 failed"
3302         opendirunlink $DIR/$tdir/striped_dir/test2 ||
3303                 error "open unlink test2 failed"
3304
3305         $CHECKSTAT -a $DIR/$tdir/striped_dir/test1 ||
3306                 error "test1 still exists"
3307         $CHECKSTAT -a $DIR/$tdir/striped_dir/test2 ||
3308                 error "test2 still exists"
3309 }
3310 run_test 31p "remove of open striped directory"
3311
3312 cleanup_test32_mount() {
3313         local rc=0
3314         trap 0
3315         local loopdev=$(losetup -a | grep $EXT2_DEV | sed -ne 's/:.*$//p')
3316         $UMOUNT $DIR/$tdir/ext2-mountpoint || rc=$?
3317         losetup -d $loopdev || true
3318         rm -rf $DIR/$tdir
3319         return $rc
3320 }
3321
3322 test_32a() {
3323         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3324
3325         echo "== more mountpoints and symlinks ================="
3326         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3327         trap cleanup_test32_mount EXIT
3328         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3329         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3330                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3331         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/.. ||
3332                 error "$DIR/$tdir/ext2-mountpoint/.. not dir type"
3333         cleanup_test32_mount
3334 }
3335 run_test 32a "stat d32a/ext2-mountpoint/.. ====================="
3336
3337 test_32b() {
3338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3339
3340         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3341         trap cleanup_test32_mount EXIT
3342         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3343         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3344                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3345         ls -al $DIR/$tdir/ext2-mountpoint/.. ||
3346                 error "Can't list $DIR/$tdir/ext2-mountpoint/.."
3347         cleanup_test32_mount
3348 }
3349 run_test 32b "open d32b/ext2-mountpoint/.. ====================="
3350
3351 test_32c() {
3352         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3353
3354         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3355         trap cleanup_test32_mount EXIT
3356         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3357         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3358                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3359         test_mkdir -p $DIR/$tdir/d2/test_dir
3360         $CHECKSTAT -t dir $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3361                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_dir not dir type"
3362         cleanup_test32_mount
3363 }
3364 run_test 32c "stat d32c/ext2-mountpoint/../d2/test_dir ========="
3365
3366 test_32d() {
3367         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3368
3369         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3370         trap cleanup_test32_mount EXIT
3371         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3372         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3373                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3374         test_mkdir -p $DIR/$tdir/d2/test_dir
3375         ls -al $DIR/$tdir/ext2-mountpoint/../d2/test_dir ||
3376                 error "Can't list $DIR/$tdir/ext2-mountpoint/../d2/test_dir"
3377         cleanup_test32_mount
3378 }
3379 run_test 32d "open d32d/ext2-mountpoint/../d2/test_dir"
3380
3381 test_32e() {
3382         rm -fr $DIR/$tdir
3383         test_mkdir -p $DIR/$tdir/tmp
3384         local tmp_dir=$DIR/$tdir/tmp
3385         ln -s $DIR/$tdir $tmp_dir/symlink11
3386         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3387         $CHECKSTAT -t link $DIR/$tdir/tmp/symlink11 || error "symlink11 bad"
3388         $CHECKSTAT -t link $DIR/$tdir/symlink01 || error "symlink01 bad"
3389 }
3390 run_test 32e "stat d32e/symlink->tmp/symlink->lustre-subdir"
3391
3392 test_32f() {
3393         rm -fr $DIR/$tdir
3394         test_mkdir -p $DIR/$tdir/tmp
3395         local tmp_dir=$DIR/$tdir/tmp
3396         ln -s $DIR/$tdir $tmp_dir/symlink11
3397         ln -s $tmp_dir/symlink11 $tmp_dir/../symlink01
3398         ls $DIR/$tdir/tmp/symlink11  || error "symlink11 bad"
3399         ls $DIR/$tdir/symlink01 || error "symlink01 bad"
3400 }
3401 run_test 32f "open d32f/symlink->tmp/symlink->lustre-subdir"
3402
3403 test_32g() {
3404         local tmp_dir=$DIR/$tdir/tmp
3405         test_mkdir -p $tmp_dir
3406         test_mkdir $DIR/${tdir}2
3407         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3408         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3409         $CHECKSTAT -t link $tmp_dir/symlink12 || error "symlink12 not a link"
3410         $CHECKSTAT -t link $DIR/$tdir/symlink02 || error "symlink02 not a link"
3411         $CHECKSTAT -t dir -f $tmp_dir/symlink12 || error "symlink12 not a dir"
3412         $CHECKSTAT -t dir -f $DIR/$tdir/symlink02 || error "symlink12 not a dir"
3413 }
3414 run_test 32g "stat d32g/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3415
3416 test_32h() {
3417         rm -fr $DIR/$tdir $DIR/${tdir}2
3418         tmp_dir=$DIR/$tdir/tmp
3419         test_mkdir -p $tmp_dir
3420         test_mkdir $DIR/${tdir}2
3421         ln -s $DIR/${tdir}2 $tmp_dir/symlink12
3422         ln -s $tmp_dir/symlink12 $tmp_dir/../symlink02
3423         ls $tmp_dir/symlink12 || error "listing symlink12"
3424         ls $DIR/$tdir/symlink02  || error "listing symlink02"
3425 }
3426 run_test 32h "open d32h/symlink->tmp/symlink->lustre-subdir/${tdir}2"
3427
3428 test_32i() {
3429         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3430
3431         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3432         trap cleanup_test32_mount EXIT
3433         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3434         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3435                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3436         touch $DIR/$tdir/test_file
3437         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../test_file ||
3438                 error "$DIR/$tdir/ext2-mountpoint/../test_file not file type"
3439         cleanup_test32_mount
3440 }
3441 run_test 32i "stat d32i/ext2-mountpoint/../test_file ==========="
3442
3443 test_32j() {
3444         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3445
3446         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3447         trap cleanup_test32_mount EXIT
3448         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3449         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3450                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3451         touch $DIR/$tdir/test_file
3452         cat $DIR/$tdir/ext2-mountpoint/../test_file ||
3453                 error "Can't open $DIR/$tdir/ext2-mountpoint/../test_file"
3454         cleanup_test32_mount
3455 }
3456 run_test 32j "open d32j/ext2-mountpoint/../test_file ==========="
3457
3458 test_32k() {
3459         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3460
3461         rm -fr $DIR/$tdir
3462         trap cleanup_test32_mount EXIT
3463         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3464         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3465                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3466         test_mkdir -p $DIR/$tdir/d2
3467         touch $DIR/$tdir/d2/test_file || error "touch failed"
3468         $CHECKSTAT -t file $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3469                 error "$DIR/$tdir/ext2-mountpoint/../d2/test_file not file type"
3470         cleanup_test32_mount
3471 }
3472 run_test 32k "stat d32k/ext2-mountpoint/../d2/test_file ========"
3473
3474 test_32l() {
3475         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3476
3477         rm -fr $DIR/$tdir
3478         trap cleanup_test32_mount EXIT
3479         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3480         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3481                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3482         test_mkdir -p $DIR/$tdir/d2
3483         touch $DIR/$tdir/d2/test_file || error "touch failed"
3484         cat  $DIR/$tdir/ext2-mountpoint/../d2/test_file ||
3485                 error "Can't open $DIR/$tdir/ext2-mountpoint/../d2/test_file"
3486         cleanup_test32_mount
3487 }
3488 run_test 32l "open d32l/ext2-mountpoint/../d2/test_file ========"
3489
3490 test_32m() {
3491         rm -fr $DIR/d32m
3492         test_mkdir -p $DIR/d32m/tmp
3493         TMP_DIR=$DIR/d32m/tmp
3494         ln -s $DIR $TMP_DIR/symlink11
3495         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3496         $CHECKSTAT -t link $DIR/d32m/tmp/symlink11 ||
3497                 error "symlink11 not a link"
3498         $CHECKSTAT -t link $DIR/d32m/symlink01 ||
3499                 error "symlink01 not a link"
3500 }
3501 run_test 32m "stat d32m/symlink->tmp/symlink->lustre-root ======"
3502
3503 test_32n() {
3504         rm -fr $DIR/d32n
3505         test_mkdir -p $DIR/d32n/tmp
3506         TMP_DIR=$DIR/d32n/tmp
3507         ln -s $DIR $TMP_DIR/symlink11
3508         ln -s $TMP_DIR/symlink11 $TMP_DIR/../symlink01
3509         ls -l $DIR/d32n/tmp/symlink11  || error "listing symlink11"
3510         ls -l $DIR/d32n/symlink01 || error "listing symlink01"
3511 }
3512 run_test 32n "open d32n/symlink->tmp/symlink->lustre-root ======"
3513
3514 test_32o() {
3515         touch $DIR/$tfile
3516         test_mkdir -p $DIR/d32o/tmp
3517         TMP_DIR=$DIR/d32o/tmp
3518         ln -s $DIR/$tfile $TMP_DIR/symlink12
3519         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3520         $CHECKSTAT -t link $DIR/d32o/tmp/symlink12 ||
3521                 error "symlink12 not a link"
3522         $CHECKSTAT -t link $DIR/d32o/symlink02 || error "symlink02 not a link"
3523         $CHECKSTAT -t file -f $DIR/d32o/tmp/symlink12 ||
3524                 error "$DIR/d32o/tmp/symlink12 not file type"
3525         $CHECKSTAT -t file -f $DIR/d32o/symlink02 ||
3526                 error "$DIR/d32o/symlink02 not file type"
3527 }
3528 run_test 32o "stat d32o/symlink->tmp/symlink->lustre-root/$tfile"
3529
3530 test_32p() {
3531         log 32p_1
3532         rm -fr $DIR/d32p
3533         log 32p_2
3534         rm -f $DIR/$tfile
3535         log 32p_3
3536         touch $DIR/$tfile
3537         log 32p_4
3538         test_mkdir -p $DIR/d32p/tmp
3539         log 32p_5
3540         TMP_DIR=$DIR/d32p/tmp
3541         log 32p_6
3542         ln -s $DIR/$tfile $TMP_DIR/symlink12
3543         log 32p_7
3544         ln -s $TMP_DIR/symlink12 $TMP_DIR/../symlink02
3545         log 32p_8
3546         cat $DIR/d32p/tmp/symlink12 ||
3547                 error "Can't open $DIR/d32p/tmp/symlink12"
3548         log 32p_9
3549         cat $DIR/d32p/symlink02 || error "Can't open $DIR/d32p/symlink02"
3550         log 32p_10
3551 }
3552 run_test 32p "open d32p/symlink->tmp/symlink->lustre-root/$tfile"
3553
3554 test_32q() {
3555         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3556
3557         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3558         trap cleanup_test32_mount EXIT
3559         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3560         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3561         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3562                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3563         ls $DIR/$tdir/ext2-mountpoint | grep "\<under_the_mount\>" && error
3564         cleanup_test32_mount
3565 }
3566 run_test 32q "stat follows mountpoints in Lustre (should return error)"
3567
3568 test_32r() {
3569         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3570
3571         [ -e $DIR/$tdir ] && rm -fr $DIR/$tdir
3572         trap cleanup_test32_mount EXIT
3573         test_mkdir -p $DIR/$tdir/ext2-mountpoint
3574         touch $DIR/$tdir/ext2-mountpoint/under_the_mount || error "touch failed"
3575         mount -t ext2 -o loop $EXT2_DEV $DIR/$tdir/ext2-mountpoint ||
3576                 error "mount failed for $EXT2_DEV $DIR/$tdir/ext2-mountpoint"
3577         ls $DIR/$tdir/ext2-mountpoint | grep -q under_the_mount && error || true
3578         cleanup_test32_mount
3579 }
3580 run_test 32r "opendir follows mountpoints in Lustre (should return error)"
3581
3582 test_33aa() {
3583         rm -f $DIR/$tfile
3584         touch $DIR/$tfile
3585         chmod 444 $DIR/$tfile
3586         chown $RUNAS_ID $DIR/$tfile
3587         log 33_1
3588         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3589         log 33_2
3590 }
3591 run_test 33aa "write file with mode 444 (should return error)"
3592
3593 test_33a() {
3594         rm -fr $DIR/$tdir
3595         test_mkdir $DIR/$tdir
3596         chown $RUNAS_ID $DIR/$tdir
3597         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile ||
3598                 error "$RUNAS create $tdir/$tfile failed"
3599         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $DIR/$tdir/$tfile &&
3600                 error "open RDWR" || true
3601 }
3602 run_test 33a "test open file(mode=0444) with O_RDWR (should return error)"
3603
3604 test_33b() {
3605         rm -fr $DIR/$tdir
3606         test_mkdir $DIR/$tdir
3607         chown $RUNAS_ID $DIR/$tdir
3608         $RUNAS $OPENFILE -f 1286739555 $DIR/$tdir/$tfile || true
3609 }
3610 run_test 33b "test open file with malformed flags (No panic)"
3611
3612 test_33c() {
3613         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3614         remote_ost_nodsh && skip "remote OST with nodsh"
3615
3616         local ostnum
3617         local ostname
3618         local write_bytes
3619         local all_zeros
3620
3621         all_zeros=:
3622         rm -fr $DIR/$tdir
3623         test_mkdir $DIR/$tdir
3624         # Read: 0, Write: 4, create/destroy: 2/0, stat: 1, punch: 0
3625
3626         sync
3627         for ostnum in $(seq $OSTCOUNT); do
3628                 # test-framework's OST numbering is one-based, while Lustre's
3629                 # is zero-based
3630                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3631                 # Parsing llobdstat's output sucks; we could grep the /proc
3632                 # path, but that's likely to not be as portable as using the
3633                 # llobdstat utility.  So we parse lctl output instead.
3634                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3635                         obdfilter/$ostname/stats |
3636                         awk '/^write_bytes/ {print $7}' )
3637                 echo "baseline_write_bytes@$OSTnum/$ostname=$write_bytes"
3638                 if (( ${write_bytes:-0} > 0 ))
3639                 then
3640                         all_zeros=false
3641                         break;
3642                 fi
3643         done
3644
3645         $all_zeros || return 0
3646
3647         # Write four bytes
3648         echo foo > $DIR/$tdir/bar
3649         # Really write them
3650         sync
3651
3652         # Total up write_bytes after writing.  We'd better find non-zeros.
3653         for ostnum in $(seq $OSTCOUNT); do
3654                 ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3655                 write_bytes=$(do_facet ost$ostnum lctl get_param -n \
3656                         obdfilter/$ostname/stats |
3657                         awk '/^write_bytes/ {print $7}' )
3658                 echo "write_bytes@$OSTnum/$ostname=$write_bytes"
3659                 if (( ${write_bytes:-0} > 0 ))
3660                 then
3661                         all_zeros=false
3662                         break;
3663                 fi
3664         done
3665
3666         if $all_zeros
3667         then
3668                 for ostnum in $(seq $OSTCOUNT); do
3669                         ostname=$(printf "$FSNAME-OST%.4x" $((ostnum - 1)))
3670                         echo "Check that write_bytes is present in obdfilter/*/stats:"
3671                         do_facet ost$ostnum lctl get_param -n \
3672                                 obdfilter/$ostname/stats
3673                 done
3674                 error "OST not keeping write_bytes stats (b22312)"
3675         fi
3676 }
3677 run_test 33c "test llobdstat and write_bytes"
3678
3679 test_33d() {
3680         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
3681         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3682
3683         local MDTIDX=1
3684         local remote_dir=$DIR/$tdir/remote_dir
3685
3686         test_mkdir $DIR/$tdir
3687         $LFS mkdir -i $MDTIDX $remote_dir ||
3688                 error "create remote directory failed"
3689
3690         touch $remote_dir/$tfile
3691         chmod 444 $remote_dir/$tfile
3692         chown $RUNAS_ID $remote_dir/$tfile
3693
3694         $RUNAS $OPENFILE -f O_RDWR $DIR/$tfile && error || true
3695
3696         chown $RUNAS_ID $remote_dir
3697         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 ||
3698                                         error "create" || true
3699         $RUNAS $OPENFILE -f O_RDWR:O_CREAT -m 0444 $remote_dir/f33 &&
3700                                     error "open RDWR" || true
3701         $RUNAS $OPENFILE -f 1286739555 $remote_dir/f33 || true
3702 }
3703 run_test 33d "openfile with 444 modes and malformed flags under remote dir"
3704
3705 test_33e() {
3706         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3707
3708         mkdir $DIR/$tdir
3709
3710         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3711         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3712         mkdir $DIR/$tdir/local_dir
3713
3714         local s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3715         local s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3716         local l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3717
3718         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3719                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode"
3720
3721         rmdir $DIR/$tdir/* || error "rmdir failed"
3722
3723         umask 777
3724         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3725         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3726         mkdir $DIR/$tdir/local_dir
3727
3728         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3729         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3730         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3731
3732         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3733                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 777"
3734
3735         rmdir $DIR/$tdir/* || error "rmdir(umask 777) failed"
3736
3737         umask 000
3738         $LFS setdirstripe -i0 -c2 $DIR/$tdir/striped_dir
3739         $LFS setdirstripe -i1 -c2 $DIR/$tdir/striped_dir1
3740         mkdir $DIR/$tdir/local_dir
3741
3742         s0_mode=$(stat -c%f $DIR/$tdir/striped_dir)
3743         s1_mode=$(stat -c%f $DIR/$tdir/striped_dir1)
3744         l_mode=$(stat -c%f $DIR/$tdir/local_dir)
3745
3746         [ "$l_mode" = "$s0_mode" -a "$l_mode" = "$s1_mode" ] ||
3747                 error "mkdir $l_mode striped0 $s0_mode striped1 $s1_mode 0"
3748 }
3749 run_test 33e "mkdir and striped directory should have same mode"
3750
3751 cleanup_33f() {
3752         trap 0
3753         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=0
3754 }
3755
3756 test_33f() {
3757         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3758         remote_mds_nodsh && skip "remote MDS with nodsh"
3759
3760         mkdir $DIR/$tdir
3761         chmod go+rwx $DIR/$tdir
3762         do_facet $SINGLEMDS $LCTL set_param mdt.*.enable_remote_dir_gid=-1
3763         trap cleanup_33f EXIT
3764
3765         $RUNAS lfs mkdir -i 0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
3766                 error "cannot create striped directory"
3767
3768         $RUNAS touch $DIR/$tdir/striped_dir/{0..16} ||
3769                 error "cannot create files in striped directory"
3770
3771         $RUNAS rm $DIR/$tdir/striped_dir/{0..16} ||
3772                 error "cannot remove files in striped directory"
3773
3774         $RUNAS rmdir $DIR/$tdir/striped_dir ||
3775                 error "cannot remove striped directory"
3776
3777         cleanup_33f
3778 }
3779 run_test 33f "nonroot user can create, access, and remove a striped directory"
3780
3781 test_33g() {
3782         mkdir -p $DIR/$tdir/dir2
3783
3784         local err=$($RUNAS mkdir $DIR/$tdir/dir2 2>&1)
3785         echo $err
3786         [[ $err =~ "exists" ]] || error "Not exists error"
3787 }
3788 run_test 33g "nonroot user create already existing root created file"
3789
3790 test_33h() {
3791         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
3792         [ $MDS1_VERSION -lt $(version_code 2.13.50) ] &&
3793                 skip "Need MDS version at least 2.13.50"
3794
3795         test_mkdir -c $MDSCOUNT -H crush $DIR/$tdir ||
3796                 error "mkdir $tdir failed"
3797         touch $DIR/$tdir/$tfile || error "touch $tfile failed"
3798
3799         local index=$($LFS getstripe -m $DIR/$tdir/$tfile)
3800         local index2
3801
3802         for fname in $DIR/$tdir/$tfile.bak \
3803                      $DIR/$tdir/$tfile.SAV \
3804                      $DIR/$tdir/$tfile.orig \
3805                      $DIR/$tdir/$tfile~; do
3806                 touch $fname  || error "touch $fname failed"
3807                 index2=$($LFS getstripe -m $fname)
3808                 [ $index -eq $index2 ] ||
3809                         error "$fname MDT index mismatch $index != $index2"
3810         done
3811
3812         local failed=0
3813         for i in {1..50}; do
3814                 for fname in $(mktemp -u $DIR/$tdir/.$tfile.XXXXXX) \
3815                              $(mktemp $DIR/$tdir/$tfile.XXXXXXXX); do
3816                         touch $fname  || error "touch $fname failed"
3817                         index2=$($LFS getstripe -m $fname)
3818                         if [[ $index != $index2 ]]; then
3819                                 failed=$((failed + 1))
3820                                 echo "$fname MDT index mismatch $index != $index2"
3821                         fi
3822                 done
3823         done
3824         echo "$failed MDT index mismatches"
3825         (( failed < 4 )) || error "MDT index mismatch $failed times"
3826
3827 }
3828 run_test 33h "temp file is located on the same MDT as target"
3829
3830 TEST_34_SIZE=${TEST_34_SIZE:-2000000000000}
3831 test_34a() {
3832         rm -f $DIR/f34
3833         $MCREATE $DIR/f34 || error "mcreate failed"
3834         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3835                 error "getstripe failed"
3836         $TRUNCATE $DIR/f34 $TEST_34_SIZE || error "truncate failed"
3837         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" ||
3838                 error "getstripe failed"
3839         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3840                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3841 }
3842 run_test 34a "truncate file that has not been opened ==========="
3843
3844 test_34b() {
3845         [ ! -f $DIR/f34 ] && test_34a
3846         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3847                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3848         $OPENFILE -f O_RDONLY $DIR/f34
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 34b "O_RDONLY opening file doesn't create objects ====="
3855
3856 test_34c() {
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_RDWR $DIR/f34
3861         $LFS getstripe $DIR/f34 2>&1 | grep -q "no stripe info" &&
3862                 error "$LFS 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 34c "O_RDWR opening file-with-size works =============="
3867
3868 test_34d() {
3869         [ ! -f $DIR/f34 ] && test_34a
3870         dd if=/dev/zero of=$DIR/f34 conv=notrunc bs=4k count=1 ||
3871                 error "dd failed"
3872         $CHECKSTAT -s $TEST_34_SIZE $DIR/f34 ||
3873                 error "Size of $DIR/f34 not equal to $TEST_34_SIZE bytes"
3874         rm $DIR/f34
3875 }
3876 run_test 34d "write to sparse file ============================="
3877
3878 test_34e() {
3879         rm -f $DIR/f34e
3880         $MCREATE $DIR/f34e || error "mcreate failed"
3881         $TRUNCATE $DIR/f34e 1000 || error "truncate failed"
3882         $CHECKSTAT -s 1000 $DIR/f34e ||
3883                 error "Size of $DIR/f34e not equal to 1000 bytes"
3884         $OPENFILE -f O_RDWR $DIR/f34e
3885         $CHECKSTAT -s 1000 $DIR/f34e ||
3886                 error "Size of $DIR/f34e not equal to 1000 bytes"
3887 }
3888 run_test 34e "create objects, some with size and some without =="
3889
3890 test_34f() { # bug 6242, 6243
3891         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3892
3893         SIZE34F=48000
3894         rm -f $DIR/f34f
3895         $MCREATE $DIR/f34f || error "mcreate failed"
3896         $TRUNCATE $DIR/f34f $SIZE34F || error "truncating $DIR/f3f to $SIZE34F"
3897         dd if=$DIR/f34f of=$TMP/f34f
3898         $CHECKSTAT -s $SIZE34F $TMP/f34f || error "$TMP/f34f not $SIZE34F bytes"
3899         dd if=/dev/zero of=$TMP/f34fzero bs=$SIZE34F count=1
3900         cmp $DIR/f34f $TMP/f34fzero || error "$DIR/f34f not all zero"
3901         cmp $TMP/f34f $TMP/f34fzero || error "$TMP/f34f not all zero"
3902         rm $TMP/f34f $TMP/f34fzero $DIR/f34f
3903 }
3904 run_test 34f "read from a file with no objects until EOF ======="
3905
3906 test_34g() {
3907         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3908
3909         dd if=/dev/zero of=$DIR/$tfile bs=1 count=100 seek=$TEST_34_SIZE ||
3910                 error "dd failed"
3911         $TRUNCATE $DIR/$tfile $((TEST_34_SIZE / 2))|| error "truncate failed"
3912         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3913                 error "Size of $DIR/$tfile not equal to $((TEST_34_SIZE / 2))"
3914         cancel_lru_locks osc
3915         $CHECKSTAT -s $((TEST_34_SIZE / 2)) $DIR/$tfile ||
3916                 error "wrong size after lock cancel"
3917
3918         $TRUNCATE $DIR/$tfile $TEST_34_SIZE || error "truncate failed"
3919         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3920                 error "expanding truncate failed"
3921         cancel_lru_locks osc
3922         $CHECKSTAT -s $TEST_34_SIZE $DIR/$tfile ||
3923                 error "wrong expanded size after lock cancel"
3924 }
3925 run_test 34g "truncate long file ==============================="
3926
3927 test_34h() {
3928         [ $PARALLEL == "yes" ] && skip "skip parallel run"
3929
3930         local gid=10
3931         local sz=1000
3932
3933         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 || error "dd failed"
3934         sync # Flush the cache so that multiop below does not block on cache
3935              # flush when getting the group lock
3936         $MULTIOP $DIR/$tfile OG${gid}T${sz}g${gid}c &
3937         MULTIPID=$!
3938
3939         # Since just timed wait is not good enough, let's do a sync write
3940         # that way we are sure enough time for a roundtrip + processing
3941         # passed + 2 seconds of extra margin.
3942         dd if=/dev/zero of=$DIR/${tfile}-1 bs=$PAGE_SIZE oflag=direct count=1
3943         rm $DIR/${tfile}-1
3944         sleep 2
3945
3946         if [[ `ps h -o comm -p $MULTIPID` == "multiop" ]]; then
3947                 error "Multiop blocked on ftruncate, pid=$MULTIPID"
3948                 kill -9 $MULTIPID
3949         fi
3950         wait $MULTIPID
3951         local nsz=`stat -c %s $DIR/$tfile`
3952         [[ $nsz == $sz ]] || error "New size wrong $nsz != $sz"
3953 }
3954 run_test 34h "ftruncate file under grouplock should not block"
3955
3956 test_35a() {
3957         cp /bin/sh $DIR/f35a
3958         chmod 444 $DIR/f35a
3959         chown $RUNAS_ID $DIR/f35a
3960         $RUNAS $DIR/f35a && error || true
3961         rm $DIR/f35a
3962 }
3963 run_test 35a "exec file with mode 444 (should return and not leak)"
3964
3965 test_36a() {
3966         rm -f $DIR/f36
3967         utime $DIR/f36 || error "utime failed for MDS"
3968 }
3969 run_test 36a "MDS utime check (mknod, utime)"
3970
3971 test_36b() {
3972         echo "" > $DIR/f36
3973         utime $DIR/f36 || error "utime failed for OST"
3974 }
3975 run_test 36b "OST utime check (open, utime)"
3976
3977 test_36c() {
3978         rm -f $DIR/d36/f36
3979         test_mkdir $DIR/d36
3980         chown $RUNAS_ID $DIR/d36
3981         $RUNAS utime $DIR/d36/f36 || error "utime failed for MDS as non-root"
3982 }
3983 run_test 36c "non-root MDS utime check (mknod, utime)"
3984
3985 test_36d() {
3986         [ ! -d $DIR/d36 ] && test_36c
3987         echo "" > $DIR/d36/f36
3988         $RUNAS utime $DIR/d36/f36 || error "utime failed for OST as non-root"
3989 }
3990 run_test 36d "non-root OST utime check (open, utime)"
3991
3992 test_36e() {
3993         [ $RUNAS_ID -eq $UID ] && skip_env "RUNAS_ID = UID = $UID -- skipping"
3994
3995         test_mkdir $DIR/$tdir
3996         touch $DIR/$tdir/$tfile
3997         $RUNAS utime $DIR/$tdir/$tfile &&
3998                 error "utime worked, expected failure" || true
3999 }
4000 run_test 36e "utime on non-owned file (should return error)"
4001
4002 subr_36fh() {
4003         local fl="$1"
4004         local LANG_SAVE=$LANG
4005         local LC_LANG_SAVE=$LC_LANG
4006         export LANG=C LC_LANG=C # for date language
4007
4008         DATESTR="Dec 20  2000"
4009         test_mkdir $DIR/$tdir
4010         lctl set_param fail_loc=$fl
4011         date; date +%s
4012         cp /etc/hosts $DIR/$tdir/$tfile
4013         sync & # write RPC generated with "current" inode timestamp, but delayed
4014         sleep 1
4015         touch --date="$DATESTR" $DIR/$tdir/$tfile # setattr timestamp in past
4016         LS_BEFORE="`ls -l $DIR/$tdir/$tfile`" # old timestamp from client cache
4017         cancel_lru_locks $OSC
4018         LS_AFTER="`ls -l $DIR/$tdir/$tfile`"  # timestamp from OST object
4019         date; date +%s
4020         [ "$LS_BEFORE" != "$LS_AFTER" ] && \
4021                 echo "BEFORE: $LS_BEFORE" && \
4022                 echo "AFTER : $LS_AFTER" && \
4023                 echo "WANT  : $DATESTR" && \
4024                 error "$DIR/$tdir/$tfile timestamps changed" || true
4025
4026         export LANG=$LANG_SAVE LC_LANG=$LC_LANG_SAVE
4027 }
4028
4029 test_36f() {
4030         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4031
4032         #define OBD_FAIL_OST_BRW_PAUSE_BULK 0x214
4033         subr_36fh "0x80000214"
4034 }
4035 run_test 36f "utime on file racing with OST BRW write =========="
4036
4037 test_36g() {
4038         remote_ost_nodsh && skip "remote OST with nodsh"
4039         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4040         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
4041                 skip "Need MDS version at least 2.12.51"
4042
4043         local fmd_max_age
4044         local fmd
4045         local facet="ost1"
4046         local tgt="obdfilter"
4047
4048         [[ $OSC == "mdc" ]] && tgt="mdt" && facet="mds1"
4049
4050         test_mkdir $DIR/$tdir
4051         fmd_max_age=$(do_facet $facet \
4052                 "lctl get_param -n $tgt.*.tgt_fmd_seconds 2> /dev/null | \
4053                 head -n 1")
4054
4055         echo "FMD max age: ${fmd_max_age}s"
4056         touch $DIR/$tdir/$tfile
4057         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4058                 gawk '{cnt=cnt+$1}  END{print cnt}')
4059         echo "FMD before: $fmd"
4060         [[ $fmd == 0 ]] &&
4061                 error "FMD wasn't create by touch"
4062         sleep $((fmd_max_age + 12))
4063         fmd=$(do_facet $facet "lctl get_param -n $tgt.*.exports.*.fmd_count" |
4064                 gawk '{cnt=cnt+$1}  END{print cnt}')
4065         echo "FMD after: $fmd"
4066         [[ $fmd == 0 ]] ||
4067                 error "FMD wasn't expired by ping"
4068 }
4069 run_test 36g "FMD cache expiry ====================="
4070
4071 test_36h() {
4072         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4073
4074         #define OBD_FAIL_OST_BRW_PAUSE_BULK2 0x227
4075         subr_36fh "0x80000227"
4076 }
4077 run_test 36h "utime on file racing with OST BRW write =========="
4078
4079 test_36i() {
4080         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
4081
4082         test_mkdir $DIR/$tdir
4083         $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir
4084
4085         local mtime=$(stat -c%Y $DIR/$tdir/striped_dir)
4086         local new_mtime=$((mtime + 200))
4087
4088         #change Modify time of striped dir
4089         touch -m -d @$new_mtime $DIR/$tdir/striped_dir ||
4090                         error "change mtime failed"
4091
4092         local got=$(stat -c%Y $DIR/$tdir/striped_dir)
4093
4094         [ "$new_mtime" = "$got" ] || error "expect $new_mtime got $got"
4095 }
4096 run_test 36i "change mtime on striped directory"
4097
4098 # test_37 - duplicate with tests 32q 32r
4099
4100 test_38() {
4101         local file=$DIR/$tfile
4102         touch $file
4103         openfile -f O_DIRECTORY $file
4104         local RC=$?
4105         local ENOTDIR=20
4106         [ $RC -eq 0 ] && error "opened file $file with O_DIRECTORY" || true
4107         [ $RC -eq $ENOTDIR ] || error "error $RC should be ENOTDIR ($ENOTDIR)"
4108 }
4109 run_test 38 "open a regular file with O_DIRECTORY should return -ENOTDIR ==="
4110
4111 test_39a() { # was test_39
4112         touch $DIR/$tfile
4113         touch $DIR/${tfile}2
4114 #       ls -l  $DIR/$tfile $DIR/${tfile}2
4115 #       ls -lu  $DIR/$tfile $DIR/${tfile}2
4116 #       ls -lc  $DIR/$tfile $DIR/${tfile}2
4117         sleep 2
4118         $OPENFILE -f O_CREAT:O_TRUNC:O_WRONLY $DIR/${tfile}2
4119         if [ ! $DIR/${tfile}2 -nt $DIR/$tfile ]; then
4120                 echo "mtime"
4121                 ls -l --full-time $DIR/$tfile $DIR/${tfile}2
4122                 echo "atime"
4123                 ls -lu --full-time $DIR/$tfile $DIR/${tfile}2
4124                 echo "ctime"
4125                 ls -lc --full-time $DIR/$tfile $DIR/${tfile}2
4126                 error "O_TRUNC didn't change timestamps"
4127         fi
4128 }
4129 run_test 39a "mtime changed on create"
4130
4131 test_39b() {
4132         test_mkdir -c1 $DIR/$tdir
4133         cp -p /etc/passwd $DIR/$tdir/fopen
4134         cp -p /etc/passwd $DIR/$tdir/flink
4135         cp -p /etc/passwd $DIR/$tdir/funlink
4136         cp -p /etc/passwd $DIR/$tdir/frename
4137         ln $DIR/$tdir/funlink $DIR/$tdir/funlink2
4138
4139         sleep 1
4140         echo "aaaaaa" >> $DIR/$tdir/fopen
4141         echo "aaaaaa" >> $DIR/$tdir/flink
4142         echo "aaaaaa" >> $DIR/$tdir/funlink
4143         echo "aaaaaa" >> $DIR/$tdir/frename
4144
4145         local open_new=`stat -c %Y $DIR/$tdir/fopen`
4146         local link_new=`stat -c %Y $DIR/$tdir/flink`
4147         local unlink_new=`stat -c %Y $DIR/$tdir/funlink`
4148         local rename_new=`stat -c %Y $DIR/$tdir/frename`
4149
4150         cat $DIR/$tdir/fopen > /dev/null
4151         ln $DIR/$tdir/flink $DIR/$tdir/flink2
4152         rm -f $DIR/$tdir/funlink2
4153         mv -f $DIR/$tdir/frename $DIR/$tdir/frename2
4154
4155         for (( i=0; i < 2; i++ )) ; do
4156                 local open_new2=`stat -c %Y $DIR/$tdir/fopen`
4157                 local link_new2=`stat -c %Y $DIR/$tdir/flink`
4158                 local unlink_new2=`stat -c %Y $DIR/$tdir/funlink`
4159                 local rename_new2=`stat -c %Y $DIR/$tdir/frename2`
4160
4161                 [ $open_new2 -eq $open_new ] || error "open file reverses mtime"
4162                 [ $link_new2 -eq $link_new ] || error "link file reverses mtime"
4163                 [ $unlink_new2 -eq $unlink_new ] || error "unlink file reverses mtime"
4164                 [ $rename_new2 -eq $rename_new ] || error "rename file reverses mtime"
4165
4166                 cancel_lru_locks $OSC
4167                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4168         done
4169 }
4170 run_test 39b "mtime change on open, link, unlink, rename  ======"
4171
4172 # this should be set to past
4173 TEST_39_MTIME=`date -d "1 year ago" +%s`
4174
4175 # bug 11063
4176 test_39c() {
4177         touch $DIR1/$tfile
4178         sleep 2
4179         local mtime0=`stat -c %Y $DIR1/$tfile`
4180
4181         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4182         local mtime1=`stat -c %Y $DIR1/$tfile`
4183         [ "$mtime1" = $TEST_39_MTIME ] || \
4184                 error "mtime is not set to past: $mtime1, should be $TEST_39_MTIME"
4185
4186         local d1=`date +%s`
4187         echo hello >> $DIR1/$tfile
4188         local d2=`date +%s`
4189         local mtime2=`stat -c %Y $DIR1/$tfile`
4190         [ "$mtime2" -ge "$d1" ] && [ "$mtime2" -le "$d2" ] || \
4191                 error "mtime is not updated on write: $d1 <= $mtime2 <= $d2"
4192
4193         mv $DIR1/$tfile $DIR1/$tfile-1
4194
4195         for (( i=0; i < 2; i++ )) ; do
4196                 local mtime3=`stat -c %Y $DIR1/$tfile-1`
4197                 [ "$mtime2" = "$mtime3" ] || \
4198                         error "mtime ($mtime2) changed (to $mtime3) on rename"
4199
4200                 cancel_lru_locks $OSC
4201                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4202         done
4203 }
4204 run_test 39c "mtime change on rename ==========================="
4205
4206 # bug 21114
4207 test_39d() {
4208         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4209
4210         touch $DIR1/$tfile
4211         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4212
4213         for (( i=0; i < 2; i++ )) ; do
4214                 local mtime=`stat -c %Y $DIR1/$tfile`
4215                 [ $mtime = $TEST_39_MTIME ] || \
4216                         error "mtime($mtime) is not set to $TEST_39_MTIME"
4217
4218                 cancel_lru_locks $OSC
4219                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4220         done
4221 }
4222 run_test 39d "create, utime, stat =============================="
4223
4224 # bug 21114
4225 test_39e() {
4226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4227
4228         touch $DIR1/$tfile
4229         local mtime1=`stat -c %Y $DIR1/$tfile`
4230
4231         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4232
4233         for (( i=0; i < 2; i++ )) ; do
4234                 local mtime2=`stat -c %Y $DIR1/$tfile`
4235                 [ $mtime2 = $TEST_39_MTIME ] || \
4236                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4237
4238                 cancel_lru_locks $OSC
4239                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4240         done
4241 }
4242 run_test 39e "create, stat, utime, stat ========================"
4243
4244 # bug 21114
4245 test_39f() {
4246         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4247
4248         touch $DIR1/$tfile
4249         mtime1=`stat -c %Y $DIR1/$tfile`
4250
4251         sleep 2
4252         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4253
4254         for (( i=0; i < 2; i++ )) ; do
4255                 local mtime2=`stat -c %Y $DIR1/$tfile`
4256                 [ $mtime2 = $TEST_39_MTIME ] || \
4257                         error "mtime($mtime2) is not set to $TEST_39_MTIME"
4258
4259                 cancel_lru_locks $OSC
4260                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4261         done
4262 }
4263 run_test 39f "create, stat, sleep, utime, stat ================="
4264
4265 # bug 11063
4266 test_39g() {
4267         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4268
4269         echo hello >> $DIR1/$tfile
4270         local mtime1=`stat -c %Y $DIR1/$tfile`
4271
4272         sleep 2
4273         chmod o+r $DIR1/$tfile
4274
4275         for (( i=0; i < 2; i++ )) ; do
4276                 local mtime2=`stat -c %Y $DIR1/$tfile`
4277                 [ "$mtime1" = "$mtime2" ] || \
4278                         error "lost mtime: $mtime2, should be $mtime1"
4279
4280                 cancel_lru_locks $OSC
4281                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4282         done
4283 }
4284 run_test 39g "write, chmod, stat ==============================="
4285
4286 # bug 11063
4287 test_39h() {
4288         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4289
4290         touch $DIR1/$tfile
4291         sleep 1
4292
4293         local d1=`date`
4294         echo hello >> $DIR1/$tfile
4295         local mtime1=`stat -c %Y $DIR1/$tfile`
4296
4297         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4298         local d2=`date`
4299         if [ "$d1" != "$d2" ]; then
4300                 echo "write and touch not within one second"
4301         else
4302                 for (( i=0; i < 2; i++ )) ; do
4303                         local mtime2=`stat -c %Y $DIR1/$tfile`
4304                         [ "$mtime2" = $TEST_39_MTIME ] || \
4305                                 error "lost mtime: $mtime2, should be $TEST_39_MTIME"
4306
4307                         cancel_lru_locks $OSC
4308                         if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4309                 done
4310         fi
4311 }
4312 run_test 39h "write, utime within one second, stat ============="
4313
4314 test_39i() {
4315         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4316
4317         touch $DIR1/$tfile
4318         sleep 1
4319
4320         echo hello >> $DIR1/$tfile
4321         local mtime1=`stat -c %Y $DIR1/$tfile`
4322
4323         mv $DIR1/$tfile $DIR1/$tfile-1
4324
4325         for (( i=0; i < 2; i++ )) ; do
4326                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4327
4328                 [ "$mtime1" = "$mtime2" ] || \
4329                         error "lost mtime: $mtime2, should be $mtime1"
4330
4331                 cancel_lru_locks $OSC
4332                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4333         done
4334 }
4335 run_test 39i "write, rename, stat =============================="
4336
4337 test_39j() {
4338         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4339
4340         start_full_debug_logging
4341         touch $DIR1/$tfile
4342         sleep 1
4343
4344         #define OBD_FAIL_OSC_DELAY_SETTIME       0x412
4345         lctl set_param fail_loc=0x80000412
4346         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c ||
4347                 error "multiop failed"
4348         local multipid=$!
4349         local mtime1=`stat -c %Y $DIR1/$tfile`
4350
4351         mv $DIR1/$tfile $DIR1/$tfile-1
4352
4353         kill -USR1 $multipid
4354         wait $multipid || error "multiop close failed"
4355
4356         for (( i=0; i < 2; i++ )) ; do
4357                 local mtime2=`stat -c %Y $DIR1/$tfile-1`
4358                 [ "$mtime1" = "$mtime2" ] ||
4359                         error "mtime is lost on close: $mtime2, " \
4360                               "should be $mtime1"
4361
4362                 cancel_lru_locks
4363                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4364         done
4365         lctl set_param fail_loc=0
4366         stop_full_debug_logging
4367 }
4368 run_test 39j "write, rename, close, stat ======================="
4369
4370 test_39k() {
4371         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4372
4373         touch $DIR1/$tfile
4374         sleep 1
4375
4376         multiop_bg_pause $DIR1/$tfile oO_RDWR:w2097152_c || error "multiop failed"
4377         local multipid=$!
4378         local mtime1=`stat -c %Y $DIR1/$tfile`
4379
4380         touch -m -d @$TEST_39_MTIME $DIR1/$tfile
4381
4382         kill -USR1 $multipid
4383         wait $multipid || error "multiop close failed"
4384
4385         for (( i=0; i < 2; i++ )) ; do
4386                 local mtime2=`stat -c %Y $DIR1/$tfile`
4387
4388                 [ "$mtime2" = $TEST_39_MTIME ] || \
4389                         error "mtime is lost on close: $mtime2, should be $TEST_39_MTIME"
4390
4391                 cancel_lru_locks
4392                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4393         done
4394 }
4395 run_test 39k "write, utime, close, stat ========================"
4396
4397 # this should be set to future
4398 TEST_39_ATIME=`date -d "1 year" +%s`
4399
4400 test_39l() {
4401         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4402         remote_mds_nodsh && skip "remote MDS with nodsh"
4403
4404         local atime_diff=$(do_facet $SINGLEMDS \
4405                                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4406         rm -rf $DIR/$tdir
4407         mkdir -p $DIR/$tdir
4408
4409         # test setting directory atime to future
4410         touch -a -d @$TEST_39_ATIME $DIR/$tdir
4411         local atime=$(stat -c %X $DIR/$tdir)
4412         [ "$atime" = $TEST_39_ATIME ] ||
4413                 error "atime is not set to future: $atime, $TEST_39_ATIME"
4414
4415         # test setting directory atime from future to now
4416         local now=$(date +%s)
4417         touch -a -d @$now $DIR/$tdir
4418
4419         atime=$(stat -c %X $DIR/$tdir)
4420         [ "$atime" -eq "$now"  ] ||
4421                 error "atime is not updated from future: $atime, $now"
4422
4423         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=2
4424         sleep 3
4425
4426         # test setting directory atime when now > dir atime + atime_diff
4427         local d1=$(date +%s)
4428         ls $DIR/$tdir
4429         local d2=$(date +%s)
4430         cancel_lru_locks mdc
4431         atime=$(stat -c %X $DIR/$tdir)
4432         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4433                 error "atime is not updated  : $atime, should be $d2"
4434
4435         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=60
4436         sleep 3
4437
4438         # test not setting directory atime when now < dir atime + atime_diff
4439         ls $DIR/$tdir
4440         cancel_lru_locks mdc
4441         atime=$(stat -c %X $DIR/$tdir)
4442         [ "$atime" -ge "$d1" -a "$atime" -le "$d2" ] ||
4443                 error "atime is updated to $atime, should remain $d1<atime<$d2"
4444
4445         do_facet $SINGLEMDS \
4446                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4447 }
4448 run_test 39l "directory atime update ==========================="
4449
4450 test_39m() {
4451         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4452
4453         touch $DIR1/$tfile
4454         sleep 2
4455         local far_past_mtime=$(date -d "May 29 1953" +%s)
4456         local far_past_atime=$(date -d "Dec 17 1903" +%s)
4457
4458         touch -m -d @$far_past_mtime $DIR1/$tfile
4459         touch -a -d @$far_past_atime $DIR1/$tfile
4460
4461         for (( i=0; i < 2; i++ )) ; do
4462                 local timestamps=$(stat -c "%X %Y" $DIR1/$tfile)
4463                 [ "$timestamps" = "$far_past_atime $far_past_mtime" ] || \
4464                         error "atime or mtime set incorrectly"
4465
4466                 cancel_lru_locks $OSC
4467                 if [ $i = 0 ] ; then echo "repeat after cancel_lru_locks"; fi
4468         done
4469 }
4470 run_test 39m "test atime and mtime before 1970"
4471
4472 test_39n() { # LU-3832
4473         remote_mds_nodsh && skip "remote MDS with nodsh"
4474
4475         local atime_diff=$(do_facet $SINGLEMDS \
4476                 lctl get_param -n mdd.*MDT0000*.atime_diff)
4477         local atime0
4478         local atime1
4479         local atime2
4480
4481         do_facet $SINGLEMDS lctl set_param -n mdd.*MDT0000*.atime_diff=1
4482
4483         rm -rf $DIR/$tfile
4484         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 status=noxfer
4485         atime0=$(stat -c %X $DIR/$tfile)
4486
4487         sleep 5
4488         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4489         atime1=$(stat -c %X $DIR/$tfile)
4490
4491         sleep 5
4492         cancel_lru_locks mdc
4493         cancel_lru_locks osc
4494         $MULTIOP $DIR/$tfile oO_RDONLY:O_NOATIME:r4096c
4495         atime2=$(stat -c %X $DIR/$tfile)
4496
4497         do_facet $SINGLEMDS \
4498                 lctl set_param -n mdd.*MDT0000*.atime_diff=$atime_diff
4499
4500         [ "$atime0" -eq "$atime1" ] || error "atime0 $atime0 != atime1 $atime1"
4501         [ "$atime1" -eq "$atime2" ] || error "atime0 $atime0 != atime1 $atime1"
4502 }
4503 run_test 39n "check that O_NOATIME is honored"
4504
4505 test_39o() {
4506         TESTDIR=$DIR/$tdir/$tfile
4507         [ -e $TESTDIR ] && rm -rf $TESTDIR
4508         mkdir -p $TESTDIR
4509         cd $TESTDIR
4510         links1=2
4511         ls
4512         mkdir a b
4513         ls
4514         links2=$(stat -c %h .)
4515         [ $(($links1 + 2)) != $links2 ] &&
4516                 error "wrong links count $(($links1 + 2)) != $links2"
4517         rmdir b
4518         links3=$(stat -c %h .)
4519         [ $(($links1 + 1)) != $links3 ] &&
4520                 error "wrong links count $links1 != $links3"
4521         return 0
4522 }
4523 run_test 39o "directory cached attributes updated after create"
4524
4525 test_39p() {
4526         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
4527
4528         local MDTIDX=1
4529         TESTDIR=$DIR/$tdir/$tdir
4530         [ -e $TESTDIR ] && rm -rf $TESTDIR
4531         test_mkdir -p $TESTDIR
4532         cd $TESTDIR
4533         links1=2
4534         ls
4535         test_mkdir -i $MDTIDX $TESTDIR/remote_dir1
4536         test_mkdir -i $MDTIDX $TESTDIR/remote_dir2
4537         ls
4538         links2=$(stat -c %h .)
4539         [ $(($links1 + 2)) != $links2 ] &&
4540                 error "wrong links count $(($links1 + 2)) != $links2"
4541         rmdir remote_dir2
4542         links3=$(stat -c %h .)
4543         [ $(($links1 + 1)) != $links3 ] &&
4544                 error "wrong links count $links1 != $links3"
4545         return 0
4546 }
4547 run_test 39p "remote directory cached attributes updated after create ========"
4548
4549 test_39r() {
4550         [ $OST1_VERSION -ge $(version_code 2.13.52) ] ||
4551                 skip "no atime update on old OST"
4552         if [ "$ost1_FSTYPE" != ldiskfs ]; then
4553                 skip_env "ldiskfs only test"
4554         fi
4555
4556         local saved_adiff
4557         saved_adiff=$(do_facet ost1 \
4558                 lctl get_param -n obdfilter.*OST0000.atime_diff)
4559         stack_trap "do_facet ost1 \
4560                 lctl set_param obdfilter.*.atime_diff=$saved_adiff"
4561
4562         do_facet ost1 "lctl set_param obdfilter.*.atime_diff=5"
4563
4564         $LFS setstripe -i 0 $DIR/$tfile
4565         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 ||
4566                 error "can't write initial file"
4567         cancel_lru_locks osc
4568
4569         # exceed atime_diff and access file
4570         sleep 6
4571         dd if=$DIR/$tfile of=/dev/null || error "can't udpate atime"
4572
4573         local atime_cli=$(stat -c %X $DIR/$tfile)
4574         echo "client atime: $atime_cli"
4575         # allow atime update to be written to device
4576         do_facet ost1 "$LCTL set_param -n osd*.*OST*.force_sync 1"
4577         sleep 5
4578
4579         local ostdev=$(ostdevname 1)
4580         local fid=($(lfs getstripe -y $DIR/$tfile |
4581                         awk '/l_fid:/ { print $2 }' | tr ':' ' '))
4582         local objpath="O/0/d$((${fid[1]} % 32))/$((${fid[1]}))"
4583         local cmd="debugfs -c -R \\\"stat $objpath\\\" $ostdev"
4584
4585         echo "OST atime: $(do_facet ost1 "$cmd" |& grep atime)"
4586         local atime_ost=$(do_facet ost1 "$cmd" |&
4587                           awk -F'[: ]' '/atime:/ { print $4 }')
4588         (( atime_cli == atime_ost )) ||
4589                 error "atime on client $atime_cli != ost $atime_ost"
4590 }
4591 run_test 39r "lazy atime update on OST"
4592
4593 test_39q() { # LU-8041
4594         local testdir=$DIR/$tdir
4595         mkdir -p $testdir
4596         multiop_bg_pause $testdir D_c || error "multiop failed"
4597         local multipid=$!
4598         cancel_lru_locks mdc
4599         kill -USR1 $multipid
4600         local atime=$(stat -c %X $testdir)
4601         [ "$atime" -ne 0 ] || error "atime is zero"
4602 }
4603 run_test 39q "close won't zero out atime"
4604
4605 test_40() {
4606         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
4607         $RUNAS $OPENFILE -f O_WRONLY:O_TRUNC $DIR/$tfile &&
4608                 error "openfile O_WRONLY:O_TRUNC $tfile failed"
4609         $CHECKSTAT -t file -s 4096 $DIR/$tfile ||
4610                 error "$tfile is not 4096 bytes in size"
4611 }
4612 run_test 40 "failed open(O_TRUNC) doesn't truncate ============="
4613
4614 test_41() {
4615         # bug 1553
4616         small_write $DIR/f41 18
4617 }
4618 run_test 41 "test small file write + fstat ====================="
4619
4620 count_ost_writes() {
4621         lctl get_param -n ${OSC}.*.stats |
4622                 awk -vwrites=0 '/ost_write/ { writes += $2 } \
4623                         END { printf("%0.0f", writes) }'
4624 }
4625
4626 # decent default
4627 WRITEBACK_SAVE=500
4628 DIRTY_RATIO_SAVE=40
4629 MAX_DIRTY_RATIO=50
4630 BG_DIRTY_RATIO_SAVE=10
4631 MAX_BG_DIRTY_RATIO=25
4632
4633 start_writeback() {
4634         trap 0
4635         # in 2.6, restore /proc/sys/vm/dirty_writeback_centisecs,
4636         # dirty_ratio, dirty_background_ratio
4637         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4638                 sysctl -w vm.dirty_writeback_centisecs=$WRITEBACK_SAVE
4639                 sysctl -w vm.dirty_background_ratio=$BG_DIRTY_RATIO_SAVE
4640                 sysctl -w vm.dirty_ratio=$DIRTY_RATIO_SAVE
4641         else
4642                 # if file not here, we are a 2.4 kernel
4643                 kill -CONT `pidof kupdated`
4644         fi
4645 }
4646
4647 stop_writeback() {
4648         # setup the trap first, so someone cannot exit the test at the
4649         # exact wrong time and mess up a machine
4650         trap start_writeback EXIT
4651         # in 2.6, save and 0 /proc/sys/vm/dirty_writeback_centisecs
4652         if [ -f /proc/sys/vm/dirty_writeback_centisecs ]; then
4653                 WRITEBACK_SAVE=`sysctl -n vm.dirty_writeback_centisecs`
4654                 sysctl -w vm.dirty_writeback_centisecs=0
4655                 sysctl -w vm.dirty_writeback_centisecs=0
4656                 # save and increase /proc/sys/vm/dirty_ratio
4657                 DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_ratio`
4658                 sysctl -w vm.dirty_ratio=$MAX_DIRTY_RATIO
4659                 # save and increase /proc/sys/vm/dirty_background_ratio
4660                 BG_DIRTY_RATIO_SAVE=`sysctl -n vm.dirty_background_ratio`
4661                 sysctl -w vm.dirty_background_ratio=$MAX_BG_DIRTY_RATIO
4662         else
4663                 # if file not here, we are a 2.4 kernel
4664                 kill -STOP `pidof kupdated`
4665         fi
4666 }
4667
4668 # ensure that all stripes have some grant before we test client-side cache
4669 setup_test42() {
4670         for i in `seq -f $DIR/f42-%g 1 $OSTCOUNT`; do
4671                 dd if=/dev/zero of=$i bs=4k count=1
4672                 rm $i
4673         done
4674 }
4675
4676 # Tests 42* verify that our behaviour is correct WRT caching, file closure,
4677 # file truncation, and file removal.
4678 test_42a() {
4679         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4680
4681         setup_test42
4682         cancel_lru_locks $OSC
4683         stop_writeback
4684         sync; sleep 1; sync # just to be safe
4685         BEFOREWRITES=`count_ost_writes`
4686         lctl get_param -n osc.*[oO][sS][cC][_-]*.cur_grant_bytes | grep "[0-9]"
4687         dd if=/dev/zero of=$DIR/f42a bs=1024 count=100
4688         AFTERWRITES=`count_ost_writes`
4689         [ $BEFOREWRITES -eq $AFTERWRITES ] || \
4690                 error "$BEFOREWRITES < $AFTERWRITES"
4691         start_writeback
4692 }
4693 run_test 42a "ensure that we don't flush on close"
4694
4695 test_42b() {
4696         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4697
4698         setup_test42
4699         cancel_lru_locks $OSC
4700         stop_writeback
4701         sync
4702         dd if=/dev/zero of=$DIR/f42b bs=1024 count=100
4703         BEFOREWRITES=$(count_ost_writes)
4704         $MUNLINK $DIR/f42b || error "$MUNLINK $DIR/f42b: $?"
4705         AFTERWRITES=$(count_ost_writes)
4706         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4707                 error "$BEFOREWRITES < $AFTERWRITES on unlink"
4708         fi
4709         BEFOREWRITES=$(count_ost_writes)
4710         sync || error "sync: $?"
4711         AFTERWRITES=$(count_ost_writes)
4712         if [[ $BEFOREWRITES -lt $AFTERWRITES ]]; then
4713                 error "$BEFOREWRITES < $AFTERWRITES on sync"
4714         fi
4715         dmesg | grep 'error from obd_brw_async' && error 'error writing back'
4716         start_writeback
4717         return 0
4718 }
4719 run_test 42b "test destroy of file with cached dirty data ======"
4720
4721 # if these tests just want to test the effect of truncation,
4722 # they have to be very careful.  consider:
4723 # - the first open gets a {0,EOF}PR lock
4724 # - the first write conflicts and gets a {0, count-1}PW
4725 # - the rest of the writes are under {count,EOF}PW
4726 # - the open for truncate tries to match a {0,EOF}PR
4727 #   for the filesize and cancels the PWs.
4728 # any number of fixes (don't get {0,EOF} on open, match
4729 # composite locks, do smarter file size management) fix
4730 # this, but for now we want these tests to verify that
4731 # the cancellation with truncate intent works, so we
4732 # start the file with a full-file pw lock to match against
4733 # until the truncate.
4734 trunc_test() {
4735         test=$1
4736         file=$DIR/$test
4737         offset=$2
4738         cancel_lru_locks $OSC
4739         stop_writeback
4740         # prime the file with 0,EOF PW to match
4741         touch $file
4742         $TRUNCATE $file 0
4743         sync; sync
4744         # now the real test..
4745         dd if=/dev/zero of=$file bs=1024 count=100
4746         BEFOREWRITES=`count_ost_writes`
4747         $TRUNCATE $file $offset
4748         cancel_lru_locks $OSC
4749         AFTERWRITES=`count_ost_writes`
4750         start_writeback
4751 }
4752
4753 test_42c() {
4754         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4755
4756         trunc_test 42c 1024
4757         [ $BEFOREWRITES -eq $AFTERWRITES ] &&
4758                 error "beforewrites $BEFOREWRITES == afterwrites $AFTERWRITES on truncate"
4759         rm $file
4760 }
4761 run_test 42c "test partial truncate of file with cached dirty data"
4762
4763 test_42d() {
4764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4765
4766         trunc_test 42d 0
4767         [ $BEFOREWRITES -eq $AFTERWRITES ] ||
4768                 error "beforewrites $BEFOREWRITES != afterwrites $AFTERWRITES on truncate"
4769         rm $file
4770 }
4771 run_test 42d "test complete truncate of file with cached dirty data"
4772
4773 test_42e() { # bug22074
4774         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4775
4776         local TDIR=$DIR/${tdir}e
4777         local pages=16 # hardcoded 16 pages, don't change it.
4778         local files=$((OSTCOUNT * 500)) # hopefully 500 files on each OST
4779         local proc_osc0="osc.${FSNAME}-OST0000-osc-[^MDT]*"
4780         local max_dirty_mb
4781         local warmup_files
4782
4783         test_mkdir $DIR/${tdir}e
4784         $LFS setstripe -c 1 $TDIR
4785         createmany -o $TDIR/f $files
4786
4787         max_dirty_mb=$($LCTL get_param -n $proc_osc0/max_dirty_mb)
4788
4789         # we assume that with $OSTCOUNT files, at least one of them will
4790         # be allocated on OST0.
4791         warmup_files=$((OSTCOUNT * max_dirty_mb))
4792         createmany -o $TDIR/w $warmup_files
4793
4794         # write a large amount of data into one file and sync, to get good
4795         # avail_grant number from OST.
4796         for ((i=0; i<$warmup_files; i++)); do
4797                 idx=$($LFS getstripe -i $TDIR/w$i)
4798                 [ $idx -ne 0 ] && continue
4799                 dd if=/dev/zero of=$TDIR/w$i bs="$max_dirty_mb"M count=1
4800                 break
4801         done
4802         [[ $i -gt $warmup_files ]] && error "OST0 is still cold"
4803         sync
4804         $LCTL get_param $proc_osc0/cur_dirty_bytes
4805         $LCTL get_param $proc_osc0/cur_grant_bytes
4806
4807         # create as much dirty pages as we can while not to trigger the actual
4808         # RPCs directly. but depends on the env, VFS may trigger flush during this
4809         # period, hopefully we are good.
4810         for ((i=0; i<$warmup_files; i++)); do
4811                 idx=$($LFS getstripe -i $TDIR/w$i)
4812                 [ $idx -ne 0 ] && continue
4813                 dd if=/dev/zero of=$TDIR/w$i bs=1M count=1 2>/dev/null
4814         done
4815         $LCTL get_param $proc_osc0/cur_dirty_bytes
4816         $LCTL get_param $proc_osc0/cur_grant_bytes
4817
4818         # perform the real test
4819         $LCTL set_param $proc_osc0/rpc_stats 0
4820         for ((;i<$files; i++)); do
4821                 [ $($LFS getstripe -i $TDIR/f$i) -eq 0 ] || continue
4822                 dd if=/dev/zero of=$TDIR/f$i bs=$PAGE_SIZE count=$pages 2>/dev/null
4823         done
4824         sync
4825         $LCTL get_param $proc_osc0/rpc_stats
4826
4827         local percent=0
4828         local have_ppr=false
4829         $LCTL get_param $proc_osc0/rpc_stats |
4830                 while read PPR RRPC RPCT RCUM BAR WRPC WPCT WCUM; do
4831                         # skip lines until we are at the RPC histogram data
4832                         [ "$PPR" == "pages" ] && have_ppr=true && continue
4833                         $have_ppr || continue
4834
4835                         # we only want the percent stat for < 16 pages
4836                         [[ $(echo $PPR | tr -d ':') -ge $pages ]] && break
4837
4838                         percent=$((percent + WPCT))
4839                         if [[ $percent -gt 15 ]]; then
4840                                 error "less than 16-pages write RPCs" \
4841                                       "$percent% > 15%"
4842                                 break
4843                         fi
4844                 done
4845         rm -rf $TDIR
4846 }
4847 run_test 42e "verify sub-RPC writes are not done synchronously"
4848
4849 test_43A() { # was test_43
4850         test_mkdir $DIR/$tdir
4851         cp -p /bin/ls $DIR/$tdir/$tfile
4852         $MULTIOP $DIR/$tdir/$tfile Ow_c &
4853         pid=$!
4854         # give multiop a chance to open
4855         sleep 1
4856
4857         $DIR/$tdir/$tfile && error "execute $DIR/$tdir/$tfile succeeded" || true
4858         kill -USR1 $pid
4859 }
4860 run_test 43A "execution of file opened for write should return -ETXTBSY"
4861
4862 test_43a() {
4863         test_mkdir $DIR/$tdir
4864         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4865         $DIR/$tdir/sleep 60 &
4866         SLEEP_PID=$!
4867         # Make sure exec of $tdir/sleep wins race with truncate
4868         sleep 1
4869         $MULTIOP $DIR/$tdir/sleep Oc && error "expected error, got success"
4870         kill $SLEEP_PID
4871 }
4872 run_test 43a "open(RDWR) of file being executed should return -ETXTBSY"
4873
4874 test_43b() {
4875         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4876
4877         test_mkdir $DIR/$tdir
4878         cp -p $(which sleep) $DIR/$tdir/sleep || error "can't copy"
4879         $DIR/$tdir/sleep 60 &
4880         SLEEP_PID=$!
4881         # Make sure exec of $tdir/sleep wins race with truncate
4882         sleep 1
4883         $TRUNCATE $DIR/$tdir/sleep 0 && error "expected error, got success"
4884         kill $SLEEP_PID
4885 }
4886 run_test 43b "truncate of file being executed should return -ETXTBSY"
4887
4888 test_43c() {
4889         local testdir="$DIR/$tdir"
4890         test_mkdir $testdir
4891         cp $SHELL $testdir/
4892         ( cd $(dirname $SHELL) && md5sum $(basename $SHELL) ) |
4893                 ( cd $testdir && md5sum -c )
4894 }
4895 run_test 43c "md5sum of copy into lustre"
4896
4897 test_44A() { # was test_44
4898         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
4899
4900         dd if=/dev/zero of=$DIR/f1 bs=4k count=1 seek=1023
4901         dd if=$DIR/f1 bs=4k count=1 > /dev/null
4902 }
4903 run_test 44A "zero length read from a sparse stripe"
4904
4905 test_44a() {
4906         local nstripe=$($LCTL lov_getconfig $DIR | grep default_stripe_count: |
4907                 awk '{ print $2 }')
4908         [ -z "$nstripe" ] && skip "can't get stripe info"
4909         [[ $nstripe -gt $OSTCOUNT ]] &&
4910                 skip "Wrong default_stripe_count: $nstripe OSTCOUNT: $OSTCOUNT"
4911
4912         local stride=$($LCTL lov_getconfig $DIR | grep default_stripe_size: |
4913                 awk '{ print $2 }')
4914         if [[ $nstripe -eq 0 || $nstripe -eq -1 ]]; then
4915                 nstripe=$($LCTL lov_getconfig $DIR | grep obd_count: |
4916                         awk '{ print $2 }')
4917         fi
4918
4919         OFFSETS="0 $((stride/2)) $((stride-1))"
4920         for offset in $OFFSETS; do
4921                 for i in $(seq 0 $((nstripe-1))); do
4922                         local GLOBALOFFSETS=""
4923                         # size in Bytes
4924                         local size=$((((i + 2 * $nstripe )*$stride + $offset)))
4925                         local myfn=$DIR/d44a-$size
4926                         echo "--------writing $myfn at $size"
4927                         ll_sparseness_write $myfn $size ||
4928                                 error "ll_sparseness_write"
4929                         GLOBALOFFSETS="$GLOBALOFFSETS $size"
4930                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4931                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4932
4933                         for j in $(seq 0 $((nstripe-1))); do
4934                                 # size in Bytes
4935                                 size=$((((j + $nstripe )*$stride + $offset)))
4936                                 ll_sparseness_write $myfn $size ||
4937                                         error "ll_sparseness_write"
4938                                 GLOBALOFFSETS="$GLOBALOFFSETS $size"
4939                         done
4940                         ll_sparseness_verify $myfn $GLOBALOFFSETS ||
4941                                 error "ll_sparseness_verify $GLOBALOFFSETS"
4942                         rm -f $myfn
4943                 done
4944         done
4945 }
4946 run_test 44a "test sparse pwrite ==============================="
4947
4948 dirty_osc_total() {
4949         tot=0
4950         for d in `lctl get_param -n ${OSC}.*.cur_dirty_bytes`; do
4951                 tot=$(($tot + $d))
4952         done
4953         echo $tot
4954 }
4955 do_dirty_record() {
4956         before=`dirty_osc_total`
4957         echo executing "\"$*\""
4958         eval $*
4959         after=`dirty_osc_total`
4960         echo before $before, after $after
4961 }
4962 test_45() {
4963         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4964
4965         f="$DIR/f45"
4966         # Obtain grants from OST if it supports it
4967         echo blah > ${f}_grant
4968         stop_writeback
4969         sync
4970         do_dirty_record "echo blah > $f"
4971         [[ $before -eq $after ]] && error "write wasn't cached"
4972         do_dirty_record "> $f"
4973         [[ $before -gt $after ]] || error "truncate didn't lower dirty count"
4974         do_dirty_record "echo blah > $f"
4975         [[ $before -eq $after ]] && error "write wasn't cached"
4976         do_dirty_record "sync"
4977         [[ $before -gt $after ]] || error "writeback didn't lower dirty count"
4978         do_dirty_record "echo blah > $f"
4979         [[ $before -eq $after ]] && error "write wasn't cached"
4980         do_dirty_record "cancel_lru_locks osc"
4981         [[ $before -gt $after ]] ||
4982                 error "lock cancellation didn't lower dirty count"
4983         start_writeback
4984 }
4985 run_test 45 "osc io page accounting ============================"
4986
4987 # in a 2 stripe file (lov.sh), page 1023 maps to page 511 in its object.  this
4988 # test tickles a bug where re-dirtying a page was failing to be mapped to the
4989 # objects offset and an assert hit when an rpc was built with 1023's mapped
4990 # offset 511 and 511's raw 511 offset. it also found general redirtying bugs.
4991 test_46() {
4992         [ $PARALLEL == "yes" ] && skip "skip parallel run"
4993
4994         f="$DIR/f46"
4995         stop_writeback
4996         sync
4997         dd if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
4998         sync
4999         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=1023 count=1
5000         dd conv=notrunc if=/dev/zero of=$f bs=$PAGE_SIZE seek=511 count=1
5001         sync
5002         start_writeback
5003 }
5004 run_test 46 "dirtying a previously written page ================"
5005
5006 # test_47 is removed "Device nodes check" is moved to test_28
5007
5008 test_48a() { # bug 2399
5009         [ "$mds1_FSTYPE" = "zfs" ] &&
5010         [ $MDS1_VERSION -lt $(version_code 2.3.63) ] &&
5011                 skip "MDS prior to 2.3.63 handle ZFS dir .. incorrectly"
5012
5013         test_mkdir $DIR/$tdir
5014         cd $DIR/$tdir
5015         mv $DIR/$tdir $DIR/$tdir.new || error "move directory failed"
5016         test_mkdir $DIR/$tdir
5017         touch foo || error "'touch foo' failed after recreating cwd"
5018         test_mkdir bar
5019         touch .foo || error "'touch .foo' failed after recreating cwd"
5020         test_mkdir .bar
5021         ls . > /dev/null || error "'ls .' failed after recreating cwd"
5022         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5023         cd . || error "'cd .' failed after recreating cwd"
5024         mkdir . && error "'mkdir .' worked after recreating cwd"
5025         rmdir . && error "'rmdir .' worked after recreating cwd"
5026         ln -s . baz || error "'ln -s .' failed after recreating cwd"
5027         cd .. || error "'cd ..' failed after recreating cwd"
5028 }
5029 run_test 48a "Access renamed working dir (should return errors)="
5030
5031 test_48b() { # bug 2399
5032         rm -rf $DIR/$tdir
5033         test_mkdir $DIR/$tdir
5034         cd $DIR/$tdir
5035         rmdir $DIR/$tdir || error "remove cwd $DIR/$tdir failed"
5036         touch foo && error "'touch foo' worked after removing cwd"
5037         mkdir foo && error "'mkdir foo' worked after removing cwd"
5038         touch .foo && error "'touch .foo' worked after removing cwd"
5039         mkdir .foo && error "'mkdir .foo' worked after removing cwd"
5040         ls . > /dev/null && error "'ls .' worked after removing cwd"
5041         ls .. > /dev/null || error "'ls ..' failed after removing cwd"
5042         mkdir . && error "'mkdir .' worked after removing cwd"
5043         rmdir . && error "'rmdir .' worked after removing cwd"
5044         ln -s . foo && error "'ln -s .' worked after removing cwd"
5045         cd .. || echo "'cd ..' failed after removing cwd `pwd`"  #bug 3517
5046 }
5047 run_test 48b "Access removed working dir (should return errors)="
5048
5049 test_48c() { # bug 2350
5050         #lctl set_param debug=-1
5051         #set -vx
5052         rm -rf $DIR/$tdir
5053         test_mkdir -p $DIR/$tdir/dir
5054         cd $DIR/$tdir/dir
5055         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5056         $TRACE touch foo && error "touch foo worked after removing cwd"
5057         $TRACE mkdir foo && error "'mkdir foo' worked after removing cwd"
5058         touch .foo && error "touch .foo worked after removing cwd"
5059         mkdir .foo && error "mkdir .foo worked after removing cwd"
5060         $TRACE ls . && error "'ls .' worked after removing cwd"
5061         $TRACE ls .. || error "'ls ..' failed after removing cwd"
5062         $TRACE mkdir . && error "'mkdir .' worked after removing cwd"
5063         $TRACE rmdir . && error "'rmdir .' worked after removing cwd"
5064         $TRACE ln -s . foo && error "'ln -s .' worked after removing cwd"
5065         $TRACE cd .. || echo "'cd ..' failed after removing cwd `pwd`" #bug 3415
5066 }
5067 run_test 48c "Access removed working subdir (should return errors)"
5068
5069 test_48d() { # bug 2350
5070         #lctl set_param debug=-1
5071         #set -vx
5072         rm -rf $DIR/$tdir
5073         test_mkdir -p $DIR/$tdir/dir
5074         cd $DIR/$tdir/dir
5075         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5076         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5077         $TRACE touch foo && error "'touch foo' worked after removing parent"
5078         $TRACE mkdir foo && error "mkdir foo worked after removing parent"
5079         touch .foo && error "'touch .foo' worked after removing parent"
5080         mkdir .foo && error "mkdir .foo worked after removing parent"
5081         $TRACE ls . && error "'ls .' worked after removing parent"
5082         $TRACE ls .. && error "'ls ..' worked after removing parent"
5083         $TRACE mkdir . && error "'mkdir .' worked after removing parent"
5084         $TRACE rmdir . && error "'rmdir .' worked after removing parent"
5085         $TRACE ln -s . foo && error "'ln -s .' worked after removing parent"
5086         true
5087 }
5088 run_test 48d "Access removed parent subdir (should return errors)"
5089
5090 test_48e() { # bug 4134
5091         #lctl set_param debug=-1
5092         #set -vx
5093         rm -rf $DIR/$tdir
5094         test_mkdir -p $DIR/$tdir/dir
5095         cd $DIR/$tdir/dir
5096         $TRACE rmdir $DIR/$tdir/dir || error "remove cwd $DIR/$tdir/dir failed"
5097         $TRACE rmdir $DIR/$tdir || error "remove parent $DIR/$tdir failed"
5098         $TRACE touch $DIR/$tdir || error "'touch $DIR/$tdir' failed"
5099         $TRACE chmod +x $DIR/$tdir || error "'chmod +x $DIR/$tdir' failed"
5100         # On a buggy kernel addition of "touch foo" after cd .. will
5101         # produce kernel oops in lookup_hash_it
5102         touch ../foo && error "'cd ..' worked after recreate parent"
5103         cd $DIR
5104         $TRACE rm $DIR/$tdir || error "rm '$DIR/$tdir' failed"
5105 }
5106 run_test 48e "Access to recreated parent subdir (should return errors)"
5107
5108 test_49() { # LU-1030
5109         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5110         remote_ost_nodsh && skip "remote OST with nodsh"
5111
5112         # get ost1 size - $FSNAME-OST0000
5113         ost1_size=$(do_facet ost1 $LFS df | grep ${ost1_svc} |
5114                 awk '{ print $4 }')
5115         # write 800M at maximum
5116         [[ $ost1_size -lt 2 ]] && ost1_size=2
5117         [[ $ost1_size -gt 819200 ]] && ost1_size=819200
5118
5119         $LFS setstripe -c 1 -i 0 $DIR/$tfile
5120         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((ost1_size >> 2)) &
5121         local dd_pid=$!
5122
5123         # change max_pages_per_rpc while writing the file
5124         local osc1_mppc=osc.$(get_osc_import_name client ost1).max_pages_per_rpc
5125         local orig_mppc=$($LCTL get_param -n $osc1_mppc)
5126         # loop until dd process exits
5127         while ps ax -opid | grep -wq $dd_pid; do
5128                 $LCTL set_param $osc1_mppc=$((RANDOM % 256 + 1))
5129                 sleep $((RANDOM % 5 + 1))
5130         done
5131         # restore original max_pages_per_rpc
5132         $LCTL set_param $osc1_mppc=$orig_mppc
5133         rm $DIR/$tfile || error "rm $DIR/$tfile failed"
5134 }
5135 run_test 49 "Change max_pages_per_rpc won't break osc extent"
5136
5137 test_50() {
5138         # bug 1485
5139         test_mkdir $DIR/$tdir
5140         cd $DIR/$tdir
5141         ls /proc/$$/cwd || error "ls /proc/$$/cwd failed"
5142 }
5143 run_test 50 "special situations: /proc symlinks  ==============="
5144
5145 test_51a() {    # was test_51
5146         # bug 1516 - create an empty entry right after ".." then split dir
5147         test_mkdir -c1 $DIR/$tdir
5148         touch $DIR/$tdir/foo
5149         $MCREATE $DIR/$tdir/bar
5150         rm $DIR/$tdir/foo
5151         createmany -m $DIR/$tdir/longfile 201
5152         FNUM=202
5153         while [[ $(ls -sd $DIR/$tdir | awk '{ print $1 }') -eq 4 ]]; do
5154                 $MCREATE $DIR/$tdir/longfile$FNUM
5155                 FNUM=$(($FNUM + 1))
5156                 echo -n "+"
5157         done
5158         echo
5159         ls -l $DIR/$tdir > /dev/null || error "ls -l $DIR/$tdir failed"
5160 }
5161 run_test 51a "special situations: split htree with empty entry =="
5162
5163 cleanup_print_lfs_df () {
5164         trap 0
5165         $LFS df
5166         $LFS df -i
5167 }
5168
5169 test_51b() {
5170         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5171
5172         local dir=$DIR/$tdir
5173         local nrdirs=$((65536 + 100))
5174
5175         # cleanup the directory
5176         rm -fr $dir
5177
5178         test_mkdir -c1 $dir
5179
5180         $LFS df
5181         $LFS df -i
5182         local mdtidx=$(printf "%04x" $($LFS getstripe -m $dir))
5183         local numfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.filesfree)
5184         [[ $numfree -lt $nrdirs ]] &&
5185                 skip "not enough free inodes ($numfree) on MDT$mdtidx"
5186
5187         # need to check free space for the directories as well
5188         local blkfree=$(lctl get_param -n mdc.$FSNAME-MDT$mdtidx*.kbytesavail)
5189         numfree=$(( blkfree / $(fs_inode_ksize) ))
5190         [[ $numfree -lt $nrdirs ]] && skip "not enough blocks ($numfree)"
5191
5192         trap cleanup_print_lfs_df EXIT
5193
5194         # create files
5195         createmany -d $dir/d $nrdirs || {
5196                 unlinkmany $dir/d $nrdirs
5197                 error "failed to create $nrdirs subdirs in MDT$mdtidx:$dir"
5198         }
5199
5200         # really created :
5201         nrdirs=$(ls -U $dir | wc -l)
5202
5203         # unlink all but 100 subdirectories, then check it still works
5204         local left=100
5205         local delete=$((nrdirs - left))
5206
5207         $LFS df
5208         $LFS df -i
5209
5210         # for ldiskfs the nlink count should be 1, but this is OSD specific
5211         # and so this is listed for informational purposes only
5212         echo "nlink before: $(stat -c %h $dir), created before: $nrdirs"
5213         unlinkmany -d $dir/d $delete ||
5214                 error "unlink of first $delete subdirs failed"
5215
5216         echo "nlink between: $(stat -c %h $dir)"
5217         local found=$(ls -U $dir | wc -l)
5218         [ $found -ne $left ] &&
5219                 error "can't find subdirs: found only $found, expected $left"
5220
5221         unlinkmany -d $dir/d $delete $left ||
5222                 error "unlink of second $left subdirs failed"
5223         # regardless of whether the backing filesystem tracks nlink accurately
5224         # or not, the nlink count shouldn't be more than "." and ".." here
5225         local after=$(stat -c %h $dir)
5226         [[ $after -gt 2 ]] && error "nlink after: $after > 2" ||
5227                 echo "nlink after: $after"
5228
5229         cleanup_print_lfs_df
5230 }
5231 run_test 51b "exceed 64k subdirectory nlink limit on create, verify unlink"
5232
5233 test_51d() {
5234         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5235         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
5236
5237         test_mkdir $DIR/$tdir
5238         createmany -o $DIR/$tdir/t- 1000
5239         $LFS getstripe $DIR/$tdir > $TMP/$tfile
5240         for N in $(seq 0 $((OSTCOUNT - 1))); do
5241                 OBJS[$N]=$(awk -vobjs=0 '($1 == '$N') { objs += 1 } \
5242                         END { printf("%0.0f", objs) }' $TMP/$tfile)
5243                 OBJS0[$N]=$(grep -A 1 idx $TMP/$tfile | awk -vobjs=0 \
5244                         '($1 == '$N') { objs += 1 } \
5245                         END { printf("%0.0f", objs) }')
5246                 log "OST$N has ${OBJS[$N]} objects, ${OBJS0[$N]} are index 0"
5247         done
5248         unlinkmany $DIR/$tdir/t- 1000
5249
5250         NLAST=0
5251         for N in $(seq 1 $((OSTCOUNT - 1))); do
5252                 [[ ${OBJS[$N]} -lt $((${OBJS[$NLAST]} - 20)) ]] &&
5253                         error "OST $N has less objects vs OST $NLAST" \
5254                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5255                 [[ ${OBJS[$N]} -gt $((${OBJS[$NLAST]} + 20)) ]] &&
5256                         error "OST $N has less objects vs OST $NLAST" \
5257                               " (${OBJS[$N]} < ${OBJS[$NLAST]}"
5258
5259                 [[ ${OBJS0[$N]} -lt $((${OBJS0[$NLAST]} - 20)) ]] &&
5260                         error "OST $N has less #0 objects vs OST $NLAST" \
5261                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5262                 [[ ${OBJS0[$N]} -gt $((${OBJS0[$NLAST]} + 20)) ]] &&
5263                         error "OST $N has less #0 objects vs OST $NLAST" \
5264                               " (${OBJS0[$N]} < ${OBJS0[$NLAST]}"
5265                 NLAST=$N
5266         done
5267         rm -f $TMP/$tfile
5268 }
5269 run_test 51d "check object distribution"
5270
5271 test_51e() {
5272         if [ "$mds1_FSTYPE" != ldiskfs ]; then
5273                 skip_env "ldiskfs only test"
5274         fi
5275
5276         test_mkdir -c1 $DIR/$tdir
5277         test_mkdir -c1 $DIR/$tdir/d0
5278
5279         touch $DIR/$tdir/d0/foo
5280         createmany -l $DIR/$tdir/d0/foo $DIR/$tdir/d0/f- 65001 &&
5281                 error "file exceed 65000 nlink limit!"
5282         unlinkmany $DIR/$tdir/d0/f- 65001
5283         return 0
5284 }
5285 run_test 51e "check file nlink limit"
5286
5287 test_51f() {
5288         test_mkdir $DIR/$tdir
5289
5290         local max=100000
5291         local ulimit_old=$(ulimit -n)
5292         local spare=20 # number of spare fd's for scripts/libraries, etc.
5293         local mdt=$($LFS getstripe -m $DIR/$tdir)
5294         local numfree=$($LFS df -i $DIR/$tdir | awk '/MDT:'$mdt'/ { print $4 }')
5295
5296         echo "MDT$mdt numfree=$numfree, max=$max"
5297         [[ $numfree -gt $max ]] && numfree=$max || numfree=$((numfree * 7 / 8))
5298         if [ $((numfree + spare)) -gt $ulimit_old ]; then
5299                 while ! ulimit -n $((numfree + spare)); do
5300                         numfree=$((numfree * 3 / 4))
5301                 done
5302                 echo "changed ulimit from $ulimit_old to $((numfree + spare))"
5303         else
5304                 echo "left ulimit at $ulimit_old"
5305         fi
5306
5307         createmany -o -k -t 120 $DIR/$tdir/f $numfree || {
5308                 unlinkmany $DIR/$tdir/f $numfree
5309                 error "create+open $numfree files in $DIR/$tdir failed"
5310         }
5311         ulimit -n $ulimit_old
5312
5313         # if createmany exits at 120s there will be fewer than $numfree files
5314         unlinkmany $DIR/$tdir/f $numfree || true
5315 }
5316 run_test 51f "check many open files limit"
5317
5318 test_52a() {
5319         [ -f $DIR/$tdir/foo ] && chattr -a $DIR/$tdir/foo
5320         test_mkdir $DIR/$tdir
5321         touch $DIR/$tdir/foo
5322         chattr +a $DIR/$tdir/foo || error "chattr +a failed"
5323         echo bar >> $DIR/$tdir/foo || error "append bar failed"
5324         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5325         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5326         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5327                                         error "link worked"
5328         echo foo >> $DIR/$tdir/foo || error "append foo failed"
5329         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5330         lsattr $DIR/$tdir/foo | egrep -q "^-+a[-e]+ $DIR/$tdir/foo" ||
5331                                                      error "lsattr"
5332         chattr -a $DIR/$tdir/foo || error "chattr -a failed"
5333         cp -r $DIR/$tdir $TMP/
5334         rm -fr $DIR/$tdir $TMP/$tdir || error "cleanup rm failed"
5335 }
5336 run_test 52a "append-only flag test (should return errors)"
5337
5338 test_52b() {
5339         [ -f $DIR/$tdir/foo ] && chattr -i $DIR/$tdir/foo
5340         test_mkdir $DIR/$tdir
5341         touch $DIR/$tdir/foo
5342         chattr +i $DIR/$tdir/foo || error "chattr +i failed"
5343         cat test > $DIR/$tdir/foo && error "cat test worked"
5344         cp /etc/hosts $DIR/$tdir/foo && error "cp worked"
5345         rm -f $DIR/$tdir/foo 2>/dev/null && error "rm worked"
5346         link $DIR/$tdir/foo $DIR/$tdir/foo_link 2>/dev/null &&
5347                                         error "link worked"
5348         echo foo >> $DIR/$tdir/foo && error "echo worked"
5349         mrename $DIR/$tdir/foo $DIR/$tdir/foo_ren && error "rename worked"
5350         [ -f $DIR/$tdir/foo ] || error "$tdir/foo is not a file"
5351         [ -f $DIR/$tdir/foo_ren ] && error "$tdir/foo_ren is not a file"
5352         lsattr $DIR/$tdir/foo | egrep -q "^-+i[-e]+ $DIR/$tdir/foo" ||
5353                                                         error "lsattr"
5354         chattr -i $DIR/$tdir/foo || error "chattr failed"
5355
5356         rm -fr $DIR/$tdir || error "unable to remove $DIR/$tdir"
5357 }
5358 run_test 52b "immutable flag test (should return errors) ======="
5359
5360 test_53() {
5361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5362         remote_mds_nodsh && skip "remote MDS with nodsh"
5363         remote_ost_nodsh && skip "remote OST with nodsh"
5364
5365         local param
5366         local param_seq
5367         local ostname
5368         local mds_last
5369         local mds_last_seq
5370         local ost_last
5371         local ost_last_seq
5372         local ost_last_id
5373         local ostnum
5374         local node
5375         local found=false
5376         local support_last_seq=true
5377
5378         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
5379                 support_last_seq=false
5380
5381         # only test MDT0000
5382         local mdtosc=$(get_mdtosc_proc_path $SINGLEMDS)
5383         local value
5384         for value in $(do_facet $SINGLEMDS \
5385                        $LCTL get_param osp.$mdtosc.prealloc_last_id) ; do
5386                 param=$(echo ${value[0]} | cut -d "=" -f1)
5387                 ostname=$(echo $param | cut -d "." -f2 | cut -d - -f 1-2)
5388
5389                 if $support_last_seq; then
5390                         param_seq=$(echo $param |
5391                                 sed -e s/prealloc_last_id/prealloc_last_seq/g)
5392                         mds_last_seq=$(do_facet $SINGLEMDS \
5393                                        $LCTL get_param -n $param_seq)
5394                 fi
5395                 mds_last=$(do_facet $SINGLEMDS $LCTL get_param -n $param)
5396
5397                 ostnum=$(index_from_ostuuid ${ostname}_UUID)
5398                 node=$(facet_active_host ost$((ostnum+1)))
5399                 param="obdfilter.$ostname.last_id"
5400                 for ost_last in $(do_node $node $LCTL get_param -n $param) ; do
5401                         echo "$ostname.last_id=$ost_last; MDS.last_id=$mds_last"
5402                         ost_last_id=$ost_last
5403
5404                         if $support_last_seq; then
5405                                 ost_last_id=$(echo $ost_last |
5406                                               awk -F':' '{print $2}' |
5407                                               sed -e "s/^0x//g")
5408                                 ost_last_seq=$(echo $ost_last |
5409                                                awk -F':' '{print $1}')
5410                                 [[ $ost_last_seq = $mds_last_seq ]] || continue
5411                         fi
5412
5413                         if [[ $ost_last_id != $mds_last ]]; then
5414                                 error "$ost_last_id != $mds_last"
5415                         else
5416                                 found=true
5417                                 break
5418                         fi
5419                 done
5420         done
5421         $found || error "can not match last_seq/last_id for $mdtosc"
5422         return 0
5423 }
5424 run_test 53 "verify that MDS and OSTs agree on pre-creation ===="
5425
5426 test_54a() {
5427         perl -MSocket -e ';' || skip "no Socket perl module installed"
5428
5429         $SOCKETSERVER $DIR/socket ||
5430                 error "$SOCKETSERVER $DIR/socket failed: $?"
5431         $SOCKETCLIENT $DIR/socket ||
5432                 error "$SOCKETCLIENT $DIR/socket failed: $?"
5433         $MUNLINK $DIR/socket || error "$MUNLINK $DIR/socket failed: $?"
5434 }
5435 run_test 54a "unix domain socket test =========================="
5436
5437 test_54b() {
5438         f="$DIR/f54b"
5439         mknod $f c 1 3
5440         chmod 0666 $f
5441         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1
5442 }
5443 run_test 54b "char device works in lustre ======================"
5444
5445 find_loop_dev() {
5446         [ -b /dev/loop/0 ] && LOOPBASE=/dev/loop/
5447         [ -b /dev/loop0 ] && LOOPBASE=/dev/loop
5448         [ -z "$LOOPBASE" ] && echo "/dev/loop/0 and /dev/loop0 gone?" && return
5449
5450         for i in $(seq 3 7); do
5451                 losetup $LOOPBASE$i > /dev/null 2>&1 && continue
5452                 LOOPDEV=$LOOPBASE$i
5453                 LOOPNUM=$i
5454                 break
5455         done
5456 }
5457
5458 cleanup_54c() {
5459         local rc=0
5460         loopdev="$DIR/loop54c"
5461
5462         trap 0
5463         $UMOUNT $DIR/$tdir || rc=$?
5464         losetup -d $loopdev || true
5465         losetup -d $LOOPDEV || true
5466         rm -rf $loopdev $DIR/$tfile $DIR/$tdir
5467         return $rc
5468 }
5469
5470 test_54c() {
5471         [ $PARALLEL == "yes" ] && skip "skip parallel run"
5472
5473         loopdev="$DIR/loop54c"
5474
5475         find_loop_dev
5476         [ -z "$LOOPNUM" ] && skip_env "couldn't find empty loop device"
5477         trap cleanup_54c EXIT
5478         mknod $loopdev b 7 $LOOPNUM
5479         echo "make a loop file system with $DIR/$tfile on $loopdev ($LOOPNUM)."
5480         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE seek=1024 count=1 > /dev/null
5481         losetup $loopdev $DIR/$tfile ||
5482                 error "can't set up $loopdev for $DIR/$tfile"
5483         mkfs.ext2 $loopdev || error "mke2fs on $loopdev"
5484         test_mkdir $DIR/$tdir
5485         mount -t ext2 $loopdev $DIR/$tdir ||
5486                 error "error mounting $loopdev on $DIR/$tdir"
5487         dd if=/dev/zero of=$DIR/$tdir/tmp bs=$PAGE_SIZE count=30 ||
5488                 error "dd write"
5489         df $DIR/$tdir
5490         dd if=$DIR/$tdir/tmp of=/dev/zero bs=$PAGE_SIZE count=30 ||
5491                 error "dd read"
5492         cleanup_54c
5493 }
5494 run_test 54c "block device works in lustre ====================="
5495
5496 test_54d() {
5497         f="$DIR/f54d"
5498         string="aaaaaa"
5499         mknod $f p
5500         [ "$string" = $(echo $string > $f | cat $f) ] || error "$f != $string"
5501 }
5502 run_test 54d "fifo device works in lustre ======================"
5503
5504 test_54e() {
5505         f="$DIR/f54e"
5506         string="aaaaaa"
5507         cp -aL /dev/console $f
5508         echo $string > $f || error "echo $string to $f failed"
5509 }
5510 run_test 54e "console/tty device works in lustre ======================"
5511
5512 test_56a() {
5513         local numfiles=3
5514         local dir=$DIR/$tdir
5515
5516         rm -rf $dir
5517         test_mkdir -p $dir/dir
5518         for i in $(seq $numfiles); do
5519                 touch $dir/file$i
5520                 touch $dir/dir/file$i
5521         done
5522
5523         local numcomp=$($LFS getstripe --component-count $dir)
5524
5525         [[ $numcomp == 0 ]] && numcomp=1
5526
5527         # test lfs getstripe with --recursive
5528         local filenum=$($LFS getstripe -r $dir | egrep -c "obdidx|l_ost_idx")
5529
5530         [[ $filenum -eq $((numfiles * 2)) ]] ||
5531                 error "$LFS getstripe -r: found $filenum != $((numfiles * 2))"
5532         filenum=$($LFS getstripe $dir | egrep -c "obdidx|l_ost_idx")
5533         [[ $filenum -eq $numfiles ]] ||
5534                 error "$LFS getstripe $dir: found $filenum, not $numfiles"
5535         echo "$LFS getstripe showed obdidx or l_ost_idx"
5536
5537         # test lfs getstripe with file instead of dir
5538         filenum=$($LFS getstripe $dir/file1 | egrep -c "obdidx|l_ost_idx")
5539         [[ $filenum -eq 1 ]] ||
5540                 error "$LFS getstripe $dir/file1: found $filenum, not 1"
5541         echo "$LFS getstripe file1 passed"
5542
5543         #test lfs getstripe with --verbose
5544         filenum=$($LFS getstripe --verbose $dir | grep -c lmm_magic)
5545         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5546                 error "$LFS getstripe --verbose $dir: "\
5547                       "got $filenum want $((numfiles * numcomp)) lmm_magic"
5548         [[ $($LFS getstripe $dir | grep -c lmm_magic) -eq 0 ]] ||
5549                 error "$LFS getstripe $dir: showed lmm_magic"
5550
5551         #test lfs getstripe with -v prints lmm_fid
5552         filenum=$($LFS getstripe -v $dir | grep -c lmm_fid)
5553         [[ $filenum -eq $((numfiles * numcomp)) ]] ||
5554                 error "$LFS getstripe -v $dir: "\
5555                       "got $filenum want $((numfiles * numcomp)) lmm_fid"
5556         [[ $($LFS getstripe $dir | grep -c lmm_fid) -eq 0 ]] ||
5557                 error "$LFS getstripe $dir: showed lmm_fid by default"
5558         echo "$LFS getstripe --verbose passed"
5559
5560         #check for FID information
5561         local fid1=$($LFS getstripe --fid $dir/file1)
5562         local fid2=$($LFS getstripe --verbose $dir/file1 |
5563                      awk '/lmm_fid: / { print $2; exit; }')
5564         local fid3=$($LFS path2fid $dir/file1)
5565
5566         [ "$fid1" != "$fid2" ] &&
5567                 error "getstripe --fid '$fid1' != getstripe --verbose '$fid2'"
5568         [ "$fid1" != "$fid3" ] &&
5569                 error "getstripe --fid '$fid1' != lfs path2fid '$fid3'"
5570         echo "$LFS getstripe --fid passed"
5571
5572         #test lfs getstripe with --obd
5573         $LFS getstripe --obd wrong_uuid $dir 2>&1 | grep -q "unknown obduuid" ||
5574                 error "$LFS getstripe --obd wrong_uuid: should return error"
5575
5576         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
5577
5578         local ostidx=1
5579         local obduuid=$(ostuuid_from_index $ostidx)
5580         local found=$($LFS getstripe -r --obd $obduuid $dir |
5581                 grep 'lmm_stripe_offset:' | grep -c " $ostidx\$")
5582
5583         filenum=$($LFS getstripe -ir $dir | grep -c "^$ostidx\$")
5584         [[ $($LFS getstripe -id $dir) -ne $ostidx ]] ||
5585                 ((filenum--))
5586         [[ $($LFS getstripe -id $dir/dir) -ne $ostidx ]] ||
5587                 ((filenum--))
5588
5589         [[ $found -eq $filenum ]] ||
5590                 error "$LFS getstripe --obd: found $found expect $filenum"
5591         [[ $($LFS getstripe -r -v --obd $obduuid $dir |
5592                 sed '/^[         ]*'${ostidx}'[  ]/d' |
5593                 sed -n '/^[      ]*[0-9][0-9]*[  ]/p' | wc -l) -eq 0 ]] ||
5594                 error "$LFS getstripe --obd: should not show file on other obd"
5595         echo "$LFS getstripe --obd passed"
5596 }
5597 run_test 56a "check $LFS getstripe"
5598
5599 test_56b() {
5600         local dir=$DIR/$tdir
5601         local numdirs=3
5602
5603         test_mkdir $dir
5604         for i in $(seq $numdirs); do
5605                 test_mkdir $dir/dir$i
5606         done
5607
5608         # test lfs getdirstripe default mode is non-recursion, which is
5609         # different from lfs getstripe
5610         local dircnt=$($LFS getdirstripe $dir | grep -c lmv_stripe_count)
5611
5612         [[ $dircnt -eq 1 ]] ||
5613                 error "$LFS getdirstripe: found $dircnt, not 1"
5614         dircnt=$($LFS getdirstripe --recursive $dir |
5615                 grep -c lmv_stripe_count)
5616         [[ $dircnt -eq $((numdirs + 1)) ]] ||
5617                 error "$LFS getdirstripe -r: $dircnt, != $((numdirs + 1))"
5618 }
5619 run_test 56b "check $LFS getdirstripe"
5620
5621 test_56c() {
5622         remote_ost_nodsh && skip "remote OST with nodsh"
5623
5624         local ost_idx=0
5625         local ost_name=$(ostname_from_index $ost_idx)
5626         local old_status=$(ost_dev_status $ost_idx)
5627
5628         [[ -z "$old_status" ]] ||
5629                 skip_env "OST $ost_name is in $old_status status"
5630
5631         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=1
5632         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5633                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=1
5634         sleep_maxage
5635
5636         local new_status=$(ost_dev_status $ost_idx)
5637
5638         [[ "$new_status" =~ "D" ]] ||
5639                 error "$ost_name status is '$new_status', missing 'D'"
5640         if [[ $OST1_VERSION -ge $(version_code 2.12.55) ]]; then
5641                 [[ "$new_status" =~ "N" ]] ||
5642                         error "$ost_name status is '$new_status', missing 'N'"
5643         fi
5644
5645         do_facet ost1 $LCTL set_param -n obdfilter.$ost_name.degraded=0
5646         [[ $OST1_VERSION -ge $(version_code 2.12.55) ]] && do_facet ost1 \
5647                 $LCTL set_param -n obdfilter.$ost_name.no_precreate=0
5648         sleep_maxage
5649
5650         new_status=$(ost_dev_status $ost_idx)
5651         [[ ! "$new_status" =~ "D" && ! "$new_status" =~ "N" ]] ||
5652                 error "$ost_name status is '$new_status', has 'D' and/or 'N'"
5653 }
5654 run_test 56c "check 'lfs df' showing device status"
5655
5656 NUMFILES=3
5657 NUMDIRS=3
5658 setup_56() {
5659         local local_tdir="$1"
5660         local local_numfiles="$2"
5661         local local_numdirs="$3"
5662         local dir_params="$4"
5663         local dir_stripe_params="$5"
5664
5665         if [ ! -d "$local_tdir" ] ; then
5666                 test_mkdir -p $dir_stripe_params $local_tdir
5667                 [ "$dir_params" ] && $LFS setstripe $dir_params $local_tdir
5668                 for i in $(seq $local_numfiles) ; do
5669                         touch $local_tdir/file$i
5670                 done
5671                 for i in $(seq $local_numdirs) ; do
5672                         test_mkdir $dir_stripe_params $local_tdir/dir$i
5673                         for j in $(seq $local_numfiles) ; do
5674                                 touch $local_tdir/dir$i/file$j
5675                         done
5676                 done
5677         fi
5678 }
5679
5680 setup_56_special() {
5681         local local_tdir=$1
5682         local local_numfiles=$2
5683         local local_numdirs=$3
5684
5685         setup_56 $local_tdir $local_numfiles $local_numdirs
5686
5687         if [ ! -e "$local_tdir/loop${local_numfiles}b" ] ; then
5688                 for i in $(seq $local_numfiles) ; do
5689                         mknod $local_tdir/loop${i}b b 7 $i
5690                         mknod $local_tdir/null${i}c c 1 3
5691                         ln -s $local_tdir/file1 $local_tdir/link${i}
5692                 done
5693                 for i in $(seq $local_numdirs) ; do
5694                         mknod $local_tdir/dir$i/loop${i}b b 7 $i
5695                         mknod $local_tdir/dir$i/null${i}c c 1 3
5696                         ln -s $local_tdir/dir$i/file1 $local_tdir/dir$i/link${i}
5697                 done
5698         fi
5699 }
5700
5701 test_56g() {
5702         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5703         local expected=$(($NUMDIRS + 2))
5704
5705         setup_56 $dir $NUMFILES $NUMDIRS
5706
5707         # test lfs find with -name
5708         for i in $(seq $NUMFILES) ; do
5709                 local nums=$($LFS find -name "*$i" $dir | wc -l)
5710
5711                 [ $nums -eq $expected ] ||
5712                         error "lfs find -name '*$i' $dir wrong: "\
5713                               "found $nums, expected $expected"
5714         done
5715 }
5716 run_test 56g "check lfs find -name"
5717
5718 test_56h() {
5719         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5720         local expected=$(((NUMDIRS + 1) * (NUMFILES - 1) + NUMFILES))
5721
5722         setup_56 $dir $NUMFILES $NUMDIRS
5723
5724         # test lfs find with ! -name
5725         for i in $(seq $NUMFILES) ; do
5726                 local nums=$($LFS find ! -name "*$i" $dir | wc -l)
5727
5728                 [ $nums -eq $expected ] ||
5729                         error "lfs find ! -name '*$i' $dir wrong: "\
5730                               "found $nums, expected $expected"
5731         done
5732 }
5733 run_test 56h "check lfs find ! -name"
5734
5735 test_56i() {
5736         local dir=$DIR/$tdir
5737
5738         test_mkdir $dir
5739
5740         local cmd="$LFS find -ost $(ostuuid_from_index 0 $dir) $dir"
5741         local out=$($cmd)
5742
5743         [ -z "$out" ] || error "'$cmd' returned directory '$out'"
5744 }
5745 run_test 56i "check 'lfs find -ost UUID' skips directories"
5746
5747 test_56j() {
5748         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5749
5750         setup_56_special $dir $NUMFILES $NUMDIRS
5751
5752         local expected=$((NUMDIRS + 1))
5753         local cmd="$LFS find -type d $dir"
5754         local nums=$($cmd | wc -l)
5755
5756         [ $nums -eq $expected ] ||
5757                 error "'$cmd' wrong: found $nums, expected $expected"
5758 }
5759 run_test 56j "check lfs find -type d"
5760
5761 test_56k() {
5762         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5763
5764         setup_56_special $dir $NUMFILES $NUMDIRS
5765
5766         local expected=$(((NUMDIRS + 1) * NUMFILES))
5767         local cmd="$LFS find -type f $dir"
5768         local nums=$($cmd | wc -l)
5769
5770         [ $nums -eq $expected ] ||
5771                 error "'$cmd' wrong: found $nums, expected $expected"
5772 }
5773 run_test 56k "check lfs find -type f"
5774
5775 test_56l() {
5776         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5777
5778         setup_56_special $dir $NUMFILES $NUMDIRS
5779
5780         local expected=$((NUMDIRS + NUMFILES))
5781         local cmd="$LFS find -type b $dir"
5782         local nums=$($cmd | wc -l)
5783
5784         [ $nums -eq $expected ] ||
5785                 error "'$cmd' wrong: found $nums, expected $expected"
5786 }
5787 run_test 56l "check lfs find -type b"
5788
5789 test_56m() {
5790         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5791
5792         setup_56_special $dir $NUMFILES $NUMDIRS
5793
5794         local expected=$((NUMDIRS + NUMFILES))
5795         local cmd="$LFS find -type c $dir"
5796         local nums=$($cmd | wc -l)
5797         [ $nums -eq $expected ] ||
5798                 error "'$cmd' wrong: found $nums, expected $expected"
5799 }
5800 run_test 56m "check lfs find -type c"
5801
5802 test_56n() {
5803         local dir=$DIR/d$(basetest $testnum)g.$TESTSUITE
5804         setup_56_special $dir $NUMFILES $NUMDIRS
5805
5806         local expected=$((NUMDIRS + NUMFILES))
5807         local cmd="$LFS find -type l $dir"
5808         local nums=$($cmd | wc -l)
5809
5810         [ $nums -eq $expected ] ||
5811                 error "'$cmd' wrong: found $nums, expected $expected"
5812 }
5813 run_test 56n "check lfs find -type l"
5814
5815 test_56o() {
5816         local dir=$DIR/$tdir
5817
5818         setup_56 $dir $NUMFILES $NUMDIRS
5819         utime $dir/file1 > /dev/null || error "utime (1)"
5820         utime $dir/file2 > /dev/null || error "utime (2)"
5821         utime $dir/dir1 > /dev/null || error "utime (3)"
5822         utime $dir/dir2 > /dev/null || error "utime (4)"
5823         utime $dir/dir1/file1 > /dev/null || error "utime (5)"
5824         dd if=/dev/zero count=1 >> $dir/dir1/file1 && sync
5825
5826         local expected=4
5827         local nums=$($LFS find -mtime +0 $dir | wc -l)
5828
5829         [ $nums -eq $expected ] ||
5830                 error "lfs find -mtime +0 $dir: found $nums expect $expected"
5831
5832         expected=12
5833         cmd="$LFS find -mtime 0 $dir"
5834         nums=$($cmd | wc -l)
5835         [ $nums -eq $expected ] ||
5836                 error "'$cmd' wrong: found $nums, expected $expected"
5837 }
5838 run_test 56o "check lfs find -mtime for old files"
5839
5840 test_56ob() {
5841         local dir=$DIR/$tdir
5842         local expected=1
5843         local count=0
5844
5845         # just to make sure there is something that won't be found
5846         test_mkdir $dir
5847         touch $dir/$tfile.now
5848
5849         for age in year week day hour min; do
5850                 count=$((count + 1))
5851
5852                 touch $dir/$tfile-a.$age $dir/$tfile-m.$age
5853                 touch --date="$count $age ago" -a $dir/$tfile-a.$age
5854                 touch --date="$count $age ago" -m $dir/$tfile-m.$age
5855
5856                 local cmd="$LFS find $dir -mtime $count${age:0:1}"
5857                 local nums=$($cmd | wc -l)
5858                 [ $nums -eq $expected ] ||
5859                         error "'$cmd' wrong: found $nums, expected $expected"
5860
5861                 cmd="$LFS find $dir -atime $count${age:0:1}"
5862                 nums=$($cmd | wc -l)
5863                 [ $nums -eq $expected ] ||
5864                         error "'$cmd' wrong: found $nums, expected $expected"
5865         done
5866
5867         sleep 2
5868         cmd="$LFS find $dir -ctime +1s -type f"
5869         nums=$($cmd | wc -l)
5870         (( $nums == $count * 2 + 1)) ||
5871                 error "'$cmd' wrong: found $nums, expected $((expected*2+1))"
5872 }
5873 run_test 56ob "check lfs find -atime -mtime -ctime with units"
5874
5875 test_newerXY_base() {
5876         local x=$1
5877         local y=$2
5878         local dir=$DIR/$tdir
5879         local ref
5880         local negref
5881
5882         if [ $y == "t" ]; then
5883                 if [ $x == "b" ]; then
5884                         ref="\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\""
5885                 else
5886                         ref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5887                 fi
5888         else
5889                 ref=$DIR/$tfile.newer.$x$y
5890                 touch $ref || error "touch $ref failed"
5891         fi
5892         sleep 2
5893         setup_56 $dir $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5894         sleep 2
5895         if [ $y == "t" ]; then
5896                 if [ $x == "b" ]; then
5897                         negref="\"$(do_facet mds1 date +"%Y-%m-%d\ %H:%M:%S")\""
5898                 else
5899                         negref="\"$(date +"%Y-%m-%d %H:%M:%S")\""
5900                 fi
5901         else
5902                 negref=$DIR/$tfile.negnewer.$x$y
5903                 touch $negref || error "touch $negref failed"
5904         fi
5905
5906         local cmd="$LFS find $dir -newer$x$y $ref"
5907         local nums=$(eval $cmd | wc -l)
5908         local expected=$(((NUMFILES + 2) * NUMDIRS + 1))
5909
5910         [ $nums -eq $expected ] ||
5911                 error "'$cmd' wrong: found $nums, expected $expected"
5912
5913         cmd="$LFS find $dir ! -newer$x$y $negref"
5914         nums=$(eval $cmd | wc -l)
5915         [ $nums -eq $expected ] ||
5916                 error "'$cmd' wrong: found $nums, expected $expected"
5917
5918         cmd="$LFS find $dir -newer$x$y $ref ! -newer$x$y $negref"
5919         nums=$(eval $cmd | wc -l)
5920         [ $nums -eq $expected ] ||
5921                 error "'$cmd' wrong: found $nums, expected $expected"
5922
5923         rm -rf $DIR/*
5924 }
5925
5926 test_56oc() {
5927         test_newerXY_base "b" "t"
5928         test_newerXY_base "a" "a"
5929         test_newerXY_base "a" "m"
5930         test_newerXY_base "a" "c"
5931         test_newerXY_base "m" "a"
5932         test_newerXY_base "m" "m"
5933         test_newerXY_base "m" "c"
5934         test_newerXY_base "c" "a"
5935         test_newerXY_base "c" "m"
5936         test_newerXY_base "c" "c"
5937         test_newerXY_base "b" "b"
5938         test_newerXY_base "a" "t"
5939         test_newerXY_base "m" "t"
5940         test_newerXY_base "c" "t"
5941         test_newerXY_base "b" "t"
5942 }
5943 run_test 56oc "check lfs find -newerXY work"
5944
5945 btime_supported() {
5946         local dir=$DIR/$tdir
5947         local rc
5948
5949         mkdir -p $dir
5950         touch $dir/$tfile
5951         $LFS find $dir -btime -1d -type f
5952         rc=$?
5953         rm -rf $dir
5954         return $rc
5955 }
5956
5957 test_56od() {
5958         [ $MDS1_VERSION -lt $(version_code 2.13.53) ] &&
5959                 ! btime_supported && skip "btime unsupported on MDS"
5960
5961         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
5962                 ! btime_supported && skip "btime unsupported on clients"
5963
5964         local dir=$DIR/$tdir
5965         local ref=$DIR/$tfile.ref
5966         local negref=$DIR/$tfile.negref
5967
5968         mkdir $dir || error "mkdir $dir failed"
5969         touch $dir/$tfile.n1 || error "touch $dir/$tfile.n1 failed"
5970         touch $dir/$tfile.n2 || error "touch $dir/$tfile.n2 failed"
5971         mkdir $dir/$tdir.n1 || error "mkdir $dir/$tdir.n1 failed"
5972         mkdir $dir/$tdir.n2 || error "mkdir $dir/$tdir.n2 failed"
5973         touch $ref || error "touch $ref failed"
5974         # sleep 3 seconds at least
5975         sleep 3
5976
5977         local before=$(do_facet mds1 date +%s)
5978         local skew=$(($(date +%s) - before + 1))
5979
5980         if (( skew < 0 && skew > -5 )); then
5981                 sleep $((0 - skew + 1))
5982                 skew=0
5983         fi
5984
5985         # Set the dir stripe params to limit files all on MDT0,
5986         # otherwise we need to calc the max clock skew between
5987         # the client and MDTs.
5988         setup_56 $dir/d.btime $NUMFILES $NUMDIRS "-i0 -c1" "-i0 -c1"
5989         sleep 2
5990         touch $negref || error "touch $negref failed"
5991
5992         local cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type f"
5993         local nums=$($cmd | wc -l)
5994         local expected=$(((NUMFILES + 1) * NUMDIRS))
5995
5996         [ $nums -eq $expected ] ||
5997                 error "'$cmd' wrong: found $nums, expected $expected"
5998
5999         cmd="$LFS find $dir -newerbb $ref ! -newerbb $negref -type d"
6000         nums=$($cmd | wc -l)
6001         expected=$((NUMFILES + 1))
6002         [ $nums -eq $expected ] ||
6003                 error "'$cmd' wrong: found $nums, expected $expected"
6004
6005         [ $skew -lt 0 ] && return
6006
6007         local after=$(do_facet mds1 date +%s)
6008         local age=$((after - before + 1 + skew))
6009
6010         cmd="$LFS find $dir -btime -${age}s -type f"
6011         nums=$($cmd | wc -l)
6012         expected=$(((NUMFILES + 1) * NUMDIRS))
6013
6014         echo "Clock skew between client and server: $skew, age:$age"
6015         [ $nums -eq $expected ] ||
6016                 error "'$cmd' wrong: found $nums, expected $expected"
6017
6018         expected=$(($NUMDIRS + 1))
6019         cmd="$LFS find $dir -btime -${age}s -type d"
6020         nums=$($cmd | wc -l)
6021         [ $nums -eq $expected ] ||
6022                 error "'$cmd' wrong: found $nums, expected $expected"
6023         rm -f $ref $negref || error "Failed to remove $ref $negref"
6024 }
6025 run_test 56od "check lfs find -btime with units"
6026
6027 test_56p() {
6028         [ $RUNAS_ID -eq $UID ] &&
6029                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6030
6031         local dir=$DIR/$tdir
6032
6033         setup_56 $dir $NUMFILES $NUMDIRS
6034         chown $RUNAS_ID $dir/file* || error "chown $DIR/${tdir}g/file$i failed"
6035
6036         local expected=$NUMFILES
6037         local cmd="$LFS find -uid $RUNAS_ID $dir"
6038         local nums=$($cmd | wc -l)
6039
6040         [ $nums -eq $expected ] ||
6041                 error "'$cmd' wrong: found $nums, expected $expected"
6042
6043         expected=$(((NUMFILES + 1) * NUMDIRS + 1))
6044         cmd="$LFS find ! -uid $RUNAS_ID $dir"
6045         nums=$($cmd | wc -l)
6046         [ $nums -eq $expected ] ||
6047                 error "'$cmd' wrong: found $nums, expected $expected"
6048 }
6049 run_test 56p "check lfs find -uid and ! -uid"
6050
6051 test_56q() {
6052         [ $RUNAS_ID -eq $UID ] &&
6053                 skip_env "RUNAS_ID = UID = $UID -- skipping"
6054
6055         local dir=$DIR/$tdir
6056
6057         setup_56 $dir $NUMFILES $NUMDIRS
6058         chgrp $RUNAS_GID $dir/file* || error "chown $dir/file$i failed"
6059
6060         local expected=$NUMFILES
6061         local cmd="$LFS find -gid $RUNAS_GID $dir"
6062         local nums=$($cmd | wc -l)
6063
6064         [ $nums -eq $expected ] ||
6065                 error "'$cmd' wrong: found $nums, expected $expected"
6066
6067         expected=$(( ($NUMFILES+1) * $NUMDIRS + 1))
6068         cmd="$LFS find ! -gid $RUNAS_GID $dir"
6069         nums=$($cmd | wc -l)
6070         [ $nums -eq $expected ] ||
6071                 error "'$cmd' wrong: found $nums, expected $expected"
6072 }
6073 run_test 56q "check lfs find -gid and ! -gid"
6074
6075 test_56r() {
6076         local dir=$DIR/$tdir
6077
6078         setup_56 $dir $NUMFILES $NUMDIRS
6079
6080         local expected=12
6081         local cmd="$LFS find -size 0 -type f -lazy $dir"
6082         local nums=$($cmd | wc -l)
6083
6084         [ $nums -eq $expected ] ||
6085                 error "'$cmd' wrong: found $nums, expected $expected"
6086         cmd="$LFS find -size 0 -type f $dir"
6087         nums=$($cmd | wc -l)
6088         [ $nums -eq $expected ] ||
6089                 error "'$cmd' wrong: found $nums, expected $expected"
6090
6091         expected=0
6092         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6093         nums=$($cmd | wc -l)
6094         [ $nums -eq $expected ] ||
6095                 error "'$cmd' wrong: found $nums, expected $expected"
6096         cmd="$LFS find ! -size 0 -type f $dir"
6097         nums=$($cmd | wc -l)
6098         [ $nums -eq $expected ] ||
6099                 error "'$cmd' wrong: found $nums, expected $expected"
6100
6101         echo "test" > $dir/$tfile
6102         echo "test2" > $dir/$tfile.2 && sync
6103         expected=1
6104         cmd="$LFS find -size 5 -type f -lazy $dir"
6105         nums=$($cmd | wc -l)
6106         [ $nums -eq $expected ] ||
6107                 error "'$cmd' wrong: found $nums, expected $expected"
6108         cmd="$LFS find -size 5 -type f $dir"
6109         nums=$($cmd | wc -l)
6110         [ $nums -eq $expected ] ||
6111                 error "'$cmd' wrong: found $nums, expected $expected"
6112
6113         expected=1
6114         cmd="$LFS find -size +5 -type f -lazy $dir"
6115         nums=$($cmd | wc -l)
6116         [ $nums -eq $expected ] ||
6117                 error "'$cmd' wrong: found $nums, expected $expected"
6118         cmd="$LFS find -size +5 -type f $dir"
6119         nums=$($cmd | wc -l)
6120         [ $nums -eq $expected ] ||
6121                 error "'$cmd' wrong: found $nums, expected $expected"
6122
6123         expected=2
6124         cmd="$LFS find -size +0 -type f -lazy $dir"
6125         nums=$($cmd | wc -l)
6126         [ $nums -eq $expected ] ||
6127                 error "'$cmd' wrong: found $nums, expected $expected"
6128         cmd="$LFS find -size +0 -type f $dir"
6129         nums=$($cmd | wc -l)
6130         [ $nums -eq $expected ] ||
6131                 error "'$cmd' wrong: found $nums, expected $expected"
6132
6133         expected=2
6134         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6135         nums=$($cmd | wc -l)
6136         [ $nums -eq $expected ] ||
6137                 error "'$cmd' wrong: found $nums, expected $expected"
6138         cmd="$LFS find ! -size -5 -type f $dir"
6139         nums=$($cmd | wc -l)
6140         [ $nums -eq $expected ] ||
6141                 error "'$cmd' wrong: found $nums, expected $expected"
6142
6143         expected=12
6144         cmd="$LFS find -size -5 -type f -lazy $dir"
6145         nums=$($cmd | wc -l)
6146         [ $nums -eq $expected ] ||
6147                 error "'$cmd' wrong: found $nums, expected $expected"
6148         cmd="$LFS find -size -5 -type f $dir"
6149         nums=$($cmd | wc -l)
6150         [ $nums -eq $expected ] ||
6151                 error "'$cmd' wrong: found $nums, expected $expected"
6152 }
6153 run_test 56r "check lfs find -size works"
6154
6155 test_56ra() {
6156         [[ $MDS1_VERSION -gt $(version_code 2.12.58) ]] ||
6157                 skip "MDS < 2.12.58 doesn't return LSOM data"
6158         local dir=$DIR/$tdir
6159
6160         [[ $OSC == "mdc" ]] && skip "DoM files" && return
6161
6162         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6163
6164         cancel_lru_locks $OSC
6165
6166         local rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6167         local expected=12
6168         local cmd="$LFS find -size 0 -type f -lazy $dir"
6169         local nums=$($cmd | wc -l)
6170
6171         [ $nums -eq $expected ] ||
6172                 error "'$cmd' wrong: found $nums, expected $expected"
6173
6174         local rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6175         [ $rpcs_before -eq $rpcs_after ] ||
6176                 error "'$cmd' should not send glimpse RPCs to OST"
6177         cmd="$LFS find -size 0 -type f $dir"
6178         nums=$($cmd | wc -l)
6179         [ $nums -eq $expected ] ||
6180                 error "'$cmd' wrong: found $nums, expected $expected"
6181         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6182         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6183         $LCTL get_param osc.*.stats
6184         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
6185                 error "'$cmd' should send 12 glimpse RPCs to OST"
6186
6187         cancel_lru_locks $OSC
6188         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6189         expected=0
6190         cmd="$LFS find ! -size 0 -type f -lazy $dir"
6191         nums=$($cmd | wc -l)
6192         [ $nums -eq $expected ] ||
6193                 error "'$cmd' wrong: found $nums, expected $expected"
6194         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6195         $LCTL get_param mdc.*.stats
6196         [ $rpcs_before -eq $rpcs_after ] ||
6197                 error "'$cmd' should not send glimpse RPCs to OST"
6198         cmd="$LFS find ! -size 0 -type f $dir"
6199         nums=$($cmd | wc -l)
6200         [ $nums -eq $expected ] ||
6201                 error "'$cmd' wrong: found $nums, expected $expected"
6202         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6203         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6204         [ $rpcs_after -eq $((rpcs_before + 12)) ] ||
6205                 error "'$cmd' should send 12 glimpse RPCs to OST"
6206
6207         echo "test" > $dir/$tfile
6208         echo "test2" > $dir/$tfile.2 && sync
6209         cancel_lru_locks $OSC
6210         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6211         expected=1
6212         cmd="$LFS find -size 5 -type f -lazy $dir"
6213         nums=$($cmd | wc -l)
6214         [ $nums -eq $expected ] ||
6215                 error "'$cmd' wrong: found $nums, expected $expected"
6216         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6217         [ $rpcs_before -eq $rpcs_after ] ||
6218                 error "'$cmd' should not send glimpse RPCs to OST"
6219         cmd="$LFS find -size 5 -type f $dir"
6220         nums=$($cmd | wc -l)
6221         [ $nums -eq $expected ] ||
6222                 error "'$cmd' wrong: found $nums, expected $expected"
6223         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6224         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6225         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6226                 error "'$cmd' should send 14 glimpse RPCs to OST"
6227
6228         cancel_lru_locks $OSC
6229         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6230         expected=1
6231         cmd="$LFS find -size +5 -type f -lazy $dir"
6232         nums=$($cmd | wc -l)
6233         [ $nums -eq $expected ] ||
6234                 error "'$cmd' wrong: found $nums, expected $expected"
6235         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6236         [ $rpcs_before -eq $rpcs_after ] ||
6237                 error "'$cmd' should not send glimpse RPCs to OST"
6238         cmd="$LFS find -size +5 -type f $dir"
6239         nums=$($cmd | wc -l)
6240         [ $nums -eq $expected ] ||
6241                 error "'$cmd' wrong: found $nums, expected $expected"
6242         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6243         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6244         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6245                 error "'$cmd' should send 14 glimpse RPCs to OST"
6246
6247         cancel_lru_locks $OSC
6248         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6249         expected=2
6250         cmd="$LFS find -size +0 -type f -lazy $dir"
6251         nums=$($cmd | wc -l)
6252         [ $nums -eq $expected ] ||
6253                 error "'$cmd' wrong: found $nums, expected $expected"
6254         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6255         [ $rpcs_before -eq $rpcs_after ] ||
6256                 error "'$cmd' should not send glimpse RPCs to OST"
6257         cmd="$LFS find -size +0 -type f $dir"
6258         nums=$($cmd | wc -l)
6259         [ $nums -eq $expected ] ||
6260                 error "'$cmd' wrong: found $nums, expected $expected"
6261         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6262         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6263         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6264                 error "'$cmd' should send 14 glimpse RPCs to OST"
6265
6266         cancel_lru_locks $OSC
6267         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6268         expected=2
6269         cmd="$LFS find ! -size -5 -type f -lazy $dir"
6270         nums=$($cmd | wc -l)
6271         [ $nums -eq $expected ] ||
6272                 error "'$cmd' wrong: found $nums, expected $expected"
6273         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6274         [ $rpcs_before -eq $rpcs_after ] ||
6275                 error "'$cmd' should not send glimpse RPCs to OST"
6276         cmd="$LFS find ! -size -5 -type f $dir"
6277         nums=$($cmd | wc -l)
6278         [ $nums -eq $expected ] ||
6279                 error "'$cmd' wrong: found $nums, expected $expected"
6280         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6281         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6282         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6283                 error "'$cmd' should send 14 glimpse RPCs to OST"
6284
6285         cancel_lru_locks $OSC
6286         rpcs_before=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6287         expected=12
6288         cmd="$LFS find -size -5 -type f -lazy $dir"
6289         nums=$($cmd | wc -l)
6290         [ $nums -eq $expected ] ||
6291                 error "'$cmd' wrong: found $nums, expected $expected"
6292         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6293         [ $rpcs_before -eq $rpcs_after ] ||
6294                 error "'$cmd' should not send glimpse RPCs to OST"
6295         cmd="$LFS find -size -5 -type f $dir"
6296         nums=$($cmd | wc -l)
6297         [ $nums -eq $expected ] ||
6298                 error "'$cmd' wrong: found $nums, expected $expected"
6299         rpcs_after=$(calc_stats $OSC.*$OSC*.stats ldlm_glimpse_enqueue)
6300         echo "Before: $rpcs_before After: $rpcs_after $NUMFILES"
6301         [ $rpcs_after -eq $((rpcs_before + 14)) ] ||
6302                 error "'$cmd' should send 14 glimpse RPCs to OST"
6303 }
6304 run_test 56ra "check lfs find -size -lazy works for data on OSTs"
6305
6306 test_56rb() {
6307         local dir=$DIR/$tdir
6308         local tmp=$TMP/$tfile.log
6309         local mdt_idx;
6310
6311         test_mkdir -p $dir || error "failed to mkdir $dir"
6312         $LFS setstripe -c 1 -i 0 $dir/$tfile ||
6313                 error "failed to setstripe $dir/$tfile"
6314         dd if=/dev/zero of=$dir/$tfile bs=1M count=1
6315
6316         stack_trap "rm -f $tmp" EXIT
6317         $LFS find --size +100K --ost 0 $dir 2>&1 | tee $tmp
6318         [ -z "$(cat $tmp | grep "obd_uuid: ")" ] ||
6319                 error "failed to find --size +100K --ost 0 $dir"
6320         $LFS find --size +100K --mdt $mdt_idx $dir 2>&1 | tee $tmp
6321         [ -z "$(cat $tmp | grep "obd_uuid: ")" ] ||
6322                 error "failed to find --size +100K --mdt $mdt_idx $dir"
6323 }
6324 run_test 56rb "check lfs find --size --ost/--mdt works"
6325
6326 test_56s() { # LU-611 #LU-9369
6327         [[ $OSTCOUNT -lt 2 ]] && skip_env "need at least 2 OSTs"
6328
6329         local dir=$DIR/$tdir
6330         local onestripe=$(((NUMDIRS + 1) * NUMFILES))
6331
6332         setup_56 $dir $NUMFILES $NUMDIRS "-c 1"
6333         for i in $(seq $NUMDIRS); do
6334                 $LFS setstripe -c $((OSTCOUNT + 1)) $dir/dir$i/$tfile
6335         done
6336
6337         local expected=$NUMDIRS
6338         local cmd="$LFS find -c $OSTCOUNT $dir"
6339         local nums=$($cmd | wc -l)
6340
6341         [ $nums -eq $expected ] || {
6342                 $LFS getstripe -R $dir
6343                 error "'$cmd' wrong: found $nums, expected $expected"
6344         }
6345
6346         expected=$((NUMDIRS + onestripe))
6347         cmd="$LFS find -stripe-count +0 -type f $dir"
6348         nums=$($cmd | wc -l)
6349         [ $nums -eq $expected ] || {
6350                 $LFS getstripe -R $dir
6351                 error "'$cmd' wrong: found $nums, expected $expected"
6352         }
6353
6354         expected=$onestripe
6355         cmd="$LFS find -stripe-count 1 -type f $dir"
6356         nums=$($cmd | wc -l)
6357         [ $nums -eq $expected ] || {
6358                 $LFS getstripe -R $dir
6359                 error "'$cmd' wrong: found $nums, expected $expected"
6360         }
6361
6362         cmd="$LFS find -stripe-count -2 -type f $dir"
6363         nums=$($cmd | wc -l)
6364         [ $nums -eq $expected ] || {
6365                 $LFS getstripe -R $dir
6366                 error "'$cmd' wrong: found $nums, expected $expected"
6367         }
6368
6369         expected=0
6370         cmd="$LFS find -stripe-count $((OSTCOUNT + 1)) -type f $dir"
6371         nums=$($cmd | wc -l)
6372         [ $nums -eq $expected ] || {
6373                 $LFS getstripe -R $dir
6374                 error "'$cmd' wrong: found $nums, expected $expected"
6375         }
6376 }
6377 run_test 56s "check lfs find -stripe-count works"
6378
6379 test_56t() { # LU-611 #LU-9369
6380         local dir=$DIR/$tdir
6381
6382         setup_56 $dir 0 $NUMDIRS
6383         for i in $(seq $NUMDIRS); do
6384                 $LFS setstripe -S 8M $dir/dir$i/$tfile
6385         done
6386
6387         local expected=$NUMDIRS
6388         local cmd="$LFS find -S 8M $dir"
6389         local nums=$($cmd | wc -l)
6390
6391         [ $nums -eq $expected ] || {
6392                 $LFS getstripe -R $dir
6393                 error "'$cmd' wrong: found $nums, expected $expected"
6394         }
6395         rm -rf $dir
6396
6397         setup_56 $dir $NUMFILES $NUMDIRS "--stripe-size 512k"
6398
6399         $LFS setstripe -S 256k $dir/$tfile.{0,1,2,3}
6400
6401         expected=$(((NUMDIRS + 1) * NUMFILES))
6402         cmd="$LFS find -stripe-size 512k -type f $dir"
6403         nums=$($cmd | wc -l)
6404         [ $nums -eq $expected ] ||
6405                 error "'$cmd' wrong: found $nums, expected $expected"
6406
6407         cmd="$LFS find -stripe-size +320k -type f $dir"
6408         nums=$($cmd | wc -l)
6409         [ $nums -eq $expected ] ||
6410                 error "'$cmd' wrong: found $nums, expected $expected"
6411
6412         expected=$(((NUMDIRS + 1) * NUMFILES + 4))
6413         cmd="$LFS find -stripe-size +200k -type f $dir"
6414         nums=$($cmd | wc -l)
6415         [ $nums -eq $expected ] ||
6416                 error "'$cmd' wrong: found $nums, expected $expected"
6417
6418         cmd="$LFS find -stripe-size -640k -type f $dir"
6419         nums=$($cmd | wc -l)
6420         [ $nums -eq $expected ] ||
6421                 error "'$cmd' wrong: found $nums, expected $expected"
6422
6423         expected=4
6424         cmd="$LFS find -stripe-size 256k -type f $dir"
6425         nums=$($cmd | wc -l)
6426         [ $nums -eq $expected ] ||
6427                 error "'$cmd' wrong: found $nums, expected $expected"
6428
6429         cmd="$LFS find -stripe-size -320k -type f $dir"
6430         nums=$($cmd | wc -l)
6431         [ $nums -eq $expected ] ||
6432                 error "'$cmd' wrong: found $nums, expected $expected"
6433
6434         expected=0
6435         cmd="$LFS find -stripe-size 1024k -type f $dir"
6436         nums=$($cmd | wc -l)
6437         [ $nums -eq $expected ] ||
6438                 error "'$cmd' wrong: found $nums, expected $expected"
6439 }
6440 run_test 56t "check lfs find -stripe-size works"
6441
6442 test_56u() { # LU-611
6443         local dir=$DIR/$tdir
6444
6445         setup_56 $dir $NUMFILES $NUMDIRS "-i 0 -c 1"
6446
6447         if [[ $OSTCOUNT -gt 1 ]]; then
6448                 $LFS setstripe -i 1 -c 1 $dir/$tfile.{0,1,2,3}
6449                 onestripe=4
6450         else
6451                 onestripe=0
6452         fi
6453
6454         local expected=$(((NUMDIRS + 1) * NUMFILES))
6455         local cmd="$LFS find -stripe-index 0 -type f $dir"
6456         local nums=$($cmd | wc -l)
6457
6458         [ $nums -eq $expected ] ||
6459                 error "'$cmd' wrong: found $nums, expected $expected"
6460
6461         expected=$onestripe
6462         cmd="$LFS find -stripe-index 1 -type f $dir"
6463         nums=$($cmd | wc -l)
6464         [ $nums -eq $expected ] ||
6465                 error "'$cmd' wrong: found $nums, expected $expected"
6466
6467         cmd="$LFS find ! -stripe-index 0 -type f $dir"
6468         nums=$($cmd | wc -l)
6469         [ $nums -eq $expected ] ||
6470                 error "'$cmd' wrong: found $nums, expected $expected"
6471
6472         expected=0
6473         # This should produce an error and not return any files
6474         cmd="$LFS find -stripe-index $OSTCOUNT -type f $dir"
6475         nums=$($cmd 2>/dev/null | wc -l)
6476         [ $nums -eq $expected ] ||
6477                 error "'$cmd' wrong: found $nums, expected $expected"
6478
6479         if [[ $OSTCOUNT -gt 1 ]]; then
6480                 expected=$(((NUMDIRS + 1) * NUMFILES + onestripe))
6481                 cmd="$LFS find -stripe-index 0,1 -type f $dir"
6482                 nums=$($cmd | wc -l)
6483                 [ $nums -eq $expected ] ||
6484                         error "'$cmd' wrong: found $nums, expected $expected"
6485         fi
6486 }
6487 run_test 56u "check lfs find -stripe-index works"
6488
6489 test_56v() {
6490         local mdt_idx=0
6491         local dir=$DIR/$tdir
6492
6493         setup_56 $dir $NUMFILES $NUMDIRS
6494
6495         UUID=$(mdtuuid_from_index $mdt_idx $dir)
6496         [ -z "$UUID" ] && error "mdtuuid_from_index cannot find MDT $mdt_idx"
6497
6498         for file in $($LFS find -m $UUID $dir); do
6499                 file_midx=$($LFS getstripe -m $file)
6500                 [ $file_midx -eq $mdt_idx ] ||
6501                         error "lfs find -m $UUID != getstripe -m $file_midx"
6502         done
6503 }
6504 run_test 56v "check 'lfs find -m match with lfs getstripe -m'"
6505
6506 test_56w() {
6507         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6508         [ $PARALLEL == "yes" ] && skip "skip parallel run"
6509
6510         local dir=$DIR/$tdir
6511
6512         setup_56 $dir $NUMFILES $NUMDIRS "-c $OSTCOUNT" "-c1"
6513
6514         local stripe_size=$($LFS getstripe -S -d $dir) ||
6515                 error "$LFS getstripe -S -d $dir failed"
6516         stripe_size=${stripe_size%% *}
6517
6518         local file_size=$((stripe_size * OSTCOUNT))
6519         local file_num=$((NUMDIRS * NUMFILES + NUMFILES))
6520         local required_space=$((file_num * file_size))
6521         local free_space=$($LCTL get_param -n lov.$FSNAME-clilov-*.kbytesavail |
6522                            head -n1)
6523         [[ $free_space -le $((required_space / 1024)) ]] &&
6524                 skip_env "need $required_space, have $free_space kbytes"
6525
6526         local dd_bs=65536
6527         local dd_count=$((file_size / dd_bs))
6528
6529         # write data into the files
6530         local i
6531         local j
6532         local file
6533
6534         for i in $(seq $NUMFILES); do
6535                 file=$dir/file$i
6536                 yes | dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6537                         error "write data into $file failed"
6538         done
6539         for i in $(seq $NUMDIRS); do
6540                 for j in $(seq $NUMFILES); do
6541                         file=$dir/dir$i/file$j
6542                         yes|dd bs=$dd_bs count=$dd_count of=$file &>/dev/null ||
6543                                 error "write data into $file failed"
6544                 done
6545         done
6546
6547         # $LFS_MIGRATE will fail if hard link migration is unsupported
6548         if [[ $MDS1_VERSION -gt $(version_code 2.5.55) ]]; then
6549                 createmany -l$dir/dir1/file1 $dir/dir1/link 200 ||
6550                         error "creating links to $dir/dir1/file1 failed"
6551         fi
6552
6553         local expected=-1
6554
6555         [[ $OSTCOUNT -gt 1 ]] && expected=$((OSTCOUNT - 1))
6556
6557         # lfs_migrate file
6558         local cmd="$LFS_MIGRATE -y -c $expected $dir/file1"
6559
6560         echo "$cmd"
6561         eval $cmd || error "$cmd failed"
6562
6563         check_stripe_count $dir/file1 $expected
6564
6565         if [ $MDS1_VERSION -ge $(version_code 2.6.90) ];
6566         then
6567                 # lfs_migrate file onto OST 0 if it is on OST 1, or onto
6568                 # OST 1 if it is on OST 0. This file is small enough to
6569                 # be on only one stripe.
6570                 file=$dir/migr_1_ost
6571                 dd bs=$dd_bs count=1 if=/dev/urandom of=$file >/dev/null 2>&1 ||
6572                         error "write data into $file failed"
6573                 local obdidx=$($LFS getstripe -i $file)
6574                 local oldmd5=$(md5sum $file)
6575                 local newobdidx=0
6576
6577                 [[ $obdidx -eq 0 ]] && newobdidx=1
6578                 cmd="$LFS migrate -i $newobdidx $file"
6579                 echo $cmd
6580                 eval $cmd || error "$cmd failed"
6581
6582                 local realobdix=$($LFS getstripe -i $file)
6583                 local newmd5=$(md5sum $file)
6584
6585                 [[ $newobdidx -ne $realobdix ]] &&
6586                         error "new OST is different (was=$obdidx, "\
6587                               "wanted=$newobdidx, got=$realobdix)"
6588                 [[ "$oldmd5" != "$newmd5" ]] &&
6589                         error "md5sum differ: $oldmd5, $newmd5"
6590         fi
6591
6592         # lfs_migrate dir
6593         cmd="$LFS_MIGRATE -y -c $expected $dir/dir1"
6594         echo "$cmd"
6595         eval $cmd || error "$cmd failed"
6596
6597         for j in $(seq $NUMFILES); do
6598                 check_stripe_count $dir/dir1/file$j $expected
6599         done
6600
6601         # lfs_migrate works with lfs find
6602         cmd="$LFS find -stripe_count $OSTCOUNT -type f $dir |
6603              $LFS_MIGRATE -y -c $expected"
6604         echo "$cmd"
6605         eval $cmd || error "$cmd failed"
6606
6607         for i in $(seq 2 $NUMFILES); do
6608                 check_stripe_count $dir/file$i $expected
6609         done
6610         for i in $(seq 2 $NUMDIRS); do
6611                 for j in $(seq $NUMFILES); do
6612                 check_stripe_count $dir/dir$i/file$j $expected
6613                 done
6614         done
6615 }
6616 run_test 56w "check lfs_migrate -c stripe_count works"
6617
6618 test_56wb() {
6619         local file1=$DIR/$tdir/file1
6620         local create_pool=false
6621         local initial_pool=$($LFS getstripe -p $DIR)
6622         local pool_list=()
6623         local pool=""
6624
6625         echo -n "Creating test dir..."
6626         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6627         echo "done."
6628
6629         echo -n "Creating test file..."
6630         touch $file1 || error "cannot create file"
6631         echo "done."
6632
6633         echo -n "Detecting existing pools..."
6634         pool_list=($($LFS pool_list $FSNAME | grep "$FSNAME\." | cut -d. -f2))
6635
6636         if [ ${#pool_list[@]} -gt 0 ]; then
6637                 echo "${pool_list[@]}"
6638                 for thispool in "${pool_list[@]}"; do
6639                         if [[ -z "$initial_pool" ||
6640                               "$initial_pool" != "$thispool" ]]; then
6641                                 pool="$thispool"
6642                                 echo "Using existing pool '$pool'"
6643                                 break
6644                         fi
6645                 done
6646         else
6647                 echo "none detected."
6648         fi
6649         if [ -z "$pool" ]; then
6650                 pool=${POOL:-testpool}
6651                 [ "$initial_pool" = "$pool" ] && pool="testpool2"
6652                 echo -n "Creating pool '$pool'..."
6653                 create_pool=true
6654                 pool_add $pool &> /dev/null ||
6655                         error "pool_add failed"
6656                 echo "done."
6657
6658                 echo -n "Adding target to pool..."
6659                 pool_add_targets $pool 0 0 1 &> /dev/null ||
6660                         error "pool_add_targets failed"
6661                 echo "done."
6662         fi
6663
6664         echo -n "Setting pool using -p option..."
6665         $LFS_MIGRATE -y -q --no-rsync -p $pool $file1 &> /dev/null ||
6666                 error "migrate failed rc = $?"
6667         echo "done."
6668
6669         echo -n "Verifying test file is in pool after migrating..."
6670         [ "$($LFS getstripe -p $file1)" = $pool ] ||
6671                 error "file was not migrated to pool $pool"
6672         echo "done."
6673
6674         echo -n "Removing test file from pool '$pool'..."
6675         # "lfs migrate $file" won't remove the file from the pool
6676         # until some striping information is changed.
6677         $LFS migrate -c 1 $file1 &> /dev/null ||
6678                 error "cannot remove from pool"
6679         [ "$($LFS getstripe -p $file1)" ] &&
6680                 error "pool still set"
6681         echo "done."
6682
6683         echo -n "Setting pool using --pool option..."
6684         $LFS_MIGRATE -y -q --no-rsync --pool $pool $file1 &> /dev/null ||
6685                 error "migrate failed rc = $?"
6686         echo "done."
6687
6688         # Clean up
6689         rm -f $file1
6690         if $create_pool; then
6691                 destroy_test_pools 2> /dev/null ||
6692                         error "destroy test pools failed"
6693         fi
6694 }
6695 run_test 56wb "check lfs_migrate pool support"
6696
6697 test_56wc() {
6698         local file1="$DIR/$tdir/file1"
6699         local parent_ssize
6700         local parent_scount
6701         local cur_ssize
6702         local cur_scount
6703         local orig_ssize
6704
6705         echo -n "Creating test dir..."
6706         test_mkdir $DIR/$tdir &> /dev/null || error "cannot create dir"
6707         $LFS setstripe -S 1M -c 1 "$DIR/$tdir" &> /dev/null ||
6708                 error "cannot set stripe by '-S 1M -c 1'"
6709         echo "done"
6710
6711         echo -n "Setting initial stripe for test file..."
6712         $LFS setstripe -S 512K -c 1 "$file1" &> /dev/null ||
6713                 error "cannot set stripe"
6714         cur_ssize=$($LFS getstripe -S "$file1")
6715         [ $cur_ssize -eq 524288 ] || error "setstripe -S $cur_ssize != 524288"
6716         echo "done."
6717
6718         # File currently set to -S 512K -c 1
6719
6720         # Ensure -c and -S options are rejected when -R is set
6721         echo -n "Verifying incompatible options are detected..."
6722         $LFS_MIGRATE -y -R -c 1 "$file1" &> /dev/null &&
6723                 error "incompatible -c and -R options not detected"
6724         $LFS_MIGRATE -y -R -S 1M "$file1" &> /dev/null &&
6725                 error "incompatible -S and -R options not detected"
6726         echo "done."
6727
6728         # Ensure unrecognized options are passed through to 'lfs migrate'
6729         echo -n "Verifying -S option is passed through to lfs migrate..."
6730         $LFS_MIGRATE -y -S 1M "$file1" &> /dev/null ||
6731                 error "migration failed"
6732         cur_ssize=$($LFS getstripe -S "$file1")
6733         [ $cur_ssize -eq 1048576 ] || error "migrate -S $cur_ssize != 1048576"
6734         echo "done."
6735
6736         # File currently set to -S 1M -c 1
6737
6738         # Ensure long options are supported
6739         echo -n "Verifying long options supported..."
6740         $LFS_MIGRATE -y --non-block "$file1" &> /dev/null ||
6741                 error "long option without argument not supported"
6742         $LFS_MIGRATE -y --stripe-size 512K "$file1" &> /dev/null ||
6743                 error "long option with argument not supported"
6744         cur_ssize=$($LFS getstripe -S "$file1")
6745         [ $cur_ssize -eq 524288 ] ||
6746                 error "migrate --stripe-size $cur_ssize != 524288"
6747         echo "done."
6748
6749         # File currently set to -S 512K -c 1
6750
6751         if [ "$OSTCOUNT" -gt 1 ]; then
6752                 echo -n "Verifying explicit stripe count can be set..."
6753                 $LFS_MIGRATE -y -c 2 "$file1" &> /dev/null ||
6754                         error "migrate failed"
6755                 cur_scount=$($LFS getstripe -c "$file1")
6756                 [ $cur_scount -eq 2 ] || error "migrate -c $cur_scount != 2"
6757                 echo "done."
6758         fi
6759
6760         # File currently set to -S 512K -c 1 or -S 512K -c 2
6761
6762         # Ensure parent striping is used if -R is set, and no stripe
6763         # count or size is specified
6764         echo -n "Setting stripe for parent directory..."
6765         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6766                 error "cannot set stripe '-S 2M -c 1'"
6767         echo "done."
6768
6769         echo -n "Verifying restripe option uses parent stripe settings..."
6770         parent_ssize=$($LFS getstripe -S $DIR/$tdir 2>/dev/null)
6771         parent_scount=$($LFS getstripe -c $DIR/$tdir 2>/dev/null)
6772         $LFS_MIGRATE -y -R "$file1" &> /dev/null ||
6773                 error "migrate failed"
6774         cur_ssize=$($LFS getstripe -S "$file1")
6775         [ $cur_ssize -eq $parent_ssize ] ||
6776                 error "migrate -R stripe_size $cur_ssize != $parent_ssize"
6777         cur_scount=$($LFS getstripe -c "$file1")
6778         [ $cur_scount -eq $parent_scount ] ||
6779                 error "migrate -R stripe_count $cur_scount != $parent_scount"
6780         echo "done."
6781
6782         # File currently set to -S 1M -c 1
6783
6784         # Ensure striping is preserved if -R is not set, and no stripe
6785         # count or size is specified
6786         echo -n "Verifying striping size preserved when not specified..."
6787         orig_ssize=$($LFS getstripe -S "$file1" 2>/dev/null)
6788         $LFS setstripe -S 2M -c 1 "$DIR/$tdir" &> /dev/null ||
6789                 error "cannot set stripe on parent directory"
6790         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6791                 error "migrate failed"
6792         cur_ssize=$($LFS getstripe -S "$file1")
6793         [ $cur_ssize -eq $orig_ssize ] ||
6794                 error "migrate by default $cur_ssize != $orig_ssize"
6795         echo "done."
6796
6797         # Ensure file name properly detected when final option has no argument
6798         echo -n "Verifying file name properly detected..."
6799         $LFS_MIGRATE -y "$file1" &> /dev/null ||
6800                 error "file name interpreted as option argument"
6801         echo "done."
6802
6803         # Clean up
6804         rm -f "$file1"
6805 }
6806 run_test 56wc "check unrecognized options for lfs_migrate are passed through"
6807
6808 test_56wd() {
6809         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6810
6811         local file1=$DIR/$tdir/file1
6812
6813         echo -n "Creating test dir..."
6814         test_mkdir $DIR/$tdir || error "cannot create dir"
6815         echo "done."
6816
6817         echo -n "Creating test file..."
6818         touch $file1
6819         echo "done."
6820
6821         # Ensure 'lfs migrate' will fail by using a non-existent option,
6822         # and make sure rsync is not called to recover
6823         echo -n "Make sure --no-rsync option works..."
6824         $LFS_MIGRATE -y --no-rsync --invalid-opt $file1 2>&1 |
6825                 grep -q 'refusing to fall back to rsync' ||
6826                 error "rsync was called with --no-rsync set"
6827         echo "done."
6828
6829         # Ensure rsync is called without trying 'lfs migrate' first
6830         echo -n "Make sure --rsync option works..."
6831         $LFS_MIGRATE -y --rsync --invalid-opt $file1 2>&1 |
6832                 grep -q 'falling back to rsync' &&
6833                 error "lfs migrate was called with --rsync set"
6834         echo "done."
6835
6836         echo -n "Make sure --rsync and --no-rsync options are exclusive..."
6837         $LFS_MIGRATE -y --rsync --no-rsync $file1 2>&1 |
6838                 grep -q 'at the same time' ||
6839                 error "--rsync and --no-rsync accepted concurrently"
6840         echo "done."
6841
6842         # Clean up
6843         rm -f $file1
6844 }
6845 run_test 56wd "check lfs_migrate --rsync and --no-rsync work"
6846
6847 test_56x() {
6848         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6849         check_swap_layouts_support
6850
6851         local dir=$DIR/$tdir
6852         local ref1=/etc/passwd
6853         local file1=$dir/file1
6854
6855         test_mkdir $dir || error "creating dir $dir"
6856         $LFS setstripe -c 2 $file1
6857         cp $ref1 $file1
6858         $LFS migrate -c 1 $file1 || error "migrate failed rc = $?"
6859         stripe=$($LFS getstripe -c $file1)
6860         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6861         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6862
6863         # clean up
6864         rm -f $file1
6865 }
6866 run_test 56x "lfs migration support"
6867
6868 test_56xa() {
6869         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
6870         check_swap_layouts_support
6871
6872         local dir=$DIR/$tdir/$testnum
6873
6874         test_mkdir -p $dir
6875
6876         local ref1=/etc/passwd
6877         local file1=$dir/file1
6878
6879         $LFS setstripe -c 2 $file1
6880         cp $ref1 $file1
6881         $LFS migrate --block -c 1 $file1 || error "migrate failed rc = $?"
6882
6883         local stripe=$($LFS getstripe -c $file1)
6884
6885         [[ $stripe == 1 ]] || error "stripe of $file1 is $stripe != 1"
6886         cmp $file1 $ref1 || error "content mismatch $file1 differs from $ref1"
6887
6888         # clean up
6889         rm -f $file1
6890 }
6891 run_test 56xa "lfs migration --block support"
6892
6893 check_migrate_links() {
6894         local dir="$1"
6895         local file1="$dir/file1"
6896         local begin="$2"
6897         local count="$3"
6898         local runas="$4"
6899         local total_count=$(($begin + $count - 1))
6900         local symlink_count=10
6901         local uniq_count=10
6902
6903         if [ ! -f "$file1" ]; then
6904                 echo -n "creating initial file..."
6905                 $LFS setstripe -c 1 -S "512k" "$file1" ||
6906                         error "cannot setstripe initial file"
6907                 echo "done"
6908
6909                 echo -n "creating symlinks..."
6910                 for s in $(seq 1 $symlink_count); do
6911                         ln -s "$file1" "$dir/slink$s" ||
6912                                 error "cannot create symlinks"
6913                 done
6914                 echo "done"
6915
6916                 echo -n "creating nonlinked files..."
6917                 createmany -o "$dir/uniq" 1 10 &> /dev/null ||
6918                         error "cannot create nonlinked files"
6919                 echo "done"
6920         fi
6921
6922         # create hard links
6923         if [ ! -f "$dir/file$total_count" ]; then
6924                 echo -n "creating hard links $begin:$total_count..."
6925                 createmany -l"$file1" "$dir/file" "$begin" "$count" &>  \
6926                         /dev/null || error "cannot create hard links"
6927                 echo "done"
6928         fi
6929
6930         echo -n "checking number of hard links listed in xattrs..."
6931         local fid=$($LFS getstripe -F "$file1")
6932         local paths=($($LFS fid2path "$MOUNT" "$fid" 2> /dev/null))
6933
6934         echo "${#paths[*]}"
6935         if [ ${#paths[*]} -lt $total_count -a "$begin" -eq 2  ]; then
6936                         skip "hard link list has unexpected size, skipping test"
6937         fi
6938         if [ ${#paths[*]} -ge $total_count -a "$begin" -ne 2  ]; then
6939                         error "link names should exceed xattrs size"
6940         fi
6941
6942         echo -n "migrating files..."
6943         local migrate_out=$($runas $LFS_MIGRATE -y -S '1m' $dir)
6944         local rc=$?
6945         [ $rc -eq 0 ] || error "migrate failed rc = $rc"
6946         echo "done"
6947
6948         # make sure all links have been properly migrated
6949         echo -n "verifying files..."
6950         fid=$($LFS getstripe -F "$file1") ||
6951                 error "cannot get fid for file $file1"
6952         for i in $(seq 2 $total_count); do
6953                 local fid2=$($LFS getstripe -F $dir/file$i)
6954
6955                 [ "$fid2" == "$fid" ] ||
6956                         error "migrated hard link has mismatched FID"
6957         done
6958
6959         # make sure hard links were properly detected, and migration was
6960         # performed only once for the entire link set; nonlinked files should
6961         # also be migrated
6962         local actual=$(grep -c 'done' <<< "$migrate_out")
6963         local expected=$(($uniq_count + 1))
6964
6965         [ "$actual" -eq  "$expected" ] ||
6966                 error "hard links individually migrated ($actual != $expected)"
6967
6968         # make sure the correct number of hard links are present
6969         local hardlinks=$(stat -c '%h' "$file1")
6970
6971         [ $hardlinks -eq $total_count ] ||
6972                 error "num hard links $hardlinks != $total_count"
6973         echo "done"
6974
6975         return 0
6976 }
6977
6978 test_56xb() {
6979         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
6980                 skip "Need MDS version at least 2.10.55"
6981
6982         local dir="$DIR/$tdir"
6983
6984         test_mkdir "$dir" || error "cannot create dir $dir"
6985
6986         echo "testing lfs migrate mode when all links fit within xattrs"
6987         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 2 99
6988
6989         echo "testing rsync mode when all links fit within xattrs"
6990         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 2 99
6991
6992         echo "testing lfs migrate mode when all links do not fit within xattrs"
6993         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100
6994
6995         echo "testing rsync mode when all links do not fit within xattrs"
6996         LFS_MIGRATE_RSYNC_MODE=true check_migrate_links "$dir" 101 100
6997
6998         chown -R $RUNAS_ID $dir
6999         echo "testing non-root lfs migrate mode when not all links are in xattr"
7000         LFS_MIGRATE_RSYNC_MODE=false check_migrate_links "$dir" 101 100 "$RUNAS"
7001
7002         # clean up
7003         rm -rf $dir
7004 }
7005 run_test 56xb "lfs migration hard link support"
7006
7007 test_56xc() {
7008         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7009
7010         local dir="$DIR/$tdir"
7011
7012         test_mkdir "$dir" || error "cannot create dir $dir"
7013
7014         # Test 1: ensure file < 1 GB is always migrated with 1 stripe
7015         echo -n "Setting initial stripe for 20MB test file..."
7016         $LFS setstripe -c 2 -i 0 "$dir/20mb" ||
7017                 error "cannot setstripe 20MB file"
7018         echo "done"
7019         echo -n "Sizing 20MB test file..."
7020         truncate "$dir/20mb" 20971520 || error "cannot create 20MB test file"
7021         echo "done"
7022         echo -n "Verifying small file autostripe count is 1..."
7023         $LFS_MIGRATE -y -A -C 1 "$dir/20mb" ||
7024                 error "cannot migrate 20MB file"
7025         local stripe_count=$($LFS getstripe -c "$dir/20mb") ||
7026                 error "cannot get stripe for $dir/20mb"
7027         [ $stripe_count -eq 1 ] ||
7028                 error "unexpected stripe count $stripe_count for 20MB file"
7029         rm -f "$dir/20mb"
7030         echo "done"
7031
7032         # Test 2: File is small enough to fit within the available space on
7033         # sqrt(size_in_gb) + 1 OSTs but is larger than 1GB.  The file must
7034         # have at least an additional 1KB for each desired stripe for test 3
7035         echo -n "Setting stripe for 1GB test file..."
7036         $LFS setstripe -c 1 -i 0 "$dir/1gb" || error "cannot setstripe 1GB file"
7037         echo "done"
7038         echo -n "Sizing 1GB test file..."
7039         # File size is 1GB + 3KB
7040         truncate "$dir/1gb" 1073744896 || error "cannot create 1GB test file"
7041         echo "done"
7042
7043         # need at least 512MB per OST for 1GB file to fit in 2 stripes
7044         local avail=$($LCTL get_param -n llite.$FSNAME*.kbytesavail)
7045         if (( avail > 524288 * OSTCOUNT )); then
7046                 echo -n "Migrating 1GB file..."
7047                 $LFS_MIGRATE -y -A -C 1 "$dir/1gb" ||
7048                         error "cannot migrate 1GB file"
7049                 echo "done"
7050                 echo -n "Verifying autostripe count is sqrt(n) + 1..."
7051                 stripe_count=$($LFS getstripe -c "$dir/1gb") ||
7052                         error "cannot getstripe for 1GB file"
7053                 [ $stripe_count -eq 2 ] ||
7054                         error "unexpected stripe count $stripe_count != 2"
7055                 echo "done"
7056         fi
7057
7058         # Test 3: File is too large to fit within the available space on
7059         # sqrt(n) + 1 OSTs.  Simulate limited available space with -X
7060         if [ $OSTCOUNT -ge 3 ]; then
7061                 # The required available space is calculated as
7062                 # file size (1GB + 3KB) / OST count (3).
7063                 local kb_per_ost=349526
7064
7065                 echo -n "Migrating 1GB file with limit..."
7066                 $LFS_MIGRATE -y -A -C 1 -X $kb_per_ost "$dir/1gb" ||
7067                         error "cannot migrate 1GB file with limit"
7068                 echo "done"
7069
7070                 stripe_count=$($LFS getstripe -c "$dir/1gb")
7071                 echo -n "Verifying 1GB autostripe count with limited space..."
7072                 [ "$stripe_count" -a $stripe_count -ge 3 ] ||
7073                         error "unexpected stripe count $stripe_count (min 3)"
7074                 echo "done"
7075         fi
7076
7077         # clean up
7078         rm -rf $dir
7079 }
7080 run_test 56xc "lfs migration autostripe"
7081
7082 test_56xd() {
7083         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7084
7085         local dir=$DIR/$tdir
7086         local f_mgrt=$dir/$tfile.mgrt
7087         local f_yaml=$dir/$tfile.yaml
7088         local f_copy=$dir/$tfile.copy
7089         local layout_yaml="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7090         local layout_copy="-c 2 -S 2M -i 1"
7091         local yamlfile=$dir/yamlfile
7092         local layout_before;
7093         local layout_after;
7094
7095         test_mkdir "$dir" || error "cannot create dir $dir"
7096         $LFS setstripe $layout_yaml $f_yaml ||
7097                 error "cannot setstripe $f_yaml with layout $layout_yaml"
7098         $LFS getstripe --yaml $f_yaml > $yamlfile
7099         $LFS setstripe $layout_copy $f_copy ||
7100                 error "cannot setstripe $f_copy with layout $layout_copy"
7101         touch $f_mgrt
7102         dd if=/dev/zero of=$f_mgrt bs=1M count=4
7103
7104         # 1. test option --yaml
7105         $LFS_MIGRATE -y --yaml $yamlfile $f_mgrt ||
7106                 error "cannot migrate $f_mgrt with --yaml $yamlfile"
7107         layout_before=$(get_layout_param $f_yaml)
7108         layout_after=$(get_layout_param $f_mgrt)
7109         [ "$layout_after" == "$layout_before" ] ||
7110                 error "lfs_migrate --yaml: $layout_after != $layout_before"
7111
7112         # 2. test option --copy
7113         $LFS_MIGRATE -y --copy $f_copy $f_mgrt ||
7114                 error "cannot migrate $f_mgrt with --copy $f_copy"
7115         layout_before=$(get_layout_param $f_copy)
7116         layout_after=$(get_layout_param $f_mgrt)
7117         [ "$layout_after" == "$layout_before" ] ||
7118                 error "lfs_migrate --copy: $layout_after != $layout_before"
7119 }
7120 run_test 56xd "check lfs_migrate --yaml and --copy support"
7121
7122 test_56xe() {
7123         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
7124
7125         local dir=$DIR/$tdir
7126         local f_comp=$dir/$tfile
7127         local layout="-E 1M -S 512K -c 1 -E -1 -S 1M -c 2 -i 0"
7128         local layout_before=""
7129         local layout_after=""
7130
7131         test_mkdir "$dir" || error "cannot create dir $dir"
7132         $LFS setstripe $layout $f_comp ||
7133                 error "cannot setstripe $f_comp with layout $layout"
7134         layout_before=$(get_layout_param $f_comp)
7135         dd if=/dev/zero of=$f_comp bs=1M count=4
7136
7137         # 1. migrate a comp layout file by lfs_migrate
7138         $LFS_MIGRATE -y $f_comp || error "cannot migrate $f_comp by lfs_migrate"
7139         layout_after=$(get_layout_param $f_comp)
7140         [ "$layout_before" == "$layout_after" ] ||
7141                 error "lfs_migrate: $layout_before != $layout_after"
7142
7143         # 2. migrate a comp layout file by lfs migrate
7144         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7145         layout_after=$(get_layout_param $f_comp)
7146         [ "$layout_before" == "$layout_after" ] ||
7147                 error "lfs migrate: $layout_before != $layout_after"
7148 }
7149 run_test 56xe "migrate a composite layout file"
7150
7151 test_56xf() {
7152         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
7153
7154         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
7155                 skip "Need server version at least 2.13.53"
7156
7157         local dir=$DIR/$tdir
7158         local f_comp=$dir/$tfile
7159         local layout="-E 1M -c1 -E -1 -c2"
7160         local fid_before=""
7161         local fid_after=""
7162
7163         test_mkdir "$dir" || error "cannot create dir $dir"
7164         $LFS setstripe $layout $f_comp ||
7165                 error "cannot setstripe $f_comp with layout $layout"
7166         fid_before=$($LFS getstripe --fid $f_comp)
7167         dd if=/dev/zero of=$f_comp bs=1M count=4
7168
7169         # 1. migrate a comp layout file to a comp layout
7170         $LFS migrate $f_comp || error "cannot migrate $f_comp by lfs migrate"
7171         fid_after=$($LFS getstripe --fid $f_comp)
7172         [ "$fid_before" == "$fid_after" ] ||
7173                 error "comp-to-comp migrate: $fid_before != $fid_after"
7174
7175         # 2. migrate a comp layout file to a plain layout
7176         $LFS migrate -c2 $f_comp ||
7177                 error "cannot migrate $f_comp by lfs migrate"
7178         fid_after=$($LFS getstripe --fid $f_comp)
7179         [ "$fid_before" == "$fid_after" ] ||
7180                 error "comp-to-plain migrate: $fid_before != $fid_after"
7181
7182         # 3. migrate a plain layout file to a comp layout
7183         $LFS migrate $layout $f_comp ||
7184                 error "cannot migrate $f_comp by lfs migrate"
7185         fid_after=$($LFS getstripe --fid $f_comp)
7186         [ "$fid_before" == "$fid_after" ] ||
7187                 error "plain-to-comp migrate: $fid_before != $fid_after"
7188 }
7189 run_test 56xf "FID is not lost during migration of a composite layout file"
7190
7191 test_56y() {
7192         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
7193                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
7194
7195         local res=""
7196         local dir=$DIR/$tdir
7197         local f1=$dir/file1
7198         local f2=$dir/file2
7199
7200         test_mkdir -p $dir || error "creating dir $dir"
7201         touch $f1 || error "creating std file $f1"
7202         $MULTIOP $f2 H2c || error "creating released file $f2"
7203
7204         # a directory can be raid0, so ask only for files
7205         res=$($LFS find $dir -L raid0 -type f | wc -l)
7206         [[ $res == 2 ]] || error "search raid0: found $res files != 2"
7207
7208         res=$($LFS find $dir \! -L raid0 -type f | wc -l)
7209         [[ $res == 0 ]] || error "search !raid0: found $res files != 0"
7210
7211         # only files can be released, so no need to force file search
7212         res=$($LFS find $dir -L released)
7213         [[ $res == $f2 ]] || error "search released: found $res != $f2"
7214
7215         res=$($LFS find $dir -type f \! -L released)
7216         [[ $res == $f1 ]] || error "search !released: found $res != $f1"
7217 }
7218 run_test 56y "lfs find -L raid0|released"
7219
7220 test_56z() { # LU-4824
7221         # This checks to make sure 'lfs find' continues after errors
7222         # There are two classes of errors that should be caught:
7223         # - If multiple paths are provided, all should be searched even if one
7224         #   errors out
7225         # - If errors are encountered during the search, it should not terminate
7226         #   early
7227         local dir=$DIR/$tdir
7228         local i
7229
7230         test_mkdir $dir
7231         for i in d{0..9}; do
7232                 test_mkdir $dir/$i
7233                 touch $dir/$i/$tfile
7234         done
7235         $LFS find $DIR/non_existent_dir $dir &&
7236                 error "$LFS find did not return an error"
7237         # Make a directory unsearchable. This should NOT be the last entry in
7238         # directory order.  Arbitrarily pick the 6th entry
7239         chmod 700 $($LFS find $dir -type d | sed '6!d')
7240
7241         $RUNAS $LFS find $DIR/non_existent $dir
7242         local count=$($RUNAS $LFS find $DIR/non_existent $dir | wc -l)
7243
7244         # The user should be able to see 10 directories and 9 files
7245         (( count == 19 )) ||
7246                 error "$LFS find found $count != 19 entries after error"
7247 }
7248 run_test 56z "lfs find should continue after an error"
7249
7250 test_56aa() { # LU-5937
7251         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
7252
7253         local dir=$DIR/$tdir
7254
7255         mkdir $dir
7256         $LFS setdirstripe -c$MDSCOUNT $dir/striped_dir
7257
7258         createmany -o $dir/striped_dir/${tfile}- 1024
7259         local dirs=$($LFS find --size +8k $dir/)
7260
7261         [ -n "$dirs" ] || error "lfs find --size wrong under striped dir"
7262 }
7263 run_test 56aa "lfs find --size under striped dir"
7264
7265 test_56ab() { # LU-10705
7266         test_mkdir $DIR/$tdir
7267         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=8k count=1 seek=2k
7268         dd if=/dev/zero of=$DIR/$tdir/$tfile.2 bs=4k count=1 seek=4k
7269         dd if=/dev/zero of=$DIR/$tdir/$tfile.3 bs=1M count=2 seek=16
7270         # Flush writes to ensure valid blocks.  Need to be more thorough for
7271         # ZFS, since blocks are not allocated/returned to client immediately.
7272         sync_all_data
7273         wait_zfs_commit ost1 2
7274         cancel_lru_locks osc
7275         ls -ls $DIR/$tdir
7276
7277         local files=$($LFS find --size +16M $DIR/$tdir | wc -l)
7278
7279         [[ $files == 3 ]] || error ">16M size files $files isn't 3 as expected"
7280
7281         files=$($LFS find --blocks +1M $DIR/$tdir | wc -l)
7282         [[ $files == 1 ]] || error ">1M blocks files $files isn't 1 as expected"
7283
7284         rm -f $DIR/$tdir/$tfile.[123]
7285 }
7286 run_test 56ab "lfs find --blocks"
7287
7288 test_56ba() {
7289         [ $MDS1_VERSION -lt $(version_code 2.10.50) ] &&
7290                 skip "Need MDS version at least 2.10.50"
7291
7292         # Create composite files with one component
7293         local dir=$DIR/$tdir
7294
7295         setup_56 $dir/1Mfiles 5 1 "-S 1M --component-end 1M"
7296         # Create composite files with three components
7297         setup_56 $dir/2Mfiles 5 2 "-E 2M -S 1M -E 4M -E 6M"
7298         # Create non-composite files
7299         createmany -o $dir/${tfile}- 10
7300
7301         local nfiles=$($LFS find --component-end 1M --type f $dir | wc -l)
7302
7303         [[ $nfiles == 10 ]] ||
7304                 error "lfs find -E 1M found $nfiles != 10 files"
7305
7306         nfiles=$($LFS find ! -E 1M --type f $dir | wc -l)
7307         [[ $nfiles == 25 ]] ||
7308                 error "lfs find ! -E 1M found $nfiles != 25 files"
7309
7310         # All files have a component that starts at 0
7311         nfiles=$($LFS find --component-start 0 --type f $dir | wc -l)
7312         [[ $nfiles == 35 ]] ||
7313                 error "lfs find --component-start 0 - $nfiles != 35 files"
7314
7315         nfiles=$($LFS find --component-start 2M --type f $dir | wc -l)
7316         [[ $nfiles == 15 ]] ||
7317                 error "lfs find --component-start 2M - $nfiles != 15 files"
7318
7319         # All files created here have a componenet that does not starts at 2M
7320         nfiles=$($LFS find ! --component-start 2M --type f $dir | wc -l)
7321         [[ $nfiles == 35 ]] ||
7322                 error "lfs find ! --component-start 2M - $nfiles != 35 files"
7323
7324         # Find files with a specified number of components
7325         local nfiles=$($LFS find --component-count 3 --type f $dir | wc -l)
7326         [[ $nfiles == 15 ]] ||
7327                 error "lfs find --component-count 3 - $nfiles != 15 files"
7328
7329         # Remember non-composite files have a component count of zero
7330         local nfiles=$($LFS find --component-count 0 --type f $dir | wc -l)
7331         [[ $nfiles == 10 ]] ||
7332                 error "lfs find --component-count 0 - $nfiles != 10 files"
7333
7334         nfiles=$($LFS find ! --component-count 3 --type f $dir | wc -l)
7335         [[ $nfiles == 20 ]] ||
7336                 error "lfs find ! --component-count 3 - $nfiles != 20 files"
7337
7338         # All files have a flag called "init"
7339         local nfiles=$($LFS find --component-flags init --type f $dir | wc -l)
7340         [[ $nfiles == 35 ]] ||
7341                 error "lfs find --component-flags init - $nfiles != 35 files"
7342
7343         # Multi-component files will have a component not initialized
7344         local nfiles=$($LFS find ! --component-flags init --type f $dir | wc -l)
7345         [[ $nfiles == 15 ]] ||
7346                 error "lfs find !--component-flags init - $nfiles != 15 files"
7347
7348         rm -rf $dir
7349
7350 }
7351 run_test 56ba "test lfs find --component-end, -start, -count, and -flags"
7352
7353 test_56ca() {
7354         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
7355                 skip "Need MDS version at least 2.10.57"
7356
7357         local td=$DIR/$tdir
7358         local tf=$td/$tfile
7359         local dir
7360         local nfiles
7361         local cmd
7362         local i
7363         local j
7364
7365         # create mirrored directories and mirrored files
7366         mkdir $td || error "mkdir $td failed"
7367         $LFS mirror create -N3 $td || error "create mirrored dir $td failed"
7368         createmany -o $tf- 10 || error "create $tf- failed"
7369
7370         for i in $(seq 2); do
7371                 dir=$td/dir$i
7372                 mkdir $dir || error "mkdir $dir failed"
7373                 $LFS mirror create -N$((3 + i)) $dir ||
7374                         error "create mirrored dir $dir failed"
7375                 createmany -o $dir/$tfile- 10 ||
7376                         error "create $dir/$tfile- failed"
7377         done
7378
7379         # change the states of some mirrored files
7380         echo foo > $tf-6
7381         for i in $(seq 2); do
7382                 dir=$td/dir$i
7383                 for j in $(seq 4 9); do
7384                         echo foo > $dir/$tfile-$j
7385                 done
7386         done
7387
7388         # find mirrored files with specific mirror count
7389         cmd="$LFS find --mirror-count 3 --type f $td"
7390         nfiles=$($cmd | wc -l)
7391         [[ $nfiles = 10 ]] || error "$cmd: $nfiles != 10 files"
7392
7393         cmd="$LFS find ! --mirror-count 3 --type f $td"
7394         nfiles=$($cmd | wc -l)
7395         [[ $nfiles = 20 ]] || error "$cmd: $nfiles != 20 files"
7396
7397         cmd="$LFS find --mirror-count +2 --type f $td"
7398         nfiles=$($cmd | wc -l)
7399         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7400
7401         cmd="$LFS find --mirror-count -6 --type f $td"
7402         nfiles=$($cmd | wc -l)
7403         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7404
7405         # find mirrored files with specific file state
7406         cmd="$LFS find --maxdepth 1 --mirror-state=^ro --type f $td"
7407         [[ $($cmd) = $tf-6 ]] || error "$cmd: didn't return $tf-6"
7408
7409         cmd="$LFS find --mirror-state=ro --type f $td"
7410         nfiles=$($cmd | wc -l)
7411         [[ $nfiles = 17 ]] || error "$cmd: $nfiles != 17 files"
7412
7413         cmd="$LFS find ! --mirror-state=ro --type f $td"
7414         nfiles=$($cmd | wc -l)
7415         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7416
7417         cmd="$LFS find --mirror-state=wp --type f $td"
7418         nfiles=$($cmd | wc -l)
7419         [[ $nfiles = 13 ]] || error "$cmd: $nfiles != 13 files"
7420
7421         cmd="$LFS find ! --mirror-state=sp --type f $td"
7422         nfiles=$($cmd | wc -l)
7423         [[ $nfiles = 30 ]] || error "$cmd: $nfiles != 30 files"
7424 }
7425 run_test 56ca "check lfs find --mirror-count|-N and --mirror-state"
7426
7427 test_57a() {
7428         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7429         # note test will not do anything if MDS is not local
7430         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7431                 skip_env "ldiskfs only test"
7432         fi
7433         remote_mds_nodsh && skip "remote MDS with nodsh"
7434
7435         local MNTDEV="osd*.*MDT*.mntdev"
7436         DEV=$(do_facet $SINGLEMDS lctl get_param -n $MNTDEV)
7437         [ -z "$DEV" ] && error "can't access $MNTDEV"
7438         for DEV in $(do_facet $SINGLEMDS lctl get_param -n $MNTDEV); do
7439                 do_facet $SINGLEMDS $DUMPE2FS -h $DEV > $TMP/t57a.dump ||
7440                         error "can't access $DEV"
7441                 DEVISIZE=$(awk '/Inode size:/ { print $3 }' $TMP/t57a.dump)
7442                 [[ $DEVISIZE -gt 128 ]] || error "inode size $DEVISIZE"
7443                 rm $TMP/t57a.dump
7444         done
7445 }
7446 run_test 57a "verify MDS filesystem created with large inodes =="
7447
7448 test_57b() {
7449         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7450         if [ "$mds1_FSTYPE" != ldiskfs ]; then
7451                 skip_env "ldiskfs only test"
7452         fi
7453         remote_mds_nodsh && skip "remote MDS with nodsh"
7454
7455         local dir=$DIR/$tdir
7456         local filecount=100
7457         local file1=$dir/f1
7458         local fileN=$dir/f$filecount
7459
7460         rm -rf $dir || error "removing $dir"
7461         test_mkdir -c1 $dir
7462         local mdtidx=$($LFS getstripe -m $dir)
7463         local mdtname=MDT$(printf %04x $mdtidx)
7464         local facet=mds$((mdtidx + 1))
7465
7466         echo "mcreating $filecount files"
7467         createmany -m $dir/f 1 $filecount || error "creating files in $dir"
7468
7469         # verify that files do not have EAs yet
7470         $LFS getstripe $file1 2>&1 | grep -q "no stripe" ||
7471                 error "$file1 has an EA"
7472         $LFS getstripe $fileN 2>&1 | grep -q "no stripe" ||
7473                 error "$fileN has an EA"
7474
7475         sync
7476         sleep 1
7477         df $dir  #make sure we get new statfs data
7478         local mdsfree=$(do_facet $facet \
7479                         lctl get_param -n osd*.*$mdtname.kbytesfree)
7480         local mdcfree=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7481         local file
7482
7483         echo "opening files to create objects/EAs"
7484         for file in $(seq -f $dir/f%g 1 $filecount); do
7485                 $OPENFILE -f O_RDWR $file > /dev/null 2>&1 ||
7486                         error "opening $file"
7487         done
7488
7489         # verify that files have EAs now
7490         $LFS getstripe $file1 | grep -q "obdidx" || error "$file1 missing EA"
7491         $LFS getstripe $fileN | grep -q "obdidx" || error "$fileN missing EA"
7492
7493         sleep 1  #make sure we get new statfs data
7494         df $dir
7495         local mdsfree2=$(do_facet $facet \
7496                          lctl get_param -n osd*.*$mdtname.kbytesfree)
7497         local mdcfree2=$(lctl get_param -n mdc.*$mdtname-mdc-*.kbytesfree)
7498
7499         if [[ $mdcfree2 -lt $((mdcfree - 16)) ]]; then
7500                 if [ "$mdsfree" != "$mdsfree2" ]; then
7501                         error "MDC before $mdcfree != after $mdcfree2"
7502                 else
7503                         echo "MDC before $mdcfree != after $mdcfree2"
7504                         echo "unable to confirm if MDS has large inodes"
7505                 fi
7506         fi
7507         rm -rf $dir
7508 }
7509 run_test 57b "default LOV EAs are stored inside large inodes ==="
7510
7511 test_58() {
7512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7513         [ -z "$(which wiretest 2>/dev/null)" ] &&
7514                         skip_env "could not find wiretest"
7515
7516         wiretest
7517 }
7518 run_test 58 "verify cross-platform wire constants =============="
7519
7520 test_59() {
7521         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7522
7523         echo "touch 130 files"
7524         createmany -o $DIR/f59- 130
7525         echo "rm 130 files"
7526         unlinkmany $DIR/f59- 130
7527         sync
7528         # wait for commitment of removal
7529         wait_delete_completed
7530 }
7531 run_test 59 "verify cancellation of llog records async ========="
7532
7533 TEST60_HEAD="test_60 run $RANDOM"
7534 test_60a() {
7535         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7536         remote_mgs_nodsh && skip "remote MGS with nodsh"
7537         do_facet mgs "! which run-llog.sh &> /dev/null" &&
7538                 do_facet mgs "! ls run-llog.sh &> /dev/null" &&
7539                         skip_env "missing subtest run-llog.sh"
7540
7541         log "$TEST60_HEAD - from kernel mode"
7542         do_facet mgs "$LCTL dk > /dev/null"
7543         do_facet mgs "bash run-llog.sh" || error "run-llog.sh failed"
7544         do_facet mgs $LCTL dk > $TMP/$tfile
7545
7546         # LU-6388: test llog_reader
7547         local llog_reader=$(do_facet mgs "which llog_reader 2> /dev/null")
7548         llog_reader=${llog_reader:-$LUSTRE/utils/llog_reader}
7549         [ -z $(do_facet mgs ls -d $llog_reader 2> /dev/null) ] &&
7550                         skip_env "missing llog_reader"
7551         local fstype=$(facet_fstype mgs)
7552         [ $fstype != ldiskfs -a $fstype != zfs ] &&
7553                 skip_env "Only for ldiskfs or zfs type mgs"
7554
7555         local mntpt=$(facet_mntpt mgs)
7556         local mgsdev=$(mgsdevname 1)
7557         local fid_list
7558         local fid
7559         local rec_list
7560         local rec
7561         local rec_type
7562         local obj_file
7563         local path
7564         local seq
7565         local oid
7566         local pass=true
7567
7568         #get fid and record list
7569         fid_list=($(awk '/9_sub.*record/ { print $NF }' $TMP/$tfile |
7570                 tail -n 4))
7571         rec_list=($(awk '/9_sub.*record/ { print $((NF-3)) }' $TMP/$tfile |
7572                 tail -n 4))
7573         #remount mgs as ldiskfs or zfs type
7574         stop mgs || error "stop mgs failed"
7575         mount_fstype mgs || error "remount mgs failed"
7576         for ((i = 0; i < ${#fid_list[@]}; i++)); do
7577                 fid=${fid_list[i]}
7578                 rec=${rec_list[i]}
7579                 seq=$(echo $fid | awk -F ':' '{ print $1 }' | sed -e "s/^0x//g")
7580                 oid=$(echo $fid | awk -F ':' '{ print $2 }' | sed -e "s/^0x//g")
7581                 oid=$((16#$oid))
7582
7583                 case $fstype in
7584                         ldiskfs )
7585                                 obj_file=$mntpt/O/$seq/d$((oid%32))/$oid ;;
7586                         zfs )
7587                                 obj_file=$mntpt/oi.$(($((16#$seq))&127))/$fid ;;
7588                 esac
7589                 echo "obj_file is $obj_file"
7590                 do_facet mgs $llog_reader $obj_file
7591
7592                 rec_type=$(do_facet mgs $llog_reader $obj_file | grep "type=" |
7593                         awk '{ print $3 }' | sed -e "s/^type=//g")
7594                 if [ $rec_type != $rec ]; then
7595                         echo "FAILED test_60a wrong record type $rec_type," \
7596                               "should be $rec"
7597                         pass=false
7598                         break
7599                 fi
7600
7601                 #check obj path if record type is LLOG_LOGID_MAGIC
7602                 if [ "$rec" == "1064553b" ]; then
7603                         path=$(do_facet mgs $llog_reader $obj_file |
7604                                 grep "path=" | awk '{ print $NF }' |
7605                                 sed -e "s/^path=//g")
7606                         if [ $obj_file != $mntpt/$path ]; then
7607                                 echo "FAILED test_60a wrong obj path" \
7608                                       "$montpt/$path, should be $obj_file"
7609                                 pass=false
7610                                 break
7611                         fi
7612                 fi
7613         done
7614         rm -f $TMP/$tfile
7615         #restart mgs before "error", otherwise it will block the next test
7616         stop mgs || error "stop mgs failed"
7617         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
7618         $pass || error "test failed, see FAILED test_60a messages for specifics"
7619 }
7620 run_test 60a "llog_test run from kernel module and test llog_reader"
7621
7622 test_60b() { # bug 6411
7623         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7624
7625         dmesg > $DIR/$tfile
7626         LLOG_COUNT=$(do_facet mgs dmesg |
7627                      awk "/$TEST60_HEAD/ { marker = 1; from_marker = 0; }
7628                           /llog_[a-z]*.c:[0-9]/ {
7629                                 if (marker)
7630                                         from_marker++
7631                                 from_begin++
7632                           }
7633                           END {
7634                                 if (marker)
7635                                         print from_marker
7636                                 else
7637                                         print from_begin
7638                           }")
7639
7640         [[ $LLOG_COUNT -gt 120 ]] &&
7641                 error "CDEBUG_LIMIT not limiting messages ($LLOG_COUNT)" || true
7642 }
7643 run_test 60b "limit repeated messages from CERROR/CWARN"
7644
7645 test_60c() {
7646         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7647
7648         echo "create 5000 files"
7649         createmany -o $DIR/f60c- 5000
7650 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED  0x137
7651         lctl set_param fail_loc=0x80000137
7652         unlinkmany $DIR/f60c- 5000
7653         lctl set_param fail_loc=0
7654 }
7655 run_test 60c "unlink file when mds full"
7656
7657 test_60d() {
7658         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7659
7660         SAVEPRINTK=$(lctl get_param -n printk)
7661         # verify "lctl mark" is even working"
7662         MESSAGE="test message ID $RANDOM $$"
7663         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7664         dmesg | grep -q "$MESSAGE" || error "didn't find debug marker in log"
7665
7666         lctl set_param printk=0 || error "set lnet.printk failed"
7667         lctl get_param -n printk | grep emerg || error "lnet.printk dropped emerg"
7668         MESSAGE="new test message ID $RANDOM $$"
7669         # Assume here that libcfs_debug_mark_buffer() uses D_WARNING
7670         $LCTL mark "$MESSAGE" || error "$LCTL mark failed"
7671         dmesg | grep -q "$MESSAGE" && error "D_WARNING wasn't masked" || true
7672
7673         lctl set_param -n printk="$SAVEPRINTK"
7674 }
7675 run_test 60d "test printk console message masking"
7676
7677 test_60e() {
7678         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7679         remote_mds_nodsh && skip "remote MDS with nodsh"
7680
7681         touch $DIR/$tfile
7682 #define OBD_FAIL_MDS_LLOG_CREATE_FAILED2  0x15b
7683         do_facet mds1 lctl set_param fail_loc=0x15b
7684         rm $DIR/$tfile
7685 }
7686 run_test 60e "no space while new llog is being created"
7687
7688 test_60g() {
7689         local pid
7690         local i
7691
7692         test_mkdir -c $MDSCOUNT $DIR/$tdir
7693
7694         (
7695                 local index=0
7696                 while true; do
7697                         $LFS setdirstripe -i $(($index % $MDSCOUNT)) \
7698                                 -c $MDSCOUNT $DIR/$tdir/subdir$index \
7699                                 2>/dev/null
7700                         mkdir $DIR/$tdir/subdir$index 2>/dev/null
7701                         rmdir $DIR/$tdir/subdir$index 2>/dev/null
7702                         index=$((index + 1))
7703                 done
7704         ) &
7705
7706         pid=$!
7707
7708         for i in {0..100}; do
7709                 # define OBD_FAIL_OSD_TXN_START    0x19a
7710                 local index=$((i % MDSCOUNT + 1))
7711
7712                 do_facet mds$index $LCTL set_param fail_loc=0x8000019a \
7713                         > /dev/null
7714                 usleep 100
7715         done
7716
7717         kill -9 $pid
7718
7719         for i in $(seq $MDSCOUNT); do
7720                 do_facet mds$i $LCTL set_param fail_loc=0 > /dev/null
7721         done
7722
7723         mkdir $DIR/$tdir/new || error "mkdir failed"
7724         rmdir $DIR/$tdir/new || error "rmdir failed"
7725
7726         do_facet mds1 $LCTL lfsck_start -M $(facet_svc mds1) -A -C \
7727                 -t namespace
7728         for i in $(seq $MDSCOUNT); do
7729                 wait_update_facet mds$i "$LCTL get_param -n \
7730                         mdd.$(facet_svc mds$i).lfsck_namespace |
7731                         awk '/^status/ { print \\\$2 }'" "completed"
7732         done
7733
7734         ls -R $DIR/$tdir || error "ls failed"
7735         rm -rf $DIR/$tdir || error "rmdir failed"
7736 }
7737 run_test 60g "transaction abort won't cause MDT hung"
7738
7739 test_60h() {
7740         [ $MDS1_VERSION -le $(version_code 2.12.52) ] &&
7741                 skip "Need MDS version at least 2.12.52"
7742         [ $MDSCOUNT -lt 2 ] && skip "Need at least 2 MDTs"
7743
7744         local f
7745
7746         #define OBD_FAIL_MDS_STRIPE_CREATE       0x188
7747         #define OBD_FAIL_MDS_STRIPE_FID          0x189
7748         for fail_loc in 0x80000188 0x80000189; do
7749                 do_facet mds1 "$LCTL set_param fail_loc=$fail_loc"
7750                 $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir-$fail_loc ||
7751                         error "mkdir $dir-$fail_loc failed"
7752                 for i in {0..10}; do
7753                         # create may fail on missing stripe
7754                         echo $i > $DIR/$tdir-$fail_loc/$i
7755                 done
7756                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7757                         error "getdirstripe $tdir-$fail_loc failed"
7758                 $LFS migrate -m 1 $DIR/$tdir-$fail_loc ||
7759                         error "migrate $tdir-$fail_loc failed"
7760                 $LFS getdirstripe $DIR/$tdir-$fail_loc ||
7761                         error "getdirstripe $tdir-$fail_loc failed"
7762                 pushd $DIR/$tdir-$fail_loc
7763                 for f in *; do
7764                         echo $f | cmp $f - || error "$f data mismatch"
7765                 done
7766                 popd
7767                 rm -rf $DIR/$tdir-$fail_loc
7768         done
7769 }
7770 run_test 60h "striped directory with missing stripes can be accessed"
7771
7772 test_61a() {
7773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7774
7775         f="$DIR/f61"
7776         dd if=/dev/zero of=$f bs=$PAGE_SIZE count=1 || error "dd $f failed"
7777         cancel_lru_locks osc
7778         $MULTIOP $f OSMWUc || error "$MULTIOP $f failed"
7779         sync
7780 }
7781 run_test 61a "mmap() writes don't make sync hang ================"
7782
7783 test_61b() {
7784         mmap_mknod_test $DIR/$tfile || error "mmap_mknod_test failed"
7785 }
7786 run_test 61b "mmap() of unstriped file is successful"
7787
7788 # bug 2330 - insufficient obd_match error checking causes LBUG
7789 test_62() {
7790         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7791
7792         f="$DIR/f62"
7793         echo foo > $f
7794         cancel_lru_locks osc
7795         lctl set_param fail_loc=0x405
7796         cat $f && error "cat succeeded, expect -EIO"
7797         lctl set_param fail_loc=0
7798 }
7799 # This test is now irrelevant (as of bug 10718 inclusion), we no longer
7800 # match every page all of the time.
7801 #run_test 62 "verify obd_match failure doesn't LBUG (should -EIO)"
7802
7803 # bug 2319 - oig_wait() interrupted causes crash because of invalid waitq.
7804 # Though this test is irrelevant anymore, it helped to reveal some
7805 # other grant bugs (LU-4482), let's keep it.
7806 test_63a() {   # was test_63
7807         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7808
7809         MAX_DIRTY_MB=$(lctl get_param -n osc.*.max_dirty_mb | head -n 1)
7810
7811         for i in `seq 10` ; do
7812                 dd if=/dev/zero of=$DIR/f63 bs=8k &
7813                 sleep 5
7814                 kill $!
7815                 sleep 1
7816         done
7817
7818         rm -f $DIR/f63 || true
7819 }
7820 run_test 63a "Verify oig_wait interruption does not crash ======="
7821
7822 # bug 2248 - async write errors didn't return to application on sync
7823 # bug 3677 - async write errors left page locked
7824 test_63b() {
7825         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7826
7827         debugsave
7828         lctl set_param debug=-1
7829
7830         # ensure we have a grant to do async writes
7831         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1
7832         rm $DIR/$tfile
7833
7834         sync    # sync lest earlier test intercept the fail_loc
7835
7836         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
7837         lctl set_param fail_loc=0x80000406
7838         $MULTIOP $DIR/$tfile Owy && \
7839                 error "sync didn't return ENOMEM"
7840         sync; sleep 2; sync     # do a real sync this time to flush page
7841         lctl get_param -n llite.*.dump_page_cache | grep locked && \
7842                 error "locked page left in cache after async error" || true
7843         debugrestore
7844 }
7845 run_test 63b "async write errors should be returned to fsync ==="
7846
7847 test_64a () {
7848         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7849
7850         lfs df $DIR
7851         lctl get_param osc.*[oO][sS][cC][_-]*.cur* | grep "=[1-9]"
7852 }
7853 run_test 64a "verify filter grant calculations (in kernel) ====="
7854
7855 test_64b () {
7856         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7857
7858         sh oos.sh $MOUNT || error "oos.sh failed: $?"
7859 }
7860 run_test 64b "check out-of-space detection on client"
7861
7862 test_64c() {
7863         $LCTL set_param osc.*OST0000-osc-[^mM]*.cur_grant_bytes=0
7864 }
7865 run_test 64c "verify grant shrink"
7866
7867 # this does exactly what osc_request.c:osc_announce_cached() does in
7868 # order to calculate max amount of grants to ask from server
7869 want_grant() {
7870         local tgt=$1
7871
7872         local nrpages=$($LCTL get_param -n osc.${tgt}.max_pages_per_rpc)
7873         local rpc_in_flight=$($LCTL get_param -n osc.${tgt}.max_rpcs_in_flight)
7874
7875         ((rpc_in_flight ++));
7876         nrpages=$((nrpages * rpc_in_flight))
7877
7878         local dirty_max_pages=$($LCTL get_param -n osc.${tgt}.max_dirty_mb)
7879
7880         dirty_max_pages=$((dirty_max_pages * 1024 * 1024 / PAGE_SIZE))
7881
7882         [[ $dirty_max_pages -gt $nrpages ]] && nrpages=$dirty_max_pages
7883         local undirty=$((nrpages * PAGE_SIZE))
7884
7885         local max_extent_pages
7886         max_extent_pages=$($LCTL get_param osc.${tgt}.import |
7887             grep grant_max_extent_size | awk '{print $2}')
7888         max_extent_pages=$((max_extent_pages / PAGE_SIZE))
7889         local nrextents=$(((nrpages + max_extent_pages - 1) / max_extent_pages))
7890         local grant_extent_tax
7891         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7892             grep grant_extent_tax | awk '{print $2}')
7893
7894         undirty=$((undirty + nrextents * grant_extent_tax))
7895
7896         echo $undirty
7897 }
7898
7899 # this is size of unit for grant allocation. It should be equal to
7900 # what tgt_grant.c:tgt_grant_chunk() calculates
7901 grant_chunk() {
7902         local tgt=$1
7903         local max_brw_size
7904         local grant_extent_tax
7905
7906         max_brw_size=$($LCTL get_param osc.${tgt}.import |
7907             grep max_brw_size | awk '{print $2}')
7908
7909         grant_extent_tax=$($LCTL get_param osc.${tgt}.import |
7910             grep grant_extent_tax | awk '{print $2}')
7911
7912         echo $(((max_brw_size + grant_extent_tax) * 2))
7913 }
7914
7915 test_64d() {
7916         [ $OST1_VERSION -lt $(version_code 2.10.56) ] &&
7917                 skip "OST < 2.10.55 doesn't limit grants enough"
7918
7919         local tgt=$($LCTL dl | grep "0000-osc-[^mM]" | awk '{print $4}')
7920         local file=$DIR/$tfile
7921
7922         [[ $($LCTL get_param osc.${tgt}.import |
7923              grep "connect_flags:.*grant_param") ]] ||
7924                 skip "no grant_param connect flag"
7925
7926         local olddebug=$($LCTL get_param -n debug 2> /dev/null)
7927
7928         $LCTL set_param debug="$OLDDEBUG" 2> /dev/null || true
7929
7930         local max_cur_granted=$(($(want_grant $tgt) + $(grant_chunk $tgt)))
7931         stack_trap "rm -f $file" EXIT
7932
7933         $LFS setstripe $file -i 0 -c 1
7934         dd if=/dev/zero of=$file bs=1M count=1000 &
7935         ddpid=$!
7936
7937         while true
7938         do
7939                 local cur_grant=$($LCTL get_param -n osc.${tgt}.cur_grant_bytes)
7940                 if [[ $cur_grant -gt $max_cur_granted ]]
7941                 then
7942                         kill $ddpid
7943                         error "cur_grant $cur_grant > $max_cur_granted"
7944                 fi
7945                 kill -0 $ddpid
7946                 [[ $? -ne 0 ]] && break;
7947                 sleep 2
7948         done
7949
7950         rm -f $DIR/$tfile
7951         wait_delete_completed
7952         $LCTL set_param debug="$olddebug" 2> /dev/null || true
7953 }
7954 run_test 64d "check grant limit exceed"
7955
7956 # bug 1414 - set/get directories' stripe info
7957 test_65a() {
7958         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7959
7960         test_mkdir $DIR/$tdir
7961         touch $DIR/$tdir/f1
7962         $LVERIFY $DIR/$tdir $DIR/$tdir/f1 || error "lverify failed"
7963 }
7964 run_test 65a "directory with no stripe info"
7965
7966 test_65b() {
7967         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7968
7969         test_mkdir $DIR/$tdir
7970         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7971
7972         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
7973                                                 error "setstripe"
7974         touch $DIR/$tdir/f2
7975         $LVERIFY $DIR/$tdir $DIR/$tdir/f2 || error "lverify failed"
7976 }
7977 run_test 65b "directory setstripe -S stripe_size*2 -i 0 -c 1"
7978
7979 test_65c() {
7980         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7981         [ $OSTCOUNT -lt 2 ] && skip_env "need at least 2 OSTs"
7982
7983         test_mkdir $DIR/$tdir
7984         local stripesize=$($LFS getstripe -S $DIR/$tdir)
7985
7986         $LFS setstripe -S $((stripesize * 4)) -i 1 \
7987                 -c $((OSTCOUNT - 1)) $DIR/$tdir || error "setstripe"
7988         touch $DIR/$tdir/f3
7989         $LVERIFY $DIR/$tdir $DIR/$tdir/f3 || error "lverify failed"
7990 }
7991 run_test 65c "directory setstripe -S stripe_size*4 -i 1 -c $((OSTCOUNT-1))"
7992
7993 test_65d() {
7994         [ $PARALLEL == "yes" ] && skip "skip parallel run"
7995
7996         test_mkdir $DIR/$tdir
7997         local STRIPECOUNT=$($LFS getstripe -c $DIR/$tdir)
7998         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
7999
8000         if [[ $STRIPECOUNT -le 0 ]]; then
8001                 sc=1
8002         elif [[ $STRIPECOUNT -gt $LOV_MAX_STRIPE_COUNT ]]; then
8003                 [[ $OSTCOUNT -gt $LOV_MAX_STRIPE_COUNT ]] &&
8004                         sc=$LOV_MAX_STRIPE_COUNT || sc=$(($OSTCOUNT - 1))
8005         else
8006                 sc=$(($STRIPECOUNT - 1))
8007         fi
8008         $LFS setstripe -S $STRIPESIZE -c $sc $DIR/$tdir || error "setstripe"
8009         touch $DIR/$tdir/f4 $DIR/$tdir/f5
8010         $LVERIFY $DIR/$tdir $DIR/$tdir/f4 $DIR/$tdir/f5 ||
8011                 error "lverify failed"
8012 }
8013 run_test 65d "directory setstripe -S stripe_size -c stripe_count"
8014
8015 test_65e() {
8016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8017
8018         test_mkdir $DIR/$tdir
8019
8020         $LFS setstripe $DIR/$tdir || error "setstripe"
8021         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8022                                         error "no stripe info failed"
8023         touch $DIR/$tdir/f6
8024         $LVERIFY $DIR/$tdir $DIR/$tdir/f6 || error "lverify failed"
8025 }
8026 run_test 65e "directory setstripe defaults"
8027
8028 test_65f() {
8029         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8030
8031         test_mkdir $DIR/${tdir}f
8032         $RUNAS $LFS setstripe $DIR/${tdir}f &&
8033                 error "setstripe succeeded" || true
8034 }
8035 run_test 65f "dir setstripe permission (should return error) ==="
8036
8037 test_65g() {
8038         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8039
8040         test_mkdir $DIR/$tdir
8041         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8042
8043         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8044                 error "setstripe -S failed"
8045         $LFS setstripe -d $DIR/$tdir || error "setstripe -d failed"
8046         $LFS getstripe -v $DIR/$tdir | grep "Default" ||
8047                 error "delete default stripe failed"
8048 }
8049 run_test 65g "directory setstripe -d"
8050
8051 test_65h() {
8052         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8053
8054         test_mkdir $DIR/$tdir
8055         local STRIPESIZE=$($LFS getstripe -S $DIR/$tdir)
8056
8057         $LFS setstripe -S $((STRIPESIZE * 2)) -i 0 -c 1 $DIR/$tdir ||
8058                 error "setstripe -S failed"
8059         test_mkdir $DIR/$tdir/dd1
8060         [ $($LFS getstripe -c $DIR/$tdir) = $($LFS getstripe -c $DIR/$tdir/dd1) ] ||
8061                 error "stripe info inherit failed"
8062 }
8063 run_test 65h "directory stripe info inherit ===================="
8064
8065 test_65i() {
8066         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8067
8068         save_layout_restore_at_exit $MOUNT
8069
8070         # bug6367: set non-default striping on root directory
8071         $LFS setstripe -S 65536 -c -1 $MOUNT || error "error setting stripe"
8072
8073         # bug12836: getstripe on -1 default directory striping
8074         $LFS getstripe $MOUNT || error "getstripe $MOUNT failed"
8075
8076         # bug12836: getstripe -v on -1 default directory striping
8077         $LFS getstripe -v $MOUNT || error "getstripe -v $MOUNT failed"
8078
8079         # bug12836: new find on -1 default directory striping
8080         $LFS find -mtime -1 $MOUNT > /dev/null || error "find $MOUNT failed"
8081 }
8082 run_test 65i "various tests to set root directory striping"
8083
8084 test_65j() { # bug6367
8085         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8086
8087         sync; sleep 1
8088
8089         # if we aren't already remounting for each test, do so for this test
8090         if [ "$I_MOUNTED" = "yes" ]; then
8091                 cleanup || error "failed to unmount"
8092                 setup
8093         fi
8094
8095         save_layout_restore_at_exit $MOUNT
8096
8097         $LFS setstripe -d $MOUNT || error "setstripe failed"
8098 }
8099 run_test 65j "set default striping on root directory (bug 6367)="
8100
8101 cleanup_65k() {
8102         rm -rf $DIR/$tdir
8103         wait_delete_completed
8104         do_facet $SINGLEMDS "lctl set_param -n \
8105                 osp.$ost*MDT0000.max_create_count=$max_count"
8106         do_facet $SINGLEMDS "lctl set_param -n \
8107                 osp.$ost*MDT0000.create_count=$count"
8108         do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8109         echo $INACTIVE_OSC "is Activate"
8110
8111         wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8112 }
8113
8114 test_65k() { # bug11679
8115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8116         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
8117         remote_mds_nodsh && skip "remote MDS with nodsh"
8118
8119         local disable_precreate=true
8120         [ $MDS1_VERSION -le $(version_code 2.8.54) ] &&
8121                 disable_precreate=false
8122
8123         echo "Check OST status: "
8124         local MDS_OSCS=$(do_facet $SINGLEMDS lctl dl |
8125                 awk '/[oO][sS][cC].*md[ts]/ { print $4 }')
8126
8127         for OSC in $MDS_OSCS; do
8128                 echo $OSC "is active"
8129                 do_facet $SINGLEMDS lctl --device %$OSC activate
8130         done
8131
8132         for INACTIVE_OSC in $MDS_OSCS; do
8133                 local ost=$(osc_to_ost $INACTIVE_OSC)
8134                 local ostnum=$(do_facet $SINGLEMDS lctl get_param -n \
8135                                lov.*md*.target_obd |
8136                                awk -F: /$ost/'{ print $1 }' | head -n 1)
8137
8138                 mkdir -p $DIR/$tdir
8139                 $LFS setstripe -i $ostnum -c 1 $DIR/$tdir
8140                 createmany -o $DIR/$tdir/$tfile.$ostnum. 1000
8141
8142                 echo "Deactivate: " $INACTIVE_OSC
8143                 do_facet $SINGLEMDS lctl --device %$INACTIVE_OSC deactivate
8144
8145                 local count=$(do_facet $SINGLEMDS "lctl get_param -n \
8146                               osp.$ost*MDT0000.create_count")
8147                 local max_count=$(do_facet $SINGLEMDS "lctl get_param -n \
8148                                   osp.$ost*MDT0000.max_create_count")
8149                 $disable_precreate &&
8150                         do_facet $SINGLEMDS "lctl set_param -n \
8151                                 osp.$ost*MDT0000.max_create_count=0"
8152
8153                 for idx in $(seq 0 $((OSTCOUNT - 1))); do
8154                         [ -f $DIR/$tdir/$idx ] && continue
8155                         echo "$LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx"
8156                         $LFS setstripe -i $idx -c 1 $DIR/$tdir/$idx ||
8157                                 { cleanup_65k;
8158                                   error "setstripe $idx should succeed"; }
8159                         rm -f $DIR/$tdir/$idx || error "rm $idx failed"
8160                 done
8161                 unlinkmany $DIR/$tdir/$tfile.$ostnum. 1000
8162                 rmdir $DIR/$tdir
8163
8164                 do_facet $SINGLEMDS "lctl set_param -n \
8165                         osp.$ost*MDT0000.max_create_count=$max_count"
8166                 do_facet $SINGLEMDS "lctl set_param -n \
8167                         osp.$ost*MDT0000.create_count=$count"
8168                 do_facet $SINGLEMDS lctl --device  %$INACTIVE_OSC activate
8169                 echo $INACTIVE_OSC "is Activate"
8170
8171                 wait_osc_import_state mds ost$(( ostnum + 1 )) FULL
8172         done
8173 }
8174 run_test 65k "validate manual striping works properly with deactivated OSCs"
8175
8176 test_65l() { # bug 12836
8177         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8178
8179         test_mkdir -p $DIR/$tdir/test_dir
8180         $LFS setstripe -c -1 $DIR/$tdir/test_dir
8181         $LFS find -mtime -1 $DIR/$tdir >/dev/null
8182 }
8183 run_test 65l "lfs find on -1 stripe dir ========================"
8184
8185 test_65m() {
8186         local layout=$(save_layout $MOUNT)
8187         $RUNAS $LFS setstripe -c 2 $MOUNT && {
8188                 restore_layout $MOUNT $layout
8189                 error "setstripe should fail by non-root users"
8190         }
8191         true
8192 }
8193 run_test 65m "normal user can't set filesystem default stripe"
8194
8195 test_65n() {
8196         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
8197         [[ $MDS1_VERSION -ge $(version_code 2.12.50) ]] ||
8198                 skip "Need MDS version at least 2.12.50"
8199         [[ $PARALLEL != "yes" ]] || skip "skip parallel run"
8200
8201         [[ $OSTCOUNT -ge 2 ]] || skip_env "needs >= 2 OSTs"
8202         which getfattr > /dev/null 2>&1 || skip_env "no getfattr command"
8203         which setfattr > /dev/null 2>&1 || skip_env "no setfattr command"
8204
8205         local root_layout=$(save_layout $MOUNT)
8206         stack_trap "restore_layout $MOUNT $root_layout" EXIT
8207
8208         # new subdirectory under root directory should not inherit
8209         # the default layout from root
8210         local dir1=$MOUNT/$tdir-1
8211         mkdir $dir1 || error "mkdir $dir1 failed"
8212         ! getfattr -n trusted.lov $dir1 &> /dev/null ||
8213                 error "$dir1 shouldn't have LOV EA"
8214
8215         # delete the default layout on root directory
8216         $LFS setstripe -d $MOUNT || error "delete root default layout failed"
8217
8218         local dir2=$MOUNT/$tdir-2
8219         mkdir $dir2 || error "mkdir $dir2 failed"
8220         ! getfattr -n trusted.lov $dir2 &> /dev/null ||
8221                 error "$dir2 shouldn't have LOV EA"
8222
8223         # set a new striping pattern on root directory
8224         local def_stripe_size=$($LFS getstripe -S $MOUNT)
8225         local new_def_stripe_size=$((def_stripe_size * 2))
8226         $LFS setstripe -S $new_def_stripe_size $MOUNT ||
8227                 error "set stripe size on $MOUNT failed"
8228
8229         # new file created in $dir2 should inherit the new stripe size from
8230         # the filesystem default
8231         local file2=$dir2/$tfile-2
8232         touch $file2 || error "touch $file2 failed"
8233
8234         local file2_stripe_size=$($LFS getstripe -S $file2)
8235         [[ $file2_stripe_size -eq $new_def_stripe_size ]] ||
8236                 error "$file2 didn't inherit stripe size $new_def_stripe_size"
8237
8238         local dir3=$MOUNT/$tdir-3
8239         mkdir $dir3 || error "mkdir $dir3 failed"
8240         # $dir3 shouldn't have LOV EA, but "lfs getstripe -d $dir3" should show
8241         # the root layout, which is the actual default layout that will be used
8242         # when new files are created in $dir3.
8243         local dir3_layout=$(get_layout_param $dir3)
8244         local root_dir_layout=$(get_layout_param $MOUNT)
8245         [[ "$dir3_layout" = "$root_dir_layout" ]] ||
8246                 error "$dir3 should show the default layout from $MOUNT"
8247
8248         # set OST pool on root directory
8249         local pool=$TESTNAME
8250         pool_add $pool || error "add $pool failed"
8251         pool_add_targets $pool 0 $((OSTCOUNT - 1)) 1 ||
8252                 error "add targets to $pool failed"
8253
8254         $LFS setstripe -p $pool $MOUNT ||
8255                 error "set OST pool on $MOUNT failed"
8256
8257         # new file created in $dir3 should inherit the pool from
8258         # the filesystem default
8259         local file3=$dir3/$tfile-3
8260         touch $file3 || error "touch $file3 failed"
8261
8262         local file3_pool=$($LFS getstripe -p $file3)
8263         [[ "$file3_pool" = "$pool" ]] ||
8264                 error "$file3 didn't inherit OST pool $pool"
8265
8266         local dir4=$MOUNT/$tdir-4
8267         mkdir $dir4 || error "mkdir $dir4 failed"
8268         local dir4_layout=$(get_layout_param $dir4)
8269         root_dir_layout=$(get_layout_param $MOUNT)
8270         echo "$LFS getstripe -d $dir4"
8271         $LFS getstripe -d $dir4
8272         echo "$LFS getstripe -d $MOUNT"
8273         $LFS getstripe -d $MOUNT
8274         [[ "$dir4_layout" = "$root_dir_layout" ]] ||
8275                 error "$dir4 should show the default layout from $MOUNT"
8276
8277         # new file created in $dir4 should inherit the pool from
8278         # the filesystem default
8279         local file4=$dir4/$tfile-4
8280         touch $file4 || error "touch $file4 failed"
8281
8282         local file4_pool=$($LFS getstripe -p $file4)
8283         [[ "$file4_pool" = "$pool" ]] ||
8284                 error "$file4 didn't inherit OST pool $pool"
8285
8286         # new subdirectory under non-root directory should inherit
8287         # the default layout from its parent directory
8288         $LFS setstripe -S $new_def_stripe_size -p $pool $dir4 ||
8289                 error "set directory layout on $dir4 failed"
8290
8291         local dir5=$dir4/$tdir-5
8292         mkdir $dir5 || error "mkdir $dir5 failed"
8293
8294         dir4_layout=$(get_layout_param $dir4)
8295         local dir5_layout=$(get_layout_param $dir5)
8296         [[ "$dir4_layout" = "$dir5_layout" ]] ||
8297                 error "$dir5 should inherit the default layout from $dir4"
8298
8299         # though subdir under ROOT doesn't inherit default layout, but
8300         # its sub dir/file should be created with default layout.
8301         [[ $MDSCOUNT -ge 2 ]] || skip_env "needs >= 2 MDTs"
8302         [[ $MDS1_VERSION -ge $(version_code 2.12.59) ]] ||
8303                 skip "Need MDS version at least 2.12.59"
8304
8305         local default_lmv_count=$($LFS getdirstripe -D -c $MOUNT)
8306         local default_lmv_index=$($LFS getdirstripe -D -i $MOUNT)
8307         local default_lmv_hash=$($LFS getdirstripe -D -H $MOUNT)
8308
8309         if [ $default_lmv_hash == "none" ]; then
8310                 stack_trap "$LFS setdirstripe -D -d $MOUNT" EXIT
8311         else
8312                 stack_trap "$LFS setdirstripe -D -i $default_lmv_index \
8313                         -c $default_lmv_count -H $default_lmv_hash $MOUNT" EXIT
8314         fi
8315
8316         $LFS setdirstripe -D -c 2 $MOUNT ||
8317                 error "setdirstripe -D -c 2 failed"
8318         mkdir $MOUNT/$tdir-6 || error "mkdir $tdir-6 failed"
8319         local lmv_count=$($LFS getdirstripe -c $MOUNT/$tdir-6)
8320         [ $lmv_count -eq 2 ] || error "$tdir-6 stripe count $lmv_count"
8321 }
8322 run_test 65n "don't inherit default layout from root for new subdirectories"
8323
8324 # bug 2543 - update blocks count on client
8325 test_66() {
8326         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8327
8328         COUNT=${COUNT:-8}
8329         dd if=/dev/zero of=$DIR/f66 bs=1k count=$COUNT
8330         sync; sync_all_data; sync; sync_all_data
8331         cancel_lru_locks osc
8332         BLOCKS=`ls -s $DIR/f66 | awk '{ print $1 }'`
8333         [ $BLOCKS -ge $COUNT ] || error "$DIR/f66 blocks $BLOCKS < $COUNT"
8334 }
8335 run_test 66 "update inode blocks count on client ==============="
8336
8337 meminfo() {
8338         awk '($1 == "'$1':") { print $2 }' /proc/meminfo
8339 }
8340
8341 swap_used() {
8342         swapon -s | awk '($1 == "'$1'") { print $4 }'
8343 }
8344
8345 # bug5265, obdfilter oa2dentry return -ENOENT
8346 # #define OBD_FAIL_SRV_ENOENT 0x217
8347 test_69() {
8348         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8349         remote_ost_nodsh && skip "remote OST with nodsh"
8350
8351         f="$DIR/$tfile"
8352         $LFS setstripe -c 1 -i 0 $f
8353
8354         $DIRECTIO write ${f}.2 0 1 || error "directio write error"
8355
8356         do_facet ost1 lctl set_param fail_loc=0x217
8357         $TRUNCATE $f 1 # vmtruncate() will ignore truncate() error.
8358         $DIRECTIO write $f 0 2 && error "write succeeded, expect -ENOENT"
8359
8360         do_facet ost1 lctl set_param fail_loc=0
8361         $DIRECTIO write $f 0 2 || error "write error"
8362
8363         cancel_lru_locks osc
8364         $DIRECTIO read $f 0 1 || error "read error"
8365
8366         do_facet ost1 lctl set_param fail_loc=0x217
8367         $DIRECTIO read $f 1 1 && error "read succeeded, expect -ENOENT"
8368
8369         do_facet ost1 lctl set_param fail_loc=0
8370         rm -f $f
8371 }
8372 run_test 69 "verify oa2dentry return -ENOENT doesn't LBUG ======"
8373
8374 test_71() {
8375         test_mkdir $DIR/$tdir
8376         $LFS setdirstripe -D -c$MDSCOUNT $DIR/$tdir
8377         sh rundbench -C -D $DIR/$tdir 2 || error "dbench failed!"
8378 }
8379 run_test 71 "Running dbench on lustre (don't segment fault) ===="
8380
8381 test_72a() { # bug 5695 - Test that on 2.6 remove_suid works properly
8382         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8383         [ "$RUNAS_ID" = "$UID" ] &&
8384                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8385         # Check that testing environment is properly set up. Skip if not
8386         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_GID $RUNAS ||
8387                 skip_env "User $RUNAS_ID does not exist - skipping"
8388
8389         touch $DIR/$tfile
8390         chmod 777 $DIR/$tfile
8391         chmod ug+s $DIR/$tfile
8392         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=512 count=1 ||
8393                 error "$RUNAS dd $DIR/$tfile failed"
8394         # See if we are still setuid/sgid
8395         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8396                 error "S/gid is not dropped on write"
8397         # Now test that MDS is updated too
8398         cancel_lru_locks mdc
8399         [ -u $DIR/$tfile ] || [ -g $DIR/$tfile ] &&
8400                 error "S/gid is not dropped on MDS"
8401         rm -f $DIR/$tfile
8402 }
8403 run_test 72a "Test that remove suid works properly (bug5695) ===="
8404
8405 test_72b() { # bug 24226 -- keep mode setting when size is not changing
8406         local perm
8407
8408         [ "$RUNAS_ID" = "$UID" ] &&
8409                 skip_env "RUNAS_ID = UID = $UID -- skipping"
8410         [ "$RUNAS_ID" -eq 0 ] &&
8411                 skip_env "RUNAS_ID = 0 -- skipping"
8412         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8413         # Check that testing environment is properly set up. Skip if not
8414         FAIL_ON_ERROR=false check_runas_id_ret $RUNAS_ID $RUNAS_ID $RUNAS ||
8415                 skip_env "User $RUNAS_ID does not exist - skipping"
8416
8417         touch $DIR/${tfile}-f{g,u}
8418         test_mkdir $DIR/${tfile}-dg
8419         test_mkdir $DIR/${tfile}-du
8420         chmod 770 $DIR/${tfile}-{f,d}{g,u}
8421         chmod g+s $DIR/${tfile}-{f,d}g
8422         chmod u+s $DIR/${tfile}-{f,d}u
8423         for perm in 777 2777 4777; do
8424                 $RUNAS chmod $perm $DIR/${tfile}-fg && error "S/gid file allowed improper chmod to $perm"
8425                 $RUNAS chmod $perm $DIR/${tfile}-fu && error "S/uid file allowed improper chmod to $perm"
8426                 $RUNAS chmod $perm $DIR/${tfile}-dg && error "S/gid dir allowed improper chmod to $perm"
8427                 $RUNAS chmod $perm $DIR/${tfile}-du && error "S/uid dir allowed improper chmod to $perm"
8428         done
8429         true
8430 }
8431 run_test 72b "Test that we keep mode setting if without file data changed (bug 24226)"
8432
8433 # bug 3462 - multiple simultaneous MDC requests
8434 test_73() {
8435         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8436
8437         test_mkdir $DIR/d73-1
8438         test_mkdir $DIR/d73-2
8439         multiop_bg_pause $DIR/d73-1/f73-1 O_c || return 1
8440         pid1=$!
8441
8442         lctl set_param fail_loc=0x80000129
8443         $MULTIOP $DIR/d73-1/f73-2 Oc &
8444         sleep 1
8445         lctl set_param fail_loc=0
8446
8447         $MULTIOP $DIR/d73-2/f73-3 Oc &
8448         pid3=$!
8449
8450         kill -USR1 $pid1
8451         wait $pid1 || return 1
8452
8453         sleep 25
8454
8455         $CHECKSTAT -t file $DIR/d73-1/f73-1 || return 4
8456         $CHECKSTAT -t file $DIR/d73-1/f73-2 || return 5
8457         $CHECKSTAT -t file $DIR/d73-2/f73-3 || return 6
8458
8459         rm -rf $DIR/d73-*
8460 }
8461 run_test 73 "multiple MDC requests (should not deadlock)"
8462
8463 test_74a() { # bug 6149, 6184
8464         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8465
8466         touch $DIR/f74a
8467         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8468         #
8469         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8470         # will spin in a tight reconnection loop
8471         $LCTL set_param fail_loc=0x8000030e
8472         # get any lock that won't be difficult - lookup works.
8473         ls $DIR/f74a
8474         $LCTL set_param fail_loc=0
8475         rm -f $DIR/f74a
8476         true
8477 }
8478 run_test 74a "ldlm_enqueue freed-export error path, ls (shouldn't LBUG)"
8479
8480 test_74b() { # bug 13310
8481         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8482
8483         #define OBD_FAIL_LDLM_ENQUEUE_OLD_EXPORT 0x30e
8484         #
8485         # very important to OR with OBD_FAIL_ONCE (0x80000000) -- otherwise it
8486         # will spin in a tight reconnection loop
8487         $LCTL set_param fail_loc=0x8000030e
8488         # get a "difficult" lock
8489         touch $DIR/f74b
8490         $LCTL set_param fail_loc=0
8491         rm -f $DIR/f74b
8492         true
8493 }
8494 run_test 74b "ldlm_enqueue freed-export error path, touch (shouldn't LBUG)"
8495
8496 test_74c() {
8497         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8498
8499         #define OBD_FAIL_LDLM_NEW_LOCK
8500         $LCTL set_param fail_loc=0x319
8501         touch $DIR/$tfile && error "touch successful"
8502         $LCTL set_param fail_loc=0
8503         true
8504 }
8505 run_test 74c "ldlm_lock_create error path, (shouldn't LBUG)"
8506
8507 num_inodes() {
8508         awk '/lustre_inode_cache/ {print $2; exit}' /proc/slabinfo
8509 }
8510
8511 test_76() { # Now for bug 20433, added originally in bug 1443
8512         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8513
8514         cancel_lru_locks osc
8515         local cpus=$(getconf _NPROCESSORS_ONLN 2>/dev/null)
8516         local before=$(num_inodes)
8517         local count=$((512 * cpus))
8518         [ "$SLOW" = "no" ] && count=$((64 * cpus))
8519
8520         echo "before inodes: $before"
8521         for i in $(seq $count); do
8522                 touch $DIR/$tfile
8523                 rm -f $DIR/$tfile
8524         done
8525         cancel_lru_locks osc
8526         local after=$(num_inodes)
8527         echo "after inodes: $after"
8528         while (( after > before + 8 * ${cpus:-1} )); do
8529                 sleep 1
8530                 after=$(num_inodes)
8531                 wait=$((wait + 1))
8532                 (( wait % 5 == 0 )) && echo "wait $wait seconds inodes: $after"
8533                 if (( wait > 30 )); then
8534                         error "inode slab grew from $before to $after"
8535                 fi
8536         done
8537 }
8538 run_test 76 "confirm clients recycle inodes properly ===="
8539
8540
8541 export ORIG_CSUM=""
8542 set_checksums()
8543 {
8544         # Note: in sptlrpc modes which enable its own bulk checksum, the
8545         # original crc32_le bulk checksum will be automatically disabled,
8546         # and the OBD_FAIL_OSC_CHECKSUM_SEND/OBD_FAIL_OSC_CHECKSUM_RECEIVE
8547         # will be checked by sptlrpc code against sptlrpc bulk checksum.
8548         # In this case set_checksums() will not be no-op, because sptlrpc
8549         # bulk checksum will be enabled all through the test.
8550
8551         [ "$ORIG_CSUM" ] || ORIG_CSUM=`lctl get_param -n osc.*.checksums | head -n1`
8552         lctl set_param -n osc.*.checksums $1
8553         return 0
8554 }
8555
8556 export ORIG_CSUM_TYPE="`lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8557                         sed 's/.*\[\(.*\)\].*/\1/g' | head -n1`"
8558 CKSUM_TYPES=${CKSUM_TYPES:-$(lctl get_param -n osc.*osc-[^mM]*.checksum_type |
8559                              tr -d [] | head -n1)}
8560 set_checksum_type()
8561 {
8562         lctl set_param -n osc.*osc-[^mM]*.checksum_type $1
8563         rc=$?
8564         log "set checksum type to $1, rc = $rc"
8565         return $rc
8566 }
8567
8568 get_osc_checksum_type()
8569 {
8570         # arugment 1: OST name, like OST0000
8571         ost=$1
8572         checksum_type=$(lctl get_param -n osc.*${ost}-osc-[^mM]*.checksum_type |
8573                         sed 's/.*\[\(.*\)\].*/\1/g')
8574         rc=$?
8575         [ $rc -ne 0 ] && error "failed to get checksum type of $ost, rc = $rc, output = $checksum_type"
8576         echo $checksum_type
8577 }
8578
8579 F77_TMP=$TMP/f77-temp
8580 F77SZ=8
8581 setup_f77() {
8582         dd if=/dev/urandom of=$F77_TMP bs=1M count=$F77SZ || \
8583                 error "error writing to $F77_TMP"
8584 }
8585
8586 test_77a() { # bug 10889
8587         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8588         $GSS && skip_env "could not run with gss"
8589
8590         [ ! -f $F77_TMP ] && setup_f77
8591         set_checksums 1
8592         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ || error "dd error"
8593         set_checksums 0
8594         rm -f $DIR/$tfile
8595 }
8596 run_test 77a "normal checksum read/write operation"
8597
8598 test_77b() { # bug 10889
8599         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8600         $GSS && skip_env "could not run with gss"
8601
8602         [ ! -f $F77_TMP ] && setup_f77
8603         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8604         $LCTL set_param fail_loc=0x80000409
8605         set_checksums 1
8606
8607         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8608                 error "dd error: $?"
8609         $LCTL set_param fail_loc=0
8610
8611         for algo in $CKSUM_TYPES; do
8612                 cancel_lru_locks osc
8613                 set_checksum_type $algo
8614                 #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8615                 $LCTL set_param fail_loc=0x80000408
8616                 cmp $F77_TMP $DIR/$tfile || error "file compare failed"
8617                 $LCTL set_param fail_loc=0
8618         done
8619         set_checksums 0
8620         set_checksum_type $ORIG_CSUM_TYPE
8621         rm -f $DIR/$tfile
8622 }
8623 run_test 77b "checksum error on client write, read"
8624
8625 cleanup_77c() {
8626         trap 0
8627         set_checksums 0
8628         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=0
8629         $check_ost &&
8630                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=0
8631         [ -n "$osc_file_prefix" ] && rm -f ${osc_file_prefix}*
8632         $check_ost && [ -n "$ost_file_prefix" ] &&
8633                 do_facet ost1 rm -f ${ost_file_prefix}\*
8634 }
8635
8636 test_77c() {
8637         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8638         $GSS && skip_env "could not run with gss"
8639         remote_ost_nodsh && skip "remote OST with nodsh"
8640
8641         local bad1
8642         local osc_file_prefix
8643         local osc_file
8644         local check_ost=false
8645         local ost_file_prefix
8646         local ost_file
8647         local orig_cksum
8648         local dump_cksum
8649         local fid
8650
8651         # ensure corruption will occur on first OSS/OST
8652         $LFS setstripe -i 0 $DIR/$tfile
8653
8654         [ ! -f $F77_TMP ] && setup_f77
8655         dd if=$F77_TMP of=$DIR/$tfile bs=1M count=$F77SZ conv=sync ||
8656                 error "dd write error: $?"
8657         fid=$($LFS path2fid $DIR/$tfile)
8658
8659         if [ $OST1_VERSION -ge $(version_code 2.9.57) ]
8660         then
8661                 check_ost=true
8662                 ost_file_prefix=$(do_facet ost1 $LCTL get_param -n debug_path)
8663                 ost_file_prefix=${ost_file_prefix}-checksum_dump-ost-\\${fid}
8664         else
8665                 echo "OSS do not support bulk pages dump upon error"
8666         fi
8667
8668         osc_file_prefix=$($LCTL get_param -n debug_path)
8669         osc_file_prefix=${osc_file_prefix}-checksum_dump-osc-\\${fid}
8670
8671         trap cleanup_77c EXIT
8672
8673         set_checksums 1
8674         # enable bulk pages dump upon error on Client
8675         $LCTL set_param osc.*osc-[^mM]*.checksum_dump=1
8676         # enable bulk pages dump upon error on OSS
8677         $check_ost &&
8678                 do_facet ost1 $LCTL set_param obdfilter.*-OST*.checksum_dump=1
8679
8680         # flush Client cache to allow next read to reach OSS
8681         cancel_lru_locks osc
8682
8683         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE       0x408
8684         $LCTL set_param fail_loc=0x80000408
8685         dd if=$DIR/$tfile of=/dev/null bs=1M || error "dd read error: $?"
8686         $LCTL set_param fail_loc=0
8687
8688         rm -f $DIR/$tfile
8689
8690         # check cksum dump on Client
8691         osc_file=$(ls ${osc_file_prefix}*)
8692         [ -n "$osc_file" ] || error "no checksum dump file on Client"
8693         # OBD_FAIL_OSC_CHECKSUM_RECEIVE corrupts with "bad1" at start of file
8694         bad1=$(dd if=$osc_file bs=1 count=4 2>/dev/null) || error "dd error: $?"
8695         [ $bad1 == "bad1" ] || error "unexpected corrupt pattern"
8696         orig_cksum=$(dd if=$F77_TMP bs=1 skip=4 count=1048572 2>/dev/null |
8697                      cksum)
8698         dump_cksum=$(dd if=$osc_file bs=1 skip=4 2>/dev/null | cksum)
8699         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8700                 error "dump content does not match on Client"
8701
8702         $check_ost || skip "No need to check cksum dump on OSS"
8703
8704         # check cksum dump on OSS
8705         ost_file=$(do_facet ost1 ls ${ost_file_prefix}\*)
8706         [ -n "$ost_file" ] || error "no checksum dump file on OSS"
8707         orig_cksum=$(dd if=$F77_TMP bs=1048576 count=1 2>/dev/null | cksum)
8708         dump_cksum=$(do_facet ost1 dd if=$ost_file 2>/dev/null \| cksum)
8709         [[ "$orig_cksum" == "$dump_cksum" ]] ||
8710                 error "dump content does not match on OSS"
8711
8712         cleanup_77c
8713 }
8714 run_test 77c "checksum error on client read with debug"
8715
8716 test_77d() { # bug 10889
8717         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8718         $GSS && skip_env "could not run with gss"
8719
8720         #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8721         $LCTL set_param fail_loc=0x80000409
8722         set_checksums 1
8723         $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8724                 error "direct write: rc=$?"
8725         $LCTL set_param fail_loc=0
8726         set_checksums 0
8727
8728         #define OBD_FAIL_OSC_CHECKSUM_RECEIVE    0x408
8729         $LCTL set_param fail_loc=0x80000408
8730         set_checksums 1
8731         cancel_lru_locks osc
8732         $DIRECTIO read $DIR/$tfile 0 $F77SZ $((1024 * 1024)) ||
8733                 error "direct read: rc=$?"
8734         $LCTL set_param fail_loc=0
8735         set_checksums 0
8736 }
8737 run_test 77d "checksum error on OST direct write, read"
8738
8739 test_77f() { # bug 10889
8740         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8741         $GSS && skip_env "could not run with gss"
8742
8743         set_checksums 1
8744         for algo in $CKSUM_TYPES; do
8745                 cancel_lru_locks osc
8746                 set_checksum_type $algo
8747                 #define OBD_FAIL_OSC_CHECKSUM_SEND       0x409
8748                 $LCTL set_param fail_loc=0x409
8749                 $DIRECTIO write $DIR/$tfile 0 $F77SZ $((1024 * 1024)) &&
8750                         error "direct write succeeded"
8751                 $LCTL set_param fail_loc=0
8752         done
8753         set_checksum_type $ORIG_CSUM_TYPE
8754         set_checksums 0
8755 }
8756 run_test 77f "repeat checksum error on write (expect error)"
8757
8758 test_77g() { # bug 10889
8759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8760         $GSS && skip_env "could not run with gss"
8761         remote_ost_nodsh && skip "remote OST with nodsh"
8762
8763         [ ! -f $F77_TMP ] && setup_f77
8764
8765         local file=$DIR/$tfile
8766         stack_trap "rm -f $file" EXIT
8767
8768         $LFS setstripe -c 1 -i 0 $file
8769         #define OBD_FAIL_OST_CHECKSUM_RECEIVE       0x21a
8770         do_facet ost1 lctl set_param fail_loc=0x8000021a
8771         set_checksums 1
8772         dd if=$F77_TMP of=$file bs=1M count=$F77SZ ||
8773                 error "write error: rc=$?"
8774         do_facet ost1 lctl set_param fail_loc=0
8775         set_checksums 0
8776
8777         cancel_lru_locks osc
8778         #define OBD_FAIL_OST_CHECKSUM_SEND          0x21b
8779         do_facet ost1 lctl set_param fail_loc=0x8000021b
8780         set_checksums 1
8781         cmp $F77_TMP $file || error "file compare failed"
8782         do_facet ost1 lctl set_param fail_loc=0
8783         set_checksums 0
8784 }
8785 run_test 77g "checksum error on OST write, read"
8786
8787 test_77k() { # LU-10906
8788         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8789         $GSS && skip_env "could not run with gss"
8790
8791         local cksum_param="osc.$FSNAME*.checksums"
8792         local get_checksum="$LCTL get_param -n $cksum_param | head -n1"
8793         local checksum
8794         local i
8795
8796         [ "$ORIG_CSUM" ] || ORIG_CSUM=$(eval $get_checksum)
8797         stack_trap "wait_update $HOSTNAME '$get_checksum' $ORIG_CSUM" EXIT
8798         stack_trap "do_facet mgs $LCTL set_param -P $cksum_param=$ORIG_CSUM" \
8799                 EXIT
8800
8801         for i in 0 1; do
8802                 do_facet mgs $LCTL set_param -P $cksum_param=$i ||
8803                         error "failed to set checksum=$i on MGS"
8804                 wait_update $HOSTNAME "$get_checksum" $i
8805                 #remount
8806                 echo "remount client, checksum should be $i"
8807                 remount_client $MOUNT || error "failed to remount client"
8808                 checksum=$(eval $get_checksum)
8809                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8810         done
8811         # remove persistent param to avoid races with checksum mountopt below
8812         do_facet mgs $LCTL set_param -P -d $cksum_param ||
8813                 error "failed to delete checksum on MGS"
8814
8815         for opt in "checksum" "nochecksum"; do
8816                 #remount with mount option
8817                 echo "remount client with option $opt, checksum should be $i"
8818                 umount_client $MOUNT || error "failed to umount client"
8819                 mount_client $MOUNT "$MOUNT_OPTS,$opt" ||
8820                         error "failed to mount client with option '$opt'"
8821                 checksum=$(eval $get_checksum)
8822                 [ $checksum -eq $i ] || error "checksum($checksum) != $i"
8823                 i=$((i - 1))
8824         done
8825
8826         remount_client $MOUNT || error "failed to remount client"
8827 }
8828 run_test 77k "enable/disable checksum correctly"
8829
8830 test_77l() {
8831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8832         $GSS && skip_env "could not run with gss"
8833
8834         set_checksums 1
8835         stack_trap "set_checksums $ORIG_CSUM" EXIT
8836         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
8837
8838         set_checksum_type invalid && error "unexpected success of invalid checksum type"
8839
8840         $LFS setstripe -c 1 -i 0 $DIR/$tfile
8841         for algo in $CKSUM_TYPES; do
8842                 set_checksum_type $algo || error "fail to set checksum type $algo"
8843                 osc_algo=$(get_osc_checksum_type OST0000)
8844                 [ "$osc_algo" != "$algo" ] && error "checksum type is $osc_algo after setting it to $algo"
8845
8846                 # no locks, no reqs to let the connection idle
8847                 cancel_lru_locks osc
8848                 lru_resize_disable osc
8849                 wait_osc_import_state client ost1 IDLE
8850
8851                 # ensure ost1 is connected
8852                 stat $DIR/$tfile >/dev/null || error "can't stat"
8853                 wait_osc_import_state client ost1 FULL
8854
8855                 osc_algo=$(get_osc_checksum_type OST0000)
8856                 [ "$osc_algo" != "$algo" ] && error "checksum type changed from $algo to $osc_algo after reconnection"
8857         done
8858         return 0
8859 }
8860 run_test 77l "preferred checksum type is remembered after reconnected"
8861
8862 [ "$ORIG_CSUM" ] && set_checksums $ORIG_CSUM || true
8863 rm -f $F77_TMP
8864 unset F77_TMP
8865
8866 cleanup_test_78() {
8867         trap 0
8868         rm -f $DIR/$tfile
8869 }
8870
8871 test_78() { # bug 10901
8872         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8873         remote_ost || skip_env "local OST"
8874
8875         NSEQ=5
8876         F78SIZE=$(($(awk '/MemFree:/ { print $2 }' /proc/meminfo) / 1024))
8877         echo "MemFree: $F78SIZE, Max file size: $MAXFREE"
8878         MEMTOTAL=$(($(awk '/MemTotal:/ { print $2 }' /proc/meminfo) / 1024))
8879         echo "MemTotal: $MEMTOTAL"
8880
8881         # reserve 256MB of memory for the kernel and other running processes,
8882         # and then take 1/2 of the remaining memory for the read/write buffers.
8883         if [ $MEMTOTAL -gt 512 ] ;then
8884                 MEMTOTAL=$(((MEMTOTAL - 256 ) / 2))
8885         else
8886                 # for those poor memory-starved high-end clusters...
8887                 MEMTOTAL=$((MEMTOTAL / 2))
8888         fi
8889         echo "Mem to use for directio: $MEMTOTAL"
8890
8891         [[ $F78SIZE -gt $MEMTOTAL ]] && F78SIZE=$MEMTOTAL
8892         [[ $F78SIZE -gt 512 ]] && F78SIZE=512
8893         [[ $F78SIZE -gt $((MAXFREE / 1024)) ]] && F78SIZE=$((MAXFREE / 1024))
8894         SMALLESTOST=$($LFS df $DIR | grep OST | awk '{ print $4 }' | sort -n |
8895                 head -n1)
8896         echo "Smallest OST: $SMALLESTOST"
8897         [[ $SMALLESTOST -lt 10240 ]] &&
8898                 skip "too small OSTSIZE, useless to run large O_DIRECT test"
8899
8900         trap cleanup_test_78 EXIT
8901
8902         [[ $F78SIZE -gt $((SMALLESTOST * $OSTCOUNT / 1024 - 80)) ]] &&
8903                 F78SIZE=$((SMALLESTOST * $OSTCOUNT / 1024 - 80))
8904
8905         [ "$SLOW" = "no" ] && NSEQ=1 && [ $F78SIZE -gt 32 ] && F78SIZE=32
8906         echo "File size: $F78SIZE"
8907         $LFS setstripe -c $OSTCOUNT $DIR/$tfile || error "setstripe failed"
8908         for i in $(seq 1 $NSEQ); do
8909                 FSIZE=$(($F78SIZE / ($NSEQ - $i + 1)))
8910                 echo directIO rdwr round $i of $NSEQ
8911                 $DIRECTIO rdwr $DIR/$tfile 0 $FSIZE 1048576||error "rdwr failed"
8912         done
8913
8914         cleanup_test_78
8915 }
8916 run_test 78 "handle large O_DIRECT writes correctly ============"
8917
8918 test_79() { # bug 12743
8919         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8920
8921         wait_delete_completed
8922
8923         BKTOTAL=$(calc_osc_kbytes kbytestotal)
8924         BKFREE=$(calc_osc_kbytes kbytesfree)
8925         BKAVAIL=$(calc_osc_kbytes kbytesavail)
8926
8927         STRING=`df -P $MOUNT | tail -n 1 | awk '{print $2","$3","$4}'`
8928         DFTOTAL=`echo $STRING | cut -d, -f1`
8929         DFUSED=`echo $STRING  | cut -d, -f2`
8930         DFAVAIL=`echo $STRING | cut -d, -f3`
8931         DFFREE=$(($DFTOTAL - $DFUSED))
8932
8933         ALLOWANCE=$((64 * $OSTCOUNT))
8934
8935         if [ $DFTOTAL -lt $(($BKTOTAL - $ALLOWANCE)) ] ||
8936            [ $DFTOTAL -gt $(($BKTOTAL + $ALLOWANCE)) ] ; then
8937                 error "df total($DFTOTAL) mismatch OST total($BKTOTAL)"
8938         fi
8939         if [ $DFFREE -lt $(($BKFREE - $ALLOWANCE)) ] ||
8940            [ $DFFREE -gt $(($BKFREE + $ALLOWANCE)) ] ; then
8941                 error "df free($DFFREE) mismatch OST free($BKFREE)"
8942         fi
8943         if [ $DFAVAIL -lt $(($BKAVAIL - $ALLOWANCE)) ] ||
8944            [ $DFAVAIL -gt $(($BKAVAIL + $ALLOWANCE)) ] ; then
8945                 error "df avail($DFAVAIL) mismatch OST avail($BKAVAIL)"
8946         fi
8947 }
8948 run_test 79 "df report consistency check ======================="
8949
8950 test_80() { # bug 10718
8951         remote_ost_nodsh && skip "remote OST with nodsh"
8952         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8953
8954         # relax strong synchronous semantics for slow backends like ZFS
8955         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
8956                 local soc="obdfilter.*.sync_lock_cancel"
8957                 local save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8958
8959                 # "sync_on_lock_cancel" was broken by v2_11_55_0-26-g7059644e9a
8960                 if [ -z "$save" ]; then
8961                         soc="obdfilter.*.sync_on_lock_cancel"
8962                         save=$(do_facet ost1 $LCTL get_param -n $soc | head -n1)
8963                 fi
8964
8965                 if [ "$save" != "never" ]; then
8966                         local hosts=$(comma_list $(osts_nodes))
8967
8968                         do_nodes $hosts $LCTL set_param $soc=never
8969                         stack_trap "do_nodes $hosts $LCTL set_param $soc=$save"
8970                 fi
8971         fi
8972
8973         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1M
8974         sync; sleep 1; sync
8975         local before=$(date +%s)
8976         cancel_lru_locks osc
8977         local after=$(date +%s)
8978         local diff=$((after - before))
8979         [ $diff -le 1 ] || error "elapsed for 1M@1T = $diff"
8980
8981         rm -f $DIR/$tfile
8982 }
8983 run_test 80 "Page eviction is equally fast at high offsets too"
8984
8985 test_81a() { # LU-456
8986         [ $PARALLEL == "yes" ] && skip "skip parallel run"
8987         remote_ost_nodsh && skip "remote OST with nodsh"
8988
8989         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
8990         # MUST OR with the OBD_FAIL_ONCE (0x80000000)
8991         do_facet ost1 lctl set_param fail_loc=0x80000228
8992
8993         # write should trigger a retry and success
8994         $LFS setstripe -i 0 -c 1 $DIR/$tfile
8995         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
8996         RC=$?
8997         if [ $RC -ne 0 ] ; then
8998                 error "write should success, but failed for $RC"
8999         fi
9000 }
9001 run_test 81a "OST should retry write when get -ENOSPC ==============="
9002
9003 test_81b() { # LU-456
9004         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9005         remote_ost_nodsh && skip "remote OST with nodsh"
9006
9007         # define OBD_FAIL_OST_MAPBLK_ENOSPC    0x228
9008         # Don't OR with the OBD_FAIL_ONCE (0x80000000)
9009         do_facet ost1 lctl set_param fail_loc=0x228
9010
9011         # write should retry several times and return -ENOSPC finally
9012         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9013         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
9014         RC=$?
9015         ENOSPC=28
9016         if [ $RC -ne $ENOSPC ] ; then
9017                 error "dd should fail for -ENOSPC, but succeed."
9018         fi
9019 }
9020 run_test 81b "OST should return -ENOSPC when retry still fails ======="
9021
9022 test_82() { # LU-1031
9023         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10
9024         local gid1=14091995
9025         local gid2=16022000
9026
9027         multiop_bg_pause $DIR/$tfile OG${gid1}_g${gid1}c || return 1
9028         local MULTIPID1=$!
9029         multiop_bg_pause $DIR/$tfile O_G${gid2}r10g${gid2}c || return 2
9030         local MULTIPID2=$!
9031         kill -USR1 $MULTIPID2
9032         sleep 2
9033         if [[ `ps h -o comm -p $MULTIPID2` == "" ]]; then
9034                 error "First grouplock does not block second one"
9035         else
9036                 echo "Second grouplock blocks first one"
9037         fi
9038         kill -USR1 $MULTIPID1
9039         wait $MULTIPID1
9040         wait $MULTIPID2
9041 }
9042 run_test 82 "Basic grouplock test"
9043
9044 test_99() {
9045         [ -z "$(which cvs 2>/dev/null)" ] && skip_env "could not find cvs"
9046
9047         test_mkdir $DIR/$tdir.cvsroot
9048         chown $RUNAS_ID $DIR/$tdir.cvsroot
9049
9050         cd $TMP
9051         $RUNAS cvs -d $DIR/$tdir.cvsroot init || error "cvs init failed"
9052
9053         cd /etc/init.d
9054         # some versions of cvs import exit(1) when asked to import links or
9055         # files they can't read.  ignore those files.
9056         local toignore=$(find . -type l -printf '-I %f\n' -o \
9057                          ! -perm /4 -printf '-I %f\n')
9058         $RUNAS cvs -d $DIR/$tdir.cvsroot import -m "nomesg" $toignore \
9059                 $tdir.reposname vtag rtag
9060
9061         cd $DIR
9062         test_mkdir $DIR/$tdir.reposname
9063         chown $RUNAS_ID $DIR/$tdir.reposname
9064         $RUNAS cvs -d $DIR/$tdir.cvsroot co $tdir.reposname
9065
9066         cd $DIR/$tdir.reposname
9067         $RUNAS touch foo99
9068         $RUNAS cvs add -m 'addmsg' foo99
9069         $RUNAS cvs update
9070         $RUNAS cvs commit -m 'nomsg' foo99
9071         rm -fr $DIR/$tdir.cvsroot
9072 }
9073 run_test 99 "cvs strange file/directory operations"
9074
9075 test_100() {
9076         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9077         [[ "$NETTYPE" =~ tcp ]] ||
9078                 skip_env "TCP secure port test, not useful for NETTYPE=$NETTYPE"
9079         remote_ost_nodsh && skip "remote OST with nodsh"
9080         remote_mds_nodsh && skip "remote MDS with nodsh"
9081         remote_servers ||
9082                 skip "useless for local single node setup"
9083
9084         netstat -tna | ( rc=1; while read PROT SND RCV LOCAL REMOTE STAT; do
9085                 [ "$PROT" != "tcp" ] && continue
9086                 RPORT=$(echo $REMOTE | cut -d: -f2)
9087                 [ "$RPORT" != "$ACCEPTOR_PORT" ] && continue
9088
9089                 rc=0
9090                 LPORT=`echo $LOCAL | cut -d: -f2`
9091                 if [ $LPORT -ge 1024 ]; then
9092                         echo "bad: $PROT $SND $RCV $LOCAL $REMOTE $STAT"
9093                         netstat -tna
9094                         error_exit "local: $LPORT > 1024, remote: $RPORT"
9095                 fi
9096         done
9097         [ "$rc" = 0 ] || error_exit "privileged port not found" )
9098 }
9099 run_test 100 "check local port using privileged port ==========="
9100
9101 function get_named_value()
9102 {
9103     local tag
9104
9105     tag=$1
9106     while read ;do
9107         line=$REPLY
9108         case $line in
9109         $tag*)
9110             echo $line | sed "s/^$tag[ ]*//"
9111             break
9112             ;;
9113         esac
9114     done
9115 }
9116
9117 export CACHE_MAX=$($LCTL get_param -n llite.*.max_cached_mb |
9118                    awk '/^max_cached_mb/ { print $2 }')
9119
9120 cleanup_101a() {
9121         $LCTL set_param -n llite.*.max_cached_mb $CACHE_MAX
9122         trap 0
9123 }
9124
9125 test_101a() {
9126         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9127
9128         local s
9129         local discard
9130         local nreads=10000
9131         local cache_limit=32
9132
9133         $LCTL set_param -n osc.*-osc*.rpc_stats 0
9134         trap cleanup_101a EXIT
9135         $LCTL set_param -n llite.*.read_ahead_stats 0
9136         $LCTL set_param -n llite.*.max_cached_mb $cache_limit
9137
9138         #
9139         # randomly read 10000 of 64K chunks from file 3x 32MB in size
9140         #
9141         echo "nreads: $nreads file size: $((cache_limit * 3))MB"
9142         $READS -f $DIR/$tfile -s$((cache_limit * 3192 * 1024)) -b65536 -C -n$nreads -t 180
9143
9144         discard=0
9145         for s in $($LCTL get_param -n llite.*.read_ahead_stats |
9146                 get_named_value 'read but discarded' | cut -d" " -f1); do
9147                         discard=$(($discard + $s))
9148         done
9149         cleanup_101a
9150
9151         $LCTL get_param osc.*-osc*.rpc_stats
9152         $LCTL get_param llite.*.read_ahead_stats
9153
9154         # Discard is generally zero, but sometimes a few random reads line up
9155         # and trigger larger readahead, which is wasted & leads to discards.
9156         if [[ $(($discard)) -gt $nreads ]]; then
9157                 error "too many ($discard) discarded pages"
9158         fi
9159         rm -f $DIR/$tfile || true
9160 }
9161 run_test 101a "check read-ahead for random reads"
9162
9163 setup_test101bc() {
9164         test_mkdir $DIR/$tdir
9165         local ssize=$1
9166         local FILE_LENGTH=$2
9167         STRIPE_OFFSET=0
9168
9169         local FILE_SIZE_MB=$((FILE_LENGTH / ssize))
9170
9171         local list=$(comma_list $(osts_nodes))
9172         set_osd_param $list '' read_cache_enable 0
9173         set_osd_param $list '' writethrough_cache_enable 0
9174
9175         trap cleanup_test101bc EXIT
9176         # prepare the read-ahead file
9177         $LFS setstripe -S $ssize -i $STRIPE_OFFSET -c $OSTCOUNT $DIR/$tfile
9178
9179         dd if=/dev/zero of=$DIR/$tfile bs=$ssize \
9180                                 count=$FILE_SIZE_MB 2> /dev/null
9181
9182 }
9183
9184 cleanup_test101bc() {
9185         trap 0
9186         rm -rf $DIR/$tdir
9187         rm -f $DIR/$tfile
9188
9189         local list=$(comma_list $(osts_nodes))
9190         set_osd_param $list '' read_cache_enable 1
9191         set_osd_param $list '' writethrough_cache_enable 1
9192 }
9193
9194 calc_total() {
9195         awk 'BEGIN{total=0}; {total+=$1}; END{print total}'
9196 }
9197
9198 ra_check_101() {
9199         local READ_SIZE=$1
9200         local STRIPE_SIZE=$2
9201         local FILE_LENGTH=$3
9202         local RA_INC=1048576
9203         local STRIDE_LENGTH=$((STRIPE_SIZE/READ_SIZE))
9204         local discard_limit=$((((STRIDE_LENGTH - 1)*3/(STRIDE_LENGTH*OSTCOUNT))* \
9205                              (STRIDE_LENGTH*OSTCOUNT - STRIDE_LENGTH)))
9206         DISCARD=$($LCTL get_param -n llite.*.read_ahead_stats |
9207                         get_named_value 'read but discarded' |
9208                         cut -d" " -f1 | calc_total)
9209         if [[ $DISCARD -gt $discard_limit ]]; then
9210                 $LCTL get_param llite.*.read_ahead_stats
9211                 error "Too many ($DISCARD) discarded pages with size (${READ_SIZE})"
9212         else
9213                 echo "Read-ahead success for size ${READ_SIZE}"
9214         fi
9215 }
9216
9217 test_101b() {
9218         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9219         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9220
9221         local STRIPE_SIZE=1048576
9222         local STRIDE_SIZE=$((STRIPE_SIZE*OSTCOUNT))
9223
9224         if [ $SLOW == "yes" ]; then
9225                 local FILE_LENGTH=$((STRIDE_SIZE * 64))
9226         else
9227                 local FILE_LENGTH=$((STRIDE_SIZE * 8))
9228         fi
9229
9230         local ITERATION=$((FILE_LENGTH / STRIDE_SIZE))
9231
9232         # prepare the read-ahead file
9233         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9234         cancel_lru_locks osc
9235         for BIDX in 2 4 8 16 32 64 128 256
9236         do
9237                 local BSIZE=$((BIDX*4096))
9238                 local READ_COUNT=$((STRIPE_SIZE/BSIZE))
9239                 local STRIDE_LENGTH=$((STRIDE_SIZE/BSIZE))
9240                 local OFFSET=$((STRIPE_SIZE/BSIZE*(OSTCOUNT - 1)))
9241                 $LCTL set_param -n llite.*.read_ahead_stats 0
9242                 $READS -f $DIR/$tfile  -l $STRIDE_LENGTH -o $OFFSET \
9243                               -s $FILE_LENGTH -b $STRIPE_SIZE -a $READ_COUNT -n $ITERATION
9244                 cancel_lru_locks osc
9245                 ra_check_101 $BSIZE $STRIPE_SIZE $FILE_LENGTH
9246         done
9247         cleanup_test101bc
9248         true
9249 }
9250 run_test 101b "check stride-io mode read-ahead ================="
9251
9252 test_101c() {
9253         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9254
9255         local STRIPE_SIZE=1048576
9256         local FILE_LENGTH=$((STRIPE_SIZE*100))
9257         local nreads=10000
9258         local rsize=65536
9259         local osc_rpc_stats
9260
9261         setup_test101bc $STRIPE_SIZE $FILE_LENGTH
9262
9263         cancel_lru_locks osc
9264         $LCTL set_param osc.*.rpc_stats 0
9265         $READS -f $DIR/$tfile -s$FILE_LENGTH -b$rsize -n$nreads -t 180
9266         $LCTL get_param osc.*.rpc_stats
9267         for osc_rpc_stats in $($LCTL get_param -N osc.*.rpc_stats); do
9268                 local stats=$($LCTL get_param -n $osc_rpc_stats)
9269                 local lines=$(echo "$stats" | awk 'END {print NR;}')
9270                 local size
9271
9272                 if [ $lines -le 20 ]; then
9273                         echo "continue debug"
9274                         continue
9275                 fi
9276                 for size in 1 2 4 8; do
9277                         local rpc=$(echo "$stats" |
9278                                     awk '($1 == "'$size':") {print $2; exit; }')
9279                         [ $rpc != 0 ] && ((size * PAGE_SIZE < rsize)) &&
9280                                 error "Small $((size*PAGE_SIZE)) read IO $rpc!"
9281                 done
9282                 echo "$osc_rpc_stats check passed!"
9283         done
9284         cleanup_test101bc
9285         true
9286 }
9287 run_test 101c "check stripe_size aligned read-ahead ================="
9288
9289 test_101d() {
9290         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9291
9292         local file=$DIR/$tfile
9293         local sz_MB=${FILESIZE_101d:-80}
9294         local ra_MB=${READAHEAD_MB:-40}
9295
9296         local free_MB=$(($(df -P $DIR | tail -n 1 | awk '{ print $4 }') / 1024))
9297         [ $free_MB -lt $sz_MB ] &&
9298                 skip "Need free space ${sz_MB}M, have ${free_MB}M"
9299
9300         echo "Create test file $file size ${sz_MB}M, ${free_MB}M free"
9301         $LFS setstripe -c -1 $file || error "setstripe failed"
9302
9303         dd if=/dev/zero of=$file bs=1M count=$sz_MB || error "dd failed"
9304         echo Cancel LRU locks on lustre client to flush the client cache
9305         cancel_lru_locks osc
9306
9307         echo Disable read-ahead
9308         local old_RA=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9309         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9310         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_RA" EXIT
9311         $LCTL get_param -n llite.*.max_read_ahead_mb
9312
9313         echo "Reading the test file $file with read-ahead disabled"
9314         local sz_KB=$((sz_MB * 1024 / 4))
9315         # 10485760 bytes transferred in 0.000938 secs (11179579337 bytes/sec)
9316         # 104857600 bytes (105 MB) copied, 0.00876352 s, 12.0 GB/s
9317         local raOFF=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
9318                       sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
9319
9320         echo "Cancel LRU locks on lustre client to flush the client cache"
9321         cancel_lru_locks osc
9322         echo Enable read-ahead with ${ra_MB}MB
9323         $LCTL set_param -n llite.*.max_read_ahead_mb=$ra_MB
9324
9325         echo "Reading the test file $file with read-ahead enabled"
9326         local raON=$(LANG=C dd if=$file of=/dev/null bs=4k count=$sz_KB |&
9327                      sed -e '/records/d' -e 's/.* \([0-9]*\.[0-9]*\) *s.*/\1/')
9328
9329         echo "read-ahead disabled time read $raOFF"
9330         echo "read-ahead enabled time read $raON"
9331
9332         rm -f $file
9333         wait_delete_completed
9334
9335         # use awk for this check instead of bash because it handles decimals
9336         awk "{ exit !($raOFF < 1.0 || $raOFF > $raON) }" <<<"ignore_me" ||
9337                 error "readahead ${raON}s > no-readahead ${raOFF}s ${sz_MB}M"
9338 }
9339 run_test 101d "file read with and without read-ahead enabled"
9340
9341 test_101e() {
9342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9343
9344         local file=$DIR/$tfile
9345         local size_KB=500  #KB
9346         local count=100
9347         local bsize=1024
9348
9349         local free_KB=$(df -P $DIR | tail -n 1 | awk '{ print $4 }')
9350         local need_KB=$((count * size_KB))
9351         [[ $free_KB -le $need_KB ]] &&
9352                 skip_env "Need free space $need_KB, have $free_KB"
9353
9354         echo "Creating $count ${size_KB}K test files"
9355         for ((i = 0; i < $count; i++)); do
9356                 dd if=/dev/zero of=$file.$i bs=$bsize count=$size_KB 2>/dev/null
9357         done
9358
9359         echo "Cancel LRU locks on lustre client to flush the client cache"
9360         cancel_lru_locks $OSC
9361
9362         echo "Reset readahead stats"
9363         $LCTL set_param -n llite.*.read_ahead_stats 0
9364
9365         for ((i = 0; i < $count; i++)); do
9366                 dd if=$file.$i of=/dev/null bs=$bsize count=$size_KB 2>/dev/null
9367         done
9368
9369         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9370                      get_named_value 'misses' | cut -d" " -f1 | calc_total)
9371
9372         for ((i = 0; i < $count; i++)); do
9373                 rm -rf $file.$i 2>/dev/null
9374         done
9375
9376         #10000 means 20% reads are missing in readahead
9377         [[ $miss -lt 10000 ]] ||  error "misses too much for small reads"
9378 }
9379 run_test 101e "check read-ahead for small read(1k) for small files(500k)"
9380
9381 test_101f() {
9382         which iozone || skip_env "no iozone installed"
9383
9384         local old_debug=$($LCTL get_param debug)
9385         old_debug=${old_debug#*=}
9386         $LCTL set_param debug="reada mmap"
9387
9388         # create a test file
9389         iozone -i 0 -+n -r 1m -s 128m -w -f $DIR/$tfile > /dev/null 2>&1
9390
9391         echo Cancel LRU locks on lustre client to flush the client cache
9392         cancel_lru_locks osc
9393
9394         echo Reset readahead stats
9395         $LCTL set_param -n llite.*.read_ahead_stats 0
9396
9397         echo mmap read the file with small block size
9398         iozone -i 1 -u 1 -l 1 -+n -r 32k -s 128m -B -f $DIR/$tfile \
9399                 > /dev/null 2>&1
9400
9401         echo checking missing pages
9402         $LCTL get_param llite.*.read_ahead_stats
9403         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9404                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9405
9406         $LCTL set_param debug="$old_debug"
9407         [ $miss -lt 3 ] || error "misses too much pages ('$miss')!"
9408         rm -f $DIR/$tfile
9409 }
9410 run_test 101f "check mmap read performance"
9411
9412 test_101g_brw_size_test() {
9413         local mb=$1
9414         local pages=$((mb * 1048576 / PAGE_SIZE))
9415         local file=$DIR/$tfile
9416
9417         $LCTL set_param osc.*.max_pages_per_rpc=${mb}M ||
9418                 { error "unable to set max_pages_per_rpc=${mb}M"; return 1; }
9419         for mp in $($LCTL get_param -n osc.*.max_pages_per_rpc); do
9420                 [ $mp -ne $pages ] && error "max_pages_per_rpc $mp != $pages" &&
9421                         return 2
9422         done
9423
9424         stack_trap "rm -f $file" EXIT
9425         $LCTL set_param -n osc.*.rpc_stats=0
9426
9427         # 10 RPCs should be enough for the test
9428         local count=10
9429         dd if=/dev/zero of=$file bs=${mb}M count=$count ||
9430                 { error "dd write ${mb} MB blocks failed"; return 3; }
9431         cancel_lru_locks osc
9432         dd of=/dev/null if=$file bs=${mb}M count=$count ||
9433                 { error "dd write ${mb} MB blocks failed"; return 4; }
9434
9435         # calculate number of full-sized read and write RPCs
9436         rpcs=($($LCTL get_param -n 'osc.*.rpc_stats' |
9437                 sed -n '/pages per rpc/,/^$/p' |
9438                 awk '/'$pages':/ { reads += $2; writes += $6 }; \
9439                 END { print reads,writes }'))
9440         [ ${rpcs[0]} -ne $count ] && error "${rpcs[0]} != $count read RPCs" &&
9441                 return 5
9442         [ ${rpcs[1]} -ne $count ] && error "${rpcs[1]} != $count write RPCs" &&
9443                 return 6
9444
9445         return 0
9446 }
9447
9448 test_101g() {
9449         remote_ost_nodsh && skip "remote OST with nodsh"
9450
9451         local rpcs
9452         local osts=$(get_facets OST)
9453         local list=$(comma_list $(osts_nodes))
9454         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
9455         local brw_size="obdfilter.*.brw_size"
9456
9457         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9458
9459         local orig_mb=$(do_facet ost1 $LCTL get_param -n $brw_size | head -n 1)
9460
9461         if { [ $OST1_VERSION -ge $(version_code 2.8.52) ] ||
9462                 { [ $OST1_VERSION -ge $(version_code 2.7.17) ] &&
9463                   [ $OST1_VERSION -lt $(version_code 2.7.50) ]; }; } &&
9464            { [ $CLIENT_VERSION -ge $(version_code 2.8.52) ] ||
9465                 { [ $CLIENT_VERSION -ge $(version_code 2.7.17) ] &&
9466                   [ $CLIENT_VERSION -lt $(version_code 2.7.50) ]; }; }; then
9467
9468                 [ $OST1_VERSION -ge $(version_code 2.9.52) ] &&
9469                         suffix="M"
9470
9471                 if [[ $orig_mb -lt 16 ]]; then
9472                         save_lustre_params $osts "$brw_size" > $p
9473                         do_nodes $list $LCTL set_param -n $brw_size=16$suffix ||
9474                                 error "set 16MB RPC size failed"
9475
9476                         echo "remount client to enable new RPC size"
9477                         remount_client $MOUNT || error "remount_client failed"
9478                 fi
9479
9480                 test_101g_brw_size_test 16 || error "16MB RPC test failed"
9481                 # should be able to set brw_size=12, but no rpc_stats for that
9482                 test_101g_brw_size_test 8 || error "8MB RPC test failed"
9483         fi
9484
9485         test_101g_brw_size_test 4 || error "4MB RPC test failed"
9486
9487         if [[ $orig_mb -lt 16 ]]; then
9488                 restore_lustre_params < $p
9489                 remount_client $MOUNT || error "remount_client restore failed"
9490         fi
9491
9492         rm -f $p $DIR/$tfile
9493 }
9494 run_test 101g "Big bulk(4/16 MiB) readahead"
9495
9496 test_101h() {
9497         $LFS setstripe -i 0 -c 1 $DIR/$tfile
9498
9499         dd if=/dev/zero of=$DIR/$tfile bs=1M count=70 ||
9500                 error "dd 70M file failed"
9501         echo Cancel LRU locks on lustre client to flush the client cache
9502         cancel_lru_locks osc
9503
9504         echo "Reset readahead stats"
9505         $LCTL set_param -n llite.*.read_ahead_stats 0
9506
9507         echo "Read 10M of data but cross 64M bundary"
9508         dd if=$DIR/$tfile of=/dev/null bs=10M skip=6 count=1
9509         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9510                         get_named_value 'misses' | cut -d" " -f1 | calc_total)
9511         [ $miss -eq 1 ] || error "expected miss 1 but got $miss"
9512         rm -f $p $DIR/$tfile
9513 }
9514 run_test 101h "Readahead should cover current read window"
9515
9516 test_101i() {
9517         dd if=/dev/zero of=$DIR/$tfile bs=1M count=10 ||
9518                 error "dd 10M file failed"
9519
9520         local max_per_file_mb=$($LCTL get_param -n \
9521                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
9522         cancel_lru_locks osc
9523         stack_trap "$LCTL set_param llite.*.max_read_ahead_per_file_mb=$max_per_file_mb"
9524         $LCTL set_param llite.*.max_read_ahead_per_file_mb=1 ||
9525                 error "set max_read_ahead_per_file_mb to 1 failed"
9526
9527         echo "Reset readahead stats"
9528         $LCTL set_param llite.*.read_ahead_stats=0
9529
9530         dd if=$DIR/$tfile of=/dev/null bs=2M
9531
9532         $LCTL get_param llite.*.read_ahead_stats
9533         local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9534                      awk '/misses/ { print $2 }')
9535         [ $miss -eq 5 ] || error "expected misses 5 but got $miss"
9536         rm -f $DIR/$tfile
9537 }
9538 run_test 101i "allow current readahead to exceed reservation"
9539
9540 test_101j() {
9541         $LFS setstripe -i 0 -c 1 $DIR/$tfile ||
9542                 error "setstripe $DIR/$tfile failed"
9543         local file_size=$((1048576 * 16))
9544         local old_ra=$($LCTL get_param -n llite.*.max_read_ahead_mb | head -n 1)
9545         stack_trap "$LCTL set_param -n llite.*.max_read_ahead_mb $old_ra" EXIT
9546
9547         echo Disable read-ahead
9548         $LCTL set_param -n llite.*.max_read_ahead_mb=0
9549
9550         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$(($file_size / 1048576))
9551         for blk in $PAGE_SIZE 1048576 $file_size; do
9552                 cancel_lru_locks osc
9553                 echo "Reset readahead stats"
9554                 $LCTL set_param -n llite.*.read_ahead_stats=0
9555                 local count=$(($file_size / $blk))
9556                 dd if=$DIR/$tfile bs=$blk count=$count of=/dev/null
9557                 local miss=$($LCTL get_param -n llite.*.read_ahead_stats |
9558                              get_named_value 'failed to fast read' |
9559                              cut -d" " -f1 | calc_total)
9560                 $LCTL get_param -n llite.*.read_ahead_stats
9561                 [ $miss -eq $count ] || error "expected $count got $miss"
9562         done
9563
9564         rm -f $p $DIR/$tfile
9565 }
9566 run_test 101j "A complete read block should be submitted when no RA"
9567
9568 setup_test102() {
9569         test_mkdir $DIR/$tdir
9570         chown $RUNAS_ID $DIR/$tdir
9571         STRIPE_SIZE=65536
9572         STRIPE_OFFSET=1
9573         STRIPE_COUNT=$OSTCOUNT
9574         [[ $OSTCOUNT -gt 4 ]] && STRIPE_COUNT=4
9575
9576         trap cleanup_test102 EXIT
9577         cd $DIR
9578         $1 $LFS setstripe -S $STRIPE_SIZE -i $STRIPE_OFFSET -c $STRIPE_COUNT $tdir
9579         cd $DIR/$tdir
9580         for num in 1 2 3 4; do
9581                 for count in $(seq 1 $STRIPE_COUNT); do
9582                         for idx in $(seq 0 $[$STRIPE_COUNT - 1]); do
9583                                 local size=`expr $STRIPE_SIZE \* $num`
9584                                 local file=file"$num-$idx-$count"
9585                                 $1 $LFS setstripe -S $size -i $idx -c $count $file
9586                         done
9587                 done
9588         done
9589
9590         cd $DIR
9591         $1 tar cf $TMP/f102.tar $tdir --xattrs
9592 }
9593
9594 cleanup_test102() {
9595         trap 0
9596         rm -f $TMP/f102.tar
9597         rm -rf $DIR/d0.sanity/d102
9598 }
9599
9600 test_102a() {
9601         [ "$UID" != 0 ] && skip "must run as root"
9602         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep xattr)" ] &&
9603                 skip_env "must have user_xattr"
9604
9605         [ -z "$(which setfattr 2>/dev/null)" ] &&
9606                 skip_env "could not find setfattr"
9607
9608         local testfile=$DIR/$tfile
9609
9610         touch $testfile
9611         echo "set/get xattr..."
9612         setfattr -n trusted.name1 -v value1 $testfile ||
9613                 error "setfattr -n trusted.name1=value1 $testfile failed"
9614         getfattr -n trusted.name1 $testfile 2> /dev/null |
9615           grep "trusted.name1=.value1" ||
9616                 error "$testfile missing trusted.name1=value1"
9617
9618         setfattr -n user.author1 -v author1 $testfile ||
9619                 error "setfattr -n user.author1=author1 $testfile failed"
9620         getfattr -n user.author1 $testfile 2> /dev/null |
9621           grep "user.author1=.author1" ||
9622                 error "$testfile missing trusted.author1=author1"
9623
9624         echo "listxattr..."
9625         setfattr -n trusted.name2 -v value2 $testfile ||
9626                 error "$testfile unable to set trusted.name2"
9627         setfattr -n trusted.name3 -v value3 $testfile ||
9628                 error "$testfile unable to set trusted.name3"
9629         [ $(getfattr -d -m "^trusted" $testfile 2> /dev/null |
9630             grep "trusted.name" | wc -l) -eq 3 ] ||
9631                 error "$testfile missing 3 trusted.name xattrs"
9632
9633         setfattr -n user.author2 -v author2 $testfile ||
9634                 error "$testfile unable to set user.author2"
9635         setfattr -n user.author3 -v author3 $testfile ||
9636                 error "$testfile unable to set user.author3"
9637         [ $(getfattr -d -m "^user" $testfile 2> /dev/null |
9638             grep "user.author" | wc -l) -eq 3 ] ||
9639                 error "$testfile missing 3 user.author xattrs"
9640
9641         echo "remove xattr..."
9642         setfattr -x trusted.name1 $testfile ||
9643                 error "$testfile error deleting trusted.name1"
9644         getfattr -d -m trusted $testfile 2> /dev/null | grep "trusted.name1" &&
9645                 error "$testfile did not delete trusted.name1 xattr"
9646
9647         setfattr -x user.author1 $testfile ||
9648                 error "$testfile error deleting user.author1"
9649         echo "set lustre special xattr ..."
9650         $LFS setstripe -c1 $testfile
9651         local lovea=$(getfattr -n "trusted.lov" -e hex $testfile |
9652                 awk -F "=" '/trusted.lov/ { print $2 }' )
9653         setfattr -n "trusted.lov" -v $lovea $testfile ||
9654                 error "$testfile doesn't ignore setting trusted.lov again"
9655         setfattr -n "trusted.lov" -v "invalid_value" $testfile &&
9656                 error "$testfile allow setting invalid trusted.lov"
9657         rm -f $testfile
9658 }
9659 run_test 102a "user xattr test =================================="
9660
9661 check_102b_layout() {
9662         local layout="$*"
9663         local testfile=$DIR/$tfile
9664
9665         echo "test layout '$layout'"
9666         $LFS setstripe $layout $testfile || error "setstripe failed"
9667         $LFS getstripe -y $testfile
9668
9669         echo "get/set/list trusted.lov xattr ..." # b=10930
9670         local value=$(getfattr -n trusted.lov -e hex $testfile | grep trusted)
9671         [[ "$value" =~ "trusted.lov" ]] ||
9672                 error "can't get trusted.lov from $testfile"
9673         local stripe_count_orig=$($LFS getstripe -c $testfile) ||
9674                 error "getstripe failed"
9675
9676         $MCREATE $testfile.2 || error "mcreate $testfile.2 failed"
9677
9678         value=$(cut -d= -f2 <<<$value)
9679         # LU-13168: truncated xattr should fail if short lov_user_md header
9680         [ $CLIENT_VERSION -lt $(version_code 2.13.53) ] &&
9681                 lens="${#value}" || lens="$(seq 4 2 ${#value})"
9682         for len in $lens; do
9683                 echo "setfattr $len $testfile.2"
9684                 setfattr -n trusted.lov -v ${value:0:$len} $testfile.2 &&
9685                         [ $len -lt 66 ] && error "short xattr len=$len worked"
9686         done
9687         local stripe_size=$($LFS getstripe -S $testfile.2)
9688         local stripe_count=$($LFS getstripe -c $testfile.2)
9689         [[ $stripe_size -eq 65536 ]] ||
9690                 error "stripe size $stripe_size != 65536"
9691         [[ $stripe_count -eq $stripe_count_orig ]] ||
9692                 error "stripe count $stripe_count != $stripe_count_orig"
9693         rm $testfile $testfile.2
9694 }
9695
9696 test_102b() {
9697         [ -z "$(which setfattr 2>/dev/null)" ] &&
9698                 skip_env "could not find setfattr"
9699         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9700
9701         # check plain layout
9702         check_102b_layout -S 65536 -i 1 -c $OSTCOUNT
9703
9704         # and also check composite layout
9705         check_102b_layout -E 1M -S 65536 -i 1 -c $OSTCOUNT -Eeof -S4M
9706
9707 }
9708 run_test 102b "getfattr/setfattr for trusted.lov EAs"
9709
9710 test_102c() {
9711         [ -z "$(which setfattr 2>/dev/null)" ] &&
9712                 skip_env "could not find setfattr"
9713         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9714
9715         # b10930: get/set/list lustre.lov xattr
9716         echo "get/set/list lustre.lov xattr ..."
9717         test_mkdir $DIR/$tdir
9718         chown $RUNAS_ID $DIR/$tdir
9719         local testfile=$DIR/$tdir/$tfile
9720         $RUNAS $LFS setstripe -S 65536 -i 1 -c $OSTCOUNT $testfile ||
9721                 error "setstripe failed"
9722         local STRIPECOUNT=$($RUNAS $LFS getstripe -c $testfile) ||
9723                 error "getstripe failed"
9724         $RUNAS getfattr -d -m "^lustre" $testfile 2> /dev/null | \
9725         grep "lustre.lov" || error "can't get lustre.lov from $testfile"
9726
9727         local testfile2=${testfile}2
9728         local value=`getfattr -n lustre.lov $testfile 2> /dev/null | \
9729                      grep "lustre.lov" |sed -e 's/[^=]\+=//'  `
9730
9731         $RUNAS $MCREATE $testfile2
9732         $RUNAS setfattr -n lustre.lov -v $value $testfile2
9733         local stripe_size=$($RUNAS $LFS getstripe -S $testfile2)
9734         local stripe_count=$($RUNAS $LFS getstripe -c $testfile2)
9735         [ $stripe_size -eq 65536 ] || error "stripe size $stripe_size != 65536"
9736         [ $stripe_count -eq $STRIPECOUNT ] ||
9737                 error "stripe count $stripe_count != $STRIPECOUNT"
9738 }
9739 run_test 102c "non-root getfattr/setfattr for lustre.lov EAs ==========="
9740
9741 compare_stripe_info1() {
9742         local stripe_index_all_zero=true
9743
9744         for num in 1 2 3 4; do
9745                 for count in $(seq 1 $STRIPE_COUNT); do
9746                         for offset in $(seq 0 $[$STRIPE_COUNT - 1]); do
9747                                 local size=$((STRIPE_SIZE * num))
9748                                 local file=file"$num-$offset-$count"
9749                                 stripe_size=$($LFS getstripe -S $PWD/$file)
9750                                 [[ $stripe_size -ne $size ]] &&
9751                                     error "$file: size $stripe_size != $size"
9752                                 stripe_count=$($LFS getstripe -c $PWD/$file)
9753                                 # allow fewer stripes to be created, ORI-601
9754                                 [[ $stripe_count -lt $(((3 * count + 3) / 4)) ]] &&
9755                                     error "$file: count $stripe_count != $count"
9756                                 stripe_index=$($LFS getstripe -i $PWD/$file)
9757                                 [[ $stripe_index -ne 0 ]] &&
9758                                         stripe_index_all_zero=false
9759                         done
9760                 done
9761         done
9762         $stripe_index_all_zero &&
9763                 error "all files are being extracted starting from OST index 0"
9764         return 0
9765 }
9766
9767 have_xattrs_include() {
9768         tar --help | grep -q xattrs-include &&
9769                 echo --xattrs-include="lustre.*"
9770 }
9771
9772 test_102d() {
9773         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9774         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9775
9776         XINC=$(have_xattrs_include)
9777         setup_test102
9778         tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9779         cd $DIR/$tdir/$tdir
9780         compare_stripe_info1
9781 }
9782 run_test 102d "tar restore stripe info from tarfile,not keep osts"
9783
9784 test_102f() {
9785         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9786         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9787
9788         XINC=$(have_xattrs_include)
9789         setup_test102
9790         test_mkdir $DIR/$tdir.restore
9791         cd $DIR
9792         tar cf - --xattrs $tdir | tar xf - \
9793                 -C $DIR/$tdir.restore --xattrs $XINC
9794         cd $DIR/$tdir.restore/$tdir
9795         compare_stripe_info1
9796 }
9797 run_test 102f "tar copy files, not keep osts"
9798
9799 grow_xattr() {
9800         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep xattr)" ] &&
9801                 skip "must have user_xattr"
9802         [ -z "$(which setfattr 2>/dev/null)" ] &&
9803                 skip_env "could not find setfattr"
9804         [ -z "$(which getfattr 2>/dev/null)" ] &&
9805                 skip_env "could not find getfattr"
9806
9807         local xsize=${1:-1024}  # in bytes
9808         local file=$DIR/$tfile
9809         local value="$(generate_string $xsize)"
9810         local xbig=trusted.big
9811         local toobig=$2
9812
9813         touch $file
9814         log "save $xbig on $file"
9815         if [ -z "$toobig" ]
9816         then
9817                 setfattr -n $xbig -v $value $file ||
9818                         error "saving $xbig on $file failed"
9819         else
9820                 setfattr -n $xbig -v $value $file &&
9821                         error "saving $xbig on $file succeeded"
9822                 return 0
9823         fi
9824
9825         local orig=$(get_xattr_value $xbig $file)
9826         [[ "$orig" != "$value" ]] && error "$xbig different after saving $xbig"
9827
9828         local xsml=trusted.sml
9829         log "save $xsml on $file"
9830         setfattr -n $xsml -v val $file || error "saving $xsml on $file failed"
9831
9832         local new=$(get_xattr_value $xbig $file)
9833         [[ "$new" != "$orig" ]] && error "$xbig different after saving $xsml"
9834
9835         log "grow $xsml on $file"
9836         setfattr -n $xsml -v "$value" $file ||
9837                 error "growing $xsml on $file failed"
9838
9839         new=$(get_xattr_value $xbig $file)
9840         [[ "$new" != "$orig" ]] && error "$xbig different after growing $xsml"
9841         log "$xbig still valid after growing $xsml"
9842
9843         rm -f $file
9844 }
9845
9846 test_102h() { # bug 15777
9847         grow_xattr 1024
9848 }
9849 run_test 102h "grow xattr from inside inode to external block"
9850
9851 test_102ha() {
9852         large_xattr_enabled || skip_env "ea_inode feature disabled"
9853
9854         echo "setting xattr of max xattr size: $(max_xattr_size)"
9855         grow_xattr $(max_xattr_size)
9856
9857         echo "setting xattr of > max xattr size: $(max_xattr_size) + 10"
9858         echo "This should fail:"
9859         grow_xattr $(($(max_xattr_size) + 10)) 1
9860 }
9861 run_test 102ha "grow xattr from inside inode to external inode"
9862
9863 test_102i() { # bug 17038
9864         [ -z "$(which getfattr 2>/dev/null)" ] &&
9865                 skip "could not find getfattr"
9866
9867         touch $DIR/$tfile
9868         ln -s $DIR/$tfile $DIR/${tfile}link
9869         getfattr -n trusted.lov $DIR/$tfile ||
9870                 error "lgetxattr on $DIR/$tfile failed"
9871         getfattr -h -n trusted.lov $DIR/${tfile}link 2>&1 |
9872                 grep -i "no such attr" ||
9873                 error "error for lgetxattr on $DIR/${tfile}link is not ENODATA"
9874         rm -f $DIR/$tfile $DIR/${tfile}link
9875 }
9876 run_test 102i "lgetxattr test on symbolic link ============"
9877
9878 test_102j() {
9879         [ $PARALLEL == "yes" ] && skip "skip parallel run"
9880         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
9881
9882         XINC=$(have_xattrs_include)
9883         setup_test102 "$RUNAS"
9884         chown $RUNAS_ID $DIR/$tdir
9885         $RUNAS tar xf $TMP/f102.tar -C $DIR/$tdir --xattrs $XINC
9886         cd $DIR/$tdir/$tdir
9887         compare_stripe_info1 "$RUNAS"
9888 }
9889 run_test 102j "non-root tar restore stripe info from tarfile, not keep osts ==="
9890
9891 test_102k() {
9892         [ -z "$(which setfattr 2>/dev/null)" ] &&
9893                 skip "could not find setfattr"
9894
9895         touch $DIR/$tfile
9896         # b22187 just check that does not crash for regular file.
9897         setfattr -n trusted.lov $DIR/$tfile
9898         # b22187 'setfattr -n trusted.lov' should remove LOV EA for directories
9899         local test_kdir=$DIR/$tdir
9900         test_mkdir $test_kdir
9901         local default_size=$($LFS getstripe -S $test_kdir)
9902         local default_count=$($LFS getstripe -c $test_kdir)
9903         local default_offset=$($LFS getstripe -i $test_kdir)
9904         $LFS setstripe -S 65536 -i 0 -c $OSTCOUNT $test_kdir ||
9905                 error 'dir setstripe failed'
9906         setfattr -n trusted.lov $test_kdir
9907         local stripe_size=$($LFS getstripe -S $test_kdir)
9908         local stripe_count=$($LFS getstripe -c $test_kdir)
9909         local stripe_offset=$($LFS getstripe -i $test_kdir)
9910         [ $stripe_size -eq $default_size ] ||
9911                 error "stripe size $stripe_size != $default_size"
9912         [ $stripe_count -eq $default_count ] ||
9913                 error "stripe count $stripe_count != $default_count"
9914         [ $stripe_offset -eq $default_offset ] ||
9915                 error "stripe offset $stripe_offset != $default_offset"
9916         rm -rf $DIR/$tfile $test_kdir
9917 }
9918 run_test 102k "setfattr without parameter of value shouldn't cause a crash"
9919
9920 test_102l() {
9921         [ -z "$(which getfattr 2>/dev/null)" ] &&
9922                 skip "could not find getfattr"
9923
9924         # LU-532 trusted. xattr is invisible to non-root
9925         local testfile=$DIR/$tfile
9926
9927         touch $testfile
9928
9929         echo "listxattr as user..."
9930         chown $RUNAS_ID $testfile
9931         $RUNAS getfattr -d -m '.*' $testfile 2>&1 |
9932             grep -q "trusted" &&
9933                 error "$testfile trusted xattrs are user visible"
9934
9935         return 0;
9936 }
9937 run_test 102l "listxattr size test =================================="
9938
9939 test_102m() { # LU-3403 llite: error of listxattr when buffer is small
9940         local path=$DIR/$tfile
9941         touch $path
9942
9943         listxattr_size_check $path || error "listattr_size_check $path failed"
9944 }
9945 run_test 102m "Ensure listxattr fails on small bufffer ========"
9946
9947 cleanup_test102
9948
9949 getxattr() { # getxattr path name
9950         # Return the base64 encoding of the value of xattr name on path.
9951         local path=$1
9952         local name=$2
9953
9954         # # getfattr --absolute-names --encoding=base64 --name=trusted.lov $path
9955         # file: $path
9956         # trusted.lov=0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9957         #
9958         # We print just 0s0AvRCwEAAAAGAAAAAAAAAAAEAAACAAAAAAAQAAEAA...AAAAAAAAA=
9959
9960         getfattr --absolute-names --encoding=base64 --name=$name $path |
9961                 awk -F= -v name=$name '$1 == name {
9962                         print substr($0, index($0, "=") + 1);
9963         }'
9964 }
9965
9966 test_102n() { # LU-4101 mdt: protect internal xattrs
9967         [ -z "$(which setfattr 2>/dev/null)" ] &&
9968                 skip "could not find setfattr"
9969         if [ $MDS1_VERSION -lt $(version_code 2.5.50) ]
9970         then
9971                 skip "MDT < 2.5.50 allows setxattr on internal trusted xattrs"
9972         fi
9973
9974         local file0=$DIR/$tfile.0
9975         local file1=$DIR/$tfile.1
9976         local xattr0=$TMP/$tfile.0
9977         local xattr1=$TMP/$tfile.1
9978         local namelist="lov lma lmv link fid version som hsm"
9979         local name
9980         local value
9981
9982         rm -rf $file0 $file1 $xattr0 $xattr1
9983         touch $file0 $file1
9984
9985         # Get 'before' xattrs of $file1.
9986         getfattr --absolute-names --dump --match=- $file1 > $xattr0
9987
9988         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
9989                 namelist+=" lfsck_namespace"
9990         for name in $namelist; do
9991                 # Try to copy xattr from $file0 to $file1.
9992                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
9993
9994                 setfattr --name=trusted.$name --value="$value" $file1 ||
9995                         error "setxattr 'trusted.$name' failed"
9996
9997                 # Try to set a garbage xattr.
9998                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
9999
10000                 if [[ x$name == "xlov" ]]; then
10001                         setfattr --name=trusted.lov --value="$value" $file1 &&
10002                         error "setxattr invalid 'trusted.lov' success"
10003                 else
10004                         setfattr --name=trusted.$name --value="$value" $file1 ||
10005                                 error "setxattr invalid 'trusted.$name' failed"
10006                 fi
10007
10008                 # Try to remove the xattr from $file1. We don't care if this
10009                 # appears to succeed or fail, we just don't want there to be
10010                 # any changes or crashes.
10011                 setfattr --remove=$trusted.$name $file1 2> /dev/null
10012         done
10013
10014         if [ $MDS1_VERSION -gt $(version_code 2.6.50) ]
10015         then
10016                 name="lfsck_ns"
10017                 # Try to copy xattr from $file0 to $file1.
10018                 value=$(getxattr $file0 trusted.$name 2> /dev/null)
10019
10020                 setfattr --name=trusted.$name --value="$value" $file1 ||
10021                         error "setxattr 'trusted.$name' failed"
10022
10023                 # Try to set a garbage xattr.
10024                 value=0sVGhlIHF1aWNrIGJyb3duIGZveCBqdW1wcyBvdmVyIGl0c2VsZi4=
10025
10026                 setfattr --name=trusted.$name --value="$value" $file1 ||
10027                         error "setxattr 'trusted.$name' failed"
10028
10029                 # Try to remove the xattr from $file1. We don't care if this
10030                 # appears to succeed or fail, we just don't want there to be
10031                 # any changes or crashes.
10032                 setfattr --remove=$trusted.$name $file1 2> /dev/null
10033         fi
10034
10035         # Get 'after' xattrs of file1.
10036         getfattr --absolute-names --dump --match=- $file1 > $xattr1
10037
10038         if ! diff $xattr0 $xattr1; then
10039                 error "before and after xattrs of '$file1' differ"
10040         fi
10041
10042         rm -rf $file0 $file1 $xattr0 $xattr1
10043
10044         return 0
10045 }
10046 run_test 102n "silently ignore setxattr on internal trusted xattrs"
10047
10048 test_102p() { # LU-4703 setxattr did not check ownership
10049         [ $MDS1_VERSION -lt $(version_code 2.5.56) ] &&
10050                 skip "MDS needs to be at least 2.5.56"
10051
10052         local testfile=$DIR/$tfile
10053
10054         touch $testfile
10055
10056         echo "setfacl as user..."
10057         $RUNAS setfacl -m "u:$RUNAS_ID:rwx" $testfile
10058         [ $? -ne 0 ] || error "setfacl by $RUNAS_ID was allowed on $testfile"
10059
10060         echo "setfattr as user..."
10061         setfacl -m "u:$RUNAS_ID:---" $testfile
10062         $RUNAS setfattr -x system.posix_acl_access $testfile
10063         [ $? -ne 0 ] || error "setfattr by $RUNAS_ID was allowed on $testfile"
10064 }
10065 run_test 102p "check setxattr(2) correctly fails without permission"
10066
10067 test_102q() {
10068         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] &&
10069                 skip "MDS needs to be at least 2.6.92"
10070
10071         orphan_linkea_check $DIR/$tfile || error "orphan_linkea_check"
10072 }
10073 run_test 102q "flistxattr should not return trusted.link EAs for orphans"
10074
10075 test_102r() {
10076         [ $MDS1_VERSION -lt $(version_code 2.6.93) ] &&
10077                 skip "MDS needs to be at least 2.6.93"
10078
10079         touch $DIR/$tfile || error "touch"
10080         setfattr -n user.$(basename $tfile) $DIR/$tfile || error "setfattr"
10081         getfattr -n user.$(basename $tfile) $DIR/$tfile || error "getfattr"
10082         rm $DIR/$tfile || error "rm"
10083
10084         #normal directory
10085         mkdir -p $DIR/$tdir || error "mkdir"
10086         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10087         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10088         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10089                 error "$testfile error deleting user.author1"
10090         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10091                 grep "user.$(basename $tdir)" &&
10092                 error "$tdir did not delete user.$(basename $tdir)"
10093         rmdir $DIR/$tdir || error "rmdir"
10094
10095         #striped directory
10096         test_mkdir $DIR/$tdir
10097         setfattr -n user.$(basename $tdir) $DIR/$tdir || error "setfattr dir"
10098         getfattr -n user.$(basename $tdir) $DIR/$tdir || error "getfattr dir"
10099         setfattr -x user.$(basename $tdir) $DIR/$tdir ||
10100                 error "$testfile error deleting user.author1"
10101         getfattr -d -m user.$(basename $tdir) 2> /dev/null |
10102                 grep "user.$(basename $tdir)" &&
10103                 error "$tdir did not delete user.$(basename $tdir)"
10104         rmdir $DIR/$tdir || error "rm striped dir"
10105 }
10106 run_test 102r "set EAs with empty values"
10107
10108 test_102s() {
10109         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10110                 skip "MDS needs to be at least 2.11.52"
10111
10112         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10113
10114         save_lustre_params client "llite.*.xattr_cache" > $save
10115
10116         for cache in 0 1; do
10117                 lctl set_param llite.*.xattr_cache=$cache
10118
10119                 rm -f $DIR/$tfile
10120                 touch $DIR/$tfile || error "touch"
10121                 for prefix in lustre security system trusted user; do
10122                         # Note getxattr() may fail with 'Operation not
10123                         # supported' or 'No such attribute' depending
10124                         # on prefix and cache.
10125                         getfattr -n $prefix.n102s $DIR/$tfile &&
10126                                 error "getxattr '$prefix.n102s' should fail (cache = $cache)"
10127                 done
10128         done
10129
10130         restore_lustre_params < $save
10131 }
10132 run_test 102s "getting nonexistent xattrs should fail"
10133
10134 test_102t() {
10135         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
10136                 skip "MDS needs to be at least 2.11.52"
10137
10138         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
10139
10140         save_lustre_params client "llite.*.xattr_cache" > $save
10141
10142         for cache in 0 1; do
10143                 lctl set_param llite.*.xattr_cache=$cache
10144
10145                 for buf_size in 0 256; do
10146                         rm -f $DIR/$tfile
10147                         touch $DIR/$tfile || error "touch"
10148                         setfattr -n user.multiop $DIR/$tfile
10149                         $MULTIOP $DIR/$tfile oa$buf_size ||
10150                                 error "cannot get zero length xattr value (buf_size = $buf_size)"
10151                 done
10152         done
10153
10154         restore_lustre_params < $save
10155 }
10156 run_test 102t "zero length xattr values handled correctly"
10157
10158 run_acl_subtest()
10159 {
10160     $LUSTRE/tests/acl/run $LUSTRE/tests/acl/$1.test
10161     return $?
10162 }
10163
10164 test_103a() {
10165         [ "$UID" != 0 ] && skip "must run as root"
10166         $GSS && skip_env "could not run under gss"
10167         [ -z "$(lctl get_param -n mdc.*-mdc-*.connect_flags | grep acl)" ] &&
10168                 skip_env "must have acl enabled"
10169         [ -z "$(which setfacl 2>/dev/null)" ] &&
10170                 skip_env "could not find setfacl"
10171         remote_mds_nodsh && skip "remote MDS with nodsh"
10172
10173         gpasswd -a daemon bin                           # LU-5641
10174         do_facet $SINGLEMDS gpasswd -a daemon bin       # LU-5641
10175
10176         declare -a identity_old
10177
10178         for num in $(seq $MDSCOUNT); do
10179                 switch_identity $num true || identity_old[$num]=$?
10180         done
10181
10182         SAVE_UMASK=$(umask)
10183         umask 0022
10184         mkdir -p $DIR/$tdir
10185         cd $DIR/$tdir
10186
10187         echo "performing cp ..."
10188         run_acl_subtest cp || error "run_acl_subtest cp failed"
10189         echo "performing getfacl-noacl..."
10190         run_acl_subtest getfacl-noacl || error "getfacl-noacl test failed"
10191         echo "performing misc..."
10192         run_acl_subtest misc || error  "misc test failed"
10193         echo "performing permissions..."
10194         run_acl_subtest permissions || error "permissions failed"
10195         # LU-1482 mdd: Setting xattr are properly checked with and without ACLs
10196         if [ $MDS1_VERSION -gt $(version_code 2.8.55) ] ||
10197                 { [ $MDS1_VERSION -lt $(version_code 2.6) ] &&
10198                         [ $MDS1_VERSION -ge $(version_code 2.5.29) ]; }
10199         then
10200                 echo "performing permissions xattr..."
10201                 run_acl_subtest permissions_xattr ||
10202                         error "permissions_xattr failed"
10203         fi
10204         echo "performing setfacl..."
10205         run_acl_subtest setfacl || error  "setfacl test failed"
10206
10207         # inheritance test got from HP
10208         echo "performing inheritance..."
10209         cp $LUSTRE/tests/acl/make-tree . || error "cannot copy make-tree"
10210         chmod +x make-tree || error "chmod +x failed"
10211         run_acl_subtest inheritance || error "inheritance test failed"
10212         rm -f make-tree
10213
10214         echo "LU-974 ignore umask when acl is enabled..."
10215         run_acl_subtest 974 || error "LU-974 umask test failed"
10216         if [ $MDSCOUNT -ge 2 ]; then
10217                 run_acl_subtest 974_remote ||
10218                         error "LU-974 umask test failed under remote dir"
10219         fi
10220
10221         echo "LU-2561 newly created file is same size as directory..."
10222         if [ "$mds1_FSTYPE" != "zfs" ]; then
10223                 run_acl_subtest 2561 || error "LU-2561 test failed"
10224         else
10225                 run_acl_subtest 2561_zfs || error "LU-2561 zfs test failed"
10226         fi
10227
10228         run_acl_subtest 4924 || error "LU-4924 test failed"
10229
10230         cd $SAVE_PWD
10231         umask $SAVE_UMASK
10232
10233         for num in $(seq $MDSCOUNT); do
10234                 if [ "${identity_old[$num]}" = 1 ]; then
10235                         switch_identity $num false || identity_old[$num]=$?
10236                 fi
10237         done
10238 }
10239 run_test 103a "acl test"
10240
10241 test_103b() {
10242         declare -a pids
10243         local U
10244
10245         for U in {0..511}; do
10246                 {
10247                 local O=$(printf "%04o" $U)
10248
10249                 umask $(printf "%04o" $((511 ^ $O)))
10250                 $LFS setstripe -c 1 $DIR/$tfile.s$O
10251                 local S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.s$O))
10252
10253                 (( $S == ($O & 0666) )) ||
10254                         error "lfs setstripe $DIR/$tfile.s$O '$S' != '$O'"
10255
10256                 $LFS setstripe -E16M -c 1 -E1G -S4M $DIR/$tfile.p$O
10257                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.p$O))
10258                 (( $S == ($O & 0666) )) ||
10259                         error "lfs setstripe -E $DIR/$tfile.p$O '$S' != '$O'"
10260
10261                 $LFS setstripe -N2 -c 1 $DIR/$tfile.m$O
10262                 S=$(printf "%04o" 0$(stat -c%a $DIR/$tfile.m$O))
10263                 (( $S == ($O & 0666) )) ||
10264                         error "lfs setstripe -N2 $DIR/$tfile.m$O '$S' != '$O'"
10265                 rm -f $DIR/$tfile.[smp]$0
10266                 } &
10267                 local pid=$!
10268
10269                 # limit the concurrently running threads to 64. LU-11878
10270                 local idx=$((U % 64))
10271                 [ -z "${pids[idx]}" ] || wait ${pids[idx]}
10272                 pids[idx]=$pid
10273         done
10274         wait
10275 }
10276 run_test 103b "umask lfs setstripe"
10277
10278 test_103c() {
10279         mkdir -p $DIR/$tdir
10280         cp -rp $DIR/$tdir $DIR/$tdir.bak
10281
10282         [ -n "$(getfattr -d -m. $DIR/$tdir | grep posix_acl_default)" ] &&
10283                 error "$DIR/$tdir shouldn't contain default ACL"
10284         [ -n "$(getfattr -d -m. $DIR/$tdir.bak | grep posix_acl_default)" ] &&
10285                 error "$DIR/$tdir.bak shouldn't contain default ACL"
10286         true
10287 }
10288 run_test 103c "'cp -rp' won't set empty acl"
10289
10290 test_104a() {
10291         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10292
10293         touch $DIR/$tfile
10294         lfs df || error "lfs df failed"
10295         lfs df -ih || error "lfs df -ih failed"
10296         lfs df -h $DIR || error "lfs df -h $DIR failed"
10297         lfs df -i $DIR || error "lfs df -i $DIR failed"
10298         lfs df $DIR/$tfile || error "lfs df $DIR/$tfile failed"
10299         lfs df -ih $DIR/$tfile || error "lfs df -ih $DIR/$tfile failed"
10300
10301         local OSC=$(lctl dl | grep OST0000-osc-[^M] | awk '{ print $4 }')
10302         lctl --device %$OSC deactivate
10303         lfs df || error "lfs df with deactivated OSC failed"
10304         lctl --device %$OSC activate
10305         # wait the osc back to normal
10306         wait_osc_import_ready client ost
10307
10308         lfs df || error "lfs df with reactivated OSC failed"
10309         rm -f $DIR/$tfile
10310 }
10311 run_test 104a "lfs df [-ih] [path] test ========================="
10312
10313 test_104b() {
10314         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10315         [ $RUNAS_ID -eq $UID ] &&
10316                 skip_env "RUNAS_ID = UID = $UID -- skipping"
10317
10318         denied_cnt=$(($($RUNAS $LFS check servers 2>&1 |
10319                         grep "Permission denied" | wc -l)))
10320         if [ $denied_cnt -ne 0 ]; then
10321                 error "lfs check servers test failed"
10322         fi
10323 }
10324 run_test 104b "$RUNAS lfs check servers test ===================="
10325
10326 test_105a() {
10327         # doesn't work on 2.4 kernels
10328         touch $DIR/$tfile
10329         if $(flock_is_enabled); then
10330                 flocks_test 1 on -f $DIR/$tfile || error "fail flock on"
10331         else
10332                 flocks_test 1 off -f $DIR/$tfile || error "fail flock off"
10333         fi
10334         rm -f $DIR/$tfile
10335 }
10336 run_test 105a "flock when mounted without -o flock test ========"
10337
10338 test_105b() {
10339         touch $DIR/$tfile
10340         if $(flock_is_enabled); then
10341                 flocks_test 1 on -c $DIR/$tfile || error "fail flock on"
10342         else
10343                 flocks_test 1 off -c $DIR/$tfile || error "fail flock off"
10344         fi
10345         rm -f $DIR/$tfile
10346 }
10347 run_test 105b "fcntl when mounted without -o flock test ========"
10348
10349 test_105c() {
10350         touch $DIR/$tfile
10351         if $(flock_is_enabled); then
10352                 flocks_test 1 on -l $DIR/$tfile || error "fail flock on"
10353         else
10354                 flocks_test 1 off -l $DIR/$tfile || error "fail flock off"
10355         fi
10356         rm -f $DIR/$tfile
10357 }
10358 run_test 105c "lockf when mounted without -o flock test"
10359
10360 test_105d() { # bug 15924
10361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10362
10363         test_mkdir $DIR/$tdir
10364         flock_is_enabled || skip_env "mount w/o flock enabled"
10365         #define OBD_FAIL_LDLM_CP_CB_WAIT  0x315
10366         $LCTL set_param fail_loc=0x80000315
10367         flocks_test 2 $DIR/$tdir
10368 }
10369 run_test 105d "flock race (should not freeze) ========"
10370
10371 test_105e() { # bug 22660 && 22040
10372         flock_is_enabled || skip_env "mount w/o flock enabled"
10373
10374         touch $DIR/$tfile
10375         flocks_test 3 $DIR/$tfile
10376 }
10377 run_test 105e "Two conflicting flocks from same process"
10378
10379 test_106() { #bug 10921
10380         test_mkdir $DIR/$tdir
10381         $DIR/$tdir && error "exec $DIR/$tdir succeeded"
10382         chmod 777 $DIR/$tdir || error "chmod $DIR/$tdir failed"
10383 }
10384 run_test 106 "attempt exec of dir followed by chown of that dir"
10385
10386 test_107() {
10387         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10388
10389         CDIR=`pwd`
10390         local file=core
10391
10392         cd $DIR
10393         rm -f $file
10394
10395         local save_pattern=$(sysctl -n kernel.core_pattern)
10396         local save_uses_pid=$(sysctl -n kernel.core_uses_pid)
10397         sysctl -w kernel.core_pattern=$file
10398         sysctl -w kernel.core_uses_pid=0
10399
10400         ulimit -c unlimited
10401         sleep 60 &
10402         SLEEPPID=$!
10403
10404         sleep 1
10405
10406         kill -s 11 $SLEEPPID
10407         wait $SLEEPPID
10408         if [ -e $file ]; then
10409                 size=`stat -c%s $file`
10410                 [ $size -eq 0 ] && error "Fail to create core file $file"
10411         else
10412                 error "Fail to create core file $file"
10413         fi
10414         rm -f $file
10415         sysctl -w kernel.core_pattern=$save_pattern
10416         sysctl -w kernel.core_uses_pid=$save_uses_pid
10417         cd $CDIR
10418 }
10419 run_test 107 "Coredump on SIG"
10420
10421 test_110() {
10422         test_mkdir $DIR/$tdir
10423         test_mkdir $DIR/$tdir/$(str_repeat 'a' 255)
10424         $LFS mkdir -c $MDSCOUNT $DIR/$tdir/$(str_repeat 'b' 256) &&
10425                 error "mkdir with 256 char should fail, but did not"
10426         touch $DIR/$tdir/$(str_repeat 'x' 255) ||
10427                 error "create with 255 char failed"
10428         touch $DIR/$tdir/$(str_repeat 'y' 256) &&
10429                 error "create with 256 char should fail, but did not"
10430
10431         ls -l $DIR/$tdir
10432         rm -rf $DIR/$tdir
10433 }
10434 run_test 110 "filename length checking"
10435
10436 #
10437 # Purpose: To verify dynamic thread (OSS) creation.
10438 #
10439 test_115() {
10440         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10441         remote_ost_nodsh && skip "remote OST with nodsh"
10442
10443         # Lustre does not stop service threads once they are started.
10444         # Reset number of running threads to default.
10445         stopall
10446         setupall
10447
10448         local OSTIO_pre
10449         local save_params="$TMP/sanity-$TESTNAME.parameters"
10450
10451         # Get ll_ost_io count before I/O
10452         OSTIO_pre=$(do_facet ost1 \
10453                 "$LCTL get_param ost.OSS.ost_io.threads_started | cut -d= -f2")
10454         # Exit if lustre is not running (ll_ost_io not running).
10455         [ -z "$OSTIO_pre" ] && error "no OSS threads"
10456
10457         echo "Starting with $OSTIO_pre threads"
10458         local thread_max=$((OSTIO_pre * 2))
10459         local rpc_in_flight=$((thread_max * 2))
10460         # Number of I/O Process proposed to be started.
10461         local nfiles
10462         local facets=$(get_facets OST)
10463
10464         save_lustre_params client "osc.*OST*.max_rpcs_in_flight" > $save_params
10465         save_lustre_params $facets "ost.OSS.ost_io.threads_max" >> $save_params
10466
10467         # Set in_flight to $rpc_in_flight
10468         $LCTL set_param osc.*OST*.max_rpcs_in_flight=$rpc_in_flight ||
10469                 error "Failed to set max_rpcs_in_flight to $rpc_in_flight"
10470         nfiles=${rpc_in_flight}
10471         # Set ost thread_max to $thread_max
10472         do_facet ost1 "$LCTL set_param ost.OSS.ost_io.threads_max=$thread_max"
10473
10474         # 5 Minutes should be sufficient for max number of OSS
10475         # threads(thread_max) to be created.
10476         local timeout=300
10477
10478         # Start I/O.
10479         local WTL=${WTL:-"$LUSTRE/tests/write_time_limit"}
10480         test_mkdir $DIR/$tdir
10481         for i in $(seq $nfiles); do
10482                 local file=$DIR/$tdir/${tfile}-$i
10483                 $LFS setstripe -c -1 -i 0 $file
10484                 ($WTL $file $timeout)&
10485         done
10486
10487         # I/O Started - Wait for thread_started to reach thread_max or report
10488         # error if thread_started is more than thread_max.
10489         echo "Waiting for thread_started to reach thread_max"
10490         local thread_started=0
10491         local end_time=$((SECONDS + timeout))
10492
10493         while [ $SECONDS -le $end_time ] ; do
10494                 echo -n "."
10495                 # Get ost i/o thread_started count.
10496                 thread_started=$(do_facet ost1 \
10497                         "$LCTL get_param \
10498                         ost.OSS.ost_io.threads_started | cut -d= -f2")
10499                 # Break out if thread_started is equal/greater than thread_max
10500                 if [[ $thread_started -ge $thread_max ]]; then
10501                         echo ll_ost_io thread_started $thread_started, \
10502                                 equal/greater than thread_max $thread_max
10503                         break
10504                 fi
10505                 sleep 1
10506         done
10507
10508         # Cleanup - We have the numbers, Kill i/o jobs if running.
10509         jobcount=($(jobs -p))
10510         for i in $(seq 0 $((${#jobcount[@]}-1)))
10511         do
10512                 kill -9 ${jobcount[$i]}
10513                 if [ $? -ne 0 ] ; then
10514                         echo Warning: \
10515                         Failed to Kill \'WTL\(I/O\)\' with pid ${jobcount[$i]}
10516                 fi
10517         done
10518
10519         # Cleanup files left by WTL binary.
10520         for i in $(seq $nfiles); do
10521                 local file=$DIR/$tdir/${tfile}-$i
10522                 rm -rf $file
10523                 if [ $? -ne 0 ] ; then
10524                         echo "Warning: Failed to delete file $file"
10525                 fi
10526         done
10527
10528         restore_lustre_params <$save_params
10529         rm -f $save_params || echo "Warning: delete file '$save_params' failed"
10530
10531         # Error out if no new thread has started or Thread started is greater
10532         # than thread max.
10533         if [[ $thread_started -le $OSTIO_pre ||
10534                         $thread_started -gt $thread_max ]]; then
10535                 error "ll_ost_io: thread_started $thread_started" \
10536                       "OSTIO_pre $OSTIO_pre, thread_max $thread_max." \
10537                       "No new thread started or thread started greater " \
10538                       "than thread_max."
10539         fi
10540 }
10541 run_test 115 "verify dynamic thread creation===================="
10542
10543 free_min_max () {
10544         wait_delete_completed
10545         AVAIL=($(lctl get_param -n osc.*[oO][sS][cC]-[^M]*.kbytesavail))
10546         echo "OST kbytes available: ${AVAIL[@]}"
10547         MAXV=${AVAIL[0]}
10548         MAXI=0
10549         MINV=${AVAIL[0]}
10550         MINI=0
10551         for ((i = 0; i < ${#AVAIL[@]}; i++)); do
10552                 #echo OST $i: ${AVAIL[i]}kb
10553                 if [[ ${AVAIL[i]} -gt $MAXV ]]; then
10554                         MAXV=${AVAIL[i]}
10555                         MAXI=$i
10556                 fi
10557                 if [[ ${AVAIL[i]} -lt $MINV ]]; then
10558                         MINV=${AVAIL[i]}
10559                         MINI=$i
10560                 fi
10561         done
10562         echo "Min free space: OST $MINI: $MINV"
10563         echo "Max free space: OST $MAXI: $MAXV"
10564 }
10565
10566 test_116a() { # was previously test_116()
10567         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10568         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
10569         remote_mds_nodsh && skip "remote MDS with nodsh"
10570
10571         echo -n "Free space priority "
10572         do_facet $SINGLEMDS lctl get_param -n lo[vd].*-mdtlov.qos_prio_free |
10573                 head -n1
10574         declare -a AVAIL
10575         free_min_max
10576
10577         [ $MINV -eq 0 ] && skip "no free space in OST$MINI, skip"
10578         [ $MINV -gt 10000000 ] && skip "too much free space in OST$MINI, skip"
10579         trap simple_cleanup_common EXIT
10580
10581         # Check if we need to generate uneven OSTs
10582         test_mkdir -p $DIR/$tdir/OST${MINI}
10583         local FILL=$((MINV / 4))
10584         local DIFF=$((MAXV - MINV))
10585         local DIFF2=$((DIFF * 100 / MINV))
10586
10587         local threshold=$(do_facet $SINGLEMDS \
10588                 lctl get_param -n *.*MDT0000-mdtlov.qos_threshold_rr | head -n1)
10589         threshold=${threshold%%%}
10590         echo -n "Check for uneven OSTs: "
10591         echo -n "diff=${DIFF}KB (${DIFF2}%) must be > ${threshold}% ..."
10592
10593         if [[ $DIFF2 -gt $threshold ]]; then
10594                 echo "ok"
10595                 echo "Don't need to fill OST$MINI"
10596         else
10597                 # generate uneven OSTs. Write 2% over the QOS threshold value
10598                 echo "no"
10599                 DIFF=$((threshold - DIFF2 + 2))
10600                 DIFF2=$((MINV * DIFF / 100))
10601                 echo "Fill $DIFF% remaining space in OST$MINI with ${DIFF2}KB"
10602                 $LFS setstripe -i $MINI -c 1 $DIR/$tdir/OST${MINI} ||
10603                         error "setstripe failed"
10604                 DIFF=$((DIFF2 / 2048))
10605                 i=0
10606                 while [ $i -lt $DIFF ]; do
10607                         i=$((i + 1))
10608                         dd if=/dev/zero of=$DIR/$tdir/OST${MINI}/$tfile-$i \
10609                                 bs=2M count=1 2>/dev/null
10610                         echo -n .
10611                 done
10612                 echo .
10613                 sync
10614                 sleep_maxage
10615                 free_min_max
10616         fi
10617
10618         DIFF=$((MAXV - MINV))
10619         DIFF2=$((DIFF * 100 / MINV))
10620         echo -n "diff=$DIFF=$DIFF2% must be > $threshold% for QOS mode..."
10621         if [ $DIFF2 -gt $threshold ]; then
10622                 echo "ok"
10623         else
10624                 echo "failed - QOS mode won't be used"
10625                 simple_cleanup_common
10626                 skip "QOS imbalance criteria not met"
10627         fi
10628
10629         MINI1=$MINI
10630         MINV1=$MINV
10631         MAXI1=$MAXI
10632         MAXV1=$MAXV
10633
10634         # now fill using QOS
10635         $LFS setstripe -c 1 $DIR/$tdir
10636         FILL=$((FILL / 200))
10637         if [ $FILL -gt 600 ]; then
10638                 FILL=600
10639         fi
10640         echo "writing $FILL files to QOS-assigned OSTs"
10641         i=0
10642         while [ $i -lt $FILL ]; do
10643                 i=$((i + 1))
10644                 dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=200k \
10645                         count=1 2>/dev/null
10646                 echo -n .
10647         done
10648         echo "wrote $i 200k files"
10649         sync
10650         sleep_maxage
10651
10652         echo "Note: free space may not be updated, so measurements might be off"
10653         free_min_max
10654         DIFF2=$((MAXV - MINV))
10655         echo "free space delta: orig $DIFF final $DIFF2"
10656         [ $DIFF2 -gt $DIFF ] && echo "delta got worse!"
10657         DIFF=$((MINV1 - ${AVAIL[$MINI1]}))
10658         echo "Wrote ${DIFF}KB to smaller OST $MINI1"
10659         DIFF2=$((MAXV1 - ${AVAIL[$MAXI1]}))
10660         echo "Wrote ${DIFF2}KB to larger OST $MAXI1"
10661         if [[ $DIFF -gt 0 ]]; then
10662                 FILL=$((DIFF2 * 100 / DIFF - 100))
10663                 echo "Wrote ${FILL}% more data to larger OST $MAXI1"
10664         fi
10665
10666         # Figure out which files were written where
10667         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10668                awk '/'$MINI1': / {print $2; exit}')
10669         echo $UUID
10670         MINC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10671         echo "$MINC files created on smaller OST $MINI1"
10672         UUID=$(lctl get_param -n lov.${FSNAME}-clilov-*.target_obd |
10673                awk '/'$MAXI1': / {print $2; exit}')
10674         echo $UUID
10675         MAXC=$($LFS getstripe --ost $UUID $DIR/$tdir | grep $DIR | wc -l)
10676         echo "$MAXC files created on larger OST $MAXI1"
10677         if [[ $MINC -gt 0 ]]; then
10678                 FILL=$((MAXC * 100 / MINC - 100))
10679                 echo "Wrote ${FILL}% more files to larger OST $MAXI1"
10680         fi
10681         [[ $MAXC -gt $MINC ]] ||
10682                 error_ignore LU-9 "stripe QOS didn't balance free space"
10683         simple_cleanup_common
10684 }
10685 run_test 116a "stripe QOS: free space balance ==================="
10686
10687 test_116b() { # LU-2093
10688         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10689         remote_mds_nodsh && skip "remote MDS with nodsh"
10690
10691 #define OBD_FAIL_MDS_OSC_CREATE_FAIL     0x147
10692         local old_rr=$(do_facet $SINGLEMDS lctl get_param -n \
10693                        lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr | head -1)
10694         [ -z "$old_rr" ] && skip "no QOS"
10695         do_facet $SINGLEMDS lctl set_param \
10696                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=0
10697         mkdir -p $DIR/$tdir
10698         do_facet $SINGLEMDS lctl set_param fail_loc=0x147
10699         createmany -o $DIR/$tdir/f- 20 || error "can't create"
10700         do_facet $SINGLEMDS lctl set_param fail_loc=0
10701         rm -rf $DIR/$tdir
10702         do_facet $SINGLEMDS lctl set_param \
10703                 lo[vd].$FSNAME-MDT0000-mdtlov.qos_threshold_rr=$old_rr
10704 }
10705 run_test 116b "QoS shouldn't LBUG if not enough OSTs found on the 2nd pass"
10706
10707 test_117() # bug 10891
10708 {
10709         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10710
10711         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
10712         #define OBD_FAIL_OST_SETATTR_CREDITS 0x21e
10713         lctl set_param fail_loc=0x21e
10714         > $DIR/$tfile || error "truncate failed"
10715         lctl set_param fail_loc=0
10716         echo "Truncate succeeded."
10717         rm -f $DIR/$tfile
10718 }
10719 run_test 117 "verify osd extend =========="
10720
10721 NO_SLOW_RESENDCOUNT=4
10722 export OLD_RESENDCOUNT=""
10723 set_resend_count () {
10724         local PROC_RESENDCOUNT="osc.${FSNAME}-OST*-osc-*.resend_count"
10725         OLD_RESENDCOUNT=$(lctl get_param -n $PROC_RESENDCOUNT | head -n1)
10726         lctl set_param -n $PROC_RESENDCOUNT $1
10727         echo resend_count is set to $(lctl get_param -n $PROC_RESENDCOUNT)
10728 }
10729
10730 # for reduce test_118* time (b=14842)
10731 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10732
10733 # Reset async IO behavior after error case
10734 reset_async() {
10735         FILE=$DIR/reset_async
10736
10737         # Ensure all OSCs are cleared
10738         $LFS setstripe -c -1 $FILE
10739         dd if=/dev/zero of=$FILE bs=64k count=$OSTCOUNT
10740         sync
10741         rm $FILE
10742 }
10743
10744 test_118a() #bug 11710
10745 {
10746         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10747
10748         reset_async
10749
10750         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10751         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10752         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
10753
10754         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10755                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10756                 return 1;
10757         fi
10758         rm -f $DIR/$tfile
10759 }
10760 run_test 118a "verify O_SYNC works =========="
10761
10762 test_118b()
10763 {
10764         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10765         remote_ost_nodsh && skip "remote OST with nodsh"
10766
10767         reset_async
10768
10769         #define OBD_FAIL_SRV_ENOENT 0x217
10770         set_nodes_failloc "$(osts_nodes)" 0x217
10771         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10772         RC=$?
10773         set_nodes_failloc "$(osts_nodes)" 0
10774         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10775         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10776                     grep -c writeback)
10777
10778         if [[ $RC -eq 0 ]]; then
10779                 error "Must return error due to dropped pages, rc=$RC"
10780                 return 1;
10781         fi
10782
10783         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10784                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10785                 return 1;
10786         fi
10787
10788         echo "Dirty pages not leaked on ENOENT"
10789
10790         # Due to the above error the OSC will issue all RPCs syncronously
10791         # until a subsequent RPC completes successfully without error.
10792         $MULTIOP $DIR/$tfile Ow4096yc
10793         rm -f $DIR/$tfile
10794
10795         return 0
10796 }
10797 run_test 118b "Reclaim dirty pages on fatal error =========="
10798
10799 test_118c()
10800 {
10801         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10802
10803         # for 118c, restore the original resend count, LU-1940
10804         [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] &&
10805                                 set_resend_count $OLD_RESENDCOUNT
10806         remote_ost_nodsh && skip "remote OST with nodsh"
10807
10808         reset_async
10809
10810         #define OBD_FAIL_OST_EROFS               0x216
10811         set_nodes_failloc "$(osts_nodes)" 0x216
10812
10813         # multiop should block due to fsync until pages are written
10814         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10815         MULTIPID=$!
10816         sleep 1
10817
10818         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10819                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10820         fi
10821
10822         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10823                     grep -c writeback)
10824         if [[ $WRITEBACK -eq 0 ]]; then
10825                 error "No page in writeback, writeback=$WRITEBACK"
10826         fi
10827
10828         set_nodes_failloc "$(osts_nodes)" 0
10829         wait $MULTIPID
10830         RC=$?
10831         if [[ $RC -ne 0 ]]; then
10832                 error "Multiop fsync failed, rc=$RC"
10833         fi
10834
10835         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10836         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10837                     grep -c writeback)
10838         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10839                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10840         fi
10841
10842         rm -f $DIR/$tfile
10843         echo "Dirty pages flushed via fsync on EROFS"
10844         return 0
10845 }
10846 run_test 118c "Fsync blocks on EROFS until dirty pages are flushed =========="
10847
10848 # continue to use small resend count to reduce test_118* time (b=14842)
10849 [ "$SLOW" = "no" ] && set_resend_count $NO_SLOW_RESENDCOUNT
10850
10851 test_118d()
10852 {
10853         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10854         remote_ost_nodsh && skip "remote OST with nodsh"
10855
10856         reset_async
10857
10858         #define OBD_FAIL_OST_BRW_PAUSE_BULK
10859         set_nodes_failloc "$(osts_nodes)" 0x214
10860         # multiop should block due to fsync until pages are written
10861         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
10862         MULTIPID=$!
10863         sleep 1
10864
10865         if [[ `ps h -o comm -p $MULTIPID` != "multiop" ]]; then
10866                 error "Multiop failed to block on fsync, pid=$MULTIPID"
10867         fi
10868
10869         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10870                     grep -c writeback)
10871         if [[ $WRITEBACK -eq 0 ]]; then
10872                 error "No page in writeback, writeback=$WRITEBACK"
10873         fi
10874
10875         wait $MULTIPID || error "Multiop fsync failed, rc=$?"
10876         set_nodes_failloc "$(osts_nodes)" 0
10877
10878         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10879         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10880                     grep -c writeback)
10881         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10882                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10883         fi
10884
10885         rm -f $DIR/$tfile
10886         echo "Dirty pages gaurenteed flushed via fsync"
10887         return 0
10888 }
10889 run_test 118d "Fsync validation inject a delay of the bulk =========="
10890
10891 test_118f() {
10892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10893
10894         reset_async
10895
10896         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
10897         lctl set_param fail_loc=0x8000040a
10898
10899         # Should simulate EINVAL error which is fatal
10900         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10901         RC=$?
10902         if [[ $RC -eq 0 ]]; then
10903                 error "Must return error due to dropped pages, rc=$RC"
10904         fi
10905
10906         lctl set_param fail_loc=0x0
10907
10908         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10909         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10910         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10911                     grep -c writeback)
10912         if [[ $LOCKED -ne 0 ]]; then
10913                 error "Locked pages remain in cache, locked=$LOCKED"
10914         fi
10915
10916         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10917                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10918         fi
10919
10920         rm -f $DIR/$tfile
10921         echo "No pages locked after fsync"
10922
10923         reset_async
10924         return 0
10925 }
10926 run_test 118f "Simulate unrecoverable OSC side error =========="
10927
10928 test_118g() {
10929         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10930
10931         reset_async
10932
10933         #define OBD_FAIL_OSC_BRW_PREP_REQ        0x406
10934         lctl set_param fail_loc=0x406
10935
10936         # simulate local -ENOMEM
10937         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10938         RC=$?
10939
10940         lctl set_param fail_loc=0
10941         if [[ $RC -eq 0 ]]; then
10942                 error "Must return error due to dropped pages, rc=$RC"
10943         fi
10944
10945         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10946         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10947         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10948                         grep -c writeback)
10949         if [[ $LOCKED -ne 0 ]]; then
10950                 error "Locked pages remain in cache, locked=$LOCKED"
10951         fi
10952
10953         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10954                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10955         fi
10956
10957         rm -f $DIR/$tfile
10958         echo "No pages locked after fsync"
10959
10960         reset_async
10961         return 0
10962 }
10963 run_test 118g "Don't stay in wait if we got local -ENOMEM  =========="
10964
10965 test_118h() {
10966         [ $PARALLEL == "yes" ] && skip "skip parallel run"
10967         remote_ost_nodsh && skip "remote OST with nodsh"
10968
10969         reset_async
10970
10971         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
10972         set_nodes_failloc "$(osts_nodes)" 0x20e
10973         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
10974         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
10975         RC=$?
10976
10977         set_nodes_failloc "$(osts_nodes)" 0
10978         if [[ $RC -eq 0 ]]; then
10979                 error "Must return error due to dropped pages, rc=$RC"
10980         fi
10981
10982         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
10983         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
10984         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache |
10985                     grep -c writeback)
10986         if [[ $LOCKED -ne 0 ]]; then
10987                 error "Locked pages remain in cache, locked=$LOCKED"
10988         fi
10989
10990         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
10991                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
10992         fi
10993
10994         rm -f $DIR/$tfile
10995         echo "No pages locked after fsync"
10996
10997         return 0
10998 }
10999 run_test 118h "Verify timeout in handling recoverables errors  =========="
11000
11001 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
11002
11003 test_118i() {
11004         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11005         remote_ost_nodsh && skip "remote OST with nodsh"
11006
11007         reset_async
11008
11009         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
11010         set_nodes_failloc "$(osts_nodes)" 0x20e
11011
11012         # Should simulate ENOMEM error which is recoverable and should be handled by timeout
11013         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c &
11014         PID=$!
11015         sleep 5
11016         set_nodes_failloc "$(osts_nodes)" 0
11017
11018         wait $PID
11019         RC=$?
11020         if [[ $RC -ne 0 ]]; then
11021                 error "got error, but should be not, rc=$RC"
11022         fi
11023
11024         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11025         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11026         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11027         if [[ $LOCKED -ne 0 ]]; then
11028                 error "Locked pages remain in cache, locked=$LOCKED"
11029         fi
11030
11031         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11032                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11033         fi
11034
11035         rm -f $DIR/$tfile
11036         echo "No pages locked after fsync"
11037
11038         return 0
11039 }
11040 run_test 118i "Fix error before timeout in recoverable error  =========="
11041
11042 [ "$SLOW" = "no" ] && set_resend_count 4
11043
11044 test_118j() {
11045         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11046         remote_ost_nodsh && skip "remote OST with nodsh"
11047
11048         reset_async
11049
11050         #define OBD_FAIL_OST_BRW_WRITE_BULK2     0x220
11051         set_nodes_failloc "$(osts_nodes)" 0x220
11052
11053         # return -EIO from OST
11054         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:O_SYNC:w4096c
11055         RC=$?
11056         set_nodes_failloc "$(osts_nodes)" 0x0
11057         if [[ $RC -eq 0 ]]; then
11058                 error "Must return error due to dropped pages, rc=$RC"
11059         fi
11060
11061         LOCKED=$(lctl get_param -n llite.*.dump_page_cache | grep -c locked)
11062         DIRTY=$(lctl get_param -n llite.*.dump_page_cache | grep -c dirty)
11063         WRITEBACK=$(lctl get_param -n llite.*.dump_page_cache | grep -c writeback)
11064         if [[ $LOCKED -ne 0 ]]; then
11065                 error "Locked pages remain in cache, locked=$LOCKED"
11066         fi
11067
11068         # in recoverable error on OST we want resend and stay until it finished
11069         if [[ $DIRTY -ne 0 || $WRITEBACK -ne 0 ]]; then
11070                 error "Dirty pages not flushed to disk, dirty=$DIRTY, writeback=$WRITEBACK"
11071         fi
11072
11073         rm -f $DIR/$tfile
11074         echo "No pages locked after fsync"
11075
11076         return 0
11077 }
11078 run_test 118j "Simulate unrecoverable OST side error =========="
11079
11080 test_118k()
11081 {
11082         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11083         remote_ost_nodsh && skip "remote OSTs with nodsh"
11084
11085         #define OBD_FAIL_OST_BRW_WRITE_BULK      0x20e
11086         set_nodes_failloc "$(osts_nodes)" 0x20e
11087         test_mkdir $DIR/$tdir
11088
11089         for ((i=0;i<10;i++)); do
11090                 (dd if=/dev/zero of=$DIR/$tdir/$tfile-$i bs=1M count=10 || \
11091                         error "dd to $DIR/$tdir/$tfile-$i failed" )&
11092                 SLEEPPID=$!
11093                 sleep 0.500s
11094                 kill $SLEEPPID
11095                 wait $SLEEPPID
11096         done
11097
11098         set_nodes_failloc "$(osts_nodes)" 0
11099         rm -rf $DIR/$tdir
11100 }
11101 run_test 118k "bio alloc -ENOMEM and IO TERM handling ========="
11102
11103 test_118l() # LU-646
11104 {
11105         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11106
11107         test_mkdir $DIR/$tdir
11108         $MULTIOP $DIR/$tdir Dy || error "fsync dir failed"
11109         rm -rf $DIR/$tdir
11110 }
11111 run_test 118l "fsync dir"
11112
11113 test_118m() # LU-3066
11114 {
11115         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11116
11117         test_mkdir $DIR/$tdir
11118         $MULTIOP $DIR/$tdir DY || error "fdatasync dir failed"
11119         rm -rf $DIR/$tdir
11120 }
11121 run_test 118m "fdatasync dir ========="
11122
11123 [ "$SLOW" = "no" ] && [ -n "$OLD_RESENDCOUNT" ] && set_resend_count $OLD_RESENDCOUNT
11124
11125 test_118n()
11126 {
11127         local begin
11128         local end
11129
11130         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11131         remote_ost_nodsh && skip "remote OSTs with nodsh"
11132
11133         # Sleep to avoid a cached response.
11134         #define OBD_STATFS_CACHE_SECONDS 1
11135         sleep 2
11136
11137         # Inject a 10 second delay in the OST_STATFS handler.
11138         #define OBD_FAIL_OST_STATFS_DELAY 0x242
11139         set_nodes_failloc "$(osts_nodes)" 0x242
11140
11141         begin=$SECONDS
11142         stat --file-system $MOUNT > /dev/null
11143         end=$SECONDS
11144
11145         set_nodes_failloc "$(osts_nodes)" 0
11146
11147         if ((end - begin > 20)); then
11148             error "statfs took $((end - begin)) seconds, expected 10"
11149         fi
11150 }
11151 run_test 118n "statfs() sends OST_STATFS requests in parallel"
11152
11153 test_119a() # bug 11737
11154 {
11155         BSIZE=$((512 * 1024))
11156         directio write $DIR/$tfile 0 1 $BSIZE
11157         # We ask to read two blocks, which is more than a file size.
11158         # directio will indicate an error when requested and actual
11159         # sizes aren't equeal (a normal situation in this case) and
11160         # print actual read amount.
11161         NOB=`directio read $DIR/$tfile 0 2 $BSIZE | awk '/error/ {print $6}'`
11162         if [ "$NOB" != "$BSIZE" ]; then
11163                 error "read $NOB bytes instead of $BSIZE"
11164         fi
11165         rm -f $DIR/$tfile
11166 }
11167 run_test 119a "Short directIO read must return actual read amount"
11168
11169 test_119b() # bug 11737
11170 {
11171         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
11172
11173         $LFS setstripe -c 2 $DIR/$tfile || error "setstripe failed"
11174         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 seek=1 || error "dd failed"
11175         sync
11176         $MULTIOP $DIR/$tfile oO_RDONLY:O_DIRECT:r$((2048 * 1024)) ||
11177                 error "direct read failed"
11178         rm -f $DIR/$tfile
11179 }
11180 run_test 119b "Sparse directIO read must return actual read amount"
11181
11182 test_119c() # bug 13099
11183 {
11184         BSIZE=1048576
11185         directio write $DIR/$tfile 3 1 $BSIZE || error "direct write failed"
11186         directio readhole $DIR/$tfile 0 2 $BSIZE || error "reading hole failed"
11187         rm -f $DIR/$tfile
11188 }
11189 run_test 119c "Testing for direct read hitting hole"
11190
11191 test_119d() # bug 15950
11192 {
11193         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11194
11195         MAX_RPCS_IN_FLIGHT=`$LCTL get_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight`
11196         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight 1
11197         BSIZE=1048576
11198         $LFS setstripe $DIR/$tfile -i 0 -c 1 || error "setstripe failed"
11199         $DIRECTIO write $DIR/$tfile 0 1 $BSIZE || error "first directio failed"
11200         #define OBD_FAIL_OSC_DIO_PAUSE           0x40d
11201         lctl set_param fail_loc=0x40d
11202         $DIRECTIO write $DIR/$tfile 1 4 $BSIZE &
11203         pid_dio=$!
11204         sleep 1
11205         cat $DIR/$tfile > /dev/null &
11206         lctl set_param fail_loc=0
11207         pid_reads=$!
11208         wait $pid_dio
11209         log "the DIO writes have completed, now wait for the reads (should not block very long)"
11210         sleep 2
11211         [ -n "`ps h -p $pid_reads -o comm`" ] && \
11212         error "the read rpcs have not completed in 2s"
11213         rm -f $DIR/$tfile
11214         $LCTL set_param -n osc.*OST0000-osc-[^mM]*.max_rpcs_in_flight $MAX_RPCS_IN_FLIGHT
11215 }
11216 run_test 119d "The DIO path should try to send a new rpc once one is completed"
11217
11218 test_120a() {
11219         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11220         remote_mds_nodsh && skip "remote MDS with nodsh"
11221         test_mkdir -i0 -c1 $DIR/$tdir
11222         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11223                 skip_env "no early lock cancel on server"
11224
11225         lru_resize_disable mdc
11226         lru_resize_disable osc
11227         cancel_lru_locks mdc
11228         # asynchronous object destroy at MDT could cause bl ast to client
11229         cancel_lru_locks osc
11230
11231         stat $DIR/$tdir > /dev/null
11232         can1=$(do_facet mds1 \
11233                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11234                awk '/ldlm_cancel/ {print $2}')
11235         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11236                awk '/ldlm_bl_callback/ {print $2}')
11237         test_mkdir -i0 -c1 $DIR/$tdir/d1
11238         can2=$(do_facet mds1 \
11239                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11240                awk '/ldlm_cancel/ {print $2}')
11241         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11242                awk '/ldlm_bl_callback/ {print $2}')
11243         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11244         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11245         lru_resize_enable mdc
11246         lru_resize_enable osc
11247 }
11248 run_test 120a "Early Lock Cancel: mkdir test"
11249
11250 test_120b() {
11251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11252         remote_mds_nodsh && skip "remote MDS with nodsh"
11253         test_mkdir $DIR/$tdir
11254         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11255                 skip_env "no early lock cancel on server"
11256
11257         lru_resize_disable mdc
11258         lru_resize_disable osc
11259         cancel_lru_locks mdc
11260         stat $DIR/$tdir > /dev/null
11261         can1=$(do_facet $SINGLEMDS \
11262                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11263                awk '/ldlm_cancel/ {print $2}')
11264         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11265                awk '/ldlm_bl_callback/ {print $2}')
11266         touch $DIR/$tdir/f1
11267         can2=$(do_facet $SINGLEMDS \
11268                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11269                awk '/ldlm_cancel/ {print $2}')
11270         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11271                awk '/ldlm_bl_callback/ {print $2}')
11272         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11273         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11274         lru_resize_enable mdc
11275         lru_resize_enable osc
11276 }
11277 run_test 120b "Early Lock Cancel: create test"
11278
11279 test_120c() {
11280         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11281         remote_mds_nodsh && skip "remote MDS with nodsh"
11282         test_mkdir -i0 -c1 $DIR/$tdir
11283         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11284                 skip "no early lock cancel on server"
11285
11286         lru_resize_disable mdc
11287         lru_resize_disable osc
11288         test_mkdir -i0 -c1 $DIR/$tdir/d1
11289         test_mkdir -i0 -c1 $DIR/$tdir/d2
11290         touch $DIR/$tdir/d1/f1
11291         cancel_lru_locks mdc
11292         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 > /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         ln $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
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 120c "Early Lock Cancel: link test"
11310
11311 test_120d() {
11312         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11313         remote_mds_nodsh && skip "remote MDS with nodsh"
11314         test_mkdir -i0 -c1 $DIR/$tdir
11315         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11316                 skip_env "no early lock cancel on server"
11317
11318         lru_resize_disable mdc
11319         lru_resize_disable osc
11320         touch $DIR/$tdir
11321         cancel_lru_locks mdc
11322         stat $DIR/$tdir > /dev/null
11323         can1=$(do_facet mds1 \
11324                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11325                awk '/ldlm_cancel/ {print $2}')
11326         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11327                awk '/ldlm_bl_callback/ {print $2}')
11328         chmod a+x $DIR/$tdir
11329         can2=$(do_facet mds1 \
11330                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11331                awk '/ldlm_cancel/ {print $2}')
11332         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11333                awk '/ldlm_bl_callback/ {print $2}')
11334         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11335         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11336         lru_resize_enable mdc
11337         lru_resize_enable osc
11338 }
11339 run_test 120d "Early Lock Cancel: setattr test"
11340
11341 test_120e() {
11342         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11343         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11344                 skip_env "no early lock cancel on server"
11345         remote_mds_nodsh && skip "remote MDS with nodsh"
11346
11347         local dlmtrace_set=false
11348
11349         test_mkdir -i0 -c1 $DIR/$tdir
11350         lru_resize_disable mdc
11351         lru_resize_disable osc
11352         ! $LCTL get_param debug | grep -q dlmtrace &&
11353                 $LCTL set_param debug=+dlmtrace && dlmtrace_set=true
11354         dd if=/dev/zero of=$DIR/$tdir/f1 count=1
11355         cancel_lru_locks mdc
11356         cancel_lru_locks osc
11357         dd if=$DIR/$tdir/f1 of=/dev/null
11358         stat $DIR/$tdir $DIR/$tdir/f1 > /dev/null
11359         # XXX client can not do early lock cancel of OST lock
11360         # during unlink (LU-4206), so cancel osc lock now.
11361         sleep 2
11362         cancel_lru_locks osc
11363         can1=$(do_facet mds1 \
11364                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11365                awk '/ldlm_cancel/ {print $2}')
11366         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11367                awk '/ldlm_bl_callback/ {print $2}')
11368         unlink $DIR/$tdir/f1
11369         sleep 5
11370         can2=$(do_facet mds1 \
11371                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11372                awk '/ldlm_cancel/ {print $2}')
11373         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11374                awk '/ldlm_bl_callback/ {print $2}')
11375         [ $can1 -ne $can2 ] && error "$((can2 - can1)) cancel RPC occured" &&
11376                 $LCTL dk $TMP/cancel.debug.txt
11377         [ $blk1 -ne $blk2 ] && error "$((blk2 - blk1)) blocking RPC occured" &&
11378                 $LCTL dk $TMP/blocking.debug.txt
11379         $dlmtrace_set && $LCTL set_param debug=-dlmtrace
11380         lru_resize_enable mdc
11381         lru_resize_enable osc
11382 }
11383 run_test 120e "Early Lock Cancel: unlink test"
11384
11385 test_120f() {
11386         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11387         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11388                 skip_env "no early lock cancel on server"
11389         remote_mds_nodsh && skip "remote MDS with nodsh"
11390
11391         test_mkdir -i0 -c1 $DIR/$tdir
11392         lru_resize_disable mdc
11393         lru_resize_disable osc
11394         test_mkdir -i0 -c1 $DIR/$tdir/d1
11395         test_mkdir -i0 -c1 $DIR/$tdir/d2
11396         dd if=/dev/zero of=$DIR/$tdir/d1/f1 count=1
11397         dd if=/dev/zero of=$DIR/$tdir/d2/f2 count=1
11398         cancel_lru_locks mdc
11399         cancel_lru_locks osc
11400         dd if=$DIR/$tdir/d1/f1 of=/dev/null
11401         dd if=$DIR/$tdir/d2/f2 of=/dev/null
11402         stat $DIR/$tdir/d1 $DIR/$tdir/d2 $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2 > /dev/null
11403         # XXX client can not do early lock cancel of OST lock
11404         # during rename (LU-4206), so cancel osc lock now.
11405         sleep 2
11406         cancel_lru_locks osc
11407         can1=$(do_facet mds1 \
11408                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11409                awk '/ldlm_cancel/ {print $2}')
11410         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11411                awk '/ldlm_bl_callback/ {print $2}')
11412         mrename $DIR/$tdir/d1/f1 $DIR/$tdir/d2/f2
11413         sleep 5
11414         can2=$(do_facet mds1 \
11415                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11416                awk '/ldlm_cancel/ {print $2}')
11417         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11418                awk '/ldlm_bl_callback/ {print $2}')
11419         [ $can1 -eq $can2 ] || error $((can2-can1)) "cancel RPC occured."
11420         [ $blk1 -eq $blk2 ] || error $((blk2-blk1)) "blocking RPC occured."
11421         lru_resize_enable mdc
11422         lru_resize_enable osc
11423 }
11424 run_test 120f "Early Lock Cancel: rename test"
11425
11426 test_120g() {
11427         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11428         $LCTL get_param -n mdc.*.connect_flags | grep -q early_lock_cancel ||
11429                 skip_env "no early lock cancel on server"
11430         remote_mds_nodsh && skip "remote MDS with nodsh"
11431
11432         lru_resize_disable mdc
11433         lru_resize_disable osc
11434         count=10000
11435         echo create $count files
11436         test_mkdir $DIR/$tdir
11437         cancel_lru_locks mdc
11438         cancel_lru_locks osc
11439         t0=$(date +%s)
11440
11441         can0=$(do_facet $SINGLEMDS \
11442                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11443                awk '/ldlm_cancel/ {print $2}')
11444         blk0=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11445                awk '/ldlm_bl_callback/ {print $2}')
11446         createmany -o $DIR/$tdir/f $count
11447         sync
11448         can1=$(do_facet $SINGLEMDS \
11449                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11450                awk '/ldlm_cancel/ {print $2}')
11451         blk1=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11452                awk '/ldlm_bl_callback/ {print $2}')
11453         t1=$(date +%s)
11454         echo total: $((can1-can0)) cancels, $((blk1-blk0)) blockings
11455         echo rm $count files
11456         rm -r $DIR/$tdir
11457         sync
11458         can2=$(do_facet $SINGLEMDS \
11459                "$LCTL get_param -n ldlm.services.ldlm_canceld.stats" |
11460                awk '/ldlm_cancel/ {print $2}')
11461         blk2=$($LCTL get_param -n ldlm.services.ldlm_cbd.stats |
11462                awk '/ldlm_bl_callback/ {print $2}')
11463         t2=$(date +%s)
11464         echo total: $count removes in $((t2-t1))
11465         echo total: $((can2-can1)) cancels, $((blk2-blk1)) blockings
11466         sleep 2
11467         # wait for commitment of removal
11468         lru_resize_enable mdc
11469         lru_resize_enable osc
11470 }
11471 run_test 120g "Early Lock Cancel: performance test"
11472
11473 test_121() { #bug #10589
11474         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11475
11476         rm -rf $DIR/$tfile
11477         writes=$(LANG=C dd if=/dev/zero of=$DIR/$tfile count=1 2>&1 | awk -F '+' '/out$/ {print $1}')
11478 #define OBD_FAIL_LDLM_CANCEL_RACE        0x310
11479         lctl set_param fail_loc=0x310
11480         cancel_lru_locks osc > /dev/null
11481         reads=$(LANG=C dd if=$DIR/$tfile of=/dev/null 2>&1 | awk -F '+' '/in$/ {print $1}')
11482         lctl set_param fail_loc=0
11483         [[ $reads -eq $writes ]] ||
11484                 error "read $reads blocks, must be $writes blocks"
11485 }
11486 run_test 121 "read cancel race ========="
11487
11488 test_123a() { # was test 123, statahead(bug 11401)
11489         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11490
11491         SLOWOK=0
11492         if ! grep -q "processor.*: 1" /proc/cpuinfo; then
11493                 log "testing UP system. Performance may be lower than expected."
11494                 SLOWOK=1
11495         fi
11496
11497         rm -rf $DIR/$tdir
11498         test_mkdir $DIR/$tdir
11499         NUMFREE=$(df -i -P $DIR | tail -n 1 | awk '{ print $4 }')
11500         [[ $NUMFREE -gt 100000 ]] && NUMFREE=100000 || NUMFREE=$((NUMFREE-1000))
11501         MULT=10
11502         for ((i=100, j=0; i<=$NUMFREE; j=$i, i=$((i * MULT)) )); do
11503                 createmany -o $DIR/$tdir/$tfile $j $((i - j))
11504
11505                 max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11506                 lctl set_param -n llite.*.statahead_max 0
11507                 lctl get_param llite.*.statahead_max
11508                 cancel_lru_locks mdc
11509                 cancel_lru_locks osc
11510                 stime=`date +%s`
11511                 time ls -l $DIR/$tdir | wc -l
11512                 etime=`date +%s`
11513                 delta=$((etime - stime))
11514                 log "ls $i files without statahead: $delta sec"
11515                 lctl set_param llite.*.statahead_max=$max
11516
11517                 swrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11518                 lctl get_param -n llite.*.statahead_max | grep '[0-9]'
11519                 cancel_lru_locks mdc
11520                 cancel_lru_locks osc
11521                 stime=`date +%s`
11522                 time ls -l $DIR/$tdir | wc -l
11523                 etime=`date +%s`
11524                 delta_sa=$((etime - stime))
11525                 log "ls $i files with statahead: $delta_sa sec"
11526                 lctl get_param -n llite.*.statahead_stats
11527                 ewrong=`lctl get_param -n llite.*.statahead_stats | grep "statahead wrong:" | awk '{print $3}'`
11528
11529                 [[ $swrong -lt $ewrong ]] &&
11530                         log "statahead was stopped, maybe too many locks held!"
11531                 [[ $delta -eq 0 || $delta_sa -eq 0 ]] && continue
11532
11533                 if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11534                     max=`lctl get_param -n llite.*.statahead_max | head -n 1`
11535                     lctl set_param -n llite.*.statahead_max 0
11536                     lctl get_param llite.*.statahead_max
11537                     cancel_lru_locks mdc
11538                     cancel_lru_locks osc
11539                     stime=`date +%s`
11540                     time ls -l $DIR/$tdir | wc -l
11541                     etime=`date +%s`
11542                     delta=$((etime - stime))
11543                     log "ls $i files again without statahead: $delta sec"
11544                     lctl set_param llite.*.statahead_max=$max
11545                     if [ $((delta_sa * 100)) -gt $((delta * 105)) -a $delta_sa -gt $((delta + 2)) ]; then
11546                         if [  $SLOWOK -eq 0 ]; then
11547                                 error "ls $i files is slower with statahead!"
11548                         else
11549                                 log "ls $i files is slower with statahead!"
11550                         fi
11551                         break
11552                     fi
11553                 fi
11554
11555                 [ $delta -gt 20 ] && break
11556                 [ $delta -gt 8 ] && MULT=$((50 / delta))
11557                 [ "$SLOW" = "no" -a $delta -gt 5 ] && break
11558         done
11559         log "ls done"
11560
11561         stime=`date +%s`
11562         rm -r $DIR/$tdir
11563         sync
11564         etime=`date +%s`
11565         delta=$((etime - stime))
11566         log "rm -r $DIR/$tdir/: $delta seconds"
11567         log "rm done"
11568         lctl get_param -n llite.*.statahead_stats
11569 }
11570 run_test 123a "verify statahead work"
11571
11572 test_123b () { # statahead(bug 15027)
11573         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11574
11575         test_mkdir $DIR/$tdir
11576         createmany -o $DIR/$tdir/$tfile-%d 1000
11577
11578         cancel_lru_locks mdc
11579         cancel_lru_locks osc
11580
11581 #define OBD_FAIL_MDC_GETATTR_ENQUEUE     0x803
11582         lctl set_param fail_loc=0x80000803
11583         ls -lR $DIR/$tdir > /dev/null
11584         log "ls done"
11585         lctl set_param fail_loc=0x0
11586         lctl get_param -n llite.*.statahead_stats
11587         rm -r $DIR/$tdir
11588         sync
11589
11590 }
11591 run_test 123b "not panic with network error in statahead enqueue (bug 15027)"
11592
11593 test_123c() {
11594         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
11595
11596         test_mkdir -i 0 -c 1 $DIR/$tdir.0
11597         test_mkdir -i 1 -c 1 $DIR/$tdir.1
11598         touch $DIR/$tdir.1/{1..3}
11599         mv $DIR/$tdir.1/{1..3} $DIR/$tdir.0
11600
11601         remount_client $MOUNT
11602
11603         $MULTIOP $DIR/$tdir.0 Q
11604
11605         # let statahead to complete
11606         ls -l $DIR/$tdir.0 > /dev/null
11607
11608         testid=$(echo $TESTNAME | tr '_' ' ')
11609         dmesg | tac | sed "/$testid/,$ d" | grep "Can not initialize inode" &&
11610                 error "statahead warning" || true
11611 }
11612 run_test 123c "Can not initialize inode warning on DNE statahead"
11613
11614 test_124a() {
11615         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11616         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11617                 skip_env "no lru resize on server"
11618
11619         local NR=2000
11620
11621         test_mkdir $DIR/$tdir
11622
11623         log "create $NR files at $DIR/$tdir"
11624         createmany -o $DIR/$tdir/f $NR ||
11625                 error "failed to create $NR files in $DIR/$tdir"
11626
11627         cancel_lru_locks mdc
11628         ls -l $DIR/$tdir > /dev/null
11629
11630         local NSDIR=""
11631         local LRU_SIZE=0
11632         for VALUE in $($LCTL get_param ldlm.namespaces.*mdc-*.lru_size); do
11633                 local PARAM=$(echo ${VALUE[0]} | cut -d "=" -f1)
11634                 LRU_SIZE=$($LCTL get_param -n $PARAM)
11635                 if [[ $LRU_SIZE -gt $(default_lru_size) ]]; then
11636                         NSDIR=$(echo $PARAM | cut -d "." -f1-3)
11637                         log "NSDIR=$NSDIR"
11638                         log "NS=$(basename $NSDIR)"
11639                         break
11640                 fi
11641         done
11642
11643         if [[ -z "$NSDIR" || $LRU_SIZE -lt $(default_lru_size) ]]; then
11644                 skip "Not enough cached locks created!"
11645         fi
11646         log "LRU=$LRU_SIZE"
11647
11648         local SLEEP=30
11649
11650         # We know that lru resize allows one client to hold $LIMIT locks
11651         # for 10h. After that locks begin to be killed by client.
11652         local MAX_HRS=10
11653         local LIMIT=$($LCTL get_param -n $NSDIR.pool.limit)
11654         log "LIMIT=$LIMIT"
11655         if [ $LIMIT -lt $LRU_SIZE ]; then
11656                 skip "Limit is too small $LIMIT"
11657         fi
11658
11659         # Make LVF so higher that sleeping for $SLEEP is enough to _start_
11660         # killing locks. Some time was spent for creating locks. This means
11661         # that up to the moment of sleep finish we must have killed some of
11662         # them (10-100 locks). This depends on how fast ther were created.
11663         # Many of them were touched in almost the same moment and thus will
11664         # be killed in groups.
11665         local LVF=$(($MAX_HRS * 60 * 60 / $SLEEP * $LIMIT / $LRU_SIZE))
11666
11667         # Use $LRU_SIZE_B here to take into account real number of locks
11668         # created in the case of CMD, LRU_SIZE_B != $NR in most of cases
11669         local LRU_SIZE_B=$LRU_SIZE
11670         log "LVF=$LVF"
11671         local OLD_LVF=$($LCTL get_param -n $NSDIR.pool.lock_volume_factor)
11672         log "OLD_LVF=$OLD_LVF"
11673         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $LVF
11674
11675         # Let's make sure that we really have some margin. Client checks
11676         # cached locks every 10 sec.
11677         SLEEP=$((SLEEP+20))
11678         log "Sleep ${SLEEP} sec"
11679         local SEC=0
11680         while ((SEC<$SLEEP)); do
11681                 echo -n "..."
11682                 sleep 5
11683                 SEC=$((SEC+5))
11684                 LRU_SIZE=$($LCTL get_param -n $NSDIR/lru_size)
11685                 echo -n "$LRU_SIZE"
11686         done
11687         echo ""
11688         $LCTL set_param -n $NSDIR.pool.lock_volume_factor $OLD_LVF
11689         local LRU_SIZE_A=$($LCTL get_param -n $NSDIR.lru_size)
11690
11691         [[ $LRU_SIZE_B -gt $LRU_SIZE_A ]] || {
11692                 error "No locks dropped in ${SLEEP}s. LRU size: $LRU_SIZE_A"
11693                 unlinkmany $DIR/$tdir/f $NR
11694                 return
11695         }
11696
11697         log "Dropped "$((LRU_SIZE_B-LRU_SIZE_A))" locks in ${SLEEP}s"
11698         log "unlink $NR files at $DIR/$tdir"
11699         unlinkmany $DIR/$tdir/f $NR
11700 }
11701 run_test 124a "lru resize ======================================="
11702
11703 get_max_pool_limit()
11704 {
11705         local limit=$($LCTL get_param \
11706                       -n ldlm.namespaces.*-MDT0000-mdc-*.pool.limit)
11707         local max=0
11708         for l in $limit; do
11709                 if [[ $l -gt $max ]]; then
11710                         max=$l
11711                 fi
11712         done
11713         echo $max
11714 }
11715
11716 test_124b() {
11717         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11718         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11719                 skip_env "no lru resize on server"
11720
11721         LIMIT=$(get_max_pool_limit)
11722
11723         NR=$(($(default_lru_size)*20))
11724         if [[ $NR -gt $LIMIT ]]; then
11725                 log "Limit lock number by $LIMIT locks"
11726                 NR=$LIMIT
11727         fi
11728
11729         IFree=$(mdsrate_inodes_available)
11730         if [ $IFree -lt $NR ]; then
11731                 log "Limit lock number by $IFree inodes"
11732                 NR=$IFree
11733         fi
11734
11735         lru_resize_disable mdc
11736         test_mkdir -p $DIR/$tdir/disable_lru_resize
11737
11738         createmany -o $DIR/$tdir/disable_lru_resize/f $NR
11739         log "doing ls -la $DIR/$tdir/disable_lru_resize 3 times"
11740         cancel_lru_locks mdc
11741         stime=`date +%s`
11742         PID=""
11743         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11744         PID="$PID $!"
11745         sleep 2
11746         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11747         PID="$PID $!"
11748         sleep 2
11749         ls -la $DIR/$tdir/disable_lru_resize > /dev/null &
11750         PID="$PID $!"
11751         wait $PID
11752         etime=`date +%s`
11753         nolruresize_delta=$((etime-stime))
11754         log "ls -la time: $nolruresize_delta seconds"
11755         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11756         unlinkmany $DIR/$tdir/disable_lru_resize/f $NR
11757
11758         lru_resize_enable mdc
11759         test_mkdir -p $DIR/$tdir/enable_lru_resize
11760
11761         createmany -o $DIR/$tdir/enable_lru_resize/f $NR
11762         log "doing ls -la $DIR/$tdir/enable_lru_resize 3 times"
11763         cancel_lru_locks mdc
11764         stime=`date +%s`
11765         PID=""
11766         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11767         PID="$PID $!"
11768         sleep 2
11769         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11770         PID="$PID $!"
11771         sleep 2
11772         ls -la $DIR/$tdir/enable_lru_resize > /dev/null &
11773         PID="$PID $!"
11774         wait $PID
11775         etime=`date +%s`
11776         lruresize_delta=$((etime-stime))
11777         log "ls -la time: $lruresize_delta seconds"
11778         log "lru_size = $(lctl get_param -n ldlm.namespaces.*mdc*.lru_size)"
11779
11780         if [ $lruresize_delta -gt $nolruresize_delta ]; then
11781                 log "ls -la is $(((lruresize_delta - $nolruresize_delta) * 100 / $nolruresize_delta))% slower with lru resize enabled"
11782         elif [ $nolruresize_delta -gt $lruresize_delta ]; then
11783                 log "ls -la is $(((nolruresize_delta - $lruresize_delta) * 100 / $nolruresize_delta))% faster with lru resize enabled"
11784         else
11785                 log "lru resize performs the same with no lru resize"
11786         fi
11787         unlinkmany $DIR/$tdir/enable_lru_resize/f $NR
11788 }
11789 run_test 124b "lru resize (performance test) ======================="
11790
11791 test_124c() {
11792         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11793         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11794                 skip_env "no lru resize on server"
11795
11796         # cache ununsed locks on client
11797         local nr=100
11798         cancel_lru_locks mdc
11799         test_mkdir $DIR/$tdir
11800         createmany -o $DIR/$tdir/f $nr ||
11801                 error "failed to create $nr files in $DIR/$tdir"
11802         ls -l $DIR/$tdir > /dev/null
11803
11804         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11805         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11806         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11807         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11808         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11809
11810         # set lru_max_age to 1 sec
11811         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11812         echo "sleep $((recalc_p * 2)) seconds..."
11813         sleep $((recalc_p * 2))
11814
11815         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11816         # restore lru_max_age
11817         $LCTL set_param -n $nsdir.lru_max_age $max_age
11818         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11819         unlinkmany $DIR/$tdir/f $nr
11820 }
11821 run_test 124c "LRUR cancel very aged locks"
11822
11823 test_124d() {
11824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11825         $LCTL get_param -n mdc.*.connect_flags | grep -q lru_resize ||
11826                 skip_env "no lru resize on server"
11827
11828         # cache ununsed locks on client
11829         local nr=100
11830
11831         lru_resize_disable mdc
11832         stack_trap "lru_resize_enable mdc" EXIT
11833
11834         cancel_lru_locks mdc
11835
11836         # asynchronous object destroy at MDT could cause bl ast to client
11837         test_mkdir $DIR/$tdir
11838         createmany -o $DIR/$tdir/f $nr ||
11839                 error "failed to create $nr files in $DIR/$tdir"
11840         stack_trap "unlinkmany $DIR/$tdir/f $nr" EXIT
11841
11842         ls -l $DIR/$tdir > /dev/null
11843
11844         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
11845         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
11846         local max_age=$($LCTL get_param -n $nsdir.lru_max_age)
11847         local recalc_p=$($LCTL get_param -n $nsdir.pool.recalc_period)
11848
11849         echo "unused=$unused, max_age=$max_age, recalc_p=$recalc_p"
11850
11851         # set lru_max_age to 1 sec
11852         $LCTL set_param $nsdir.lru_max_age=1000 # milliseconds
11853         stack_trap "$LCTL set_param -n $nsdir.lru_max_age $max_age" EXIT
11854
11855         echo "sleep $((recalc_p * 2)) seconds..."
11856         sleep $((recalc_p * 2))
11857
11858         local remaining=$($LCTL get_param -n $nsdir.lock_unused_count)
11859
11860         [ $remaining -eq 0 ] || error "$remaining locks are not canceled"
11861 }
11862 run_test 124d "cancel very aged locks if lru-resize diasbaled"
11863
11864 test_125() { # 13358
11865         $LCTL get_param -n llite.*.client_type | grep -q local ||
11866                 skip "must run as local client"
11867         $LCTL get_param -n mdc.*-mdc-*.connect_flags | grep -q acl ||
11868                 skip_env "must have acl enabled"
11869         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
11870
11871         test_mkdir $DIR/$tdir
11872         $LFS setstripe -S 65536 -c -1 $DIR/$tdir || error "setstripe failed"
11873         setfacl -R -m u:bin:rwx $DIR/$tdir || error "setfacl $DIR/$tdir failed"
11874         ls -ld $DIR/$tdir || error "cannot access $DIR/$tdir"
11875 }
11876 run_test 125 "don't return EPROTO when a dir has a non-default striping and ACLs"
11877
11878 test_126() { # bug 12829/13455
11879         $GSS && skip_env "must run as gss disabled"
11880         $LCTL get_param -n llite.*.client_type | grep -q local ||
11881                 skip "must run as local client"
11882         [ "$UID" != 0 ] && skip "must run as root, not UID $UID"
11883
11884         $RUNAS -u 0 -g 1 touch $DIR/$tfile || error "touch failed"
11885         gid=`ls -n $DIR/$tfile | awk '{print $4}'`
11886         rm -f $DIR/$tfile
11887         [ $gid -eq "1" ] || error "gid is set to" $gid "instead of 1"
11888 }
11889 run_test 126 "check that the fsgid provided by the client is taken into account"
11890
11891 test_127a() { # bug 15521
11892         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11893         local name count samp unit min max sum sumsq
11894
11895         $LFS setstripe -i 0 -c 1 $DIR/$tfile || error "setstripe failed"
11896         echo "stats before reset"
11897         $LCTL get_param osc.*.stats
11898         $LCTL set_param osc.*.stats=0
11899         local fsize=$((2048 * 1024))
11900
11901         dd if=/dev/zero of=$DIR/$tfile bs=$fsize count=1
11902         cancel_lru_locks osc
11903         dd if=$DIR/$tfile of=/dev/null bs=$fsize
11904
11905         $LCTL get_param osc.*0000-osc-*.stats | grep samples > $DIR/$tfile.tmp
11906         stack_trap "rm -f $TMP/$tfile.tmp"
11907         while read name count samp unit min max sum sumsq; do
11908                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11909                 [ ! $min ] && error "Missing min value for $name proc entry"
11910                 eval $name=$count || error "Wrong proc format"
11911
11912                 case $name in
11913                 read_bytes|write_bytes)
11914                         [[ "$unit" =~ "bytes" ]] ||
11915                                 error "unit is not 'bytes': $unit"
11916                         (( $min >= 4096 )) || error "min is too small: $min"
11917                         (( $min <= $fsize )) || error "min is too big: $min"
11918                         (( $max >= 4096 )) || error "max is too small: $max"
11919                         (( $max <= $fsize )) || error "max is too big: $max"
11920                         (( $sum == $fsize )) || error "sum is wrong: $sum"
11921                         (( $sumsq >= ($fsize / 4096) * (4096 * 4096) )) ||
11922                                 error "sumsquare is too small: $sumsq"
11923                         (( $sumsq <= $fsize * $fsize )) ||
11924                                 error "sumsquare is too big: $sumsq"
11925                         ;;
11926                 ost_read|ost_write)
11927                         [[ "$unit" =~ "usec" ]] ||
11928                                 error "unit is not 'usec': $unit"
11929                         ;;
11930                 *)      ;;
11931                 esac
11932         done < $DIR/$tfile.tmp
11933
11934         #check that we actually got some stats
11935         [ "$read_bytes" ] || error "Missing read_bytes stats"
11936         [ "$write_bytes" ] || error "Missing write_bytes stats"
11937         [ "$read_bytes" != 0 ] || error "no read done"
11938         [ "$write_bytes" != 0 ] || error "no write done"
11939 }
11940 run_test 127a "verify the client stats are sane"
11941
11942 test_127b() { # bug LU-333
11943         [ $PARALLEL == "yes" ] && skip "skip parallel run"
11944         local name count samp unit min max sum sumsq
11945
11946         echo "stats before reset"
11947         $LCTL get_param llite.*.stats
11948         $LCTL set_param llite.*.stats=0
11949
11950         # perform 2 reads and writes so MAX is different from SUM.
11951         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11952         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1
11953         cancel_lru_locks osc
11954         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11955         dd if=$DIR/$tfile of=/dev/null bs=$PAGE_SIZE count=1
11956
11957         $LCTL get_param llite.*.stats | grep samples > $TMP/$tfile.tmp
11958         stack_trap "rm -f $TMP/$tfile.tmp"
11959         while read name count samp unit min max sum sumsq; do
11960                 echo "got name=$name count=$count unit=$unit min=$min max=$max"
11961                 eval $name=$count || error "Wrong proc format"
11962
11963                 case $name in
11964                 read_bytes|write_bytes)
11965                         [[ "$unit" =~ "bytes" ]] ||
11966                                 error "unit is not 'bytes': $unit"
11967                         (( $count == 2 )) || error "count is not 2: $count"
11968                         (( $min == $PAGE_SIZE )) ||
11969                                 error "min is not $PAGE_SIZE: $min"
11970                         (( $max == $PAGE_SIZE )) ||
11971                                 error "max is not $PAGE_SIZE: $max"
11972                         (( $sum == $PAGE_SIZE * 2 )) ||
11973                                 error "sum is not $((PAGE_SIZE * 2)): $sum"
11974                         ;;
11975                 read|write)
11976                         [[ "$unit" =~ "usec" ]] ||
11977                                 error "unit is not 'usec': $unit"
11978                         ;;
11979                 *)      ;;
11980                 esac
11981         done < $TMP/$tfile.tmp
11982
11983         #check that we actually got some stats
11984         [ "$read_bytes" ] || error "Missing read_bytes stats"
11985         [ "$write_bytes" ] || error "Missing write_bytes stats"
11986         [ "$read_bytes" != 0 ] || error "no read done"
11987         [ "$write_bytes" != 0 ] || error "no write done"
11988 }
11989 run_test 127b "verify the llite client stats are sane"
11990
11991 test_127c() { # LU-12394
11992         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
11993         local size
11994         local bsize
11995         local reads
11996         local writes
11997         local count
11998
11999         $LCTL set_param llite.*.extents_stats=1
12000         stack_trap "$LCTL set_param llite.*.extents_stats=0" EXIT
12001
12002         # Use two stripes so there is enough space in default config
12003         $LFS setstripe -c 2 $DIR/$tfile
12004
12005         # Extent stats start at 0-4K and go in power of two buckets
12006         # LL_HIST_START = 12 --> 2^12 = 4K
12007         # We do 3K*2^i, so 3K, 6K, 12K, 24K... hitting each bucket.
12008         # We do not do buckets larger than 64 MiB to avoid ENOSPC issues on
12009         # small configs
12010         for size in 3K 6K 12K 24K 48K 96K 192K 384K 768K 1536K 3M 6M 12M 24M 48M;
12011                 do
12012                 # Write and read, 2x each, second time at a non-zero offset
12013                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1
12014                 dd if=/dev/zero of=$DIR/$tfile bs=$size count=1 seek=10
12015                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1
12016                 dd if=$DIR/$tfile of=/dev/null bs=$size count=1 seek=10
12017                 rm -f $DIR/$tfile
12018         done
12019
12020         $LCTL get_param llite.*.extents_stats
12021
12022         count=2
12023         for bsize in 4K 8K 16K 32K 64K 128K 256K 512K 1M 2M 4M 8M 16M 32M 64M;
12024                 do
12025                 local bucket=$($LCTL get_param -n llite.*.extents_stats |
12026                                 grep -m 1 $bsize)
12027                 reads=$(echo $bucket | awk '{print $5}')
12028                 writes=$(echo $bucket | awk '{print $9}')
12029                 [ "$reads" -eq $count ] ||
12030                         error "$reads reads in < $bsize bucket, expect $count"
12031                 [ "$writes" -eq $count ] ||
12032                         error "$writes writes in < $bsize bucket, expect $count"
12033         done
12034
12035         # Test mmap write and read
12036         $LCTL set_param llite.*.extents_stats=c
12037         size=512
12038         dd if=/dev/zero of=$DIR/$tfile bs=${size}K count=1
12039         $MULTIOP $DIR/$tfile OSMRUc || error "$MULTIOP $DIR/$tfile failed"
12040         $MULTIOP $DIR/$tfile OSMWUc || error "$MULTIOP $DIR/$tfile failed"
12041
12042         $LCTL get_param llite.*.extents_stats
12043
12044         count=$(((size*1024) / PAGE_SIZE))
12045
12046         bsize=$((2 * PAGE_SIZE / 1024))K
12047
12048         bucket=$($LCTL get_param -n llite.*.extents_stats |
12049                         grep -m 1 $bsize)
12050         reads=$(echo $bucket | awk '{print $5}')
12051         writes=$(echo $bucket | awk '{print $9}')
12052         # mmap writes fault in the page first, creating an additonal read
12053         [ "$reads" -eq $((2 * count)) ] ||
12054                 error "$reads reads in < $bsize bucket, expect $count"
12055         [ "$writes" -eq $count ] ||
12056                 error "$writes writes in < $bsize bucket, expect $count"
12057 }
12058 run_test 127c "test llite extent stats with regular & mmap i/o"
12059
12060 test_128() { # bug 15212
12061         touch $DIR/$tfile
12062         $LFS 2>&1 <<-EOF | tee $TMP/$tfile.log
12063                 find $DIR/$tfile
12064                 find $DIR/$tfile
12065         EOF
12066
12067         result=$(grep error $TMP/$tfile.log)
12068         rm -f $DIR/$tfile $TMP/$tfile.log
12069         [ -z "$result" ] ||
12070                 error "consecutive find's under interactive lfs failed"
12071 }
12072 run_test 128 "interactive lfs for 2 consecutive find's"
12073
12074 set_dir_limits () {
12075         local mntdev
12076         local canondev
12077         local node
12078
12079         local ldproc=/proc/fs/ldiskfs
12080         local facets=$(get_facets MDS)
12081
12082         for facet in ${facets//,/ }; do
12083                 canondev=$(ldiskfs_canon \
12084                            *.$(convert_facet2label $facet).mntdev $facet)
12085                 do_facet $facet "test -e $ldproc/$canondev/max_dir_size" ||
12086                         ldproc=/sys/fs/ldiskfs
12087                 do_facet $facet "echo $1 >$ldproc/$canondev/max_dir_size"
12088                 do_facet $facet "echo $2 >$ldproc/$canondev/warning_dir_size"
12089         done
12090 }
12091
12092 check_mds_dmesg() {
12093         local facets=$(get_facets MDS)
12094         for facet in ${facets//,/ }; do
12095                 do_facet $facet "dmesg | tail -3 | grep -q $1" && return 0
12096         done
12097         return 1
12098 }
12099
12100 test_129() {
12101         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12102         [[ $MDS1_VERSION -ge $(version_code 2.5.56) ]] ||
12103                 skip "Need MDS version with at least 2.5.56"
12104         if [ "$mds1_FSTYPE" != ldiskfs ]; then
12105                 skip_env "ldiskfs only test"
12106         fi
12107         remote_mds_nodsh && skip "remote MDS with nodsh"
12108
12109         local ENOSPC=28
12110         local EFBIG=27
12111         local has_warning=false
12112
12113         rm -rf $DIR/$tdir
12114         mkdir -p $DIR/$tdir
12115
12116         # block size of mds1
12117         local maxsize=$(($($LCTL get_param -n mdc.*MDT0000*.blocksize) * 5))
12118         set_dir_limits $maxsize $maxsize
12119         local dirsize=$(stat -c%s "$DIR/$tdir")
12120         local nfiles=0
12121         while [[ $dirsize -le $maxsize ]]; do
12122                 $MULTIOP $DIR/$tdir/file_base_$nfiles Oc
12123                 rc=$?
12124                 if ! $has_warning; then
12125                         check_mds_dmesg '"is approaching"' && has_warning=true
12126                 fi
12127                 # check two errors:
12128                 # ENOSPC for new ext4 max_dir_size (kernel commit df981d03ee)
12129                 # EFBIG for previous versions included in ldiskfs series
12130                 if [ $rc -eq $EFBIG ] || [ $rc -eq $ENOSPC ]; then
12131                         set_dir_limits 0 0
12132                         echo "return code $rc received as expected"
12133
12134                         createmany -o $DIR/$tdir/file_extra_$nfiles. 5 ||
12135                                 error_exit "create failed w/o dir size limit"
12136
12137                         check_mds_dmesg '"has reached"' ||
12138                                 error_exit "reached message should be output"
12139
12140                         [ $has_warning = "false" ] &&
12141                                 error_exit "warning message should be output"
12142
12143                         dirsize=$(stat -c%s "$DIR/$tdir")
12144
12145                         [[ $dirsize -ge $maxsize ]] && return 0
12146                         error_exit "current dir size $dirsize, " \
12147                                    "previous limit $maxsize"
12148                 elif [ $rc -ne 0 ]; then
12149                         set_dir_limits 0 0
12150                         error_exit "return $rc received instead of expected " \
12151                                    "$EFBIG or $ENOSPC, files in dir $dirsize"
12152                 fi
12153                 nfiles=$((nfiles + 1))
12154                 dirsize=$(stat -c%s "$DIR/$tdir")
12155         done
12156
12157         set_dir_limits 0 0
12158         error "exceeded dir size limit $maxsize($MDSCOUNT) : $dirsize bytes"
12159 }
12160 run_test 129 "test directory size limit ========================"
12161
12162 OLDIFS="$IFS"
12163 cleanup_130() {
12164         trap 0
12165         IFS="$OLDIFS"
12166 }
12167
12168 test_130a() {
12169         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12170         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12171
12172         trap cleanup_130 EXIT RETURN
12173
12174         local fm_file=$DIR/$tfile
12175         $LFS setstripe -S 65536 -c 1 $fm_file || error "setstripe on $fm_file"
12176         dd if=/dev/zero of=$fm_file bs=65536 count=1 ||
12177                 error "dd failed for $fm_file"
12178
12179         # LU-1795: test filefrag/FIEMAP once, even if unsupported
12180         filefrag -ves $fm_file
12181         RC=$?
12182         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12183                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12184         [ $RC != 0 ] && error "filefrag $fm_file failed"
12185
12186         filefrag_op=$(filefrag -ve -k $fm_file |
12187                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12188         lun=$($LFS getstripe -i $fm_file)
12189
12190         start_blk=`echo $filefrag_op | cut -d: -f2 | cut -d. -f1`
12191         IFS=$'\n'
12192         tot_len=0
12193         for line in $filefrag_op
12194         do
12195                 frag_lun=`echo $line | cut -d: -f5`
12196                 ext_len=`echo $line | cut -d: -f4`
12197                 if (( $frag_lun != $lun )); then
12198                         cleanup_130
12199                         error "FIEMAP on 1-stripe file($fm_file) failed"
12200                         return
12201                 fi
12202                 (( tot_len += ext_len ))
12203         done
12204
12205         if (( lun != frag_lun || start_blk != 0 || tot_len != 64 )); then
12206                 cleanup_130
12207                 error "FIEMAP on 1-stripe file($fm_file) failed;"
12208                 return
12209         fi
12210
12211         cleanup_130
12212
12213         echo "FIEMAP on single striped file succeeded"
12214 }
12215 run_test 130a "FIEMAP (1-stripe file)"
12216
12217 test_130b() {
12218         [ "$OSTCOUNT" -lt "2" ] && skip_env "needs >= 2 OSTs"
12219
12220         local filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12221         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12222
12223         trap cleanup_130 EXIT RETURN
12224
12225         local fm_file=$DIR/$tfile
12226         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12227                         error "setstripe on $fm_file"
12228         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12229                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12230
12231         dd if=/dev/zero of=$fm_file bs=1M count=$OSTCOUNT ||
12232                 error "dd failed on $fm_file"
12233
12234         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12235         filefrag_op=$(filefrag -ve -k $fm_file |
12236                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12237
12238         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12239                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12240
12241         IFS=$'\n'
12242         tot_len=0
12243         num_luns=1
12244         for line in $filefrag_op
12245         do
12246                 frag_lun=$(echo $line | cut -d: -f5 |
12247                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12248                 ext_len=$(echo $line | cut -d: -f4)
12249                 if (( $frag_lun != $last_lun )); then
12250                         if (( tot_len != 1024 )); then
12251                                 cleanup_130
12252                                 error "FIEMAP on $fm_file failed; returned " \
12253                                 "len $tot_len for OST $last_lun instead of 1024"
12254                                 return
12255                         else
12256                                 (( num_luns += 1 ))
12257                                 tot_len=0
12258                         fi
12259                 fi
12260                 (( tot_len += ext_len ))
12261                 last_lun=$frag_lun
12262         done
12263         if (( num_luns != $OSTCOUNT || tot_len != 1024 )); then
12264                 cleanup_130
12265                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12266                         "luns or wrong len for OST $last_lun"
12267                 return
12268         fi
12269
12270         cleanup_130
12271
12272         echo "FIEMAP on $OSTCOUNT-stripe file succeeded"
12273 }
12274 run_test 130b "FIEMAP ($OSTCOUNT-stripe file)"
12275
12276 test_130c() {
12277         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12278
12279         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12280         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12281
12282         trap cleanup_130 EXIT RETURN
12283
12284         local fm_file=$DIR/$tfile
12285         $LFS setstripe -S 65536 -c 2 $fm_file || error "setstripe on $fm_file"
12286         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12287                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12288
12289         dd if=/dev/zero of=$fm_file seek=1 bs=1M count=1 ||
12290                         error "dd failed on $fm_file"
12291
12292         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12293         filefrag_op=$(filefrag -ve -k $fm_file |
12294                 sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12295
12296         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12297                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12298
12299         IFS=$'\n'
12300         tot_len=0
12301         num_luns=1
12302         for line in $filefrag_op
12303         do
12304                 frag_lun=$(echo $line | cut -d: -f5 |
12305                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12306                 ext_len=$(echo $line | cut -d: -f4)
12307                 if (( $frag_lun != $last_lun )); then
12308                         logical=`echo $line | cut -d: -f2 | cut -d. -f1`
12309                         if (( logical != 512 )); then
12310                                 cleanup_130
12311                                 error "FIEMAP on $fm_file failed; returned " \
12312                                 "logical start for lun $logical instead of 512"
12313                                 return
12314                         fi
12315                         if (( tot_len != 512 )); then
12316                                 cleanup_130
12317                                 error "FIEMAP on $fm_file failed; returned " \
12318                                 "len $tot_len for OST $last_lun instead of 1024"
12319                                 return
12320                         else
12321                                 (( num_luns += 1 ))
12322                                 tot_len=0
12323                         fi
12324                 fi
12325                 (( tot_len += ext_len ))
12326                 last_lun=$frag_lun
12327         done
12328         if (( num_luns != 2 || tot_len != 512 )); then
12329                 cleanup_130
12330                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12331                         "luns or wrong len for OST $last_lun"
12332                 return
12333         fi
12334
12335         cleanup_130
12336
12337         echo "FIEMAP on 2-stripe file with hole succeeded"
12338 }
12339 run_test 130c "FIEMAP (2-stripe file with hole)"
12340
12341 test_130d() {
12342         [[ $OSTCOUNT -lt 3 ]] && skip_env "needs >= 3 OSTs"
12343
12344         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12345         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12346
12347         trap cleanup_130 EXIT RETURN
12348
12349         local fm_file=$DIR/$tfile
12350         $LFS setstripe -S 65536 -c $OSTCOUNT $fm_file ||
12351                         error "setstripe on $fm_file"
12352         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12353                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12354
12355         local actual_stripe_count=$($LFS getstripe -c $fm_file)
12356         dd if=/dev/zero of=$fm_file bs=1M count=$actual_stripe_count ||
12357                 error "dd failed on $fm_file"
12358
12359         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12360         filefrag_op=$(filefrag -ve -k $fm_file |
12361                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12362
12363         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12364                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12365
12366         IFS=$'\n'
12367         tot_len=0
12368         num_luns=1
12369         for line in $filefrag_op
12370         do
12371                 frag_lun=$(echo $line | cut -d: -f5 |
12372                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12373                 ext_len=$(echo $line | cut -d: -f4)
12374                 if (( $frag_lun != $last_lun )); then
12375                         if (( tot_len != 1024 )); then
12376                                 cleanup_130
12377                                 error "FIEMAP on $fm_file failed; returned " \
12378                                 "len $tot_len for OST $last_lun instead of 1024"
12379                                 return
12380                         else
12381                                 (( num_luns += 1 ))
12382                                 tot_len=0
12383                         fi
12384                 fi
12385                 (( tot_len += ext_len ))
12386                 last_lun=$frag_lun
12387         done
12388         if (( num_luns != actual_stripe_count || tot_len != 1024 )); then
12389                 cleanup_130
12390                 error "FIEMAP on $fm_file failed; returned wrong number of " \
12391                         "luns or wrong len for OST $last_lun"
12392                 return
12393         fi
12394
12395         cleanup_130
12396
12397         echo "FIEMAP on N-stripe file succeeded"
12398 }
12399 run_test 130d "FIEMAP (N-stripe file)"
12400
12401 test_130e() {
12402         [[ $OSTCOUNT -lt 2 ]] && skip_env "needs >= 2 OSTs"
12403
12404         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12405         [ -n "$filefrag_op" ] && skip_env "filefrag does not support FIEMAP"
12406
12407         trap cleanup_130 EXIT RETURN
12408
12409         local fm_file=$DIR/$tfile
12410         $LFS setstripe -S 131072 -c 2 $fm_file || error "setstripe on $fm_file"
12411         [ "$(facet_fstype ost$(($($LFS getstripe -i $fm_file) + 1)))" = "zfs" ] &&
12412                 skip_env "ORI-366/LU-1941: FIEMAP unimplemented on ZFS"
12413
12414         NUM_BLKS=512
12415         EXPECTED_LEN=$(( (NUM_BLKS / 2) * 64 ))
12416         for ((i = 0; i < $NUM_BLKS; i++))
12417         do
12418                 dd if=/dev/zero of=$fm_file count=1 bs=64k seek=$((2*$i)) conv=notrunc > /dev/null 2>&1
12419         done
12420
12421         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12422         filefrag_op=$(filefrag -ve -k $fm_file |
12423                         sed -n '/ext:/,/found/{/ext:/d; /found/d; p}')
12424
12425         last_lun=$(echo $filefrag_op | cut -d: -f5 |
12426                 sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12427
12428         IFS=$'\n'
12429         tot_len=0
12430         num_luns=1
12431         for line in $filefrag_op
12432         do
12433                 frag_lun=$(echo $line | cut -d: -f5 |
12434                         sed -e 's/^[ \t]*/0x/' | sed -e 's/0x0x/0x/')
12435                 ext_len=$(echo $line | cut -d: -f4)
12436                 if (( $frag_lun != $last_lun )); then
12437                         if (( tot_len != $EXPECTED_LEN )); then
12438                                 cleanup_130
12439                                 error "FIEMAP on $fm_file failed; returned " \
12440                                 "len $tot_len for OST $last_lun instead " \
12441                                 "of $EXPECTED_LEN"
12442                                 return
12443                         else
12444                                 (( num_luns += 1 ))
12445                                 tot_len=0
12446                         fi
12447                 fi
12448                 (( tot_len += ext_len ))
12449                 last_lun=$frag_lun
12450         done
12451         if (( num_luns != 2 || tot_len != $EXPECTED_LEN )); then
12452                 cleanup_130
12453                 error "FIEMAP on $fm_file failed; returned wrong number " \
12454                         "of luns or wrong len for OST $last_lun"
12455                 return
12456         fi
12457
12458         cleanup_130
12459
12460         echo "FIEMAP with continuation calls succeeded"
12461 }
12462 run_test 130e "FIEMAP (test continuation FIEMAP calls)"
12463
12464 test_130f() {
12465         filefrag_op=$(filefrag -e 2>&1 | grep "invalid option")
12466         [ -n "$filefrag_op" ] && skip "filefrag does not support FIEMAP"
12467
12468         local fm_file=$DIR/$tfile
12469         $MULTIOP $fm_file oO_RDWR:O_CREAT:O_LOV_DELAY_CREATE:T33554432c ||
12470                 error "multiop create with lov_delay_create on $fm_file"
12471
12472         filefrag -ves $fm_file || error "filefrag $fm_file failed"
12473         filefrag_extents=$(filefrag -vek $fm_file |
12474                            awk '/extents? found/ { print $2 }')
12475         if [[ "$filefrag_extents" != "0" ]]; then
12476                 error "FIEMAP on $fm_file failed; " \
12477                       "returned $filefrag_extents expected 0"
12478         fi
12479
12480         rm -f $fm_file
12481 }
12482 run_test 130f "FIEMAP (unstriped file)"
12483
12484 # Test for writev/readv
12485 test_131a() {
12486         rwv -f $DIR/$tfile -w -n 3 524288 1048576 1572864 ||
12487                 error "writev test failed"
12488         rwv -f $DIR/$tfile -r -v -n 2 1572864 1048576 ||
12489                 error "readv failed"
12490         rm -f $DIR/$tfile
12491 }
12492 run_test 131a "test iov's crossing stripe boundary for writev/readv"
12493
12494 test_131b() {
12495         local fsize=$((524288 + 1048576 + 1572864))
12496         rwv -f $DIR/$tfile -w -a -n 3 524288 1048576 1572864 &&
12497                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12498                         error "append writev test failed"
12499
12500         ((fsize += 1572864 + 1048576))
12501         rwv -f $DIR/$tfile -w -a -n 2 1572864 1048576 &&
12502                 $CHECKSTAT -t file $DIR/$tfile -s $fsize ||
12503                         error "append writev test failed"
12504         rm -f $DIR/$tfile
12505 }
12506 run_test 131b "test append writev"
12507
12508 test_131c() {
12509         rwv -f $DIR/$tfile -w -d -n 1 1048576 || return 0
12510         error "NOT PASS"
12511 }
12512 run_test 131c "test read/write on file w/o objects"
12513
12514 test_131d() {
12515         rwv -f $DIR/$tfile -w -n 1 1572864
12516         NOB=`rwv -f $DIR/$tfile -r -n 3 524288 524288 1048576 | awk '/error/ {print $6}'`
12517         if [ "$NOB" != 1572864 ]; then
12518                 error "Short read filed: read $NOB bytes instead of 1572864"
12519         fi
12520         rm -f $DIR/$tfile
12521 }
12522 run_test 131d "test short read"
12523
12524 test_131e() {
12525         rwv -f $DIR/$tfile -w -s 1048576 -n 1 1048576
12526         rwv -f $DIR/$tfile -r -z -s 0 -n 1 524288 || \
12527         error "read hitting hole failed"
12528         rm -f $DIR/$tfile
12529 }
12530 run_test 131e "test read hitting hole"
12531
12532 check_stats() {
12533         local facet=$1
12534         local op=$2
12535         local want=${3:-0}
12536         local res
12537
12538         case $facet in
12539         mds*) res=$(do_facet $facet \
12540                    $LCTL get_param mdt.$FSNAME-MDT0000.md_stats | grep "$op")
12541                  ;;
12542         ost*) res=$(do_facet $facet \
12543                    $LCTL get_param obdfilter.$FSNAME-OST0000.stats | grep "$op")
12544                  ;;
12545         *) error "Wrong facet '$facet'" ;;
12546         esac
12547         [ "$res" ] || error "The counter for $op on $facet was not incremented"
12548         # if the argument $3 is zero, it means any stat increment is ok.
12549         if [[ $want -gt 0 ]]; then
12550                 local count=$(echo $res | awk '{ print $2 }')
12551                 [[ $count -ne $want ]] &&
12552                         error "The $op counter on $facet is $count, not $want"
12553         fi
12554 }
12555
12556 test_133a() {
12557         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12558         remote_ost_nodsh && skip "remote OST with nodsh"
12559         remote_mds_nodsh && skip "remote MDS with nodsh"
12560         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12561                 skip_env "MDS doesn't support rename stats"
12562
12563         local testdir=$DIR/${tdir}/stats_testdir
12564
12565         mkdir -p $DIR/${tdir}
12566
12567         # clear stats.
12568         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12569         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12570
12571         # verify mdt stats first.
12572         mkdir ${testdir} || error "mkdir failed"
12573         check_stats $SINGLEMDS "mkdir" 1
12574         touch ${testdir}/${tfile} || error "touch failed"
12575         check_stats $SINGLEMDS "open" 1
12576         check_stats $SINGLEMDS "close" 1
12577         [ $MDS1_VERSION -ge $(version_code 2.8.54) ] && {
12578                 mknod ${testdir}/${tfile}-pipe p || error "mknod failed"
12579                 check_stats $SINGLEMDS "mknod" 2
12580         }
12581         rm -f ${testdir}/${tfile}-pipe || error "pipe remove failed"
12582         check_stats $SINGLEMDS "unlink" 1
12583         rm -f ${testdir}/${tfile} || error "file remove failed"
12584         check_stats $SINGLEMDS "unlink" 2
12585
12586         # remove working dir and check mdt stats again.
12587         rmdir ${testdir} || error "rmdir failed"
12588         check_stats $SINGLEMDS "rmdir" 1
12589
12590         local testdir1=$DIR/${tdir}/stats_testdir1
12591         mkdir -p ${testdir}
12592         mkdir -p ${testdir1}
12593         touch ${testdir1}/test1
12594         mv ${testdir1}/test1 ${testdir} || error "file crossdir rename"
12595         check_stats $SINGLEMDS "crossdir_rename" 1
12596
12597         mv ${testdir}/test1 ${testdir}/test0 || error "file samedir rename"
12598         check_stats $SINGLEMDS "samedir_rename" 1
12599
12600         rm -rf $DIR/${tdir}
12601 }
12602 run_test 133a "Verifying MDT stats ========================================"
12603
12604 test_133b() {
12605         local res
12606
12607         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12608         remote_ost_nodsh && skip "remote OST with nodsh"
12609         remote_mds_nodsh && skip "remote MDS with nodsh"
12610
12611         local testdir=$DIR/${tdir}/stats_testdir
12612
12613         mkdir -p ${testdir} || error "mkdir failed"
12614         touch ${testdir}/${tfile} || error "touch failed"
12615         cancel_lru_locks mdc
12616
12617         # clear stats.
12618         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12619         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12620
12621         # extra mdt stats verification.
12622         chmod 444 ${testdir}/${tfile} || error "chmod failed"
12623         check_stats $SINGLEMDS "setattr" 1
12624         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12625         if [ $MDS1_VERSION -ne $(version_code 2.2.0) ]
12626         then            # LU-1740
12627                 ls -l ${testdir}/${tfile} > /dev/null|| error "ls failed"
12628                 check_stats $SINGLEMDS "getattr" 1
12629         fi
12630         rm -rf $DIR/${tdir}
12631
12632         # when DNE is enabled, MDT uses STATFS RPC to ping other targets
12633         # so the check below is not reliable
12634         [ $MDSCOUNT -eq 1 ] || return 0
12635
12636         # Sleep to avoid a cached response.
12637         #define OBD_STATFS_CACHE_SECONDS 1
12638         sleep 2
12639         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12640         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12641         $LFS df || error "lfs failed"
12642         check_stats $SINGLEMDS "statfs" 1
12643
12644         # check aggregated statfs (LU-10018)
12645         [ $MDS1_VERSION -lt $(version_code 2.11.54) ] &&
12646                 return 0
12647         [ $CLIENT_VERSION -lt $(version_code 2.11.54) ] &&
12648                 return 0
12649         sleep 2
12650         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12651         do_facet ost1 $LCTL set_param obdfilter.*.exports.*.stats=clear
12652         df $DIR
12653         check_stats $SINGLEMDS "statfs" 1
12654
12655         # We want to check that the client didn't send OST_STATFS to
12656         # ost1 but the MDT also uses OST_STATFS for precreate. So some
12657         # extra care is needed here.
12658         if remote_mds; then
12659                 local nid=$($LCTL list_nids | head -1 | sed  "s/\./\\\./g")
12660                 local param="obdfilter.$FSNAME-OST0000.exports.'$nid'.stats"
12661
12662                 res=$(do_facet ost1 $LCTL get_param $param | grep statfs)
12663                 [ "$res" ] && error "OST got STATFS"
12664         fi
12665
12666         return 0
12667 }
12668 run_test 133b "Verifying extra MDT stats =================================="
12669
12670 test_133c() {
12671         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12672         remote_ost_nodsh && skip "remote OST with nodsh"
12673         remote_mds_nodsh && skip "remote MDS with nodsh"
12674
12675         local testdir=$DIR/$tdir/stats_testdir
12676
12677         test_mkdir -p $testdir
12678
12679         # verify obdfilter stats.
12680         $LFS setstripe -c 1 -i 0 $testdir/$tfile
12681         sync
12682         cancel_lru_locks osc
12683         wait_delete_completed
12684
12685         # clear stats.
12686         do_facet $SINGLEMDS $LCTL set_param mdt.*.md_stats=clear
12687         do_facet ost1 $LCTL set_param obdfilter.*.stats=clear
12688
12689         dd if=/dev/zero of=$testdir/$tfile conv=notrunc bs=512k count=1 ||
12690                 error "dd failed"
12691         sync
12692         cancel_lru_locks osc
12693         check_stats ost1 "write" 1
12694
12695         dd if=$testdir/$tfile of=/dev/null bs=1k count=1 || error "dd failed"
12696         check_stats ost1 "read" 1
12697
12698         > $testdir/$tfile || error "truncate failed"
12699         check_stats ost1 "punch" 1
12700
12701         rm -f $testdir/$tfile || error "file remove failed"
12702         wait_delete_completed
12703         check_stats ost1 "destroy" 1
12704
12705         rm -rf $DIR/$tdir
12706 }
12707 run_test 133c "Verifying OST stats ========================================"
12708
12709 order_2() {
12710         local value=$1
12711         local orig=$value
12712         local order=1
12713
12714         while [ $value -ge 2 ]; do
12715                 order=$((order*2))
12716                 value=$((value/2))
12717         done
12718
12719         if [ $orig -gt $order ]; then
12720                 order=$((order*2))
12721         fi
12722         echo $order
12723 }
12724
12725 size_in_KMGT() {
12726     local value=$1
12727     local size=('K' 'M' 'G' 'T');
12728     local i=0
12729     local size_string=$value
12730
12731     while [ $value -ge 1024 ]; do
12732         if [ $i -gt 3 ]; then
12733             #T is the biggest unit we get here, if that is bigger,
12734             #just return XXXT
12735             size_string=${value}T
12736             break
12737         fi
12738         value=$((value >> 10))
12739         if [ $value -lt 1024 ]; then
12740             size_string=${value}${size[$i]}
12741             break
12742         fi
12743         i=$((i + 1))
12744     done
12745
12746     echo $size_string
12747 }
12748
12749 get_rename_size() {
12750         local size=$1
12751         local context=${2:-.}
12752         local sample=$(do_facet $SINGLEMDS $LCTL \
12753                 get_param mdt.$FSNAME-MDT0000.rename_stats |
12754                 grep -A1 $context |
12755                 awk '/ '${size}'/ {print $4}' | sed -e "s/,//g")
12756         echo $sample
12757 }
12758
12759 test_133d() {
12760         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12761         remote_ost_nodsh && skip "remote OST with nodsh"
12762         remote_mds_nodsh && skip "remote MDS with nodsh"
12763         do_facet $SINGLEMDS $LCTL list_param mdt.*.rename_stats ||
12764                 skip_env "MDS doesn't support rename stats"
12765
12766         local testdir1=$DIR/${tdir}/stats_testdir1
12767         local testdir2=$DIR/${tdir}/stats_testdir2
12768         mkdir -p $DIR/${tdir}
12769
12770         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12771
12772         lfs mkdir -i 0 -c 1 ${testdir1} || error "mkdir failed"
12773         lfs mkdir -i 0 -c 1 ${testdir2} || error "mkdir failed"
12774
12775         createmany -o $testdir1/test 512 || error "createmany failed"
12776
12777         # check samedir rename size
12778         mv ${testdir1}/test0 ${testdir1}/test_0
12779
12780         local testdir1_size=$(ls -l $DIR/${tdir} |
12781                 awk '/stats_testdir1/ {print $5}')
12782         local testdir2_size=$(ls -l $DIR/${tdir} |
12783                 awk '/stats_testdir2/ {print $5}')
12784
12785         testdir1_size=$(order_2 $testdir1_size)
12786         testdir2_size=$(order_2 $testdir2_size)
12787
12788         testdir1_size=$(size_in_KMGT $testdir1_size)
12789         testdir2_size=$(size_in_KMGT $testdir2_size)
12790
12791         echo "source rename dir size: ${testdir1_size}"
12792         echo "target rename dir size: ${testdir2_size}"
12793
12794         local cmd="do_facet $SINGLEMDS $LCTL "
12795         cmd+="get_param mdt.$FSNAME-MDT0000.rename_stats"
12796
12797         eval $cmd || error "$cmd failed"
12798         local samedir=$($cmd | grep 'same_dir')
12799         local same_sample=$(get_rename_size $testdir1_size)
12800         [ -z "$samedir" ] && error "samedir_rename_size count error"
12801         [[ $same_sample -eq 1 ]] ||
12802                 error "samedir_rename_size error $same_sample"
12803         echo "Check same dir rename stats success"
12804
12805         do_facet $SINGLEMDS $LCTL set_param mdt.*.rename_stats=clear
12806
12807         # check crossdir rename size
12808         mv ${testdir1}/test_0 ${testdir2}/test_0
12809
12810         testdir1_size=$(ls -l $DIR/${tdir} |
12811                 awk '/stats_testdir1/ {print $5}')
12812         testdir2_size=$(ls -l $DIR/${tdir} |
12813                 awk '/stats_testdir2/ {print $5}')
12814
12815         testdir1_size=$(order_2 $testdir1_size)
12816         testdir2_size=$(order_2 $testdir2_size)
12817
12818         testdir1_size=$(size_in_KMGT $testdir1_size)
12819         testdir2_size=$(size_in_KMGT $testdir2_size)
12820
12821         echo "source rename dir size: ${testdir1_size}"
12822         echo "target rename dir size: ${testdir2_size}"
12823
12824         eval $cmd || error "$cmd failed"
12825         local crossdir=$($cmd | grep 'crossdir')
12826         local src_sample=$(get_rename_size $testdir1_size crossdir_src)
12827         local tgt_sample=$(get_rename_size $testdir2_size crossdir_tgt)
12828         [ -z "$crossdir" ] && error "crossdir_rename_size count error"
12829         [[ $src_sample -eq 1 ]] ||
12830                 error "crossdir_rename_size error $src_sample"
12831         [[ $tgt_sample -eq 1 ]] ||
12832                 error "crossdir_rename_size error $tgt_sample"
12833         echo "Check cross dir rename stats success"
12834         rm -rf $DIR/${tdir}
12835 }
12836 run_test 133d "Verifying rename_stats ========================================"
12837
12838 test_133e() {
12839         remote_mds_nodsh && skip "remote MDS with nodsh"
12840         remote_ost_nodsh && skip "remote OST with nodsh"
12841         [ $PARALLEL == "yes" ] && skip "skip parallel run"
12842
12843         local testdir=$DIR/${tdir}/stats_testdir
12844         local ctr f0 f1 bs=32768 count=42 sum
12845
12846         mkdir -p ${testdir} || error "mkdir failed"
12847
12848         $LFS setstripe -c 1 -i 0 ${testdir}/${tfile}
12849
12850         for ctr in {write,read}_bytes; do
12851                 sync
12852                 cancel_lru_locks osc
12853
12854                 do_facet ost1 $LCTL set_param -n \
12855                         "obdfilter.*.exports.clear=clear"
12856
12857                 if [ $ctr = write_bytes ]; then
12858                         f0=/dev/zero
12859                         f1=${testdir}/${tfile}
12860                 else
12861                         f0=${testdir}/${tfile}
12862                         f1=/dev/null
12863                 fi
12864
12865                 dd if=$f0 of=$f1 conv=notrunc bs=$bs count=$count || \
12866                         error "dd failed"
12867                 sync
12868                 cancel_lru_locks osc
12869
12870                 sum=$(do_facet ost1 $LCTL get_param \
12871                         "obdfilter.*.exports.*.stats" |
12872                         awk -v ctr=$ctr 'BEGIN { sum = 0 }
12873                                 $1 == ctr { sum += $7 }
12874                                 END { printf("%0.0f", sum) }')
12875
12876                 if ((sum != bs * count)); then
12877                         error "Bad $ctr sum, expected $((bs * count)), got $sum"
12878                 fi
12879         done
12880
12881         rm -rf $DIR/${tdir}
12882 }
12883 run_test 133e "Verifying OST {read,write}_bytes nid stats ================="
12884
12885 proc_regexp="/{proc,sys}/{fs,sys,kernel/debug}/{lustre,lnet}/"
12886
12887 # Some versions of find (4.5.11, 4.5.14) included in CentOS 7.3-7.5 do
12888 # not honor the -ignore_readdir_race option correctly. So we call
12889 # error_ignore() rather than error() in these cases. See LU-11152.
12890 error_133() {
12891         if (find --version; do_facet mds1 find --version) |
12892                 grep -q '\b4\.5\.1[1-4]\b'; then
12893                 error_ignore LU-11152 "$@"
12894         else
12895                 error "$@"
12896         fi
12897 }
12898
12899 test_133f() {
12900         # First without trusting modes.
12901         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
12902         echo "proc_dirs='$proc_dirs'"
12903         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
12904         find $proc_dirs -exec cat '{}' \; &> /dev/null
12905
12906         # Second verifying readability.
12907         $LCTL get_param -R '*' &> /dev/null
12908
12909         # Verifing writability with badarea_io.
12910         find $proc_dirs \
12911                 -ignore_readdir_race \
12912                 -type f \
12913                 -not -name force_lbug \
12914                 -not -name changelog_mask \
12915                 -exec badarea_io '{}' \; ||
12916                         error_133 "find $proc_dirs failed"
12917 }
12918 run_test 133f "Check reads/writes of client lustre proc files with bad area io"
12919
12920 test_133g() {
12921         remote_mds_nodsh && skip "remote MDS with nodsh"
12922         remote_ost_nodsh && skip "remote OST with nodsh"
12923
12924         local facet
12925         for facet in mds1 ost1; do
12926                 local facet_ver=$(lustre_version_code $facet)
12927                 if [ $facet_ver -ge $(version_code 2.7.65) ]; then
12928                         do_facet $facet "$LCTL get_param -R '*'" &> /dev/null
12929                 else
12930                         log "$facet: too old lustre for get_param -R"
12931                 fi
12932                 if [ $facet_ver -ge $(version_code 2.5.54) ]; then
12933                         do_facet $facet "$LCTL list_param -R '*' | grep '=' |
12934                                 tr -d = | egrep -v 'force_lbug|changelog_mask' |
12935                                 xargs badarea_io" ||
12936                                         error_133 "$facet badarea_io failed"
12937                 else
12938                         skip_noexit "$facet: too old lustre for get_param -R"
12939                 fi
12940         done
12941
12942         # remount the FS in case writes/reads /proc break the FS
12943         cleanup || error "failed to unmount"
12944         setup || error "failed to setup"
12945         true
12946 }
12947 run_test 133g "Check reads/writes of server lustre proc files with bad area io"
12948
12949 test_133h() {
12950         remote_mds_nodsh && skip "remote MDS with nodsh"
12951         remote_ost_nodsh && skip "remote OST with nodsh"
12952         [[ $MDS1_VERSION -lt $(version_code 2.9.54) ]] &&
12953                 skip "Need MDS version at least 2.9.54"
12954
12955         local facet
12956
12957         for facet in client mds1 ost1; do
12958                 local facet_proc_dirs=$(do_facet $facet \
12959                                         \\\ls -d $proc_regexp 2> /dev/null)
12960                 [ -z "$facet_proc_dirs" ] && error "no proc_dirs on $facet"
12961                 echo "${facet}_proc_dirs='$facet_proc_dirs'"
12962                 # Get the list of files that are missing the terminating newline
12963                 local missing=($(do_facet $facet \
12964                         find ${facet_proc_dirs} -type f \|              \
12965                                 while read F\; do                       \
12966                                         awk -v FS='\v' -v RS='\v\v'     \
12967                                         "'END { if(NR>0 &&              \
12968                                         \\\$NF !~ /.*\\\n\$/)           \
12969                                                 print FILENAME}'"       \
12970                                         '\$F'\;                         \
12971                                 done 2>/dev/null))
12972                 [ ${#missing[*]} -eq 0 ] ||
12973                         error "files do not end with newline: ${missing[*]}"
12974         done
12975 }
12976 run_test 133h "Proc files should end with newlines"
12977
12978 test_134a() {
12979         remote_mds_nodsh && skip "remote MDS with nodsh"
12980         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
12981                 skip "Need MDS version at least 2.7.54"
12982
12983         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
12984         cancel_lru_locks mdc
12985
12986         local nsdir="ldlm.namespaces.*-MDT0000-mdc-*"
12987         local unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12988         [ $unused -eq 0 ] || error "$unused locks are not cleared"
12989
12990         local nr=1000
12991         createmany -o $DIR/$tdir/f $nr ||
12992                 error "failed to create $nr files in $DIR/$tdir"
12993         unused=$($LCTL get_param -n $nsdir.lock_unused_count)
12994
12995         #define OBD_FAIL_LDLM_WATERMARK_LOW     0x327
12996         do_facet mds1 $LCTL set_param fail_loc=0x327
12997         do_facet mds1 $LCTL set_param fail_val=500
12998         touch $DIR/$tdir/m
12999
13000         echo "sleep 10 seconds ..."
13001         sleep 10
13002         local lck_cnt=$($LCTL get_param -n $nsdir.lock_unused_count)
13003
13004         do_facet mds1 $LCTL set_param fail_loc=0
13005         do_facet mds1 $LCTL set_param fail_val=0
13006         [ $lck_cnt -lt $unused ] ||
13007                 error "No locks reclaimed, before:$unused, after:$lck_cnt"
13008
13009         rm $DIR/$tdir/m
13010         unlinkmany $DIR/$tdir/f $nr
13011 }
13012 run_test 134a "Server reclaims locks when reaching lock_reclaim_threshold"
13013
13014 test_134b() {
13015         remote_mds_nodsh && skip "remote MDS with nodsh"
13016         [[ $MDS1_VERSION -lt $(version_code 2.7.54) ]] &&
13017                 skip "Need MDS version at least 2.7.54"
13018
13019         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13020         cancel_lru_locks mdc
13021
13022         local low_wm=$(do_facet mds1 $LCTL get_param -n \
13023                         ldlm.lock_reclaim_threshold_mb)
13024         # disable reclaim temporarily
13025         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=0
13026
13027         #define OBD_FAIL_LDLM_WATERMARK_HIGH     0x328
13028         do_facet mds1 $LCTL set_param fail_loc=0x328
13029         do_facet mds1 $LCTL set_param fail_val=500
13030
13031         $LCTL set_param debug=+trace
13032
13033         local nr=600
13034         createmany -o $DIR/$tdir/f $nr &
13035         local create_pid=$!
13036
13037         echo "Sleep $TIMEOUT seconds ..."
13038         sleep $TIMEOUT
13039         if ! ps -p $create_pid  > /dev/null 2>&1; then
13040                 do_facet mds1 $LCTL set_param fail_loc=0
13041                 do_facet mds1 $LCTL set_param fail_val=0
13042                 do_facet mds1 $LCTL set_param \
13043                         ldlm.lock_reclaim_threshold_mb=${low_wm}m
13044                 error "createmany finished incorrectly!"
13045         fi
13046         do_facet mds1 $LCTL set_param fail_loc=0
13047         do_facet mds1 $LCTL set_param fail_val=0
13048         do_facet mds1 $LCTL set_param ldlm.lock_reclaim_threshold_mb=${low_wm}m
13049         wait $create_pid || return 1
13050
13051         unlinkmany $DIR/$tdir/f $nr
13052 }
13053 run_test 134b "Server rejects lock request when reaching lock_limit_mb"
13054
13055 test_135() {
13056         remote_mds_nodsh && skip "remote MDS with nodsh"
13057         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
13058                 skip "Need MDS version at least 2.13.50"
13059         local fname
13060
13061         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13062
13063 #define OBD_FAIL_PLAIN_RECORDS 0x1319
13064         #set only one record at plain llog
13065         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x1319 fail_val=1
13066
13067         #fill already existed plain llog each 64767
13068         #wrapping whole catalog
13069         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
13070
13071         createmany -o $DIR/$tdir/$tfile_ 64700
13072         for (( i = 0; i < 64700; i = i + 2 ))
13073         do
13074                 rm $DIR/$tdir/$tfile_$i &
13075                 rm $DIR/$tdir/$tfile_$((i + 1)) &
13076                 local pid=$!
13077                 wait $pid
13078         done
13079
13080         #waiting osp synchronization
13081         wait_delete_completed
13082 }
13083 run_test 135 "Race catalog processing"
13084
13085 test_136() {
13086         remote_mds_nodsh && skip "remote MDS with nodsh"
13087         [[ $MDS1_VERSION -lt $(version_code 2.13.50) ]] &&
13088                 skip "Need MDS version at least 2.13.50"
13089         local fname
13090
13091         mkdir -p $DIR/$tdir || error "failed to create $DIR/$tdir"
13092         $LFS setstripe -c 1 -i 0 $DIR/$tdir || error "failed to set striping"
13093         #set only one record at plain llog
13094 #define OBD_FAIL_CATALOG_FULL_CHECK                0x131a
13095         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x131a fail_val=1
13096
13097         #fill already existed 2 plain llogs each 64767
13098         #wrapping whole catalog
13099         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 1))
13100         createmany -o -u $DIR/$tdir/$tfile- $((64767 * 3 / 2))
13101         wait_delete_completed
13102
13103         createmany -o $DIR/$tdir/$tfile_ 10
13104         sleep 25
13105
13106         do_facet $SINGLEMDS $LCTL set_param fail_val=3
13107         for (( i = 0; i < 10; i = i + 3 ))
13108         do
13109                 rm $DIR/$tdir/$tfile_$i &
13110                 rm $DIR/$tdir/$tfile_$((i + 1)) &
13111                 local pid=$!
13112                 wait $pid
13113                 sleep 7
13114                 rm $DIR/$tdir/$tfile_$((i + 2)) &
13115         done
13116
13117         #waiting osp synchronization
13118         wait_delete_completed
13119 }
13120 run_test 136 "Race catalog processing 2"
13121
13122 test_140() { #bug-17379
13123         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13124
13125         test_mkdir $DIR/$tdir
13126         cd $DIR/$tdir || error "Changing to $DIR/$tdir"
13127         cp $(which stat) . || error "Copying stat to $DIR/$tdir"
13128
13129         # VFS limits max symlink depth to 5(4KSTACK) or 7(8KSTACK) or 8
13130         # For kernel > 3.5, bellow only tests consecutive symlink (MAX 40)
13131         local i=0
13132         while i=$((i + 1)); do
13133                 test_mkdir $i
13134                 cd $i || error "Changing to $i"
13135                 ln -s ../stat stat || error "Creating stat symlink"
13136                 # Read the symlink until ELOOP present,
13137                 # not LBUGing the system is considered success,
13138                 # we didn't overrun the stack.
13139                 $OPENFILE -f O_RDONLY stat >/dev/null 2>&1; ret=$?
13140                 if [ $ret -ne 0 ]; then
13141                         if [ $ret -eq 40 ]; then
13142                                 break  # -ELOOP
13143                         else
13144                                 error "Open stat symlink"
13145                                         return
13146                         fi
13147                 fi
13148         done
13149         i=$((i - 1))
13150         echo "The symlink depth = $i"
13151         [ $i -eq 5 ] || [ $i -eq 7 ] || [ $i -eq 8 ] || [ $i -eq 40 ] ||
13152                 error "Invalid symlink depth"
13153
13154         # Test recursive symlink
13155         ln -s symlink_self symlink_self
13156         $OPENFILE -f O_RDONLY symlink_self >/dev/null 2>&1; ret=$?
13157         echo "open symlink_self returns $ret"
13158         [ $ret -eq 40 ] || error "recursive symlink doesn't return -ELOOP"
13159 }
13160 run_test 140 "Check reasonable stack depth (shouldn't LBUG) ===="
13161
13162 test_150() {
13163         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13164
13165         local TF="$TMP/$tfile"
13166
13167         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13168         cp $TF $DIR/$tfile
13169         cancel_lru_locks $OSC
13170         cmp $TF $DIR/$tfile || error "$TMP/$tfile $DIR/$tfile differ"
13171         remount_client $MOUNT
13172         df -P $MOUNT
13173         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (remount)"
13174
13175         $TRUNCATE $TF 6000
13176         $TRUNCATE $DIR/$tfile 6000
13177         cancel_lru_locks $OSC
13178         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (truncate1)"
13179
13180         echo "12345" >>$TF
13181         echo "12345" >>$DIR/$tfile
13182         cancel_lru_locks $OSC
13183         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append1)"
13184
13185         echo "12345" >>$TF
13186         echo "12345" >>$DIR/$tfile
13187         cancel_lru_locks $OSC
13188         cmp $TF $DIR/$tfile || error "$TF $DIR/$tfile differ (append2)"
13189
13190         rm -f $TF
13191         true
13192 }
13193 run_test 150 "truncate/append tests"
13194
13195 #LU-2902 roc_hit was not able to read all values from lproc
13196 function roc_hit_init() {
13197         local list=$(comma_list $(osts_nodes))
13198         local dir=$DIR/$tdir-check
13199         local file=$dir/$tfile
13200         local BEFORE
13201         local AFTER
13202         local idx
13203
13204         test_mkdir $dir
13205         #use setstripe to do a write to every ost
13206         for i in $(seq 0 $((OSTCOUNT-1))); do
13207                 $LFS setstripe -c 1 -i $i $dir || error "$LFS setstripe $file failed"
13208                 dd if=/dev/urandom of=$file bs=4k count=4 2>&1 > /dev/null
13209                 idx=$(printf %04x $i)
13210                 BEFORE=$(get_osd_param $list *OST*$idx stats |
13211                         awk '$1 == "cache_access" {sum += $7}
13212                                 END { printf("%0.0f", sum) }')
13213
13214                 cancel_lru_locks osc
13215                 cat $file >/dev/null
13216
13217                 AFTER=$(get_osd_param $list *OST*$idx stats |
13218                         awk '$1 == "cache_access" {sum += $7}
13219                                 END { printf("%0.0f", sum) }')
13220
13221                 echo BEFORE:$BEFORE AFTER:$AFTER
13222                 if ! let "AFTER - BEFORE == 4"; then
13223                         rm -rf $dir
13224                         error "roc_hit is not safe to use"
13225                 fi
13226                 rm $file
13227         done
13228
13229         rm -rf $dir
13230 }
13231
13232 function roc_hit() {
13233         local list=$(comma_list $(osts_nodes))
13234         echo $(get_osd_param $list '' stats |
13235                 awk '$1 == "cache_hit" {sum += $7}
13236                         END { printf("%0.0f", sum) }')
13237 }
13238
13239 function set_cache() {
13240         local on=1
13241
13242         if [ "$2" == "off" ]; then
13243                 on=0;
13244         fi
13245         local list=$(comma_list $(osts_nodes))
13246         set_osd_param $list '' $1_cache_enable $on
13247
13248         cancel_lru_locks osc
13249 }
13250
13251 test_151() {
13252         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13253         remote_ost_nodsh && skip "remote OST with nodsh"
13254
13255         local CPAGES=3
13256         local list=$(comma_list $(osts_nodes))
13257
13258         # check whether obdfilter is cache capable at all
13259         if ! get_osd_param $list '' read_cache_enable >/dev/null; then
13260                 skip "not cache-capable obdfilter"
13261         fi
13262
13263         # check cache is enabled on all obdfilters
13264         if get_osd_param $list '' read_cache_enable | grep 0; then
13265                 skip "oss cache is disabled"
13266         fi
13267
13268         set_osd_param $list '' writethrough_cache_enable 1
13269
13270         # check write cache is enabled on all obdfilters
13271         if get_osd_param $list '' writethrough_cache_enable | grep 0; then
13272                 skip "oss write cache is NOT enabled"
13273         fi
13274
13275         roc_hit_init
13276
13277         #define OBD_FAIL_OBD_NO_LRU  0x609
13278         do_nodes $list $LCTL set_param fail_loc=0x609
13279
13280         # pages should be in the case right after write
13281         dd if=/dev/urandom of=$DIR/$tfile bs=4k count=$CPAGES ||
13282                 error "dd failed"
13283
13284         local BEFORE=$(roc_hit)
13285         cancel_lru_locks osc
13286         cat $DIR/$tfile >/dev/null
13287         local AFTER=$(roc_hit)
13288
13289         do_nodes $list $LCTL set_param fail_loc=0
13290
13291         if ! let "AFTER - BEFORE == CPAGES"; then
13292                 error "NOT IN CACHE: before: $BEFORE, after: $AFTER"
13293         fi
13294
13295         cancel_lru_locks osc
13296         # invalidates OST cache
13297         do_nodes $list "echo 1 > /proc/sys/vm/drop_caches"
13298         set_osd_param $list '' read_cache_enable 0
13299         cat $DIR/$tfile >/dev/null
13300
13301         # now data shouldn't be found in the cache
13302         BEFORE=$(roc_hit)
13303         cancel_lru_locks osc
13304         cat $DIR/$tfile >/dev/null
13305         AFTER=$(roc_hit)
13306         if let "AFTER - BEFORE != 0"; then
13307                 error "IN CACHE: before: $BEFORE, after: $AFTER"
13308         fi
13309
13310         set_osd_param $list '' read_cache_enable 1
13311         rm -f $DIR/$tfile
13312 }
13313 run_test 151 "test cache on oss and controls ==============================="
13314
13315 test_152() {
13316         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13317
13318         local TF="$TMP/$tfile"
13319
13320         # simulate ENOMEM during write
13321 #define OBD_FAIL_OST_NOMEM      0x226
13322         lctl set_param fail_loc=0x80000226
13323         dd if=/dev/urandom of=$TF bs=6096 count=1 || error "dd failed"
13324         cp $TF $DIR/$tfile
13325         sync || error "sync failed"
13326         lctl set_param fail_loc=0
13327
13328         # discard client's cache
13329         cancel_lru_locks osc
13330
13331         # simulate ENOMEM during read
13332         lctl set_param fail_loc=0x80000226
13333         cmp $TF $DIR/$tfile || error "cmp failed"
13334         lctl set_param fail_loc=0
13335
13336         rm -f $TF
13337 }
13338 run_test 152 "test read/write with enomem ============================"
13339
13340 test_153() {
13341         $MULTIOP $DIR/$tfile Ow4096Ycu || error "multiop failed"
13342 }
13343 run_test 153 "test if fdatasync does not crash ======================="
13344
13345 dot_lustre_fid_permission_check() {
13346         local fid=$1
13347         local ffid=$MOUNT/.lustre/fid/$fid
13348         local test_dir=$2
13349
13350         echo "stat fid $fid"
13351         stat $ffid > /dev/null || error "stat $ffid failed."
13352         echo "touch fid $fid"
13353         touch $ffid || error "touch $ffid failed."
13354         echo "write to fid $fid"
13355         cat /etc/hosts > $ffid || error "write $ffid failed."
13356         echo "read fid $fid"
13357         diff /etc/hosts $ffid || error "read $ffid failed."
13358         echo "append write to fid $fid"
13359         cat /etc/hosts >> $ffid || error "append write $ffid failed."
13360         echo "rename fid $fid"
13361         mv $ffid $test_dir/$tfile.1 &&
13362                 error "rename $ffid to $tfile.1 should fail."
13363         touch $test_dir/$tfile.1
13364         mv $test_dir/$tfile.1 $ffid &&
13365                 error "rename $tfile.1 to $ffid should fail."
13366         rm -f $test_dir/$tfile.1
13367         echo "truncate fid $fid"
13368         $TRUNCATE $ffid 777 || error "truncate $ffid failed."
13369         echo "link fid $fid"
13370         ln -f $ffid $test_dir/tfile.lnk || error "link $ffid failed."
13371         if [[ $($LCTL get_param -n mdc.*-mdc-*.connect_flags) =~ acl ]]; then
13372                 echo "setfacl fid $fid"
13373                 setfacl -R -m u:bin:rwx $ffid || error "setfacl $ffid failed."
13374                 echo "getfacl fid $fid"
13375                 getfacl $ffid >/dev/null || error "getfacl $ffid failed."
13376         fi
13377         echo "unlink fid $fid"
13378         unlink $MOUNT/.lustre/fid/$fid && error "unlink $ffid should fail."
13379         echo "mknod fid $fid"
13380         mknod $ffid c 1 3 && error "mknod $ffid should fail."
13381
13382         fid=[0xf00000400:0x1:0x0]
13383         ffid=$MOUNT/.lustre/fid/$fid
13384
13385         echo "stat non-exist fid $fid"
13386         stat $ffid > /dev/null && error "stat non-exist $ffid should fail."
13387         echo "write to non-exist fid $fid"
13388         cat /etc/hosts > $ffid && error "write non-exist $ffid should fail."
13389         echo "link new fid $fid"
13390         ln $test_dir/$tfile $ffid && error "link $ffid should fail."
13391
13392         mkdir -p $test_dir/$tdir
13393         touch $test_dir/$tdir/$tfile
13394         fid=$($LFS path2fid $test_dir/$tdir)
13395         rc=$?
13396         [ $rc -ne 0 ] &&
13397                 error "error: could not get fid for $test_dir/$dir/$tfile."
13398
13399         ffid=$MOUNT/.lustre/fid/$fid
13400
13401         echo "ls $fid"
13402         ls $ffid > /dev/null || error "ls $ffid failed."
13403         echo "touch $fid/$tfile.1"
13404         touch $ffid/$tfile.1 || error "touch $ffid/$tfile.1 failed."
13405
13406         echo "touch $MOUNT/.lustre/fid/$tfile"
13407         touch $MOUNT/.lustre/fid/$tfile && \
13408                 error "touch $MOUNT/.lustre/fid/$tfile should fail."
13409
13410         echo "setxattr to $MOUNT/.lustre/fid"
13411         setfattr -n trusted.name1 -v value1 $MOUNT/.lustre/fid
13412
13413         echo "listxattr for $MOUNT/.lustre/fid"
13414         getfattr -d -m "^trusted" $MOUNT/.lustre/fid
13415
13416         echo "delxattr from $MOUNT/.lustre/fid"
13417         setfattr -x trusted.name1 $MOUNT/.lustre/fid
13418
13419         echo "touch invalid fid: $MOUNT/.lustre/fid/[0x200000400:0x2:0x3]"
13420         touch $MOUNT/.lustre/fid/[0x200000400:0x2:0x3] &&
13421                 error "touch invalid fid should fail."
13422
13423         echo "touch non-normal fid: $MOUNT/.lustre/fid/[0x1:0x2:0x0]"
13424         touch $MOUNT/.lustre/fid/[0x1:0x2:0x0] &&
13425                 error "touch non-normal fid should fail."
13426
13427         echo "rename $tdir to $MOUNT/.lustre/fid"
13428         mrename $test_dir/$tdir $MOUNT/.lustre/fid &&
13429                 error "rename to $MOUNT/.lustre/fid should fail."
13430
13431         if [ $MDS1_VERSION -ge $(version_code 2.3.51) ]
13432         then            # LU-3547
13433                 local old_obf_mode=$(stat --format="%a" $DIR/.lustre/fid)
13434                 local new_obf_mode=777
13435
13436                 echo "change mode of $DIR/.lustre/fid to $new_obf_mode"
13437                 chmod $new_obf_mode $DIR/.lustre/fid ||
13438                         error "chmod $new_obf_mode $DIR/.lustre/fid failed"
13439
13440                 local obf_mode=$(stat --format=%a $DIR/.lustre/fid)
13441                 [ $obf_mode -eq $new_obf_mode ] ||
13442                         error "stat $DIR/.lustre/fid returned wrong mode $obf_mode"
13443
13444                 echo "restore mode of $DIR/.lustre/fid to $old_obf_mode"
13445                 chmod $old_obf_mode $DIR/.lustre/fid ||
13446                         error "chmod $old_obf_mode $DIR/.lustre/fid failed"
13447         fi
13448
13449         $OPENFILE -f O_LOV_DELAY_CREATE:O_CREAT $test_dir/$tfile-2
13450         fid=$($LFS path2fid $test_dir/$tfile-2)
13451
13452         if [ $MDS1_VERSION -ge $(version_code 2.6.50) ]
13453         then # LU-5424
13454                 echo "cp /etc/passwd $MOUNT/.lustre/fid/$fid"
13455                 cp /etc/passwd $MOUNT/.lustre/fid/$fid ||
13456                         error "create lov data thru .lustre failed"
13457         fi
13458         echo "cp /etc/passwd $test_dir/$tfile-2"
13459         cp /etc/passwd $test_dir/$tfile-2 ||
13460                 error "copy to $test_dir/$tfile-2 failed."
13461         echo "diff /etc/passwd $MOUNT/.lustre/fid/$fid"
13462         diff /etc/passwd $MOUNT/.lustre/fid/$fid ||
13463                 error "diff /etc/passwd $MOUNT/.lustre/fid/$fid failed."
13464
13465         rm -rf $test_dir/tfile.lnk
13466         rm -rf $test_dir/$tfile-2
13467 }
13468
13469 test_154A() {
13470         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13471                 skip "Need MDS version at least 2.4.1"
13472
13473         local tf=$DIR/$tfile
13474         touch $tf
13475
13476         local fid=$($LFS path2fid $tf)
13477         [ -z "$fid" ] && error "path2fid unable to get $tf FID"
13478
13479         # check that we get the same pathname back
13480         local found=$($LFS fid2path $MOUNT "$fid")
13481         [ -z "$found" ] && error "fid2path unable to get '$fid' path"
13482         [ "$found" == "$tf" ] ||
13483                 error "fid2path($fid=path2fid($tf)) = $found != $tf"
13484 }
13485 run_test 154A "lfs path2fid and fid2path basic checks"
13486
13487 test_154B() {
13488         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13489                 skip "Need MDS version at least 2.4.1"
13490
13491         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
13492         touch $DIR/$tdir/$tfile || error "touch $DIR/$tdir/$tfile failed"
13493         local linkea=$($LL_DECODE_LINKEA $DIR/$tdir/$tfile | grep 'pfid')
13494         [ -z "$linkea" ] && error "decode linkea $DIR/$tdir/$tfile failed"
13495
13496         local name=$(echo $linkea | awk '/pfid/ {print $5}' | sed -e "s/'//g")
13497         local PFID=$(echo $linkea | awk '/pfid/ {print $3}' | sed -e "s/,//g")
13498
13499         # check that we get the same pathname
13500         echo "PFID: $PFID, name: $name"
13501         local FOUND=$($LFS fid2path $MOUNT "$PFID")
13502         [ -z "$FOUND" ] && error "fid2path unable to get $PFID path"
13503         [ "$FOUND/$name" != "$DIR/$tdir/$tfile" ] &&
13504                 error "ll_decode_linkea has $FOUND/$name != $DIR/$tdir/$tfile"
13505
13506         rm -rf $DIR/$tdir || error "Can not delete directory $DIR/$tdir"
13507 }
13508 run_test 154B "verify the ll_decode_linkea tool"
13509
13510 test_154a() {
13511         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13512         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13513         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13514                 skip "Need MDS version at least 2.2.51"
13515         [ -z "$(which setfacl)" ] && skip_env "must have setfacl tool"
13516
13517         cp /etc/hosts $DIR/$tfile
13518
13519         fid=$($LFS path2fid $DIR/$tfile)
13520         rc=$?
13521         [ $rc -ne 0 ] && error "error: could not get fid for $DIR/$tfile."
13522
13523         dot_lustre_fid_permission_check "$fid" $DIR ||
13524                 error "dot lustre permission check $fid failed"
13525
13526         ls -a $MOUNT | grep "\.lustre" && error ".lustre should not be listed"
13527
13528         rm -rf $MOUNT/.lustre && error ".lustre is not allowed to be unlinked"
13529
13530         touch $MOUNT/.lustre/file &&
13531                 error "creation is not allowed under .lustre"
13532
13533         mkdir $MOUNT/.lustre/dir &&
13534                 error "mkdir is not allowed under .lustre"
13535
13536         rm -rf $DIR/$tfile
13537 }
13538 run_test 154a "Open-by-FID"
13539
13540 test_154b() {
13541         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13542         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13543         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
13544         [[ $MDS1_VERSION -ge $(version_code 2.2.51) ]] ||
13545                 skip "Need MDS version at least 2.2.51"
13546
13547         local remote_dir=$DIR/$tdir/remote_dir
13548         local MDTIDX=1
13549         local rc=0
13550
13551         mkdir -p $DIR/$tdir
13552         $LFS mkdir -i $MDTIDX $remote_dir ||
13553                 error "create remote directory failed"
13554
13555         cp /etc/hosts $remote_dir/$tfile
13556
13557         fid=$($LFS path2fid $remote_dir/$tfile)
13558         rc=$?
13559         [ $rc -ne 0 ] && error "error: could not get fid for $remote_dir/$tfile"
13560
13561         dot_lustre_fid_permission_check "$fid" $remote_dir ||
13562                 error "dot lustre permission check $fid failed"
13563         rm -rf $DIR/$tdir
13564 }
13565 run_test 154b "Open-by-FID for remote directory"
13566
13567 test_154c() {
13568         [[ $MDS1_VERSION -lt $(version_code 2.4.1) ]] &&
13569                 skip "Need MDS version at least 2.4.1"
13570
13571         touch $DIR/$tfile.1 $DIR/$tfile.2 $DIR/$tfile.3
13572         local FID1=$($LFS path2fid $DIR/$tfile.1)
13573         local FID2=$($LFS path2fid $DIR/$tfile.2)
13574         local FID3=$($LFS path2fid $DIR/$tfile.3)
13575
13576         local N=1
13577         $LFS path2fid $DIR/$tfile.[123] | while read PATHNAME FID; do
13578                 [ "$PATHNAME" = "$DIR/$tfile.$N:" ] ||
13579                         error "path2fid pathname $PATHNAME != $DIR/$tfile.$N:"
13580                 local want=FID$N
13581                 [ "$FID" = "${!want}" ] ||
13582                         error "path2fid $PATHNAME FID $FID != FID$N ${!want}"
13583                 N=$((N + 1))
13584         done
13585
13586         $LFS fid2path $MOUNT "$FID1" "$FID2" "$FID3" | while read PATHNAME;
13587         do
13588                 [ "$PATHNAME" = "$DIR/$tfile.$N" ] ||
13589                         error "fid2path pathname $PATHNAME != $DIR/$tfile.$N:"
13590                 N=$((N + 1))
13591         done
13592 }
13593 run_test 154c "lfs path2fid and fid2path multiple arguments"
13594
13595 test_154d() {
13596         remote_mds_nodsh && skip "remote MDS with nodsh"
13597         [[ $MDS1_VERSION -lt $(version_code 2.5.53) ]] &&
13598                 skip "Need MDS version at least 2.5.53"
13599
13600         if remote_mds; then
13601                 nid=$($LCTL list_nids | sed  "s/\./\\\./g")
13602         else
13603                 nid="0@lo"
13604         fi
13605         local proc_ofile="mdt.*.exports.'$nid'.open_files"
13606         local fd
13607         local cmd
13608
13609         rm -f $DIR/$tfile
13610         touch $DIR/$tfile
13611
13612         local fid=$($LFS path2fid $DIR/$tfile)
13613         # Open the file
13614         fd=$(free_fd)
13615         cmd="exec $fd<$DIR/$tfile"
13616         eval $cmd
13617         local fid_list=$(do_facet $SINGLEMDS $LCTL get_param $proc_ofile)
13618         echo "$fid_list" | grep "$fid"
13619         rc=$?
13620
13621         cmd="exec $fd>/dev/null"
13622         eval $cmd
13623         if [ $rc -ne 0 ]; then
13624                 error "FID $fid not found in open files list $fid_list"
13625         fi
13626 }
13627 run_test 154d "Verify open file fid"
13628
13629 test_154e()
13630 {
13631         [[ $MDS1_VERSION -lt $(version_code 2.6.50) ]] &&
13632                 skip "Need MDS version at least 2.6.50"
13633
13634         if ls -a $MOUNT | grep -q '^\.lustre$'; then
13635                 error ".lustre returned by readdir"
13636         fi
13637 }
13638 run_test 154e ".lustre is not returned by readdir"
13639
13640 test_154f() {
13641         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13642
13643         # create parent directory on a single MDT to avoid cross-MDT hardlinks
13644         test_mkdir -p -c1 $DIR/$tdir/d
13645         # test dirs inherit from its stripe
13646         mkdir -p $DIR/$tdir/d/foo1 || error "mkdir error"
13647         mkdir -p $DIR/$tdir/d/foo2 || error "mkdir error"
13648         cp /etc/hosts $DIR/$tdir/d/foo1/$tfile
13649         ln $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/link
13650         touch $DIR/f
13651
13652         # get fid of parents
13653         local FID0=$($LFS path2fid $DIR/$tdir/d)
13654         local FID1=$($LFS path2fid $DIR/$tdir/d/foo1)
13655         local FID2=$($LFS path2fid $DIR/$tdir/d/foo2)
13656         local FID3=$($LFS path2fid $DIR)
13657
13658         # check that path2fid --parents returns expected <parent_fid>/name
13659         # 1) test for a directory (single parent)
13660         local parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1)
13661         [ "$parent" == "$FID0/foo1" ] ||
13662                 error "expected parent: $FID0/foo1, got: $parent"
13663
13664         # 2) test for a file with nlink > 1 (multiple parents)
13665         parent=$($LFS path2fid --parents $DIR/$tdir/d/foo1/$tfile)
13666         echo "$parent" | grep -F "$FID1/$tfile" ||
13667                 error "$FID1/$tfile not returned in parent list"
13668         echo "$parent" | grep -F "$FID2/link" ||
13669                 error "$FID2/link not returned in parent list"
13670
13671         # 3) get parent by fid
13672         local file_fid=$($LFS path2fid $DIR/$tdir/d/foo1/$tfile)
13673         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13674         echo "$parent" | grep -F "$FID1/$tfile" ||
13675                 error "$FID1/$tfile not returned in parent list (by fid)"
13676         echo "$parent" | grep -F "$FID2/link" ||
13677                 error "$FID2/link not returned in parent list (by fid)"
13678
13679         # 4) test for entry in root directory
13680         parent=$($LFS path2fid --parents $DIR/f)
13681         echo "$parent" | grep -F "$FID3/f" ||
13682                 error "$FID3/f not returned in parent list"
13683
13684         # 5) test it on root directory
13685         [ -z "$($LFS path2fid --parents $MOUNT 2>/dev/null)" ] ||
13686                 error "$MOUNT should not have parents"
13687
13688         # enable xattr caching and check that linkea is correctly updated
13689         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
13690         save_lustre_params client "llite.*.xattr_cache" > $save
13691         lctl set_param llite.*.xattr_cache 1
13692
13693         # 6.1) linkea update on rename
13694         mv $DIR/$tdir/d/foo1/$tfile $DIR/$tdir/d/foo2/$tfile.moved
13695
13696         # get parents by fid
13697         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13698         # foo1 should no longer be returned in parent list
13699         echo "$parent" | grep -F "$FID1" &&
13700                 error "$FID1 should no longer be in parent list"
13701         # the new path should appear
13702         echo "$parent" | grep -F "$FID2/$tfile.moved" ||
13703                 error "$FID2/$tfile.moved is not in parent list"
13704
13705         # 6.2) linkea update on unlink
13706         rm -f $DIR/$tdir/d/foo2/link
13707         parent=$($LFS path2fid --parents $MOUNT/.lustre/fid/$file_fid)
13708         # foo2/link should no longer be returned in parent list
13709         echo "$parent" | grep -F "$FID2/link" &&
13710                 error "$FID2/link should no longer be in parent list"
13711         true
13712
13713         rm -f $DIR/f
13714         restore_lustre_params < $save
13715         rm -f $save
13716 }
13717 run_test 154f "get parent fids by reading link ea"
13718
13719 test_154g()
13720 {
13721         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
13722         [[ $MDS1_VERSION -ge $(version_code 2.6.92) &&
13723            $CLIENT_VERSION -gt $(version_code 2.6.99) ]] ||
13724                 skip "Need MDS version at least 2.6.92"
13725
13726         mkdir -p $DIR/$tdir
13727         llapi_fid_test -d $DIR/$tdir
13728 }
13729 run_test 154g "various llapi FID tests"
13730
13731 test_155_small_load() {
13732     local temp=$TMP/$tfile
13733     local file=$DIR/$tfile
13734
13735     dd if=/dev/urandom of=$temp bs=6096 count=1 || \
13736         error "dd of=$temp bs=6096 count=1 failed"
13737     cp $temp $file
13738     cancel_lru_locks $OSC
13739     cmp $temp $file || error "$temp $file differ"
13740
13741     $TRUNCATE $temp 6000
13742     $TRUNCATE $file 6000
13743     cmp $temp $file || error "$temp $file differ (truncate1)"
13744
13745     echo "12345" >>$temp
13746     echo "12345" >>$file
13747     cmp $temp $file || error "$temp $file differ (append1)"
13748
13749     echo "12345" >>$temp
13750     echo "12345" >>$file
13751     cmp $temp $file || error "$temp $file differ (append2)"
13752
13753     rm -f $temp $file
13754     true
13755 }
13756
13757 test_155_big_load() {
13758         remote_ost_nodsh && skip "remote OST with nodsh"
13759
13760         local temp=$TMP/$tfile
13761         local file=$DIR/$tfile
13762
13763         free_min_max
13764         local cache_size=$(do_facet ost$((MAXI+1)) \
13765                 "awk '/cache/ {sum+=\\\$4} END {print sum}' /proc/cpuinfo")
13766         local large_file_size=$((cache_size * 2))
13767
13768         echo "OSS cache size: $cache_size KB"
13769         echo "Large file size: $large_file_size KB"
13770
13771         [ $MAXV -le $large_file_size ] &&
13772                 skip_env "max available OST size needs > $large_file_size KB"
13773
13774         $LFS setstripe $file -c 1 -i $MAXI || error "$LFS setstripe $file failed"
13775
13776         dd if=/dev/urandom of=$temp bs=$large_file_size count=1k ||
13777                 error "dd of=$temp bs=$large_file_size count=1k failed"
13778         cp $temp $file
13779         ls -lh $temp $file
13780         cancel_lru_locks osc
13781         cmp $temp $file || error "$temp $file differ"
13782
13783         rm -f $temp $file
13784         true
13785 }
13786
13787 save_writethrough() {
13788         local facets=$(get_facets OST)
13789
13790         save_lustre_params $facets "osd-*.*.writethrough_cache_enable" > $1
13791 }
13792
13793 test_155a() {
13794         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13795
13796         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13797
13798         save_writethrough $p
13799
13800         set_cache read on
13801         set_cache writethrough on
13802         test_155_small_load
13803         restore_lustre_params < $p
13804         rm -f $p
13805 }
13806 run_test 155a "Verify small file correctness: read cache:on write_cache:on"
13807
13808 test_155b() {
13809         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13810
13811         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13812
13813         save_writethrough $p
13814
13815         set_cache read on
13816         set_cache writethrough off
13817         test_155_small_load
13818         restore_lustre_params < $p
13819         rm -f $p
13820 }
13821 run_test 155b "Verify small file correctness: read cache:on write_cache:off"
13822
13823 test_155c() {
13824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13825
13826         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13827
13828         save_writethrough $p
13829
13830         set_cache read off
13831         set_cache writethrough on
13832         test_155_small_load
13833         restore_lustre_params < $p
13834         rm -f $p
13835 }
13836 run_test 155c "Verify small file correctness: read cache:off write_cache:on"
13837
13838 test_155d() {
13839         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13840
13841         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13842
13843         save_writethrough $p
13844
13845         set_cache read off
13846         set_cache writethrough off
13847         test_155_small_load
13848         restore_lustre_params < $p
13849         rm -f $p
13850 }
13851 run_test 155d "Verify small file correctness: read cache:off write_cache:off"
13852
13853 test_155e() {
13854         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13855
13856         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13857
13858         save_writethrough $p
13859
13860         set_cache read on
13861         set_cache writethrough on
13862         test_155_big_load
13863         restore_lustre_params < $p
13864         rm -f $p
13865 }
13866 run_test 155e "Verify big file correctness: read cache:on write_cache:on"
13867
13868 test_155f() {
13869         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13870
13871         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13872
13873         save_writethrough $p
13874
13875         set_cache read on
13876         set_cache writethrough off
13877         test_155_big_load
13878         restore_lustre_params < $p
13879         rm -f $p
13880 }
13881 run_test 155f "Verify big file correctness: read cache:on write_cache:off"
13882
13883 test_155g() {
13884         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13885
13886         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13887
13888         save_writethrough $p
13889
13890         set_cache read off
13891         set_cache writethrough on
13892         test_155_big_load
13893         restore_lustre_params < $p
13894         rm -f $p
13895 }
13896 run_test 155g "Verify big file correctness: read cache:off write_cache:on"
13897
13898 test_155h() {
13899         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13900
13901         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13902
13903         save_writethrough $p
13904
13905         set_cache read off
13906         set_cache writethrough off
13907         test_155_big_load
13908         restore_lustre_params < $p
13909         rm -f $p
13910 }
13911 run_test 155h "Verify big file correctness: read cache:off write_cache:off"
13912
13913 test_156() {
13914         [ $PARALLEL == "yes" ] && skip "skip parallel run"
13915         remote_ost_nodsh && skip "remote OST with nodsh"
13916         [ $OST1_VERSION -lt $(version_code 2.6.93) ] &&
13917                 skip "stats not implemented on old servers"
13918         [ "$ost1_FSTYPE" = "zfs" ] &&
13919                 skip "LU-1956/LU-2261: stats not implemented on OSD ZFS"
13920
13921         local CPAGES=3
13922         local BEFORE
13923         local AFTER
13924         local file="$DIR/$tfile"
13925         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
13926
13927         save_writethrough $p
13928         roc_hit_init
13929
13930         log "Turn on read and write cache"
13931         set_cache read on
13932         set_cache writethrough on
13933
13934         log "Write data and read it back."
13935         log "Read should be satisfied from the cache."
13936         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13937         BEFORE=$(roc_hit)
13938         cancel_lru_locks osc
13939         cat $file >/dev/null
13940         AFTER=$(roc_hit)
13941         if ! let "AFTER - BEFORE == CPAGES"; then
13942                 error "NOT IN CACHE (2): before: $BEFORE, after: $AFTER"
13943         else
13944                 log "cache hits: before: $BEFORE, after: $AFTER"
13945         fi
13946
13947         log "Read again; it should be satisfied from the cache."
13948         BEFORE=$AFTER
13949         cancel_lru_locks osc
13950         cat $file >/dev/null
13951         AFTER=$(roc_hit)
13952         if ! let "AFTER - BEFORE == CPAGES"; then
13953                 error "NOT IN CACHE (3): before: $BEFORE, after: $AFTER"
13954         else
13955                 log "cache hits:: before: $BEFORE, after: $AFTER"
13956         fi
13957
13958         log "Turn off the read cache and turn on the write cache"
13959         set_cache read off
13960         set_cache writethrough on
13961
13962         log "Read again; it should be satisfied from the cache."
13963         BEFORE=$(roc_hit)
13964         cancel_lru_locks osc
13965         cat $file >/dev/null
13966         AFTER=$(roc_hit)
13967         if ! let "AFTER - BEFORE == CPAGES"; then
13968                 error "NOT IN CACHE (4): before: $BEFORE, after: $AFTER"
13969         else
13970                 log "cache hits:: before: $BEFORE, after: $AFTER"
13971         fi
13972
13973         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
13974                 # > 2.12.56 uses pagecache if cached
13975                 log "Read again; it should not be satisfied from the cache."
13976                 BEFORE=$AFTER
13977                 cancel_lru_locks osc
13978                 cat $file >/dev/null
13979                 AFTER=$(roc_hit)
13980                 if ! let "AFTER - BEFORE == 0"; then
13981                         error "IN CACHE (5): before: $BEFORE, after: $AFTER"
13982                 else
13983                         log "cache hits:: before: $BEFORE, after: $AFTER"
13984                 fi
13985         fi
13986
13987         log "Write data and read it back."
13988         log "Read should be satisfied from the cache."
13989         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
13990         BEFORE=$(roc_hit)
13991         cancel_lru_locks osc
13992         cat $file >/dev/null
13993         AFTER=$(roc_hit)
13994         if ! let "AFTER - BEFORE == CPAGES"; then
13995                 error "NOT IN CACHE (6): before: $BEFORE, after: $AFTER"
13996         else
13997                 log "cache hits:: before: $BEFORE, after: $AFTER"
13998         fi
13999
14000         if [ $OST1_VERSION -lt $(version_code 2.12.55) ]; then
14001                 # > 2.12.56 uses pagecache if cached
14002                 log "Read again; it should not be satisfied from the cache."
14003                 BEFORE=$AFTER
14004                 cancel_lru_locks osc
14005                 cat $file >/dev/null
14006                 AFTER=$(roc_hit)
14007                 if ! let "AFTER - BEFORE == 0"; then
14008                         error "IN CACHE (7): before: $BEFORE, after: $AFTER"
14009                 else
14010                         log "cache hits:: before: $BEFORE, after: $AFTER"
14011                 fi
14012         fi
14013
14014         log "Turn off read and write cache"
14015         set_cache read off
14016         set_cache writethrough off
14017
14018         log "Write data and read it back"
14019         log "It should not be satisfied from the cache."
14020         rm -f $file
14021         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14022         cancel_lru_locks osc
14023         BEFORE=$(roc_hit)
14024         cat $file >/dev/null
14025         AFTER=$(roc_hit)
14026         if ! let "AFTER - BEFORE == 0"; then
14027                 error_ignore bz20762 "IN CACHE (8):before:$BEFORE,after:$AFTER"
14028         else
14029                 log "cache hits:: before: $BEFORE, after: $AFTER"
14030         fi
14031
14032         log "Turn on the read cache and turn off the write cache"
14033         set_cache read on
14034         set_cache writethrough off
14035
14036         log "Write data and read it back"
14037         log "It should not be satisfied from the cache."
14038         rm -f $file
14039         dd if=/dev/urandom of=$file bs=4k count=$CPAGES || error "dd failed"
14040         BEFORE=$(roc_hit)
14041         cancel_lru_locks osc
14042         cat $file >/dev/null
14043         AFTER=$(roc_hit)
14044         if ! let "AFTER - BEFORE == 0"; then
14045                 error_ignore bz20762 "IN CACHE (9):before:$BEFORE,after:$AFTER"
14046         else
14047                 log "cache hits:: before: $BEFORE, after: $AFTER"
14048         fi
14049
14050         log "Read again; it should be satisfied from the cache."
14051         BEFORE=$(roc_hit)
14052         cancel_lru_locks osc
14053         cat $file >/dev/null
14054         AFTER=$(roc_hit)
14055         if ! let "AFTER - BEFORE == CPAGES"; then
14056                 error "NOT IN CACHE (1): before: $BEFORE, after: $AFTER"
14057         else
14058                 log "cache hits:: before: $BEFORE, after: $AFTER"
14059         fi
14060
14061         restore_lustre_params < $p
14062         rm -f $p $file
14063 }
14064 run_test 156 "Verification of tunables"
14065
14066 test_160a() {
14067         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14068         remote_mds_nodsh && skip "remote MDS with nodsh"
14069         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
14070                 skip "Need MDS version at least 2.2.0"
14071
14072         changelog_register || error "changelog_register failed"
14073         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14074         changelog_users $SINGLEMDS | grep -q $cl_user ||
14075                 error "User $cl_user not found in changelog_users"
14076
14077         # change something
14078         test_mkdir -p $DIR/$tdir/pics/2008/zachy
14079         changelog_clear 0 || error "changelog_clear failed"
14080         touch $DIR/$tdir/pics/2008/zachy/$tfile                 # open 1
14081         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg       # open 2
14082         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
14083         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
14084         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
14085         rm $DIR/$tdir/pics/desktop.jpg
14086
14087         changelog_dump | tail -10
14088
14089         echo "verifying changelog mask"
14090         changelog_chmask "-MKDIR"
14091         changelog_chmask "-CLOSE"
14092
14093         test_mkdir -p $DIR/$tdir/pics/zach/sofia                # not logged
14094         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # not logged
14095
14096         changelog_chmask "+MKDIR"
14097         changelog_chmask "+CLOSE"
14098
14099         test_mkdir -p $DIR/$tdir/pics/2008/sofia                # mkdir 1
14100         echo "zzzzzz" > $DIR/$tdir/pics/zach/file               # open 3
14101
14102         changelog_dump | tail -10
14103         MKDIRS=$(changelog_dump | grep -c "MKDIR")
14104         CLOSES=$(changelog_dump | grep -c "CLOSE")
14105         [ $MKDIRS -eq 1 ] || error "MKDIR changelog mask count $MKDIRS != 1"
14106         [ $CLOSES -eq 3 ] || error "CLOSE changelog mask count $CLOSES != 3"
14107
14108         # verify contents
14109         echo "verifying target fid"
14110         local fidc=$(changelog_extract_field "CREAT" "$tfile" "t=")
14111         local fidf=$($LFS path2fid $DIR/$tdir/pics/zach/$tfile)
14112         [ "$fidc" == "$fidf" ] ||
14113                 error "changelog '$tfile' fid $fidc != file fid $fidf"
14114         echo "verifying parent fid"
14115         # The FID returned from the Changelog may be the directory shard on
14116         # a different MDT, and not the FID returned by path2fid on the parent.
14117         # Instead of comparing FIDs, verify that fid2path(fidp) is correct,
14118         # since this is what will matter when recreating this file in the tree.
14119         local fidp=$(changelog_extract_field "CREAT" "$tfile" "p=")
14120         local pathp=$($LFS fid2path $MOUNT "$fidp")
14121         [ "${pathp%/}" == "$DIR/$tdir/pics/zach" ] ||
14122                 error "changelog fid2path($fidc) $pathp != $DIR/$tdir/pics/zach"
14123
14124         echo "getting records for $cl_user"
14125         changelog_users $SINGLEMDS
14126         local user_rec1=$(changelog_user_rec $SINGLEMDS $cl_user)
14127         local nclr=3
14128         __changelog_clear $SINGLEMDS $cl_user +$nclr ||
14129                 error "changelog_clear failed"
14130         local user_rec2=$(changelog_user_rec $SINGLEMDS $cl_user)
14131         echo "verifying user clear: $user_rec1 + $nclr == $user_rec2"
14132         [ $user_rec2 == $((user_rec1 + nclr)) ] ||
14133                 error "user index expect $user_rec1 + $nclr != $user_rec2"
14134
14135         local min0_rec=$(changelog_users $SINGLEMDS |
14136                 awk 'min == "" || $2 < min { min = $2 }; END { print min }')
14137         local first_rec=$($LFS changelog $(facet_svc $SINGLEMDS) |
14138                           awk '{ print $1; exit; }')
14139
14140         changelog_dump | tail -n 5
14141         echo "verifying user min purge: $min0_rec + 1 == $first_rec"
14142         [ $first_rec == $((min0_rec + 1)) ] ||
14143                 error "first index should be $min0_rec + 1 not $first_rec"
14144
14145         # LU-3446 changelog index reset on MDT restart
14146         local cur_rec1=$(changelog_users $SINGLEMDS |
14147                          awk '/^current.index:/ { print $NF }')
14148         changelog_clear 0 ||
14149                 error "clear all changelog records for $cl_user failed"
14150         stop $SINGLEMDS || error "Fail to stop $SINGLEMDS"
14151         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
14152                 error "Fail to start $SINGLEMDS"
14153         local cur_rec2=$(changelog_users $SINGLEMDS |
14154                          awk '/^current.index:/ { print $NF }')
14155         echo "verifying index survives MDT restart: $cur_rec1 == $cur_rec2"
14156         [ $cur_rec1 == $cur_rec2 ] ||
14157                 error "current index should be $cur_rec1 not $cur_rec2"
14158
14159         echo "verifying users from this test are deregistered"
14160         changelog_deregister || error "changelog_deregister failed"
14161         changelog_users $SINGLEMDS | grep -q $cl_user &&
14162                 error "User '$cl_user' still in changelog_users"
14163
14164         # lctl get_param -n mdd.*.changelog_users
14165         # current index: 144
14166         # ID    index (idle seconds)
14167         # cl3   144 (2)
14168         if ! changelog_users $SINGLEMDS | grep "^cl"; then
14169                 # this is the normal case where all users were deregistered
14170                 # make sure no new records are added when no users are present
14171                 local last_rec1=$(changelog_users $SINGLEMDS |
14172                                   awk '/^current.index:/ { print $NF }')
14173                 touch $DIR/$tdir/chloe
14174                 local last_rec2=$(changelog_users $SINGLEMDS |
14175                                   awk '/^current.index:/ { print $NF }')
14176                 echo "verify changelogs are off: $last_rec1 == $last_rec2"
14177                 [ $last_rec1 == $last_rec2 ] || error "changelogs not off"
14178         else
14179                 # any changelog users must be leftovers from a previous test
14180                 changelog_users $SINGLEMDS
14181                 echo "other changelog users; can't verify off"
14182         fi
14183 }
14184 run_test 160a "changelog sanity"
14185
14186 test_160b() { # LU-3587
14187         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14188         remote_mds_nodsh && skip "remote MDS with nodsh"
14189         [ $MDS1_VERSION -ge $(version_code 2.2.0) ] ||
14190                 skip "Need MDS version at least 2.2.0"
14191
14192         changelog_register || error "changelog_register failed"
14193         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14194         changelog_users $SINGLEMDS | grep -q $cl_user ||
14195                 error "User '$cl_user' not found in changelog_users"
14196
14197         local longname1=$(str_repeat a 255)
14198         local longname2=$(str_repeat b 255)
14199
14200         cd $DIR
14201         echo "creating very long named file"
14202         touch $longname1 || error "create of '$longname1' failed"
14203         echo "renaming very long named file"
14204         mv $longname1 $longname2
14205
14206         changelog_dump | grep RENME | tail -n 5
14207         rm -f $longname2
14208 }
14209 run_test 160b "Verify that very long rename doesn't crash in changelog"
14210
14211 test_160c() {
14212         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14213         remote_mds_nodsh && skip "remote MDS with nodsh"
14214
14215         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
14216                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
14217                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
14218                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
14219
14220         local rc=0
14221
14222         # Registration step
14223         changelog_register || error "changelog_register failed"
14224
14225         rm -rf $DIR/$tdir
14226         mkdir -p $DIR/$tdir
14227         $MCREATE $DIR/$tdir/foo_160c
14228         changelog_chmask "-TRUNC"
14229         $TRUNCATE $DIR/$tdir/foo_160c 200
14230         changelog_chmask "+TRUNC"
14231         $TRUNCATE $DIR/$tdir/foo_160c 199
14232         changelog_dump | tail -n 5
14233         local truncs=$(changelog_dump | tail -n 5 | grep -c TRUNC)
14234         [ $truncs -eq 1 ] || error "TRUNC changelog mask count $truncs != 1"
14235 }
14236 run_test 160c "verify that changelog log catch the truncate event"
14237
14238 test_160d() {
14239         remote_mds_nodsh && skip "remote MDS with nodsh"
14240         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
14241         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14242         [[ $MDS1_VERSION -ge $(version_code 2.7.60) ]] ||
14243                 skip "Need MDS version at least 2.7.60"
14244
14245         # Registration step
14246         changelog_register || error "changelog_register failed"
14247
14248         mkdir -p $DIR/$tdir/migrate_dir
14249         changelog_clear 0 || error "changelog_clear failed"
14250
14251         $LFS migrate -m 1 $DIR/$tdir/migrate_dir || error "migrate fails"
14252         changelog_dump | tail -n 5
14253         local migrates=$(changelog_dump | grep -c "MIGRT")
14254         [ $migrates -eq 1 ] || error "MIGRATE changelog count $migrates != 1"
14255 }
14256 run_test 160d "verify that changelog log catch the migrate event"
14257
14258 test_160e() {
14259         remote_mds_nodsh && skip "remote MDS with nodsh"
14260
14261         # Create a user
14262         changelog_register || error "changelog_register failed"
14263
14264         # Delete a future user (expect fail)
14265         local MDT0=$(facet_svc $SINGLEMDS)
14266         do_facet $SINGLEMDS $LCTL --device $MDT0 changelog_deregister "cl77"
14267         local rc=$?
14268
14269         if [ $rc -eq 0 ]; then
14270                 error "Deleted non-existant user cl77"
14271         elif [ $rc -ne 2 ]; then
14272                 error "changelog_deregister failed with $rc, expect 2 (ENOENT)"
14273         fi
14274
14275         # Clear to a bad index (1 billion should be safe)
14276         $LFS changelog_clear $MDT0 "${CL_USERS[$SINGLEMDS]%% *}" 1000000000
14277         rc=$?
14278
14279         if [ $rc -eq 0 ]; then
14280                 error "Successfully cleared to invalid CL index"
14281         elif [ $rc -ne 22 ]; then
14282                 error "changelog_clear failed with $rc, expected 22 (EINVAL)"
14283         fi
14284 }
14285 run_test 160e "changelog negative testing (should return errors)"
14286
14287 test_160f() {
14288         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14289         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14290                 skip "Need MDS version at least 2.10.56"
14291
14292         local mdts=$(comma_list $(mdts_nodes))
14293
14294         # Create a user
14295         changelog_register || error "first changelog_register failed"
14296         changelog_register || error "second changelog_register failed"
14297         local cl_users
14298         declare -A cl_user1
14299         declare -A cl_user2
14300         local user_rec1
14301         local user_rec2
14302         local i
14303
14304         # generate some changelog records to accumulate on each MDT
14305         # use fnv1a because created files should be evenly distributed
14306         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14307                 error "test_mkdir $tdir failed"
14308         log "$(date +%s): creating first files"
14309         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14310                 error "create $DIR/$tdir/$tfile failed"
14311
14312         # check changelogs have been generated
14313         local start=$SECONDS
14314         local idle_time=$((MDSCOUNT * 5 + 5))
14315         local nbcl=$(changelog_dump | wc -l)
14316         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14317
14318         for param in "changelog_max_idle_time=$idle_time" \
14319                      "changelog_gc=1" \
14320                      "changelog_min_gc_interval=2" \
14321                      "changelog_min_free_cat_entries=3"; do
14322                 local MDT0=$(facet_svc $SINGLEMDS)
14323                 local var="${param%=*}"
14324                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14325
14326                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14327                 do_nodes $mdts $LCTL set_param mdd.*.$param
14328         done
14329
14330         # force cl_user2 to be idle (1st part), but also cancel the
14331         # cl_user1 records so that it is not evicted later in the test.
14332         local sleep1=$((idle_time / 2))
14333         echo "$(date +%s): sleep1 $sleep1/${idle_time}s"
14334         sleep $sleep1
14335
14336         # simulate changelog catalog almost full
14337         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14338         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14339
14340         for i in $(seq $MDSCOUNT); do
14341                 cl_users=(${CL_USERS[mds$i]})
14342                 cl_user1[mds$i]="${cl_users[0]}"
14343                 cl_user2[mds$i]="${cl_users[1]}"
14344
14345                 [ -n "${cl_user1[mds$i]}" ] ||
14346                         error "mds$i: no user registered"
14347                 [ -n "${cl_user2[mds$i]}" ] ||
14348                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14349
14350                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14351                 [ -n "$user_rec1" ] ||
14352                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14353                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14354                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14355                 [ -n "$user_rec2" ] ||
14356                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14357                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14358                      "$user_rec1 + 2 == $user_rec2"
14359                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14360                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14361                               "$user_rec1 + 2, but is $user_rec2"
14362                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14363                 [ -n "$user_rec2" ] ||
14364                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14365                 [ $user_rec1 == $user_rec2 ] ||
14366                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14367                               "$user_rec1, but is $user_rec2"
14368         done
14369
14370         # force cl_user2 idle (2nd part) to just exceed changelog_max_idle_time
14371         local sleep2=$((idle_time - (SECONDS - start) + 1))
14372         echo "$(date +%s): sleep2 $sleep2/${idle_time}s"
14373         sleep $sleep2
14374
14375         # Generate one more changelog to trigger GC at fail_loc for cl_user2.
14376         # cl_user1 should be OK because it recently processed records.
14377         echo "$(date +%s): creating $((MDSCOUNT * 2)) files"
14378         createmany -m $DIR/$tdir/${tfile}b $((MDSCOUNT * 2)) ||
14379                 error "create $DIR/$tdir/${tfile}b failed"
14380
14381         # ensure gc thread is done
14382         for i in $(mdts_nodes); do
14383                 wait_update $i \
14384                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14385                         error "$i: GC-thread not done"
14386         done
14387
14388         local first_rec
14389         for i in $(seq $MDSCOUNT); do
14390                 # check cl_user1 still registered
14391                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14392                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14393                 # check cl_user2 unregistered
14394                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14395                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14396
14397                 # check changelogs are present and starting at $user_rec1 + 1
14398                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14399                 [ -n "$user_rec1" ] ||
14400                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14401                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14402                             awk '{ print $1; exit; }')
14403
14404                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14405                 [ $((user_rec1 + 1)) == $first_rec ] ||
14406                         error "mds$i: first index should be $user_rec1 + 1, " \
14407                               "but is $first_rec"
14408         done
14409 }
14410 run_test 160f "changelog garbage collect (timestamped users)"
14411
14412 test_160g() {
14413         remote_mds_nodsh && skip "remote MDS with nodsh"
14414         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14415                 skip "Need MDS version at least 2.10.56"
14416
14417         local mdts=$(comma_list $(mdts_nodes))
14418
14419         #define OBD_FAIL_TIME_IN_CHLOG_USER     0x1314
14420         do_nodes $mdts $LCTL set_param fail_loc=0x1314
14421
14422         # Create a user
14423         changelog_register || error "first changelog_register failed"
14424         changelog_register || error "second changelog_register failed"
14425         local cl_users
14426         declare -A cl_user1
14427         declare -A cl_user2
14428         local user_rec1
14429         local user_rec2
14430         local i
14431
14432         # generate some changelog records to accumulate on each MDT
14433         # use fnv1a because created files should be evenly distributed
14434         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14435                 error "mkdir $tdir failed"
14436         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14437                 error "create $DIR/$tdir/$tfile failed"
14438
14439         # check changelogs have been generated
14440         local nbcl=$(changelog_dump | wc -l)
14441         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14442
14443         # reduce the max_idle_indexes value to make sure we exceed it
14444         max_ndx=$((nbcl / 2 - 1))
14445
14446         for param in "changelog_max_idle_indexes=$max_ndx" \
14447                      "changelog_gc=1" \
14448                      "changelog_min_gc_interval=2" \
14449                      "changelog_min_free_cat_entries=3"; do
14450                 local MDT0=$(facet_svc $SINGLEMDS)
14451                 local var="${param%=*}"
14452                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14453
14454                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14455                 do_nodes $mdts $LCTL set_param mdd.*.$param ||
14456                         error "unable to set mdd.*.$param"
14457         done
14458
14459         # simulate changelog catalog almost full
14460         #define OBD_FAIL_CAT_FREE_RECORDS       0x1313
14461         do_nodes $mdts $LCTL set_param fail_loc=0x1313 fail_val=3
14462
14463         for i in $(seq $MDSCOUNT); do
14464                 cl_users=(${CL_USERS[mds$i]})
14465                 cl_user1[mds$i]="${cl_users[0]}"
14466                 cl_user2[mds$i]="${cl_users[1]}"
14467
14468                 [ -n "${cl_user1[mds$i]}" ] ||
14469                         error "mds$i: no user registered"
14470                 [ -n "${cl_user2[mds$i]}" ] ||
14471                         error "mds$i: only ${cl_user1[mds$i]} is registered"
14472
14473                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14474                 [ -n "$user_rec1" ] ||
14475                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14476                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14477                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14478                 [ -n "$user_rec2" ] ||
14479                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14480                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14481                      "$user_rec1 + 2 == $user_rec2"
14482                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14483                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14484                               "$user_rec1 + 2, but is $user_rec2"
14485                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14486                 [ -n "$user_rec2" ] ||
14487                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14488                 [ $user_rec1 == $user_rec2 ] ||
14489                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14490                               "$user_rec1, but is $user_rec2"
14491         done
14492
14493         # ensure we are past the previous changelog_min_gc_interval set above
14494         sleep 2
14495
14496         # generate one more changelog to trigger fail_loc
14497         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14498                 error "create $DIR/$tdir/${tfile}bis failed"
14499
14500         # ensure gc thread is done
14501         for i in $(mdts_nodes); do
14502                 wait_update $i \
14503                         "ps -e -o comm= | grep chlg_gc_thread" "" 20 ||
14504                         error "$i: GC-thread not done"
14505         done
14506
14507         local first_rec
14508         for i in $(seq $MDSCOUNT); do
14509                 # check cl_user1 still registered
14510                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14511                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14512                 # check cl_user2 unregistered
14513                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14514                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14515
14516                 # check changelogs are present and starting at $user_rec1 + 1
14517                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14518                 [ -n "$user_rec1" ] ||
14519                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14520                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14521                             awk '{ print $1; exit; }')
14522
14523                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14524                 [ $((user_rec1 + 1)) == $first_rec ] ||
14525                         error "mds$i: first index should be $user_rec1 + 1, " \
14526                               "but is $first_rec"
14527         done
14528 }
14529 run_test 160g "changelog garbage collect (old users)"
14530
14531 test_160h() {
14532         remote_mds_nodsh && skip "remote MDS with nodsh" && return
14533         [[ $MDS1_VERSION -ge $(version_code 2.10.56) ]] ||
14534                 skip "Need MDS version at least 2.10.56"
14535
14536         local mdts=$(comma_list $(mdts_nodes))
14537
14538         # Create a user
14539         changelog_register || error "first changelog_register failed"
14540         changelog_register || error "second changelog_register failed"
14541         local cl_users
14542         declare -A cl_user1
14543         declare -A cl_user2
14544         local user_rec1
14545         local user_rec2
14546         local i
14547
14548         # generate some changelog records to accumulate on each MDT
14549         # use fnv1a because created files should be evenly distributed
14550         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14551                 error "test_mkdir $tdir failed"
14552         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14553                 error "create $DIR/$tdir/$tfile failed"
14554
14555         # check changelogs have been generated
14556         local nbcl=$(changelog_dump | wc -l)
14557         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14558
14559         for param in "changelog_max_idle_time=10" \
14560                      "changelog_gc=1" \
14561                      "changelog_min_gc_interval=2"; do
14562                 local MDT0=$(facet_svc $SINGLEMDS)
14563                 local var="${param%=*}"
14564                 local old=$(do_facet mds1 "$LCTL get_param -n mdd.$MDT0.$var")
14565
14566                 stack_trap "do_nodes $mdts $LCTL set_param mdd.*.$var=$old" EXIT
14567                 do_nodes $mdts $LCTL set_param mdd.*.$param
14568         done
14569
14570         # force cl_user2 to be idle (1st part)
14571         sleep 9
14572
14573         for i in $(seq $MDSCOUNT); do
14574                 cl_users=(${CL_USERS[mds$i]})
14575                 cl_user1[mds$i]="${cl_users[0]}"
14576                 cl_user2[mds$i]="${cl_users[1]}"
14577
14578                 [ -n "${cl_user1[mds$i]}" ] ||
14579                         error "mds$i: no user registered"
14580                 [ -n "${cl_user2[mds$i]}" ] ||
14581                         error "mds$i: only ${cl_user2[mds$i]} is registered"
14582
14583                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14584                 [ -n "$user_rec1" ] ||
14585                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14586                 __changelog_clear mds$i ${cl_user1[mds$i]} +2
14587                 user_rec2=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14588                 [ -n "$user_rec2" ] ||
14589                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14590                 echo "mds$i: verifying user ${cl_user1[mds$i]} clear: " \
14591                      "$user_rec1 + 2 == $user_rec2"
14592                 [ $((user_rec1 + 2)) == $user_rec2 ] ||
14593                         error "mds$i: user ${cl_user1[mds$i]} index expected " \
14594                               "$user_rec1 + 2, but is $user_rec2"
14595                 user_rec2=$(changelog_user_rec mds$i ${cl_user2[mds$i]})
14596                 [ -n "$user_rec2" ] ||
14597                         error "mds$i: User ${cl_user2[mds$i]} not registered"
14598                 [ $user_rec1 == $user_rec2 ] ||
14599                         error "mds$i: user ${cl_user2[mds$i]} index expected " \
14600                               "$user_rec1, but is $user_rec2"
14601         done
14602
14603         # force cl_user2 to be idle (2nd part) and to reach
14604         # changelog_max_idle_time
14605         sleep 2
14606
14607         # force each GC-thread start and block then
14608         # one per MDT/MDD, set fail_val accordingly
14609         #define OBD_FAIL_FORCE_GC_THREAD 0x1316
14610         do_nodes $mdts $LCTL set_param fail_loc=0x1316
14611
14612         # generate more changelogs to trigger fail_loc
14613         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14614                 error "create $DIR/$tdir/${tfile}bis failed"
14615
14616         # stop MDT to stop GC-thread, should be done in back-ground as it will
14617         # block waiting for the thread to be released and exit
14618         declare -A stop_pids
14619         for i in $(seq $MDSCOUNT); do
14620                 stop mds$i &
14621                 stop_pids[mds$i]=$!
14622         done
14623
14624         for i in $(mdts_nodes); do
14625                 local facet
14626                 local nb=0
14627                 local facets=$(facets_up_on_host $i)
14628
14629                 for facet in ${facets//,/ }; do
14630                         if [[ $facet == mds* ]]; then
14631                                 nb=$((nb + 1))
14632                         fi
14633                 done
14634                 # ensure each MDS's gc threads are still present and all in "R"
14635                 # state (OBD_FAIL_FORCE_GC_THREAD effect!)
14636                 [[ $(do_node $i pgrep chlg_gc_thread | wc -l) -eq $nb ]] ||
14637                         error "$i: expected $nb GC-thread"
14638                 wait_update $i \
14639                         "ps -C chlg_gc_thread -o state --no-headers | uniq" \
14640                         "R" 20 ||
14641                         error "$i: GC-thread not found in R-state"
14642                 # check umounts of each MDT on MDS have reached kthread_stop()
14643                 [[ $(do_node $i pgrep umount | wc -l) -eq $nb ]] ||
14644                         error "$i: expected $nb umount"
14645                 wait_update $i \
14646                         "ps -C umount -o state --no-headers | uniq" "D" 20 ||
14647                         error "$i: umount not found in D-state"
14648         done
14649
14650         # release all GC-threads
14651         do_nodes $mdts $LCTL set_param fail_loc=0
14652
14653         # wait for MDT stop to complete
14654         for i in $(seq $MDSCOUNT); do
14655                 wait ${stop_pids[mds$i]} || error "mds$i: stop failed"
14656         done
14657
14658         # XXX
14659         # may try to check if any orphan changelog records are present
14660         # via ldiskfs/zfs and llog_reader...
14661
14662         # re-start/mount MDTs
14663         for i in $(seq $MDSCOUNT); do
14664                 start mds$i $(mdsdevname $i) $MDS_MOUNT_OPTS ||
14665                         error "Fail to start mds$i"
14666         done
14667
14668         local first_rec
14669         for i in $(seq $MDSCOUNT); do
14670                 # check cl_user1 still registered
14671                 changelog_users mds$i | grep -q "${cl_user1[mds$i]}" ||
14672                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14673                 # check cl_user2 unregistered
14674                 changelog_users mds$i | grep -q "${cl_user2[mds$i]}" &&
14675                         error "mds$i: User ${cl_user2[mds$i]} still registered"
14676
14677                 # check changelogs are present and starting at $user_rec1 + 1
14678                 user_rec1=$(changelog_user_rec mds$i ${cl_user1[mds$i]})
14679                 [ -n "$user_rec1" ] ||
14680                         error "mds$i: User ${cl_user1[mds$i]} not registered"
14681                 first_rec=$($LFS changelog $(facet_svc mds$i) |
14682                             awk '{ print $1; exit; }')
14683
14684                 echo "mds$i: verifying first index $user_rec1 + 1 == $first_rec"
14685                 [ $((user_rec1 + 1)) == $first_rec ] ||
14686                         error "mds$i: first index should be $user_rec1 + 1, " \
14687                               "but is $first_rec"
14688         done
14689 }
14690 run_test 160h "changelog gc thread stop upon umount, orphan records delete " \
14691               "during mount"
14692
14693 test_160i() {
14694
14695         local mdts=$(comma_list $(mdts_nodes))
14696
14697         changelog_register || error "first changelog_register failed"
14698
14699         # generate some changelog records to accumulate on each MDT
14700         # use fnv1a because created files should be evenly distributed
14701         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14702                 error "mkdir $tdir failed"
14703         createmany -m $DIR/$tdir/$tfile $((MDSCOUNT * 2)) ||
14704                 error "create $DIR/$tdir/$tfile failed"
14705
14706         # check changelogs have been generated
14707         local nbcl=$(changelog_dump | wc -l)
14708         [[ $nbcl -eq 0 ]] && error "no changelogs found"
14709
14710         # simulate race between register and unregister
14711         # XXX as fail_loc is set per-MDS, with DNE configs the race
14712         # simulation will only occur for one MDT per MDS and for the
14713         # others the normal race scenario will take place
14714         #define CFS_FAIL_CHLOG_USER_REG_UNREG_RACE          0x1315
14715         do_nodes $mdts $LCTL set_param fail_loc=0x10001315
14716         do_nodes $mdts $LCTL set_param fail_val=1
14717
14718         # unregister 1st user
14719         changelog_deregister &
14720         local pid1=$!
14721         # wait some time for deregister work to reach race rdv
14722         sleep 2
14723         # register 2nd user
14724         changelog_register || error "2nd user register failed"
14725
14726         wait $pid1 || error "1st user deregister failed"
14727
14728         local i
14729         local last_rec
14730         declare -A LAST_REC
14731         for i in $(seq $MDSCOUNT); do
14732                 if changelog_users mds$i | grep "^cl"; then
14733                         # make sure new records are added with one user present
14734                         LAST_REC[mds$i]=$(changelog_users $SINGLEMDS |
14735                                           awk '/^current.index:/ { print $NF }')
14736                 else
14737                         error "mds$i has no user registered"
14738                 fi
14739         done
14740
14741         # generate more changelog records to accumulate on each MDT
14742         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14743                 error "create $DIR/$tdir/${tfile}bis failed"
14744
14745         for i in $(seq $MDSCOUNT); do
14746                 last_rec=$(changelog_users $SINGLEMDS |
14747                            awk '/^current.index:/ { print $NF }')
14748                 echo "verify changelogs are on: $last_rec != ${LAST_REC[mds$i]}"
14749                 [ $last_rec != ${LAST_REC[mds$i]} ] ||
14750                         error "changelogs are off on mds$i"
14751         done
14752 }
14753 run_test 160i "changelog user register/unregister race"
14754
14755 test_160j() {
14756         remote_mds_nodsh && skip "remote MDS with nodsh"
14757         [[ $MDS1_VERSION -lt $(version_code 2.12.56) ]] &&
14758                 skip "Need MDS version at least 2.12.56"
14759
14760         mount_client $MOUNT2 || error "mount_client on $MOUNT2 failed"
14761         stack_trap "umount $MOUNT2" EXIT
14762
14763         changelog_register || error "first changelog_register failed"
14764         stack_trap "changelog_deregister" EXIT
14765
14766         # generate some changelog
14767         # use fnv1a because created files should be evenly distributed
14768         test_mkdir -c $MDSCOUNT -H fnv_1a_64 $DIR/$tdir ||
14769                 error "mkdir $tdir failed"
14770         createmany -m $DIR/$tdir/${tfile}bis $((MDSCOUNT * 2)) ||
14771                 error "create $DIR/$tdir/${tfile}bis failed"
14772
14773         # open the changelog device
14774         exec 3>/dev/changelog-$FSNAME-MDT0000
14775         stack_trap "exec 3>&-" EXIT
14776         exec 4</dev/changelog-$FSNAME-MDT0000
14777         stack_trap "exec 4<&-" EXIT
14778
14779         # umount the first lustre mount
14780         umount $MOUNT
14781         stack_trap "mount_client $MOUNT" EXIT
14782
14783         # read changelog
14784         cat <&4 >/dev/null || error "read changelog failed"
14785
14786         # clear changelog
14787         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14788         changelog_users $SINGLEMDS | grep -q $cl_user ||
14789                 error "User $cl_user not found in changelog_users"
14790
14791         printf 'clear:'$cl_user':0' >&3
14792 }
14793 run_test 160j "client can be umounted  while its chanangelog is being used"
14794
14795 test_160k() {
14796         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14797         remote_mds_nodsh && skip "remote MDS with nodsh"
14798
14799         mkdir -p $DIR/$tdir/1/1
14800
14801         changelog_register || error "changelog_register failed"
14802         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
14803
14804         changelog_users $SINGLEMDS | grep -q $cl_user ||
14805                 error "User '$cl_user' not found in changelog_users"
14806 #define OBD_FAIL_MDS_CHANGELOG_REORDER 0x15d
14807         do_facet mds1 $LCTL set_param fail_loc=0x8000015d fail_val=3
14808         rmdir $DIR/$tdir/1/1 & sleep 1
14809         mkdir $DIR/$tdir/2
14810         touch $DIR/$tdir/2/2
14811         rm -rf $DIR/$tdir/2
14812
14813         wait
14814         sleep 4
14815
14816         changelog_dump | grep rmdir || error "rmdir not recorded"
14817
14818         rm -rf $DIR/$tdir
14819         changelog_deregister
14820 }
14821 run_test 160k "Verify that changelog records are not lost"
14822
14823 test_161a() {
14824         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14825
14826         test_mkdir -c1 $DIR/$tdir
14827         cp /etc/hosts $DIR/$tdir/$tfile
14828         test_mkdir -c1 $DIR/$tdir/foo1
14829         test_mkdir -c1 $DIR/$tdir/foo2
14830         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/sofia
14831         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/zachary
14832         ln $DIR/$tdir/$tfile $DIR/$tdir/foo1/luna
14833         ln $DIR/$tdir/$tfile $DIR/$tdir/foo2/thor
14834         local FID=$($LFS path2fid $DIR/$tdir/$tfile | tr -d '[]')
14835         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14836                 $LFS fid2path $DIR $FID
14837                 error "bad link ea"
14838         fi
14839         # middle
14840         rm $DIR/$tdir/foo2/zachary
14841         # last
14842         rm $DIR/$tdir/foo2/thor
14843         # first
14844         rm $DIR/$tdir/$tfile
14845         # rename
14846         mv $DIR/$tdir/foo1/sofia $DIR/$tdir/foo2/maggie
14847         [ "$($LFS fid2path $FSNAME --link 1 $FID)" != "$tdir/foo2/maggie" ] &&
14848                 { $LFS fid2path $DIR $FID; error "bad link rename"; }
14849         rm $DIR/$tdir/foo2/maggie
14850
14851         # overflow the EA
14852         local longname=$tfile.avg_len_is_thirty_two_
14853         stack_trap "unlinkmany $DIR/$tdir/foo2/$longname 1000 || \
14854                 error_noexit 'failed to unlink many hardlinks'" EXIT
14855         createmany -l$DIR/$tdir/foo1/luna $DIR/$tdir/foo2/$longname 1000 ||
14856                 error "failed to hardlink many files"
14857         links=$($LFS fid2path $DIR $FID | wc -l)
14858         echo -n "${links}/1000 links in link EA"
14859         [[ $links -gt 60 ]] || error "expected at least 60 links in link EA"
14860 }
14861 run_test 161a "link ea sanity"
14862
14863 test_161b() {
14864         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14865         [ $MDSCOUNT -lt 2 ] && skip_env "skipping remote directory test"
14866
14867         local MDTIDX=1
14868         local remote_dir=$DIR/$tdir/remote_dir
14869
14870         mkdir -p $DIR/$tdir
14871         $LFS mkdir -i $MDTIDX $remote_dir ||
14872                 error "create remote directory failed"
14873
14874         cp /etc/hosts $remote_dir/$tfile
14875         mkdir -p $remote_dir/foo1
14876         mkdir -p $remote_dir/foo2
14877         ln $remote_dir/$tfile $remote_dir/foo1/sofia
14878         ln $remote_dir/$tfile $remote_dir/foo2/zachary
14879         ln $remote_dir/$tfile $remote_dir/foo1/luna
14880         ln $remote_dir/$tfile $remote_dir/foo2/thor
14881
14882         local FID=$($LFS path2fid $remote_dir/$tfile | tr -d '[' |
14883                      tr -d ']')
14884         if [ "$($LFS fid2path $DIR $FID | wc -l)" != "5" ]; then
14885                 $LFS fid2path $DIR $FID
14886                 error "bad link ea"
14887         fi
14888         # middle
14889         rm $remote_dir/foo2/zachary
14890         # last
14891         rm $remote_dir/foo2/thor
14892         # first
14893         rm $remote_dir/$tfile
14894         # rename
14895         mv $remote_dir/foo1/sofia $remote_dir/foo2/maggie
14896         local link_path=$($LFS fid2path $FSNAME --link 1 $FID)
14897         if [ "$DIR/$link_path" != "$remote_dir/foo2/maggie" ]; then
14898                 $LFS fid2path $DIR $FID
14899                 error "bad link rename"
14900         fi
14901         rm $remote_dir/foo2/maggie
14902
14903         # overflow the EA
14904         local longname=filename_avg_len_is_thirty_two_
14905         createmany -l$remote_dir/foo1/luna $remote_dir/foo2/$longname 1000 ||
14906                 error "failed to hardlink many files"
14907         links=$($LFS fid2path $DIR $FID | wc -l)
14908         echo -n "${links}/1000 links in link EA"
14909         [[ ${links} -gt 60 ]] ||
14910                 error "expected at least 60 links in link EA"
14911         unlinkmany $remote_dir/foo2/$longname 1000 ||
14912         error "failed to unlink many hardlinks"
14913 }
14914 run_test 161b "link ea sanity under remote directory"
14915
14916 test_161c() {
14917         remote_mds_nodsh && skip "remote MDS with nodsh"
14918         [ $PARALLEL == "yes" ] && skip "skip parallel run"
14919         [[ $MDS1_VERSION -lt $(version_code 2.1.5) ]] &&
14920                 skip "Need MDS version at least 2.1.5"
14921
14922         # define CLF_RENAME_LAST 0x0001
14923         # rename overwrite a target having nlink = 1 (changelog flag 0x1)
14924         changelog_register || error "changelog_register failed"
14925
14926         rm -rf $DIR/$tdir
14927         test_mkdir -i $((MDSCOUNT - 1)) $DIR/$tdir
14928         touch $DIR/$tdir/foo_161c
14929         touch $DIR/$tdir/bar_161c
14930         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14931         changelog_dump | grep RENME | tail -n 5
14932         local flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14933         changelog_clear 0 || error "changelog_clear failed"
14934         if [ x$flags != "x0x1" ]; then
14935                 error "flag $flags is not 0x1"
14936         fi
14937
14938         echo "rename overwrite target with nlink = 1, changelog flags=$flags"
14939         # rename overwrite a target having nlink > 1 (changelog flag 0x0)
14940         touch $DIR/$tdir/foo_161c
14941         touch $DIR/$tdir/bar_161c
14942         ln $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14943         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/bar_161c
14944         changelog_dump | grep RENME | tail -n 5
14945         flags=$(changelog_dump | grep "RENME.*bar_161c" | cut -f5 -d' ')
14946         changelog_clear 0 || error "changelog_clear failed"
14947         if [ x$flags != "x0x0" ]; then
14948                 error "flag $flags is not 0x0"
14949         fi
14950         echo "rename overwrite a target having nlink > 1," \
14951                 "changelog record has flags of $flags"
14952
14953         # rename doesn't overwrite a target (changelog flag 0x0)
14954         touch $DIR/$tdir/foo_161c
14955         mv -f $DIR/$tdir/foo_161c $DIR/$tdir/foo2_161c
14956         changelog_dump | grep RENME | tail -n 5
14957         flags=$(changelog_dump | grep RENME | tail -1 | cut -f5 -d' ')
14958         changelog_clear 0 || error "changelog_clear failed"
14959         if [ x$flags != "x0x0" ]; then
14960                 error "flag $flags is not 0x0"
14961         fi
14962         echo "rename doesn't overwrite a target," \
14963                 "changelog record has flags of $flags"
14964
14965         # define CLF_UNLINK_LAST 0x0001
14966         # unlink a file having nlink = 1 (changelog flag 0x1)
14967         rm -f $DIR/$tdir/foo2_161c
14968         changelog_dump | grep UNLNK | tail -n 5
14969         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14970         changelog_clear 0 || error "changelog_clear failed"
14971         if [ x$flags != "x0x1" ]; then
14972                 error "flag $flags is not 0x1"
14973         fi
14974         echo "unlink a file having nlink = 1," \
14975                 "changelog record has flags of $flags"
14976
14977         # unlink a file having nlink > 1 (changelog flag 0x0)
14978         ln -f $DIR/$tdir/bar_161c $DIR/$tdir/foobar_161c
14979         rm -f $DIR/$tdir/foobar_161c
14980         changelog_dump | grep UNLNK | tail -n 5
14981         flags=$(changelog_dump | grep UNLNK | tail -1 | cut -f5 -d' ')
14982         changelog_clear 0 || error "changelog_clear failed"
14983         if [ x$flags != "x0x0" ]; then
14984                 error "flag $flags is not 0x0"
14985         fi
14986         echo "unlink a file having nlink > 1, changelog record flags '$flags'"
14987 }
14988 run_test 161c "check CL_RENME[UNLINK] changelog record flags"
14989
14990 test_161d() {
14991         remote_mds_nodsh && skip "remote MDS with nodsh"
14992         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
14993
14994         local pid
14995         local fid
14996
14997         changelog_register || error "changelog_register failed"
14998
14999         # work in a standalone dir to avoid locking on $DIR/$MOUNT to
15000         # interfer with $MOUNT/.lustre/fid/ access
15001         mkdir $DIR/$tdir
15002         [[ $? -eq 0 ]] || error "mkdir failed"
15003
15004         #define OBD_FAIL_LLITE_CREATE_NODE_PAUSE 0x140c | OBD_FAIL_ONCE
15005         $LCTL set_param fail_loc=0x8000140c
15006         # 5s pause
15007         $LCTL set_param fail_val=5
15008
15009         # create file
15010         echo foofoo > $DIR/$tdir/$tfile &
15011         pid=$!
15012
15013         # wait for create to be delayed
15014         sleep 2
15015
15016         ps -p $pid
15017         [[ $? -eq 0 ]] || error "create should be blocked"
15018
15019         local tempfile="$(mktemp --tmpdir $tfile.XXXXXX)"
15020         stack_trap "rm -f $tempfile"
15021         fid=$(changelog_extract_field "CREAT" "$tfile" "t=")
15022         cat $MOUNT/.lustre/fid/$fid 2>/dev/null >$tempfile || error "cat failed"
15023         # some delay may occur during ChangeLog publishing and file read just
15024         # above, that could allow file write to happen finally
15025         [[ -s $tempfile ]] && echo "file should be empty"
15026
15027         $LCTL set_param fail_loc=0
15028
15029         wait $pid
15030         [[ $? -eq 0 ]] || error "create failed"
15031 }
15032 run_test 161d "create with concurrent .lustre/fid access"
15033
15034 check_path() {
15035         local expected="$1"
15036         shift
15037         local fid="$2"
15038
15039         local path
15040         path=$($LFS fid2path "$@")
15041         local rc=$?
15042
15043         if [ $rc -ne 0 ]; then
15044                 error "path looked up of '$expected' failed: rc=$rc"
15045         elif [ "$path" != "$expected" ]; then
15046                 error "path looked up '$path' instead of '$expected'"
15047         else
15048                 echo "FID '$fid' resolves to path '$path' as expected"
15049         fi
15050 }
15051
15052 test_162a() { # was test_162
15053         test_mkdir -p -c1 $DIR/$tdir/d2
15054         touch $DIR/$tdir/d2/$tfile
15055         touch $DIR/$tdir/d2/x1
15056         touch $DIR/$tdir/d2/x2
15057         test_mkdir -p -c1 $DIR/$tdir/d2/a/b/c
15058         test_mkdir -p -c1 $DIR/$tdir/d2/p/q/r
15059         # regular file
15060         local fid=$($LFS path2fid $DIR/$tdir/d2/$tfile | tr -d '[]')
15061         check_path "$tdir/d2/$tfile" $FSNAME "$fid" --link 0
15062
15063         # softlink
15064         ln -s $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/slink
15065         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink | tr -d '[]')
15066         check_path "$tdir/d2/p/q/r/slink" $FSNAME "$fid" --link 0
15067
15068         # softlink to wrong file
15069         ln -s /this/is/garbage $DIR/$tdir/d2/p/q/r/slink.wrong
15070         fid=$($LFS path2fid $DIR/$tdir/d2/p/q/r/slink.wrong | tr -d '[]')
15071         check_path "$tdir/d2/p/q/r/slink.wrong" $FSNAME "$fid" --link 0
15072
15073         # hardlink
15074         ln $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/p/q/r/hlink
15075         mv $DIR/$tdir/d2/$tfile $DIR/$tdir/d2/a/b/c/new_file
15076         fid=$($LFS path2fid $DIR/$tdir/d2/a/b/c/new_file | tr -d '[]')
15077         # fid2path dir/fsname should both work
15078         check_path "$tdir/d2/a/b/c/new_file" $FSNAME "$fid" --link 1
15079         check_path "$DIR/$tdir/d2/p/q/r/hlink" $DIR "$fid" --link 0
15080
15081         # hardlink count: check that there are 2 links
15082         local nlinks=$($LFS fid2path $DIR "$fid" | wc -l)
15083         [ $nlinks -eq 2 ] || error "expect 2 links, found $nlinks"
15084
15085         # hardlink indexing: remove the first link
15086         rm $DIR/$tdir/d2/p/q/r/hlink
15087         check_path "$tdir/d2/a/b/c/new_file" $FSNAME $fid --link 0
15088 }
15089 run_test 162a "path lookup sanity"
15090
15091 test_162b() {
15092         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15093         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
15094
15095         mkdir $DIR/$tdir
15096         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
15097                                 error "create striped dir failed"
15098
15099         local FID=$($LFS getdirstripe $DIR/$tdir/striped_dir |
15100                                         tail -n 1 | awk '{print $2}')
15101         stat $MOUNT/.lustre/fid/$FID && error "sub_stripe can be accessed"
15102
15103         touch $DIR/$tdir/striped_dir/f{0..4} || error "touch f0..4 failed"
15104         mkdir $DIR/$tdir/striped_dir/d{0..4} || error "mkdir d0..4 failed"
15105
15106         # regular file
15107         for ((i=0;i<5;i++)); do
15108                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/f$i | tr -d '[]') ||
15109                         error "get fid for f$i failed"
15110                 check_path "$tdir/striped_dir/f$i" $FSNAME $FID --link 0
15111
15112                 FID=$($LFS path2fid $DIR/$tdir/striped_dir/d$i | tr -d '[]') ||
15113                         error "get fid for d$i failed"
15114                 check_path "$tdir/striped_dir/d$i" $FSNAME $FID --link 0
15115         done
15116
15117         return 0
15118 }
15119 run_test 162b "striped directory path lookup sanity"
15120
15121 # LU-4239: Verify fid2path works with paths 100 or more directories deep
15122 test_162c() {
15123         [[ $MDS1_VERSION -lt $(version_code 2.7.51) ]] &&
15124                 skip "Need MDS version at least 2.7.51"
15125
15126         local lpath=$tdir.local
15127         local rpath=$tdir.remote
15128
15129         test_mkdir $DIR/$lpath
15130         test_mkdir $DIR/$rpath
15131
15132         for ((i = 0; i <= 101; i++)); do
15133                 lpath="$lpath/$i"
15134                 mkdir $DIR/$lpath
15135                 FID=$($LFS path2fid $DIR/$lpath | tr -d '[]') ||
15136                         error "get fid for local directory $DIR/$lpath failed"
15137                 check_path "$DIR/$lpath" $MOUNT $FID --link 0
15138
15139                 rpath="$rpath/$i"
15140                 test_mkdir $DIR/$rpath
15141                 FID=$($LFS path2fid $DIR/$rpath | tr -d '[]') ||
15142                         error "get fid for remote directory $DIR/$rpath failed"
15143                 check_path "$DIR/$rpath" $MOUNT $FID --link 0
15144         done
15145
15146         return 0
15147 }
15148 run_test 162c "fid2path works with paths 100 or more directories deep"
15149
15150 oalr_event_count() {
15151         local event="${1}"
15152         local trace="${2}"
15153
15154         awk -v name="${FSNAME}-OST0000" \
15155             -v event="${event}" \
15156             '$1 == "TRACE" && $2 == event && $3 == name' \
15157             "${trace}" |
15158         wc -l
15159 }
15160
15161 oalr_expect_event_count() {
15162         local event="${1}"
15163         local trace="${2}"
15164         local expect="${3}"
15165         local count
15166
15167         count=$(oalr_event_count "${event}" "${trace}")
15168         if ((count == expect)); then
15169                 return 0
15170         fi
15171
15172         error_noexit "${event} event count was '${count}', expected ${expect}"
15173         cat "${trace}" >&2
15174         exit 1
15175 }
15176
15177 cleanup_165() {
15178         do_facet ost1 killall --quiet -KILL ofd_access_log_reader || true
15179         stop ost1
15180         start ost1 "$(ostdevname 1)" $OST_MOUNT_OPTS
15181 }
15182
15183 setup_165() {
15184         sync # Flush previous IOs so we can count log entries.
15185         do_facet ost1 $LCTL set_param "obdfilter.${FSNAME}-OST0000.access_log_size=4096"
15186         stack_trap cleanup_165 EXIT
15187 }
15188
15189 test_165a() {
15190         local trace="/tmp/${tfile}.trace"
15191         local rc
15192         local count
15193
15194         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15195         setup_165
15196         sleep 5
15197
15198         do_facet ost1 ofd_access_log_reader --list
15199         stop ost1
15200
15201         do_facet ost1 killall -TERM ofd_access_log_reader
15202         wait
15203         rc=$?
15204
15205         if ((rc != 0)); then
15206                 error "ofd_access_log_reader exited with rc = '${rc}'"
15207         fi
15208
15209         # Parse trace file for discovery events:
15210         oalr_expect_event_count alr_log_add "${trace}" 1
15211         oalr_expect_event_count alr_log_eof "${trace}" 1
15212         oalr_expect_event_count alr_log_free "${trace}" 1
15213 }
15214 run_test 165a "ofd access log discovery"
15215
15216 test_165b() {
15217         local trace="/tmp/${tfile}.trace"
15218         local file="${DIR}/${tfile}"
15219         local pfid1
15220         local pfid2
15221         local -a entry
15222         local rc
15223         local count
15224         local size
15225         local flags
15226
15227         setup_165
15228
15229         lfs setstripe -c 1 -i 0 "${file}"
15230         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15231         do_facet ost1 ofd_access_log_reader --list
15232
15233         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15234         sleep 5
15235         do_facet ost1 killall -TERM ofd_access_log_reader
15236         wait
15237         rc=$?
15238
15239         if ((rc != 0)); then
15240                 error "ofd_access_log_reader exited with rc = '${rc}'"
15241         fi
15242
15243         oalr_expect_event_count alr_log_entry "${trace}" 1
15244
15245         pfid1=$($LFS path2fid "${file}")
15246
15247         # 1     2             3   4    5     6   7    8    9     10
15248         # TRACE alr_log_entry OST PFID BEGIN END TIME SIZE COUNT FLAGS
15249         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
15250
15251         echo "entry = '${entry[*]}'" >&2
15252
15253         pfid2=${entry[4]}
15254         if [[ "${pfid1}" != "${pfid2}" ]]; then
15255                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
15256         fi
15257
15258         size=${entry[8]}
15259         if ((size != 1048576)); then
15260                 error "entry '${entry[*]}' has invalid io size '${size}', expected 1048576"
15261         fi
15262
15263         flags=${entry[10]}
15264         if [[ "${flags}" != "w" ]]; then
15265                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'w'"
15266         fi
15267
15268         do_facet ost1 ofd_access_log_reader --debug=- --trace=- > "${trace}" &
15269         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r524288c || error "cannot read '${file}'"
15270         sleep 5
15271         do_facet ost1 killall -TERM ofd_access_log_reader
15272         wait
15273         rc=$?
15274
15275         if ((rc != 0)); then
15276                 error "ofd_access_log_reader exited with rc = '${rc}'"
15277         fi
15278
15279         oalr_expect_event_count alr_log_entry "${trace}" 1
15280
15281         entry=( - $(awk -v pfid="${pfid}" '$1 == "TRACE" && $2 == "alr_log_entry"' "${trace}" ) )
15282         echo "entry = '${entry[*]}'" >&2
15283
15284         pfid2=${entry[4]}
15285         if [[ "${pfid1}" != "${pfid2}" ]]; then
15286                 error "entry '${entry[*]}' has invalid PFID '${pfid2}', expected ${pfid1}"
15287         fi
15288
15289         size=${entry[8]}
15290         if ((size != 524288)); then
15291                 error "entry '${entry[*]}' has invalid io size '${size}', 524288"
15292         fi
15293
15294         flags=${entry[10]}
15295         if [[ "${flags}" != "r" ]]; then
15296                 error "entry '${entry[*]}' has invalid io flags '${flags}', expected 'r'"
15297         fi
15298 }
15299 run_test 165b "ofd access log entries are produced and consumed"
15300
15301 test_165c() {
15302         local file="${DIR}/${tdir}/${tfile}"
15303         test_mkdir "${DIR}/${tdir}"
15304
15305         setup_165
15306
15307         lfs setstripe -c 1 -i 0 "${DIR}/${tdir}"
15308
15309         # 4096 / 64 = 64. Create twice as many entries.
15310         for ((i = 0; i < 128; i++)); do
15311                 $MULTIOP "${file}-${i}" oO_CREAT:O_WRONLY:w512c || error "cannot create file"
15312         done
15313
15314         sync
15315         do_facet ost1 ofd_access_log_reader --list
15316         unlinkmany  "${file}-%d" 128
15317 }
15318 run_test 165c "full ofd access logs do not block IOs"
15319
15320 oal_peek_entry_count() {
15321         do_facet ost1 ofd_access_log_reader --list | awk '$1 == "_entry_count:" { print $2; }'
15322 }
15323
15324 oal_expect_entry_count() {
15325         local entry_count=$(oal_peek_entry_count)
15326         local expect="$1"
15327
15328         if ((entry_count == expect)); then
15329                 return 0
15330         fi
15331
15332         error_noexit "bad entry count, got ${entry_count}, expected ${expect}"
15333         do_facet ost1 ofd_access_log_reader --list >&2
15334         exit 1
15335 }
15336
15337 test_165d() {
15338         local trace="/tmp/${tfile}.trace"
15339         local file="${DIR}/${tdir}/${tfile}"
15340         local param="obdfilter.${FSNAME}-OST0000.access_log_mask"
15341         local entry_count
15342         test_mkdir "${DIR}/${tdir}"
15343
15344         setup_165
15345         lfs setstripe -c 1 -i 0 "${file}"
15346
15347         do_facet ost1 lctl set_param "${param}=rw"
15348         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15349         oal_expect_entry_count 1
15350
15351         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15352         oal_expect_entry_count 2
15353
15354         do_facet ost1 lctl set_param "${param}=r"
15355         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15356         oal_expect_entry_count 2
15357
15358         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15359         oal_expect_entry_count 3
15360
15361         do_facet ost1 lctl set_param "${param}=w"
15362         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15363         oal_expect_entry_count 4
15364
15365         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15366         oal_expect_entry_count 4
15367
15368         do_facet ost1 lctl set_param "${param}=0"
15369         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_WRONLY:w1048576c || error "cannot create '${file}'"
15370         oal_expect_entry_count 4
15371
15372         $MULTIOP "${file}" oO_CREAT:O_DIRECT:O_RDONLY:r1048576c || error "cannot read '${file}'"
15373         oal_expect_entry_count 4
15374 }
15375 run_test 165d "ofd_access_log mask works"
15376
15377 test_169() {
15378         # do directio so as not to populate the page cache
15379         log "creating a 10 Mb file"
15380         $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
15381         log "starting reads"
15382         dd if=$DIR/$tfile of=/dev/null bs=4096 &
15383         log "truncating the file"
15384         $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
15385         log "killing dd"
15386         kill %+ || true # reads might have finished
15387         echo "wait until dd is finished"
15388         wait
15389         log "removing the temporary file"
15390         rm -rf $DIR/$tfile || error "tmp file removal failed"
15391 }
15392 run_test 169 "parallel read and truncate should not deadlock"
15393
15394 test_170() {
15395         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15396
15397         $LCTL clear     # bug 18514
15398         $LCTL debug_daemon start $TMP/${tfile}_log_good
15399         touch $DIR/$tfile
15400         $LCTL debug_daemon stop
15401         sed -e "s/^...../a/g" $TMP/${tfile}_log_good > $TMP/${tfile}_log_bad ||
15402                 error "sed failed to read log_good"
15403
15404         $LCTL debug_daemon start $TMP/${tfile}_log_good
15405         rm -rf $DIR/$tfile
15406         $LCTL debug_daemon stop
15407
15408         $LCTL df $TMP/${tfile}_log_bad > $TMP/${tfile}_log_bad.out 2>&1 ||
15409                error "lctl df log_bad failed"
15410
15411         local bad_line=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15412         local good_line1=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15413
15414         $LCTL df $TMP/${tfile}_log_good > $TMP/${tfile}_log_good.out 2>&1
15415         local good_line2=$(tail -n 1 $TMP/${tfile}_log_good.out | awk '{print $5}')
15416
15417         [ "$bad_line" ] && [ "$good_line1" ] && [ "$good_line2" ] ||
15418                 error "bad_line good_line1 good_line2 are empty"
15419
15420         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15421         cat $TMP/${tfile}_log_bad >> $TMP/${tfile}_logs_corrupt
15422         cat $TMP/${tfile}_log_good >> $TMP/${tfile}_logs_corrupt
15423
15424         $LCTL df $TMP/${tfile}_logs_corrupt > $TMP/${tfile}_log_bad.out 2>&1
15425         local bad_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $9}')
15426         local good_line_new=$(tail -n 1 $TMP/${tfile}_log_bad.out | awk '{print $5}')
15427
15428         [ "$bad_line_new" ] && [ "$good_line_new" ] ||
15429                 error "bad_line_new good_line_new are empty"
15430
15431         local expected_good=$((good_line1 + good_line2*2))
15432
15433         rm -f $TMP/${tfile}*
15434         # LU-231, short malformed line may not be counted into bad lines
15435         if [ $bad_line -ne $bad_line_new ] &&
15436                    [ $bad_line -ne $((bad_line_new - 1)) ]; then
15437                 error "expected $bad_line bad lines, but got $bad_line_new"
15438                 return 1
15439         fi
15440
15441         if [ $expected_good -ne $good_line_new ]; then
15442                 error "expected $expected_good good lines, but got $good_line_new"
15443                 return 2
15444         fi
15445         true
15446 }
15447 run_test 170 "test lctl df to handle corrupted log ====================="
15448
15449 test_171() { # bug20592
15450         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15451
15452         #define OBD_FAIL_PTLRPC_DUMP_LOG         0x50e
15453         $LCTL set_param fail_loc=0x50e
15454         $LCTL set_param fail_val=3000
15455         multiop_bg_pause $DIR/$tfile O_s || true
15456         local MULTIPID=$!
15457         kill -USR1 $MULTIPID
15458         # cause log dump
15459         sleep 3
15460         wait $MULTIPID
15461         if dmesg | grep "recursive fault"; then
15462                 error "caught a recursive fault"
15463         fi
15464         $LCTL set_param fail_loc=0
15465         true
15466 }
15467 run_test 171 "test libcfs_debug_dumplog_thread stuck in do_exit() ======"
15468
15469 # it would be good to share it with obdfilter-survey/iokit-libecho code
15470 setup_obdecho_osc () {
15471         local rc=0
15472         local ost_nid=$1
15473         local obdfilter_name=$2
15474         echo "Creating new osc for $obdfilter_name on $ost_nid"
15475         # make sure we can find loopback nid
15476         $LCTL add_uuid $ost_nid $ost_nid >/dev/null 2>&1
15477
15478         [ $rc -eq 0 ] && { $LCTL attach osc ${obdfilter_name}_osc     \
15479                            ${obdfilter_name}_osc_UUID || rc=2; }
15480         [ $rc -eq 0 ] && { $LCTL --device ${obdfilter_name}_osc setup \
15481                            ${obdfilter_name}_UUID  $ost_nid || rc=3; }
15482         return $rc
15483 }
15484
15485 cleanup_obdecho_osc () {
15486         local obdfilter_name=$1
15487         $LCTL --device ${obdfilter_name}_osc cleanup >/dev/null
15488         $LCTL --device ${obdfilter_name}_osc detach  >/dev/null
15489         return 0
15490 }
15491
15492 obdecho_test() {
15493         local OBD=$1
15494         local node=$2
15495         local pages=${3:-64}
15496         local rc=0
15497         local id
15498
15499         local count=10
15500         local obd_size=$(get_obd_size $node $OBD)
15501         local page_size=$(get_page_size $node)
15502         if [[ -n "$obd_size" ]]; then
15503                 local new_count=$((obd_size / (pages * page_size / 1024)))
15504                 [[ $new_count -ge $count ]] || count=$new_count
15505         fi
15506
15507         do_facet $node "$LCTL attach echo_client ec ec_uuid" || rc=1
15508         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec setup $OBD" ||
15509                            rc=2; }
15510         if [ $rc -eq 0 ]; then
15511             id=$(do_facet $node "$LCTL --device ec create 1"  | awk '/object id/ {print $6}')
15512             [ ${PIPESTATUS[0]} -eq 0 -a -n "$id" ] || rc=3
15513         fi
15514         echo "New object id is $id"
15515         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec getattr $id" ||
15516                            rc=4; }
15517         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec "                 \
15518                            "test_brw $count w v $pages $id" || rc=4; }
15519         [ $rc -eq 0 ] && { do_facet $node "$LCTL --device ec destroy $id 1" ||
15520                            rc=4; }
15521         [ $rc -eq 0 ] || [ $rc -gt 2 ] &&
15522                 { do_facet $node "$LCTL --device ec cleanup" || rc=5; }
15523         [ $rc -eq 0 ] || [ $rc -gt 1 ] &&
15524                 { do_facet $node "$LCTL --device ec detach" || rc=6; }
15525         [ $rc -ne 0 ] && echo "obecho_create_test failed: $rc"
15526         return $rc
15527 }
15528
15529 test_180a() {
15530         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15531
15532         if ! module_loaded obdecho; then
15533                 load_module obdecho/obdecho &&
15534                         stack_trap "rmmod obdecho" EXIT ||
15535                         error "unable to load obdecho on client"
15536         fi
15537
15538         local osc=$($LCTL dl | grep -v mdt | awk '$3 == "osc" {print $4; exit}')
15539         local host=$($LCTL get_param -n osc.$osc.import |
15540                      awk '/current_connection:/ { print $2 }' )
15541         local target=$($LCTL get_param -n osc.$osc.import |
15542                        awk '/target:/ { print $2 }' )
15543         target=${target%_UUID}
15544
15545         if [ -n "$target" ]; then
15546                 setup_obdecho_osc $host $target &&
15547                         stack_trap "cleanup_obdecho_osc $target" EXIT ||
15548                         { error "obdecho setup failed with $?"; return; }
15549
15550                 obdecho_test ${target}_osc client ||
15551                         error "obdecho_test failed on ${target}_osc"
15552         else
15553                 $LCTL get_param osc.$osc.import
15554                 error "there is no osc.$osc.import target"
15555         fi
15556 }
15557 run_test 180a "test obdecho on osc"
15558
15559 test_180b() {
15560         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15561         remote_ost_nodsh && skip "remote OST with nodsh"
15562
15563         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15564                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15565                 error "failed to load module obdecho"
15566
15567         local target=$(do_facet ost1 $LCTL dl |
15568                        awk '/obdfilter/ { print $4; exit; }')
15569
15570         if [ -n "$target" ]; then
15571                 obdecho_test $target ost1 || error "obdecho_test failed with $?"
15572         else
15573                 do_facet ost1 $LCTL dl
15574                 error "there is no obdfilter target on ost1"
15575         fi
15576 }
15577 run_test 180b "test obdecho directly on obdfilter"
15578
15579 test_180c() { # LU-2598
15580         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15581         remote_ost_nodsh && skip "remote OST with nodsh"
15582         [[ $MDS1_VERSION -lt $(version_code 2.4.0) ]] &&
15583                 skip "Need MDS version at least 2.4.0"
15584
15585         do_rpc_nodes $(facet_active_host ost1) load_module obdecho/obdecho &&
15586                 stack_trap "do_facet ost1 rmmod obdecho" EXIT ||
15587                 error "failed to load module obdecho"
15588
15589         local target=$(do_facet ost1 $LCTL dl |
15590                        awk '/obdfilter/ { print $4; exit; }')
15591
15592         if [ -n "$target" ]; then
15593                 local pages=16384 # 64MB bulk I/O RPC size
15594
15595                 obdecho_test "$target" ost1 "$pages" ||
15596                         error "obdecho_test with pages=$pages failed with $?"
15597         else
15598                 do_facet ost1 $LCTL dl
15599                 error "there is no obdfilter target on ost1"
15600         fi
15601 }
15602 run_test 180c "test huge bulk I/O size on obdfilter, don't LASSERT"
15603
15604 test_181() { # bug 22177
15605         test_mkdir $DIR/$tdir
15606         # create enough files to index the directory
15607         createmany -o $DIR/$tdir/foobar 4000
15608         # print attributes for debug purpose
15609         lsattr -d .
15610         # open dir
15611         multiop_bg_pause $DIR/$tdir D_Sc || return 1
15612         MULTIPID=$!
15613         # remove the files & current working dir
15614         unlinkmany $DIR/$tdir/foobar 4000
15615         rmdir $DIR/$tdir
15616         kill -USR1 $MULTIPID
15617         wait $MULTIPID
15618         stat $DIR/$tdir && error "open-unlinked dir was not removed!"
15619         return 0
15620 }
15621 run_test 181 "Test open-unlinked dir ========================"
15622
15623 test_182() {
15624         local fcount=1000
15625         local tcount=10
15626
15627         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15628
15629         $LCTL set_param mdc.*.rpc_stats=clear
15630
15631         for (( i = 0; i < $tcount; i++ )) ; do
15632                 mkdir $DIR/$tdir/$i
15633         done
15634
15635         for (( i = 0; i < $tcount; i++ )) ; do
15636                 createmany -o $DIR/$tdir/$i/f- $fcount &
15637         done
15638         wait
15639
15640         for (( i = 0; i < $tcount; i++ )) ; do
15641                 unlinkmany $DIR/$tdir/$i/f- $fcount &
15642         done
15643         wait
15644
15645         $LCTL get_param mdc.*.rpc_stats
15646
15647         rm -rf $DIR/$tdir
15648 }
15649 run_test 182 "Test parallel modify metadata operations ================"
15650
15651 test_183() { # LU-2275
15652         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15653         remote_mds_nodsh && skip "remote MDS with nodsh"
15654         [[ $MDS1_VERSION -lt $(version_code 2.3.56) ]] &&
15655                 skip "Need MDS version at least 2.3.56"
15656
15657         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15658         echo aaa > $DIR/$tdir/$tfile
15659
15660 #define OBD_FAIL_MDS_NEGATIVE_POSITIVE  0x148
15661         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x148
15662
15663         ls -l $DIR/$tdir && error "ls succeeded, should have failed"
15664         cat $DIR/$tdir/$tfile && error "cat succeeded, should have failed"
15665
15666         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
15667
15668         # Flush negative dentry cache
15669         touch $DIR/$tdir/$tfile
15670
15671         # We are not checking for any leaked references here, they'll
15672         # become evident next time we do cleanup with module unload.
15673         rm -rf $DIR/$tdir
15674 }
15675 run_test 183 "No crash or request leak in case of strange dispositions ========"
15676
15677 # test suite 184 is for LU-2016, LU-2017
15678 test_184a() {
15679         check_swap_layouts_support
15680
15681         dir0=$DIR/$tdir/$testnum
15682         test_mkdir -p -c1 $dir0
15683         ref1=/etc/passwd
15684         ref2=/etc/group
15685         file1=$dir0/f1
15686         file2=$dir0/f2
15687         $LFS setstripe -c1 $file1
15688         cp $ref1 $file1
15689         $LFS setstripe -c2 $file2
15690         cp $ref2 $file2
15691         gen1=$($LFS getstripe -g $file1)
15692         gen2=$($LFS getstripe -g $file2)
15693
15694         $LFS swap_layouts $file1 $file2 || error "swap of file layout failed"
15695         gen=$($LFS getstripe -g $file1)
15696         [[ $gen1 != $gen ]] ||
15697                 "Layout generation on $file1 does not change"
15698         gen=$($LFS getstripe -g $file2)
15699         [[ $gen2 != $gen ]] ||
15700                 "Layout generation on $file2 does not change"
15701
15702         cmp $ref1 $file2 || error "content compare failed ($ref1 != $file2)"
15703         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
15704
15705         lfsck_verify_pfid $file1 $file2 || error "PFID are not transferred"
15706 }
15707 run_test 184a "Basic layout swap"
15708
15709 test_184b() {
15710         check_swap_layouts_support
15711
15712         dir0=$DIR/$tdir/$testnum
15713         mkdir -p $dir0 || error "creating dir $dir0"
15714         file1=$dir0/f1
15715         file2=$dir0/f2
15716         file3=$dir0/f3
15717         dir1=$dir0/d1
15718         dir2=$dir0/d2
15719         mkdir $dir1 $dir2
15720         $LFS setstripe -c1 $file1
15721         $LFS setstripe -c2 $file2
15722         $LFS setstripe -c1 $file3
15723         chown $RUNAS_ID $file3
15724         gen1=$($LFS getstripe -g $file1)
15725         gen2=$($LFS getstripe -g $file2)
15726
15727         $LFS swap_layouts $dir1 $dir2 &&
15728                 error "swap of directories layouts should fail"
15729         $LFS swap_layouts $dir1 $file1 &&
15730                 error "swap of directory and file layouts should fail"
15731         $RUNAS $LFS swap_layouts $file1 $file2 &&
15732                 error "swap of file we cannot write should fail"
15733         $LFS swap_layouts $file1 $file3 &&
15734                 error "swap of file with different owner should fail"
15735         /bin/true # to clear error code
15736 }
15737 run_test 184b "Forbidden layout swap (will generate errors)"
15738
15739 test_184c() {
15740         local cmpn_arg=$(cmp -n 2>&1 | grep "invalid option")
15741         [ -n "$cmpn_arg" ] && skip_env "cmp does not support -n"
15742         check_swap_layouts_support
15743
15744         local dir0=$DIR/$tdir/$testnum
15745         mkdir -p $dir0 || error "creating dir $dir0"
15746
15747         local ref1=$dir0/ref1
15748         local ref2=$dir0/ref2
15749         local file1=$dir0/file1
15750         local file2=$dir0/file2
15751         # create a file large enough for the concurrent test
15752         dd if=/dev/urandom of=$ref1 bs=1M count=$((RANDOM % 50 + 20))
15753         dd if=/dev/urandom of=$ref2 bs=1M count=$((RANDOM % 50 + 20))
15754         echo "ref file size: ref1($(stat -c %s $ref1))," \
15755              "ref2($(stat -c %s $ref2))"
15756
15757         cp $ref2 $file2
15758         dd if=$ref1 of=$file1 bs=16k &
15759         local DD_PID=$!
15760
15761         # Make sure dd starts to copy file
15762         while [ ! -f $file1 ]; do sleep 0.1; done
15763
15764         $LFS swap_layouts $file1 $file2
15765         local rc=$?
15766         wait $DD_PID
15767         [[ $? == 0 ]] || error "concurrent write on $file1 failed"
15768         [[ $rc == 0 ]] || error "swap of $file1 and $file2 failed"
15769
15770         # how many bytes copied before swapping layout
15771         local copied=$(stat -c %s $file2)
15772         local remaining=$(stat -c %s $ref1)
15773         remaining=$((remaining - copied))
15774         echo "Copied $copied bytes before swapping layout..."
15775
15776         cmp -n $copied $file1 $ref2 | grep differ &&
15777                 error "Content mismatch [0, $copied) of ref2 and file1"
15778         cmp -n $copied $file2 $ref1 ||
15779                 error "Content mismatch [0, $copied) of ref1 and file2"
15780         cmp -i $copied:$copied -n $remaining $file1 $ref1 ||
15781                 error "Content mismatch [$copied, EOF) of ref1 and file1"
15782
15783         # clean up
15784         rm -f $ref1 $ref2 $file1 $file2
15785 }
15786 run_test 184c "Concurrent write and layout swap"
15787
15788 test_184d() {
15789         check_swap_layouts_support
15790         [ -z "$(which getfattr 2>/dev/null)" ] &&
15791                 skip_env "no getfattr command"
15792
15793         local file1=$DIR/$tdir/$tfile-1
15794         local file2=$DIR/$tdir/$tfile-2
15795         local file3=$DIR/$tdir/$tfile-3
15796         local lovea1
15797         local lovea2
15798
15799         mkdir -p $DIR/$tdir
15800         touch $file1 || error "create $file1 failed"
15801         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15802                 error "create $file2 failed"
15803         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15804                 error "create $file3 failed"
15805         lovea1=$(get_layout_param $file1)
15806
15807         $LFS swap_layouts $file2 $file3 ||
15808                 error "swap $file2 $file3 layouts failed"
15809         $LFS swap_layouts $file1 $file2 ||
15810                 error "swap $file1 $file2 layouts failed"
15811
15812         lovea2=$(get_layout_param $file2)
15813         echo "$lovea1"
15814         echo "$lovea2"
15815         [ "$lovea1" == "$lovea2" ] || error "lovea $lovea1 != $lovea2"
15816
15817         lovea1=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15818         [[ -z "$lovea1" ]] || error "$file1 shouldn't have lovea"
15819 }
15820 run_test 184d "allow stripeless layouts swap"
15821
15822 test_184e() {
15823         [[ $MDS1_VERSION -ge $(version_code 2.6.94) ]] ||
15824                 skip "Need MDS version at least 2.6.94"
15825         check_swap_layouts_support
15826         [ -z "$(which getfattr 2>/dev/null)" ] &&
15827                 skip_env "no getfattr command"
15828
15829         local file1=$DIR/$tdir/$tfile-1
15830         local file2=$DIR/$tdir/$tfile-2
15831         local file3=$DIR/$tdir/$tfile-3
15832         local lovea
15833
15834         mkdir -p $DIR/$tdir
15835         touch $file1 || error "create $file1 failed"
15836         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file2 ||
15837                 error "create $file2 failed"
15838         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE $file3 ||
15839                 error "create $file3 failed"
15840
15841         $LFS swap_layouts $file1 $file2 ||
15842                 error "swap $file1 $file2 layouts failed"
15843
15844         lovea=$(getfattr -n trusted.lov $file1 | grep ^trusted)
15845         [[ -z "$lovea" ]] || error "$file1 shouldn't have lovea"
15846
15847         echo 123 > $file1 || error "Should be able to write into $file1"
15848
15849         $LFS swap_layouts $file1 $file3 ||
15850                 error "swap $file1 $file3 layouts failed"
15851
15852         echo 123 > $file1 || error "Should be able to write into $file1"
15853
15854         rm -rf $file1 $file2 $file3
15855 }
15856 run_test 184e "Recreate layout after stripeless layout swaps"
15857
15858 test_184f() {
15859         # Create a file with name longer than sizeof(struct stat) ==
15860         # 144 to see if we can get chars from the file name to appear
15861         # in the returned striping. Note that 'f' == 0x66.
15862         local file=$(for ((i = 0; i < 200; i++)); do echo -n f; done)
15863
15864         mkdir -p $DIR/$tdir
15865         mcreate $DIR/$tdir/$file
15866         if lfs find --stripe-count 0x6666 $DIR/$tdir | grep $file; then
15867                 error "IOC_MDC_GETFILEINFO returned garbage striping"
15868         fi
15869 }
15870 run_test 184f "IOC_MDC_GETFILEINFO for files with long names but no striping"
15871
15872 test_185() { # LU-2441
15873         # LU-3553 - no volatile file support in old servers
15874         [[ $MDS1_VERSION -ge $(version_code 2.3.60) ]] ||
15875                 skip "Need MDS version at least 2.3.60"
15876
15877         mkdir -p $DIR/$tdir || error "creating dir $DIR/$tdir"
15878         touch $DIR/$tdir/spoo
15879         local mtime1=$(stat -c "%Y" $DIR/$tdir)
15880         local fid=$($MULTIOP $DIR/$tdir VFw4096c) ||
15881                 error "cannot create/write a volatile file"
15882         [ "$FILESET" == "" ] &&
15883         $CHECKSTAT -t file $MOUNT/.lustre/fid/$fid 2>/dev/null &&
15884                 error "FID is still valid after close"
15885
15886         multiop_bg_pause $DIR/$tdir vVw4096_c
15887         local multi_pid=$!
15888
15889         local OLD_IFS=$IFS
15890         IFS=":"
15891         local fidv=($fid)
15892         IFS=$OLD_IFS
15893         # assume that the next FID for this client is sequential, since stdout
15894         # is unfortunately eaten by multiop_bg_pause
15895         local n=$((${fidv[1]} + 1))
15896         local next_fid="${fidv[0]}:$(printf "0x%x" $n):${fidv[2]}"
15897         if [ "$FILESET" == "" ]; then
15898                 $CHECKSTAT -t file $MOUNT/.lustre/fid/$next_fid ||
15899                         error "FID is missing before close"
15900         fi
15901         kill -USR1 $multi_pid
15902         # 1 second delay, so if mtime change we will see it
15903         sleep 1
15904         local mtime2=$(stat -c "%Y" $DIR/$tdir)
15905         [[ $mtime1 == $mtime2 ]] || error "mtime has changed"
15906 }
15907 run_test 185 "Volatile file support"
15908
15909 function create_check_volatile() {
15910         local idx=$1
15911         local tgt
15912
15913         $MULTIOP $MOUNT/.lustre/fid V${idx}Fw4096_c >&/tmp/${tfile}.fid &
15914         local PID=$!
15915         sleep 1
15916         local FID=$(cat /tmp/${tfile}.fid)
15917         [ "$FID" == "" ] && error "can't get FID for volatile"
15918         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID || error "can't stat $FID"
15919         tgt=$($LFS getstripe -m $MOUNT/.lustre/fid/$FID)
15920         [ "$tgt" != "$idx" ] && error "wrong MDS $tgt, expected $idx"
15921         kill -USR1 $PID
15922         wait
15923         sleep 1
15924         cancel_lru_locks mdc # flush opencache
15925         $CHECKSTAT -t file $MOUNT/.lustre/fid/$FID && error "can stat $FID"
15926         return 0
15927 }
15928
15929 test_185a(){
15930         # LU-12516 - volatile creation via .lustre
15931         [[ $MDS1_VERSION -ge $(version_code 2.12.55) ]] ||
15932                 skip "Need MDS version at least 2.3.55"
15933
15934         create_check_volatile 0
15935         [ $MDSCOUNT -lt 2 ] && return 0
15936
15937         # DNE case
15938         create_check_volatile 1
15939
15940         return 0
15941 }
15942 run_test 185a "Volatile file creation in .lustre/fid/"
15943
15944 test_187a() {
15945         remote_mds_nodsh && skip "remote MDS with nodsh"
15946         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15947                 skip "Need MDS version at least 2.3.0"
15948
15949         local dir0=$DIR/$tdir/$testnum
15950         mkdir -p $dir0 || error "creating dir $dir0"
15951
15952         local file=$dir0/file1
15953         dd if=/dev/urandom of=$file count=10 bs=1M conv=fsync
15954         local dv1=$($LFS data_version $file)
15955         dd if=/dev/urandom of=$file seek=10 count=1 bs=1M conv=fsync
15956         local dv2=$($LFS data_version $file)
15957         [[ $dv1 != $dv2 ]] ||
15958                 error "data version did not change on write $dv1 == $dv2"
15959
15960         # clean up
15961         rm -f $file1
15962 }
15963 run_test 187a "Test data version change"
15964
15965 test_187b() {
15966         remote_mds_nodsh && skip "remote MDS with nodsh"
15967         [ $MDS1_VERSION -lt $(version_code 2.3.0) ] &&
15968                 skip "Need MDS version at least 2.3.0"
15969
15970         local dir0=$DIR/$tdir/$testnum
15971         mkdir -p $dir0 || error "creating dir $dir0"
15972
15973         declare -a DV=$($MULTIOP $dir0 Vw1000xYw1000xY | cut -f3 -d" ")
15974         [[ ${DV[0]} != ${DV[1]} ]] ||
15975                 error "data version did not change on write"\
15976                       " ${DV[0]} == ${DV[1]}"
15977
15978         # clean up
15979         rm -f $file1
15980 }
15981 run_test 187b "Test data version change on volatile file"
15982
15983 test_200() {
15984         [ $PARALLEL == "yes" ] && skip "skip parallel run"
15985         remote_mgs_nodsh && skip "remote MGS with nodsh"
15986         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
15987
15988         local POOL=${POOL:-cea1}
15989         local POOL_ROOT=${POOL_ROOT:-$DIR/d200.pools}
15990         local POOL_DIR_NAME=${POOL_DIR_NAME:-dir_tst}
15991         # Pool OST targets
15992         local first_ost=0
15993         local last_ost=$(($OSTCOUNT - 1))
15994         local ost_step=2
15995         local ost_list=$(seq $first_ost $ost_step $last_ost)
15996         local ost_range="$first_ost $last_ost $ost_step"
15997         local test_path=$POOL_ROOT/$POOL_DIR_NAME
15998         local file_dir=$POOL_ROOT/file_tst
15999         local subdir=$test_path/subdir
16000         local rc=0
16001
16002         while : ; do
16003                 # former test_200a test_200b
16004                 pool_add $POOL                          || { rc=$? ; break; }
16005                 pool_add_targets  $POOL $ost_range      || { rc=$? ; break; }
16006                 # former test_200c test_200d
16007                 mkdir -p $test_path
16008                 pool_set_dir      $POOL $test_path      || { rc=$? ; break; }
16009                 pool_check_dir    $POOL $test_path      || { rc=$? ; break; }
16010                 mkdir -p $subdir
16011                 pool_check_dir    $POOL $subdir         || { rc=$? ; break; }
16012                 pool_dir_rel_path $POOL $POOL_DIR_NAME $POOL_ROOT \
16013                                                         || { rc=$? ; break; }
16014                 # former test_200e test_200f
16015                 local files=$((OSTCOUNT*3))
16016                 pool_alloc_files  $POOL $test_path $files "$ost_list" \
16017                                                         || { rc=$? ; break; }
16018                 pool_create_files $POOL $file_dir $files "$ost_list" \
16019                                                         || { rc=$? ; break; }
16020                 # former test_200g test_200h
16021                 pool_lfs_df $POOL                       || { rc=$? ; break; }
16022                 pool_file_rel_path $POOL $test_path     || { rc=$? ; break; }
16023
16024                 # former test_201a test_201b test_201c
16025                 pool_remove_first_target $POOL          || { rc=$? ; break; }
16026
16027                 local f=$test_path/$tfile
16028                 pool_remove_all_targets $POOL $f        || { rc=$? ; break; }
16029                 pool_remove $POOL $f                    || { rc=$? ; break; }
16030                 break
16031         done
16032
16033         destroy_test_pools
16034
16035         return $rc
16036 }
16037 run_test 200 "OST pools"
16038
16039 # usage: default_attr <count | size | offset>
16040 default_attr() {
16041         $LCTL get_param -n lov.$FSNAME-clilov-\*.stripe${1}
16042 }
16043
16044 # usage: check_default_stripe_attr
16045 check_default_stripe_attr() {
16046         ACTUAL=$($LFS getstripe $* $DIR/$tdir)
16047         case $1 in
16048         --stripe-count|-c)
16049                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr count);;
16050         --stripe-size|-S)
16051                 [ -n "$2" ] && EXPECTED=0 || EXPECTED=$(default_attr size);;
16052         --stripe-index|-i)
16053                 EXPECTED=-1;;
16054         *)
16055                 error "unknown getstripe attr '$1'"
16056         esac
16057
16058         [ $ACTUAL == $EXPECTED ] ||
16059                 error "$DIR/$tdir has $1 '$ACTUAL', not '$EXPECTED'"
16060 }
16061
16062 test_204a() {
16063         test_mkdir $DIR/$tdir
16064         $LFS setstripe --stripe-count 0 --stripe-size 0 --stripe-index -1 $DIR/$tdir
16065
16066         check_default_stripe_attr --stripe-count
16067         check_default_stripe_attr --stripe-size
16068         check_default_stripe_attr --stripe-index
16069 }
16070 run_test 204a "Print default stripe attributes"
16071
16072 test_204b() {
16073         test_mkdir $DIR/$tdir
16074         $LFS setstripe --stripe-count 1 $DIR/$tdir
16075
16076         check_default_stripe_attr --stripe-size
16077         check_default_stripe_attr --stripe-index
16078 }
16079 run_test 204b "Print default stripe size and offset"
16080
16081 test_204c() {
16082         test_mkdir $DIR/$tdir
16083         $LFS setstripe --stripe-size 65536 $DIR/$tdir
16084
16085         check_default_stripe_attr --stripe-count
16086         check_default_stripe_attr --stripe-index
16087 }
16088 run_test 204c "Print default stripe count and offset"
16089
16090 test_204d() {
16091         test_mkdir $DIR/$tdir
16092         $LFS setstripe --stripe-index 0 $DIR/$tdir
16093
16094         check_default_stripe_attr --stripe-count
16095         check_default_stripe_attr --stripe-size
16096 }
16097 run_test 204d "Print default stripe count and size"
16098
16099 test_204e() {
16100         test_mkdir $DIR/$tdir
16101         $LFS setstripe -d $DIR/$tdir
16102
16103         check_default_stripe_attr --stripe-count --raw
16104         check_default_stripe_attr --stripe-size --raw
16105         check_default_stripe_attr --stripe-index --raw
16106 }
16107 run_test 204e "Print raw stripe attributes"
16108
16109 test_204f() {
16110         test_mkdir $DIR/$tdir
16111         $LFS setstripe --stripe-count 1 $DIR/$tdir
16112
16113         check_default_stripe_attr --stripe-size --raw
16114         check_default_stripe_attr --stripe-index --raw
16115 }
16116 run_test 204f "Print raw stripe size and offset"
16117
16118 test_204g() {
16119         test_mkdir $DIR/$tdir
16120         $LFS setstripe --stripe-size 65536 $DIR/$tdir
16121
16122         check_default_stripe_attr --stripe-count --raw
16123         check_default_stripe_attr --stripe-index --raw
16124 }
16125 run_test 204g "Print raw stripe count and offset"
16126
16127 test_204h() {
16128         test_mkdir $DIR/$tdir
16129         $LFS setstripe --stripe-index 0 $DIR/$tdir
16130
16131         check_default_stripe_attr --stripe-count --raw
16132         check_default_stripe_attr --stripe-size --raw
16133 }
16134 run_test 204h "Print raw stripe count and size"
16135
16136 # Figure out which job scheduler is being used, if any,
16137 # or use a fake one
16138 if [ -n "$SLURM_JOB_ID" ]; then # SLURM
16139         JOBENV=SLURM_JOB_ID
16140 elif [ -n "$LSB_JOBID" ]; then # Load Sharing Facility
16141         JOBENV=LSB_JOBID
16142 elif [ -n "$PBS_JOBID" ]; then # PBS/Maui/Moab
16143         JOBENV=PBS_JOBID
16144 elif [ -n "$LOADL_STEPID" ]; then # LoadLeveller
16145         JOBENV=LOADL_STEP_ID
16146 elif [ -n "$JOB_ID" ]; then # Sun Grid Engine
16147         JOBENV=JOB_ID
16148 else
16149         $LCTL list_param jobid_name > /dev/null 2>&1
16150         if [ $? -eq 0 ]; then
16151                 JOBENV=nodelocal
16152         else
16153                 JOBENV=FAKE_JOBID
16154         fi
16155 fi
16156 LUSTRE_JOBID_SIZE=31 # plus NUL terminator
16157
16158 verify_jobstats() {
16159         local cmd=($1)
16160         shift
16161         local facets="$@"
16162
16163 # we don't really need to clear the stats for this test to work, since each
16164 # command has a unique jobid, but it makes debugging easier if needed.
16165 #       for facet in $facets; do
16166 #               local dev=$(convert_facet2label $facet)
16167 #               # clear old jobstats
16168 #               do_facet $facet lctl set_param *.$dev.job_stats="clear"
16169 #       done
16170
16171         # use a new JobID for each test, or we might see an old one
16172         [ "$JOBENV" = "FAKE_JOBID" ] &&
16173                 FAKE_JOBID=id.$testnum.$(basename ${cmd[0]}).$RANDOM
16174
16175         JOBVAL=${!JOBENV:0:$LUSTRE_JOBID_SIZE}
16176
16177         [ "$JOBENV" = "nodelocal" ] && {
16178                 FAKE_JOBID=id.$testnum.%e.$RANDOM
16179                 $LCTL set_param jobid_name=$FAKE_JOBID
16180                 JOBVAL=${FAKE_JOBID/\%e/$(basename ${cmd[0]})}
16181         }
16182
16183         log "Test: ${cmd[*]}"
16184         log "Using JobID environment $($LCTL get_param -n jobid_var)=$JOBVAL"
16185
16186         if [ $JOBENV = "FAKE_JOBID" ]; then
16187                 FAKE_JOBID=$JOBVAL ${cmd[*]}
16188         else
16189                 ${cmd[*]}
16190         fi
16191
16192         # all files are created on OST0000
16193         for facet in $facets; do
16194                 local stats="*.$(convert_facet2label $facet).job_stats"
16195
16196                 # strip out libtool wrappers for in-tree executables
16197                 if [ $(do_facet $facet lctl get_param $stats |
16198                        sed -e 's/\.lt-/./' | grep -c $JOBVAL) -ne 1 ]; then
16199                         do_facet $facet lctl get_param $stats
16200                         error "No jobstats for $JOBVAL found on $facet::$stats"
16201                 fi
16202         done
16203 }
16204
16205 jobstats_set() {
16206         local new_jobenv=$1
16207
16208         set_persistent_param_and_check client "jobid_var" \
16209                 "$FSNAME.sys.jobid_var" $new_jobenv
16210 }
16211
16212 test_205a() { # Job stats
16213         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16214         [[ $MDS1_VERSION -ge $(version_code 2.7.1) ]] ||
16215                 skip "Need MDS version with at least 2.7.1"
16216         remote_mgs_nodsh && skip "remote MGS with nodsh"
16217         remote_mds_nodsh && skip "remote MDS with nodsh"
16218         remote_ost_nodsh && skip "remote OST with nodsh"
16219         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep jobstats)" ] &&
16220                 skip "Server doesn't support jobstats"
16221         [[ $JOBID_VAR = disable ]] && skip_env "jobstats is disabled"
16222
16223         local old_jobenv=$($LCTL get_param -n jobid_var)
16224         [ $old_jobenv != $JOBENV ] && jobstats_set $JOBENV
16225
16226         if [[ $PERM_CMD == *"set_param -P"* ]]; then
16227                 stack_trap "do_facet mgs $PERM_CMD jobid_var=$old_jobenv" EXIT
16228         else
16229                 stack_trap "do_facet mgs $PERM_CMD \
16230                         $FSNAME.sys.jobid_var=$old_jobenv" EXIT
16231         fi
16232         changelog_register
16233
16234         local old_interval=$(do_facet $SINGLEMDS lctl get_param -n \
16235                                 mdt.*.job_cleanup_interval | head -n 1)
16236         local new_interval=5
16237         do_facet $SINGLEMDS \
16238                 $LCTL set_param mdt.*.job_cleanup_interval=$new_interval
16239         stack_trap "do_facet $SINGLEMDS \
16240                 $LCTL set_param mdt.*.job_cleanup_interval=$old_interval" EXIT
16241         local start=$SECONDS
16242
16243         local cmd
16244         # mkdir
16245         cmd="mkdir $DIR/$tdir"
16246         verify_jobstats "$cmd" "$SINGLEMDS"
16247         # rmdir
16248         cmd="rmdir $DIR/$tdir"
16249         verify_jobstats "$cmd" "$SINGLEMDS"
16250         # mkdir on secondary MDT
16251         if [ $MDSCOUNT -gt 1 ]; then
16252                 cmd="lfs mkdir -i 1 $DIR/$tdir.remote"
16253                 verify_jobstats "$cmd" "mds2"
16254         fi
16255         # mknod
16256         cmd="mknod $DIR/$tfile c 1 3"
16257         verify_jobstats "$cmd" "$SINGLEMDS"
16258         # unlink
16259         cmd="rm -f $DIR/$tfile"
16260         verify_jobstats "$cmd" "$SINGLEMDS"
16261         # create all files on OST0000 so verify_jobstats can find OST stats
16262         # open & close
16263         cmd="$LFS setstripe -i 0 -c 1 $DIR/$tfile"
16264         verify_jobstats "$cmd" "$SINGLEMDS"
16265         # setattr
16266         cmd="touch $DIR/$tfile"
16267         verify_jobstats "$cmd" "$SINGLEMDS ost1"
16268         # write
16269         cmd="dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=sync"
16270         verify_jobstats "$cmd" "ost1"
16271         # read
16272         cancel_lru_locks osc
16273         cmd="dd if=$DIR/$tfile of=/dev/null bs=1M count=1 iflag=direct"
16274         verify_jobstats "$cmd" "ost1"
16275         # truncate
16276         cmd="$TRUNCATE $DIR/$tfile 0"
16277         verify_jobstats "$cmd" "$SINGLEMDS ost1"
16278         # rename
16279         cmd="mv -f $DIR/$tfile $DIR/$tdir.rename"
16280         verify_jobstats "$cmd" "$SINGLEMDS"
16281         # jobstats expiry - sleep until old stats should be expired
16282         local left=$((new_interval + 5 - (SECONDS - start)))
16283         [ $left -ge 0 ] && wait_update_facet $SINGLEMDS \
16284                 "lctl get_param *.*.job_stats | grep -c 'job_id.*mkdir'" \
16285                         "0" $left
16286         cmd="mkdir $DIR/$tdir.expire"
16287         verify_jobstats "$cmd" "$SINGLEMDS"
16288         [ $(do_facet $SINGLEMDS lctl get_param *.*.job_stats |
16289             grep -c "job_id.*mkdir") -gt 1 ] && error "old jobstats not expired"
16290
16291         # Ensure that jobid are present in changelog (if supported by MDS)
16292         if [ $MDS1_VERSION -ge $(version_code 2.6.52) ];then
16293                 changelog_dump | tail -10
16294                 jobids=$(changelog_dump | tail -9 | grep -c "j=")
16295                 [ $jobids -eq 9 ] ||
16296                         error "Wrong changelog jobid count $jobids != 9"
16297
16298                 # LU-5862
16299                 JOBENV="disable"
16300                 jobstats_set $JOBENV
16301                 touch $DIR/$tfile
16302                 changelog_dump | grep $tfile
16303                 jobids=$(changelog_dump | grep $tfile | tail -1 | grep -c "j=")
16304                 [ $jobids -eq 0 ] ||
16305                         error "Unexpected jobids when jobid_var=$JOBENV"
16306         fi
16307
16308         lctl set_param jobid_var=USER jobid_name="S.%j.%e.%u.%h.E"
16309         JOBENV="JOBCOMPLEX"
16310         JOBCOMPLEX="S.$USER.touch.$(id -u).$(hostname).E"
16311
16312         verify_jobstats "touch $DIR/$tfile" $SINGLEMDS
16313 }
16314 run_test 205a "Verify job stats"
16315
16316 # LU-13117
16317 test_205b() {
16318         $LCTL set_param jobid_var=USER jobid_name="%e.%u"
16319         env -i USERTESTJOBSTATS=foolish touch $DIR/$tfile.1
16320         do_facet $SINGLEMDS $LCTL get_param mdt.*.job_stats |
16321                 grep job_id: | grep foolish &&
16322                         error "Unexpected jobid found"
16323         true
16324 }
16325 run_test 205b "Verify job stats jobid parsing"
16326
16327 # LU-1480, LU-1773 and LU-1657
16328 test_206() {
16329         mkdir -p $DIR/$tdir
16330         $LFS setstripe -c -1 $DIR/$tdir
16331 #define OBD_FAIL_LOV_INIT 0x1403
16332         $LCTL set_param fail_loc=0xa0001403
16333         $LCTL set_param fail_val=1
16334         touch $DIR/$tdir/$tfile || true
16335 }
16336 run_test 206 "fail lov_init_raid0() doesn't lbug"
16337
16338 test_207a() {
16339         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
16340         local fsz=`stat -c %s $DIR/$tfile`
16341         cancel_lru_locks mdc
16342
16343         # do not return layout in getattr intent
16344 #define OBD_FAIL_MDS_NO_LL_GETATTR 0x170
16345         $LCTL set_param fail_loc=0x170
16346         local sz=`stat -c %s $DIR/$tfile`
16347
16348         [ $fsz -eq $sz ] || error "file size expected $fsz, actual $sz"
16349
16350         rm -rf $DIR/$tfile
16351 }
16352 run_test 207a "can refresh layout at glimpse"
16353
16354 test_207b() {
16355         dd if=/dev/zero of=$DIR/$tfile bs=4k count=$((RANDOM%10+1))
16356         local cksum=`md5sum $DIR/$tfile`
16357         local fsz=`stat -c %s $DIR/$tfile`
16358         cancel_lru_locks mdc
16359         cancel_lru_locks osc
16360
16361         # do not return layout in getattr intent
16362 #define OBD_FAIL_MDS_NO_LL_OPEN 0x171
16363         $LCTL set_param fail_loc=0x171
16364
16365         # it will refresh layout after the file is opened but before read issues
16366         echo checksum is "$cksum"
16367         echo "$cksum" |md5sum -c --quiet || error "file differs"
16368
16369         rm -rf $DIR/$tfile
16370 }
16371 run_test 207b "can refresh layout at open"
16372
16373 test_208() {
16374         # FIXME: in this test suite, only RD lease is used. This is okay
16375         # for now as only exclusive open is supported. After generic lease
16376         # is done, this test suite should be revised. - Jinshan
16377
16378         remote_mds_nodsh && skip "remote MDS with nodsh"
16379         [[ $MDS1_VERSION -ge $(version_code 2.4.52) ]] ||
16380                 skip "Need MDS version at least 2.4.52"
16381
16382         echo "==== test 1: verify get lease work"
16383         $MULTIOP $DIR/$tfile oO_CREAT:O_RDWR:eRE+eU || error "get lease error"
16384
16385         echo "==== test 2: verify lease can be broken by upcoming open"
16386         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
16387         local PID=$!
16388         sleep 1
16389
16390         $MULTIOP $DIR/$tfile oO_RDONLY:c
16391         kill -USR1 $PID && wait $PID || error "break lease error"
16392
16393         echo "==== test 3: verify lease can't be granted if an open already exists"
16394         $MULTIOP $DIR/$tfile oO_RDONLY:_c &
16395         local PID=$!
16396         sleep 1
16397
16398         $MULTIOP $DIR/$tfile oO_RDONLY:eReUc && error "apply lease should fail"
16399         kill -USR1 $PID && wait $PID || error "open file error"
16400
16401         echo "==== test 4: lease can sustain over recovery"
16402         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E+eUc &
16403         PID=$!
16404         sleep 1
16405
16406         fail mds1
16407
16408         kill -USR1 $PID && wait $PID || error "lease broken over recovery"
16409
16410         echo "==== test 5: lease broken can't be regained by replay"
16411         $MULTIOP $DIR/$tfile oO_RDONLY:eR_E-eUc &
16412         PID=$!
16413         sleep 1
16414
16415         # open file to break lease and then recovery
16416         $MULTIOP $DIR/$tfile oO_RDWR:c || error "open file error"
16417         fail mds1
16418
16419         kill -USR1 $PID && wait $PID || error "lease not broken over recovery"
16420
16421         rm -f $DIR/$tfile
16422 }
16423 run_test 208 "Exclusive open"
16424
16425 test_209() {
16426         [ -z "$(lctl get_param -n mdc.*.connect_flags | grep disp_stripe)" ] &&
16427                 skip_env "must have disp_stripe"
16428
16429         touch $DIR/$tfile
16430         sync; sleep 5; sync;
16431
16432         echo 3 > /proc/sys/vm/drop_caches
16433         req_before=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16434
16435         # open/close 500 times
16436         for i in $(seq 500); do
16437                 cat $DIR/$tfile
16438         done
16439
16440         echo 3 > /proc/sys/vm/drop_caches
16441         req_after=$(awk '/ptlrpc_cache / { print $2 }' /proc/slabinfo)
16442
16443         echo "before: $req_before, after: $req_after"
16444         [ $((req_after - req_before)) -ge 300 ] &&
16445                 error "open/close requests are not freed"
16446         return 0
16447 }
16448 run_test 209 "read-only open/close requests should be freed promptly"
16449
16450 test_212() {
16451         size=`date +%s`
16452         size=$((size % 8192 + 1))
16453         dd if=/dev/urandom of=$DIR/f212 bs=1k count=$size
16454         sendfile $DIR/f212 $DIR/f212.xyz || error "sendfile wrong"
16455         rm -f $DIR/f212 $DIR/f212.xyz
16456 }
16457 run_test 212 "Sendfile test ============================================"
16458
16459 test_213() {
16460         dd if=/dev/zero of=$DIR/$tfile bs=4k count=4
16461         cancel_lru_locks osc
16462         lctl set_param fail_loc=0x8000040f
16463         # generate a read lock
16464         cat $DIR/$tfile > /dev/null
16465         # write to the file, it will try to cancel the above read lock.
16466         cat /etc/hosts >> $DIR/$tfile
16467 }
16468 run_test 213 "OSC lock completion and cancel race don't crash - bug 18829"
16469
16470 test_214() { # for bug 20133
16471         mkdir -p $DIR/$tdir/d214c || error "mkdir $DIR/$tdir/d214c failed"
16472         for (( i=0; i < 340; i++ )) ; do
16473                 touch $DIR/$tdir/d214c/a$i
16474         done
16475
16476         ls -l $DIR/$tdir || error "ls -l $DIR/d214p failed"
16477         mv $DIR/$tdir/d214c $DIR/ || error "mv $DIR/d214p/d214c $DIR/ failed"
16478         ls $DIR/d214c || error "ls $DIR/d214c failed"
16479         rm -rf $DIR/$tdir || error "rm -rf $DIR/d214* failed"
16480         rm -rf $DIR/d214* || error "rm -rf $DIR/d214* failed"
16481 }
16482 run_test 214 "hash-indexed directory test - bug 20133"
16483
16484 # having "abc" as 1st arg, creates $TMP/lnet_abc.out and $TMP/lnet_abc.sys
16485 create_lnet_proc_files() {
16486         lctl get_param -n $1 >$TMP/lnet_$1.sys || error "cannot read lnet.$1"
16487 }
16488
16489 # counterpart of create_lnet_proc_files
16490 remove_lnet_proc_files() {
16491         rm -f $TMP/lnet_$1.sys
16492 }
16493
16494 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16495 # 3rd arg as regexp for body
16496 check_lnet_proc_stats() {
16497         local l=$(cat "$TMP/lnet_$1" |wc -l)
16498         [ $l = 1 ] || (cat "$TMP/lnet_$1" && error "$2 is not of 1 line: $l")
16499
16500         grep -E "$3" "$TMP/lnet_$1" || (cat "$TMP/lnet_$1" && error "$2 misformatted")
16501 }
16502
16503 # uses 1st arg as trailing part of filename, 2nd arg as description for reports,
16504 # 3rd arg as regexp for body, 4th arg as regexp for 1st line, 5th arg is
16505 # optional and can be regexp for 2nd line (lnet.routes case)
16506 check_lnet_proc_entry() {
16507         local blp=2          # blp stands for 'position of 1st line of body'
16508         [ -z "$5" ] || blp=3 # lnet.routes case
16509
16510         local l=$(cat "$TMP/lnet_$1" |wc -l)
16511         # subtracting one from $blp because the body can be empty
16512         [ "$l" -ge "$(($blp - 1))" ] || (cat "$TMP/lnet_$1" && error "$2 is too short: $l")
16513
16514         sed -n '1 p' "$TMP/lnet_$1" |grep -E "$4" >/dev/null ||
16515                 (cat "$TMP/lnet_$1" && error "1st line of $2 misformatted")
16516
16517         [ "$5" = "" ] || sed -n '2 p' "$TMP/lnet_$1" |grep -E "$5" >/dev/null ||
16518                 (cat "$TMP/lnet_$1" && error "2nd line of $2 misformatted")
16519
16520         # bail out if any unexpected line happened
16521         sed -n "$blp p" "$TMP/lnet_$1" | grep -Ev "$3"
16522         [ "$?" != 0 ] || error "$2 misformatted"
16523 }
16524
16525 test_215() { # for bugs 18102, 21079, 21517
16526         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16527
16528         local N='(0|[1-9][0-9]*)'       # non-negative numeric
16529         local P='[1-9][0-9]*'           # positive numeric
16530         local I='(0|-?[1-9][0-9]*|NA)'  # any numeric (0 | >0 | <0) or NA if no value
16531         local NET='[a-z][a-z0-9]*'      # LNET net like o2ib2
16532         local ADDR='[0-9.]+'            # LNET addr like 10.0.0.1
16533         local NID="$ADDR@$NET"          # LNET nid like 10.0.0.1@o2ib2
16534
16535         local L1 # regexp for 1st line
16536         local L2 # regexp for 2nd line (optional)
16537         local BR # regexp for the rest (body)
16538
16539         # lnet.stats should look as 11 space-separated non-negative numerics
16540         BR="^$N $N $N $N $N $N $N $N $N $N $N$"
16541         create_lnet_proc_files "stats"
16542         check_lnet_proc_stats "stats.sys" "lnet.stats" "$BR"
16543         remove_lnet_proc_files "stats"
16544
16545         # lnet.routes should look like this:
16546         # Routing disabled/enabled
16547         # net hops priority state router
16548         # where net is a string like tcp0, hops > 0, priority >= 0,
16549         # state is up/down,
16550         # router is a string like 192.168.1.1@tcp2
16551         L1="^Routing (disabled|enabled)$"
16552         L2="^net +hops +priority +state +router$"
16553         BR="^$NET +$N +(0|1) +(up|down) +$NID$"
16554         create_lnet_proc_files "routes"
16555         check_lnet_proc_entry "routes.sys" "lnet.routes" "$BR" "$L1" "$L2"
16556         remove_lnet_proc_files "routes"
16557
16558         # lnet.routers should look like this:
16559         # ref rtr_ref alive_cnt state last_ping ping_sent deadline down_ni router
16560         # where ref > 0, rtr_ref > 0, alive_cnt >= 0, state is up/down,
16561         # last_ping >= 0, ping_sent is boolean (0/1), deadline and down_ni are
16562         # numeric (0 or >0 or <0), router is a string like 192.168.1.1@tcp2
16563         L1="^ref +rtr_ref +alive +router$"
16564         BR="^$P +$P +(up|down) +$NID$"
16565         create_lnet_proc_files "routers"
16566         check_lnet_proc_entry "routers.sys" "lnet.routers" "$BR" "$L1"
16567         remove_lnet_proc_files "routers"
16568
16569         # lnet.peers should look like this:
16570         # nid refs state last max rtr min tx min queue
16571         # where nid is a string like 192.168.1.1@tcp2, refs > 0,
16572         # state is up/down/NA, max >= 0. last, rtr, min, tx, min are
16573         # numeric (0 or >0 or <0), queue >= 0.
16574         L1="^nid +refs +state +last +max +rtr +min +tx +min +queue$"
16575         BR="^$NID +$P +(up|down|NA) +$I +$N +$I +$I +$I +$I +$N$"
16576         create_lnet_proc_files "peers"
16577         check_lnet_proc_entry "peers.sys" "lnet.peers" "$BR" "$L1"
16578         remove_lnet_proc_files "peers"
16579
16580         # lnet.buffers  should look like this:
16581         # pages count credits min
16582         # where pages >=0, count >=0, credits and min are numeric (0 or >0 or <0)
16583         L1="^pages +count +credits +min$"
16584         BR="^ +$N +$N +$I +$I$"
16585         create_lnet_proc_files "buffers"
16586         check_lnet_proc_entry "buffers.sys" "lnet.buffers" "$BR" "$L1"
16587         remove_lnet_proc_files "buffers"
16588
16589         # lnet.nis should look like this:
16590         # nid status alive refs peer rtr max tx min
16591         # where nid is a string like 192.168.1.1@tcp2, status is up/down,
16592         # alive is numeric (0 or >0 or <0), refs >= 0, peer >= 0,
16593         # rtr >= 0, max >=0, tx and min are numeric (0 or >0 or <0).
16594         L1="^nid +status +alive +refs +peer +rtr +max +tx +min$"
16595         BR="^$NID +(up|down) +$I +$N +$N +$N +$N +$I +$I$"
16596         create_lnet_proc_files "nis"
16597         check_lnet_proc_entry "nis.sys" "lnet.nis" "$BR" "$L1"
16598         remove_lnet_proc_files "nis"
16599
16600         # can we successfully write to lnet.stats?
16601         lctl set_param -n stats=0 || error "cannot write to lnet.stats"
16602 }
16603 run_test 215 "lnet exists and has proper content - bugs 18102, 21079, 21517"
16604
16605 test_216() { # bug 20317
16606         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16607         remote_ost_nodsh && skip "remote OST with nodsh"
16608
16609         local node
16610         local facets=$(get_facets OST)
16611         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16612
16613         save_lustre_params client "osc.*.contention_seconds" > $p
16614         save_lustre_params $facets \
16615                 "ldlm.namespaces.filter-*.max_nolock_bytes" >> $p
16616         save_lustre_params $facets \
16617                 "ldlm.namespaces.filter-*.contended_locks" >> $p
16618         save_lustre_params $facets \
16619                 "ldlm.namespaces.filter-*.contention_seconds" >> $p
16620         clear_stats osc.*.osc_stats
16621
16622         # agressive lockless i/o settings
16623         do_nodes $(comma_list $(osts_nodes)) \
16624                 "lctl set_param -n ldlm.namespaces.*.max_nolock_bytes=2000000 \
16625                         ldlm.namespaces.filter-*.contended_locks=0 \
16626                         ldlm.namespaces.filter-*.contention_seconds=60"
16627         lctl set_param -n osc.*.contention_seconds=60
16628
16629         $DIRECTIO write $DIR/$tfile 0 10 4096
16630         $CHECKSTAT -s 40960 $DIR/$tfile
16631
16632         # disable lockless i/o
16633         do_nodes $(comma_list $(osts_nodes)) \
16634                 "lctl set_param -n ldlm.namespaces.filter-*.max_nolock_bytes=0 \
16635                         ldlm.namespaces.filter-*.contended_locks=32 \
16636                         ldlm.namespaces.filter-*.contention_seconds=0"
16637         lctl set_param -n osc.*.contention_seconds=0
16638         clear_stats osc.*.osc_stats
16639
16640         dd if=/dev/zero of=$DIR/$tfile count=0
16641         $CHECKSTAT -s 0 $DIR/$tfile
16642
16643         restore_lustre_params <$p
16644         rm -f $p
16645         rm $DIR/$tfile
16646 }
16647 run_test 216 "check lockless direct write updates file size and kms correctly"
16648
16649 test_217() { # bug 22430
16650         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16651
16652         local node
16653         local nid
16654
16655         for node in $(nodes_list); do
16656                 nid=$(host_nids_address $node $NETTYPE)
16657                 if [[ $nid = *-* ]] ; then
16658                         echo "lctl ping $(h2nettype $nid)"
16659                         lctl ping $(h2nettype $nid)
16660                 else
16661                         echo "skipping $node (no hyphen detected)"
16662                 fi
16663         done
16664 }
16665 run_test 217 "check lctl ping for hostnames with hiphen ('-')"
16666
16667 test_218() {
16668        # do directio so as not to populate the page cache
16669        log "creating a 10 Mb file"
16670        $MULTIOP $DIR/$tfile oO_CREAT:O_DIRECT:O_RDWR:w$((10*1048576))c || error "multiop failed while creating a file"
16671        log "starting reads"
16672        dd if=$DIR/$tfile of=/dev/null bs=4096 &
16673        log "truncating the file"
16674        $MULTIOP $DIR/$tfile oO_TRUNC:c || error "multiop failed while truncating the file"
16675        log "killing dd"
16676        kill %+ || true # reads might have finished
16677        echo "wait until dd is finished"
16678        wait
16679        log "removing the temporary file"
16680        rm -rf $DIR/$tfile || error "tmp file removal failed"
16681 }
16682 run_test 218 "parallel read and truncate should not deadlock"
16683
16684 test_219() {
16685         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16686
16687         # write one partial page
16688         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1
16689         # set no grant so vvp_io_commit_write will do sync write
16690         $LCTL set_param fail_loc=0x411
16691         # write a full page at the end of file
16692         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=1 conv=notrunc
16693
16694         $LCTL set_param fail_loc=0
16695         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 seek=3
16696         $LCTL set_param fail_loc=0x411
16697         dd if=/dev/zero of=$DIR/$tfile bs=1024 count=1 seek=2 conv=notrunc
16698
16699         # LU-4201
16700         dd if=/dev/zero of=$DIR/$tfile-2 bs=1024 count=1
16701         $CHECKSTAT -s 1024 $DIR/$tfile-2 || error "checkstat wrong size"
16702 }
16703 run_test 219 "LU-394: Write partial won't cause uncontiguous pages vec at LND"
16704
16705 test_220() { #LU-325
16706         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16707         remote_ost_nodsh && skip "remote OST with nodsh"
16708         remote_mds_nodsh && skip "remote MDS with nodsh"
16709         remote_mgs_nodsh && skip "remote MGS with nodsh"
16710
16711         local OSTIDX=0
16712
16713         # create on MDT0000 so the last_id and next_id are correct
16714         mkdir $DIR/$tdir
16715         local OST=$($LFS df $DIR | awk '/OST:'$OSTIDX'/ { print $1 }')
16716         OST=${OST%_UUID}
16717
16718         # on the mdt's osc
16719         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $OST)
16720         local last_id=$(do_facet $SINGLEMDS lctl get_param -n \
16721                         osp.$mdtosc_proc1.prealloc_last_id)
16722         local next_id=$(do_facet $SINGLEMDS lctl get_param -n \
16723                         osp.$mdtosc_proc1.prealloc_next_id)
16724
16725         $LFS df -i
16726
16727         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=-1
16728         #define OBD_FAIL_OST_ENOINO              0x229
16729         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0x229
16730         create_pool $FSNAME.$TESTNAME || return 1
16731         do_facet mgs $LCTL pool_add $FSNAME.$TESTNAME $OST || return 2
16732
16733         $LFS setstripe $DIR/$tdir -i $OSTIDX -c 1 -p $FSNAME.$TESTNAME
16734
16735         MDSOBJS=$((last_id - next_id))
16736         echo "preallocated objects on MDS is $MDSOBJS" "($last_id - $next_id)"
16737
16738         blocks=$($LFS df $MOUNT | awk '($1 == '$OSTIDX') { print $4 }')
16739         echo "OST still has $count kbytes free"
16740
16741         echo "create $MDSOBJS files @next_id..."
16742         createmany -o $DIR/$tdir/f $MDSOBJS || return 3
16743
16744         local last_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16745                         osp.$mdtosc_proc1.prealloc_last_id)
16746         local next_id2=$(do_facet mds${MDSIDX} lctl get_param -n \
16747                         osp.$mdtosc_proc1.prealloc_next_id)
16748
16749         echo "after creation, last_id=$last_id2, next_id=$next_id2"
16750         $LFS df -i
16751
16752         echo "cleanup..."
16753
16754         do_facet ost$((OSTIDX + 1)) lctl set_param fail_val=0
16755         do_facet ost$((OSTIDX + 1)) lctl set_param fail_loc=0
16756
16757         do_facet mgs $LCTL pool_remove $FSNAME.$TESTNAME $OST ||
16758                 error "$LCTL pool_remove $FSNAME.$TESTNAME $OST failed"
16759         do_facet mgs $LCTL pool_destroy $FSNAME.$TESTNAME ||
16760                 error "$LCTL pool_destroy $FSNAME.$TESTNAME failed"
16761         echo "unlink $MDSOBJS files @$next_id..."
16762         unlinkmany $DIR/$tdir/f $MDSOBJS || error "unlinkmany failed"
16763 }
16764 run_test 220 "preallocated MDS objects still used if ENOSPC from OST"
16765
16766 test_221() {
16767         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16768
16769         dd if=`which date` of=$MOUNT/date oflag=sync
16770         chmod +x $MOUNT/date
16771
16772         #define OBD_FAIL_LLITE_FAULT_TRUNC_RACE  0x1401
16773         $LCTL set_param fail_loc=0x80001401
16774
16775         $MOUNT/date > /dev/null
16776         rm -f $MOUNT/date
16777 }
16778 run_test 221 "make sure fault and truncate race to not cause OOM"
16779
16780 test_222a () {
16781         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16782
16783         rm -rf $DIR/$tdir
16784         test_mkdir $DIR/$tdir
16785         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16786         createmany -o $DIR/$tdir/$tfile 10
16787         cancel_lru_locks mdc
16788         cancel_lru_locks osc
16789         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16790         $LCTL set_param fail_loc=0x31a
16791         ls -l $DIR/$tdir > /dev/null || error "AGL for ls failed"
16792         $LCTL set_param fail_loc=0
16793         rm -r $DIR/$tdir
16794 }
16795 run_test 222a "AGL for ls should not trigger CLIO lock failure"
16796
16797 test_222b () {
16798         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16799
16800         rm -rf $DIR/$tdir
16801         test_mkdir $DIR/$tdir
16802         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16803         createmany -o $DIR/$tdir/$tfile 10
16804         cancel_lru_locks mdc
16805         cancel_lru_locks osc
16806         #define OBD_FAIL_LDLM_AGL_DELAY           0x31a
16807         $LCTL set_param fail_loc=0x31a
16808         rm -r $DIR/$tdir || error "AGL for rmdir failed"
16809         $LCTL set_param fail_loc=0
16810 }
16811 run_test 222b "AGL for rmdir should not trigger CLIO lock failure"
16812
16813 test_223 () {
16814         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16815
16816         rm -rf $DIR/$tdir
16817         test_mkdir $DIR/$tdir
16818         $LFS setstripe -c 1 -i 0 $DIR/$tdir
16819         createmany -o $DIR/$tdir/$tfile 10
16820         cancel_lru_locks mdc
16821         cancel_lru_locks osc
16822         #define OBD_FAIL_LDLM_AGL_NOLOCK          0x31b
16823         $LCTL set_param fail_loc=0x31b
16824         ls -l $DIR/$tdir > /dev/null || error "reenqueue failed"
16825         $LCTL set_param fail_loc=0
16826         rm -r $DIR/$tdir
16827 }
16828 run_test 223 "osc reenqueue if without AGL lock granted ======================="
16829
16830 test_224a() { # LU-1039, MRP-303
16831         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16832
16833         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB   0x508
16834         $LCTL set_param fail_loc=0x508
16835         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1 conv=fsync
16836         $LCTL set_param fail_loc=0
16837         df $DIR
16838 }
16839 run_test 224a "Don't panic on bulk IO failure"
16840
16841 test_224b() { # LU-1039, MRP-303
16842         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16843
16844         dd if=/dev/zero of=$DIR/$tfile bs=4096 count=1
16845         cancel_lru_locks osc
16846         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB2   0x515
16847         $LCTL set_param fail_loc=0x515
16848         dd of=/dev/null if=$DIR/$tfile bs=4096 count=1
16849         $LCTL set_param fail_loc=0
16850         df $DIR
16851 }
16852 run_test 224b "Don't panic on bulk IO failure"
16853
16854 test_224c() { # LU-6441
16855         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16856         remote_mds_nodsh && skip "remote MDS with nodsh"
16857
16858         local p="$TMP/$TESTSUITE-$TESTNAME.parameters"
16859         save_writethrough $p
16860         set_cache writethrough on
16861
16862         local pages_per_rpc=$($LCTL get_param osc.*.max_pages_per_rpc)
16863         local at_max=$($LCTL get_param -n at_max)
16864         local timeout=$($LCTL get_param -n timeout)
16865         local test_at="at_max"
16866         local param_at="$FSNAME.sys.at_max"
16867         local test_timeout="timeout"
16868         local param_timeout="$FSNAME.sys.timeout"
16869
16870         $LCTL set_param -n osc.*.max_pages_per_rpc=1024
16871
16872         set_persistent_param_and_check client "$test_at" "$param_at" 0
16873         set_persistent_param_and_check client "$test_timeout" "$param_timeout" 5
16874
16875         #define OBD_FAIL_PTLRPC_CLIENT_BULK_CB3 0x520
16876         do_facet ost1 "$LCTL set_param fail_loc=0x520"
16877         $LFS setstripe -c 1 -i 0 $DIR/$tfile
16878         dd if=/dev/zero of=$DIR/$tfile bs=8MB count=1
16879         sync
16880         do_facet ost1 "$LCTL set_param fail_loc=0"
16881
16882         set_persistent_param_and_check client "$test_at" "$param_at" $at_max
16883         set_persistent_param_and_check client "$test_timeout" "$param_timeout" \
16884                 $timeout
16885
16886         $LCTL set_param -n $pages_per_rpc
16887         restore_lustre_params < $p
16888         rm -f $p
16889 }
16890 run_test 224c "Don't hang if one of md lost during large bulk RPC"
16891
16892 MDSSURVEY=${MDSSURVEY:-$(which mds-survey 2>/dev/null || true)}
16893 test_225a () {
16894         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16895         if [ -z ${MDSSURVEY} ]; then
16896                 skip_env "mds-survey not found"
16897         fi
16898         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16899                 skip "Need MDS version at least 2.2.51"
16900
16901         local mds=$(facet_host $SINGLEMDS)
16902         local target=$(do_nodes $mds 'lctl dl' |
16903                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16904
16905         local cmd1="file_count=1000 thrhi=4"
16906         local cmd2="dir_count=2 layer=mdd stripe_count=0"
16907         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16908         local cmd="$cmd1 $cmd2 $cmd3"
16909
16910         rm -f ${TMP}/mds_survey*
16911         echo + $cmd
16912         eval $cmd || error "mds-survey with zero-stripe failed"
16913         cat ${TMP}/mds_survey*
16914         rm -f ${TMP}/mds_survey*
16915 }
16916 run_test 225a "Metadata survey sanity with zero-stripe"
16917
16918 test_225b () {
16919         if [ -z ${MDSSURVEY} ]; then
16920                 skip_env "mds-survey not found"
16921         fi
16922         [ $MDS1_VERSION -ge $(version_code 2.2.51) ] ||
16923                 skip "Need MDS version at least 2.2.51"
16924         [ $PARALLEL == "yes" ] && skip "skip parallel run"
16925         remote_mds_nodsh && skip "remote MDS with nodsh"
16926         if [ $($LCTL dl | grep -c osc) -eq 0 ]; then
16927                 skip_env "Need to mount OST to test"
16928         fi
16929
16930         local mds=$(facet_host $SINGLEMDS)
16931         local target=$(do_nodes $mds 'lctl dl' |
16932                        awk '{ if ($2 == "UP" && $3 == "mdt") { print $4 }}')
16933
16934         local cmd1="file_count=1000 thrhi=4"
16935         local cmd2="dir_count=2 layer=mdd stripe_count=1"
16936         local cmd3="rslt_loc=${TMP} targets=\"$mds:$target\" $MDSSURVEY"
16937         local cmd="$cmd1 $cmd2 $cmd3"
16938
16939         rm -f ${TMP}/mds_survey*
16940         echo + $cmd
16941         eval $cmd || error "mds-survey with stripe_count failed"
16942         cat ${TMP}/mds_survey*
16943         rm -f ${TMP}/mds_survey*
16944 }
16945 run_test 225b "Metadata survey sanity with stripe_count = 1"
16946
16947 mcreate_path2fid () {
16948         local mode=$1
16949         local major=$2
16950         local minor=$3
16951         local name=$4
16952         local desc=$5
16953         local path=$DIR/$tdir/$name
16954         local fid
16955         local rc
16956         local fid_path
16957
16958         $MCREATE --mode=$1 --major=$2 --minor=$3 $path ||
16959                 error "cannot create $desc"
16960
16961         fid=$($LFS path2fid $path | tr -d '[' | tr -d ']')
16962         rc=$?
16963         [ $rc -ne 0 ] && error "cannot get fid of a $desc"
16964
16965         fid_path=$($LFS fid2path $MOUNT $fid)
16966         rc=$?
16967         [ $rc -ne 0 ] && error "cannot get path of $desc by $DIR $path $fid"
16968
16969         [ "$path" == "$fid_path" ] ||
16970                 error "fid2path returned $fid_path, expected $path"
16971
16972         echo "pass with $path and $fid"
16973 }
16974
16975 test_226a () {
16976         rm -rf $DIR/$tdir
16977         mkdir -p $DIR/$tdir
16978
16979         mcreate_path2fid 0010666 0 0 fifo "FIFO"
16980         mcreate_path2fid 0020666 1 3 null "character special file (null)"
16981         mcreate_path2fid 0020666 1 255 none "character special file (no device)"
16982         mcreate_path2fid 0040666 0 0 dir "directory"
16983         mcreate_path2fid 0060666 7 0 loop0 "block special file (loop)"
16984         mcreate_path2fid 0100666 0 0 file "regular file"
16985         mcreate_path2fid 0120666 0 0 link "symbolic link"
16986         mcreate_path2fid 0140666 0 0 sock "socket"
16987 }
16988 run_test 226a "call path2fid and fid2path on files of all type"
16989
16990 test_226b () {
16991         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
16992
16993         local MDTIDX=1
16994
16995         rm -rf $DIR/$tdir
16996         mkdir -p $DIR/$tdir
16997         $LFS setdirstripe -i $MDTIDX $DIR/$tdir/remote_dir ||
16998                 error "create remote directory failed"
16999         mcreate_path2fid 0010666 0 0 "remote_dir/fifo" "FIFO"
17000         mcreate_path2fid 0020666 1 3 "remote_dir/null" \
17001                                 "character special file (null)"
17002         mcreate_path2fid 0020666 1 255 "remote_dir/none" \
17003                                 "character special file (no device)"
17004         mcreate_path2fid 0040666 0 0 "remote_dir/dir" "directory"
17005         mcreate_path2fid 0060666 7 0 "remote_dir/loop0" \
17006                                 "block special file (loop)"
17007         mcreate_path2fid 0100666 0 0 "remote_dir/file" "regular file"
17008         mcreate_path2fid 0120666 0 0 "remote_dir/link" "symbolic link"
17009         mcreate_path2fid 0140666 0 0 "remote_dir/sock" "socket"
17010 }
17011 run_test 226b "call path2fid and fid2path on files of all type under remote dir"
17012
17013 # LU-1299 Executing or running ldd on a truncated executable does not
17014 # cause an out-of-memory condition.
17015 test_227() {
17016         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17017         [ -z "$(which ldd)" ] && skip_env "should have ldd tool"
17018
17019         dd if=$(which date) of=$MOUNT/date bs=1k count=1
17020         chmod +x $MOUNT/date
17021
17022         $MOUNT/date > /dev/null
17023         ldd $MOUNT/date > /dev/null
17024         rm -f $MOUNT/date
17025 }
17026 run_test 227 "running truncated executable does not cause OOM"
17027
17028 # LU-1512 try to reuse idle OI blocks
17029 test_228a() {
17030         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17031         remote_mds_nodsh && skip "remote MDS with nodsh"
17032         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17033
17034         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17035         local myDIR=$DIR/$tdir
17036
17037         mkdir -p $myDIR
17038         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17039         $LCTL set_param fail_loc=0x80001002
17040         createmany -o $myDIR/t- 10000
17041         $LCTL set_param fail_loc=0
17042         # The guard is current the largest FID holder
17043         touch $myDIR/guard
17044         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17045                     tr -d '[')
17046         local IDX=$(($SEQ % 64))
17047
17048         do_facet $SINGLEMDS sync
17049         # Make sure journal flushed.
17050         sleep 6
17051         local blk1=$(do_facet $SINGLEMDS \
17052                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17053                      grep Blockcount | awk '{print $4}')
17054
17055         # Remove old files, some OI blocks will become idle.
17056         unlinkmany $myDIR/t- 10000
17057         # Create new files, idle OI blocks should be reused.
17058         createmany -o $myDIR/t- 2000
17059         do_facet $SINGLEMDS sync
17060         # Make sure journal flushed.
17061         sleep 6
17062         local blk2=$(do_facet $SINGLEMDS \
17063                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17064                      grep Blockcount | awk '{print $4}')
17065
17066         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17067 }
17068 run_test 228a "try to reuse idle OI blocks"
17069
17070 test_228b() {
17071         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17072         remote_mds_nodsh && skip "remote MDS with nodsh"
17073         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17074
17075         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17076         local myDIR=$DIR/$tdir
17077
17078         mkdir -p $myDIR
17079         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17080         $LCTL set_param fail_loc=0x80001002
17081         createmany -o $myDIR/t- 10000
17082         $LCTL set_param fail_loc=0
17083         # The guard is current the largest FID holder
17084         touch $myDIR/guard
17085         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17086                     tr -d '[')
17087         local IDX=$(($SEQ % 64))
17088
17089         do_facet $SINGLEMDS sync
17090         # Make sure journal flushed.
17091         sleep 6
17092         local blk1=$(do_facet $SINGLEMDS \
17093                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17094                      grep Blockcount | awk '{print $4}')
17095
17096         # Remove old files, some OI blocks will become idle.
17097         unlinkmany $myDIR/t- 10000
17098
17099         # stop the MDT
17100         stop $SINGLEMDS || error "Fail to stop MDT."
17101         # remount the MDT
17102         start $SINGLEMDS $MDT_DEV $MDS_MOUNT_OPTS || error "Fail to start MDT."
17103
17104         df $MOUNT || error "Fail to df."
17105         # Create new files, idle OI blocks should be reused.
17106         createmany -o $myDIR/t- 2000
17107         do_facet $SINGLEMDS sync
17108         # Make sure journal flushed.
17109         sleep 6
17110         local blk2=$(do_facet $SINGLEMDS \
17111                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17112                      grep Blockcount | awk '{print $4}')
17113
17114         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17115 }
17116 run_test 228b "idle OI blocks can be reused after MDT restart"
17117
17118 #LU-1881
17119 test_228c() {
17120         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17121         remote_mds_nodsh && skip "remote MDS with nodsh"
17122         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
17123
17124         local MDT_DEV=$(mdsdevname ${SINGLEMDS//mds/})
17125         local myDIR=$DIR/$tdir
17126
17127         mkdir -p $myDIR
17128         #define OBD_FAIL_SEQ_EXHAUST             0x1002
17129         $LCTL set_param fail_loc=0x80001002
17130         # 20000 files can guarantee there are index nodes in the OI file
17131         createmany -o $myDIR/t- 20000
17132         $LCTL set_param fail_loc=0
17133         # The guard is current the largest FID holder
17134         touch $myDIR/guard
17135         local SEQ=$($LFS path2fid $myDIR/guard | awk -F ':' '{print $1}' |
17136                     tr -d '[')
17137         local IDX=$(($SEQ % 64))
17138
17139         do_facet $SINGLEMDS sync
17140         # Make sure journal flushed.
17141         sleep 6
17142         local blk1=$(do_facet $SINGLEMDS \
17143                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17144                      grep Blockcount | awk '{print $4}')
17145
17146         # Remove old files, some OI blocks will become idle.
17147         unlinkmany $myDIR/t- 20000
17148         rm -f $myDIR/guard
17149         # The OI file should become empty now
17150
17151         # Create new files, idle OI blocks should be reused.
17152         createmany -o $myDIR/t- 2000
17153         do_facet $SINGLEMDS sync
17154         # Make sure journal flushed.
17155         sleep 6
17156         local blk2=$(do_facet $SINGLEMDS \
17157                      "$DEBUGFS -c -R \\\"stat oi.16.${IDX}\\\" $MDT_DEV" |
17158                      grep Blockcount | awk '{print $4}')
17159
17160         [ $blk1 == $blk2 ] || error "old blk1=$blk1, new blk2=$blk2, unmatched!"
17161 }
17162 run_test 228c "NOT shrink the last entry in OI index node to recycle idle leaf"
17163
17164 test_229() { # LU-2482, LU-3448
17165         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17166         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
17167         [ $MDS1_VERSION -lt $(version_code 2.4.53) ] &&
17168                 skip "No HSM $(lustre_build_version $SINGLEMDS) MDS < 2.4.53"
17169
17170         rm -f $DIR/$tfile
17171
17172         # Create a file with a released layout and stripe count 2.
17173         $MULTIOP $DIR/$tfile H2c ||
17174                 error "failed to create file with released layout"
17175
17176         $LFS getstripe -v $DIR/$tfile
17177
17178         local pattern=$($LFS getstripe -L $DIR/$tfile)
17179         [ X"$pattern" = X"released" ] || error "pattern error ($pattern)"
17180
17181         local stripe_count=$($LFS getstripe -c $DIR/$tfile) ||
17182                 error "getstripe"
17183         [ $stripe_count -eq 2 ] || error "stripe count not 2 ($stripe_count)"
17184         stat $DIR/$tfile || error "failed to stat released file"
17185
17186         chown $RUNAS_ID $DIR/$tfile ||
17187                 error "chown $RUNAS_ID $DIR/$tfile failed"
17188
17189         chgrp $RUNAS_ID $DIR/$tfile ||
17190                 error "chgrp $RUNAS_ID $DIR/$tfile failed"
17191
17192         touch $DIR/$tfile || error "touch $DIR/$tfile failed"
17193         rm $DIR/$tfile || error "failed to remove released file"
17194 }
17195 run_test 229 "getstripe/stat/rm/attr changes work on released files"
17196
17197 test_230a() {
17198         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17199         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17200         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17201                 skip "Need MDS version at least 2.11.52"
17202
17203         local MDTIDX=1
17204
17205         test_mkdir $DIR/$tdir
17206         test_mkdir -i0 -c1 $DIR/$tdir/test_230_local
17207         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230_local)
17208         [ $mdt_idx -ne 0 ] &&
17209                 error "create local directory on wrong MDT $mdt_idx"
17210
17211         $LFS mkdir -i $MDTIDX $DIR/$tdir/test_230 ||
17212                         error "create remote directory failed"
17213         local mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230)
17214         [ $mdt_idx -ne $MDTIDX ] &&
17215                 error "create remote directory on wrong MDT $mdt_idx"
17216
17217         createmany -o $DIR/$tdir/test_230/t- 10 ||
17218                 error "create files on remote directory failed"
17219         mdt_idx=$($LFS getstripe -m $DIR/$tdir/test_230/t-0)
17220         [ $mdt_idx -ne $MDTIDX ] && error "create files on wrong MDT $mdt_idx"
17221         rm -r $DIR/$tdir || error "unlink remote directory failed"
17222 }
17223 run_test 230a "Create remote directory and files under the remote directory"
17224
17225 test_230b() {
17226         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17227         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17228         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17229                 skip "Need MDS version at least 2.11.52"
17230
17231         local MDTIDX=1
17232         local mdt_index
17233         local i
17234         local file
17235         local pid
17236         local stripe_count
17237         local migrate_dir=$DIR/$tdir/migrate_dir
17238         local other_dir=$DIR/$tdir/other_dir
17239
17240         test_mkdir $DIR/$tdir
17241         test_mkdir -i0 -c1 $migrate_dir
17242         test_mkdir -i0 -c1 $other_dir
17243         for ((i=0; i<10; i++)); do
17244                 mkdir -p $migrate_dir/dir_${i}
17245                 createmany -o $migrate_dir/dir_${i}/f 10 ||
17246                         error "create files under remote dir failed $i"
17247         done
17248
17249         cp /etc/passwd $migrate_dir/$tfile
17250         cp /etc/passwd $other_dir/$tfile
17251         chattr +SAD $migrate_dir
17252         chattr +SAD $migrate_dir/$tfile
17253
17254         local old_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17255         local old_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17256         local old_dir_mode=$(stat -c%f $migrate_dir)
17257         local old_file_mode=$(stat -c%f $migrate_dir/$tfile)
17258
17259         mkdir -p $migrate_dir/dir_default_stripe2
17260         $LFS setstripe -c 2 $migrate_dir/dir_default_stripe2
17261         $LFS setstripe -c 2 $migrate_dir/${tfile}_stripe2
17262
17263         mkdir -p $other_dir
17264         ln $migrate_dir/$tfile $other_dir/luna
17265         ln $migrate_dir/$tfile $migrate_dir/sofia
17266         ln $other_dir/$tfile $migrate_dir/david
17267         ln -s $migrate_dir/$tfile $other_dir/zachary
17268         ln -s $migrate_dir/$tfile $migrate_dir/${tfile}_ln
17269         ln -s $other_dir/$tfile $migrate_dir/${tfile}_ln_other
17270
17271         local len
17272         local lnktgt
17273
17274         # inline symlink
17275         for len in 58 59 60; do
17276                 lnktgt=$(str_repeat 'l' $len)
17277                 touch $migrate_dir/$lnktgt
17278                 ln -s $lnktgt $migrate_dir/${len}char_ln
17279         done
17280
17281         # PATH_MAX
17282         for len in 4094 4095; do
17283                 lnktgt=$(str_repeat 'l' $len)
17284                 ln -s $lnktgt $migrate_dir/${len}char_ln
17285         done
17286
17287         # NAME_MAX
17288         for len in 254 255; do
17289                 touch $migrate_dir/$(str_repeat 'l' $len)
17290         done
17291
17292         $LFS migrate -m $MDTIDX $migrate_dir ||
17293                 error "fails on migrating remote dir to MDT1"
17294
17295         echo "migratate to MDT1, then checking.."
17296         for ((i = 0; i < 10; i++)); do
17297                 for file in $(find $migrate_dir/dir_${i}); do
17298                         mdt_index=$($LFS getstripe -m $file)
17299                         # broken symlink getstripe will fail
17300                         [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
17301                                 error "$file is not on MDT${MDTIDX}"
17302                 done
17303         done
17304
17305         # the multiple link file should still in MDT0
17306         mdt_index=$($LFS getstripe -m $migrate_dir/$tfile)
17307         [ $mdt_index == 0 ] ||
17308                 error "$file is not on MDT${MDTIDX}"
17309
17310         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17311         [ "$old_dir_flag" = "$new_dir_flag" ] ||
17312                 error " expect $old_dir_flag get $new_dir_flag"
17313
17314         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17315         [ "$old_file_flag" = "$new_file_flag" ] ||
17316                 error " expect $old_file_flag get $new_file_flag"
17317
17318         local new_dir_mode=$(stat -c%f $migrate_dir)
17319         [ "$old_dir_mode" = "$new_dir_mode" ] ||
17320                 error "expect mode $old_dir_mode get $new_dir_mode"
17321
17322         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
17323         [ "$old_file_mode" = "$new_file_mode" ] ||
17324                 error "expect mode $old_file_mode get $new_file_mode"
17325
17326         diff /etc/passwd $migrate_dir/$tfile ||
17327                 error "$tfile different after migration"
17328
17329         diff /etc/passwd $other_dir/luna ||
17330                 error "luna different after migration"
17331
17332         diff /etc/passwd $migrate_dir/sofia ||
17333                 error "sofia different after migration"
17334
17335         diff /etc/passwd $migrate_dir/david ||
17336                 error "david different after migration"
17337
17338         diff /etc/passwd $other_dir/zachary ||
17339                 error "zachary different after migration"
17340
17341         diff /etc/passwd $migrate_dir/${tfile}_ln ||
17342                 error "${tfile}_ln different after migration"
17343
17344         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
17345                 error "${tfile}_ln_other different after migration"
17346
17347         stripe_count=$($LFS getstripe -c $migrate_dir/dir_default_stripe2)
17348         [ $stripe_count = 2 ] ||
17349                 error "dir strpe_count $d != 2 after migration."
17350
17351         stripe_count=$($LFS getstripe -c $migrate_dir/${tfile}_stripe2)
17352         [ $stripe_count = 2 ] ||
17353                 error "file strpe_count $d != 2 after migration."
17354
17355         #migrate back to MDT0
17356         MDTIDX=0
17357
17358         $LFS migrate -m $MDTIDX $migrate_dir ||
17359                 error "fails on migrating remote dir to MDT0"
17360
17361         echo "migrate back to MDT0, checking.."
17362         for file in $(find $migrate_dir); do
17363                 mdt_index=$($LFS getstripe -m $file)
17364                 [ $mdt_index -ne $MDTIDX ] && stat -L $file &&
17365                         error "$file is not on MDT${MDTIDX}"
17366         done
17367
17368         local new_dir_flag=$(lsattr -a $migrate_dir | awk '/\/\.$/ {print $1}')
17369         [ "$old_dir_flag" = "$new_dir_flag" ] ||
17370                 error " expect $old_dir_flag get $new_dir_flag"
17371
17372         local new_file_flag=$(lsattr $migrate_dir/$tfile | awk '{print $1}')
17373         [ "$old_file_flag" = "$new_file_flag" ] ||
17374                 error " expect $old_file_flag get $new_file_flag"
17375
17376         local new_dir_mode=$(stat -c%f $migrate_dir)
17377         [ "$old_dir_mode" = "$new_dir_mode" ] ||
17378                 error "expect mode $old_dir_mode get $new_dir_mode"
17379
17380         local new_file_mode=$(stat -c%f $migrate_dir/$tfile)
17381         [ "$old_file_mode" = "$new_file_mode" ] ||
17382                 error "expect mode $old_file_mode get $new_file_mode"
17383
17384         diff /etc/passwd ${migrate_dir}/$tfile ||
17385                 error "$tfile different after migration"
17386
17387         diff /etc/passwd ${other_dir}/luna ||
17388                 error "luna different after migration"
17389
17390         diff /etc/passwd ${migrate_dir}/sofia ||
17391                 error "sofia different after migration"
17392
17393         diff /etc/passwd ${other_dir}/zachary ||
17394                 error "zachary different after migration"
17395
17396         diff /etc/passwd $migrate_dir/${tfile}_ln ||
17397                 error "${tfile}_ln different after migration"
17398
17399         diff /etc/passwd $migrate_dir/${tfile}_ln_other ||
17400                 error "${tfile}_ln_other different after migration"
17401
17402         stripe_count=$($LFS getstripe -c ${migrate_dir}/dir_default_stripe2)
17403         [ $stripe_count = 2 ] ||
17404                 error "dir strpe_count $d != 2 after migration."
17405
17406         stripe_count=$($LFS getstripe -c ${migrate_dir}/${tfile}_stripe2)
17407         [ $stripe_count = 2 ] ||
17408                 error "file strpe_count $d != 2 after migration."
17409
17410         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17411 }
17412 run_test 230b "migrate directory"
17413
17414 test_230c() {
17415         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17416         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17417         remote_mds_nodsh && skip "remote MDS with nodsh"
17418         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17419                 skip "Need MDS version at least 2.11.52"
17420
17421         local MDTIDX=1
17422         local total=3
17423         local mdt_index
17424         local file
17425         local migrate_dir=$DIR/$tdir/migrate_dir
17426
17427         #If migrating directory fails in the middle, all entries of
17428         #the directory is still accessiable.
17429         test_mkdir $DIR/$tdir
17430         test_mkdir -i0 -c1 $migrate_dir
17431         test_mkdir -i1 -c1 $DIR/$tdir/remote_dir
17432         stat $migrate_dir
17433         createmany -o $migrate_dir/f $total ||
17434                 error "create files under ${migrate_dir} failed"
17435
17436         # fail after migrating top dir, and this will fail only once, so the
17437         # first sub file migration will fail (currently f3), others succeed.
17438         #OBD_FAIL_MIGRATE_ENTRIES       0x1801
17439         do_facet mds1 lctl set_param fail_loc=0x1801
17440         local t=$(ls $migrate_dir | wc -l)
17441         $LFS migrate --mdt-index $MDTIDX $migrate_dir &&
17442                 error "migrate should fail"
17443         local u=$(ls $migrate_dir | wc -l)
17444         [ "$u" == "$t" ] || error "$u != $t during migration"
17445
17446         # add new dir/file should succeed
17447         mkdir $migrate_dir/dir ||
17448                 error "mkdir failed under migrating directory"
17449         touch $migrate_dir/file ||
17450                 error "create file failed under migrating directory"
17451
17452         # add file with existing name should fail
17453         for file in $migrate_dir/f*; do
17454                 stat $file > /dev/null || error "stat $file failed"
17455                 $OPENFILE -f O_CREAT:O_EXCL $file &&
17456                         error "open(O_CREAT|O_EXCL) $file should fail"
17457                 $MULTIOP $file m && error "create $file should fail"
17458                 touch $DIR/$tdir/remote_dir/$tfile ||
17459                         error "touch $tfile failed"
17460                 ln $DIR/$tdir/remote_dir/$tfile $file &&
17461                         error "link $file should fail"
17462                 mdt_index=$($LFS getstripe -m $file)
17463                 if [ $mdt_index == 0 ]; then
17464                         # file failed to migrate is not allowed to rename to
17465                         mv $DIR/$tdir/remote_dir/$tfile $file &&
17466                                 error "rename to $file should fail"
17467                 else
17468                         mv $DIR/$tdir/remote_dir/$tfile $file ||
17469                                 error "rename to $file failed"
17470                 fi
17471                 echo hello >> $file || error "write $file failed"
17472         done
17473
17474         # resume migration with different options should fail
17475         $LFS migrate -m 0 $migrate_dir &&
17476                 error "migrate -m 0 $migrate_dir should fail"
17477
17478         $LFS migrate -m $MDTIDX -c 2 $migrate_dir &&
17479                 error "migrate -c 2 $migrate_dir should fail"
17480
17481         # resume migration should succeed
17482         $LFS migrate -m $MDTIDX $migrate_dir ||
17483                 error "migrate $migrate_dir failed"
17484
17485         echo "Finish migration, then checking.."
17486         for file in $(find $migrate_dir); do
17487                 mdt_index=$($LFS getstripe -m $file)
17488                 [ $mdt_index == $MDTIDX ] ||
17489                         error "$file is not on MDT${MDTIDX}"
17490         done
17491
17492         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17493 }
17494 run_test 230c "check directory accessiblity if migration failed"
17495
17496 test_230d() {
17497         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17498         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17499         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17500                 skip "Need MDS version at least 2.11.52"
17501         # LU-11235
17502         [ "$mds1_FSTYPE" == "zfs" ] && skip "skip ZFS backend"
17503
17504         local migrate_dir=$DIR/$tdir/migrate_dir
17505         local old_index
17506         local new_index
17507         local old_count
17508         local new_count
17509         local new_hash
17510         local mdt_index
17511         local i
17512         local j
17513
17514         old_index=$((RANDOM % MDSCOUNT))
17515         old_count=$((MDSCOUNT - old_index))
17516         new_index=$((RANDOM % MDSCOUNT))
17517         new_count=$((MDSCOUNT - new_index))
17518         new_hash=1 # for all_char
17519
17520         [ $old_count -gt 1 ] && old_count=$((old_count - RANDOM % old_count))
17521         [ $new_count -gt 1 ] && new_count=$((new_count - RANDOM % new_count))
17522
17523         test_mkdir $DIR/$tdir
17524         test_mkdir -i $old_index -c $old_count $migrate_dir
17525
17526         for ((i=0; i<100; i++)); do
17527                 test_mkdir -i0 -c1 $migrate_dir/dir_${i}
17528                 createmany -o $migrate_dir/dir_${i}/f 100 ||
17529                         error "create files under remote dir failed $i"
17530         done
17531
17532         echo -n "Migrate from MDT$old_index "
17533         [ $old_count -gt 1 ] && echo -n "... MDT$((old_index + old_count - 1)) "
17534         echo -n "to MDT$new_index"
17535         [ $new_count -gt 1 ] && echo -n " ... MDT$((new_index + new_count - 1))"
17536         echo
17537
17538         echo "$LFS migrate -m$new_index -c$new_count -H $new_hash $migrate_dir"
17539         $LFS migrate -m $new_index -c $new_count -H $new_hash $migrate_dir ||
17540                 error "migrate remote dir error"
17541
17542         echo "Finish migration, then checking.."
17543         for file in $(find $migrate_dir); do
17544                 mdt_index=$($LFS getstripe -m $file)
17545                 if [ $mdt_index -lt $new_index ] ||
17546                    [ $mdt_index -gt $((new_index + new_count - 1)) ]; then
17547                         error "$file is on MDT$mdt_index"
17548                 fi
17549         done
17550
17551         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17552 }
17553 run_test 230d "check migrate big directory"
17554
17555 test_230e() {
17556         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17557         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17558         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17559                 skip "Need MDS version at least 2.11.52"
17560
17561         local i
17562         local j
17563         local a_fid
17564         local b_fid
17565
17566         mkdir -p $DIR/$tdir
17567         mkdir $DIR/$tdir/migrate_dir
17568         mkdir $DIR/$tdir/other_dir
17569         touch $DIR/$tdir/migrate_dir/a
17570         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/b
17571         ls $DIR/$tdir/other_dir
17572
17573         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17574                 error "migrate dir fails"
17575
17576         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17577         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17578
17579         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17580         [ $mdt_index == 0 ] || error "a is not on MDT0"
17581
17582         $LFS migrate -m 1 $DIR/$tdir/other_dir ||
17583                 error "migrate dir fails"
17584
17585         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir)
17586         [ $mdt_index == 1 ] || error "other_dir is not on MDT1"
17587
17588         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17589         [ $mdt_index == 1 ] || error "a is not on MDT1"
17590
17591         mdt_index=$($LFS getstripe -m $DIR/$tdir/other_dir/b)
17592         [ $mdt_index == 1 ] || error "b is not on MDT1"
17593
17594         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17595         b_fid=$($LFS path2fid $DIR/$tdir/other_dir/b)
17596
17597         [ "$a_fid" = "$b_fid" ] || error "different fid after migration"
17598
17599         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17600 }
17601 run_test 230e "migrate mulitple local link files"
17602
17603 test_230f() {
17604         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17605         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17606         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17607                 skip "Need MDS version at least 2.11.52"
17608
17609         local a_fid
17610         local ln_fid
17611
17612         mkdir -p $DIR/$tdir
17613         mkdir $DIR/$tdir/migrate_dir
17614         $LFS mkdir -i1 $DIR/$tdir/other_dir
17615         touch $DIR/$tdir/migrate_dir/a
17616         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln1
17617         ln $DIR/$tdir/migrate_dir/a $DIR/$tdir/other_dir/ln2
17618         ls $DIR/$tdir/other_dir
17619
17620         # a should be migrated to MDT1, since no other links on MDT0
17621         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17622                 error "#1 migrate dir fails"
17623         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir)
17624         [ $mdt_index == 1 ] || error "migrate_dir is not on MDT1"
17625         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17626         [ $mdt_index == 1 ] || error "a is not on MDT1"
17627
17628         # a should stay on MDT1, because it is a mulitple link file
17629         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17630                 error "#2 migrate dir fails"
17631         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17632         [ $mdt_index == 1 ] || error "a is not on MDT1"
17633
17634         $LFS migrate -m 1 $DIR/$tdir/migrate_dir ||
17635                 error "#3 migrate dir fails"
17636
17637         a_fid=$($LFS path2fid $DIR/$tdir/migrate_dir/a)
17638         ln_fid=$($LFS path2fid $DIR/$tdir/other_dir/ln1)
17639         [ "$a_fid" = "$ln_fid" ] || error "different fid after migrate to MDT1"
17640
17641         rm -rf $DIR/$tdir/other_dir/ln1 || error "unlink ln1 fails"
17642         rm -rf $DIR/$tdir/other_dir/ln2 || error "unlink ln2 fails"
17643
17644         # a should be migrated to MDT0, since no other links on MDT1
17645         $LFS migrate -m 0 $DIR/$tdir/migrate_dir ||
17646                 error "#4 migrate dir fails"
17647         mdt_index=$($LFS getstripe -m $DIR/$tdir/migrate_dir/a)
17648         [ $mdt_index == 0 ] || error "a is not on MDT0"
17649
17650         rm -rf $DIR/$tdir || error "rm dir failed after migration"
17651 }
17652 run_test 230f "migrate mulitple remote link files"
17653
17654 test_230g() {
17655         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17656         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17657         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17658                 skip "Need MDS version at least 2.11.52"
17659
17660         mkdir -p $DIR/$tdir/migrate_dir
17661
17662         $LFS migrate -m 1000 $DIR/$tdir/migrate_dir &&
17663                 error "migrating dir to non-exist MDT succeeds"
17664         true
17665 }
17666 run_test 230g "migrate dir to non-exist MDT"
17667
17668 test_230h() {
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 mdt_index
17675
17676         mkdir -p $DIR/$tdir/migrate_dir
17677
17678         $LFS migrate -m1 $DIR &&
17679                 error "migrating mountpoint1 should fail"
17680
17681         $LFS migrate -m1 $DIR/$tdir/.. &&
17682                 error "migrating mountpoint2 should fail"
17683
17684         # same as mv
17685         $LFS migrate -m1 $DIR/$tdir/migrate_dir/.. &&
17686                 error "migrating $tdir/migrate_dir/.. should fail"
17687
17688         true
17689 }
17690 run_test 230h "migrate .. and root"
17691
17692 test_230i() {
17693         [ $PARALLEL == "yes" ] && skip "skip parallel run"
17694         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
17695         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17696                 skip "Need MDS version at least 2.11.52"
17697
17698         mkdir -p $DIR/$tdir/migrate_dir
17699
17700         $LFS migrate -m 1 $DIR/$tdir/migrate_dir/ ||
17701                 error "migration fails with a tailing slash"
17702
17703         $LFS migrate -m 0 $DIR/$tdir/migrate_dir// ||
17704                 error "migration fails with two tailing slashes"
17705 }
17706 run_test 230i "lfs migrate -m tolerates trailing slashes"
17707
17708 test_230j() {
17709         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17710         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
17711                 skip "Need MDS version at least 2.11.52"
17712
17713         $LFS mkdir -m 0 -c 1 $DIR/$tdir || error "mkdir $tdir failed"
17714         $LFS setstripe -E 1M -L mdt $DIR/$tdir/$tfile ||
17715                 error "create $tfile failed"
17716         cat /etc/passwd > $DIR/$tdir/$tfile
17717
17718         $LFS migrate -m 1 $DIR/$tdir
17719
17720         cmp /etc/passwd $DIR/$tdir/$tfile ||
17721                 error "DoM file mismatch after migration"
17722 }
17723 run_test 230j "DoM file data not changed after dir migration"
17724
17725 test_230k() {
17726         [ $MDSCOUNT -lt 4 ] && skip "needs >= 4 MDTs"
17727         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17728                 skip "Need MDS version at least 2.11.56"
17729
17730         local total=20
17731         local files_on_starting_mdt=0
17732
17733         $LFS mkdir -i -1 -c 2 $DIR/$tdir || error "mkdir failed"
17734         $LFS getdirstripe $DIR/$tdir
17735         for i in $(seq $total); do
17736                 echo $((i*i - i)) > $DIR/$tdir/$tfile.$i || error "write failed"
17737                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17738                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17739         done
17740
17741         echo "$files_on_starting_mdt files on MDT0"
17742
17743         $LFS migrate -m 1,3 $DIR/$tdir || error "migrate -m 1,3 failed"
17744         $LFS getdirstripe $DIR/$tdir
17745
17746         files_on_starting_mdt=0
17747         for i in $(seq $total); do
17748                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17749                         error "file $tfile.$i mismatch after migration"
17750                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 1 ]] &&
17751                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17752         done
17753
17754         echo "$files_on_starting_mdt files on MDT1 after migration"
17755         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT1"
17756
17757         $LFS migrate -m 0 -c 2 $DIR/$tdir || error "migrate -m 0 -c 2 failed"
17758         $LFS getdirstripe $DIR/$tdir
17759
17760         files_on_starting_mdt=0
17761         for i in $(seq $total); do
17762                 $(echo $((i*i - i)) | cmp $DIR/$tdir/$tfile.$i -) ||
17763                         error "file $tfile.$i mismatch after 2nd migration"
17764                 [[ $($LFS getstripe -m $DIR/$tdir/$tfile.$i) -eq 0 ]] &&
17765                         files_on_starting_mdt=$((files_on_starting_mdt + 1))
17766         done
17767
17768         echo "$files_on_starting_mdt files on MDT0 after 2nd migration"
17769         [[ $files_on_starting_mdt -eq $total ]] && error "all files on MDT0"
17770
17771         true
17772 }
17773 run_test 230k "file data not changed after dir migration"
17774
17775 test_230l() {
17776         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17777         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17778                 skip "Need MDS version at least 2.11.56"
17779
17780         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "mkdir failed"
17781         createmany -o $DIR/$tdir/f___________________________________ 1000 ||
17782                 error "create files under remote dir failed $i"
17783         $LFS migrate -m 1 $DIR/$tdir || error "migrate failed"
17784 }
17785 run_test 230l "readdir between MDTs won't crash"
17786
17787 test_230m() {
17788         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17789         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
17790                 skip "Need MDS version at least 2.11.56"
17791
17792         local MDTIDX=1
17793         local mig_dir=$DIR/$tdir/migrate_dir
17794         local longstr="aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
17795         local shortstr="b"
17796         local val
17797
17798         echo "Creating files and dirs with xattrs"
17799         test_mkdir $DIR/$tdir
17800         test_mkdir -i0 -c1 $mig_dir
17801         mkdir $mig_dir/dir
17802         setfattr -n user.attr1 -v $longstr $mig_dir/dir ||
17803                 error "cannot set xattr attr1 on dir"
17804         setfattr -n user.attr2 -v $shortstr $mig_dir/dir ||
17805                 error "cannot set xattr attr2 on dir"
17806         touch $mig_dir/dir/f0
17807         setfattr -n user.attr1 -v $longstr $mig_dir/dir/f0 ||
17808                 error "cannot set xattr attr1 on file"
17809         setfattr -n user.attr2 -v $shortstr $mig_dir/dir/f0 ||
17810                 error "cannot set xattr attr2 on file"
17811         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17812         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17813         [ "$val" = $longstr ] || error "xattr attr1 not set properly on dir"
17814         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17815         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on dir"
17816         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17817         [ "$val" = $longstr ] || error "xattr attr1 not set properly on file"
17818         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17819         [ "$val" = $shortstr ] || error "xattr attr2 not set properly on file"
17820
17821         echo "Migrating to MDT1"
17822         $LFS migrate -m $MDTIDX $mig_dir ||
17823                 error "fails on migrating dir to MDT1"
17824
17825         sync ; sync ; echo 3 > /proc/sys/vm/drop_caches
17826         echo "Checking xattrs"
17827         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir 2>/dev/null)
17828         [ "$val" = $longstr ] ||
17829                 error "expecting xattr1 $longstr on dir, found $val"
17830         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir 2>/dev/null)
17831         [ "$val" = $shortstr ] ||
17832                 error "expecting xattr2 $shortstr on dir, found $val"
17833         val=$(getfattr --only-values -n user.attr1 $mig_dir/dir/f0 2>/dev/null)
17834         [ "$val" = $longstr ] ||
17835                 error "expecting xattr1 $longstr on file, found $val"
17836         val=$(getfattr --only-values -n user.attr2 $mig_dir/dir/f0 2>/dev/null)
17837         [ "$val" = $shortstr ] ||
17838                 error "expecting xattr2 $shortstr on file, found $val"
17839 }
17840 run_test 230m "xattrs not changed after dir migration"
17841
17842 test_230n() {
17843         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
17844         [[ $MDS1_VERSION -ge $(version_code 2.13.53) ]] ||
17845                 skip "Need MDS version at least 2.13.53"
17846
17847         $LFS mkdir -i 0 $DIR/$tdir || error "mkdir $tdir failed"
17848         cat /etc/hosts > $DIR/$tdir/$tfile
17849         $LFS mirror extend -N1 $DIR/$tdir/$tfile || error "Mirroring failed"
17850         $LFS migrate -m 1 $DIR/$tdir || error "Migration failed"
17851
17852         cmp /etc/hosts $DIR/$tdir/$tfile ||
17853                 error "File data mismatch after migration"
17854 }
17855 run_test 230n "Dir migration with mirrored file"
17856
17857 test_231a()
17858 {
17859         # For simplicity this test assumes that max_pages_per_rpc
17860         # is the same across all OSCs
17861         local max_pages=$($LCTL get_param -n osc.*.max_pages_per_rpc | head -n1)
17862         local bulk_size=$((max_pages * PAGE_SIZE))
17863         local brw_size=$(do_facet ost1 $LCTL get_param -n obdfilter.*.brw_size |
17864                                        head -n 1)
17865
17866         mkdir -p $DIR/$tdir
17867         $LFS setstripe -S ${brw_size}M $DIR/$tdir ||
17868                 error "failed to set stripe with -S ${brw_size}M option"
17869
17870         # clear the OSC stats
17871         $LCTL set_param osc.*.stats=0 &>/dev/null
17872         stop_writeback
17873
17874         # Client writes $bulk_size - there must be 1 rpc for $max_pages.
17875         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=$bulk_size count=1 \
17876                 oflag=direct &>/dev/null || error "dd failed"
17877
17878         sync; sleep 1; sync # just to be safe
17879         local nrpcs=$($LCTL get_param osc.*.stats |awk '/ost_write/ {print $2}')
17880         if [ x$nrpcs != "x1" ]; then
17881                 $LCTL get_param osc.*.stats
17882                 error "found $nrpcs ost_write RPCs, not 1 as expected"
17883         fi
17884
17885         start_writeback
17886         # Drop the OSC cache, otherwise we will read from it
17887         cancel_lru_locks osc
17888
17889         # clear the OSC stats
17890         $LCTL set_param osc.*.stats=0 &>/dev/null
17891
17892         # Client reads $bulk_size.
17893         dd if=$DIR/$tdir/$tfile of=/dev/null bs=$bulk_size count=1 \
17894                 iflag=direct &>/dev/null || error "dd failed"
17895
17896         nrpcs=$($LCTL get_param osc.*.stats | awk '/ost_read/ { print $2 }')
17897         if [ x$nrpcs != "x1" ]; then
17898                 $LCTL get_param osc.*.stats
17899                 error "found $nrpcs ost_read RPCs, not 1 as expected"
17900         fi
17901 }
17902 run_test 231a "checking that reading/writing of BRW RPC size results in one RPC"
17903
17904 test_231b() {
17905         mkdir -p $DIR/$tdir
17906         local i
17907         for i in {0..1023}; do
17908                 dd if=/dev/zero of=$DIR/$tdir/$tfile conv=notrunc \
17909                         seek=$((2 * i)) bs=4096 count=1 &>/dev/null ||
17910                         error "dd of=$DIR/$tdir/$tfile seek=$((2 * i)) failed"
17911         done
17912         sync
17913 }
17914 run_test 231b "must not assert on fully utilized OST request buffer"
17915
17916 test_232a() {
17917         mkdir -p $DIR/$tdir
17918         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17919
17920         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17921         do_facet ost1 $LCTL set_param fail_loc=0x31c
17922
17923         # ignore dd failure
17924         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1 || true
17925
17926         do_facet ost1 $LCTL set_param fail_loc=0
17927         umount_client $MOUNT || error "umount failed"
17928         mount_client $MOUNT || error "mount failed"
17929         stop ost1 || error "cannot stop ost1"
17930         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17931 }
17932 run_test 232a "failed lock should not block umount"
17933
17934 test_232b() {
17935         [ $MDS1_VERSION -ge $(version_code 2.10.58) ] ||
17936                 skip "Need MDS version at least 2.10.58"
17937
17938         mkdir -p $DIR/$tdir
17939         $LFS setstripe -c1 -i0 $DIR/$tdir/$tfile
17940         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=1
17941         sync
17942         cancel_lru_locks osc
17943
17944         #define OBD_FAIL_LDLM_OST_LVB            0x31c
17945         do_facet ost1 $LCTL set_param fail_loc=0x31c
17946
17947         # ignore failure
17948         $LFS data_version $DIR/$tdir/$tfile || true
17949
17950         do_facet ost1 $LCTL set_param fail_loc=0
17951         umount_client $MOUNT || error "umount failed"
17952         mount_client $MOUNT || error "mount failed"
17953         stop ost1 || error "cannot stop ost1"
17954         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
17955 }
17956 run_test 232b "failed data version lock should not block umount"
17957
17958 test_233a() {
17959         [ $MDS1_VERSION -ge $(version_code 2.3.64) ] ||
17960                 skip "Need MDS version at least 2.3.64"
17961         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17962
17963         local fid=$($LFS path2fid $MOUNT)
17964
17965         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17966                 error "cannot access $MOUNT using its FID '$fid'"
17967 }
17968 run_test 233a "checking that OBF of the FS root succeeds"
17969
17970 test_233b() {
17971         [ $MDS1_VERSION -ge $(version_code 2.5.90) ] ||
17972                 skip "Need MDS version at least 2.5.90"
17973         [ -n "$FILESET" ] && skip_env "SKIP due to FILESET set"
17974
17975         local fid=$($LFS path2fid $MOUNT/.lustre)
17976
17977         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17978                 error "cannot access $MOUNT/.lustre using its FID '$fid'"
17979
17980         fid=$($LFS path2fid $MOUNT/.lustre/fid)
17981         stat $MOUNT/.lustre/fid/$fid > /dev/null ||
17982                 error "cannot access $MOUNT/.lustre/fid using its FID '$fid'"
17983 }
17984 run_test 233b "checking that OBF of the FS .lustre succeeds"
17985
17986 test_234() {
17987         local p="$TMP/sanityN-$TESTNAME.parameters"
17988         save_lustre_params client "llite.*.xattr_cache" > $p
17989         lctl set_param llite.*.xattr_cache 1 ||
17990                 skip_env "xattr cache is not supported"
17991
17992         mkdir -p $DIR/$tdir || error "mkdir failed"
17993         touch $DIR/$tdir/$tfile || error "touch failed"
17994         # OBD_FAIL_LLITE_XATTR_ENOMEM
17995         $LCTL set_param fail_loc=0x1405
17996         getfattr -n user.attr $DIR/$tdir/$tfile &&
17997                 error "getfattr should have failed with ENOMEM"
17998         $LCTL set_param fail_loc=0x0
17999         rm -rf $DIR/$tdir
18000
18001         restore_lustre_params < $p
18002         rm -f $p
18003 }
18004 run_test 234 "xattr cache should not crash on ENOMEM"
18005
18006 test_235() {
18007         [ $MDS1_VERSION -lt $(version_code 2.4.52) ] &&
18008                 skip "Need MDS version at least 2.4.52"
18009
18010         flock_deadlock $DIR/$tfile
18011         local RC=$?
18012         case $RC in
18013                 0)
18014                 ;;
18015                 124) error "process hangs on a deadlock"
18016                 ;;
18017                 *) error "error executing flock_deadlock $DIR/$tfile"
18018                 ;;
18019         esac
18020 }
18021 run_test 235 "LU-1715: flock deadlock detection does not work properly"
18022
18023 #LU-2935
18024 test_236() {
18025         check_swap_layouts_support
18026
18027         local ref1=/etc/passwd
18028         local ref2=/etc/group
18029         local file1=$DIR/$tdir/f1
18030         local file2=$DIR/$tdir/f2
18031
18032         test_mkdir -c1 $DIR/$tdir
18033         $LFS setstripe -c 1 $file1 || error "cannot setstripe on '$file1': rc = $?"
18034         cp $ref1 $file1 || error "cp $ref1 $file1 failed: rc = $?"
18035         $LFS setstripe -c 2 $file2 || error "cannot setstripe on '$file2': rc = $?"
18036         cp $ref2 $file2 || error "cp $ref2 $file2 failed: rc = $?"
18037         local fd=$(free_fd)
18038         local cmd="exec $fd<>$file2"
18039         eval $cmd
18040         rm $file2
18041         $LFS swap_layouts $file1 /proc/self/fd/${fd} ||
18042                 error "cannot swap layouts of '$file1' and /proc/self/fd/${fd}"
18043         cmd="exec $fd>&-"
18044         eval $cmd
18045         cmp $ref2 $file1 || error "content compare failed ($ref2 != $file1)"
18046
18047         #cleanup
18048         rm -rf $DIR/$tdir
18049 }
18050 run_test 236 "Layout swap on open unlinked file"
18051
18052 # LU-4659 linkea consistency
18053 test_238() {
18054         [[ $MDS1_VERSION -gt $(version_code 2.5.57) ]] ||
18055                 [[ $MDS1_VERSION -gt $(version_code 2.5.1) &&
18056                    $MDS1_VERSION -lt $(version_code 2.5.50) ]] ||
18057                 skip "Need MDS version at least 2.5.58 or 2.5.2+"
18058
18059         touch $DIR/$tfile
18060         ln $DIR/$tfile $DIR/$tfile.lnk
18061         touch $DIR/$tfile.new
18062         mv $DIR/$tfile.new $DIR/$tfile
18063         local fid1=$($LFS path2fid $DIR/$tfile)
18064         local fid2=$($LFS path2fid $DIR/$tfile.lnk)
18065         local path1=$($LFS fid2path $FSNAME "$fid1")
18066         [ $tfile == $path1 ] || error "linkea inconsistent: $tfile $fid1 $path1"
18067         local path2=$($LFS fid2path $FSNAME "$fid2")
18068         [ $tfile.lnk == $path2 ] ||
18069                 error "linkea inconsistent: $tfile.lnk $fid2 $path2!"
18070         rm -f $DIR/$tfile*
18071 }
18072 run_test 238 "Verify linkea consistency"
18073
18074 test_239A() { # was test_239
18075         [ $MDS1_VERSION -lt $(version_code 2.5.60) ] &&
18076                 skip "Need MDS version at least 2.5.60"
18077
18078         local list=$(comma_list $(mdts_nodes))
18079
18080         mkdir -p $DIR/$tdir
18081         createmany -o $DIR/$tdir/f- 5000
18082         unlinkmany $DIR/$tdir/f- 5000
18083         [ $MDS1_VERSION -gt $(version_code 2.10.4) ] &&
18084                 do_nodes $list "lctl set_param -n osp.*.force_sync=1"
18085         changes=$(do_nodes $list "lctl get_param -n osp.*MDT*.sync_changes \
18086                         osp.*MDT*.sync_in_flight" | calc_sum)
18087         [ "$changes" -eq 0 ] || error "$changes not synced"
18088 }
18089 run_test 239A "osp_sync test"
18090
18091 test_239a() { #LU-5297
18092         remote_mds_nodsh && skip "remote MDS with nodsh"
18093
18094         touch $DIR/$tfile
18095         #define OBD_FAIL_OSP_CHECK_INVALID_REC     0x2100
18096         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2100
18097         chgrp $RUNAS_GID $DIR/$tfile
18098         wait_delete_completed
18099 }
18100 run_test 239a "process invalid osp sync record correctly"
18101
18102 test_239b() { #LU-5297
18103         remote_mds_nodsh && skip "remote MDS with nodsh"
18104
18105         touch $DIR/$tfile1
18106         #define OBD_FAIL_OSP_CHECK_ENOMEM     0x2101
18107         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2101
18108         chgrp $RUNAS_GID $DIR/$tfile1
18109         wait_delete_completed
18110         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
18111         touch $DIR/$tfile2
18112         chgrp $RUNAS_GID $DIR/$tfile2
18113         wait_delete_completed
18114 }
18115 run_test 239b "process osp sync record with ENOMEM error correctly"
18116
18117 test_240() {
18118         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
18119         remote_mds_nodsh && skip "remote MDS with nodsh"
18120
18121         mkdir -p $DIR/$tdir
18122
18123         $LFS mkdir -i 0 $DIR/$tdir/d0 ||
18124                 error "failed to mkdir $DIR/$tdir/d0 on MDT0"
18125         $LFS mkdir -i 1 $DIR/$tdir/d0/d1 ||
18126                 error "failed to mkdir $DIR/$tdir/d0/d1 on MDT1"
18127
18128         umount_client $MOUNT || error "umount failed"
18129         #define OBD_FAIL_TGT_DELAY_CONDITIONAL   0x713
18130         do_facet mds2 lctl set_param fail_loc=0x713 fail_val=1
18131         mount_client $MOUNT || error "failed to mount client"
18132
18133         echo "stat $DIR/$tdir/d0/d1, should not fail/ASSERT"
18134         stat $DIR/$tdir/d0/d1 || error "fail to stat $DIR/$tdir/d0/d1"
18135 }
18136 run_test 240 "race between ldlm enqueue and the connection RPC (no ASSERT)"
18137
18138 test_241_bio() {
18139         local count=$1
18140         local bsize=$2
18141
18142         for LOOP in $(seq $count); do
18143                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 2>/dev/null
18144                 cancel_lru_locks $OSC || true
18145         done
18146 }
18147
18148 test_241_dio() {
18149         local count=$1
18150         local bsize=$2
18151
18152         for LOOP in $(seq $1); do
18153                 dd if=$DIR/$tfile of=/dev/null bs=$bsize count=1 iflag=direct \
18154                         2>/dev/null
18155         done
18156 }
18157
18158 test_241a() { # was test_241
18159         local bsize=$PAGE_SIZE
18160
18161         (( bsize < 40960 )) && bsize=40960
18162         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
18163         ls -la $DIR/$tfile
18164         cancel_lru_locks $OSC
18165         test_241_bio 1000 $bsize &
18166         PID=$!
18167         test_241_dio 1000 $bsize
18168         wait $PID
18169 }
18170 run_test 241a "bio vs dio"
18171
18172 test_241b() {
18173         local bsize=$PAGE_SIZE
18174
18175         (( bsize < 40960 )) && bsize=40960
18176         dd if=/dev/zero of=$DIR/$tfile count=1 bs=$bsize
18177         ls -la $DIR/$tfile
18178         test_241_dio 1000 $bsize &
18179         PID=$!
18180         test_241_dio 1000 $bsize
18181         wait $PID
18182 }
18183 run_test 241b "dio vs dio"
18184
18185 test_242() {
18186         remote_mds_nodsh && skip "remote MDS with nodsh"
18187
18188         mkdir -p $DIR/$tdir
18189         touch $DIR/$tdir/$tfile
18190
18191         #define OBD_FAIL_MDS_READPAGE_PACK      0x105
18192         do_facet mds1 lctl set_param fail_loc=0x105
18193         /bin/ls $DIR/$tdir && error "ls $DIR/$tdir should fail"
18194
18195         do_facet mds1 lctl set_param fail_loc=0
18196         /bin/ls $DIR/$tdir || error "ls $DIR/$tdir failed"
18197 }
18198 run_test 242 "mdt_readpage failure should not cause directory unreadable"
18199
18200 test_243()
18201 {
18202         test_mkdir $DIR/$tdir
18203         group_lock_test -d $DIR/$tdir || error "A group lock test failed"
18204 }
18205 run_test 243 "various group lock tests"
18206
18207 test_244a()
18208 {
18209         test_mkdir $DIR/$tdir
18210         dd if=/dev/zero of=$DIR/$tdir/$tfile bs=1M count=35
18211         sendfile_grouplock $DIR/$tdir/$tfile || \
18212                 error "sendfile+grouplock failed"
18213         rm -rf $DIR/$tdir
18214 }
18215 run_test 244a "sendfile with group lock tests"
18216
18217 test_244b()
18218 {
18219         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
18220
18221         local threads=50
18222         local size=$((1024*1024))
18223
18224         test_mkdir $DIR/$tdir
18225         for i in $(seq 1 $threads); do
18226                 local file=$DIR/$tdir/file_$((i / 10))
18227                 $MULTIOP $file OG1234w$size_$((i % 3))w$size_$((i % 4))g1234c &
18228                 local pids[$i]=$!
18229         done
18230         for i in $(seq 1 $threads); do
18231                 wait ${pids[$i]}
18232         done
18233 }
18234 run_test 244b "multi-threaded write with group lock"
18235
18236 test_245() {
18237         local flagname="multi_mod_rpcs"
18238         local connect_data_name="max_mod_rpcs"
18239         local out
18240
18241         # check if multiple modify RPCs flag is set
18242         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import |
18243                 grep "connect_flags:")
18244         echo "$out"
18245
18246         echo "$out" | grep -qw $flagname
18247         if [ $? -ne 0 ]; then
18248                 echo "connect flag $flagname is not set"
18249                 return
18250         fi
18251
18252         # check if multiple modify RPCs data is set
18253         out=$($LCTL get_param mdc.$FSNAME-MDT0000-*.import)
18254         echo "$out"
18255
18256         echo "$out" | grep -qw $connect_data_name ||
18257                 error "import should have connect data $connect_data_name"
18258 }
18259 run_test 245 "check mdc connection flag/data: multiple modify RPCs"
18260
18261 cleanup_247() {
18262         local submount=$1
18263
18264         trap 0
18265         umount_client $submount
18266         rmdir $submount
18267 }
18268
18269 test_247a() {
18270         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18271                 grep -q subtree ||
18272                 skip_env "Fileset feature is not supported"
18273
18274         local submount=${MOUNT}_$tdir
18275
18276         mkdir $MOUNT/$tdir
18277         mkdir -p $submount || error "mkdir $submount failed"
18278         FILESET="$FILESET/$tdir" mount_client $submount ||
18279                 error "mount $submount failed"
18280         trap "cleanup_247 $submount" EXIT
18281         echo foo > $submount/$tfile || error "write $submount/$tfile failed"
18282         [ $(cat $MOUNT/$tdir/$tfile) = "foo" ] ||
18283                 error "read $MOUNT/$tdir/$tfile failed"
18284         cleanup_247 $submount
18285 }
18286 run_test 247a "mount subdir as fileset"
18287
18288 test_247b() {
18289         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18290                 skip_env "Fileset feature is not supported"
18291
18292         local submount=${MOUNT}_$tdir
18293
18294         rm -rf $MOUNT/$tdir
18295         mkdir -p $submount || error "mkdir $submount failed"
18296         SKIP_FILESET=1
18297         FILESET="$FILESET/$tdir" mount_client $submount &&
18298                 error "mount $submount should fail"
18299         rmdir $submount
18300 }
18301 run_test 247b "mount subdir that dose not exist"
18302
18303 test_247c() {
18304         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18305                 skip_env "Fileset feature is not supported"
18306
18307         local submount=${MOUNT}_$tdir
18308
18309         mkdir -p $MOUNT/$tdir/dir1
18310         mkdir -p $submount || error "mkdir $submount failed"
18311         trap "cleanup_247 $submount" EXIT
18312         FILESET="$FILESET/$tdir" mount_client $submount ||
18313                 error "mount $submount failed"
18314         local fid=$($LFS path2fid $MOUNT/)
18315         $LFS fid2path $submount $fid && error "fid2path should fail"
18316         cleanup_247 $submount
18317 }
18318 run_test 247c "running fid2path outside root"
18319
18320 test_247d() {
18321         lctl get_param -n mdc.$FSNAME-MDT0000*.import | grep -q subtree ||
18322                 skip "Fileset feature is not supported"
18323
18324         local submount=${MOUNT}_$tdir
18325
18326         mkdir -p $MOUNT/$tdir/dir1
18327         mkdir -p $submount || error "mkdir $submount failed"
18328         FILESET="$FILESET/$tdir" mount_client $submount ||
18329                 error "mount $submount failed"
18330         trap "cleanup_247 $submount" EXIT
18331         local fid=$($LFS path2fid $submount/dir1)
18332         $LFS fid2path $submount $fid || error "fid2path should succeed"
18333         cleanup_247 $submount
18334 }
18335 run_test 247d "running fid2path inside root"
18336
18337 # LU-8037
18338 test_247e() {
18339         lctl get_param -n mdc.$FSNAME-MDT0000*.import |
18340                 grep -q subtree ||
18341                 skip "Fileset feature is not supported"
18342
18343         local submount=${MOUNT}_$tdir
18344
18345         mkdir $MOUNT/$tdir
18346         mkdir -p $submount || error "mkdir $submount failed"
18347         FILESET="$FILESET/.." mount_client $submount &&
18348                 error "mount $submount should fail"
18349         rmdir $submount
18350 }
18351 run_test 247e "mount .. as fileset"
18352
18353 test_248a() {
18354         local fast_read_sav=$($LCTL get_param -n llite.*.fast_read 2>/dev/null)
18355         [ -z "$fast_read_sav" ] && skip "no fast read support"
18356
18357         # create a large file for fast read verification
18358         dd if=/dev/zero of=$DIR/$tfile bs=1M count=128 > /dev/null 2>&1
18359
18360         # make sure the file is created correctly
18361         $CHECKSTAT -s $((128*1024*1024)) $DIR/$tfile ||
18362                 { rm -f $DIR/$tfile; skip "file creation error"; }
18363
18364         echo "Test 1: verify that fast read is 4 times faster on cache read"
18365
18366         # small read with fast read enabled
18367         $LCTL set_param -n llite.*.fast_read=1
18368         local t_fast=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
18369                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18370                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18371         # small read with fast read disabled
18372         $LCTL set_param -n llite.*.fast_read=0
18373         local t_slow=$(dd if=$DIR/$tfile of=/dev/null bs=4k 2>&1 |
18374                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18375                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18376
18377         # verify that fast read is 4 times faster for cache read
18378         [ $(bc <<< "4 * $t_fast < $t_slow") -eq 1 ] ||
18379                 error_not_in_vm "fast read was not 4 times faster: " \
18380                            "$t_fast vs $t_slow"
18381
18382         echo "Test 2: verify the performance between big and small read"
18383         $LCTL set_param -n llite.*.fast_read=1
18384
18385         # 1k non-cache read
18386         cancel_lru_locks osc
18387         local t_1k=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
18388                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18389                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18390
18391         # 1M non-cache read
18392         cancel_lru_locks osc
18393         local t_1m=$(dd if=$DIR/$tfile of=/dev/null bs=1k 2>&1 |
18394                 egrep -o '([[:digit:]\.\,e-]+) s' | cut -d's' -f1 |
18395                 sed -e 's/,/./' -e 's/[eE]+*/\*10\^/')
18396
18397         # verify that big IO is not 4 times faster than small IO
18398         [ $(bc <<< "4 * $t_1k >= $t_1m") -eq 1 ] ||
18399                 error_not_in_vm "bigger IO is way too fast: $t_1k vs $t_1m"
18400
18401         $LCTL set_param -n llite.*.fast_read=$fast_read_sav
18402         rm -f $DIR/$tfile
18403 }
18404 run_test 248a "fast read verification"
18405
18406 test_248b() {
18407         # Default short_io_bytes=16384, try both smaller and larger sizes.
18408         # Lustre O_DIRECT read and write needs to be a multiple of PAGE_SIZE.
18409         # 6017024 = 2^12*13*113 = 47008*128 = 11752*512 = 4096*1469 = 53248*113
18410         echo "bs=53248 count=113 normal buffered write"
18411         dd if=/dev/urandom of=$TMP/$tfile.0 bs=53248 count=113 ||
18412                 error "dd of initial data file failed"
18413         stack_trap "rm -f $DIR/$tfile.[0-3] $TMP/$tfile.[0-3]" EXIT
18414
18415         echo "bs=47008 count=128 oflag=dsync normal write $tfile.0"
18416         dd if=$TMP/$tfile.0 of=$DIR/$tfile.0 bs=47008 count=128 oflag=dsync ||
18417                 error "dd with sync normal writes failed"
18418         cmp $TMP/$tfile.0 $DIR/$tfile.0 || error "compare $DIR/$tfile.0 failed"
18419
18420         echo "bs=11752 count=512 oflag=dsync small write $tfile.1"
18421         dd if=$TMP/$tfile.0 of=$DIR/$tfile.1 bs=11752 count=512 oflag=dsync ||
18422                 error "dd with sync small writes failed"
18423         cmp $TMP/$tfile.0 $DIR/$tfile.1 || error "compare $DIR/$tfile.1 failed"
18424
18425         cancel_lru_locks osc
18426
18427         # calculate the small O_DIRECT size and count for the client PAGE_SIZE
18428         local num=$((13 * 113 / (PAGE_SIZE / 4096)))
18429         echo "bs=$PAGE_SIZE count=$num iflag=direct small read $tfile.1"
18430         dd if=$DIR/$tfile.1 of=$TMP/$tfile.1 bs=$PAGE_SIZE count=$num \
18431                 iflag=direct || error "dd with O_DIRECT small read failed"
18432         # adjust bytes checked to handle larger PAGE_SIZE for ARM/PPC
18433         cmp --bytes=$((PAGE_SIZE * num)) $TMP/$tfile.0 $TMP/$tfile.1 ||
18434                 error "compare $TMP/$tfile.1 failed"
18435
18436         local save=$($LCTL get_param -n osc.*OST000*.short_io_bytes | head -n 1)
18437         stack_trap "$LCTL set_param osc.$FSNAME-*.short_io_bytes=$save" EXIT
18438
18439         # just to see what the maximum tunable value is, and test parsing
18440         echo "test invalid parameter 2MB"
18441         $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=2M &&
18442                 error "too-large short_io_bytes allowed"
18443         echo "test maximum parameter 512KB"
18444         # if we can set a larger short_io_bytes, run test regardless of version
18445         if ! $LCTL set_param osc.$FSNAME-OST0000*.short_io_bytes=512K; then
18446                 # older clients may not allow setting it this large, that's OK
18447                 [ $CLIENT_VERSION -ge $(version_code 2.13.50) ] ||
18448                         skip "Need at least client version 2.13.50"
18449                 error "medium short_io_bytes failed"
18450         fi
18451         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18452         size=$($LCTL get_param -n osc.$FSNAME-OST0000*.short_io_bytes)
18453
18454         echo "test large parameter 64KB"
18455         $LCTL set_param osc.$FSNAME-*.short_io_bytes=65536
18456         $LCTL get_param osc.$FSNAME-OST0000*.short_io_bytes
18457
18458         echo "bs=47008 count=128 oflag=dsync large write $tfile.2"
18459         dd if=$TMP/$tfile.0 of=$DIR/$tfile.2 bs=47008 count=128 oflag=dsync ||
18460                 error "dd with sync large writes failed"
18461         cmp $TMP/$tfile.0 $DIR/$tfile.2 || error "compare $DIR/$tfile.2 failed"
18462
18463         # calculate the large O_DIRECT size and count for the client PAGE_SIZE
18464         local size=$(((4096 * 13 + PAGE_SIZE - 1) / PAGE_SIZE * PAGE_SIZE))
18465         num=$((113 * 4096 / PAGE_SIZE))
18466         echo "bs=$size count=$num oflag=direct large write $tfile.3"
18467         dd if=$TMP/$tfile.0 of=$DIR/$tfile.3 bs=$size count=$num oflag=direct ||
18468                 error "dd with O_DIRECT large writes failed"
18469         cmp --bytes=$((size * num)) $TMP/$tfile.0 $DIR/$tfile.3 ||
18470                 error "compare $DIR/$tfile.3 failed"
18471
18472         cancel_lru_locks osc
18473
18474         echo "bs=$size count=$num iflag=direct large read $tfile.2"
18475         dd if=$DIR/$tfile.2 of=$TMP/$tfile.2 bs=$size count=$num iflag=direct ||
18476                 error "dd with O_DIRECT large read failed"
18477         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.2 ||
18478                 error "compare $TMP/$tfile.2 failed"
18479
18480         echo "bs=$size count=$num iflag=direct large read $tfile.3"
18481         dd if=$DIR/$tfile.3 of=$TMP/$tfile.3 bs=$size count=$num iflag=direct ||
18482                 error "dd with O_DIRECT large read failed"
18483         cmp --bytes=$((size * num)) $TMP/$tfile.0 $TMP/$tfile.3 ||
18484                 error "compare $TMP/$tfile.3 failed"
18485 }
18486 run_test 248b "test short_io read and write for both small and large sizes"
18487
18488 test_249() { # LU-7890
18489         [ $MDS1_VERSION -lt $(version_code 2.8.53) ] &&
18490                 skip "Need at least version 2.8.54"
18491
18492         rm -f $DIR/$tfile
18493         $LFS setstripe -c 1 $DIR/$tfile
18494         # Offset 2T == 4k * 512M
18495         dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 seek=512M ||
18496                 error "dd to 2T offset failed"
18497 }
18498 run_test 249 "Write above 2T file size"
18499
18500 test_250() {
18501         [ "$(facet_fstype ost$(($($LFS getstripe -i $DIR/$tfile) + 1)))" = "zfs" ] \
18502          && skip "no 16TB file size limit on ZFS"
18503
18504         $LFS setstripe -c 1 $DIR/$tfile
18505         # ldiskfs extent file size limit is (16TB - 4KB - 1) bytes
18506         local size=$((16 * 1024 * 1024 * 1024 * 1024 - 4096 - 1))
18507         $TRUNCATE $DIR/$tfile $size || error "truncate $tfile to $size failed"
18508         dd if=/dev/zero of=$DIR/$tfile bs=10 count=1 oflag=append \
18509                 conv=notrunc,fsync && error "append succeeded"
18510         return 0
18511 }
18512 run_test 250 "Write above 16T limit"
18513
18514 test_251() {
18515         $LFS setstripe -c -1 -S 1048576 $DIR/$tfile
18516
18517         #define OBD_FAIL_LLITE_LOST_LAYOUT 0x1407
18518         #Skip once - writing the first stripe will succeed
18519         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18520         $MULTIOP $DIR/$tfile o:O_RDWR:w2097152c 2>&1 | grep -q "short write" &&
18521                 error "short write happened"
18522
18523         $LCTL set_param fail_loc=0xa0001407 fail_val=1
18524         $MULTIOP $DIR/$tfile or2097152c 2>&1 | grep -q "short read" &&
18525                 error "short read happened"
18526
18527         rm -f $DIR/$tfile
18528 }
18529 run_test 251 "Handling short read and write correctly"
18530
18531 test_252() {
18532         remote_mds_nodsh && skip "remote MDS with nodsh"
18533         remote_ost_nodsh && skip "remote OST with nodsh"
18534         if [ "$ost1_FSTYPE" != ldiskfs ] || [ "$mds1_FSTYPE" != ldiskfs ]; then
18535                 skip_env "ldiskfs only test"
18536         fi
18537
18538         local tgt
18539         local dev
18540         local out
18541         local uuid
18542         local num
18543         local gen
18544
18545         # check lr_reader on OST0000
18546         tgt=ost1
18547         dev=$(facet_device $tgt)
18548         out=$(do_facet $tgt $LR_READER $dev)
18549         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18550         echo "$out"
18551         uuid=$(echo "$out" | grep -i uuid | awk '{ print $2 }')
18552         [ "$uuid" == "$(ostuuid_from_index 0)" ] ||
18553                 error "Invalid uuid returned by $LR_READER on target $tgt"
18554         echo -e "uuid returned by $LR_READER is '$uuid'\n"
18555
18556         # check lr_reader -c on MDT0000
18557         tgt=mds1
18558         dev=$(facet_device $tgt)
18559         if ! do_facet $tgt $LR_READER -h | grep -q OPTIONS; then
18560                 skip "$LR_READER does not support additional options"
18561         fi
18562         out=$(do_facet $tgt $LR_READER -c $dev)
18563         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18564         echo "$out"
18565         num=$(echo "$out" | grep -c "mdtlov")
18566         [ "$num" -eq $((MDSCOUNT - 1)) ] ||
18567                 error "Invalid number of mdtlov clients returned by $LR_READER"
18568         echo -e "Number of mdtlov clients returned by $LR_READER is '$num'\n"
18569
18570         # check lr_reader -cr on MDT0000
18571         out=$(do_facet $tgt $LR_READER -cr $dev)
18572         [ $? -eq 0 ] || error "$LR_READER failed on target $tgt device $dev"
18573         echo "$out"
18574         echo "$out" | grep -q "^reply_data:$" ||
18575                 error "$LR_READER should have returned 'reply_data' section"
18576         num=$(echo "$out" | grep -c "client_generation")
18577         echo -e "Number of reply data returned by $LR_READER is '$num'\n"
18578 }
18579 run_test 252 "check lr_reader tool"
18580
18581 test_253() {
18582         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18583         remote_mds_nodsh && skip "remote MDS with nodsh"
18584         remote_mgs_nodsh && skip "remote MGS with nodsh"
18585
18586         local ostidx=0
18587         local rc=0
18588         local ost_name=$(ostname_from_index $ostidx)
18589
18590         # on the mdt's osc
18591         local mdtosc_proc1=$(get_mdtosc_proc_path $SINGLEMDS $ost_name)
18592         do_facet $SINGLEMDS $LCTL get_param -n \
18593                 osp.$mdtosc_proc1.reserved_mb_high ||
18594                 skip  "remote MDS does not support reserved_mb_high"
18595
18596         rm -rf $DIR/$tdir
18597         wait_mds_ost_sync
18598         wait_delete_completed
18599         mkdir $DIR/$tdir
18600
18601         pool_add $TESTNAME || error "Pool creation failed"
18602         pool_add_targets $TESTNAME 0 || error "Pool add targets failed"
18603
18604         $LFS setstripe $DIR/$tdir -i $ostidx -c 1 -p $FSNAME.$TESTNAME ||
18605                 error "Setstripe failed"
18606
18607         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M count=10
18608
18609         local wms=$(ost_watermarks_set_enospc $tfile $ostidx |
18610                     grep "watermarks")
18611         stack_trap "ost_watermarks_clear_enospc $tfile $ostidx $wms" EXIT
18612
18613         local oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18614                         osp.$mdtosc_proc1.prealloc_status)
18615         echo "prealloc_status $oa_status"
18616
18617         dd if=/dev/zero of=$DIR/$tdir/$tfile.1 bs=1M count=1 &&
18618                 error "File creation should fail"
18619
18620         #object allocation was stopped, but we still able to append files
18621         dd if=/dev/zero of=$DIR/$tdir/$tfile.0 bs=1M seek=6 count=5 \
18622                 oflag=append || error "Append failed"
18623
18624         rm -f $DIR/$tdir/$tfile.0
18625
18626         # For this test, we want to delete the files we created to go out of
18627         # space but leave the watermark, so we remain nearly out of space
18628         ost_watermarks_enospc_delete_files $tfile $ostidx
18629
18630         wait_delete_completed
18631
18632         sleep_maxage
18633
18634         for i in $(seq 10 12); do
18635                 dd if=/dev/zero of=$DIR/$tdir/$tfile.$i bs=1M count=1 \
18636                         2>/dev/null || error "File creation failed after rm"
18637         done
18638
18639         oa_status=$(do_facet $SINGLEMDS $LCTL get_param -n \
18640                         osp.$mdtosc_proc1.prealloc_status)
18641         echo "prealloc_status $oa_status"
18642
18643         if (( oa_status != 0 )); then
18644                 error "Object allocation still disable after rm"
18645         fi
18646 }
18647 run_test 253 "Check object allocation limit"
18648
18649 test_254() {
18650         [ $PARALLEL == "yes" ] && skip "skip parallel run"
18651         remote_mds_nodsh && skip "remote MDS with nodsh"
18652         do_facet $SINGLEMDS $LCTL get_param -n mdd.$MDT0.changelog_size ||
18653                 skip "MDS does not support changelog_size"
18654
18655         local cl_user
18656         local MDT0=$(facet_svc $SINGLEMDS)
18657
18658         changelog_register || error "changelog_register failed"
18659
18660         changelog_clear 0 || error "changelog_clear failed"
18661
18662         local size1=$(do_facet $SINGLEMDS \
18663                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18664         echo "Changelog size $size1"
18665
18666         rm -rf $DIR/$tdir
18667         $LFS mkdir -i 0 $DIR/$tdir
18668         # change something
18669         mkdir -p $DIR/$tdir/pics/2008/zachy
18670         touch $DIR/$tdir/pics/2008/zachy/timestamp
18671         cp /etc/hosts $DIR/$tdir/pics/2008/zachy/pic1.jpg
18672         mv $DIR/$tdir/pics/2008/zachy $DIR/$tdir/pics/zach
18673         ln $DIR/$tdir/pics/zach/pic1.jpg $DIR/$tdir/pics/2008/portland.jpg
18674         ln -s $DIR/$tdir/pics/2008/portland.jpg $DIR/$tdir/pics/desktop.jpg
18675         rm $DIR/$tdir/pics/desktop.jpg
18676
18677         local size2=$(do_facet $SINGLEMDS \
18678                       $LCTL get_param -n mdd.$MDT0.changelog_size)
18679         echo "Changelog size after work $size2"
18680
18681         (( $size2 > $size1 )) ||
18682                 error "new Changelog size=$size2 less than old size=$size1"
18683 }
18684 run_test 254 "Check changelog size"
18685
18686 ladvise_no_type()
18687 {
18688         local type=$1
18689         local file=$2
18690
18691         lfs ladvise -a invalid $file 2>&1 | grep "Valid types" |
18692                 awk -F: '{print $2}' | grep $type > /dev/null
18693         if [ $? -ne 0 ]; then
18694                 return 0
18695         fi
18696         return 1
18697 }
18698
18699 ladvise_no_ioctl()
18700 {
18701         local file=$1
18702
18703         lfs ladvise -a willread $file > /dev/null 2>&1
18704         if [ $? -eq 0 ]; then
18705                 return 1
18706         fi
18707
18708         lfs ladvise -a willread $file 2>&1 |
18709                 grep "Inappropriate ioctl for device" > /dev/null
18710         if [ $? -eq 0 ]; then
18711                 return 0
18712         fi
18713         return 1
18714 }
18715
18716 percent() {
18717         bc <<<"scale=2; ($1 - $2) * 100 / $2"
18718 }
18719
18720 # run a random read IO workload
18721 # usage: random_read_iops <filename> <filesize> <iosize>
18722 random_read_iops() {
18723         local file=$1
18724         local fsize=$2
18725         local iosize=${3:-4096}
18726
18727         $READS -f $file -s $fsize -b $iosize -n $((fsize / iosize)) -t 60 |
18728                 sed -e '/^$/d' -e 's#.*s, ##' -e 's#MB/s##'
18729 }
18730
18731 drop_file_oss_cache() {
18732         local file="$1"
18733         local nodes="$2"
18734
18735         $LFS ladvise -a dontneed $file 2>/dev/null ||
18736                 do_nodes $nodes "echo 3 > /proc/sys/vm/drop_caches"
18737 }
18738
18739 ladvise_willread_performance()
18740 {
18741         local repeat=10
18742         local average_origin=0
18743         local average_cache=0
18744         local average_ladvise=0
18745
18746         for ((i = 1; i <= $repeat; i++)); do
18747                 echo "Iter $i/$repeat: reading without willread hint"
18748                 cancel_lru_locks osc
18749                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18750                 local speed_origin=$(random_read_iops $DIR/$tfile $size)
18751                 echo "Iter $i/$repeat: uncached speed: $speed_origin"
18752                 average_origin=$(bc <<<"$average_origin + $speed_origin")
18753
18754                 cancel_lru_locks osc
18755                 local speed_cache=$(random_read_iops $DIR/$tfile $size)
18756                 echo "Iter $i/$repeat: OSS cache speed: $speed_cache"
18757                 average_cache=$(bc <<<"$average_cache + $speed_cache")
18758
18759                 cancel_lru_locks osc
18760                 drop_file_oss_cache $DIR/$tfile $(comma_list $(osts_nodes))
18761                 $LFS ladvise -a willread $DIR/$tfile || error "ladvise failed"
18762                 local speed_ladvise=$(random_read_iops $DIR/$tfile $size)
18763                 echo "Iter $i/$repeat: ladvise speed: $speed_ladvise"
18764                 average_ladvise=$(bc <<<"$average_ladvise + $speed_ladvise")
18765         done
18766         average_origin=$(bc <<<"scale=2; $average_origin / $repeat")
18767         average_cache=$(bc <<<"scale=2; $average_cache / $repeat")
18768         average_ladvise=$(bc <<<"scale=2; $average_ladvise / $repeat")
18769
18770         speedup_cache=$(percent $average_cache $average_origin)
18771         speedup_ladvise=$(percent $average_ladvise $average_origin)
18772
18773         echo "Average uncached read: $average_origin"
18774         echo "Average speedup with OSS cached read: " \
18775                 "$average_cache = +$speedup_cache%"
18776         echo "Average speedup with ladvise willread: " \
18777                 "$average_ladvise = +$speedup_ladvise%"
18778
18779         local lowest_speedup=20
18780         if [ ${average_cache%.*} -lt $lowest_speedup ]; then
18781                 echo "Speedup with OSS cached read less than $lowest_speedup%," \
18782                         "got $average_cache%. Skipping ladvise willread check."
18783                 return 0
18784         fi
18785
18786         # the test won't work on ZFS until it supports 'ladvise dontneed', but
18787         # it is still good to run until then to exercise 'ladvise willread'
18788         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18789                 [ "$ost1_FSTYPE" = "zfs" ] &&
18790                 echo "osd-zfs does not support dontneed or drop_caches" &&
18791                 return 0
18792
18793         lowest_speedup=$(bc <<<"scale=2; $average_cache / 2")
18794         [ ${average_ladvise%.*} -gt $lowest_speedup ] ||
18795                 error_not_in_vm "Speedup with willread is less than " \
18796                         "$lowest_speedup%, got $average_ladvise%"
18797 }
18798
18799 test_255a() {
18800         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18801                 skip "lustre < 2.8.54 does not support ladvise "
18802         remote_ost_nodsh && skip "remote OST with nodsh"
18803
18804         lfs setstripe -c -1 -i 0 $DIR/$tfile || error "$tfile failed"
18805
18806         ladvise_no_type willread $DIR/$tfile &&
18807                 skip "willread ladvise is not supported"
18808
18809         ladvise_no_ioctl $DIR/$tfile &&
18810                 skip "ladvise ioctl is not supported"
18811
18812         local size_mb=100
18813         local size=$((size_mb * 1048576))
18814         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18815                 error "dd to $DIR/$tfile failed"
18816
18817         lfs ladvise -a willread $DIR/$tfile ||
18818                 error "Ladvise failed with no range argument"
18819
18820         lfs ladvise -a willread -s 0 $DIR/$tfile ||
18821                 error "Ladvise failed with no -l or -e argument"
18822
18823         lfs ladvise -a willread -e 1 $DIR/$tfile ||
18824                 error "Ladvise failed with only -e argument"
18825
18826         lfs ladvise -a willread -l 1 $DIR/$tfile ||
18827                 error "Ladvise failed with only -l argument"
18828
18829         lfs ladvise -a willread -s 2 -e 1 $DIR/$tfile &&
18830                 error "End offset should not be smaller than start offset"
18831
18832         lfs ladvise -a willread -s 2 -e 2 $DIR/$tfile &&
18833                 error "End offset should not be equal to start offset"
18834
18835         lfs ladvise -a willread -s $size -l 1 $DIR/$tfile ||
18836                 error "Ladvise failed with overflowing -s argument"
18837
18838         lfs ladvise -a willread -s 1 -e $((size + 1)) $DIR/$tfile ||
18839                 error "Ladvise failed with overflowing -e argument"
18840
18841         lfs ladvise -a willread -s 1 -l $size $DIR/$tfile ||
18842                 error "Ladvise failed with overflowing -l argument"
18843
18844         lfs ladvise -a willread -l 1 -e 2 $DIR/$tfile &&
18845                 error "Ladvise succeeded with conflicting -l and -e arguments"
18846
18847         echo "Synchronous ladvise should wait"
18848         local delay=4
18849 #define OBD_FAIL_OST_LADVISE_PAUSE       0x237
18850         do_nodes $(comma_list $(osts_nodes)) \
18851                 $LCTL set_param fail_val=$delay fail_loc=0x237
18852
18853         local start_ts=$SECONDS
18854         lfs ladvise -a willread $DIR/$tfile ||
18855                 error "Ladvise failed with no range argument"
18856         local end_ts=$SECONDS
18857         local inteval_ts=$((end_ts - start_ts))
18858
18859         if [ $inteval_ts -lt $(($delay - 1)) ]; then
18860                 error "Synchronous advice didn't wait reply"
18861         fi
18862
18863         echo "Asynchronous ladvise shouldn't wait"
18864         local start_ts=$SECONDS
18865         lfs ladvise -a willread -b $DIR/$tfile ||
18866                 error "Ladvise failed with no range argument"
18867         local end_ts=$SECONDS
18868         local inteval_ts=$((end_ts - start_ts))
18869
18870         if [ $inteval_ts -gt $(($delay / 2)) ]; then
18871                 error "Asynchronous advice blocked"
18872         fi
18873
18874         do_nodes $(comma_list $(osts_nodes)) $LCTL set_param fail_loc=0
18875         ladvise_willread_performance
18876 }
18877 run_test 255a "check 'lfs ladvise -a willread'"
18878
18879 facet_meminfo() {
18880         local facet=$1
18881         local info=$2
18882
18883         do_facet $facet "cat /proc/meminfo | grep ^${info}:" | awk '{print $2}'
18884 }
18885
18886 test_255b() {
18887         [ $OST1_VERSION -lt $(version_code 2.8.54) ] &&
18888                 skip "lustre < 2.8.54 does not support ladvise "
18889         remote_ost_nodsh && skip "remote OST with nodsh"
18890
18891         lfs setstripe -c 1 -i 0 $DIR/$tfile
18892
18893         ladvise_no_type dontneed $DIR/$tfile &&
18894                 skip "dontneed ladvise is not supported"
18895
18896         ladvise_no_ioctl $DIR/$tfile &&
18897                 skip "ladvise ioctl is not supported"
18898
18899         ! $LFS ladvise -a dontneed $DIR/$tfile &&
18900                 [ "$ost1_FSTYPE" = "zfs" ] &&
18901                 skip "zfs-osd does not support 'ladvise dontneed'"
18902
18903         local size_mb=100
18904         local size=$((size_mb * 1048576))
18905         # In order to prevent disturbance of other processes, only check 3/4
18906         # of the memory usage
18907         local kibibytes=$((size_mb * 1024 * 3 / 4))
18908
18909         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=$size_mb ||
18910                 error "dd to $DIR/$tfile failed"
18911
18912         #force write to complete before dropping OST cache & checking memory
18913         sync
18914
18915         local total=$(facet_meminfo ost1 MemTotal)
18916         echo "Total memory: $total KiB"
18917
18918         do_facet ost1 "sync && echo 3 > /proc/sys/vm/drop_caches"
18919         local before_read=$(facet_meminfo ost1 Cached)
18920         echo "Cache used before read: $before_read KiB"
18921
18922         lfs ladvise -a willread $DIR/$tfile ||
18923                 error "Ladvise willread failed"
18924         local after_read=$(facet_meminfo ost1 Cached)
18925         echo "Cache used after read: $after_read KiB"
18926
18927         lfs ladvise -a dontneed $DIR/$tfile ||
18928                 error "Ladvise dontneed again failed"
18929         local no_read=$(facet_meminfo ost1 Cached)
18930         echo "Cache used after dontneed ladvise: $no_read KiB"
18931
18932         if [ $total -lt $((before_read + kibibytes)) ]; then
18933                 echo "Memory is too small, abort checking"
18934                 return 0
18935         fi
18936
18937         if [ $((before_read + kibibytes)) -gt $after_read ]; then
18938                 error "Ladvise willread should use more memory" \
18939                         "than $kibibytes KiB"
18940         fi
18941
18942         if [ $((no_read + kibibytes)) -gt $after_read ]; then
18943                 error "Ladvise dontneed should release more memory" \
18944                         "than $kibibytes KiB"
18945         fi
18946 }
18947 run_test 255b "check 'lfs ladvise -a dontneed'"
18948
18949 test_255c() {
18950         [ $OST1_VERSION -lt $(version_code 2.10.50) ] &&
18951                 skip "lustre < 2.10.50 does not support lockahead"
18952
18953         local count
18954         local new_count
18955         local difference
18956         local i
18957         local rc
18958
18959         test_mkdir -p $DIR/$tdir
18960         $LFS setstripe -i 0 -c 1 $DIR/$tdir
18961
18962         #test 10 returns only success/failure
18963         i=10
18964         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18965         rc=$?
18966         if [ $rc -eq 255 ]; then
18967                 error "Ladvise test${i} failed, ${rc}"
18968         fi
18969
18970         #test 11 counts lock enqueue requests, all others count new locks
18971         i=11
18972         count=$(do_facet ost1 \
18973                 $LCTL get_param -n ost.OSS.ost.stats)
18974         count=$(echo "$count" | grep ldlm_extent_enqueue | awk '{ print $2 }')
18975
18976         lockahead_test -d $DIR/$tdir -t $i -f $tfile
18977         rc=$?
18978         if [ $rc -eq 255 ]; then
18979                 error "Ladvise test${i} failed, ${rc}"
18980         fi
18981
18982         new_count=$(do_facet ost1 \
18983                 $LCTL get_param -n ost.OSS.ost.stats)
18984         new_count=$(echo "$new_count" | grep ldlm_extent_enqueue | \
18985                    awk '{ print $2 }')
18986
18987         difference="$((new_count - count))"
18988         if [ $difference -ne $rc ]; then
18989                 error "Ladvise test${i}, bad enqueue count, returned " \
18990                       "${rc}, actual ${difference}"
18991         fi
18992
18993         for i in $(seq 12 21); do
18994                 # If we do not do this, we run the risk of having too many
18995                 # locks and starting lock cancellation while we are checking
18996                 # lock counts.
18997                 cancel_lru_locks osc
18998
18999                 count=$($LCTL get_param -n \
19000                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
19001
19002                 lockahead_test -d $DIR/$tdir -t $i -f $tfile
19003                 rc=$?
19004                 if [ $rc -eq 255 ]; then
19005                         error "Ladvise test ${i} failed, ${rc}"
19006                 fi
19007
19008                 new_count=$($LCTL get_param -n \
19009                        ldlm.namespaces.$FSNAME-OST0000*osc-[-0-9a-f]*.lock_unused_count)
19010                 difference="$((new_count - count))"
19011
19012                 # Test 15 output is divided by 100 to map down to valid return
19013                 if [ $i -eq 15 ]; then
19014                         rc="$((rc * 100))"
19015                 fi
19016
19017                 if [ $difference -ne $rc ]; then
19018                         error "Ladvise test ${i}, bad lock count, returned " \
19019                               "${rc}, actual ${difference}"
19020                 fi
19021         done
19022
19023         #test 22 returns only success/failure
19024         i=22
19025         lockahead_test -d $DIR/$tdir -t $i -f $tfile
19026         rc=$?
19027         if [ $rc -eq 255 ]; then
19028                 error "Ladvise test${i} failed, ${rc}"
19029         fi
19030 }
19031 run_test 255c "suite of ladvise lockahead tests"
19032
19033 test_256() {
19034         [ $PARALLEL == "yes" ] && skip "skip parallel run"
19035         remote_mds_nodsh && skip "remote MDS with nodsh"
19036         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
19037         changelog_users $SINGLEMDS | grep "^cl" &&
19038                 skip "active changelog user"
19039
19040         local cl_user
19041         local cat_sl
19042         local mdt_dev
19043
19044         mdt_dev=$(mdsdevname 1)
19045         echo $mdt_dev
19046
19047         changelog_register || error "changelog_register failed"
19048
19049         rm -rf $DIR/$tdir
19050         mkdir -p $DIR/$tdir
19051
19052         changelog_clear 0 || error "changelog_clear failed"
19053
19054         # change something
19055         touch $DIR/$tdir/{1..10}
19056
19057         # stop the MDT
19058         stop $SINGLEMDS || error "Fail to stop MDT"
19059
19060         # remount the MDT
19061
19062         start $SINGLEMDS $mdt_dev $MDS_MOUNT_OPTS || error "Fail to start MDT"
19063
19064         #after mount new plainllog is used
19065         touch $DIR/$tdir/{11..19}
19066         local tmpfile="$(mktemp --tmpdir -u $tfile.XXXXXX)"
19067         stack_trap "rm -f $tmpfile"
19068         cat_sl=$(do_facet $SINGLEMDS "sync; \
19069                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
19070                  llog_reader $tmpfile | grep -c type=1064553b")
19071         do_facet $SINGLEMDS llog_reader $tmpfile
19072
19073         [ $cat_sl != 2 ] && error "Changelog catalog has $cat_sl != 2 slots"
19074
19075         changelog_clear 0 || error "changelog_clear failed"
19076
19077         cat_sl=$(do_facet $SINGLEMDS "sync; \
19078                  $DEBUGFS -c -R 'dump changelog_catalog $tmpfile' $mdt_dev; \
19079                  llog_reader $tmpfile | grep -c type=1064553b")
19080
19081         if (( cat_sl == 2 )); then
19082                 error "Empty plain llog was not deleted from changelog catalog"
19083         elif (( cat_sl != 1 )); then
19084                 error "Active plain llog shouldn't be deleted from catalog"
19085         fi
19086 }
19087 run_test 256 "Check llog delete for empty and not full state"
19088
19089 test_257() {
19090         remote_mds_nodsh && skip "remote MDS with nodsh"
19091         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
19092                 skip "Need MDS version at least 2.8.55"
19093
19094         test_mkdir $DIR/$tdir
19095
19096         setfattr -n trusted.name1 -v value1 $DIR/$tdir ||
19097                 error "setfattr -n trusted.name1=value1 $DIR/$tdir failed"
19098         stat $DIR/$tdir
19099
19100 #define OBD_FAIL_MDS_XATTR_REP                  0x161
19101         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19102         local facet=mds$((mdtidx + 1))
19103         set_nodes_failloc $(facet_active_host $facet) 0x80000161
19104         getfattr -n trusted.name1 $DIR/$tdir 2> /dev/null
19105
19106         stop $facet || error "stop MDS failed"
19107         start $facet $(mdsdevname $((mdtidx + 1))) $MDS_MOUNT_OPTS ||
19108                 error "start MDS fail"
19109         wait_recovery_complete $facet
19110 }
19111 run_test 257 "xattr locks are not lost"
19112
19113 # Verify we take the i_mutex when security requires it
19114 test_258a() {
19115 #define OBD_FAIL_IMUTEX_SEC 0x141c
19116         $LCTL set_param fail_loc=0x141c
19117         touch $DIR/$tfile
19118         chmod u+s $DIR/$tfile
19119         chmod a+rwx $DIR/$tfile
19120         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
19121         RC=$?
19122         if [ $RC -ne 0 ]; then
19123                 error "error, failed to take i_mutex, rc=$?"
19124         fi
19125         rm -f $DIR/$tfile
19126 }
19127 run_test 258a "verify i_mutex security behavior when suid attributes is set"
19128
19129 # Verify we do NOT take the i_mutex in the normal case
19130 test_258b() {
19131 #define OBD_FAIL_IMUTEX_NOSEC 0x141d
19132         $LCTL set_param fail_loc=0x141d
19133         touch $DIR/$tfile
19134         chmod a+rwx $DIR
19135         chmod a+rw $DIR/$tfile
19136         $RUNAS dd if=/dev/zero of=$DIR/$tfile bs=4k count=1 oflag=append
19137         RC=$?
19138         if [ $RC -ne 0 ]; then
19139                 error "error, took i_mutex unnecessarily, rc=$?"
19140         fi
19141         rm -f $DIR/$tfile
19142
19143 }
19144 run_test 258b "verify i_mutex security behavior"
19145
19146 test_259() {
19147         local file=$DIR/$tfile
19148         local before
19149         local after
19150
19151         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip "ldiskfs only test"
19152
19153         stack_trap "rm -f $file" EXIT
19154
19155         wait_delete_completed
19156         before=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19157         echo "before: $before"
19158
19159         $LFS setstripe -i 0 -c 1 $file
19160         dd if=/dev/zero of=$file bs=1M count=10 || error "couldn't write"
19161         sync_all_data
19162         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19163         echo "after write: $after"
19164
19165 #define OBD_FAIL_OSD_FAIL_AT_TRUNCATE          0x2301
19166         do_facet ost1 $LCTL set_param fail_loc=0x2301
19167         $TRUNCATE $file 0
19168         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19169         echo "after truncate: $after"
19170
19171         stop ost1
19172         do_facet ost1 $LCTL set_param fail_loc=0
19173         start ost1 $(ostdevname 1) $OST_MOUNT_OPTS || error "cannot start ost1"
19174         sleep 2
19175         after=$(do_facet ost1 "$LCTL get_param -n osd-*.*OST0000.kbytesfree")
19176         echo "after restart: $after"
19177         [ $((after - before)) -ge $(fs_log_size ost1) ] &&
19178                 error "missing truncate?"
19179
19180         return 0
19181 }
19182 run_test 259 "crash at delayed truncate"
19183
19184 test_260() {
19185 #define OBD_FAIL_MDC_CLOSE               0x806
19186         $LCTL set_param fail_loc=0x80000806
19187         touch $DIR/$tfile
19188
19189 }
19190 run_test 260 "Check mdc_close fail"
19191
19192 ### Data-on-MDT sanity tests ###
19193 test_270a() {
19194         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19195                 skip "Need MDS version at least 2.10.55 for DoM"
19196
19197         # create DoM file
19198         local dom=$DIR/$tdir/dom_file
19199         local tmp=$DIR/$tdir/tmp_file
19200
19201         mkdir -p $DIR/$tdir
19202
19203         # basic checks for DoM component creation
19204         $LFS setstripe -E 1024K -E 2048K -L mdt $dom 2>/dev/null &&
19205                 error "Can set MDT layout to non-first entry"
19206
19207         $LFS setstripe -E 1024K -L mdt -E 2048K -L mdt $dom 2>/dev/null &&
19208                 error "Can define multiple entries as MDT layout"
19209
19210         $LFS setstripe -E 1M -L mdt $dom || error "Can't create DoM layout"
19211
19212         [ $($LFS getstripe -L $dom) == "mdt" ] || error "bad pattern"
19213         [ $($LFS getstripe -c $dom) == 0 ] || error "bad stripe count"
19214         [ $($LFS getstripe -S $dom) == 1048576 ] || error "bad stripe size"
19215
19216         local mdtidx=$($LFS getstripe -m $dom)
19217         local mdtname=MDT$(printf %04x $mdtidx)
19218         local facet=mds$((mdtidx + 1))
19219         local space_check=1
19220
19221         # Skip free space checks with ZFS
19222         [ "$(facet_fstype $facet)" == "zfs" ] && space_check=0
19223
19224         # write
19225         sync
19226         local size_tmp=$((65536 * 3))
19227         local mdtfree1=$(do_facet $facet \
19228                          lctl get_param -n osd*.*$mdtname.kbytesfree)
19229
19230         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
19231         # check also direct IO along write
19232         # IO size must be a multiple of PAGE_SIZE on all platforms (ARM=64KB)
19233         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
19234         sync
19235         cmp $tmp $dom || error "file data is different"
19236         [ $(stat -c%s $dom) == $size_tmp ] ||
19237                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
19238         if [ $space_check == 1 ]; then
19239                 local mdtfree2=$(do_facet $facet \
19240                                  lctl get_param -n osd*.*$mdtname.kbytesfree)
19241
19242                 # increase in usage from by $size_tmp
19243                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
19244                         error "MDT free space wrong after write: " \
19245                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
19246         fi
19247
19248         # truncate
19249         local size_dom=10000
19250
19251         $TRUNCATE $dom $size_dom
19252         [ $(stat -c%s $dom) == $size_dom ] ||
19253                 error "bad size after truncate: $(stat -c%s $dom) != $size_dom"
19254         if [ $space_check == 1 ]; then
19255                 mdtfree1=$(do_facet $facet \
19256                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19257                 # decrease in usage from $size_tmp to new $size_dom
19258                 [ $(($mdtfree1 - $mdtfree2)) -ge \
19259                   $(((size_tmp - size_dom) / 1024)) ] ||
19260                         error "MDT free space is wrong after truncate: " \
19261                               "$mdtfree1 >= $mdtfree2 + ($size_tmp - $size_dom) / 1024"
19262         fi
19263
19264         # append
19265         cat $tmp >> $dom
19266         sync
19267         size_dom=$((size_dom + size_tmp))
19268         [ $(stat -c%s $dom) == $size_dom ] ||
19269                 error "bad size after append: $(stat -c%s $dom) != $size_dom"
19270         if [ $space_check == 1 ]; then
19271                 mdtfree2=$(do_facet $facet \
19272                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19273                 # increase in usage by $size_tmp from previous
19274                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_tmp / 1024)) ] ||
19275                         error "MDT free space is wrong after append: " \
19276                               "$mdtfree1 >= $mdtfree2 + $size_tmp/1024"
19277         fi
19278
19279         # delete
19280         rm $dom
19281         if [ $space_check == 1 ]; then
19282                 mdtfree1=$(do_facet $facet \
19283                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19284                 # decrease in usage by $size_dom from previous
19285                 [ $(($mdtfree1 - $mdtfree2)) -ge $((size_dom / 1024)) ] ||
19286                         error "MDT free space is wrong after removal: " \
19287                               "$mdtfree1 >= $mdtfree2 + $size_dom/1024"
19288         fi
19289
19290         # combined striping
19291         $LFS setstripe -E 1024K -L mdt -E EOF $dom ||
19292                 error "Can't create DoM + OST striping"
19293
19294         size_tmp=2031616 # must be a multiple of PAGE_SIZE=65536 on ARM
19295         dd if=/dev/urandom of=$tmp bs=1024 count=$((size_tmp / 1024))
19296         # check also direct IO along write
19297         dd if=$tmp of=$dom bs=65536 count=$((size_tmp / 65536)) oflag=direct
19298         sync
19299         cmp $tmp $dom || error "file data is different"
19300         [ $(stat -c%s $dom) == $size_tmp ] ||
19301                 error "bad size after write: $(stat -c%s $dom) != $size_tmp"
19302         rm $dom $tmp
19303
19304         return 0
19305 }
19306 run_test 270a "DoM: basic functionality tests"
19307
19308 test_270b() {
19309         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19310                 skip "Need MDS version at least 2.10.55"
19311
19312         local dom=$DIR/$tdir/dom_file
19313         local max_size=1048576
19314
19315         mkdir -p $DIR/$tdir
19316         $LFS setstripe -E $max_size -L mdt $dom
19317
19318         # truncate over the limit
19319         $TRUNCATE $dom $(($max_size + 1)) &&
19320                 error "successful truncate over the maximum size"
19321         # write over the limit
19322         dd if=/dev/zero of=$dom bs=$max_size seek=1 count=1 &&
19323                 error "successful write over the maximum size"
19324         # append over the limit
19325         dd if=/dev/zero of=$dom bs=$(($max_size - 3)) count=1
19326         echo "12345" >> $dom && error "successful append over the maximum size"
19327         rm $dom
19328
19329         return 0
19330 }
19331 run_test 270b "DoM: maximum size overflow checks for DoM-only file"
19332
19333 test_270c() {
19334         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19335                 skip "Need MDS version at least 2.10.55"
19336
19337         mkdir -p $DIR/$tdir
19338         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19339
19340         # check files inherit DoM EA
19341         touch $DIR/$tdir/first
19342         [ $($LFS getstripe -L $DIR/$tdir/first) == "mdt" ] ||
19343                 error "bad pattern"
19344         [ $($LFS getstripe -c $DIR/$tdir/first) == 0 ] ||
19345                 error "bad stripe count"
19346         [ $($LFS getstripe -S $DIR/$tdir/first) == 1048576 ] ||
19347                 error "bad stripe size"
19348
19349         # check directory inherits DoM EA and uses it as default
19350         mkdir $DIR/$tdir/subdir
19351         touch $DIR/$tdir/subdir/second
19352         [ $($LFS getstripe -L $DIR/$tdir/subdir/second) == "mdt" ] ||
19353                 error "bad pattern in sub-directory"
19354         [ $($LFS getstripe -c $DIR/$tdir/subdir/second) == 0 ] ||
19355                 error "bad stripe count in sub-directory"
19356         [ $($LFS getstripe -S $DIR/$tdir/subdir/second) == 1048576 ] ||
19357                 error "bad stripe size in sub-directory"
19358         return 0
19359 }
19360 run_test 270c "DoM: DoM EA inheritance tests"
19361
19362 test_270d() {
19363         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19364                 skip "Need MDS version at least 2.10.55"
19365
19366         mkdir -p $DIR/$tdir
19367         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19368
19369         # inherit default DoM striping
19370         mkdir $DIR/$tdir/subdir
19371         touch $DIR/$tdir/subdir/f1
19372
19373         # change default directory striping
19374         $LFS setstripe -c 1 $DIR/$tdir/subdir
19375         touch $DIR/$tdir/subdir/f2
19376         [ $($LFS getstripe -c $DIR/$tdir/subdir/f2) == 1 ] ||
19377                 error "wrong default striping in file 2"
19378         [ $($LFS getstripe -L $DIR/$tdir/subdir/f2) == "raid0" ] ||
19379                 error "bad pattern in file 2"
19380         return 0
19381 }
19382 run_test 270d "DoM: change striping from DoM to RAID0"
19383
19384 test_270e() {
19385         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19386                 skip "Need MDS version at least 2.10.55"
19387
19388         mkdir -p $DIR/$tdir/dom
19389         mkdir -p $DIR/$tdir/norm
19390         DOMFILES=20
19391         NORMFILES=10
19392         $LFS setstripe -E 1M -L mdt $DIR/$tdir/dom
19393         $LFS setstripe -i 0 -S 2M $DIR/$tdir/norm
19394
19395         createmany -o $DIR/$tdir/dom/dom- $DOMFILES
19396         createmany -o $DIR/$tdir/norm/norm- $NORMFILES
19397
19398         # find DoM files by layout
19399         NUM=$($LFS find -L mdt -type f $DIR/$tdir 2>/dev/null | wc -l)
19400         [ $NUM -eq  $DOMFILES ] ||
19401                 error "lfs find -L: found $NUM, expected $DOMFILES"
19402         echo "Test 1: lfs find 20 DOM files by layout: OK"
19403
19404         # there should be 1 dir with default DOM striping
19405         NUM=$($LFS find -L mdt -type d $DIR/$tdir 2>/dev/null | wc -l)
19406         [ $NUM -eq  1 ] ||
19407                 error "lfs find -L: found $NUM, expected 1 dir"
19408         echo "Test 2: lfs find 1 DOM dir by layout: OK"
19409
19410         # find DoM files by stripe size
19411         NUM=$($LFS find -S -1200K -type f $DIR/$tdir 2>/dev/null | wc -l)
19412         [ $NUM -eq  $DOMFILES ] ||
19413                 error "lfs find -S: found $NUM, expected $DOMFILES"
19414         echo "Test 4: lfs find 20 DOM files by stripe size: OK"
19415
19416         # find files by stripe offset except DoM files
19417         NUM=$($LFS find -i 0 -type f $DIR/$tdir 2>/dev/null | wc -l)
19418         [ $NUM -eq  $NORMFILES ] ||
19419                 error "lfs find -i: found $NUM, expected $NORMFILES"
19420         echo "Test 5: lfs find no DOM files by stripe index: OK"
19421         return 0
19422 }
19423 run_test 270e "DoM: lfs find with DoM files test"
19424
19425 test_270f() {
19426         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19427                 skip "Need MDS version at least 2.10.55"
19428
19429         local mdtname=${FSNAME}-MDT0000-mdtlov
19430         local dom=$DIR/$tdir/dom_file
19431         local dom_limit_saved=$(do_facet mds1 $LCTL get_param -n \
19432                                                 lod.$mdtname.dom_stripesize)
19433         local dom_limit=131072
19434
19435         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=$dom_limit
19436         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19437                                                 lod.$mdtname.dom_stripesize)
19438         [ ${dom_limit} -eq ${dom_current} ] ||
19439                 error "Cannot change per-MDT DoM stripe limit to $dom_limit"
19440
19441         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19442         $LFS setstripe -d $DIR/$tdir
19443         $LFS setstripe -E $dom_limit -L mdt $DIR/$tdir ||
19444                 error "Can't set directory default striping"
19445
19446         # exceed maximum stripe size
19447         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19448                 error "Can't create file with $((dom_limit * 2)) DoM stripe"
19449         [ $($LFS getstripe -S $dom) -eq $((dom_limit * 2)) ] &&
19450                 error "Able to create DoM component size more than LOD limit"
19451
19452         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=0
19453         dom_current=$(do_facet mds1 $LCTL get_param -n \
19454                                                 lod.$mdtname.dom_stripesize)
19455         [ 0 -eq ${dom_current} ] ||
19456                 error "Can't set zero DoM stripe limit"
19457         rm $dom
19458
19459         # attempt to create DoM file on server with disabled DoM should
19460         # remove DoM entry from layout and be succeed
19461         $LFS setstripe -E $dom_limit -L mdt -E -1 $dom ||
19462                 error "Can't create DoM file (DoM is disabled)"
19463         [ $($LFS getstripe -L $dom) == "mdt" ] &&
19464                 error "File has DoM component while DoM is disabled"
19465         rm $dom
19466
19467         # attempt to create DoM file with only DoM stripe should return error
19468         $LFS setstripe -E $dom_limit -L mdt $dom &&
19469                 error "Able to create DoM-only file while DoM is disabled"
19470
19471         # too low values to be aligned with smallest stripe size 64K
19472         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=30000
19473         dom_current=$(do_facet mds1 $LCTL get_param -n \
19474                                                 lod.$mdtname.dom_stripesize)
19475         [ 30000 -eq ${dom_current} ] &&
19476                 error "Can set too small DoM stripe limit"
19477
19478         # 64K is a minimal stripe size in Lustre, expect limit of that size
19479         [ 65536 -eq ${dom_current} ] ||
19480                 error "Limit is not set to 64K but ${dom_current}"
19481
19482         do_facet mds1 $LCTL set_param -n lod.$mdtname.dom_stripesize=2147483648
19483         dom_current=$(do_facet mds1 $LCTL get_param -n \
19484                                                 lod.$mdtname.dom_stripesize)
19485         echo $dom_current
19486         [ 2147483648 -eq ${dom_current} ] &&
19487                 error "Can set too large DoM stripe limit"
19488
19489         do_facet mds1 $LCTL set_param -n \
19490                                 lod.$mdtname.dom_stripesize=$((dom_limit * 2))
19491         $LFS setstripe -E $((dom_limit * 2)) -L mdt $dom ||
19492                 error "Can't create DoM component size after limit change"
19493         do_facet mds1 $LCTL set_param -n \
19494                                 lod.$mdtname.dom_stripesize=$((dom_limit / 2))
19495         $LFS setstripe -E $dom_limit -L mdt ${dom}_big ||
19496                 error "Can't create DoM file after limit decrease"
19497         [ $($LFS getstripe -S ${dom}_big) -eq $((dom_limit / 2)) ] ||
19498                 error "Can create big DoM component after limit decrease"
19499         touch ${dom}_def ||
19500                 error "Can't create file with old default layout"
19501
19502         do_facet mds1 $LCTL set_param -n lod.*.dom_stripesize=$dom_limit_saved
19503         return 0
19504 }
19505 run_test 270f "DoM: maximum DoM stripe size checks"
19506
19507 test_270g() {
19508         [ $MDS1_VERSION -ge $(version_code 2.13.52) ] ||
19509                 skip "Need MDS version at least 2.13.52"
19510         local dom=$DIR/$tdir/$tfile
19511
19512         $LFS mkdir -i 0 -c 1 $DIR/$tdir
19513         local lodname=${FSNAME}-MDT0000-mdtlov
19514
19515         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
19516         save_lustre_params mds1 "lod.${lodname}.dom_stripesize_max_kb" > $save
19517         save_lustre_params mds1 "lod.${lodname}.dom_threshold_free_mb" >> $save
19518         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
19519
19520         local dom_limit=1024
19521         local dom_threshold="50%"
19522
19523         $LFS setstripe -d $DIR/$tdir
19524         $LFS setstripe -E ${dom_limit}K -L mdt $DIR/$tdir ||
19525                 error "Can't set directory default striping"
19526
19527         do_facet mds1 $LCTL set_param -n \
19528                                 lod.${lodname}.dom_stripesize_max_kb=$dom_limit
19529         # set 0 threshold and create DOM file to change tunable stripesize
19530         do_facet mds1 $LCTL set_param -n lod.${lodname}.dom_threshold_free_mb=0
19531         $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
19532                 error "Failed to create $dom file"
19533         # now tunable dom_cur_stripesize should reach maximum
19534         local dom_current=$(do_facet mds1 $LCTL get_param -n \
19535                                         lod.${lodname}.dom_stripesize_cur_kb)
19536         [[ $dom_current == $dom_limit ]] ||
19537                 error "Current DOM stripesize is not maximum"
19538         rm $dom
19539
19540         # set threshold for further tests
19541         do_facet mds1 $LCTL set_param -n \
19542                         lod.${lodname}.dom_threshold_free_mb=$dom_threshold
19543         echo "DOM threshold is $dom_threshold free space"
19544         local dom_def
19545         local dom_set
19546         # Spoof bfree to exceed threshold
19547         #define OBD_FAIL_MDS_STATFS_SPOOF   0x168
19548         do_facet mds1 $LCTL set_param -n fail_loc=0x0168
19549         for spfree in 40 20 0 15 30 55; do
19550                 do_facet mds1 $LCTL set_param -n fail_val=$spfree
19551                 $LFS setstripe -E ${dom_limit}K -L mdt -E -1 $dom ||
19552                         error "Failed to create $dom file"
19553                 dom_def=$(do_facet mds1 $LCTL get_param -n \
19554                                         lod.${lodname}.dom_stripesize_cur_kb)
19555                 echo "Free space: ${spfree}%, default DOM stripe: ${dom_def}K"
19556                 [[ $dom_def != $dom_current ]] ||
19557                         error "Default stripe size was not changed"
19558                 if [[ $spfree > 0 ]] ; then
19559                         dom_set=$($LFS getstripe -S $dom)
19560                         [[ $dom_set == $((dom_def * 1024)) ]] ||
19561                                 error "DOM component size is still old"
19562                 else
19563                         [[ $($LFS getstripe -L $dom) != "mdt" ]] ||
19564                                 error "DoM component is set with no free space"
19565                 fi
19566                 rm $dom
19567                 dom_current=$dom_def
19568         done
19569 }
19570 run_test 270g "DoM: default DoM stripe size depends on free space"
19571
19572 test_271a() {
19573         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19574                 skip "Need MDS version at least 2.10.55"
19575
19576         local dom=$DIR/$tdir/dom
19577
19578         mkdir -p $DIR/$tdir
19579
19580         $LFS setstripe -E 1024K -L mdt $dom
19581
19582         lctl set_param -n mdc.*.stats=clear
19583         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19584         cat $dom > /dev/null
19585         local reads=$(lctl get_param -n mdc.*.stats | grep -c ost_read)
19586         [ $reads -eq 0 ] || error "Unexpected $reads READ RPCs"
19587         ls $dom
19588         rm -f $dom
19589 }
19590 run_test 271a "DoM: data is cached for read after write"
19591
19592 test_271b() {
19593         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19594                 skip "Need MDS version at least 2.10.55"
19595
19596         local dom=$DIR/$tdir/dom
19597
19598         mkdir -p $DIR/$tdir
19599
19600         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19601
19602         lctl set_param -n mdc.*.stats=clear
19603         dd if=/dev/zero of=$dom bs=4096 count=1 || return 1
19604         cancel_lru_locks mdc
19605         $CHECKSTAT -t file -s 4096 $dom || error "stat #1 fails"
19606         # second stat to check size is cached on client
19607         $CHECKSTAT -t file -s 4096 $dom || error "stat #2 fails"
19608         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19609         [ $gls -eq 0 ] || error "Unexpected $gls glimpse RPCs"
19610         rm -f $dom
19611 }
19612 run_test 271b "DoM: no glimpse RPC for stat (DoM only file)"
19613
19614 test_271ba() {
19615         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19616                 skip "Need MDS version at least 2.10.55"
19617
19618         local dom=$DIR/$tdir/dom
19619
19620         mkdir -p $DIR/$tdir
19621
19622         $LFS setstripe -E 1024K -L mdt -E EOF $dom
19623
19624         lctl set_param -n mdc.*.stats=clear
19625         lctl set_param -n osc.*.stats=clear
19626         dd if=/dev/zero of=$dom bs=2048K count=1 || return 1
19627         cancel_lru_locks mdc
19628         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19629         # second stat to check size is cached on client
19630         $CHECKSTAT -t file -s 2097152 $dom || error "stat"
19631         local gls=$(lctl get_param -n mdc.*.stats | grep -c ldlm_glimpse)
19632         [ $gls == 0 ] || error "Unexpected $gls glimpse RPCs"
19633         local gls=$(lctl get_param -n osc.*.stats | grep -c ldlm_glimpse)
19634         [ $gls == 0 ] || error "Unexpected $gls OSC glimpse RPCs"
19635         rm -f $dom
19636 }
19637 run_test 271ba "DoM: no glimpse RPC for stat (combined file)"
19638
19639
19640 get_mdc_stats() {
19641         local mdtidx=$1
19642         local param=$2
19643         local mdt=MDT$(printf %04x $mdtidx)
19644
19645         if [ -z $param ]; then
19646                 lctl get_param -n mdc.*$mdt*.stats
19647         else
19648                 lctl get_param -n mdc.*$mdt*.stats | awk "/$param/"'{print $2}'
19649         fi
19650 }
19651
19652 test_271c() {
19653         [ $MDS1_VERSION -lt $(version_code 2.10.55) ] &&
19654                 skip "Need MDS version at least 2.10.55"
19655
19656         local dom=$DIR/$tdir/dom
19657
19658         mkdir -p $DIR/$tdir
19659
19660         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19661
19662         local mdtidx=$($LFS getstripe -m $DIR/$tdir)
19663         local facet=mds$((mdtidx + 1))
19664
19665         cancel_lru_locks mdc
19666         do_facet $facet lctl set_param -n mdt.*.dom_lock=0
19667         createmany -o $dom 1000
19668         lctl set_param -n mdc.*.stats=clear
19669         smalliomany -w $dom 1000 200
19670         get_mdc_stats $mdtidx
19671         local enq=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19672         # Each file has 1 open, 1 IO enqueues, total 2000
19673         # but now we have also +1 getxattr for security.capability, total 3000
19674         [ $enq -ge 2000 ] || error "Too few enqueues $enq, expected > 2000"
19675         unlinkmany $dom 1000
19676
19677         cancel_lru_locks mdc
19678         do_facet $facet lctl set_param -n mdt.*.dom_lock=1
19679         createmany -o $dom 1000
19680         lctl set_param -n mdc.*.stats=clear
19681         smalliomany -w $dom 1000 200
19682         local enq_2=$(get_mdc_stats $mdtidx ldlm_ibits_enqueue)
19683         # Expect to see reduced amount of RPCs by 1000 due to single enqueue
19684         # for OPEN and IO lock.
19685         [ $((enq - enq_2)) -ge 1000 ] ||
19686                 error "Too many enqueues $enq_2, expected about $((enq - 1000))"
19687         unlinkmany $dom 1000
19688         return 0
19689 }
19690 run_test 271c "DoM: IO lock at open saves enqueue RPCs"
19691
19692 cleanup_271def_tests() {
19693         trap 0
19694         rm -f $1
19695 }
19696
19697 test_271d() {
19698         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19699                 skip "Need MDS version at least 2.10.57"
19700
19701         local dom=$DIR/$tdir/dom
19702         local tmp=$TMP/$tfile
19703         trap "cleanup_271def_tests $tmp" EXIT
19704
19705         mkdir -p $DIR/$tdir
19706
19707         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19708
19709         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19710
19711         cancel_lru_locks mdc
19712         dd if=/dev/urandom of=$tmp bs=1000 count=1
19713         dd if=$tmp of=$dom bs=1000 count=1
19714         cancel_lru_locks mdc
19715
19716         cat /etc/hosts >> $tmp
19717         lctl set_param -n mdc.*.stats=clear
19718
19719         # append data to the same file it should update local page
19720         echo "Append to the same page"
19721         cat /etc/hosts >> $dom
19722         local num=$(get_mdc_stats $mdtidx ost_read)
19723         local ra=$(get_mdc_stats $mdtidx req_active)
19724         local rw=$(get_mdc_stats $mdtidx req_waittime)
19725
19726         [ -z $num ] || error "$num READ RPC occured"
19727         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19728         echo "... DONE"
19729
19730         # compare content
19731         cmp $tmp $dom || error "file miscompare"
19732
19733         cancel_lru_locks mdc
19734         lctl set_param -n mdc.*.stats=clear
19735
19736         echo "Open and read file"
19737         cat $dom > /dev/null
19738         local num=$(get_mdc_stats $mdtidx ost_read)
19739         local ra=$(get_mdc_stats $mdtidx req_active)
19740         local rw=$(get_mdc_stats $mdtidx req_waittime)
19741
19742         [ -z $num ] || error "$num READ RPC occured"
19743         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19744         echo "... DONE"
19745
19746         # compare content
19747         cmp $tmp $dom || error "file miscompare"
19748
19749         return 0
19750 }
19751 run_test 271d "DoM: read on open (1K file in reply buffer)"
19752
19753 test_271f() {
19754         [ $MDS1_VERSION -lt $(version_code 2.10.57) ] &&
19755                 skip "Need MDS version at least 2.10.57"
19756
19757         local dom=$DIR/$tdir/dom
19758         local tmp=$TMP/$tfile
19759         trap "cleanup_271def_tests $tmp" EXIT
19760
19761         mkdir -p $DIR/$tdir
19762
19763         $LFS setstripe -E 1024K -L mdt $DIR/$tdir
19764
19765         local mdtidx=$($LFS getstripe --mdt-index $DIR/$tdir)
19766
19767         cancel_lru_locks mdc
19768         dd if=/dev/urandom of=$tmp bs=265000 count=1
19769         dd if=$tmp of=$dom bs=265000 count=1
19770         cancel_lru_locks mdc
19771         cat /etc/hosts >> $tmp
19772         lctl set_param -n mdc.*.stats=clear
19773
19774         echo "Append to the same page"
19775         cat /etc/hosts >> $dom
19776         local num=$(get_mdc_stats $mdtidx ost_read)
19777         local ra=$(get_mdc_stats $mdtidx req_active)
19778         local rw=$(get_mdc_stats $mdtidx req_waittime)
19779
19780         [ -z $num ] || error "$num READ RPC occured"
19781         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19782         echo "... DONE"
19783
19784         # compare content
19785         cmp $tmp $dom || error "file miscompare"
19786
19787         cancel_lru_locks mdc
19788         lctl set_param -n mdc.*.stats=clear
19789
19790         echo "Open and read file"
19791         cat $dom > /dev/null
19792         local num=$(get_mdc_stats $mdtidx ost_read)
19793         local ra=$(get_mdc_stats $mdtidx req_active)
19794         local rw=$(get_mdc_stats $mdtidx req_waittime)
19795
19796         [ -z $num ] && num=0
19797         [ $num -eq 1 ] || error "expect 1 READ RPC, $num occured"
19798         [ $ra == $rw ] || error "$((ra - rw)) resend occured"
19799         echo "... DONE"
19800
19801         # compare content
19802         cmp $tmp $dom || error "file miscompare"
19803
19804         return 0
19805 }
19806 run_test 271f "DoM: read on open (200K file and read tail)"
19807
19808 test_271g() {
19809         [[ $($LCTL get_param mdc.*.import) =~ async_discard ]] ||
19810                 skip "Skipping due to old client or server version"
19811
19812         $LFS setstripe -E 1024K -L mdt -E EOF $DIR1/$tfile
19813         # to get layout
19814         $CHECKSTAT -t file $DIR1/$tfile
19815
19816         $MULTIOP $DIR1/$tfile Ow40960_w4096c &
19817         MULTIOP_PID=$!
19818         sleep 1
19819         #define OBD_FAIL_LDLM_CANCEL_BL_CB_RACE
19820         $LCTL set_param fail_loc=0x80000314
19821         rm $DIR1/$tfile || error "Unlink fails"
19822         RC=$?
19823         kill -USR1 $MULTIOP_PID && wait $MULTIOP_PID || error "multiop failure"
19824         [ $RC -eq 0 ] || error "Failed write to stale object"
19825 }
19826 run_test 271g "Discard DoM data vs client flush race"
19827
19828 test_272a() {
19829         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19830                 skip "Need MDS version at least 2.11.50"
19831
19832         local dom=$DIR/$tdir/dom
19833         mkdir -p $DIR/$tdir
19834
19835         $LFS setstripe -E 256K -L mdt -E -1 -c1 $dom
19836         dd if=/dev/urandom of=$dom bs=512K count=1 ||
19837                 error "failed to write data into $dom"
19838         local old_md5=$(md5sum $dom)
19839
19840         $LFS migrate -E 256K -L mdt -E -1 -c2 $dom ||
19841                 error "failed to migrate to the same DoM component"
19842
19843         local new_md5=$(md5sum $dom)
19844
19845         [ "$old_md5" == "$new_md5" ] ||
19846                 error "md5sum differ: $old_md5, $new_md5"
19847
19848         [ $($LFS getstripe -c $dom) -eq 2 ] ||
19849                 error "bad final stripe count: $($LFS getstripe -c $dom) != 2"
19850 }
19851 run_test 272a "DoM migration: new layout with the same DOM component"
19852
19853 test_272b() {
19854         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19855                 skip "Need MDS version at least 2.11.50"
19856
19857         local dom=$DIR/$tdir/dom
19858         mkdir -p $DIR/$tdir
19859         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19860
19861         local mdtidx=$($LFS getstripe -m $dom)
19862         local mdtname=MDT$(printf %04x $mdtidx)
19863         local facet=mds$((mdtidx + 1))
19864
19865         local mdtfree1=$(do_facet $facet \
19866                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19867         dd if=/dev/urandom of=$dom bs=2M count=1 ||
19868                 error "failed to write data into $dom"
19869         local old_md5=$(md5sum $dom)
19870         cancel_lru_locks mdc
19871         local mdtfree1=$(do_facet $facet \
19872                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19873
19874         $LFS migrate -c2 $dom ||
19875                 error "failed to migrate to the new composite layout"
19876         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19877                 error "MDT stripe was not removed"
19878
19879         cancel_lru_locks mdc
19880         local new_md5=$(md5sum $dom)
19881         [ "$old_md5" == "$new_md5" ] ||
19882                 error "$old_md5 != $new_md5"
19883
19884         # Skip free space checks with ZFS
19885         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19886                 local mdtfree2=$(do_facet $facet \
19887                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19888                 [ $mdtfree2 -gt $mdtfree1 ] ||
19889                         error "MDT space is not freed after migration"
19890         fi
19891         return 0
19892 }
19893 run_test 272b "DoM migration: DOM file to the OST-striped file (plain)"
19894
19895 test_272c() {
19896         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
19897                 skip "Need MDS version at least 2.11.50"
19898
19899         local dom=$DIR/$tdir/$tfile
19900         mkdir -p $DIR/$tdir
19901         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19902
19903         local mdtidx=$($LFS getstripe -m $dom)
19904         local mdtname=MDT$(printf %04x $mdtidx)
19905         local facet=mds$((mdtidx + 1))
19906
19907         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19908                 error "failed to write data into $dom"
19909         local old_md5=$(md5sum $dom)
19910         cancel_lru_locks mdc
19911         local mdtfree1=$(do_facet $facet \
19912                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19913
19914         $LFS migrate -E 2M -c1 -E -1 -c2 $dom ||
19915                 error "failed to migrate to the new composite layout"
19916         [ $($LFS getstripe -L $dom) == 'mdt' ] &&
19917                 error "MDT stripe was not removed"
19918
19919         cancel_lru_locks mdc
19920         local new_md5=$(md5sum $dom)
19921         [ "$old_md5" == "$new_md5" ] ||
19922                 error "$old_md5 != $new_md5"
19923
19924         # Skip free space checks with ZFS
19925         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19926                 local mdtfree2=$(do_facet $facet \
19927                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19928                 [ $mdtfree2 -gt $mdtfree1 ] ||
19929                         error "MDS space is not freed after migration"
19930         fi
19931         return 0
19932 }
19933 run_test 272c "DoM migration: DOM file to the OST-striped file (composite)"
19934
19935 test_272d() {
19936         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19937                 skip "Need MDS version at least 2.12.55"
19938
19939         local dom=$DIR/$tdir/$tfile
19940         mkdir -p $DIR/$tdir
19941         $LFS setstripe -E 1M -L mdt -E -1 -c1 $dom
19942
19943         local mdtidx=$($LFS getstripe -m $dom)
19944         local mdtname=MDT$(printf %04x $mdtidx)
19945         local facet=mds$((mdtidx + 1))
19946
19947         dd if=/dev/urandom of=$dom bs=2M count=1 oflag=direct ||
19948                 error "failed to write data into $dom"
19949         local old_md5=$(md5sum $dom)
19950         cancel_lru_locks mdc
19951         local mdtfree1=$(do_facet $facet \
19952                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19953
19954         $LFS mirror extend -N -E 2M -c1 -E -1 -c2 $dom ||
19955                 error "failed mirroring to the new composite layout"
19956         $LFS mirror resync $dom ||
19957                 error "failed mirror resync"
19958         $LFS mirror split --mirror-id 1 -d $dom ||
19959                 error "failed mirror split"
19960
19961         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
19962                 error "MDT stripe was not removed"
19963
19964         cancel_lru_locks mdc
19965         local new_md5=$(md5sum $dom)
19966         [ "$old_md5" == "$new_md5" ] ||
19967                 error "$old_md5 != $new_md5"
19968
19969         # Skip free space checks with ZFS
19970         if [ "$(facet_fstype $facet)" != "zfs" ]; then
19971                 local mdtfree2=$(do_facet $facet \
19972                                 lctl get_param -n osd*.*$mdtname.kbytesfree)
19973                 [ $mdtfree2 -gt $mdtfree1 ] ||
19974                         error "MDS space is not freed after DOM mirror deletion"
19975         fi
19976         return 0
19977 }
19978 run_test 272d "DoM mirroring: OST-striped mirror to DOM file"
19979
19980 test_272e() {
19981         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
19982                 skip "Need MDS version at least 2.12.55"
19983
19984         local dom=$DIR/$tdir/$tfile
19985         mkdir -p $DIR/$tdir
19986         $LFS setstripe -c 2 $dom
19987
19988         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
19989                 error "failed to write data into $dom"
19990         local old_md5=$(md5sum $dom)
19991         cancel_lru_locks mdc
19992
19993         $LFS mirror extend -N -E 1M -L mdt -E eof -c2 $dom ||
19994                 error "failed mirroring to the DOM layout"
19995         $LFS mirror resync $dom ||
19996                 error "failed mirror resync"
19997         $LFS mirror split --mirror-id 1 -d $dom ||
19998                 error "failed mirror split"
19999
20000         [ $($LFS getstripe -L $dom) != 'mdt' ] ||
20001                 error "MDT stripe was not removed"
20002
20003         cancel_lru_locks mdc
20004         local new_md5=$(md5sum $dom)
20005         [ "$old_md5" == "$new_md5" ] ||
20006                 error "$old_md5 != $new_md5"
20007
20008         return 0
20009 }
20010 run_test 272e "DoM mirroring: DOM mirror to the OST-striped file"
20011
20012 test_272f() {
20013         [ $MDS1_VERSION -lt $(version_code 2.12.55) ] &&
20014                 skip "Need MDS version at least 2.12.55"
20015
20016         local dom=$DIR/$tdir/$tfile
20017         mkdir -p $DIR/$tdir
20018         $LFS setstripe -c 2 $dom
20019
20020         dd if=/dev/urandom of=$dom bs=512K count=1 oflag=direct ||
20021                 error "failed to write data into $dom"
20022         local old_md5=$(md5sum $dom)
20023         cancel_lru_locks mdc
20024
20025         $LFS migrate -E 1M -L mdt -E eof -c2 -v $dom ||
20026                 error "failed migrating to the DOM file"
20027
20028         cancel_lru_locks mdc
20029         local new_md5=$(md5sum $dom)
20030         [ "$old_md5" != "$new_md5" ] &&
20031                 error "$old_md5 != $new_md5"
20032
20033         return 0
20034 }
20035 run_test 272f "DoM migration: OST-striped file to DOM file"
20036
20037 test_273a() {
20038         [ $MDS1_VERSION -lt $(version_code 2.11.50) ] &&
20039                 skip "Need MDS version at least 2.11.50"
20040
20041         # Layout swap cannot be done if either file has DOM component,
20042         # this will never be supported, migration should be used instead
20043
20044         local dom=$DIR/$tdir/$tfile
20045         mkdir -p $DIR/$tdir
20046
20047         $LFS setstripe -c2 ${dom}_plain
20048         $LFS setstripe -E 1M -L mdt -E -1 -c2 ${dom}_dom
20049         $LFS swap_layouts ${dom}_plain ${dom}_dom &&
20050                 error "can swap layout with DoM component"
20051         $LFS swap_layouts ${dom}_dom ${dom}_plain &&
20052                 error "can swap layout with DoM component"
20053
20054         $LFS setstripe -E 1M -c1 -E -1 -c2 ${dom}_comp
20055         $LFS swap_layouts ${dom}_comp ${dom}_dom &&
20056                 error "can swap layout with DoM component"
20057         $LFS swap_layouts ${dom}_dom ${dom}_comp &&
20058                 error "can swap layout with DoM component"
20059         return 0
20060 }
20061 run_test 273a "DoM: layout swapping should fail with DOM"
20062
20063 test_275() {
20064         remote_ost_nodsh && skip "remote OST with nodsh"
20065         [ $OST1_VERSION -lt $(version_code 2.10.57) ] &&
20066                 skip "Need OST version >= 2.10.57"
20067
20068         local file=$DIR/$tfile
20069         local oss
20070
20071         oss=$(comma_list $(osts_nodes))
20072
20073         dd if=/dev/urandom of=$file bs=1M count=2 ||
20074                 error "failed to create a file"
20075         cancel_lru_locks osc
20076
20077         #lock 1
20078         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
20079                 error "failed to read a file"
20080
20081 #define OBD_FAIL_LDLM_PAUSE_CANCEL2      0x31f
20082         $LCTL set_param fail_loc=0x8000031f
20083
20084         cancel_lru_locks osc &
20085         sleep 1
20086
20087 #define OBD_FAIL_LDLM_PROLONG_PAUSE      0x32b
20088         do_nodes $oss $LCTL set_param fail_loc=0x8000032b
20089         #IO takes another lock, but matches the PENDING one
20090         #and places it to the IO RPC
20091         dd if=$file of=/dev/null bs=1M count=1 iflag=direct ||
20092                 error "failed to read a file with PENDING lock"
20093 }
20094 run_test 275 "Read on a canceled duplicate lock"
20095
20096 test_276() {
20097         remote_ost_nodsh && skip "remote OST with nodsh"
20098         local pid
20099
20100         do_facet ost1 "(while true; do \
20101                 $LCTL get_param obdfilter.*.filesfree > /dev/null 2>&1; \
20102                 done) & pid=\\\$!; echo \\\$pid > $TMP/sanity_276_pid" &
20103         pid=$!
20104
20105         for LOOP in $(seq 20); do
20106                 stop ost1
20107                 start ost1 $(ostdevname 1) $OST_MOUNT_OPTS
20108         done
20109         kill -9 $pid
20110         do_facet ost1 "pid=\\\$(cat $TMP/sanity_276_pid); kill -9 \\\$pid; \
20111                 rm $TMP/sanity_276_pid"
20112 }
20113 run_test 276 "Race between mount and obd_statfs"
20114
20115 test_277() {
20116         $LCTL set_param ldlm.namespaces.*.lru_size=0
20117         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
20118         local cached_mb=$($LCTL get_param llite.*.max_cached_mb |
20119                         grep ^used_mb | awk '{print $2}')
20120         [ $cached_mb -eq 1 ] || error "expected mb 1 got $cached_mb"
20121         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 \
20122                 oflag=direct conv=notrunc
20123         cached_mb=$($LCTL get_param llite.*.max_cached_mb |
20124                         grep ^used_mb | awk '{print $2}')
20125         [ $cached_mb -eq 0 ] || error "expected mb 0 got $cached_mb"
20126 }
20127 run_test 277 "Direct IO shall drop page cache"
20128
20129 test_278() {
20130         [ $PARALLEL == "yes" ] && skip "skip parallel run" && return
20131         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20132         [[ "$(facet_host mds1)" != "$(facet_host mds2)" ]] &&
20133                 skip "needs the same host for mdt1 mdt2" && return
20134
20135         local pid1
20136         local pid2
20137
20138 #define OBD_FAIL_OBD_STOP_MDS_RACE     0x60b
20139         do_facet mds2 $LCTL set_param fail_loc=0x8000060c
20140         stop mds2 &
20141         pid2=$!
20142
20143         stop mds1
20144
20145         echo "Starting MDTs"
20146         start mds1 $(mdsdevname 1) $MDS_MOUNT_OPTS
20147         wait $pid2
20148 #For the error assertion will happen. lu_env_get_key(..., &mdt_thread_key)
20149 #will return NULL
20150         do_facet mds2 $LCTL set_param fail_loc=0
20151
20152         start mds2 $(mdsdevname 2) $MDS_MOUNT_OPTS
20153         wait_recovery_complete mds2
20154 }
20155 run_test 278 "Race starting MDS between MDTs stop/start"
20156
20157 test_280() {
20158         [ $MGS_VERSION -lt $(version_code 2.13.52) ] &&
20159                 skip "Need MGS version at least 2.13.52"
20160         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20161         combined_mgs_mds || skip "needs combined MGS/MDT"
20162
20163         umount_client $MOUNT
20164 #define OBD_FAIL_MDS_LLOG_UMOUNT_RACE   0x15e
20165         do_facet mgs $LCTL set_param fail_loc=0x8000015e fail_val=0
20166
20167         mount_client $MOUNT &
20168         sleep 1
20169         stop mgs || error "stop mgs failed"
20170         #for a race mgs would crash
20171         start mgs $(mgsdevname) $MGS_MOUNT_OPTS || error "start mgs failed"
20172         mount_client $MOUNT || error "mount client failed"
20173 }
20174 run_test 280 "Race between MGS umount and client llog processing"
20175
20176 cleanup_test_300() {
20177         trap 0
20178         umask $SAVE_UMASK
20179 }
20180 test_striped_dir() {
20181         local mdt_index=$1
20182         local stripe_count
20183         local stripe_index
20184
20185         mkdir -p $DIR/$tdir
20186
20187         SAVE_UMASK=$(umask)
20188         trap cleanup_test_300 RETURN EXIT
20189
20190         $LFS setdirstripe -i $mdt_index -c 2 -H all_char -o 755 \
20191                                                 $DIR/$tdir/striped_dir ||
20192                 error "set striped dir error"
20193
20194         local mode=$(stat -c%a $DIR/$tdir/striped_dir)
20195         [ "$mode" = "755" ] || error "expect 755 got $mode"
20196
20197         $LFS getdirstripe $DIR/$tdir/striped_dir > /dev/null 2>&1 ||
20198                 error "getdirstripe failed"
20199         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir)
20200         if [ "$stripe_count" != "2" ]; then
20201                 error "1:stripe_count is $stripe_count, expect 2"
20202         fi
20203         stripe_count=$($LFS getdirstripe -T $DIR/$tdir/striped_dir)
20204         if [ "$stripe_count" != "2" ]; then
20205                 error "2:stripe_count is $stripe_count, expect 2"
20206         fi
20207
20208         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir)
20209         if [ "$stripe_index" != "$mdt_index" ]; then
20210                 error "stripe_index is $stripe_index, expect $mdt_index"
20211         fi
20212
20213         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
20214                 error "nlink error after create striped dir"
20215
20216         mkdir $DIR/$tdir/striped_dir/a
20217         mkdir $DIR/$tdir/striped_dir/b
20218
20219         stat $DIR/$tdir/striped_dir/a ||
20220                 error "create dir under striped dir failed"
20221         stat $DIR/$tdir/striped_dir/b ||
20222                 error "create dir under striped dir failed"
20223
20224         [ $(stat -c%h $DIR/$tdir/striped_dir) == '4' ] ||
20225                 error "nlink error after mkdir"
20226
20227         rmdir $DIR/$tdir/striped_dir/a
20228         [ $(stat -c%h $DIR/$tdir/striped_dir) == '3' ] ||
20229                 error "nlink error after rmdir"
20230
20231         rmdir $DIR/$tdir/striped_dir/b
20232         [ $(stat -c%h $DIR/$tdir/striped_dir) == '2' ] ||
20233                 error "nlink error after rmdir"
20234
20235         chattr +i $DIR/$tdir/striped_dir
20236         createmany -o $DIR/$tdir/striped_dir/f 10 &&
20237                 error "immutable flags not working under striped dir!"
20238         chattr -i $DIR/$tdir/striped_dir
20239
20240         rmdir $DIR/$tdir/striped_dir ||
20241                 error "rmdir striped dir error"
20242
20243         cleanup_test_300
20244
20245         true
20246 }
20247
20248 test_300a() {
20249         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20250                 skip "skipped for lustre < 2.7.0"
20251         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20252         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20253
20254         test_striped_dir 0 || error "failed on striped dir on MDT0"
20255         test_striped_dir 1 || error "failed on striped dir on MDT0"
20256 }
20257 run_test 300a "basic striped dir sanity test"
20258
20259 test_300b() {
20260         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20261                 skip "skipped for lustre < 2.7.0"
20262         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20263         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20264
20265         local i
20266         local mtime1
20267         local mtime2
20268         local mtime3
20269
20270         test_mkdir $DIR/$tdir || error "mkdir fail"
20271         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20272                 error "set striped dir error"
20273         for i in {0..9}; do
20274                 mtime1=$(stat -c %Y $DIR/$tdir/striped_dir)
20275                 sleep 1
20276                 touch $DIR/$tdir/striped_dir/file_$i || error "touch error $i"
20277                 mtime2=$(stat -c %Y $DIR/$tdir/striped_dir)
20278                 [ $mtime1 -eq $mtime2 ] && error "mtime unchanged after create"
20279                 sleep 1
20280                 rm -f $DIR/$tdir/striped_dir/file_$i || error "unlink error $i"
20281                 mtime3=$(stat -c %Y $DIR/$tdir/striped_dir)
20282                 [ $mtime2 -eq $mtime3 ] && error "mtime unchanged after unlink"
20283         done
20284         true
20285 }
20286 run_test 300b "check ctime/mtime for striped dir"
20287
20288 test_300c() {
20289         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20290                 skip "skipped for lustre < 2.7.0"
20291         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20292         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20293
20294         local file_count
20295
20296         mkdir -p $DIR/$tdir
20297         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir ||
20298                 error "set striped dir error"
20299
20300         chown $RUNAS_ID:$RUNAS_GID $DIR/$tdir/striped_dir ||
20301                 error "chown striped dir failed"
20302
20303         $RUNAS createmany -o $DIR/$tdir/striped_dir/f 5000 ||
20304                 error "create 5k files failed"
20305
20306         file_count=$(ls $DIR/$tdir/striped_dir | wc -l)
20307
20308         [ "$file_count" = 5000 ] || error "file count $file_count != 5000"
20309
20310         rm -rf $DIR/$tdir
20311 }
20312 run_test 300c "chown && check ls under striped directory"
20313
20314 test_300d() {
20315         [ $MDS1_VERSION -lt $(version_code 2.7.0) ] &&
20316                 skip "skipped for lustre < 2.7.0"
20317         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20318         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20319
20320         local stripe_count
20321         local file
20322
20323         mkdir -p $DIR/$tdir
20324         $LFS setstripe -c 2 $DIR/$tdir
20325
20326         #local striped directory
20327         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20328                 error "set striped dir error"
20329         #look at the directories for debug purposes
20330         ls -l $DIR/$tdir
20331         $LFS getdirstripe $DIR/$tdir
20332         ls -l $DIR/$tdir/striped_dir
20333         $LFS getdirstripe $DIR/$tdir/striped_dir
20334         createmany -o $DIR/$tdir/striped_dir/f 10 ||
20335                 error "create 10 files failed"
20336
20337         #remote striped directory
20338         $LFS setdirstripe -i 1 -c 2 $DIR/$tdir/remote_striped_dir ||
20339                 error "set striped dir error"
20340         #look at the directories for debug purposes
20341         ls -l $DIR/$tdir
20342         $LFS getdirstripe $DIR/$tdir
20343         ls -l $DIR/$tdir/remote_striped_dir
20344         $LFS getdirstripe $DIR/$tdir/remote_striped_dir
20345         createmany -o $DIR/$tdir/remote_striped_dir/f 10 ||
20346                 error "create 10 files failed"
20347
20348         for file in $(find $DIR/$tdir); do
20349                 stripe_count=$($LFS getstripe -c $file)
20350                 [ $stripe_count -eq 2 ] ||
20351                         error "wrong stripe $stripe_count for $file"
20352         done
20353
20354         rm -rf $DIR/$tdir
20355 }
20356 run_test 300d "check default stripe under striped directory"
20357
20358 test_300e() {
20359         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20360                 skip "Need MDS version at least 2.7.55"
20361         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20362         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20363
20364         local stripe_count
20365         local file
20366
20367         mkdir -p $DIR/$tdir
20368
20369         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20370                 error "set striped dir error"
20371
20372         touch $DIR/$tdir/striped_dir/a
20373         touch $DIR/$tdir/striped_dir/b
20374         touch $DIR/$tdir/striped_dir/c
20375
20376         mkdir $DIR/$tdir/striped_dir/dir_a
20377         mkdir $DIR/$tdir/striped_dir/dir_b
20378         mkdir $DIR/$tdir/striped_dir/dir_c
20379
20380         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_a ||
20381                 error "set striped adir under striped dir error"
20382
20383         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_b ||
20384                 error "set striped bdir under striped dir error"
20385
20386         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir/stp_c ||
20387                 error "set striped cdir under striped dir error"
20388
20389         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir/dir_b ||
20390                 error "rename dir under striped dir fails"
20391
20392         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir/stp_b ||
20393                 error "rename dir under different stripes fails"
20394
20395         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir/c ||
20396                 error "rename file under striped dir should succeed"
20397
20398         mrename $DIR/$tdir/striped_dir/dir_b $DIR/$tdir/striped_dir/dir_c ||
20399                 error "rename dir under striped dir should succeed"
20400
20401         rm -rf $DIR/$tdir
20402 }
20403 run_test 300e "check rename under striped directory"
20404
20405 test_300f() {
20406         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20407         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20408         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20409                 skip "Need MDS version at least 2.7.55"
20410
20411         local stripe_count
20412         local file
20413
20414         rm -rf $DIR/$tdir
20415         mkdir -p $DIR/$tdir
20416
20417         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir ||
20418                 error "set striped dir error"
20419
20420         $LFS setdirstripe -i 0 -c 2 -H all_char $DIR/$tdir/striped_dir1 ||
20421                 error "set striped dir error"
20422
20423         touch $DIR/$tdir/striped_dir/a
20424         mkdir $DIR/$tdir/striped_dir/dir_a
20425         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_a ||
20426                 error "create striped dir under striped dir fails"
20427
20428         touch $DIR/$tdir/striped_dir1/b
20429         mkdir $DIR/$tdir/striped_dir1/dir_b
20430         $LFS setdirstripe -i 0 -c 2 $DIR/$tdir/striped_dir/stp_b ||
20431                 error "create striped dir under striped dir fails"
20432
20433         mrename $DIR/$tdir/striped_dir/dir_a $DIR/$tdir/striped_dir1/dir_b ||
20434                 error "rename dir under different striped dir should fail"
20435
20436         mrename $DIR/$tdir/striped_dir/stp_a $DIR/$tdir/striped_dir1/stp_b ||
20437                 error "rename striped dir under diff striped dir should fail"
20438
20439         mrename $DIR/$tdir/striped_dir/a $DIR/$tdir/striped_dir1/a ||
20440                 error "rename file under diff striped dirs fails"
20441
20442         rm -rf $DIR/$tdir
20443 }
20444 run_test 300f "check rename cross striped directory"
20445
20446 test_300_check_default_striped_dir()
20447 {
20448         local dirname=$1
20449         local default_count=$2
20450         local default_index=$3
20451         local stripe_count
20452         local stripe_index
20453         local dir_stripe_index
20454         local dir
20455
20456         echo "checking $dirname $default_count $default_index"
20457         $LFS setdirstripe -D -c $default_count -i $default_index \
20458                                 -t all_char $DIR/$tdir/$dirname ||
20459                 error "set default stripe on striped dir error"
20460         stripe_count=$($LFS getdirstripe -D -c $DIR/$tdir/$dirname)
20461         [ $stripe_count -eq $default_count ] ||
20462                 error "expect $default_count get $stripe_count for $dirname"
20463
20464         stripe_index=$($LFS getdirstripe -D -i $DIR/$tdir/$dirname)
20465         [ $stripe_index -eq $default_index ] ||
20466                 error "expect $default_index get $stripe_index for $dirname"
20467
20468         mkdir $DIR/$tdir/$dirname/{test1,test2,test3,test4} ||
20469                                                 error "create dirs failed"
20470
20471         createmany -o $DIR/$tdir/$dirname/f- 10 || error "create files failed"
20472         unlinkmany $DIR/$tdir/$dirname/f- 10    || error "unlink files failed"
20473         for dir in $(find $DIR/$tdir/$dirname/*); do
20474                 stripe_count=$($LFS getdirstripe -c $dir)
20475                 [ $stripe_count -eq $default_count ] ||
20476                 [ $stripe_count -eq 0 ] || [ $default_count -eq 1 ] ||
20477                 error "stripe count $default_count != $stripe_count for $dir"
20478
20479                 stripe_index=$($LFS getdirstripe -i $dir)
20480                 [ $default_index -eq -1 ] ||
20481                         [ $stripe_index -eq $default_index ] ||
20482                         error "$stripe_index != $default_index for $dir"
20483
20484                 #check default stripe
20485                 stripe_count=$($LFS getdirstripe -D -c $dir)
20486                 [ $stripe_count -eq $default_count ] ||
20487                 error "default count $default_count != $stripe_count for $dir"
20488
20489                 stripe_index=$($LFS getdirstripe -D -i $dir)
20490                 [ $stripe_index -eq $default_index ] ||
20491                 error "default index $default_index != $stripe_index for $dir"
20492         done
20493         rmdir $DIR/$tdir/$dirname/* || error "rmdir failed"
20494 }
20495
20496 test_300g() {
20497         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20498         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20499                 skip "Need MDS version at least 2.7.55"
20500
20501         local dir
20502         local stripe_count
20503         local stripe_index
20504
20505         mkdir $DIR/$tdir
20506         mkdir $DIR/$tdir/normal_dir
20507
20508         #Checking when client cache stripe index
20509         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
20510         $LFS setdirstripe -D -i1 $DIR/$tdir/striped_dir ||
20511                 error "create striped_dir failed"
20512
20513         $LFS setdirstripe -i0 $DIR/$tdir/striped_dir/dir0 ||
20514                 error "create dir0 fails"
20515         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir0)
20516         [ $stripe_index -eq 0 ] ||
20517                 error "dir0 expect index 0 got $stripe_index"
20518
20519         mkdir $DIR/$tdir/striped_dir/dir1 ||
20520                 error "create dir1 fails"
20521         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/dir1)
20522         [ $stripe_index -eq 1 ] ||
20523                 error "dir1 expect index 1 got $stripe_index"
20524
20525         #check default stripe count/stripe index
20526         test_300_check_default_striped_dir normal_dir $MDSCOUNT 1
20527         test_300_check_default_striped_dir normal_dir 1 0
20528         test_300_check_default_striped_dir normal_dir 2 1
20529         test_300_check_default_striped_dir normal_dir 2 -1
20530
20531         #delete default stripe information
20532         echo "delete default stripeEA"
20533         $LFS setdirstripe -d $DIR/$tdir/normal_dir ||
20534                 error "set default stripe on striped dir error"
20535
20536         mkdir -p $DIR/$tdir/normal_dir/{test1,test2,test3,test4}
20537         for dir in $(find $DIR/$tdir/normal_dir/*); do
20538                 stripe_count=$($LFS getdirstripe -c $dir)
20539                 [ $stripe_count -eq 0 ] ||
20540                         error "expect 1 get $stripe_count for $dir"
20541                 stripe_index=$($LFS getdirstripe -i $dir)
20542                 [ $stripe_index -eq 0 ] ||
20543                         error "expect 0 get $stripe_index for $dir"
20544         done
20545 }
20546 run_test 300g "check default striped directory for normal directory"
20547
20548 test_300h() {
20549         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20550         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20551                 skip "Need MDS version at least 2.7.55"
20552
20553         local dir
20554         local stripe_count
20555
20556         mkdir $DIR/$tdir
20557         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20558                 error "set striped dir error"
20559
20560         test_300_check_default_striped_dir striped_dir $MDSCOUNT 1
20561         test_300_check_default_striped_dir striped_dir 1 0
20562         test_300_check_default_striped_dir striped_dir 2 1
20563         test_300_check_default_striped_dir striped_dir 2 -1
20564
20565         #delete default stripe information
20566         $LFS setdirstripe -d $DIR/$tdir/striped_dir ||
20567                 error "set default stripe on striped dir error"
20568
20569         mkdir -p $DIR/$tdir/striped_dir/{test1,test2,test3,test4}
20570         for dir in $(find $DIR/$tdir/striped_dir/*); do
20571                 stripe_count=$($LFS getdirstripe -c $dir)
20572                 [ $stripe_count -eq 0 ] ||
20573                         error "expect 1 get $stripe_count for $dir"
20574         done
20575 }
20576 run_test 300h "check default striped directory for striped directory"
20577
20578 test_300i() {
20579         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20580         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20581         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20582                 skip "Need MDS version at least 2.7.55"
20583
20584         local stripe_count
20585         local file
20586
20587         mkdir $DIR/$tdir
20588
20589         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20590                 error "set striped dir error"
20591
20592         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20593                 error "create files under striped dir failed"
20594
20595         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir ||
20596                 error "set striped hashdir error"
20597
20598         $LFS setdirstripe -i0 -c$MDSCOUNT -H all_char $DIR/$tdir/hashdir/d0 ||
20599                 error "create dir0 under hash dir failed"
20600         $LFS setdirstripe -i0 -c$MDSCOUNT -H fnv_1a_64 $DIR/$tdir/hashdir/d1 ||
20601                 error "create dir1 under hash dir failed"
20602
20603         # unfortunately, we need to umount to clear dir layout cache for now
20604         # once we fully implement dir layout, we can drop this
20605         umount_client $MOUNT || error "umount failed"
20606         mount_client $MOUNT || error "mount failed"
20607
20608         $LFS find -H fnv_1a_64 $DIR/$tdir/hashdir
20609         local dircnt=$($LFS find -H fnv_1a_64 $DIR/$tdir/hashdir | wc -l)
20610         [ $dircnt -eq 1 ] || error "lfs find striped dir got:$dircnt,except:1"
20611
20612         #set the stripe to be unknown hash type
20613         #define OBD_FAIL_UNKNOWN_LMV_STRIPE     0x1901
20614         $LCTL set_param fail_loc=0x1901
20615         for ((i = 0; i < 10; i++)); do
20616                 $CHECKSTAT -t file $DIR/$tdir/striped_dir/f-$i ||
20617                         error "stat f-$i failed"
20618                 rm $DIR/$tdir/striped_dir/f-$i || error "unlink f-$i failed"
20619         done
20620
20621         touch $DIR/$tdir/striped_dir/f0 &&
20622                 error "create under striped dir with unknown hash should fail"
20623
20624         $LCTL set_param fail_loc=0
20625
20626         umount_client $MOUNT || error "umount failed"
20627         mount_client $MOUNT || error "mount failed"
20628
20629         return 0
20630 }
20631 run_test 300i "client handle unknown hash type striped directory"
20632
20633 test_300j() {
20634         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20635         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20636         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20637                 skip "Need MDS version at least 2.7.55"
20638
20639         local stripe_count
20640         local file
20641
20642         mkdir $DIR/$tdir
20643
20644         #define OBD_FAIL_SPLIT_UPDATE_REC       0x1702
20645         $LCTL set_param fail_loc=0x1702
20646         $LFS setdirstripe -i 0 -c$MDSCOUNT -H all_char $DIR/$tdir/striped_dir ||
20647                 error "set striped dir error"
20648
20649         createmany -o $DIR/$tdir/striped_dir/f- 10 ||
20650                 error "create files under striped dir failed"
20651
20652         $LCTL set_param fail_loc=0
20653
20654         rm -rf $DIR/$tdir || error "unlink striped dir fails"
20655
20656         return 0
20657 }
20658 run_test 300j "test large update record"
20659
20660 test_300k() {
20661         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20662         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20663         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20664                 skip "Need MDS version at least 2.7.55"
20665
20666         # this test needs a huge transaction
20667         local kb
20668         kb=$(do_facet $SINGLEMDS "$LCTL get_param -n \
20669              osd*.$FSNAME-MDT0000.kbytestotal")
20670         [ $kb -lt $((1024*1024)) ] && skip "MDT0 too small: $kb"
20671
20672         local stripe_count
20673         local file
20674
20675         mkdir $DIR/$tdir
20676
20677         #define OBD_FAIL_LARGE_STRIPE   0x1703
20678         $LCTL set_param fail_loc=0x1703
20679         $LFS setdirstripe -i 0 -c192 $DIR/$tdir/striped_dir ||
20680                 error "set striped dir error"
20681         $LCTL set_param fail_loc=0
20682
20683         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20684                 error "getstripeddir fails"
20685         rm -rf $DIR/$tdir/striped_dir ||
20686                 error "unlink striped dir fails"
20687
20688         return 0
20689 }
20690 run_test 300k "test large striped directory"
20691
20692 test_300l() {
20693         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20694         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20695         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20696                 skip "Need MDS version at least 2.7.55"
20697
20698         local stripe_index
20699
20700         test_mkdir -p $DIR/$tdir/striped_dir
20701         chown $RUNAS_ID $DIR/$tdir/striped_dir ||
20702                         error "chown $RUNAS_ID failed"
20703         $LFS setdirstripe -i 1 -D $DIR/$tdir/striped_dir ||
20704                 error "set default striped dir failed"
20705
20706         #define OBD_FAIL_MDS_STALE_DIR_LAYOUT    0x158
20707         $LCTL set_param fail_loc=0x80000158
20708         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir || error "create dir fails"
20709
20710         stripe_index=$($LFS getdirstripe -i $DIR/$tdir/striped_dir/test_dir)
20711         [ $stripe_index -eq 1 ] ||
20712                 error "expect 1 get $stripe_index for $dir"
20713 }
20714 run_test 300l "non-root user to create dir under striped dir with stale layout"
20715
20716 test_300m() {
20717         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20718         [ $MDSCOUNT -ge 2 ] && skip_env "Only for single MDT"
20719         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20720                 skip "Need MDS version at least 2.7.55"
20721
20722         mkdir -p $DIR/$tdir/striped_dir
20723         $LFS setdirstripe -D -c 1 $DIR/$tdir/striped_dir ||
20724                 error "set default stripes dir error"
20725
20726         mkdir $DIR/$tdir/striped_dir/a || error "mkdir a fails"
20727
20728         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/a)
20729         [ $stripe_count -eq 0 ] ||
20730                         error "expect 0 get $stripe_count for a"
20731
20732         $LFS setdirstripe -D -c 2 $DIR/$tdir/striped_dir ||
20733                 error "set default stripes dir error"
20734
20735         mkdir $DIR/$tdir/striped_dir/b || error "mkdir b fails"
20736
20737         stripe_count=$($LFS getdirstripe -c $DIR/$tdir/striped_dir/b)
20738         [ $stripe_count -eq 0 ] ||
20739                         error "expect 0 get $stripe_count for b"
20740
20741         $LFS setdirstripe -D -c1 -i2 $DIR/$tdir/striped_dir ||
20742                 error "set default stripes dir error"
20743
20744         mkdir $DIR/$tdir/striped_dir/c &&
20745                 error "default stripe_index is invalid, mkdir c should fails"
20746
20747         rm -rf $DIR/$tdir || error "rmdir fails"
20748 }
20749 run_test 300m "setstriped directory on single MDT FS"
20750
20751 cleanup_300n() {
20752         local list=$(comma_list $(mdts_nodes))
20753
20754         trap 0
20755         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20756 }
20757
20758 test_300n() {
20759         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20760         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20761         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20762                 skip "Need MDS version at least 2.7.55"
20763         remote_mds_nodsh && skip "remote MDS with nodsh"
20764
20765         local stripe_index
20766         local list=$(comma_list $(mdts_nodes))
20767
20768         trap cleanup_300n RETURN EXIT
20769         mkdir -p $DIR/$tdir
20770         chmod 777 $DIR/$tdir
20771         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT \
20772                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20773                 error "create striped dir succeeds with gid=0"
20774
20775         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20776         $RUNAS $LFS setdirstripe -i0 -c$MDSCOUNT $DIR/$tdir/striped_dir ||
20777                 error "create striped dir fails with gid=-1"
20778
20779         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20780         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D \
20781                                 $DIR/$tdir/striped_dir > /dev/null 2>&1 &&
20782                 error "set default striped dir succeeds with gid=0"
20783
20784
20785         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=-1
20786         $RUNAS $LFS setdirstripe -i 1 -c$MDSCOUNT -D $DIR/$tdir/striped_dir ||
20787                 error "set default striped dir fails with gid=-1"
20788
20789
20790         do_nodes $list $LCTL set_param -n mdt.*.enable_remote_dir_gid=0
20791         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir ||
20792                                         error "create test_dir fails"
20793         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir1 ||
20794                                         error "create test_dir1 fails"
20795         $RUNAS mkdir $DIR/$tdir/striped_dir/test_dir2 ||
20796                                         error "create test_dir2 fails"
20797         cleanup_300n
20798 }
20799 run_test 300n "non-root user to create dir under striped dir with default EA"
20800
20801 test_300o() {
20802         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20803         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20804         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20805                 skip "Need MDS version at least 2.7.55"
20806
20807         local numfree1
20808         local numfree2
20809
20810         mkdir -p $DIR/$tdir
20811
20812         numfree1=$(lctl get_param -n mdc.*MDT0000*.filesfree)
20813         numfree2=$(lctl get_param -n mdc.*MDT0001*.filesfree)
20814         if [ $numfree1 -lt 66000 ] || [ $numfree2 -lt 66000 ]; then
20815                 skip "not enough free inodes $numfree1 $numfree2"
20816         fi
20817
20818         numfree1=$(lctl get_param -n mdc.*MDT0000-mdc-*.kbytesfree)
20819         numfree2=$(lctl get_param -n mdc.*MDT0001-mdc-*.kbytesfree)
20820         if [ $numfree1 -lt 300000 ] || [ $numfree2 -lt 300000 ]; then
20821                 skip "not enough free space $numfree1 $numfree2"
20822         fi
20823
20824         $LFS setdirstripe -c2 $DIR/$tdir/striped_dir ||
20825                 error "setdirstripe fails"
20826
20827         createmany -d $DIR/$tdir/striped_dir/d 131000 ||
20828                 error "create dirs fails"
20829
20830         $LCTL set_param ldlm.namespaces.*mdc-*.lru_size=0
20831         ls $DIR/$tdir/striped_dir > /dev/null ||
20832                 error "ls striped dir fails"
20833         unlinkmany -d $DIR/$tdir/striped_dir/d 131000 ||
20834                 error "unlink big striped dir fails"
20835 }
20836 run_test 300o "unlink big sub stripe(> 65000 subdirs)"
20837
20838 test_300p() {
20839         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20840         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20841         remote_mds_nodsh && skip "remote MDS with nodsh"
20842
20843         mkdir -p $DIR/$tdir
20844
20845         #define OBD_FAIL_OUT_ENOSPC     0x1704
20846         do_facet mds2 lctl set_param fail_loc=0x80001704
20847         $LFS setdirstripe -i 0 -c2 $DIR/$tdir/bad_striped_dir > /dev/null 2>&1 \
20848                  && error "create striped directory should fail"
20849
20850         [ -e $DIR/$tdir/bad_striped_dir ] && error "striped dir exists"
20851
20852         $LFS setdirstripe -c2 $DIR/$tdir/bad_striped_dir
20853         true
20854 }
20855 run_test 300p "create striped directory without space"
20856
20857 test_300q() {
20858         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20859         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
20860
20861         local fd=$(free_fd)
20862         local cmd="exec $fd<$tdir"
20863         cd $DIR
20864         $LFS mkdir -c $MDSCOUNT $tdir || error "create $tdir fails"
20865         eval $cmd
20866         cmd="exec $fd<&-"
20867         trap "eval $cmd" EXIT
20868         cd $tdir || error "cd $tdir fails"
20869         rmdir  ../$tdir || error "rmdir $tdir fails"
20870         mkdir local_dir && error "create dir succeeds"
20871         $LFS setdirstripe -i1 remote_dir && error "create remote dir succeeds"
20872         eval $cmd
20873         return 0
20874 }
20875 run_test 300q "create remote directory under orphan directory"
20876
20877 test_300r() {
20878         [ $MDS1_VERSION -lt $(version_code 2.7.55) ] &&
20879                 skip "Need MDS version at least 2.7.55" && return
20880         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return
20881
20882         mkdir $DIR/$tdir
20883
20884         $LFS setdirstripe -i 0 -c -1 $DIR/$tdir/striped_dir ||
20885                 error "set striped dir error"
20886
20887         $LFS getdirstripe $DIR/$tdir/striped_dir ||
20888                 error "getstripeddir fails"
20889
20890         local stripe_count
20891         stripe_count=$($LFS getdirstripe $DIR/$tdir/striped_dir |
20892                       awk '/lmv_stripe_count:/ { print $2 }')
20893
20894         [ $MDSCOUNT -ne $stripe_count ] &&
20895                 error "wrong stripe count $stripe_count expected $MDSCOUNT"
20896
20897         rm -rf $DIR/$tdir/striped_dir ||
20898                 error "unlink striped dir fails"
20899 }
20900 run_test 300r "test -1 striped directory"
20901
20902 prepare_remote_file() {
20903         mkdir $DIR/$tdir/src_dir ||
20904                 error "create remote source failed"
20905
20906         cp /etc/hosts $DIR/$tdir/src_dir/a ||
20907                  error "cp to remote source failed"
20908         touch $DIR/$tdir/src_dir/a
20909
20910         $LFS mkdir -i 1 $DIR/$tdir/tgt_dir ||
20911                 error "create remote target dir failed"
20912
20913         touch $DIR/$tdir/tgt_dir/b
20914
20915         mrename $DIR/$tdir/src_dir/a $DIR/$tdir/tgt_dir/b ||
20916                 error "rename dir cross MDT failed!"
20917
20918         $CHECKSTAT -t file $DIR/$tdir/src_dir/a &&
20919                 error "src_child still exists after rename"
20920
20921         $CHECKSTAT -t file $DIR/$tdir/tgt_dir/b ||
20922                 error "missing file(a) after rename"
20923
20924         diff /etc/hosts $DIR/$tdir/tgt_dir/b ||
20925                 error "diff after rename"
20926 }
20927
20928 test_310a() {
20929         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20930         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20931
20932         local remote_file=$DIR/$tdir/tgt_dir/b
20933
20934         mkdir -p $DIR/$tdir
20935
20936         prepare_remote_file || error "prepare remote file failed"
20937
20938         #open-unlink file
20939         $OPENUNLINK $remote_file $remote_file ||
20940                 error "openunlink $remote_file failed"
20941         $CHECKSTAT -a $remote_file || error "$remote_file exists"
20942 }
20943 run_test 310a "open unlink remote file"
20944
20945 test_310b() {
20946         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 4 MDTs"
20947         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20948
20949         local remote_file=$DIR/$tdir/tgt_dir/b
20950
20951         mkdir -p $DIR/$tdir
20952
20953         prepare_remote_file || error "prepare remote file failed"
20954
20955         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20956         $MULTIOP $DIR/$tfile Ouc || error "mulitop failed"
20957         $CHECKSTAT -t file $remote_file || error "check file failed"
20958 }
20959 run_test 310b "unlink remote file with multiple links while open"
20960
20961 test_310c() {
20962         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20963         [[ $MDSCOUNT -lt 4 ]] && skip_env "needs >= 4 MDTs"
20964
20965         local remote_file=$DIR/$tdir/tgt_dir/b
20966
20967         mkdir -p $DIR/$tdir
20968
20969         prepare_remote_file || error "prepare remote file failed"
20970
20971         ln $remote_file $DIR/$tfile || error "link failed for remote file"
20972         multiop_bg_pause $remote_file O_uc ||
20973                         error "mulitop failed for remote file"
20974         MULTIPID=$!
20975         $MULTIOP $DIR/$tfile Ouc
20976         kill -USR1 $MULTIPID
20977         wait $MULTIPID
20978 }
20979 run_test 310c "open-unlink remote file with multiple links"
20980
20981 #LU-4825
20982 test_311() {
20983         [ $PARALLEL == "yes" ] && skip "skip parallel run"
20984         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
20985         [ $MDS1_VERSION -lt $(version_code 2.8.54) ] &&
20986                 skip "lustre < 2.8.54 does not contain LU-4825 fix"
20987         remote_mds_nodsh && skip "remote MDS with nodsh"
20988
20989         local old_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
20990         local mdts=$(comma_list $(mdts_nodes))
20991
20992         mkdir -p $DIR/$tdir
20993         $LFS setstripe -i 0 -c 1 $DIR/$tdir
20994         createmany -o $DIR/$tdir/$tfile. 1000
20995
20996         # statfs data is not real time, let's just calculate it
20997         old_iused=$((old_iused + 1000))
20998
20999         local count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
21000                         osp.*OST0000*MDT0000.create_count")
21001         local max_count=$(do_facet $SINGLEMDS "$LCTL get_param -n \
21002                                 osp.*OST0000*MDT0000.max_create_count")
21003         do_nodes $mdts "$LCTL set_param -n osp.*OST0000*.max_create_count=0"
21004
21005         $LFS setstripe -i 0 $DIR/$tdir/$tfile || error "setstripe failed"
21006         local index=$($LFS getstripe -i $DIR/$tdir/$tfile)
21007         [ $index -ne 0 ] || error "$tfile stripe index is 0"
21008
21009         unlinkmany $DIR/$tdir/$tfile. 1000
21010
21011         do_nodes $mdts "$LCTL set_param -n \
21012                         osp.*OST0000*.max_create_count=$max_count"
21013         [ $MDS1_VERSION -lt $(version_code 2.12.51) ] &&
21014                 do_nodes $mdts "$LCTL set_param -n \
21015                                 osp.*OST0000*.create_count=$count"
21016         do_nodes $mdts "$LCTL get_param osp.*OST0000*.create_count" |
21017                         grep "=0" && error "create_count is zero"
21018
21019         local new_iused
21020         for i in $(seq 120); do
21021                 new_iused=$($LFS df -i | grep OST0000 | awk '{ print $3 }')
21022                 # system may be too busy to destroy all objs in time, use
21023                 # a somewhat small value to not fail autotest
21024                 [ $((old_iused - new_iused)) -gt 400 ] && break
21025                 sleep 1
21026         done
21027
21028         echo "waited $i sec, old Iused $old_iused, new Iused $new_iused"
21029         [ $((old_iused - new_iused)) -gt 400 ] ||
21030                 error "objs not destroyed after unlink"
21031 }
21032 run_test 311 "disable OSP precreate, and unlink should destroy objs"
21033
21034 zfs_oid_to_objid()
21035 {
21036         local ost=$1
21037         local objid=$2
21038
21039         local vdevdir=$(dirname $(facet_vdevice $ost))
21040         local cmd="$ZDB -e -p $vdevdir -ddddd $(facet_device $ost)"
21041         local zfs_zapid=$(do_facet $ost $cmd |
21042                           grep -w "/O/0/d$((objid%32))" -C 5 |
21043                           awk '/Object/{getline; print $1}')
21044         local zfs_objid=$(do_facet $ost $cmd $zfs_zapid |
21045                           awk "/$objid = /"'{printf $3}')
21046
21047         echo $zfs_objid
21048 }
21049
21050 zfs_object_blksz() {
21051         local ost=$1
21052         local objid=$2
21053
21054         local vdevdir=$(dirname $(facet_vdevice $ost))
21055         local cmd="$ZDB -e -p $vdevdir -dddd $(facet_device $ost)"
21056         local blksz=$(do_facet $ost $cmd $objid |
21057                       awk '/dblk/{getline; printf $4}')
21058
21059         case "${blksz: -1}" in
21060                 k|K) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024)) ;;
21061                 m|M) blksz=$((${blksz:0:$((${#blksz} - 1))}*1024*1024)) ;;
21062                 *) ;;
21063         esac
21064
21065         echo $blksz
21066 }
21067
21068 test_312() { # LU-4856
21069         remote_ost_nodsh && skip "remote OST with nodsh"
21070         [ "$ost1_FSTYPE" = "zfs" ] ||
21071                 skip_env "the test only applies to zfs"
21072
21073         local max_blksz=$(do_facet ost1 \
21074                           $ZFS get -p recordsize $(facet_device ost1) |
21075                           awk '!/VALUE/{print $3}')
21076
21077         # to make life a little bit easier
21078         $LFS mkdir -c 1 -i 0 $DIR/$tdir
21079         $LFS setstripe -c 1 -i 0 $DIR/$tdir
21080
21081         local tf=$DIR/$tdir/$tfile
21082         touch $tf
21083         local oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21084
21085         # Get ZFS object id
21086         local zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21087         # block size change by sequential overwrite
21088         local bs
21089
21090         for ((bs=$PAGE_SIZE; bs <= max_blksz; bs *= 4)) ; do
21091                 dd if=/dev/zero of=$tf bs=$bs count=1 oflag=sync conv=notrunc
21092
21093                 local blksz=$(zfs_object_blksz ost1 $zfs_objid)
21094                 [ $blksz -eq $bs ] || error "blksz error: $blksz, expected: $bs"
21095         done
21096         rm -f $tf
21097
21098         # block size change by sequential append write
21099         dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=1 oflag=sync conv=notrunc
21100         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21101         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21102         local count
21103
21104         for ((count = 1; count < $((max_blksz / PAGE_SIZE)); count *= 2)); do
21105                 dd if=/dev/zero of=$tf bs=$PAGE_SIZE count=$count seek=$count \
21106                         oflag=sync conv=notrunc
21107
21108                 blksz=$(zfs_object_blksz ost1 $zfs_objid)
21109                 [ $blksz -eq $((2 * count * PAGE_SIZE)) ] ||
21110                         error "blksz error, actual $blksz, " \
21111                                 "expected: 2 * $count * $PAGE_SIZE"
21112         done
21113         rm -f $tf
21114
21115         # random write
21116         touch $tf
21117         oid=$($LFS getstripe $tf | awk '/obdidx/{getline; print $2}')
21118         zfs_objid=$(zfs_oid_to_objid ost1 $oid)
21119
21120         dd if=/dev/zero of=$tf bs=1K count=1 oflag=sync conv=notrunc
21121         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21122         [ $blksz -eq $PAGE_SIZE ] ||
21123                 error "blksz error: $blksz, expected: $PAGE_SIZE"
21124
21125         dd if=/dev/zero of=$tf bs=64K count=1 oflag=sync conv=notrunc seek=128
21126         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21127         [ $blksz -eq 65536 ] || error "blksz error: $blksz, expected: 64k"
21128
21129         dd if=/dev/zero of=$tf bs=1M count=1 oflag=sync conv=notrunc
21130         blksz=$(zfs_object_blksz ost1 $zfs_objid)
21131         [ $blksz -eq 65536 ] || error "rewrite error: $blksz, expected: 64k"
21132 }
21133 run_test 312 "make sure ZFS adjusts its block size by write pattern"
21134
21135 test_313() {
21136         remote_ost_nodsh && skip "remote OST with nodsh"
21137
21138         local file=$DIR/$tfile
21139
21140         rm -f $file
21141         $LFS setstripe -c 1 -i 0 $file || error "setstripe failed"
21142
21143         # define OBD_FAIL_TGT_RCVD_EIO           0x720
21144         do_facet ost1 "$LCTL set_param fail_loc=0x720"
21145         dd if=/dev/zero of=$file bs=$PAGE_SIZE oflag=direct count=1 &&
21146                 error "write should failed"
21147         do_facet ost1 "$LCTL set_param fail_loc=0"
21148         rm -f $file
21149 }
21150 run_test 313 "io should fail after last_rcvd update fail"
21151
21152 test_314() {
21153         [ $OSTCOUNT -lt 2 ] && skip "needs >= 2 OSTs"
21154
21155         $LFS setstripe -c 2 -i 0 $DIR/$tfile || error "setstripe failed"
21156         do_facet ost1 "$LCTL set_param fail_loc=0x720"
21157         rm -f $DIR/$tfile
21158         wait_delete_completed
21159         do_facet ost1 "$LCTL set_param fail_loc=0"
21160 }
21161 run_test 314 "OSP shouldn't fail after last_rcvd update failure"
21162
21163 test_315() { # LU-618
21164         [ -f /proc/$$/io ] || skip_env "no IO accounting in kernel"
21165
21166         local file=$DIR/$tfile
21167         rm -f $file
21168
21169         $MULTIOP $file oO_CREAT:O_DIRECT:O_RDWR:w4063232c ||
21170                 error "multiop file write failed"
21171         $MULTIOP $file oO_RDONLY:r4063232_c &
21172         PID=$!
21173
21174         sleep 2
21175
21176         local rbytes=$(awk '/read_bytes/ { print $2 }' /proc/$PID/io)
21177         kill -USR1 $PID
21178
21179         [ $rbytes -gt 4000000 ] || error "read is not accounted ($rbytes)"
21180         rm -f $file
21181 }
21182 run_test 315 "read should be accounted"
21183
21184 test_316() {
21185         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21186         large_xattr_enabled || skip_env "ea_inode feature disabled"
21187
21188         rm -rf $DIR/$tdir/d
21189         mkdir -p $DIR/$tdir/d
21190         chown nobody $DIR/$tdir/d
21191         touch $DIR/$tdir/d/file
21192
21193         $LFS mv -m1 $DIR/$tdir/d || error "lfs mv failed"
21194 }
21195 run_test 316 "lfs mv"
21196
21197 test_317() {
21198         [ $MDS1_VERSION -lt $(version_code 2.11.53) ] &&
21199                 skip "Need MDS version at least 2.11.53"
21200         if [ "$ost1_FSTYPE" == "zfs" ]; then
21201                 skip "LU-10370: no implementation for ZFS"
21202         fi
21203
21204         local trunc_sz
21205         local grant_blk_size
21206
21207         grant_blk_size=$($LCTL get_param osc.$FSNAME*.import |
21208                         awk '/grant_block_size:/ { print $2; exit; }')
21209         #
21210         # Create File of size 5M. Truncate it to below size's and verify
21211         # blocks count.
21212         #
21213         dd if=/dev/zero of=$DIR/$tfile bs=5M count=1 conv=fsync ||
21214                 error "Create file $DIR/$tfile failed"
21215         stack_trap "rm -f $DIR/$tfile" EXIT
21216
21217         for trunc_sz in 2097152 4097 4000 509 0; do
21218                 $TRUNCATE $DIR/$tfile $trunc_sz ||
21219                         error "truncate $tfile to $trunc_sz failed"
21220                 local sz=$(stat --format=%s $DIR/$tfile)
21221                 local blk=$(stat --format=%b $DIR/$tfile)
21222                 local trunc_blk=$((((trunc_sz + (grant_blk_size - 1) ) /
21223                                      grant_blk_size) * 8))
21224
21225                 if [[ $blk -ne $trunc_blk ]]; then
21226                         $(which stat) $DIR/$tfile
21227                         error "Expected Block $trunc_blk got $blk for $tfile"
21228                 fi
21229
21230                 $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
21231                         error "Expected Size $trunc_sz got $sz for $tfile"
21232         done
21233
21234         #
21235         # sparse file test
21236         # Create file with a hole and write actual two blocks. Block count
21237         # must be 16.
21238         #
21239         dd if=/dev/zero of=$DIR/$tfile bs=$grant_blk_size count=2 seek=5 \
21240                 conv=fsync || error "Create file : $DIR/$tfile"
21241
21242         # Calculate the final truncate size.
21243         trunc_sz=$(($(stat --format=%s $DIR/$tfile) - (grant_blk_size + 1)))
21244
21245         #
21246         # truncate to size $trunc_sz bytes. Strip the last block
21247         # The block count must drop to 8
21248         #
21249         $TRUNCATE $DIR/$tfile $trunc_sz ||
21250                 error "truncate $tfile to $trunc_sz failed"
21251
21252         local trunc_bsz=$((grant_blk_size / $(stat --format=%B $DIR/$tfile)))
21253         sz=$(stat --format=%s $DIR/$tfile)
21254         blk=$(stat --format=%b $DIR/$tfile)
21255
21256         if [[ $blk -ne $trunc_bsz ]]; then
21257                 $(which stat) $DIR/$tfile
21258                 error "Expected Block $trunc_bsz got $blk for $tfile"
21259         fi
21260
21261         $CHECKSTAT -s $trunc_sz $DIR/$tfile ||
21262                 error "Expected Size $trunc_sz got $sz for $tfile"
21263 }
21264 run_test 317 "Verify blocks get correctly update after truncate"
21265
21266 test_318() {
21267         local old_max_active=$($LCTL get_param -n \
21268                             llite.*.max_read_ahead_async_active 2>/dev/null)
21269
21270         $LCTL set_param llite.*.max_read_ahead_async_active=256
21271         local max_active=$($LCTL get_param -n \
21272                            llite.*.max_read_ahead_async_active 2>/dev/null)
21273         [ $max_active -ne 256 ] && error "expected 256 but got $max_active"
21274
21275         $LCTL set_param llite.*.max_read_ahead_async_active=0 ||
21276                 error "set max_read_ahead_async_active should succeed"
21277
21278         $LCTL set_param llite.*.max_read_ahead_async_active=512
21279         max_active=$($LCTL get_param -n \
21280                      llite.*.max_read_ahead_async_active 2>/dev/null)
21281         [ $max_active -eq 512 ] || error "expected 512 but got $max_active"
21282
21283         # restore @max_active
21284         [ $old_max_active -ne 0 ] && $LCTL set_param \
21285                 llite.*.max_read_ahead_async_active=$old_max_active
21286
21287         local old_threshold=$($LCTL get_param -n \
21288                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
21289         local max_per_file_mb=$($LCTL get_param -n \
21290                 llite.*.max_read_ahead_per_file_mb 2>/dev/null)
21291
21292         local invalid=$(($max_per_file_mb + 1))
21293         $LCTL set_param \
21294                 llite.*.read_ahead_async_file_threshold_mb=$invalid\
21295                         && error "set $invalid should fail"
21296
21297         local valid=$(($invalid - 1))
21298         $LCTL set_param \
21299                 llite.*.read_ahead_async_file_threshold_mb=$valid ||
21300                         error "set $valid should succeed"
21301         local threshold=$($LCTL get_param -n \
21302                 llite.*.read_ahead_async_file_threshold_mb 2>/dev/null)
21303         [ $threshold -eq $valid ] || error \
21304                 "expect threshold $valid got $threshold"
21305         $LCTL set_param \
21306                 llite.*.read_ahead_async_file_threshold_mb=$old_threshold
21307 }
21308 run_test 318 "Verify async readahead tunables"
21309
21310 test_319() {
21311         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs" && return 0
21312
21313         local before=$(date +%s)
21314         local evict
21315         local mdir=$DIR/$tdir
21316         local file=$mdir/xxx
21317
21318         $LFS mkdir -i0 $mdir || error "mkdir $mdir fails"
21319         touch $file
21320
21321 #define OBD_FAIL_LDLM_LOCAL_CANCEL_PAUSE 0x32c
21322         $LCTL set_param fail_val=5 fail_loc=0x8000032c
21323         $LFS mv -m1 $file &
21324
21325         sleep 1
21326         dd if=$file of=/dev/null
21327         wait
21328         evict=$($LCTL get_param mdc.$FSNAME-MDT*.state |
21329           awk -F"[ [,]" '/EVICTED ]$/ { if (mx<$5) {mx=$5;} } END { print mx }')
21330
21331         [ -z "$evict" ] || [[ $evict -le $before ]] || error "eviction happened"
21332 }
21333 run_test 319 "lost lease lock on migrate error"
21334
21335 test_398a() { # LU-4198
21336         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21337         $LCTL set_param ldlm.namespaces.*.lru_size=clear
21338
21339         # request a new lock on client
21340         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
21341
21342         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
21343         local lock_count=$($LCTL get_param -n \
21344                            ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
21345         [[ $lock_count -eq 0 ]] || error "lock should be cancelled by direct IO"
21346
21347         $LCTL set_param ldlm.namespaces.*-OST0000-osc-ffff*.lru_size=clear
21348
21349         # no lock cached, should use lockless IO and not enqueue new lock
21350         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1 oflag=direct conv=notrunc
21351         lock_count=$($LCTL get_param -n \
21352                      ldlm.namespaces.*-OST0000-osc-ffff*.lru_size)
21353         [[ $lock_count -eq 0 ]] || error "no lock should be held by direct IO"
21354 }
21355 run_test 398a "direct IO should cancel lock otherwise lockless"
21356
21357 test_398b() { # LU-4198
21358         which fio || skip_env "no fio installed"
21359         $LFS setstripe -c -1 $DIR/$tfile
21360
21361         local size=12
21362         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size
21363
21364         local njobs=4
21365         echo "mix direct rw ${size}M to OST0 by fio with $njobs jobs..."
21366         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
21367                 --numjobs=$njobs --fallocate=none \
21368                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21369                 --filename=$DIR/$tfile &
21370         bg_pid=$!
21371
21372         echo "mix buffer rw ${size}M to OST0 by fio with $njobs jobs..."
21373         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE \
21374                 --numjobs=$njobs --fallocate=none \
21375                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21376                 --filename=$DIR/$tfile || true
21377         wait $bg_pid
21378
21379         rm -rf $DIR/$tfile
21380 }
21381 run_test 398b "DIO and buffer IO race"
21382
21383 test_398c() { # LU-4198
21384         which fio || skip_env "no fio installed"
21385
21386         saved_debug=$($LCTL get_param -n debug)
21387         $LCTL set_param debug=0
21388
21389         local size=$(lctl get_param -n osc.$FSNAME-OST0000*.kbytesavail | head -1)
21390         ((size /= 1024)) # by megabytes
21391         ((size /= 2)) # write half of the OST at most
21392         [ $size -gt 40 ] && size=40 #reduce test time anyway
21393
21394         $LFS setstripe -c 1 $DIR/$tfile
21395
21396         # it seems like ldiskfs reserves more space than necessary if the
21397         # writing blocks are not mapped, so it extends the file firstly
21398         dd if=/dev/zero of=$DIR/$tfile bs=1M count=$size && sync
21399         cancel_lru_locks osc
21400
21401         # clear and verify rpc_stats later
21402         $LCTL set_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats=clear
21403
21404         local njobs=4
21405         echo "writing ${size}M to OST0 by fio with $njobs jobs..."
21406         fio --name=rand-write --rw=randwrite --bs=$PAGE_SIZE --direct=1 \
21407                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
21408                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21409                 --filename=$DIR/$tfile
21410         [ $? -eq 0 ] || error "fio write error"
21411
21412         [ $($LCTL get_param -n \
21413          ldlm.namespaces.${FSNAME}-OST0000-osc-ffff*.lock_count) -eq 0 ] ||
21414                 error "Locks were requested while doing AIO"
21415
21416         # get the percentage of 1-page I/O
21417         pct=$($LCTL get_param osc.${FSNAME}-OST0000-osc-ffff*.rpc_stats |
21418                 grep -A 1 'pages per rpc' | grep -v 'pages per rpc' |
21419                 awk '{print $7}')
21420         [ $pct -le 50 ] || error "$pct% of I/O are 1-page"
21421
21422         echo "mix rw ${size}M to OST0 by fio with $njobs jobs..."
21423         fio --name=rand-rw --rw=randrw --bs=$PAGE_SIZE --direct=1 \
21424                 --numjobs=$njobs --fallocate=none --ioengine=libaio \
21425                 --iodepth=16 --allow_file_create=0 --size=$((size/njobs))M \
21426                 --filename=$DIR/$tfile
21427         [ $? -eq 0 ] || error "fio mixed read write error"
21428
21429         rm -rf $DIR/$tfile
21430         $LCTL set_param debug="$saved_debug"
21431 }
21432 run_test 398c "run fio to test AIO"
21433
21434 test_fake_rw() {
21435         local read_write=$1
21436         if [ "$read_write" = "write" ]; then
21437                 local dd_cmd="dd if=/dev/zero of=$DIR/$tfile"
21438         elif [ "$read_write" = "read" ]; then
21439                 local dd_cmd="dd of=/dev/null if=$DIR/$tfile"
21440         else
21441                 error "argument error"
21442         fi
21443
21444         # turn off debug for performance testing
21445         local saved_debug=$($LCTL get_param -n debug)
21446         $LCTL set_param debug=0
21447
21448         $LFS setstripe -c 1 -i 0 $DIR/$tfile
21449
21450         # get ost1 size - $FSNAME-OST0000
21451         local ost1_avail_size=$($LFS df | awk /${ost1_svc}/'{ print $4 }')
21452         local blocks=$((ost1_avail_size/2/1024)) # half avail space by megabytes
21453         [ $blocks -gt 1000 ] && blocks=1000 # 1G in maximum
21454
21455         if [ "$read_write" = "read" ]; then
21456                 truncate -s $(expr 1048576 \* $blocks) $DIR/$tfile
21457         fi
21458
21459         local start_time=$(date +%s.%N)
21460         $dd_cmd bs=1M count=$blocks oflag=sync ||
21461                 error "real dd $read_write error"
21462         local duration=$(bc <<< "$(date +%s.%N) - $start_time")
21463
21464         if [ "$read_write" = "write" ]; then
21465                 rm -f $DIR/$tfile
21466         fi
21467
21468         # define OBD_FAIL_OST_FAKE_RW           0x238
21469         do_facet ost1 $LCTL set_param fail_loc=0x238
21470
21471         local start_time=$(date +%s.%N)
21472         $dd_cmd bs=1M count=$blocks oflag=sync ||
21473                 error "fake dd $read_write error"
21474         local duration_fake=$(bc <<< "$(date +%s.%N) - $start_time")
21475
21476         if [ "$read_write" = "write" ]; then
21477                 # verify file size
21478                 cancel_lru_locks osc
21479                 $CHECKSTAT -t file -s $((blocks * 1024 * 1024)) $DIR/$tfile ||
21480                         error "$tfile size not $blocks MB"
21481         fi
21482         do_facet ost1 $LCTL set_param fail_loc=0
21483
21484         echo "fake $read_write $duration_fake vs. normal $read_write" \
21485                 "$duration in seconds"
21486         [ $(bc <<< "$duration_fake < $duration") -eq 1 ] ||
21487                 error_not_in_vm "fake write is slower"
21488
21489         $LCTL set_param -n debug="$saved_debug"
21490         rm -f $DIR/$tfile
21491 }
21492 test_399a() { # LU-7655 for OST fake write
21493         remote_ost_nodsh && skip "remote OST with nodsh"
21494
21495         test_fake_rw write
21496 }
21497 run_test 399a "fake write should not be slower than normal write"
21498
21499 test_399b() { # LU-8726 for OST fake read
21500         remote_ost_nodsh && skip "remote OST with nodsh"
21501         if [ "$ost1_FSTYPE" != "ldiskfs" ]; then
21502                 skip_env "ldiskfs only test"
21503         fi
21504
21505         test_fake_rw read
21506 }
21507 run_test 399b "fake read should not be slower than normal read"
21508
21509 test_400a() { # LU-1606, was conf-sanity test_74
21510         if ! which $CC > /dev/null 2>&1; then
21511                 skip_env "$CC is not installed"
21512         fi
21513
21514         local extra_flags=''
21515         local out=$TMP/$tfile
21516         local prefix=/usr/include/lustre
21517         local prog
21518
21519         # Oleg removes c files in his test rig so test if any c files exist
21520         [ -z "$(ls -A $LUSTRE_TESTS_API_DIR)" ] && \
21521                 skip_env "Needed c test files are missing"
21522
21523         if ! [[ -d $prefix ]]; then
21524                 # Assume we're running in tree and fixup the include path.
21525                 extra_flags+=" -I$LUSTRE/../lnet/include/uapi -I$LUSTRE/include/uapi -I$LUSTRE/include"
21526                 extra_flags+=" -L$LUSTRE/utils/.lib"
21527         fi
21528
21529         for prog in $LUSTRE_TESTS_API_DIR/*.c; do
21530                 $CC -Wall -Werror -std=c99 $extra_flags -o $out $prog -llustreapi ||
21531                         error "client api broken"
21532         done
21533         rm -f $out
21534 }
21535 run_test 400a "Lustre client api program can compile and link"
21536
21537 test_400b() { # LU-1606, LU-5011
21538         local header
21539         local out=$TMP/$tfile
21540         local prefix=/usr/include/linux/lustre
21541
21542         # We use a hard coded prefix so that this test will not fail
21543         # when run in tree. There are headers in lustre/include/lustre/
21544         # that are not packaged (like lustre_idl.h) and have more
21545         # complicated include dependencies (like config.h and lnet/types.h).
21546         # Since this test about correct packaging we just skip them when
21547         # they don't exist (see below) rather than try to fixup cppflags.
21548
21549         if ! which $CC > /dev/null 2>&1; then
21550                 skip_env "$CC is not installed"
21551         fi
21552
21553         for header in $prefix/*.h; do
21554                 if ! [[ -f "$header" ]]; then
21555                         continue
21556                 fi
21557
21558                 if [[ "$(basename $header)" == lustre_ioctl.h ]]; then
21559                         continue # lustre_ioctl.h is internal header
21560                 fi
21561
21562                 $CC -Wall -Werror -std=c99 -include $header -c -x c /dev/null -o $out ||
21563                         error "cannot compile '$header'"
21564         done
21565         rm -f $out
21566 }
21567 run_test 400b "packaged headers can be compiled"
21568
21569 test_401a() { #LU-7437
21570         local printf_arg=$(find -printf 2>&1 | grep "unrecognized:")
21571         [ -n "$printf_arg" ] && skip_env "find does not support -printf"
21572
21573         #count the number of parameters by "list_param -R"
21574         local params=$($LCTL list_param -R '*' 2>/dev/null | wc -l)
21575         #count the number of parameters by listing proc files
21576         local proc_dirs=$(eval \ls -d $proc_regexp 2>/dev/null)
21577         echo "proc_dirs='$proc_dirs'"
21578         [ -n "$proc_dirs" ] || error "no proc_dirs on $HOSTNAME"
21579         local procs=$(find -L $proc_dirs -mindepth 1 -printf '%P\n' 2>/dev/null|
21580                       sort -u | wc -l)
21581
21582         [ $params -eq $procs ] ||
21583                 error "found $params parameters vs. $procs proc files"
21584
21585         # test the list_param -D option only returns directories
21586         params=$($LCTL list_param -R -D '*' 2>/dev/null | wc -l)
21587         #count the number of parameters by listing proc directories
21588         procs=$(find -L $proc_dirs -mindepth 1 -type d -printf '%P\n' 2>/dev/null |
21589                 sort -u | wc -l)
21590
21591         [ $params -eq $procs ] ||
21592                 error "found $params parameters vs. $procs proc files"
21593 }
21594 run_test 401a "Verify if 'lctl list_param -R' can list parameters recursively"
21595
21596 test_401b() {
21597         local save=$($LCTL get_param -n jobid_var)
21598         local tmp=testing
21599
21600         $LCTL set_param foo=bar jobid_var=$tmp bar=baz &&
21601                 error "no error returned when setting bad parameters"
21602
21603         local jobid_new=$($LCTL get_param -n foe jobid_var baz)
21604         [[ "$jobid_new" == "$tmp" ]] || error "jobid tmp $jobid_new != $tmp"
21605
21606         $LCTL set_param -n fog=bam jobid_var=$save bat=fog
21607         local jobid_old=$($LCTL get_param -n foe jobid_var bag)
21608         [[ "$jobid_old" == "$save" ]] || error "jobid new $jobid_old != $save"
21609 }
21610 run_test 401b "Verify 'lctl {get,set}_param' continue after error"
21611
21612 test_401c() {
21613         local jobid_var_old=$($LCTL get_param -n jobid_var)
21614         local jobid_var_new
21615
21616         $LCTL set_param jobid_var= &&
21617                 error "no error returned for 'set_param a='"
21618
21619         jobid_var_new=$($LCTL get_param -n jobid_var)
21620         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21621                 error "jobid_var was changed by setting without value"
21622
21623         $LCTL set_param jobid_var &&
21624                 error "no error returned for 'set_param a'"
21625
21626         jobid_var_new=$($LCTL get_param -n jobid_var)
21627         [[ "$jobid_var_old" == "$jobid_var_new" ]] ||
21628                 error "jobid_var was changed by setting without value"
21629 }
21630 run_test 401c "Verify 'lctl set_param' without value fails in either format."
21631
21632 test_401d() {
21633         local jobid_var_old=$($LCTL get_param -n jobid_var)
21634         local jobid_var_new
21635         local new_value="foo=bar"
21636
21637         $LCTL set_param jobid_var=$new_value ||
21638                 error "'set_param a=b' did not accept a value containing '='"
21639
21640         jobid_var_new=$($LCTL get_param -n jobid_var)
21641         [[ "$jobid_var_new" == "$new_value" ]] ||
21642                 error "'set_param a=b' failed on a value containing '='"
21643
21644         # Reset the jobid_var to test the other format
21645         $LCTL set_param jobid_var=$jobid_var_old
21646         jobid_var_new=$($LCTL get_param -n jobid_var)
21647         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21648                 error "failed to reset jobid_var"
21649
21650         $LCTL set_param jobid_var $new_value ||
21651                 error "'set_param a b' did not accept a value containing '='"
21652
21653         jobid_var_new=$($LCTL get_param -n jobid_var)
21654         [[ "$jobid_var_new" == "$new_value" ]] ||
21655                 error "'set_param a b' failed on a value containing '='"
21656
21657         $LCTL set_param jobid_var $jobid_var_old
21658         jobid_var_new=$($LCTL get_param -n jobid_var)
21659         [[ "$jobid_var_new" == "$jobid_var_old" ]] ||
21660                 error "failed to reset jobid_var"
21661 }
21662 run_test 401d "Verify 'lctl set_param' accepts values containing '='"
21663
21664 test_402() {
21665         [[ $MDS1_VERSION -ge $(version_code 2.7.66) ]] ||
21666         [[ $MDS1_VERSION -ge $(version_code 2.7.18.4) &&
21667                 $MDS1_VERSION -lt $(version_code 2.7.50) ]] ||
21668         [[ $MDS1_VERSION -ge $(version_code 2.7.2) &&
21669                 $MDS1_VERSION -lt $(version_code 2.7.11) ]] ||
21670                 skip "Need MDS version 2.7.2+ or 2.7.18.4+ or 2.7.66+"
21671         remote_mds_nodsh && skip "remote MDS with nodsh"
21672
21673         $LFS setdirstripe -i 0 $DIR/$tdir || error "setdirstripe -i 0 failed"
21674 #define OBD_FAIL_MDS_FLD_LOOKUP 0x15c
21675         do_facet mds1 "lctl set_param fail_loc=0x8000015c"
21676         touch $DIR/$tdir/$tfile && error "touch should fail with ENOENT" ||
21677                 echo "Touch failed - OK"
21678 }
21679 run_test 402 "Return ENOENT to lod_generate_and_set_lovea"
21680
21681 test_403() {
21682         local file1=$DIR/$tfile.1
21683         local file2=$DIR/$tfile.2
21684         local tfile=$TMP/$tfile
21685
21686         rm -f $file1 $file2 $tfile
21687
21688         touch $file1
21689         ln $file1 $file2
21690
21691         # 30 sec OBD_TIMEOUT in ll_getattr()
21692         # right before populating st_nlink
21693         $LCTL set_param fail_loc=0x80001409
21694         stat -c %h $file1 > $tfile &
21695
21696         # create an alias, drop all locks and reclaim the dentry
21697         < $file2
21698         cancel_lru_locks mdc
21699         cancel_lru_locks osc
21700         sysctl -w vm.drop_caches=2
21701
21702         wait
21703
21704         [ $(cat $tfile) -gt 0 ] || error "wrong nlink count: $(cat $tfile)"
21705
21706         rm -f $tfile $file1 $file2
21707 }
21708 run_test 403 "i_nlink should not drop to zero due to aliasing"
21709
21710 test_404() { # LU-6601
21711         [[ $MDS1_VERSION -ge $(version_code 2.8.53) ]] ||
21712                 skip "Need server version newer than 2.8.52"
21713         remote_mds_nodsh && skip "remote MDS with nodsh"
21714
21715         local mosps=$(do_facet $SINGLEMDS $LCTL dl |
21716                 awk '/osp .*-osc-MDT/ { print $4}')
21717
21718         local osp
21719         for osp in $mosps; do
21720                 echo "Deactivate: " $osp
21721                 do_facet $SINGLEMDS $LCTL --device %$osp deactivate
21722                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21723                         awk -vp=$osp '$4 == p { print $2 }')
21724                 [ $stat = IN ] || {
21725                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21726                         error "deactivate error"
21727                 }
21728                 echo "Activate: " $osp
21729                 do_facet $SINGLEMDS $LCTL --device %$osp activate
21730                 local stat=$(do_facet $SINGLEMDS $LCTL dl |
21731                         awk -vp=$osp '$4 == p { print $2 }')
21732                 [ $stat = UP ] || {
21733                         do_facet $SINGLEMDS $LCTL dl | grep -w $osp
21734                         error "activate error"
21735                 }
21736         done
21737 }
21738 run_test 404 "validate manual {de}activated works properly for OSPs"
21739
21740 test_405() {
21741         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
21742         [ $MDS1_VERSION -lt $(version_code 2.6.92) ] ||
21743                 [ $CLIENT_VERSION -lt $(version_code 2.6.99) ] &&
21744                         skip "Layout swap lock is not supported"
21745
21746         check_swap_layouts_support
21747
21748         test_mkdir $DIR/$tdir
21749         swap_lock_test -d $DIR/$tdir ||
21750                 error "One layout swap locked test failed"
21751 }
21752 run_test 405 "Various layout swap lock tests"
21753
21754 test_406() {
21755         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21756         [ $OSTCOUNT -lt 2 ] && skip_env "needs >= 2 OSTs"
21757         [ -n "$FILESET" ] && skip "SKIP due to FILESET set"
21758         [ $PARALLEL == "yes" ] && skip "skip parallel run"
21759         [ $MDS1_VERSION -lt $(version_code 2.8.50) ] &&
21760                 skip "Need MDS version at least 2.8.50"
21761
21762         local def_stripe_size=$($LFS getstripe -S $MOUNT)
21763         local test_pool=$TESTNAME
21764
21765         pool_add $test_pool || error "pool_add failed"
21766         pool_add_targets $test_pool 0 $(($OSTCOUNT - 1)) 1 ||
21767                 error "pool_add_targets failed"
21768
21769         save_layout_restore_at_exit $MOUNT
21770
21771         # parent set default stripe count only, child will stripe from both
21772         # parent and fs default
21773         $LFS setstripe -c 1 -i 1 -S $((def_stripe_size * 2)) -p $test_pool $MOUNT ||
21774                 error "setstripe $MOUNT failed"
21775         $LFS mkdir -c $MDSCOUNT $DIR/$tdir || error "mkdir $tdir failed"
21776         $LFS setstripe -c $OSTCOUNT $DIR/$tdir || error "setstripe $tdir failed"
21777         for i in $(seq 10); do
21778                 local f=$DIR/$tdir/$tfile.$i
21779                 touch $f || error "touch failed"
21780                 local count=$($LFS getstripe -c $f)
21781                 [ $count -eq $OSTCOUNT ] ||
21782                         error "$f stripe count $count != $OSTCOUNT"
21783                 local offset=$($LFS getstripe -i $f)
21784                 [ $offset -eq 1 ] || error "$f stripe offset $offset != 1"
21785                 local size=$($LFS getstripe -S $f)
21786                 [ $size -eq $((def_stripe_size * 2)) ] ||
21787                         error "$f stripe size $size != $((def_stripe_size * 2))"
21788                 local pool=$($LFS getstripe -p $f)
21789                 [ $pool == $test_pool ] || error "$f pool $pool != $test_pool"
21790         done
21791
21792         # change fs default striping, delete parent default striping, now child
21793         # will stripe from new fs default striping only
21794         $LFS setstripe -c 1 -S $def_stripe_size -i 0 $MOUNT ||
21795                 error "change $MOUNT default stripe failed"
21796         $LFS setstripe -c 0 $DIR/$tdir ||
21797                 error "delete $tdir default stripe failed"
21798         for i in $(seq 11 20); do
21799                 local f=$DIR/$tdir/$tfile.$i
21800                 touch $f || error "touch $f failed"
21801                 local count=$($LFS getstripe -c $f)
21802                 [ $count -eq 1 ] || error "$f stripe count $count != 1"
21803                 local offset=$($LFS getstripe -i $f)
21804                 [ $offset -eq 0 ] || error "$f stripe offset $offset != 0"
21805                 local size=$($LFS getstripe -S $f)
21806                 [ $size -eq $def_stripe_size ] ||
21807                         error "$f stripe size $size != $def_stripe_size"
21808                 local pool=$($LFS getstripe -p $f)
21809                 [ $pool == $test_pool ] || error "$f pool $pool isn't set"
21810         done
21811
21812         unlinkmany $DIR/$tdir/$tfile. 1 20
21813
21814         local f=$DIR/$tdir/$tfile
21815         pool_remove_all_targets $test_pool $f
21816         pool_remove $test_pool $f
21817 }
21818 run_test 406 "DNE support fs default striping"
21819
21820 test_407() {
21821         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21822         [[ $MDS1_VERSION -lt $(version_code 2.8.55) ]] &&
21823                 skip "Need MDS version at least 2.8.55"
21824         remote_mds_nodsh && skip "remote MDS with nodsh"
21825
21826         $LFS mkdir -i 0 -c 1 $DIR/$tdir.0 ||
21827                 error "$LFS mkdir -i 0 -c 1 $tdir.0 failed"
21828         $LFS mkdir -i 1 -c 1 $DIR/$tdir.1 ||
21829                 error "$LFS mkdir -i 1 -c 1 $tdir.1 failed"
21830         touch $DIR/$tdir.0/$tfile.0 || error "touch $tdir.0/$tfile.0 failed"
21831
21832         #define OBD_FAIL_DT_TXN_STOP    0x2019
21833         for idx in $(seq $MDSCOUNT); do
21834                 do_facet mds$idx "lctl set_param fail_loc=0x2019"
21835         done
21836         $LFS mkdir -c 2 $DIR/$tdir && error "$LFS mkdir -c 2 $tdir should fail"
21837         mv $DIR/$tdir.0/$tfile.0 $DIR/$tdir.1/$tfile.1 &&
21838                 error "mv $tdir.0/$tfile.0 $tdir.1/$tfile.1 should fail"
21839         true
21840 }
21841 run_test 407 "transaction fail should cause operation fail"
21842
21843 test_408() {
21844         dd if=/dev/zero of=$DIR/$tfile bs=$PAGE_SIZE count=1 oflag=direct
21845
21846         #define OBD_FAIL_OSC_BRW_PREP_REQ2        0x40a
21847         lctl set_param fail_loc=0x8000040a
21848         # let ll_prepare_partial_page() fail
21849         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 conv=notrunc || true
21850
21851         rm -f $DIR/$tfile
21852
21853         # create at least 100 unused inodes so that
21854         # shrink_icache_memory(0) should not return 0
21855         touch $DIR/$tfile-{0..100}
21856         rm -f $DIR/$tfile-{0..100}
21857         sync
21858
21859         echo 2 > /proc/sys/vm/drop_caches
21860 }
21861 run_test 408 "drop_caches should not hang due to page leaks"
21862
21863 test_409()
21864 {
21865         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
21866
21867         mkdir -p $DIR/$tdir || error "(0) Fail to mkdir"
21868         $LFS mkdir -i 1 -c 2 $DIR/$tdir/foo || error "(1) Fail to mkdir"
21869         touch $DIR/$tdir/guard || error "(2) Fail to create"
21870
21871         local PREFIX=$(str_repeat 'A' 128)
21872         echo "Create 1K hard links start at $(date)"
21873         createmany -l $DIR/$tdir/guard $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21874                 error "(3) Fail to hard link"
21875
21876         echo "Links count should be right although linkEA overflow"
21877         stat $DIR/$tdir/guard || error "(4) Fail to stat"
21878         local linkcount=$(stat --format=%h $DIR/$tdir/guard)
21879         [ $linkcount -eq 1001 ] ||
21880                 error "(5) Unexpected hard links count: $linkcount"
21881
21882         echo "List all links start at $(date)"
21883         ls -l $DIR/$tdir/foo > /dev/null ||
21884                 error "(6) Fail to list $DIR/$tdir/foo"
21885
21886         echo "Unlink hard links start at $(date)"
21887         unlinkmany $DIR/$tdir/foo/${PREFIX}_ 1000 ||
21888                 error "(7) Fail to unlink"
21889         echo "Unlink hard links finished at $(date)"
21890 }
21891 run_test 409 "Large amount of cross-MDTs hard links on the same file"
21892
21893 test_410()
21894 {
21895         [[ $CLIENT_VERSION -lt $(version_code 2.9.59) ]] &&
21896                 skip "Need client version at least 2.9.59"
21897
21898         # Create a file, and stat it from the kernel
21899         local testfile=$DIR/$tfile
21900         touch $testfile
21901
21902         local run_id=$RANDOM
21903         local my_ino=$(stat --format "%i" $testfile)
21904
21905         # Try to insert the module. This will always fail as the
21906         # module is designed to not be inserted.
21907         insmod $LUSTRE/tests/kernel/kinode.ko run_id=$run_id fname=$testfile \
21908             &> /dev/null
21909
21910         # Anything but success is a test failure
21911         dmesg | grep -q \
21912             "lustre_kinode_$run_id: inode numbers are identical: $my_ino" ||
21913             error "no inode match"
21914 }
21915 run_test 410 "Test inode number returned from kernel thread"
21916
21917 cleanup_test411_cgroup() {
21918         trap 0
21919         rmdir "$1"
21920 }
21921
21922 test_411() {
21923         local cg_basedir=/sys/fs/cgroup/memory
21924         # LU-9966
21925         test -f "$cg_basedir/memory.kmem.limit_in_bytes" ||
21926                 skip "no setup for cgroup"
21927
21928         dd if=/dev/zero of=$DIR/$tfile bs=1M count=100 conv=fsync ||
21929                 error "test file creation failed"
21930         cancel_lru_locks osc
21931
21932         # Create a very small memory cgroup to force a slab allocation error
21933         local cgdir=$cg_basedir/osc_slab_alloc
21934         mkdir $cgdir || error "cgroup mkdir '$cgdir' failed"
21935         trap "cleanup_test411_cgroup $cgdir" EXIT
21936         echo 2M > $cgdir/memory.kmem.limit_in_bytes
21937         echo 1M > $cgdir/memory.limit_in_bytes
21938
21939         # Should not LBUG, just be killed by oom-killer
21940         # dd will return 0 even allocation failure in some environment.
21941         # So don't check return value
21942         sh -c "echo \$$ > $cgdir/tasks && dd if=$DIR/$tfile of=/dev/null"
21943         cleanup_test411_cgroup $cgdir
21944
21945         return 0
21946 }
21947 run_test 411 "Slab allocation error with cgroup does not LBUG"
21948
21949 test_412() {
21950         [ $MDSCOUNT -lt 2 ] && skip_env "needs >= 2 MDTs"
21951         if [ $MDS1_VERSION -lt $(version_code 2.10.55) ]; then
21952                 skip "Need server version at least 2.10.55"
21953         fi
21954
21955         $LFS mkdir -i $((MDSCOUNT - 1)),$((MDSCOUNT - 2)) $DIR/$tdir ||
21956                 error "mkdir failed"
21957         $LFS getdirstripe $DIR/$tdir
21958         local stripe_index=$($LFS getdirstripe -i $DIR/$tdir)
21959         [ $stripe_index -eq $((MDSCOUNT - 1)) ] ||
21960                 error "expect $((MDSCOUT - 1)) get $stripe_index"
21961         local stripe_count=$($LFS getdirstripe -T $DIR/$tdir)
21962         [ $stripe_count -eq 2 ] ||
21963                 error "expect 2 get $stripe_count"
21964 }
21965 run_test 412 "mkdir on specific MDTs"
21966
21967 test_qos_mkdir() {
21968         local mkdir_cmd=$1
21969         local stripe_count=$2
21970         local mdts=$(comma_list $(mdts_nodes))
21971
21972         local testdir
21973         local lmv_qos_prio_free
21974         local lmv_qos_threshold_rr
21975         local lmv_qos_maxage
21976         local lod_qos_prio_free
21977         local lod_qos_threshold_rr
21978         local lod_qos_maxage
21979         local count
21980         local i
21981
21982         lmv_qos_prio_free=$($LCTL get_param -n lmv.*.qos_prio_free | head -n1)
21983         lmv_qos_prio_free=${lmv_qos_prio_free%%%}
21984         lmv_qos_threshold_rr=$($LCTL get_param -n lmv.*.qos_threshold_rr |
21985                 head -n1)
21986         lmv_qos_threshold_rr=${lmv_qos_threshold_rr%%%}
21987         lmv_qos_maxage=$($LCTL get_param -n lmv.*.qos_maxage)
21988         stack_trap "$LCTL set_param \
21989                 lmv.*.qos_prio_free=$lmv_qos_prio_free > /dev/null" EXIT
21990         stack_trap "$LCTL set_param \
21991                 lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null" EXIT
21992         stack_trap "$LCTL set_param \
21993                 lmv.*.qos_maxage=$lmv_qos_maxage > /dev/null" EXIT
21994
21995         lod_qos_prio_free=$(do_facet mds1 $LCTL get_param -n \
21996                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_prio_free | head -n1)
21997         lod_qos_prio_free=${lod_qos_prio_free%%%}
21998         lod_qos_threshold_rr=$(do_facet mds1 $LCTL get_param -n \
21999                 lod.$FSNAME-MDT0000-mdtlov.mdt_qos_threshold_rr | head -n1)
22000         lod_qos_threshold_rr=${lod_qos_threshold_rr%%%}
22001         lod_qos_maxage=$(do_facet mds1 $LCTL get_param -n \
22002                 lod.$FSNAME-MDT0000-mdtlov.qos_maxage | awk '{ print $1 }')
22003         stack_trap "do_nodes $mdts $LCTL set_param \
22004                 lod.*.mdt_qos_prio_free=$lod_qos_prio_free > /dev/null" EXIT
22005         stack_trap "do_nodes $mdts $LCTL set_param \
22006                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null" \
22007                 EXIT
22008         stack_trap "do_nodes $mdts $LCTL set_param \
22009                 lod.*.mdt_qos_maxage=$lod_qos_maxage > /dev/null" EXIT
22010
22011         echo
22012         echo "Mkdir (stripe_count $stripe_count) roundrobin:"
22013
22014         $LCTL set_param lmv.*.qos_threshold_rr=100 > /dev/null
22015         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_threshold_rr=100 > /dev/null
22016
22017         testdir=$DIR/$tdir-s$stripe_count/rr
22018
22019         for i in $(seq $((100 * MDSCOUNT))); do
22020                 eval $mkdir_cmd $testdir/subdir$i ||
22021                         error "$mkdir_cmd subdir$i failed"
22022         done
22023
22024         for i in $(seq $MDSCOUNT); do
22025                 count=$($LFS getdirstripe -i $testdir/* |
22026                                 grep ^$((i - 1))$ | wc -l)
22027                 echo "$count directories created on MDT$((i - 1))"
22028                 [ $count -eq 100 ] || error "subdirs are not evenly distributed"
22029
22030                 if [ $stripe_count -gt 1 ]; then
22031                         count=$($LFS getdirstripe $testdir/* |
22032                                 grep -P "^\s+$((i - 1))\t" | wc -l)
22033                         echo "$count stripes created on MDT$((i - 1))"
22034                         # deviation should < 5% of average
22035                         [ $count -lt $((95 * stripe_count)) ] ||
22036                         [ $count -gt $((105 * stripe_count)) ] &&
22037                                 error "stripes are not evenly distributed"
22038                 fi
22039         done
22040
22041         $LCTL set_param lmv.*.qos_threshold_rr=$lmv_qos_threshold_rr > /dev/null
22042         do_nodes $mdts $LCTL set_param \
22043                 lod.*.mdt_qos_threshold_rr=$lod_qos_threshold_rr > /dev/null
22044
22045         echo
22046         echo "Check for uneven MDTs: "
22047
22048         local ffree
22049         local bavail
22050         local max
22051         local min
22052         local max_index
22053         local min_index
22054         local tmp
22055
22056         ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.filesfree))
22057         bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-[^M]*.kbytesavail))
22058         bsize=$(lctl get_param -n mdc.*MDT0000*.blocksize)
22059
22060         max=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
22061         min=$(((${ffree[0]} >> 8) * (${bavail[0]} * bsize >> 16)))
22062         max_index=0
22063         min_index=0
22064         for ((i = 1; i < ${#ffree[@]}; i++)); do
22065                 tmp=$(((${ffree[i]} >> 8) * (${bavail[i]} * bsize >> 16)))
22066                 if [ $tmp -gt $max ]; then
22067                         max=$tmp
22068                         max_index=$i
22069                 fi
22070                 if [ $tmp -lt $min ]; then
22071                         min=$tmp
22072                         min_index=$i
22073                 fi
22074         done
22075
22076         [ ${ffree[min_index]} -eq 0 ] &&
22077                 skip "no free files in MDT$min_index"
22078         [ ${ffree[min_index]} -gt 100000000 ] &&
22079                 skip "too much free files in MDT$min_index"
22080
22081         # Check if we need to generate uneven MDTs
22082         local threshold=50
22083         local diff=$(((max - min) * 100 / min))
22084         local value="$(generate_string 1024)"
22085
22086         while [ $diff -lt $threshold ]; do
22087                 # generate uneven MDTs, create till $threshold% diff
22088                 echo -n "weight diff=$diff% must be > $threshold% ..."
22089                 count=$((${ffree[min_index]} / 10))
22090                 # 50 sec per 10000 files in vm
22091                 [ $count -gt 40000 ] && [ "$SLOW" = "no" ] &&
22092                         skip "$count files to create"
22093                 echo "Fill MDT$min_index with $count files"
22094                 [ -d $DIR/$tdir-MDT$min_index ] ||
22095                         $LFS mkdir -i $min_index $DIR/$tdir-MDT$min_index ||
22096                         error "mkdir $tdir-MDT$min_index failed"
22097                 for i in $(seq $count); do
22098                         $OPENFILE -f O_CREAT:O_LOV_DELAY_CREATE \
22099                                 $DIR/$tdir-MDT$min_index/f$j_$i > /dev/null ||
22100                                 error "create f$j_$i failed"
22101                         setfattr -n user.413b -v $value \
22102                                 $DIR/$tdir-MDT$min_index/f$j_$i ||
22103                                 error "setfattr f$j_$i failed"
22104                 done
22105
22106                 ffree=($(lctl get_param -n mdc.*[mM][dD][cC]-*.filesfree))
22107                 bavail=($(lctl get_param -n mdc.*[mM][dD][cC]-*.kbytesavail))
22108                 max=$(((${ffree[max_index]} >> 8) * \
22109                         (${bavail[max_index]} * bsize >> 16)))
22110                 min=$(((${ffree[min_index]} >> 8) * \
22111                         (${bavail[min_index]} * bsize >> 16)))
22112                 diff=$(((max - min) * 100 / min))
22113         done
22114
22115         echo "MDT filesfree available: ${ffree[@]}"
22116         echo "MDT blocks available: ${bavail[@]}"
22117         echo "weight diff=$diff%"
22118
22119         echo
22120         echo "Mkdir (stripe_count $stripe_count) with balanced space usage:"
22121
22122         $LCTL set_param lmv.*.qos_prio_free=100 > /dev/null
22123         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_prio_free=100 > /dev/null
22124         # decrease statfs age, so that it can be updated in time
22125         $LCTL set_param lmv.*.qos_maxage=1 > /dev/null
22126         do_nodes $mdts $LCTL set_param lod.*.mdt_qos_maxage=1 > /dev/null
22127
22128         sleep 1
22129
22130         testdir=$DIR/$tdir-s$stripe_count/qos
22131
22132         for i in $(seq $((100 * MDSCOUNT))); do
22133                 eval $mkdir_cmd $testdir/subdir$i ||
22134                         error "$mkdir_cmd subdir$i failed"
22135         done
22136
22137         for i in $(seq $MDSCOUNT); do
22138                 count=$($LFS getdirstripe -i $testdir/* | grep ^$((i - 1))$ |
22139                         wc -l)
22140                 echo "$count directories created on MDT$((i - 1))"
22141
22142                 if [ $stripe_count -gt 1 ]; then
22143                         count=$($LFS getdirstripe $testdir/* |
22144                                 grep -P "^\s+$((i - 1))\t" | wc -l)
22145                         echo "$count stripes created on MDT$((i - 1))"
22146                 fi
22147         done
22148
22149         max=$($LFS getdirstripe -i $testdir/* | grep ^$max_index$ | wc -l)
22150         min=$($LFS getdirstripe -i $testdir/* | grep ^$min_index$ | wc -l)
22151
22152         # D-value should > 10% of averge
22153         [ $((max - min)) -lt 10 ] &&
22154                 error "subdirs shouldn't be evenly distributed"
22155
22156         # ditto
22157         if [ $stripe_count -gt 1 ]; then
22158                 max=$($LFS getdirstripe $testdir/* |
22159                         grep -P "^\s+$max_index\t" | wc -l)
22160                 min=$($LFS getdirstripe $testdir/* |
22161                         grep -P "^\s+$min_index\t" | wc -l)
22162                 [ $((max - min)) -le $((10 * stripe_count)) ] &&
22163                         error "stripes shouldn't be evenly distributed"|| true
22164         fi
22165 }
22166
22167 test_413a() {
22168         [ $MDSCOUNT -lt 2 ] &&
22169                 skip "We need at least 2 MDTs for this test"
22170
22171         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
22172                 skip "Need server version at least 2.12.52"
22173
22174         local stripe_count
22175
22176         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
22177                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
22178                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
22179                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
22180                 test_qos_mkdir "$LFS mkdir -c $stripe_count" $stripe_count
22181         done
22182 }
22183 run_test 413a "QoS mkdir with 'lfs mkdir -i -1'"
22184
22185 test_413b() {
22186         [ $MDSCOUNT -lt 2 ] &&
22187                 skip "We need at least 2 MDTs for this test"
22188
22189         [ $MDS1_VERSION -lt $(version_code 2.12.52) ] &&
22190                 skip "Need server version at least 2.12.52"
22191
22192         local stripe_count
22193
22194         for stripe_count in $(seq 1 $((MDSCOUNT - 1))); do
22195                 mkdir $DIR/$tdir-s$stripe_count || error "mkdir failed"
22196                 mkdir $DIR/$tdir-s$stripe_count/rr || error "mkdir failed"
22197                 mkdir $DIR/$tdir-s$stripe_count/qos || error "mkdir failed"
22198                 $LFS setdirstripe -D -c $stripe_count \
22199                         $DIR/$tdir-s$stripe_count/rr ||
22200                         error "setdirstripe failed"
22201                 $LFS setdirstripe -D -c $stripe_count \
22202                         $DIR/$tdir-s$stripe_count/qos ||
22203                         error "setdirstripe failed"
22204                 test_qos_mkdir "mkdir" $stripe_count
22205         done
22206 }
22207 run_test 413b "QoS mkdir under dir whose default LMV starting MDT offset is -1"
22208
22209 test_414() {
22210 #define OBD_FAIL_PTLRPC_BULK_ATTACH      0x521
22211         $LCTL set_param fail_loc=0x80000521
22212         dd if=/dev/zero of=$DIR/$tfile bs=2M count=1 oflag=sync
22213         rm -f $DIR/$tfile
22214 }
22215 run_test 414 "simulate ENOMEM in ptlrpc_register_bulk()"
22216
22217 test_415() {
22218         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22219         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
22220                 skip "Need server version at least 2.11.52"
22221
22222         # LU-11102
22223         local total
22224         local setattr_pid
22225         local start_time
22226         local end_time
22227         local duration
22228
22229         total=500
22230         # this test may be slow on ZFS
22231         [ "$mds1_FSTYPE" == "zfs" ] && total=100
22232
22233         # though this test is designed for striped directory, let's test normal
22234         # directory too since lock is always saved as CoS lock.
22235         test_mkdir $DIR/$tdir || error "mkdir $tdir"
22236         createmany -o $DIR/$tdir/$tfile. $total || error "createmany"
22237
22238         (
22239                 while true; do
22240                         touch $DIR/$tdir
22241                 done
22242         ) &
22243         setattr_pid=$!
22244
22245         start_time=$(date +%s)
22246         for i in $(seq $total); do
22247                 mrename $DIR/$tdir/$tfile.$i $DIR/$tdir/$tfile-new.$i \
22248                         > /dev/null
22249         done
22250         end_time=$(date +%s)
22251         duration=$((end_time - start_time))
22252
22253         kill -9 $setattr_pid
22254
22255         echo "rename $total files took $duration sec"
22256         [ $duration -lt 100 ] || error "rename took $duration sec"
22257 }
22258 run_test 415 "lock revoke is not missing"
22259
22260 test_416() {
22261         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
22262                 skip "Need server version at least 2.11.55"
22263
22264         # define OBD_FAIL_OSD_TXN_START    0x19a
22265         do_facet mds1 lctl set_param fail_loc=0x19a
22266
22267         lfs mkdir -c $MDSCOUNT $DIR/$tdir
22268
22269         true
22270 }
22271 run_test 416 "transaction start failure won't cause system hung"
22272
22273 cleanup_417() {
22274         trap 0
22275         do_nodes $(comma_list $(mdts_nodes)) \
22276                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=1"
22277         do_nodes $(comma_list $(mdts_nodes)) \
22278                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=1"
22279         do_nodes $(comma_list $(mdts_nodes)) \
22280                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=1"
22281 }
22282
22283 test_417() {
22284         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22285         [[ $MDS1_VERSION -lt $(version_code 2.11.56) ]] &&
22286                 skip "Need MDS version at least 2.11.56"
22287
22288         trap cleanup_417 RETURN EXIT
22289
22290         $LFS mkdir -i 1 $DIR/$tdir.1 || error "create remote dir $tdir.1 failed"
22291         do_nodes $(comma_list $(mdts_nodes)) \
22292                 "$LCTL set_param -n mdt.*MDT*.enable_dir_migration=0"
22293         $LFS migrate -m 0 $DIR/$tdir.1 &&
22294                 error "migrate dir $tdir.1 should fail"
22295
22296         do_nodes $(comma_list $(mdts_nodes)) \
22297                 "$LCTL set_param -n mdt.*MDT*.enable_remote_dir=0"
22298         $LFS mkdir -i 1 $DIR/$tdir.2 &&
22299                 error "create remote dir $tdir.2 should fail"
22300
22301         do_nodes $(comma_list $(mdts_nodes)) \
22302                 "$LCTL set_param -n mdt.*MDT*.enable_striped_dir=0"
22303         $LFS mkdir -c 2 $DIR/$tdir.3 &&
22304                 error "create striped dir $tdir.3 should fail"
22305         true
22306 }
22307 run_test 417 "disable remote dir, striped dir and dir migration"
22308
22309 # Checks that the outputs of df [-i] and lfs df [-i] match
22310 #
22311 # usage: check_lfs_df <blocks | inodes> <mountpoint>
22312 check_lfs_df() {
22313         local dir=$2
22314         local inodes
22315         local df_out
22316         local lfs_df_out
22317         local count
22318         local passed=false
22319
22320         # blocks or inodes
22321         [ "$1" == "blocks" ] && inodes= || inodes="-i"
22322
22323         for count in {1..100}; do
22324                 cancel_lru_locks
22325                 sync; sleep 0.2
22326
22327                 # read the lines of interest
22328                 df_out=($(df -P $inodes $dir | tail -n +2)) ||
22329                         error "df $inodes $dir | tail -n +2 failed"
22330                 lfs_df_out=($($LFS df $inodes $dir | grep summary:)) ||
22331                         error "lfs df $inodes $dir | grep summary: failed"
22332
22333                 # skip first substrings of each output as they are different
22334                 # "<NID>:/<fsname>" for df, "filesystem_summary:" for lfs df
22335                 # compare the two outputs
22336                 passed=true
22337                 for i in {1..5}; do
22338                         [ "${df_out[i]}" != "${lfs_df_out[i]}" ] && passed=false
22339                 done
22340                 $passed && break
22341         done
22342
22343         if ! $passed; then
22344                 df -P $inodes $dir
22345                 echo
22346                 lfs df $inodes $dir
22347                 error "df and lfs df $1 output mismatch: "      \
22348                       "df ${inodes}: ${df_out[*]}, "            \
22349                       "lfs df ${inodes}: ${lfs_df_out[*]}"
22350         fi
22351 }
22352
22353 test_418() {
22354         [ $PARALLEL == "yes" ] && skip "skip parallel run"
22355
22356         local dir=$DIR/$tdir
22357         local numfiles=$((RANDOM % 4096 + 2))
22358         local numblocks=$((RANDOM % 256 + 1))
22359
22360         wait_delete_completed
22361         test_mkdir $dir
22362
22363         # check block output
22364         check_lfs_df blocks $dir
22365         # check inode output
22366         check_lfs_df inodes $dir
22367
22368         # create a single file and retest
22369         echo "Creating a single file and testing"
22370         createmany -o $dir/$tfile- 1 &>/dev/null ||
22371                 error "creating 1 file in $dir failed"
22372         check_lfs_df blocks $dir
22373         check_lfs_df inodes $dir
22374
22375         # create a random number of files
22376         echo "Creating $((numfiles - 1)) files and testing"
22377         createmany -o $dir/$tfile- 1 $((numfiles - 1)) &>/dev/null ||
22378                 error "creating $((numfiles - 1)) files in $dir failed"
22379
22380         # write a random number of blocks to the first test file
22381         echo "Writing $numblocks 4K blocks and testing"
22382         dd if=/dev/urandom of=$dir/${tfile}-0 bs=4K conv=fsync \
22383                 count=$numblocks &>/dev/null ||
22384                 error "dd to $dir/${tfile}-0 failed"
22385
22386         # retest
22387         check_lfs_df blocks $dir
22388         check_lfs_df inodes $dir
22389
22390         unlinkmany $dir/$tfile- $numfiles &>/dev/null ||
22391                 error "unlinking $numfiles files in $dir failed"
22392 }
22393 run_test 418 "df and lfs df outputs match"
22394
22395 test_419()
22396 {
22397         local dir=$DIR/$tdir
22398
22399         mkdir -p $dir
22400         touch $dir/file
22401
22402         cancel_lru_locks mdc
22403
22404         #OBD_FAIL_LLITE_OPEN_BY_NAME    0x1410
22405         $LCTL set_param fail_loc=0x1410
22406         cat $dir/file
22407         $LCTL set_param fail_loc=0
22408         rm -rf $dir
22409 }
22410 run_test 419 "Verify open file by name doesn't crash kernel"
22411
22412 test_420()
22413 {
22414         [[ $MDS1_VERSION -ge $(version_code 2.12.53) ]] ||
22415                 skip "Need MDS version at least 2.12.53"
22416
22417         local SAVE_UMASK=$(umask)
22418         local dir=$DIR/$tdir
22419         local uname=$(getent passwd $RUNAS_ID | cut -d: -f1)
22420
22421         mkdir -p $dir
22422         umask 0000
22423         mkdir -m03777 $dir/testdir
22424         ls -dn $dir/testdir
22425         # Need to remove trailing '.' when SELinux is enabled
22426         local dirperms=$(ls -dn $dir/testdir |
22427                          awk '{ sub(/\.$/, "", $1); print $1}')
22428         [ $dirperms == "drwxrwsrwt" ] ||
22429                 error "incorrect perms on $dir/testdir"
22430
22431         su - $uname -c "PATH=$LUSTRE/tests:\$PATH; \
22432                 openfile -f O_RDONLY:O_CREAT -m 02755 $dir/testdir/testfile"
22433         ls -n $dir/testdir/testfile
22434         local fileperms=$(ls -n $dir/testdir/testfile |
22435                           awk '{ sub(/\.$/, "", $1); print $1}')
22436         [ $fileperms == "-rwxr-xr-x" ] ||
22437                 error "incorrect perms on $dir/testdir/testfile"
22438
22439         umask $SAVE_UMASK
22440 }
22441 run_test 420 "clear SGID bit on non-directories for non-members"
22442
22443 test_421a() {
22444         local cnt
22445         local fid1
22446         local fid2
22447
22448         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22449                 skip "Need MDS version at least 2.12.54"
22450
22451         test_mkdir $DIR/$tdir
22452         createmany -o $DIR/$tdir/f 3
22453         cnt=$(ls -1 $DIR/$tdir | wc -l)
22454         [ $cnt != 3 ] && error "unexpected #files: $cnt"
22455
22456         fid1=$(lfs path2fid $DIR/$tdir/f1)
22457         fid2=$(lfs path2fid $DIR/$tdir/f2)
22458         $LFS rmfid $DIR $fid1 $fid2 || error "rmfid failed"
22459
22460         stat $DIR/$tdir/f1 && error "f1 still visible on the client"
22461         stat $DIR/$tdir/f2 && error "f2 still visible on the client"
22462
22463         cnt=$(ls -1 $DIR/$tdir | wc -l)
22464         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
22465
22466         rm -f $DIR/$tdir/f3 || error "can't remove f3"
22467         createmany -o $DIR/$tdir/f 3
22468         cnt=$(ls -1 $DIR/$tdir | wc -l)
22469         [ $cnt != 3 ] && error "unexpected #files: $cnt"
22470
22471         fid1=$(lfs path2fid $DIR/$tdir/f1)
22472         fid2=$(lfs path2fid $DIR/$tdir/f2)
22473         echo "remove using fsname $FSNAME"
22474         $LFS rmfid $FSNAME $fid1 $fid2 || error "rmfid with fsname failed"
22475
22476         cnt=$(ls -1 $DIR/$tdir | wc -l)
22477         [ $cnt == 1 ] || error "unexpected #files after: $cnt"
22478 }
22479 run_test 421a "simple rm by fid"
22480
22481 test_421b() {
22482         local cnt
22483         local FID1
22484         local FID2
22485
22486         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22487                 skip "Need MDS version at least 2.12.54"
22488
22489         test_mkdir $DIR/$tdir
22490         createmany -o $DIR/$tdir/f 3
22491         multiop_bg_pause $DIR/$tdir/f1 o_c || error "multiop failed to start"
22492         MULTIPID=$!
22493
22494         FID1=$(lfs path2fid $DIR/$tdir/f1)
22495         FID2=$(lfs path2fid $DIR/$tdir/f2)
22496         $LFS rmfid $DIR $FID1 $FID2 && error "rmfid didn't fail"
22497
22498         kill -USR1 $MULTIPID
22499         wait
22500
22501         cnt=$(ls $DIR/$tdir | wc -l)
22502         [ $cnt == 2 ] || error "unexpected #files after: $cnt"
22503 }
22504 run_test 421b "rm by fid on open file"
22505
22506 test_421c() {
22507         local cnt
22508         local FIDS
22509
22510         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22511                 skip "Need MDS version at least 2.12.54"
22512
22513         test_mkdir $DIR/$tdir
22514         createmany -o $DIR/$tdir/f 3
22515         touch $DIR/$tdir/$tfile
22516         createmany -l$DIR/$tdir/$tfile $DIR/$tdir/h 180
22517         cnt=$(ls -1 $DIR/$tdir | wc -l)
22518         [ $cnt != 184 ] && error "unexpected #files: $cnt"
22519
22520         FID1=$(lfs path2fid $DIR/$tdir/$tfile)
22521         $LFS rmfid $DIR $FID1 || error "rmfid failed"
22522
22523         cnt=$(ls $DIR/$tdir | wc -l)
22524         [ $cnt == 3 ] || error "unexpected #files after: $cnt"
22525 }
22526 run_test 421c "rm by fid against hardlinked files"
22527
22528 test_421d() {
22529         local cnt
22530         local FIDS
22531
22532         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22533                 skip "Need MDS version at least 2.12.54"
22534
22535         test_mkdir $DIR/$tdir
22536         createmany -o $DIR/$tdir/f 4097
22537         cnt=$(ls -1 $DIR/$tdir | wc -l)
22538         [ $cnt != 4097 ] && error "unexpected #files: $cnt"
22539
22540         FIDS=$(lfs path2fid $DIR/$tdir/f* | sed "s/[/][^:]*://g")
22541         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22542
22543         cnt=$(ls $DIR/$tdir | wc -l)
22544         rm -rf $DIR/$tdir
22545         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22546 }
22547 run_test 421d "rmfid en masse"
22548
22549 test_421e() {
22550         local cnt
22551         local FID
22552
22553         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22554         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22555                 skip "Need MDS version at least 2.12.54"
22556
22557         mkdir -p $DIR/$tdir
22558         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22559         createmany -o $DIR/$tdir/striped_dir/f 512
22560         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22561         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22562
22563         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22564                 sed "s/[/][^:]*://g")
22565         $LFS rmfid $DIR $FIDS || error "rmfid failed"
22566
22567         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22568         rm -rf $DIR/$tdir
22569         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22570 }
22571 run_test 421e "rmfid in DNE"
22572
22573 test_421f() {
22574         local cnt
22575         local FID
22576
22577         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22578                 skip "Need MDS version at least 2.12.54"
22579
22580         test_mkdir $DIR/$tdir
22581         touch $DIR/$tdir/f
22582         cnt=$(ls -1 $DIR/$tdir | wc -l)
22583         [ $cnt != 1 ] && error "unexpected #files: $cnt"
22584
22585         FID=$(lfs path2fid $DIR/$tdir/f)
22586         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (1)"
22587         # rmfid should fail
22588         cnt=$(ls -1 $DIR/$tdir | wc -l)
22589         [ $cnt != 1 ] && error "unexpected #files after (2): $cnt"
22590
22591         chmod a+rw $DIR/$tdir
22592         ls -la $DIR/$tdir
22593         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail (2)"
22594         # rmfid should fail
22595         cnt=$(ls -1 $DIR/$tdir | wc -l)
22596         [ $cnt != 1 ] && error "unexpected #files after (3): $cnt"
22597
22598         rm -f $DIR/$tdir/f
22599         $RUNAS touch $DIR/$tdir/f
22600         FID=$(lfs path2fid $DIR/$tdir/f)
22601         echo "rmfid as root"
22602         $LFS rmfid $DIR $FID || error "rmfid as root failed"
22603         cnt=$(ls -1 $DIR/$tdir | wc -l)
22604         [ $cnt == 0 ] || error "unexpected #files after (4): $cnt"
22605
22606         rm -f $DIR/$tdir/f
22607         $RUNAS touch $DIR/$tdir/f
22608         cnt=$(ls -1 $DIR/$tdir | wc -l)
22609         [ $cnt != 1 ] && error "unexpected #files (4): $cnt"
22610         FID=$(lfs path2fid $DIR/$tdir/f)
22611         # rmfid w/o user_fid2path mount option should fail
22612         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail(3)"
22613         cnt=$(ls -1 $DIR/$tdir | wc -l)
22614         [ $cnt == 1 ] || error "unexpected #files after (5): $cnt"
22615
22616         umount_client $MOUNT || error "failed to umount client"
22617         mount_client $MOUNT "$MOUNT_OPTS,user_fid2path" ||
22618                 error "failed to mount client'"
22619
22620         $RUNAS $LFS rmfid $DIR $FID || error "rmfid failed"
22621         # rmfid should succeed
22622         cnt=$(ls -1 $DIR/$tdir | wc -l)
22623         [ $cnt == 0 ] || error "unexpected #files after (6): $cnt"
22624
22625         # rmfid shouldn't allow to remove files due to dir's permission
22626         chmod a+rwx $DIR/$tdir
22627         touch $DIR/$tdir/f
22628         ls -la $DIR/$tdir
22629         FID=$(lfs path2fid $DIR/$tdir/f)
22630         $RUNAS $LFS rmfid $DIR $FID && error "rmfid didn't fail"
22631
22632         umount_client $MOUNT || error "failed to umount client"
22633         mount_client $MOUNT "$MOUNT_OPTS" ||
22634                 error "failed to mount client'"
22635
22636 }
22637 run_test 421f "rmfid checks permissions"
22638
22639 test_421g() {
22640         local cnt
22641         local FIDS
22642
22643         [ $MDSCOUNT -lt 2 ] && skip "needs >= 2 MDTs"
22644         [ $MDS1_VERSION -lt $(version_code 2.12.54) ] &&
22645                 skip "Need MDS version at least 2.12.54"
22646
22647         mkdir -p $DIR/$tdir
22648         $LFS setdirstripe -c$MDSCOUNT $DIR/$tdir/striped_dir
22649         createmany -o $DIR/$tdir/striped_dir/f 512
22650         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22651         [ $cnt != 512 ] && error "unexpected #files: $cnt"
22652
22653         FIDS=$(lfs path2fid $DIR/$tdir/striped_dir/f* |
22654                 sed "s/[/][^:]*://g")
22655
22656         rm -f $DIR/$tdir/striped_dir/f1*
22657         cnt=$(ls -1 $DIR/$tdir/striped_dir | wc -l)
22658         removed=$((512 - cnt))
22659
22660         # few files have been just removed, so we expect
22661         # rmfid to fail on their fids
22662         errors=$($LFS rmfid $DIR $FIDS 2>&1 | wc -l)
22663         [ $removed != $errors ] && error "$errors != $removed"
22664
22665         cnt=$(ls $DIR/$tdir/striped_dir | wc -l)
22666         rm -rf $DIR/$tdir
22667         [ $cnt == 0 ] || error "unexpected #files after: $cnt"
22668 }
22669 run_test 421g "rmfid to return errors properly"
22670
22671 test_422() {
22672         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d1
22673         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d2
22674         test_mkdir -i 0 -c 1 -p $DIR/$tdir/d3
22675         dd if=/dev/zero of=$DIR/$tdir/d1/file1 bs=1k count=1
22676         dd if=/dev/zero of=$DIR/$tdir/d2/file1 bs=1k count=1
22677
22678         local amc=$(at_max_get client)
22679         local amo=$(at_max_get mds1)
22680         local timeout=`lctl get_param -n timeout`
22681
22682         at_max_set 0 client
22683         at_max_set 0 mds1
22684
22685 #define OBD_FAIL_PTLRPC_PAUSE_REQ        0x50a
22686         do_facet mds1 $LCTL set_param fail_loc=0x8000050a \
22687                         fail_val=$(((2*timeout + 10)*1000))
22688         touch $DIR/$tdir/d3/file &
22689         sleep 2
22690 #define OBD_FAIL_TGT_REPLY_DATA_RACE     0x722
22691         do_facet mds1 $LCTL set_param fail_loc=0x80000722 \
22692                         fail_val=$((2*timeout + 5))
22693         mv $DIR/$tdir/d1/file1 $DIR/$tdir/d1/file2 &
22694         local pid=$!
22695         sleep 1
22696         kill -9 $pid
22697         sleep $((2 * timeout))
22698         echo kill $pid
22699         kill -9 $pid
22700         lctl mark touch
22701         touch $DIR/$tdir/d2/file3
22702         touch $DIR/$tdir/d2/file4
22703         touch $DIR/$tdir/d2/file5
22704
22705         wait
22706         at_max_set $amc client
22707         at_max_set $amo mds1
22708
22709         # LU-12838 - verify the ptlrpc thread watchdog is not always throttled
22710         do_facet mds1 "dmesg | grep 'Dumping the stack trace for debugging'" ||
22711                 error "Watchdog is always throttled"
22712 }
22713 run_test 422 "kill a process with RPC in progress"
22714
22715 stat_test() {
22716     df -h $MOUNT &
22717     df -h $MOUNT &
22718     df -h $MOUNT &
22719     df -h $MOUNT &
22720     df -h $MOUNT &
22721     df -h $MOUNT &
22722 }
22723
22724 test_423() {
22725     local _stats
22726     # ensure statfs cache is expired
22727     sleep 2;
22728
22729     _stats=$(stat_test | grep $MOUNT | sort -u | wc -l)
22730     [[ ${_stats} -ne 1 ]] && error "statfs wrong"
22731
22732     return 0
22733 }
22734 run_test 423 "statfs should return a right data"
22735
22736 prep_801() {
22737         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22738         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22739                 skip "Need server version at least 2.9.55"
22740
22741         start_full_debug_logging
22742 }
22743
22744 post_801() {
22745         stop_full_debug_logging
22746 }
22747
22748 barrier_stat() {
22749         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22750                 local st=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22751                            awk '/The barrier for/ { print $7 }')
22752                 echo $st
22753         else
22754                 local st=$(do_facet mgs $LCTL barrier_stat -s $FSNAME)
22755                 echo \'$st\'
22756         fi
22757 }
22758
22759 barrier_expired() {
22760         local expired
22761
22762         if [ $MGS_VERSION -le $(version_code 2.10.0) ]; then
22763                 expired=$(do_facet mgs $LCTL barrier_stat $FSNAME |
22764                           awk '/will be expired/ { print $7 }')
22765         else
22766                 expired=$(do_facet mgs $LCTL barrier_stat -t $FSNAME)
22767         fi
22768
22769         echo $expired
22770 }
22771
22772 test_801a() {
22773         prep_801
22774
22775         echo "Start barrier_freeze at: $(date)"
22776         #define OBD_FAIL_BARRIER_DELAY          0x2202
22777         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22778         # Do not reduce barrier time - See LU-11873
22779         do_facet mgs $LCTL barrier_freeze $FSNAME 20 &
22780
22781         sleep 2
22782         local b_status=$(barrier_stat)
22783         echo "Got barrier status at: $(date)"
22784         [ "$b_status" = "'freezing_p1'" ] ||
22785                 error "(1) unexpected barrier status $b_status"
22786
22787         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22788         wait
22789         b_status=$(barrier_stat)
22790         [ "$b_status" = "'frozen'" ] ||
22791                 error "(2) unexpected barrier status $b_status"
22792
22793         local expired=$(barrier_expired)
22794         echo "sleep $((expired + 3)) seconds, then the barrier will be expired"
22795         sleep $((expired + 3))
22796
22797         b_status=$(barrier_stat)
22798         [ "$b_status" = "'expired'" ] ||
22799                 error "(3) unexpected barrier status $b_status"
22800
22801         # Do not reduce barrier time - See LU-11873
22802         do_facet mgs $LCTL barrier_freeze $FSNAME 20 ||
22803                 error "(4) fail to freeze barrier"
22804
22805         b_status=$(barrier_stat)
22806         [ "$b_status" = "'frozen'" ] ||
22807                 error "(5) unexpected barrier status $b_status"
22808
22809         echo "Start barrier_thaw at: $(date)"
22810         #define OBD_FAIL_BARRIER_DELAY          0x2202
22811         do_facet mgs $LCTL set_param fail_val=5 fail_loc=0x2202
22812         do_facet mgs $LCTL barrier_thaw $FSNAME &
22813
22814         sleep 2
22815         b_status=$(barrier_stat)
22816         echo "Got barrier status at: $(date)"
22817         [ "$b_status" = "'thawing'" ] ||
22818                 error "(6) unexpected barrier status $b_status"
22819
22820         do_facet mgs $LCTL set_param fail_val=0 fail_loc=0
22821         wait
22822         b_status=$(barrier_stat)
22823         [ "$b_status" = "'thawed'" ] ||
22824                 error "(7) unexpected barrier status $b_status"
22825
22826         #define OBD_FAIL_BARRIER_FAILURE        0x2203
22827         do_facet $SINGLEMDS $LCTL set_param fail_loc=0x2203
22828         do_facet mgs $LCTL barrier_freeze $FSNAME
22829
22830         b_status=$(barrier_stat)
22831         [ "$b_status" = "'failed'" ] ||
22832                 error "(8) unexpected barrier status $b_status"
22833
22834         do_facet $SINGLEMDS $LCTL set_param fail_loc=0
22835         do_facet mgs $LCTL barrier_thaw $FSNAME
22836
22837         post_801
22838 }
22839 run_test 801a "write barrier user interfaces and stat machine"
22840
22841 test_801b() {
22842         prep_801
22843
22844         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22845         createmany -d $DIR/$tdir/d 6 || "(2) fail to mkdir"
22846         touch $DIR/$tdir/d2/f10 || error "(3) fail to touch"
22847         touch $DIR/$tdir/d3/f11 || error "(4) fail to touch"
22848         touch $DIR/$tdir/d4/f12 || error "(5) fail to touch"
22849
22850         cancel_lru_locks mdc
22851
22852         # 180 seconds should be long enough
22853         do_facet mgs $LCTL barrier_freeze $FSNAME 180
22854
22855         local b_status=$(barrier_stat)
22856         [ "$b_status" = "'frozen'" ] ||
22857                 error "(6) unexpected barrier status $b_status"
22858
22859         mkdir $DIR/$tdir/d0/d10 &
22860         mkdir_pid=$!
22861
22862         touch $DIR/$tdir/d1/f13 &
22863         touch_pid=$!
22864
22865         ln $DIR/$tdir/d2/f10 $DIR/$tdir/d2/f14 &
22866         ln_pid=$!
22867
22868         mv $DIR/$tdir/d3/f11 $DIR/$tdir/d3/f15 &
22869         mv_pid=$!
22870
22871         rm -f $DIR/$tdir/d4/f12 &
22872         rm_pid=$!
22873
22874         stat $DIR/$tdir/d5 || error "(7) stat should succeed"
22875
22876         # To guarantee taht the 'stat' is not blocked
22877         b_status=$(barrier_stat)
22878         [ "$b_status" = "'frozen'" ] ||
22879                 error "(8) unexpected barrier status $b_status"
22880
22881         # let above commands to run at background
22882         sleep 5
22883
22884         ps -p $mkdir_pid || error "(9) mkdir should be blocked"
22885         ps -p $touch_pid || error "(10) touch should be blocked"
22886         ps -p $ln_pid || error "(11) link should be blocked"
22887         ps -p $mv_pid || error "(12) rename should be blocked"
22888         ps -p $rm_pid || error "(13) unlink should be blocked"
22889
22890         b_status=$(barrier_stat)
22891         [ "$b_status" = "'frozen'" ] ||
22892                 error "(14) unexpected barrier status $b_status"
22893
22894         do_facet mgs $LCTL barrier_thaw $FSNAME
22895         b_status=$(barrier_stat)
22896         [ "$b_status" = "'thawed'" ] ||
22897                 error "(15) unexpected barrier status $b_status"
22898
22899         wait $mkdir_pid || error "(16) mkdir should succeed"
22900         wait $touch_pid || error "(17) touch should succeed"
22901         wait $ln_pid || error "(18) link should succeed"
22902         wait $mv_pid || error "(19) rename should succeed"
22903         wait $rm_pid || error "(20) unlink should succeed"
22904
22905         post_801
22906 }
22907 run_test 801b "modification will be blocked by write barrier"
22908
22909 test_801c() {
22910         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
22911
22912         prep_801
22913
22914         stop mds2 || error "(1) Fail to stop mds2"
22915
22916         do_facet mgs $LCTL barrier_freeze $FSNAME 30
22917
22918         local b_status=$(barrier_stat)
22919         [ "$b_status" = "'expired'" ] || [ "$b_status" = "'failed'" ] || {
22920                 do_facet mgs $LCTL barrier_thaw $FSNAME
22921                 error "(2) unexpected barrier status $b_status"
22922         }
22923
22924         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22925                 error "(3) Fail to rescan barrier bitmap"
22926
22927         # Do not reduce barrier time - See LU-11873
22928         do_facet mgs $LCTL barrier_freeze $FSNAME 20
22929
22930         b_status=$(barrier_stat)
22931         [ "$b_status" = "'frozen'" ] ||
22932                 error "(4) unexpected barrier status $b_status"
22933
22934         do_facet mgs $LCTL barrier_thaw $FSNAME
22935         b_status=$(barrier_stat)
22936         [ "$b_status" = "'thawed'" ] ||
22937                 error "(5) unexpected barrier status $b_status"
22938
22939         local devname=$(mdsdevname 2)
22940
22941         start mds2 $devname $MDS_MOUNT_OPTS || error "(6) Fail to start mds2"
22942
22943         do_facet mgs $LCTL barrier_rescan $FSNAME ||
22944                 error "(7) Fail to rescan barrier bitmap"
22945
22946         post_801
22947 }
22948 run_test 801c "rescan barrier bitmap"
22949
22950 saved_MGS_MOUNT_OPTS=$MGS_MOUNT_OPTS
22951 saved_MDS_MOUNT_OPTS=$MDS_MOUNT_OPTS
22952 saved_OST_MOUNT_OPTS=$OST_MOUNT_OPTS
22953 saved_MOUNT_OPTS=$MOUNT_OPTS
22954
22955 cleanup_802a() {
22956         trap 0
22957
22958         stopall
22959         MGS_MOUNT_OPTS=$saved_MGS_MOUNT_OPTS
22960         MDS_MOUNT_OPTS=$saved_MDS_MOUNT_OPTS
22961         OST_MOUNT_OPTS=$saved_OST_MOUNT_OPTS
22962         MOUNT_OPTS=$saved_MOUNT_OPTS
22963         setupall
22964 }
22965
22966 test_802a() {
22967         [[ $mds1_FSTYPE = zfs ]] || skip "ZFS specific test"
22968         [[ $MDS1_VERSION -lt $(version_code 2.9.55) ]] ||
22969         [[ $OST1_VERSION -lt $(version_code 2.9.55) ]] &&
22970                 skip "Need server version at least 2.9.55"
22971
22972         [[ $ENABLE_QUOTA ]] && skip "Quota enabled for read-only test"
22973
22974         mkdir $DIR/$tdir || error "(1) fail to mkdir"
22975
22976         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
22977                 error "(2) Fail to copy"
22978
22979         trap cleanup_802a EXIT
22980
22981         # sync by force before remount as readonly
22982         sync; sync_all_data; sleep 3; sync_all_data
22983
22984         stopall
22985
22986         MGS_MOUNT_OPTS=$(csa_add "$MGS_MOUNT_OPTS" -o rdonly_dev)
22987         MDS_MOUNT_OPTS=$(csa_add "$MDS_MOUNT_OPTS" -o rdonly_dev)
22988         OST_MOUNT_OPTS=$(csa_add "$OST_MOUNT_OPTS" -o rdonly_dev)
22989
22990         echo "Mount the server as read only"
22991         setupall server_only || error "(3) Fail to start servers"
22992
22993         echo "Mount client without ro should fail"
22994         mount_client $MOUNT &&
22995                 error "(4) Mount client without 'ro' should fail"
22996
22997         echo "Mount client with ro should succeed"
22998         MOUNT_OPTS=$(csa_add "$MOUNT_OPTS" -o ro)
22999         mount_client $MOUNT ||
23000                 error "(5) Mount client with 'ro' should succeed"
23001
23002         echo "Modify should be refused"
23003         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
23004
23005         echo "Read should be allowed"
23006         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
23007                 error "(7) Read should succeed under ro mode"
23008
23009         cleanup_802a
23010 }
23011 run_test 802a "simulate readonly device"
23012
23013 test_802b() {
23014         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23015         remote_mds_nodsh && skip "remote MDS with nodsh"
23016
23017         do_facet $SINGLEMDS $LCTL get_param mdt.*.readonly ||
23018                 skip "readonly option not available"
23019
23020         $LFS mkdir -i 0 -c 1 $DIR/$tdir || error "(1) fail to mkdir"
23021
23022         cp $LUSTRE/tests/test-framework.sh $DIR/$tdir/ ||
23023                 error "(2) Fail to copy"
23024
23025         # write back all cached data before setting MDT to readonly
23026         cancel_lru_locks
23027         sync_all_data
23028
23029         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=1
23030         stack_trap "do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0" EXIT
23031
23032         echo "Modify should be refused"
23033         touch $DIR/$tdir/guard && error "(6) Touch should fail under ro mode"
23034
23035         echo "Read should be allowed"
23036         diff $LUSTRE/tests/test-framework.sh $DIR/$tdir/test-framework.sh ||
23037                 error "(7) Read should succeed under ro mode"
23038
23039         # disable readonly
23040         do_facet $SINGLEMDS $LCTL set_param mdt.*.readonly=0
23041 }
23042 run_test 802b "be able to set MDTs to readonly"
23043
23044 test_803() {
23045         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23046         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
23047                 skip "MDS needs to be newer than 2.10.54"
23048
23049         mkdir -p $DIR/$tdir
23050         # Create some objects on all MDTs to trigger related logs objects
23051         for idx in $(seq $MDSCOUNT); do
23052                 $LFS mkdir -c $MDSCOUNT -i $((idx % $MDSCOUNT)) \
23053                         $DIR/$tdir/dir${idx} ||
23054                         error "Fail to create $DIR/$tdir/dir${idx}"
23055         done
23056
23057         sync; sleep 3
23058         wait_delete_completed # ensure old test cleanups are finished
23059         echo "before create:"
23060         $LFS df -i $MOUNT
23061         local before_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23062
23063         for i in {1..10}; do
23064                 $LFS mkdir -c 1 -i 1 $DIR/$tdir/foo$i ||
23065                         error "Fail to create $DIR/$tdir/foo$i"
23066         done
23067
23068         sync; sleep 3
23069         echo "after create:"
23070         $LFS df -i $MOUNT
23071         local after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23072
23073         # allow for an llog to be cleaned up during the test
23074         [ $after_used -ge $((before_used + 10 - 1)) ] ||
23075                 error "before ($before_used) + 10 > after ($after_used)"
23076
23077         for i in {1..10}; do
23078                 rm -rf $DIR/$tdir/foo$i ||
23079                         error "Fail to remove $DIR/$tdir/foo$i"
23080         done
23081
23082         sleep 3 # avoid MDT return cached statfs
23083         wait_delete_completed
23084         echo "after unlink:"
23085         $LFS df -i $MOUNT
23086         after_used=$($LFS df -i | grep MDT0000_UUID | awk '{print $3}')
23087
23088         # allow for an llog to be created during the test
23089         [ $after_used -le $((before_used + 1)) ] ||
23090                 error "after ($after_used) > before ($before_used) + 1"
23091 }
23092 run_test 803 "verify agent object for remote object"
23093
23094 test_804() {
23095         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23096         [ $MDS1_VERSION -lt $(version_code 2.10.54) ] &&
23097                 skip "MDS needs to be newer than 2.10.54"
23098         [ "$mds1_FSTYPE" != "ldiskfs" ] && skip_env "ldiskfs only test"
23099
23100         mkdir -p $DIR/$tdir
23101         $LFS mkdir -c 1 -i 1 $DIR/$tdir/dir0 ||
23102                 error "Fail to create $DIR/$tdir/dir0"
23103
23104         local fid=$($LFS path2fid $DIR/$tdir/dir0)
23105         local dev=$(mdsdevname 2)
23106
23107         do_facet mds2 "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23108                 grep ${fid} || error "NOT found agent entry for dir0"
23109
23110         $LFS mkdir -c $MDSCOUNT -i 0 $DIR/$tdir/dir1 ||
23111                 error "Fail to create $DIR/$tdir/dir1"
23112
23113         touch $DIR/$tdir/dir1/foo0 ||
23114                 error "Fail to create $DIR/$tdir/dir1/foo0"
23115         fid=$($LFS path2fid $DIR/$tdir/dir1/foo0)
23116         local rc=0
23117
23118         for idx in $(seq $MDSCOUNT); do
23119                 dev=$(mdsdevname $idx)
23120                 do_facet mds${idx} \
23121                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23122                         grep ${fid} && rc=$idx
23123         done
23124
23125         mv $DIR/$tdir/dir1/foo0 $DIR/$tdir/dir1/foo1 ||
23126                 error "Fail to rename foo0 to foo1"
23127         if [ $rc -eq 0 ]; then
23128                 for idx in $(seq $MDSCOUNT); do
23129                         dev=$(mdsdevname $idx)
23130                         do_facet mds${idx} \
23131                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23132                         grep ${fid} && rc=$idx
23133                 done
23134         fi
23135
23136         mv $DIR/$tdir/dir1/foo1 $DIR/$tdir/dir1/foo2 ||
23137                 error "Fail to rename foo1 to foo2"
23138         if [ $rc -eq 0 ]; then
23139                 for idx in $(seq $MDSCOUNT); do
23140                         dev=$(mdsdevname $idx)
23141                         do_facet mds${idx} \
23142                         "$DEBUGFS -c -R 'ls /REMOTE_PARENT_DIR' $dev" |
23143                         grep ${fid} && rc=$idx
23144                 done
23145         fi
23146
23147         [ $rc -ne 0 ] || error "NOT found agent entry for foo"
23148
23149         ln $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir0/guard ||
23150                 error "Fail to link to $DIR/$tdir/dir1/foo2"
23151         mv $DIR/$tdir/dir1/foo2 $DIR/$tdir/dir1/foo0 ||
23152                 error "Fail to rename foo2 to foo0"
23153         unlink $DIR/$tdir/dir1/foo0 ||
23154                 error "Fail to unlink $DIR/$tdir/dir1/foo0"
23155         rm -rf $DIR/$tdir/dir0 ||
23156                 error "Fail to rm $DIR/$tdir/dir0"
23157
23158         for idx in $(seq $MDSCOUNT); do
23159                 dev=$(mdsdevname $idx)
23160                 rc=0
23161
23162                 stop mds${idx}
23163                 run_e2fsck $(facet_active_host mds$idx) $dev -n ||
23164                         rc=$?
23165                 start mds${idx} $dev $MDS_MOUNT_OPTS ||
23166                         error "mount mds$idx failed"
23167                 df $MOUNT > /dev/null 2>&1
23168
23169                 # e2fsck should not return error
23170                 [ $rc -eq 0 ] ||
23171                         error "e2fsck detected error on MDT${idx}: rc=$rc"
23172         done
23173 }
23174 run_test 804 "verify agent entry for remote entry"
23175
23176 cleanup_805() {
23177         do_facet $SINGLEMDS zfs set quota=$old $fsset
23178         unlinkmany $DIR/$tdir/f- 1000000
23179         trap 0
23180 }
23181
23182 test_805() {
23183         local zfs_version=$(do_facet mds1 cat /sys/module/zfs/version)
23184         [ "$mds1_FSTYPE" != "zfs" ] && skip "ZFS specific test"
23185         [ $(version_code $zfs_version) -lt $(version_code 0.7.2) ] &&
23186                 skip "netfree not implemented before 0.7"
23187         [[ $MDS1_VERSION -ge $(version_code 2.10.57) ]] ||
23188                 skip "Need MDS version at least 2.10.57"
23189
23190         local fsset
23191         local freekb
23192         local usedkb
23193         local old
23194         local quota
23195         local pref="osd-zfs.$FSNAME-MDT0000."
23196
23197         # limit available space on MDS dataset to meet nospace issue
23198         # quickly. then ZFS 0.7.2 can use reserved space if asked
23199         # properly (using netfree flag in osd_declare_destroy()
23200         fsset=$(do_facet $SINGLEMDS lctl get_param -n $pref.mntdev)
23201         old=$(do_facet $SINGLEMDS zfs get -H quota $fsset | \
23202                 gawk '{print $3}')
23203         freekb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytesfree)
23204         usedkb=$(do_facet $SINGLEMDS lctl get_param -n $pref.kbytestotal)
23205         let "usedkb=usedkb-freekb"
23206         let "freekb=freekb/2"
23207         if let "freekb > 5000"; then
23208                 let "freekb=5000"
23209         fi
23210         do_facet $SINGLEMDS zfs set quota=$(((usedkb+freekb)*1024)) $fsset
23211         trap cleanup_805 EXIT
23212         mkdir $DIR/$tdir
23213         $LFS setstripe -E 1M -c2 -E 4M -c2 -E -1 -c2 $DIR/$tdir ||
23214                 error "Can't set PFL layout"
23215         createmany -m $DIR/$tdir/f- 1000000 && error "ENOSPC wasn't met"
23216         rm -rf $DIR/$tdir || error "not able to remove"
23217         do_facet $SINGLEMDS zfs set quota=$old $fsset
23218         trap 0
23219 }
23220 run_test 805 "ZFS can remove from full fs"
23221
23222 # Size-on-MDS test
23223 check_lsom_data()
23224 {
23225         local file=$1
23226         local size=$($LFS getsom -s $file)
23227         local expect=$(stat -c %s $file)
23228
23229         [[ $size == $expect ]] ||
23230                 error "$file expected size: $expect, got: $size"
23231
23232         local blocks=$($LFS getsom -b $file)
23233         expect=$(stat -c %b $file)
23234         [[ $blocks == $expect ]] ||
23235                 error "$file expected blocks: $expect, got: $blocks"
23236 }
23237
23238 check_lsom_size()
23239 {
23240         local size=$($LFS getsom -s $1)
23241         local expect=$2
23242
23243         [[ $size == $expect ]] ||
23244                 error "$file expected size: $expect, got: $size"
23245 }
23246
23247 test_806() {
23248         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
23249                 skip "Need MDS version at least 2.11.52"
23250
23251         local bs=1048576
23252
23253         touch $DIR/$tfile || error "touch $tfile failed"
23254
23255         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23256         save_lustre_params client "llite.*.xattr_cache" > $save
23257         lctl set_param llite.*.xattr_cache=0
23258         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23259
23260         # single-threaded write
23261         echo "Test SOM for single-threaded write"
23262         dd if=/dev/zero of=$DIR/$tfile bs=$bs count=1 ||
23263                 error "write $tfile failed"
23264         check_lsom_size $DIR/$tfile $bs
23265
23266         local num=32
23267         local size=$(($num * $bs))
23268         local offset=0
23269         local i
23270
23271         echo "Test SOM for single client multi-threaded($num) write"
23272         $TRUNCATE $DIR/$tfile 0
23273         for ((i = 0; i < $num; i++)); do
23274                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23275                 local pids[$i]=$!
23276                 offset=$((offset + $bs))
23277         done
23278         for (( i=0; i < $num; i++ )); do
23279                 wait ${pids[$i]}
23280         done
23281         check_lsom_size $DIR/$tfile $size
23282
23283         $TRUNCATE $DIR/$tfile 0
23284         for ((i = 0; i < $num; i++)); do
23285                 offset=$((offset - $bs))
23286                 $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23287                 local pids[$i]=$!
23288         done
23289         for (( i=0; i < $num; i++ )); do
23290                 wait ${pids[$i]}
23291         done
23292         check_lsom_size $DIR/$tfile $size
23293
23294         # multi-client writes
23295         num=$(get_node_count ${CLIENTS//,/ })
23296         size=$(($num * $bs))
23297         offset=0
23298         i=0
23299
23300         echo "Test SOM for multi-client ($num) writes"
23301         $TRUNCATE $DIR/$tfile 0
23302         for client in ${CLIENTS//,/ }; do
23303                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23304                 local pids[$i]=$!
23305                 i=$((i + 1))
23306                 offset=$((offset + $bs))
23307         done
23308         for (( i=0; i < $num; i++ )); do
23309                 wait ${pids[$i]}
23310         done
23311         check_lsom_size $DIR/$tfile $offset
23312
23313         i=0
23314         $TRUNCATE $DIR/$tfile 0
23315         for client in ${CLIENTS//,/ }; do
23316                 offset=$((offset - $bs))
23317                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23318                 local pids[$i]=$!
23319                 i=$((i + 1))
23320         done
23321         for (( i=0; i < $num; i++ )); do
23322                 wait ${pids[$i]}
23323         done
23324         check_lsom_size $DIR/$tfile $size
23325
23326         # verify truncate
23327         echo "Test SOM for truncate"
23328         $TRUNCATE $DIR/$tfile 1048576
23329         check_lsom_size $DIR/$tfile 1048576
23330         $TRUNCATE $DIR/$tfile 1234
23331         check_lsom_size $DIR/$tfile 1234
23332
23333         # verify SOM blocks count
23334         echo "Verify SOM block count"
23335         $TRUNCATE $DIR/$tfile 0
23336         $MULTIOP $DIR/$tfile oO_TRUNC:O_RDWR:w1048576YSc ||
23337                 error "failed to write file $tfile"
23338         check_lsom_data $DIR/$tfile
23339 }
23340 run_test 806 "Verify Lazy Size on MDS"
23341
23342 test_807() {
23343         [ -n "$FILESET" ] && skip "Not functional for FILESET set"
23344         [ $MDS1_VERSION -lt $(version_code 2.11.52) ] &&
23345                 skip "Need MDS version at least 2.11.52"
23346
23347         # Registration step
23348         changelog_register || error "changelog_register failed"
23349         local cl_user="${CL_USERS[$SINGLEMDS]%% *}"
23350         changelog_users $SINGLEMDS | grep -q $cl_user ||
23351                 error "User $cl_user not found in changelog_users"
23352
23353         local save="$TMP/$TESTSUITE-$TESTNAME.parameters"
23354         save_lustre_params client "llite.*.xattr_cache" > $save
23355         lctl set_param llite.*.xattr_cache=0
23356         stack_trap "restore_lustre_params < $save; rm -f $save" EXIT
23357
23358         rm -rf $DIR/$tdir || error "rm $tdir failed"
23359         mkdir -p $DIR/$tdir || error "mkdir $tdir failed"
23360         touch $DIR/$tdir/trunc || error "touch $tdir/trunc failed"
23361         $TRUNCATE $DIR/$tdir/trunc 1024 || error "truncate $tdir/trunc failed"
23362         $TRUNCATE $DIR/$tdir/trunc 1048576 ||
23363                 error "truncate $tdir/trunc failed"
23364
23365         local bs=1048576
23366         dd if=/dev/zero of=$DIR/$tdir/single_dd bs=$bs count=1 conv=fsync ||
23367                 error "write $tfile failed"
23368
23369         # multi-client wirtes
23370         local num=$(get_node_count ${CLIENTS//,/ })
23371         local offset=0
23372         local i=0
23373
23374         echo "Test SOM for multi-client ($num) writes"
23375         touch $DIR/$tfile || error "touch $tfile failed"
23376         $TRUNCATE $DIR/$tfile 0
23377         for client in ${CLIENTS//,/ }; do
23378                 do_node $client $MULTIOP $DIR/$tfile Oz${offset}w${bs}c &
23379                 local pids[$i]=$!
23380                 i=$((i + 1))
23381                 offset=$((offset + $bs))
23382         done
23383         for (( i=0; i < $num; i++ )); do
23384                 wait ${pids[$i]}
23385         done
23386
23387         do_rpc_nodes "$CLIENTS" cancel_lru_locks osc
23388         do_nodes "$CLIENTS" "sync ; sleep 5 ; sync"
23389         $LSOM_SYNC -u $cl_user -m $FSNAME-MDT0000 $MOUNT
23390         check_lsom_data $DIR/$tdir/trunc
23391         check_lsom_data $DIR/$tdir/single_dd
23392         check_lsom_data $DIR/$tfile
23393
23394         rm -rf $DIR/$tdir
23395         # Deregistration step
23396         changelog_deregister || error "changelog_deregister failed"
23397 }
23398 run_test 807 "verify LSOM syncing tool"
23399
23400 check_som_nologged()
23401 {
23402         local lines=$($LFS changelog $FSNAME-MDT0000 |
23403                 grep 'x=trusted.som' | wc -l)
23404         [ $lines -ne 0 ] && error "trusted.som xattr is logged in Changelogs"
23405 }
23406
23407 test_808() {
23408         [ $MDS1_VERSION -lt $(version_code 2.11.55) ] &&
23409                 skip "Need MDS version at least 2.11.55"
23410
23411         # Registration step
23412         changelog_register || error "changelog_register failed"
23413
23414         touch $DIR/$tfile || error "touch $tfile failed"
23415         check_som_nologged
23416
23417         dd if=/dev/zero of=$DIR/$tfile bs=1048576 count=1 ||
23418                 error "write $tfile failed"
23419         check_som_nologged
23420
23421         $TRUNCATE $DIR/$tfile 1234
23422         check_som_nologged
23423
23424         $TRUNCATE $DIR/$tfile 1048576
23425         check_som_nologged
23426
23427         # Deregistration step
23428         changelog_deregister || error "changelog_deregister failed"
23429 }
23430 run_test 808 "Check trusted.som xattr not logged in Changelogs"
23431
23432 check_som_nodata()
23433 {
23434         $LFS getsom $1
23435         [[ $? -eq 61 ]] || error "DoM-only file $1 has SOM xattr"
23436 }
23437
23438 test_809() {
23439         [ $MDS1_VERSION -lt $(version_code 2.11.56) ] &&
23440                 skip "Need MDS version at least 2.11.56"
23441
23442         $LFS setstripe -E 1M -L mdt $DIR/$tfile ||
23443                 error "failed to create DoM-only file $DIR/$tfile"
23444         touch $DIR/$tfile || error "touch $tfile failed"
23445         check_som_nodata $DIR/$tfile
23446
23447         dd if=/dev/zero of=$DIR/$tfile bs=2048 count=1 ||
23448                 error "write $tfile failed"
23449         check_som_nodata $DIR/$tfile
23450
23451         $TRUNCATE $DIR/$tfile 1234
23452         check_som_nodata $DIR/$tfile
23453
23454         $TRUNCATE $DIR/$tfile 4097
23455         check_som_nodata $DIR/$file
23456 }
23457 run_test 809 "Verify no SOM xattr store for DoM-only files"
23458
23459 test_810() {
23460         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23461         $GSS && skip_env "could not run with gss"
23462         [[ $OST1_VERSION -gt $(version_code 2.12.58) ]] ||
23463                 skip "OST < 2.12.58 doesn't align checksum"
23464
23465         set_checksums 1
23466         stack_trap "set_checksums $ORIG_CSUM" EXIT
23467         stack_trap "set_checksum_type $ORIG_CSUM_TYPE" EXIT
23468
23469         local csum
23470         local before
23471         local after
23472         for csum in $CKSUM_TYPES; do
23473                 #define OBD_FAIL_OSC_NO_GRANT   0x411
23474                 $LCTL set_param osc.*.checksum_type=$csum fail_loc=0x411
23475                 for i in "10240 0" "10000 0" "4000 1" "500 1"; do
23476                         eval set -- $i
23477                         dd if=/dev/urandom of=$DIR/$tfile bs=$1 count=2 seek=$2
23478                         before=$(md5sum $DIR/$tfile)
23479                         $LCTL set_param ldlm.namespaces.*osc*.lru_size=clear
23480                         after=$(md5sum $DIR/$tfile)
23481                         [ "$before" == "$after" ] ||
23482                                 error "$csum: $before != $after bs=$1 seek=$2"
23483                 done
23484         done
23485 }
23486 run_test 810 "partial page writes on ZFS (LU-11663)"
23487
23488 test_812a() {
23489         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23490                 skip "OST < 2.12.51 doesn't support this fail_loc"
23491         [ "$SHARED_KEY" = true ] &&
23492                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23493
23494         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23495         # ensure ost1 is connected
23496         stat $DIR/$tfile >/dev/null || error "can't stat"
23497         wait_osc_import_state client ost1 FULL
23498         # no locks, no reqs to let the connection idle
23499         cancel_lru_locks osc
23500
23501         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23502 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23503         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23504         wait_osc_import_state client ost1 CONNECTING
23505         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23506
23507         stat $DIR/$tfile >/dev/null || error "can't stat file"
23508 }
23509 run_test 812a "do not drop reqs generated when imp is going to idle (LU-11951)"
23510
23511 test_812b() { # LU-12378
23512         [ $OST1_VERSION -lt $(version_code 2.12.51) ] &&
23513                 skip "OST < 2.12.51 doesn't support this fail_loc"
23514         [ "$SHARED_KEY" = true ] &&
23515                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23516
23517         $LFS setstripe -c 1 -i 0 $DIR/$tfile || error "setstripe failed"
23518         # ensure ost1 is connected
23519         stat $DIR/$tfile >/dev/null || error "can't stat"
23520         wait_osc_import_state client ost1 FULL
23521         # no locks, no reqs to let the connection idle
23522         cancel_lru_locks osc
23523
23524         # delay OST_DISCONNECT on OST1 to put OSC into intermediate state
23525 #define OBD_FAIL_OST_DISCONNECT_DELAY    0x245
23526         do_facet ost1 "$LCTL set_param fail_loc=0x245 fail_val=8"
23527         wait_osc_import_state client ost1 CONNECTING
23528         do_facet ost1 "$LCTL set_param fail_loc=0 fail_val=0"
23529
23530         $LFS quota -u 0 $DIR/ || error "lfs quota should succeed"
23531         wait_osc_import_state client ost1 IDLE
23532 }
23533 run_test 812b "do not drop no resend request for idle connect"
23534
23535 test_813() {
23536         local file_heat_sav=$($LCTL get_param -n llite.*.file_heat 2>/dev/null)
23537         [ -z "$file_heat_sav" ] && skip "no file heat support"
23538
23539         local readsample
23540         local writesample
23541         local readbyte
23542         local writebyte
23543         local readsample1
23544         local writesample1
23545         local readbyte1
23546         local writebyte1
23547
23548         local period_second=$($LCTL get_param -n llite.*.heat_period_second)
23549         local decay_pct=$($LCTL get_param -n llite.*.heat_decay_percentage)
23550
23551         $LCTL set_param -n llite.*.file_heat=1
23552         echo "Turn on file heat"
23553         echo "Period second: $period_second, Decay percentage: $decay_pct"
23554
23555         echo "QQQQ" > $DIR/$tfile
23556         echo "QQQQ" > $DIR/$tfile
23557         echo "QQQQ" > $DIR/$tfile
23558         cat $DIR/$tfile > /dev/null
23559         cat $DIR/$tfile > /dev/null
23560         cat $DIR/$tfile > /dev/null
23561         cat $DIR/$tfile > /dev/null
23562
23563         local out=$($LFS heat_get $DIR/$tfile)
23564
23565         $LFS heat_get $DIR/$tfile
23566         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23567         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23568         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23569         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23570
23571         [ $readsample -le 4 ] || error "read sample ($readsample) is wrong"
23572         [ $writesample -le 3 ] || error "write sample ($writesample) is wrong"
23573         [ $readbyte -le 20 ] || error "read bytes ($readbyte) is wrong"
23574         [ $writebyte -le 15 ] || error "write bytes ($writebyte) is wrong"
23575
23576         sleep $((period_second + 3))
23577         echo "Sleep $((period_second + 3)) seconds..."
23578         # The recursion formula to calculate the heat of the file f is as
23579         # follow:
23580         # Hi+1(f) = (1-P)*Hi(f)+ P*Ci
23581         # Where Hi is the heat value in the period between time points i*I and
23582         # (i+1)*I; Ci is the access count in the period; the symbol P refers
23583         # to the weight of Ci.
23584         out=$($LFS heat_get $DIR/$tfile)
23585         $LFS heat_get $DIR/$tfile
23586         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23587         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23588         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23589         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23590
23591         [ $(bc <<< "$readsample <= 4 * $decay_pct / 100") -eq 1 ] ||
23592                 error "read sample ($readsample) is wrong"
23593         [ $(bc <<< "$writesample <= 3 * $decay_pct / 100") -eq 1 ] ||
23594                 error "write sample ($writesample) is wrong"
23595         [ $(bc <<< "$readbyte <= 20 * $decay_pct / 100") -eq 1 ] ||
23596                 error "read bytes ($readbyte) is wrong"
23597         [ $(bc <<< "$writebyte <= 15 * $decay_pct / 100") -eq 1 ] ||
23598                 error "write bytes ($writebyte) is wrong"
23599
23600         echo "QQQQ" > $DIR/$tfile
23601         echo "QQQQ" > $DIR/$tfile
23602         echo "QQQQ" > $DIR/$tfile
23603         cat $DIR/$tfile > /dev/null
23604         cat $DIR/$tfile > /dev/null
23605         cat $DIR/$tfile > /dev/null
23606         cat $DIR/$tfile > /dev/null
23607
23608         sleep $((period_second + 3))
23609         echo "Sleep $((period_second + 3)) seconds..."
23610
23611         out=$($LFS heat_get $DIR/$tfile)
23612         $LFS heat_get $DIR/$tfile
23613         readsample1=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23614         writesample1=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23615         readbyte1=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23616         writebyte1=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23617
23618         [ $(bc <<< "$readsample1 <= ($readsample * (100 - $decay_pct) + \
23619                 4 * $decay_pct) / 100") -eq 1 ] ||
23620                 error "read sample ($readsample1) is wrong"
23621         [ $(bc <<< "$writesample1 <= ($writesample * (100 - $decay_pct) + \
23622                 3 * $decay_pct) / 100") -eq 1 ] ||
23623                 error "write sample ($writesample1) is wrong"
23624         [ $(bc <<< "$readbyte1 <= ($readbyte * (100 - $decay_pct) + \
23625                 20 * $decay_pct) / 100") -eq 1 ] ||
23626                 error "read bytes ($readbyte1) is wrong"
23627         [ $(bc <<< "$writebyte1 <= ($writebyte * (100 - $decay_pct) + \
23628                 15 * $decay_pct) / 100") -eq 1 ] ||
23629                 error "write bytes ($writebyte1) is wrong"
23630
23631         echo "Turn off file heat for the file $DIR/$tfile"
23632         $LFS heat_set -o $DIR/$tfile
23633
23634         echo "QQQQ" > $DIR/$tfile
23635         echo "QQQQ" > $DIR/$tfile
23636         echo "QQQQ" > $DIR/$tfile
23637         cat $DIR/$tfile > /dev/null
23638         cat $DIR/$tfile > /dev/null
23639         cat $DIR/$tfile > /dev/null
23640         cat $DIR/$tfile > /dev/null
23641
23642         out=$($LFS heat_get $DIR/$tfile)
23643         $LFS heat_get $DIR/$tfile
23644         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23645         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23646         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23647         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23648
23649         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23650         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23651         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23652         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23653
23654         echo "Trun on file heat for the file $DIR/$tfile"
23655         $LFS heat_set -O $DIR/$tfile
23656
23657         echo "QQQQ" > $DIR/$tfile
23658         echo "QQQQ" > $DIR/$tfile
23659         echo "QQQQ" > $DIR/$tfile
23660         cat $DIR/$tfile > /dev/null
23661         cat $DIR/$tfile > /dev/null
23662         cat $DIR/$tfile > /dev/null
23663         cat $DIR/$tfile > /dev/null
23664
23665         out=$($LFS heat_get $DIR/$tfile)
23666         $LFS heat_get $DIR/$tfile
23667         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23668         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23669         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23670         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23671
23672         [ $readsample -gt 0 ] || error "read sample ($readsample) is wrong"
23673         [ $writesample -gt 0 ] || error "write sample ($writesample) is wrong"
23674         [ $readbyte -gt 0 ] || error "read bytes ($readbyte) is wrong"
23675         [ $writebyte -gt 0 ] || error "write bytes ($writebyte) is wrong"
23676
23677         $LFS heat_set -c $DIR/$tfile
23678         $LCTL set_param -n llite.*.file_heat=0
23679         echo "Turn off file heat support for the Lustre filesystem"
23680
23681         echo "QQQQ" > $DIR/$tfile
23682         echo "QQQQ" > $DIR/$tfile
23683         echo "QQQQ" > $DIR/$tfile
23684         cat $DIR/$tfile > /dev/null
23685         cat $DIR/$tfile > /dev/null
23686         cat $DIR/$tfile > /dev/null
23687         cat $DIR/$tfile > /dev/null
23688
23689         out=$($LFS heat_get $DIR/$tfile)
23690         $LFS heat_get $DIR/$tfile
23691         readsample=$(echo "$out" | grep 'readsample' | awk '{ print $2 }')
23692         writesample=$(echo "$out" | grep 'writesample' | awk '{ print $2 }')
23693         readbyte=$(echo "$out" | grep 'readbyte' | awk '{ print $2 }')
23694         writebyte=$(echo "$out" | grep 'writebyte' | awk '{ print $2 }')
23695
23696         [ $readsample -eq 0 ] || error "read sample ($readsample) is wrong"
23697         [ $writesample -eq 0 ] || error "write sample ($writesample) is wrong"
23698         [ $readbyte -eq 0 ] || error "read bytes ($readbyte) is wrong"
23699         [ $writebyte -eq 0 ] || error "write bytes ($writebyte) is wrong"
23700
23701         $LCTL set_param -n llite.*.file_heat=$file_heat_sav
23702         rm -f $DIR/$tfile
23703 }
23704 run_test 813 "File heat verfication"
23705
23706 test_814()
23707 {
23708         dd of=$DIR/$tfile seek=128 bs=1k < /dev/null
23709         echo -n y >> $DIR/$tfile
23710         cp --sparse=always $DIR/$tfile $DIR/${tfile}.cp || error "copy failed"
23711         diff $DIR/$tfile $DIR/${tfile}.cp || error "files should be same"
23712 }
23713 run_test 814 "sparse cp works as expected (LU-12361)"
23714
23715 test_815()
23716 {
23717         writeme -b 100 $DIR/$tfile || error "write 100 bytes failed"
23718         writeme -b 0 $DIR/$tfile || error "write 0 byte failed"
23719 }
23720 run_test 815 "zero byte tiny write doesn't hang (LU-12382)"
23721
23722 test_816() {
23723         [ "$SHARED_KEY" = true ] &&
23724                 skip "OSC connections never go IDLE with Shared-Keys enabled"
23725
23726         $LFS setstripe -c 1 -i 0 $DIR/$tfile
23727         # ensure ost1 is connected
23728         stat $DIR/$tfile >/dev/null || error "can't stat"
23729         wait_osc_import_state client ost1 FULL
23730         # no locks, no reqs to let the connection idle
23731         cancel_lru_locks osc
23732         lru_resize_disable osc
23733         local before
23734         local now
23735         before=$($LCTL get_param -n \
23736                  ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23737
23738         wait_osc_import_state client ost1 IDLE
23739         dd if=/dev/null of=$DIR/$tfile bs=1k count=1 conv=sync
23740         now=$($LCTL get_param -n \
23741               ldlm.namespaces.$FSNAME-OST0000-osc-[^M]*.lru_size)
23742         [ $before == $now ] || error "lru_size changed $before != $now"
23743 }
23744 run_test 816 "do not reset lru_resize on idle reconnect"
23745
23746 cleanup_817() {
23747         umount $tmpdir
23748         exportfs -u localhost:$DIR/nfsexp
23749         rm -rf $DIR/nfsexp
23750 }
23751
23752 test_817() {
23753         systemctl restart nfs-server.service || skip "failed to restart nfsd"
23754
23755         mkdir -p $DIR/nfsexp
23756         exportfs -orw,no_root_squash localhost:$DIR/nfsexp ||
23757                 error "failed to export nfs"
23758
23759         tmpdir=$(mktemp -d /tmp/nfs-XXXXXX)
23760         stack_trap cleanup_817 EXIT
23761
23762         mount -t nfs -orw localhost:$DIR/nfsexp $tmpdir ||
23763                 error "failed to mount nfs to $tmpdir"
23764
23765         cp /bin/true $tmpdir
23766         $DIR/nfsexp/true || error "failed to execute 'true' command"
23767 }
23768 run_test 817 "nfsd won't cache write lock for exec file"
23769
23770 test_818() {
23771         mkdir $DIR/$tdir
23772         $LFS setstripe -c1 -i0 $DIR/$tfile
23773         $LFS setstripe -c1 -i1 $DIR/$tfile
23774         stop $SINGLEMDS
23775         #define OBD_FAIL_OSP_CANT_PROCESS_LLOG          0x2105
23776         do_facet $SINGLEMDS lctl set_param fail_loc=0x80002105
23777         start $SINGLEMDS $(mdsdevname ${SINGLEMDS//mds/}) $MDS_MOUNT_OPTS ||
23778                 error "start $SINGLEMDS failed"
23779         rm -rf $DIR/$tdir
23780 }
23781 run_test 818 "unlink with failed llog"
23782
23783 test_819a() {
23784         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23785         cancel_lru_locks osc
23786         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23787         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23788         dd if=$DIR/$tfile of=/dev/null bs=1M count=1
23789         rm -f $TDIR/$tfile
23790 }
23791 run_test 819a "too big niobuf in read"
23792
23793 test_819b() {
23794         #define OBD_FAIL_OST_2BIG_NIOBUF                0x248
23795         do_facet $SINGLEMDS lctl set_param fail_loc=0x80000248
23796         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23797         cancel_lru_locks osc
23798         sleep 1
23799         rm -f $TDIR/$tfile
23800 }
23801 run_test 819b "too big niobuf in write"
23802
23803
23804 function test_820_start_ost() {
23805         sleep 5
23806
23807         for num in $(seq $OSTCOUNT); do
23808                 start ost$num $(ostdevname $num) $OST_MOUNT_OPTS
23809         done
23810 }
23811
23812 test_820() {
23813         [[ $MDSCOUNT -lt 2 ]] && skip_env "needs >= 2 MDTs"
23814
23815         mkdir $DIR/$tdir
23816         umount_client $MOUNT || error "umount failed"
23817         for num in $(seq $OSTCOUNT); do
23818                 stop ost$num
23819         done
23820
23821         # mount client with no active OSTs
23822         # so that the client can't initialize max LOV EA size
23823         # from OSC notifications
23824         mount_client $MOUNT || error "mount failed"
23825         # delay OST starting to keep this 0 max EA size for a while
23826         test_820_start_ost &
23827
23828         # create a directory on MDS2
23829         test_mkdir -i 1 -c1 $DIR/$tdir/mds2 ||
23830                 error "Failed to create directory"
23831         # open intent should update default EA size
23832         # see mdc_update_max_ea_from_body()
23833         # notice this is the very first RPC to MDS2
23834         cp /etc/services $DIR/$tdir/mds2 ||
23835                 error "Failed to copy files to mds$n"
23836 }
23837 run_test 820 "update max EA from open intent"
23838
23839 #
23840 # tests that do cleanup/setup should be run at the end
23841 #
23842
23843 test_900() {
23844         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23845         local ls
23846
23847         #define OBD_FAIL_MGC_PAUSE_PROCESS_LOG   0x903
23848         $LCTL set_param fail_loc=0x903
23849
23850         cancel_lru_locks MGC
23851
23852         FAIL_ON_ERROR=true cleanup
23853         FAIL_ON_ERROR=true setup
23854 }
23855 run_test 900 "umount should not race with any mgc requeue thread"
23856
23857 # LUS-6253/LU-11185
23858 test_901() {
23859         local oldc
23860         local newc
23861         local olds
23862         local news
23863         [ $PARALLEL == "yes" ] && skip "skip parallel run"
23864
23865         # some get_param have a bug to handle dot in param name
23866         cancel_lru_locks MGC
23867         oldc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23868         olds=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23869         umount_client $MOUNT || error "umount failed"
23870         mount_client $MOUNT || error "mount failed"
23871         cancel_lru_locks MGC
23872         newc=$($LCTL get_param -n 'ldlm.namespaces.MGC*.lock_count')
23873         news=$(do_facet mgs $LCTL get_param -n 'ldlm.namespaces.MGS*.lock_count')
23874
23875         [ $oldc -lt $newc ] && error "mgc lock leak ($oldc != $newc)"
23876         [ $olds -lt $news ] && error "mgs lock leak ($olds != $news)"
23877
23878         return 0
23879 }
23880 run_test 901 "don't leak a mgc lock on client umount"
23881
23882 # LU-13377
23883 test_902() {
23884         [ $CLIENT_VERSION -lt $(version_code 2.13.52) ] &&
23885                 skip "client does not have LU-13377 fix"
23886         #define OBD_FAIL_LLITE_SHORT_COMMIT 0x1415
23887         $LCTL set_param fail_loc=0x1415
23888         dd if=/dev/zero of=$DIR/$tfile bs=1M count=1
23889         cancel_lru_locks osc
23890         rm -f $DIR/$tfile
23891 }
23892 run_test 902 "test short write doesn't hang lustre"
23893
23894 complete $SECONDS
23895 [ -f $EXT2_DEV ] && rm $EXT2_DEV || true
23896 check_and_cleanup_lustre
23897 if [ "$I_MOUNTED" != "yes" ]; then
23898         lctl set_param debug="$OLDDEBUG" 2> /dev/null || true
23899 fi
23900 exit_status